1 /***************************************************************************
3 * Project ___| | | | _ \| |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * Copyright (C) 2012 - 2016, 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.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 * RFC2617 Basic and Digest Access Authentication
23 * RFC2831 DIGEST-MD5 authentication
24 * RFC4422 Simple Authentication and Security Layer (SASL)
25 * RFC4616 PLAIN authentication
26 * RFC6749 OAuth 2.0 Authorization Framework
27 * RFC7628 A Set of SASL Mechanisms for OAuth
28 * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
30 ***************************************************************************/
32 #include "curl_setup.h"
34 #include <curl/curl.h>
37 #include "curl_base64.h"
39 #include "vauth/vauth.h"
40 #include "vtls/vtls.h"
41 #include "curl_hmac.h"
42 #include "curl_sasl.h"
46 #include "non-ascii.h" /* included for Curl_convert_... prototypes */
47 /* The last 3 #include files should be in this order */
48 #include "curl_printf.h"
49 #include "curl_memory.h"
52 /* Supported mechanisms */
54 const char *name; /* Name */
55 size_t len; /* Name length */
56 unsigned int bit; /* Flag bit */
58 { "LOGIN", 5, SASL_MECH_LOGIN },
59 { "PLAIN", 5, SASL_MECH_PLAIN },
60 { "CRAM-MD5", 8, SASL_MECH_CRAM_MD5 },
61 { "DIGEST-MD5", 10, SASL_MECH_DIGEST_MD5 },
62 { "GSSAPI", 6, SASL_MECH_GSSAPI },
63 { "EXTERNAL", 8, SASL_MECH_EXTERNAL },
64 { "NTLM", 4, SASL_MECH_NTLM },
65 { "XOAUTH2", 7, SASL_MECH_XOAUTH2 },
66 { "OAUTHBEARER", 11, SASL_MECH_OAUTHBEARER },
73 * This is used to cleanup any libraries or curl modules used by the sasl
78 * conn [in] - The connection data.
79 * authused [in] - The authentication mechanism used.
81 void Curl_sasl_cleanup(struct connectdata *conn, unsigned int authused)
83 #if defined(USE_KERBEROS5)
84 /* Cleanup the gssapi structure */
85 if(authused == SASL_MECH_GSSAPI) {
86 Curl_auth_gssapi_cleanup(&conn->krb5);
91 /* Cleanup the NTLM structure */
92 if(authused == SASL_MECH_NTLM) {
93 Curl_auth_ntlm_cleanup(&conn->ntlm);
97 #if !defined(USE_KERBEROS5) && !defined(USE_NTLM)
98 /* Reserved for future use */
105 * Curl_sasl_decode_mech()
107 * Convert a SASL mechanism name into a token.
111 * ptr [in] - The mechanism string.
112 * maxlen [in] - Maximum mechanism string length.
113 * len [out] - If not NULL, effective name length.
115 * Returns the SASL mechanism token or 0 if no match.
117 unsigned int Curl_sasl_decode_mech(const char *ptr, size_t maxlen, size_t *len)
122 for(i = 0; mechtable[i].name; i++) {
123 if(maxlen >= mechtable[i].len &&
124 !memcmp(ptr, mechtable[i].name, mechtable[i].len)) {
126 *len = mechtable[i].len;
128 if(maxlen == mechtable[i].len)
129 return mechtable[i].bit;
131 c = ptr[mechtable[i].len];
132 if(!ISUPPER(c) && !ISDIGIT(c) && c != '-' && c != '_')
133 return mechtable[i].bit;
141 * Curl_sasl_parse_url_auth_option()
143 * Parse the URL login options.
145 CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
146 const char *value, size_t len)
148 CURLcode result = CURLE_OK;
149 unsigned int mechbit;
153 return CURLE_URL_MALFORMAT;
155 if(sasl->resetprefs) {
156 sasl->resetprefs = FALSE;
157 sasl->prefmech = SASL_AUTH_NONE;
160 if(!strncmp(value, "*", len))
161 sasl->prefmech = SASL_AUTH_DEFAULT;
163 mechbit = Curl_sasl_decode_mech(value, len, &mechlen);
164 if(mechbit && mechlen == len)
165 sasl->prefmech |= mechbit;
167 result = CURLE_URL_MALFORMAT;
176 * Initializes the SASL structure.
178 void Curl_sasl_init(struct SASL *sasl, const struct SASLproto *params)
180 sasl->params = params; /* Set protocol dependent parameters */
181 sasl->state = SASL_STOP; /* Not yet running */
182 sasl->authmechs = SASL_AUTH_NONE; /* No known authentication mechanism yet */
183 sasl->prefmech = SASL_AUTH_DEFAULT; /* Prefer all mechanisms */
184 sasl->authused = SASL_AUTH_NONE; /* No the authentication mechanism used */
185 sasl->resetprefs = TRUE; /* Reset prefmech upon AUTH parsing. */
186 sasl->mutual_auth = FALSE; /* No mutual authentication (GSSAPI only) */
187 sasl->force_ir = FALSE; /* Respect external option */
193 * This is the ONLY way to change SASL state!
195 static void state(struct SASL *sasl, struct connectdata *conn,
198 #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
199 /* for debug purposes */
200 static const char * const names[]={
221 if(sasl->state != newstate)
222 infof(conn->data, "SASL %p state change from %s to %s\n",
223 (void *)sasl, names[sasl->state], names[newstate]);
228 sasl->state = newstate;
232 * Curl_sasl_can_authenticate()
234 * Check if we have enough auth data and capabilities to authenticate.
236 bool Curl_sasl_can_authenticate(struct SASL *sasl, struct connectdata *conn)
238 /* Have credentials been provided? */
239 if(conn->bits.user_passwd)
242 /* EXTERNAL can authenticate without a user name and/or password */
243 if(sasl->authmechs & sasl->prefmech & SASL_MECH_EXTERNAL)
252 * Calculate the required login details for SASL authentication.
254 CURLcode Curl_sasl_start(struct SASL *sasl, struct connectdata *conn,
255 bool force_ir, saslprogress *progress)
257 CURLcode result = CURLE_OK;
258 struct Curl_easy *data = conn->data;
259 unsigned int enabledmechs;
260 const char *mech = NULL;
263 saslstate state1 = SASL_STOP;
264 saslstate state2 = SASL_FINAL;
265 const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
267 const long int port = SSL_IS_PROXY() ? conn->port : conn->remote_port;
268 #if defined(USE_KERBEROS5)
269 const char *service = data->set.str[STRING_SERVICE_NAME] ?
270 data->set.str[STRING_SERVICE_NAME] :
271 sasl->params->service;
274 sasl->force_ir = force_ir; /* Latch for future use */
275 sasl->authused = 0; /* No mechanism used yet */
276 enabledmechs = sasl->authmechs & sasl->prefmech;
277 *progress = SASL_IDLE;
279 /* Calculate the supported authentication mechanism, by decreasing order of
280 security, as well as the initial response where appropriate */
281 if((enabledmechs & SASL_MECH_EXTERNAL) && !conn->passwd[0]) {
282 mech = SASL_MECH_STRING_EXTERNAL;
283 state1 = SASL_EXTERNAL;
284 sasl->authused = SASL_MECH_EXTERNAL;
286 if(force_ir || data->set.sasl_ir)
287 result = Curl_auth_create_external_message(data, conn->user, &resp,
290 else if(conn->bits.user_passwd) {
291 #if defined(USE_KERBEROS5)
292 if((enabledmechs & SASL_MECH_GSSAPI) && Curl_auth_is_gssapi_supported() &&
293 Curl_auth_user_contains_domain(conn->user)) {
294 sasl->mutual_auth = FALSE; /* TODO: Calculate mutual authentication */
295 mech = SASL_MECH_STRING_GSSAPI;
296 state1 = SASL_GSSAPI;
297 state2 = SASL_GSSAPI_TOKEN;
298 sasl->authused = SASL_MECH_GSSAPI;
300 if(force_ir || data->set.sasl_ir)
301 result = Curl_auth_create_gssapi_user_message(data, conn->user,
312 #ifndef CURL_DISABLE_CRYPTO_AUTH
313 if((enabledmechs & SASL_MECH_DIGEST_MD5) &&
314 Curl_auth_is_digest_supported()) {
315 mech = SASL_MECH_STRING_DIGEST_MD5;
316 state1 = SASL_DIGESTMD5;
317 sasl->authused = SASL_MECH_DIGEST_MD5;
319 else if(enabledmechs & SASL_MECH_CRAM_MD5) {
320 mech = SASL_MECH_STRING_CRAM_MD5;
321 state1 = SASL_CRAMMD5;
322 sasl->authused = SASL_MECH_CRAM_MD5;
327 if((enabledmechs & SASL_MECH_NTLM) && Curl_auth_is_ntlm_supported()) {
328 mech = SASL_MECH_STRING_NTLM;
330 state2 = SASL_NTLM_TYPE2MSG;
331 sasl->authused = SASL_MECH_NTLM;
333 if(force_ir || data->set.sasl_ir)
334 result = Curl_auth_create_ntlm_type1_message(conn->user, conn->passwd,
335 &conn->ntlm, &resp, &len);
339 if((enabledmechs & SASL_MECH_OAUTHBEARER) && conn->oauth_bearer) {
340 mech = SASL_MECH_STRING_OAUTHBEARER;
341 state1 = SASL_OAUTH2;
342 state2 = SASL_OAUTH2_RESP;
343 sasl->authused = SASL_MECH_OAUTHBEARER;
345 if(force_ir || data->set.sasl_ir)
346 result = Curl_auth_create_oauth_bearer_message(data, conn->user,
352 else if((enabledmechs & SASL_MECH_XOAUTH2) && conn->oauth_bearer) {
353 mech = SASL_MECH_STRING_XOAUTH2;
354 state1 = SASL_OAUTH2;
355 sasl->authused = SASL_MECH_XOAUTH2;
357 if(force_ir || data->set.sasl_ir)
358 result = Curl_auth_create_oauth_bearer_message(data, conn->user,
363 else if(enabledmechs & SASL_MECH_LOGIN) {
364 mech = SASL_MECH_STRING_LOGIN;
366 state2 = SASL_LOGIN_PASSWD;
367 sasl->authused = SASL_MECH_LOGIN;
369 if(force_ir || data->set.sasl_ir)
370 result = Curl_auth_create_login_message(data, conn->user, &resp, &len);
372 else if(enabledmechs & SASL_MECH_PLAIN) {
373 mech = SASL_MECH_STRING_PLAIN;
375 sasl->authused = SASL_MECH_PLAIN;
377 if(force_ir || data->set.sasl_ir)
378 result = Curl_auth_create_plain_message(data, conn->user, conn->passwd,
383 if(!result && mech) {
384 if(resp && sasl->params->maxirlen &&
385 strlen(mech) + len > sasl->params->maxirlen) {
390 result = sasl->params->sendauth(conn, mech, resp);
392 *progress = SASL_INPROGRESS;
393 state(sasl, conn, resp ? state2 : state1);
403 * Curl_sasl_continue()
405 * Continue the authentication.
407 CURLcode Curl_sasl_continue(struct SASL *sasl, struct connectdata *conn,
408 int code, saslprogress *progress)
410 CURLcode result = CURLE_OK;
411 struct Curl_easy *data = conn->data;
412 saslstate newstate = SASL_FINAL;
414 const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
416 const long int port = SSL_IS_PROXY() ? conn->port : conn->remote_port;
417 #if !defined(CURL_DISABLE_CRYPTO_AUTH)
422 #if !defined(CURL_DISABLE_CRYPTO_AUTH) || defined(USE_KERBEROS5)
423 const char *service = data->set.str[STRING_SERVICE_NAME] ?
424 data->set.str[STRING_SERVICE_NAME] :
425 sasl->params->service;
429 *progress = SASL_INPROGRESS;
431 if(sasl->state == SASL_FINAL) {
432 if(code != sasl->params->finalcode)
433 result = CURLE_LOGIN_DENIED;
434 *progress = SASL_DONE;
435 state(sasl, conn, SASL_STOP);
439 if(sasl->state != SASL_CANCEL && sasl->state != SASL_OAUTH2_RESP &&
440 code != sasl->params->contcode) {
441 *progress = SASL_DONE;
442 state(sasl, conn, SASL_STOP);
443 return CURLE_LOGIN_DENIED;
446 switch(sasl->state) {
448 *progress = SASL_DONE;
451 result = Curl_auth_create_plain_message(data, conn->user, conn->passwd,
456 result = Curl_auth_create_login_message(data, conn->user, &resp, &len);
457 newstate = SASL_LOGIN_PASSWD;
459 case SASL_LOGIN_PASSWD:
460 result = Curl_auth_create_login_message(data, conn->passwd, &resp, &len);
463 result = Curl_auth_create_external_message(data, conn->user, &resp, &len);
466 #ifndef CURL_DISABLE_CRYPTO_AUTH
468 sasl->params->getmessage(data->state.buffer, &serverdata);
469 result = Curl_auth_decode_cram_md5_message(serverdata, &chlg, &chlglen);
471 result = Curl_auth_create_cram_md5_message(data, chlg, conn->user,
472 conn->passwd, &resp, &len);
476 sasl->params->getmessage(data->state.buffer, &serverdata);
477 result = Curl_auth_create_digest_md5_message(data, serverdata,
478 conn->user, conn->passwd,
481 newstate = SASL_DIGESTMD5_RESP;
483 case SASL_DIGESTMD5_RESP:
486 result = CURLE_OUT_OF_MEMORY;
492 /* Create the type-1 message */
493 result = Curl_auth_create_ntlm_type1_message(conn->user, conn->passwd,
494 &conn->ntlm, &resp, &len);
495 newstate = SASL_NTLM_TYPE2MSG;
497 case SASL_NTLM_TYPE2MSG:
498 /* Decode the type-2 message */
499 sasl->params->getmessage(data->state.buffer, &serverdata);
500 result = Curl_auth_decode_ntlm_type2_message(data, serverdata,
503 result = Curl_auth_create_ntlm_type3_message(data, conn->user,
504 conn->passwd, &conn->ntlm,
509 #if defined(USE_KERBEROS5)
511 result = Curl_auth_create_gssapi_user_message(data, conn->user,
514 data->easy_conn->host.name,
515 sasl->mutual_auth, NULL,
518 newstate = SASL_GSSAPI_TOKEN;
520 case SASL_GSSAPI_TOKEN:
521 sasl->params->getmessage(data->state.buffer, &serverdata);
522 if(sasl->mutual_auth) {
523 /* Decode the user token challenge and create the optional response
525 result = Curl_auth_create_gssapi_user_message(data, NULL, NULL,
528 serverdata, &conn->krb5,
530 newstate = SASL_GSSAPI_NO_DATA;
533 /* Decode the security challenge and create the response message */
534 result = Curl_auth_create_gssapi_security_message(data, serverdata,
538 case SASL_GSSAPI_NO_DATA:
539 sasl->params->getmessage(data->state.buffer, &serverdata);
540 /* Decode the security challenge and create the response message */
541 result = Curl_auth_create_gssapi_security_message(data, serverdata,
548 /* Create the authorisation message */
549 if(sasl->authused == SASL_MECH_OAUTHBEARER) {
550 result = Curl_auth_create_oauth_bearer_message(data, conn->user,
556 /* Failures maybe sent by the server as continuations for OAUTHBEARER */
557 newstate = SASL_OAUTH2_RESP;
560 result = Curl_auth_create_oauth_bearer_message(data, conn->user,
566 case SASL_OAUTH2_RESP:
567 /* The continuation is optional so check the response code */
568 if(code == sasl->params->finalcode) {
569 /* Final response was received so we are done */
570 *progress = SASL_DONE;
571 state(sasl, conn, SASL_STOP);
574 else if(code == sasl->params->contcode) {
575 /* Acknowledge the continuation by sending a 0x01 response base64
577 resp = strdup("AQ==");
579 result = CURLE_OUT_OF_MEMORY;
583 *progress = SASL_DONE;
584 state(sasl, conn, SASL_STOP);
585 return CURLE_LOGIN_DENIED;
589 /* Remove the offending mechanism from the supported list */
590 sasl->authmechs ^= sasl->authused;
592 /* Start an alternative SASL authentication */
593 result = Curl_sasl_start(sasl, conn, sasl->force_ir, progress);
594 newstate = sasl->state; /* Use state from Curl_sasl_start() */
597 failf(data, "Unsupported SASL authentication mechanism");
598 result = CURLE_UNSUPPORTED_PROTOCOL; /* Should not happen */
603 case CURLE_BAD_CONTENT_ENCODING:
605 result = sasl->params->sendcont(conn, "*");
606 newstate = SASL_CANCEL;
610 result = sasl->params->sendcont(conn, resp);
613 newstate = SASL_STOP; /* Stop on error */
614 *progress = SASL_DONE;
620 state(sasl, conn, newstate);