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