/src/gnutls/lib/privkey.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * GnuTLS PKCS#11 support |
3 | | * Copyright (C) 2010-2014 Free Software Foundation, Inc. |
4 | | * Copyright (C) 2012-2015 Nikos Mavrogiannopoulos |
5 | | * Copyright (C) 2016-2017 Red Hat, Inc. |
6 | | * |
7 | | * Author: Nikos Mavrogiannopoulos |
8 | | * |
9 | | * The GnuTLS is free software; you can redistribute it and/or |
10 | | * modify it under the terms of the GNU Lesser General Public License |
11 | | * as published by the Free Software Foundation; either version 2.1 of |
12 | | * the License, or (at your option) any later version. |
13 | | * |
14 | | * This library is distributed in the hope that it will be useful, but |
15 | | * WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
17 | | * Lesser General Public License for more details. |
18 | | * |
19 | | * You should have received a copy of the GNU Lesser General Public License |
20 | | * along with this program. If not, see <https://www.gnu.org/licenses/> |
21 | | */ |
22 | | |
23 | | #include "gnutls_int.h" |
24 | | #include <gnutls/pkcs11.h> |
25 | | #include <stdio.h> |
26 | | #include <string.h> |
27 | | #include "errors.h" |
28 | | #include "datum.h" |
29 | | #include "pkcs11_int.h" |
30 | | #include <gnutls/abstract.h> |
31 | | #include "pk.h" |
32 | | #include "x509_int.h" |
33 | | #include "tls-sig.h" |
34 | | #include "algorithms.h" |
35 | | #include "fips.h" |
36 | | #include "system-keys.h" |
37 | | #include "urls.h" |
38 | | #include "tpm2.h" |
39 | | #include "pkcs11_int.h" |
40 | | #include "abstract_int.h" |
41 | | |
42 | | static int privkey_sign_prehashed(gnutls_privkey_t signer, |
43 | | const gnutls_sign_entry_st *se, |
44 | | const gnutls_datum_t *hash_data, |
45 | | gnutls_datum_t *signature, |
46 | | gnutls_x509_spki_st *params); |
47 | | |
48 | | /** |
49 | | * gnutls_privkey_get_type: |
50 | | * @key: should contain a #gnutls_privkey_t type |
51 | | * |
52 | | * This function will return the type of the private key. This is |
53 | | * actually the type of the subsystem used to set this private key. |
54 | | * |
55 | | * Returns: a member of the #gnutls_privkey_type_t enumeration on |
56 | | * success, or a negative error code on error. |
57 | | * |
58 | | * Since: 2.12.0 |
59 | | **/ |
60 | | gnutls_privkey_type_t gnutls_privkey_get_type(gnutls_privkey_t key) |
61 | 0 | { |
62 | 0 | return key->type; |
63 | 0 | } |
64 | | |
65 | | /** |
66 | | * gnutls_privkey_get_seed: |
67 | | * @key: should contain a #gnutls_privkey_t type |
68 | | * @digest: if non-NULL it will contain the digest algorithm used for key generation (if applicable) |
69 | | * @seed: where seed will be copied to |
70 | | * @seed_size: originally holds the size of @seed, will be updated with actual size |
71 | | * |
72 | | * This function will return the seed that was used to generate the |
73 | | * given private key. That function will succeed only if the key was generated |
74 | | * as a provable key. |
75 | | * |
76 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
77 | | * negative error value. |
78 | | * |
79 | | * Since: 3.5.0 |
80 | | **/ |
81 | | int gnutls_privkey_get_seed(gnutls_privkey_t key, |
82 | | gnutls_digest_algorithm_t *digest, void *seed, |
83 | | size_t *seed_size) |
84 | 0 | { |
85 | 0 | if (key->type != GNUTLS_PRIVKEY_X509) |
86 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
87 | 0 | return gnutls_x509_privkey_get_seed(key->key.x509, digest, seed, |
88 | 0 | seed_size); |
89 | 0 | } |
90 | | |
91 | | /** |
92 | | * gnutls_privkey_verify_seed: |
93 | | * @key: should contain a #gnutls_privkey_t type |
94 | | * @digest: it contains the digest algorithm used for key generation (if applicable) |
95 | | * @seed: the seed of the key to be checked with |
96 | | * @seed_size: holds the size of @seed |
97 | | * |
98 | | * This function will verify that the given private key was generated from |
99 | | * the provided seed. |
100 | | * |
101 | | * Returns: In case of a verification failure %GNUTLS_E_PRIVKEY_VERIFICATION_ERROR |
102 | | * is returned, and zero or positive code on success. |
103 | | * |
104 | | * Since: 3.5.0 |
105 | | **/ |
106 | | int gnutls_privkey_verify_seed(gnutls_privkey_t key, |
107 | | gnutls_digest_algorithm_t digest, |
108 | | const void *seed, size_t seed_size) |
109 | 0 | { |
110 | 0 | if (key->type != GNUTLS_PRIVKEY_X509) |
111 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
112 | 0 | return gnutls_x509_privkey_verify_seed(key->key.x509, digest, seed, |
113 | 0 | seed_size); |
114 | 0 | } |
115 | | |
116 | | /** |
117 | | * gnutls_privkey_get_pk_algorithm: |
118 | | * @key: should contain a #gnutls_privkey_t type |
119 | | * @bits: If set will return the number of bits of the parameters (may be NULL) |
120 | | * |
121 | | * This function will return the public key algorithm of a private |
122 | | * key and if possible will return a number of bits that indicates |
123 | | * the security parameter of the key. |
124 | | * |
125 | | * Returns: a member of the #gnutls_pk_algorithm_t enumeration on |
126 | | * success, or a negative error code on error. |
127 | | * |
128 | | * Since: 2.12.0 |
129 | | **/ |
130 | | int gnutls_privkey_get_pk_algorithm(gnutls_privkey_t key, unsigned int *bits) |
131 | 0 | { |
132 | 0 | switch (key->type) { |
133 | | #ifdef ENABLE_PKCS11 |
134 | | case GNUTLS_PRIVKEY_PKCS11: |
135 | | return gnutls_pkcs11_privkey_get_pk_algorithm(key->key.pkcs11, |
136 | | bits); |
137 | | #endif |
138 | 0 | case GNUTLS_PRIVKEY_X509: |
139 | 0 | if (bits) { |
140 | 0 | *bits = pubkey_to_bits(&key->key.x509->params); |
141 | 0 | } |
142 | |
|
143 | 0 | return gnutls_x509_privkey_get_pk_algorithm(key->key.x509); |
144 | 0 | case GNUTLS_PRIVKEY_EXT: |
145 | 0 | if (bits) |
146 | 0 | *bits = key->key.ext.bits; |
147 | |
|
148 | 0 | return key->pk_algorithm; |
149 | 0 | default: |
150 | 0 | gnutls_assert(); |
151 | 0 | return GNUTLS_E_INVALID_REQUEST; |
152 | 0 | } |
153 | 0 | } |
154 | | |
155 | | static int privkey_to_pubkey(gnutls_pk_algorithm_t pk, |
156 | | const gnutls_pk_params_st *priv, |
157 | | gnutls_pk_params_st *pub) |
158 | 0 | { |
159 | 0 | int ret; |
160 | |
|
161 | 0 | pub->algo = priv->algo; |
162 | 0 | pub->pkflags = priv->pkflags; |
163 | 0 | pub->curve = priv->curve; |
164 | 0 | pub->gost_params = priv->gost_params; |
165 | 0 | pub->qbits = priv->qbits; |
166 | 0 | memcpy(&pub->spki, &priv->spki, sizeof(gnutls_x509_spki_st)); |
167 | |
|
168 | 0 | switch (pk) { |
169 | 0 | case GNUTLS_PK_RSA_PSS: |
170 | 0 | case GNUTLS_PK_RSA: |
171 | 0 | pub->params[0] = _gnutls_mpi_copy(priv->params[0]); |
172 | 0 | pub->params[1] = _gnutls_mpi_copy(priv->params[1]); |
173 | |
|
174 | 0 | pub->params_nr = RSA_PUBLIC_PARAMS; |
175 | |
|
176 | 0 | if (pub->params[0] == NULL || pub->params[1] == NULL) { |
177 | 0 | gnutls_assert(); |
178 | 0 | ret = GNUTLS_E_MEMORY_ERROR; |
179 | 0 | goto cleanup; |
180 | 0 | } |
181 | | |
182 | 0 | break; |
183 | 0 | case GNUTLS_PK_DSA: |
184 | 0 | pub->params[DSA_P] = _gnutls_mpi_copy(priv->params[DSA_P]); |
185 | 0 | pub->params[DSA_Q] = _gnutls_mpi_copy(priv->params[DSA_Q]); |
186 | 0 | pub->params[DSA_G] = _gnutls_mpi_copy(priv->params[DSA_G]); |
187 | 0 | pub->params[DSA_Y] = _gnutls_mpi_copy(priv->params[DSA_Y]); |
188 | |
|
189 | 0 | pub->params_nr = DSA_PUBLIC_PARAMS; |
190 | |
|
191 | 0 | if (pub->params[DSA_P] == NULL || pub->params[DSA_Q] == NULL || |
192 | 0 | pub->params[DSA_G] == NULL || pub->params[DSA_Y] == NULL) { |
193 | 0 | gnutls_assert(); |
194 | 0 | ret = GNUTLS_E_MEMORY_ERROR; |
195 | 0 | goto cleanup; |
196 | 0 | } |
197 | | |
198 | 0 | break; |
199 | 0 | case GNUTLS_PK_DH: |
200 | 0 | pub->params[DH_P] = _gnutls_mpi_copy(priv->params[DH_P]); |
201 | 0 | pub->params[DH_G] = _gnutls_mpi_copy(priv->params[DH_G]); |
202 | 0 | pub->params[DH_Y] = _gnutls_mpi_copy(priv->params[DH_Y]); |
203 | |
|
204 | 0 | if (pub->params[DH_P] == NULL || pub->params[DH_G] == NULL || |
205 | 0 | pub->params[DH_Y] == NULL) { |
206 | 0 | gnutls_assert(); |
207 | 0 | ret = GNUTLS_E_MEMORY_ERROR; |
208 | 0 | goto cleanup; |
209 | 0 | } |
210 | | |
211 | 0 | if (priv->params[DH_Q]) { |
212 | 0 | pub->params[DH_Q] = |
213 | 0 | _gnutls_mpi_copy(priv->params[DH_Q]); |
214 | 0 | if (pub->params[DH_Q] == NULL) { |
215 | 0 | gnutls_assert(); |
216 | 0 | ret = GNUTLS_E_MEMORY_ERROR; |
217 | 0 | goto cleanup; |
218 | 0 | } |
219 | 0 | } |
220 | | |
221 | 0 | pub->params_nr = DH_PUBLIC_PARAMS; |
222 | |
|
223 | 0 | break; |
224 | 0 | case GNUTLS_PK_ECDSA: |
225 | 0 | pub->params[ECC_X] = _gnutls_mpi_copy(priv->params[ECC_X]); |
226 | 0 | pub->params[ECC_Y] = _gnutls_mpi_copy(priv->params[ECC_Y]); |
227 | |
|
228 | 0 | pub->params_nr = ECC_PUBLIC_PARAMS; |
229 | |
|
230 | 0 | if (pub->params[ECC_X] == NULL || pub->params[ECC_Y] == NULL) { |
231 | 0 | gnutls_assert(); |
232 | 0 | ret = GNUTLS_E_MEMORY_ERROR; |
233 | 0 | goto cleanup; |
234 | 0 | } |
235 | | |
236 | 0 | break; |
237 | 0 | case GNUTLS_PK_EDDSA_ED25519: |
238 | 0 | case GNUTLS_PK_EDDSA_ED448: |
239 | 0 | case GNUTLS_PK_ECDH_X25519: |
240 | 0 | case GNUTLS_PK_ECDH_X448: |
241 | 0 | ret = _gnutls_set_datum(&pub->raw_pub, priv->raw_pub.data, |
242 | 0 | priv->raw_pub.size); |
243 | 0 | if (ret < 0) |
244 | 0 | return gnutls_assert_val(ret); |
245 | | |
246 | 0 | break; |
247 | 0 | case GNUTLS_PK_GOST_01: |
248 | 0 | case GNUTLS_PK_GOST_12_256: |
249 | 0 | case GNUTLS_PK_GOST_12_512: |
250 | 0 | pub->params[GOST_X] = _gnutls_mpi_copy(priv->params[GOST_X]); |
251 | 0 | pub->params[GOST_Y] = _gnutls_mpi_copy(priv->params[GOST_Y]); |
252 | |
|
253 | 0 | pub->params_nr = GOST_PUBLIC_PARAMS; |
254 | |
|
255 | 0 | if (pub->params[GOST_X] == NULL || |
256 | 0 | pub->params[GOST_Y] == NULL) { |
257 | 0 | gnutls_assert(); |
258 | 0 | ret = GNUTLS_E_MEMORY_ERROR; |
259 | 0 | goto cleanup; |
260 | 0 | } |
261 | | |
262 | 0 | break; |
263 | 0 | default: |
264 | 0 | gnutls_assert(); |
265 | 0 | return GNUTLS_E_INVALID_REQUEST; |
266 | 0 | } |
267 | | |
268 | 0 | return 0; |
269 | 0 | cleanup: |
270 | 0 | gnutls_pk_params_release(pub); |
271 | 0 | return ret; |
272 | 0 | } |
273 | | |
274 | | /* Returns the public key of the private key (if possible) |
275 | | */ |
276 | | int _gnutls_privkey_get_mpis(gnutls_privkey_t key, gnutls_pk_params_st *params) |
277 | 0 | { |
278 | 0 | int ret; |
279 | |
|
280 | 0 | switch (key->type) { |
281 | 0 | case GNUTLS_PRIVKEY_X509: |
282 | 0 | ret = _gnutls_pk_params_copy(params, &key->key.x509->params); |
283 | 0 | break; |
284 | | #ifdef ENABLE_PKCS11 |
285 | | case GNUTLS_PRIVKEY_PKCS11: { |
286 | | gnutls_pubkey_t pubkey; |
287 | | |
288 | | ret = _pkcs11_privkey_get_pubkey(key->key.pkcs11, &pubkey, 0); |
289 | | if (ret < 0) |
290 | | return gnutls_assert_val(ret); |
291 | | |
292 | | ret = _gnutls_pubkey_get_mpis(pubkey, params); |
293 | | gnutls_pubkey_deinit(pubkey); |
294 | | |
295 | | break; |
296 | | } |
297 | | #endif |
298 | 0 | default: |
299 | 0 | if (key->key.ext.pk_params_func) { |
300 | 0 | ret = key->key.ext.pk_params_func( |
301 | 0 | key, key->key.ext.userdata, params); |
302 | 0 | if (ret < 0) |
303 | 0 | return gnutls_assert_val(ret); |
304 | 0 | return ret; |
305 | 0 | } |
306 | 0 | gnutls_assert(); |
307 | 0 | return GNUTLS_E_INVALID_REQUEST; |
308 | 0 | } |
309 | | |
310 | 0 | return ret; |
311 | 0 | } |
312 | | |
313 | | int _gnutls_privkey_get_public_mpis(gnutls_privkey_t key, |
314 | | gnutls_pk_params_st *params) |
315 | 0 | { |
316 | 0 | int ret; |
317 | 0 | gnutls_pk_params_st tmp1; |
318 | |
|
319 | 0 | gnutls_pk_params_init(&tmp1); |
320 | |
|
321 | 0 | ret = _gnutls_privkey_get_mpis(key, &tmp1); |
322 | 0 | if (ret < 0) |
323 | 0 | return gnutls_assert_val(ret); |
324 | | |
325 | 0 | ret = privkey_to_pubkey(key->pk_algorithm, &tmp1, params); |
326 | |
|
327 | 0 | gnutls_pk_params_release(&tmp1); |
328 | |
|
329 | 0 | if (ret < 0) |
330 | 0 | gnutls_assert(); |
331 | |
|
332 | 0 | return ret; |
333 | 0 | } |
334 | | |
335 | | /* This function retrieves default sign parameters from KEY. */ |
336 | | int _gnutls_privkey_get_spki_params(gnutls_privkey_t key, |
337 | | gnutls_x509_spki_st *params) |
338 | 0 | { |
339 | 0 | switch (key->type) { |
340 | | #ifdef ENABLE_PKCS11 |
341 | | case GNUTLS_PRIVKEY_PKCS11: |
342 | | break; |
343 | | #endif |
344 | 0 | case GNUTLS_PRIVKEY_EXT: |
345 | 0 | break; |
346 | 0 | case GNUTLS_PRIVKEY_X509: |
347 | 0 | _gnutls_x509_privkey_get_spki_params(key->key.x509, params); |
348 | 0 | return 0; |
349 | 0 | default: |
350 | 0 | gnutls_assert(); |
351 | 0 | return GNUTLS_E_INVALID_REQUEST; |
352 | 0 | } |
353 | | |
354 | 0 | memset(params, 0, sizeof(gnutls_x509_spki_st)); |
355 | |
|
356 | 0 | return 0; |
357 | 0 | } |
358 | | |
359 | | /* This function fills in PARAMS with the necessary parameters to sign |
360 | | * with PK and DIG. PARAMS must be initialized with |
361 | | * _gnutls_privkey_get_spki_params in advance. |
362 | | * |
363 | | * After calling this function the params structure will |
364 | | * be initialized even if the original SubjectPublicKeyInfo was empty. |
365 | | */ |
366 | | int _gnutls_privkey_update_spki_params(gnutls_privkey_t key, |
367 | | gnutls_pk_algorithm_t pk, |
368 | | gnutls_digest_algorithm_t dig, |
369 | | unsigned flags, |
370 | | gnutls_x509_spki_st *params) |
371 | 0 | { |
372 | 0 | unsigned salt_size = 0; |
373 | 0 | unsigned bits = 0; |
374 | 0 | gnutls_pk_algorithm_t key_pk; |
375 | |
|
376 | 0 | if (flags & GNUTLS_PRIVKEY_SIGN_FLAG_RSA_PSS) { |
377 | 0 | if (!GNUTLS_PK_IS_RSA(pk)) |
378 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
379 | 0 | pk = GNUTLS_PK_RSA_PSS; |
380 | 0 | } |
381 | | |
382 | 0 | key_pk = gnutls_privkey_get_pk_algorithm(key, &bits); |
383 | 0 | if ((key_pk != pk) && |
384 | 0 | !(key_pk == GNUTLS_PK_RSA && pk == GNUTLS_PK_RSA_PSS)) { |
385 | 0 | gnutls_assert(); |
386 | 0 | return GNUTLS_E_CONSTRAINT_ERROR; |
387 | 0 | } |
388 | | |
389 | 0 | if (pk == GNUTLS_PK_RSA_PSS) { |
390 | 0 | const mac_entry_st *me; |
391 | 0 | int ret; |
392 | |
|
393 | 0 | me = hash_to_entry(dig); |
394 | 0 | if (unlikely(me == NULL)) |
395 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
396 | | |
397 | 0 | if (params->pk == GNUTLS_PK_RSA) |
398 | 0 | salt_size = 0; |
399 | 0 | else if (params->pk == GNUTLS_PK_RSA_PSS) { |
400 | 0 | if (params->rsa_pss_dig != GNUTLS_DIG_UNKNOWN && |
401 | 0 | dig != params->rsa_pss_dig) { |
402 | 0 | return gnutls_assert_val( |
403 | 0 | GNUTLS_E_CONSTRAINT_ERROR); |
404 | 0 | } |
405 | | |
406 | 0 | salt_size = params->salt_size; |
407 | 0 | } |
408 | | |
409 | 0 | if (flags & GNUTLS_PRIVKEY_FLAG_REPRODUCIBLE) |
410 | 0 | params->salt_size = 0; |
411 | 0 | else { |
412 | 0 | ret = _gnutls_find_rsa_pss_salt_size(bits, me, |
413 | 0 | salt_size); |
414 | 0 | if (ret < 0) |
415 | 0 | return gnutls_assert_val(ret); |
416 | 0 | if (flags & GNUTLS_PRIVKEY_FLAG_RSA_PSS_FIXED_SALT_LENGTH && |
417 | 0 | (size_t)ret != _gnutls_hash_get_algo_len(me)) { |
418 | 0 | return gnutls_assert_val( |
419 | 0 | GNUTLS_E_CONSTRAINT_ERROR); |
420 | 0 | } |
421 | 0 | params->salt_size = ret; |
422 | 0 | } |
423 | 0 | params->rsa_pss_dig = dig; |
424 | 0 | } |
425 | | |
426 | 0 | params->pk = pk; |
427 | |
|
428 | 0 | return 0; |
429 | 0 | } |
430 | | |
431 | | /** |
432 | | * gnutls_privkey_init: |
433 | | * @key: A pointer to the type to be initialized |
434 | | * |
435 | | * This function will initialize a private key object. The object can |
436 | | * be used to generate, import, and perform cryptographic operations |
437 | | * on the associated private key. |
438 | | * |
439 | | * Note that when the underlying private key is a PKCS#11 key (i.e., |
440 | | * when imported with a PKCS#11 URI), the limitations of gnutls_pkcs11_privkey_init() |
441 | | * apply to this object as well. In versions of GnuTLS later than 3.5.11 the object |
442 | | * is protected using locks and a single %gnutls_privkey_t can be re-used |
443 | | * by many threads. However, for performance it is recommended to utilize |
444 | | * one object per key per thread. |
445 | | * |
446 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
447 | | * negative error value. |
448 | | * |
449 | | * Since: 2.12.0 |
450 | | **/ |
451 | | int gnutls_privkey_init(gnutls_privkey_t *key) |
452 | 0 | { |
453 | 0 | *key = NULL; |
454 | 0 | FAIL_IF_LIB_ERROR; |
455 | | |
456 | 0 | *key = gnutls_calloc(1, sizeof(struct gnutls_privkey_st)); |
457 | 0 | if (*key == NULL) { |
458 | 0 | gnutls_assert(); |
459 | 0 | return GNUTLS_E_MEMORY_ERROR; |
460 | 0 | } |
461 | | |
462 | 0 | return 0; |
463 | 0 | } |
464 | | |
465 | | /** |
466 | | * gnutls_privkey_deinit: |
467 | | * @key: The key to be deinitialized |
468 | | * |
469 | | * This function will deinitialize a private key structure. |
470 | | * |
471 | | * Since: 2.12.0 |
472 | | **/ |
473 | | void gnutls_privkey_deinit(gnutls_privkey_t key) |
474 | 0 | { |
475 | 0 | if (key == NULL) |
476 | 0 | return; |
477 | | |
478 | 0 | if (key->flags & GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE || |
479 | 0 | key->flags & GNUTLS_PRIVKEY_IMPORT_COPY) |
480 | 0 | switch (key->type) { |
481 | | #ifdef ENABLE_PKCS11 |
482 | | case GNUTLS_PRIVKEY_PKCS11: |
483 | | gnutls_pkcs11_privkey_deinit(key->key.pkcs11); |
484 | | break; |
485 | | #endif |
486 | 0 | case GNUTLS_PRIVKEY_X509: |
487 | 0 | gnutls_x509_privkey_deinit(key->key.x509); |
488 | 0 | break; |
489 | 0 | case GNUTLS_PRIVKEY_EXT: |
490 | 0 | if (key->key.ext.deinit_func != NULL) |
491 | 0 | key->key.ext.deinit_func(key, |
492 | 0 | key->key.ext.userdata); |
493 | 0 | break; |
494 | 0 | default: |
495 | 0 | break; |
496 | 0 | } |
497 | 0 | gnutls_free(key); |
498 | 0 | } |
499 | | |
500 | | /* Will erase all private key information, except PIN */ |
501 | | void _gnutls_privkey_cleanup(gnutls_privkey_t key) |
502 | 0 | { |
503 | 0 | memset(&key->key, 0, sizeof(key->key)); |
504 | 0 | key->type = 0; |
505 | 0 | key->pk_algorithm = 0; |
506 | 0 | key->flags = 0; |
507 | 0 | } |
508 | | |
509 | | /* will fail if the private key contains an actual key. |
510 | | */ |
511 | | static int check_if_clean(gnutls_privkey_t key) |
512 | 0 | { |
513 | 0 | if (key->type != 0) |
514 | 0 | return GNUTLS_E_INVALID_REQUEST; |
515 | | |
516 | 0 | return 0; |
517 | 0 | } |
518 | | |
519 | | #ifdef ENABLE_PKCS11 |
520 | | |
521 | | /** |
522 | | * gnutls_privkey_import_pkcs11: |
523 | | * @pkey: The private key |
524 | | * @key: The private key to be imported |
525 | | * @flags: Flags for the import |
526 | | * |
527 | | * This function will import the given private key to the abstract |
528 | | * #gnutls_privkey_t type. |
529 | | * |
530 | | * The #gnutls_pkcs11_privkey_t object must not be deallocated |
531 | | * during the lifetime of this structure. |
532 | | * |
533 | | * @flags might be zero or one of %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE |
534 | | * and %GNUTLS_PRIVKEY_IMPORT_COPY. |
535 | | * |
536 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
537 | | * negative error value. |
538 | | * |
539 | | * Since: 2.12.0 |
540 | | **/ |
541 | | int gnutls_privkey_import_pkcs11(gnutls_privkey_t pkey, |
542 | | gnutls_pkcs11_privkey_t key, |
543 | | unsigned int flags) |
544 | | { |
545 | | int ret; |
546 | | |
547 | | ret = check_if_clean(pkey); |
548 | | if (ret < 0) { |
549 | | gnutls_assert(); |
550 | | return ret; |
551 | | } |
552 | | |
553 | | if (flags & GNUTLS_PRIVKEY_IMPORT_COPY) |
554 | | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
555 | | |
556 | | pkey->key.pkcs11 = key; |
557 | | pkey->type = GNUTLS_PRIVKEY_PKCS11; |
558 | | pkey->pk_algorithm = gnutls_pkcs11_privkey_get_pk_algorithm(key, NULL); |
559 | | pkey->flags = flags; |
560 | | |
561 | | if (pkey->pin.data) |
562 | | gnutls_pkcs11_privkey_set_pin_function(key, pkey->pin.cb, |
563 | | pkey->pin.data); |
564 | | |
565 | | return 0; |
566 | | } |
567 | | |
568 | | #if 0 |
569 | | /** |
570 | | * gnutls_privkey_import_pkcs11_url: |
571 | | * @key: A key of type #gnutls_pubkey_t |
572 | | * @url: A PKCS 11 url |
573 | | * |
574 | | * This function will import a PKCS 11 private key to a #gnutls_privkey_t |
575 | | * type. |
576 | | * |
577 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
578 | | * negative error value. |
579 | | * |
580 | | * Since: 3.1.0 |
581 | | **/ |
582 | | |
583 | | int gnutls_privkey_import_pkcs11_url(gnutls_privkey_t key, const char *url) |
584 | | { |
585 | | int x; |
586 | | } |
587 | | #endif |
588 | | |
589 | | static int _gnutls_privkey_import_pkcs11_url(gnutls_privkey_t key, |
590 | | const char *url, unsigned flags) |
591 | | { |
592 | | gnutls_pkcs11_privkey_t pkey; |
593 | | int ret; |
594 | | |
595 | | ret = gnutls_pkcs11_privkey_init(&pkey); |
596 | | if (ret < 0) { |
597 | | gnutls_assert(); |
598 | | return ret; |
599 | | } |
600 | | |
601 | | if (key->pin.cb) |
602 | | gnutls_pkcs11_privkey_set_pin_function(pkey, key->pin.cb, |
603 | | key->pin.data); |
604 | | |
605 | | ret = gnutls_pkcs11_privkey_import_url(pkey, url, flags); |
606 | | if (ret < 0) { |
607 | | gnutls_assert(); |
608 | | goto cleanup; |
609 | | } |
610 | | |
611 | | ret = gnutls_privkey_import_pkcs11(key, pkey, |
612 | | GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE); |
613 | | if (ret < 0) { |
614 | | gnutls_assert(); |
615 | | goto cleanup; |
616 | | } |
617 | | |
618 | | return 0; |
619 | | |
620 | | cleanup: |
621 | | gnutls_pkcs11_privkey_deinit(pkey); |
622 | | |
623 | | return ret; |
624 | | } |
625 | | |
626 | | /** |
627 | | * gnutls_privkey_export_pkcs11: |
628 | | * @pkey: The private key |
629 | | * @key: Location for the key to be exported. |
630 | | * |
631 | | * Converts the given abstract private key to a #gnutls_pkcs11_privkey_t |
632 | | * type. The key must be of type %GNUTLS_PRIVKEY_PKCS11. The key |
633 | | * returned in @key must be deinitialized with |
634 | | * gnutls_pkcs11_privkey_deinit(). |
635 | | * |
636 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
637 | | * negative error value. |
638 | | * |
639 | | * Since: 3.4.0 |
640 | | */ |
641 | | int gnutls_privkey_export_pkcs11(gnutls_privkey_t pkey, |
642 | | gnutls_pkcs11_privkey_t *key) |
643 | | { |
644 | | int ret; |
645 | | |
646 | | *key = NULL; |
647 | | if (pkey->type != GNUTLS_PRIVKEY_PKCS11) { |
648 | | gnutls_assert(); |
649 | | return GNUTLS_E_INVALID_REQUEST; |
650 | | } |
651 | | |
652 | | ret = gnutls_pkcs11_privkey_init(key); |
653 | | if (ret < 0) |
654 | | return gnutls_assert_val(ret); |
655 | | |
656 | | ret = gnutls_pkcs11_privkey_cpy(*key, pkey->key.pkcs11); |
657 | | if (ret < 0) { |
658 | | gnutls_pkcs11_privkey_deinit(*key); |
659 | | *key = NULL; |
660 | | |
661 | | return gnutls_assert_val(ret); |
662 | | } |
663 | | |
664 | | return 0; |
665 | | } |
666 | | #endif /* ENABLE_PKCS11 */ |
667 | | |
668 | | /** |
669 | | * gnutls_privkey_import_ext: |
670 | | * @pkey: The private key |
671 | | * @pk: The public key algorithm |
672 | | * @userdata: private data to be provided to the callbacks |
673 | | * @sign_func: callback for signature operations |
674 | | * @decrypt_func: callback for decryption operations |
675 | | * @flags: Flags for the import |
676 | | * |
677 | | * This function will associate the given callbacks with the |
678 | | * #gnutls_privkey_t type. At least one of the two callbacks |
679 | | * must be non-null. |
680 | | * |
681 | | * Note that the signing function is supposed to "raw" sign data, i.e., |
682 | | * without any hashing or preprocessing. In case of RSA the DigestInfo |
683 | | * will be provided, and the signing function is expected to do the PKCS #1 |
684 | | * 1.5 padding and the exponentiation. |
685 | | * |
686 | | * See also gnutls_privkey_import_ext3(). |
687 | | * |
688 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
689 | | * negative error value. |
690 | | * |
691 | | * Since: 3.0 |
692 | | **/ |
693 | | int gnutls_privkey_import_ext(gnutls_privkey_t pkey, gnutls_pk_algorithm_t pk, |
694 | | void *userdata, |
695 | | gnutls_privkey_sign_func sign_func, |
696 | | gnutls_privkey_decrypt_func decrypt_func, |
697 | | unsigned int flags) |
698 | 0 | { |
699 | 0 | return gnutls_privkey_import_ext2(pkey, pk, userdata, sign_func, |
700 | 0 | decrypt_func, NULL, flags); |
701 | 0 | } |
702 | | |
703 | | #define PK_IS_OK_FOR_EXT2(pk) \ |
704 | 0 | ((pk == GNUTLS_PK_RSA) || (pk == GNUTLS_PK_ECDSA) || \ |
705 | 0 | (pk == GNUTLS_PK_DSA)) |
706 | | |
707 | | /** |
708 | | * gnutls_privkey_import_ext2: |
709 | | * @pkey: The private key |
710 | | * @pk: The public key algorithm |
711 | | * @userdata: private data to be provided to the callbacks |
712 | | * @sign_fn: callback for signature operations |
713 | | * @decrypt_fn: callback for decryption operations |
714 | | * @deinit_fn: a deinitialization function |
715 | | * @flags: Flags for the import |
716 | | * |
717 | | * This function will associate the given callbacks with the |
718 | | * #gnutls_privkey_t type. At least one of the two callbacks |
719 | | * must be non-null. If a deinitialization function is provided |
720 | | * then flags is assumed to contain %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE. |
721 | | * |
722 | | * Note that the signing function is supposed to "raw" sign data, i.e., |
723 | | * without any hashing or preprocessing. In case of RSA the DigestInfo |
724 | | * will be provided, and the signing function is expected to do the PKCS #1 |
725 | | * 1.5 padding and the exponentiation. |
726 | | * |
727 | | * See also gnutls_privkey_import_ext3(). |
728 | | * |
729 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
730 | | * negative error value. |
731 | | * |
732 | | * Since: 3.1 |
733 | | **/ |
734 | | int gnutls_privkey_import_ext2(gnutls_privkey_t pkey, gnutls_pk_algorithm_t pk, |
735 | | void *userdata, gnutls_privkey_sign_func sign_fn, |
736 | | gnutls_privkey_decrypt_func decrypt_fn, |
737 | | gnutls_privkey_deinit_func deinit_fn, |
738 | | unsigned int flags) |
739 | 0 | { |
740 | 0 | int ret; |
741 | |
|
742 | 0 | ret = check_if_clean(pkey); |
743 | 0 | if (ret < 0) { |
744 | 0 | gnutls_assert(); |
745 | 0 | return ret; |
746 | 0 | } |
747 | | |
748 | 0 | if (!PK_IS_OK_FOR_EXT2(pk)) |
749 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
750 | | |
751 | 0 | if (sign_fn == NULL && decrypt_fn == NULL) |
752 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
753 | | |
754 | 0 | pkey->key.ext.sign_func = sign_fn; |
755 | 0 | pkey->key.ext.decrypt_func = decrypt_fn; |
756 | 0 | pkey->key.ext.deinit_func = deinit_fn; |
757 | 0 | pkey->key.ext.userdata = userdata; |
758 | 0 | pkey->type = GNUTLS_PRIVKEY_EXT; |
759 | 0 | pkey->pk_algorithm = pk; |
760 | 0 | pkey->flags = flags; |
761 | | |
762 | | /* Ensure gnutls_privkey_deinit() calls the deinit_func */ |
763 | 0 | if (deinit_fn) |
764 | 0 | pkey->flags |= GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE; |
765 | |
|
766 | 0 | return 0; |
767 | 0 | } |
768 | | |
769 | | /** |
770 | | * gnutls_privkey_import_ext3: |
771 | | * @pkey: The private key |
772 | | * @userdata: private data to be provided to the callbacks |
773 | | * @sign_fn: callback for signature operations |
774 | | * @decrypt_fn: callback for decryption operations |
775 | | * @deinit_fn: a deinitialization function |
776 | | * @info_fn: returns info about the public key algorithm (should not be %NULL) |
777 | | * @flags: Flags for the import |
778 | | * |
779 | | * This function will associate the given callbacks with the |
780 | | * #gnutls_privkey_t type. At least one of the two callbacks |
781 | | * must be non-null. If a deinitialization function is provided |
782 | | * then flags is assumed to contain %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE. |
783 | | * |
784 | | * Note that the signing function is supposed to "raw" sign data, i.e., |
785 | | * without any hashing or preprocessing. In case of RSA the DigestInfo |
786 | | * will be provided, and the signing function is expected to do the PKCS #1 |
787 | | * 1.5 padding and the exponentiation. |
788 | | * |
789 | | * The @info_fn must provide information on the algorithms supported by |
790 | | * this private key, and should support the flags %GNUTLS_PRIVKEY_INFO_PK_ALGO and |
791 | | * %GNUTLS_PRIVKEY_INFO_SIGN_ALGO. It must return -1 on unknown flags. |
792 | | * |
793 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
794 | | * negative error value. |
795 | | * |
796 | | * Since: 3.4.0 |
797 | | **/ |
798 | | int gnutls_privkey_import_ext3(gnutls_privkey_t pkey, void *userdata, |
799 | | gnutls_privkey_sign_func sign_fn, |
800 | | gnutls_privkey_decrypt_func decrypt_fn, |
801 | | gnutls_privkey_deinit_func deinit_fn, |
802 | | gnutls_privkey_info_func info_fn, |
803 | | unsigned int flags) |
804 | 0 | { |
805 | 0 | int ret; |
806 | |
|
807 | 0 | ret = check_if_clean(pkey); |
808 | 0 | if (ret < 0) { |
809 | 0 | gnutls_assert(); |
810 | 0 | return ret; |
811 | 0 | } |
812 | | |
813 | 0 | if (sign_fn == NULL && decrypt_fn == NULL) |
814 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
815 | | |
816 | 0 | if (info_fn == NULL) |
817 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
818 | | |
819 | 0 | pkey->key.ext.sign_func = sign_fn; |
820 | 0 | pkey->key.ext.decrypt_func = decrypt_fn; |
821 | 0 | pkey->key.ext.deinit_func = deinit_fn; |
822 | 0 | pkey->key.ext.info_func = info_fn; |
823 | 0 | pkey->key.ext.userdata = userdata; |
824 | 0 | pkey->type = GNUTLS_PRIVKEY_EXT; |
825 | 0 | pkey->flags = flags; |
826 | |
|
827 | 0 | pkey->pk_algorithm = pkey->key.ext.info_func( |
828 | 0 | pkey, GNUTLS_PRIVKEY_INFO_PK_ALGO, pkey->key.ext.userdata); |
829 | |
|
830 | 0 | if (!PK_IS_OK_FOR_EXT2(pkey->pk_algorithm)) |
831 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
832 | | |
833 | | /* Ensure gnutls_privkey_deinit() calls the deinit_func */ |
834 | 0 | if (deinit_fn) |
835 | 0 | pkey->flags |= GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE; |
836 | |
|
837 | 0 | return 0; |
838 | 0 | } |
839 | | |
840 | | /** |
841 | | * gnutls_privkey_import_ext4: |
842 | | * @pkey: The private key |
843 | | * @userdata: private data to be provided to the callbacks |
844 | | * @sign_data_fn: callback for signature operations (may be %NULL) |
845 | | * @sign_hash_fn: callback for signature operations (may be %NULL) |
846 | | * @decrypt_fn: callback for decryption operations (may be %NULL) |
847 | | * @deinit_fn: a deinitialization function |
848 | | * @info_fn: returns info about the public key algorithm (should not be %NULL) |
849 | | * @flags: Flags for the import |
850 | | * |
851 | | * This function will associate the given callbacks with the |
852 | | * #gnutls_privkey_t type. At least one of the callbacks |
853 | | * must be non-null. If a deinitialization function is provided |
854 | | * then flags is assumed to contain %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE. |
855 | | * |
856 | | * Note that in contrast with the signing function of |
857 | | * gnutls_privkey_import_ext3(), the signing functions provided to this |
858 | | * function take explicitly the signature algorithm as parameter and |
859 | | * different functions are provided to sign the data and hashes. |
860 | | * |
861 | | * The @sign_hash_fn is to be called to sign pre-hashed data. The input |
862 | | * to the callback is the output of the hash (such as SHA256) corresponding |
863 | | * to the signature algorithm. For RSA PKCS#1 signatures, the signature |
864 | | * algorithm can be set to %GNUTLS_SIGN_RSA_RAW, and in that case the data |
865 | | * should be handled as if they were an RSA PKCS#1 DigestInfo structure. |
866 | | * |
867 | | * The @sign_data_fn is to be called to sign data. The input data will be |
868 | | * he data to be signed (and hashed), with the provided signature |
869 | | * algorithm. This function is to be used for signature algorithms like |
870 | | * Ed25519 which cannot take pre-hashed data as input. |
871 | | * |
872 | | * When both @sign_data_fn and @sign_hash_fn functions are provided they |
873 | | * must be able to operate on all the supported signature algorithms, |
874 | | * unless prohibited by the type of the algorithm (e.g., as with Ed25519). |
875 | | * |
876 | | * The @info_fn must provide information on the signature algorithms supported by |
877 | | * this private key, and should support the flags %GNUTLS_PRIVKEY_INFO_PK_ALGO, |
878 | | * %GNUTLS_PRIVKEY_INFO_HAVE_SIGN_ALGO and %GNUTLS_PRIVKEY_INFO_PK_ALGO_BITS. |
879 | | * It must return -1 on unknown flags. |
880 | | * |
881 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
882 | | * negative error value. |
883 | | * |
884 | | * Since: 3.6.0 |
885 | | **/ |
886 | | int gnutls_privkey_import_ext4(gnutls_privkey_t pkey, void *userdata, |
887 | | gnutls_privkey_sign_data_func sign_data_fn, |
888 | | gnutls_privkey_sign_hash_func sign_hash_fn, |
889 | | gnutls_privkey_decrypt_func decrypt_fn, |
890 | | gnutls_privkey_deinit_func deinit_fn, |
891 | | gnutls_privkey_info_func info_fn, |
892 | | unsigned int flags) |
893 | 0 | { |
894 | 0 | int ret; |
895 | |
|
896 | 0 | ret = check_if_clean(pkey); |
897 | 0 | if (ret < 0) { |
898 | 0 | gnutls_assert(); |
899 | 0 | return ret; |
900 | 0 | } |
901 | | |
902 | 0 | if (sign_data_fn == NULL && sign_hash_fn == NULL && decrypt_fn == NULL) |
903 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
904 | | |
905 | 0 | if (info_fn == NULL) |
906 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
907 | | |
908 | 0 | pkey->key.ext.sign_data_func = sign_data_fn; |
909 | 0 | pkey->key.ext.sign_hash_func = sign_hash_fn; |
910 | 0 | pkey->key.ext.decrypt_func = decrypt_fn; |
911 | 0 | pkey->key.ext.deinit_func = deinit_fn; |
912 | 0 | pkey->key.ext.info_func = info_fn; |
913 | 0 | pkey->key.ext.userdata = userdata; |
914 | 0 | pkey->type = GNUTLS_PRIVKEY_EXT; |
915 | 0 | pkey->flags = flags; |
916 | |
|
917 | 0 | pkey->pk_algorithm = pkey->key.ext.info_func( |
918 | 0 | pkey, GNUTLS_PRIVKEY_INFO_PK_ALGO, pkey->key.ext.userdata); |
919 | |
|
920 | 0 | ret = pkey->key.ext.info_func(pkey, GNUTLS_PRIVKEY_INFO_PK_ALGO_BITS, |
921 | 0 | pkey->key.ext.userdata); |
922 | 0 | if (ret >= 0) |
923 | 0 | pkey->key.ext.bits = ret; |
924 | | |
925 | | /* Ensure gnutls_privkey_deinit() calls the deinit_func */ |
926 | 0 | if (deinit_fn) |
927 | 0 | pkey->flags |= GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE; |
928 | |
|
929 | 0 | return 0; |
930 | 0 | } |
931 | | |
932 | | /** |
933 | | * gnutls_privkey_import_x509: |
934 | | * @pkey: The private key |
935 | | * @key: The private key to be imported |
936 | | * @flags: Flags for the import |
937 | | * |
938 | | * This function will import the given private key to the abstract |
939 | | * #gnutls_privkey_t type. |
940 | | * |
941 | | * The #gnutls_x509_privkey_t object must not be deallocated |
942 | | * during the lifetime of this structure. |
943 | | * |
944 | | * @flags might be zero or one of %GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE |
945 | | * and %GNUTLS_PRIVKEY_IMPORT_COPY. |
946 | | * |
947 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
948 | | * negative error value. |
949 | | * |
950 | | * Since: 2.12.0 |
951 | | **/ |
952 | | int gnutls_privkey_import_x509(gnutls_privkey_t pkey, gnutls_x509_privkey_t key, |
953 | | unsigned int flags) |
954 | 0 | { |
955 | 0 | int ret; |
956 | |
|
957 | 0 | ret = check_if_clean(pkey); |
958 | 0 | if (ret < 0) { |
959 | 0 | gnutls_assert(); |
960 | 0 | return ret; |
961 | 0 | } |
962 | | |
963 | 0 | if (flags & GNUTLS_PRIVKEY_IMPORT_COPY) { |
964 | 0 | ret = gnutls_x509_privkey_init(&pkey->key.x509); |
965 | 0 | if (ret < 0) |
966 | 0 | return gnutls_assert_val(ret); |
967 | | |
968 | 0 | ret = gnutls_x509_privkey_cpy(pkey->key.x509, key); |
969 | 0 | if (ret < 0) { |
970 | 0 | gnutls_x509_privkey_deinit(pkey->key.x509); |
971 | 0 | return gnutls_assert_val(ret); |
972 | 0 | } |
973 | 0 | } else |
974 | 0 | pkey->key.x509 = key; |
975 | | |
976 | 0 | pkey->type = GNUTLS_PRIVKEY_X509; |
977 | 0 | pkey->pk_algorithm = gnutls_x509_privkey_get_pk_algorithm(key); |
978 | 0 | pkey->flags = flags; |
979 | |
|
980 | 0 | return 0; |
981 | 0 | } |
982 | | |
983 | | /** |
984 | | * gnutls_privkey_export_x509: |
985 | | * @pkey: The private key |
986 | | * @key: Location for the key to be exported. |
987 | | * |
988 | | * Converts the given abstract private key to a #gnutls_x509_privkey_t |
989 | | * type. The abstract key must be of type %GNUTLS_PRIVKEY_X509. The input |
990 | | * @key must not be initialized. The key returned in @key should be deinitialized |
991 | | * using gnutls_x509_privkey_deinit(). |
992 | | * |
993 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
994 | | * negative error value. |
995 | | * |
996 | | * Since: 3.4.0 |
997 | | */ |
998 | | int gnutls_privkey_export_x509(gnutls_privkey_t pkey, |
999 | | gnutls_x509_privkey_t *key) |
1000 | 0 | { |
1001 | 0 | int ret; |
1002 | |
|
1003 | 0 | *key = NULL; |
1004 | 0 | if (pkey->type != GNUTLS_PRIVKEY_X509) { |
1005 | 0 | gnutls_assert(); |
1006 | 0 | return GNUTLS_E_INVALID_REQUEST; |
1007 | 0 | } |
1008 | | |
1009 | 0 | ret = gnutls_x509_privkey_init(key); |
1010 | 0 | if (ret < 0) |
1011 | 0 | return gnutls_assert_val(ret); |
1012 | | |
1013 | 0 | ret = gnutls_x509_privkey_cpy(*key, pkey->key.x509); |
1014 | 0 | if (ret < 0) { |
1015 | 0 | gnutls_x509_privkey_deinit(*key); |
1016 | 0 | *key = NULL; |
1017 | |
|
1018 | 0 | return gnutls_assert_val(ret); |
1019 | 0 | } |
1020 | | |
1021 | 0 | return 0; |
1022 | 0 | } |
1023 | | |
1024 | | /** |
1025 | | * gnutls_privkey_generate: |
1026 | | * @pkey: An initialized private key |
1027 | | * @algo: is one of the algorithms in #gnutls_pk_algorithm_t. |
1028 | | * @bits: the size of the parameters to generate |
1029 | | * @flags: Must be zero or flags from #gnutls_privkey_flags_t. |
1030 | | * |
1031 | | * This function will generate a random private key. Note that this |
1032 | | * function must be called on an initialized private key. |
1033 | | * |
1034 | | * The flag %GNUTLS_PRIVKEY_FLAG_PROVABLE |
1035 | | * instructs the key generation process to use algorithms like Shawe-Taylor |
1036 | | * (from FIPS PUB186-4) which generate provable parameters out of a seed |
1037 | | * for RSA and DSA keys. See gnutls_privkey_generate2() for more |
1038 | | * information. |
1039 | | * |
1040 | | * Note that when generating an elliptic curve key, the curve |
1041 | | * can be substituted in the place of the bits parameter using the |
1042 | | * GNUTLS_CURVE_TO_BITS() macro. The input to the macro is any curve from |
1043 | | * %gnutls_ecc_curve_t. |
1044 | | * |
1045 | | * For DSA keys, if the subgroup size needs to be specified check |
1046 | | * the GNUTLS_SUBGROUP_TO_BITS() macro. |
1047 | | * |
1048 | | * It is recommended to do not set the number of @bits directly, use gnutls_sec_param_to_pk_bits() instead . |
1049 | | * |
1050 | | * See also gnutls_privkey_generate2(). |
1051 | | * |
1052 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1053 | | * negative error value. |
1054 | | * |
1055 | | * Since: 3.3.0 |
1056 | | **/ |
1057 | | int gnutls_privkey_generate(gnutls_privkey_t pkey, gnutls_pk_algorithm_t algo, |
1058 | | unsigned int bits, unsigned int flags) |
1059 | 0 | { |
1060 | 0 | return gnutls_privkey_generate2(pkey, algo, bits, flags, NULL, 0); |
1061 | 0 | } |
1062 | | |
1063 | | /** |
1064 | | * gnutls_privkey_generate2: |
1065 | | * @pkey: The private key |
1066 | | * @algo: is one of the algorithms in #gnutls_pk_algorithm_t. |
1067 | | * @bits: the size of the modulus |
1068 | | * @flags: Must be zero or flags from #gnutls_privkey_flags_t. |
1069 | | * @data: Allow specifying %gnutls_keygen_data_st types such as the seed to be used. |
1070 | | * @data_size: The number of @data available. |
1071 | | * |
1072 | | * This function will generate a random private key. Note that this |
1073 | | * function must be called on an initialized private key. |
1074 | | * |
1075 | | * The flag %GNUTLS_PRIVKEY_FLAG_PROVABLE |
1076 | | * instructs the key generation process to use algorithms like Shawe-Taylor |
1077 | | * (from FIPS PUB186-4) which generate provable parameters out of a seed |
1078 | | * for RSA and DSA keys. On DSA keys the PQG parameters are generated using the |
1079 | | * seed, while on RSA the two primes. To specify an explicit seed |
1080 | | * (by default a random seed is used), use the @data with a %GNUTLS_KEYGEN_SEED |
1081 | | * type. |
1082 | | * |
1083 | | * Note that when generating an elliptic curve key, the curve |
1084 | | * can be substituted in the place of the bits parameter using the |
1085 | | * GNUTLS_CURVE_TO_BITS() macro. |
1086 | | * |
1087 | | * To export the generated keys in memory or in files it is recommended to use the |
1088 | | * PKCS#8 form as it can handle all key types, and can store additional parameters |
1089 | | * such as the seed, in case of provable RSA or DSA keys. |
1090 | | * Generated keys can be exported in memory using gnutls_privkey_export_x509(), |
1091 | | * and then with gnutls_x509_privkey_export2_pkcs8(). |
1092 | | * |
1093 | | * If key generation is part of your application, avoid setting the number |
1094 | | * of bits directly, and instead use gnutls_sec_param_to_pk_bits(). |
1095 | | * That way the generated keys will adapt to the security levels |
1096 | | * of the underlying GnuTLS library. |
1097 | | * |
1098 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1099 | | * negative error value. |
1100 | | * |
1101 | | * Since: 3.5.0 |
1102 | | **/ |
1103 | | int gnutls_privkey_generate2(gnutls_privkey_t pkey, gnutls_pk_algorithm_t algo, |
1104 | | unsigned int bits, unsigned int flags, |
1105 | | const gnutls_keygen_data_st *data, |
1106 | | unsigned data_size) |
1107 | 0 | { |
1108 | 0 | int ret; |
1109 | |
|
1110 | 0 | ret = gnutls_x509_privkey_init(&pkey->key.x509); |
1111 | 0 | if (ret < 0) |
1112 | 0 | return gnutls_assert_val(ret); |
1113 | | |
1114 | 0 | ret = gnutls_x509_privkey_generate2(pkey->key.x509, algo, bits, flags, |
1115 | 0 | data, data_size); |
1116 | 0 | if (ret < 0) { |
1117 | 0 | gnutls_x509_privkey_deinit(pkey->key.x509); |
1118 | 0 | pkey->key.x509 = NULL; |
1119 | 0 | return gnutls_assert_val(ret); |
1120 | 0 | } |
1121 | | |
1122 | 0 | pkey->type = GNUTLS_PRIVKEY_X509; |
1123 | 0 | pkey->pk_algorithm = algo; |
1124 | 0 | pkey->flags = flags | GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE; |
1125 | |
|
1126 | 0 | return 0; |
1127 | 0 | } |
1128 | | |
1129 | | /** |
1130 | | * gnutls_privkey_sign_data: |
1131 | | * @signer: Holds the key |
1132 | | * @hash: should be a digest algorithm |
1133 | | * @flags: Zero or one of %gnutls_privkey_flags_t |
1134 | | * @data: holds the data to be signed |
1135 | | * @signature: will contain the signature allocated with gnutls_malloc() |
1136 | | * |
1137 | | * This function will sign the given data using a signature algorithm |
1138 | | * supported by the private key. Signature algorithms are always used |
1139 | | * together with a hash functions. Different hash functions may be |
1140 | | * used for the RSA algorithm, but only the SHA family for the DSA keys. |
1141 | | * |
1142 | | * You may use gnutls_pubkey_get_preferred_hash_algorithm() to determine |
1143 | | * the hash algorithm. |
1144 | | * |
1145 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1146 | | * negative error value. |
1147 | | * |
1148 | | * Since: 2.12.0 |
1149 | | **/ |
1150 | | int gnutls_privkey_sign_data(gnutls_privkey_t signer, |
1151 | | gnutls_digest_algorithm_t hash, unsigned int flags, |
1152 | | const gnutls_datum_t *data, |
1153 | | gnutls_datum_t *signature) |
1154 | 0 | { |
1155 | 0 | int ret; |
1156 | 0 | gnutls_x509_spki_st params; |
1157 | |
|
1158 | 0 | if (flags & GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA) |
1159 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1160 | | |
1161 | 0 | ret = _gnutls_privkey_get_spki_params(signer, ¶ms); |
1162 | 0 | if (ret < 0) { |
1163 | 0 | gnutls_assert(); |
1164 | 0 | return ret; |
1165 | 0 | } |
1166 | | |
1167 | 0 | ret = _gnutls_privkey_update_spki_params(signer, signer->pk_algorithm, |
1168 | 0 | hash, flags, ¶ms); |
1169 | 0 | if (ret < 0) { |
1170 | 0 | gnutls_assert(); |
1171 | 0 | return ret; |
1172 | 0 | } |
1173 | | |
1174 | 0 | FIX_SIGN_PARAMS(params, flags, hash); |
1175 | |
|
1176 | 0 | return privkey_sign_and_hash_data( |
1177 | 0 | signer, _gnutls_pk_to_sign_entry(params.pk, hash), data, |
1178 | 0 | signature, ¶ms); |
1179 | 0 | } |
1180 | | |
1181 | | /** |
1182 | | * gnutls_privkey_sign_data2: |
1183 | | * @signer: Holds the key |
1184 | | * @algo: The signature algorithm used |
1185 | | * @flags: Zero or one of %gnutls_privkey_flags_t |
1186 | | * @data: holds the data to be signed |
1187 | | * @signature: will contain the signature allocated with gnutls_malloc() |
1188 | | * |
1189 | | * This function will sign the given data using the specified signature |
1190 | | * algorithm. This function is an enhancement of gnutls_privkey_sign_data(), |
1191 | | * as it allows utilizing a alternative signature algorithm where possible |
1192 | | * (e.g, use an RSA key with RSA-PSS). |
1193 | | * |
1194 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1195 | | * negative error value. |
1196 | | * |
1197 | | * Since: 3.6.0 |
1198 | | **/ |
1199 | | int gnutls_privkey_sign_data2(gnutls_privkey_t signer, |
1200 | | gnutls_sign_algorithm_t algo, unsigned int flags, |
1201 | | const gnutls_datum_t *data, |
1202 | | gnutls_datum_t *signature) |
1203 | 0 | { |
1204 | 0 | int ret; |
1205 | 0 | gnutls_x509_spki_st params; |
1206 | 0 | const gnutls_sign_entry_st *se; |
1207 | |
|
1208 | 0 | if (flags & GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA) |
1209 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1210 | | |
1211 | 0 | se = _gnutls_sign_to_entry(algo); |
1212 | 0 | if (se == NULL) |
1213 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1214 | | |
1215 | 0 | ret = _gnutls_privkey_get_spki_params(signer, ¶ms); |
1216 | 0 | if (ret < 0) { |
1217 | 0 | gnutls_assert(); |
1218 | 0 | return ret; |
1219 | 0 | } |
1220 | | |
1221 | 0 | ret = _gnutls_privkey_update_spki_params(signer, se->pk, se->hash, |
1222 | 0 | flags, ¶ms); |
1223 | 0 | if (ret < 0) { |
1224 | 0 | gnutls_assert(); |
1225 | 0 | return ret; |
1226 | 0 | } |
1227 | | |
1228 | 0 | FIX_SIGN_PARAMS(params, flags, se->hash); |
1229 | |
|
1230 | 0 | return privkey_sign_and_hash_data(signer, se, data, signature, ¶ms); |
1231 | 0 | } |
1232 | | |
1233 | | /** |
1234 | | * gnutls_privkey_sign_hash2: |
1235 | | * @signer: Holds the signer's key |
1236 | | * @algo: The signature algorithm used |
1237 | | * @flags: Zero or one of %gnutls_privkey_flags_t |
1238 | | * @hash_data: holds the data to be signed |
1239 | | * @signature: will contain newly allocated signature |
1240 | | * |
1241 | | * This function will sign the given hashed data using the specified signature |
1242 | | * algorithm. This function is an enhancement of gnutls_privkey_sign_hash(), |
1243 | | * as it allows utilizing a alternative signature algorithm where possible |
1244 | | * (e.g, use an RSA key with RSA-PSS). |
1245 | | * |
1246 | | * The flags may be %GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA. |
1247 | | * In that case this function will ignore @hash_algo and perform a raw PKCS1 signature. |
1248 | | * Note that this flag is supported since 3.6.9. |
1249 | | * |
1250 | | * Note also that, not all algorithm support signing already hashed data. When |
1251 | | * signing with Ed25519, gnutls_privkey_sign_data2() should be used instead. |
1252 | | * |
1253 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1254 | | * negative error value. |
1255 | | * |
1256 | | * Since: 3.6.0 |
1257 | | **/ |
1258 | | int gnutls_privkey_sign_hash2(gnutls_privkey_t signer, |
1259 | | gnutls_sign_algorithm_t algo, unsigned int flags, |
1260 | | const gnutls_datum_t *hash_data, |
1261 | | gnutls_datum_t *signature) |
1262 | 0 | { |
1263 | 0 | int ret; |
1264 | 0 | gnutls_x509_spki_st params; |
1265 | 0 | const gnutls_sign_entry_st *se; |
1266 | |
|
1267 | 0 | if (flags & GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA) { |
1268 | | /* the corresponding signature algorithm is SIGN_RSA_RAW, |
1269 | | * irrespective of hash algorithm. */ |
1270 | 0 | se = _gnutls_sign_to_entry(GNUTLS_SIGN_RSA_RAW); |
1271 | 0 | } else { |
1272 | 0 | se = _gnutls_sign_to_entry(algo); |
1273 | 0 | if (unlikely(se == NULL)) { |
1274 | 0 | ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1275 | 0 | goto cleanup; |
1276 | 0 | } |
1277 | 0 | } |
1278 | | |
1279 | 0 | ret = _gnutls_privkey_get_spki_params(signer, ¶ms); |
1280 | 0 | if (ret < 0) { |
1281 | 0 | gnutls_assert(); |
1282 | 0 | goto cleanup; |
1283 | 0 | } |
1284 | | |
1285 | 0 | ret = _gnutls_privkey_update_spki_params(signer, se->pk, se->hash, |
1286 | 0 | flags, ¶ms); |
1287 | 0 | if (ret < 0) { |
1288 | 0 | gnutls_assert(); |
1289 | 0 | goto cleanup; |
1290 | 0 | } |
1291 | | |
1292 | 0 | FIX_SIGN_PARAMS(params, flags, se->hash); |
1293 | |
|
1294 | 0 | ret = privkey_sign_prehashed(signer, se, hash_data, signature, ¶ms); |
1295 | |
|
1296 | 0 | cleanup: |
1297 | 0 | if (ret < 0) { |
1298 | 0 | _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR); |
1299 | 0 | } else { |
1300 | 0 | _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED); |
1301 | 0 | } |
1302 | 0 | return ret; |
1303 | 0 | } |
1304 | | |
1305 | | int privkey_sign_and_hash_data(gnutls_privkey_t signer, |
1306 | | const gnutls_sign_entry_st *se, |
1307 | | const gnutls_datum_t *data, |
1308 | | gnutls_datum_t *signature, |
1309 | | gnutls_x509_spki_st *params) |
1310 | 0 | { |
1311 | 0 | int ret; |
1312 | 0 | gnutls_datum_t digest; |
1313 | 0 | const mac_entry_st *me; |
1314 | |
|
1315 | 0 | if (unlikely(se == NULL)) |
1316 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1317 | | |
1318 | 0 | if (_gnutls_pk_is_not_prehashed(se->pk)) { |
1319 | 0 | return privkey_sign_raw_data(signer, se, data, signature, |
1320 | 0 | params); |
1321 | 0 | } |
1322 | | |
1323 | 0 | me = hash_to_entry(se->hash); |
1324 | 0 | if (me == NULL) |
1325 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1326 | | |
1327 | 0 | ret = pk_hash_data(se->pk, me, NULL, data, &digest); |
1328 | 0 | if (ret < 0) { |
1329 | 0 | gnutls_assert(); |
1330 | 0 | return ret; |
1331 | 0 | } |
1332 | | |
1333 | 0 | ret = pk_prepare_hash(se->pk, me, &digest); |
1334 | 0 | if (ret < 0) { |
1335 | 0 | gnutls_assert(); |
1336 | 0 | goto cleanup; |
1337 | 0 | } |
1338 | | |
1339 | 0 | ret = privkey_sign_raw_data(signer, se, &digest, signature, params); |
1340 | 0 | _gnutls_free_datum(&digest); |
1341 | |
|
1342 | 0 | if (ret < 0) { |
1343 | 0 | gnutls_assert(); |
1344 | 0 | return ret; |
1345 | 0 | } |
1346 | | |
1347 | 0 | return 0; |
1348 | | |
1349 | 0 | cleanup: |
1350 | 0 | _gnutls_free_datum(&digest); |
1351 | 0 | return ret; |
1352 | 0 | } |
1353 | | |
1354 | | /** |
1355 | | * gnutls_privkey_sign_hash: |
1356 | | * @signer: Holds the signer's key |
1357 | | * @hash_algo: The hash algorithm used |
1358 | | * @flags: Zero or one of %gnutls_privkey_flags_t |
1359 | | * @hash_data: holds the data to be signed |
1360 | | * @signature: will contain newly allocated signature |
1361 | | * |
1362 | | * This function will sign the given hashed data using a signature algorithm |
1363 | | * supported by the private key. Signature algorithms are always used |
1364 | | * together with a hash functions. Different hash functions may be |
1365 | | * used for the RSA algorithm, but only SHA-XXX for the DSA keys. |
1366 | | * |
1367 | | * You may use gnutls_pubkey_get_preferred_hash_algorithm() to determine |
1368 | | * the hash algorithm. |
1369 | | * |
1370 | | * The flags may be %GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA or %GNUTLS_PRIVKEY_SIGN_FLAG_RSA_PSS. |
1371 | | * In the former case this function will ignore @hash_algo and perform a raw PKCS1 signature, |
1372 | | * and in the latter an RSA-PSS signature will be generated. |
1373 | | * |
1374 | | * Note that, not all algorithm support signing already hashed data. When |
1375 | | * signing with Ed25519, gnutls_privkey_sign_data() should be used. |
1376 | | * |
1377 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1378 | | * negative error value. |
1379 | | * |
1380 | | * Since: 2.12.0 |
1381 | | **/ |
1382 | | int gnutls_privkey_sign_hash(gnutls_privkey_t signer, |
1383 | | gnutls_digest_algorithm_t hash_algo, |
1384 | | unsigned int flags, |
1385 | | const gnutls_datum_t *hash_data, |
1386 | | gnutls_datum_t *signature) |
1387 | 0 | { |
1388 | 0 | int ret; |
1389 | 0 | gnutls_x509_spki_st params; |
1390 | 0 | const gnutls_sign_entry_st *se; |
1391 | |
|
1392 | 0 | ret = _gnutls_privkey_get_spki_params(signer, ¶ms); |
1393 | 0 | if (ret < 0) { |
1394 | 0 | gnutls_assert(); |
1395 | 0 | goto cleanup; |
1396 | 0 | } |
1397 | | |
1398 | 0 | ret = _gnutls_privkey_update_spki_params(signer, signer->pk_algorithm, |
1399 | 0 | hash_algo, flags, ¶ms); |
1400 | 0 | if (ret < 0) { |
1401 | 0 | gnutls_assert(); |
1402 | 0 | goto cleanup; |
1403 | 0 | } |
1404 | | |
1405 | | /* legacy callers of this API could use a hash algorithm of 0 (unknown) |
1406 | | * to indicate raw hashing. As we now always want to know the signing |
1407 | | * algorithm involved, we try discovering the hash algorithm. */ |
1408 | 0 | if (hash_algo == 0 && |
1409 | 0 | (params.pk == GNUTLS_PK_DSA || params.pk == GNUTLS_PK_ECDSA)) { |
1410 | 0 | hash_algo = _gnutls_hash_size_to_sha_hash(hash_data->size); |
1411 | 0 | } |
1412 | |
|
1413 | 0 | if (params.pk == GNUTLS_PK_RSA && |
1414 | 0 | (flags & GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA)) { |
1415 | | /* the corresponding signature algorithm is SIGN_RSA_RAW, |
1416 | | * irrespective of hash algorithm. */ |
1417 | 0 | se = _gnutls_sign_to_entry(GNUTLS_SIGN_RSA_RAW); |
1418 | 0 | } else { |
1419 | 0 | se = _gnutls_pk_to_sign_entry(params.pk, hash_algo); |
1420 | 0 | } |
1421 | |
|
1422 | 0 | if (unlikely(se == NULL)) { |
1423 | 0 | ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1424 | 0 | goto cleanup; |
1425 | 0 | } |
1426 | | |
1427 | 0 | FIX_SIGN_PARAMS(params, flags, hash_algo); |
1428 | |
|
1429 | 0 | ret = privkey_sign_prehashed(signer, se, hash_data, signature, ¶ms); |
1430 | 0 | cleanup: |
1431 | 0 | if (ret < 0) { |
1432 | 0 | _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_ERROR); |
1433 | 0 | } else { |
1434 | 0 | _gnutls_switch_fips_state(GNUTLS_FIPS140_OP_NOT_APPROVED); |
1435 | 0 | } |
1436 | 0 | return ret; |
1437 | 0 | } |
1438 | | |
1439 | | static int privkey_sign_prehashed(gnutls_privkey_t signer, |
1440 | | const gnutls_sign_entry_st *se, |
1441 | | const gnutls_datum_t *hash_data, |
1442 | | gnutls_datum_t *signature, |
1443 | | gnutls_x509_spki_st *params) |
1444 | 0 | { |
1445 | 0 | int ret; |
1446 | 0 | gnutls_datum_t digest; |
1447 | |
|
1448 | 0 | if (unlikely(se == NULL)) |
1449 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1450 | | |
1451 | 0 | if (se->id == GNUTLS_SIGN_RSA_RAW) { |
1452 | 0 | return privkey_sign_raw_data(signer, se, hash_data, signature, |
1453 | 0 | params); |
1454 | 0 | } |
1455 | | |
1456 | 0 | if (_gnutls_pk_is_not_prehashed(signer->pk_algorithm)) { |
1457 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1458 | 0 | } |
1459 | | |
1460 | 0 | digest.data = gnutls_malloc(hash_data->size); |
1461 | 0 | if (digest.data == NULL) { |
1462 | 0 | gnutls_assert(); |
1463 | 0 | return GNUTLS_E_MEMORY_ERROR; |
1464 | 0 | } |
1465 | 0 | digest.size = hash_data->size; |
1466 | 0 | memcpy(digest.data, hash_data->data, digest.size); |
1467 | |
|
1468 | 0 | ret = pk_prepare_hash(se->pk, hash_to_entry(se->hash), &digest); |
1469 | 0 | if (ret < 0) { |
1470 | 0 | gnutls_assert(); |
1471 | 0 | goto cleanup; |
1472 | 0 | } |
1473 | | |
1474 | 0 | ret = privkey_sign_raw_data(signer, se, &digest, signature, params); |
1475 | 0 | if (ret < 0) { |
1476 | 0 | gnutls_assert(); |
1477 | 0 | goto cleanup; |
1478 | 0 | } |
1479 | | |
1480 | 0 | ret = 0; |
1481 | |
|
1482 | 0 | cleanup: |
1483 | 0 | _gnutls_free_datum(&digest); |
1484 | 0 | return ret; |
1485 | 0 | } |
1486 | | |
1487 | | /*- |
1488 | | * privkey_sign_raw_data: |
1489 | | * @key: Holds the key |
1490 | | * @data: holds the data to be signed |
1491 | | * @signature: will contain the signature allocated with gnutls_malloc() |
1492 | | * @params: holds the signing parameters |
1493 | | * |
1494 | | * This function will sign the given data using a signature algorithm |
1495 | | * supported by the private key. Note that this is a low-level function |
1496 | | * and does not apply any preprocessing or hash on the signed data. |
1497 | | * For example on an RSA key the input @data should be of the DigestInfo |
1498 | | * PKCS #1 1.5 format, on RSA-PSS, DSA or ECDSA the input should be a hash output |
1499 | | * and on Ed25519 the raw data to be signed. |
1500 | | * |
1501 | | * Note this function is equivalent to using the %GNUTLS_PRIVKEY_SIGN_FLAG_TLS1_RSA |
1502 | | * flag with gnutls_privkey_sign_hash(). |
1503 | | * |
1504 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1505 | | * negative error value. |
1506 | | * |
1507 | | * Since: 3.1.10 |
1508 | | -*/ |
1509 | | int privkey_sign_raw_data(gnutls_privkey_t key, const gnutls_sign_entry_st *se, |
1510 | | const gnutls_datum_t *data, gnutls_datum_t *signature, |
1511 | | gnutls_x509_spki_st *params) |
1512 | 0 | { |
1513 | 0 | if (unlikely(se == NULL)) |
1514 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1515 | | |
1516 | 0 | switch (key->type) { |
1517 | | #ifdef ENABLE_PKCS11 |
1518 | | case GNUTLS_PRIVKEY_PKCS11: |
1519 | | return _gnutls_pkcs11_privkey_sign(key->key.pkcs11, se, data, |
1520 | | signature, params); |
1521 | | #endif |
1522 | 0 | case GNUTLS_PRIVKEY_X509: |
1523 | 0 | return _gnutls_pk_sign(se->pk, signature, data, |
1524 | 0 | &key->key.x509->params, params); |
1525 | 0 | case GNUTLS_PRIVKEY_EXT: |
1526 | 0 | if (unlikely(key->key.ext.sign_data_func == NULL && |
1527 | 0 | key->key.ext.sign_hash_func == NULL && |
1528 | 0 | key->key.ext.sign_func == NULL)) |
1529 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1530 | | |
1531 | 0 | if (_gnutls_pk_is_not_prehashed(se->pk)) { |
1532 | 0 | if (!key->key.ext.sign_data_func) |
1533 | 0 | return gnutls_assert_val( |
1534 | 0 | GNUTLS_E_INVALID_REQUEST); |
1535 | | |
1536 | 0 | return key->key.ext.sign_data_func( |
1537 | 0 | key, se->id, key->key.ext.userdata, 0, data, |
1538 | 0 | signature); |
1539 | 0 | } else if (key->key.ext.sign_hash_func) { |
1540 | 0 | if (se->pk == GNUTLS_PK_RSA) { |
1541 | 0 | se = _gnutls_sign_to_entry(GNUTLS_SIGN_RSA_RAW); |
1542 | 0 | assert(se != NULL); |
1543 | 0 | } |
1544 | | |
1545 | | /* se may not be set here if we are doing legacy RSA */ |
1546 | 0 | return key->key.ext.sign_hash_func( |
1547 | 0 | key, se->id, key->key.ext.userdata, 0, data, |
1548 | 0 | signature); |
1549 | 0 | } else { |
1550 | 0 | if (!PK_IS_OK_FOR_EXT2(se->pk)) |
1551 | 0 | return gnutls_assert_val( |
1552 | 0 | GNUTLS_E_INVALID_REQUEST); |
1553 | | |
1554 | 0 | return key->key.ext.sign_func( |
1555 | 0 | key, key->key.ext.userdata, data, signature); |
1556 | 0 | } |
1557 | 0 | default: |
1558 | 0 | gnutls_assert(); |
1559 | 0 | return GNUTLS_E_INVALID_REQUEST; |
1560 | 0 | } |
1561 | 0 | } |
1562 | | |
1563 | | /** |
1564 | | * gnutls_privkey_decrypt_data: |
1565 | | * @key: Holds the key |
1566 | | * @flags: zero for now |
1567 | | * @ciphertext: holds the data to be decrypted |
1568 | | * @plaintext: will contain the decrypted data, allocated with gnutls_malloc() |
1569 | | * |
1570 | | * This function will decrypt the given data using the algorithm |
1571 | | * supported by the private key. |
1572 | | * |
1573 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1574 | | * negative error value. |
1575 | | * |
1576 | | * Since: 2.12.0 |
1577 | | **/ |
1578 | | int gnutls_privkey_decrypt_data(gnutls_privkey_t key, unsigned int flags, |
1579 | | const gnutls_datum_t *ciphertext, |
1580 | | gnutls_datum_t *plaintext) |
1581 | 0 | { |
1582 | 0 | switch (key->type) { |
1583 | 0 | case GNUTLS_PRIVKEY_X509: |
1584 | 0 | return _gnutls_pk_decrypt(key->pk_algorithm, plaintext, |
1585 | 0 | ciphertext, &key->key.x509->params); |
1586 | | #ifdef ENABLE_PKCS11 |
1587 | | case GNUTLS_PRIVKEY_PKCS11: |
1588 | | return _gnutls_pkcs11_privkey_decrypt_data( |
1589 | | key->key.pkcs11, flags, ciphertext, plaintext); |
1590 | | #endif |
1591 | 0 | case GNUTLS_PRIVKEY_EXT: |
1592 | 0 | if (key->key.ext.decrypt_func == NULL) |
1593 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1594 | | |
1595 | 0 | return key->key.ext.decrypt_func(key, key->key.ext.userdata, |
1596 | 0 | ciphertext, plaintext); |
1597 | 0 | default: |
1598 | 0 | gnutls_assert(); |
1599 | 0 | return GNUTLS_E_INVALID_REQUEST; |
1600 | 0 | } |
1601 | 0 | } |
1602 | | |
1603 | | /** |
1604 | | * gnutls_privkey_decrypt_data2: |
1605 | | * @key: Holds the key |
1606 | | * @flags: zero for now |
1607 | | * @ciphertext: holds the data to be decrypted |
1608 | | * @plaintext: a preallocated buffer that will be filled with the plaintext |
1609 | | * @plaintext_size: in/out size of the plaintext |
1610 | | * |
1611 | | * This function will decrypt the given data using the algorithm |
1612 | | * supported by the private key. Unlike with gnutls_privkey_decrypt_data() |
1613 | | * this function operates in constant time and constant memory access. |
1614 | | * |
1615 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1616 | | * negative error value. |
1617 | | * |
1618 | | * Since: 3.6.5 |
1619 | | **/ |
1620 | | |
1621 | | int gnutls_privkey_decrypt_data2(gnutls_privkey_t key, unsigned int flags, |
1622 | | const gnutls_datum_t *ciphertext, |
1623 | | unsigned char *plaintext, |
1624 | | size_t plaintext_size) |
1625 | 0 | { |
1626 | | /* Note: except for the backwards compatibility function, no |
1627 | | * conditional code should be called after the decryption |
1628 | | * function call, to avoid creating oracle attacks based |
1629 | | * on cache/timing side channels */ |
1630 | | |
1631 | | /* backwards compatibility */ |
1632 | 0 | if (key->type == GNUTLS_PRIVKEY_EXT && |
1633 | 0 | key->key.ext.decrypt_func2 == NULL && |
1634 | 0 | key->key.ext.decrypt_func != NULL) { |
1635 | 0 | gnutls_datum_t plain; |
1636 | 0 | int ret; |
1637 | 0 | ret = key->key.ext.decrypt_func(key, key->key.ext.userdata, |
1638 | 0 | ciphertext, &plain); |
1639 | 0 | if (plain.size != plaintext_size) { |
1640 | 0 | ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1641 | 0 | } else { |
1642 | 0 | memcpy(plaintext, plain.data, plain.size); |
1643 | 0 | } |
1644 | 0 | gnutls_free(plain.data); |
1645 | 0 | return ret; |
1646 | 0 | } |
1647 | | |
1648 | 0 | switch (key->type) { |
1649 | 0 | case GNUTLS_PRIVKEY_X509: |
1650 | 0 | return _gnutls_pk_decrypt2(key->pk_algorithm, ciphertext, |
1651 | 0 | plaintext, plaintext_size, |
1652 | 0 | &key->key.x509->params); |
1653 | | #ifdef ENABLE_PKCS11 |
1654 | | case GNUTLS_PRIVKEY_PKCS11: |
1655 | | return _gnutls_pkcs11_privkey_decrypt_data2(key->key.pkcs11, |
1656 | | flags, ciphertext, |
1657 | | plaintext, |
1658 | | plaintext_size); |
1659 | | #endif |
1660 | 0 | case GNUTLS_PRIVKEY_EXT: |
1661 | 0 | if (key->key.ext.decrypt_func2 == NULL) |
1662 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1663 | | |
1664 | 0 | return key->key.ext.decrypt_func2(key, key->key.ext.userdata, |
1665 | 0 | ciphertext, plaintext, |
1666 | 0 | plaintext_size); |
1667 | 0 | default: |
1668 | 0 | gnutls_assert(); |
1669 | 0 | return GNUTLS_E_INVALID_REQUEST; |
1670 | 0 | } |
1671 | 0 | } |
1672 | | |
1673 | | /** |
1674 | | * gnutls_privkey_import_x509_raw: |
1675 | | * @pkey: The private key |
1676 | | * @data: The private key data to be imported |
1677 | | * @format: The format of the private key |
1678 | | * @password: A password (optional) |
1679 | | * @flags: an ORed sequence of gnutls_pkcs_encrypt_flags_t |
1680 | | * |
1681 | | * This function will import the given private key to the abstract |
1682 | | * #gnutls_privkey_t type. |
1683 | | * |
1684 | | * The supported formats are basic unencrypted key, PKCS8, PKCS12, |
1685 | | * TSS2, and the openssl format. |
1686 | | * |
1687 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1688 | | * negative error value. |
1689 | | * |
1690 | | * Since: 3.1.0 |
1691 | | **/ |
1692 | | int gnutls_privkey_import_x509_raw(gnutls_privkey_t pkey, |
1693 | | const gnutls_datum_t *data, |
1694 | | gnutls_x509_crt_fmt_t format, |
1695 | | const char *password, unsigned int flags) |
1696 | 0 | { |
1697 | 0 | gnutls_x509_privkey_t xpriv; |
1698 | 0 | int ret; |
1699 | |
|
1700 | | #ifdef HAVE_TSS2 |
1701 | | if (format == GNUTLS_X509_FMT_PEM && |
1702 | | memmem(data->data, data->size, "--BEGIN TSS2", 12) != NULL) { |
1703 | | ret = _gnutls_load_tpm2_key(pkey, data); |
1704 | | if (ret < 0) |
1705 | | return gnutls_assert_val(ret); |
1706 | | |
1707 | | return 0; |
1708 | | } |
1709 | | #endif |
1710 | |
|
1711 | 0 | ret = gnutls_x509_privkey_init(&xpriv); |
1712 | 0 | if (ret < 0) |
1713 | 0 | return gnutls_assert_val(ret); |
1714 | | |
1715 | 0 | if (pkey->pin.cb) { |
1716 | 0 | gnutls_x509_privkey_set_pin_function(xpriv, pkey->pin.cb, |
1717 | 0 | pkey->pin.data); |
1718 | 0 | } |
1719 | |
|
1720 | 0 | ret = gnutls_x509_privkey_import2(xpriv, data, format, password, flags); |
1721 | 0 | if (ret < 0) { |
1722 | 0 | gnutls_assert(); |
1723 | 0 | goto cleanup; |
1724 | 0 | } |
1725 | | |
1726 | 0 | ret = gnutls_privkey_import_x509(pkey, xpriv, |
1727 | 0 | GNUTLS_PRIVKEY_IMPORT_AUTO_RELEASE); |
1728 | 0 | if (ret < 0) { |
1729 | 0 | gnutls_assert(); |
1730 | 0 | goto cleanup; |
1731 | 0 | } |
1732 | | |
1733 | 0 | return 0; |
1734 | | |
1735 | 0 | cleanup: |
1736 | 0 | gnutls_x509_privkey_deinit(xpriv); |
1737 | |
|
1738 | 0 | return ret; |
1739 | 0 | } |
1740 | | |
1741 | | /** |
1742 | | * gnutls_privkey_import_url: |
1743 | | * @key: A key of type #gnutls_privkey_t |
1744 | | * @url: A PKCS 11 url |
1745 | | * @flags: should be zero |
1746 | | * |
1747 | | * This function will import a PKCS11 or TPM URL as a |
1748 | | * private key. The supported URL types can be checked |
1749 | | * using gnutls_url_is_supported(). |
1750 | | * |
1751 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1752 | | * negative error value. |
1753 | | * |
1754 | | * Since: 3.1.0 |
1755 | | **/ |
1756 | | int gnutls_privkey_import_url(gnutls_privkey_t key, const char *url, |
1757 | | unsigned int flags) |
1758 | 0 | { |
1759 | 0 | unsigned i; |
1760 | 0 | int ret; |
1761 | |
|
1762 | 0 | for (i = 0; i < _gnutls_custom_urls_size; i++) { |
1763 | 0 | if (strncmp(url, _gnutls_custom_urls[i].name, |
1764 | 0 | _gnutls_custom_urls[i].name_size) == 0) { |
1765 | 0 | if (_gnutls_custom_urls[i].import_key) { |
1766 | 0 | ret = _gnutls_custom_urls[i].import_key( |
1767 | 0 | key, url, flags); |
1768 | 0 | goto cleanup; |
1769 | 0 | } |
1770 | 0 | break; |
1771 | 0 | } |
1772 | 0 | } |
1773 | | |
1774 | 0 | if (strncmp(url, PKCS11_URL, PKCS11_URL_SIZE) == 0) { |
1775 | | #ifdef ENABLE_PKCS11 |
1776 | | ret = _gnutls_privkey_import_pkcs11_url(key, url, flags); |
1777 | | #else |
1778 | 0 | ret = gnutls_assert_val(GNUTLS_E_UNIMPLEMENTED_FEATURE); |
1779 | 0 | #endif |
1780 | 0 | goto cleanup; |
1781 | 0 | } |
1782 | | |
1783 | 0 | if (strncmp(url, TPMKEY_URL, TPMKEY_URL_SIZE) == 0) { |
1784 | | #ifdef HAVE_TROUSERS |
1785 | | ret = gnutls_privkey_import_tpm_url(key, url, NULL, NULL, 0); |
1786 | | #else |
1787 | 0 | ret = gnutls_assert_val(GNUTLS_E_UNIMPLEMENTED_FEATURE); |
1788 | 0 | #endif |
1789 | 0 | goto cleanup; |
1790 | 0 | } |
1791 | | |
1792 | 0 | if (strncmp(url, SYSTEM_URL, SYSTEM_URL_SIZE) == 0) { |
1793 | 0 | ret = _gnutls_privkey_import_system_url(key, url); |
1794 | 0 | goto cleanup; |
1795 | 0 | } |
1796 | | |
1797 | 0 | ret = gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
1798 | 0 | cleanup: |
1799 | 0 | return ret; |
1800 | 0 | } |
1801 | | |
1802 | | /** |
1803 | | * gnutls_privkey_set_pin_function: |
1804 | | * @key: A key of type #gnutls_privkey_t |
1805 | | * @fn: the callback |
1806 | | * @userdata: data associated with the callback |
1807 | | * |
1808 | | * This function will set a callback function to be used when |
1809 | | * required to access the object. This function overrides any other |
1810 | | * global PIN functions. |
1811 | | * |
1812 | | * Note that this function must be called right after initialization |
1813 | | * to have effect. |
1814 | | * |
1815 | | * Since: 3.1.0 |
1816 | | * |
1817 | | **/ |
1818 | | void gnutls_privkey_set_pin_function(gnutls_privkey_t key, |
1819 | | gnutls_pin_callback_t fn, void *userdata) |
1820 | 0 | { |
1821 | 0 | key->pin.cb = fn; |
1822 | 0 | key->pin.data = userdata; |
1823 | 0 | } |
1824 | | |
1825 | | /** |
1826 | | * gnutls_privkey_set_flags: |
1827 | | * @key: A key of type #gnutls_privkey_t |
1828 | | * @flags: flags from the %gnutls_privkey_flags |
1829 | | * |
1830 | | * This function will set flags for the specified private key, after |
1831 | | * it is generated. Currently this is useful for the %GNUTLS_PRIVKEY_FLAG_EXPORT_COMPAT |
1832 | | * to allow exporting a "provable" private key in backwards compatible way. |
1833 | | * |
1834 | | * Since: 3.5.0 |
1835 | | * |
1836 | | **/ |
1837 | | void gnutls_privkey_set_flags(gnutls_privkey_t key, unsigned int flags) |
1838 | 0 | { |
1839 | 0 | key->flags |= flags; |
1840 | 0 | if (key->type == GNUTLS_PRIVKEY_X509) |
1841 | 0 | gnutls_x509_privkey_set_flags(key->key.x509, flags); |
1842 | 0 | } |
1843 | | |
1844 | | /** |
1845 | | * gnutls_privkey_status: |
1846 | | * @key: Holds the key |
1847 | | * |
1848 | | * Checks the status of the private key token. This function |
1849 | | * is an actual wrapper over gnutls_pkcs11_privkey_status(), and |
1850 | | * if the private key is a PKCS #11 token it will check whether |
1851 | | * it is inserted or not. |
1852 | | * |
1853 | | * Returns: this function will return non-zero if the token |
1854 | | * holding the private key is still available (inserted), and zero otherwise. |
1855 | | * |
1856 | | * Since: 3.1.10 |
1857 | | * |
1858 | | **/ |
1859 | | int gnutls_privkey_status(gnutls_privkey_t key) |
1860 | 0 | { |
1861 | 0 | switch (key->type) { |
1862 | | #ifdef ENABLE_PKCS11 |
1863 | | case GNUTLS_PRIVKEY_PKCS11: |
1864 | | return gnutls_pkcs11_privkey_status(key->key.pkcs11); |
1865 | | #endif |
1866 | 0 | default: |
1867 | 0 | return 1; |
1868 | 0 | } |
1869 | 0 | } |
1870 | | |
1871 | | /** |
1872 | | * gnutls_privkey_verify_params: |
1873 | | * @key: should contain a #gnutls_privkey_t type |
1874 | | * |
1875 | | * This function will verify the private key parameters. |
1876 | | * |
1877 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1878 | | * negative error value. |
1879 | | * |
1880 | | * Since: 3.3.0 |
1881 | | **/ |
1882 | | int gnutls_privkey_verify_params(gnutls_privkey_t key) |
1883 | 0 | { |
1884 | 0 | gnutls_pk_params_st params; |
1885 | 0 | int ret; |
1886 | |
|
1887 | 0 | gnutls_pk_params_init(¶ms); |
1888 | |
|
1889 | 0 | ret = _gnutls_privkey_get_mpis(key, ¶ms); |
1890 | 0 | if (ret < 0) |
1891 | 0 | return gnutls_assert_val(ret); |
1892 | | |
1893 | 0 | ret = _gnutls_pk_verify_priv_params(key->pk_algorithm, ¶ms); |
1894 | |
|
1895 | 0 | gnutls_pk_params_release(¶ms); |
1896 | |
|
1897 | 0 | if (ret < 0) { |
1898 | 0 | gnutls_assert(); |
1899 | 0 | return ret; |
1900 | 0 | } |
1901 | | |
1902 | 0 | return 0; |
1903 | 0 | } |
1904 | | |
1905 | | /** |
1906 | | * gnutls_privkey_get_spki: |
1907 | | * @privkey: a public key of type #gnutls_privkey_t |
1908 | | * @spki: a SubjectPublicKeyInfo structure of type #gnutls_privkey_spki_t |
1909 | | * @flags: must be zero |
1910 | | * |
1911 | | * This function will return the public key information if available. |
1912 | | * The provided @spki must be initialized. |
1913 | | * |
1914 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1915 | | * negative error value. |
1916 | | * |
1917 | | * Since: 3.6.0 |
1918 | | **/ |
1919 | | int gnutls_privkey_get_spki(gnutls_privkey_t privkey, gnutls_x509_spki_t spki, |
1920 | | unsigned int flags) |
1921 | 0 | { |
1922 | 0 | gnutls_x509_spki_t p = &privkey->key.x509->params.spki; |
1923 | |
|
1924 | 0 | if (privkey == NULL || privkey->type != GNUTLS_PRIVKEY_X509) { |
1925 | 0 | gnutls_assert(); |
1926 | 0 | return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE; |
1927 | 0 | } |
1928 | | |
1929 | 0 | if (p->pk == GNUTLS_PK_UNKNOWN) |
1930 | 0 | return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE); |
1931 | | |
1932 | 0 | memcpy(spki, p, sizeof(gnutls_x509_spki_st)); |
1933 | |
|
1934 | 0 | return 0; |
1935 | 0 | } |
1936 | | |
1937 | | /** |
1938 | | * gnutls_privkey_set_spki: |
1939 | | * @privkey: a public key of type #gnutls_privkey_t |
1940 | | * @spki: a SubjectPublicKeyInfo structure of type #gnutls_privkey_spki_t |
1941 | | * @flags: must be zero |
1942 | | * |
1943 | | * This function will set the public key information. |
1944 | | * The provided @spki must be initialized. |
1945 | | * |
1946 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
1947 | | * negative error value. |
1948 | | * |
1949 | | * Since: 3.6.0 |
1950 | | **/ |
1951 | | int gnutls_privkey_set_spki(gnutls_privkey_t privkey, |
1952 | | const gnutls_x509_spki_t spki, unsigned int flags) |
1953 | 0 | { |
1954 | 0 | if (privkey == NULL || privkey->type != GNUTLS_PRIVKEY_X509) { |
1955 | 0 | gnutls_assert(); |
1956 | 0 | return GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE; |
1957 | 0 | } |
1958 | | |
1959 | 0 | return gnutls_x509_privkey_set_spki(privkey->key.x509, spki, flags); |
1960 | 0 | } |
1961 | | |
1962 | | /* Checks whether the public key given is compatible with the |
1963 | | * signature algorithm used. The session is only used for audit logging, and |
1964 | | * it may be null. |
1965 | | */ |
1966 | | unsigned _gnutls_privkey_compatible_with_sig(gnutls_privkey_t privkey, |
1967 | | gnutls_sign_algorithm_t sign) |
1968 | 0 | { |
1969 | 0 | const gnutls_sign_entry_st *se; |
1970 | |
|
1971 | 0 | if (unlikely(privkey == NULL)) |
1972 | 0 | return gnutls_assert_val(0); |
1973 | | |
1974 | 0 | se = _gnutls_sign_to_entry(sign); |
1975 | 0 | if (unlikely(se == NULL)) |
1976 | 0 | return gnutls_assert_val(0); |
1977 | | |
1978 | | /* Prevent RSA-PSS private keys from negotiating an RSA signature, |
1979 | | * and RSA keys which cannot do RSA-PSS (e.g., smart card) from |
1980 | | * negotiating RSA-PSS sig. |
1981 | | */ |
1982 | | |
1983 | 0 | if (se->pk != |
1984 | 0 | privkey->pk_algorithm) { /* if the PK algorithm of the signature differs to the one on the pubkey */ |
1985 | 0 | if (!sign_supports_priv_pk_algorithm(se, |
1986 | 0 | privkey->pk_algorithm)) { |
1987 | 0 | _gnutls_handshake_log( |
1988 | 0 | "cannot use privkey of %s with %s\n", |
1989 | 0 | gnutls_pk_get_name(privkey->pk_algorithm), |
1990 | 0 | se->name); |
1991 | 0 | return 0; |
1992 | 0 | } |
1993 | 0 | } |
1994 | | |
1995 | 0 | if (privkey->type == GNUTLS_PRIVKEY_EXT) { |
1996 | 0 | if (privkey->key.ext.info_func) { |
1997 | 0 | int ret; |
1998 | |
|
1999 | 0 | ret = privkey->key.ext.info_func( |
2000 | 0 | privkey, |
2001 | 0 | GNUTLS_SIGN_ALGO_TO_FLAGS(sign) | |
2002 | 0 | GNUTLS_PRIVKEY_INFO_HAVE_SIGN_ALGO, |
2003 | 0 | privkey->key.ext.userdata); |
2004 | 0 | if (ret != -1) |
2005 | 0 | return ret; |
2006 | | |
2007 | | /* use the old flag */ |
2008 | 0 | ret = privkey->key.ext.info_func( |
2009 | 0 | privkey, GNUTLS_PRIVKEY_INFO_SIGN_ALGO, |
2010 | 0 | privkey->key.ext.userdata); |
2011 | 0 | if (ret == (int)sign) |
2012 | 0 | return 1; |
2013 | 0 | } |
2014 | | |
2015 | | /* This key type is very limited on what it can handle */ |
2016 | 0 | if (!PK_IS_OK_FOR_EXT2(se->pk)) |
2017 | 0 | return gnutls_assert_val(0); |
2018 | 0 | } |
2019 | | #ifdef ENABLE_PKCS11 |
2020 | | else if (privkey->type == GNUTLS_PRIVKEY_PKCS11) { |
2021 | | if (privkey->pk_algorithm == GNUTLS_PK_RSA && |
2022 | | se->pk == GNUTLS_PK_RSA_PSS) { |
2023 | | if (!privkey->key.pkcs11->rsa_pss_ok) |
2024 | | return 0; |
2025 | | } |
2026 | | } |
2027 | | #endif |
2028 | | |
2029 | 0 | return 1; |
2030 | 0 | } |
2031 | | |
2032 | | /** |
2033 | | * gnutls_privkey_derive_secret: |
2034 | | * @privkey: a private key of type #gnutls_privkey_t |
2035 | | * @pubkey: a public key of type #gnutls_pubkey_t |
2036 | | * @nonce: an optional nonce value |
2037 | | * @secret: where shared secret will be stored |
2038 | | * @flags: must be zero |
2039 | | * |
2040 | | * This function will calculate a shared secret from our @privkey and |
2041 | | * peer's @pubkey. The result will be stored in @secret, whose data |
2042 | | * member should be freed after use using gnutls_free(). @privkey and |
2043 | | * @pubkey must be backed by the X.509 keys. |
2044 | | * |
2045 | | * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a |
2046 | | * negative error value. |
2047 | | * |
2048 | | * Since: 3.8.2 |
2049 | | **/ |
2050 | | int gnutls_privkey_derive_secret(gnutls_privkey_t privkey, |
2051 | | gnutls_pubkey_t pubkey, |
2052 | | const gnutls_datum_t *nonce, |
2053 | | gnutls_datum_t *secret, unsigned int flags) |
2054 | 0 | { |
2055 | 0 | if (unlikely(privkey == NULL || privkey->type != GNUTLS_PRIVKEY_X509)) { |
2056 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
2057 | 0 | } |
2058 | | |
2059 | 0 | if (unlikely(pubkey == NULL || |
2060 | 0 | pubkey->params.algo != privkey->pk_algorithm)) { |
2061 | 0 | return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST); |
2062 | 0 | } |
2063 | | |
2064 | 0 | return _gnutls_pk_derive_nonce(privkey->pk_algorithm, secret, |
2065 | 0 | &privkey->key.x509->params, |
2066 | 0 | &pubkey->params, nonce); |
2067 | 0 | } |