1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2014 - 2016, Steve Holme, <steve_holme@hotmail.com>.
9 * Copyright (C) 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
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.haxx.se/docs/copyright.html.
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.
19 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20 * KIND, either express or implied.
22 * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
24 ***************************************************************************/
26 #include "curl_setup.h"
28 #if defined(HAVE_GSSAPI) && defined(USE_KERBEROS5)
30 #include <curl/curl.h>
32 #include "curl_sasl.h"
34 #include "curl_base64.h"
35 #include "curl_gssapi.h"
37 #include "curl_printf.h"
39 /* The last #include files should be: */
40 #include "curl_memory.h"
44 * Curl_sasl_build_gssapi_spn()
46 * This is used to build a SPN string in the format service@instance.
50 * service [in] - The service type such as www, smtp, pop or imap.
51 * instance [in] - The host name or realm.
53 * Returns a pointer to the newly allocated SPN.
55 char *Curl_sasl_build_gssapi_spn(const char *service, const char *instance)
57 /* Generate and return our SPN */
58 return aprintf("%s@%s", service, instance);
62 * Curl_sasl_create_gssapi_user_message()
64 * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
65 * message ready for sending to the recipient.
69 * data [in] - The session handle.
70 * userp [in] - The user name.
71 * passdwp [in] - The user's password.
72 * service [in] - The service type such as www, smtp, pop or imap.
73 * mutual_auth [in] - Flag specifing whether or not mutual authentication
75 * chlg64 [in] - Pointer to the optional base64 encoded challenge
77 * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
78 * outptr [in/out] - The address where a pointer to newly allocated memory
79 * holding the result will be stored upon completion.
80 * outlen [out] - The length of the output message.
82 * Returns CURLE_OK on success.
84 CURLcode Curl_sasl_create_gssapi_user_message(struct SessionHandle *data,
88 const bool mutual_auth,
90 struct kerberos5data *krb5,
91 char **outptr, size_t *outlen)
93 CURLcode result = CURLE_OK;
95 unsigned char *chlg = NULL;
97 OM_uint32 gss_major_status;
98 OM_uint32 gss_minor_status;
99 gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
100 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
101 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
106 if(krb5->context == GSS_C_NO_CONTEXT) {
107 /* Generate our SPN */
108 char *spn = Curl_sasl_build_gssapi_spn(service,
109 data->easy_conn->host.name);
111 return CURLE_OUT_OF_MEMORY;
113 /* Populate the SPN structure */
114 spn_token.value = spn;
115 spn_token.length = strlen(spn);
118 gss_major_status = gss_import_name(&gss_minor_status, &spn_token,
119 GSS_C_NT_HOSTBASED_SERVICE, &krb5->spn);
120 if(GSS_ERROR(gss_major_status)) {
121 Curl_gss_log_error(data, gss_minor_status, "gss_import_name() failed: ");
125 return CURLE_OUT_OF_MEMORY;
131 /* Decode the base-64 encoded challenge message */
132 if(strlen(chlg64) && *chlg64 != '=') {
133 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
138 /* Ensure we have a valid challenge message */
140 infof(data, "GSSAPI handshake failure (empty challenge message)\n");
142 return CURLE_BAD_CONTENT_ENCODING;
145 /* Setup the challenge "input" security buffer */
146 input_token.value = chlg;
147 input_token.length = chlglen;
150 gss_major_status = Curl_gss_init_sec_context(data,
155 GSS_C_NO_CHANNEL_BINDINGS,
161 free(input_token.value);
163 if(GSS_ERROR(gss_major_status)) {
164 if(output_token.value)
165 gss_release_buffer(&gss_status, &output_token);
167 Curl_gss_log_error(data, gss_minor_status,
168 "gss_init_sec_context() failed: ");
170 return CURLE_RECV_ERROR;
173 if(output_token.value && output_token.length) {
174 /* Base64 encode the response */
175 result = Curl_base64_encode(data, (char *) output_token.value,
176 output_token.length, outptr, outlen);
178 gss_release_buffer(&gss_status, &output_token);
185 * Curl_sasl_create_gssapi_security_message()
187 * This is used to generate an already encoded GSSAPI (Kerberos V5) security
188 * token message ready for sending to the recipient.
192 * data [in] - The session handle.
193 * chlg64 [in] - Pointer to the optional base64 encoded challenge message.
194 * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
195 * outptr [in/out] - The address where a pointer to newly allocated memory
196 * holding the result will be stored upon completion.
197 * outlen [out] - The length of the output message.
199 * Returns CURLE_OK on success.
201 CURLcode Curl_sasl_create_gssapi_security_message(struct SessionHandle *data,
203 struct kerberos5data *krb5,
207 CURLcode result = CURLE_OK;
209 size_t messagelen = 0;
210 unsigned char *chlg = NULL;
211 unsigned char *message = NULL;
212 OM_uint32 gss_status;
213 OM_uint32 gss_major_status;
214 OM_uint32 gss_minor_status;
215 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
216 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
217 unsigned int indata = 0;
218 unsigned int outdata = 0;
219 gss_qop_t qop = GSS_C_QOP_DEFAULT;
220 unsigned int sec_layer = 0;
221 unsigned int max_size = 0;
222 gss_name_t username = GSS_C_NO_NAME;
223 gss_buffer_desc username_token;
225 /* Decode the base-64 encoded input message */
226 if(strlen(chlg64) && *chlg64 != '=') {
227 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
232 /* Ensure we have a valid challenge message */
234 infof(data, "GSSAPI handshake failure (empty security message)\n");
236 return CURLE_BAD_CONTENT_ENCODING;
239 /* Get the fully qualified username back from the context */
240 gss_major_status = gss_inquire_context(&gss_minor_status, krb5->context,
241 &username, NULL, NULL, NULL, NULL,
243 if(GSS_ERROR(gss_major_status)) {
244 Curl_gss_log_error(data, gss_minor_status,
245 "gss_inquire_context() failed: ");
249 return CURLE_OUT_OF_MEMORY;
252 /* Convert the username from internal format to a displayable token */
253 gss_major_status = gss_display_name(&gss_minor_status, username,
254 &username_token, NULL);
255 if(GSS_ERROR(gss_major_status)) {
256 Curl_gss_log_error(data, gss_minor_status, "gss_display_name() failed: ");
260 return CURLE_OUT_OF_MEMORY;
263 /* Setup the challenge "input" security buffer */
264 input_token.value = chlg;
265 input_token.length = chlglen;
267 /* Decrypt the inbound challenge and obtain the qop */
268 gss_major_status = gss_unwrap(&gss_minor_status, krb5->context, &input_token,
269 &output_token, NULL, &qop);
270 if(GSS_ERROR(gss_major_status)) {
271 Curl_gss_log_error(data, gss_minor_status, "gss_unwrap() failed: ");
273 gss_release_buffer(&gss_status, &username_token);
276 return CURLE_BAD_CONTENT_ENCODING;
279 /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
280 if(output_token.length != 4) {
281 infof(data, "GSSAPI handshake failure (invalid security data)\n");
283 gss_release_buffer(&gss_status, &username_token);
286 return CURLE_BAD_CONTENT_ENCODING;
289 /* Copy the data out and free the challenge as it is not required anymore */
290 memcpy(&indata, output_token.value, 4);
291 gss_release_buffer(&gss_status, &output_token);
294 /* Extract the security layer */
295 sec_layer = indata & 0x000000FF;
296 if(!(sec_layer & GSSAUTH_P_NONE)) {
297 infof(data, "GSSAPI handshake failure (invalid security layer)\n");
299 gss_release_buffer(&gss_status, &username_token);
301 return CURLE_BAD_CONTENT_ENCODING;
304 /* Extract the maximum message size the server can receive */
305 max_size = ntohl(indata & 0xFFFFFF00);
307 /* The server has told us it supports a maximum receive buffer, however, as
308 we don't require one unless we are encrypting data, we tell the server
309 our receive buffer is zero. */
313 /* Allocate our message */
314 messagelen = sizeof(outdata) + username_token.length + 1;
315 message = malloc(messagelen);
317 gss_release_buffer(&gss_status, &username_token);
319 return CURLE_OUT_OF_MEMORY;
322 /* Populate the message with the security layer, client supported receive
323 message size and authorization identity including the 0x00 based
324 terminator. Note: Dispite RFC4752 Section 3.1 stating "The authorization
325 identity is not terminated with the zero-valued (%x00) octet." it seems
326 necessary to include it. */
327 outdata = htonl(max_size) | sec_layer;
328 memcpy(message, &outdata, sizeof(outdata));
329 memcpy(message + sizeof(outdata), username_token.value,
330 username_token.length);
331 message[messagelen - 1] = '\0';
333 /* Free the username token as it is not required anymore */
334 gss_release_buffer(&gss_status, &username_token);
336 /* Setup the "authentication data" security buffer */
337 input_token.value = message;
338 input_token.length = messagelen;
340 /* Encrypt the data */
341 gss_major_status = gss_wrap(&gss_minor_status, krb5->context, 0,
342 GSS_C_QOP_DEFAULT, &input_token, NULL,
344 if(GSS_ERROR(gss_major_status)) {
345 Curl_gss_log_error(data, gss_minor_status, "gss_wrap() failed: ");
349 return CURLE_OUT_OF_MEMORY;
352 /* Base64 encode the response */
353 result = Curl_base64_encode(data, (char *) output_token.value,
354 output_token.length, outptr, outlen);
356 /* Free the output buffer */
357 gss_release_buffer(&gss_status, &output_token);
359 /* Free the message buffer */
366 * Curl_sasl_gssapi_cleanup()
368 * This is used to clean up the GSSAPI (Kerberos V5) specific data.
372 * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
375 void Curl_sasl_gssapi_cleanup(struct kerberos5data *krb5)
377 OM_uint32 minor_status;
379 /* Free our security context */
380 if(krb5->context != GSS_C_NO_CONTEXT) {
381 gss_delete_sec_context(&minor_status, &krb5->context, GSS_C_NO_BUFFER);
382 krb5->context = GSS_C_NO_CONTEXT;
386 if(krb5->spn != GSS_C_NO_NAME) {
387 gss_release_name(&minor_status, &krb5->spn);
388 krb5->spn = GSS_C_NO_NAME;
392 #endif /* HAVE_GSSAPI && USE_KERBEROS5 */