Greenbone Vulnerability Management Libraries  22.8.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>
12 Describe (PBA);
14 {
15 }
16 AfterEach (PBA)
17 {
18 }
19 
20 Ensure (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 }
25 Ensure (PBA, returns_true_on_phc_compliant_setting)
26 {
27  assert_true (pba_is_phc_compliant ("$password"));
28 }
29 Ensure (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 }
37 Ensure (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 }
49 Ensure (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 
64 Ensure (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 }
73 
74 Ensure (PBA, defaults)
75 {
76  int i;
77  struct PBASettings *settings = pba_init (NULL, 0, 0, NULL);
78  assert_equal (settings->count, 20000);
79  for (i = 0; i < MAX_PEPPER_SIZE; i++)
80  assert_equal_with_message (settings->pepper[i], 0,
81  "init_without_pepper_should_not_have_pepper");
82  assert_string_equal (settings->prefix, "$6$");
83  pba_finalize (settings);
84 }
85 Ensure (PBA, initialization)
86 {
87  int i;
88  struct PBASettings *settings = pba_init ("444", 3, 1, "$6$");
89  assert_equal (settings->count, 1);
90  for (i = 0; i < MAX_PEPPER_SIZE - 1; i++)
91  assert_equal_with_message (settings->pepper[i], '4',
92  "init_with_pepper_should_be_set");
93  assert_equal_with_message (settings->pepper[MAX_PEPPER_SIZE - 1], '\0',
94  "last_pepper_should_be_unset_by_pepper_3");
95  assert_string_equal (settings->prefix, "$6$");
96  pba_finalize (settings);
97  settings = pba_init ("444", MAX_PEPPER_SIZE + 1, 1, "$6$");
98  assert_equal_with_message (settings, NULL,
99  "should_fail_due_to_too_much_pepper");
100  settings = pba_init ("444", MAX_PEPPER_SIZE, 1, "$WALDFEE$");
101  assert_equal_with_message (settings, NULL,
102  "should_fail_due_to_unknown_prefix");
103 }
104 
105 Ensure (PBA, handle_md5_hash)
106 {
107  struct PBASettings *settings = pba_init (NULL, 0, 0, NULL);
108  char *hash;
109  assert_equal (gvm_auth_init (), 0);
110  hash = get_password_hashes ("admin");
111  assert_equal (pba_verify_hash (settings, hash, "admin"), UPDATE_RECOMMENDED);
112  pba_finalize (settings);
113 }
114 
115 int
116 main (int argc, char **argv)
117 {
118  TestSuite *suite;
119 
120  suite = create_test_suite ();
121 
122  add_test_with_context (suite, PBA,
123  returns_false_on_not_phc_compliant_setting);
124  add_test_with_context (suite, PBA, returns_true_on_phc_compliant_setting);
125  add_test_with_context (suite, PBA, returns_NULL_on_unsupport_settings);
126  add_test_with_context (suite, PBA, unique_hash_without_adding_used_pepper);
127  add_test_with_context (suite, PBA, verify_hash);
128  add_test_with_context (suite, PBA,
129  verify_hash_returns_invalid_on_np_hash_np_password);
130  add_test_with_context (suite, PBA, handle_md5_hash);
131  add_test_with_context (suite, PBA, defaults);
132  add_test_with_context (suite, PBA, initialization);
133  if (argc > 1)
134  return run_single_test (suite, argv[1], create_text_reporter ());
135  return run_test_suite (suite, create_text_reporter ());
136 }
main
int main(int argc, char **argv)
Definition: passwordbasedauthentication_tests.c:116
PBASettings::pepper
char pepper[MAX_PEPPER_SIZE]
Definition: passwordbasedauthentication.h:24
pba_finalize
void pba_finalize(struct PBASettings *settings)
Definition: passwordbasedauthentication.c:152
get_password_hashes
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:189
BeforeEach
BeforeEach(PBA)
Definition: passwordbasedauthentication_tests.c:13
pba_is_phc_compliant
static int pba_is_phc_compliant(const char *setting)
Definition: passwordbasedauthentication.c:158
MAX_PEPPER_SIZE
#define MAX_PEPPER_SIZE
Definition: passwordbasedauthentication.h:10
UPDATE_RECOMMENDED
@ UPDATE_RECOMMENDED
Definition: passwordbasedauthentication.h:48
gvm_auth_init
int gvm_auth_init(void)
Initializes Gcrypt.
Definition: authutils.c:89
pba_verify_hash
enum pba_rc pba_verify_hash(const struct PBASettings *setting, const char *hash, const char *password)
Definition: passwordbasedauthentication.c:215
AfterEach
AfterEach(PBA)
Definition: passwordbasedauthentication_tests.c:16
authutils.h
Authentication mechanism(s).
VALID
@ VALID
Definition: passwordbasedauthentication.h:47
passwordbasedauthentication.c
PBASettings
Definition: passwordbasedauthentication.h:23
PBASettings::count
unsigned int count
Definition: passwordbasedauthentication.h:25
pba_hash
char * pba_hash(struct PBASettings *setting, const char *password)
Definition: passwordbasedauthentication.c:168
Ensure
Ensure(PBA, returns_false_on_not_phc_compliant_setting)
Definition: passwordbasedauthentication_tests.c:20
PBASettings::prefix
char * prefix
Definition: passwordbasedauthentication.h:26
INVALID
@ INVALID
Definition: passwordbasedauthentication.h:49
pba_init
struct PBASettings * pba_init(const char *pepper, unsigned int pepper_size, unsigned int count, char *prefix)
Definition: passwordbasedauthentication.c:133
Describe
Describe(PBA)