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