curl_sasl.c 24 KB

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