1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2012 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
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 http://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 * RFC2195 CRAM-MD5 authentication
22 * RFC2831 DIGEST-MD5 authentication
23 * RFC4422 Simple Authentication and Security Layer (SASL)
24 * RFC4616 PLAIN authentication
25 * RFC6749 OAuth 2.0 Authorization Framework
26 * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
28 ***************************************************************************/
30 #include "curl_setup.h"
32 #include <curl/curl.h>
35 #include "curl_base64.h"
37 #include "vtls/vtls.h"
38 #include "curl_hmac.h"
39 #include "curl_ntlm_msgs.h"
40 #include "curl_sasl.h"
42 #include "curl_memory.h"
47 #include "vtls/nssg.h" /* for Curl_nss_force_init() */
50 #define _MPRINTF_REPLACE /* use our functions only */
51 #include <curl/mprintf.h>
53 /* The last #include file should be: */
56 #if !defined(CURL_DISABLE_CRYPTO_AUTH) && !defined(USE_WINDOWS_SSPI)
57 #define DIGEST_QOP_VALUE_AUTH (1 << 0)
58 #define DIGEST_QOP_VALUE_AUTH_INT (1 << 1)
59 #define DIGEST_QOP_VALUE_AUTH_CONF (1 << 2)
61 #define DIGEST_QOP_VALUE_STRING_AUTH "auth"
62 #define DIGEST_QOP_VALUE_STRING_AUTH_INT "auth-int"
63 #define DIGEST_QOP_VALUE_STRING_AUTH_CONF "auth-conf"
65 /* Retrieves the value for a corresponding key from the challenge string
66 * returns TRUE if the key could be found, FALSE if it does not exists
68 static bool sasl_digest_get_key_value(const char *chlg,
77 find_pos = strstr(chlg, key);
81 find_pos += strlen(key);
83 for(i = 0; *find_pos && *find_pos != end_char && i < max_val_len - 1; ++i)
84 value[i] = *find_pos++;
90 static CURLcode sasl_digest_get_qop_values(const char *options, int *value)
96 /* Initialise the output */
99 /* Tokenise the list of qop values. Use a temporary clone of the buffer since
100 strtok_r() ruins it. */
101 tmp = strdup(options);
103 return CURLE_OUT_OF_MEMORY;
105 token = strtok_r(tmp, ",", &tok_buf);
106 while(token != NULL) {
107 if(Curl_raw_equal(token, DIGEST_QOP_VALUE_STRING_AUTH))
108 *value |= DIGEST_QOP_VALUE_AUTH;
109 else if(Curl_raw_equal(token, DIGEST_QOP_VALUE_STRING_AUTH_INT))
110 *value |= DIGEST_QOP_VALUE_AUTH_INT;
111 else if(Curl_raw_equal(token, DIGEST_QOP_VALUE_STRING_AUTH_CONF))
112 *value |= DIGEST_QOP_VALUE_AUTH_CONF;
114 token = strtok_r(NULL, ",", &tok_buf);
124 * Curl_sasl_create_plain_message()
126 * This is used to generate an already encoded PLAIN message ready
127 * for sending to the recipient.
131 * data [in] - The session handle.
132 * userp [in] - The user name.
133 * passdwp [in] - The user's password.
134 * outptr [in/out] - The address where a pointer to newly allocated memory
135 * holding the result will be stored upon completion.
136 * outlen [out] - The length of the output message.
138 * Returns CURLE_OK on success.
140 CURLcode Curl_sasl_create_plain_message(struct SessionHandle *data,
143 char **outptr, size_t *outlen)
150 ulen = strlen(userp);
151 plen = strlen(passwdp);
153 plainauth = malloc(2 * ulen + plen + 2);
157 return CURLE_OUT_OF_MEMORY;
160 /* Calculate the reply */
161 memcpy(plainauth, userp, ulen);
162 plainauth[ulen] = '\0';
163 memcpy(plainauth + ulen + 1, userp, ulen);
164 plainauth[2 * ulen + 1] = '\0';
165 memcpy(plainauth + 2 * ulen + 2, passwdp, plen);
167 /* Base64 encode the reply */
168 result = Curl_base64_encode(data, plainauth, 2 * ulen + plen + 2, outptr,
170 Curl_safefree(plainauth);
175 * Curl_sasl_create_login_message()
177 * This is used to generate an already encoded LOGIN message containing the
178 * user name or password ready for sending to the recipient.
182 * data [in] - The session handle.
183 * valuep [in] - The user name or user's password.
184 * outptr [in/out] - The address where a pointer to newly allocated memory
185 * holding the result will be stored upon completion.
186 * outlen [out] - The length of the output message.
188 * Returns CURLE_OK on success.
190 CURLcode Curl_sasl_create_login_message(struct SessionHandle *data,
191 const char *valuep, char **outptr,
194 size_t vlen = strlen(valuep);
197 /* Calculate an empty reply */
198 *outptr = strdup("=");
200 *outlen = (size_t) 1;
205 return CURLE_OUT_OF_MEMORY;
208 /* Base64 encode the value */
209 return Curl_base64_encode(data, valuep, vlen, outptr, outlen);
212 #ifndef CURL_DISABLE_CRYPTO_AUTH
214 * Curl_sasl_decode_cram_md5_message()
216 * This is used to decode an already encoded CRAM-MD5 challenge message.
220 * chlg64 [in] - Pointer to the base64 encoded challenge message.
221 * outptr [in/out] - The address where a pointer to newly allocated memory
222 * holding the result will be stored upon completion.
223 * outlen [out] - The length of the output message.
225 * Returns CURLE_OK on success.
227 CURLcode Curl_sasl_decode_cram_md5_message(const char *chlg64, char **outptr,
230 CURLcode result = CURLE_OK;
231 size_t chlg64len = strlen(chlg64);
236 /* Decode the challenge if necessary */
237 if(chlg64len && *chlg64 != '=')
238 result = Curl_base64_decode(chlg64, (unsigned char **) outptr, outlen);
244 * Curl_sasl_create_cram_md5_message()
246 * This is used to generate an already encoded CRAM-MD5 response message ready
247 * for sending to the recipient.
251 * data [in] - The session handle.
252 * chlg [in] - The challenge.
253 * userp [in] - The user name.
254 * passdwp [in] - The user's password.
255 * outptr [in/out] - The address where a pointer to newly allocated memory
256 * holding the result will be stored upon completion.
257 * outlen [out] - The length of the output message.
259 * Returns CURLE_OK on success.
261 CURLcode Curl_sasl_create_cram_md5_message(struct SessionHandle *data,
265 char **outptr, size_t *outlen)
267 CURLcode result = CURLE_OK;
270 unsigned char digest[MD5_DIGEST_LEN];
274 chlglen = strlen(chlg);
276 /* Compute the digest using the password as the key */
277 ctxt = Curl_HMAC_init(Curl_HMAC_MD5,
278 (const unsigned char *) passwdp,
279 curlx_uztoui(strlen(passwdp)));
281 return CURLE_OUT_OF_MEMORY;
283 /* Update the digest with the given challenge */
285 Curl_HMAC_update(ctxt, (const unsigned char *) chlg,
286 curlx_uztoui(chlglen));
288 /* Finalise the digest */
289 Curl_HMAC_final(ctxt, digest);
291 /* Generate the response */
293 "%s %02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
294 userp, digest[0], digest[1], digest[2], digest[3], digest[4],
295 digest[5], digest[6], digest[7], digest[8], digest[9], digest[10],
296 digest[11], digest[12], digest[13], digest[14], digest[15]);
298 return CURLE_OUT_OF_MEMORY;
300 /* Base64 encode the response */
301 result = Curl_base64_encode(data, response, 0, outptr, outlen);
303 Curl_safefree(response);
308 #ifndef USE_WINDOWS_SSPI
310 * sasl_decode_digest_md5_message()
312 * This is used internally to decode an already encoded DIGEST-MD5 challenge
313 * message into the seperate attributes.
317 * chlg64 [in] - Pointer to the base64 encoded challenge message.
318 * nonce [in/out] - The buffer where the nonce will be stored.
319 * nlen [in] - The length of the nonce buffer.
320 * realm [in/out] - The buffer where the realm will be stored.
321 * rlen [in] - The length of the realm buffer.
322 * alg [in/out] - The buffer where the algorithm will be stored.
323 * alen [in] - The length of the algorithm buffer.
324 * qop [in/out] - The buffer where the qop-options will be stored.
325 * qlen [in] - The length of the qop buffer.
327 * Returns CURLE_OK on success.
329 static CURLcode sasl_decode_digest_md5_message(const char *chlg64,
330 char *nonce, size_t nlen,
331 char *realm, size_t rlen,
332 char *alg, size_t alen,
333 char *qop, size_t qlen)
335 CURLcode result = CURLE_OK;
336 unsigned char *chlg = NULL;
338 size_t chlg64len = strlen(chlg64);
340 /* Decode the base-64 encoded challenge message */
341 if(chlg64len && *chlg64 != '=') {
342 result = Curl_base64_decode(chlg64, &chlg, &chlglen);
347 /* Ensure we have a valid challenge message */
349 return CURLE_BAD_CONTENT_ENCODING;
351 /* Retrieve nonce string from the challenge */
352 if(!sasl_digest_get_key_value((char *)chlg, "nonce=\"", nonce, nlen, '\"')) {
354 return CURLE_BAD_CONTENT_ENCODING;
357 /* Retrieve realm string from the challenge */
358 if(!sasl_digest_get_key_value((char *)chlg, "realm=\"", realm, rlen, '\"')) {
359 /* Challenge does not have a realm, set empty string [RFC2831] page 6 */
363 /* Retrieve algorithm string from the challenge */
364 if(!sasl_digest_get_key_value((char *)chlg, "algorithm=", alg, alen, ',')) {
366 return CURLE_BAD_CONTENT_ENCODING;
369 /* Retrieve qop-options string from the challenge */
370 if(!sasl_digest_get_key_value((char *)chlg, "qop=\"", qop, qlen, '\"')) {
372 return CURLE_BAD_CONTENT_ENCODING;
381 * Curl_sasl_create_digest_md5_message()
383 * This is used to generate an already encoded DIGEST-MD5 response message
384 * ready for sending to the recipient.
388 * data [in] - The session handle.
389 * chlg64 [in] - Pointer to the base64 encoded challenge message.
390 * userp [in] - The user name.
391 * passdwp [in] - The user's password.
392 * service [in] - The service type such as www, smtp, pop or imap.
393 * outptr [in/out] - The address where a pointer to newly allocated memory
394 * holding the result will be stored upon completion.
395 * outlen [out] - The length of the output message.
397 * Returns CURLE_OK on success.
399 CURLcode Curl_sasl_create_digest_md5_message(struct SessionHandle *data,
404 char **outptr, size_t *outlen)
406 CURLcode result = CURLE_OK;
409 char *response = NULL;
410 unsigned char digest[MD5_DIGEST_LEN];
411 char HA1_hex[2 * MD5_DIGEST_LEN + 1];
412 char HA2_hex[2 * MD5_DIGEST_LEN + 1];
413 char resp_hash_hex[2 * MD5_DIGEST_LEN + 1];
417 char qop_options[64];
420 unsigned int entropy[4];
421 char nonceCount[] = "00000001";
422 char method[] = "AUTHENTICATE";
423 char qop[] = DIGEST_QOP_VALUE_STRING_AUTH;
426 /* Decode the challange message */
427 result = sasl_decode_digest_md5_message(chlg64, nonce, sizeof(nonce),
428 realm, sizeof(realm),
429 algorithm, sizeof(algorithm),
430 qop_options, sizeof(qop_options));
434 /* We only support md5 sessions */
435 if(strcmp(algorithm, "md5-sess") != 0)
436 return CURLE_BAD_CONTENT_ENCODING;
438 /* Get the qop-values from the qop-options */
439 result = sasl_digest_get_qop_values(qop_options, &qop_values);
443 /* We only support auth quality-of-protection */
444 if(!(qop_values & DIGEST_QOP_VALUE_AUTH))
445 return CURLE_BAD_CONTENT_ENCODING;
447 /* Generate 16 bytes of random data */
448 entropy[0] = Curl_rand(data);
449 entropy[1] = Curl_rand(data);
450 entropy[2] = Curl_rand(data);
451 entropy[3] = Curl_rand(data);
453 /* Convert the random data into a 32 byte hex string */
454 snprintf(cnonce, sizeof(cnonce), "%08x%08x%08x%08x",
455 entropy[0], entropy[1], entropy[2], entropy[3]);
457 /* So far so good, now calculate A1 and H(A1) according to RFC 2831 */
458 ctxt = Curl_MD5_init(Curl_DIGEST_MD5);
460 return CURLE_OUT_OF_MEMORY;
462 Curl_MD5_update(ctxt, (const unsigned char *) userp,
463 curlx_uztoui(strlen(userp)));
464 Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
465 Curl_MD5_update(ctxt, (const unsigned char *) realm,
466 curlx_uztoui(strlen(realm)));
467 Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
468 Curl_MD5_update(ctxt, (const unsigned char *) passwdp,
469 curlx_uztoui(strlen(passwdp)));
470 Curl_MD5_final(ctxt, digest);
472 ctxt = Curl_MD5_init(Curl_DIGEST_MD5);
474 return CURLE_OUT_OF_MEMORY;
476 Curl_MD5_update(ctxt, (const unsigned char *) digest, MD5_DIGEST_LEN);
477 Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
478 Curl_MD5_update(ctxt, (const unsigned char *) nonce,
479 curlx_uztoui(strlen(nonce)));
480 Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
481 Curl_MD5_update(ctxt, (const unsigned char *) cnonce,
482 curlx_uztoui(strlen(cnonce)));
483 Curl_MD5_final(ctxt, digest);
485 /* Convert calculated 16 octet hex into 32 bytes string */
486 for(i = 0; i < MD5_DIGEST_LEN; i++)
487 snprintf(&HA1_hex[2 * i], 3, "%02x", digest[i]);
489 /* Prepare the URL string */
490 snprintf(uri, sizeof(uri), "%s/%s", service, realm);
492 /* Calculate H(A2) */
493 ctxt = Curl_MD5_init(Curl_DIGEST_MD5);
495 return CURLE_OUT_OF_MEMORY;
497 Curl_MD5_update(ctxt, (const unsigned char *) method,
498 curlx_uztoui(strlen(method)));
499 Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
500 Curl_MD5_update(ctxt, (const unsigned char *) uri,
501 curlx_uztoui(strlen(uri)));
502 Curl_MD5_final(ctxt, digest);
504 for(i = 0; i < MD5_DIGEST_LEN; i++)
505 snprintf(&HA2_hex[2 * i], 3, "%02x", digest[i]);
507 /* Now calculate the response hash */
508 ctxt = Curl_MD5_init(Curl_DIGEST_MD5);
510 return CURLE_OUT_OF_MEMORY;
512 Curl_MD5_update(ctxt, (const unsigned char *) HA1_hex, 2 * MD5_DIGEST_LEN);
513 Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
514 Curl_MD5_update(ctxt, (const unsigned char *) nonce,
515 curlx_uztoui(strlen(nonce)));
516 Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
518 Curl_MD5_update(ctxt, (const unsigned char *) nonceCount,
519 curlx_uztoui(strlen(nonceCount)));
520 Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
521 Curl_MD5_update(ctxt, (const unsigned char *) cnonce,
522 curlx_uztoui(strlen(cnonce)));
523 Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
524 Curl_MD5_update(ctxt, (const unsigned char *) qop,
525 curlx_uztoui(strlen(qop)));
526 Curl_MD5_update(ctxt, (const unsigned char *) ":", 1);
528 Curl_MD5_update(ctxt, (const unsigned char *) HA2_hex, 2 * MD5_DIGEST_LEN);
529 Curl_MD5_final(ctxt, digest);
531 for(i = 0; i < MD5_DIGEST_LEN; i++)
532 snprintf(&resp_hash_hex[2 * i], 3, "%02x", digest[i]);
534 /* Generate the response */
535 response = aprintf("username=\"%s\",realm=\"%s\",nonce=\"%s\","
536 "cnonce=\"%s\",nc=\"%s\",digest-uri=\"%s\",response=%s,"
539 cnonce, nonceCount, uri, resp_hash_hex, qop);
541 return CURLE_OUT_OF_MEMORY;
543 /* Base64 encode the response */
544 result = Curl_base64_encode(data, response, 0, outptr, outlen);
549 #endif /* USE_WINDOWS_SSPI */
551 #endif /* CURL_DISABLE_CRYPTO_AUTH */
555 * Curl_sasl_create_ntlm_type1_message()
557 * This is used to generate an already encoded NTLM type-1 message ready for
558 * sending to the recipient.
560 * Note: This is a simple wrapper of the NTLM function which means that any
561 * SASL based protocols don't have to include the NTLM functions directly.
565 * userp [in] - The user name in the format User or Domain\User.
566 * passdwp [in] - The user's password.
567 * ntlm [in/out] - The ntlm data struct being used and modified.
568 * outptr [in/out] - The address where a pointer to newly allocated memory
569 * holding the result will be stored upon completion.
570 * outlen [out] - The length of the output message.
572 * Returns CURLE_OK on success.
574 CURLcode Curl_sasl_create_ntlm_type1_message(const char *userp,
576 struct ntlmdata *ntlm,
577 char **outptr, size_t *outlen)
579 return Curl_ntlm_create_type1_message(userp, passwdp, ntlm, outptr, outlen);
583 * Curl_sasl_decode_ntlm_type2_message()
585 * This is used to decode an already encoded NTLM type-2 message.
589 * data [in] - Pointer to session handle.
590 * type2msg [in] - Pointer to the base64 encoded type-2 message.
591 * ntlm [in/out] - The ntlm data struct being used and modified.
593 * Returns CURLE_OK on success.
595 CURLcode Curl_sasl_decode_ntlm_type2_message(struct SessionHandle *data,
596 const char *type2msg,
597 struct ntlmdata *ntlm)
602 /* make sure the crypto backend is initialized */
603 result = Curl_nss_force_init(data);
608 return Curl_ntlm_decode_type2_message(data, type2msg, ntlm);
612 * Curl_sasl_create_ntlm_type3_message()
614 * This is used to generate an already encoded NTLM type-3 message ready for
615 * sending to the recipient.
619 * data [in] - Pointer to session handle.
620 * userp [in] - The user name in the format User or Domain\User.
621 * passdwp [in] - The user's password.
622 * ntlm [in/out] - The ntlm data struct being used and modified.
623 * outptr [in/out] - The address where a pointer to newly allocated memory
624 * holding the result will be stored upon completion.
625 * outlen [out] - The length of the output message.
627 * Returns CURLE_OK on success.
629 CURLcode Curl_sasl_create_ntlm_type3_message(struct SessionHandle *data,
632 struct ntlmdata *ntlm,
633 char **outptr, size_t *outlen)
635 return Curl_ntlm_create_type3_message(data, userp, passwdp, ntlm, outptr,
638 #endif /* USE_NTLM */
641 * Curl_sasl_create_xoauth2_message()
643 * This is used to generate an already encoded OAuth 2.0 message ready for
644 * sending to the recipient.
648 * data [in] - The session handle.
649 * user [in] - The user name.
650 * bearer [in] - The bearer token.
651 * outptr [in/out] - The address where a pointer to newly allocated memory
652 * holding the result will be stored upon completion.
653 * outlen [out] - The length of the output message.
655 * Returns CURLE_OK on success.
657 CURLcode Curl_sasl_create_xoauth2_message(struct SessionHandle *data,
660 char **outptr, size_t *outlen)
662 CURLcode result = CURLE_OK;
665 /* Generate the message */
666 xoauth = aprintf("user=%s\1auth=Bearer %s\1\1", user, bearer);
668 return CURLE_OUT_OF_MEMORY;
670 /* Base64 encode the reply */
671 result = Curl_base64_encode(data, xoauth, strlen(xoauth), outptr, outlen);
673 Curl_safefree(xoauth);
679 * Curl_sasl_cleanup()
681 * This is used to cleanup any libraries or curl modules used by the sasl
686 * conn [in] - Pointer to the connection data.
687 * authused [in] - The authentication mechanism used.
689 void Curl_sasl_cleanup(struct connectdata *conn, unsigned int authused)
692 /* Cleanup the ntlm structure */
693 if(authused == SASL_MECH_NTLM) {
694 Curl_ntlm_sspi_cleanup(&conn->ntlm);
698 /* Reserved for future use */