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