1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2014 - 2017, 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 "vauth/vauth.h"
33 #include "curl_sasl.h"
35 #include "curl_base64.h"
36 #include "curl_gssapi.h"
38 #include "curl_printf.h"
40 /* The last #include files should be: */
41 #include "curl_memory.h"
45 * Curl_auth_is_gssapi_supported()
47 * This is used to evaluate if GSSAPI (Kerberos V5) is supported.
51 * Returns TRUE if Kerberos V5 is supported by the GSS-API library.
53 bool Curl_auth_is_gssapi_supported(void)
59 * Curl_auth_create_gssapi_user_message()
61 * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
62 * message ready for sending to the recipient.
66 * data [in] - The session handle.
67 * userp [in] - The user name.
68 * passdwp [in] - The user's password.
69 * service [in] - The service type such as http, smtp, pop or imap.
70 * host [in[ - The host name.
71 * mutual_auth [in] - Flag specifying whether or not mutual authentication
73 * chlg64 [in] - Pointer to the optional base64 encoded challenge
75 * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
76 * outptr [in/out] - The address where a pointer to newly allocated memory
77 * holding the result will be stored upon completion.
78 * outlen [out] - The length of the output message.
80 * Returns CURLE_OK on success.
82 CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
87 const bool mutual_auth,
89 struct kerberos5data *krb5,
90 char **outptr, size_t *outlen)
92 CURLcode result = CURLE_OK;
94 unsigned char *chlg = NULL;
95 OM_uint32 major_status;
96 OM_uint32 minor_status;
97 OM_uint32 unused_status;
98 gss_buffer_desc spn_token = GSS_C_EMPTY_BUFFER;
99 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
100 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
106 /* Generate our SPN */
107 char *spn = Curl_auth_build_spn(service, NULL, host);
109 return CURLE_OUT_OF_MEMORY;
111 /* Populate the SPN structure */
112 spn_token.value = spn;
113 spn_token.length = strlen(spn);
116 major_status = gss_import_name(&minor_status, &spn_token,
117 GSS_C_NT_HOSTBASED_SERVICE, &krb5->spn);
118 if(GSS_ERROR(major_status)) {
119 Curl_gss_log_error(data, "gss_import_name() failed: ",
120 major_status, minor_status);
124 return CURLE_OUT_OF_MEMORY;
130 if(chlg64 && *chlg64) {
131 /* Decode the base-64 encoded challenge message */
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 major_status = Curl_gss_init_sec_context(data,
155 GSS_C_NO_CHANNEL_BINDINGS,
161 /* Free the decoded challenge as it is not required anymore */
162 free(input_token.value);
164 if(GSS_ERROR(major_status)) {
165 if(output_token.value)
166 gss_release_buffer(&unused_status, &output_token);
168 Curl_gss_log_error(data, "gss_init_sec_context() failed: ",
169 major_status, minor_status);
171 return CURLE_RECV_ERROR;
174 if(output_token.value && output_token.length) {
175 /* Base64 encode the response */
176 result = Curl_base64_encode(data, (char *) output_token.value,
177 output_token.length, outptr, outlen);
179 gss_release_buffer(&unused_status, &output_token);
181 else if(mutual_auth) {
182 *outptr = strdup("");
184 result = CURLE_OUT_OF_MEMORY;
191 * Curl_auth_create_gssapi_security_message()
193 * This is used to generate an already encoded GSSAPI (Kerberos V5) security
194 * token message ready for sending to the recipient.
198 * data [in] - The session handle.
199 * chlg64 [in] - Pointer to the optional base64 encoded challenge message.
200 * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
201 * outptr [in/out] - The address where a pointer to newly allocated memory
202 * holding the result will be stored upon completion.
203 * outlen [out] - The length of the output message.
205 * Returns CURLE_OK on success.
207 CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
209 struct kerberos5data *krb5,
213 CURLcode result = CURLE_OK;
215 size_t messagelen = 0;
216 unsigned char *chlg = NULL;
217 unsigned char *message = NULL;
218 OM_uint32 major_status;
219 OM_uint32 minor_status;
220 OM_uint32 unused_status;
221 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
222 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
223 unsigned int indata = 0;
224 unsigned int outdata = 0;
225 gss_qop_t qop = GSS_C_QOP_DEFAULT;
226 unsigned int sec_layer = 0;
227 unsigned int max_size = 0;
228 gss_name_t username = GSS_C_NO_NAME;
229 gss_buffer_desc username_token;
231 /* Decode the base-64 encoded input message */
232 if(strlen(chlg64) && *chlg64 != '=') {
233 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
238 /* Ensure we have a valid challenge message */
240 infof(data, "GSSAPI handshake failure (empty security message)\n");
242 return CURLE_BAD_CONTENT_ENCODING;
245 /* Get the fully qualified username back from the context */
246 major_status = gss_inquire_context(&minor_status, krb5->context,
247 &username, NULL, NULL, NULL, NULL,
249 if(GSS_ERROR(major_status)) {
250 Curl_gss_log_error(data, "gss_inquire_context() failed: ",
251 major_status, minor_status);
255 return CURLE_OUT_OF_MEMORY;
258 /* Convert the username from internal format to a displayable token */
259 major_status = gss_display_name(&minor_status, username,
260 &username_token, NULL);
261 if(GSS_ERROR(major_status)) {
262 Curl_gss_log_error(data, "gss_display_name() failed: ",
263 major_status, minor_status);
267 return CURLE_OUT_OF_MEMORY;
270 /* Setup the challenge "input" security buffer */
271 input_token.value = chlg;
272 input_token.length = chlglen;
274 /* Decrypt the inbound challenge and obtain the qop */
275 major_status = gss_unwrap(&minor_status, krb5->context, &input_token,
276 &output_token, NULL, &qop);
277 if(GSS_ERROR(major_status)) {
278 Curl_gss_log_error(data, "gss_unwrap() failed: ",
279 major_status, minor_status);
281 gss_release_buffer(&unused_status, &username_token);
284 return CURLE_BAD_CONTENT_ENCODING;
287 /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
288 if(output_token.length != 4) {
289 infof(data, "GSSAPI handshake failure (invalid security data)\n");
291 gss_release_buffer(&unused_status, &username_token);
294 return CURLE_BAD_CONTENT_ENCODING;
297 /* Copy the data out and free the challenge as it is not required anymore */
298 memcpy(&indata, output_token.value, 4);
299 gss_release_buffer(&unused_status, &output_token);
302 /* Extract the security layer */
303 sec_layer = indata & 0x000000FF;
304 if(!(sec_layer & GSSAUTH_P_NONE)) {
305 infof(data, "GSSAPI handshake failure (invalid security layer)\n");
307 gss_release_buffer(&unused_status, &username_token);
309 return CURLE_BAD_CONTENT_ENCODING;
312 /* Extract the maximum message size the server can receive */
313 max_size = ntohl(indata & 0xFFFFFF00);
315 /* The server has told us it supports a maximum receive buffer, however, as
316 we don't require one unless we are encrypting data, we tell the server
317 our receive buffer is zero. */
321 /* Allocate our message */
322 messagelen = sizeof(outdata) + username_token.length + 1;
323 message = malloc(messagelen);
325 gss_release_buffer(&unused_status, &username_token);
327 return CURLE_OUT_OF_MEMORY;
330 /* Populate the message with the security layer, client supported receive
331 message size and authorization identity including the 0x00 based
332 terminator. Note: Despite RFC4752 Section 3.1 stating "The authorization
333 identity is not terminated with the zero-valued (%x00) octet." it seems
334 necessary to include it. */
335 outdata = htonl(max_size) | sec_layer;
336 memcpy(message, &outdata, sizeof(outdata));
337 memcpy(message + sizeof(outdata), username_token.value,
338 username_token.length);
339 message[messagelen - 1] = '\0';
341 /* Free the username token as it is not required anymore */
342 gss_release_buffer(&unused_status, &username_token);
344 /* Setup the "authentication data" security buffer */
345 input_token.value = message;
346 input_token.length = messagelen;
348 /* Encrypt the data */
349 major_status = gss_wrap(&minor_status, krb5->context, 0,
350 GSS_C_QOP_DEFAULT, &input_token, NULL,
352 if(GSS_ERROR(major_status)) {
353 Curl_gss_log_error(data, "gss_wrap() failed: ",
354 major_status, minor_status);
358 return CURLE_OUT_OF_MEMORY;
361 /* Base64 encode the response */
362 result = Curl_base64_encode(data, (char *) output_token.value,
363 output_token.length, outptr, outlen);
365 /* Free the output buffer */
366 gss_release_buffer(&unused_status, &output_token);
368 /* Free the message buffer */
375 * Curl_auth_gssapi_cleanup()
377 * This is used to clean up the GSSAPI (Kerberos V5) specific data.
381 * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
384 void Curl_auth_gssapi_cleanup(struct kerberos5data *krb5)
386 OM_uint32 minor_status;
388 /* Free our security context */
389 if(krb5->context != GSS_C_NO_CONTEXT) {
390 gss_delete_sec_context(&minor_status, &krb5->context, GSS_C_NO_BUFFER);
391 krb5->context = GSS_C_NO_CONTEXT;
395 if(krb5->spn != GSS_C_NO_NAME) {
396 gss_release_name(&minor_status, &krb5->spn);
397 krb5->spn = GSS_C_NO_NAME;
401 #endif /* HAVE_GSSAPI && USE_KERBEROS5 */