1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2012 - 2022, 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 https://curl.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 * RFC2617 Basic and Digest Access Authentication
23 * RFC2831 DIGEST-MD5 authentication
24 * RFC4422 Simple Authentication and Security Layer (SASL)
25 * RFC4616 PLAIN authentication
26 * RFC5802 SCRAM-SHA-1 authentication
27 * RFC7677 SCRAM-SHA-256 authentication
28 * RFC6749 OAuth 2.0 Authorization Framework
29 * RFC7628 A Set of SASL Mechanisms for OAuth
30 * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
32 ***************************************************************************/
34 #include "curl_setup.h"
36 #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \
37 !defined(CURL_DISABLE_POP3)
39 #include <curl/curl.h>
42 #include "curl_base64.h"
44 #include "vauth/vauth.h"
45 #include "vtls/vtls.h"
46 #include "curl_hmac.h"
47 #include "curl_sasl.h"
51 /* The last 3 #include files should be in this order */
52 #include "curl_printf.h"
53 #include "curl_memory.h"
56 /* Supported mechanisms */
58 const char *name; /* Name */
59 size_t len; /* Name length */
60 unsigned short bit; /* Flag bit */
62 { "LOGIN", 5, SASL_MECH_LOGIN },
63 { "PLAIN", 5, SASL_MECH_PLAIN },
64 { "CRAM-MD5", 8, SASL_MECH_CRAM_MD5 },
65 { "DIGEST-MD5", 10, SASL_MECH_DIGEST_MD5 },
66 { "GSSAPI", 6, SASL_MECH_GSSAPI },
67 { "EXTERNAL", 8, SASL_MECH_EXTERNAL },
68 { "NTLM", 4, SASL_MECH_NTLM },
69 { "XOAUTH2", 7, SASL_MECH_XOAUTH2 },
70 { "OAUTHBEARER", 11, SASL_MECH_OAUTHBEARER },
71 { "SCRAM-SHA-1", 11, SASL_MECH_SCRAM_SHA_1 },
72 { "SCRAM-SHA-256",13, SASL_MECH_SCRAM_SHA_256 },
79 * This is used to cleanup any libraries or curl modules used by the sasl
84 * conn [in] - The connection data.
85 * authused [in] - The authentication mechanism used.
87 void Curl_sasl_cleanup(struct connectdata *conn, unsigned short authused)
92 #if defined(USE_KERBEROS5)
93 /* Cleanup the gssapi structure */
94 if(authused == SASL_MECH_GSSAPI) {
95 Curl_auth_cleanup_gssapi(&conn->krb5);
99 #if defined(USE_GSASL)
100 /* Cleanup the GSASL structure */
101 if(authused & (SASL_MECH_SCRAM_SHA_1 | SASL_MECH_SCRAM_SHA_256)) {
102 Curl_auth_gsasl_cleanup(&conn->gsasl);
106 #if defined(USE_NTLM)
107 /* Cleanup the NTLM structure */
108 if(authused == SASL_MECH_NTLM) {
109 Curl_auth_cleanup_ntlm(&conn->ntlm);
115 * Curl_sasl_decode_mech()
117 * Convert a SASL mechanism name into a token.
121 * ptr [in] - The mechanism string.
122 * maxlen [in] - Maximum mechanism string length.
123 * len [out] - If not NULL, effective name length.
125 * Returns the SASL mechanism token or 0 if no match.
127 unsigned short Curl_sasl_decode_mech(const char *ptr, size_t maxlen,
133 for(i = 0; mechtable[i].name; i++) {
134 if(maxlen >= mechtable[i].len &&
135 !memcmp(ptr, mechtable[i].name, mechtable[i].len)) {
137 *len = mechtable[i].len;
139 if(maxlen == mechtable[i].len)
140 return mechtable[i].bit;
142 c = ptr[mechtable[i].len];
143 if(!ISUPPER(c) && !ISDIGIT(c) && c != '-' && c != '_')
144 return mechtable[i].bit;
152 * Curl_sasl_parse_url_auth_option()
154 * Parse the URL login options.
156 CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
157 const char *value, size_t len)
159 CURLcode result = CURLE_OK;
163 return CURLE_URL_MALFORMAT;
165 if(sasl->resetprefs) {
166 sasl->resetprefs = FALSE;
167 sasl->prefmech = SASL_AUTH_NONE;
170 if(!strncmp(value, "*", len))
171 sasl->prefmech = SASL_AUTH_DEFAULT;
173 unsigned short mechbit = Curl_sasl_decode_mech(value, len, &mechlen);
174 if(mechbit && mechlen == len)
175 sasl->prefmech |= mechbit;
177 result = CURLE_URL_MALFORMAT;
186 * Initializes the SASL structure.
188 void Curl_sasl_init(struct SASL *sasl, struct Curl_easy *data,
189 const struct SASLproto *params)
191 unsigned long auth = data->set.httpauth;
193 sasl->params = params; /* Set protocol dependent parameters */
194 sasl->state = SASL_STOP; /* Not yet running */
195 sasl->curmech = NULL; /* No mechanism yet. */
196 sasl->authmechs = SASL_AUTH_NONE; /* No known authentication mechanism yet */
197 sasl->prefmech = params->defmechs; /* Default preferred mechanisms */
198 sasl->authused = SASL_AUTH_NONE; /* The authentication mechanism used */
199 sasl->resetprefs = TRUE; /* Reset prefmech upon AUTH parsing. */
200 sasl->mutual_auth = FALSE; /* No mutual authentication (GSSAPI only) */
201 sasl->force_ir = FALSE; /* Respect external option */
203 if(auth != CURLAUTH_BASIC) {
204 sasl->resetprefs = FALSE;
205 sasl->prefmech = SASL_AUTH_NONE;
206 if(auth & CURLAUTH_BASIC)
207 sasl->prefmech |= SASL_MECH_PLAIN | SASL_MECH_LOGIN;
208 if(auth & CURLAUTH_DIGEST)
209 sasl->prefmech |= SASL_MECH_DIGEST_MD5;
210 if(auth & CURLAUTH_NTLM)
211 sasl->prefmech |= SASL_MECH_NTLM;
212 if(auth & CURLAUTH_BEARER)
213 sasl->prefmech |= SASL_MECH_OAUTHBEARER | SASL_MECH_XOAUTH2;
214 if(auth & CURLAUTH_GSSAPI)
215 sasl->prefmech |= SASL_MECH_GSSAPI;
222 * This is the ONLY way to change SASL state!
224 static void state(struct SASL *sasl, struct Curl_easy *data,
227 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
228 /* for debug purposes */
229 static const char * const names[]={
251 if(sasl->state != newstate)
252 infof(data, "SASL %p state change from %s to %s",
253 (void *)sasl, names[sasl->state], names[newstate]);
258 sasl->state = newstate;
261 /* Get the SASL server message and convert it to binary. */
262 static CURLcode get_server_message(struct SASL *sasl, struct Curl_easy *data,
265 CURLcode result = CURLE_OK;
267 result = sasl->params->getmessage(data, out);
268 if(!result && (sasl->params->flags & SASL_FLAG_BASE64)) {
271 const char *serverdata = (const char *) Curl_bufref_ptr(out);
273 if(!*serverdata || *serverdata == '=')
274 Curl_bufref_set(out, NULL, 0, NULL);
276 result = Curl_base64_decode(serverdata, &msg, &msglen);
278 Curl_bufref_set(out, msg, msglen, curl_free);
284 /* Encode the outgoing SASL message. */
285 static CURLcode build_message(struct SASL *sasl, struct bufref *msg)
287 CURLcode result = CURLE_OK;
289 if(sasl->params->flags & SASL_FLAG_BASE64) {
290 if(!Curl_bufref_ptr(msg)) /* Empty message. */
291 Curl_bufref_set(msg, "", 0, NULL);
292 else if(!Curl_bufref_len(msg)) /* Explicit empty response. */
293 Curl_bufref_set(msg, "=", 1, NULL);
298 result = Curl_base64_encode((const char *) Curl_bufref_ptr(msg),
299 Curl_bufref_len(msg), &base64, &base64len);
301 Curl_bufref_set(msg, base64, base64len, curl_free);
309 * Curl_sasl_can_authenticate()
311 * Check if we have enough auth data and capabilities to authenticate.
313 bool Curl_sasl_can_authenticate(struct SASL *sasl, struct Curl_easy *data)
315 /* Have credentials been provided? */
316 if(data->state.aptr.user)
319 /* EXTERNAL can authenticate without a user name and/or password */
320 if(sasl->authmechs & sasl->prefmech & SASL_MECH_EXTERNAL)
329 * Calculate the required login details for SASL authentication.
331 CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
332 bool force_ir, saslprogress *progress)
334 CURLcode result = CURLE_OK;
335 struct connectdata *conn = data->conn;
336 unsigned short enabledmechs;
337 const char *mech = NULL;
339 saslstate state1 = SASL_STOP;
340 saslstate state2 = SASL_FINAL;
341 const char * const hostname = SSL_HOST_NAME();
342 const long int port = SSL_HOST_PORT();
343 #if defined(USE_KERBEROS5) || defined(USE_NTLM)
344 const char *service = data->set.str[STRING_SERVICE_NAME] ?
345 data->set.str[STRING_SERVICE_NAME] :
346 sasl->params->service;
348 const char *oauth_bearer = data->set.str[STRING_BEARER];
349 struct bufref nullmsg;
351 Curl_bufref_init(&nullmsg);
352 Curl_bufref_init(&resp);
353 sasl->force_ir = force_ir; /* Latch for future use */
354 sasl->authused = 0; /* No mechanism used yet */
355 enabledmechs = sasl->authmechs & sasl->prefmech;
356 *progress = SASL_IDLE;
358 /* Calculate the supported authentication mechanism, by decreasing order of
359 security, as well as the initial response where appropriate */
360 if((enabledmechs & SASL_MECH_EXTERNAL) && !conn->passwd[0]) {
361 mech = SASL_MECH_STRING_EXTERNAL;
362 state1 = SASL_EXTERNAL;
363 sasl->authused = SASL_MECH_EXTERNAL;
365 if(force_ir || data->set.sasl_ir)
366 result = Curl_auth_create_external_message(conn->user, &resp);
368 else if(data->state.aptr.user) {
369 #if defined(USE_KERBEROS5)
370 if((enabledmechs & SASL_MECH_GSSAPI) && Curl_auth_is_gssapi_supported() &&
371 Curl_auth_user_contains_domain(conn->user)) {
372 sasl->mutual_auth = FALSE;
373 mech = SASL_MECH_STRING_GSSAPI;
374 state1 = SASL_GSSAPI;
375 state2 = SASL_GSSAPI_TOKEN;
376 sasl->authused = SASL_MECH_GSSAPI;
378 if(force_ir || data->set.sasl_ir)
379 result = Curl_auth_create_gssapi_user_message(data, conn->user,
390 if((enabledmechs & SASL_MECH_SCRAM_SHA_256) &&
391 Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_256,
393 mech = SASL_MECH_STRING_SCRAM_SHA_256;
394 sasl->authused = SASL_MECH_SCRAM_SHA_256;
398 result = Curl_auth_gsasl_start(data, conn->user,
399 conn->passwd, &conn->gsasl);
400 if(result == CURLE_OK && (force_ir || data->set.sasl_ir))
401 result = Curl_auth_gsasl_token(data, &nullmsg, &conn->gsasl, &resp);
403 else if((enabledmechs & SASL_MECH_SCRAM_SHA_1) &&
404 Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_1,
406 mech = SASL_MECH_STRING_SCRAM_SHA_1;
407 sasl->authused = SASL_MECH_SCRAM_SHA_1;
411 result = Curl_auth_gsasl_start(data, conn->user,
412 conn->passwd, &conn->gsasl);
413 if(result == CURLE_OK && (force_ir || data->set.sasl_ir))
414 result = Curl_auth_gsasl_token(data, &nullmsg, &conn->gsasl, &resp);
418 #ifndef CURL_DISABLE_CRYPTO_AUTH
419 if((enabledmechs & SASL_MECH_DIGEST_MD5) &&
420 Curl_auth_is_digest_supported()) {
421 mech = SASL_MECH_STRING_DIGEST_MD5;
422 state1 = SASL_DIGESTMD5;
423 sasl->authused = SASL_MECH_DIGEST_MD5;
425 else if(enabledmechs & SASL_MECH_CRAM_MD5) {
426 mech = SASL_MECH_STRING_CRAM_MD5;
427 state1 = SASL_CRAMMD5;
428 sasl->authused = SASL_MECH_CRAM_MD5;
433 if((enabledmechs & SASL_MECH_NTLM) && Curl_auth_is_ntlm_supported()) {
434 mech = SASL_MECH_STRING_NTLM;
436 state2 = SASL_NTLM_TYPE2MSG;
437 sasl->authused = SASL_MECH_NTLM;
439 if(force_ir || data->set.sasl_ir)
440 result = Curl_auth_create_ntlm_type1_message(data,
441 conn->user, conn->passwd,
448 if((enabledmechs & SASL_MECH_OAUTHBEARER) && oauth_bearer) {
449 mech = SASL_MECH_STRING_OAUTHBEARER;
450 state1 = SASL_OAUTH2;
451 state2 = SASL_OAUTH2_RESP;
452 sasl->authused = SASL_MECH_OAUTHBEARER;
454 if(force_ir || data->set.sasl_ir)
455 result = Curl_auth_create_oauth_bearer_message(conn->user,
461 else if((enabledmechs & SASL_MECH_XOAUTH2) && oauth_bearer) {
462 mech = SASL_MECH_STRING_XOAUTH2;
463 state1 = SASL_OAUTH2;
464 sasl->authused = SASL_MECH_XOAUTH2;
466 if(force_ir || data->set.sasl_ir)
467 result = Curl_auth_create_xoauth_bearer_message(conn->user,
471 else if(enabledmechs & SASL_MECH_PLAIN) {
472 mech = SASL_MECH_STRING_PLAIN;
474 sasl->authused = SASL_MECH_PLAIN;
476 if(force_ir || data->set.sasl_ir)
477 result = Curl_auth_create_plain_message(conn->sasl_authzid,
478 conn->user, conn->passwd,
481 else if(enabledmechs & SASL_MECH_LOGIN) {
482 mech = SASL_MECH_STRING_LOGIN;
484 state2 = SASL_LOGIN_PASSWD;
485 sasl->authused = SASL_MECH_LOGIN;
487 if(force_ir || data->set.sasl_ir)
488 result = Curl_auth_create_login_message(conn->user, &resp);
492 if(!result && mech) {
493 sasl->curmech = mech;
494 if(Curl_bufref_ptr(&resp))
495 result = build_message(sasl, &resp);
497 if(sasl->params->maxirlen &&
498 strlen(mech) + Curl_bufref_len(&resp) > sasl->params->maxirlen)
499 Curl_bufref_free(&resp);
502 result = sasl->params->sendauth(data, mech, &resp);
505 *progress = SASL_INPROGRESS;
506 state(sasl, data, Curl_bufref_ptr(&resp) ? state2 : state1);
510 Curl_bufref_free(&resp);
515 * Curl_sasl_continue()
517 * Continue the authentication.
519 CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
520 int code, saslprogress *progress)
522 CURLcode result = CURLE_OK;
523 struct connectdata *conn = data->conn;
524 saslstate newstate = SASL_FINAL;
526 const char * const hostname = SSL_HOST_NAME();
527 const long int port = SSL_HOST_PORT();
528 #if !defined(CURL_DISABLE_CRYPTO_AUTH) || defined(USE_KERBEROS5) || \
530 const char *service = data->set.str[STRING_SERVICE_NAME] ?
531 data->set.str[STRING_SERVICE_NAME] :
532 sasl->params->service;
534 const char *oauth_bearer = data->set.str[STRING_BEARER];
535 struct bufref serverdata;
537 Curl_bufref_init(&serverdata);
538 Curl_bufref_init(&resp);
539 *progress = SASL_INPROGRESS;
541 if(sasl->state == SASL_FINAL) {
542 if(code != sasl->params->finalcode)
543 result = CURLE_LOGIN_DENIED;
544 *progress = SASL_DONE;
545 state(sasl, data, SASL_STOP);
549 if(sasl->state != SASL_CANCEL && sasl->state != SASL_OAUTH2_RESP &&
550 code != sasl->params->contcode) {
551 *progress = SASL_DONE;
552 state(sasl, data, SASL_STOP);
553 return CURLE_LOGIN_DENIED;
556 switch(sasl->state) {
558 *progress = SASL_DONE;
561 result = Curl_auth_create_plain_message(conn->sasl_authzid,
562 conn->user, conn->passwd, &resp);
565 result = Curl_auth_create_login_message(conn->user, &resp);
566 newstate = SASL_LOGIN_PASSWD;
568 case SASL_LOGIN_PASSWD:
569 result = Curl_auth_create_login_message(conn->passwd, &resp);
572 result = Curl_auth_create_external_message(conn->user, &resp);
574 #ifndef CURL_DISABLE_CRYPTO_AUTH
577 result = get_server_message(sasl, data, &serverdata);
579 result = Curl_auth_gsasl_token(data, &serverdata, &conn->gsasl, &resp);
580 if(!result && Curl_bufref_len(&resp) > 0)
581 newstate = SASL_GSASL;
585 result = get_server_message(sasl, data, &serverdata);
587 result = Curl_auth_create_cram_md5_message(&serverdata, conn->user,
588 conn->passwd, &resp);
591 result = get_server_message(sasl, data, &serverdata);
593 result = Curl_auth_create_digest_md5_message(data, &serverdata,
594 conn->user, conn->passwd,
596 if(!result && (sasl->params->flags & SASL_FLAG_BASE64))
597 newstate = SASL_DIGESTMD5_RESP;
599 case SASL_DIGESTMD5_RESP:
600 /* Keep response NULL to output an empty line. */
606 /* Create the type-1 message */
607 result = Curl_auth_create_ntlm_type1_message(data,
608 conn->user, conn->passwd,
611 newstate = SASL_NTLM_TYPE2MSG;
613 case SASL_NTLM_TYPE2MSG:
614 /* Decode the type-2 message */
615 result = get_server_message(sasl, data, &serverdata);
617 result = Curl_auth_decode_ntlm_type2_message(data, &serverdata,
620 result = Curl_auth_create_ntlm_type3_message(data, conn->user,
621 conn->passwd, &conn->ntlm,
626 #if defined(USE_KERBEROS5)
628 result = Curl_auth_create_gssapi_user_message(data, conn->user,
632 sasl->mutual_auth, NULL,
635 newstate = SASL_GSSAPI_TOKEN;
637 case SASL_GSSAPI_TOKEN:
638 result = get_server_message(sasl, data, &serverdata);
640 if(sasl->mutual_auth) {
641 /* Decode the user token challenge and create the optional response
643 result = Curl_auth_create_gssapi_user_message(data, NULL, NULL,
649 newstate = SASL_GSSAPI_NO_DATA;
652 /* Decode the security challenge and create the response message */
653 result = Curl_auth_create_gssapi_security_message(data,
660 case SASL_GSSAPI_NO_DATA:
661 /* Decode the security challenge and create the response message */
662 result = get_server_message(sasl, data, &serverdata);
664 result = Curl_auth_create_gssapi_security_message(data,
673 /* Create the authorization message */
674 if(sasl->authused == SASL_MECH_OAUTHBEARER) {
675 result = Curl_auth_create_oauth_bearer_message(conn->user,
681 /* Failures maybe sent by the server as continuations for OAUTHBEARER */
682 newstate = SASL_OAUTH2_RESP;
685 result = Curl_auth_create_xoauth_bearer_message(conn->user,
690 case SASL_OAUTH2_RESP:
691 /* The continuation is optional so check the response code */
692 if(code == sasl->params->finalcode) {
693 /* Final response was received so we are done */
694 *progress = SASL_DONE;
695 state(sasl, data, SASL_STOP);
698 else if(code == sasl->params->contcode) {
699 /* Acknowledge the continuation by sending a 0x01 response. */
700 Curl_bufref_set(&resp, "\x01", 1, NULL);
704 *progress = SASL_DONE;
705 state(sasl, data, SASL_STOP);
706 return CURLE_LOGIN_DENIED;
710 /* Remove the offending mechanism from the supported list */
711 sasl->authmechs ^= sasl->authused;
713 /* Start an alternative SASL authentication */
714 return Curl_sasl_start(sasl, data, sasl->force_ir, progress);
716 failf(data, "Unsupported SASL authentication mechanism");
717 result = CURLE_UNSUPPORTED_PROTOCOL; /* Should not happen */
721 Curl_bufref_free(&serverdata);
724 case CURLE_BAD_CONTENT_ENCODING:
726 result = sasl->params->cancelauth(data, sasl->curmech);
727 newstate = SASL_CANCEL;
730 result = build_message(sasl, &resp);
732 result = sasl->params->contauth(data, sasl->curmech, &resp);
735 newstate = SASL_STOP; /* Stop on error */
736 *progress = SASL_DONE;
740 Curl_bufref_free(&resp);
742 state(sasl, data, newstate);
746 #endif /* protocols are enabled that use SASL */