]>
Commit | Line | Data |
---|---|---|
a937ce2d ZJS |
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2 | ||
83764f8d ZJS |
3 | #if HAVE_CRYPT_H |
4 | # include <crypt.h> | |
5 | #else | |
6 | # include <unistd.h> | |
7 | #endif | |
8 | ||
a937ce2d ZJS |
9 | #include "strv.h" |
10 | #include "tests.h" | |
11 | #include "libcrypt-util.h" | |
12 | ||
5d3fe6f7 ZJS |
13 | static int test_hash_password(void) { |
14 | log_info("/* %s */", __func__); | |
15 | ||
16 | /* As a warmup exercise, check if we can hash passwords. */ | |
17 | ||
18 | bool have_sane_hash = false; | |
19 | const char *hash; | |
20 | ||
21 | FOREACH_STRING(hash, | |
22 | "ew3bU1.hoKk4o", | |
23 | "$1$gc5rWpTB$wK1aul1PyBn9AX1z93stk1", | |
24 | "$2b$12$BlqcGkB/7BFvNMXKGxDea.5/8D6FTny.cbNcHW/tqcrcyo6ZJd8u2", | |
25 | "$5$lGhDrcrao9zb5oIK$05KlOVG3ocknx/ThreqXE/gk.XzFFBMTksc4t2CPDUD", | |
26 | "$6$c7wB/3GiRk0VHf7e$zXJ7hN0aLZapE.iO4mn/oHu6.prsXTUG/5k1AxpgR85ELolyAcaIGRgzfwJs3isTChMDBjnthZyaMCfCNxo9I.", | |
27 | "$y$j9T$$9cKOWsAm4m97WiYk61lPPibZpy3oaGPIbsL4koRe/XD") { | |
28 | int b; | |
29 | ||
30 | b = test_password_one(hash, "ppp"); | |
31 | log_info("%s: %s", hash, yes_no(b)); | |
32 | #if defined(XCRYPT_VERSION_MAJOR) | |
33 | /* xcrypt is supposed to always implement all methods. */ | |
34 | assert_se(b); | |
35 | #endif | |
36 | ||
37 | if (b && IN_SET(hash[1], '6', 'y')) | |
38 | have_sane_hash = true; | |
39 | } | |
40 | ||
41 | return have_sane_hash; | |
42 | } | |
43 | ||
a937ce2d ZJS |
44 | static void test_hash_password_full(void) { |
45 | log_info("/* %s */", __func__); | |
46 | ||
47 | _cleanup_free_ void *cd_data = NULL; | |
48 | const char *i; | |
49 | int cd_size = 0; | |
50 | ||
51 | log_info("sizeof(struct crypt_data): %zu bytes", sizeof(struct crypt_data)); | |
52 | ||
53 | for (unsigned c = 0; c < 2; c++) | |
6016b4b0 | 54 | FOREACH_STRING(i, "abc123", "h⸿sło") { |
a937ce2d ZJS |
55 | _cleanup_free_ char *hashed; |
56 | ||
57 | if (c == 0) | |
58 | assert_se(hash_password_full(i, &cd_data, &cd_size, &hashed) == 0); | |
59 | else | |
60 | assert_se(hash_password_full(i, NULL, NULL, &hashed) == 0); | |
61 | log_debug("\"%s\" → \"%s\"", i, hashed); | |
62 | log_info("crypt_r[a] buffer size: %i bytes", cd_size); | |
6016b4b0 ZJS |
63 | |
64 | assert_se(test_password_one(hashed, i) == true); | |
65 | assert_se(test_password_one(i, hashed) <= 0); /* We get an error for non-utf8 */ | |
66 | assert_se(test_password_one(hashed, "foobar") == false); | |
67 | assert_se(test_password_many(STRV_MAKE(hashed), i) == true); | |
68 | assert_se(test_password_many(STRV_MAKE(hashed), "foobar") == false); | |
69 | assert_se(test_password_many(STRV_MAKE(hashed, hashed, hashed), "foobar") == false); | |
70 | assert_se(test_password_many(STRV_MAKE("$y$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH", | |
71 | hashed, | |
72 | "$y$j9T$SAayASazWZIQeJd9AS02m/$"), | |
73 | i) == true); | |
35e22827 ZJS |
74 | assert_se(test_password_many(STRV_MAKE("$W$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH", /* no such method exists... */ |
75 | hashed, | |
76 | "$y$j9T$SAayASazWZIQeJd9AS02m/$"), | |
77 | i) == true); | |
6016b4b0 ZJS |
78 | assert_se(test_password_many(STRV_MAKE("$y$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH", |
79 | hashed, | |
80 | "$y$j9T$SAayASazWZIQeJd9AS02m/$"), | |
81 | "") == false); | |
35e22827 ZJS |
82 | assert_se(test_password_many(STRV_MAKE("$W$j9T$dlCXwkX0GC5L6B8Gf.4PN/$VCyEH", /* no such method exists... */ |
83 | hashed, | |
84 | "$y$j9T$SAayASazWZIQeJd9AS02m/$"), | |
85 | "") == false); | |
a937ce2d ZJS |
86 | } |
87 | } | |
88 | ||
89 | int main(int argc, char *argv[]) { | |
90 | test_setup_logging(LOG_DEBUG); | |
91 | ||
bd99297b ZJS |
92 | #if defined(__powerpc__) && !defined(XCRYPT_VERSION_MAJOR) |
93 | return log_tests_skipped("crypt_r() causes a buffer overflow on ppc64el, see https://github.com/systemd/systemd/pull/16981#issuecomment-691203787"); | |
94 | #endif | |
95 | ||
5d3fe6f7 ZJS |
96 | if (!test_hash_password()) |
97 | return log_tests_skipped("crypt doesn't support yescrypt or sha512crypt"); | |
98 | ||
a937ce2d ZJS |
99 | test_hash_password_full(); |
100 | ||
101 | return 0; | |
102 | } |