1 | /***************************************************************************
|
---|
2 | * _ _ ____ _
|
---|
3 | * Project ___| | | | _ \| |
|
---|
4 | * / __| | | | |_) | |
|
---|
5 | * | (__| |_| | _ <| |___
|
---|
6 | * \___|\___/|_| \_\_____|
|
---|
7 | *
|
---|
8 | * Copyright (C) Steve Holme, <[email protected]>.
|
---|
9 | * Copyright (C) Daniel Stenberg, <[email protected]>, et al.
|
---|
10 | *
|
---|
11 | * This software is licensed as described in the file COPYING, which
|
---|
12 | * you should have received as part of this distribution. The terms
|
---|
13 | * are also available at https://curl.se/docs/copyright.html.
|
---|
14 | *
|
---|
15 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
---|
16 | * copies of the Software, and permit persons to whom the Software is
|
---|
17 | * furnished to do so, under the terms of the COPYING file.
|
---|
18 | *
|
---|
19 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
---|
20 | * KIND, either express or implied.
|
---|
21 | *
|
---|
22 | * SPDX-License-Identifier: curl
|
---|
23 | *
|
---|
24 | * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
|
---|
25 | *
|
---|
26 | ***************************************************************************/
|
---|
27 |
|
---|
28 | #include "curl_setup.h"
|
---|
29 |
|
---|
30 | #if defined(HAVE_GSSAPI) && defined(USE_KERBEROS5)
|
---|
31 |
|
---|
32 | #include <curl/curl.h>
|
---|
33 |
|
---|
34 | #include "vauth/vauth.h"
|
---|
35 | #include "curl_sasl.h"
|
---|
36 | #include "urldata.h"
|
---|
37 | #include "curl_gssapi.h"
|
---|
38 | #include "sendf.h"
|
---|
39 | #include "curl_printf.h"
|
---|
40 |
|
---|
41 | /* The last #include files should be: */
|
---|
42 | #include "curl_memory.h"
|
---|
43 | #include "memdebug.h"
|
---|
44 |
|
---|
45 | /*
|
---|
46 | * Curl_auth_is_gssapi_supported()
|
---|
47 | *
|
---|
48 | * This is used to evaluate if GSSAPI (Kerberos V5) is supported.
|
---|
49 | *
|
---|
50 | * Parameters: None
|
---|
51 | *
|
---|
52 | * Returns TRUE if Kerberos V5 is supported by the GSS-API library.
|
---|
53 | */
|
---|
54 | bool Curl_auth_is_gssapi_supported(void)
|
---|
55 | {
|
---|
56 | return TRUE;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /*
|
---|
60 | * Curl_auth_create_gssapi_user_message()
|
---|
61 | *
|
---|
62 | * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
|
---|
63 | * message ready for sending to the recipient.
|
---|
64 | *
|
---|
65 | * Parameters:
|
---|
66 | *
|
---|
67 | * data [in] - The session handle.
|
---|
68 | * userp [in] - The user name.
|
---|
69 | * passwdp [in] - The user's password.
|
---|
70 | * service [in] - The service type such as http, smtp, pop or imap.
|
---|
71 | * host [in[ - The host name.
|
---|
72 | * mutual_auth [in] - Flag specifying whether or not mutual authentication
|
---|
73 | * is enabled.
|
---|
74 | * chlg [in] - Optional challenge message.
|
---|
75 | * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
|
---|
76 | * out [out] - The result storage.
|
---|
77 | *
|
---|
78 | * Returns CURLE_OK on success.
|
---|
79 | */
|
---|
80 | CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
|
---|
81 | const char *userp,
|
---|
82 | const char *passwdp,
|
---|
83 | const char *service,
|
---|
84 | const char *host,
|
---|
85 | const bool mutual_auth,
|
---|
86 | const struct bufref *chlg,
|
---|
87 | struct kerberos5data *krb5,
|
---|
88 | struct bufref *out)
|
---|
89 | {
|
---|
90 | CURLcode result = CURLE_OK;
|
---|
91 | OM_uint32 major_status;
|
---|
92 | OM_uint32 minor_status;
|
---|
93 | OM_uint32 unused_status;
|
---|
94 | gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
|
---|
95 | gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
|
---|
96 | gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
|
---|
97 |
|
---|
98 | (void) userp;
|
---|
99 | (void) passwdp;
|
---|
100 |
|
---|
101 | if(!krb5->spn) {
|
---|
102 | /* Generate our SPN */
|
---|
103 | char *spn = Curl_auth_build_spn(service, NULL, host);
|
---|
104 | if(!spn)
|
---|
105 | return CURLE_OUT_OF_MEMORY;
|
---|
106 |
|
---|
107 | /* Populate the SPN structure */
|
---|
108 | spn_token.value = spn;
|
---|
109 | spn_token.length = strlen(spn);
|
---|
110 |
|
---|
111 | /* Import the SPN */
|
---|
112 | major_status = gss_import_name(&minor_status, &spn_token,
|
---|
113 | GSS_C_NT_HOSTBASED_SERVICE, &krb5->spn);
|
---|
114 | if(GSS_ERROR(major_status)) {
|
---|
115 | Curl_gss_log_error(data, "gss_import_name() failed: ",
|
---|
116 | major_status, minor_status);
|
---|
117 |
|
---|
118 | free(spn);
|
---|
119 |
|
---|
120 | return CURLE_AUTH_ERROR;
|
---|
121 | }
|
---|
122 |
|
---|
123 | free(spn);
|
---|
124 | }
|
---|
125 |
|
---|
126 | if(chlg) {
|
---|
127 | if(!Curl_bufref_len(chlg)) {
|
---|
128 | infof(data, "GSSAPI handshake failure (empty challenge message)");
|
---|
129 | return CURLE_BAD_CONTENT_ENCODING;
|
---|
130 | }
|
---|
131 | input_token.value = (void *) Curl_bufref_ptr(chlg);
|
---|
132 | input_token.length = Curl_bufref_len(chlg);
|
---|
133 | }
|
---|
134 |
|
---|
135 | major_status = Curl_gss_init_sec_context(data,
|
---|
136 | &minor_status,
|
---|
137 | &krb5->context,
|
---|
138 | krb5->spn,
|
---|
139 | &Curl_krb5_mech_oid,
|
---|
140 | GSS_C_NO_CHANNEL_BINDINGS,
|
---|
141 | &input_token,
|
---|
142 | &output_token,
|
---|
143 | mutual_auth,
|
---|
144 | NULL);
|
---|
145 |
|
---|
146 | if(GSS_ERROR(major_status)) {
|
---|
147 | if(output_token.value)
|
---|
148 | gss_release_buffer(&unused_status, &output_token);
|
---|
149 |
|
---|
150 | Curl_gss_log_error(data, "gss_init_sec_context() failed: ",
|
---|
151 | major_status, minor_status);
|
---|
152 |
|
---|
153 | return CURLE_AUTH_ERROR;
|
---|
154 | }
|
---|
155 |
|
---|
156 | if(output_token.value && output_token.length) {
|
---|
157 | result = Curl_bufref_memdup(out, output_token.value, output_token.length);
|
---|
158 | gss_release_buffer(&unused_status, &output_token);
|
---|
159 | }
|
---|
160 | else
|
---|
161 | Curl_bufref_set(out, mutual_auth? "": NULL, 0, NULL);
|
---|
162 |
|
---|
163 | return result;
|
---|
164 | }
|
---|
165 |
|
---|
166 | /*
|
---|
167 | * Curl_auth_create_gssapi_security_message()
|
---|
168 | *
|
---|
169 | * This is used to generate an already encoded GSSAPI (Kerberos V5) security
|
---|
170 | * token message ready for sending to the recipient.
|
---|
171 | *
|
---|
172 | * Parameters:
|
---|
173 | *
|
---|
174 | * data [in] - The session handle.
|
---|
175 | * authzid [in] - The authorization identity if some.
|
---|
176 | * chlg [in] - Optional challenge message.
|
---|
177 | * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
|
---|
178 | * out [out] - The result storage.
|
---|
179 | *
|
---|
180 | * Returns CURLE_OK on success.
|
---|
181 | */
|
---|
182 | CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
|
---|
183 | const char *authzid,
|
---|
184 | const struct bufref *chlg,
|
---|
185 | struct kerberos5data *krb5,
|
---|
186 | struct bufref *out)
|
---|
187 | {
|
---|
188 | CURLcode result = CURLE_OK;
|
---|
189 | size_t messagelen = 0;
|
---|
190 | unsigned char *message = NULL;
|
---|
191 | OM_uint32 major_status;
|
---|
192 | OM_uint32 minor_status;
|
---|
193 | OM_uint32 unused_status;
|
---|
194 | gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
|
---|
195 | gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
|
---|
196 | unsigned char *indata;
|
---|
197 | gss_qop_t qop = GSS_C_QOP_DEFAULT;
|
---|
198 | unsigned int sec_layer = 0;
|
---|
199 | unsigned int max_size = 0;
|
---|
200 |
|
---|
201 | /* Ensure we have a valid challenge message */
|
---|
202 | if(!Curl_bufref_len(chlg)) {
|
---|
203 | infof(data, "GSSAPI handshake failure (empty security message)");
|
---|
204 | return CURLE_BAD_CONTENT_ENCODING;
|
---|
205 | }
|
---|
206 |
|
---|
207 | /* Setup the challenge "input" security buffer */
|
---|
208 | input_token.value = (void *) Curl_bufref_ptr(chlg);
|
---|
209 | input_token.length = Curl_bufref_len(chlg);
|
---|
210 |
|
---|
211 | /* Decrypt the inbound challenge and obtain the qop */
|
---|
212 | major_status = gss_unwrap(&minor_status, krb5->context, &input_token,
|
---|
213 | &output_token, NULL, &qop);
|
---|
214 | if(GSS_ERROR(major_status)) {
|
---|
215 | Curl_gss_log_error(data, "gss_unwrap() failed: ",
|
---|
216 | major_status, minor_status);
|
---|
217 | return CURLE_BAD_CONTENT_ENCODING;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
|
---|
221 | if(output_token.length != 4) {
|
---|
222 | infof(data, "GSSAPI handshake failure (invalid security data)");
|
---|
223 | return CURLE_BAD_CONTENT_ENCODING;
|
---|
224 | }
|
---|
225 |
|
---|
226 | /* Extract the security layer and the maximum message size */
|
---|
227 | indata = output_token.value;
|
---|
228 | sec_layer = indata[0];
|
---|
229 | max_size = ((unsigned int)indata[1] << 16) |
|
---|
230 | ((unsigned int)indata[2] << 8) | indata[3];
|
---|
231 |
|
---|
232 | /* Free the challenge as it is not required anymore */
|
---|
233 | gss_release_buffer(&unused_status, &output_token);
|
---|
234 |
|
---|
235 | /* Process the security layer */
|
---|
236 | if(!(sec_layer & GSSAUTH_P_NONE)) {
|
---|
237 | infof(data, "GSSAPI handshake failure (invalid security layer)");
|
---|
238 |
|
---|
239 | return CURLE_BAD_CONTENT_ENCODING;
|
---|
240 | }
|
---|
241 | sec_layer &= GSSAUTH_P_NONE; /* We do not support a security layer */
|
---|
242 |
|
---|
243 | /* Process the maximum message size the server can receive */
|
---|
244 | if(max_size > 0) {
|
---|
245 | /* The server has told us it supports a maximum receive buffer, however, as
|
---|
246 | we don't require one unless we are encrypting data, we tell the server
|
---|
247 | our receive buffer is zero. */
|
---|
248 | max_size = 0;
|
---|
249 | }
|
---|
250 |
|
---|
251 | /* Allocate our message */
|
---|
252 | messagelen = 4;
|
---|
253 | if(authzid)
|
---|
254 | messagelen += strlen(authzid);
|
---|
255 | message = malloc(messagelen);
|
---|
256 | if(!message)
|
---|
257 | return CURLE_OUT_OF_MEMORY;
|
---|
258 |
|
---|
259 | /* Populate the message with the security layer and client supported receive
|
---|
260 | message size. */
|
---|
261 | message[0] = sec_layer & 0xFF;
|
---|
262 | message[1] = (max_size >> 16) & 0xFF;
|
---|
263 | message[2] = (max_size >> 8) & 0xFF;
|
---|
264 | message[3] = max_size & 0xFF;
|
---|
265 |
|
---|
266 | /* If given, append the authorization identity. */
|
---|
267 |
|
---|
268 | if(authzid && *authzid)
|
---|
269 | memcpy(message + 4, authzid, messagelen - 4);
|
---|
270 |
|
---|
271 | /* Setup the "authentication data" security buffer */
|
---|
272 | input_token.value = message;
|
---|
273 | input_token.length = messagelen;
|
---|
274 |
|
---|
275 | /* Encrypt the data */
|
---|
276 | major_status = gss_wrap(&minor_status, krb5->context, 0,
|
---|
277 | GSS_C_QOP_DEFAULT, &input_token, NULL,
|
---|
278 | &output_token);
|
---|
279 | if(GSS_ERROR(major_status)) {
|
---|
280 | Curl_gss_log_error(data, "gss_wrap() failed: ",
|
---|
281 | major_status, minor_status);
|
---|
282 | free(message);
|
---|
283 | return CURLE_AUTH_ERROR;
|
---|
284 | }
|
---|
285 |
|
---|
286 | /* Return the response. */
|
---|
287 | result = Curl_bufref_memdup(out, output_token.value, output_token.length);
|
---|
288 | /* Free the output buffer */
|
---|
289 | gss_release_buffer(&unused_status, &output_token);
|
---|
290 |
|
---|
291 | /* Free the message buffer */
|
---|
292 | free(message);
|
---|
293 |
|
---|
294 | return result;
|
---|
295 | }
|
---|
296 |
|
---|
297 | /*
|
---|
298 | * Curl_auth_cleanup_gssapi()
|
---|
299 | *
|
---|
300 | * This is used to clean up the GSSAPI (Kerberos V5) specific data.
|
---|
301 | *
|
---|
302 | * Parameters:
|
---|
303 | *
|
---|
304 | * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
|
---|
305 | *
|
---|
306 | */
|
---|
307 | void Curl_auth_cleanup_gssapi(struct kerberos5data *krb5)
|
---|
308 | {
|
---|
309 | OM_uint32 minor_status;
|
---|
310 |
|
---|
311 | /* Free our security context */
|
---|
312 | if(krb5->context != GSS_C_NO_CONTEXT) {
|
---|
313 | gss_delete_sec_context(&minor_status, &krb5->context, GSS_C_NO_BUFFER);
|
---|
314 | krb5->context = GSS_C_NO_CONTEXT;
|
---|
315 | }
|
---|
316 |
|
---|
317 | /* Free the SPN */
|
---|
318 | if(krb5->spn != GSS_C_NO_NAME) {
|
---|
319 | gss_release_name(&minor_status, &krb5->spn);
|
---|
320 | krb5->spn = GSS_C_NO_NAME;
|
---|
321 | }
|
---|
322 | }
|
---|
323 |
|
---|
324 | #endif /* HAVE_GSSAPI && USE_KERBEROS5 */
|
---|