1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2014 - 2017, Steve Holme, <steve_holme@hotmail.com>.
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.haxx.se/docs/copyright.html.
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
21 * RFC4752 The Kerberos V5 ("GSSAPI") SASL Mechanism
23 ***************************************************************************/
25 #include "curl_setup.h"
27 #if defined(USE_WINDOWS_SSPI) && defined(USE_KERBEROS5)
29 #include <curl/curl.h>
31 #include "vauth/vauth.h"
33 #include "curl_base64.h"
35 #include "curl_multibyte.h"
38 /* The last #include files should be: */
39 #include "curl_memory.h"
43 * Curl_auth_is_gssapi_supported()
45 * This is used to evaluate if GSSAPI (Kerberos V5) is supported.
49 * Returns TRUE if Kerberos V5 is supported by Windows SSPI.
51 bool Curl_auth_is_gssapi_supported(void)
53 PSecPkgInfo SecurityPackage;
54 SECURITY_STATUS status;
56 /* Query the security package for Kerberos */
57 status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
58 TEXT(SP_NAME_KERBEROS),
61 return (status == SEC_E_OK ? TRUE : FALSE);
65 * Curl_auth_create_gssapi_user_message()
67 * This is used to generate an already encoded GSSAPI (Kerberos V5) user token
68 * message ready for sending to the recipient.
72 * data [in] - The session handle.
73 * userp [in] - The user name in the format User or Domain\User.
74 * passdwp [in] - The user's password.
75 * service [in] - The service type such as http, smtp, pop or imap.
76 * host [in] - The host name.
77 * mutual_auth [in] - Flag specifying whether or not mutual authentication
79 * chlg64 [in] - The optional base64 encoded challenge message.
80 * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
81 * outptr [in/out] - The address where a pointer to newly allocated memory
82 * holding the result will be stored upon completion.
83 * outlen [out] - The length of the output message.
85 * Returns CURLE_OK on success.
87 CURLcode Curl_auth_create_gssapi_user_message(struct Curl_easy *data,
92 const bool mutual_auth,
94 struct kerberos5data *krb5,
95 char **outptr, size_t *outlen)
97 CURLcode result = CURLE_OK;
99 unsigned char *chlg = NULL;
101 PSecPkgInfo SecurityPackage;
104 SecBufferDesc chlg_desc;
105 SecBufferDesc resp_desc;
106 SECURITY_STATUS status;
108 TimeStamp expiry; /* For Windows 9x compatibility of SSPI calls */
111 /* Generate our SPN */
112 krb5->spn = Curl_auth_build_spn(service, host, NULL);
114 return CURLE_OUT_OF_MEMORY;
117 if(!krb5->output_token) {
118 /* Query the security package for Kerberos */
119 status = s_pSecFn->QuerySecurityPackageInfo((TCHAR *)
120 TEXT(SP_NAME_KERBEROS),
122 if(status != SEC_E_OK) {
123 return CURLE_NOT_BUILT_IN;
126 krb5->token_max = SecurityPackage->cbMaxToken;
128 /* Release the package buffer as it is not required anymore */
129 s_pSecFn->FreeContextBuffer(SecurityPackage);
131 /* Allocate our response buffer */
132 krb5->output_token = malloc(krb5->token_max);
133 if(!krb5->output_token)
134 return CURLE_OUT_OF_MEMORY;
137 if(!krb5->credentials) {
138 /* Do we have credientials to use or are we using single sign-on? */
139 if(userp && *userp) {
140 /* Populate our identity structure */
141 result = Curl_create_sspi_identity(userp, passwdp, &krb5->identity);
145 /* Allow proper cleanup of the identity structure */
146 krb5->p_identity = &krb5->identity;
149 /* Use the current Windows user */
150 krb5->p_identity = NULL;
152 /* Allocate our credentials handle */
153 krb5->credentials = malloc(sizeof(CredHandle));
154 if(!krb5->credentials)
155 return CURLE_OUT_OF_MEMORY;
157 memset(krb5->credentials, 0, sizeof(CredHandle));
159 /* Acquire our credentials handle */
160 status = s_pSecFn->AcquireCredentialsHandle(NULL,
162 TEXT(SP_NAME_KERBEROS),
163 SECPKG_CRED_OUTBOUND, NULL,
164 krb5->p_identity, NULL, NULL,
165 krb5->credentials, &expiry);
166 if(status != SEC_E_OK)
167 return CURLE_LOGIN_DENIED;
169 /* Allocate our new context handle */
170 krb5->context = malloc(sizeof(CtxtHandle));
172 return CURLE_OUT_OF_MEMORY;
174 memset(krb5->context, 0, sizeof(CtxtHandle));
177 if(chlg64 && *chlg64) {
178 /* Decode the base-64 encoded challenge message */
180 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
185 /* Ensure we have a valid challenge message */
187 infof(data, "GSSAPI handshake failure (empty challenge message)\n");
189 return CURLE_BAD_CONTENT_ENCODING;
192 /* Setup the challenge "input" security buffer */
193 chlg_desc.ulVersion = SECBUFFER_VERSION;
194 chlg_desc.cBuffers = 1;
195 chlg_desc.pBuffers = &chlg_buf;
196 chlg_buf.BufferType = SECBUFFER_TOKEN;
197 chlg_buf.pvBuffer = chlg;
198 chlg_buf.cbBuffer = curlx_uztoul(chlglen);
201 /* Setup the response "output" security buffer */
202 resp_desc.ulVersion = SECBUFFER_VERSION;
203 resp_desc.cBuffers = 1;
204 resp_desc.pBuffers = &resp_buf;
205 resp_buf.BufferType = SECBUFFER_TOKEN;
206 resp_buf.pvBuffer = krb5->output_token;
207 resp_buf.cbBuffer = curlx_uztoul(krb5->token_max);
209 /* Generate our challenge-response message */
210 status = s_pSecFn->InitializeSecurityContext(krb5->credentials,
211 chlg ? krb5->context : NULL,
214 ISC_REQ_MUTUAL_AUTH : 0),
215 0, SECURITY_NATIVE_DREP,
216 chlg ? &chlg_desc : NULL, 0,
221 /* Free the decoded challenge as it is not required anymore */
224 if(status != SEC_E_OK && status != SEC_I_CONTINUE_NEEDED) {
225 return CURLE_RECV_ERROR;
228 if(memcmp(&context, krb5->context, sizeof(context))) {
229 s_pSecFn->DeleteSecurityContext(krb5->context);
231 memcpy(krb5->context, &context, sizeof(context));
234 if(resp_buf.cbBuffer) {
235 /* Base64 encode the response */
236 result = Curl_base64_encode(data, (char *) resp_buf.pvBuffer,
237 resp_buf.cbBuffer, outptr, outlen);
239 else if(mutual_auth) {
240 *outptr = strdup("");
242 result = CURLE_OUT_OF_MEMORY;
249 * Curl_auth_create_gssapi_security_message()
251 * This is used to generate an already encoded GSSAPI (Kerberos V5) security
252 * token message ready for sending to the recipient.
256 * data [in] - The session handle.
257 * chlg64 [in] - The optional base64 encoded challenge message.
258 * krb5 [in/out] - The Kerberos 5 data struct being used and modified.
259 * outptr [in/out] - The address where a pointer to newly allocated memory
260 * holding the result will be stored upon completion.
261 * outlen [out] - The length of the output message.
263 * Returns CURLE_OK on success.
265 CURLcode Curl_auth_create_gssapi_security_message(struct Curl_easy *data,
267 struct kerberos5data *krb5,
271 CURLcode result = CURLE_OK;
274 size_t messagelen = 0;
275 size_t appdatalen = 0;
276 unsigned char *chlg = NULL;
277 unsigned char *trailer = NULL;
278 unsigned char *message = NULL;
279 unsigned char *padding = NULL;
280 unsigned char *appdata = NULL;
281 SecBuffer input_buf[2];
282 SecBuffer wrap_buf[3];
283 SecBufferDesc input_desc;
284 SecBufferDesc wrap_desc;
285 unsigned long indata = 0;
286 unsigned long outdata = 0;
287 unsigned long qop = 0;
288 unsigned long sec_layer = 0;
289 unsigned long max_size = 0;
290 SecPkgContext_Sizes sizes;
291 SecPkgCredentials_Names names;
292 SECURITY_STATUS status;
295 /* Decode the base-64 encoded input message */
296 if(strlen(chlg64) && *chlg64 != '=') {
297 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
302 /* Ensure we have a valid challenge message */
304 infof(data, "GSSAPI handshake failure (empty security message)\n");
306 return CURLE_BAD_CONTENT_ENCODING;
309 /* Get our response size information */
310 status = s_pSecFn->QueryContextAttributes(krb5->context,
313 if(status != SEC_E_OK) {
316 return CURLE_OUT_OF_MEMORY;
319 /* Get the fully qualified username back from the context */
320 status = s_pSecFn->QueryCredentialsAttributes(krb5->credentials,
321 SECPKG_CRED_ATTR_NAMES,
323 if(status != SEC_E_OK) {
326 return CURLE_RECV_ERROR;
329 /* Setup the "input" security buffer */
330 input_desc.ulVersion = SECBUFFER_VERSION;
331 input_desc.cBuffers = 2;
332 input_desc.pBuffers = input_buf;
333 input_buf[0].BufferType = SECBUFFER_STREAM;
334 input_buf[0].pvBuffer = chlg;
335 input_buf[0].cbBuffer = curlx_uztoul(chlglen);
336 input_buf[1].BufferType = SECBUFFER_DATA;
337 input_buf[1].pvBuffer = NULL;
338 input_buf[1].cbBuffer = 0;
340 /* Decrypt the inbound challenge and obtain the qop */
341 status = s_pSecFn->DecryptMessage(krb5->context, &input_desc, 0, &qop);
342 if(status != SEC_E_OK) {
343 infof(data, "GSSAPI handshake failure (empty security message)\n");
347 return CURLE_BAD_CONTENT_ENCODING;
350 /* Not 4 octets long so fail as per RFC4752 Section 3.1 */
351 if(input_buf[1].cbBuffer != 4) {
352 infof(data, "GSSAPI handshake failure (invalid security data)\n");
356 return CURLE_BAD_CONTENT_ENCODING;
359 /* Copy the data out and free the challenge as it is not required anymore */
360 memcpy(&indata, input_buf[1].pvBuffer, 4);
361 s_pSecFn->FreeContextBuffer(input_buf[1].pvBuffer);
364 /* Extract the security layer */
365 sec_layer = indata & 0x000000FF;
366 if(!(sec_layer & KERB_WRAP_NO_ENCRYPT)) {
367 infof(data, "GSSAPI handshake failure (invalid security layer)\n");
369 return CURLE_BAD_CONTENT_ENCODING;
372 /* Extract the maximum message size the server can receive */
373 max_size = ntohl(indata & 0xFFFFFF00);
375 /* The server has told us it supports a maximum receive buffer, however, as
376 we don't require one unless we are encrypting data, we tell the server
377 our receive buffer is zero. */
381 /* Allocate the trailer */
382 trailer = malloc(sizes.cbSecurityTrailer);
384 return CURLE_OUT_OF_MEMORY;
386 /* Convert the user name to UTF8 when operating with Unicode */
387 user_name = Curl_convert_tchar_to_UTF8(names.sUserName);
391 return CURLE_OUT_OF_MEMORY;
394 /* Allocate our message */
395 messagelen = sizeof(outdata) + strlen(user_name) + 1;
396 message = malloc(messagelen);
399 Curl_unicodefree(user_name);
401 return CURLE_OUT_OF_MEMORY;
404 /* Populate the message with the security layer, client supported receive
405 message size and authorization identity including the 0x00 based
406 terminator. Note: Despite RFC4752 Section 3.1 stating "The authorization
407 identity is not terminated with the zero-valued (%x00) octet." it seems
408 necessary to include it. */
409 outdata = htonl(max_size) | sec_layer;
410 memcpy(message, &outdata, sizeof(outdata));
411 strcpy((char *) message + sizeof(outdata), user_name);
412 Curl_unicodefree(user_name);
414 /* Allocate the padding */
415 padding = malloc(sizes.cbBlockSize);
420 return CURLE_OUT_OF_MEMORY;
423 /* Setup the "authentication data" security buffer */
424 wrap_desc.ulVersion = SECBUFFER_VERSION;
425 wrap_desc.cBuffers = 3;
426 wrap_desc.pBuffers = wrap_buf;
427 wrap_buf[0].BufferType = SECBUFFER_TOKEN;
428 wrap_buf[0].pvBuffer = trailer;
429 wrap_buf[0].cbBuffer = sizes.cbSecurityTrailer;
430 wrap_buf[1].BufferType = SECBUFFER_DATA;
431 wrap_buf[1].pvBuffer = message;
432 wrap_buf[1].cbBuffer = curlx_uztoul(messagelen);
433 wrap_buf[2].BufferType = SECBUFFER_PADDING;
434 wrap_buf[2].pvBuffer = padding;
435 wrap_buf[2].cbBuffer = sizes.cbBlockSize;
437 /* Encrypt the data */
438 status = s_pSecFn->EncryptMessage(krb5->context, KERB_WRAP_NO_ENCRYPT,
440 if(status != SEC_E_OK) {
445 return CURLE_OUT_OF_MEMORY;
448 /* Allocate the encryption (wrap) buffer */
449 appdatalen = wrap_buf[0].cbBuffer + wrap_buf[1].cbBuffer +
450 wrap_buf[2].cbBuffer;
451 appdata = malloc(appdatalen);
457 return CURLE_OUT_OF_MEMORY;
460 /* Populate the encryption buffer */
461 memcpy(appdata, wrap_buf[0].pvBuffer, wrap_buf[0].cbBuffer);
462 offset += wrap_buf[0].cbBuffer;
463 memcpy(appdata + offset, wrap_buf[1].pvBuffer, wrap_buf[1].cbBuffer);
464 offset += wrap_buf[1].cbBuffer;
465 memcpy(appdata + offset, wrap_buf[2].pvBuffer, wrap_buf[2].cbBuffer);
467 /* Base64 encode the response */
468 result = Curl_base64_encode(data, (char *) appdata, appdatalen, outptr,
471 /* Free all of our local buffers */
481 * Curl_auth_gssapi_cleanup()
483 * This is used to clean up the GSSAPI (Kerberos V5) specific data.
487 * krb5 [in/out] - The Kerberos 5 data struct being cleaned up.
490 void Curl_auth_gssapi_cleanup(struct kerberos5data *krb5)
492 /* Free our security context */
494 s_pSecFn->DeleteSecurityContext(krb5->context);
496 krb5->context = NULL;
499 /* Free our credentials handle */
500 if(krb5->credentials) {
501 s_pSecFn->FreeCredentialsHandle(krb5->credentials);
502 free(krb5->credentials);
503 krb5->credentials = NULL;
506 /* Free our identity */
507 Curl_sspi_free_identity(krb5->p_identity);
508 krb5->p_identity = NULL;
510 /* Free the SPN and output token */
511 Curl_safefree(krb5->spn);
512 Curl_safefree(krb5->output_token);
514 /* Reset any variables */
518 #endif /* USE_WINDOWS_SSPI && USE_KERBEROS5*/