/src/curl/lib/vauth/digest.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 | | * RFC2831 DIGEST-MD5 authentication |
24 | | * RFC7616 DIGEST-SHA256, DIGEST-SHA512-256 authentication |
25 | | * |
26 | | ***************************************************************************/ |
27 | | #include "curl_setup.h" |
28 | | |
29 | | #ifndef CURL_DISABLE_DIGEST_AUTH |
30 | | |
31 | | #include "vauth/vauth.h" |
32 | | #include "vauth/digest.h" |
33 | | #include "curlx/base64.h" |
34 | | #include "curl_md5.h" |
35 | | #include "curl_sha256.h" |
36 | | #include "curl_sha512_256.h" |
37 | | #include "curlx/strparse.h" |
38 | | #include "rand.h" |
39 | | #include "escape.h" |
40 | | |
41 | | #ifndef USE_WINDOWS_SSPI |
42 | 0 | #define SESSION_ALGO 1 /* for algos with this bit set */ |
43 | | |
44 | 0 | #define DIGEST_QOP_VALUE_STRING_AUTH "auth" |
45 | 0 | #define DIGEST_QOP_VALUE_STRING_AUTH_INT "auth-int" |
46 | | |
47 | 108 | #define ALGO_MD5 0 |
48 | | #ifdef CURL_HAVE_MD5 |
49 | 0 | #define ALGO_MD5SESS (ALGO_MD5 | SESSION_ALGO) |
50 | | |
51 | 0 | #define DIGEST_QOP_VALUE_STRING_AUTH_CONF "auth-conf" |
52 | | |
53 | 0 | #define DIGEST_QOP_VALUE_AUTH (1 << 0) |
54 | 0 | #define DIGEST_QOP_VALUE_AUTH_INT (1 << 1) |
55 | 0 | #define DIGEST_QOP_VALUE_AUTH_CONF (1 << 2) |
56 | | #endif |
57 | | |
58 | | #ifdef CURL_HAVE_SHA256 |
59 | 0 | #define ALGO_SHA256 2 |
60 | 0 | #define ALGO_SHA256SESS (ALGO_SHA256 | SESSION_ALGO) |
61 | | #endif |
62 | | |
63 | | #ifdef CURL_HAVE_SHA512_256 |
64 | 0 | #define ALGO_SHA512_256 4 |
65 | 0 | #define ALGO_SHA512_256SESS (ALGO_SHA512_256 | SESSION_ALGO) |
66 | | #endif |
67 | | |
68 | | #endif /* USE_WINDOWS_SSPI */ |
69 | | |
70 | | bool Curl_auth_digest_get_pair(const char *str, struct Curl_str *value, |
71 | | char *content, const char **endptr) |
72 | 0 | { |
73 | 0 | int c; |
74 | 0 | size_t content_length = 0; |
75 | 0 | bool starts_with_quote = FALSE; |
76 | 0 | bool escape = FALSE; |
77 | |
|
78 | 0 | if(curlx_str_until(&str, value, DIGEST_MAX_VALUE_LENGTH, '=') || |
79 | 0 | curlx_str_single(&str, '=')) |
80 | | /* eek, no match */ |
81 | 0 | return FALSE; |
82 | | |
83 | 0 | if('\"' == *str) { |
84 | | /* This starts with a quote so it must end with one as well! */ |
85 | 0 | str++; |
86 | 0 | starts_with_quote = TRUE; |
87 | 0 | } |
88 | |
|
89 | 0 | for(c = DIGEST_MAX_CONTENT_LENGTH; *str && c--; str++) { |
90 | 0 | if(!escape) { |
91 | 0 | switch(*str) { |
92 | 0 | case '\\': |
93 | 0 | if(starts_with_quote) { |
94 | | /* the start of an escaped quote */ |
95 | 0 | escape = TRUE; |
96 | 0 | continue; |
97 | 0 | } |
98 | 0 | break; |
99 | | |
100 | 0 | case ',': |
101 | 0 | if(!starts_with_quote) { |
102 | | /* This signals the end of the content if we did not get a starting |
103 | | quote and then we do "sloppy" parsing */ |
104 | 0 | c = 0; /* the end */ |
105 | 0 | continue; |
106 | 0 | } |
107 | 0 | break; |
108 | | |
109 | 0 | case '\r': |
110 | 0 | case '\n': |
111 | | /* end of string */ |
112 | 0 | if(starts_with_quote) |
113 | 0 | return FALSE; /* No closing quote */ |
114 | 0 | c = 0; |
115 | 0 | continue; |
116 | | |
117 | 0 | case '\"': |
118 | 0 | if(starts_with_quote) { |
119 | | /* end of string */ |
120 | 0 | c = 0; |
121 | 0 | continue; |
122 | 0 | } |
123 | 0 | else |
124 | 0 | return FALSE; |
125 | 0 | } |
126 | 0 | } |
127 | | |
128 | 0 | if(content_length >= DIGEST_MAX_CONTENT_LENGTH - 1) { |
129 | | /* no room for this byte plus the terminator */ |
130 | 0 | return FALSE; |
131 | 0 | } |
132 | | |
133 | 0 | escape = FALSE; |
134 | 0 | *content++ = *str; |
135 | 0 | content_length++; |
136 | 0 | } |
137 | | |
138 | 0 | if(escape) |
139 | 0 | return FALSE; /* No character after backslash */ |
140 | | |
141 | 0 | *content = 0; |
142 | 0 | *endptr = str; |
143 | |
|
144 | 0 | return TRUE; |
145 | 0 | } |
146 | | |
147 | | #ifndef USE_WINDOWS_SSPI |
148 | | |
149 | | #if defined(CURL_HAVE_MD5) || defined(CURL_HAVE_SHA256) || \ |
150 | | defined(CURL_HAVE_SHA512_256) |
151 | | /* Perform quoted-string escaping as described in RFC 9110, ch. 5.6.4, |
152 | | * esp. passing %80-FF unencoded as defined for 'obs-text'. */ |
153 | | static char *auth_digest_string_quoted(const char *s) |
154 | 0 | { |
155 | 0 | struct dynbuf out; |
156 | 0 | curlx_dyn_init(&out, 2048); |
157 | 0 | if(!*s) /* for zero length input, make sure we return an empty string */ |
158 | 0 | return curlx_strdup(""); |
159 | 0 | while(*s) { |
160 | 0 | CURLcode result; |
161 | 0 | if(*s == '"' || *s == '\\') { |
162 | 0 | result = curlx_dyn_addn(&out, "\\", 1); |
163 | 0 | if(!result) |
164 | 0 | result = curlx_dyn_addn(&out, s, 1); |
165 | 0 | } |
166 | 0 | else if(((unsigned char)*s < 0x20) || (*s == 0x7f)) { |
167 | 0 | unsigned char buf[3] = { '%' }; |
168 | 0 | Curl_hexbyte(&buf[1], (unsigned char)*s); |
169 | 0 | result = curlx_dyn_addn(&out, buf, 3); |
170 | 0 | } |
171 | 0 | else |
172 | 0 | result = curlx_dyn_addn(&out, s, 1); |
173 | 0 | if(result) |
174 | 0 | return NULL; |
175 | 0 | s++; |
176 | 0 | } |
177 | 0 | return curlx_dyn_ptr(&out); |
178 | 0 | } |
179 | | #endif |
180 | | |
181 | | #if defined(CURL_HAVE_SHA256) || defined(CURL_HAVE_SHA512_256) |
182 | | /* Convert sha256 or SHA-512/256 chunk to RFC7616 -suitable ASCII string */ |
183 | | static void auth_digest_sha256_to_ascii( |
184 | | const unsigned char *source, /* 32 bytes */ |
185 | | unsigned char *dest) /* 65 bytes */ |
186 | 0 | { |
187 | 0 | int i; |
188 | 0 | for(i = 0; i < 32; i++) |
189 | 0 | curl_msnprintf((char *)&dest[i * 2], 3, "%02x", source[i]); |
190 | 0 | } |
191 | | #endif |
192 | | |
193 | | #ifdef CURL_HAVE_MD5 |
194 | | /* Convert MD5 chunk to RFC2617 (section 3.1.3) -suitable ASCII string */ |
195 | | static void auth_digest_md5_to_ascii( |
196 | | const unsigned char *source, /* 16 bytes */ |
197 | | unsigned char *dest) /* 33 bytes */ |
198 | 0 | { |
199 | 0 | int i; |
200 | 0 | for(i = 0; i < 16; i++) |
201 | 0 | curl_msnprintf((char *)&dest[i * 2], 3, "%02x", source[i]); |
202 | 0 | } |
203 | | |
204 | | /* Retrieves the value for a corresponding key from the challenge string |
205 | | * returns TRUE if the key could be found, FALSE if it does not exist |
206 | | */ |
207 | | static bool auth_digest_get_key_value(const char *chlg, const char *key, |
208 | | char *buf, size_t buflen) |
209 | 0 | { |
210 | | /* keyword=[value],keyword2=[value] |
211 | | The values may or may not be quoted. |
212 | | */ |
213 | |
|
214 | 0 | do { |
215 | 0 | struct Curl_str data; |
216 | 0 | struct Curl_str name; |
217 | |
|
218 | 0 | curlx_str_passblanks(&chlg); |
219 | |
|
220 | 0 | if(!curlx_str_until(&chlg, &name, 64, '=') && |
221 | 0 | !curlx_str_single(&chlg, '=')) { |
222 | | /* this is the key, get the value, possibly quoted */ |
223 | 0 | int rc = curlx_str_quotedword(&chlg, &data, 256); |
224 | 0 | if(rc == STRE_BEGQUOTE) |
225 | | /* try unquoted until comma */ |
226 | 0 | rc = curlx_str_until(&chlg, &data, 256, ','); |
227 | 0 | if(rc) |
228 | 0 | return FALSE; /* weird */ |
229 | | |
230 | 0 | if(curlx_str_cmp(&name, key)) { |
231 | | /* if this is our key, return the value */ |
232 | 0 | size_t len = curlx_strlen(&data); |
233 | 0 | const char *src = curlx_str(&data); |
234 | 0 | size_t i; |
235 | 0 | size_t outlen = 0; |
236 | |
|
237 | 0 | if(len >= buflen) |
238 | | /* does not fit */ |
239 | 0 | return FALSE; |
240 | | |
241 | 0 | for(i = 0; i < len; i++) { |
242 | 0 | if(src[i] == '\\' && i + 1 < len) { |
243 | 0 | i++; /* skip backslash */ |
244 | 0 | } |
245 | 0 | buf[outlen++] = src[i]; |
246 | 0 | } |
247 | 0 | buf[outlen] = 0; |
248 | 0 | return TRUE; |
249 | 0 | } |
250 | 0 | if(curlx_str_single(&chlg, ',')) |
251 | 0 | return FALSE; |
252 | 0 | } |
253 | 0 | else /* odd syntax */ |
254 | 0 | break; |
255 | 0 | } while(1); |
256 | | |
257 | 0 | return FALSE; |
258 | 0 | } |
259 | | |
260 | | static void auth_digest_get_qop_values(const char *options, int *value) |
261 | 0 | { |
262 | 0 | struct Curl_str out; |
263 | | /* Initialize the output */ |
264 | 0 | *value = 0; |
265 | |
|
266 | 0 | while(!curlx_str_until(&options, &out, 32, ',')) { |
267 | 0 | if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH)) |
268 | 0 | *value |= DIGEST_QOP_VALUE_AUTH; |
269 | 0 | else if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH_INT)) |
270 | 0 | *value |= DIGEST_QOP_VALUE_AUTH_INT; |
271 | 0 | else if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH_CONF)) |
272 | 0 | *value |= DIGEST_QOP_VALUE_AUTH_CONF; |
273 | 0 | if(curlx_str_single(&options, ',')) |
274 | 0 | break; |
275 | 0 | } |
276 | 0 | } |
277 | | |
278 | | /* |
279 | | * auth_decode_digest_md5_message() |
280 | | * |
281 | | * This is used internally to decode an already encoded DIGEST-MD5 challenge |
282 | | * message into the separate attributes. |
283 | | * |
284 | | * Parameters: |
285 | | * |
286 | | * chlgref [in] - The challenge message. |
287 | | * nonce [in/out] - The buffer where the nonce is stored. |
288 | | * nlen [in] - The length of the nonce buffer. |
289 | | * realm [in/out] - The buffer where the realm is stored. |
290 | | * rlen [in] - The length of the realm buffer. |
291 | | * alg [in/out] - The buffer where the algorithm is stored. |
292 | | * alen [in] - The length of the algorithm buffer. |
293 | | * qop [in/out] - The buffer where the qop-options is stored. |
294 | | * qlen [in] - The length of the qop buffer. |
295 | | * |
296 | | * Returns CURLE_OK on success. |
297 | | */ |
298 | | static CURLcode auth_decode_digest_md5_message(const struct bufref *chlgref, |
299 | | char *nonce, size_t nlen, |
300 | | char *realm, size_t rlen, |
301 | | char *alg, size_t alen, |
302 | | char *qop, size_t qlen) |
303 | 0 | { |
304 | 0 | const char *chlg = Curl_bufref_ptr(chlgref); |
305 | | |
306 | | /* Ensure we have a valid challenge message */ |
307 | 0 | if(!Curl_bufref_len(chlgref)) |
308 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
309 | | |
310 | | /* Retrieve nonce string from the challenge */ |
311 | 0 | if(!auth_digest_get_key_value(chlg, "nonce", nonce, nlen)) |
312 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
313 | | |
314 | | /* Retrieve realm string from the challenge */ |
315 | 0 | if(!auth_digest_get_key_value(chlg, "realm", realm, rlen)) { |
316 | | /* Challenge does not have a realm, set empty string [RFC2831] page 6 */ |
317 | 0 | *realm = '\0'; |
318 | 0 | } |
319 | | |
320 | | /* Retrieve algorithm string from the challenge */ |
321 | 0 | if(!auth_digest_get_key_value(chlg, "algorithm", alg, alen)) |
322 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
323 | | |
324 | | /* Retrieve qop-options string from the challenge */ |
325 | 0 | if(!auth_digest_get_key_value(chlg, "qop", qop, qlen)) |
326 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
327 | | |
328 | 0 | return CURLE_OK; |
329 | 0 | } |
330 | | #endif /* CURL_HAVE_MD5 */ |
331 | | |
332 | | /* |
333 | | * Curl_auth_is_digest_supported() |
334 | | * |
335 | | * This is used to evaluate if DIGEST is supported. |
336 | | * |
337 | | * Parameters: None |
338 | | * |
339 | | * Returns TRUE as DIGEST as handled by libcurl. |
340 | | */ |
341 | | bool Curl_auth_is_digest_supported(void) |
342 | 0 | { |
343 | 0 | #if defined(CURL_HAVE_MD5) || defined(CURL_HAVE_SHA256) || \ |
344 | 0 | defined(CURL_HAVE_SHA512_256) |
345 | 0 | return TRUE; |
346 | | #else |
347 | | return FALSE; |
348 | | #endif |
349 | 0 | } |
350 | | |
351 | | /* |
352 | | * Curl_auth_create_digest_md5_message() |
353 | | * |
354 | | * This is used to generate an already encoded DIGEST-MD5 response message |
355 | | * ready for sending to the recipient. |
356 | | * |
357 | | * Parameters: |
358 | | * |
359 | | * data [in] - The session handle. |
360 | | * chlg [in] - The challenge message. |
361 | | * userp [in] - The username. |
362 | | * passwdp [in] - The user's password. |
363 | | * service [in] - The service type such as http, smtp, pop or imap. |
364 | | * out [out] - The result storage. |
365 | | * |
366 | | * Returns CURLE_OK on success. |
367 | | */ |
368 | | CURLcode Curl_auth_create_digest_md5_message(struct Curl_easy *data, |
369 | | const struct bufref *chlg, |
370 | | struct Curl_creds *creds, |
371 | | const char *default_service, |
372 | | struct bufref *out) |
373 | 0 | { |
374 | 0 | CURLcode result; |
375 | | #ifndef CURL_HAVE_MD5 |
376 | | (void)data; |
377 | | (void)chlg; |
378 | | (void)creds; |
379 | | (void)default_service; |
380 | | (void)out; |
381 | | result = CURLE_NOT_BUILT_IN; |
382 | | #else |
383 | 0 | const char *service = Curl_creds_has_sasl_service(creds) ? |
384 | 0 | Curl_creds_sasl_service(creds) : default_service; |
385 | 0 | size_t i; |
386 | 0 | void *ctxt; |
387 | 0 | const char *userp = Curl_creds_user(creds); |
388 | 0 | const char *passwdp = Curl_creds_passwd(creds); |
389 | 0 | char *response = NULL; |
390 | 0 | uint8_t digest[CURL_MD5_DIGEST_LEN]; |
391 | 0 | char HA1_hex[(2 * CURL_MD5_DIGEST_LEN) + 1]; |
392 | 0 | char HA2_hex[(2 * CURL_MD5_DIGEST_LEN) + 1]; |
393 | 0 | char resp_hash_hex[(2 * CURL_MD5_DIGEST_LEN) + 1]; |
394 | 0 | char nonce[64]; |
395 | 0 | char realm[128]; |
396 | 0 | char algorithm[64]; |
397 | 0 | char qop_options[64]; |
398 | 0 | int qop_values; |
399 | 0 | char cnonce[33]; |
400 | 0 | char nonceCount[] = "00000001"; |
401 | 0 | char method[] = "AUTHENTICATE"; |
402 | 0 | char qop[] = DIGEST_QOP_VALUE_STRING_AUTH; |
403 | 0 | char *spn = NULL; |
404 | 0 | char *qrealm; |
405 | 0 | char *qnonce; |
406 | 0 | char *quserp; |
407 | 0 | char *qspn; |
408 | 0 | CURLcode hok; |
409 | | |
410 | | /* Decode the challenge message */ |
411 | 0 | result = auth_decode_digest_md5_message(chlg, |
412 | 0 | nonce, sizeof(nonce), |
413 | 0 | realm, sizeof(realm), |
414 | 0 | algorithm, |
415 | 0 | sizeof(algorithm), |
416 | 0 | qop_options, |
417 | 0 | sizeof(qop_options)); |
418 | 0 | if(result) |
419 | 0 | return result; |
420 | | |
421 | | /* We only support md5 sessions */ |
422 | 0 | if(strcmp(algorithm, "md5-sess")) |
423 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
424 | | |
425 | | /* Get the qop-values from the qop-options */ |
426 | 0 | auth_digest_get_qop_values(qop_options, &qop_values); |
427 | | |
428 | | /* We only support auth quality-of-protection */ |
429 | 0 | if(!(qop_values & DIGEST_QOP_VALUE_AUTH)) |
430 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
431 | | |
432 | | /* Generate 32 random hex chars, 32 bytes + 1 null-termination */ |
433 | 0 | result = Curl_rand_hex(data, (unsigned char *)cnonce, sizeof(cnonce)); |
434 | 0 | if(result) |
435 | 0 | return result; |
436 | | |
437 | | /* Good so far, now calculate A1 and H(A1) according to RFC 2831 */ |
438 | 0 | ctxt = Curl_MD5_init(); |
439 | 0 | if(!ctxt) |
440 | 0 | return CURLE_OUT_OF_MEMORY; |
441 | | |
442 | 0 | hok = CURLE_OK; |
443 | 0 | hok |= Curl_MD5_update(ctxt, userp, strlen(userp)); |
444 | 0 | hok |= Curl_MD5_update(ctxt, ":", 1); |
445 | 0 | hok |= Curl_MD5_update(ctxt, realm, strlen(realm)); |
446 | 0 | hok |= Curl_MD5_update(ctxt, ":", 1); |
447 | 0 | hok |= Curl_MD5_update(ctxt, passwdp, strlen(passwdp)); |
448 | 0 | hok |= Curl_MD5_final(ctxt, digest); |
449 | 0 | if(hok) |
450 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
451 | | |
452 | 0 | ctxt = Curl_MD5_init(); |
453 | 0 | if(!ctxt) |
454 | 0 | return CURLE_OUT_OF_MEMORY; |
455 | | |
456 | 0 | hok = CURLE_OK; |
457 | 0 | hok |= Curl_MD5_update(ctxt, digest, CURL_MD5_DIGEST_LEN); |
458 | 0 | hok |= Curl_MD5_update(ctxt, ":", 1); |
459 | 0 | hok |= Curl_MD5_update(ctxt, nonce, strlen(nonce)); |
460 | 0 | hok |= Curl_MD5_update(ctxt, ":", 1); |
461 | 0 | hok |= Curl_MD5_update(ctxt, cnonce, strlen(cnonce)); |
462 | 0 | hok |= Curl_MD5_final(ctxt, digest); |
463 | 0 | if(hok) |
464 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
465 | | |
466 | | /* Convert calculated 16 octet hex into 32 bytes string */ |
467 | 0 | for(i = 0; i < CURL_MD5_DIGEST_LEN; i++) |
468 | 0 | curl_msnprintf(&HA1_hex[2 * i], 3, "%02x", digest[i]); |
469 | | |
470 | | /* Generate our SPN */ |
471 | 0 | spn = Curl_auth_build_spn(service, data->state.origin->hostname, NULL); |
472 | 0 | if(!spn) |
473 | 0 | return CURLE_OUT_OF_MEMORY; |
474 | | |
475 | | /* Calculate H(A2) */ |
476 | 0 | ctxt = Curl_MD5_init(); |
477 | 0 | if(!ctxt) { |
478 | 0 | curlx_free(spn); |
479 | 0 | return CURLE_OUT_OF_MEMORY; |
480 | 0 | } |
481 | | |
482 | 0 | hok = CURLE_OK; |
483 | 0 | hok |= Curl_MD5_update(ctxt, method, strlen(method)); |
484 | 0 | hok |= Curl_MD5_update(ctxt, ":", 1); |
485 | 0 | hok |= Curl_MD5_update(ctxt, spn, strlen(spn)); |
486 | 0 | hok |= Curl_MD5_final(ctxt, digest); |
487 | 0 | if(hok) |
488 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
489 | | |
490 | 0 | for(i = 0; i < CURL_MD5_DIGEST_LEN; i++) |
491 | 0 | curl_msnprintf(&HA2_hex[2 * i], 3, "%02x", digest[i]); |
492 | | |
493 | | /* Now calculate the response hash */ |
494 | 0 | ctxt = Curl_MD5_init(); |
495 | 0 | if(!ctxt) { |
496 | 0 | curlx_free(spn); |
497 | 0 | return CURLE_OUT_OF_MEMORY; |
498 | 0 | } |
499 | | |
500 | 0 | hok = CURLE_OK; |
501 | 0 | hok |= Curl_MD5_update(ctxt, HA1_hex, 2 * CURL_MD5_DIGEST_LEN); |
502 | 0 | hok |= Curl_MD5_update(ctxt, ":", 1); |
503 | 0 | hok |= Curl_MD5_update(ctxt, nonce, strlen(nonce)); |
504 | 0 | hok |= Curl_MD5_update(ctxt, ":", 1); |
505 | |
|
506 | 0 | hok |= Curl_MD5_update(ctxt, nonceCount, strlen(nonceCount)); |
507 | 0 | hok |= Curl_MD5_update(ctxt, ":", 1); |
508 | 0 | hok |= Curl_MD5_update(ctxt, cnonce, strlen(cnonce)); |
509 | 0 | hok |= Curl_MD5_update(ctxt, ":", 1); |
510 | 0 | hok |= Curl_MD5_update(ctxt, qop, strlen(qop)); |
511 | 0 | hok |= Curl_MD5_update(ctxt, ":", 1); |
512 | |
|
513 | 0 | hok |= Curl_MD5_update(ctxt, HA2_hex, 2 * CURL_MD5_DIGEST_LEN); |
514 | 0 | hok |= Curl_MD5_final(ctxt, digest); |
515 | 0 | if(hok) { |
516 | 0 | curlx_free(spn); |
517 | 0 | return CURLE_BAD_FUNCTION_ARGUMENT; |
518 | 0 | } |
519 | | |
520 | 0 | for(i = 0; i < CURL_MD5_DIGEST_LEN; i++) |
521 | 0 | curl_msnprintf(&resp_hash_hex[2 * i], 3, "%02x", digest[i]); |
522 | | |
523 | | /* escape double quotes and backslashes in the username, realm, nonce and |
524 | | spn as necessary */ |
525 | 0 | qrealm = auth_digest_string_quoted(realm); |
526 | 0 | qnonce = auth_digest_string_quoted(nonce); |
527 | 0 | quserp = auth_digest_string_quoted(userp); |
528 | 0 | qspn = auth_digest_string_quoted(spn); |
529 | 0 | if(qrealm && qnonce && quserp && qspn) |
530 | | /* Generate the response */ |
531 | 0 | response = curl_maprintf("username=\"%s\",realm=\"%s\",nonce=\"%s\"," |
532 | 0 | "cnonce=\"%s\",nc=\"%s\",digest-uri=\"%s\"," |
533 | 0 | "response=%s,qop=%s", |
534 | 0 | quserp, qrealm, qnonce, |
535 | 0 | cnonce, nonceCount, qspn, resp_hash_hex, qop); |
536 | |
|
537 | 0 | curlx_free(qrealm); |
538 | 0 | curlx_free(qnonce); |
539 | 0 | curlx_free(quserp); |
540 | 0 | curlx_free(spn); |
541 | 0 | curlx_free(qspn); |
542 | 0 | if(!response) |
543 | 0 | return CURLE_OUT_OF_MEMORY; |
544 | | |
545 | | /* Return the response. */ |
546 | 0 | Curl_bufref_set(out, response, strlen(response), curl_free); |
547 | 0 | #endif /* !CURL_HAVE_MD5 */ |
548 | 0 | return result; |
549 | 0 | } |
550 | | |
551 | | /* |
552 | | * Curl_auth_decode_digest_http_message() |
553 | | * |
554 | | * This is used to decode an HTTP DIGEST challenge message into the separate |
555 | | * attributes. |
556 | | * |
557 | | * Parameters: |
558 | | * |
559 | | * chlg [in] - The challenge message. |
560 | | * digest [in/out] - The digest data struct being used and modified. |
561 | | * |
562 | | * Returns CURLE_OK on success. |
563 | | */ |
564 | | CURLcode Curl_auth_decode_digest_http_message(const char *chlg, |
565 | | struct digestdata *digest) |
566 | 0 | { |
567 | 0 | bool before = FALSE; /* got a nonce before */ |
568 | | |
569 | | /* If we already have received a nonce, keep that in mind */ |
570 | 0 | if(digest->nonce) |
571 | 0 | before = TRUE; |
572 | | |
573 | | /* Clean up any former leftovers and initialize to defaults */ |
574 | 0 | Curl_auth_digest_cleanup(digest); |
575 | |
|
576 | 0 | for(;;) { |
577 | 0 | struct Curl_str value; |
578 | 0 | char content[DIGEST_MAX_CONTENT_LENGTH]; |
579 | | |
580 | | /* Pass all additional spaces here */ |
581 | 0 | curlx_str_passblanks(&chlg); |
582 | | |
583 | | /* Extract a value=content pair */ |
584 | 0 | if(Curl_auth_digest_get_pair(chlg, &value, content, &chlg)) { |
585 | 0 | if(curlx_str_casecompare2(&value, STRCONST("nonce"))) { |
586 | 0 | curlx_free(digest->nonce); |
587 | 0 | digest->nonce = curlx_strdup(content); |
588 | 0 | if(!digest->nonce) |
589 | 0 | return CURLE_OUT_OF_MEMORY; |
590 | 0 | } |
591 | 0 | else if(curlx_str_casecompare2(&value, STRCONST("stale"))) { |
592 | 0 | if(curl_strequal(content, "true")) { |
593 | 0 | digest->stale = TRUE; |
594 | 0 | digest->nc = 1; /* we make a new nonce now */ |
595 | 0 | } |
596 | 0 | } |
597 | 0 | else if(curlx_str_casecompare2(&value, STRCONST("realm"))) { |
598 | 0 | curlx_free(digest->realm); |
599 | 0 | digest->realm = curlx_strdup(content); |
600 | 0 | if(!digest->realm) |
601 | 0 | return CURLE_OUT_OF_MEMORY; |
602 | 0 | } |
603 | 0 | else if(curlx_str_casecompare2(&value, STRCONST("opaque"))) { |
604 | 0 | curlx_free(digest->opaque); |
605 | 0 | digest->opaque = curlx_strdup(content); |
606 | 0 | if(!digest->opaque) |
607 | 0 | return CURLE_OUT_OF_MEMORY; |
608 | 0 | } |
609 | 0 | else if(curlx_str_casecompare2(&value, STRCONST("qop"))) { |
610 | 0 | const char *token = content; |
611 | 0 | struct Curl_str out; |
612 | 0 | bool foundAuth = FALSE; |
613 | 0 | bool foundAuthInt = FALSE; |
614 | | /* Pass leading spaces */ |
615 | 0 | curlx_str_passblanks(&token); |
616 | |
|
617 | 0 | while(!curlx_str_until(&token, &out, 32, ',')) { |
618 | 0 | if(curlx_str_casecompare(&out, DIGEST_QOP_VALUE_STRING_AUTH)) |
619 | 0 | foundAuth = TRUE; |
620 | 0 | else if(curlx_str_casecompare(&out, |
621 | 0 | DIGEST_QOP_VALUE_STRING_AUTH_INT)) |
622 | 0 | foundAuthInt = TRUE; |
623 | 0 | if(curlx_str_single(&token, ',')) |
624 | 0 | break; |
625 | 0 | curlx_str_passblanks(&token); |
626 | 0 | } |
627 | | |
628 | | /* Select only auth or auth-int. Otherwise, ignore */ |
629 | 0 | if(foundAuth) { |
630 | 0 | curlx_free(digest->qop); |
631 | 0 | digest->qop = curlx_strdup(DIGEST_QOP_VALUE_STRING_AUTH); |
632 | 0 | if(!digest->qop) |
633 | 0 | return CURLE_OUT_OF_MEMORY; |
634 | 0 | } |
635 | 0 | else if(foundAuthInt) { |
636 | 0 | curlx_free(digest->qop); |
637 | 0 | digest->qop = curlx_strdup(DIGEST_QOP_VALUE_STRING_AUTH_INT); |
638 | 0 | if(!digest->qop) |
639 | 0 | return CURLE_OUT_OF_MEMORY; |
640 | 0 | } |
641 | 0 | } |
642 | 0 | else if(curlx_str_casecompare2(&value, STRCONST("algorithm"))) { |
643 | 0 | curlx_free(digest->algorithm); |
644 | 0 | digest->algorithm = curlx_strdup(content); |
645 | 0 | if(!digest->algorithm) |
646 | 0 | return CURLE_OUT_OF_MEMORY; |
647 | | |
648 | 0 | if(curl_strequal(content, "MD5-sess")) { |
649 | 0 | #ifdef CURL_HAVE_MD5 |
650 | 0 | digest->algo = ALGO_MD5SESS; |
651 | | #else |
652 | | Curl_auth_digest_cleanup(digest); |
653 | | return CURLE_NOT_BUILT_IN; |
654 | | #endif |
655 | 0 | } |
656 | 0 | else if(curl_strequal(content, "MD5")) { |
657 | 0 | #ifdef CURL_HAVE_MD5 |
658 | 0 | digest->algo = ALGO_MD5; |
659 | | #else |
660 | | Curl_auth_digest_cleanup(digest); |
661 | | return CURLE_NOT_BUILT_IN; |
662 | | #endif |
663 | 0 | } |
664 | 0 | else if(curl_strequal(content, "SHA-256")) { |
665 | 0 | #ifdef CURL_HAVE_SHA256 |
666 | 0 | digest->algo = ALGO_SHA256; |
667 | | #else |
668 | | Curl_auth_digest_cleanup(digest); |
669 | | return CURLE_NOT_BUILT_IN; |
670 | | #endif |
671 | 0 | } |
672 | 0 | else if(curl_strequal(content, "SHA-256-SESS")) { |
673 | 0 | #ifdef CURL_HAVE_SHA256 |
674 | 0 | digest->algo = ALGO_SHA256SESS; |
675 | | #else |
676 | | Curl_auth_digest_cleanup(digest); |
677 | | return CURLE_NOT_BUILT_IN; |
678 | | #endif |
679 | 0 | } |
680 | 0 | else if(curl_strequal(content, "SHA-512-256")) { |
681 | 0 | #ifdef CURL_HAVE_SHA512_256 |
682 | 0 | digest->algo = ALGO_SHA512_256; |
683 | | #else |
684 | | Curl_auth_digest_cleanup(digest); |
685 | | return CURLE_NOT_BUILT_IN; |
686 | | #endif |
687 | 0 | } |
688 | 0 | else if(curl_strequal(content, "SHA-512-256-SESS")) { |
689 | 0 | #ifdef CURL_HAVE_SHA512_256 |
690 | 0 | digest->algo = ALGO_SHA512_256SESS; |
691 | | #else |
692 | | Curl_auth_digest_cleanup(digest); |
693 | | return CURLE_NOT_BUILT_IN; |
694 | | #endif |
695 | 0 | } |
696 | 0 | else |
697 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
698 | 0 | } |
699 | 0 | else if(curlx_str_casecompare2(&value, STRCONST("userhash"))) { |
700 | 0 | if(curl_strequal(content, "true")) { |
701 | 0 | digest->userhash = TRUE; |
702 | 0 | } |
703 | 0 | } |
704 | 0 | else { |
705 | | /* Unknown specifier, ignore it! */ |
706 | 0 | } |
707 | 0 | } |
708 | 0 | else |
709 | 0 | break; /* We are done here */ |
710 | | |
711 | | /* Pass all additional spaces here */ |
712 | 0 | curlx_str_passblanks(&chlg); |
713 | | |
714 | | /* Allow the list to be comma-separated */ |
715 | 0 | (void)curlx_str_single(&chlg, ','); |
716 | 0 | } |
717 | | |
718 | | /* We had a nonce since before, and we got another one now without |
719 | | 'stale=true'. This means we provided bad credentials in the previous |
720 | | request */ |
721 | 0 | if(before && !digest->stale) |
722 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
723 | | |
724 | | /* We got this header without a nonce, that is a bad Digest line! */ |
725 | 0 | if(!digest->nonce) |
726 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
727 | | |
728 | | /* "<algo>-sess" protocol versions require "auth" or "auth-int" qop */ |
729 | 0 | if(!digest->qop && (digest->algo & SESSION_ALGO)) |
730 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
731 | | |
732 | 0 | #ifdef CURL_HAVE_MD5 |
733 | | /* MD5 can be disabled at runtime (FIPS) */ |
734 | 0 | if(digest->algo == ALGO_MD5 || digest->algo == ALGO_MD5SESS) { |
735 | 0 | uint8_t md5[CURL_MD5_DIGEST_LEN]; |
736 | 0 | CURLcode result = Curl_md5it(md5, "", 0); |
737 | 0 | if(result == CURLE_FAILED_INIT) { |
738 | 0 | Curl_auth_digest_cleanup(digest); /* forget this challenge */ |
739 | 0 | return CURLE_NOT_BUILT_IN; |
740 | 0 | } |
741 | 0 | if(result) |
742 | 0 | return result; |
743 | 0 | } |
744 | 0 | #endif |
745 | | |
746 | 0 | return CURLE_OK; |
747 | 0 | } |
748 | | |
749 | | #if defined(CURL_HAVE_MD5) || defined(CURL_HAVE_SHA256) || \ |
750 | | defined(CURL_HAVE_SHA512_256) |
751 | | |
752 | | /* |
753 | | * auth_create_digest_http_message() |
754 | | * |
755 | | * This is used to generate an HTTP DIGEST response message ready for sending |
756 | | * to the recipient. |
757 | | * |
758 | | * Parameters: |
759 | | * |
760 | | * data [in] - The session handle. |
761 | | * creds [in] - The credentials |
762 | | * request [in] - The HTTP request. |
763 | | * uripath [in] - The path of the HTTP uri. |
764 | | * digest [in/out] - The digest data struct being used and modified. |
765 | | * outptr [in/out] - The address where a pointer to newly allocated memory |
766 | | * holding the result is stored upon completion. |
767 | | * outlen [out] - The length of the output message. |
768 | | * |
769 | | * Returns CURLE_OK on success. |
770 | | */ |
771 | | static CURLcode auth_create_digest_http_message( |
772 | | struct Curl_easy *data, |
773 | | struct Curl_creds *creds, |
774 | | const unsigned char *request, |
775 | | const unsigned char *uripath, |
776 | | struct digestdata *digest, |
777 | | char **outptr, size_t *outlen, |
778 | | void (*convert_to_ascii)(const unsigned char *, unsigned char *), |
779 | | CURLcode (*hash)(uint8_t *output, const void *input, const size_t len)) |
780 | 0 | { |
781 | 0 | CURLcode result; |
782 | 0 | const char *userp = Curl_creds_user(creds); |
783 | 0 | const char *passwdp = Curl_creds_passwd(creds); |
784 | 0 | uint8_t hashbuf[32]; /* 32 bytes/256 bits */ |
785 | 0 | unsigned char request_digest[65]; |
786 | 0 | unsigned char ha1[65]; /* 64 digits and 1 zero byte */ |
787 | 0 | unsigned char ha2[65]; /* 64 digits and 1 zero byte */ |
788 | 0 | char userh[65]; |
789 | 0 | char *cnonce = NULL; |
790 | 0 | size_t cnonce_sz = 0; |
791 | 0 | char *userp_quoted = NULL; |
792 | 0 | char *realm_quoted = NULL; |
793 | 0 | char *nonce_quoted = NULL; |
794 | 0 | char *hashthis = NULL; |
795 | 0 | char *uri_quoted = NULL; |
796 | 0 | struct dynbuf response; |
797 | 0 | *outptr = NULL; |
798 | |
|
799 | 0 | curlx_dyn_init(&response, 4096); /* arbitrary max */ |
800 | |
|
801 | 0 | memset(hashbuf, 0, sizeof(hashbuf)); |
802 | 0 | if(!digest->nc) |
803 | 0 | digest->nc = 1; |
804 | |
|
805 | 0 | if(!digest->cnonce) { |
806 | 0 | char cnoncebuf[12]; |
807 | 0 | result = Curl_rand_bytes(data, |
808 | 0 | #ifdef DEBUGBUILD |
809 | 0 | TRUE, |
810 | 0 | #endif |
811 | 0 | (unsigned char *)cnoncebuf, |
812 | 0 | sizeof(cnoncebuf)); |
813 | 0 | if(!result) |
814 | 0 | result = curlx_base64_encode((uint8_t *)cnoncebuf, sizeof(cnoncebuf), |
815 | 0 | &cnonce, &cnonce_sz); |
816 | 0 | if(result) |
817 | 0 | goto oom; |
818 | | |
819 | 0 | digest->cnonce = cnonce; |
820 | 0 | } |
821 | | |
822 | 0 | if(digest->userhash) { |
823 | 0 | char *hasht = curl_maprintf("%s:%s", userp, |
824 | 0 | digest->realm ? digest->realm : ""); |
825 | 0 | if(!hasht) { |
826 | 0 | result = CURLE_OUT_OF_MEMORY; |
827 | 0 | goto oom; |
828 | 0 | } |
829 | | |
830 | 0 | result = hash(hashbuf, hasht, strlen(hasht)); |
831 | 0 | curlx_free(hasht); |
832 | 0 | if(result) |
833 | 0 | goto oom; |
834 | 0 | convert_to_ascii(hashbuf, (unsigned char *)userh); |
835 | 0 | } |
836 | | |
837 | | /* If the algorithm is "MD5" or unspecified (which then defaults to MD5): |
838 | | |
839 | | A1 = unq(username-value) ":" unq(realm-value) ":" passwd |
840 | | |
841 | | If the algorithm is "MD5-sess" then: |
842 | | |
843 | | A1 = H(unq(username-value) ":" unq(realm-value) ":" passwd) ":" |
844 | | unq(nonce-value) ":" unq(cnonce-value) |
845 | | */ |
846 | | |
847 | 0 | hashthis = curl_maprintf("%s:%s:%s", userp, digest->realm ? |
848 | 0 | digest->realm : "", passwdp); |
849 | 0 | if(!hashthis) { |
850 | 0 | result = CURLE_OUT_OF_MEMORY; |
851 | 0 | goto oom; |
852 | 0 | } |
853 | | |
854 | 0 | result = hash(hashbuf, hashthis, strlen(hashthis)); |
855 | 0 | curlx_free(hashthis); |
856 | 0 | if(result) |
857 | 0 | goto oom; |
858 | 0 | convert_to_ascii(hashbuf, ha1); |
859 | |
|
860 | 0 | if(digest->algo & SESSION_ALGO) { |
861 | | /* nonce and cnonce are OUTSIDE the hash */ |
862 | 0 | char *tmp = curl_maprintf("%s:%s:%s", ha1, digest->nonce, digest->cnonce); |
863 | 0 | if(!tmp) { |
864 | 0 | result = CURLE_OUT_OF_MEMORY; |
865 | 0 | goto oom; |
866 | 0 | } |
867 | | |
868 | 0 | result = hash(hashbuf, tmp, strlen(tmp)); |
869 | 0 | curlx_free(tmp); |
870 | 0 | if(result) |
871 | 0 | goto oom; |
872 | 0 | convert_to_ascii(hashbuf, ha1); |
873 | 0 | } |
874 | | |
875 | | /* If the "qop" directive's value is "auth" or is unspecified, then A2 is: |
876 | | |
877 | | A2 = Method ":" digest-uri-value |
878 | | |
879 | | If the "qop" value is "auth-int", then A2 is: |
880 | | |
881 | | A2 = Method ":" digest-uri-value ":" H(entity-body) |
882 | | |
883 | | (The "Method" value is the HTTP request method as specified in section |
884 | | 5.1.1 of RFC 2616) |
885 | | */ |
886 | | |
887 | 0 | uri_quoted = auth_digest_string_quoted((const char *)uripath); |
888 | 0 | if(!uri_quoted) { |
889 | 0 | result = CURLE_OUT_OF_MEMORY; |
890 | 0 | goto oom; |
891 | 0 | } |
892 | | |
893 | 0 | hashthis = curl_maprintf("%s:%s", request, uripath); |
894 | 0 | if(!hashthis) { |
895 | 0 | result = CURLE_OUT_OF_MEMORY; |
896 | 0 | goto oom; |
897 | 0 | } |
898 | | |
899 | 0 | if(digest->qop && curl_strequal(digest->qop, "auth-int")) { |
900 | | /* We do not support auth-int for PUT or POST */ |
901 | 0 | char hashed[65]; |
902 | 0 | char *hashthis2; |
903 | |
|
904 | 0 | result = hash(hashbuf, "", 0); |
905 | 0 | if(result) { |
906 | 0 | curlx_free(hashthis); |
907 | 0 | goto oom; |
908 | 0 | } |
909 | 0 | convert_to_ascii(hashbuf, (unsigned char *)hashed); |
910 | |
|
911 | 0 | hashthis2 = curl_maprintf("%s:%s", hashthis, hashed); |
912 | 0 | curlx_free(hashthis); |
913 | 0 | hashthis = hashthis2; |
914 | 0 | if(!hashthis) { |
915 | 0 | result = CURLE_OUT_OF_MEMORY; |
916 | 0 | goto oom; |
917 | 0 | } |
918 | 0 | } |
919 | | |
920 | 0 | result = hash(hashbuf, hashthis, strlen(hashthis)); |
921 | 0 | curlx_free(hashthis); |
922 | 0 | if(result) |
923 | 0 | goto oom; |
924 | 0 | convert_to_ascii(hashbuf, ha2); |
925 | |
|
926 | 0 | if(digest->qop) |
927 | 0 | hashthis = curl_maprintf("%s:%s:%08x:%s:%s:%s", ha1, digest->nonce, |
928 | 0 | (unsigned int)digest->nc, digest->cnonce, |
929 | 0 | digest->qop, ha2); |
930 | 0 | else |
931 | 0 | hashthis = curl_maprintf("%s:%s:%s", ha1, digest->nonce, ha2); |
932 | |
|
933 | 0 | if(!hashthis) { |
934 | 0 | result = CURLE_OUT_OF_MEMORY; |
935 | 0 | goto oom; |
936 | 0 | } |
937 | | |
938 | 0 | result = hash(hashbuf, hashthis, strlen(hashthis)); |
939 | 0 | curlx_free(hashthis); |
940 | 0 | if(result) |
941 | 0 | goto oom; |
942 | 0 | convert_to_ascii(hashbuf, request_digest); |
943 | | |
944 | | /* For test case 64 (snooped from a Mozilla 1.3a request) |
945 | | |
946 | | Authorization: Digest username="testuser", realm="testrealm", \ |
947 | | nonce="1053604145", uri="/64", response="c55f7f30d83d774a3d2dcacf725abaca" |
948 | | |
949 | | Digest parameters are all quoted strings. Username which is provided by |
950 | | the user needs double quotes and backslashes within it escaped. |
951 | | realm, nonce, and opaque needs backslashes as well as they were |
952 | | de-escaped when copied from request header. cnonce is generated with |
953 | | web-safe characters. uri is already percent encoded. nc is 8 hex |
954 | | characters. algorithm and qop with standard values only contain web-safe |
955 | | characters. |
956 | | */ |
957 | 0 | userp_quoted = auth_digest_string_quoted(digest->userhash ? userh : userp); |
958 | 0 | if(!userp_quoted) { |
959 | 0 | result = CURLE_OUT_OF_MEMORY; |
960 | 0 | goto oom; |
961 | 0 | } |
962 | 0 | if(digest->realm) |
963 | 0 | realm_quoted = auth_digest_string_quoted(digest->realm); |
964 | 0 | else { |
965 | 0 | realm_quoted = curlx_malloc(1); |
966 | 0 | if(realm_quoted) |
967 | 0 | realm_quoted[0] = 0; |
968 | 0 | } |
969 | 0 | if(!realm_quoted) { |
970 | 0 | result = CURLE_OUT_OF_MEMORY; |
971 | 0 | goto oom; |
972 | 0 | } |
973 | | |
974 | 0 | nonce_quoted = auth_digest_string_quoted(digest->nonce); |
975 | 0 | if(!nonce_quoted) { |
976 | 0 | result = CURLE_OUT_OF_MEMORY; |
977 | 0 | goto oom; |
978 | 0 | } |
979 | | |
980 | 0 | if(digest->qop) { |
981 | 0 | result = curlx_dyn_addf(&response, "username=\"%s\", " |
982 | 0 | "realm=\"%s\", " |
983 | 0 | "nonce=\"%s\", " |
984 | 0 | "uri=\"%s\", " |
985 | 0 | "cnonce=\"%s\", " |
986 | 0 | "nc=%08x, " |
987 | 0 | "qop=%s, " |
988 | 0 | "response=\"%s\"", |
989 | 0 | userp_quoted, |
990 | 0 | realm_quoted, |
991 | 0 | nonce_quoted, |
992 | 0 | uri_quoted, |
993 | 0 | digest->cnonce, |
994 | 0 | (unsigned int)digest->nc, |
995 | 0 | digest->qop, |
996 | 0 | request_digest); |
997 | | |
998 | | /* Increment nonce-count to use another nc value for the next request */ |
999 | 0 | digest->nc++; |
1000 | 0 | } |
1001 | 0 | else { |
1002 | 0 | result = curlx_dyn_addf(&response, "username=\"%s\", " |
1003 | 0 | "realm=\"%s\", " |
1004 | 0 | "nonce=\"%s\", " |
1005 | 0 | "uri=\"%s\", " |
1006 | 0 | "response=\"%s\"", |
1007 | 0 | userp_quoted, |
1008 | 0 | realm_quoted, |
1009 | 0 | nonce_quoted, |
1010 | 0 | uri_quoted, |
1011 | 0 | request_digest); |
1012 | 0 | } |
1013 | 0 | if(result) |
1014 | 0 | goto oom; |
1015 | | |
1016 | | /* Add the optional fields */ |
1017 | 0 | if(digest->opaque) { |
1018 | | /* Append the opaque */ |
1019 | 0 | char *opaque_quoted = auth_digest_string_quoted(digest->opaque); |
1020 | 0 | if(!opaque_quoted) { |
1021 | 0 | result = CURLE_OUT_OF_MEMORY; |
1022 | 0 | goto oom; |
1023 | 0 | } |
1024 | 0 | result = curlx_dyn_addf(&response, ", opaque=\"%s\"", opaque_quoted); |
1025 | 0 | curlx_free(opaque_quoted); |
1026 | 0 | if(result) |
1027 | 0 | goto oom; |
1028 | 0 | } |
1029 | | |
1030 | 0 | if(digest->algorithm) { |
1031 | | /* Append the algorithm */ |
1032 | 0 | result = curlx_dyn_addf(&response, ", algorithm=%s", digest->algorithm); |
1033 | 0 | if(result) |
1034 | 0 | goto oom; |
1035 | 0 | } |
1036 | | |
1037 | 0 | if(digest->userhash) { |
1038 | | /* Append the userhash */ |
1039 | 0 | result = curlx_dyn_add(&response, ", userhash=true"); |
1040 | 0 | if(result) |
1041 | 0 | goto oom; |
1042 | 0 | } |
1043 | | |
1044 | | /* Return the output */ |
1045 | 0 | *outptr = curlx_dyn_ptr(&response); |
1046 | 0 | *outlen = curlx_dyn_len(&response); |
1047 | 0 | result = CURLE_OK; |
1048 | |
|
1049 | 0 | oom: |
1050 | 0 | curlx_free(nonce_quoted); |
1051 | 0 | curlx_free(realm_quoted); |
1052 | 0 | curlx_free(uri_quoted); |
1053 | 0 | curlx_free(userp_quoted); |
1054 | 0 | if(result) |
1055 | 0 | curlx_dyn_free(&response); |
1056 | 0 | return result; |
1057 | 0 | } |
1058 | | #endif /* CURL_HAVE_MD5 || CURL_HAVE_SHA256 || CURL_HAVE_SHA512_256 */ |
1059 | | |
1060 | | /* |
1061 | | * Curl_auth_create_digest_http_message() |
1062 | | * |
1063 | | * This is used to generate an HTTP DIGEST response message ready for sending |
1064 | | * to the recipient. |
1065 | | * |
1066 | | * Parameters: |
1067 | | * |
1068 | | * data [in] - The session handle. |
1069 | | * userp [in] - The username. |
1070 | | * passwdp [in] - The user's password. |
1071 | | * request [in] - The HTTP request. |
1072 | | * uripath [in] - The path of the HTTP uri. |
1073 | | * digest [in/out] - The digest data struct being used and modified. |
1074 | | * outptr [in/out] - The address where a pointer to newly allocated memory |
1075 | | * holding the result is stored upon completion. |
1076 | | * outlen [out] - The length of the output message. |
1077 | | * |
1078 | | * Returns CURLE_OK on success. |
1079 | | */ |
1080 | | CURLcode Curl_auth_create_digest_http_message(struct Curl_easy *data, |
1081 | | struct Curl_creds *creds, |
1082 | | const unsigned char *request, |
1083 | | const unsigned char *uripath, |
1084 | | struct digestdata *digest, |
1085 | | char **outptr, size_t *outlen) |
1086 | 0 | { |
1087 | 0 | (void)data; |
1088 | 0 | (void)creds; |
1089 | 0 | (void)request; |
1090 | 0 | (void)uripath; |
1091 | 0 | (void)digest; |
1092 | 0 | (void)outptr; |
1093 | 0 | (void)outlen; |
1094 | |
|
1095 | 0 | #ifdef CURL_HAVE_MD5 |
1096 | 0 | if(digest->algo == ALGO_MD5 || |
1097 | 0 | digest->algo == ALGO_MD5SESS) |
1098 | 0 | return auth_create_digest_http_message(data, creds, |
1099 | 0 | request, uripath, digest, |
1100 | 0 | outptr, outlen, |
1101 | 0 | auth_digest_md5_to_ascii, |
1102 | 0 | Curl_md5it); |
1103 | 0 | #endif |
1104 | 0 | #ifdef CURL_HAVE_SHA256 |
1105 | 0 | if(digest->algo == ALGO_SHA256 || |
1106 | 0 | digest->algo == ALGO_SHA256SESS) |
1107 | 0 | return auth_create_digest_http_message(data, creds, |
1108 | 0 | request, uripath, digest, |
1109 | 0 | outptr, outlen, |
1110 | 0 | auth_digest_sha256_to_ascii, |
1111 | 0 | Curl_sha256it); |
1112 | 0 | #endif |
1113 | 0 | #ifdef CURL_HAVE_SHA512_256 |
1114 | 0 | if(digest->algo == ALGO_SHA512_256 || |
1115 | 0 | digest->algo == ALGO_SHA512_256SESS) |
1116 | 0 | return auth_create_digest_http_message(data, creds, |
1117 | 0 | request, uripath, digest, |
1118 | 0 | outptr, outlen, |
1119 | 0 | auth_digest_sha256_to_ascii, |
1120 | 0 | Curl_sha512_256it); |
1121 | 0 | #endif |
1122 | | |
1123 | | /* Should be unreachable */ |
1124 | 0 | return CURLE_BAD_CONTENT_ENCODING; |
1125 | 0 | } |
1126 | | |
1127 | | /* |
1128 | | * Curl_auth_digest_cleanup() |
1129 | | * |
1130 | | * This is used to clean up the digest specific data. |
1131 | | * |
1132 | | * Parameters: |
1133 | | * |
1134 | | * digest [in/out] - The digest data struct being cleaned up. |
1135 | | * |
1136 | | */ |
1137 | | void Curl_auth_digest_cleanup(struct digestdata *digest) |
1138 | 108 | { |
1139 | 108 | Curl_peer_unlink(&digest->origin); |
1140 | 108 | Curl_creds_unlink(&digest->creds); |
1141 | 108 | curlx_safefree(digest->nonce); |
1142 | 108 | curlx_safefree(digest->cnonce); |
1143 | 108 | curlx_safefree(digest->realm); |
1144 | 108 | curlx_safefree(digest->opaque); |
1145 | 108 | curlx_safefree(digest->qop); |
1146 | 108 | curlx_safefree(digest->algorithm); |
1147 | | |
1148 | 108 | digest->nc = 0; |
1149 | 108 | digest->algo = ALGO_MD5; /* default algorithm */ |
1150 | 108 | digest->stale = FALSE; /* default means normal, not stale */ |
1151 | | digest->userhash = FALSE; |
1152 | 108 | } |
1153 | | #endif /* !USE_WINDOWS_SSPI */ |
1154 | | |
1155 | | #endif /* !CURL_DISABLE_DIGEST_AUTH */ |