curl_sasl.c 30 KB

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