summaryrefslogtreecommitdiffstats
path: root/tests/libdnssec/test_keystore_pkcs8.c
blob: b7c74e705f9aad99f489beeb4d87c53f701e90c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*  Copyright (C) 2018 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <string.h>
#include <tap/basic.h>
#include <stdbool.h>

#include "binary.h"
#include "crypto.h"
#include "error.h"
#include "key.h"
#include "keyid.h"
#include "keystore.h"

/* -- mock key store ------------------------------------------------------- */

static void *test_handle = (void *)0x42;

static bool test_handle_new_ok = false;
static int test_handle_new(void **handle_ptr)
{
	if (handle_ptr) {
		*handle_ptr = test_handle;
		test_handle_new_ok = true;
	}

	return DNSSEC_EOK;
}

static bool test_handle_free_ok = false;
static int test_handle_free(void *handle)
{
	test_handle_free_ok = (handle == test_handle);

	return DNSSEC_EOK;
}

static bool test_init_ok = false;
static int test_init(void *handle, const char *config)
{
	test_init_ok = (handle == test_handle && config && strcmp(config, "init config") == 0);

	return DNSSEC_EOK;
}

static bool test_open_ok = false;
static int test_open(void *handle, const char *config)
{
	test_open_ok = (handle == test_handle && config && strcmp(config, "open config") == 0);

	return DNSSEC_EOK;
}

static bool test_close_ok = false;
static int test_close(void *handle)
{
	test_close_ok = (handle == test_handle);

	return DNSSEC_EOK;
}

static bool test_write_ok = false;
static char *test_write_id = NULL;
static dnssec_binary_t test_write_pem = { 0 };
static int test_write(void *handle, const char *id, const dnssec_binary_t *pem)
{
	if (handle == test_handle && id && pem) {
		test_write_ok = true;
		test_write_id = dnssec_keyid_copy(id);
		dnssec_binary_dup(pem, &test_write_pem);
	}

	return DNSSEC_EOK;
}

static bool test_read_ok = false;
static char *test_read_id = NULL;
static int test_read(void *handle, const char *id, dnssec_binary_t *pem)
{
	if (handle == test_handle && id && pem) {
		test_read_ok = true;
		test_read_id = dnssec_keyid_copy(id);
		dnssec_binary_dup(&test_write_pem, pem);
	}

	return DNSSEC_EOK;
}

static bool test_list_ok = false;
static int test_list(void *handle, dnssec_list_t **list_ptr)
{
	if (handle == test_handle && list_ptr) {
		test_list_ok = true;
	}

	if (list_ptr) {
		*list_ptr = dnssec_list_new();
	}

	return DNSSEC_EOK;
}

static bool test_remove_ok = false;
static char *test_remove_id = NULL;
static int test_remove(void *handle, const char *id)
{
	test_remove_ok = (handle == test_handle && id);
	test_remove_id = dnssec_keyid_copy(id);

	return DNSSEC_EOK;
}

static const dnssec_keystore_pkcs8_functions_t test_store = {
	.handle_new  = test_handle_new,
	.handle_free = test_handle_free,
	.init        = test_init,
	.open        = test_open,
	.close       = test_close,
	.read        = test_read,
	.write       = test_write,
	.list        = test_list,
	.remove      = test_remove,
};

/* -- test plan ------------------------------------------------------------ */

int main(void)
{
	plan_lazy();

	dnssec_crypto_init();

	int r = 0;

	// create/init/open

	dnssec_keystore_t *store = NULL;
	r = dnssec_keystore_init_pkcs8_custom(&store, &test_store);
	ok(r == DNSSEC_EOK, "dnssec_keystore_init_pkcs8_custom()");
	ok(test_handle_new_ok, "test_handle_new_ok() called");

	r = dnssec_keystore_init(store, "init config");
	ok(r == DNSSEC_EOK, "dnssec_keystore_init()");
	ok(test_init_ok, "test_init() called");

	r = dnssec_keystore_open(store, "open config");
	ok(r == DNSSEC_EOK && test_open_ok, "dnssec_keystore_open()");
	ok(test_open_ok, "test_open() called");

	// write

	char *gen_id = NULL;
	r = dnssec_keystore_generate_key(store, DNSSEC_KEY_ALGORITHM_RSA_SHA256,
	                                 1024, &gen_id);
	ok(r == DNSSEC_EOK, "dnssec_keystore_generate_key()");
	ok(test_write_ok, "test_write() called");
	is_string(gen_id, test_write_id, "test_write() correct key ID");

	// read

	dnssec_key_t *key = NULL;
	dnssec_key_new(&key);
	dnssec_key_set_algorithm(key, DNSSEC_KEY_ALGORITHM_RSA_SHA256);
	r = dnssec_key_import_keystore(key, store, gen_id);
	ok(r == DNSSEC_EOK, "dnssec_key_import_keystore()");
	ok(test_read_ok, "test_read() called");
	is_string(gen_id, test_read_id, "test_read() correct key ID");
	dnssec_key_free(key);

	// remove

	r = dnssec_keystore_remove_key(store, gen_id);
	ok(r == DNSSEC_EOK, "dnssec_keystore_remove_key()");
	ok(test_remove_ok, "test_remove() called");
	is_string(gen_id, test_remove_id, "test_remove() correct key ID");

	// close

	r = dnssec_keystore_close(store);
	ok(r == DNSSEC_EOK, "dnssec_keystore_clse()");
	ok(test_close_ok, "test_close() called");

	// list

	dnssec_list_t *list = NULL;
	r = dnssec_keystore_list_keys(store, &list);
	ok(r == DNSSEC_EOK, "dnssec_keystore_list_keys()");
	ok(test_list_ok, "test_list() called");
	ok(list && dnssec_list_size(list) == 0, "dnssec_list() correct output");
	dnssec_list_free(list);

	// cleanup

	dnssec_keystore_deinit(store);
	ok(test_handle_free_ok, "test_handle_free() called");

	dnssec_crypto_cleanup();

	free(gen_id);
	free(test_write_id);
	dnssec_binary_free(&test_write_pem);
	free(test_read_id);
	free(test_remove_id);

	return 0;
}