curl_sasl.c 24 KB

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