curl_sasl.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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. (!defined(CURL_DISABLE_LDAP) && defined(USE_OPENLDAP))
  39. #include <curl/curl.h>
  40. #include "urldata.h"
  41. #include "curlx/base64.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 "curlx/warnless.h"
  48. #include "sendf.h"
  49. /* The last 2 #include files should be in this order */
  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_decode_mech()
  73. *
  74. * Convert a SASL mechanism name into a token.
  75. *
  76. * Parameters:
  77. *
  78. * ptr [in] - The mechanism string.
  79. * maxlen [in] - Maximum mechanism string length.
  80. * len [out] - If not NULL, effective name length.
  81. *
  82. * Returns the SASL mechanism token or 0 if no match.
  83. */
  84. unsigned short Curl_sasl_decode_mech(const char *ptr, size_t maxlen,
  85. size_t *len)
  86. {
  87. unsigned int i;
  88. char c;
  89. for(i = 0; mechtable[i].name; i++) {
  90. if(maxlen >= mechtable[i].len &&
  91. !memcmp(ptr, mechtable[i].name, mechtable[i].len)) {
  92. if(len)
  93. *len = mechtable[i].len;
  94. if(maxlen == mechtable[i].len)
  95. return mechtable[i].bit;
  96. c = ptr[mechtable[i].len];
  97. if(!ISUPPER(c) && !ISDIGIT(c) && c != '-' && c != '_')
  98. return mechtable[i].bit;
  99. }
  100. }
  101. return 0;
  102. }
  103. /*
  104. * Curl_sasl_parse_url_auth_option()
  105. *
  106. * Parse the URL login options.
  107. */
  108. CURLcode Curl_sasl_parse_url_auth_option(struct SASL *sasl,
  109. const char *value, size_t len)
  110. {
  111. CURLcode result = CURLE_OK;
  112. size_t mechlen;
  113. if(!len)
  114. return CURLE_URL_MALFORMAT;
  115. if(sasl->resetprefs) {
  116. sasl->resetprefs = FALSE;
  117. sasl->prefmech = SASL_AUTH_NONE;
  118. }
  119. if(!strncmp(value, "*", len))
  120. sasl->prefmech = SASL_AUTH_DEFAULT;
  121. else {
  122. unsigned short mechbit = Curl_sasl_decode_mech(value, len, &mechlen);
  123. if(mechbit && mechlen == len)
  124. sasl->prefmech |= mechbit;
  125. else
  126. result = CURLE_URL_MALFORMAT;
  127. }
  128. return result;
  129. }
  130. /*
  131. * Curl_sasl_init()
  132. *
  133. * Initializes the SASL structure.
  134. */
  135. void Curl_sasl_init(struct SASL *sasl, struct Curl_easy *data,
  136. const struct SASLproto *params)
  137. {
  138. unsigned long auth = data->set.httpauth;
  139. sasl->params = params; /* Set protocol dependent parameters */
  140. sasl->state = SASL_STOP; /* Not yet running */
  141. sasl->curmech = NULL; /* No mechanism yet. */
  142. sasl->authmechs = SASL_AUTH_NONE; /* No known authentication mechanism yet */
  143. sasl->prefmech = params->defmechs; /* Default preferred mechanisms */
  144. sasl->authused = SASL_AUTH_NONE; /* The authentication mechanism used */
  145. sasl->resetprefs = TRUE; /* Reset prefmech upon AUTH parsing. */
  146. sasl->mutual_auth = FALSE; /* No mutual authentication (GSSAPI only) */
  147. sasl->force_ir = FALSE; /* Respect external option */
  148. if(auth != CURLAUTH_BASIC) {
  149. unsigned short mechs = SASL_AUTH_NONE;
  150. /* If some usable http authentication options have been set, determine
  151. new defaults from them. */
  152. if(auth & CURLAUTH_BASIC)
  153. mechs |= SASL_MECH_PLAIN | SASL_MECH_LOGIN;
  154. if(auth & CURLAUTH_DIGEST)
  155. mechs |= SASL_MECH_DIGEST_MD5;
  156. if(auth & CURLAUTH_NTLM)
  157. mechs |= SASL_MECH_NTLM;
  158. if(auth & CURLAUTH_BEARER)
  159. mechs |= SASL_MECH_OAUTHBEARER | SASL_MECH_XOAUTH2;
  160. if(auth & CURLAUTH_GSSAPI)
  161. mechs |= SASL_MECH_GSSAPI;
  162. if(mechs != SASL_AUTH_NONE)
  163. sasl->prefmech = mechs;
  164. }
  165. }
  166. /*
  167. * sasl_state()
  168. *
  169. * This is the ONLY way to change SASL state!
  170. */
  171. static void sasl_state(struct SASL *sasl, struct Curl_easy *data,
  172. saslstate newstate)
  173. {
  174. #if defined(DEBUGBUILD) && !defined(CURL_DISABLE_VERBOSE_STRINGS)
  175. /* for debug purposes */
  176. static const char * const names[]={
  177. "STOP",
  178. "PLAIN",
  179. "LOGIN",
  180. "LOGIN_PASSWD",
  181. "EXTERNAL",
  182. "CRAMMD5",
  183. "DIGESTMD5",
  184. "DIGESTMD5_RESP",
  185. "NTLM",
  186. "NTLM_TYPE2MSG",
  187. "GSSAPI",
  188. "GSSAPI_TOKEN",
  189. "GSSAPI_NO_DATA",
  190. "OAUTH2",
  191. "OAUTH2_RESP",
  192. "GSASL",
  193. "CANCEL",
  194. "FINAL",
  195. /* LAST */
  196. };
  197. if(sasl->state != newstate)
  198. infof(data, "SASL %p state change from %s to %s",
  199. (void *)sasl, names[sasl->state], names[newstate]);
  200. #else
  201. (void)data;
  202. #endif
  203. sasl->state = newstate;
  204. }
  205. #if defined(USE_NTLM) || defined(USE_GSASL) || defined(USE_KERBEROS5) || \
  206. !defined(CURL_DISABLE_DIGEST_AUTH)
  207. /* Get the SASL server message and convert it to binary. */
  208. static CURLcode get_server_message(struct SASL *sasl, struct Curl_easy *data,
  209. struct bufref *out)
  210. {
  211. CURLcode result = CURLE_OK;
  212. result = sasl->params->getmessage(data, out);
  213. if(!result && (sasl->params->flags & SASL_FLAG_BASE64)) {
  214. unsigned char *msg;
  215. size_t msglen;
  216. const char *serverdata = (const char *) Curl_bufref_ptr(out);
  217. if(!*serverdata || *serverdata == '=')
  218. Curl_bufref_set(out, NULL, 0, NULL);
  219. else {
  220. result = curlx_base64_decode(serverdata, &msg, &msglen);
  221. if(!result)
  222. Curl_bufref_set(out, msg, msglen, curl_free);
  223. }
  224. }
  225. return result;
  226. }
  227. #endif
  228. /* Encode the outgoing SASL message. */
  229. static CURLcode build_message(struct SASL *sasl, struct bufref *msg)
  230. {
  231. CURLcode result = CURLE_OK;
  232. if(sasl->params->flags & SASL_FLAG_BASE64) {
  233. if(!Curl_bufref_ptr(msg)) /* Empty message. */
  234. Curl_bufref_set(msg, "", 0, NULL);
  235. else if(!Curl_bufref_len(msg)) /* Explicit empty response. */
  236. Curl_bufref_set(msg, "=", 1, NULL);
  237. else {
  238. char *base64;
  239. size_t base64len;
  240. result = curlx_base64_encode((const char *) Curl_bufref_ptr(msg),
  241. Curl_bufref_len(msg), &base64, &base64len);
  242. if(!result)
  243. Curl_bufref_set(msg, base64, base64len, curl_free);
  244. }
  245. }
  246. return result;
  247. }
  248. /*
  249. * Curl_sasl_can_authenticate()
  250. *
  251. * Check if we have enough auth data and capabilities to authenticate.
  252. */
  253. bool Curl_sasl_can_authenticate(struct SASL *sasl, struct Curl_easy *data)
  254. {
  255. /* Have credentials been provided? */
  256. if(data->state.aptr.user)
  257. return TRUE;
  258. /* EXTERNAL can authenticate without a username and/or password */
  259. if(sasl->authmechs & sasl->prefmech & SASL_MECH_EXTERNAL)
  260. return TRUE;
  261. return FALSE;
  262. }
  263. struct sasl_ctx {
  264. struct SASL *sasl;
  265. struct connectdata *conn;
  266. const char *user;
  267. unsigned short enabledmechs;
  268. const char *mech;
  269. saslstate state1;
  270. saslstate state2;
  271. struct bufref resp;
  272. CURLcode result;
  273. };
  274. static bool sasl_choose_external(struct Curl_easy *data, struct sasl_ctx *sctx)
  275. {
  276. if((sctx->enabledmechs & SASL_MECH_EXTERNAL) && !sctx->conn->passwd[0]) {
  277. sctx->mech = SASL_MECH_STRING_EXTERNAL;
  278. sctx->state1 = SASL_EXTERNAL;
  279. sctx->sasl->authused = SASL_MECH_EXTERNAL;
  280. if(sctx->sasl->force_ir || data->set.sasl_ir)
  281. Curl_auth_create_external_message(sctx->conn->user, &sctx->resp);
  282. return TRUE;
  283. }
  284. return FALSE;
  285. }
  286. #ifdef USE_KERBEROS5
  287. static bool sasl_choose_krb5(struct Curl_easy *data, struct sasl_ctx *sctx)
  288. {
  289. if(sctx->user &&
  290. (sctx->enabledmechs & SASL_MECH_GSSAPI) &&
  291. Curl_auth_is_gssapi_supported() &&
  292. Curl_auth_user_contains_domain(sctx->conn->user)) {
  293. const char *service = data->set.str[STRING_SERVICE_NAME] ?
  294. data->set.str[STRING_SERVICE_NAME] :
  295. sctx->sasl->params->service;
  296. sctx->sasl->mutual_auth = FALSE;
  297. sctx->mech = SASL_MECH_STRING_GSSAPI;
  298. sctx->state1 = SASL_GSSAPI;
  299. sctx->state2 = SASL_GSSAPI_TOKEN;
  300. sctx->sasl->authused = SASL_MECH_GSSAPI;
  301. if(sctx->sasl->force_ir || data->set.sasl_ir) {
  302. struct kerberos5data *krb5 = Curl_auth_krb5_get(sctx->conn);
  303. sctx->result = !krb5 ? CURLE_OUT_OF_MEMORY :
  304. Curl_auth_create_gssapi_user_message(data, sctx->conn->user,
  305. sctx->conn->passwd,
  306. service, sctx->conn->host.name,
  307. sctx->sasl->mutual_auth, NULL,
  308. krb5, &sctx->resp);
  309. }
  310. return TRUE;
  311. }
  312. return FALSE;
  313. }
  314. #endif /* USE_KERBEROS5 */
  315. #ifdef USE_GSASL
  316. static bool sasl_choose_gsasl(struct Curl_easy *data, struct sasl_ctx *sctx)
  317. {
  318. struct gsasldata *gsasl;
  319. struct bufref nullmsg;
  320. if(sctx->user &&
  321. (sctx->enabledmechs & (SASL_MECH_SCRAM_SHA_256|SASL_MECH_SCRAM_SHA_1))) {
  322. gsasl = Curl_auth_gsasl_get(sctx->conn);
  323. if(!gsasl) {
  324. sctx->result = CURLE_OUT_OF_MEMORY;
  325. return TRUE; /* attempted, but failed */
  326. }
  327. if((sctx->enabledmechs & SASL_MECH_SCRAM_SHA_256) &&
  328. Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_256,
  329. gsasl)) {
  330. sctx->mech = SASL_MECH_STRING_SCRAM_SHA_256;
  331. sctx->sasl->authused = SASL_MECH_SCRAM_SHA_256;
  332. }
  333. else if((sctx->enabledmechs & SASL_MECH_SCRAM_SHA_1) &&
  334. Curl_auth_gsasl_is_supported(data, SASL_MECH_STRING_SCRAM_SHA_1,
  335. gsasl)) {
  336. sctx->mech = SASL_MECH_STRING_SCRAM_SHA_1;
  337. sctx->sasl->authused = SASL_MECH_SCRAM_SHA_1;
  338. }
  339. else
  340. return FALSE;
  341. Curl_bufref_init(&nullmsg);
  342. sctx->state1 = SASL_GSASL;
  343. sctx->state2 = SASL_GSASL;
  344. sctx->result = Curl_auth_gsasl_start(data, sctx->conn->user,
  345. sctx->conn->passwd, gsasl);
  346. if(!sctx->result && (sctx->sasl->force_ir || data->set.sasl_ir))
  347. sctx->result = Curl_auth_gsasl_token(data, &nullmsg, gsasl, &sctx->resp);
  348. return TRUE;
  349. }
  350. return FALSE;
  351. }
  352. #endif /* USE_GSASL */
  353. #ifndef CURL_DISABLE_DIGEST_AUTH
  354. static bool sasl_choose_digest(struct Curl_easy *data, struct sasl_ctx *sctx)
  355. {
  356. (void)data;
  357. if(!sctx->user)
  358. return FALSE;
  359. else if((sctx->enabledmechs & SASL_MECH_DIGEST_MD5) &&
  360. Curl_auth_is_digest_supported()) {
  361. sctx->mech = SASL_MECH_STRING_DIGEST_MD5;
  362. sctx->state1 = SASL_DIGESTMD5;
  363. sctx->sasl->authused = SASL_MECH_DIGEST_MD5;
  364. return TRUE;
  365. }
  366. else if(sctx->enabledmechs & SASL_MECH_CRAM_MD5) {
  367. sctx->mech = SASL_MECH_STRING_CRAM_MD5;
  368. sctx->state1 = SASL_CRAMMD5;
  369. sctx->sasl->authused = SASL_MECH_CRAM_MD5;
  370. return TRUE;
  371. }
  372. return FALSE;
  373. }
  374. #endif /* !CURL_DISABLE_DIGEST_AUTH */
  375. #ifdef USE_NTLM
  376. static bool sasl_choose_ntlm(struct Curl_easy *data, struct sasl_ctx *sctx)
  377. {
  378. if(!sctx->user)
  379. return FALSE;
  380. else if((sctx->enabledmechs & SASL_MECH_NTLM) &&
  381. Curl_auth_is_ntlm_supported()) {
  382. const char *service = data->set.str[STRING_SERVICE_NAME] ?
  383. data->set.str[STRING_SERVICE_NAME] :
  384. sctx->sasl->params->service;
  385. const char *hostname;
  386. int port;
  387. Curl_conn_get_current_host(data, FIRSTSOCKET, &hostname, &port);
  388. sctx->mech = SASL_MECH_STRING_NTLM;
  389. sctx->state1 = SASL_NTLM;
  390. sctx->state2 = SASL_NTLM_TYPE2MSG;
  391. sctx->sasl->authused = SASL_MECH_NTLM;
  392. if(sctx->sasl->force_ir || data->set.sasl_ir) {
  393. struct ntlmdata *ntlm = Curl_auth_ntlm_get(sctx->conn, FALSE);
  394. sctx->result = !ntlm ? CURLE_OUT_OF_MEMORY :
  395. Curl_auth_create_ntlm_type1_message(data,
  396. sctx->conn->user,
  397. sctx->conn->passwd,
  398. service, hostname,
  399. ntlm, &sctx->resp);
  400. }
  401. return TRUE;
  402. }
  403. return FALSE;
  404. }
  405. #endif /* USE_NTLM */
  406. static bool sasl_choose_oauth(struct Curl_easy *data, struct sasl_ctx *sctx)
  407. {
  408. const char *oauth_bearer = data->set.str[STRING_BEARER];
  409. if(sctx->user && oauth_bearer &&
  410. (sctx->enabledmechs & SASL_MECH_OAUTHBEARER)) {
  411. const char *hostname;
  412. int port;
  413. Curl_conn_get_current_host(data, FIRSTSOCKET, &hostname, &port);
  414. sctx->mech = SASL_MECH_STRING_OAUTHBEARER;
  415. sctx->state1 = SASL_OAUTH2;
  416. sctx->state2 = SASL_OAUTH2_RESP;
  417. sctx->sasl->authused = SASL_MECH_OAUTHBEARER;
  418. if(sctx->sasl->force_ir || data->set.sasl_ir)
  419. sctx->result =
  420. Curl_auth_create_oauth_bearer_message(sctx->conn->user,
  421. hostname, port,
  422. oauth_bearer, &sctx->resp);
  423. return TRUE;
  424. }
  425. return FALSE;
  426. }
  427. static bool sasl_choose_oauth2(struct Curl_easy *data, struct sasl_ctx *sctx)
  428. {
  429. const char *oauth_bearer = data->set.str[STRING_BEARER];
  430. if(sctx->user && oauth_bearer &&
  431. (sctx->enabledmechs & SASL_MECH_XOAUTH2)) {
  432. sctx->mech = SASL_MECH_STRING_XOAUTH2;
  433. sctx->state1 = SASL_OAUTH2;
  434. sctx->sasl->authused = SASL_MECH_XOAUTH2;
  435. if(sctx->sasl->force_ir || data->set.sasl_ir)
  436. sctx->result = Curl_auth_create_xoauth_bearer_message(sctx->conn->user,
  437. oauth_bearer,
  438. &sctx->resp);
  439. return TRUE;
  440. }
  441. return FALSE;
  442. }
  443. static bool sasl_choose_plain(struct Curl_easy *data, struct sasl_ctx *sctx)
  444. {
  445. if(sctx->user && (sctx->enabledmechs & SASL_MECH_PLAIN)) {
  446. sctx->mech = SASL_MECH_STRING_PLAIN;
  447. sctx->state1 = SASL_PLAIN;
  448. sctx->sasl->authused = SASL_MECH_PLAIN;
  449. if(sctx->sasl->force_ir || data->set.sasl_ir)
  450. sctx->result =
  451. Curl_auth_create_plain_message(sctx->conn->sasl_authzid,
  452. sctx->conn->user, sctx->conn->passwd,
  453. &sctx->resp);
  454. return TRUE;
  455. }
  456. return FALSE;
  457. }
  458. static bool sasl_choose_login(struct Curl_easy *data, struct sasl_ctx *sctx)
  459. {
  460. if(sctx->user && (sctx->enabledmechs & SASL_MECH_LOGIN)) {
  461. sctx->mech = SASL_MECH_STRING_LOGIN;
  462. sctx->state1 = SASL_LOGIN;
  463. sctx->state2 = SASL_LOGIN_PASSWD;
  464. sctx->sasl->authused = SASL_MECH_LOGIN;
  465. if(sctx->sasl->force_ir || data->set.sasl_ir)
  466. Curl_auth_create_login_message(sctx->conn->user, &sctx->resp);
  467. return TRUE;
  468. }
  469. return FALSE;
  470. }
  471. /*
  472. * Curl_sasl_start()
  473. *
  474. * Calculate the required login details for SASL authentication.
  475. */
  476. CURLcode Curl_sasl_start(struct SASL *sasl, struct Curl_easy *data,
  477. bool force_ir, saslprogress *progress)
  478. {
  479. struct sasl_ctx sctx;
  480. sasl->force_ir = force_ir; /* Latch for future use */
  481. sasl->authused = 0; /* No mechanism used yet */
  482. *progress = SASL_IDLE;
  483. memset(&sctx, 0, sizeof(sctx));
  484. sctx.sasl = sasl;
  485. sctx.conn = data->conn;
  486. sctx.user = data->state.aptr.user;
  487. Curl_bufref_init(&sctx.resp);
  488. sctx.enabledmechs = sasl->authmechs & sasl->prefmech;
  489. sctx.state1 = SASL_STOP;
  490. sctx.state2 = SASL_FINAL;
  491. /* Calculate the supported authentication mechanism, by decreasing order of
  492. security, as well as the initial response where appropriate */
  493. if(sasl_choose_external(data, &sctx) ||
  494. #ifdef USE_KERBEROS5
  495. sasl_choose_krb5(data, &sctx) ||
  496. #endif
  497. #ifdef USE_GSASL
  498. sasl_choose_gsasl(data, &sctx) ||
  499. #endif
  500. #ifndef CURL_DISABLE_DIGEST_AUTH
  501. sasl_choose_digest(data, &sctx) ||
  502. #endif
  503. #ifdef USE_NTLM
  504. sasl_choose_ntlm(data, &sctx) ||
  505. #endif
  506. sasl_choose_oauth(data, &sctx) ||
  507. sasl_choose_oauth2(data, &sctx) ||
  508. sasl_choose_plain(data, &sctx) ||
  509. sasl_choose_login(data, &sctx)) {
  510. /* selected, either we have a mechanism or a failure */
  511. DEBUGASSERT(sctx.mech || sctx.result);
  512. }
  513. if(!sctx.result && sctx.mech) {
  514. sasl->curmech = sctx.mech;
  515. if(Curl_bufref_ptr(&sctx.resp))
  516. sctx.result = build_message(sasl, &sctx.resp);
  517. if(sasl->params->maxirlen &&
  518. strlen(sctx.mech) + Curl_bufref_len(&sctx.resp) >
  519. sasl->params->maxirlen)
  520. Curl_bufref_free(&sctx.resp);
  521. if(!sctx.result)
  522. sctx.result = sasl->params->sendauth(data, sctx.mech, &sctx.resp);
  523. if(!sctx.result) {
  524. *progress = SASL_INPROGRESS;
  525. sasl_state(sasl, data, Curl_bufref_ptr(&sctx.resp) ?
  526. sctx.state2 : sctx.state1);
  527. }
  528. }
  529. Curl_bufref_free(&sctx.resp);
  530. return sctx.result;
  531. }
  532. /*
  533. * Curl_sasl_continue()
  534. *
  535. * Continue the authentication.
  536. */
  537. CURLcode Curl_sasl_continue(struct SASL *sasl, struct Curl_easy *data,
  538. int code, saslprogress *progress)
  539. {
  540. CURLcode result = CURLE_OK;
  541. struct connectdata *conn = data->conn;
  542. saslstate newstate = SASL_FINAL;
  543. struct bufref resp;
  544. const char *hostname;
  545. int port;
  546. #if defined(USE_KERBEROS5) || defined(USE_NTLM) || \
  547. !defined(CURL_DISABLE_DIGEST_AUTH)
  548. const char *service = data->set.str[STRING_SERVICE_NAME] ?
  549. data->set.str[STRING_SERVICE_NAME] :
  550. sasl->params->service;
  551. #endif
  552. const char *oauth_bearer = data->set.str[STRING_BEARER];
  553. struct bufref serverdata;
  554. Curl_conn_get_current_host(data, FIRSTSOCKET, &hostname, &port);
  555. Curl_bufref_init(&serverdata);
  556. Curl_bufref_init(&resp);
  557. *progress = SASL_INPROGRESS;
  558. if(sasl->state == SASL_FINAL) {
  559. if(code != sasl->params->finalcode)
  560. result = CURLE_LOGIN_DENIED;
  561. *progress = SASL_DONE;
  562. sasl_state(sasl, data, SASL_STOP);
  563. return result;
  564. }
  565. if(sasl->state != SASL_CANCEL && sasl->state != SASL_OAUTH2_RESP &&
  566. code != sasl->params->contcode) {
  567. *progress = SASL_DONE;
  568. sasl_state(sasl, data, SASL_STOP);
  569. return CURLE_LOGIN_DENIED;
  570. }
  571. switch(sasl->state) {
  572. case SASL_STOP:
  573. *progress = SASL_DONE;
  574. return result;
  575. case SASL_PLAIN:
  576. result = Curl_auth_create_plain_message(conn->sasl_authzid,
  577. conn->user, conn->passwd, &resp);
  578. break;
  579. case SASL_LOGIN:
  580. Curl_auth_create_login_message(conn->user, &resp);
  581. newstate = SASL_LOGIN_PASSWD;
  582. break;
  583. case SASL_LOGIN_PASSWD:
  584. Curl_auth_create_login_message(conn->passwd, &resp);
  585. break;
  586. case SASL_EXTERNAL:
  587. Curl_auth_create_external_message(conn->user, &resp);
  588. break;
  589. #ifdef USE_GSASL
  590. case SASL_GSASL:
  591. result = get_server_message(sasl, data, &serverdata);
  592. if(!result) {
  593. struct gsasldata *gsasl = Curl_auth_gsasl_get(conn);
  594. result = !gsasl ? CURLE_OUT_OF_MEMORY :
  595. Curl_auth_gsasl_token(data, &serverdata, gsasl, &resp);
  596. }
  597. if(!result && Curl_bufref_len(&resp) > 0)
  598. newstate = SASL_GSASL;
  599. break;
  600. #endif
  601. #ifndef CURL_DISABLE_DIGEST_AUTH
  602. case SASL_CRAMMD5:
  603. result = get_server_message(sasl, data, &serverdata);
  604. if(!result)
  605. result = Curl_auth_create_cram_md5_message(&serverdata, conn->user,
  606. conn->passwd, &resp);
  607. break;
  608. case SASL_DIGESTMD5:
  609. result = get_server_message(sasl, data, &serverdata);
  610. if(!result)
  611. result = Curl_auth_create_digest_md5_message(data, &serverdata,
  612. conn->user, conn->passwd,
  613. service, &resp);
  614. if(!result && (sasl->params->flags & SASL_FLAG_BASE64))
  615. newstate = SASL_DIGESTMD5_RESP;
  616. break;
  617. case SASL_DIGESTMD5_RESP:
  618. /* Keep response NULL to output an empty line. */
  619. break;
  620. #endif
  621. #ifdef USE_NTLM
  622. case SASL_NTLM: {
  623. /* Create the type-1 message */
  624. struct ntlmdata *ntlm = Curl_auth_ntlm_get(conn, FALSE);
  625. result = !ntlm ? CURLE_OUT_OF_MEMORY :
  626. Curl_auth_create_ntlm_type1_message(data,
  627. conn->user, conn->passwd,
  628. service, hostname,
  629. ntlm, &resp);
  630. newstate = SASL_NTLM_TYPE2MSG;
  631. break;
  632. }
  633. case SASL_NTLM_TYPE2MSG: {
  634. /* Decode the type-2 message */
  635. struct ntlmdata *ntlm = Curl_auth_ntlm_get(conn, FALSE);
  636. result = !ntlm ? CURLE_FAILED_INIT :
  637. get_server_message(sasl, data, &serverdata);
  638. if(!result)
  639. result = Curl_auth_decode_ntlm_type2_message(data, &serverdata, ntlm);
  640. if(!result)
  641. result = Curl_auth_create_ntlm_type3_message(data, conn->user,
  642. conn->passwd, ntlm,
  643. &resp);
  644. break;
  645. }
  646. #endif
  647. #ifdef USE_KERBEROS5
  648. case SASL_GSSAPI: {
  649. struct kerberos5data *krb5 = Curl_auth_krb5_get(conn);
  650. result = !krb5 ? CURLE_OUT_OF_MEMORY :
  651. Curl_auth_create_gssapi_user_message(data, conn->user, conn->passwd,
  652. service, conn->host.name,
  653. sasl->mutual_auth, NULL,
  654. krb5, &resp);
  655. newstate = SASL_GSSAPI_TOKEN;
  656. break;
  657. }
  658. case SASL_GSSAPI_TOKEN:
  659. result = get_server_message(sasl, data, &serverdata);
  660. if(!result) {
  661. struct kerberos5data *krb5 = Curl_auth_krb5_get(conn);
  662. if(!krb5)
  663. result = CURLE_OUT_OF_MEMORY;
  664. else if(sasl->mutual_auth) {
  665. /* Decode the user token challenge and create the optional response
  666. message */
  667. result = Curl_auth_create_gssapi_user_message(data, NULL, NULL,
  668. NULL, NULL,
  669. sasl->mutual_auth,
  670. &serverdata,
  671. krb5, &resp);
  672. newstate = SASL_GSSAPI_NO_DATA;
  673. }
  674. else
  675. /* Decode the security challenge and create the response message */
  676. result = Curl_auth_create_gssapi_security_message(data,
  677. conn->sasl_authzid,
  678. &serverdata,
  679. krb5, &resp);
  680. }
  681. break;
  682. case SASL_GSSAPI_NO_DATA:
  683. /* Decode the security challenge and create the response message */
  684. result = get_server_message(sasl, data, &serverdata);
  685. if(!result) {
  686. struct kerberos5data *krb5 = Curl_auth_krb5_get(conn);
  687. if(!krb5)
  688. result = CURLE_OUT_OF_MEMORY;
  689. else
  690. result = Curl_auth_create_gssapi_security_message(data,
  691. conn->sasl_authzid,
  692. &serverdata,
  693. krb5, &resp);
  694. }
  695. break;
  696. #endif
  697. case SASL_OAUTH2:
  698. /* Create the authorization message */
  699. if(sasl->authused == SASL_MECH_OAUTHBEARER) {
  700. result = Curl_auth_create_oauth_bearer_message(conn->user,
  701. hostname,
  702. port,
  703. oauth_bearer,
  704. &resp);
  705. /* Failures maybe sent by the server as continuations for OAUTHBEARER */
  706. newstate = SASL_OAUTH2_RESP;
  707. }
  708. else
  709. result = Curl_auth_create_xoauth_bearer_message(conn->user,
  710. oauth_bearer,
  711. &resp);
  712. break;
  713. case SASL_OAUTH2_RESP:
  714. /* The continuation is optional so check the response code */
  715. if(code == sasl->params->finalcode) {
  716. /* Final response was received so we are done */
  717. *progress = SASL_DONE;
  718. sasl_state(sasl, data, SASL_STOP);
  719. return result;
  720. }
  721. else if(code == sasl->params->contcode) {
  722. /* Acknowledge the continuation by sending a 0x01 response. */
  723. Curl_bufref_set(&resp, "\x01", 1, NULL);
  724. break;
  725. }
  726. else {
  727. *progress = SASL_DONE;
  728. sasl_state(sasl, data, SASL_STOP);
  729. return CURLE_LOGIN_DENIED;
  730. }
  731. case SASL_CANCEL:
  732. /* Remove the offending mechanism from the supported list */
  733. sasl->authmechs &= (unsigned short)~sasl->authused;
  734. sasl->authused = SASL_AUTH_NONE;
  735. sasl->curmech = NULL;
  736. /* Start an alternative SASL authentication */
  737. return Curl_sasl_start(sasl, data, sasl->force_ir, progress);
  738. default:
  739. failf(data, "Unsupported SASL authentication mechanism");
  740. result = CURLE_UNSUPPORTED_PROTOCOL; /* Should not happen */
  741. break;
  742. }
  743. Curl_bufref_free(&serverdata);
  744. switch(result) {
  745. case CURLE_BAD_CONTENT_ENCODING:
  746. /* Cancel dialog */
  747. result = sasl->params->cancelauth(data, sasl->curmech);
  748. newstate = SASL_CANCEL;
  749. break;
  750. case CURLE_OK:
  751. result = build_message(sasl, &resp);
  752. if(!result)
  753. result = sasl->params->contauth(data, sasl->curmech, &resp);
  754. break;
  755. default:
  756. newstate = SASL_STOP; /* Stop on error */
  757. *progress = SASL_DONE;
  758. break;
  759. }
  760. Curl_bufref_free(&resp);
  761. sasl_state(sasl, data, newstate);
  762. return result;
  763. }
  764. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  765. static void sasl_unchosen(struct Curl_easy *data, unsigned short mech,
  766. unsigned short enabledmechs,
  767. bool built_in, bool platform,
  768. const char *param_missing)
  769. {
  770. const char *mname = NULL;
  771. size_t i;
  772. if(!(enabledmechs & mech))
  773. return;
  774. for(i = 0; mechtable[i].name; ++i) {
  775. if(mechtable[i].bit == mech) {
  776. mname = mechtable[i].name;
  777. break;
  778. }
  779. }
  780. if(!mname) /* should not happen */
  781. return;
  782. if(!built_in)
  783. infof(data, "SASL: %s not builtin", mname);
  784. else if(!platform)
  785. infof(data, "SASL: %s not supported by the platform/libraries", mname);
  786. else {
  787. if(param_missing)
  788. infof(data, "SASL: %s is missing %s", mname, param_missing);
  789. if(!data->state.aptr.user)
  790. infof(data, "SASL: %s is missing username", mname);
  791. }
  792. }
  793. #endif /* CURL_DISABLE_VERBOSE_STRINGS */
  794. CURLcode Curl_sasl_is_blocked(struct SASL *sasl, struct Curl_easy *data)
  795. {
  796. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  797. #ifdef USE_KERBEROS5
  798. #define CURL_SASL_KERBEROS5 TRUE
  799. #else
  800. #define CURL_SASL_KERBEROS5 FALSE
  801. #endif
  802. #ifdef USE_GSASL
  803. #define CURL_SASL_GASL TRUE
  804. #else
  805. #define CURL_SASL_GASL FALSE
  806. #endif
  807. #ifdef CURL_DISABLE_DIGEST_AUTH
  808. #define CURL_SASL_DIGEST TRUE
  809. #else
  810. #define CURL_SASL_DIGEST FALSE
  811. #endif
  812. #ifndef USE_NTLM
  813. #define CURL_SASL_NTLM TRUE
  814. #else
  815. #define CURL_SASL_NTLM FALSE
  816. #endif
  817. /* Failing SASL authentication is a pain. Give a helping hand if
  818. * we were unable to select an AUTH mechanism.
  819. * `sasl->authmechs` are mechanisms offered by the peer
  820. * `sasl->prefmech` are mechanisms preferred by us */
  821. unsigned short enabledmechs = sasl->authmechs & sasl->prefmech;
  822. if(!sasl->authmechs)
  823. infof(data, "SASL: no auth mechanism was offered or recognized");
  824. else if(!enabledmechs)
  825. infof(data, "SASL: no overlap between offered and configured "
  826. "auth mechanisms");
  827. else {
  828. infof(data, "SASL: no auth mechanism offered could be selected");
  829. if((enabledmechs & SASL_MECH_EXTERNAL) && data->conn->passwd[0])
  830. infof(data, "SASL: auth EXTERNAL not chosen with password");
  831. sasl_unchosen(data, SASL_MECH_GSSAPI, enabledmechs,
  832. CURL_SASL_KERBEROS5, Curl_auth_is_gssapi_supported(), NULL);
  833. sasl_unchosen(data, SASL_MECH_SCRAM_SHA_256, enabledmechs,
  834. CURL_SASL_GASL, FALSE, NULL);
  835. sasl_unchosen(data, SASL_MECH_SCRAM_SHA_1, enabledmechs,
  836. CURL_SASL_GASL, FALSE, NULL);
  837. sasl_unchosen(data, SASL_MECH_DIGEST_MD5, enabledmechs,
  838. CURL_SASL_DIGEST, Curl_auth_is_digest_supported(), NULL);
  839. sasl_unchosen(data, SASL_MECH_CRAM_MD5, enabledmechs,
  840. CURL_SASL_DIGEST, TRUE, NULL);
  841. sasl_unchosen(data, SASL_MECH_NTLM, enabledmechs,
  842. CURL_SASL_NTLM, Curl_auth_is_ntlm_supported(), NULL);
  843. sasl_unchosen(data, SASL_MECH_OAUTHBEARER, enabledmechs, TRUE, TRUE,
  844. data->set.str[STRING_BEARER] ?
  845. NULL : "CURLOPT_XOAUTH2_BEARER");
  846. sasl_unchosen(data, SASL_MECH_XOAUTH2, enabledmechs, TRUE, TRUE,
  847. data->set.str[STRING_BEARER] ?
  848. NULL : "CURLOPT_XOAUTH2_BEARER");
  849. }
  850. #endif /* CURL_DISABLE_VERBOSE_STRINGS */
  851. (void)sasl;
  852. (void)data;
  853. return CURLE_LOGIN_DENIED;
  854. }
  855. #endif /* protocols are enabled that use SASL */