/src/gnutls/lib/x509/privkey.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright (C) 2003-2016 Free Software Foundation, Inc.  | 
3  |  |  * Copyright (C) 2012-2016 Nikos Mavrogiannopoulos  | 
4  |  |  * Copyright (C) 2015-2017 Red Hat, Inc.  | 
5  |  |  *  | 
6  |  |  * Author: Nikos Mavrogiannopoulos  | 
7  |  |  *  | 
8  |  |  * This file is part of GnuTLS.  | 
9  |  |  *  | 
10  |  |  * The GnuTLS is free software; you can redistribute it and/or  | 
11  |  |  * modify it under the terms of the GNU Lesser General Public License  | 
12  |  |  * as published by the Free Software Foundation; either version 2.1 of  | 
13  |  |  * the License, or (at your option) any later version.  | 
14  |  |  *  | 
15  |  |  * This library is distributed in the hope that it will be useful, but  | 
16  |  |  * WITHOUT ANY WARRANTY; without even the implied warranty of  | 
17  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  | 
18  |  |  * Lesser General Public License for more details.  | 
19  |  |  *  | 
20  |  |  * You should have received a copy of the GNU Lesser General Public License  | 
21  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>  | 
22  |  |  *  | 
23  |  |  */  | 
24  |  |  | 
25  |  | #include "gnutls_int.h"  | 
26  |  | #include "datum.h"  | 
27  |  | #include "global.h"  | 
28  |  | #include "errors.h"  | 
29  |  | #include "tls-sig.h"  | 
30  |  | #include "common.h"  | 
31  |  | #include "x509.h"  | 
32  |  | #include "x509_b64.h"  | 
33  |  | #include "x509_int.h"  | 
34  |  | #include "pk.h"  | 
35  |  | #include "mpi.h"  | 
36  |  | #include "ecc.h"  | 
37  |  | #include "pin.h"  | 
38  |  |  | 
39  |  | /**  | 
40  |  |  * gnutls_x509_privkey_init:  | 
41  |  |  * @key: A pointer to the type to be initialized  | 
42  |  |  *  | 
43  |  |  * This function will initialize a private key type.  | 
44  |  |  *  | 
45  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
46  |  |  *   negative error value.  | 
47  |  |  **/  | 
48  |  | int gnutls_x509_privkey_init(gnutls_x509_privkey_t *key)  | 
49  | 0  | { | 
50  | 0  |   *key = NULL;  | 
51  | 0  |   FAIL_IF_LIB_ERROR;  | 
52  |  |  | 
53  | 0  |   *key = gnutls_calloc(1, sizeof(gnutls_x509_privkey_int));  | 
54  |  | 
  | 
55  | 0  |   if (*key) { | 
56  | 0  |     (*key)->key = NULL;  | 
57  | 0  |     return 0; /* success */  | 
58  | 0  |   }  | 
59  |  |  | 
60  | 0  |   return GNUTLS_E_MEMORY_ERROR;  | 
61  | 0  | }  | 
62  |  |  | 
63  |  | void _gnutls_x509_privkey_reinit(gnutls_x509_privkey_t key)  | 
64  | 0  | { | 
65  | 0  |   gnutls_pk_params_clear(&key->params);  | 
66  | 0  |   gnutls_pk_params_release(&key->params);  | 
67  |  |   /* avoid reuse of fields which may have had some sensible value */  | 
68  | 0  |   zeroize_key(&key->params, sizeof(key->params));  | 
69  |  | 
  | 
70  | 0  |   if (key->key)  | 
71  | 0  |     asn1_delete_structure2(&key->key, ASN1_DELETE_FLAG_ZEROIZE);  | 
72  | 0  |   key->key = NULL;  | 
73  | 0  | }  | 
74  |  |  | 
75  |  | /**  | 
76  |  |  * gnutls_x509_privkey_deinit:  | 
77  |  |  * @key: The key to be deinitialized  | 
78  |  |  *  | 
79  |  |  * This function will deinitialize a private key structure.  | 
80  |  |  **/  | 
81  |  | void gnutls_x509_privkey_deinit(gnutls_x509_privkey_t key)  | 
82  | 0  | { | 
83  | 0  |   if (!key)  | 
84  | 0  |     return;  | 
85  |  |  | 
86  | 0  |   _gnutls_x509_privkey_reinit(key);  | 
87  | 0  |   gnutls_free(key);  | 
88  | 0  | }  | 
89  |  |  | 
90  |  | /**  | 
91  |  |  * gnutls_x509_privkey_cpy:  | 
92  |  |  * @dst: The destination key, which should be initialized.  | 
93  |  |  * @src: The source key  | 
94  |  |  *  | 
95  |  |  * This function will copy a private key from source to destination  | 
96  |  |  * key. Destination has to be initialized.  | 
97  |  |  *  | 
98  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
99  |  |  *   negative error value.  | 
100  |  |  **/  | 
101  |  | int gnutls_x509_privkey_cpy(gnutls_x509_privkey_t dst,  | 
102  |  |           gnutls_x509_privkey_t src)  | 
103  | 0  | { | 
104  | 0  |   int ret;  | 
105  |  | 
  | 
106  | 0  |   if (!src || !dst)  | 
107  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
108  |  |  | 
109  | 0  |   ret = _gnutls_pk_params_copy(&dst->params, &src->params);  | 
110  | 0  |   if (ret < 0) { | 
111  | 0  |     return gnutls_assert_val(ret);  | 
112  | 0  |   }  | 
113  |  |  | 
114  | 0  |   ret = _gnutls_asn1_encode_privkey(&dst->key, &dst->params);  | 
115  | 0  |   if (ret < 0) { | 
116  | 0  |     gnutls_assert();  | 
117  | 0  |     gnutls_pk_params_release(&dst->params);  | 
118  | 0  |     return ret;  | 
119  | 0  |   }  | 
120  |  |  | 
121  | 0  |   return 0;  | 
122  | 0  | }  | 
123  |  |  | 
124  |  | /* Converts an RSA PKCS#1 key to  | 
125  |  |  * an internal structure (gnutls_private_key)  | 
126  |  |  */  | 
127  |  | asn1_node _gnutls_privkey_decode_pkcs1_rsa_key(const gnutls_datum_t *raw_key,  | 
128  |  |                  gnutls_x509_privkey_t pkey)  | 
129  | 0  | { | 
130  | 0  |   int result;  | 
131  | 0  |   asn1_node pkey_asn;  | 
132  |  | 
  | 
133  | 0  |   gnutls_pk_params_init(&pkey->params);  | 
134  |  | 
  | 
135  | 0  |   if (asn1_create_element(_gnutls_get_gnutls_asn(),  | 
136  | 0  |         "GNUTLS.RSAPrivateKey",  | 
137  | 0  |         &pkey_asn) != ASN1_SUCCESS) { | 
138  | 0  |     gnutls_assert();  | 
139  | 0  |     return NULL;  | 
140  | 0  |   }  | 
141  |  |  | 
142  | 0  |   result = _asn1_strict_der_decode(&pkey_asn, raw_key->data,  | 
143  | 0  |            raw_key->size, NULL);  | 
144  | 0  |   if (result != ASN1_SUCCESS) { | 
145  | 0  |     gnutls_assert();  | 
146  | 0  |     goto error;  | 
147  | 0  |   }  | 
148  |  |  | 
149  | 0  |   if (_gnutls_x509_read_int(pkey_asn, "modulus",  | 
150  | 0  |           &pkey->params.params[0]) < 0) { | 
151  | 0  |     gnutls_assert();  | 
152  | 0  |     goto error;  | 
153  | 0  |   }  | 
154  | 0  |   pkey->params.params_nr++;  | 
155  |  | 
  | 
156  | 0  |   if (_gnutls_x509_read_int(pkey_asn, "publicExponent",  | 
157  | 0  |           &pkey->params.params[1]) < 0) { | 
158  | 0  |     gnutls_assert();  | 
159  | 0  |     goto error;  | 
160  | 0  |   }  | 
161  | 0  |   pkey->params.params_nr++;  | 
162  |  | 
  | 
163  | 0  |   if (_gnutls_x509_read_key_int(pkey_asn, "privateExponent",  | 
164  | 0  |               &pkey->params.params[2]) < 0) { | 
165  | 0  |     gnutls_assert();  | 
166  | 0  |     goto error;  | 
167  | 0  |   }  | 
168  | 0  |   pkey->params.params_nr++;  | 
169  |  | 
  | 
170  | 0  |   if (_gnutls_x509_read_key_int(pkey_asn, "prime1",  | 
171  | 0  |               &pkey->params.params[3]) < 0) { | 
172  | 0  |     gnutls_assert();  | 
173  | 0  |     goto error;  | 
174  | 0  |   }  | 
175  | 0  |   pkey->params.params_nr++;  | 
176  |  | 
  | 
177  | 0  |   if (_gnutls_x509_read_key_int(pkey_asn, "prime2",  | 
178  | 0  |               &pkey->params.params[4]) < 0) { | 
179  | 0  |     gnutls_assert();  | 
180  | 0  |     goto error;  | 
181  | 0  |   }  | 
182  | 0  |   pkey->params.params_nr++;  | 
183  |  | 
  | 
184  | 0  |   if (_gnutls_x509_read_key_int(pkey_asn, "coefficient",  | 
185  | 0  |               &pkey->params.params[5]) < 0) { | 
186  | 0  |     gnutls_assert();  | 
187  | 0  |     goto error;  | 
188  | 0  |   }  | 
189  | 0  |   pkey->params.params_nr++;  | 
190  |  | 
  | 
191  | 0  |   if (_gnutls_x509_read_key_int(pkey_asn, "exponent1",  | 
192  | 0  |               &pkey->params.params[6]) < 0) { | 
193  | 0  |     gnutls_assert();  | 
194  | 0  |     goto error;  | 
195  | 0  |   }  | 
196  | 0  |   pkey->params.params_nr++;  | 
197  |  | 
  | 
198  | 0  |   if (_gnutls_x509_read_key_int(pkey_asn, "exponent2",  | 
199  | 0  |               &pkey->params.params[7]) < 0) { | 
200  | 0  |     gnutls_assert();  | 
201  | 0  |     goto error;  | 
202  | 0  |   }  | 
203  | 0  |   pkey->params.params_nr++;  | 
204  |  | 
  | 
205  | 0  |   pkey->params.params_nr = RSA_PRIVATE_PARAMS;  | 
206  | 0  |   pkey->params.algo = GNUTLS_PK_RSA;  | 
207  |  | 
  | 
208  | 0  |   return pkey_asn;  | 
209  |  |  | 
210  | 0  | error:  | 
211  | 0  |   asn1_delete_structure2(&pkey_asn, ASN1_DELETE_FLAG_ZEROIZE);  | 
212  | 0  |   gnutls_pk_params_clear(&pkey->params);  | 
213  | 0  |   gnutls_pk_params_release(&pkey->params);  | 
214  | 0  |   return NULL;  | 
215  | 0  | }  | 
216  |  |  | 
217  |  | /* Converts an ECC key to  | 
218  |  |  * an internal structure (gnutls_private_key)  | 
219  |  |  */  | 
220  |  | int _gnutls_privkey_decode_ecc_key(asn1_node *pkey_asn,  | 
221  |  |            const gnutls_datum_t *raw_key,  | 
222  |  |            gnutls_x509_privkey_t pkey,  | 
223  |  |            gnutls_ecc_curve_t curve)  | 
224  | 0  | { | 
225  | 0  |   int ret;  | 
226  | 0  |   unsigned int version;  | 
227  | 0  |   char oid[MAX_OID_SIZE];  | 
228  | 0  |   int oid_size;  | 
229  | 0  |   gnutls_datum_t out;  | 
230  |  | 
  | 
231  | 0  |   if (curve_is_eddsa(curve)) { | 
232  | 0  |     return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
233  | 0  |   }  | 
234  |  |  | 
235  | 0  |   gnutls_pk_params_init(&pkey->params);  | 
236  |  | 
  | 
237  | 0  |   if ((ret = asn1_create_element(_gnutls_get_gnutls_asn(),  | 
238  | 0  |                "GNUTLS.ECPrivateKey", pkey_asn)) !=  | 
239  | 0  |       ASN1_SUCCESS) { | 
240  | 0  |     gnutls_assert();  | 
241  | 0  |     return _gnutls_asn2err(ret);  | 
242  | 0  |   }  | 
243  |  |  | 
244  | 0  |   ret = _asn1_strict_der_decode(pkey_asn, raw_key->data, raw_key->size,  | 
245  | 0  |               NULL);  | 
246  | 0  |   if (ret != ASN1_SUCCESS) { | 
247  | 0  |     gnutls_assert();  | 
248  | 0  |     ret = _gnutls_asn2err(ret);  | 
249  | 0  |     goto error;  | 
250  | 0  |   }  | 
251  |  |  | 
252  | 0  |   ret = _gnutls_x509_read_uint(*pkey_asn, "Version", &version);  | 
253  | 0  |   if (ret < 0) { | 
254  | 0  |     gnutls_assert();  | 
255  | 0  |     goto error;  | 
256  | 0  |   }  | 
257  |  |  | 
258  | 0  |   if (version != 1) { | 
259  | 0  |     _gnutls_debug_log(  | 
260  | 0  |       "ECC private key version %u is not supported\n",  | 
261  | 0  |       version);  | 
262  | 0  |     gnutls_assert();  | 
263  | 0  |     ret = GNUTLS_E_ECC_UNSUPPORTED_CURVE;  | 
264  | 0  |     goto error;  | 
265  | 0  |   }  | 
266  |  |  | 
267  |  |   /* read the curve */  | 
268  | 0  |   if (curve == GNUTLS_ECC_CURVE_INVALID) { | 
269  | 0  |     oid_size = sizeof(oid);  | 
270  | 0  |     ret = asn1_read_value(*pkey_asn, "parameters.namedCurve", oid,  | 
271  | 0  |               &oid_size);  | 
272  | 0  |     if (ret != ASN1_SUCCESS) { | 
273  | 0  |       gnutls_assert();  | 
274  | 0  |       ret = _gnutls_asn2err(ret);  | 
275  | 0  |       goto error;  | 
276  | 0  |     }  | 
277  |  |  | 
278  | 0  |     pkey->params.curve = gnutls_oid_to_ecc_curve(oid);  | 
279  |  | 
  | 
280  | 0  |     if (pkey->params.curve == GNUTLS_ECC_CURVE_INVALID) { | 
281  | 0  |       _gnutls_debug_log("Curve %s is not supported\n", oid); | 
282  | 0  |       gnutls_assert();  | 
283  | 0  |       ret = GNUTLS_E_ECC_UNSUPPORTED_CURVE;  | 
284  | 0  |       goto error;  | 
285  | 0  |     }  | 
286  | 0  |   } else { | 
287  | 0  |     pkey->params.curve = curve;  | 
288  | 0  |   }  | 
289  |  |  | 
290  |  |   /* read the public key */  | 
291  | 0  |   ret = _gnutls_x509_read_value(*pkey_asn, "publicKey", &out);  | 
292  | 0  |   if (ret < 0) { | 
293  | 0  |     gnutls_assert();  | 
294  | 0  |     goto error;  | 
295  | 0  |   }  | 
296  |  |  | 
297  | 0  |   ret = _gnutls_ecc_ansi_x962_import(out.data, out.size,  | 
298  | 0  |              &pkey->params.params[ECC_X],  | 
299  | 0  |              &pkey->params.params[ECC_Y]);  | 
300  |  | 
  | 
301  | 0  |   _gnutls_free_datum(&out);  | 
302  | 0  |   if (ret < 0) { | 
303  | 0  |     gnutls_assert();  | 
304  | 0  |     goto error;  | 
305  | 0  |   }  | 
306  | 0  |   pkey->params.params_nr += 2;  | 
307  |  |  | 
308  |  |   /* read the private key */  | 
309  | 0  |   ret = _gnutls_x509_read_key_int(*pkey_asn, "privateKey",  | 
310  | 0  |           &pkey->params.params[ECC_K]);  | 
311  | 0  |   if (ret < 0) { | 
312  | 0  |     gnutls_assert();  | 
313  | 0  |     goto error;  | 
314  | 0  |   }  | 
315  | 0  |   pkey->params.params_nr++;  | 
316  | 0  |   pkey->params.algo = GNUTLS_PK_EC;  | 
317  |  | 
  | 
318  | 0  |   return 0;  | 
319  |  |  | 
320  | 0  | error:  | 
321  | 0  |   asn1_delete_structure2(pkey_asn, ASN1_DELETE_FLAG_ZEROIZE);  | 
322  | 0  |   gnutls_pk_params_clear(&pkey->params);  | 
323  | 0  |   gnutls_pk_params_release(&pkey->params);  | 
324  | 0  |   return ret;  | 
325  | 0  | }  | 
326  |  |  | 
327  |  | static int decode_ml_dsa_key(asn1_node *pkey_asn, const gnutls_datum_t *raw_key,  | 
328  |  |            gnutls_x509_privkey_t pkey)  | 
329  | 0  | { | 
330  | 0  |   int result, ret;  | 
331  | 0  |   unsigned int version;  | 
332  | 0  |   char oid[MAX_OID_SIZE];  | 
333  | 0  |   int oid_size;  | 
334  | 0  |   size_t raw_pub_size, raw_priv_size;  | 
335  |  | 
  | 
336  | 0  |   result = _asn1_strict_der_decode(pkey_asn, raw_key->data, raw_key->size,  | 
337  | 0  |            NULL);  | 
338  | 0  |   if (result != ASN1_SUCCESS) { | 
339  | 0  |     gnutls_assert();  | 
340  | 0  |     return _gnutls_asn2err(result);  | 
341  | 0  |   }  | 
342  |  |  | 
343  | 0  |   ret = _gnutls_x509_read_uint(*pkey_asn, "version", &version);  | 
344  | 0  |   if (ret < 0) { | 
345  | 0  |     gnutls_assert();  | 
346  | 0  |     return ret;  | 
347  | 0  |   }  | 
348  |  |  | 
349  | 0  |   oid_size = sizeof(oid);  | 
350  | 0  |   result = asn1_read_value(*pkey_asn, "privateKeyAlgorithm.algorithm",  | 
351  | 0  |          oid, &oid_size);  | 
352  | 0  |   if (result != ASN1_SUCCESS) { | 
353  | 0  |     gnutls_assert();  | 
354  | 0  |     return _gnutls_asn2err(result);  | 
355  | 0  |   }  | 
356  |  |  | 
357  | 0  |   pkey->params.algo = gnutls_oid_to_pk(oid);  | 
358  |  | 
  | 
359  | 0  |   switch (pkey->params.algo) { | 
360  | 0  |   case GNUTLS_PK_MLDSA44:  | 
361  | 0  |     raw_priv_size = MLDSA44_PRIVKEY_SIZE;  | 
362  | 0  |     raw_pub_size = MLDSA44_PUBKEY_SIZE;  | 
363  | 0  |     break;  | 
364  | 0  |   case GNUTLS_PK_MLDSA65:  | 
365  | 0  |     raw_priv_size = MLDSA65_PRIVKEY_SIZE;  | 
366  | 0  |     raw_pub_size = MLDSA65_PUBKEY_SIZE;  | 
367  | 0  |     break;  | 
368  | 0  |   case GNUTLS_PK_MLDSA87:  | 
369  | 0  |     raw_priv_size = MLDSA87_PRIVKEY_SIZE;  | 
370  | 0  |     raw_pub_size = MLDSA87_PUBKEY_SIZE;  | 
371  | 0  |     break;  | 
372  | 0  |   default:  | 
373  | 0  |     return gnutls_assert_val(  | 
374  | 0  |       GNUTLS_E_UNSUPPORTED_SIGNATURE_ALGORITHM);  | 
375  | 0  |   }  | 
376  |  |  | 
377  | 0  |   ret = _gnutls_x509_read_value(*pkey_asn, "privateKey",  | 
378  | 0  |               &pkey->params.raw_priv);  | 
379  | 0  |   if (ret < 0) { | 
380  | 0  |     gnutls_assert();  | 
381  | 0  |     return ret;  | 
382  | 0  |   }  | 
383  |  |  | 
384  | 0  |   switch (version) { | 
385  | 0  |   case 0:  | 
386  |  |     /* if version is 0, public key is embedded in  | 
387  |  |      * privateKey field, concatenated after a private  | 
388  |  |      * key */  | 
389  | 0  |     if (pkey->params.raw_priv.size != raw_priv_size + raw_pub_size)  | 
390  | 0  |       return gnutls_assert_val(GNUTLS_E_ASN1_DER_ERROR);  | 
391  | 0  |     ret = _gnutls_set_datum(  | 
392  | 0  |       &pkey->params.raw_pub,  | 
393  | 0  |       &pkey->params.raw_priv.data[raw_priv_size],  | 
394  | 0  |       raw_pub_size);  | 
395  | 0  |     if (ret < 0) { | 
396  | 0  |       gnutls_assert();  | 
397  | 0  |       return ret;  | 
398  | 0  |     }  | 
399  | 0  |     pkey->params.raw_priv.size = raw_priv_size;  | 
400  | 0  |     break;  | 
401  | 0  |   case 1:  | 
402  |  |     /* if version is 1, public key is embedded in a  | 
403  |  |      * separate field */  | 
404  | 0  |     ret = _gnutls_x509_read_value(*pkey_asn, "publicKey",  | 
405  | 0  |                 &pkey->params.raw_pub);  | 
406  | 0  |     if (ret < 0) { | 
407  | 0  |       gnutls_assert();  | 
408  | 0  |       return ret;  | 
409  | 0  |     }  | 
410  | 0  |     break;  | 
411  | 0  |   default:  | 
412  | 0  |     return gnutls_assert_val(GNUTLS_E_ASN1_DER_ERROR);  | 
413  | 0  |   }  | 
414  |  |  | 
415  | 0  |   if (pkey->params.raw_pub.size != raw_pub_size ||  | 
416  | 0  |       pkey->params.raw_priv.size != raw_priv_size)  | 
417  | 0  |     return gnutls_assert_val(GNUTLS_E_ASN1_DER_ERROR);  | 
418  |  |  | 
419  | 0  |   return GNUTLS_E_SUCCESS;  | 
420  | 0  | }  | 
421  |  |  | 
422  |  | static int _gnutls_privkey_decode_ml_dsa_key(asn1_node *pkey_asn,  | 
423  |  |                const gnutls_datum_t *raw_key,  | 
424  |  |                gnutls_x509_privkey_t pkey)  | 
425  | 0  | { | 
426  | 0  |   int result;  | 
427  |  | 
  | 
428  | 0  |   gnutls_pk_params_init(&pkey->params);  | 
429  |  | 
  | 
430  | 0  |   if ((result = asn1_create_element(_gnutls_get_gnutls_asn(),  | 
431  | 0  |             "GNUTLS.MLDSAPrivateKey",  | 
432  | 0  |             pkey_asn)) != ASN1_SUCCESS) { | 
433  | 0  |     gnutls_assert();  | 
434  | 0  |     return _gnutls_asn2err(result);  | 
435  | 0  |   }  | 
436  |  |  | 
437  | 0  |   result = decode_ml_dsa_key(pkey_asn, raw_key, pkey);  | 
438  | 0  |   asn1_delete_structure2(pkey_asn, ASN1_DELETE_FLAG_ZEROIZE);  | 
439  | 0  |   if (result < 0) { | 
440  | 0  |     gnutls_pk_params_clear(&pkey->params);  | 
441  | 0  |     gnutls_pk_params_release(&pkey->params);  | 
442  | 0  |   }  | 
443  |  | 
  | 
444  | 0  |   return result;  | 
445  | 0  | }  | 
446  |  |  | 
447  |  | static asn1_node decode_dsa_key(const gnutls_datum_t *raw_key,  | 
448  |  |         gnutls_x509_privkey_t pkey)  | 
449  | 0  | { | 
450  | 0  |   int result;  | 
451  | 0  |   asn1_node dsa_asn;  | 
452  | 0  |   gnutls_datum_t seed = { NULL, 0 }; | 
453  | 0  |   char oid[MAX_OID_SIZE];  | 
454  | 0  |   int oid_size;  | 
455  |  | 
  | 
456  | 0  |   if (asn1_create_element(_gnutls_get_gnutls_asn(),  | 
457  | 0  |         "GNUTLS.DSAPrivateKey",  | 
458  | 0  |         &dsa_asn) != ASN1_SUCCESS) { | 
459  | 0  |     gnutls_assert();  | 
460  | 0  |     return NULL;  | 
461  | 0  |   }  | 
462  |  |  | 
463  | 0  |   gnutls_pk_params_init(&pkey->params);  | 
464  |  | 
  | 
465  | 0  |   result = _asn1_strict_der_decode(&dsa_asn, raw_key->data, raw_key->size,  | 
466  | 0  |            NULL);  | 
467  | 0  |   if (result != ASN1_SUCCESS) { | 
468  | 0  |     gnutls_assert();  | 
469  | 0  |     goto error;  | 
470  | 0  |   }  | 
471  |  |  | 
472  | 0  |   if (_gnutls_x509_read_int(dsa_asn, "p", &pkey->params.params[0]) < 0) { | 
473  | 0  |     gnutls_assert();  | 
474  | 0  |     goto error;  | 
475  | 0  |   }  | 
476  | 0  |   pkey->params.params_nr++;  | 
477  |  | 
  | 
478  | 0  |   if (_gnutls_x509_read_int(dsa_asn, "q", &pkey->params.params[1]) < 0) { | 
479  | 0  |     gnutls_assert();  | 
480  | 0  |     goto error;  | 
481  | 0  |   }  | 
482  | 0  |   pkey->params.params_nr++;  | 
483  |  | 
  | 
484  | 0  |   if (_gnutls_x509_read_int(dsa_asn, "g", &pkey->params.params[2]) < 0) { | 
485  | 0  |     gnutls_assert();  | 
486  | 0  |     goto error;  | 
487  | 0  |   }  | 
488  | 0  |   pkey->params.params_nr++;  | 
489  |  | 
  | 
490  | 0  |   if (_gnutls_x509_read_int(dsa_asn, "Y", &pkey->params.params[3]) < 0) { | 
491  | 0  |     gnutls_assert();  | 
492  | 0  |     goto error;  | 
493  | 0  |   }  | 
494  | 0  |   pkey->params.params_nr++;  | 
495  |  | 
  | 
496  | 0  |   if (_gnutls_x509_read_key_int(dsa_asn, "priv",  | 
497  | 0  |               &pkey->params.params[4]) < 0) { | 
498  | 0  |     gnutls_assert();  | 
499  | 0  |     goto error;  | 
500  | 0  |   }  | 
501  | 0  |   pkey->params.params_nr++;  | 
502  | 0  |   pkey->params.algo = GNUTLS_PK_DSA;  | 
503  |  | 
  | 
504  | 0  |   oid_size = sizeof(oid);  | 
505  | 0  |   result = asn1_read_value(dsa_asn, "seed.algorithm", oid, &oid_size);  | 
506  | 0  |   if (result == ASN1_SUCCESS) { | 
507  | 0  |     pkey->params.palgo = gnutls_oid_to_digest(oid);  | 
508  |  | 
  | 
509  | 0  |     result = _gnutls_x509_read_value(dsa_asn, "seed.seed", &seed);  | 
510  | 0  |     if (result == ASN1_SUCCESS) { | 
511  | 0  |       if (seed.size <= sizeof(pkey->params.seed)) { | 
512  | 0  |         memcpy(pkey->params.seed, seed.data, seed.size);  | 
513  | 0  |         pkey->params.seed_size = seed.size;  | 
514  | 0  |       }  | 
515  | 0  |       gnutls_free(seed.data);  | 
516  | 0  |     }  | 
517  | 0  |   }  | 
518  |  | 
  | 
519  | 0  |   return dsa_asn;  | 
520  |  |  | 
521  | 0  | error:  | 
522  | 0  |   asn1_delete_structure2(&dsa_asn, ASN1_DELETE_FLAG_ZEROIZE);  | 
523  | 0  |   gnutls_pk_params_clear(&pkey->params);  | 
524  | 0  |   gnutls_pk_params_release(&pkey->params);  | 
525  | 0  |   return NULL;  | 
526  | 0  | }  | 
527  |  |  | 
528  | 0  | #define PEM_KEY_DSA "DSA PRIVATE KEY"  | 
529  | 0  | #define PEM_KEY_RSA "RSA PRIVATE KEY"  | 
530  | 0  | #define PEM_KEY_ECC "EC PRIVATE KEY"  | 
531  | 0  | #define PEM_KEY_ML_DSA "ML-DSA PRIVATE KEY"  | 
532  | 0  | #define PEM_KEY_PKCS8 "PRIVATE KEY"  | 
533  |  |  | 
534  | 0  | #define MAX_PEM_HEADER_SIZE 25  | 
535  |  |  | 
536  |  | /**  | 
537  |  |  * gnutls_x509_privkey_import:  | 
538  |  |  * @key: The data to store the parsed key  | 
539  |  |  * @data: The DER or PEM encoded certificate.  | 
540  |  |  * @format: One of DER or PEM  | 
541  |  |  *  | 
542  |  |  * This function will convert the given DER or PEM encoded key to the  | 
543  |  |  * native #gnutls_x509_privkey_t format. The output will be stored in  | 
544  |  |  * @key .  | 
545  |  |  *  | 
546  |  |  * If the key is PEM encoded it should have a header that contains "PRIVATE  | 
547  |  |  * KEY". Note that this function falls back to PKCS #8 decoding without  | 
548  |  |  * password, if the default format fails to import.  | 
549  |  |  *  | 
550  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
551  |  |  *   negative error value.  | 
552  |  |  **/  | 
553  |  | int gnutls_x509_privkey_import(gnutls_x509_privkey_t key,  | 
554  |  |              const gnutls_datum_t *data,  | 
555  |  |              gnutls_x509_crt_fmt_t format)  | 
556  | 0  | { | 
557  | 0  |   int result = 0, need_free = 0;  | 
558  | 0  |   gnutls_datum_t _data;  | 
559  |  | 
  | 
560  | 0  |   if (key == NULL) { | 
561  | 0  |     gnutls_assert();  | 
562  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
563  | 0  |   }  | 
564  |  |  | 
565  | 0  |   _data.data = data->data;  | 
566  | 0  |   _data.size = data->size;  | 
567  |  | 
  | 
568  | 0  |   key->params.algo = GNUTLS_PK_UNKNOWN;  | 
569  |  |  | 
570  |  |   /* If the Certificate is in PEM format then decode it  | 
571  |  |    */  | 
572  | 0  |   if (format == GNUTLS_X509_FMT_PEM) { | 
573  | 0  |     unsigned left;  | 
574  | 0  |     char *ptr;  | 
575  | 0  |     uint8_t *begin_ptr;  | 
576  |  | 
  | 
577  | 0  |     ptr = memmem(data->data, data->size, "PRIVATE KEY-----",  | 
578  | 0  |            sizeof("PRIVATE KEY-----") - 1); | 
579  |  | 
  | 
580  | 0  |     result = GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE;  | 
581  |  | 
  | 
582  | 0  |     if (ptr != NULL) { | 
583  | 0  |       left = data->size -  | 
584  | 0  |              ((ptrdiff_t)ptr - (ptrdiff_t)data->data);  | 
585  |  | 
  | 
586  | 0  |       if (data->size - left > MAX_PEM_HEADER_SIZE) { | 
587  | 0  |         ptr -= MAX_PEM_HEADER_SIZE;  | 
588  | 0  |         left += MAX_PEM_HEADER_SIZE;  | 
589  | 0  |       } else { | 
590  | 0  |         ptr = (char *)data->data;  | 
591  | 0  |         left = data->size;  | 
592  | 0  |       }  | 
593  |  | 
  | 
594  | 0  |       ptr = memmem(ptr, left, "-----BEGIN ",  | 
595  | 0  |              sizeof("-----BEGIN ") - 1); | 
596  | 0  |       if (ptr != NULL) { | 
597  | 0  |         begin_ptr = (uint8_t *)ptr;  | 
598  | 0  |         left = data->size - ((ptrdiff_t)begin_ptr -  | 
599  | 0  |                  (ptrdiff_t)data->data);  | 
600  |  | 
  | 
601  | 0  |         ptr += sizeof("-----BEGIN ") - 1; | 
602  |  | 
  | 
603  | 0  |         if (left > sizeof(PEM_KEY_RSA) &&  | 
604  | 0  |             memcmp(ptr, PEM_KEY_RSA,  | 
605  | 0  |              sizeof(PEM_KEY_RSA) - 1) == 0) { | 
606  | 0  |           result = _gnutls_fbase64_decode(  | 
607  | 0  |             PEM_KEY_RSA, begin_ptr, left,  | 
608  | 0  |             &_data);  | 
609  | 0  |           if (result >= 0)  | 
610  | 0  |             key->params.algo =  | 
611  | 0  |               GNUTLS_PK_RSA;  | 
612  | 0  |         } else if (left > sizeof(PEM_KEY_ECC) &&  | 
613  | 0  |              memcmp(ptr, PEM_KEY_ECC,  | 
614  | 0  |               sizeof(PEM_KEY_ECC) - 1) ==  | 
615  | 0  |                0) { | 
616  | 0  |           result = _gnutls_fbase64_decode(  | 
617  | 0  |             PEM_KEY_ECC, begin_ptr, left,  | 
618  | 0  |             &_data);  | 
619  | 0  |           if (result >= 0)  | 
620  | 0  |             key->params.algo = GNUTLS_PK_EC;  | 
621  | 0  |         } else if (left > sizeof(PEM_KEY_DSA) &&  | 
622  | 0  |              memcmp(ptr, PEM_KEY_DSA,  | 
623  | 0  |               sizeof(PEM_KEY_DSA) - 1) ==  | 
624  | 0  |                0) { | 
625  | 0  |           result = _gnutls_fbase64_decode(  | 
626  | 0  |             PEM_KEY_DSA, begin_ptr, left,  | 
627  | 0  |             &_data);  | 
628  | 0  |           if (result >= 0)  | 
629  | 0  |             key->params.algo =  | 
630  | 0  |               GNUTLS_PK_DSA;  | 
631  | 0  |         } else if (left > sizeof(PEM_KEY_ML_DSA) &&  | 
632  | 0  |              memcmp(ptr, PEM_KEY_ML_DSA,  | 
633  | 0  |               sizeof(PEM_KEY_ML_DSA) - 1) ==  | 
634  | 0  |                0) { | 
635  | 0  |           result = _gnutls_fbase64_decode(  | 
636  | 0  |             PEM_KEY_ML_DSA, begin_ptr, left,  | 
637  | 0  |             &_data);  | 
638  | 0  |           if (result >= 0) { | 
639  | 0  |             key->params.algo =  | 
640  | 0  |               GNUTLS_PK_MLDSA44;  | 
641  | 0  |           }  | 
642  | 0  |         }  | 
643  |  | 
  | 
644  | 0  |         if (key->params.algo == GNUTLS_PK_UNKNOWN &&  | 
645  | 0  |             left >= sizeof(PEM_KEY_PKCS8)) { | 
646  | 0  |           if (memcmp(ptr, PEM_KEY_PKCS8,  | 
647  | 0  |                sizeof(PEM_KEY_PKCS8) - 1) ==  | 
648  | 0  |               0) { | 
649  | 0  |             result = _gnutls_fbase64_decode(  | 
650  | 0  |               PEM_KEY_PKCS8,  | 
651  | 0  |               begin_ptr, left,  | 
652  | 0  |               &_data);  | 
653  | 0  |             if (result >= 0) { | 
654  |  |               /* signal for PKCS #8 keys */  | 
655  | 0  |               key->params.algo = -1;  | 
656  | 0  |             }  | 
657  | 0  |           }  | 
658  | 0  |         }  | 
659  | 0  |       }  | 
660  | 0  |     }  | 
661  |  | 
  | 
662  | 0  |     if (result < 0) { | 
663  | 0  |       gnutls_assert();  | 
664  | 0  |       return result;  | 
665  | 0  |     }  | 
666  |  |  | 
667  | 0  |     need_free = 1;  | 
668  | 0  |   }  | 
669  |  |  | 
670  | 0  |   if (key->expanded) { | 
671  | 0  |     _gnutls_x509_privkey_reinit(key);  | 
672  | 0  |   }  | 
673  | 0  |   key->expanded = 1;  | 
674  |  | 
  | 
675  | 0  |   if (key->params.algo == (gnutls_pk_algorithm_t)-1) { | 
676  | 0  |     result = gnutls_x509_privkey_import_pkcs8(  | 
677  | 0  |       key, data, format, NULL, GNUTLS_PKCS_PLAIN);  | 
678  | 0  |     if (result < 0) { | 
679  | 0  |       gnutls_assert();  | 
680  | 0  |       key->key = NULL;  | 
681  | 0  |       goto cleanup;  | 
682  | 0  |     } else { | 
683  |  |       /* some keys under PKCS#8 don't set key->key */  | 
684  | 0  |       goto finish;  | 
685  | 0  |     }  | 
686  | 0  |   } else if (key->params.algo == GNUTLS_PK_RSA) { | 
687  | 0  |     key->key = _gnutls_privkey_decode_pkcs1_rsa_key(&_data, key);  | 
688  | 0  |     if (key->key == NULL)  | 
689  | 0  |       gnutls_assert();  | 
690  | 0  |   } else if (key->params.algo == GNUTLS_PK_DSA) { | 
691  | 0  |     key->key = decode_dsa_key(&_data, key);  | 
692  | 0  |     if (key->key == NULL)  | 
693  | 0  |       gnutls_assert();  | 
694  | 0  |   } else if (key->params.algo == GNUTLS_PK_EC) { | 
695  | 0  |     result = _gnutls_privkey_decode_ecc_key(&key->key, &_data, key,  | 
696  | 0  |               0);  | 
697  | 0  |     if (result < 0) { | 
698  | 0  |       gnutls_assert();  | 
699  | 0  |       key->key = NULL;  | 
700  | 0  |     }  | 
701  | 0  |   } else if (IS_ML_DSA(key->params.algo)) { | 
702  | 0  |     result = _gnutls_privkey_decode_ml_dsa_key(&key->key, &_data,  | 
703  | 0  |                  key);  | 
704  | 0  |     if (result < 0) { | 
705  | 0  |       gnutls_assert();  | 
706  | 0  |       key->key = NULL;  | 
707  | 0  |     }  | 
708  | 0  |   } else { | 
709  |  |     /* Try decoding each of the keys, and accept the one that  | 
710  |  |      * succeeds.  | 
711  |  |      */  | 
712  | 0  |     key->params.algo = GNUTLS_PK_RSA;  | 
713  | 0  |     key->key = _gnutls_privkey_decode_pkcs1_rsa_key(&_data, key);  | 
714  |  | 
  | 
715  | 0  |     if (key->key == NULL) { | 
716  | 0  |       key->params.algo = GNUTLS_PK_DSA;  | 
717  | 0  |       key->key = decode_dsa_key(&_data, key);  | 
718  | 0  |       if (key->key == NULL) { | 
719  | 0  |         key->params.algo = GNUTLS_PK_EC;  | 
720  | 0  |         result = _gnutls_privkey_decode_ecc_key(  | 
721  | 0  |           &key->key, &_data, key, 0);  | 
722  | 0  |         if (result < 0) { | 
723  | 0  |           result =  | 
724  | 0  |             gnutls_x509_privkey_import_pkcs8(  | 
725  | 0  |               key, data, format, NULL,  | 
726  | 0  |               GNUTLS_PKCS_PLAIN);  | 
727  | 0  |           if (result >= 0) { | 
728  |  |             /* there are keys (ed25519) which leave key->key NULL */  | 
729  | 0  |             goto finish;  | 
730  | 0  |           }  | 
731  |  |  | 
732  |  |           /* result < 0 */  | 
733  | 0  |           gnutls_assert();  | 
734  | 0  |           key->key = NULL;  | 
735  |  | 
  | 
736  | 0  |           if (result ==  | 
737  | 0  |               GNUTLS_E_PK_INVALID_PRIVKEY)  | 
738  | 0  |             goto cleanup;  | 
739  | 0  |         }  | 
740  | 0  |       }  | 
741  | 0  |     }  | 
742  | 0  |   }  | 
743  |  |  | 
744  | 0  |   if (key->key == NULL) { | 
745  | 0  |     gnutls_assert();  | 
746  | 0  |     result = GNUTLS_E_ASN1_DER_ERROR;  | 
747  | 0  |     goto cleanup;  | 
748  | 0  |   }  | 
749  |  |  | 
750  | 0  | finish:  | 
751  | 0  |   result =  | 
752  | 0  |     _gnutls_pk_fixup(key->params.algo, GNUTLS_IMPORT, &key->params);  | 
753  | 0  |   if (result < 0) { | 
754  | 0  |     gnutls_assert();  | 
755  | 0  |   }  | 
756  |  | 
  | 
757  | 0  | cleanup:  | 
758  | 0  |   if (need_free) { | 
759  | 0  |     zeroize_temp_key(_data.data, _data.size);  | 
760  | 0  |     _gnutls_free_datum(&_data);  | 
761  | 0  |   }  | 
762  |  |  | 
763  |  |   /* The key has now been decoded.  | 
764  |  |    */  | 
765  |  | 
  | 
766  | 0  |   return result;  | 
767  | 0  | }  | 
768  |  |  | 
769  |  | static int import_pkcs12_privkey(gnutls_x509_privkey_t key,  | 
770  |  |          const gnutls_datum_t *data,  | 
771  |  |          gnutls_x509_crt_fmt_t format,  | 
772  |  |          const char *password, unsigned int flags)  | 
773  | 0  | { | 
774  | 0  |   int ret;  | 
775  | 0  |   gnutls_pkcs12_t p12;  | 
776  | 0  |   gnutls_x509_privkey_t newkey;  | 
777  |  | 
  | 
778  | 0  |   ret = gnutls_pkcs12_init(&p12);  | 
779  | 0  |   if (ret < 0)  | 
780  | 0  |     return gnutls_assert_val(ret);  | 
781  |  |  | 
782  | 0  |   ret = gnutls_pkcs12_import(p12, data, format, flags);  | 
783  | 0  |   if (ret < 0) { | 
784  | 0  |     gnutls_assert();  | 
785  | 0  |     goto fail;  | 
786  | 0  |   }  | 
787  |  |  | 
788  | 0  |   ret = gnutls_pkcs12_simple_parse(p12, password, &newkey, NULL, NULL,  | 
789  | 0  |            NULL, NULL, NULL, 0);  | 
790  | 0  |   if (ret < 0) { | 
791  | 0  |     gnutls_assert();  | 
792  | 0  |     goto fail;  | 
793  | 0  |   }  | 
794  |  |  | 
795  | 0  |   ret = gnutls_x509_privkey_cpy(key, newkey);  | 
796  | 0  |   gnutls_x509_privkey_deinit(newkey);  | 
797  | 0  |   if (ret < 0) { | 
798  | 0  |     gnutls_assert();  | 
799  | 0  |     goto fail;  | 
800  | 0  |   }  | 
801  |  |  | 
802  | 0  |   ret = 0;  | 
803  | 0  | fail:  | 
804  |  | 
  | 
805  | 0  |   gnutls_pkcs12_deinit(p12);  | 
806  |  | 
  | 
807  | 0  |   return ret;  | 
808  | 0  | }  | 
809  |  |  | 
810  | 0  | #define MAX_ALGORITHM_NAME_SIZE_IN_PEM_HEADER 21  | 
811  |  |  | 
812  |  | /**  | 
813  |  |  * gnutls_x509_privkey_import2:  | 
814  |  |  * @key: The data to store the parsed key  | 
815  |  |  * @data: The DER or PEM encoded key.  | 
816  |  |  * @format: One of DER or PEM  | 
817  |  |  * @password: A password (optional)  | 
818  |  |  * @flags: an ORed sequence of gnutls_pkcs_encrypt_flags_t  | 
819  |  |  *  | 
820  |  |  * This function will import the given DER or PEM encoded key, to   | 
821  |  |  * the native #gnutls_x509_privkey_t format, irrespective of the  | 
822  |  |  * input format. The input format is auto-detected.  | 
823  |  |  *  | 
824  |  |  * The supported formats are basic unencrypted key, PKCS8, PKCS12,  | 
825  |  |  * and the openssl format.  | 
826  |  |  *  | 
827  |  |  * If the provided key is encrypted but no password was given, then  | 
828  |  |  * %GNUTLS_E_DECRYPTION_FAILED is returned. Since GnuTLS 3.4.0 this  | 
829  |  |  * function will utilize the PIN callbacks if any.  | 
830  |  |  *  | 
831  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
832  |  |  *   negative error value.  | 
833  |  |  **/  | 
834  |  | int gnutls_x509_privkey_import2(gnutls_x509_privkey_t key,  | 
835  |  |         const gnutls_datum_t *data,  | 
836  |  |         gnutls_x509_crt_fmt_t format,  | 
837  |  |         const char *password, unsigned int flags)  | 
838  | 0  | { | 
839  | 0  |   int ret = 0;  | 
840  | 0  |   int saved_ret = GNUTLS_E_PARSING_ERROR;  | 
841  | 0  |   char pin[GNUTLS_PKCS11_MAX_PIN_LEN];  | 
842  | 0  |   unsigned head_enc = 1;  | 
843  |  | 
  | 
844  | 0  |   if (format == GNUTLS_X509_FMT_PEM) { | 
845  | 0  |     size_t left;  | 
846  | 0  |     char *ptr;  | 
847  |  | 
  | 
848  | 0  |     ptr = memmem(data->data, data->size, "PRIVATE KEY-----",  | 
849  | 0  |            sizeof("PRIVATE KEY-----") - 1); | 
850  |  | 
  | 
851  | 0  |     if (ptr != NULL) { | 
852  | 0  |       left = data->size -  | 
853  | 0  |              ((ptrdiff_t)ptr - (ptrdiff_t)data->data);  | 
854  |  | 
  | 
855  | 0  |       if (data->size - left >  | 
856  | 0  |           MAX_ALGORITHM_NAME_SIZE_IN_PEM_HEADER) { | 
857  | 0  |         ptr -= MAX_ALGORITHM_NAME_SIZE_IN_PEM_HEADER;  | 
858  | 0  |         left += MAX_ALGORITHM_NAME_SIZE_IN_PEM_HEADER;  | 
859  | 0  |       } else { | 
860  | 0  |         ptr = (char *)data->data;  | 
861  | 0  |         left = data->size;  | 
862  | 0  |       }  | 
863  |  | 
  | 
864  | 0  |       ptr = memmem(ptr, left, "-----BEGIN ",  | 
865  | 0  |              sizeof("-----BEGIN ") - 1); | 
866  | 0  |       if (ptr != NULL) { | 
867  | 0  |         ptr += sizeof("-----BEGIN ") - 1; | 
868  | 0  |         left = data->size -  | 
869  | 0  |                ((ptrdiff_t)ptr - (ptrdiff_t)data->data);  | 
870  | 0  |       }  | 
871  |  | 
  | 
872  | 0  |       if (ptr != NULL) { | 
873  | 0  |         if ((left > sizeof(PEM_KEY_RSA) &&  | 
874  | 0  |              memcmp(ptr, PEM_KEY_RSA,  | 
875  | 0  |               sizeof(PEM_KEY_RSA) - 1) == 0) ||  | 
876  | 0  |             (left > sizeof(PEM_KEY_ECC) &&  | 
877  | 0  |              memcmp(ptr, PEM_KEY_ECC,  | 
878  | 0  |               sizeof(PEM_KEY_ECC) - 1) == 0) ||  | 
879  | 0  |             (left > sizeof(PEM_KEY_DSA) &&  | 
880  | 0  |              memcmp(ptr, PEM_KEY_DSA,  | 
881  | 0  |               sizeof(PEM_KEY_DSA) - 1) == 0) ||  | 
882  | 0  |             (left > sizeof(PEM_KEY_ML_DSA) &&  | 
883  | 0  |              memcmp(ptr, PEM_KEY_ML_DSA,  | 
884  | 0  |               sizeof(PEM_KEY_ML_DSA) - 1) == 0)) { | 
885  | 0  |           head_enc = 0;  | 
886  | 0  |         }  | 
887  | 0  |       }  | 
888  | 0  |     }  | 
889  | 0  |   }  | 
890  |  | 
  | 
891  | 0  |   if (head_enc == 0 ||  | 
892  | 0  |       (password == NULL && !(flags & GNUTLS_PKCS_NULL_PASSWORD))) { | 
893  | 0  |     ret = gnutls_x509_privkey_import(key, data, format);  | 
894  | 0  |     if (ret >= 0)  | 
895  | 0  |       return ret;  | 
896  |  |  | 
897  | 0  |     gnutls_assert();  | 
898  | 0  |     saved_ret = ret;  | 
899  |  |     /* fall through to PKCS #8 decoding */  | 
900  | 0  |   }  | 
901  |  |  | 
902  | 0  |   if ((password != NULL || (flags & GNUTLS_PKCS_NULL_PASSWORD)) ||  | 
903  | 0  |       ret < 0) { | 
904  | 0  |     ret = gnutls_x509_privkey_import_pkcs8(key, data, format,  | 
905  | 0  |                    password, flags);  | 
906  |  | 
  | 
907  | 0  |     if (ret == GNUTLS_E_DECRYPTION_FAILED && password == NULL &&  | 
908  | 0  |         (!(flags & GNUTLS_PKCS_PLAIN))) { | 
909  |  |       /* use the callback if any */  | 
910  | 0  |       ret = _gnutls_retrieve_pin(&key->pin, "key:", "", 0,  | 
911  | 0  |                pin, sizeof(pin));  | 
912  | 0  |       if (ret == 0) { | 
913  | 0  |         password = pin;  | 
914  | 0  |       }  | 
915  |  | 
  | 
916  | 0  |       ret = gnutls_x509_privkey_import_pkcs8(  | 
917  | 0  |         key, data, format, password, flags);  | 
918  | 0  |     }  | 
919  |  | 
  | 
920  | 0  |     if (saved_ret == GNUTLS_E_PARSING_ERROR)  | 
921  | 0  |       saved_ret = ret;  | 
922  |  | 
  | 
923  | 0  |     if (ret < 0) { | 
924  | 0  |       if (ret == GNUTLS_E_DECRYPTION_FAILED)  | 
925  | 0  |         goto cleanup;  | 
926  | 0  |       ret = import_pkcs12_privkey(key, data, format, password,  | 
927  | 0  |                 flags);  | 
928  | 0  |       if (ret < 0 && format == GNUTLS_X509_FMT_PEM) { | 
929  | 0  |         if (ret == GNUTLS_E_DECRYPTION_FAILED)  | 
930  | 0  |           goto cleanup;  | 
931  |  |  | 
932  | 0  |         ret = gnutls_x509_privkey_import_openssl(  | 
933  | 0  |           key, data, password);  | 
934  |  | 
  | 
935  | 0  |         if (ret == GNUTLS_E_DECRYPTION_FAILED &&  | 
936  | 0  |             password == NULL &&  | 
937  | 0  |             (key->pin.cb || _gnutls_pin_func)) { | 
938  |  |           /* use the callback if any */  | 
939  | 0  |           memset(pin, 0,  | 
940  | 0  |                  GNUTLS_PKCS11_MAX_PIN_LEN);  | 
941  | 0  |           ret = _gnutls_retrieve_pin(&key->pin,  | 
942  | 0  |                    "key:", "",  | 
943  | 0  |                    0, pin,  | 
944  | 0  |                    sizeof(pin));  | 
945  | 0  |           if (ret == 0) { | 
946  | 0  |             ret = gnutls_x509_privkey_import_openssl(  | 
947  | 0  |               key, data, pin);  | 
948  | 0  |           }  | 
949  | 0  |         }  | 
950  |  | 
  | 
951  | 0  |         if (ret < 0) { | 
952  | 0  |           gnutls_assert();  | 
953  | 0  |           goto cleanup;  | 
954  | 0  |         }  | 
955  | 0  |       } else { | 
956  | 0  |         gnutls_assert();  | 
957  | 0  |         goto cleanup;  | 
958  | 0  |       }  | 
959  | 0  |     }  | 
960  | 0  |   }  | 
961  |  |  | 
962  | 0  |   ret = 0;  | 
963  |  | 
  | 
964  | 0  | cleanup:  | 
965  | 0  |   if (ret == GNUTLS_E_PARSING_ERROR)  | 
966  | 0  |     ret = saved_ret;  | 
967  |  | 
  | 
968  | 0  |   return ret;  | 
969  | 0  | }  | 
970  |  |  | 
971  |  | /**  | 
972  |  |  * gnutls_x509_privkey_import_rsa_raw:  | 
973  |  |  * @key: The data to store the parsed key  | 
974  |  |  * @m: holds the modulus  | 
975  |  |  * @e: holds the public exponent  | 
976  |  |  * @d: holds the private exponent  | 
977  |  |  * @p: holds the first prime (p)  | 
978  |  |  * @q: holds the second prime (q)  | 
979  |  |  * @u: holds the coefficient  | 
980  |  |  *  | 
981  |  |  * This function will convert the given RSA raw parameters to the  | 
982  |  |  * native #gnutls_x509_privkey_t format.  The output will be stored in  | 
983  |  |  * @key.  | 
984  |  |  *  | 
985  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
986  |  |  *   negative error value.  | 
987  |  |  **/  | 
988  |  | int gnutls_x509_privkey_import_rsa_raw(gnutls_x509_privkey_t key,  | 
989  |  |                const gnutls_datum_t *m,  | 
990  |  |                const gnutls_datum_t *e,  | 
991  |  |                const gnutls_datum_t *d,  | 
992  |  |                const gnutls_datum_t *p,  | 
993  |  |                const gnutls_datum_t *q,  | 
994  |  |                const gnutls_datum_t *u)  | 
995  | 0  | { | 
996  | 0  |   return gnutls_x509_privkey_import_rsa_raw2(key, m, e, d, p, q, u, NULL,  | 
997  | 0  |                NULL);  | 
998  | 0  | }  | 
999  |  |  | 
1000  |  | /**  | 
1001  |  |  * gnutls_x509_privkey_import_rsa_raw2:  | 
1002  |  |  * @key: The data to store the parsed key  | 
1003  |  |  * @m: holds the modulus  | 
1004  |  |  * @e: holds the public exponent  | 
1005  |  |  * @d: holds the private exponent  | 
1006  |  |  * @p: holds the first prime (p)  | 
1007  |  |  * @q: holds the second prime (q)  | 
1008  |  |  * @u: holds the coefficient (optional)  | 
1009  |  |  * @e1: holds e1 = d mod (p-1) (optional)  | 
1010  |  |  * @e2: holds e2 = d mod (q-1) (optional)  | 
1011  |  |  *  | 
1012  |  |  * This function will convert the given RSA raw parameters to the  | 
1013  |  |  * native #gnutls_x509_privkey_t format.  The output will be stored in  | 
1014  |  |  * @key.  | 
1015  |  |  *  | 
1016  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1017  |  |  *   negative error value.  | 
1018  |  |  **/  | 
1019  |  | int gnutls_x509_privkey_import_rsa_raw2(  | 
1020  |  |   gnutls_x509_privkey_t key, const gnutls_datum_t *m,  | 
1021  |  |   const gnutls_datum_t *e, const gnutls_datum_t *d,  | 
1022  |  |   const gnutls_datum_t *p, const gnutls_datum_t *q,  | 
1023  |  |   const gnutls_datum_t *u, const gnutls_datum_t *e1,  | 
1024  |  |   const gnutls_datum_t *e2)  | 
1025  | 0  | { | 
1026  | 0  |   int ret;  | 
1027  |  | 
  | 
1028  | 0  |   if (key == NULL) { | 
1029  | 0  |     gnutls_assert();  | 
1030  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
1031  | 0  |   }  | 
1032  |  |  | 
1033  | 0  |   gnutls_pk_params_init(&key->params);  | 
1034  |  | 
  | 
1035  | 0  |   if (_gnutls_mpi_init_scan_nz(&key->params.params[RSA_MODULUS], m->data,  | 
1036  | 0  |              m->size)) { | 
1037  | 0  |     gnutls_assert();  | 
1038  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1039  | 0  |     goto cleanup;  | 
1040  | 0  |   }  | 
1041  | 0  |   key->params.params_nr++;  | 
1042  |  | 
  | 
1043  | 0  |   if (_gnutls_mpi_init_scan_nz(&key->params.params[RSA_PUB], e->data,  | 
1044  | 0  |              e->size)) { | 
1045  | 0  |     gnutls_assert();  | 
1046  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1047  | 0  |     goto cleanup;  | 
1048  | 0  |   }  | 
1049  | 0  |   key->params.params_nr++;  | 
1050  |  | 
  | 
1051  | 0  |   if (d) { | 
1052  | 0  |     if (_gnutls_mpi_init_scan_nz(&key->params.params[RSA_PRIV],  | 
1053  | 0  |                d->data, d->size)) { | 
1054  | 0  |       gnutls_assert();  | 
1055  | 0  |       ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1056  | 0  |       goto cleanup;  | 
1057  | 0  |     }  | 
1058  | 0  |     key->params.params_nr++;  | 
1059  | 0  |   }  | 
1060  |  |  | 
1061  | 0  |   if (_gnutls_mpi_init_scan_nz(&key->params.params[RSA_PRIME1], p->data,  | 
1062  | 0  |              p->size)) { | 
1063  | 0  |     gnutls_assert();  | 
1064  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1065  | 0  |     goto cleanup;  | 
1066  | 0  |   }  | 
1067  | 0  |   key->params.params_nr++;  | 
1068  |  | 
  | 
1069  | 0  |   if (_gnutls_mpi_init_scan_nz(&key->params.params[RSA_PRIME2], q->data,  | 
1070  | 0  |              q->size)) { | 
1071  | 0  |     gnutls_assert();  | 
1072  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1073  | 0  |     goto cleanup;  | 
1074  | 0  |   }  | 
1075  | 0  |   key->params.params_nr++;  | 
1076  |  | 
  | 
1077  | 0  |   if (u) { | 
1078  | 0  |     if (_gnutls_mpi_init_scan_nz(&key->params.params[RSA_COEF],  | 
1079  | 0  |                u->data, u->size)) { | 
1080  | 0  |       gnutls_assert();  | 
1081  | 0  |       ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1082  | 0  |       goto cleanup;  | 
1083  | 0  |     }  | 
1084  | 0  |     key->params.params_nr++;  | 
1085  | 0  |   }  | 
1086  |  |  | 
1087  | 0  |   if (e1 && e2) { | 
1088  | 0  |     if (_gnutls_mpi_init_scan_nz(&key->params.params[RSA_E1],  | 
1089  | 0  |                e1->data, e1->size)) { | 
1090  | 0  |       gnutls_assert();  | 
1091  | 0  |       ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1092  | 0  |       goto cleanup;  | 
1093  | 0  |     }  | 
1094  | 0  |     key->params.params_nr++;  | 
1095  |  | 
  | 
1096  | 0  |     if (_gnutls_mpi_init_scan_nz(&key->params.params[RSA_E2],  | 
1097  | 0  |                e2->data, e2->size)) { | 
1098  | 0  |       gnutls_assert();  | 
1099  | 0  |       ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1100  | 0  |       goto cleanup;  | 
1101  | 0  |     }  | 
1102  | 0  |     key->params.params_nr++;  | 
1103  | 0  |   }  | 
1104  |  |  | 
1105  | 0  |   key->params.algo = GNUTLS_PK_RSA;  | 
1106  |  | 
  | 
1107  | 0  |   ret = _gnutls_pk_fixup(GNUTLS_PK_RSA, GNUTLS_IMPORT, &key->params);  | 
1108  | 0  |   if (ret < 0) { | 
1109  | 0  |     gnutls_assert();  | 
1110  | 0  |     goto cleanup;  | 
1111  | 0  |   }  | 
1112  |  |  | 
1113  | 0  |   key->params.params_nr = RSA_PRIVATE_PARAMS;  | 
1114  | 0  |   key->params.algo = GNUTLS_PK_RSA;  | 
1115  |  | 
  | 
1116  | 0  |   ret = _gnutls_asn1_encode_privkey(&key->key, &key->params);  | 
1117  | 0  |   if (ret < 0) { | 
1118  | 0  |     gnutls_assert();  | 
1119  | 0  |     goto cleanup;  | 
1120  | 0  |   }  | 
1121  |  |  | 
1122  | 0  |   return 0;  | 
1123  |  |  | 
1124  | 0  | cleanup:  | 
1125  | 0  |   gnutls_pk_params_clear(&key->params);  | 
1126  | 0  |   gnutls_pk_params_release(&key->params);  | 
1127  | 0  |   return ret;  | 
1128  | 0  | }  | 
1129  |  |  | 
1130  |  | /**  | 
1131  |  |  * gnutls_x509_privkey_import_dsa_raw:  | 
1132  |  |  * @key: The data to store the parsed key  | 
1133  |  |  * @p: holds the p  | 
1134  |  |  * @q: holds the q  | 
1135  |  |  * @g: holds the g  | 
1136  |  |  * @y: holds the y (optional)  | 
1137  |  |  * @x: holds the x  | 
1138  |  |  *  | 
1139  |  |  * This function will convert the given DSA raw parameters to the  | 
1140  |  |  * native #gnutls_x509_privkey_t format.  The output will be stored  | 
1141  |  |  * in @key.  | 
1142  |  |  *  | 
1143  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1144  |  |  *   negative error value.  | 
1145  |  |  **/  | 
1146  |  | int gnutls_x509_privkey_import_dsa_raw(gnutls_x509_privkey_t key,  | 
1147  |  |                const gnutls_datum_t *p,  | 
1148  |  |                const gnutls_datum_t *q,  | 
1149  |  |                const gnutls_datum_t *g,  | 
1150  |  |                const gnutls_datum_t *y,  | 
1151  |  |                const gnutls_datum_t *x)  | 
1152  | 0  | { | 
1153  | 0  |   int ret;  | 
1154  |  | 
  | 
1155  | 0  |   if (unlikely(key == NULL || p == NULL || q == NULL || g == NULL ||  | 
1156  | 0  |          x == NULL)) { | 
1157  | 0  |     return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
1158  | 0  |   }  | 
1159  |  |  | 
1160  | 0  |   gnutls_pk_params_init(&key->params);  | 
1161  |  | 
  | 
1162  | 0  |   if (_gnutls_mpi_init_scan_nz(&key->params.params[DSA_P], p->data,  | 
1163  | 0  |              p->size)) { | 
1164  | 0  |     gnutls_assert();  | 
1165  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1166  | 0  |     goto cleanup;  | 
1167  | 0  |   }  | 
1168  |  |  | 
1169  | 0  |   if (_gnutls_mpi_init_scan_nz(&key->params.params[DSA_Q], q->data,  | 
1170  | 0  |              q->size)) { | 
1171  | 0  |     gnutls_assert();  | 
1172  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1173  | 0  |     goto cleanup;  | 
1174  | 0  |   }  | 
1175  |  |  | 
1176  | 0  |   if (_gnutls_mpi_init_scan_nz(&key->params.params[DSA_G], g->data,  | 
1177  | 0  |              g->size)) { | 
1178  | 0  |     gnutls_assert();  | 
1179  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1180  | 0  |     goto cleanup;  | 
1181  | 0  |   }  | 
1182  |  |  | 
1183  | 0  |   if (y) { | 
1184  | 0  |     if (_gnutls_mpi_init_scan_nz(&key->params.params[DSA_Y],  | 
1185  | 0  |                y->data, y->size)) { | 
1186  | 0  |       gnutls_assert();  | 
1187  | 0  |       ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1188  | 0  |       goto cleanup;  | 
1189  | 0  |     }  | 
1190  | 0  |   }  | 
1191  |  |  | 
1192  | 0  |   if (_gnutls_mpi_init_scan_nz(&key->params.params[DSA_X], x->data,  | 
1193  | 0  |              x->size)) { | 
1194  | 0  |     gnutls_assert();  | 
1195  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1196  | 0  |     goto cleanup;  | 
1197  | 0  |   }  | 
1198  |  |  | 
1199  | 0  |   ret = _gnutls_pk_fixup(GNUTLS_PK_DSA, GNUTLS_IMPORT, &key->params);  | 
1200  | 0  |   if (ret < 0) { | 
1201  | 0  |     gnutls_assert();  | 
1202  | 0  |     goto cleanup;  | 
1203  | 0  |   }  | 
1204  |  |  | 
1205  | 0  |   key->params.algo = GNUTLS_PK_DSA;  | 
1206  | 0  |   key->params.params_nr = DSA_PRIVATE_PARAMS;  | 
1207  |  | 
  | 
1208  | 0  |   ret = _gnutls_asn1_encode_privkey(&key->key, &key->params);  | 
1209  | 0  |   if (ret < 0) { | 
1210  | 0  |     gnutls_assert();  | 
1211  | 0  |     goto cleanup;  | 
1212  | 0  |   }  | 
1213  |  |  | 
1214  | 0  |   return 0;  | 
1215  |  |  | 
1216  | 0  | cleanup:  | 
1217  | 0  |   gnutls_pk_params_clear(&key->params);  | 
1218  | 0  |   gnutls_pk_params_release(&key->params);  | 
1219  | 0  |   return ret;  | 
1220  | 0  | }  | 
1221  |  |  | 
1222  |  | /**  | 
1223  |  |  * gnutls_x509_privkey_import_dh_raw:  | 
1224  |  |  * @key: The data to store the parsed key  | 
1225  |  |  * @params: holds the %gnutls_dh_params_t  | 
1226  |  |  * @y: holds the y (optional)  | 
1227  |  |  * @x: holds the x  | 
1228  |  |  *  | 
1229  |  |  * This function will convert the given Diffie-Hellman raw parameters  | 
1230  |  |  * to the native #gnutls_x509_privkey_t format.  The output will be  | 
1231  |  |  * stored in @key.  | 
1232  |  |  *  | 
1233  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1234  |  |  *   negative error value.  | 
1235  |  |  **/  | 
1236  |  | int gnutls_x509_privkey_import_dh_raw(gnutls_x509_privkey_t key,  | 
1237  |  |               const gnutls_dh_params_t params,  | 
1238  |  |               const gnutls_datum_t *y,  | 
1239  |  |               const gnutls_datum_t *x)  | 
1240  | 0  | { | 
1241  | 0  |   int ret;  | 
1242  |  | 
  | 
1243  | 0  |   if (unlikely(key == NULL || params == NULL || x == NULL)) { | 
1244  | 0  |     return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
1245  | 0  |   }  | 
1246  |  |  | 
1247  | 0  |   gnutls_pk_params_init(&key->params);  | 
1248  |  | 
  | 
1249  | 0  |   key->params.params[DH_P] = _gnutls_mpi_copy(params->params[0]);  | 
1250  | 0  |   key->params.params[DH_G] = _gnutls_mpi_copy(params->params[1]);  | 
1251  | 0  |   if (params->params[2]) { | 
1252  | 0  |     key->params.params[DH_Q] = _gnutls_mpi_copy(params->params[2]);  | 
1253  | 0  |   }  | 
1254  | 0  |   key->params.qbits = params->q_bits;  | 
1255  |  | 
  | 
1256  | 0  |   if (y) { | 
1257  | 0  |     if (_gnutls_mpi_init_scan_nz(&key->params.params[DH_Y], y->data,  | 
1258  | 0  |                y->size)) { | 
1259  | 0  |       gnutls_assert();  | 
1260  | 0  |       ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1261  | 0  |       goto cleanup;  | 
1262  | 0  |     }  | 
1263  | 0  |   }  | 
1264  |  |  | 
1265  | 0  |   if (_gnutls_mpi_init_scan_nz(&key->params.params[DH_X], x->data,  | 
1266  | 0  |              x->size)) { | 
1267  | 0  |     gnutls_assert();  | 
1268  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1269  | 0  |     goto cleanup;  | 
1270  | 0  |   }  | 
1271  |  |  | 
1272  | 0  |   ret = _gnutls_pk_fixup(GNUTLS_PK_DH, GNUTLS_IMPORT, &key->params);  | 
1273  | 0  |   if (ret < 0) { | 
1274  | 0  |     gnutls_assert();  | 
1275  | 0  |     goto cleanup;  | 
1276  | 0  |   }  | 
1277  |  |  | 
1278  | 0  |   key->params.algo = GNUTLS_PK_DH;  | 
1279  | 0  |   key->params.params_nr = DH_PRIVATE_PARAMS;  | 
1280  |  | 
  | 
1281  | 0  |   return 0;  | 
1282  |  |  | 
1283  | 0  | cleanup:  | 
1284  | 0  |   gnutls_pk_params_clear(&key->params);  | 
1285  | 0  |   gnutls_pk_params_release(&key->params);  | 
1286  | 0  |   return ret;  | 
1287  | 0  | }  | 
1288  |  |  | 
1289  |  | /**  | 
1290  |  |  * gnutls_x509_privkey_import_ecc_raw:  | 
1291  |  |  * @key: The data to store the parsed key  | 
1292  |  |  * @curve: holds the curve  | 
1293  |  |  * @x: holds the x-coordinate  | 
1294  |  |  * @y: holds the y-coordinate  | 
1295  |  |  * @k: holds the k  | 
1296  |  |  *  | 
1297  |  |  * This function will convert the given elliptic curve parameters to the  | 
1298  |  |  * native #gnutls_x509_privkey_t format.  The output will be stored  | 
1299  |  |  * in @key. For EdDSA keys, the @x and @k values must be in the  | 
1300  |  |  * native to curve format.  | 
1301  |  |  *  | 
1302  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1303  |  |  *   negative error value.  | 
1304  |  |  *  | 
1305  |  |  * Since: 3.0  | 
1306  |  |  **/  | 
1307  |  | int gnutls_x509_privkey_import_ecc_raw(gnutls_x509_privkey_t key,  | 
1308  |  |                gnutls_ecc_curve_t curve,  | 
1309  |  |                const gnutls_datum_t *x,  | 
1310  |  |                const gnutls_datum_t *y,  | 
1311  |  |                const gnutls_datum_t *k)  | 
1312  | 0  | { | 
1313  | 0  |   int ret;  | 
1314  |  | 
  | 
1315  | 0  |   if (key == NULL) { | 
1316  | 0  |     gnutls_assert();  | 
1317  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
1318  | 0  |   }  | 
1319  |  |  | 
1320  | 0  |   gnutls_pk_params_init(&key->params);  | 
1321  |  | 
  | 
1322  | 0  |   key->params.curve = curve;  | 
1323  |  | 
  | 
1324  | 0  |   if (curve_is_eddsa(curve) || curve_is_modern_ecdh(curve)) { | 
1325  | 0  |     unsigned size;  | 
1326  | 0  |     switch (curve) { | 
1327  | 0  |     case GNUTLS_ECC_CURVE_ED25519:  | 
1328  | 0  |       key->params.algo = GNUTLS_PK_EDDSA_ED25519;  | 
1329  | 0  |       break;  | 
1330  | 0  |     case GNUTLS_ECC_CURVE_ED448:  | 
1331  | 0  |       key->params.algo = GNUTLS_PK_EDDSA_ED448;  | 
1332  | 0  |       break;  | 
1333  | 0  |     case GNUTLS_ECC_CURVE_X25519:  | 
1334  | 0  |       key->params.algo = GNUTLS_PK_ECDH_X25519;  | 
1335  | 0  |       break;  | 
1336  | 0  |     case GNUTLS_ECC_CURVE_X448:  | 
1337  | 0  |       key->params.algo = GNUTLS_PK_ECDH_X448;  | 
1338  | 0  |       break;  | 
1339  | 0  |     default:  | 
1340  | 0  |       ret = gnutls_assert_val(GNUTLS_E_INTERNAL_ERROR);  | 
1341  | 0  |       goto cleanup;  | 
1342  | 0  |     }  | 
1343  |  |  | 
1344  | 0  |     if (curve_is_eddsa(curve)) { | 
1345  | 0  |       size = gnutls_ecc_curve_get_size(curve);  | 
1346  | 0  |       if (x->size != size || k->size != size) { | 
1347  | 0  |         ret = gnutls_assert_val(  | 
1348  | 0  |           GNUTLS_E_INVALID_REQUEST);  | 
1349  | 0  |         goto cleanup;  | 
1350  | 0  |       }  | 
1351  |  |  | 
1352  | 0  |       ret = _gnutls_set_datum(&key->params.raw_pub, x->data,  | 
1353  | 0  |             x->size);  | 
1354  | 0  |       if (ret < 0) { | 
1355  | 0  |         gnutls_assert();  | 
1356  | 0  |         goto cleanup;  | 
1357  | 0  |       }  | 
1358  | 0  |     }  | 
1359  |  |  | 
1360  | 0  |     ret = _gnutls_set_datum(&key->params.raw_priv, k->data,  | 
1361  | 0  |           k->size);  | 
1362  | 0  |     if (ret < 0) { | 
1363  | 0  |       gnutls_assert();  | 
1364  | 0  |       goto cleanup;  | 
1365  | 0  |     }  | 
1366  |  |  | 
1367  | 0  |     return 0;  | 
1368  | 0  |   }  | 
1369  |  |  | 
1370  | 0  |   if (_gnutls_mpi_init_scan_nz(&key->params.params[ECC_X], x->data,  | 
1371  | 0  |              x->size)) { | 
1372  | 0  |     gnutls_assert();  | 
1373  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1374  | 0  |     goto cleanup;  | 
1375  | 0  |   }  | 
1376  | 0  |   key->params.params_nr++;  | 
1377  |  | 
  | 
1378  | 0  |   if (_gnutls_mpi_init_scan_nz(&key->params.params[ECC_Y], y->data,  | 
1379  | 0  |              y->size)) { | 
1380  | 0  |     gnutls_assert();  | 
1381  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1382  | 0  |     goto cleanup;  | 
1383  | 0  |   }  | 
1384  | 0  |   key->params.params_nr++;  | 
1385  |  | 
  | 
1386  | 0  |   if (_gnutls_mpi_init_scan_nz(&key->params.params[ECC_K], k->data,  | 
1387  | 0  |              k->size)) { | 
1388  | 0  |     gnutls_assert();  | 
1389  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1390  | 0  |     goto cleanup;  | 
1391  | 0  |   }  | 
1392  | 0  |   key->params.params_nr++;  | 
1393  |  | 
  | 
1394  | 0  |   key->params.algo = GNUTLS_PK_EC;  | 
1395  |  | 
  | 
1396  | 0  |   ret = _gnutls_pk_fixup(GNUTLS_PK_EC, GNUTLS_IMPORT, &key->params);  | 
1397  | 0  |   if (ret < 0) { | 
1398  | 0  |     gnutls_assert();  | 
1399  | 0  |     goto cleanup;  | 
1400  | 0  |   }  | 
1401  |  |  | 
1402  | 0  |   ret = _gnutls_asn1_encode_privkey(&key->key, &key->params);  | 
1403  | 0  |   if (ret < 0) { | 
1404  | 0  |     gnutls_assert();  | 
1405  | 0  |     goto cleanup;  | 
1406  | 0  |   }  | 
1407  |  |  | 
1408  | 0  |   return 0;  | 
1409  |  |  | 
1410  | 0  | cleanup:  | 
1411  | 0  |   gnutls_pk_params_clear(&key->params);  | 
1412  | 0  |   gnutls_pk_params_release(&key->params);  | 
1413  | 0  |   return ret;  | 
1414  | 0  | }  | 
1415  |  |  | 
1416  |  | /**  | 
1417  |  |  * gnutls_x509_privkey_import_gost_raw:  | 
1418  |  |  * @key: The data to store the parsed key  | 
1419  |  |  * @curve: holds the curve  | 
1420  |  |  * @digest: will hold the digest  | 
1421  |  |  * @paramset: will hold the GOST parameter set ID  | 
1422  |  |  * @x: holds the x-coordinate  | 
1423  |  |  * @y: holds the y-coordinate  | 
1424  |  |  * @k: holds the k (private key)  | 
1425  |  |  *  | 
1426  |  |  * This function will convert the given GOST private key's parameters to the  | 
1427  |  |  * native #gnutls_x509_privkey_t format.  The output will be stored  | 
1428  |  |  * in @key.  @digest should be one of GNUTLS_DIG_GOSR_94,  | 
1429  |  |  * GNUTLS_DIG_STREEBOG_256 or GNUTLS_DIG_STREEBOG_512.  If @paramset is set to  | 
1430  |  |  * GNUTLS_GOST_PARAMSET_UNKNOWN default one will be selected depending on  | 
1431  |  |  * @digest.  | 
1432  |  |  *  | 
1433  |  |  * Note: parameters should be stored with least significant byte first. On  | 
1434  |  |  * version 3.6.3 big-endian format was used incorrectly.  | 
1435  |  |  *  | 
1436  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1437  |  |  *   negative error value.  | 
1438  |  |  *  | 
1439  |  |  * Since: 3.6.3  | 
1440  |  |  **/  | 
1441  |  | int gnutls_x509_privkey_import_gost_raw(gnutls_x509_privkey_t key,  | 
1442  |  |           gnutls_ecc_curve_t curve,  | 
1443  |  |           gnutls_digest_algorithm_t digest,  | 
1444  |  |           gnutls_gost_paramset_t paramset,  | 
1445  |  |           const gnutls_datum_t *x,  | 
1446  |  |           const gnutls_datum_t *y,  | 
1447  |  |           const gnutls_datum_t *k)  | 
1448  | 0  | { | 
1449  | 0  |   int ret;  | 
1450  |  | 
  | 
1451  | 0  |   if (key == NULL) { | 
1452  | 0  |     gnutls_assert();  | 
1453  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
1454  | 0  |   }  | 
1455  |  |  | 
1456  | 0  |   key->params.curve = curve;  | 
1457  | 0  |   key->params.algo = _gnutls_digest_gost(digest);  | 
1458  |  | 
  | 
1459  | 0  |   if (paramset == GNUTLS_GOST_PARAMSET_UNKNOWN)  | 
1460  | 0  |     paramset = _gnutls_gost_paramset_default(key->params.algo);  | 
1461  |  | 
  | 
1462  | 0  |   key->params.gost_params = paramset;  | 
1463  |  | 
  | 
1464  | 0  |   if (_gnutls_mpi_init_scan_le(&key->params.params[GOST_X], x->data,  | 
1465  | 0  |              x->size)) { | 
1466  | 0  |     gnutls_assert();  | 
1467  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1468  | 0  |     goto cleanup;  | 
1469  | 0  |   }  | 
1470  | 0  |   key->params.params_nr++;  | 
1471  |  | 
  | 
1472  | 0  |   if (_gnutls_mpi_init_scan_le(&key->params.params[GOST_Y], y->data,  | 
1473  | 0  |              y->size)) { | 
1474  | 0  |     gnutls_assert();  | 
1475  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1476  | 0  |     goto cleanup;  | 
1477  | 0  |   }  | 
1478  | 0  |   key->params.params_nr++;  | 
1479  |  | 
  | 
1480  | 0  |   if (_gnutls_mpi_init_scan_le(&key->params.params[GOST_K], k->data,  | 
1481  | 0  |              k->size)) { | 
1482  | 0  |     gnutls_assert();  | 
1483  | 0  |     ret = GNUTLS_E_MPI_SCAN_FAILED;  | 
1484  | 0  |     goto cleanup;  | 
1485  | 0  |   }  | 
1486  | 0  |   key->params.params_nr++;  | 
1487  |  | 
  | 
1488  | 0  |   ret = _gnutls_pk_fixup(key->params.algo, GNUTLS_IMPORT, &key->params);  | 
1489  | 0  |   if (ret < 0) { | 
1490  | 0  |     gnutls_assert();  | 
1491  | 0  |     goto cleanup;  | 
1492  | 0  |   }  | 
1493  |  |  | 
1494  | 0  |   return 0;  | 
1495  |  |  | 
1496  | 0  | cleanup:  | 
1497  | 0  |   gnutls_pk_params_clear(&key->params);  | 
1498  | 0  |   gnutls_pk_params_release(&key->params);  | 
1499  | 0  |   return ret;  | 
1500  | 0  | }  | 
1501  |  |  | 
1502  |  | /**  | 
1503  |  |  * gnutls_x509_privkey_get_pk_algorithm:  | 
1504  |  |  * @key: should contain a #gnutls_x509_privkey_t type  | 
1505  |  |  *  | 
1506  |  |  * This function will return the public key algorithm of a private  | 
1507  |  |  * key.  | 
1508  |  |  *  | 
1509  |  |  * Returns: a member of the #gnutls_pk_algorithm_t enumeration on  | 
1510  |  |  *   success, or a negative error code on error.  | 
1511  |  |  **/  | 
1512  |  | int gnutls_x509_privkey_get_pk_algorithm(gnutls_x509_privkey_t key)  | 
1513  | 0  | { | 
1514  | 0  |   if (key == NULL) { | 
1515  | 0  |     gnutls_assert();  | 
1516  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
1517  | 0  |   }  | 
1518  |  |  | 
1519  | 0  |   return key->params.algo;  | 
1520  | 0  | }  | 
1521  |  |  | 
1522  |  | /**  | 
1523  |  |  * gnutls_x509_privkey_get_pk_algorithm2:  | 
1524  |  |  * @key: should contain a #gnutls_x509_privkey_t type  | 
1525  |  |  * @bits: The number of bits in the public key algorithm  | 
1526  |  |  *  | 
1527  |  |  * This function will return the public key algorithm of a private  | 
1528  |  |  * key.  | 
1529  |  |  *  | 
1530  |  |  * Returns: a member of the #gnutls_pk_algorithm_t enumeration on  | 
1531  |  |  *   success, or a negative error code on error.  | 
1532  |  |  **/  | 
1533  |  | int gnutls_x509_privkey_get_pk_algorithm2(gnutls_x509_privkey_t key,  | 
1534  |  |             unsigned int *bits)  | 
1535  | 0  | { | 
1536  | 0  |   int ret;  | 
1537  |  | 
  | 
1538  | 0  |   if (key == NULL) { | 
1539  | 0  |     gnutls_assert();  | 
1540  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
1541  | 0  |   }  | 
1542  |  |  | 
1543  | 0  |   if (bits) { | 
1544  | 0  |     ret = pubkey_to_bits(&key->params);  | 
1545  | 0  |     if (ret < 0)  | 
1546  | 0  |       ret = 0;  | 
1547  | 0  |     *bits = ret;  | 
1548  | 0  |   }  | 
1549  |  | 
  | 
1550  | 0  |   return key->params.algo;  | 
1551  | 0  | }  | 
1552  |  |  | 
1553  |  | int _gnutls_x509_privkey_get_spki_params(gnutls_x509_privkey_t key,  | 
1554  |  |            gnutls_x509_spki_st *params)  | 
1555  | 0  | { | 
1556  | 0  |   return _gnutls_x509_spki_copy(params, &key->params.spki);  | 
1557  | 0  | }  | 
1558  |  |  | 
1559  |  | /**  | 
1560  |  |  * gnutls_x509_privkey_get_spki:  | 
1561  |  |  * @key: should contain a #gnutls_x509_privkey_t type  | 
1562  |  |  * @spki: a SubjectPublicKeyInfo structure of type #gnutls_x509_spki_t  | 
1563  |  |  * @flags: must be zero  | 
1564  |  |  *  | 
1565  |  |  * This function will return the public key information of a private  | 
1566  |  |  * key. The provided @spki must be initialized.  | 
1567  |  |  *  | 
1568  |  |  * Returns: Zero on success, or a negative error code on error.  | 
1569  |  |  **/  | 
1570  |  | int gnutls_x509_privkey_get_spki(gnutls_x509_privkey_t key,  | 
1571  |  |          gnutls_x509_spki_t spki, unsigned int flags)  | 
1572  | 0  | { | 
1573  | 0  |   if (key == NULL) { | 
1574  | 0  |     gnutls_assert();  | 
1575  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
1576  | 0  |   }  | 
1577  |  |  | 
1578  | 0  |   if (key->params.spki.pk == GNUTLS_PK_UNKNOWN)  | 
1579  | 0  |     return gnutls_assert_val(GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);  | 
1580  |  |  | 
1581  | 0  |   return _gnutls_x509_privkey_get_spki_params(key, spki);  | 
1582  | 0  | }  | 
1583  |  |  | 
1584  |  | /**  | 
1585  |  |  * gnutls_x509_privkey_set_spki:  | 
1586  |  |  * @key: should contain a #gnutls_x509_privkey_t type  | 
1587  |  |  * @spki: a SubjectPublicKeyInfo structure of type #gnutls_x509_spki_t  | 
1588  |  |  * @flags: must be zero  | 
1589  |  |  *  | 
1590  |  |  * This function will return the public key information of a private  | 
1591  |  |  * key. The provided @spki must be initialized.  | 
1592  |  |  *  | 
1593  |  |  * Returns: Zero on success, or a negative error code on error.  | 
1594  |  |  **/  | 
1595  |  | int gnutls_x509_privkey_set_spki(gnutls_x509_privkey_t key,  | 
1596  |  |          const gnutls_x509_spki_t spki,  | 
1597  |  |          unsigned int flags)  | 
1598  | 0  | { | 
1599  | 0  |   gnutls_pk_params_st tparams;  | 
1600  | 0  |   int ret;  | 
1601  |  | 
  | 
1602  | 0  |   if (key == NULL) { | 
1603  | 0  |     gnutls_assert();  | 
1604  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
1605  | 0  |   }  | 
1606  |  |  | 
1607  | 0  |   if (!_gnutls_pk_are_compat(key->params.algo, spki->pk))  | 
1608  | 0  |     return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
1609  |  |  | 
1610  | 0  |   memcpy(&tparams, &key->params, sizeof(gnutls_pk_params_st));  | 
1611  |  |   /* No need for a deep copy, as this is only for one time check */  | 
1612  | 0  |   memcpy(&tparams.spki, spki, sizeof(gnutls_x509_spki_st));  | 
1613  | 0  |   ret = _gnutls_x509_check_pubkey_params(&tparams);  | 
1614  | 0  |   if (ret < 0)  | 
1615  | 0  |     return gnutls_assert_val(ret);  | 
1616  |  |  | 
1617  | 0  |   ret = _gnutls_x509_spki_copy(&key->params.spki, spki);  | 
1618  | 0  |   if (ret < 0)  | 
1619  | 0  |     return gnutls_assert_val(ret);  | 
1620  |  |  | 
1621  | 0  |   key->params.algo = spki->pk;  | 
1622  |  | 
  | 
1623  | 0  |   return 0;  | 
1624  | 0  | }  | 
1625  |  |  | 
1626  |  | static const char *set_msg(gnutls_x509_privkey_t key)  | 
1627  | 0  | { | 
1628  | 0  |   switch (key->params.algo) { | 
1629  | 0  |   case GNUTLS_PK_RSA:  | 
1630  | 0  |   case GNUTLS_PK_RSA_PSS:  | 
1631  | 0  |     return PEM_KEY_RSA;  | 
1632  | 0  |   case GNUTLS_PK_DSA:  | 
1633  | 0  |     return PEM_KEY_DSA;  | 
1634  | 0  |   case GNUTLS_PK_EC:  | 
1635  | 0  |     return PEM_KEY_ECC;  | 
1636  | 0  |   case GNUTLS_PK_MLDSA44:  | 
1637  | 0  |   case GNUTLS_PK_MLDSA65:  | 
1638  | 0  |   case GNUTLS_PK_MLDSA87:  | 
1639  | 0  |     return PEM_KEY_ML_DSA;  | 
1640  | 0  |   default:  | 
1641  | 0  |     return "UNKNOWN";  | 
1642  | 0  |   }  | 
1643  | 0  | }  | 
1644  |  |  | 
1645  |  | /**  | 
1646  |  |  * gnutls_x509_privkey_export:  | 
1647  |  |  * @key: Holds the key  | 
1648  |  |  * @format: the format of output params. One of PEM or DER.  | 
1649  |  |  * @output_data: will contain a private key PEM or DER encoded  | 
1650  |  |  * @output_data_size: holds the size of output_data (and will be  | 
1651  |  |  *   replaced by the actual size of parameters)  | 
1652  |  |  *  | 
1653  |  |  * This function will export the private key to a PKCS#1 structure for  | 
1654  |  |  * RSA or RSA-PSS keys, and integer sequence for DSA keys. Other keys types  | 
1655  |  |  * will be exported in PKCS#8 form.  | 
1656  |  |  *  | 
1657  |  |  * If the structure is PEM encoded, it will have a header  | 
1658  |  |  * of "BEGIN RSA PRIVATE KEY".  | 
1659  |  |  *  | 
1660  |  |  * It is recommended to use gnutls_x509_privkey_export_pkcs8() instead  | 
1661  |  |  * of this function, when a consistent output format is required.  | 
1662  |  |  *  | 
1663  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1664  |  |  *   negative error value.  | 
1665  |  |  **/  | 
1666  |  | int gnutls_x509_privkey_export(gnutls_x509_privkey_t key,  | 
1667  |  |              gnutls_x509_crt_fmt_t format, void *output_data,  | 
1668  |  |              size_t *output_data_size)  | 
1669  | 0  | { | 
1670  | 0  |   gnutls_datum_t out;  | 
1671  | 0  |   int ret;  | 
1672  |  | 
  | 
1673  | 0  |   ret = gnutls_x509_privkey_export2(key, format, &out);  | 
1674  | 0  |   if (ret < 0)  | 
1675  | 0  |     return gnutls_assert_val(ret);  | 
1676  |  |  | 
1677  | 0  |   if (format == GNUTLS_X509_FMT_PEM)  | 
1678  | 0  |     ret = _gnutls_copy_string(&out, output_data, output_data_size);  | 
1679  | 0  |   else  | 
1680  | 0  |     ret = _gnutls_copy_data(&out, output_data, output_data_size);  | 
1681  | 0  |   gnutls_free(out.data);  | 
1682  |  | 
  | 
1683  | 0  |   return ret;  | 
1684  | 0  | }  | 
1685  |  |  | 
1686  |  | /**  | 
1687  |  |  * gnutls_x509_privkey_export2:  | 
1688  |  |  * @key: Holds the key  | 
1689  |  |  * @format: the format of output params. One of PEM or DER.  | 
1690  |  |  * @out: will contain a private key PEM or DER encoded  | 
1691  |  |  *  | 
1692  |  |  * This function will export the private key to a PKCS#1 structure for  | 
1693  |  |  * RSA or RSA-PSS keys, and integer sequence for DSA keys. Other keys types  | 
1694  |  |  * will be exported in PKCS#8 form.  | 
1695  |  |  *  | 
1696  |  |  * The output buffer is allocated using gnutls_malloc().  | 
1697  |  |  *  | 
1698  |  |  * It is recommended to use gnutls_x509_privkey_export2_pkcs8() instead  | 
1699  |  |  * of this function, when a consistent output format is required.  | 
1700  |  |  *  | 
1701  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1702  |  |  *   negative error value.  | 
1703  |  |  *  | 
1704  |  |  * Since 3.1.3  | 
1705  |  |  **/  | 
1706  |  | int gnutls_x509_privkey_export2(gnutls_x509_privkey_t key,  | 
1707  |  |         gnutls_x509_crt_fmt_t format,  | 
1708  |  |         gnutls_datum_t *out)  | 
1709  | 0  | { | 
1710  | 0  |   const char *msg;  | 
1711  | 0  |   int ret;  | 
1712  |  | 
  | 
1713  | 0  |   if (key == NULL) { | 
1714  | 0  |     gnutls_assert();  | 
1715  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
1716  | 0  |   }  | 
1717  |  |  | 
1718  | 0  |   if (key->key == NULL) { /* can only export in PKCS#8 form */ | 
1719  | 0  |     return gnutls_x509_privkey_export2_pkcs8(key, format, NULL, 0,  | 
1720  | 0  |                out);  | 
1721  | 0  |   }  | 
1722  |  |  | 
1723  | 0  |   msg = set_msg(key);  | 
1724  |  | 
  | 
1725  | 0  |   if (key->flags & GNUTLS_PRIVKEY_FLAG_EXPORT_COMPAT) { | 
1726  | 0  |     ret = gnutls_x509_privkey_fix(key);  | 
1727  | 0  |     if (ret < 0)  | 
1728  | 0  |       return gnutls_assert_val(ret);  | 
1729  | 0  |   }  | 
1730  |  |  | 
1731  | 0  |   return _gnutls_x509_export_int2(key->key, format, msg, out);  | 
1732  | 0  | }  | 
1733  |  |  | 
1734  |  | /**  | 
1735  |  |  * gnutls_x509_privkey_sec_param:  | 
1736  |  |  * @key: a key  | 
1737  |  |  *  | 
1738  |  |  * This function will return the security parameter appropriate with  | 
1739  |  |  * this private key.  | 
1740  |  |  *  | 
1741  |  |  * Returns: On success, a valid security parameter is returned otherwise  | 
1742  |  |  * %GNUTLS_SEC_PARAM_UNKNOWN is returned.  | 
1743  |  |  *  | 
1744  |  |  * Since: 2.12.0  | 
1745  |  |  **/  | 
1746  |  | gnutls_sec_param_t gnutls_x509_privkey_sec_param(gnutls_x509_privkey_t key)  | 
1747  | 0  | { | 
1748  | 0  |   int bits;  | 
1749  |  | 
  | 
1750  | 0  |   bits = pubkey_to_bits(&key->params);  | 
1751  | 0  |   if (bits <= 0)  | 
1752  | 0  |     return GNUTLS_SEC_PARAM_UNKNOWN;  | 
1753  |  |  | 
1754  | 0  |   return gnutls_pk_bits_to_sec_param(key->params.algo, bits);  | 
1755  | 0  | }  | 
1756  |  |  | 
1757  |  | /**  | 
1758  |  |  * gnutls_x509_privkey_export_ecc_raw:  | 
1759  |  |  * @key: a key  | 
1760  |  |  * @curve: will hold the curve  | 
1761  |  |  * @x: will hold the x-coordinate  | 
1762  |  |  * @y: will hold the y-coordinate  | 
1763  |  |  * @k: will hold the private key  | 
1764  |  |  *  | 
1765  |  |  * This function will export the ECC private key's parameters found  | 
1766  |  |  * in the given structure. The new parameters will be allocated using  | 
1767  |  |  * gnutls_malloc() and will be stored in the appropriate datum.  | 
1768  |  |  *  | 
1769  |  |  * In EdDSA curves the @y parameter will be %NULL and the other parameters  | 
1770  |  |  * will be in the native format for the curve.  | 
1771  |  |  *  | 
1772  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1773  |  |  *   negative error value.  | 
1774  |  |  *  | 
1775  |  |  * Since: 3.0  | 
1776  |  |  **/  | 
1777  |  | int gnutls_x509_privkey_export_ecc_raw(gnutls_x509_privkey_t key,  | 
1778  |  |                gnutls_ecc_curve_t *curve,  | 
1779  |  |                gnutls_datum_t *x, gnutls_datum_t *y,  | 
1780  |  |                gnutls_datum_t *k)  | 
1781  | 0  | { | 
1782  | 0  |   if (key == NULL) { | 
1783  | 0  |     gnutls_assert();  | 
1784  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
1785  | 0  |   }  | 
1786  |  |  | 
1787  | 0  |   return _gnutls_params_get_ecc_raw(&key->params, curve, x, y, k, 0);  | 
1788  | 0  | }  | 
1789  |  |  | 
1790  |  | /**  | 
1791  |  |  * gnutls_x509_privkey_export_gost_raw:  | 
1792  |  |  * @key: a key  | 
1793  |  |  * @curve: will hold the curve  | 
1794  |  |  * @digest: will hold the digest  | 
1795  |  |  * @paramset: will hold the GOST parameter set ID  | 
1796  |  |  * @x: will hold the x-coordinate  | 
1797  |  |  * @y: will hold the y-coordinate  | 
1798  |  |  * @k: will hold the private key  | 
1799  |  |  *  | 
1800  |  |  * This function will export the GOST private key's parameters found  | 
1801  |  |  * in the given structure. The new parameters will be allocated using  | 
1802  |  |  * gnutls_malloc() and will be stored in the appropriate datum.  | 
1803  |  |  *  | 
1804  |  |  * Note: parameters will be stored with least significant byte first. On  | 
1805  |  |  * version 3.6.3 this was incorrectly returned in big-endian format.  | 
1806  |  |  *  | 
1807  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1808  |  |  *   negative error value.  | 
1809  |  |  *  | 
1810  |  |  * Since: 3.6.3  | 
1811  |  |  **/  | 
1812  |  | int gnutls_x509_privkey_export_gost_raw(gnutls_x509_privkey_t key,  | 
1813  |  |           gnutls_ecc_curve_t *curve,  | 
1814  |  |           gnutls_digest_algorithm_t *digest,  | 
1815  |  |           gnutls_gost_paramset_t *paramset,  | 
1816  |  |           gnutls_datum_t *x, gnutls_datum_t *y,  | 
1817  |  |           gnutls_datum_t *k)  | 
1818  | 0  | { | 
1819  | 0  |   if (key == NULL) { | 
1820  | 0  |     gnutls_assert();  | 
1821  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
1822  | 0  |   }  | 
1823  |  |  | 
1824  | 0  |   return _gnutls_params_get_gost_raw(&key->params, curve, digest,  | 
1825  | 0  |              paramset, x, y, k, 0);  | 
1826  | 0  | }  | 
1827  |  |  | 
1828  |  | /**  | 
1829  |  |  * gnutls_x509_privkey_export_rsa_raw:  | 
1830  |  |  * @key: a key  | 
1831  |  |  * @m: will hold the modulus  | 
1832  |  |  * @e: will hold the public exponent  | 
1833  |  |  * @d: will hold the private exponent  | 
1834  |  |  * @p: will hold the first prime (p)  | 
1835  |  |  * @q: will hold the second prime (q)  | 
1836  |  |  * @u: will hold the coefficient  | 
1837  |  |  *  | 
1838  |  |  * This function will export the RSA private key's parameters found  | 
1839  |  |  * in the given structure. The new parameters will be allocated using  | 
1840  |  |  * gnutls_malloc() and will be stored in the appropriate datum.  | 
1841  |  |  *  | 
1842  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1843  |  |  *   negative error value.  | 
1844  |  |  **/  | 
1845  |  | int gnutls_x509_privkey_export_rsa_raw(gnutls_x509_privkey_t key,  | 
1846  |  |                gnutls_datum_t *m, gnutls_datum_t *e,  | 
1847  |  |                gnutls_datum_t *d, gnutls_datum_t *p,  | 
1848  |  |                gnutls_datum_t *q, gnutls_datum_t *u)  | 
1849  | 0  | { | 
1850  | 0  |   return _gnutls_params_get_rsa_raw(&key->params, m, e, d, p, q, u, NULL,  | 
1851  | 0  |             NULL, 0);  | 
1852  | 0  | }  | 
1853  |  |  | 
1854  |  | /**  | 
1855  |  |  * gnutls_x509_privkey_export_rsa_raw2:  | 
1856  |  |  * @key: a key  | 
1857  |  |  * @m: will hold the modulus  | 
1858  |  |  * @e: will hold the public exponent  | 
1859  |  |  * @d: will hold the private exponent  | 
1860  |  |  * @p: will hold the first prime (p)  | 
1861  |  |  * @q: will hold the second prime (q)  | 
1862  |  |  * @u: will hold the coefficient  | 
1863  |  |  * @e1: will hold e1 = d mod (p-1)  | 
1864  |  |  * @e2: will hold e2 = d mod (q-1)  | 
1865  |  |  *  | 
1866  |  |  * This function will export the RSA private key's parameters found  | 
1867  |  |  * in the given structure. The new parameters will be allocated using  | 
1868  |  |  * gnutls_malloc() and will be stored in the appropriate datum.  | 
1869  |  |  *  | 
1870  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1871  |  |  *   negative error value.  | 
1872  |  |  *  | 
1873  |  |  * Since: 2.12.0  | 
1874  |  |  **/  | 
1875  |  | int gnutls_x509_privkey_export_rsa_raw2(gnutls_x509_privkey_t key,  | 
1876  |  |           gnutls_datum_t *m, gnutls_datum_t *e,  | 
1877  |  |           gnutls_datum_t *d, gnutls_datum_t *p,  | 
1878  |  |           gnutls_datum_t *q, gnutls_datum_t *u,  | 
1879  |  |           gnutls_datum_t *e1, gnutls_datum_t *e2)  | 
1880  | 0  | { | 
1881  | 0  |   return _gnutls_params_get_rsa_raw(&key->params, m, e, d, p, q, u, e1,  | 
1882  | 0  |             e2, 0);  | 
1883  | 0  | }  | 
1884  |  |  | 
1885  |  | /**  | 
1886  |  |  * gnutls_x509_privkey_export_dsa_raw:  | 
1887  |  |  * @key: a key  | 
1888  |  |  * @p: will hold the p  | 
1889  |  |  * @q: will hold the q  | 
1890  |  |  * @g: will hold the g  | 
1891  |  |  * @y: will hold the y  | 
1892  |  |  * @x: will hold the x  | 
1893  |  |  *  | 
1894  |  |  * This function will export the DSA private key's parameters found  | 
1895  |  |  * in the given structure. The new parameters will be allocated using  | 
1896  |  |  * gnutls_malloc() and will be stored in the appropriate datum.  | 
1897  |  |  *  | 
1898  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1899  |  |  *   negative error value.  | 
1900  |  |  **/  | 
1901  |  | int gnutls_x509_privkey_export_dsa_raw(gnutls_x509_privkey_t key,  | 
1902  |  |                gnutls_datum_t *p, gnutls_datum_t *q,  | 
1903  |  |                gnutls_datum_t *g, gnutls_datum_t *y,  | 
1904  |  |                gnutls_datum_t *x)  | 
1905  | 0  | { | 
1906  | 0  |   return _gnutls_params_get_dsa_raw(&key->params, p, q, g, y, x, 0);  | 
1907  | 0  | }  | 
1908  |  |  | 
1909  |  | /**  | 
1910  |  |  * gnutls_x509_privkey_generate:  | 
1911  |  |  * @key: an initialized key  | 
1912  |  |  * @algo: is one of the algorithms in #gnutls_pk_algorithm_t.  | 
1913  |  |  * @bits: the size of the parameters to generate  | 
1914  |  |  * @flags: Must be zero or flags from #gnutls_privkey_flags_t.  | 
1915  |  |  *  | 
1916  |  |  * This function will generate a random private key. Note that this  | 
1917  |  |  * function must be called on an initialized private key.  | 
1918  |  |  *  | 
1919  |  |  * The flag %GNUTLS_PRIVKEY_FLAG_PROVABLE  | 
1920  |  |  * instructs the key generation process to use algorithms like Shawe-Taylor  | 
1921  |  |  * (from FIPS PUB186-4) which generate provable parameters out of a seed  | 
1922  |  |  * for RSA and DSA keys. See gnutls_x509_privkey_generate2() for more  | 
1923  |  |  * information.  | 
1924  |  |  *  | 
1925  |  |  * Note that when generating an elliptic curve key, the curve  | 
1926  |  |  * can be substituted in the place of the bits parameter using the  | 
1927  |  |  * GNUTLS_CURVE_TO_BITS() macro. The input to the macro is any curve from  | 
1928  |  |  * %gnutls_ecc_curve_t.  | 
1929  |  |  *  | 
1930  |  |  * For DSA keys, if the subgroup size needs to be specified check  | 
1931  |  |  * the GNUTLS_SUBGROUP_TO_BITS() macro.  | 
1932  |  |  *  | 
1933  |  |  * It is recommended to do not set the number of @bits directly, use gnutls_sec_param_to_pk_bits() instead .  | 
1934  |  |  *  | 
1935  |  |  * See also gnutls_privkey_generate(), gnutls_x509_privkey_generate2().  | 
1936  |  |  *  | 
1937  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1938  |  |  *   negative error value.  | 
1939  |  |  **/  | 
1940  |  | int gnutls_x509_privkey_generate(gnutls_x509_privkey_t key,  | 
1941  |  |          gnutls_pk_algorithm_t algo, unsigned int bits,  | 
1942  |  |          unsigned int flags)  | 
1943  | 0  | { | 
1944  | 0  |   return gnutls_x509_privkey_generate2(key, algo, bits, flags, NULL, 0);  | 
1945  | 0  | }  | 
1946  |  |  | 
1947  |  | /**  | 
1948  |  |  * gnutls_x509_privkey_generate2:  | 
1949  |  |  * @key: a key  | 
1950  |  |  * @algo: is one of the algorithms in #gnutls_pk_algorithm_t.  | 
1951  |  |  * @bits: the size of the modulus  | 
1952  |  |  * @flags: Must be zero or flags from #gnutls_privkey_flags_t.  | 
1953  |  |  * @data: Allow specifying %gnutls_keygen_data_st types such as the seed to be used.  | 
1954  |  |  * @data_size: The number of @data available.  | 
1955  |  |  *  | 
1956  |  |  * This function will generate a random private key. Note that this  | 
1957  |  |  * function must be called on an initialized private key.  | 
1958  |  |  *  | 
1959  |  |  * The flag %GNUTLS_PRIVKEY_FLAG_PROVABLE  | 
1960  |  |  * instructs the key generation process to use algorithms like Shawe-Taylor  | 
1961  |  |  * (from FIPS PUB186-4) which generate provable parameters out of a seed  | 
1962  |  |  * for RSA and DSA keys. On DSA keys the PQG parameters are generated using the  | 
1963  |  |  * seed, while on RSA the two primes. To specify an explicit seed  | 
1964  |  |  * (by default a random seed is used), use the @data with a %GNUTLS_KEYGEN_SEED  | 
1965  |  |  * type.  | 
1966  |  |  *  | 
1967  |  |  * Note that when generating an elliptic curve key, the curve  | 
1968  |  |  * can be substituted in the place of the bits parameter using the  | 
1969  |  |  * GNUTLS_CURVE_TO_BITS() macro.  | 
1970  |  |  *  | 
1971  |  |  * To export the generated keys in memory or in files it is recommended to use the  | 
1972  |  |  * PKCS#8 form as it can handle all key types, and can store additional parameters  | 
1973  |  |  * such as the seed, in case of provable RSA or DSA keys.  | 
1974  |  |  * Generated keys can be exported in memory using gnutls_privkey_export_x509(),  | 
1975  |  |  * and then with gnutls_x509_privkey_export2_pkcs8().  | 
1976  |  |  *  | 
1977  |  |  * If key generation is part of your application, avoid setting the number  | 
1978  |  |  * of bits directly, and instead use gnutls_sec_param_to_pk_bits().  | 
1979  |  |  * That way the generated keys will adapt to the security levels  | 
1980  |  |  * of the underlying GnuTLS library.  | 
1981  |  |  *  | 
1982  |  |  * See also gnutls_privkey_generate2().  | 
1983  |  |  *  | 
1984  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
1985  |  |  *   negative error value.  | 
1986  |  |  **/  | 
1987  |  | int gnutls_x509_privkey_generate2(gnutls_x509_privkey_t key,  | 
1988  |  |           gnutls_pk_algorithm_t algo, unsigned int bits,  | 
1989  |  |           unsigned int flags,  | 
1990  |  |           const gnutls_keygen_data_st *data,  | 
1991  |  |           unsigned data_size)  | 
1992  | 0  | { | 
1993  | 0  |   int ret;  | 
1994  | 0  |   unsigned i;  | 
1995  | 0  |   gnutls_x509_spki_t spki = NULL;  | 
1996  | 0  |   gnutls_dh_params_t dh_params = NULL;  | 
1997  |  | 
  | 
1998  | 0  |   if (key == NULL) { | 
1999  | 0  |     gnutls_assert();  | 
2000  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
2001  | 0  |   }  | 
2002  |  |  | 
2003  | 0  |   gnutls_pk_params_init(&key->params);  | 
2004  |  | 
  | 
2005  | 0  |   for (i = 0; i < data_size; i++) { | 
2006  | 0  |     switch (data[i].type) { | 
2007  | 0  |     case GNUTLS_KEYGEN_SEED:  | 
2008  | 0  |       if (data[i].size < sizeof(key->params.seed)) { | 
2009  | 0  |         key->params.seed_size = data[i].size;  | 
2010  | 0  |         memcpy(key->params.seed, data[i].data,  | 
2011  | 0  |                data[i].size);  | 
2012  | 0  |       }  | 
2013  | 0  |       break;  | 
2014  | 0  |     case GNUTLS_KEYGEN_DIGEST:  | 
2015  | 0  |       key->params.palgo = data[i].size;  | 
2016  | 0  |       break;  | 
2017  | 0  |     case GNUTLS_KEYGEN_SPKI:  | 
2018  | 0  |       spki = (void *)data[i].data;  | 
2019  | 0  |       break;  | 
2020  | 0  |     case GNUTLS_KEYGEN_DH:  | 
2021  | 0  |       if (algo != GNUTLS_PK_DH) { | 
2022  | 0  |         return gnutls_assert_val(  | 
2023  | 0  |           GNUTLS_E_INVALID_REQUEST);  | 
2024  | 0  |       }  | 
2025  | 0  |       dh_params = (void *)data[i].data;  | 
2026  | 0  |       break;  | 
2027  | 0  |     }  | 
2028  | 0  |   }  | 
2029  |  |  | 
2030  | 0  |   if (IS_EC(algo)) { | 
2031  | 0  |     if (GNUTLS_BITS_ARE_CURVE(bits))  | 
2032  | 0  |       bits = GNUTLS_BITS_TO_CURVE(bits);  | 
2033  | 0  |     else  | 
2034  | 0  |       bits = _gnutls_ecc_bits_to_curve(algo, bits);  | 
2035  |  | 
  | 
2036  | 0  |     if (gnutls_ecc_curve_get_pk(bits) != algo) { | 
2037  | 0  |       _gnutls_debug_log(  | 
2038  | 0  |         "curve is incompatible with public key algorithm\n");  | 
2039  | 0  |       return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
2040  | 0  |     }  | 
2041  | 0  |   }  | 
2042  |  |  | 
2043  | 0  |   if (IS_GOSTEC(algo)) { | 
2044  | 0  |     int size;  | 
2045  |  | 
  | 
2046  | 0  |     if (GNUTLS_BITS_ARE_CURVE(bits))  | 
2047  | 0  |       bits = GNUTLS_BITS_TO_CURVE(bits);  | 
2048  | 0  |     else  | 
2049  | 0  |       bits = _gnutls_ecc_bits_to_curve(algo, bits);  | 
2050  |  | 
  | 
2051  | 0  |     size = gnutls_ecc_curve_get_size(bits);  | 
2052  |  | 
  | 
2053  | 0  |     if ((algo == GNUTLS_PK_GOST_01 && size != 32) ||  | 
2054  | 0  |         (algo == GNUTLS_PK_GOST_12_256 && size != 32) ||  | 
2055  | 0  |         (algo == GNUTLS_PK_GOST_12_512 && size != 64)) { | 
2056  | 0  |       _gnutls_debug_log(  | 
2057  | 0  |         "curve is incompatible with public key algorithm\n");  | 
2058  | 0  |       return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
2059  | 0  |     }  | 
2060  |  |  | 
2061  | 0  |     key->params.gost_params = _gnutls_gost_paramset_default(algo);  | 
2062  | 0  |   }  | 
2063  |  |  | 
2064  | 0  |   if (flags & GNUTLS_PRIVKEY_FLAG_PROVABLE) { | 
2065  | 0  |     key->params.pkflags |= GNUTLS_PK_FLAG_PROVABLE;  | 
2066  | 0  |   }  | 
2067  |  | 
  | 
2068  | 0  |   key->params.algo = algo;  | 
2069  |  |  | 
2070  |  |   /* DH params are given, no need to regenerate */  | 
2071  | 0  |   if (algo == GNUTLS_PK_DH && dh_params != NULL) { | 
2072  | 0  |     key->params.params[DH_P] =  | 
2073  | 0  |       _gnutls_mpi_copy(dh_params->params[0]);  | 
2074  | 0  |     key->params.params[DH_G] =  | 
2075  | 0  |       _gnutls_mpi_copy(dh_params->params[1]);  | 
2076  | 0  |     if (dh_params->params[2]) { | 
2077  | 0  |       key->params.params[DH_Q] =  | 
2078  | 0  |         _gnutls_mpi_copy(dh_params->params[2]);  | 
2079  | 0  |     }  | 
2080  |  |     /* X and Y will be added by _gnutls_pk_generate_keys */  | 
2081  | 0  |     key->params.params_nr = 3;  | 
2082  | 0  |     key->params.qbits = dh_params->q_bits;  | 
2083  | 0  |   } else { | 
2084  | 0  |     ret = _gnutls_pk_generate_params(algo, bits, &key->params);  | 
2085  | 0  |     if (ret < 0) { | 
2086  | 0  |       gnutls_assert();  | 
2087  | 0  |       return ret;  | 
2088  | 0  |     }  | 
2089  | 0  |   }  | 
2090  |  |  | 
2091  | 0  |   if (algo == GNUTLS_PK_RSA_PSS && (flags & GNUTLS_PRIVKEY_FLAG_CA) &&  | 
2092  | 0  |       !key->params.spki.pk) { | 
2093  | 0  |     const mac_entry_st *me;  | 
2094  | 0  |     key->params.spki.pk = GNUTLS_PK_RSA_PSS;  | 
2095  |  | 
  | 
2096  | 0  |     key->params.spki.rsa_pss_dig =  | 
2097  | 0  |       _gnutls_pk_bits_to_sha_hash(bits);  | 
2098  |  | 
  | 
2099  | 0  |     me = hash_to_entry(key->params.spki.rsa_pss_dig);  | 
2100  | 0  |     if (unlikely(me == NULL)) { | 
2101  | 0  |       gnutls_assert();  | 
2102  | 0  |       ret = GNUTLS_E_INVALID_REQUEST;  | 
2103  | 0  |       goto cleanup;  | 
2104  | 0  |     }  | 
2105  |  |  | 
2106  | 0  |     ret = _gnutls_find_rsa_pss_salt_size(bits, me, 0);  | 
2107  | 0  |     if (ret < 0) { | 
2108  | 0  |       gnutls_assert();  | 
2109  | 0  |       goto cleanup;  | 
2110  | 0  |     }  | 
2111  |  |  | 
2112  | 0  |     key->params.spki.salt_size = ret;  | 
2113  | 0  |   }  | 
2114  |  |  | 
2115  | 0  |   if (algo == GNUTLS_PK_RSA_OAEP && !key->params.spki.pk) { | 
2116  | 0  |     const mac_entry_st *me;  | 
2117  | 0  |     key->params.spki.pk = GNUTLS_PK_RSA_OAEP;  | 
2118  |  | 
  | 
2119  | 0  |     key->params.spki.rsa_oaep_dig =  | 
2120  | 0  |       _gnutls_pk_bits_to_sha_hash(bits);  | 
2121  |  | 
  | 
2122  | 0  |     me = hash_to_entry(key->params.spki.rsa_oaep_dig);  | 
2123  | 0  |     if (unlikely(me == NULL)) { | 
2124  | 0  |       gnutls_assert();  | 
2125  | 0  |       ret = GNUTLS_E_INVALID_REQUEST;  | 
2126  | 0  |       goto cleanup;  | 
2127  | 0  |     }  | 
2128  | 0  |   }  | 
2129  |  |  | 
2130  | 0  |   ret = _gnutls_pk_generate_keys(algo, bits, &key->params, 0);  | 
2131  | 0  |   if (ret < 0) { | 
2132  | 0  |     gnutls_assert();  | 
2133  | 0  |     goto cleanup;  | 
2134  | 0  |   }  | 
2135  |  |  | 
2136  | 0  |   ret = _gnutls_pk_verify_priv_params(algo, &key->params);  | 
2137  | 0  |   if (ret < 0) { | 
2138  | 0  |     gnutls_assert();  | 
2139  | 0  |     goto cleanup;  | 
2140  | 0  |   }  | 
2141  |  |  | 
2142  | 0  |   if (spki) { | 
2143  | 0  |     ret = gnutls_x509_privkey_set_spki(key, spki, 0);  | 
2144  | 0  |     if (ret < 0) { | 
2145  | 0  |       gnutls_assert();  | 
2146  | 0  |       goto cleanup;  | 
2147  | 0  |     }  | 
2148  | 0  |   }  | 
2149  |  |  | 
2150  |  |   /* DH keys are only exportable in PKCS#8 format */  | 
2151  | 0  |   if (algo != GNUTLS_PK_DH) { | 
2152  | 0  |     ret = _gnutls_asn1_encode_privkey(&key->key, &key->params);  | 
2153  | 0  |     if (ret < 0) { | 
2154  | 0  |       gnutls_assert();  | 
2155  | 0  |       goto cleanup;  | 
2156  | 0  |     }  | 
2157  | 0  |   }  | 
2158  |  |  | 
2159  | 0  |   return 0;  | 
2160  |  |  | 
2161  | 0  | cleanup:  | 
2162  | 0  |   key->params.algo = GNUTLS_PK_UNKNOWN;  | 
2163  | 0  |   gnutls_pk_params_clear(&key->params);  | 
2164  | 0  |   gnutls_pk_params_release(&key->params);  | 
2165  |  | 
  | 
2166  | 0  |   return ret;  | 
2167  | 0  | }  | 
2168  |  |  | 
2169  |  | /**  | 
2170  |  |  * gnutls_x509_privkey_get_seed:  | 
2171  |  |  * @key: should contain a #gnutls_x509_privkey_t type  | 
2172  |  |  * @digest: if non-NULL it will contain the digest algorithm used for key generation (if applicable)  | 
2173  |  |  * @seed: where seed will be copied to  | 
2174  |  |  * @seed_size: originally holds the size of @seed, will be updated with actual size  | 
2175  |  |  *  | 
2176  |  |  * This function will return the seed that was used to generate the  | 
2177  |  |  * given private key. That function will succeed only if the key was generated  | 
2178  |  |  * as a provable key.  | 
2179  |  |  *  | 
2180  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
2181  |  |  *   negative error value.  | 
2182  |  |  *  | 
2183  |  |  * Since: 3.5.0  | 
2184  |  |  **/  | 
2185  |  | int gnutls_x509_privkey_get_seed(gnutls_x509_privkey_t key,  | 
2186  |  |          gnutls_digest_algorithm_t *digest, void *seed,  | 
2187  |  |          size_t *seed_size)  | 
2188  | 0  | { | 
2189  | 0  |   if (key->params.seed_size == 0)  | 
2190  | 0  |     return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
2191  |  |  | 
2192  | 0  |   if (seed_size == NULL || seed == NULL) { | 
2193  | 0  |     if (key->params.seed_size)  | 
2194  | 0  |       return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);  | 
2195  | 0  |     else  | 
2196  | 0  |       return gnutls_assert_val(GNUTLS_E_INVALID_REQUEST);  | 
2197  | 0  |   }  | 
2198  |  |  | 
2199  | 0  |   if (*seed_size < key->params.seed_size) { | 
2200  | 0  |     *seed_size = key->params.seed_size;  | 
2201  | 0  |     return gnutls_assert_val(GNUTLS_E_SHORT_MEMORY_BUFFER);  | 
2202  | 0  |   }  | 
2203  |  |  | 
2204  | 0  |   if (digest)  | 
2205  | 0  |     *digest = key->params.palgo;  | 
2206  |  | 
  | 
2207  | 0  |   memcpy(seed, key->params.seed, key->params.seed_size);  | 
2208  | 0  |   *seed_size = key->params.seed_size;  | 
2209  | 0  |   return 0;  | 
2210  | 0  | }  | 
2211  |  |  | 
2212  |  | static int cmp_rsa_key(gnutls_x509_privkey_t key1, gnutls_x509_privkey_t key2)  | 
2213  | 0  | { | 
2214  | 0  |   gnutls_datum_t m1 = { NULL, 0 }, e1 = { NULL, 0 }, d1 = { NULL, 0 }, | 
2215  | 0  |            p1 = { NULL, 0 }, q1 = { NULL, 0 }; | 
2216  | 0  |   gnutls_datum_t m2 = { NULL, 0 }, e2 = { NULL, 0 }, d2 = { NULL, 0 }, | 
2217  | 0  |            p2 = { NULL, 0 }, q2 = { NULL, 0 }; | 
2218  | 0  |   int ret;  | 
2219  |  | 
  | 
2220  | 0  |   ret = gnutls_x509_privkey_export_rsa_raw(key1, &m1, &e1, &d1, &p1, &q1,  | 
2221  | 0  |              NULL);  | 
2222  | 0  |   if (ret < 0) { | 
2223  | 0  |     gnutls_assert();  | 
2224  | 0  |     return ret;  | 
2225  | 0  |   }  | 
2226  |  |  | 
2227  | 0  |   ret = gnutls_x509_privkey_export_rsa_raw(key2, &m2, &e2, &d2, &p2, &q2,  | 
2228  | 0  |              NULL);  | 
2229  | 0  |   if (ret < 0) { | 
2230  | 0  |     gnutls_assert();  | 
2231  | 0  |     goto cleanup;  | 
2232  | 0  |   }  | 
2233  |  |  | 
2234  | 0  |   if (m1.size != m2.size || memcmp(m1.data, m2.data, m1.size) != 0) { | 
2235  | 0  |     gnutls_assert();  | 
2236  | 0  |     ret = GNUTLS_E_PRIVKEY_VERIFICATION_ERROR;  | 
2237  | 0  |     goto cleanup;  | 
2238  | 0  |   }  | 
2239  |  |  | 
2240  | 0  |   if (d1.size != d2.size || memcmp(d1.data, d2.data, d1.size) != 0) { | 
2241  | 0  |     gnutls_assert();  | 
2242  | 0  |     ret = GNUTLS_E_PRIVKEY_VERIFICATION_ERROR;  | 
2243  | 0  |     goto cleanup;  | 
2244  | 0  |   }  | 
2245  |  |  | 
2246  | 0  |   if (e1.size != e2.size || memcmp(e1.data, e2.data, e1.size) != 0) { | 
2247  | 0  |     gnutls_assert();  | 
2248  | 0  |     ret = GNUTLS_E_PRIVKEY_VERIFICATION_ERROR;  | 
2249  | 0  |     goto cleanup;  | 
2250  | 0  |   }  | 
2251  |  |  | 
2252  | 0  |   if (p1.size != p2.size || memcmp(p1.data, p2.data, p1.size) != 0) { | 
2253  | 0  |     gnutls_assert();  | 
2254  | 0  |     ret = GNUTLS_E_PRIVKEY_VERIFICATION_ERROR;  | 
2255  | 0  |     goto cleanup;  | 
2256  | 0  |   }  | 
2257  |  |  | 
2258  | 0  |   if (q1.size != q2.size || memcmp(q1.data, q2.data, q1.size) != 0) { | 
2259  | 0  |     gnutls_assert();  | 
2260  | 0  |     ret = GNUTLS_E_PRIVKEY_VERIFICATION_ERROR;  | 
2261  | 0  |     goto cleanup;  | 
2262  | 0  |   }  | 
2263  |  |  | 
2264  | 0  |   ret = 0;  | 
2265  | 0  | cleanup:  | 
2266  | 0  |   gnutls_free(m1.data);  | 
2267  | 0  |   gnutls_free(e1.data);  | 
2268  | 0  |   gnutls_free(d1.data);  | 
2269  | 0  |   gnutls_free(p1.data);  | 
2270  | 0  |   gnutls_free(q1.data);  | 
2271  | 0  |   gnutls_free(m2.data);  | 
2272  | 0  |   gnutls_free(e2.data);  | 
2273  | 0  |   gnutls_free(d2.data);  | 
2274  | 0  |   gnutls_free(p2.data);  | 
2275  | 0  |   gnutls_free(q2.data);  | 
2276  | 0  |   return ret;  | 
2277  | 0  | }  | 
2278  |  |  | 
2279  |  | static int cmp_dsa_key(gnutls_x509_privkey_t key1, gnutls_x509_privkey_t key2)  | 
2280  | 0  | { | 
2281  | 0  |   gnutls_datum_t p1 = { NULL, 0 }, q1 = { NULL, 0 }, g1 = { NULL, 0 }; | 
2282  | 0  |   gnutls_datum_t p2 = { NULL, 0 }, q2 = { NULL, 0 }, g2 = { NULL, 0 }; | 
2283  | 0  |   int ret;  | 
2284  |  | 
  | 
2285  | 0  |   ret = gnutls_x509_privkey_export_dsa_raw(key1, &p1, &q1, &g1, NULL,  | 
2286  | 0  |              NULL);  | 
2287  | 0  |   if (ret < 0) { | 
2288  | 0  |     gnutls_assert();  | 
2289  | 0  |     return ret;  | 
2290  | 0  |   }  | 
2291  |  |  | 
2292  | 0  |   ret = gnutls_x509_privkey_export_dsa_raw(key2, &p2, &q2, &g2, NULL,  | 
2293  | 0  |              NULL);  | 
2294  | 0  |   if (ret < 0) { | 
2295  | 0  |     gnutls_assert();  | 
2296  | 0  |     goto cleanup;  | 
2297  | 0  |   }  | 
2298  |  |  | 
2299  | 0  |   if (g1.size != g2.size || memcmp(g1.data, g2.data, g1.size) != 0) { | 
2300  | 0  |     gnutls_assert();  | 
2301  | 0  |     ret = GNUTLS_E_PRIVKEY_VERIFICATION_ERROR;  | 
2302  | 0  |     goto cleanup;  | 
2303  | 0  |   }  | 
2304  |  |  | 
2305  | 0  |   if (p1.size != p2.size || memcmp(p1.data, p2.data, p1.size) != 0) { | 
2306  | 0  |     gnutls_assert();  | 
2307  | 0  |     ret = GNUTLS_E_PRIVKEY_VERIFICATION_ERROR;  | 
2308  | 0  |     goto cleanup;  | 
2309  | 0  |   }  | 
2310  |  |  | 
2311  | 0  |   if (q1.size != q2.size || memcmp(q1.data, q2.data, q1.size) != 0) { | 
2312  | 0  |     gnutls_assert();  | 
2313  | 0  |     ret = GNUTLS_E_PRIVKEY_VERIFICATION_ERROR;  | 
2314  | 0  |     goto cleanup;  | 
2315  | 0  |   }  | 
2316  |  |  | 
2317  | 0  |   ret = 0;  | 
2318  | 0  | cleanup:  | 
2319  | 0  |   gnutls_free(g1.data);  | 
2320  | 0  |   gnutls_free(p1.data);  | 
2321  | 0  |   gnutls_free(q1.data);  | 
2322  | 0  |   gnutls_free(g2.data);  | 
2323  | 0  |   gnutls_free(p2.data);  | 
2324  | 0  |   gnutls_free(q2.data);  | 
2325  | 0  |   return ret;  | 
2326  | 0  | }  | 
2327  |  |  | 
2328  |  | /**  | 
2329  |  |  * gnutls_x509_privkey_verify_seed:  | 
2330  |  |  * @key: should contain a #gnutls_x509_privkey_t type  | 
2331  |  |  * @digest: it contains the digest algorithm used for key generation (if applicable)  | 
2332  |  |  * @seed: the seed of the key to be checked with  | 
2333  |  |  * @seed_size: holds the size of @seed  | 
2334  |  |  *  | 
2335  |  |  * This function will verify that the given private key was generated from  | 
2336  |  |  * the provided seed. If @seed is %NULL then the seed stored in the @key's structure  | 
2337  |  |  * will be used for verification.  | 
2338  |  |  *  | 
2339  |  |  * Returns: In case of a verification failure %GNUTLS_E_PRIVKEY_VERIFICATION_ERROR  | 
2340  |  |  * is returned, and zero or positive code on success.  | 
2341  |  |  *  | 
2342  |  |  * Since: 3.5.0  | 
2343  |  |  **/  | 
2344  |  | int gnutls_x509_privkey_verify_seed(gnutls_x509_privkey_t key,  | 
2345  |  |             gnutls_digest_algorithm_t digest,  | 
2346  |  |             const void *seed, size_t seed_size)  | 
2347  | 0  | { | 
2348  | 0  |   int ret;  | 
2349  | 0  |   gnutls_x509_privkey_t okey;  | 
2350  | 0  |   unsigned bits;  | 
2351  | 0  |   gnutls_keygen_data_st data;  | 
2352  |  | 
  | 
2353  | 0  |   if (key == NULL) { | 
2354  | 0  |     gnutls_assert();  | 
2355  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
2356  | 0  |   }  | 
2357  |  |  | 
2358  | 0  |   if (key->params.algo != GNUTLS_PK_RSA &&  | 
2359  | 0  |       key->params.algo != GNUTLS_PK_DSA)  | 
2360  | 0  |     return gnutls_assert_val(GNUTLS_E_UNIMPLEMENTED_FEATURE);  | 
2361  |  |  | 
2362  | 0  |   ret = gnutls_x509_privkey_get_pk_algorithm2(key, &bits);  | 
2363  | 0  |   if (ret < 0)  | 
2364  | 0  |     return gnutls_assert_val(ret);  | 
2365  |  |  | 
2366  | 0  |   ret = gnutls_x509_privkey_init(&okey);  | 
2367  | 0  |   if (ret < 0)  | 
2368  | 0  |     return gnutls_assert_val(ret);  | 
2369  |  |  | 
2370  | 0  |   if (seed == NULL) { | 
2371  | 0  |     seed = key->params.seed;  | 
2372  | 0  |     seed_size = key->params.seed_size;  | 
2373  | 0  |   }  | 
2374  |  | 
  | 
2375  | 0  |   if (seed == NULL || seed_size == 0)  | 
2376  | 0  |     return gnutls_assert_val(GNUTLS_E_PK_NO_VALIDATION_PARAMS);  | 
2377  |  |  | 
2378  | 0  |   data.type = GNUTLS_KEYGEN_SEED;  | 
2379  | 0  |   data.data = (void *)seed;  | 
2380  | 0  |   data.size = seed_size;  | 
2381  |  | 
  | 
2382  | 0  |   ret = gnutls_x509_privkey_generate2(okey, key->params.algo, bits,  | 
2383  | 0  |               GNUTLS_PRIVKEY_FLAG_PROVABLE, &data,  | 
2384  | 0  |               1);  | 
2385  | 0  |   if (ret < 0) { | 
2386  | 0  |     gnutls_assert();  | 
2387  | 0  |     goto cleanup;  | 
2388  | 0  |   }  | 
2389  |  |  | 
2390  | 0  |   if (key->params.algo == GNUTLS_PK_RSA)  | 
2391  | 0  |     ret = cmp_rsa_key(key, okey);  | 
2392  | 0  |   else  | 
2393  | 0  |     ret = cmp_dsa_key(key, okey);  | 
2394  |  | 
  | 
2395  | 0  | cleanup:  | 
2396  | 0  |   gnutls_x509_privkey_deinit(okey);  | 
2397  |  | 
  | 
2398  | 0  |   return ret;  | 
2399  | 0  | }  | 
2400  |  |  | 
2401  |  | /**  | 
2402  |  |  * gnutls_x509_privkey_verify_params:  | 
2403  |  |  * @key: a key  | 
2404  |  |  *  | 
2405  |  |  * This function will verify the private key parameters.  | 
2406  |  |  *  | 
2407  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
2408  |  |  *   negative error value.  | 
2409  |  |  **/  | 
2410  |  | int gnutls_x509_privkey_verify_params(gnutls_x509_privkey_t key)  | 
2411  | 0  | { | 
2412  | 0  |   int ret;  | 
2413  |  | 
  | 
2414  | 0  |   ret = _gnutls_pk_verify_priv_params(key->params.algo, &key->params);  | 
2415  | 0  |   if (ret < 0) { | 
2416  | 0  |     gnutls_assert();  | 
2417  | 0  |     return ret;  | 
2418  | 0  |   }  | 
2419  |  |  | 
2420  | 0  |   return 0;  | 
2421  | 0  | }  | 
2422  |  |  | 
2423  |  | /**  | 
2424  |  |  * gnutls_x509_privkey_get_key_id:  | 
2425  |  |  * @key: a key  | 
2426  |  |  * @flags: should be one of the flags from %gnutls_keyid_flags_t  | 
2427  |  |  * @output_data: will contain the key ID  | 
2428  |  |  * @output_data_size: holds the size of output_data (and will be  | 
2429  |  |  *   replaced by the actual size of parameters)  | 
2430  |  |  *  | 
2431  |  |  * This function will return a unique ID that depends on the public key  | 
2432  |  |  * parameters. This ID can be used in checking whether a certificate  | 
2433  |  |  * corresponds to the given key.  | 
2434  |  |  *  | 
2435  |  |  * If the buffer provided is not long enough to hold the output, then  | 
2436  |  |  * *@output_data_size is updated and %GNUTLS_E_SHORT_MEMORY_BUFFER will  | 
2437  |  |  * be returned.  The output will normally be a SHA-1 hash output,  | 
2438  |  |  * which is 20 bytes.  | 
2439  |  |  *  | 
2440  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
2441  |  |  *   negative error value.  | 
2442  |  |  **/  | 
2443  |  | int gnutls_x509_privkey_get_key_id(gnutls_x509_privkey_t key,  | 
2444  |  |            unsigned int flags,  | 
2445  |  |            unsigned char *output_data,  | 
2446  |  |            size_t *output_data_size)  | 
2447  | 0  | { | 
2448  | 0  |   int ret;  | 
2449  |  | 
  | 
2450  | 0  |   if (key == NULL) { | 
2451  | 0  |     gnutls_assert();  | 
2452  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
2453  | 0  |   }  | 
2454  |  |  | 
2455  | 0  |   ret = _gnutls_get_key_id(&key->params, output_data, output_data_size,  | 
2456  | 0  |          flags);  | 
2457  | 0  |   if (ret < 0) { | 
2458  | 0  |     gnutls_assert();  | 
2459  | 0  |   }  | 
2460  |  | 
  | 
2461  | 0  |   return ret;  | 
2462  | 0  | }  | 
2463  |  |  | 
2464  |  | /**  | 
2465  |  |  * gnutls_x509_privkey_sign_hash:  | 
2466  |  |  * @key: a key  | 
2467  |  |  * @hash: holds the data to be signed  | 
2468  |  |  * @signature: will contain newly allocated signature  | 
2469  |  |  *  | 
2470  |  |  * This function will sign the given hash using the private key. Do not  | 
2471  |  |  * use this function directly unless you know what it is. Typical signing  | 
2472  |  |  * requires the data to be hashed and stored in special formats   | 
2473  |  |  * (e.g. BER Digest-Info for RSA).  | 
2474  |  |  *  | 
2475  |  |  * This API is provided only for backwards compatibility, and thus  | 
2476  |  |  * restricted to RSA, DSA and ECDSA key types. For other key types please  | 
2477  |  |  * use gnutls_privkey_sign_hash() and gnutls_privkey_sign_data().  | 
2478  |  |  *  | 
2479  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
2480  |  |  *   negative error value.  | 
2481  |  |  *  | 
2482  |  |  * Deprecated in: 2.12.0  | 
2483  |  |  */  | 
2484  |  | int gnutls_x509_privkey_sign_hash(gnutls_x509_privkey_t key,  | 
2485  |  |           const gnutls_datum_t *hash,  | 
2486  |  |           gnutls_datum_t *signature)  | 
2487  | 0  | { | 
2488  | 0  |   int result;  | 
2489  |  | 
  | 
2490  | 0  |   if (key == NULL) { | 
2491  | 0  |     gnutls_assert();  | 
2492  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
2493  | 0  |   }  | 
2494  |  |  | 
2495  | 0  |   if (key->params.algo != GNUTLS_PK_RSA &&  | 
2496  | 0  |       key->params.algo != GNUTLS_PK_ECDSA &&  | 
2497  | 0  |       key->params.algo != GNUTLS_PK_DSA) { | 
2498  |  |     /* too primitive API - use only with legacy types */  | 
2499  | 0  |     gnutls_assert();  | 
2500  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
2501  | 0  |   }  | 
2502  |  |  | 
2503  | 0  |   result = _gnutls_pk_sign(key->params.algo, signature, hash,  | 
2504  | 0  |          &key->params, &key->params.spki);  | 
2505  |  | 
  | 
2506  | 0  |   if (result < 0) { | 
2507  | 0  |     gnutls_assert();  | 
2508  | 0  |     return result;  | 
2509  | 0  |   }  | 
2510  |  |  | 
2511  | 0  |   return 0;  | 
2512  | 0  | }  | 
2513  |  |  | 
2514  |  | /**  | 
2515  |  |  * gnutls_x509_privkey_sign_data:  | 
2516  |  |  * @key: a key  | 
2517  |  |  * @digest: should be a digest algorithm  | 
2518  |  |  * @flags: should be 0 for now  | 
2519  |  |  * @data: holds the data to be signed  | 
2520  |  |  * @signature: will contain the signature  | 
2521  |  |  * @signature_size: holds the size of signature (and will be replaced  | 
2522  |  |  *   by the new size)  | 
2523  |  |  *  | 
2524  |  |  * This function will sign the given data using a signature algorithm  | 
2525  |  |  * supported by the private key. Signature algorithms are always used  | 
2526  |  |  * together with a hash functions.  Different hash functions may be  | 
2527  |  |  * used for the RSA algorithm, but only SHA-1 for the DSA keys.  | 
2528  |  |  *  | 
2529  |  |  * If the buffer provided is not long enough to hold the output, then  | 
2530  |  |  * *@signature_size is updated and %GNUTLS_E_SHORT_MEMORY_BUFFER will  | 
2531  |  |  * be returned.  | 
2532  |  |  *  | 
2533  |  |  * Use gnutls_x509_crt_get_preferred_hash_algorithm() to determine  | 
2534  |  |  * the hash algorithm.  | 
2535  |  |  *  | 
2536  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
2537  |  |  *   negative error value.  | 
2538  |  |  */  | 
2539  |  | int gnutls_x509_privkey_sign_data(gnutls_x509_privkey_t key,  | 
2540  |  |           gnutls_digest_algorithm_t digest,  | 
2541  |  |           unsigned int flags,  | 
2542  |  |           const gnutls_datum_t *data, void *signature,  | 
2543  |  |           size_t *signature_size)  | 
2544  | 0  | { | 
2545  | 0  |   gnutls_privkey_t privkey;  | 
2546  | 0  |   gnutls_datum_t sig = { NULL, 0 }; | 
2547  | 0  |   int ret;  | 
2548  |  | 
  | 
2549  | 0  |   ret = gnutls_privkey_init(&privkey);  | 
2550  | 0  |   if (ret < 0)  | 
2551  | 0  |     return gnutls_assert_val(ret);  | 
2552  |  |  | 
2553  | 0  |   ret = gnutls_privkey_import_x509(privkey, key, 0);  | 
2554  | 0  |   if (ret < 0) { | 
2555  | 0  |     gnutls_assert();  | 
2556  | 0  |     goto cleanup;  | 
2557  | 0  |   }  | 
2558  |  |  | 
2559  | 0  |   ret = gnutls_privkey_sign_data(privkey, digest, flags, data, &sig);  | 
2560  | 0  |   if (ret < 0) { | 
2561  | 0  |     gnutls_assert();  | 
2562  | 0  |     goto cleanup;  | 
2563  | 0  |   }  | 
2564  |  |  | 
2565  | 0  |   if (*signature_size < sig.size) { | 
2566  | 0  |     *signature_size = sig.size;  | 
2567  | 0  |     ret = GNUTLS_E_SHORT_MEMORY_BUFFER;  | 
2568  | 0  |     goto cleanup;  | 
2569  | 0  |   }  | 
2570  |  |  | 
2571  | 0  |   *signature_size = sig.size;  | 
2572  | 0  |   memcpy(signature, sig.data, sig.size);  | 
2573  |  | 
  | 
2574  | 0  | cleanup:  | 
2575  | 0  |   _gnutls_free_datum(&sig);  | 
2576  | 0  |   gnutls_privkey_deinit(privkey);  | 
2577  | 0  |   return ret;  | 
2578  | 0  | }  | 
2579  |  |  | 
2580  |  | /**  | 
2581  |  |  * gnutls_x509_privkey_fix:  | 
2582  |  |  * @key: a key  | 
2583  |  |  *  | 
2584  |  |  * This function will recalculate the secondary parameters in a key.  | 
2585  |  |  * In RSA keys, this can be the coefficient and exponent1,2.  | 
2586  |  |  *  | 
2587  |  |  * Returns: On success, %GNUTLS_E_SUCCESS (0) is returned, otherwise a  | 
2588  |  |  *   negative error value.  | 
2589  |  |  **/  | 
2590  |  | int gnutls_x509_privkey_fix(gnutls_x509_privkey_t key)  | 
2591  | 0  | { | 
2592  | 0  |   int ret;  | 
2593  |  | 
  | 
2594  | 0  |   if (key == NULL) { | 
2595  | 0  |     gnutls_assert();  | 
2596  | 0  |     return GNUTLS_E_INVALID_REQUEST;  | 
2597  | 0  |   }  | 
2598  |  |  | 
2599  | 0  |   if (key->key) { | 
2600  | 0  |     asn1_delete_structure2(&key->key, ASN1_DELETE_FLAG_ZEROIZE);  | 
2601  |  | 
  | 
2602  | 0  |     ret = _gnutls_asn1_encode_privkey(&key->key, &key->params);  | 
2603  | 0  |     if (ret < 0) { | 
2604  | 0  |       gnutls_assert();  | 
2605  | 0  |       return ret;  | 
2606  | 0  |     }  | 
2607  | 0  |   }  | 
2608  |  |  | 
2609  | 0  |   return 0;  | 
2610  | 0  | }  | 
2611  |  |  | 
2612  |  | /**  | 
2613  |  |  * gnutls_x509_privkey_set_pin_function:  | 
2614  |  |  * @privkey: The certificate structure  | 
2615  |  |  * @fn: the callback  | 
2616  |  |  * @userdata: data associated with the callback  | 
2617  |  |  *  | 
2618  |  |  * This function will set a callback function to be used when  | 
2619  |  |  * it is required to access a protected object. This function overrides   | 
2620  |  |  * the global function set using gnutls_pkcs11_set_pin_function().  | 
2621  |  |  *  | 
2622  |  |  * Note that this callback is used when decrypting a key.  | 
2623  |  |  *  | 
2624  |  |  * Since: 3.4.0  | 
2625  |  |  *  | 
2626  |  |  **/  | 
2627  |  | void gnutls_x509_privkey_set_pin_function(gnutls_x509_privkey_t privkey,  | 
2628  |  |             gnutls_pin_callback_t fn,  | 
2629  |  |             void *userdata)  | 
2630  | 0  | { | 
2631  | 0  |   privkey->pin.cb = fn;  | 
2632  | 0  |   privkey->pin.data = userdata;  | 
2633  | 0  | }  | 
2634  |  |  | 
2635  |  | /**  | 
2636  |  |  * gnutls_x509_privkey_set_flags:  | 
2637  |  |  * @key: A key of type #gnutls_x509_privkey_t  | 
2638  |  |  * @flags: flags from the %gnutls_privkey_flags  | 
2639  |  |  *  | 
2640  |  |  * This function will set flags for the specified private key, after  | 
2641  |  |  * it is generated. Currently this is useful for the %GNUTLS_PRIVKEY_FLAG_EXPORT_COMPAT  | 
2642  |  |  * to allow exporting a "provable" private key in backwards compatible way.  | 
2643  |  |  *  | 
2644  |  |  * Since: 3.5.0  | 
2645  |  |  *  | 
2646  |  |  **/  | 
2647  |  | void gnutls_x509_privkey_set_flags(gnutls_x509_privkey_t key,  | 
2648  |  |            unsigned int flags)  | 
2649  | 0  | { | 
2650  | 0  |   key->flags |= flags;  | 
2651  | 0  | }  |