curl_sasl.c 23 KB

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