aboutsummaryrefslogtreecommitdiff
path: root/src/microhttpd/test_dauth_userhash.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/microhttpd/test_dauth_userhash.c')
-rw-r--r--src/microhttpd/test_dauth_userhash.c529
1 files changed, 529 insertions, 0 deletions
diff --git a/src/microhttpd/test_dauth_userhash.c b/src/microhttpd/test_dauth_userhash.c
new file mode 100644
index 00000000..6e525773
--- /dev/null
+++ b/src/microhttpd/test_dauth_userhash.c
@@ -0,0 +1,529 @@
1/*
2 This file is part of libmicrohttpd
3 Copyright (C) 2022 Evgeny Grin (Karlson2)
4
5 This test tool is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2, or
8 (at your option) any later version.
9
10 This test tool is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19
20/**
21 * @file microhttpd/test_dauth_userhash.c
22 * @brief Tests for Digest Auth calculations of userhash
23 * @author Karlson2k (Evgeny Grin)
24 */
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include "microhttpd.h"
30#include "test_helpers.h"
31
32static int verbose = 1; /* verbose level (0-1)*/
33
34/* Declarations and data */
35
36struct data_md5
37{
38 unsigned int line_num;
39 const char *const username;
40 const char *const realm;
41 const uint8_t hash[MHD_MD5_DIGEST_SIZE];
42};
43
44
45static const struct data_md5 md5_tests[] = {
46 {__LINE__,
47 "u", "r",
48 {0xba, 0x84, 0xbe, 0x20, 0x3f, 0xdf, 0xc7, 0xd3, 0x4e, 0x05, 0x4a, 0x76,
49 0xd2, 0x85, 0xd0, 0xc9}},
50 {__LINE__,
51 "testuser", "testrealm",
52 {0xab, 0xae, 0x15, 0x95, 0x24, 0xe5, 0x17, 0xbf, 0x48, 0xf4, 0x4a, 0xab,
53 0xfe, 0xb9, 0x37, 0x40}},
54 {__LINE__,
55 "test_user", "TestRealm", /* Values from testcurl/test_digestauth2.c */
56 {0xc5, 0x3c, 0x60, 0x15, 0x03, 0xff, 0x17, 0x6f, 0x18, 0xf6, 0x23, 0x72,
57 0x5f, 0xba, 0x42, 0x81}},
58 {__LINE__,
59 "Mufasa", "myhost@testrealm.com",
60 {0x26, 0x45, 0xae, 0x13, 0xd1, 0xa2, 0xa6, 0x9e, 0xd2, 0x6d, 0xd2, 0x1a,
61 0xa5, 0x52, 0x86, 0xe3}},
62 {__LINE__,
63 "Mufasa", "myhost@example.com",
64 {0x4f, 0xc8, 0x64, 0x02, 0xd7, 0x18, 0x5d, 0x7b, 0x38, 0xd4, 0x38, 0xad,
65 0xd5, 0x5a, 0x35, 0x84}},
66 {__LINE__,
67 "Mufasa", "http-auth@example.org",
68 {0x42, 0x38, 0xf3, 0xa1, 0x61, 0x67, 0x37, 0x3f, 0xeb, 0xb9, 0xbc, 0x4d,
69 0x43, 0xdb, 0x9c, 0xc4}},
70 {__LINE__,
71 "J" "\xC3\xA4" "s" "\xC3\xB8" "n Doe" /* "Jäsøn Doe" */, "api@example.org",
72 {0x2e, 0x06, 0x3f, 0xa2, 0xc5, 0x4d, 0xea, 0x1c, 0x36, 0x80, 0x8b, 0x7a,
73 0x6e, 0x3b, 0x14, 0xc9}}
74};
75
76struct data_sha256
77{
78 unsigned int line_num;
79 const char *const username;
80 const char *const realm;
81 const uint8_t hash[MHD_SHA256_DIGEST_SIZE];
82};
83
84static const struct data_sha256 sha256_tests[] = {
85 {__LINE__,
86 "u", "r",
87 {0x1d, 0x8a, 0x03, 0xa6, 0xe2, 0x1a, 0x4c, 0xe7, 0x75, 0x06, 0x0e, 0xa5,
88 0x73, 0x60, 0x32, 0x9a, 0xc7, 0x50, 0xde, 0xa5, 0xd8, 0x47, 0x29, 0x7b,
89 0x42, 0xf0, 0xd4, 0x65, 0x39, 0xaf, 0x8a, 0xb2}},
90 {__LINE__,
91 "testuser", "testrealm",
92 {0x75, 0xaf, 0x8a, 0x35, 0x00, 0xf7, 0x71, 0xe5, 0x8a, 0x52, 0x09, 0x3a,
93 0x25, 0xe7, 0x90, 0x5d, 0x6e, 0x42, 0x8a, 0x51, 0x12, 0x85, 0xc1, 0x2e,
94 0xa1, 0x42, 0x0c, 0x73, 0x07, 0x8d, 0xfd, 0x61}},
95 {__LINE__,
96 "test_user", "TestRealm", /* Values from testcurl/test_digestauth2.c */
97 {0x09, 0x0c, 0x7e, 0x06, 0xb7, 0x7d, 0x66, 0x14, 0xcf, 0x5f, 0xe6, 0xca,
98 0xfa, 0x00, 0x4d, 0x2e, 0x5f, 0x8f, 0xb3, 0x6b, 0xa4, 0x5a, 0x0e, 0x35,
99 0xea, 0xcb, 0x2e, 0xb7, 0x72, 0x8f, 0x34, 0xde}},
100 {__LINE__,
101 "Mufasa", "myhost@testrealm.com",
102 {0x92, 0x9f, 0xac, 0x9e, 0x6c, 0x8b, 0x76, 0xcc, 0xab, 0xa9, 0xe0, 0x6f,
103 0xf6, 0x4d, 0xf2, 0x6f, 0xcb, 0x40, 0x56, 0x4c, 0x19, 0x9c, 0x32, 0xd9,
104 0xea, 0xd9, 0x12, 0x4b, 0x25, 0x34, 0xe1, 0xf9}},
105 {__LINE__,
106 "Mufasa", "myhost@example.com",
107 {0x97, 0x06, 0xf0, 0x07, 0x1c, 0xec, 0x05, 0x3f, 0x88, 0x22, 0xb6, 0x63,
108 0x69, 0xc4, 0xa4, 0x00, 0x39, 0x79, 0xb7, 0xe7, 0x42, 0xb7, 0x4e, 0x42,
109 0x59, 0x63, 0x57, 0xf4, 0xd3, 0x02, 0xae, 0x16}},
110 {__LINE__,
111 "Mufasa", "http-auth@example.org",
112 {0xa9, 0x47, 0xaa, 0xd2, 0x05, 0xe8, 0x0e, 0x42, 0x99, 0x58, 0xa3, 0x87,
113 0x39, 0x49, 0x44, 0xc6, 0xb4, 0x96, 0x30, 0x1e, 0x79, 0xf8, 0x9d, 0x35,
114 0xa4, 0xcc, 0x23, 0xb6, 0xee, 0x12, 0xb5, 0xb6}},
115 {__LINE__,
116 "J" "\xC3\xA4" "s" "\xC3\xB8" "n Doe" /* "Jäsøn Doe" */, "api@example.org",
117 {0x5a, 0x1a, 0x8a, 0x47, 0xdf, 0x5c, 0x29, 0x85, 0x51, 0xb9, 0xb4, 0x2b,
118 0xa9, 0xb0, 0x58, 0x35, 0x17, 0x4a, 0x5b, 0xd7, 0xd5, 0x11, 0xff, 0x7f,
119 0xe9, 0x19, 0x1d, 0x8e, 0x94, 0x6f, 0xc4, 0xe7}}
120};
121
122
123/*
124 * Helper functions
125 */
126
127/**
128 * Print bin as lower case hex
129 *
130 * @param bin binary data
131 * @param len number of bytes in bin
132 * @param hex pointer to len*2+1 bytes buffer
133 */
134static void
135bin2hex (const uint8_t *bin,
136 size_t len,
137 char *hex)
138{
139 while (len-- > 0)
140 {
141 unsigned int b1, b2;
142 b1 = (*bin >> 4) & 0xf;
143 *hex++ = (char) ((b1 > 9) ? (b1 + 'a' - 10) : (b1 + '0'));
144 b2 = *bin++ & 0xf;
145 *hex++ = (char) ((b2 > 9) ? (b2 + 'a' - 10) : (b2 + '0'));
146 }
147 *hex = 0;
148}
149
150
151/* Tests */
152
153static unsigned int
154check_md5 (const struct data_md5 *const data)
155{
156 static const enum MHD_DigestAuthAlgo3 algo3 = MHD_DIGEST_AUTH_ALGO3_MD5;
157 uint8_t hash_bin[MHD_MD5_DIGEST_SIZE];
158 char hash_hex[MHD_MD5_DIGEST_SIZE * 2 + 1];
159 char expected_hex[MHD_MD5_DIGEST_SIZE * 2 + 1];
160 const char *func_name;
161 unsigned int failed = 0;
162
163 func_name = "MHD_digest_auth_calc_userhash";
164 if (MHD_YES != MHD_digest_auth_calc_userhash (algo3,
165 data->username, data->realm,
166 hash_bin, sizeof(hash_bin)))
167 {
168 failed++;
169 fprintf (stderr,
170 "FAILED: %s() has not returned MHD_YES.\n",
171 func_name);
172 }
173 else if (0 != memcmp (hash_bin, data->hash, sizeof(data->hash)))
174 {
175 failed++;
176 bin2hex (hash_bin, sizeof(hash_bin), hash_hex);
177 bin2hex (data->hash, sizeof(data->hash), expected_hex);
178 fprintf (stderr,
179 "FAILED: %s() produced wrong hash. "
180 "Calculated digest %s, expected digest %s.\n",
181 func_name,
182 hash_hex, expected_hex);
183 }
184
185 func_name = "MHD_digest_auth_calc_userhash_hex";
186 if (MHD_YES !=
187 MHD_digest_auth_calc_userhash_hex (algo3,
188 data->username, data->realm,
189 hash_hex, sizeof(hash_hex)))
190 {
191 failed++;
192 fprintf (stderr,
193 "FAILED: %s() has not returned MHD_YES.\n",
194 func_name);
195 }
196 else if (sizeof(hash_hex) - 1 != strlen (hash_hex))
197 {
198 failed++;
199 fprintf (stderr,
200 "FAILED: %s produced hash with wrong length. "
201 "Calculated length %u, expected digest %u.\n",
202 func_name,
203 (unsigned) strlen (hash_hex),
204 (unsigned) (sizeof(hash_hex) - 1));
205 }
206 else
207 {
208 bin2hex (data->hash, sizeof(data->hash), expected_hex);
209 if (0 != memcmp (hash_hex, expected_hex, sizeof(hash_hex)))
210 {
211 failed++;
212 fprintf (stderr,
213 "FAILED: %s() produced wrong hash. "
214 "Calculated digest %s, expected digest %s.\n",
215 func_name,
216 hash_hex, expected_hex);
217 }
218 }
219
220 if (failed)
221 {
222 fprintf (stderr,
223 "The check failed for data located at line: %u.\n",
224 data->line_num);
225 fflush (stderr);
226 }
227 else if (verbose)
228 {
229 printf ("PASSED: check for data at line: %u.\n",
230 data->line_num);
231 }
232 return failed ? 1 : 0;
233}
234
235
236static unsigned int
237test_md5 (void)
238{
239 unsigned int num_failed = 0;
240 size_t i;
241
242 for (i = 0; i < sizeof(md5_tests) / sizeof(md5_tests[0]); i++)
243 num_failed += check_md5 (md5_tests + i);
244 return num_failed;
245}
246
247
248static unsigned int
249test_md5_failure (void)
250{
251 static const enum MHD_DigestAuthAlgo3 algo3 = MHD_DIGEST_AUTH_ALGO3_MD5;
252 uint8_t hash_bin[MHD_MD5_DIGEST_SIZE];
253 char hash_hex[MHD_MD5_DIGEST_SIZE * 2 + 1];
254 const char *func_name;
255 unsigned int failed = 0;
256
257 func_name = "MHD_digest_auth_calc_userhash";
258 if (MHD_NO != MHD_digest_auth_calc_userhash (algo3,
259 "u", "r",
260 hash_bin, sizeof(hash_bin) - 1))
261 {
262 failed++;
263 fprintf (stderr,
264 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
265 func_name, (unsigned) __LINE__);
266 }
267 if (MHD_NO != MHD_digest_auth_calc_userhash (algo3,
268 "u", "r",
269 hash_bin, 0))
270 {
271 failed++;
272 fprintf (stderr,
273 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
274 func_name, (unsigned) __LINE__);
275 }
276 if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_MD5))
277 {
278 if (MHD_NO != MHD_digest_auth_calc_userhash (algo3,
279 "u", "r",
280 hash_bin, sizeof(hash_bin)))
281 {
282 failed++;
283 fprintf (stderr,
284 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
285 func_name, (unsigned) __LINE__);
286 }
287 }
288
289 func_name = "MHD_digest_auth_calc_userhash_hex";
290 if (MHD_NO !=
291 MHD_digest_auth_calc_userhash_hex (algo3,
292 "u", "r",
293 hash_hex, sizeof(hash_hex) - 1))
294 {
295 failed++;
296 fprintf (stderr,
297 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
298 func_name, (unsigned) __LINE__);
299 }
300 if (MHD_NO !=
301 MHD_digest_auth_calc_userhash_hex (algo3,
302 "u", "r",
303 hash_hex, 0))
304 {
305 failed++;
306 fprintf (stderr,
307 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
308 func_name, (unsigned) __LINE__);
309 }
310 if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_MD5))
311 {
312 if (MHD_NO !=
313 MHD_digest_auth_calc_userhash_hex (algo3,
314 "u", "r",
315 hash_hex, sizeof(hash_hex)))
316 {
317 failed++;
318 fprintf (stderr,
319 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
320 func_name, (unsigned) __LINE__);
321 }
322 }
323
324 if (! failed && verbose)
325 {
326 printf ("PASSED: all checks with expected MHD_NO result near line: %u.\n",
327 (unsigned) __LINE__);
328 }
329 return failed ? 1 : 0;
330}
331
332
333static unsigned int
334check_sha256 (const struct data_sha256 *const data)
335{
336 static const enum MHD_DigestAuthAlgo3 algo3 = MHD_DIGEST_AUTH_ALGO3_SHA256;
337 uint8_t hash_bin[MHD_SHA256_DIGEST_SIZE];
338 char hash_hex[MHD_SHA256_DIGEST_SIZE * 2 + 1];
339 char expected_hex[MHD_SHA256_DIGEST_SIZE * 2 + 1];
340 const char *func_name;
341 unsigned int failed = 0;
342
343 func_name = "MHD_digest_auth_calc_userhash";
344 if (MHD_YES != MHD_digest_auth_calc_userhash (algo3,
345 data->username, data->realm,
346 hash_bin, sizeof(hash_bin)))
347 {
348 failed++;
349 fprintf (stderr,
350 "FAILED: %s() has not returned MHD_YES.\n",
351 func_name);
352 }
353 else if (0 != memcmp (hash_bin, data->hash, sizeof(data->hash)))
354 {
355 failed++;
356 bin2hex (hash_bin, sizeof(hash_bin), hash_hex);
357 bin2hex (data->hash, sizeof(data->hash), expected_hex);
358 fprintf (stderr,
359 "FAILED: %s() produced wrong hash. "
360 "Calculated digest %s, expected digest %s.\n",
361 func_name,
362 hash_hex, expected_hex);
363 }
364
365 func_name = "MHD_digest_auth_calc_userhash_hex";
366 if (MHD_YES !=
367 MHD_digest_auth_calc_userhash_hex (algo3,
368 data->username, data->realm,
369 hash_hex, sizeof(hash_hex)))
370 {
371 failed++;
372 fprintf (stderr,
373 "FAILED: %s() has not returned MHD_YES.\n",
374 func_name);
375 }
376 else if (sizeof(hash_hex) - 1 != strlen (hash_hex))
377 {
378 failed++;
379 fprintf (stderr,
380 "FAILED: %s produced hash with wrong length. "
381 "Calculated length %u, expected digest %u.\n",
382 func_name,
383 (unsigned) strlen (hash_hex),
384 (unsigned) (sizeof(hash_hex) - 1));
385 }
386 else
387 {
388 bin2hex (data->hash, sizeof(data->hash), expected_hex);
389 if (0 != memcmp (hash_hex, expected_hex, sizeof(hash_hex)))
390 {
391 failed++;
392 fprintf (stderr,
393 "FAILED: %s() produced wrong hash. "
394 "Calculated digest %s, expected digest %s.\n",
395 func_name,
396 hash_hex, expected_hex);
397 }
398 }
399
400 if (failed)
401 {
402 fprintf (stderr,
403 "The check failed for data located at line: %u.\n",
404 data->line_num);
405 fflush (stderr);
406 }
407 else if (verbose)
408 {
409 printf ("PASSED: check for data at line: %u.\n",
410 data->line_num);
411 }
412 return failed ? 1 : 0;
413}
414
415
416static unsigned int
417test_sha256 (void)
418{
419 unsigned int num_failed = 0;
420 size_t i;
421
422 for (i = 0; i < sizeof(sha256_tests) / sizeof(sha256_tests[0]); i++)
423 num_failed += check_sha256 (sha256_tests + i);
424 return num_failed;
425}
426
427
428static unsigned int
429test_sha256_failure (void)
430{
431 static const enum MHD_DigestAuthAlgo3 algo3 = MHD_DIGEST_AUTH_ALGO3_SHA256;
432 uint8_t hash_bin[MHD_SHA256_DIGEST_SIZE];
433 char hash_hex[MHD_SHA256_DIGEST_SIZE * 2 + 1];
434 const char *func_name;
435 unsigned int failed = 0;
436
437 func_name = "MHD_digest_auth_calc_userhash";
438 if (MHD_NO != MHD_digest_auth_calc_userhash (algo3,
439 "u", "r",
440 hash_bin, sizeof(hash_bin) - 1))
441 {
442 failed++;
443 fprintf (stderr,
444 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
445 func_name, (unsigned) __LINE__);
446 }
447 if (MHD_NO != MHD_digest_auth_calc_userhash (algo3,
448 "u", "r",
449 hash_bin, 0))
450 {
451 failed++;
452 fprintf (stderr,
453 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
454 func_name, (unsigned) __LINE__);
455 }
456 if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_MD5))
457 {
458 if (MHD_NO != MHD_digest_auth_calc_userhash (algo3,
459 "u", "r",
460 hash_bin, sizeof(hash_bin)))
461 {
462 failed++;
463 fprintf (stderr,
464 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
465 func_name, (unsigned) __LINE__);
466 }
467 }
468
469 func_name = "MHD_digest_auth_calc_userhash_hex";
470 if (MHD_NO !=
471 MHD_digest_auth_calc_userhash_hex (algo3,
472 "u", "r",
473 hash_hex, sizeof(hash_hex) - 1))
474 {
475 failed++;
476 fprintf (stderr,
477 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
478 func_name, (unsigned) __LINE__);
479 }
480 if (MHD_NO !=
481 MHD_digest_auth_calc_userhash_hex (algo3,
482 "u", "r",
483 hash_hex, 0))
484 {
485 failed++;
486 fprintf (stderr,
487 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
488 func_name, (unsigned) __LINE__);
489 }
490 if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_MD5))
491 {
492 if (MHD_NO !=
493 MHD_digest_auth_calc_userhash_hex (algo3,
494 "u", "r",
495 hash_hex, sizeof(hash_hex)))
496 {
497 failed++;
498 fprintf (stderr,
499 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
500 func_name, (unsigned) __LINE__);
501 }
502 }
503
504 if (! failed && verbose)
505 {
506 printf ("PASSED: all checks with expected MHD_NO result near line: %u.\n",
507 (unsigned) __LINE__);
508 }
509 return failed ? 1 : 0;
510}
511
512
513int
514main (int argc, char *argv[])
515{
516 unsigned int num_failed = 0;
517 (void) has_in_name; /* Mute compiler warning. */
518 if (has_param (argc, argv, "-s") || has_param (argc, argv, "--silent"))
519 verbose = 0;
520
521 if (MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_MD5))
522 num_failed += test_md5 ();
523 num_failed += test_md5_failure ();
524 if (MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_SHA256))
525 num_failed += test_sha256 ();
526 num_failed += test_sha256_failure ();
527
528 return num_failed ? 1 : 0;
529}