summaryrefslogtreecommitdiff
path: root/lib/cert-cred-rawpk.c
blob: cfa65eb3188047717074393322f0fd952a2fb1c5 (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
 * Copyright (C) 2017 - 2018 ARPA2 project
 *
 * Author: Tom Vrancken (dev@tomvrancken.nl)
 *
 * This file is part of GnuTLS.
 *
 * The 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/gnutls.h>
#include "datum.h"
#include "auth/cert.h"
#include "x509.h"
#include "cert-cred.h"
#include "read-file.h"
#include <stdint.h>


/**
 * gnutls_certificate_set_rawpk_key_mem:
 * @cred: is a #gnutls_certificate_credentials_t type.
 * @spki: contains a raw public key in
 *   PKIX.SubjectPublicKeyInfo format.
 * @pkey: contains a raw private key.
 * @format: encoding of the keys. DER or PEM.
 * @pass: an optional password to unlock the private key pkey.
 * @key_usage: An ORed sequence of %GNUTLS_KEY_* flags.
 * @names: is an array of DNS names belonging to the public-key (NULL if none).
 * @names_length: holds the length of the names list.
 * @flags: an ORed sequence of #gnutls_pkcs_encrypt_flags_t.
 *   These apply to the private key pkey.
 *
 * This function sets a public/private keypair in the
 * #gnutls_certificate_credentials_t type to be used for authentication
 * and/or encryption. @spki and @privkey should match otherwise set
 * signatures cannot be validated. In case of no match this function
 * returns %GNUTLS_E_CERTIFICATE_KEY_MISMATCH. This function should
 * be called once for the client because there is currently no mechanism
 * to determine which raw public-key to select for the peer when there
 * are multiple present. Multiple raw public keys for the server can be
 * distinghuished by setting the @names.
 *
 * Note here that @spki is a raw public-key as defined
 * in RFC7250. It means that there is no surrounding certificate that
 * holds the public key and that there is therefore no direct mechanism
 * to prove the authenticity of this key. The keypair can be used during
 * a TLS handshake but its authenticity should be established via a
 * different mechanism (e.g. TOFU or known fingerprint).
 *
 * The supported formats are basic unencrypted key, PKCS8, PKCS12,
 * and the openssl format and will be autodetected.
 *
 * If the raw public-key and the private key are given in PEM encoding
 * then the strings that hold their values must be null terminated.
 *
 * Key usage (as defined by X.509 extension (2.5.29.15)) can be explicitly
 * set because there is no certificate structure around the key to define
 * this value. See for more info gnutls_x509_crt_get_key_usage().
 *
 * Note that, this function by default returns zero on success and a
 * negative value on error. Since 3.5.6, when the flag %GNUTLS_CERTIFICATE_API_V2
 * is set using gnutls_certificate_set_flags() it returns an index
 * (greater or equal to zero). That index can be used in other functions
 * to refer to the added key-pair.
 *
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, in case the
 *   key pair does not match %GNUTLS_E_CERTIFICATE_KEY_MISMATCH is returned,
 *   in other erroneous cases a different negative error code is returned.
 *
 * Since: 3.6.6
 **/
int gnutls_certificate_set_rawpk_key_mem(gnutls_certificate_credentials_t cred,
				    const gnutls_datum_t* spki,
				    const gnutls_datum_t* pkey,
				    gnutls_x509_crt_fmt_t format,
				    const char* pass,
				    unsigned int key_usage,
				    const char **names,
				    unsigned int names_length,
				    unsigned int flags)
{
	int ret;
	gnutls_privkey_t privkey;
	gnutls_pcert_st* pcert;
	gnutls_str_array_t str_names;
	unsigned int i;

	if (pkey == NULL || spki == NULL) {
		return gnutls_assert_val(GNUTLS_E_INSUFFICIENT_CREDENTIALS);
	}

	/* Import our private key. This function does all the necessary
	 * inits, checks and imports. */
	ret = _gnutls_read_key_mem(cred, pkey->data, pkey->size,
				format, pass, flags, &privkey);
	if (ret < 0) {
		return gnutls_assert_val(ret);
	}

	/* We now convert our raw public key to a parsed certificate (pcert) structure */
	pcert = gnutls_calloc(1, sizeof(*pcert));
	if (pcert == NULL) {
		gnutls_privkey_deinit(privkey);

		return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
	}
	// Import our raw public key to the pcert structure
	ret = gnutls_pcert_import_rawpk_raw(pcert, spki,
					format, key_usage, 0);
	if (ret < 0) {
		gnutls_privkey_deinit(privkey);

		return gnutls_assert_val(ret);
	}

	/* Process the names, if any */
	_gnutls_str_array_init(&str_names);

	if (names != NULL && names_length > 0) {
		for (i = 0; i < names_length; i++) {
			ret =
			    _gnutls_str_array_append_idna(&str_names, names[i],
						     strlen(names[i]));
			if (ret < 0) {
				gnutls_privkey_deinit(privkey);
				_gnutls_str_array_clear(&str_names);

				return gnutls_assert_val(ret);
			}
		}
	}

	/* Now that we have converted the key material to our internal structures
	 * we can now add them to the credentials structure */
	ret = _gnutls_certificate_credential_append_keypair(cred, privkey, str_names, pcert, 1);
	// Check for errors
	if (ret < 0) {
		gnutls_privkey_deinit(privkey);
		gnutls_pcert_deinit(pcert);
		gnutls_free(pcert);

		return gnutls_assert_val(ret);
	}
	// Successfully added a certificate
	cred->ncerts++;

	/* Check whether the key pair matches.
	 * After this point we do not deinitialize anything on failure to avoid
	 * double freeing. We intentionally keep everything as the credentials state
	 * is documented to be in undefined state. */
	if ((ret = _gnutls_check_key_cert_match(cred)) < 0) {
		return gnutls_assert_val(ret);
	}

	CRED_RET_SUCCESS(cred);
}


/**
 * gnutls_certificate_set_rawpk_key_file:
 * @cred: is a #gnutls_certificate_credentials_t type.
 * @rawpkfile: contains a raw public key in
 *   PKIX.SubjectPublicKeyInfo format.
 * @privkeyfile: contains a file path to a private key.
 * @format: encoding of the keys. DER or PEM.
 * @pass: an optional password to unlock the private key privkeyfile.
 * @key_usage: an ORed sequence of %GNUTLS_KEY_* flags.
 * @names: is an array of DNS names belonging to the public-key (NULL if none).
 * @names_length: holds the length of the names list.
 * @privkey_flags: an ORed sequence of #gnutls_pkcs_encrypt_flags_t.
 *   These apply to the private key pkey.
 * @pkcs11_flags: one of gnutls_pkcs11_obj_flags. These apply to URLs.
 *
 * This function sets a public/private keypair read from file in the
 * #gnutls_certificate_credentials_t type to be used for authentication
 * and/or encryption. @spki and @privkey should match otherwise set
 * signatures cannot be validated. In case of no match this function
 * returns %GNUTLS_E_CERTIFICATE_KEY_MISMATCH. This function should
 * be called once for the client because there is currently no mechanism
 * to determine which raw public-key to select for the peer when there
 * are multiple present. Multiple raw public keys for the server can be
 * distinghuished by setting the @names.
 *
 * Note here that @spki is a raw public-key as defined
 * in RFC7250. It means that there is no surrounding certificate that
 * holds the public key and that there is therefore no direct mechanism
 * to prove the authenticity of this key. The keypair can be used during
 * a TLS handshake but its authenticity should be established via a
 * different mechanism (e.g. TOFU or known fingerprint).
 *
 * The supported formats are basic unencrypted key, PKCS8, PKCS12,
 * and the openssl format and will be autodetected.
 *
 * If the raw public-key and the private key are given in PEM encoding
 * then the strings that hold their values must be null terminated.
 *
 * Key usage (as defined by X.509 extension (2.5.29.15)) can be explicitly
 * set because there is no certificate structure around the key to define
 * this value. See for more info gnutls_x509_crt_get_key_usage().
 *
 * Note that, this function by default returns zero on success and a
 * negative value on error. Since 3.5.6, when the flag %GNUTLS_CERTIFICATE_API_V2
 * is set using gnutls_certificate_set_flags() it returns an index
 * (greater or equal to zero). That index can be used in other functions
 * to refer to the added key-pair.
 *
 * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, in case the
 *   key pair does not match %GNUTLS_E_CERTIFICATE_KEY_MISMATCH is returned,
 *   in other erroneous cases a different negative error code is returned.
 *
 * Since: 3.6.6
 */
int gnutls_certificate_set_rawpk_key_file(gnutls_certificate_credentials_t cred,
				      const char* rawpkfile,
				      const char* privkeyfile,
				      gnutls_x509_crt_fmt_t format,
				      const char *pass,
				      unsigned int key_usage,
				      const char **names,
				      unsigned int names_length,
				      unsigned int privkey_flags,
				      unsigned int pkcs11_flags)
{
	int ret;
	gnutls_privkey_t privkey;
	gnutls_pubkey_t pubkey;
	gnutls_pcert_st* pcert;
	gnutls_datum_t rawpubkey = { NULL, 0 }; // to hold rawpk data from file
	size_t key_size;
	gnutls_str_array_t str_names;
	unsigned int i;

	if (rawpkfile == NULL || privkeyfile == NULL) {
		return gnutls_assert_val(GNUTLS_E_INSUFFICIENT_CREDENTIALS);
	}

	/* Import our private key. This function does all the necessary
	 * inits, checks and imports. */
	ret = _gnutls_read_key_file(cred, privkeyfile, format, pass, privkey_flags, &privkey);
	if (ret < 0) {
		return gnutls_assert_val(ret);
	}

	pcert = gnutls_calloc(1, sizeof(*pcert));
	if (pcert == NULL) {
		gnutls_privkey_deinit(privkey);

		return gnutls_assert_val(GNUTLS_E_MEMORY_ERROR);
	}

	/* Check whether we are importing our raw public-key from a URL
	 * or from a regular file.
	 */
	if (gnutls_url_is_supported(rawpkfile)) {

		ret = gnutls_pubkey_init(&pubkey);
		if (ret < 0) {
			gnutls_privkey_deinit(privkey);

			return gnutls_assert_val(ret);
		}

		ret = gnutls_pubkey_import_url(pubkey, rawpkfile, pkcs11_flags);
		if (ret < 0) {
			gnutls_privkey_deinit(privkey);
			gnutls_pubkey_deinit(pubkey);

			return gnutls_assert_val(ret);
		}

		ret = gnutls_pcert_import_rawpk(pcert, pubkey, 0);
		if (ret < 0) {
			gnutls_privkey_deinit(privkey);
			gnutls_pubkey_deinit(pubkey);

			return gnutls_assert_val(ret);
		}

	} else {
		/* Read our raw public-key into memory from file */
		rawpubkey.data = (void*) read_binary_file(rawpkfile, &key_size);
		if (rawpubkey.data == NULL) {
			gnutls_privkey_deinit(privkey);

			return gnutls_assert_val(GNUTLS_E_FILE_ERROR);
		}
		rawpubkey.size = key_size; // Implicit type casting

		/* We now convert our raw public key that we've loaded into memory to
		 * a parsed certificate (pcert) structure. Note that rawpubkey will
		 * be copied into pcert. Therefore we can directly cleanup rawpubkey.
		 */
		ret = gnutls_pcert_import_rawpk_raw(pcert, &rawpubkey,
						format, key_usage, 0);

		_gnutls_free_datum(&rawpubkey);

		if (ret < 0) {
			gnutls_privkey_deinit(privkey);

			return gnutls_assert_val(ret);
		}

	}

	/* Process the names, if any */
	_gnutls_str_array_init(&str_names);

	if (names != NULL && names_length > 0) {
		for (i = 0; i < names_length; i++) {
			ret =
			    _gnutls_str_array_append_idna(&str_names, names[i],
						     strlen(names[i]));
			if (ret < 0) {
				gnutls_privkey_deinit(privkey);
				_gnutls_str_array_clear(&str_names);

				return gnutls_assert_val(ret);
			}
		}
	}

	/* Now that we have converted the key material to our internal structures
	 * we can now add them to the credentials structure */
	ret = _gnutls_certificate_credential_append_keypair(cred, privkey, str_names, pcert, 1);
	if (ret < 0) {
		gnutls_privkey_deinit(privkey);
		gnutls_pcert_deinit(pcert);
		gnutls_free(pcert);

		return gnutls_assert_val(ret);
	}
	// Successfully added a certificate
	cred->ncerts++;

	/* Check whether the key pair matches.
	 * After this point we do not deinitialize anything on failure to avoid
	 * double freeing. We intentionally keep everything as the credentials state
	 * is documented to be in undefined state. */
	if ((ret = _gnutls_check_key_cert_match(cred)) < 0) {
		return gnutls_assert_val(ret);
	}

	CRED_RET_SUCCESS(cred);
}