curl_sasl.c 24 KB

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