pkcs11.c 8.8 KB
Newer Older
1
/*  Copyright (C) 2022 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
Jan Včelák's avatar
Jan Včelák committed
2
3
4
5
6
7
8
9
10
11
12
13

    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
14
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
 */
Jan Včelák's avatar
Jan Včelák committed
16

17
#include <gnutls/gnutls.h>
18
#include <pthread.h>
19

20
#include "contrib/string.h"
21
22
23
24
25
26
#include "libdnssec/error.h"
#include "libdnssec/keyid.h"
#include "libdnssec/shared/keyid_gnutls.h"
#include "libdnssec/keystore.h"
#include "libdnssec/keystore/internal.h"
#include "libdnssec/p11/p11.h"
Daniel Salzman's avatar
Daniel Salzman committed
27
#include "libdnssec/pem.h"
28
#include "libdnssec/shared/shared.h"
29

30
#ifdef ENABLE_PKCS11
31

32
33
34
35
36
37
struct pkcs11_ctx {
	char *url;
};

typedef struct pkcs11_ctx pkcs11_ctx_t;

38
39
40
/*!
 * Flags used when generating/import key into the token.
 */
41
42
static const int TOKEN_ADD_FLAGS = GNUTLS_PKCS11_OBJ_FLAG_MARK_SENSITIVE
				 | GNUTLS_PKCS11_OBJ_FLAG_MARK_PRIVATE;
43

44
static int key_url(const char *token_uri, const char *key_id, char **url_ptr)
45
46
47
{
	assert(token_uri);
	assert(key_id);
48
	assert(url_ptr);
49
50

	if (!dnssec_keyid_is_valid(key_id)) {
51
		return DNSSEC_INVALID_KEY_ID;
52
53
54
55
56
57
58
59
60
61
	}

	size_t token_len = strlen(token_uri);
	size_t id_len = strlen(key_id);

	// url: <token-url>;id=%aa%bb%cc..

	size_t len = token_len + 4 + (id_len / 2 * 3);
	char *url = malloc(len + 1);
	if (!url) {
62
		return DNSSEC_ENOMEM;
63
64
65
66
67
	}

	size_t prefix = snprintf(url, len, "%s;id=", token_uri);
	if (prefix != token_len + 4) {
		free(url);
68
		return DNSSEC_ENOMEM;
69
70
71
72
73
74
75
76
77
78
79
80
	}

	assert(id_len % 2 == 0);
	char *pos = url + prefix;
	for (int i = 0; i < id_len; i += 2, pos += 3) {
		pos[0] = '%';
		pos[1] = key_id[i];
		pos[2] = key_id[i+1];
	}
	assert(url + len == pos);
	url[len] = '\0';

81
82
	*url_ptr = url;
	return DNSSEC_EOK;
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
 * Parse configuration string. Accepted format: "<pkcs11-url> <module-path>"
 */
static int parse_config(const char *config, char **uri_ptr, char **module_ptr)
{
	const char *space = strchr(config, ' ');
	if (!space) {
		return DNSSEC_KEYSTORE_INVALID_CONFIG;
	}

	char *url = strndup(config, space - config);
	char *module = strdup(space + 1);

	if (!url || !module) {
		free(url);
		free(module);
		return DNSSEC_ENOMEM;
	}

	*uri_ptr = url;
	*module_ptr = module;

	return DNSSEC_EOK;
}

110
111
112
113
114
115
116
117
118
119
120
121
122
/*!
 * Load PKCS #11 module and check if the token is available.
 */
static int safe_open(const char *config, char **url_ptr)
{
	char *url = NULL;
	char *module = NULL;

	int r = parse_config(config, &url, &module);
	if (r != DNSSEC_EOK) {
		return r;
	}

123
	r = p11_load_module(module);
124
125
126
	free(module);
	if (r != GNUTLS_E_SUCCESS) {
		free(url);
127
		return DNSSEC_P11_FAILED_TO_LOAD_MODULE;
128
129
130
131
132
133
	}

	unsigned int flags = 0;
	r = gnutls_pkcs11_token_get_flags(url, &flags);
	if (r != GNUTLS_E_SUCCESS) {
		free(url);
134
		return DNSSEC_P11_TOKEN_NOT_AVAILABLE;
135
136
137
138
139
140
	}

	*url_ptr = url;

	return DNSSEC_EOK;
}
141

142
143
/* -- internal API --------------------------------------------------------- */

144
145
146
147
148
149
static void disable_pkcs11_callbacks(void)
{
	gnutls_pkcs11_set_pin_function(NULL, NULL);
	gnutls_pkcs11_set_token_function(NULL, NULL);
}

150
static int pkcs11_ctx_new(void **ctx_ptr)
151
{
152
153
154
155
156
157
158
159
160
161
162
	static pthread_once_t once = PTHREAD_ONCE_INIT;
	pthread_once(&once, disable_pkcs11_callbacks);

	pkcs11_ctx_t *ctx = calloc(1, sizeof(*ctx));
	if (!ctx) {
		return DNSSEC_ENOMEM;
	}

	*ctx_ptr = ctx;

	return DNSSEC_EOK;
163
164
}

165
static void pkcs11_ctx_free(void *ctx)
Jan Včelák's avatar
Jan Včelák committed
166
{
167
	free(ctx);
Jan Včelák's avatar
Jan Včelák committed
168
169
170
171
}

static int pkcs11_init(void *ctx, const char *config)
{
172
173
174
175
176
177
178
179
180
	/*
	 * Current keystore initialization is idempotent. We don't really
	 * initialize the token because don't want to wipe the data. We just
	 * check that the token is available the same way pkcs11_open() does.
	 */

	_cleanup_free_ char *url = NULL;

	return safe_open(config, &url);
Jan Včelák's avatar
Jan Včelák committed
181
182
183
184
}

static int pkcs11_open(void *_ctx, const char *config)
{
185
186
	pkcs11_ctx_t *ctx = _ctx;

187
	return safe_open(config, &ctx->url);
Jan Včelák's avatar
Jan Včelák committed
188
189
}

190
static int pkcs11_close(void *_ctx)
Jan Včelák's avatar
Jan Včelák committed
191
{
192
193
194
195
196
	pkcs11_ctx_t *ctx = _ctx;

	free(ctx->url);
	clear_struct(ctx);

197
	return DNSSEC_EOK;
Jan Včelák's avatar
Jan Včelák committed
198
199
}

200
static int pkcs11_generate_key(void *_ctx, gnutls_pk_algorithm_t algorithm,
201
			       unsigned bits, const char *label, char **id_ptr)
Jan Včelák's avatar
Jan Včelák committed
202
{
203
204
	pkcs11_ctx_t *ctx = _ctx;

205
206
207
	uint8_t buf[20] = { 0 };
	gnutls_rnd(GNUTLS_RND_RANDOM, buf, sizeof(buf));
	dnssec_binary_t cka_id = { .data = buf, .size = sizeof(buf) };
208

209
	int flags = TOKEN_ADD_FLAGS | GNUTLS_PKCS11_OBJ_FLAG_LOGIN;
210
	gnutls_datum_t gt_cka_id = binary_to_datum(&cka_id);
211
212
	int r = gnutls_pkcs11_privkey_generate3(ctx->url, algorithm, bits, label,
	                                        &gt_cka_id, 0, NULL, 0, flags);
213
214
215
216
	if (r != GNUTLS_E_SUCCESS) {
		return DNSSEC_KEY_GENERATE_ERROR;
	}

Jan Hák's avatar
Jan Hák committed
217
	char *id = bin_to_hex(cka_id.data, cka_id.size, false);
218
	if (id == NULL) {
219
220
221
222
		return DNSSEC_ENOMEM;
	}

	*id_ptr = id;
223

224
	return DNSSEC_EOK;
Jan Včelák's avatar
Jan Včelák committed
225
226
}

227
228
229
static int import_pem(const dnssec_binary_t *pem,
		      gnutls_x509_privkey_t *key_ptr,
		      gnutls_pubkey_t *pubkey_ptr)
Jan Včelák's avatar
Jan Včelák committed
230
{
231
232
233
234
	gnutls_x509_privkey_t x509_key = NULL;
	gnutls_privkey_t key = NULL;
	gnutls_pubkey_t pubkey = NULL;

Daniel Salzman's avatar
Daniel Salzman committed
235
	int r = dnssec_pem_to_x509(pem, &x509_key);
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
	if (r != DNSSEC_EOK) {
		goto fail;
	}

	if (gnutls_privkey_init(&key) != GNUTLS_E_SUCCESS ||
	    gnutls_pubkey_init(&pubkey) != GNUTLS_E_SUCCESS
	) {
		r = DNSSEC_ENOMEM;
		goto fail;
	}

	if (gnutls_privkey_import_x509(key, x509_key, 0) != GNUTLS_E_SUCCESS ||
	    gnutls_pubkey_import_privkey(pubkey, key, 0, 0) != GNUTLS_E_SUCCESS
	) {
		r = DNSSEC_KEY_IMPORT_ERROR;
		goto fail;
	}

fail:
	gnutls_privkey_deinit(key);

	if (r == DNSSEC_EOK) {
		*key_ptr = x509_key;
		*pubkey_ptr = pubkey;
	} else {
		gnutls_x509_privkey_deinit(x509_key);
		gnutls_pubkey_deinit(pubkey);
	}

	return r;
}

static int pkcs11_import_key(void *_ctx, const dnssec_binary_t *pem, char **id_ptr)
{
	pkcs11_ctx_t *ctx = _ctx;

	_cleanup_x509_privkey_ gnutls_x509_privkey_t key = NULL;
	_cleanup_pubkey_ gnutls_pubkey_t pubkey = NULL;

	int r = import_pem(pem, &key, &pubkey);
	if (r != DNSSEC_EOK) {
		return r;
	}

	_cleanup_binary_ dnssec_binary_t id = { 0 };
	r = keyid_x509(key, &id);
	if (r != DNSSEC_EOK) {
		return r;
	}

	int flags = TOKEN_ADD_FLAGS | GNUTLS_PKCS11_OBJ_FLAG_LOGIN;
	gnutls_datum_t gid = binary_to_datum(&id);

	r = gnutls_pkcs11_copy_x509_privkey2(ctx->url, key, NULL, &gid, 0, flags);
	if (r != GNUTLS_E_SUCCESS) {
		return DNSSEC_KEY_IMPORT_ERROR;
	}

	r = gnutls_pkcs11_copy_pubkey(ctx->url, pubkey, NULL, &gid, 0, flags);
	if (r != GNUTLS_E_SUCCESS) {
		// note, we result with dangling private key in the token
		return DNSSEC_KEY_IMPORT_ERROR;
	}

Jan Hák's avatar
Jan Hák committed
300
	*id_ptr = bin_to_hex(id.data, id.size, false);
301
	if (*id_ptr == NULL) {
302
303
304
305
		return DNSSEC_ENOMEM;
	}

	return DNSSEC_EOK;
Jan Včelák's avatar
Jan Včelák committed
306
307
}

308
static int pkcs11_remove_key(void *_ctx, const char *id)
Jan Včelák's avatar
Jan Včelák committed
309
{
310
311
	pkcs11_ctx_t *ctx = _ctx;

312
313
314
315
	_cleanup_free_ char *url = NULL;
	int r = key_url(ctx->url, id, &url);
	if (r != DNSSEC_EOK) {
		return r;
316
317
	}

318
	r = gnutls_pkcs11_delete_url(url, GNUTLS_PKCS11_OBJ_FLAG_LOGIN);
319
320
321
322
	if (r < 0) {
		return DNSSEC_ERROR;
	} else if (r == 0) {
		return DNSSEC_ENOENT;
323
324
325
	}

	return DNSSEC_EOK;
Jan Včelák's avatar
Jan Včelák committed
326
327
}

328
static int pkcs11_get_private(void *_ctx, const char *id, gnutls_privkey_t *key_ptr)
Jan Včelák's avatar
Jan Včelák committed
329
{
330
	pkcs11_ctx_t *ctx = _ctx;
331
332
333
334
335

	_cleanup_free_ char *url = NULL;
	int r = key_url(ctx->url, id, &url);
	if (r != DNSSEC_EOK) {
		return r;
336
337
338
	}

	gnutls_privkey_t key = NULL;
339
	r = gnutls_privkey_init(&key);
340
341
342
343
344
345
346
	if (r != GNUTLS_E_SUCCESS) {
		return DNSSEC_ENOMEM;
	}

	r = gnutls_privkey_import_pkcs11_url(key, url);
	if (r != GNUTLS_E_SUCCESS) {
		gnutls_privkey_deinit(key);
347
		return DNSSEC_NOT_FOUND;
348
349
350
351
352
	}

	*key_ptr = key;

	return DNSSEC_EOK;
Jan Včelák's avatar
Jan Včelák committed
353
}
354

Libor Peltan's avatar
Libor Peltan committed
355
356
357
358
359
360
361
362
363
364
365
366
367
static int pkcs11_set_private(void *ctx, gnutls_privkey_t key)
{
	_cleanup_binary_ dnssec_binary_t pem = { 0 };
	int r = dnssec_pem_from_privkey(key, &pem);
	if (r != DNSSEC_EOK) {
		return r;
	}

	_cleanup_free_ char *keyid = NULL;

	return pkcs11_import_key(ctx, &pem, &keyid);
}

368
369
/* -- public API ----------------------------------------------------------- */

370
_public_
Jan Včelák's avatar
Jan Včelák committed
371
int dnssec_keystore_init_pkcs11(dnssec_keystore_t **store_ptr)
372
{
Jan Včelák's avatar
Jan Včelák committed
373
374
375
376
377
378
379
380
381
382
	static const keystore_functions_t IMPLEMENTATION = {
		.ctx_new      = pkcs11_ctx_new,
		.ctx_free     = pkcs11_ctx_free,
		.init         = pkcs11_init,
		.open         = pkcs11_open,
		.close        = pkcs11_close,
		.generate_key = pkcs11_generate_key,
		.import_key   = pkcs11_import_key,
		.remove_key   = pkcs11_remove_key,
		.get_private  = pkcs11_get_private,
Libor Peltan's avatar
Libor Peltan committed
383
		.set_private  = pkcs11_set_private,
Jan Včelák's avatar
Jan Včelák committed
384
385
	};

386
	return keystore_create(store_ptr, &IMPLEMENTATION);
387
}
388

389
#else // !ENABLE_PKCS11
390
391
392
393
394
395
396
397

_public_
int dnssec_keystore_init_pkcs11(dnssec_keystore_t **store_ptr)
{
	return DNSSEC_NOT_IMPLEMENTED_ERROR;
}

#endif