curl_sasl.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  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.
  13. *
  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.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. * RFC2195 CRAM-MD5 authentication
  24. * RFC2617 Basic and Digest Access Authentication
  25. * RFC2831 DIGEST-MD5 authentication
  26. * RFC4422 Simple Authentication and Security Layer (SASL)
  27. * RFC4616 PLAIN authentication
  28. * RFC5802 SCRAM-SHA-1 authentication
  29. * RFC7677 SCRAM-SHA-256 authentication
  30. * RFC6749 OAuth 2.0 Authorization Framework
  31. * RFC7628 A Set of SASL Mechanisms for OAuth
  32. * Draft LOGIN SASL Mechanism <draft-murchison-sasl-login-00.txt>
  33. *
  34. ***************************************************************************/
  35. #include "curl_setup.h"
  36. #if !defined(CURL_DISABLE_IMAP) || !defined(CURL_DISABLE_SMTP) || \
  37. !defined(CURL_DISABLE_POP3)
  38. #include <curl/curl.h>
  39. #include "urldata.h"
  40. #include "curl_base64.h"
  41. #include "curl_md5.h"
  42. #include "vauth/vauth.h"
  43. #include "vtls/vtls.h"
  44. #include "curl_hmac.h"
  45. #include "curl_sasl.h"
  46. #include "warnless.h"
  47. #include "strtok.h"
  48. #include "sendf.h"
  49. /* The last 3 #include files should be in this order */
  50. #include "curl_printf.h"
  51. #include "curl_memory.h"
  52. #include "memdebug.h"
  53. /* Supported mechanisms */
  54. static const struct {
  55. const char *name; /* Name */
  56. size_t len; /* Name length */
  57. unsigned short bit; /* Flag bit */
  58. } mechtable[] = {
  59. { "LOGIN", 5, SASL_MECH_LOGIN },
  60. { "PLAIN", 5, SASL_MECH_PLAIN },
  61. { "CRAM-MD5", 8, SASL_MECH_CRAM_MD5 },
  62. { "DIGEST-MD5", 10, SASL_MECH_DIGEST_MD5 },
  63. { "GSSAPI", 6, SASL_MECH_GSSAPI },
  64. { "EXTERNAL", 8, SASL_MECH_EXTERNAL },
  65. { "NTLM", 4, SASL_MECH_NTLM },
  66. { "XOAUTH2", 7, SASL_MECH_XOAUTH2 },
  67. { "OAUTHBEARER", 11, SASL_MECH_OAUTHBEARER },
  68. { "SCRAM-SHA-1", 11, SASL_MECH_SCRAM_SHA_1 },
  69. { "SCRAM-SHA-256",13, SASL_MECH_SCRAM_SHA_256 },
  70. { ZERO_NULL, 0, 0 }
  71. };
  72. /*
  73. * Curl_sasl_cleanup()
  74. *
  75. * This is used to cleanup any libraries or curl modules used by the sasl
  76. * functions.
  77. *
  78. * Parameters:
  79. *
  80. * conn [in] - The connection data.
  81. * authused [in] - The authentication mechanism used.
  82. */
  83. void Curl_sasl_cleanup(struct connectdata *conn, unsigned short authused)
  84. {
  85. (void)conn;
  86. (void)authused;
  87. #if defined(USE_KERBEROS5)
  88. /* Cleanup the gssapi structure */
  89. if(authused == SASL_MECH_GSSAPI) {
  90. Curl_auth_cleanup_gssapi(&conn->krb5);
  91. }
  92. #endif
  93. #if defined(USE_GSASL)
  94. /* Cleanup the GSASL structure */
  95. if(authused & (SASL_MECH_SCRAM_SHA_1 | SASL_MECH_SCRAM_SHA_256)) {
  96. Curl_auth_gsasl_cleanup(&conn->gsasl);
  97. }
  98. #endif
  99. #if defined(USE_NTLM)
  100. /* Cleanup the NTLM structure */
  101. if(authused == SASL_MECH_NTLM) {
  102. Curl_auth_cleanup_ntlm(&conn->ntlm);
  103. }
  104. #endif
  105. }
  106. /*
  107. * Curl_sasl_decode_mech()
  108. *
  109. * Convert a SASL mechanism name into a token.
  110. *
  111. * Parameters:
  112. *
  113. * ptr [in] - The mechanism string.
  114. * maxlen [in] - Maximum mechanism string length.
  115. * len [out] - If not NULL, effective name length.
  116. *
  117. * Returns the SASL mechanism token or 0 if no match.
  118. */
  119. unsigned short Curl_sasl_decode_mech(const char *ptr, size_t maxlen,
  120. size_t *len)
  121. {
  122. unsigned int i;
  123. char c;
  124. for(i = 0; mechtable[i].name; i++) {
  125. if(maxlen >= mechtable[i].len &&
  126. !memcmp(ptr, mechtable[i].name, mechtable[i].len)) {
  127. if(len)
  128. *len = mechtable[i].len;
  129. if(maxlen == mechtable[i].len)
  130. return mechtable[i].bit;
  131. c = ptr[mechtable[i].len];
  132. if(!ISUPPER(c) && !ISDIGIT(c) && c != '-' && c != '_')
  133. return mechtable[i].bit;
  134. }
  135. }
  136. return 0;
  137. }
  138. /*
  139. * Curl_sasl_parse_url_auth_option()
  140. *
  141. * Parse the URL login options.
  142. */
  143. CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
  144. const char *value, size_t len)
  145. {
  146. CURLcode result = CURLE_OK;
  147. size_t mechlen;
  148. if(!len)
  149. return CURLE_URL_MALFORMAT;
  150. if(sasl->resetprefs) {
  151. sasl->resetprefs = FALSE;
  152. sasl->prefmech = SASL_AUTH_NONE;
  153. }
  154. if(!strncmp(value, "*", len))
  155. sasl->prefmech = SASL_AUTH_DEFAULT;
  156. else {
  157. unsigned short mechbit = Curl_sasl_decode_mech(value, len, &mechlen);
  158. if(mechbit && mechlen == len)
  159. sasl->prefmech |= mechbit;
  160. else
  161. result = CURLE_URL_MALFORMAT;
  162. }
  163. return result;
  164. }
  165. /*
  166. * Curl_sasl_init()
  167. *
  168. * Initializes the SASL structure.
  169. */
  170. void Curl_sasl_init(struct SASL *sasl, struct Curl_easy *data,
  171. const struct SASLproto *params)
  172. {
  173. unsigned long auth = data->set.httpauth;
  174. sasl->params = params; /* Set protocol dependent parameters */
  175. sasl->state = SASL_STOP; /* Not yet running */
  176. sasl->curmech = NULL; /* No mechanism yet. */
  177. sasl->authmechs = SASL_AUTH_NONE; /* No known authentication mechanism yet */
  178. sasl->prefmech = params->defmechs; /* Default preferred mechanisms */
  179. sasl->authused = SASL_AUTH_NONE; /* The authentication mechanism used */
  180. sasl->resetprefs = TRUE; /* Reset prefmech upon AUTH parsing. */
  181. sasl->mutual_auth = FALSE; /* No mutual authentication (GSSAPI only) */
  182. sasl->force_ir = FALSE; /* Respect external option */
  183. if(auth != CURLAUTH_BASIC) {
  184. sasl->resetprefs = FALSE;
  185. sasl->prefmech = SASL_AUTH_NONE;
  186. if(auth & CURLAUTH_BASIC)
  187. sasl->prefmech |= SASL_MECH_PLAIN | SASL_MECH_LOGIN;
  188. if(auth & CURLAUTH_DIGEST)
  189. sasl->prefmech |= SASL_MECH_DIGEST_MD5;
  190. if(auth & CURLAUTH_NTLM)
  191. sasl->prefmech |= SASL_MECH_NTLM;
  192. if(auth & CURLAUTH_BEARER)
  193. sasl->prefmech |= SASL_MECH_OAUTHBEARER | SASL_MECH_XOAUTH2;
  194. if(auth & CURLAUTH_GSSAPI)
  195. sasl->prefmech |= SASL_MECH_GSSAPI;
  196. }
  197. }
  198. /*
  199. * state()
  200. *
  201. * This is the ONLY way to change SASL state!
  202. */
  203. static void state(struct SASL *sasl, struct Curl_easy *data,
  204. saslstate newstate)
  205. {
  206. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  207. /* for debug purposes */
  208. static const char * const names[]={
  209. "STOP",
  210. "PLAIN",
  211. "LOGIN",
  212. "LOGIN_PASSWD",
  213. "EXTERNAL",
  214. "CRAMMD5",
  215. "DIGESTMD5",
  216. "DIGESTMD5_RESP",
  217. "NTLM",
  218. "NTLM_TYPE2MSG",
  219. "GSSAPI",
  220. "GSSAPI_TOKEN",
  221. "GSSAPI_NO_DATA",
  222. "OAUTH2",
  223. "OAUTH2_RESP",
  224. "GSASL",
  225. "CANCEL",
  226. "FINAL",
  227. /* LAST */
  228. };
  229. if(sasl->state != newstate)
  230. infof(data, "SASL %p state change from %s to %s",
  231. (void *)sasl, names[sasl->state], names[newstate]);
  232. #else
  233. (void) data;
  234. #endif
  235. sasl->state = newstate;
  236. }
  237. /* Get the SASL server message and convert it to binary. */
  238. static CURLcode get_server_message(struct SASL *sasl, struct Curl_easy *data,
  239. struct bufref *out)
  240. {
  241. CURLcode result = CURLE_OK;
  242. result = sasl->params->getmessage(data, out);
  243. if(!result && (sasl->params->flags & SASL_FLAG_BASE64)) {
  244. unsigned char *msg;
  245. size_t msglen;
  246. const char *serverdata = (const char *) Curl_bufref_ptr(out);
  247. if(!*serverdata || *serverdata == '=')
  248. Curl_bufref_set(out, NULL, 0, NULL);
  249. else {
  250. result = Curl_base64_decode(serverdata, &msg, &msglen);
  251. if(!result)
  252. Curl_bufref_set(out, msg, msglen, curl_free);
  253. }
  254. }
  255. return result;
  256. }
  257. /* Encode the outgoing SASL message. */
  258. static CURLcode build_message(struct SASL *sasl, struct bufref *msg)
  259. {
  260. CURLcode result = CURLE_OK;
  261. if(sasl->params->flags & SASL_FLAG_BASE64) {
  262. if(!Curl_bufref_ptr(msg)) /* Empty message. */
  263. Curl_bufref_set(msg, "", 0, NULL);
  264. else if(!Curl_bufref_len(msg)) /* Explicit empty response. */
  265. Curl_bufref_set(msg, "=", 1, NULL);
  266. else {
  267. char *base64;
  268. size_t base64len;
  269. result = Curl_base64_encode((const char *) Curl_bufref_ptr(msg),
  270. Curl_bufref_len(msg), &base64, &base64len);
  271. if(!result)
  272. Curl_bufref_set(msg, base64, base64len, curl_free);
  273. }
  274. }
  275. return result;
  276. }
  277. /*
  278. * Curl_sasl_can_authenticate()
  279. *
  280. * Check if we have enough auth data and capabilities to authenticate.
  281. */
  282. bool Curl_sasl_can_authenticate(struct SASL *sasl, struct Curl_easy *data)
  283. {
  284. /* Have credentials been provided? */
  285. if(data->state.aptr.user)
  286. return TRUE;
  287. /* EXTERNAL can authenticate without a user name and/or password */
  288. if(sasl->authmechs & sasl->prefmech & SASL_MECH_EXTERNAL)
  289. return TRUE;
  290. return FALSE;
  291. }
  292. /*
  293. * Curl_sasl_start()
  294. *
  295. * Calculate the required login details for SASL authentication.
  296. */
  297. CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
  298. bool force_ir, saslprogress *progress)
  299. {
  300. CURLcode result = CURLE_OK;
  301. struct connectdata *conn = data->conn;
  302. unsigned short enabledmechs;
  303. const char *mech = NULL;
  304. struct bufref resp;
  305. saslstate state1 = SASL_STOP;
  306. saslstate state2 = SASL_FINAL;
  307. const char * const hostname = SSL_HOST_NAME();
  308. const long int port = SSL_HOST_PORT();
  309. #if defined(USE_KERBEROS5) || defined(USE_NTLM)
  310. const char *service = data->set.str[STRING_SERVICE_NAME] ?
  311. data->set.str[STRING_SERVICE_NAME] :
  312. sasl->params->service;
  313. #endif
  314. const char *oauth_bearer = data->set.str[STRING_BEARER];
  315. struct bufref nullmsg;
  316. Curl_bufref_init(&nullmsg);
  317. Curl_bufref_init(&resp);
  318. sasl->force_ir = force_ir; /* Latch for future use */
  319. sasl->authused = 0; /* No mechanism used yet */
  320. enabledmechs = sasl->authmechs & sasl->prefmech;
  321. *progress = SASL_IDLE;
  322. /* Calculate the supported authentication mechanism, by decreasing order of
  323. security, as well as the initial response where appropriate */
  324. if((enabledmechs & SASL_MECH_EXTERNAL) && !conn->passwd[0]) {
  325. mech = SASL_MECH_STRING_EXTERNAL;
  326. state1 = SASL_EXTERNAL;
  327. sasl->authused = SASL_MECH_EXTERNAL;
  328. if(force_ir || data->set.sasl_ir)
  329. result = Curl_auth_create_external_message(conn->user, &resp);
  330. }
  331. else if(data->state.aptr.user) {
  332. #if defined(USE_KERBEROS5)
  333. if((enabledmechs & SASL_MECH_GSSAPI) && Curl_auth_is_gssapi_supported() &&
  334. Curl_auth_user_contains_domain(conn->user)) {
  335. sasl->mutual_auth = FALSE;
  336. mech = SASL_MECH_STRING_GSSAPI;
  337. state1 = SASL_GSSAPI;
  338. state2 = SASL_GSSAPI_TOKEN;
  339. sasl->authused = SASL_MECH_GSSAPI;
  340. if(force_ir || data->set.sasl_ir)
  341. result = Curl_auth_create_gssapi_user_message(data, conn->user,
  342. conn->passwd,
  343. service,
  344. conn->host.name,
  345. sasl->mutual_auth,
  346. NULL, &conn->krb5,
  347. &resp);
  348. }
  349. else
  350. #endif
  351. #ifdef USE_GSASL
  352. if((enabledmechs & SASL_MECH_SCRAM_SHA_256) &&
  353. Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_256,
  354. &conn->gsasl)) {
  355. mech = SASL_MECH_STRING_SCRAM_SHA_256;
  356. sasl->authused = SASL_MECH_SCRAM_SHA_256;
  357. state1 = SASL_GSASL;
  358. state2 = SASL_GSASL;
  359. result = Curl_auth_gsasl_start(data, conn->user,
  360. conn->passwd, &conn->gsasl);
  361. if(result == CURLE_OK && (force_ir || data->set.sasl_ir))
  362. result = Curl_auth_gsasl_token(data, &nullmsg, &conn->gsasl, &resp);
  363. }
  364. else if((enabledmechs & SASL_MECH_SCRAM_SHA_1) &&
  365. Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_1,
  366. &conn->gsasl)) {
  367. mech = SASL_MECH_STRING_SCRAM_SHA_1;
  368. sasl->authused = SASL_MECH_SCRAM_SHA_1;
  369. state1 = SASL_GSASL;
  370. state2 = SASL_GSASL;
  371. result = Curl_auth_gsasl_start(data, conn->user,
  372. conn->passwd, &conn->gsasl);
  373. if(result == CURLE_OK && (force_ir || data->set.sasl_ir))
  374. result = Curl_auth_gsasl_token(data, &nullmsg, &conn->gsasl, &resp);
  375. }
  376. else
  377. #endif
  378. #ifndef CURL_DISABLE_CRYPTO_AUTH
  379. if((enabledmechs & SASL_MECH_DIGEST_MD5) &&
  380. Curl_auth_is_digest_supported()) {
  381. mech = SASL_MECH_STRING_DIGEST_MD5;
  382. state1 = SASL_DIGESTMD5;
  383. sasl->authused = SASL_MECH_DIGEST_MD5;
  384. }
  385. else if(enabledmechs & SASL_MECH_CRAM_MD5) {
  386. mech = SASL_MECH_STRING_CRAM_MD5;
  387. state1 = SASL_CRAMMD5;
  388. sasl->authused = SASL_MECH_CRAM_MD5;
  389. }
  390. else
  391. #endif
  392. #ifdef USE_NTLM
  393. if((enabledmechs & SASL_MECH_NTLM) && Curl_auth_is_ntlm_supported()) {
  394. mech = SASL_MECH_STRING_NTLM;
  395. state1 = SASL_NTLM;
  396. state2 = SASL_NTLM_TYPE2MSG;
  397. sasl->authused = SASL_MECH_NTLM;
  398. if(force_ir || data->set.sasl_ir)
  399. result = Curl_auth_create_ntlm_type1_message(data,
  400. conn->user, conn->passwd,
  401. service,
  402. hostname,
  403. &conn->ntlm, &resp);
  404. }
  405. else
  406. #endif
  407. if((enabledmechs & SASL_MECH_OAUTHBEARER) && oauth_bearer) {
  408. mech = SASL_MECH_STRING_OAUTHBEARER;
  409. state1 = SASL_OAUTH2;
  410. state2 = SASL_OAUTH2_RESP;
  411. sasl->authused = SASL_MECH_OAUTHBEARER;
  412. if(force_ir || data->set.sasl_ir)
  413. result = Curl_auth_create_oauth_bearer_message(conn->user,
  414. hostname,
  415. port,
  416. oauth_bearer,
  417. &resp);
  418. }
  419. else if((enabledmechs & SASL_MECH_XOAUTH2) && oauth_bearer) {
  420. mech = SASL_MECH_STRING_XOAUTH2;
  421. state1 = SASL_OAUTH2;
  422. sasl->authused = SASL_MECH_XOAUTH2;
  423. if(force_ir || data->set.sasl_ir)
  424. result = Curl_auth_create_xoauth_bearer_message(conn->user,
  425. oauth_bearer,
  426. &resp);
  427. }
  428. else if(enabledmechs & SASL_MECH_PLAIN) {
  429. mech = SASL_MECH_STRING_PLAIN;
  430. state1 = SASL_PLAIN;
  431. sasl->authused = SASL_MECH_PLAIN;
  432. if(force_ir || data->set.sasl_ir)
  433. result = Curl_auth_create_plain_message(conn->sasl_authzid,
  434. conn->user, conn->passwd,
  435. &resp);
  436. }
  437. else if(enabledmechs & SASL_MECH_LOGIN) {
  438. mech = SASL_MECH_STRING_LOGIN;
  439. state1 = SASL_LOGIN;
  440. state2 = SASL_LOGIN_PASSWD;
  441. sasl->authused = SASL_MECH_LOGIN;
  442. if(force_ir || data->set.sasl_ir)
  443. result = Curl_auth_create_login_message(conn->user, &resp);
  444. }
  445. }
  446. if(!result && mech) {
  447. sasl->curmech = mech;
  448. if(Curl_bufref_ptr(&resp))
  449. result = build_message(sasl, &resp);
  450. if(sasl->params->maxirlen &&
  451. strlen(mech) + Curl_bufref_len(&resp) > sasl->params->maxirlen)
  452. Curl_bufref_free(&resp);
  453. if(!result)
  454. result = sasl->params->sendauth(data, mech, &resp);
  455. if(!result) {
  456. *progress = SASL_INPROGRESS;
  457. state(sasl, data, Curl_bufref_ptr(&resp) ? state2 : state1);
  458. }
  459. }
  460. Curl_bufref_free(&resp);
  461. return result;
  462. }
  463. /*
  464. * Curl_sasl_continue()
  465. *
  466. * Continue the authentication.
  467. */
  468. CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
  469. int code, saslprogress *progress)
  470. {
  471. CURLcode result = CURLE_OK;
  472. struct connectdata *conn = data->conn;
  473. saslstate newstate = SASL_FINAL;
  474. struct bufref resp;
  475. const char * const hostname = SSL_HOST_NAME();
  476. const long int port = SSL_HOST_PORT();
  477. #if !defined(CURL_DISABLE_CRYPTO_AUTH) || defined(USE_KERBEROS5) || \
  478. defined(USE_NTLM)
  479. const char *service = data->set.str[STRING_SERVICE_NAME] ?
  480. data->set.str[STRING_SERVICE_NAME] :
  481. sasl->params->service;
  482. #endif
  483. const char *oauth_bearer = data->set.str[STRING_BEARER];
  484. struct bufref serverdata;
  485. Curl_bufref_init(&serverdata);
  486. Curl_bufref_init(&resp);
  487. *progress = SASL_INPROGRESS;
  488. if(sasl->state == SASL_FINAL) {
  489. if(code != sasl->params->finalcode)
  490. result = CURLE_LOGIN_DENIED;
  491. *progress = SASL_DONE;
  492. state(sasl, data, SASL_STOP);
  493. return result;
  494. }
  495. if(sasl->state != SASL_CANCEL && sasl->state != SASL_OAUTH2_RESP &&
  496. code != sasl->params->contcode) {
  497. *progress = SASL_DONE;
  498. state(sasl, data, SASL_STOP);
  499. return CURLE_LOGIN_DENIED;
  500. }
  501. switch(sasl->state) {
  502. case SASL_STOP:
  503. *progress = SASL_DONE;
  504. return result;
  505. case SASL_PLAIN:
  506. result = Curl_auth_create_plain_message(conn->sasl_authzid,
  507. conn->user, conn->passwd, &resp);
  508. break;
  509. case SASL_LOGIN:
  510. result = Curl_auth_create_login_message(conn->user, &resp);
  511. newstate = SASL_LOGIN_PASSWD;
  512. break;
  513. case SASL_LOGIN_PASSWD:
  514. result = Curl_auth_create_login_message(conn->passwd, &resp);
  515. break;
  516. case SASL_EXTERNAL:
  517. result = Curl_auth_create_external_message(conn->user, &resp);
  518. break;
  519. #ifndef CURL_DISABLE_CRYPTO_AUTH
  520. #ifdef USE_GSASL
  521. case SASL_GSASL:
  522. result = get_server_message(sasl, data, &serverdata);
  523. if(!result)
  524. result = Curl_auth_gsasl_token(data, &serverdata, &conn->gsasl, &resp);
  525. if(!result && Curl_bufref_len(&resp) > 0)
  526. newstate = SASL_GSASL;
  527. break;
  528. #endif
  529. case SASL_CRAMMD5:
  530. result = get_server_message(sasl, data, &serverdata);
  531. if(!result)
  532. result = Curl_auth_create_cram_md5_message(&serverdata, conn->user,
  533. conn->passwd, &resp);
  534. break;
  535. case SASL_DIGESTMD5:
  536. result = get_server_message(sasl, data, &serverdata);
  537. if(!result)
  538. result = Curl_auth_create_digest_md5_message(data, &serverdata,
  539. conn->user, conn->passwd,
  540. service, &resp);
  541. if(!result && (sasl->params->flags & SASL_FLAG_BASE64))
  542. newstate = SASL_DIGESTMD5_RESP;
  543. break;
  544. case SASL_DIGESTMD5_RESP:
  545. /* Keep response NULL to output an empty line. */
  546. break;
  547. #endif
  548. #ifdef USE_NTLM
  549. case SASL_NTLM:
  550. /* Create the type-1 message */
  551. result = Curl_auth_create_ntlm_type1_message(data,
  552. conn->user, conn->passwd,
  553. service, hostname,
  554. &conn->ntlm, &resp);
  555. newstate = SASL_NTLM_TYPE2MSG;
  556. break;
  557. case SASL_NTLM_TYPE2MSG:
  558. /* Decode the type-2 message */
  559. result = get_server_message(sasl, data, &serverdata);
  560. if(!result)
  561. result = Curl_auth_decode_ntlm_type2_message(data, &serverdata,
  562. &conn->ntlm);
  563. if(!result)
  564. result = Curl_auth_create_ntlm_type3_message(data, conn->user,
  565. conn->passwd, &conn->ntlm,
  566. &resp);
  567. break;
  568. #endif
  569. #if defined(USE_KERBEROS5)
  570. case SASL_GSSAPI:
  571. result = Curl_auth_create_gssapi_user_message(data, conn->user,
  572. conn->passwd,
  573. service,
  574. conn->host.name,
  575. sasl->mutual_auth, NULL,
  576. &conn->krb5,
  577. &resp);
  578. newstate = SASL_GSSAPI_TOKEN;
  579. break;
  580. case SASL_GSSAPI_TOKEN:
  581. result = get_server_message(sasl, data, &serverdata);
  582. if(!result) {
  583. if(sasl->mutual_auth) {
  584. /* Decode the user token challenge and create the optional response
  585. message */
  586. result = Curl_auth_create_gssapi_user_message(data, NULL, NULL,
  587. NULL, NULL,
  588. sasl->mutual_auth,
  589. &serverdata,
  590. &conn->krb5,
  591. &resp);
  592. newstate = SASL_GSSAPI_NO_DATA;
  593. }
  594. else
  595. /* Decode the security challenge and create the response message */
  596. result = Curl_auth_create_gssapi_security_message(data,
  597. conn->sasl_authzid,
  598. &serverdata,
  599. &conn->krb5,
  600. &resp);
  601. }
  602. break;
  603. case SASL_GSSAPI_NO_DATA:
  604. /* Decode the security challenge and create the response message */
  605. result = get_server_message(sasl, data, &serverdata);
  606. if(!result)
  607. result = Curl_auth_create_gssapi_security_message(data,
  608. conn->sasl_authzid,
  609. &serverdata,
  610. &conn->krb5,
  611. &resp);
  612. break;
  613. #endif
  614. case SASL_OAUTH2:
  615. /* Create the authorization message */
  616. if(sasl->authused == SASL_MECH_OAUTHBEARER) {
  617. result = Curl_auth_create_oauth_bearer_message(conn->user,
  618. hostname,
  619. port,
  620. oauth_bearer,
  621. &resp);
  622. /* Failures maybe sent by the server as continuations for OAUTHBEARER */
  623. newstate = SASL_OAUTH2_RESP;
  624. }
  625. else
  626. result = Curl_auth_create_xoauth_bearer_message(conn->user,
  627. oauth_bearer,
  628. &resp);
  629. break;
  630. case SASL_OAUTH2_RESP:
  631. /* The continuation is optional so check the response code */
  632. if(code == sasl->params->finalcode) {
  633. /* Final response was received so we are done */
  634. *progress = SASL_DONE;
  635. state(sasl, data, SASL_STOP);
  636. return result;
  637. }
  638. else if(code == sasl->params->contcode) {
  639. /* Acknowledge the continuation by sending a 0x01 response. */
  640. Curl_bufref_set(&resp, "\x01", 1, NULL);
  641. break;
  642. }
  643. else {
  644. *progress = SASL_DONE;
  645. state(sasl, data, SASL_STOP);
  646. return CURLE_LOGIN_DENIED;
  647. }
  648. case SASL_CANCEL:
  649. /* Remove the offending mechanism from the supported list */
  650. sasl->authmechs ^= sasl->authused;
  651. /* Start an alternative SASL authentication */
  652. return Curl_sasl_start(sasl, data, sasl->force_ir, progress);
  653. default:
  654. failf(data, "Unsupported SASL authentication mechanism");
  655. result = CURLE_UNSUPPORTED_PROTOCOL; /* Should not happen */
  656. break;
  657. }
  658. Curl_bufref_free(&serverdata);
  659. switch(result) {
  660. case CURLE_BAD_CONTENT_ENCODING:
  661. /* Cancel dialog */
  662. result = sasl->params->cancelauth(data, sasl->curmech);
  663. newstate = SASL_CANCEL;
  664. break;
  665. case CURLE_OK:
  666. result = build_message(sasl, &resp);
  667. if(!result)
  668. result = sasl->params->contauth(data, sasl->curmech, &resp);
  669. break;
  670. default:
  671. newstate = SASL_STOP; /* Stop on error */
  672. *progress = SASL_DONE;
  673. break;
  674. }
  675. Curl_bufref_free(&resp);
  676. state(sasl, data, newstate);
  677. return result;
  678. }
  679. #endif /* protocols are enabled that use SASL */