Greenbone Vulnerability Management Libraries 22.32.0
passwordbasedauthentication_tests.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019-2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
6#include "authutils.h"
8
9#include <cgreen/cgreen.h>
10#include <cgreen/mocks.h>
11#include <string.h>
14{
15}
17{
18}
19
20Ensure (PBA, returns_false_on_not_phc_compliant_setting)
21{
22 assert_false (pba_is_phc_compliant ("$"));
23 assert_false (pba_is_phc_compliant ("password"));
24}
25Ensure (PBA, returns_true_on_phc_compliant_setting)
26{
27 assert_true (pba_is_phc_compliant ("$password"));
28}
29Ensure (PBA, returns_NULL_on_unsupport_settings)
30{
31 struct PBASettings setting = {"0000", 20000, "$6$"};
32 assert_false (pba_hash (NULL, "*password"));
33 assert_false (pba_hash (&setting, NULL));
34 setting.prefix = "$1$";
35 assert_false (pba_hash (&setting, "*password"));
36}
37Ensure (PBA, unique_hash_without_adding_used_pepper)
38{
39 struct PBASettings setting = {"4242", 20000, "$6$"};
40 char *cmp_hash, *hash;
41 hash = pba_hash (&setting, "*password");
42 assert_not_equal (hash, NULL);
43 assert_false (string_contains (hash, setting.pepper));
44 cmp_hash = pba_hash (&setting, "*password");
45 assert_string_not_equal (hash, cmp_hash);
46 free (hash);
47 free (cmp_hash);
48}
49Ensure (PBA, verify_hash)
50{
51 struct PBASettings setting = {"4242", 20000, "$6$"};
52 char *hash;
53 hash = pba_hash (&setting, "*password");
54 assert_not_equal (hash, NULL);
55 assert_equal (pba_verify_hash (&setting, hash, "*password"), VALID);
56 assert_equal (pba_verify_hash (&setting, hash, "*password1"), INVALID);
57 free (hash);
58 struct PBASettings setting_wo_pepper = {"\0\0\0\0", 20000, "$6$"};
59 hash = pba_hash (&setting_wo_pepper, "*password");
60 assert_equal (pba_verify_hash (&setting_wo_pepper, hash, "*password"), VALID);
61 free (hash);
62}
63
64Ensure (PBA, verify_hash_returns_invalid_on_np_hash_np_password)
65{
66 struct PBASettings setting = {"4242", 20000, "$6$"};
67 char *hash;
68 hash = pba_hash (&setting, "*password");
69 assert_not_equal (hash, NULL);
70 assert_equal (pba_verify_hash (&setting, NULL, "*password"), INVALID);
71 assert_equal (pba_verify_hash (&setting, hash, NULL), INVALID);
72 free (hash);
73}
74
75Ensure (PBA, defaults)
76{
77 int i;
78 struct PBASettings *settings = pba_init (NULL, 0, 0, NULL);
79 assert_equal (settings->count, 20000);
80 for (i = 0; i < MAX_PEPPER_SIZE; i++)
81 assert_equal_with_message (settings->pepper[i], 0,
82 "init_without_pepper_should_not_have_pepper");
83 assert_string_equal (settings->prefix, "$6$");
84 pba_finalize (settings);
85}
86Ensure (PBA, initialization)
87{
88 int i;
89 struct PBASettings *settings = pba_init ("444", 3, 1, "$6$");
90 assert_equal (settings->count, 1);
91 for (i = 0; i < MAX_PEPPER_SIZE - 1; i++)
92 assert_equal_with_message (settings->pepper[i], '4',
93 "init_with_pepper_should_be_set");
94 assert_equal_with_message (settings->pepper[MAX_PEPPER_SIZE - 1], '\0',
95 "last_pepper_should_be_unset_by_pepper_3");
96 assert_string_equal (settings->prefix, "$6$");
97 pba_finalize (settings);
98 settings = pba_init ("444", MAX_PEPPER_SIZE + 1, 1, "$6$");
99 assert_equal_with_message (settings, NULL,
100 "should_fail_due_to_too_much_pepper");
101 settings = pba_init ("444", MAX_PEPPER_SIZE, 1, "$WALDFEE$");
102 assert_equal_with_message (settings, NULL,
103 "should_fail_due_to_unknown_prefix");
104}
105
106Ensure (PBA, handle_md5_hash)
107{
108 struct PBASettings *settings = pba_init (NULL, 0, 0, NULL);
109 char *hash;
110 assert_equal (gvm_auth_init (), 0);
111 hash = get_password_hashes ("admin");
112 assert_equal (pba_verify_hash (settings, hash, "admin"), UPDATE_RECOMMENDED);
113 pba_finalize (settings);
114 g_free (hash);
115}
116
117int
118main (int argc, char **argv)
119{
120 int ret;
121 TestSuite *suite;
122
123 suite = create_test_suite ();
124
125 add_test_with_context (suite, PBA,
126 returns_false_on_not_phc_compliant_setting);
127 add_test_with_context (suite, PBA, returns_true_on_phc_compliant_setting);
128 add_test_with_context (suite, PBA, returns_NULL_on_unsupport_settings);
129 add_test_with_context (suite, PBA, unique_hash_without_adding_used_pepper);
130 add_test_with_context (suite, PBA, verify_hash);
131 add_test_with_context (suite, PBA,
132 verify_hash_returns_invalid_on_np_hash_np_password);
133 add_test_with_context (suite, PBA, handle_md5_hash);
134 add_test_with_context (suite, PBA, defaults);
135 add_test_with_context (suite, PBA, initialization);
136
137 if (argc > 1)
138 ret = run_single_test (suite, argv[1], create_text_reporter ());
139 else
140 ret = run_test_suite (suite, create_text_reporter ());
141
142 destroy_test_suite (suite);
143
144 return ret;
145}
int gvm_auth_init(void)
Initializes Gcrypt.
Definition authutils.c:109
gchar * get_password_hashes(const gchar *password)
Generate a pair of md5 hashes to be used in the "auth/hash" file for the user.
Definition authutils.c:210
Authentication mechanism(s).
static int pba_is_phc_compliant(const char *setting)
Check if a PBA settings is PHC compliant.
void pba_finalize(struct PBASettings *settings)
Cleanup PBA settings.
char * pba_hash(struct PBASettings *setting, const char *password)
Create a password hash.
struct PBASettings * pba_init(const char *pepper, unsigned int pepper_size, unsigned int count, char *prefix)
Init PBA.
enum pba_rc pba_verify_hash(const struct PBASettings *setting, const char *hash, const char *password)
Verify a password hash.
#define MAX_PEPPER_SIZE
Ensure(PBA, returns_false_on_not_phc_compliant_setting)
Describe(PBA)
int main(int argc, char **argv)
char pepper[MAX_PEPPER_SIZE]