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