aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEvgeny Grin (Karlson2k) <k2k@narod.ru>2022-09-04 12:30:16 +0300
committerEvgeny Grin (Karlson2k) <k2k@narod.ru>2022-09-04 12:37:01 +0300
commit87fe3bdd88ff78267a72c81f23d89e8877a0baa5 (patch)
treef827a8e5bd9fddafe4825139bbe516646086f752 /src
parent62175acf7cdb5804da98cbfa0364a8b6b6cfed54 (diff)
downloadlibmicrohttpd-87fe3bdd88ff78267a72c81f23d89e8877a0baa5.tar.gz
libmicrohttpd-87fe3bdd88ff78267a72c81f23d89e8877a0baa5.zip
Added tests for userdigest and userhash calculations
Diffstat (limited to 'src')
-rw-r--r--src/microhttpd/.gitignore2
-rw-r--r--src/microhttpd/Makefile.am14
-rw-r--r--src/microhttpd/test_dauth_userdigest.c432
-rw-r--r--src/microhttpd/test_dauth_userhash.c529
4 files changed, 976 insertions, 1 deletions
diff --git a/src/microhttpd/.gitignore b/src/microhttpd/.gitignore
index 1353f1ec..8b16c7a9 100644
--- a/src/microhttpd/.gitignore
+++ b/src/microhttpd/.gitignore
@@ -83,3 +83,5 @@ test_postprocessor_md
83/test_str_base64 83/test_str_base64
84/test_str_pct 84/test_str_pct
85/test_str_bin_hex 85/test_str_bin_hex
86/test_dauth_userdigest
87/test_dauth_userhash
diff --git a/src/microhttpd/Makefile.am b/src/microhttpd/Makefile.am
index b4339f08..fbc545eb 100644
--- a/src/microhttpd/Makefile.am
+++ b/src/microhttpd/Makefile.am
@@ -263,7 +263,9 @@ check_PROGRAMS += \
263endif 263endif
264if ENABLE_DAUTH 264if ENABLE_DAUTH
265check_PROGRAMS += \ 265check_PROGRAMS += \
266 test_str_quote 266 test_str_quote \
267 test_dauth_userdigest \
268 test_dauth_userhash
267endif 269endif
268if ENABLE_BAUTH 270if ENABLE_BAUTH
269check_PROGRAMS += \ 271check_PROGRAMS += \
@@ -535,4 +537,14 @@ test_set_panic_SOURCES = \
535test_set_panic_LDADD = \ 537test_set_panic_LDADD = \
536 libmicrohttpd.la 538 libmicrohttpd.la
537 539
540test_dauth_userdigest_SOURCES = \
541 test_dauth_userdigest.c
542test_dauth_userdigest_LDADD = \
543 libmicrohttpd.la
544
545test_dauth_userhash_SOURCES = \
546 test_dauth_userhash.c
547test_dauth_userhash_LDADD = \
548 libmicrohttpd.la
549
538.PHONY: update-po-POTFILES.in 550.PHONY: update-po-POTFILES.in
diff --git a/src/microhttpd/test_dauth_userdigest.c b/src/microhttpd/test_dauth_userdigest.c
new file mode 100644
index 00000000..3af45156
--- /dev/null
+++ b/src/microhttpd/test_dauth_userdigest.c
@@ -0,0 +1,432 @@
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_userdigest.c
22 * @brief Tests for Digest Auth calculations of userdigest
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 char *const password;
42 const uint8_t hash[MHD_MD5_DIGEST_SIZE];
43};
44
45
46static const struct data_md5 md5_tests[] = {
47 {__LINE__,
48 "u", "r", "p",
49 {0x44, 0xad, 0xd2, 0x2b, 0x6f, 0x31, 0x79, 0xb7, 0x51, 0xea, 0xfd, 0x68,
50 0xee, 0x37, 0x0f, 0x7d}},
51 {__LINE__,
52 "testuser", "testrealm", "testpass",
53 {0xeb, 0xff, 0x22, 0x5e, 0x1c, 0xeb, 0x73, 0xe0, 0x26, 0xfc, 0xc6, 0x45,
54 0xaf, 0x3e, 0x84, 0xf6}},
55 {__LINE__, /* Values from testcurl/test_digestauth2.c */
56 "test_user", "TestRealm", "test pass",
57 {0xd8, 0xb4, 0xa6, 0xd0, 0x01, 0x13, 0x07, 0xb7, 0x67, 0x94, 0xea, 0x66,
58 0x86, 0x03, 0x6b, 0x43}},
59 {__LINE__,
60 "Mufasa", "myhost@testrealm.com", "CircleOfLife",
61 {0x7e, 0xbe, 0xcc, 0x07, 0x18, 0xa5, 0x4a, 0xb4, 0x7e, 0x21, 0x65, 0x69,
62 0x07, 0x66, 0x41, 0x6a}},
63 {__LINE__,
64 "Mufasa", "myhost@example.com", "Circle Of Life",
65 {0x6a, 0x6d, 0x4e, 0x7c, 0xd7, 0x15, 0x18, 0x68, 0xf9, 0xb8, 0xc7, 0xc8,
66 0xd1, 0xcd, 0xd4, 0xe0}},
67 {__LINE__,
68 "Mufasa", "http-auth@example.org", "Circle of Life",
69 {0x3d, 0x78, 0x80, 0x7d, 0xef, 0xe7, 0xde, 0x21, 0x57, 0xe2, 0xb0, 0xb6,
70 0x57, 0x3a, 0x85, 0x5f}},
71 {__LINE__,
72 "J" "\xC3\xA4" "s" "\xC3\xB8" "n Doe" /* "Jäsøn Doe" */,
73 "api@example.org", "Secret, or not?",
74 {0x83, 0xa3, 0xf7, 0xf6, 0xb8, 0x3f, 0x71, 0xc5, 0xc2, 0xeb, 0x7c, 0x6d,
75 0xd2, 0xdd, 0x4c, 0x4b}}
76};
77
78struct data_sha256
79{
80 unsigned int line_num;
81 const char *const username;
82 const char *const realm;
83 const char *const password;
84 const uint8_t hash[MHD_SHA256_DIGEST_SIZE];
85};
86
87static const struct data_sha256 sha256_tests[] = {
88 {__LINE__,
89 "u", "r", "p",
90 {0xdc, 0xb0, 0x21, 0x10, 0x2e, 0x49, 0x1e, 0x70, 0x1a, 0x4a, 0x23, 0x6d,
91 0xaa, 0x89, 0x23, 0xaf, 0x21, 0x61, 0x44, 0x7b, 0xce, 0x7b, 0xb7, 0x26,
92 0x0a, 0x35, 0x1e, 0xe8, 0x3e, 0x9f, 0x81, 0x54}},
93 {__LINE__,
94 "testuser", "testrealm", "testpass",
95 {0xa9, 0x2e, 0xf6, 0x3b, 0x3d, 0xec, 0x38, 0x95, 0xb0, 0x8f, 0x3d, 0x4d,
96 0x67, 0x33, 0xf0, 0x70, 0x74, 0xcb, 0xe6, 0xd4, 0xa0, 0x01, 0x27, 0xf5,
97 0x74, 0x1a, 0x77, 0x4f, 0x05, 0xf9, 0xd4, 0x99}},
98 {__LINE__, /* Values from testcurl/test_digestauth2.c */
99 "test_user", "TestRealm", "test pass",
100 {0xc3, 0x4e, 0x16, 0x5a, 0x17, 0x0f, 0xe5, 0xac, 0x04, 0xf1, 0x6e, 0x46,
101 0x48, 0x2b, 0xa0, 0xc6, 0x56, 0xc1, 0xfb, 0x8f, 0x66, 0xa6, 0xd6, 0x3f,
102 0x91, 0x12, 0xf8, 0x56, 0xa5, 0xec, 0x6d, 0x6d}},
103 {__LINE__,
104 "Mufasa", "myhost@testrealm.com", "CircleOfLife",
105 {0x8e, 0x64, 0x1f, 0xaa, 0x71, 0x7d, 0x20, 0x70, 0x5a, 0xd7, 0x3c, 0x54,
106 0xfb, 0x04, 0x9e, 0x32, 0x6a, 0xe1, 0x1c, 0x80, 0xd6, 0x05, 0x9f, 0xc3,
107 0x7e, 0xbb, 0x2d, 0x7b, 0x60, 0x6c, 0x11, 0xb9}},
108 {__LINE__,
109 "Mufasa", "myhost@example.com", "Circle Of Life",
110 {0x8b, 0xc5, 0xa8, 0xed, 0xe3, 0x02, 0x15, 0x6b, 0x9f, 0x51, 0xce, 0x97,
111 0x81, 0xb5, 0x26, 0xff, 0x99, 0x29, 0x0b, 0xb2, 0xc3, 0xe4, 0x41, 0x71,
112 0x8e, 0xa3, 0xa1, 0x7e, 0x5a, 0xd9, 0xd6, 0x49}},
113 {__LINE__,
114 "Mufasa", "http-auth@example.org", "Circle of Life",
115 {0x79, 0x87, 0xc6, 0x4c, 0x30, 0xe2, 0x5f, 0x1b, 0x74, 0xbe, 0x53, 0xf9,
116 0x66, 0xb4, 0x9b, 0x90, 0xf2, 0x80, 0x8a, 0xa9, 0x2f, 0xaf, 0x9a, 0x00,
117 0x26, 0x23, 0x92, 0xd7, 0xb4, 0x79, 0x42, 0x32}},
118 {__LINE__,
119 "J" "\xC3\xA4" "s" "\xC3\xB8" "n Doe" /* "Jäsøn Doe" */,
120 "api@example.org", "Secret, or not?",
121 {0xfd, 0x0b, 0xe3, 0x93, 0x9d, 0xca, 0x4b, 0x5c, 0x2d, 0x46, 0xe8, 0xfa,
122 0x6a, 0x3d, 0x16, 0xdb, 0xea, 0x82, 0x47, 0x4c, 0xb9, 0xa5, 0x88, 0xd4,
123 0xcb, 0x14, 0x9c, 0x54, 0xf3, 0x7c, 0xff, 0x37}}
124};
125
126
127/*
128 * Helper functions
129 */
130
131/**
132 * Print bin as lower case hex
133 *
134 * @param bin binary data
135 * @param len number of bytes in bin
136 * @param hex pointer to len*2+1 bytes buffer
137 */
138static void
139bin2hex (const uint8_t *bin,
140 size_t len,
141 char *hex)
142{
143 while (len-- > 0)
144 {
145 unsigned int b1, b2;
146 b1 = (*bin >> 4) & 0xf;
147 *hex++ = (char) ((b1 > 9) ? (b1 + 'a' - 10) : (b1 + '0'));
148 b2 = *bin++ & 0xf;
149 *hex++ = (char) ((b2 > 9) ? (b2 + 'a' - 10) : (b2 + '0'));
150 }
151 *hex = 0;
152}
153
154
155/* Tests */
156
157static unsigned int
158check_md5 (const struct data_md5 *const data)
159{
160 static const enum MHD_DigestAuthAlgo3 algo3 = MHD_DIGEST_AUTH_ALGO3_MD5;
161 uint8_t hash_bin[MHD_MD5_DIGEST_SIZE];
162 char hash_hex[MHD_MD5_DIGEST_SIZE * 2 + 1];
163 char expected_hex[MHD_MD5_DIGEST_SIZE * 2 + 1];
164 const char *func_name;
165 unsigned int failed = 0;
166
167 func_name = "MHD_digest_auth_calc_userdigest";
168 if (MHD_YES != MHD_digest_auth_calc_userdigest (algo3,
169 data->username,
170 data->realm,
171 data->password,
172 hash_bin, sizeof(hash_bin)))
173 {
174 failed++;
175 fprintf (stderr,
176 "FAILED: %s() has not returned MHD_YES.\n",
177 func_name);
178 }
179 else if (0 != memcmp (hash_bin, data->hash, sizeof(data->hash)))
180 {
181 failed++;
182 bin2hex (hash_bin, sizeof(hash_bin), hash_hex);
183 bin2hex (data->hash, sizeof(data->hash), expected_hex);
184 fprintf (stderr,
185 "FAILED: %s() produced wrong hash. "
186 "Calculated digest %s, expected digest %s.\n",
187 func_name,
188 hash_hex, expected_hex);
189 }
190
191 if (failed)
192 {
193 fprintf (stderr,
194 "The check failed for data located at line: %u.\n",
195 data->line_num);
196 fflush (stderr);
197 }
198 else if (verbose)
199 {
200 printf ("PASSED: check for data at line: %u.\n",
201 data->line_num);
202 }
203 return failed ? 1 : 0;
204}
205
206
207static unsigned int
208test_md5 (void)
209{
210 unsigned int num_failed = 0;
211 size_t i;
212
213 for (i = 0; i < sizeof(md5_tests) / sizeof(md5_tests[0]); i++)
214 num_failed += check_md5 (md5_tests + i);
215 return num_failed;
216}
217
218
219static unsigned int
220test_md5_failure (void)
221{
222 static const enum MHD_DigestAuthAlgo3 algo3 = MHD_DIGEST_AUTH_ALGO3_MD5;
223 uint8_t hash_bin[MHD_MD5_DIGEST_SIZE];
224 const char *func_name;
225 unsigned int failed = 0;
226
227 func_name = "MHD_digest_auth_calc_userdigest";
228 if (MHD_NO != MHD_digest_auth_calc_userdigest (algo3,
229 "u", "r", "p",
230 hash_bin, sizeof(hash_bin)
231 - 1))
232 {
233 failed++;
234 fprintf (stderr,
235 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
236 func_name, (unsigned) __LINE__);
237 }
238 if (MHD_NO != MHD_digest_auth_calc_userdigest (algo3,
239 "u", "r", "p",
240 hash_bin, 0))
241 {
242 failed++;
243 fprintf (stderr,
244 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
245 func_name, (unsigned) __LINE__);
246 }
247 if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_MD5))
248 {
249 if (MHD_NO != MHD_digest_auth_calc_userdigest (algo3,
250 "u", "r", "p",
251 hash_bin, sizeof(hash_bin)))
252 {
253 failed++;
254 fprintf (stderr,
255 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
256 func_name, (unsigned) __LINE__);
257 }
258 }
259
260 if (! failed && verbose)
261 {
262 printf ("PASSED: all checks with expected MHD_NO result near line: %u.\n",
263 (unsigned) __LINE__);
264 }
265 return failed ? 1 : 0;
266}
267
268
269static unsigned int
270check_sha256 (const struct data_sha256 *const data)
271{
272 static const enum MHD_DigestAuthAlgo3 algo3 = MHD_DIGEST_AUTH_ALGO3_SHA256;
273 uint8_t hash_bin[MHD_SHA256_DIGEST_SIZE];
274 char hash_hex[MHD_SHA256_DIGEST_SIZE * 2 + 1];
275 char expected_hex[MHD_SHA256_DIGEST_SIZE * 2 + 1];
276 const char *func_name;
277 unsigned int failed = 0;
278
279 func_name = "MHD_digest_auth_calc_userdigest";
280 if (MHD_YES != MHD_digest_auth_calc_userdigest (algo3,
281 data->username,
282 data->realm,
283 data->password,
284 hash_bin, sizeof(hash_bin)))
285 {
286 failed++;
287 fprintf (stderr,
288 "FAILED: %s() has not returned MHD_YES.\n",
289 func_name);
290 }
291 else if (0 != memcmp (hash_bin, data->hash, sizeof(data->hash)))
292 {
293 failed++;
294 bin2hex (hash_bin, sizeof(hash_bin), hash_hex);
295 bin2hex (data->hash, sizeof(data->hash), expected_hex);
296 fprintf (stderr,
297 "FAILED: %s() produced wrong hash. "
298 "Calculated digest %s, expected digest %s.\n",
299 func_name,
300 hash_hex, expected_hex);
301 }
302
303 if (failed)
304 {
305 fprintf (stderr,
306 "The check failed for data located at line: %u.\n",
307 data->line_num);
308 fflush (stderr);
309 }
310 else if (verbose)
311 {
312 printf ("PASSED: check for data at line: %u.\n",
313 data->line_num);
314 }
315 return failed ? 1 : 0;
316}
317
318
319static unsigned int
320test_sha256 (void)
321{
322 unsigned int num_failed = 0;
323 size_t i;
324
325 for (i = 0; i < sizeof(sha256_tests) / sizeof(sha256_tests[0]); i++)
326 num_failed += check_sha256 (sha256_tests + i);
327 return num_failed;
328}
329
330
331static unsigned int
332test_sha256_failure (void)
333{
334 static const enum MHD_DigestAuthAlgo3 algo3 = MHD_DIGEST_AUTH_ALGO3_SHA256;
335 uint8_t hash_bin[MHD_SHA256_DIGEST_SIZE];
336 char hash_hex[MHD_SHA256_DIGEST_SIZE * 2 + 1];
337 const char *func_name;
338 unsigned int failed = 0;
339
340 func_name = "MHD_digest_auth_calc_userhash";
341 if (MHD_NO != MHD_digest_auth_calc_userhash (algo3,
342 "u", "r",
343 hash_bin, sizeof(hash_bin) - 1))
344 {
345 failed++;
346 fprintf (stderr,
347 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
348 func_name, (unsigned) __LINE__);
349 }
350 if (MHD_NO != MHD_digest_auth_calc_userhash (algo3,
351 "u", "r",
352 hash_bin, 0))
353 {
354 failed++;
355 fprintf (stderr,
356 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
357 func_name, (unsigned) __LINE__);
358 }
359 if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_MD5))
360 {
361 if (MHD_NO != MHD_digest_auth_calc_userhash (algo3,
362 "u", "r",
363 hash_bin, sizeof(hash_bin)))
364 {
365 failed++;
366 fprintf (stderr,
367 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
368 func_name, (unsigned) __LINE__);
369 }
370 }
371
372 func_name = "MHD_digest_auth_calc_userhash_hex";
373 if (MHD_NO !=
374 MHD_digest_auth_calc_userhash_hex (algo3,
375 "u", "r",
376 hash_hex, sizeof(hash_hex) - 1))
377 {
378 failed++;
379 fprintf (stderr,
380 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
381 func_name, (unsigned) __LINE__);
382 }
383 if (MHD_NO !=
384 MHD_digest_auth_calc_userhash_hex (algo3,
385 "u", "r",
386 hash_hex, 0))
387 {
388 failed++;
389 fprintf (stderr,
390 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
391 func_name, (unsigned) __LINE__);
392 }
393 if (MHD_NO == MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_MD5))
394 {
395 if (MHD_NO !=
396 MHD_digest_auth_calc_userhash_hex (algo3,
397 "u", "r",
398 hash_hex, sizeof(hash_hex)))
399 {
400 failed++;
401 fprintf (stderr,
402 "FAILED: %s() has not returned MHD_NO at line: %u.\n",
403 func_name, (unsigned) __LINE__);
404 }
405 }
406
407 if (! failed && verbose)
408 {
409 printf ("PASSED: all checks with expected MHD_NO result near line: %u.\n",
410 (unsigned) __LINE__);
411 }
412 return failed ? 1 : 0;
413}
414
415
416int
417main (int argc, char *argv[])
418{
419 unsigned int num_failed = 0;
420 (void) has_in_name; /* Mute compiler warning. */
421 if (has_param (argc, argv, "-s") || has_param (argc, argv, "--silent"))
422 verbose = 0;
423
424 if (MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_MD5))
425 num_failed += test_md5 ();
426 num_failed += test_md5_failure ();
427 if (MHD_is_feature_supported (MHD_FEATURE_DIGEST_AUTH_SHA256))
428 num_failed += test_sha256 ();
429 num_failed += test_sha256_failure ();
430
431 return num_failed ? 1 : 0;
432}
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}