summaryrefslogtreecommitdiff
path: root/lib/pkcs11x.c
blob: dfaee58a398743431c7fdd0731884d48ec88a7e8 (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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
 * GnuTLS PKCS#11 support
 * Copyright (C) 2010-2016 Free Software Foundation, Inc.
 * Copyright (C) 2016 Red Hat
 * 
 * Authors: Nikos Mavrogiannopoulos
 *
 * GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>
 */

#include "gnutls_int.h"
#include <gnutls/pkcs11.h>
#include <global.h>
#include "errors.h"
#include "x509/common.h"

#include <pkcs11_int.h>
#include <p11-kit/p11-kit.h>
#include "pkcs11x.h"
#include "intprops.h"

struct find_ext_data_st {
	/* in */
	gnutls_pkcs11_obj_t obj;
	gnutls_datum_t spki;

	/* out */
	gnutls_x509_ext_st *exts;
	unsigned int exts_size;
};

static int override_ext(gnutls_x509_crt_t crt, gnutls_datum_t *ext)
{
	gnutls_x509_ext_st parsed;
	int ret;

	ret = _gnutls_x509_decode_ext(ext, &parsed);
	if (ret < 0)
		return gnutls_assert_val(ret);

	/* set the new extension */
	ret = _gnutls_x509_crt_set_extension(crt, parsed.oid, &parsed.data, parsed.critical);
	if (ret < 0) {
		gnutls_assert();
		goto cleanup;
	}
	
	ret = 0;
 cleanup:
	gnutls_x509_ext_deinit(&parsed);
	return ret;
}

/* This function re-encodes a certificate to contain its stapled extensions.
 * That assumes that the certificate is not in the distrusted list.
 */
int pkcs11_override_cert_exts(struct pkcs11_session_info *sinfo, gnutls_datum_t *spki, gnutls_datum_t *der)
{
	int ret;
	gnutls_datum_t new_der = {NULL, 0};
	struct ck_attribute a[2];
	struct ck_attribute b[1];
	unsigned long count;
	unsigned ext_data_size = der->size;
	uint8_t *ext_data = NULL;
	ck_object_class_t class = -1;
	gnutls_x509_crt_t crt = NULL;
	unsigned finalize = 0;
	ck_rv_t rv;
	ck_object_handle_t obj;

	if (sinfo->trusted == 0) {
		_gnutls_debug_log("p11: cannot override extensions on a non-p11-kit trust module\n");
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);
	}

	/* retrieve the extensions */
	class = CKO_X_CERTIFICATE_EXTENSION;
	a[0].type = CKA_CLASS;
	a[0].value = &class;
	a[0].value_len = sizeof class;

	a[1].type = CKA_PUBLIC_KEY_INFO;
	a[1].value = spki->data;
	a[1].value_len = spki->size;

	rv = pkcs11_find_objects_init(sinfo->module, sinfo->pks, a, 2);
	if (rv != CKR_OK) {
		gnutls_assert();
		_gnutls_debug_log
		    ("p11: FindObjectsInit failed for cert extensions.\n");
		ret = pkcs11_rv_to_err(rv);
		goto cleanup;
	}
	finalize = 1;

	rv = pkcs11_find_objects(sinfo->module, sinfo->pks, &obj, 1, &count);
	if (rv == CKR_OK && count == 1) {
		ext_data = gnutls_malloc(ext_data_size);
		if (ext_data == NULL) {
			gnutls_assert();
			ret = GNUTLS_E_MEMORY_ERROR;
			goto cleanup;
		}

		ret = gnutls_x509_crt_init(&crt);
		if (ret < 0) {
			gnutls_assert();
			goto cleanup;
		}

		ret = gnutls_x509_crt_import(crt, der, GNUTLS_X509_FMT_DER);
		if (ret < 0) {
			gnutls_assert();
			goto cleanup;
		}

		do {

			b[0].type = CKA_VALUE;
			b[0].value = ext_data;
			b[0].value_len = ext_data_size;

			if (pkcs11_get_attribute_value
			    (sinfo->module, sinfo->pks, obj, b, 1) == CKR_OK) {
				gnutls_datum_t data = { b[0].value, b[0].value_len };

				ret = override_ext(crt, &data);
				if (ret < 0) {
					gnutls_assert();
					goto cleanup;
				}
			}
		} while (pkcs11_find_objects(sinfo->module, sinfo->pks, &obj, 1, &count) == CKR_OK && count == 1);

		/* overwrite the old certificate with the new */
		ret = gnutls_x509_crt_export2(crt, GNUTLS_X509_FMT_DER, &new_der);
		if (ret < 0) {
			gnutls_assert();
			goto cleanup;
		}

		gnutls_free(der->data);
		der->data = new_der.data;
		der->size = new_der.size;
	}

	ret = 0;
 cleanup:
	if (crt != NULL)
		gnutls_x509_crt_deinit(crt);
	if (finalize != 0)
		pkcs11_find_objects_final(sinfo);
	gnutls_free(ext_data);
	return ret;

}

static int
find_ext_cb(struct ck_function_list *module, struct pkcs11_session_info *sinfo,
	    struct ck_token_info *tinfo, struct ck_info *lib_info,
	    void *input)
{
	struct find_ext_data_st *find_data = input;
	struct ck_attribute a[4];
	ck_object_class_t class = -1;
	unsigned long count;
	ck_rv_t rv;
	ck_object_handle_t obj;
	int ret;
	gnutls_datum_t ext;

	if (tinfo == NULL) {	/* we don't support multiple calls */
		gnutls_assert();
		return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
	}

	/* do not bother reading the token if basic fields do not match
	 */
	if (!p11_kit_uri_match_token_info
	    (find_data->obj->info, tinfo)
	    || !p11_kit_uri_match_module_info(find_data->obj->info,
					      lib_info)) {
		gnutls_assert();
		return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;
	}

	/* retrieve the extensions */
	class = CKO_X_CERTIFICATE_EXTENSION;
	a[0].type = CKA_CLASS;
	a[0].value = &class;
	a[0].value_len = sizeof class;

	a[1].type = CKA_PUBLIC_KEY_INFO;
	a[1].value = find_data->spki.data;
	a[1].value_len = find_data->spki.size;

	rv = pkcs11_find_objects_init(sinfo->module, sinfo->pks, a, 2);
	if (rv != CKR_OK) {
		gnutls_assert();
		_gnutls_debug_log
		    ("p11: FindObjectsInit failed for cert extensions.\n");
		return pkcs11_rv_to_err(rv);
	}

	while(pkcs11_find_objects(sinfo->module, sinfo->pks, &obj, 1, &count) == CKR_OK && count == 1) {
		rv = pkcs11_get_attribute_avalue(sinfo->module, sinfo->pks, obj, CKA_VALUE, &ext);
		if (rv == CKR_OK) {

			if (unlikely(INT_ADD_OVERFLOW(find_data->exts_size, 1))) {
				ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
				goto cleanup;
			}

			find_data->exts =
				_gnutls_reallocarray_fast(find_data->exts,
							  find_data->exts_size + 1,
							  sizeof(find_data->exts[0]));
			if (find_data->exts == NULL) {
				ret = gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
				goto cleanup;
			}

			if (_gnutls_x509_decode_ext(&ext, &find_data->exts[find_data->exts_size]) == 0) {
				find_data->exts_size++;
			}
			gnutls_free(ext.data);
		}
	}

	ret = 0;
 cleanup:
	pkcs11_find_objects_final(sinfo);
	return ret;
}

/**
 * gnutls_pkcs11_obj_get_exts:
 * @obj: should contain a #gnutls_pkcs11_obj_t type
 * @exts: a pointer to a %gnutls_x509_ext_st pointer
 * @exts_size: will be updated with the number of @exts
 * @flags: Or sequence of %GNUTLS_PKCS11_OBJ_* flags 
 *
 * This function will return information about attached extensions
 * that associate to the provided object (which should be a certificate).
 * The extensions are the attached p11-kit trust module extensions.
 *
 * Each element of @exts must be deinitialized using gnutls_x509_ext_deinit()
 * while @exts should be deallocated using gnutls_free().
 *
 * Returns: %GNUTLS_E_SUCCESS (0) on success or a negative error code on error.
 *
 * Since: 3.3.8
 **/
int
gnutls_pkcs11_obj_get_exts(gnutls_pkcs11_obj_t obj,
			   gnutls_x509_ext_st **exts, unsigned int *exts_size,
			   unsigned int flags)
{
	int ret;
	gnutls_datum_t spki = {NULL, 0};
	struct find_ext_data_st find_data;
	unsigned deinit_spki = 0;

	PKCS11_CHECK_INIT;
	memset(&find_data, 0, sizeof(find_data));

	*exts_size = 0;

	if (obj->type != GNUTLS_PKCS11_OBJ_X509_CRT && obj->type != GNUTLS_PKCS11_OBJ_PUBKEY)
		return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);

	if (obj->type == GNUTLS_PKCS11_OBJ_PUBKEY) {
		spki.data = obj->raw.data;
		spki.size = obj->raw.size;
	} else {
		ret = _gnutls_x509_raw_crt_to_raw_pubkey(&obj->raw, &spki);
		if (ret < 0)
			return gnutls_assert_val(ret);
		deinit_spki = 1;
	}

	find_data.spki.data = spki.data;
	find_data.spki.size = spki.size;
	find_data.obj = obj;
	ret =
	    _pkcs11_traverse_tokens(find_ext_cb, &find_data, obj->info,
				    &obj->pin,
				    pkcs11_obj_flags_to_int(flags));
	if (ret < 0) {
		gnutls_assert();
		goto cleanup;
	}

	*exts = find_data.exts;
	*exts_size = find_data.exts_size;

	ret = 0;
 cleanup:
	if (deinit_spki)
		gnutls_free(spki.data);
	return ret;
}