/src/openssl31/crypto/evp/p_lib.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 1995-2024 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
5  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
6  |  |  * in the file LICENSE in the source distribution or at  | 
7  |  |  * https://www.openssl.org/source/license.html  | 
8  |  |  */  | 
9  |  |  | 
10  |  | /*  | 
11  |  |  * DSA low level APIs are deprecated for public use, but still ok for  | 
12  |  |  * internal use.  | 
13  |  |  */  | 
14  |  | #include "internal/deprecated.h"  | 
15  |  |  | 
16  |  | #include <assert.h>  | 
17  |  | #include <stdio.h>  | 
18  |  | #include "internal/cryptlib.h"  | 
19  |  | #include "internal/refcount.h"  | 
20  |  | #include "internal/namemap.h"  | 
21  |  | #include <openssl/bn.h>  | 
22  |  | #include <openssl/err.h>  | 
23  |  | #include <openssl/objects.h>  | 
24  |  | #include <openssl/evp.h>  | 
25  |  | #include <openssl/rsa.h>  | 
26  |  | #include <openssl/dsa.h>  | 
27  |  | #include <openssl/dh.h>  | 
28  |  | #include <openssl/ec.h>  | 
29  |  | #include <openssl/cmac.h>  | 
30  |  | #ifndef FIPS_MODULE  | 
31  |  | # include <openssl/engine.h>  | 
32  |  | #endif  | 
33  |  | #include <openssl/params.h>  | 
34  |  | #include <openssl/param_build.h>  | 
35  |  | #include <openssl/encoder.h>  | 
36  |  | #include <openssl/core_names.h>  | 
37  |  |  | 
38  |  | #include "internal/numbers.h"   /* includes SIZE_MAX */  | 
39  |  | #include "internal/ffc.h"  | 
40  |  | #include "crypto/evp.h"  | 
41  |  | #include "crypto/dh.h"  | 
42  |  | #include "crypto/dsa.h"  | 
43  |  | #include "crypto/ec.h"  | 
44  |  | #include "crypto/ecx.h"  | 
45  |  | #include "crypto/rsa.h"  | 
46  |  | #ifndef FIPS_MODULE  | 
47  |  | # include "crypto/asn1.h"  | 
48  |  | # include "crypto/x509.h"  | 
49  |  | #endif  | 
50  |  | #include "internal/provider.h"  | 
51  |  | #include "evp_local.h"  | 
52  |  |  | 
53  |  | static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,  | 
54  |  |                          int len, EVP_KEYMGMT *keymgmt);  | 
55  |  | static void evp_pkey_free_it(EVP_PKEY *key);  | 
56  |  |  | 
57  |  | #ifndef FIPS_MODULE  | 
58  |  |  | 
59  |  | /* The type of parameters selected in key parameter functions */  | 
60  | 250k  | # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS  | 
61  |  |  | 
62  |  | int EVP_PKEY_get_bits(const EVP_PKEY *pkey)  | 
63  | 6  | { | 
64  | 6  |     int size = 0;  | 
65  |  |  | 
66  | 6  |     if (pkey != NULL) { | 
67  | 6  |         size = pkey->cache.bits;  | 
68  | 6  |         if (pkey->ameth != NULL && pkey->ameth->pkey_bits != NULL)  | 
69  | 0  |             size = pkey->ameth->pkey_bits(pkey);  | 
70  | 6  |     }  | 
71  | 6  |     return size < 0 ? 0 : size;  | 
72  | 6  | }  | 
73  |  |  | 
74  |  | int EVP_PKEY_get_security_bits(const EVP_PKEY *pkey)  | 
75  | 37.1k  | { | 
76  | 37.1k  |     int size = 0;  | 
77  |  |  | 
78  | 37.1k  |     if (pkey != NULL) { | 
79  | 37.1k  |         size = pkey->cache.security_bits;  | 
80  | 37.1k  |         if (pkey->ameth != NULL && pkey->ameth->pkey_security_bits != NULL)  | 
81  | 0  |             size = pkey->ameth->pkey_security_bits(pkey);  | 
82  | 37.1k  |     }  | 
83  | 37.1k  |     return size < 0 ? 0 : size;  | 
84  | 37.1k  | }  | 
85  |  |  | 
86  |  | int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)  | 
87  | 0  | { | 
88  | 0  | # ifndef OPENSSL_NO_DSA  | 
89  | 0  |     if (pkey->type == EVP_PKEY_DSA) { | 
90  | 0  |         int ret = pkey->save_parameters;  | 
91  |  | 
  | 
92  | 0  |         if (mode >= 0)  | 
93  | 0  |             pkey->save_parameters = mode;  | 
94  | 0  |         return ret;  | 
95  | 0  |     }  | 
96  | 0  | # endif  | 
97  | 0  | # ifndef OPENSSL_NO_EC  | 
98  | 0  |     if (pkey->type == EVP_PKEY_EC) { | 
99  | 0  |         int ret = pkey->save_parameters;  | 
100  |  | 
  | 
101  | 0  |         if (mode >= 0)  | 
102  | 0  |             pkey->save_parameters = mode;  | 
103  | 0  |         return ret;  | 
104  | 0  |     }  | 
105  | 0  | # endif  | 
106  | 0  |     return 0;  | 
107  | 0  | }  | 
108  |  |  | 
109  |  | int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg)  | 
110  | 0  | { | 
111  | 0  |     return CRYPTO_set_ex_data(&key->ex_data, idx, arg);  | 
112  | 0  | }  | 
113  |  |  | 
114  |  | void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx)  | 
115  | 0  | { | 
116  | 0  |     return CRYPTO_get_ex_data(&key->ex_data, idx);  | 
117  | 0  | }  | 
118  |  |  | 
119  |  | int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)  | 
120  | 91.6k  | { | 
121  |  |     /*  | 
122  |  |      * Clean up legacy stuff from this function when legacy support is gone.  | 
123  |  |      */  | 
124  |  |  | 
125  | 91.6k  |     EVP_PKEY *downgraded_from = NULL;  | 
126  | 91.6k  |     int ok = 0;  | 
127  |  |  | 
128  |  |     /*  | 
129  |  |      * If |to| is a legacy key and |from| isn't, we must make a downgraded  | 
130  |  |      * copy of |from|.  If that fails, this function fails.  | 
131  |  |      */  | 
132  | 91.6k  |     if (evp_pkey_is_legacy(to) && evp_pkey_is_provided(from)) { | 
133  | 0  |         if (!evp_pkey_copy_downgraded(&downgraded_from, from))  | 
134  | 0  |             goto end;  | 
135  | 0  |         from = downgraded_from;  | 
136  | 0  |     }  | 
137  |  |  | 
138  |  |     /*  | 
139  |  |      * Make sure |to| is typed.  Content is less important at this early  | 
140  |  |      * stage.  | 
141  |  |      *  | 
142  |  |      * 1.  If |to| is untyped, assign |from|'s key type to it.  | 
143  |  |      * 2.  If |to| contains a legacy key, compare its |type| to |from|'s.  | 
144  |  |      *     (|from| was already downgraded above)  | 
145  |  |      *  | 
146  |  |      * If |to| is a provided key, there's nothing more to do here, functions  | 
147  |  |      * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called  | 
148  |  |      * further down help us find out if they are the same or not.  | 
149  |  |      */  | 
150  | 91.6k  |     if (evp_pkey_is_blank(to)) { | 
151  | 15.3k  |         if (evp_pkey_is_legacy(from)) { | 
152  | 0  |             if (EVP_PKEY_set_type(to, from->type) == 0)  | 
153  | 0  |                 goto end;  | 
154  | 15.3k  |         } else { | 
155  | 15.3k  |             if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0)  | 
156  | 0  |                 goto end;  | 
157  | 15.3k  |         }  | 
158  | 76.3k  |     } else if (evp_pkey_is_legacy(to)) { | 
159  | 0  |         if (to->type != from->type) { | 
160  | 0  |             ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);  | 
161  | 0  |             goto end;  | 
162  | 0  |         }  | 
163  | 0  |     }  | 
164  |  |  | 
165  | 91.6k  |     if (EVP_PKEY_missing_parameters(from)) { | 
166  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_MISSING_PARAMETERS);  | 
167  | 0  |         goto end;  | 
168  | 0  |     }  | 
169  |  |  | 
170  | 91.6k  |     if (!EVP_PKEY_missing_parameters(to)) { | 
171  | 76.3k  |         if (EVP_PKEY_parameters_eq(to, from) == 1)  | 
172  | 76.3k  |             ok = 1;  | 
173  | 0  |         else  | 
174  | 76.3k  |             ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);  | 
175  | 76.3k  |         goto end;  | 
176  | 76.3k  |     }  | 
177  |  |  | 
178  |  |     /* For purely provided keys, we just call the keymgmt utility */  | 
179  | 15.3k  |     if (to->keymgmt != NULL && from->keymgmt != NULL) { | 
180  | 15.3k  |         ok = evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS);  | 
181  | 15.3k  |         goto end;  | 
182  | 15.3k  |     }  | 
183  |  |  | 
184  |  |     /*  | 
185  |  |      * If |to| is provided, we know that |from| is legacy at this point.  | 
186  |  |      * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_dup()  | 
187  |  |      * to copy the appropriate data to |to|'s keydata.  | 
188  |  |      * We cannot override existing data so do it only if there is no keydata  | 
189  |  |      * in |to| yet.  | 
190  |  |      */  | 
191  | 0  |     if (to->keymgmt != NULL && to->keydata == NULL) { | 
192  | 0  |         EVP_KEYMGMT *to_keymgmt = to->keymgmt;  | 
193  | 0  |         void *from_keydata =  | 
194  | 0  |             evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,  | 
195  | 0  |                                         NULL);  | 
196  |  |  | 
197  |  |         /*  | 
198  |  |          * If we get a NULL, it could be an internal error, or it could be  | 
199  |  |          * that there's a key mismatch.  We're pretending the latter...  | 
200  |  |          */  | 
201  | 0  |         if (from_keydata == NULL)  | 
202  | 0  |             ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);  | 
203  | 0  |         else  | 
204  | 0  |             ok = (to->keydata = evp_keymgmt_dup(to->keymgmt,  | 
205  | 0  |                                                 from_keydata,  | 
206  | 0  |                                                 SELECT_PARAMETERS)) != NULL;  | 
207  | 0  |         goto end;  | 
208  | 0  |     }  | 
209  |  |  | 
210  |  |     /* Both keys are legacy */  | 
211  | 0  |     if (from->ameth != NULL && from->ameth->param_copy != NULL)  | 
212  | 0  |         ok = from->ameth->param_copy(to, from);  | 
213  | 91.6k  |  end:  | 
214  | 91.6k  |     EVP_PKEY_free(downgraded_from);  | 
215  | 91.6k  |     return ok;  | 
216  | 0  | }  | 
217  |  |  | 
218  |  | int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)  | 
219  | 120k  | { | 
220  | 120k  |     if (pkey != NULL) { | 
221  | 120k  |         if (pkey->keymgmt != NULL)  | 
222  | 74.4k  |             return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);  | 
223  | 46.3k  |         else if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)  | 
224  | 30.9k  |             return pkey->ameth->param_missing(pkey);  | 
225  | 120k  |     }  | 
226  | 15.4k  |     return 0;  | 
227  | 120k  | }  | 
228  |  |  | 
229  |  | /*  | 
230  |  |  * This function is called for any mixture of keys except pure legacy pair.  | 
231  |  |  * When legacy keys are gone, we replace a call to this functions with  | 
232  |  |  * a call to evp_keymgmt_util_match().  | 
233  |  |  */  | 
234  |  | static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,  | 
235  |  |                             int selection)  | 
236  | 161k  | { | 
237  | 161k  |     EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;  | 
238  | 161k  |     void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;  | 
239  |  |  | 
240  |  |     /* If none of them are provided, this function shouldn't have been called */  | 
241  | 161k  |     if (!ossl_assert(evp_pkey_is_provided(a) || evp_pkey_is_provided(b)))  | 
242  | 0  |         return -2;  | 
243  |  |  | 
244  |  |     /* For purely provided keys, we just call the keymgmt utility */  | 
245  | 161k  |     if (evp_pkey_is_provided(a) && evp_pkey_is_provided(b))  | 
246  | 8.48k  |         return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);  | 
247  |  |  | 
248  |  |     /*  | 
249  |  |      * At this point, one of them is provided, the other not.  This allows  | 
250  |  |      * us to compare types using legacy NIDs.  | 
251  |  |      */  | 
252  | 152k  |     if (evp_pkey_is_legacy(a)  | 
253  | 152k  |         && !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type)))  | 
254  | 0  |         return -1;               /* not the same key type */  | 
255  | 152k  |     if (evp_pkey_is_legacy(b)  | 
256  | 152k  |         && !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type)))  | 
257  | 0  |         return -1;               /* not the same key type */  | 
258  |  |  | 
259  |  |     /*  | 
260  |  |      * We've determined that they both are the same keytype, so the next  | 
261  |  |      * step is to do a bit of cross export to ensure we have keydata for  | 
262  |  |      * both keys in the same keymgmt.  | 
263  |  |      */  | 
264  | 152k  |     keymgmt1 = a->keymgmt;  | 
265  | 152k  |     keydata1 = a->keydata;  | 
266  | 152k  |     keymgmt2 = b->keymgmt;  | 
267  | 152k  |     keydata2 = b->keydata;  | 
268  |  |  | 
269  | 152k  |     if (keymgmt2 != NULL && keymgmt2->match != NULL) { | 
270  | 0  |         tmp_keydata =  | 
271  | 0  |             evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);  | 
272  | 0  |         if (tmp_keydata != NULL) { | 
273  | 0  |             keymgmt1 = keymgmt2;  | 
274  | 0  |             keydata1 = tmp_keydata;  | 
275  | 0  |         }  | 
276  | 0  |     }  | 
277  | 152k  |     if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) { | 
278  | 152k  |         tmp_keydata =  | 
279  | 152k  |             evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);  | 
280  | 152k  |         if (tmp_keydata != NULL) { | 
281  | 152k  |             keymgmt2 = keymgmt1;  | 
282  | 152k  |             keydata2 = tmp_keydata;  | 
283  | 152k  |         }  | 
284  | 152k  |     }  | 
285  |  |  | 
286  |  |     /* If we still don't have matching keymgmt implementations, we give up */  | 
287  | 152k  |     if (keymgmt1 != keymgmt2)  | 
288  | 0  |         return -2;  | 
289  |  |  | 
290  |  |     /* If the keymgmt implementations are NULL, the export failed */  | 
291  | 152k  |     if (keymgmt1 == NULL)  | 
292  | 0  |         return -2;  | 
293  |  |  | 
294  | 152k  |     return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);  | 
295  | 152k  | }  | 
296  |  |  | 
297  |  | # ifndef OPENSSL_NO_DEPRECATED_3_0  | 
298  |  | int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)  | 
299  | 0  | { | 
300  | 0  |     return EVP_PKEY_parameters_eq(a, b);  | 
301  | 0  | }  | 
302  |  | #endif  | 
303  |  |  | 
304  |  | int EVP_PKEY_parameters_eq(const EVP_PKEY *a, const EVP_PKEY *b)  | 
305  | 76.3k  | { | 
306  |  |     /*  | 
307  |  |      * This will just call evp_keymgmt_util_match when legacy support  | 
308  |  |      * is gone.  | 
309  |  |      */  | 
310  |  |  | 
311  | 76.3k  |     if (a->keymgmt != NULL || b->keymgmt != NULL)  | 
312  | 76.3k  |         return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);  | 
313  |  |  | 
314  |  |     /* All legacy keys */  | 
315  | 0  |     if (a->type != b->type)  | 
316  | 0  |         return -1;  | 
317  | 0  |     if (a->ameth != NULL && a->ameth->param_cmp != NULL)  | 
318  | 0  |         return a->ameth->param_cmp(a, b);  | 
319  | 0  |     return -2;  | 
320  | 0  | }  | 
321  |  |  | 
322  |  | # ifndef OPENSSL_NO_DEPRECATED_3_0  | 
323  |  | int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)  | 
324  | 0  | { | 
325  | 0  |     return EVP_PKEY_eq(a, b);  | 
326  | 0  | }  | 
327  |  | #endif  | 
328  |  |  | 
329  |  | int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b)  | 
330  | 84.8k  | { | 
331  |  |     /*  | 
332  |  |      * This will just call evp_keymgmt_util_match when legacy support  | 
333  |  |      * is gone.  | 
334  |  |      */  | 
335  |  |  | 
336  |  |     /* Trivial shortcuts */  | 
337  | 84.8k  |     if (a == b)  | 
338  | 41  |         return 1;  | 
339  | 84.7k  |     if (a == NULL || b == NULL)  | 
340  | 0  |         return 0;  | 
341  |  |  | 
342  | 84.7k  |     if (a->keymgmt != NULL || b->keymgmt != NULL) { | 
343  | 84.7k  |         int selection = SELECT_PARAMETERS;  | 
344  |  |  | 
345  | 84.7k  |         if (evp_keymgmt_util_has((EVP_PKEY *)a, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)  | 
346  | 84.7k  |             && evp_keymgmt_util_has((EVP_PKEY *)b, OSSL_KEYMGMT_SELECT_PUBLIC_KEY))  | 
347  | 4.30k  |             selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;  | 
348  | 80.4k  |         else  | 
349  | 80.4k  |             selection |= OSSL_KEYMGMT_SELECT_KEYPAIR;  | 
350  | 84.7k  |         return evp_pkey_cmp_any(a, b, selection);  | 
351  | 84.7k  |     }  | 
352  |  |  | 
353  |  |     /* All legacy keys */  | 
354  | 0  |     if (a->type != b->type)  | 
355  | 0  |         return -1;  | 
356  |  |  | 
357  | 0  |     if (a->ameth != NULL) { | 
358  | 0  |         int ret;  | 
359  |  |         /* Compare parameters if the algorithm has them */  | 
360  | 0  |         if (a->ameth->param_cmp != NULL) { | 
361  | 0  |             ret = a->ameth->param_cmp(a, b);  | 
362  | 0  |             if (ret <= 0)  | 
363  | 0  |                 return ret;  | 
364  | 0  |         }  | 
365  |  |  | 
366  | 0  |         if (a->ameth->pub_cmp != NULL)  | 
367  | 0  |             return a->ameth->pub_cmp(a, b);  | 
368  | 0  |     }  | 
369  |  |  | 
370  | 0  |     return -2;  | 
371  | 0  | }  | 
372  |  |  | 
373  |  |  | 
374  |  | static EVP_PKEY *new_raw_key_int(OSSL_LIB_CTX *libctx,  | 
375  |  |                                  const char *strtype,  | 
376  |  |                                  const char *propq,  | 
377  |  |                                  int nidtype,  | 
378  |  |                                  ENGINE *e,  | 
379  |  |                                  const unsigned char *key,  | 
380  |  |                                  size_t len,  | 
381  |  |                                  int key_is_priv)  | 
382  | 7.17k  | { | 
383  | 7.17k  |     EVP_PKEY *pkey = NULL;  | 
384  | 7.17k  |     EVP_PKEY_CTX *ctx = NULL;  | 
385  | 7.17k  |     const EVP_PKEY_ASN1_METHOD *ameth = NULL;  | 
386  | 7.17k  |     int result = 0;  | 
387  |  |  | 
388  | 7.17k  | # ifndef OPENSSL_NO_ENGINE  | 
389  |  |     /* Check if there is an Engine for this type */  | 
390  | 7.17k  |     if (e == NULL) { | 
391  | 7.17k  |         ENGINE *tmpe = NULL;  | 
392  |  |  | 
393  | 7.17k  |         if (strtype != NULL)  | 
394  | 7.17k  |             ameth = EVP_PKEY_asn1_find_str(&tmpe, strtype, -1);  | 
395  | 2  |         else if (nidtype != EVP_PKEY_NONE)  | 
396  | 0  |             ameth = EVP_PKEY_asn1_find(&tmpe, nidtype);  | 
397  |  |  | 
398  |  |         /* If tmpe is NULL then no engine is claiming to support this type */  | 
399  | 7.17k  |         if (tmpe == NULL)  | 
400  | 7.17k  |             ameth = NULL;  | 
401  |  |  | 
402  | 7.17k  |         ENGINE_finish(tmpe);  | 
403  | 7.17k  |     }  | 
404  | 7.17k  | # endif  | 
405  |  |  | 
406  | 7.17k  |     if (e == NULL && ameth == NULL) { | 
407  |  |         /*  | 
408  |  |          * No engine is claiming to support this type, so lets see if we have  | 
409  |  |          * a provider.  | 
410  |  |          */  | 
411  | 7.17k  |         ctx = EVP_PKEY_CTX_new_from_name(libctx,  | 
412  | 7.17k  |                                          strtype != NULL ? strtype  | 
413  | 7.17k  |                                                          : OBJ_nid2sn(nidtype),  | 
414  | 7.17k  |                                          propq);  | 
415  | 7.17k  |         if (ctx == NULL)  | 
416  | 5  |             goto err;  | 
417  |  |         /* May fail if no provider available */  | 
418  | 7.16k  |         ERR_set_mark();  | 
419  | 7.16k  |         if (EVP_PKEY_fromdata_init(ctx) == 1) { | 
420  | 7.16k  |             OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END }; | 
421  |  |  | 
422  | 7.16k  |             ERR_clear_last_mark();  | 
423  | 7.16k  |             params[0] = OSSL_PARAM_construct_octet_string(  | 
424  | 7.16k  |                             key_is_priv ? OSSL_PKEY_PARAM_PRIV_KEY  | 
425  | 7.16k  |                                         : OSSL_PKEY_PARAM_PUB_KEY,  | 
426  | 7.16k  |                             (void *)key, len);  | 
427  |  |  | 
428  | 7.16k  |             if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) { | 
429  | 103  |                 ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);  | 
430  | 103  |                 goto err;  | 
431  | 103  |             }  | 
432  |  |  | 
433  | 7.06k  |             EVP_PKEY_CTX_free(ctx);  | 
434  |  |  | 
435  | 7.06k  |             return pkey;  | 
436  | 7.16k  |         }  | 
437  | 0  |         ERR_pop_to_mark();  | 
438  |  |         /* else not supported so fallback to legacy */  | 
439  | 0  |     }  | 
440  |  |  | 
441  |  |     /* Legacy code path */  | 
442  |  |  | 
443  | 0  |     pkey = EVP_PKEY_new();  | 
444  | 0  |     if (pkey == NULL) { | 
445  | 0  |         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);  | 
446  | 0  |         goto err;  | 
447  | 0  |     }  | 
448  |  |  | 
449  | 0  |     if (!pkey_set_type(pkey, e, nidtype, strtype, -1, NULL)) { | 
450  |  |         /* ERR_raise(ERR_LIB_EVP, ...) already called */  | 
451  | 0  |         goto err;  | 
452  | 0  |     }  | 
453  |  |  | 
454  | 0  |     if (!ossl_assert(pkey->ameth != NULL))  | 
455  | 0  |         goto err;  | 
456  |  |  | 
457  | 0  |     if (key_is_priv) { | 
458  | 0  |         if (pkey->ameth->set_priv_key == NULL) { | 
459  | 0  |             ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
460  | 0  |             goto err;  | 
461  | 0  |         }  | 
462  |  |  | 
463  | 0  |         if (!pkey->ameth->set_priv_key(pkey, key, len)) { | 
464  | 0  |             ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);  | 
465  | 0  |             goto err;  | 
466  | 0  |         }  | 
467  | 0  |     } else { | 
468  | 0  |         if (pkey->ameth->set_pub_key == NULL) { | 
469  | 0  |             ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
470  | 0  |             goto err;  | 
471  | 0  |         }  | 
472  |  |  | 
473  | 0  |         if (!pkey->ameth->set_pub_key(pkey, key, len)) { | 
474  | 0  |             ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);  | 
475  | 0  |             goto err;  | 
476  | 0  |         }  | 
477  | 0  |     }  | 
478  |  |  | 
479  | 0  |     result = 1;  | 
480  | 108  |  err:  | 
481  | 108  |     if (!result) { | 
482  | 108  |         EVP_PKEY_free(pkey);  | 
483  | 108  |         pkey = NULL;  | 
484  | 108  |     }  | 
485  | 108  |     EVP_PKEY_CTX_free(ctx);  | 
486  | 108  |     return pkey;  | 
487  | 0  | }  | 
488  |  |  | 
489  |  | EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,  | 
490  |  |                                           const char *keytype,  | 
491  |  |                                           const char *propq,  | 
492  |  |                                           const unsigned char *priv, size_t len)  | 
493  | 7.10k  | { | 
494  | 7.10k  |     return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, priv,  | 
495  | 7.10k  |                            len, 1);  | 
496  | 7.10k  | }  | 
497  |  |  | 
498  |  | EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,  | 
499  |  |                                        const unsigned char *priv,  | 
500  |  |                                        size_t len)  | 
501  | 0  | { | 
502  | 0  |     return new_raw_key_int(NULL, NULL, NULL, type, e, priv, len, 1);  | 
503  | 0  | }  | 
504  |  |  | 
505  |  | EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,  | 
506  |  |                                          const char *keytype, const char *propq,  | 
507  |  |                                          const unsigned char *pub, size_t len)  | 
508  | 64  | { | 
509  | 64  |     return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, pub,  | 
510  | 64  |                            len, 0);  | 
511  | 64  | }  | 
512  |  |  | 
513  |  | EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,  | 
514  |  |                                       const unsigned char *pub,  | 
515  |  |                                       size_t len)  | 
516  | 0  | { | 
517  | 0  |     return new_raw_key_int(NULL, NULL, NULL, type, e, pub, len, 0);  | 
518  | 0  | }  | 
519  |  |  | 
520  |  | struct raw_key_details_st  | 
521  |  | { | 
522  |  |     unsigned char **key;  | 
523  |  |     size_t *len;  | 
524  |  |     int selection;  | 
525  |  | };  | 
526  |  |  | 
527  |  | static OSSL_CALLBACK get_raw_key_details;  | 
528  |  | static int get_raw_key_details(const OSSL_PARAM params[], void *arg)  | 
529  | 0  | { | 
530  | 0  |     const OSSL_PARAM *p = NULL;  | 
531  | 0  |     struct raw_key_details_st *raw_key = arg;  | 
532  |  | 
  | 
533  | 0  |     if (raw_key->selection == OSSL_KEYMGMT_SELECT_PRIVATE_KEY) { | 
534  | 0  |         if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY))  | 
535  | 0  |                 != NULL)  | 
536  | 0  |             return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,  | 
537  | 0  |                                                raw_key->key == NULL ? 0 : *raw_key->len,  | 
538  | 0  |                                                raw_key->len);  | 
539  | 0  |     } else if (raw_key->selection == OSSL_KEYMGMT_SELECT_PUBLIC_KEY) { | 
540  | 0  |         if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY))  | 
541  | 0  |                 != NULL)  | 
542  | 0  |             return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,  | 
543  | 0  |                                                raw_key->key == NULL ? 0 : *raw_key->len,  | 
544  | 0  |                                                raw_key->len);  | 
545  | 0  |     }  | 
546  |  |  | 
547  | 0  |     return 0;  | 
548  | 0  | }  | 
549  |  |  | 
550  |  | int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,  | 
551  |  |                                  size_t *len)  | 
552  | 0  | { | 
553  | 0  |     if (pkey->keymgmt != NULL) { | 
554  | 0  |         struct raw_key_details_st raw_key;  | 
555  |  | 
  | 
556  | 0  |         raw_key.key = priv == NULL ? NULL : &priv;  | 
557  | 0  |         raw_key.len = len;  | 
558  | 0  |         raw_key.selection = OSSL_KEYMGMT_SELECT_PRIVATE_KEY;  | 
559  |  | 
  | 
560  | 0  |         return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,  | 
561  | 0  |                                        get_raw_key_details, &raw_key);  | 
562  | 0  |     }  | 
563  |  |  | 
564  | 0  |     if (pkey->ameth == NULL) { | 
565  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
566  | 0  |         return 0;  | 
567  | 0  |     }  | 
568  |  |  | 
569  | 0  |     if (pkey->ameth->get_priv_key == NULL) { | 
570  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
571  | 0  |         return 0;  | 
572  | 0  |     }  | 
573  |  |  | 
574  | 0  |     if (!pkey->ameth->get_priv_key(pkey, priv, len)) { | 
575  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);  | 
576  | 0  |         return 0;  | 
577  | 0  |     }  | 
578  |  |  | 
579  | 0  |     return 1;  | 
580  | 0  | }  | 
581  |  |  | 
582  |  | int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,  | 
583  |  |                                 size_t *len)  | 
584  | 0  | { | 
585  | 0  |     if (pkey->keymgmt != NULL) { | 
586  | 0  |         struct raw_key_details_st raw_key;  | 
587  |  | 
  | 
588  | 0  |         raw_key.key = pub == NULL ? NULL : &pub;  | 
589  | 0  |         raw_key.len = len;  | 
590  | 0  |         raw_key.selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;  | 
591  |  | 
  | 
592  | 0  |         return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,  | 
593  | 0  |                                        get_raw_key_details, &raw_key);  | 
594  | 0  |     }  | 
595  |  |  | 
596  | 0  |     if (pkey->ameth == NULL) { | 
597  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
598  | 0  |         return 0;  | 
599  | 0  |     }  | 
600  |  |  | 
601  | 0  |      if (pkey->ameth->get_pub_key == NULL) { | 
602  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
603  | 0  |         return 0;  | 
604  | 0  |     }  | 
605  |  |  | 
606  | 0  |     if (!pkey->ameth->get_pub_key(pkey, pub, len)) { | 
607  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);  | 
608  | 0  |         return 0;  | 
609  | 0  |     }  | 
610  |  |  | 
611  | 0  |     return 1;  | 
612  | 0  | }  | 
613  |  |  | 
614  |  | static EVP_PKEY *new_cmac_key_int(const unsigned char *priv, size_t len,  | 
615  |  |                                   const char *cipher_name,  | 
616  |  |                                   const EVP_CIPHER *cipher,  | 
617  |  |                                   OSSL_LIB_CTX *libctx,  | 
618  |  |                                   const char *propq, ENGINE *e)  | 
619  | 0  | { | 
620  | 0  | # ifndef OPENSSL_NO_CMAC  | 
621  | 0  | #  ifndef OPENSSL_NO_ENGINE  | 
622  | 0  |     const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;  | 
623  | 0  | #  endif  | 
624  | 0  |     OSSL_PARAM params[5], *p = params;  | 
625  | 0  |     EVP_PKEY *pkey = NULL;  | 
626  | 0  |     EVP_PKEY_CTX *ctx;  | 
627  |  | 
  | 
628  | 0  |     if (cipher != NULL)  | 
629  | 0  |         cipher_name = EVP_CIPHER_get0_name(cipher);  | 
630  |  | 
  | 
631  | 0  |     if (cipher_name == NULL) { | 
632  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);  | 
633  | 0  |         return NULL;  | 
634  | 0  |     }  | 
635  |  |  | 
636  | 0  |     ctx = EVP_PKEY_CTX_new_from_name(libctx, "CMAC", propq);  | 
637  | 0  |     if (ctx == NULL)  | 
638  | 0  |         goto err;  | 
639  |  |  | 
640  | 0  |     if (EVP_PKEY_fromdata_init(ctx) <= 0) { | 
641  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);  | 
642  | 0  |         goto err;  | 
643  | 0  |     }  | 
644  |  |  | 
645  | 0  |     *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,  | 
646  | 0  |                                             (void *)priv, len);  | 
647  | 0  |     *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER,  | 
648  | 0  |                                             (char *)cipher_name, 0);  | 
649  | 0  |     if (propq != NULL)  | 
650  | 0  |         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES,  | 
651  | 0  |                                                 (char *)propq, 0);  | 
652  | 0  | #  ifndef OPENSSL_NO_ENGINE  | 
653  | 0  |     if (engine_id != NULL)  | 
654  | 0  |         *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_ENGINE,  | 
655  | 0  |                                                 (char *)engine_id, 0);  | 
656  | 0  | #  endif  | 
657  | 0  |     *p = OSSL_PARAM_construct_end();  | 
658  |  | 
  | 
659  | 0  |     if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0) { | 
660  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);  | 
661  | 0  |         goto err;  | 
662  | 0  |     }  | 
663  |  |  | 
664  | 0  |  err:  | 
665  | 0  |     EVP_PKEY_CTX_free(ctx);  | 
666  |  | 
  | 
667  | 0  |     return pkey;  | 
668  |  | # else  | 
669  |  |     ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);  | 
670  |  |     return NULL;  | 
671  |  | # endif  | 
672  | 0  | }  | 
673  |  |  | 
674  |  | EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,  | 
675  |  |                                 size_t len, const EVP_CIPHER *cipher)  | 
676  | 0  | { | 
677  | 0  |     return new_cmac_key_int(priv, len, NULL, cipher, NULL, NULL, e);  | 
678  | 0  | }  | 
679  |  |  | 
680  |  | int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)  | 
681  | 3.36M  | { | 
682  | 3.36M  |     return pkey_set_type(pkey, NULL, type, NULL, -1, NULL);  | 
683  | 3.36M  | }  | 
684  |  |  | 
685  |  | int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)  | 
686  | 0  | { | 
687  | 0  |     return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL);  | 
688  | 0  | }  | 
689  |  |  | 
690  |  | # ifndef OPENSSL_NO_ENGINE  | 
691  |  | int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)  | 
692  | 0  | { | 
693  | 0  |     if (e != NULL) { | 
694  | 0  |         if (!ENGINE_init(e)) { | 
695  | 0  |             ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);  | 
696  | 0  |             return 0;  | 
697  | 0  |         }  | 
698  | 0  |         if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) { | 
699  | 0  |             ENGINE_finish(e);  | 
700  | 0  |             ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);  | 
701  | 0  |             return 0;  | 
702  | 0  |         }  | 
703  | 0  |     }  | 
704  | 0  |     ENGINE_finish(pkey->pmeth_engine);  | 
705  | 0  |     pkey->pmeth_engine = e;  | 
706  | 0  |     return 1;  | 
707  | 0  | }  | 
708  |  |  | 
709  |  | ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)  | 
710  | 0  | { | 
711  | 0  |     return pkey->engine;  | 
712  | 0  | }  | 
713  |  | # endif  | 
714  |  |  | 
715  |  | # ifndef OPENSSL_NO_DEPRECATED_3_0  | 
716  |  | static void detect_foreign_key(EVP_PKEY *pkey)  | 
717  | 527k  | { | 
718  | 527k  |     switch (pkey->type) { | 
719  | 87.7k  |     case EVP_PKEY_RSA:  | 
720  | 127k  |     case EVP_PKEY_RSA_PSS:  | 
721  | 127k  |         pkey->foreign = pkey->pkey.rsa != NULL  | 
722  | 127k  |                         && ossl_rsa_is_foreign(pkey->pkey.rsa);  | 
723  | 127k  |         break;  | 
724  | 0  | #  ifndef OPENSSL_NO_EC  | 
725  | 7.56k  |     case EVP_PKEY_SM2:  | 
726  | 7.56k  |         break;  | 
727  | 268k  |     case EVP_PKEY_EC:  | 
728  | 268k  |         pkey->foreign = pkey->pkey.ec != NULL  | 
729  | 268k  |                         && ossl_ec_key_is_foreign(pkey->pkey.ec);  | 
730  | 268k  |         break;  | 
731  | 0  | #  endif  | 
732  | 0  | #  ifndef OPENSSL_NO_DSA  | 
733  | 89.4k  |     case EVP_PKEY_DSA:  | 
734  | 89.4k  |         pkey->foreign = pkey->pkey.dsa != NULL  | 
735  | 89.4k  |                         && ossl_dsa_is_foreign(pkey->pkey.dsa);  | 
736  | 89.4k  |         break;  | 
737  | 0  | #endif  | 
738  | 0  | #  ifndef OPENSSL_NO_DH  | 
739  | 13.3k  |     case EVP_PKEY_DH:  | 
740  | 13.3k  |         pkey->foreign = pkey->pkey.dh != NULL  | 
741  | 13.3k  |                         && ossl_dh_is_foreign(pkey->pkey.dh);  | 
742  | 13.3k  |         break;  | 
743  | 0  | #endif  | 
744  | 21.5k  |     default:  | 
745  | 21.5k  |         pkey->foreign = 0;  | 
746  | 21.5k  |         break;  | 
747  | 527k  |     }  | 
748  | 527k  | }  | 
749  |  |  | 
750  |  | int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)  | 
751  | 527k  | { | 
752  | 527k  | #  ifndef OPENSSL_NO_EC  | 
753  | 527k  |     int pktype;  | 
754  |  |  | 
755  | 527k  |     pktype = EVP_PKEY_type(type);  | 
756  | 527k  |     if ((key != NULL) && (pktype == EVP_PKEY_EC || pktype == EVP_PKEY_SM2)) { | 
757  | 276k  |         const EC_GROUP *group = EC_KEY_get0_group(key);  | 
758  |  |  | 
759  | 276k  |         if (group != NULL) { | 
760  | 276k  |             int curve = EC_GROUP_get_curve_name(group);  | 
761  |  |  | 
762  |  |             /*  | 
763  |  |              * Regardless of what is requested the SM2 curve must be SM2 type,  | 
764  |  |              * and non SM2 curves are EC type.  | 
765  |  |              */  | 
766  | 276k  |             if (curve == NID_sm2 && pktype == EVP_PKEY_EC)  | 
767  | 7.56k  |                 type = EVP_PKEY_SM2;  | 
768  | 268k  |             else if(curve != NID_sm2 && pktype == EVP_PKEY_SM2)  | 
769  | 0  |                 type = EVP_PKEY_EC;  | 
770  | 276k  |         }  | 
771  | 276k  |     }  | 
772  | 527k  | #  endif  | 
773  |  |  | 
774  | 527k  |     if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))  | 
775  | 0  |         return 0;  | 
776  |  |  | 
777  | 527k  |     pkey->pkey.ptr = key;  | 
778  | 527k  |     detect_foreign_key(pkey);  | 
779  |  |  | 
780  | 527k  |     return (key != NULL);  | 
781  | 527k  | }  | 
782  |  | # endif  | 
783  |  |  | 
784  |  | void *EVP_PKEY_get0(const EVP_PKEY *pkey)  | 
785  | 0  | { | 
786  | 0  |     if (pkey == NULL)  | 
787  | 0  |         return NULL;  | 
788  |  |  | 
789  | 0  |     if (!evp_pkey_is_provided(pkey))  | 
790  | 0  |         return pkey->pkey.ptr;  | 
791  |  |  | 
792  | 0  |     return NULL;  | 
793  | 0  | }  | 
794  |  |  | 
795  |  | const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)  | 
796  | 0  | { | 
797  | 0  |     const ASN1_OCTET_STRING *os = NULL;  | 
798  | 0  |     if (pkey->type != EVP_PKEY_HMAC) { | 
799  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_HMAC_KEY);  | 
800  | 0  |         return NULL;  | 
801  | 0  |     }  | 
802  | 0  |     os = evp_pkey_get_legacy((EVP_PKEY *)pkey);  | 
803  | 0  |     if (os != NULL) { | 
804  | 0  |         *len = os->length;  | 
805  | 0  |         return os->data;  | 
806  | 0  |     }  | 
807  | 0  |     return NULL;  | 
808  | 0  | }  | 
809  |  |  | 
810  |  | # ifndef OPENSSL_NO_POLY1305  | 
811  |  | const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)  | 
812  | 0  | { | 
813  | 0  |     const ASN1_OCTET_STRING *os = NULL;  | 
814  | 0  |     if (pkey->type != EVP_PKEY_POLY1305) { | 
815  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_POLY1305_KEY);  | 
816  | 0  |         return NULL;  | 
817  | 0  |     }  | 
818  | 0  |     os = evp_pkey_get_legacy((EVP_PKEY *)pkey);  | 
819  | 0  |     if (os != NULL) { | 
820  | 0  |         *len = os->length;  | 
821  | 0  |         return os->data;  | 
822  | 0  |     }  | 
823  | 0  |     return NULL;  | 
824  | 0  | }  | 
825  |  | # endif  | 
826  |  |  | 
827  |  | # ifndef OPENSSL_NO_SIPHASH  | 
828  |  | const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)  | 
829  | 0  | { | 
830  | 0  |     const ASN1_OCTET_STRING *os = NULL;  | 
831  |  | 
  | 
832  | 0  |     if (pkey->type != EVP_PKEY_SIPHASH) { | 
833  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_SIPHASH_KEY);  | 
834  | 0  |         return NULL;  | 
835  | 0  |     }  | 
836  | 0  |     os = evp_pkey_get_legacy((EVP_PKEY *)pkey);  | 
837  | 0  |     if (os != NULL) { | 
838  | 0  |         *len = os->length;  | 
839  | 0  |         return os->data;  | 
840  | 0  |     }  | 
841  | 0  |     return NULL;  | 
842  | 0  | }  | 
843  |  | # endif  | 
844  |  |  | 
845  |  | # ifndef OPENSSL_NO_DSA  | 
846  |  | static DSA *evp_pkey_get0_DSA_int(const EVP_PKEY *pkey)  | 
847  | 63.6k  | { | 
848  | 63.6k  |     if (pkey->type != EVP_PKEY_DSA) { | 
849  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DSA_KEY);  | 
850  | 0  |         return NULL;  | 
851  | 0  |     }  | 
852  | 63.6k  |     return evp_pkey_get_legacy((EVP_PKEY *)pkey);  | 
853  | 63.6k  | }  | 
854  |  |  | 
855  |  | const DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)  | 
856  | 0  | { | 
857  | 0  |     return evp_pkey_get0_DSA_int(pkey);  | 
858  | 0  | }  | 
859  |  |  | 
860  |  | int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)  | 
861  | 0  | { | 
862  | 0  |     int ret = EVP_PKEY_assign_DSA(pkey, key);  | 
863  | 0  |     if (ret)  | 
864  | 0  |         DSA_up_ref(key);  | 
865  | 0  |     return ret;  | 
866  | 0  | }  | 
867  |  | DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)  | 
868  | 41.5k  | { | 
869  | 41.5k  |     DSA *ret = evp_pkey_get0_DSA_int(pkey);  | 
870  |  |  | 
871  | 41.5k  |     if (ret != NULL)  | 
872  | 41.5k  |         DSA_up_ref(ret);  | 
873  | 41.5k  |     return ret;  | 
874  | 41.5k  | }  | 
875  |  | # endif /*  OPENSSL_NO_DSA */  | 
876  |  |  | 
877  |  | # ifndef OPENSSL_NO_EC  | 
878  |  | static const ECX_KEY *evp_pkey_get0_ECX_KEY(const EVP_PKEY *pkey, int type)  | 
879  | 3.70k  | { | 
880  | 3.70k  |     if (EVP_PKEY_get_base_id(pkey) != type) { | 
881  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_ECX_KEY);  | 
882  | 0  |         return NULL;  | 
883  | 0  |     }  | 
884  | 3.70k  |     return evp_pkey_get_legacy((EVP_PKEY *)pkey);  | 
885  | 3.70k  | }  | 
886  |  |  | 
887  |  | static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type)  | 
888  | 3.70k  | { | 
889  | 3.70k  |     ECX_KEY *ret = (ECX_KEY *)evp_pkey_get0_ECX_KEY(pkey, type);  | 
890  |  |  | 
891  | 3.70k  |     if (ret != NULL && !ossl_ecx_key_up_ref(ret))  | 
892  | 0  |         ret = NULL;  | 
893  | 3.70k  |     return ret;  | 
894  | 3.70k  | }  | 
895  |  |  | 
896  |  | #  define IMPLEMENT_ECX_VARIANT(NAME)                                   \  | 
897  |  |     ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey)                  \  | 
898  | 3.70k  |     {                                                                   \ | 
899  | 3.70k  |         return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME);            \  | 
900  | 3.70k  |     } ossl_evp_pkey_get1_X25519 Line  | Count  | Source  |  898  | 247  |     {                                                                   \ |  899  | 247  |         return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME);            \  |  900  | 247  |     }  |  
 Line  | Count  | Source  |  898  | 2.66k  |     {                                                                   \ |  899  | 2.66k  |         return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME);            \  |  900  | 2.66k  |     }  |  
 ossl_evp_pkey_get1_ED25519 Line  | Count  | Source  |  898  | 291  |     {                                                                   \ |  899  | 291  |         return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME);            \  |  900  | 291  |     }  |  
 Line  | Count  | Source  |  898  | 505  |     {                                                                   \ |  899  | 505  |         return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME);            \  |  900  | 505  |     }  |  
  | 
901  |  | IMPLEMENT_ECX_VARIANT(X25519)  | 
902  |  | IMPLEMENT_ECX_VARIANT(X448)  | 
903  |  | IMPLEMENT_ECX_VARIANT(ED25519)  | 
904  |  | IMPLEMENT_ECX_VARIANT(ED448)  | 
905  |  |  | 
906  |  | # endif  | 
907  |  |  | 
908  |  | # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)  | 
909  |  |  | 
910  |  | int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *dhkey)  | 
911  | 0  | { | 
912  | 0  |     int ret, type;  | 
913  |  |  | 
914  |  |     /*  | 
915  |  |      * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups  | 
916  |  |      * related to ffdhe and modp (which cache q = (p - 1) / 2),  | 
917  |  |      * and returns 0 for all other dh parameter generation types including  | 
918  |  |      * RFC5114 named groups.  | 
919  |  |      *  | 
920  |  |      * The EVP_PKEY_DH type is used for dh parameter generation types:  | 
921  |  |      *  - named safe prime groups related to ffdhe and modp  | 
922  |  |      *  - safe prime generator  | 
923  |  |      *  | 
924  |  |      * The type EVP_PKEY_DHX is used for dh parameter generation types  | 
925  |  |      *  - fips186-4 and fips186-2  | 
926  |  |      *  - rfc5114 named groups.  | 
927  |  |      *  | 
928  |  |      * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored  | 
929  |  |      * without a q value.  | 
930  |  |      * The EVP_PKEY_DHX type is used to save X9.42 data that requires the  | 
931  |  |      * q value to be stored.  | 
932  |  |      */  | 
933  | 0  |     if (ossl_dh_is_named_safe_prime_group(dhkey))  | 
934  | 0  |         type = EVP_PKEY_DH;  | 
935  | 0  |     else  | 
936  | 0  |         type = DH_get0_q(dhkey) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;  | 
937  |  | 
  | 
938  | 0  |     ret = EVP_PKEY_assign(pkey, type, dhkey);  | 
939  |  | 
  | 
940  | 0  |     if (ret)  | 
941  | 0  |         DH_up_ref(dhkey);  | 
942  | 0  |     return ret;  | 
943  | 0  | }  | 
944  |  |  | 
945  |  | DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey)  | 
946  | 31.1k  | { | 
947  | 31.1k  |     if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) { | 
948  | 0  |         ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DH_KEY);  | 
949  | 0  |         return NULL;  | 
950  | 0  |     }  | 
951  | 31.1k  |     return evp_pkey_get_legacy((EVP_PKEY *)pkey);  | 
952  | 31.1k  | }  | 
953  |  |  | 
954  |  | const DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)  | 
955  | 0  | { | 
956  | 0  |     return evp_pkey_get0_DH_int(pkey);  | 
957  | 0  | }  | 
958  |  |  | 
959  |  | DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)  | 
960  | 27.3k  | { | 
961  | 27.3k  |     DH *ret = evp_pkey_get0_DH_int(pkey);  | 
962  |  |  | 
963  | 27.3k  |     if (ret != NULL)  | 
964  | 27.3k  |         DH_up_ref(ret);  | 
965  | 27.3k  |     return ret;  | 
966  | 27.3k  | }  | 
967  |  | # endif  | 
968  |  |  | 
969  |  | int EVP_PKEY_type(int type)  | 
970  | 3.20M  | { | 
971  | 3.20M  |     int ret;  | 
972  | 3.20M  |     const EVP_PKEY_ASN1_METHOD *ameth;  | 
973  | 3.20M  |     ENGINE *e;  | 
974  | 3.20M  |     ameth = EVP_PKEY_asn1_find(&e, type);  | 
975  | 3.20M  |     if (ameth)  | 
976  | 2.04M  |         ret = ameth->pkey_id;  | 
977  | 1.15M  |     else  | 
978  | 1.15M  |         ret = NID_undef;  | 
979  | 3.20M  | # ifndef OPENSSL_NO_ENGINE  | 
980  | 3.20M  |     ENGINE_finish(e);  | 
981  | 3.20M  | # endif  | 
982  | 3.20M  |     return ret;  | 
983  | 3.20M  | }  | 
984  |  |  | 
985  |  | int EVP_PKEY_get_id(const EVP_PKEY *pkey)  | 
986  | 288k  | { | 
987  | 288k  |     return pkey->type;  | 
988  | 288k  | }  | 
989  |  |  | 
990  |  | int EVP_PKEY_get_base_id(const EVP_PKEY *pkey)  | 
991  | 258k  | { | 
992  | 258k  |     return EVP_PKEY_type(pkey->type);  | 
993  | 258k  | }  | 
994  |  |  | 
995  |  | /*  | 
996  |  |  * These hard coded cases are pure hackery to get around the fact  | 
997  |  |  * that names in crypto/objects/objects.txt are a mess.  There is  | 
998  |  |  * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's  | 
999  |  |  * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1, | 
1000  |  |  * the NID of which is used for EVP_PKEY_RSA.  Strangely enough,  | 
1001  |  |  * "DSA" is accurate...  but still, better be safe and hard-code  | 
1002  |  |  * names that we know.  | 
1003  |  |  * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in  | 
1004  |  |  * EVP_PKEY_EC, because of aliasing.  | 
1005  |  |  * This should be cleaned away along with all other #legacy support.  | 
1006  |  |  */  | 
1007  |  | static const OSSL_ITEM standard_name2type[] = { | 
1008  |  |     { EVP_PKEY_RSA,     "RSA" }, | 
1009  |  |     { EVP_PKEY_RSA_PSS, "RSA-PSS" }, | 
1010  |  |     { EVP_PKEY_EC,      "EC" }, | 
1011  |  |     { EVP_PKEY_ED25519, "ED25519" }, | 
1012  |  |     { EVP_PKEY_ED448,   "ED448" }, | 
1013  |  |     { EVP_PKEY_X25519,  "X25519" }, | 
1014  |  |     { EVP_PKEY_X448,    "X448" }, | 
1015  |  |     { EVP_PKEY_SM2,     "SM2" }, | 
1016  |  |     { EVP_PKEY_DH,      "DH" }, | 
1017  |  |     { EVP_PKEY_DHX,     "X9.42 DH" }, | 
1018  |  |     { EVP_PKEY_DHX,     "DHX" }, | 
1019  |  |     { EVP_PKEY_DSA,     "DSA" }, | 
1020  |  | };  | 
1021  |  |  | 
1022  |  | int evp_pkey_name2type(const char *name)  | 
1023  | 2.33M  | { | 
1024  | 2.33M  |     int type;  | 
1025  | 2.33M  |     size_t i;  | 
1026  |  |  | 
1027  | 26.2M  |     for (i = 0; i < OSSL_NELEM(standard_name2type); i++) { | 
1028  | 24.4M  |         if (OPENSSL_strcasecmp(name, standard_name2type[i].ptr) == 0)  | 
1029  | 592k  |             return (int)standard_name2type[i].id;  | 
1030  | 24.4M  |     }  | 
1031  |  |  | 
1032  | 1.73M  |     if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef)  | 
1033  | 1.05M  |         return type;  | 
1034  | 679k  |     return EVP_PKEY_type(OBJ_ln2nid(name));  | 
1035  | 1.73M  | }  | 
1036  |  |  | 
1037  |  | const char *evp_pkey_type2name(int type)  | 
1038  | 0  | { | 
1039  | 0  |     size_t i;  | 
1040  |  | 
  | 
1041  | 0  |     for (i = 0; i < OSSL_NELEM(standard_name2type); i++) { | 
1042  | 0  |         if (type == (int)standard_name2type[i].id)  | 
1043  | 0  |             return standard_name2type[i].ptr;  | 
1044  | 0  |     }  | 
1045  |  |  | 
1046  | 0  |     return OBJ_nid2sn(type);  | 
1047  | 0  | }  | 
1048  |  |  | 
1049  |  | int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)  | 
1050  | 909k  | { | 
1051  | 909k  |     if (pkey == NULL)  | 
1052  | 0  |         return 0;  | 
1053  | 909k  |     if (pkey->keymgmt == NULL)  | 
1054  | 330k  |         return pkey->type == evp_pkey_name2type(name);  | 
1055  | 578k  |     return EVP_KEYMGMT_is_a(pkey->keymgmt, name);  | 
1056  | 909k  | }  | 
1057  |  |  | 
1058  |  | int EVP_PKEY_type_names_do_all(const EVP_PKEY *pkey,  | 
1059  |  |                                void (*fn)(const char *name, void *data),  | 
1060  |  |                                void *data)  | 
1061  | 0  | { | 
1062  | 0  |     if (!evp_pkey_is_typed(pkey))  | 
1063  | 0  |         return 0;  | 
1064  |  |  | 
1065  | 0  |     if (!evp_pkey_is_provided(pkey)) { | 
1066  | 0  |         const char *name = OBJ_nid2sn(EVP_PKEY_get_id(pkey));  | 
1067  |  | 
  | 
1068  | 0  |         fn(name, data);  | 
1069  | 0  |         return 1;  | 
1070  | 0  |     }  | 
1071  | 0  |     return EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data);  | 
1072  | 0  | }  | 
1073  |  |  | 
1074  |  | int EVP_PKEY_can_sign(const EVP_PKEY *pkey)  | 
1075  | 25.4k  | { | 
1076  | 25.4k  |     if (pkey->keymgmt == NULL) { | 
1077  | 0  |         switch (EVP_PKEY_get_base_id(pkey)) { | 
1078  | 0  |         case EVP_PKEY_RSA:  | 
1079  | 0  |         case EVP_PKEY_RSA_PSS:  | 
1080  | 0  |             return 1;  | 
1081  | 0  | # ifndef OPENSSL_NO_DSA  | 
1082  | 0  |         case EVP_PKEY_DSA:  | 
1083  | 0  |             return 1;  | 
1084  | 0  | # endif  | 
1085  | 0  | # ifndef OPENSSL_NO_EC  | 
1086  | 0  |         case EVP_PKEY_ED25519:  | 
1087  | 0  |         case EVP_PKEY_ED448:  | 
1088  | 0  |             return 1;  | 
1089  | 0  |         case EVP_PKEY_EC:        /* Including SM2 */  | 
1090  | 0  |             return EC_KEY_can_sign(pkey->pkey.ec);  | 
1091  | 0  | # endif  | 
1092  | 0  |         default:  | 
1093  | 0  |             break;  | 
1094  | 0  |         }  | 
1095  | 25.4k  |     } else { | 
1096  | 25.4k  |         const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);  | 
1097  | 25.4k  |         OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);  | 
1098  | 25.4k  |         const char *supported_sig =  | 
1099  | 25.4k  |             pkey->keymgmt->query_operation_name != NULL  | 
1100  | 25.4k  |             ? pkey->keymgmt->query_operation_name(OSSL_OP_SIGNATURE)  | 
1101  | 25.4k  |             : EVP_KEYMGMT_get0_name(pkey->keymgmt);  | 
1102  | 25.4k  |         EVP_SIGNATURE *signature = NULL;  | 
1103  |  |  | 
1104  | 25.4k  |         signature = EVP_SIGNATURE_fetch(libctx, supported_sig, NULL);  | 
1105  | 25.4k  |         if (signature != NULL) { | 
1106  | 25.4k  |             EVP_SIGNATURE_free(signature);  | 
1107  | 25.4k  |             return 1;  | 
1108  | 25.4k  |         }  | 
1109  | 25.4k  |     }  | 
1110  | 0  |     return 0;  | 
1111  | 25.4k  | }  | 
1112  |  |  | 
1113  |  | static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)  | 
1114  | 37.9k  | { | 
1115  | 37.9k  |     BIO_set_indent(*out, saved_indent);  | 
1116  | 37.9k  |     if (pop_f_prefix) { | 
1117  | 31.3k  |         BIO *next = BIO_pop(*out);  | 
1118  |  |  | 
1119  | 31.3k  |         BIO_free(*out);  | 
1120  | 31.3k  |         *out = next;  | 
1121  | 31.3k  |     }  | 
1122  | 37.9k  |     return 1;  | 
1123  | 37.9k  | }  | 
1124  |  |  | 
1125  |  | static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,  | 
1126  |  |                             long indent)  | 
1127  | 37.9k  | { | 
1128  | 37.9k  |     *pop_f_prefix = 0;  | 
1129  | 37.9k  |     *saved_indent = 0;  | 
1130  | 37.9k  |     if (indent > 0) { | 
1131  | 31.3k  |         long i = BIO_get_indent(*out);  | 
1132  |  |  | 
1133  | 31.3k  |         *saved_indent =  (i < 0 ? 0 : i);  | 
1134  | 31.3k  |         if (BIO_set_indent(*out, indent) <= 0) { | 
1135  | 31.3k  |             BIO *prefbio = BIO_new(BIO_f_prefix());  | 
1136  |  |  | 
1137  | 31.3k  |             if (prefbio == NULL)  | 
1138  | 0  |                 return 0;  | 
1139  | 31.3k  |             *out = BIO_push(prefbio, *out);  | 
1140  | 31.3k  |             *pop_f_prefix = 1;  | 
1141  | 31.3k  |         }  | 
1142  | 31.3k  |         if (BIO_set_indent(*out, indent) <= 0) { | 
1143  | 0  |             print_reset_indent(out, *pop_f_prefix, *saved_indent);  | 
1144  | 0  |             return 0;  | 
1145  | 0  |         }  | 
1146  | 31.3k  |     }  | 
1147  | 37.9k  |     return 1;  | 
1148  | 37.9k  | }  | 
1149  |  |  | 
1150  |  | static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,  | 
1151  |  |                      const char *kstr)  | 
1152  | 0  | { | 
1153  | 0  |     return BIO_indent(out, indent, 128)  | 
1154  | 0  |         && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",  | 
1155  | 0  |                       kstr, OBJ_nid2ln(pkey->type)) > 0;  | 
1156  | 0  | }  | 
1157  |  |  | 
1158  |  | static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,  | 
1159  |  |                       int selection /* For provided encoding */,  | 
1160  |  |                       const char *propquery /* For provided encoding */,  | 
1161  |  |                       int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,  | 
1162  |  |                                           int indent, ASN1_PCTX *pctx),  | 
1163  |  |                       ASN1_PCTX *legacy_pctx /* For legacy print */)  | 
1164  | 37.9k  | { | 
1165  | 37.9k  |     int pop_f_prefix;  | 
1166  | 37.9k  |     long saved_indent;  | 
1167  | 37.9k  |     OSSL_ENCODER_CTX *ctx = NULL;  | 
1168  | 37.9k  |     int ret = -2;                /* default to unsupported */  | 
1169  |  |  | 
1170  | 37.9k  |     if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))  | 
1171  | 0  |         return 0;  | 
1172  |  |  | 
1173  | 37.9k  |     ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL,  | 
1174  | 37.9k  |                                         propquery);  | 
1175  | 37.9k  |     if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)  | 
1176  | 37.0k  |         ret = OSSL_ENCODER_to_bio(ctx, out);  | 
1177  | 37.9k  |     OSSL_ENCODER_CTX_free(ctx);  | 
1178  |  |  | 
1179  | 37.9k  |     if (ret != -2)  | 
1180  | 37.0k  |         goto end;  | 
1181  |  |  | 
1182  |  |     /* legacy fallback */  | 
1183  | 874  |     if (legacy_print != NULL)  | 
1184  | 874  |         ret = legacy_print(out, pkey, 0, legacy_pctx);  | 
1185  | 0  |     else  | 
1186  | 0  |         ret = unsup_alg(out, pkey, 0, "Public Key");  | 
1187  |  |  | 
1188  | 37.9k  |  end:  | 
1189  | 37.9k  |     print_reset_indent(&out, pop_f_prefix, saved_indent);  | 
1190  | 37.9k  |     return ret;  | 
1191  | 874  | }  | 
1192  |  |  | 
1193  |  | int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,  | 
1194  |  |                           int indent, ASN1_PCTX *pctx)  | 
1195  | 14.8k  | { | 
1196  | 14.8k  |     return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,  | 
1197  | 14.8k  |                       (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),  | 
1198  | 14.8k  |                       pctx);  | 
1199  | 14.8k  | }  | 
1200  |  |  | 
1201  |  | int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,  | 
1202  |  |                            int indent, ASN1_PCTX *pctx)  | 
1203  | 14.8k  | { | 
1204  | 14.8k  |     return print_pkey(pkey, out, indent, EVP_PKEY_PRIVATE_KEY, NULL,  | 
1205  | 14.8k  |                       (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),  | 
1206  | 14.8k  |                       pctx);  | 
1207  | 14.8k  | }  | 
1208  |  |  | 
1209  |  | int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,  | 
1210  |  |                           int indent, ASN1_PCTX *pctx)  | 
1211  | 8.25k  | { | 
1212  | 8.25k  |     return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,  | 
1213  | 8.25k  |                       (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),  | 
1214  | 8.25k  |                       pctx);  | 
1215  | 8.25k  | }  | 
1216  |  |  | 
1217  |  | # ifndef OPENSSL_NO_STDIO  | 
1218  |  | int EVP_PKEY_print_public_fp(FILE *fp, const EVP_PKEY *pkey,  | 
1219  |  |                              int indent, ASN1_PCTX *pctx)  | 
1220  | 0  | { | 
1221  | 0  |     int ret;  | 
1222  | 0  |     BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);  | 
1223  |  | 
  | 
1224  | 0  |     if (b == NULL)  | 
1225  | 0  |         return 0;  | 
1226  | 0  |     ret = EVP_PKEY_print_public(b, pkey, indent, pctx);  | 
1227  | 0  |     BIO_free(b);  | 
1228  | 0  |     return ret;  | 
1229  | 0  | }  | 
1230  |  |  | 
1231  |  | int EVP_PKEY_print_private_fp(FILE *fp, const EVP_PKEY *pkey,  | 
1232  |  |                               int indent, ASN1_PCTX *pctx)  | 
1233  | 0  | { | 
1234  | 0  |     int ret;  | 
1235  | 0  |     BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);  | 
1236  |  | 
  | 
1237  | 0  |     if (b == NULL)  | 
1238  | 0  |         return 0;  | 
1239  | 0  |     ret = EVP_PKEY_print_private(b, pkey, indent, pctx);  | 
1240  | 0  |     BIO_free(b);  | 
1241  | 0  |     return ret;  | 
1242  | 0  | }  | 
1243  |  |  | 
1244  |  | int EVP_PKEY_print_params_fp(FILE *fp, const EVP_PKEY *pkey,  | 
1245  |  |                              int indent, ASN1_PCTX *pctx)  | 
1246  | 0  | { | 
1247  | 0  |     int ret;  | 
1248  | 0  |     BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);  | 
1249  |  | 
  | 
1250  | 0  |     if (b == NULL)  | 
1251  | 0  |         return 0;  | 
1252  | 0  |     ret = EVP_PKEY_print_params(b, pkey, indent, pctx);  | 
1253  | 0  |     BIO_free(b);  | 
1254  | 0  |     return ret;  | 
1255  | 0  | }  | 
1256  |  | # endif  | 
1257  |  |  | 
1258  |  | static void mdname2nid(const char *mdname, void *data)  | 
1259  | 0  | { | 
1260  | 0  |     int *nid = (int *)data;  | 
1261  |  | 
  | 
1262  | 0  |     if (*nid != NID_undef)  | 
1263  | 0  |         return;  | 
1264  |  |  | 
1265  | 0  |     *nid = OBJ_sn2nid(mdname);  | 
1266  | 0  |     if (*nid == NID_undef)  | 
1267  | 0  |         *nid = OBJ_ln2nid(mdname);  | 
1268  | 0  | }  | 
1269  |  |  | 
1270  |  | static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,  | 
1271  |  |                                      int arg1, void *arg2)  | 
1272  | 0  | { | 
1273  | 0  |     if (pkey->keymgmt == NULL)  | 
1274  | 0  |         return 0;  | 
1275  | 0  |     switch (op) { | 
1276  | 0  |     case ASN1_PKEY_CTRL_DEFAULT_MD_NID:  | 
1277  | 0  |         { | 
1278  | 0  |             char mdname[80] = "";  | 
1279  | 0  |             int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,  | 
1280  | 0  |                                                       sizeof(mdname));  | 
1281  |  | 
  | 
1282  | 0  |             if (rv > 0) { | 
1283  | 0  |                 int mdnum;  | 
1284  | 0  |                 OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov);  | 
1285  |  |                 /* Make sure the MD is in the namemap if available */  | 
1286  | 0  |                 EVP_MD *md;  | 
1287  | 0  |                 OSSL_NAMEMAP *namemap;  | 
1288  | 0  |                 int nid = NID_undef;  | 
1289  |  | 
  | 
1290  | 0  |                 (void)ERR_set_mark();  | 
1291  | 0  |                 md = EVP_MD_fetch(libctx, mdname, NULL);  | 
1292  | 0  |                 (void)ERR_pop_to_mark();  | 
1293  | 0  |                 namemap = ossl_namemap_stored(libctx);  | 
1294  |  |  | 
1295  |  |                 /*  | 
1296  |  |                  * The only reason to fetch the MD was to make sure it is in the  | 
1297  |  |                  * namemap. We can immediately free it.  | 
1298  |  |                  */  | 
1299  | 0  |                 EVP_MD_free(md);  | 
1300  | 0  |                 mdnum = ossl_namemap_name2num(namemap, mdname);  | 
1301  | 0  |                 if (mdnum == 0)  | 
1302  | 0  |                     return 0;  | 
1303  |  |  | 
1304  |  |                 /*  | 
1305  |  |                  * We have the namemap number - now we need to find the  | 
1306  |  |                  * associated nid  | 
1307  |  |                  */  | 
1308  | 0  |                 if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid))  | 
1309  | 0  |                     return 0;  | 
1310  | 0  |                 *(int *)arg2 = nid;  | 
1311  | 0  |             }  | 
1312  | 0  |             return rv;  | 
1313  | 0  |         }  | 
1314  | 0  |     default:  | 
1315  | 0  |         return -2;  | 
1316  | 0  |     }  | 
1317  | 0  | }  | 
1318  |  |  | 
1319  |  | static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)  | 
1320  | 0  | { | 
1321  | 0  |     if (pkey->ameth == NULL)  | 
1322  | 0  |         return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);  | 
1323  | 0  |     if (pkey->ameth->pkey_ctrl == NULL)  | 
1324  | 0  |         return -2;  | 
1325  | 0  |     return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);  | 
1326  | 0  | }  | 
1327  |  |  | 
1328  |  | int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)  | 
1329  | 0  | { | 
1330  | 0  |     if (pkey == NULL)  | 
1331  | 0  |         return 0;  | 
1332  | 0  |     return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);  | 
1333  | 0  | }  | 
1334  |  |  | 
1335  |  | int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,  | 
1336  |  |                                      char *mdname, size_t mdname_sz)  | 
1337  | 0  | { | 
1338  | 0  |     if (pkey->ameth == NULL)  | 
1339  | 0  |         return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,  | 
1340  | 0  |                                                       pkey->keydata,  | 
1341  | 0  |                                                       mdname, mdname_sz);  | 
1342  |  |  | 
1343  | 0  |     { | 
1344  | 0  |         int nid = NID_undef;  | 
1345  | 0  |         int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);  | 
1346  | 0  |         const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;  | 
1347  |  | 
  | 
1348  | 0  |         if (rv > 0)  | 
1349  | 0  |             OPENSSL_strlcpy(mdname, name, mdname_sz);  | 
1350  | 0  |         return rv;  | 
1351  | 0  |     }  | 
1352  | 0  | }  | 
1353  |  |  | 
1354  |  | int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz,  | 
1355  |  |                             size_t *gname_len)  | 
1356  | 15.7k  | { | 
1357  | 15.7k  |     return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,  | 
1358  | 15.7k  |                                           gname, gname_sz, gname_len);  | 
1359  | 15.7k  | }  | 
1360  |  |  | 
1361  |  | int EVP_PKEY_digestsign_supports_digest(EVP_PKEY *pkey, OSSL_LIB_CTX *libctx,  | 
1362  |  |                                         const char *name, const char *propq)  | 
1363  | 39.8k  | { | 
1364  | 39.8k  |     int rv;  | 
1365  | 39.8k  |     EVP_MD_CTX *ctx = NULL;  | 
1366  |  |  | 
1367  | 39.8k  |     if ((ctx = EVP_MD_CTX_new()) == NULL)  | 
1368  | 0  |         return -1;  | 
1369  |  |  | 
1370  | 39.8k  |     ERR_set_mark();  | 
1371  | 39.8k  |     rv = EVP_DigestSignInit_ex(ctx, NULL, name, libctx,  | 
1372  | 39.8k  |                                propq, pkey, NULL);  | 
1373  | 39.8k  |     ERR_pop_to_mark();  | 
1374  |  |  | 
1375  | 39.8k  |     EVP_MD_CTX_free(ctx);  | 
1376  | 39.8k  |     return rv;  | 
1377  | 39.8k  | }  | 
1378  |  |  | 
1379  |  | int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub,  | 
1380  |  |                                      size_t publen)  | 
1381  | 24.1k  | { | 
1382  | 24.1k  |     if (pkey == NULL)  | 
1383  | 0  |         return 0;  | 
1384  | 24.1k  |     if (evp_pkey_is_provided(pkey))  | 
1385  | 24.1k  |         return  | 
1386  | 24.1k  |             EVP_PKEY_set_octet_string_param(pkey,  | 
1387  | 24.1k  |                                             OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,  | 
1388  | 24.1k  |                                             (unsigned char *)pub, publen);  | 
1389  |  |  | 
1390  | 0  |     if (publen > INT_MAX)  | 
1391  | 0  |         return 0;  | 
1392  |  |     /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */  | 
1393  | 0  |     if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, publen,  | 
1394  | 0  |                            (void *)pub) <= 0)  | 
1395  | 0  |         return 0;  | 
1396  | 0  |     return 1;  | 
1397  | 0  | }  | 
1398  |  |  | 
1399  |  | size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub)  | 
1400  | 81.7k  | { | 
1401  | 81.7k  |     int rv;  | 
1402  |  |  | 
1403  | 81.7k  |     if (pkey == NULL)  | 
1404  | 0  |         return 0;  | 
1405  | 81.7k  |     if (evp_pkey_is_provided(pkey)) { | 
1406  | 81.7k  |         size_t return_size = OSSL_PARAM_UNMODIFIED;  | 
1407  | 81.7k  |         unsigned char *buf;  | 
1408  |  |  | 
1409  |  |         /*  | 
1410  |  |          * We know that this is going to fail, but it will give us a size  | 
1411  |  |          * to allocate.  | 
1412  |  |          */  | 
1413  | 81.7k  |         EVP_PKEY_get_octet_string_param(pkey,  | 
1414  | 81.7k  |                                         OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,  | 
1415  | 81.7k  |                                         NULL, 0, &return_size);  | 
1416  | 81.7k  |         if (return_size == OSSL_PARAM_UNMODIFIED)  | 
1417  | 0  |             return 0;  | 
1418  |  |  | 
1419  | 81.7k  |         *ppub = NULL;  | 
1420  | 81.7k  |         buf = OPENSSL_malloc(return_size);  | 
1421  | 81.7k  |         if (buf == NULL)  | 
1422  | 0  |             return 0;  | 
1423  |  |  | 
1424  | 81.7k  |         if (!EVP_PKEY_get_octet_string_param(pkey,  | 
1425  | 81.7k  |                                              OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,  | 
1426  | 81.7k  |                                              buf, return_size, NULL)) { | 
1427  | 0  |             OPENSSL_free(buf);  | 
1428  | 0  |             return 0;  | 
1429  | 0  |         }  | 
1430  | 81.7k  |         *ppub = buf;  | 
1431  | 81.7k  |         return return_size;  | 
1432  | 81.7k  |     }  | 
1433  |  |  | 
1434  |  |  | 
1435  | 0  |     rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub);  | 
1436  | 0  |     if (rv <= 0)  | 
1437  | 0  |         return 0;  | 
1438  | 0  |     return rv;  | 
1439  | 0  | }  | 
1440  |  |  | 
1441  |  | #endif /* FIPS_MODULE */  | 
1442  |  |  | 
1443  |  | /*- All methods below can also be used in FIPS_MODULE */  | 
1444  |  |  | 
1445  |  | EVP_PKEY *EVP_PKEY_new(void)  | 
1446  | 730k  | { | 
1447  | 730k  |     EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));  | 
1448  |  |  | 
1449  | 730k  |     if (ret == NULL) { | 
1450  | 0  |         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);  | 
1451  | 0  |         return NULL;  | 
1452  | 0  |     }  | 
1453  |  |  | 
1454  | 730k  |     ret->type = EVP_PKEY_NONE;  | 
1455  | 730k  |     ret->save_type = EVP_PKEY_NONE;  | 
1456  | 730k  |     ret->references = 1;  | 
1457  |  |  | 
1458  | 730k  |     ret->lock = CRYPTO_THREAD_lock_new();  | 
1459  | 730k  |     if (ret->lock == NULL) { | 
1460  | 0  |         EVPerr(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);  | 
1461  | 0  |         goto err;  | 
1462  | 0  |     }  | 
1463  |  |  | 
1464  | 730k  | #ifndef FIPS_MODULE  | 
1465  | 730k  |     ret->save_parameters = 1;  | 
1466  | 730k  |     if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) { | 
1467  | 0  |         ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);  | 
1468  | 0  |         goto err;  | 
1469  | 0  |     }  | 
1470  | 730k  | #endif  | 
1471  | 730k  |     return ret;  | 
1472  |  |  | 
1473  | 0  |  err:  | 
1474  | 0  |     CRYPTO_THREAD_lock_free(ret->lock);  | 
1475  | 0  |     OPENSSL_free(ret);  | 
1476  | 0  |     return NULL;  | 
1477  | 730k  | }  | 
1478  |  |  | 
1479  |  | /*  | 
1480  |  |  * Setup a public key management method.  | 
1481  |  |  *  | 
1482  |  |  * For legacy keys, either |type| or |str| is expected to have the type  | 
1483  |  |  * information.  In this case, the setup consists of finding an ASN1 method  | 
1484  |  |  * and potentially an ENGINE, and setting those fields in |pkey|.  | 
1485  |  |  *  | 
1486  |  |  * For provider side keys, |keymgmt| is expected to be non-NULL.  In this  | 
1487  |  |  * case, the setup consists of setting the |keymgmt| field in |pkey|.  | 
1488  |  |  *  | 
1489  |  |  * If pkey is NULL just return 1 or 0 if the key management method exists.  | 
1490  |  |  */  | 
1491  |  |  | 
1492  |  | static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,  | 
1493  |  |                          int len, EVP_KEYMGMT *keymgmt)  | 
1494  | 6.43M  | { | 
1495  | 6.43M  | #ifndef FIPS_MODULE  | 
1496  | 6.43M  |     const EVP_PKEY_ASN1_METHOD *ameth = NULL;  | 
1497  | 6.43M  |     ENGINE **eptr = (e == NULL) ? &e :  NULL;  | 
1498  | 6.43M  | #endif  | 
1499  |  |  | 
1500  |  |     /*  | 
1501  |  |      * The setups can't set both legacy and provider side methods.  | 
1502  |  |      * It is forbidden  | 
1503  |  |      */  | 
1504  | 6.43M  |     if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)  | 
1505  | 6.43M  |         || !ossl_assert(e == NULL || keymgmt == NULL)) { | 
1506  | 0  |         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);  | 
1507  | 0  |         return 0;  | 
1508  | 0  |     }  | 
1509  |  |  | 
1510  | 6.43M  |     if (pkey != NULL) { | 
1511  | 3.99M  |         int free_it = 0;  | 
1512  |  |  | 
1513  | 3.99M  | #ifndef FIPS_MODULE  | 
1514  | 3.99M  |         free_it = free_it || pkey->pkey.ptr != NULL;  | 
1515  | 3.99M  | #endif  | 
1516  | 3.99M  |         free_it = free_it || pkey->keydata != NULL;  | 
1517  | 3.99M  |         if (free_it)  | 
1518  | 0  |             evp_pkey_free_it(pkey);  | 
1519  | 3.99M  | #ifndef FIPS_MODULE  | 
1520  |  |         /*  | 
1521  |  |          * If key type matches and a method exists then this lookup has  | 
1522  |  |          * succeeded once so just indicate success.  | 
1523  |  |          */  | 
1524  | 3.99M  |         if (pkey->type != EVP_PKEY_NONE  | 
1525  | 3.99M  |             && type == pkey->save_type  | 
1526  | 3.99M  |             && pkey->ameth != NULL)  | 
1527  | 1.86M  |             return 1;  | 
1528  | 2.12M  | # ifndef OPENSSL_NO_ENGINE  | 
1529  |  |         /* If we have ENGINEs release them */  | 
1530  | 2.12M  |         ENGINE_finish(pkey->engine);  | 
1531  | 2.12M  |         pkey->engine = NULL;  | 
1532  | 2.12M  |         ENGINE_finish(pkey->pmeth_engine);  | 
1533  | 2.12M  |         pkey->pmeth_engine = NULL;  | 
1534  | 2.12M  | # endif  | 
1535  | 2.12M  | #endif  | 
1536  | 2.12M  |     }  | 
1537  | 4.56M  | #ifndef FIPS_MODULE  | 
1538  | 4.56M  |     if (str != NULL)  | 
1539  | 3.01M  |         ameth = EVP_PKEY_asn1_find_str(eptr, str, len);  | 
1540  | 1.54M  |     else if (type != EVP_PKEY_NONE)  | 
1541  | 1.49M  |         ameth = EVP_PKEY_asn1_find(eptr, type);  | 
1542  | 4.56M  | # ifndef OPENSSL_NO_ENGINE  | 
1543  | 4.56M  |     if (pkey == NULL && eptr != NULL)  | 
1544  | 2.44M  |         ENGINE_finish(e);  | 
1545  | 4.56M  | # endif  | 
1546  | 4.56M  | #endif  | 
1547  |  |  | 
1548  |  |  | 
1549  | 4.56M  |     { | 
1550  | 4.56M  |         int check = 1;  | 
1551  |  |  | 
1552  | 4.56M  | #ifndef FIPS_MODULE  | 
1553  | 4.56M  |         check = check && ameth == NULL;  | 
1554  | 4.56M  | #endif  | 
1555  | 4.56M  |         check = check && keymgmt == NULL;  | 
1556  | 4.56M  |         if (check) { | 
1557  | 1.87M  |             ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);  | 
1558  | 1.87M  |             return 0;  | 
1559  | 1.87M  |         }  | 
1560  | 4.56M  |     }  | 
1561  | 2.69M  |     if (pkey != NULL) { | 
1562  | 2.12M  |         if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) { | 
1563  | 0  |             ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);  | 
1564  | 0  |             return 0;  | 
1565  | 0  |         }  | 
1566  |  |  | 
1567  | 2.12M  |         pkey->keymgmt = keymgmt;  | 
1568  |  |  | 
1569  | 2.12M  |         pkey->save_type = type;  | 
1570  | 2.12M  |         pkey->type = type;  | 
1571  |  |  | 
1572  | 2.12M  | #ifndef FIPS_MODULE  | 
1573  |  |         /*  | 
1574  |  |          * If the internal "origin" key is provider side, don't save |ameth|.  | 
1575  |  |          * The main reason is that |ameth| is one factor to detect that the  | 
1576  |  |          * internal "origin" key is a legacy one.  | 
1577  |  |          */  | 
1578  | 2.12M  |         if (keymgmt == NULL)  | 
1579  | 1.49M  |             pkey->ameth = ameth;  | 
1580  |  |  | 
1581  |  |         /*  | 
1582  |  |          * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose  | 
1583  |  |          * for any key type that has a legacy implementation, regardless of  | 
1584  |  |          * if the internal key is a legacy or a provider side one.  When  | 
1585  |  |          * there is no legacy implementation for the key, the type becomes  | 
1586  |  |          * EVP_PKEY_KEYMGMT, which indicates that one should be cautious  | 
1587  |  |          * with functions that expect legacy internal keys.  | 
1588  |  |          */  | 
1589  | 2.12M  |         if (ameth != NULL) { | 
1590  | 2.07M  |             if (type == EVP_PKEY_NONE)  | 
1591  | 574k  |                 pkey->type = ameth->pkey_id;  | 
1592  | 2.07M  |         } else { | 
1593  | 52.3k  |             pkey->type = EVP_PKEY_KEYMGMT;  | 
1594  | 52.3k  |         }  | 
1595  | 2.12M  | # ifndef OPENSSL_NO_ENGINE  | 
1596  | 2.12M  |         if (eptr == NULL && e != NULL && !ENGINE_init(e)) { | 
1597  | 0  |             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);  | 
1598  | 0  |             return 0;  | 
1599  | 0  |         }  | 
1600  | 2.12M  | # endif  | 
1601  | 2.12M  |         pkey->engine = e;  | 
1602  | 2.12M  | #endif  | 
1603  | 2.12M  |     }  | 
1604  | 2.69M  |     return 1;  | 
1605  | 2.69M  | }  | 
1606  |  |  | 
1607  |  | #ifndef FIPS_MODULE  | 
1608  |  | static void find_ameth(const char *name, void *data)  | 
1609  | 2.44M  | { | 
1610  | 2.44M  |     const char **str = data;  | 
1611  |  |  | 
1612  |  |     /*  | 
1613  |  |      * The error messages from pkey_set_type() are uninteresting here,  | 
1614  |  |      * and misleading.  | 
1615  |  |      */  | 
1616  | 2.44M  |     ERR_set_mark();  | 
1617  |  |  | 
1618  | 2.44M  |     if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name),  | 
1619  | 2.44M  |                       NULL)) { | 
1620  | 574k  |         if (str[0] == NULL)  | 
1621  | 574k  |             str[0] = name;  | 
1622  | 0  |         else if (str[1] == NULL)  | 
1623  | 0  |             str[1] = name;  | 
1624  | 574k  |     }  | 
1625  |  |  | 
1626  | 2.44M  |     ERR_pop_to_mark();  | 
1627  | 2.44M  | }  | 
1628  |  | #endif  | 
1629  |  |  | 
1630  |  | int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)  | 
1631  | 626k  | { | 
1632  | 626k  | #ifndef FIPS_MODULE  | 
1633  | 626k  | # define EVP_PKEY_TYPE_STR str[0]  | 
1634  | 626k  | # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))  | 
1635  |  |     /*  | 
1636  |  |      * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD  | 
1637  |  |      * Ideally, only one should be found.  If two (or more) are found, the  | 
1638  |  |      * match is ambiguous.  This should never happen, but...  | 
1639  |  |      */  | 
1640  | 626k  |     const char *str[2] = { NULL, NULL }; | 
1641  |  |  | 
1642  | 626k  |     if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str)  | 
1643  | 626k  |             || str[1] != NULL) { | 
1644  | 0  |         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);  | 
1645  | 0  |         return 0;  | 
1646  | 0  |     }  | 
1647  |  | #else  | 
1648  |  | # define EVP_PKEY_TYPE_STR NULL  | 
1649  |  | # define EVP_PKEY_TYPE_STRLEN -1  | 
1650  |  | #endif  | 
1651  | 626k  |     return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,  | 
1652  | 626k  |                          EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,  | 
1653  | 626k  |                          keymgmt);  | 
1654  |  |  | 
1655  | 626k  | #undef EVP_PKEY_TYPE_STR  | 
1656  | 626k  | #undef EVP_PKEY_TYPE_STRLEN  | 
1657  | 626k  | }  | 
1658  |  |  | 
1659  |  | int EVP_PKEY_up_ref(EVP_PKEY *pkey)  | 
1660  | 3.13M  | { | 
1661  | 3.13M  |     int i;  | 
1662  |  |  | 
1663  | 3.13M  |     if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)  | 
1664  | 0  |         return 0;  | 
1665  |  |  | 
1666  | 3.13M  |     REF_PRINT_COUNT("EVP_PKEY", pkey); | 
1667  | 3.13M  |     REF_ASSERT_ISNT(i < 2);  | 
1668  | 3.13M  |     return ((i > 1) ? 1 : 0);  | 
1669  | 3.13M  | }  | 
1670  |  |  | 
1671  |  | #ifndef FIPS_MODULE  | 
1672  |  | EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey)  | 
1673  | 8.25k  | { | 
1674  | 8.25k  |     EVP_PKEY *dup_pk;  | 
1675  |  |  | 
1676  | 8.25k  |     if (pkey == NULL) { | 
1677  | 0  |         ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);  | 
1678  | 0  |         return NULL;  | 
1679  | 0  |     }  | 
1680  |  |  | 
1681  | 8.25k  |     if ((dup_pk = EVP_PKEY_new()) == NULL)  | 
1682  | 0  |         return NULL;  | 
1683  |  |  | 
1684  | 8.25k  |     if (evp_pkey_is_blank(pkey))  | 
1685  | 0  |         goto done;  | 
1686  |  |  | 
1687  | 8.25k  |     if (evp_pkey_is_provided(pkey)) { | 
1688  | 8.25k  |         if (!evp_keymgmt_util_copy(dup_pk, pkey,  | 
1689  | 8.25k  |                                    OSSL_KEYMGMT_SELECT_ALL))  | 
1690  | 0  |             goto err;  | 
1691  | 8.25k  |         goto done;  | 
1692  | 8.25k  |     }  | 
1693  |  |  | 
1694  | 0  |     if (evp_pkey_is_legacy(pkey)) { | 
1695  | 0  |         const EVP_PKEY_ASN1_METHOD *ameth = pkey->ameth;  | 
1696  |  | 
  | 
1697  | 0  |         if (ameth == NULL || ameth->copy == NULL) { | 
1698  | 0  |             if (pkey->pkey.ptr == NULL /* empty key, just set type */  | 
1699  | 0  |                 && EVP_PKEY_set_type(dup_pk, pkey->type) != 0)  | 
1700  | 0  |                 goto done;  | 
1701  | 0  |             ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);  | 
1702  | 0  |             goto err;  | 
1703  | 0  |         }  | 
1704  | 0  |         if (!ameth->copy(dup_pk, pkey))  | 
1705  | 0  |             goto err;  | 
1706  | 0  |         goto done;  | 
1707  | 0  |     }  | 
1708  |  |  | 
1709  | 0  |     goto err;  | 
1710  | 8.25k  | done:  | 
1711  |  |     /* copy auxiliary data */  | 
1712  | 8.25k  |     if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY,  | 
1713  | 8.25k  |                             &dup_pk->ex_data, &pkey->ex_data))  | 
1714  | 0  |         goto err;  | 
1715  |  |  | 
1716  | 8.25k  |     if (pkey->attributes != NULL) { | 
1717  | 0  |         if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL)  | 
1718  | 0  |             goto err;  | 
1719  | 0  |     }  | 
1720  | 8.25k  |     return dup_pk;  | 
1721  | 0  | err:  | 
1722  | 0  |     EVP_PKEY_free(dup_pk);  | 
1723  | 0  |     return NULL;  | 
1724  | 8.25k  | }  | 
1725  |  |  | 
1726  |  | void evp_pkey_free_legacy(EVP_PKEY *x)  | 
1727  | 1.75M  | { | 
1728  | 1.75M  |     const EVP_PKEY_ASN1_METHOD *ameth = x->ameth;  | 
1729  | 1.75M  |     ENGINE *tmpe = NULL;  | 
1730  |  |  | 
1731  | 1.75M  |     if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL)  | 
1732  | 55.4k  |         ameth = EVP_PKEY_asn1_find(&tmpe, x->type);  | 
1733  |  |  | 
1734  | 1.75M  |     if (ameth != NULL) { | 
1735  | 1.05M  |         if (x->legacy_cache_pkey.ptr != NULL) { | 
1736  |  |             /*  | 
1737  |  |              * We should never have both a legacy origin key, and a key in the  | 
1738  |  |              * legacy cache.  | 
1739  |  |              */  | 
1740  | 55.4k  |             assert(x->pkey.ptr == NULL);  | 
1741  |  |             /*  | 
1742  |  |              * For the purposes of freeing we make the legacy cache look like  | 
1743  |  |              * a legacy origin key.  | 
1744  |  |              */  | 
1745  | 55.4k  |             x->pkey = x->legacy_cache_pkey;  | 
1746  | 55.4k  |             x->legacy_cache_pkey.ptr = NULL;  | 
1747  | 55.4k  |         }  | 
1748  | 1.05M  |         if (ameth->pkey_free != NULL)  | 
1749  | 1.05M  |             ameth->pkey_free(x);  | 
1750  | 1.05M  |         x->pkey.ptr = NULL;  | 
1751  | 1.05M  |     }  | 
1752  | 1.75M  | # ifndef OPENSSL_NO_ENGINE  | 
1753  | 1.75M  |     ENGINE_finish(tmpe);  | 
1754  | 1.75M  |     ENGINE_finish(x->engine);  | 
1755  | 1.75M  |     x->engine = NULL;  | 
1756  | 1.75M  |     ENGINE_finish(x->pmeth_engine);  | 
1757  | 1.75M  |     x->pmeth_engine = NULL;  | 
1758  | 1.75M  | # endif  | 
1759  | 1.75M  | }  | 
1760  |  | #endif  /* FIPS_MODULE */  | 
1761  |  |  | 
1762  |  | static void evp_pkey_free_it(EVP_PKEY *x)  | 
1763  | 1.63M  | { | 
1764  |  |     /* internal function; x is never NULL */  | 
1765  | 1.63M  |     evp_keymgmt_util_clear_operation_cache(x, 1);  | 
1766  | 1.63M  | #ifndef FIPS_MODULE  | 
1767  | 1.63M  |     evp_pkey_free_legacy(x);  | 
1768  | 1.63M  | #endif  | 
1769  |  |  | 
1770  | 1.63M  |     if (x->keymgmt != NULL) { | 
1771  | 626k  |         evp_keymgmt_freedata(x->keymgmt, x->keydata);  | 
1772  | 626k  |         EVP_KEYMGMT_free(x->keymgmt);  | 
1773  | 626k  |         x->keymgmt = NULL;  | 
1774  | 626k  |         x->keydata = NULL;  | 
1775  | 626k  |     }  | 
1776  | 1.63M  |     x->type = EVP_PKEY_NONE;  | 
1777  | 1.63M  | }  | 
1778  |  |  | 
1779  |  | void EVP_PKEY_free(EVP_PKEY *x)  | 
1780  | 12.9M  | { | 
1781  | 12.9M  |     int i;  | 
1782  |  |  | 
1783  | 12.9M  |     if (x == NULL)  | 
1784  | 8.17M  |         return;  | 
1785  |  |  | 
1786  | 4.76M  |     CRYPTO_DOWN_REF(&x->references, &i, x->lock);  | 
1787  | 4.76M  |     REF_PRINT_COUNT("EVP_PKEY", x); | 
1788  | 4.76M  |     if (i > 0)  | 
1789  | 3.13M  |         return;  | 
1790  | 1.63M  |     REF_ASSERT_ISNT(i < 0);  | 
1791  | 1.63M  |     evp_pkey_free_it(x);  | 
1792  | 1.63M  | #ifndef FIPS_MODULE  | 
1793  | 1.63M  |     CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);  | 
1794  | 1.63M  | #endif  | 
1795  | 1.63M  |     CRYPTO_THREAD_lock_free(x->lock);  | 
1796  | 1.63M  | #ifndef FIPS_MODULE  | 
1797  | 1.63M  |     sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);  | 
1798  | 1.63M  | #endif  | 
1799  | 1.63M  |     OPENSSL_free(x);  | 
1800  | 1.63M  | }  | 
1801  |  |  | 
1802  |  | int EVP_PKEY_get_size(const EVP_PKEY *pkey)  | 
1803  | 1.75k  | { | 
1804  | 1.75k  |     int size = 0;  | 
1805  |  |  | 
1806  | 1.75k  |     if (pkey != NULL) { | 
1807  | 1.75k  |         size = pkey->cache.size;  | 
1808  | 1.75k  | #ifndef FIPS_MODULE  | 
1809  | 1.75k  |         if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)  | 
1810  | 374  |             size = pkey->ameth->pkey_size(pkey);  | 
1811  | 1.75k  | #endif  | 
1812  | 1.75k  |     }  | 
1813  | 1.75k  |     return size < 0 ? 0 : size;  | 
1814  | 1.75k  | }  | 
1815  |  |  | 
1816  |  | const char *EVP_PKEY_get0_description(const EVP_PKEY *pkey)  | 
1817  | 0  | { | 
1818  | 0  |     if (!evp_pkey_is_assigned(pkey))  | 
1819  | 0  |         return NULL;  | 
1820  |  |  | 
1821  | 0  |     if (evp_pkey_is_provided(pkey) && pkey->keymgmt->description != NULL)  | 
1822  | 0  |         return pkey->keymgmt->description;  | 
1823  | 0  | #ifndef FIPS_MODULE  | 
1824  | 0  |     if (pkey->ameth != NULL)  | 
1825  | 0  |         return pkey->ameth->info;  | 
1826  | 0  | #endif  | 
1827  | 0  |     return NULL;  | 
1828  | 0  | }  | 
1829  |  |  | 
1830  |  | void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,  | 
1831  |  |                                   EVP_KEYMGMT **keymgmt,  | 
1832  |  |                                   const char *propquery)  | 
1833  | 346k  | { | 
1834  | 346k  |     EVP_KEYMGMT *allocated_keymgmt = NULL;  | 
1835  | 346k  |     EVP_KEYMGMT *tmp_keymgmt = NULL;  | 
1836  | 346k  |     int selection = OSSL_KEYMGMT_SELECT_ALL;  | 
1837  | 346k  |     void *keydata = NULL;  | 
1838  | 346k  |     int check;  | 
1839  |  |  | 
1840  | 346k  |     if (pk == NULL)  | 
1841  | 0  |         return NULL;  | 
1842  |  |  | 
1843  |  |     /* No key data => nothing to export */  | 
1844  | 346k  |     check = 1;  | 
1845  | 346k  | #ifndef FIPS_MODULE  | 
1846  | 346k  |     check = check && pk->pkey.ptr == NULL;  | 
1847  | 346k  | #endif  | 
1848  | 346k  |     check = check && pk->keydata == NULL;  | 
1849  | 346k  |     if (check)  | 
1850  | 0  |         return NULL;  | 
1851  |  |  | 
1852  | 346k  | #ifndef FIPS_MODULE  | 
1853  | 346k  |     if (pk->pkey.ptr != NULL) { | 
1854  |  |         /*  | 
1855  |  |          * If the legacy key doesn't have an dirty counter or export function,  | 
1856  |  |          * give up  | 
1857  |  |          */  | 
1858  | 204k  |         if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)  | 
1859  | 0  |             return NULL;  | 
1860  | 204k  |     }  | 
1861  | 346k  | #endif  | 
1862  |  |  | 
1863  | 346k  |     if (keymgmt != NULL) { | 
1864  | 346k  |         tmp_keymgmt = *keymgmt;  | 
1865  | 346k  |         *keymgmt = NULL;  | 
1866  | 346k  |     }  | 
1867  |  |  | 
1868  |  |     /*  | 
1869  |  |      * If no keymgmt was given or found, get a default keymgmt.  We do so by  | 
1870  |  |      * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.  | 
1871  |  |      */  | 
1872  | 346k  |     if (tmp_keymgmt == NULL) { | 
1873  | 0  |         EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);  | 
1874  |  | 
  | 
1875  | 0  |         if (ctx == NULL)  | 
1876  | 0  |             goto end;  | 
1877  | 0  |         allocated_keymgmt = tmp_keymgmt = ctx->keymgmt;  | 
1878  | 0  |         ctx->keymgmt = NULL;  | 
1879  | 0  |         EVP_PKEY_CTX_free(ctx);  | 
1880  | 0  |     }  | 
1881  |  |  | 
1882  |  |     /* If there's still no keymgmt to be had, give up */  | 
1883  | 346k  |     if (tmp_keymgmt == NULL)  | 
1884  | 0  |         goto end;  | 
1885  |  |  | 
1886  | 346k  | #ifndef FIPS_MODULE  | 
1887  | 346k  |     if (pk->pkey.ptr != NULL) { | 
1888  | 204k  |         OP_CACHE_ELEM *op;  | 
1889  |  |  | 
1890  |  |         /*  | 
1891  |  |          * If the legacy "origin" hasn't changed since last time, we try  | 
1892  |  |          * to find our keymgmt in the operation cache.  If it has changed,  | 
1893  |  |          * |i| remains zero, and we will clear the cache further down.  | 
1894  |  |          */  | 
1895  | 204k  |         if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) { | 
1896  | 148k  |             if (!CRYPTO_THREAD_read_lock(pk->lock))  | 
1897  | 0  |                 goto end;  | 
1898  | 148k  |             op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt,  | 
1899  | 148k  |                                                        selection);  | 
1900  |  |  | 
1901  |  |             /*  | 
1902  |  |              * If |tmp_keymgmt| is present in the operation cache, it means  | 
1903  |  |              * that export doesn't need to be redone.  In that case, we take  | 
1904  |  |              * token copies of the cached pointers, to have token success  | 
1905  |  |              * values to return. It is possible (e.g. in a no-cached-fetch  | 
1906  |  |              * build), for op->keymgmt to be a different pointer to tmp_keymgmt  | 
1907  |  |              * even though the name/provider must be the same. In other words  | 
1908  |  |              * the keymgmt instance may be different but still equivalent, i.e.  | 
1909  |  |              * same algorithm/provider instance - but we make the simplifying  | 
1910  |  |              * assumption that the keydata can be used with either keymgmt  | 
1911  |  |              * instance. Not doing so introduces significant complexity and  | 
1912  |  |              * probably requires refactoring - since we would have to ripple  | 
1913  |  |              * the change in keymgmt instance up the call chain.  | 
1914  |  |              */  | 
1915  | 148k  |             if (op != NULL && op->keymgmt != NULL) { | 
1916  | 127k  |                 keydata = op->keydata;  | 
1917  | 127k  |                 CRYPTO_THREAD_unlock(pk->lock);  | 
1918  | 127k  |                 goto end;  | 
1919  | 127k  |             }  | 
1920  | 20.8k  |             CRYPTO_THREAD_unlock(pk->lock);  | 
1921  | 20.8k  |         }  | 
1922  |  |  | 
1923  |  |         /* Make sure that the keymgmt key type matches the legacy NID */  | 
1924  | 76.3k  |         if (!EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))  | 
1925  | 0  |             goto end;  | 
1926  |  |  | 
1927  | 76.3k  |         if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)  | 
1928  | 0  |             goto end;  | 
1929  |  |  | 
1930  | 76.3k  |         if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt->import,  | 
1931  | 76.3k  |                                   libctx, propquery)) { | 
1932  | 0  |             evp_keymgmt_freedata(tmp_keymgmt, keydata);  | 
1933  | 0  |             keydata = NULL;  | 
1934  | 0  |             goto end;  | 
1935  | 0  |         }  | 
1936  |  |  | 
1937  |  |         /*  | 
1938  |  |          * If the dirty counter changed since last time, then clear the  | 
1939  |  |          * operation cache.  In that case, we know that |i| is zero.  Just  | 
1940  |  |          * in case this is a re-export, we increment then decrement the  | 
1941  |  |          * keymgmt reference counter.  | 
1942  |  |          */  | 
1943  | 76.3k  |         if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */ | 
1944  | 0  |             evp_keymgmt_freedata(tmp_keymgmt, keydata);  | 
1945  | 0  |             keydata = NULL;  | 
1946  | 0  |             goto end;  | 
1947  | 0  |         }  | 
1948  |  |  | 
1949  | 76.3k  |         if (!CRYPTO_THREAD_write_lock(pk->lock))  | 
1950  | 0  |             goto end;  | 
1951  | 76.3k  |         if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy  | 
1952  | 76.3k  |                 && !evp_keymgmt_util_clear_operation_cache(pk, 0)) { | 
1953  | 0  |             CRYPTO_THREAD_unlock(pk->lock);  | 
1954  | 0  |             evp_keymgmt_freedata(tmp_keymgmt, keydata);  | 
1955  | 0  |             keydata = NULL;  | 
1956  | 0  |             EVP_KEYMGMT_free(tmp_keymgmt);  | 
1957  | 0  |             goto end;  | 
1958  | 0  |         }  | 
1959  | 76.3k  |         EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */  | 
1960  |  |  | 
1961  |  |         /* Check to make sure some other thread didn't get there first */  | 
1962  | 76.3k  |         op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt, selection);  | 
1963  | 76.3k  |         if (op != NULL && op->keymgmt != NULL) { | 
1964  | 0  |             void *tmp_keydata = op->keydata;  | 
1965  |  | 
  | 
1966  | 0  |             CRYPTO_THREAD_unlock(pk->lock);  | 
1967  | 0  |             evp_keymgmt_freedata(tmp_keymgmt, keydata);  | 
1968  | 0  |             keydata = tmp_keydata;  | 
1969  | 0  |             goto end;  | 
1970  | 0  |         }  | 
1971  |  |  | 
1972  |  |         /* Add the new export to the operation cache */  | 
1973  | 76.3k  |         if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata,  | 
1974  | 76.3k  |                                             selection)) { | 
1975  | 0  |             CRYPTO_THREAD_unlock(pk->lock);  | 
1976  | 0  |             evp_keymgmt_freedata(tmp_keymgmt, keydata);  | 
1977  | 0  |             keydata = NULL;  | 
1978  | 0  |             goto end;  | 
1979  | 0  |         }  | 
1980  |  |  | 
1981  |  |         /* Synchronize the dirty count */  | 
1982  | 76.3k  |         pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);  | 
1983  |  |  | 
1984  | 76.3k  |         CRYPTO_THREAD_unlock(pk->lock);  | 
1985  | 76.3k  |         goto end;  | 
1986  | 76.3k  |     }  | 
1987  | 141k  | #endif  /* FIPS_MODULE */  | 
1988  |  |  | 
1989  | 141k  |     keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt, selection);  | 
1990  |  |  | 
1991  | 346k  |  end:  | 
1992  |  |     /*  | 
1993  |  |      * If nothing was exported, |tmp_keymgmt| might point at a freed  | 
1994  |  |      * EVP_KEYMGMT, so we clear it to be safe.  It shouldn't be useful for  | 
1995  |  |      * the caller either way in that case.  | 
1996  |  |      */  | 
1997  | 346k  |     if (keydata == NULL)  | 
1998  | 0  |         tmp_keymgmt = NULL;  | 
1999  |  |  | 
2000  | 346k  |     if (keymgmt != NULL && tmp_keymgmt != NULL) { | 
2001  | 346k  |         *keymgmt = tmp_keymgmt;  | 
2002  | 346k  |         allocated_keymgmt = NULL;  | 
2003  | 346k  |     }  | 
2004  |  |  | 
2005  | 346k  |     EVP_KEYMGMT_free(allocated_keymgmt);  | 
2006  | 346k  |     return keydata;  | 
2007  | 141k  | }  | 
2008  |  |  | 
2009  |  | #ifndef FIPS_MODULE  | 
2010  |  | int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)  | 
2011  | 55.4k  | { | 
2012  | 55.4k  |     EVP_PKEY *allocpkey = NULL;  | 
2013  |  |  | 
2014  | 55.4k  |     if (!ossl_assert(dest != NULL))  | 
2015  | 0  |         return 0;  | 
2016  |  |  | 
2017  | 55.4k  |     if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) { | 
2018  | 55.4k  |         EVP_KEYMGMT *keymgmt = src->keymgmt;  | 
2019  | 55.4k  |         void *keydata = src->keydata;  | 
2020  | 55.4k  |         int type = src->type;  | 
2021  | 55.4k  |         const char *keytype = NULL;  | 
2022  |  |  | 
2023  | 55.4k  |         keytype = EVP_KEYMGMT_get0_name(keymgmt);  | 
2024  |  |  | 
2025  |  |         /*  | 
2026  |  |          * If the type is EVP_PKEY_NONE, then we have a problem somewhere  | 
2027  |  |          * else in our code.  If it's not one of the well known EVP_PKEY_xxx  | 
2028  |  |          * values, it should at least be EVP_PKEY_KEYMGMT at this point.  | 
2029  |  |          * The check is kept as a safety measure.  | 
2030  |  |          */  | 
2031  | 55.4k  |         if (!ossl_assert(type != EVP_PKEY_NONE)) { | 
2032  | 0  |             ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,  | 
2033  | 0  |                            "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",  | 
2034  | 0  |                            keytype);  | 
2035  | 0  |             return 0;  | 
2036  | 0  |         }  | 
2037  |  |  | 
2038  |  |         /* Prefer the legacy key type name for error reporting */  | 
2039  | 55.4k  |         if (type != EVP_PKEY_KEYMGMT)  | 
2040  | 55.4k  |             keytype = OBJ_nid2sn(type);  | 
2041  |  |  | 
2042  |  |         /* Make sure we have a clean slate to copy into */  | 
2043  | 55.4k  |         if (*dest == NULL) { | 
2044  | 55.4k  |             allocpkey = *dest = EVP_PKEY_new();  | 
2045  | 55.4k  |             if (*dest == NULL) { | 
2046  | 0  |                 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);  | 
2047  | 0  |                 return 0;  | 
2048  | 0  |             }  | 
2049  | 55.4k  |         } else { | 
2050  | 0  |             evp_pkey_free_it(*dest);  | 
2051  | 0  |         }  | 
2052  |  |  | 
2053  | 55.4k  |         if (EVP_PKEY_set_type(*dest, type)) { | 
2054  |  |             /* If the key is typed but empty, we're done */  | 
2055  | 55.4k  |             if (keydata == NULL)  | 
2056  | 0  |                 return 1;  | 
2057  |  |  | 
2058  | 55.4k  |             if ((*dest)->ameth->import_from == NULL) { | 
2059  | 0  |                 ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,  | 
2060  | 0  |                                "key type = %s", keytype);  | 
2061  | 55.4k  |             } else { | 
2062  |  |                 /*  | 
2063  |  |                  * We perform the export in the same libctx as the keymgmt  | 
2064  |  |                  * that we are using.  | 
2065  |  |                  */  | 
2066  | 55.4k  |                 OSSL_LIB_CTX *libctx =  | 
2067  | 55.4k  |                     ossl_provider_libctx(keymgmt->prov);  | 
2068  | 55.4k  |                 EVP_PKEY_CTX *pctx =  | 
2069  | 55.4k  |                     EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL);  | 
2070  |  |  | 
2071  | 55.4k  |                 if (pctx == NULL)  | 
2072  | 55.4k  |                     ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);  | 
2073  |  |  | 
2074  | 55.4k  |                 if (pctx != NULL  | 
2075  | 55.4k  |                     && evp_keymgmt_export(keymgmt, keydata,  | 
2076  | 55.4k  |                                           OSSL_KEYMGMT_SELECT_ALL,  | 
2077  | 55.4k  |                                           (*dest)->ameth->import_from,  | 
2078  | 55.4k  |                                           pctx)) { | 
2079  |  |                     /* Synchronize the dirty count */  | 
2080  | 55.4k  |                     (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest);  | 
2081  |  |  | 
2082  | 55.4k  |                     EVP_PKEY_CTX_free(pctx);  | 
2083  | 55.4k  |                     return 1;  | 
2084  | 55.4k  |                 }  | 
2085  | 0  |                 EVP_PKEY_CTX_free(pctx);  | 
2086  | 0  |             }  | 
2087  |  |  | 
2088  | 0  |             ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,  | 
2089  | 0  |                            "key type = %s", keytype);  | 
2090  | 0  |         }  | 
2091  | 55.4k  |     }  | 
2092  |  |  | 
2093  | 0  |     if (allocpkey != NULL) { | 
2094  | 0  |         EVP_PKEY_free(allocpkey);  | 
2095  | 0  |         *dest = NULL;  | 
2096  | 0  |     }  | 
2097  | 0  |     return 0;  | 
2098  | 55.4k  | }  | 
2099  |  |  | 
2100  |  | void *evp_pkey_get_legacy(EVP_PKEY *pk)  | 
2101  | 452k  | { | 
2102  | 452k  |     EVP_PKEY *tmp_copy = NULL;  | 
2103  | 452k  |     void *ret = NULL;  | 
2104  |  |  | 
2105  | 452k  |     if (!ossl_assert(pk != NULL))  | 
2106  | 0  |         return NULL;  | 
2107  |  |  | 
2108  |  |     /*  | 
2109  |  |      * If this isn't an assigned provider side key, we just use any existing  | 
2110  |  |      * origin legacy key.  | 
2111  |  |      */  | 
2112  | 452k  |     if (!evp_pkey_is_assigned(pk))  | 
2113  | 0  |         return NULL;  | 
2114  | 452k  |     if (!evp_pkey_is_provided(pk))  | 
2115  | 397k  |         return pk->pkey.ptr;  | 
2116  |  |  | 
2117  | 55.4k  |     if (!CRYPTO_THREAD_read_lock(pk->lock))  | 
2118  | 0  |         return NULL;  | 
2119  |  |  | 
2120  | 55.4k  |     ret = pk->legacy_cache_pkey.ptr;  | 
2121  |  |  | 
2122  | 55.4k  |     if (!CRYPTO_THREAD_unlock(pk->lock))  | 
2123  | 0  |         return NULL;  | 
2124  |  |  | 
2125  | 55.4k  |     if (ret != NULL)  | 
2126  | 0  |         return ret;  | 
2127  |  |  | 
2128  | 55.4k  |     if (!evp_pkey_copy_downgraded(&tmp_copy, pk))  | 
2129  | 0  |         goto err;  | 
2130  |  |  | 
2131  | 55.4k  |     if (!CRYPTO_THREAD_write_lock(pk->lock))  | 
2132  | 0  |         goto err;  | 
2133  |  |  | 
2134  |  |     /* Check again in case some other thread has updated it in the meantime */  | 
2135  | 55.4k  |     ret = pk->legacy_cache_pkey.ptr;  | 
2136  | 55.4k  |     if (ret == NULL) { | 
2137  |  |         /* Steal the legacy key reference from the temporary copy */  | 
2138  | 55.4k  |         ret = pk->legacy_cache_pkey.ptr = tmp_copy->pkey.ptr;  | 
2139  | 55.4k  |         tmp_copy->pkey.ptr = NULL;  | 
2140  | 55.4k  |     }  | 
2141  |  |  | 
2142  | 55.4k  |     if (!CRYPTO_THREAD_unlock(pk->lock)) { | 
2143  | 0  |         ret = NULL;  | 
2144  | 0  |         goto err;  | 
2145  | 0  |     }  | 
2146  |  |  | 
2147  | 55.4k  |  err:  | 
2148  | 55.4k  |     EVP_PKEY_free(tmp_copy);  | 
2149  |  |  | 
2150  | 55.4k  |     return ret;  | 
2151  | 55.4k  | }  | 
2152  |  | #endif  /* FIPS_MODULE */  | 
2153  |  |  | 
2154  |  | int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,  | 
2155  |  |                           BIGNUM **bn)  | 
2156  | 0  | { | 
2157  | 0  |     int ret = 0;  | 
2158  | 0  |     OSSL_PARAM params[2];  | 
2159  | 0  |     unsigned char buffer[2048];  | 
2160  | 0  |     unsigned char *buf = NULL;  | 
2161  | 0  |     size_t buf_sz = 0;  | 
2162  |  | 
  | 
2163  | 0  |     if (key_name == NULL  | 
2164  | 0  |         || bn == NULL)  | 
2165  | 0  |         return 0;  | 
2166  |  |  | 
2167  | 0  |     memset(buffer, 0, sizeof(buffer));  | 
2168  | 0  |     params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));  | 
2169  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
2170  | 0  |     if (!EVP_PKEY_get_params(pkey, params)) { | 
2171  | 0  |         if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)  | 
2172  | 0  |             return 0;  | 
2173  | 0  |         buf_sz = params[0].return_size;  | 
2174  |  |         /*  | 
2175  |  |          * If it failed because the buffer was too small then allocate the  | 
2176  |  |          * required buffer size and retry.  | 
2177  |  |          */  | 
2178  | 0  |         buf = OPENSSL_zalloc(buf_sz);  | 
2179  | 0  |         if (buf == NULL)  | 
2180  | 0  |             return 0;  | 
2181  | 0  |         params[0].data = buf;  | 
2182  | 0  |         params[0].data_size = buf_sz;  | 
2183  |  | 
  | 
2184  | 0  |         if (!EVP_PKEY_get_params(pkey, params))  | 
2185  | 0  |             goto err;  | 
2186  | 0  |     }  | 
2187  |  |     /* Fail if the param was not found */  | 
2188  | 0  |     if (!OSSL_PARAM_modified(params))  | 
2189  | 0  |         goto err;  | 
2190  | 0  |     ret = OSSL_PARAM_get_BN(params, bn);  | 
2191  | 0  | err:  | 
2192  | 0  |     if (buf != NULL) { | 
2193  | 0  |         if (OSSL_PARAM_modified(params))  | 
2194  | 0  |             OPENSSL_clear_free(buf, buf_sz);  | 
2195  | 0  |         else  | 
2196  | 0  |             OPENSSL_free(buf);  | 
2197  | 0  |     } else if (OSSL_PARAM_modified(params)) { | 
2198  | 0  |         OPENSSL_cleanse(buffer, params[0].data_size);  | 
2199  | 0  |     }  | 
2200  | 0  |     return ret;  | 
2201  | 0  | }  | 
2202  |  |  | 
2203  |  | int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name,  | 
2204  |  |                                     unsigned char *buf, size_t max_buf_sz,  | 
2205  |  |                                     size_t *out_len)  | 
2206  | 163k  | { | 
2207  | 163k  |     OSSL_PARAM params[2];  | 
2208  | 163k  |     int ret1 = 0, ret2 = 0;  | 
2209  |  |  | 
2210  | 163k  |     if (key_name == NULL)  | 
2211  | 0  |         return 0;  | 
2212  |  |  | 
2213  | 163k  |     params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);  | 
2214  | 163k  |     params[1] = OSSL_PARAM_construct_end();  | 
2215  | 163k  |     if ((ret1 = EVP_PKEY_get_params(pkey, params)))  | 
2216  | 163k  |         ret2 = OSSL_PARAM_modified(params);  | 
2217  | 163k  |     if (ret2 && out_len != NULL)  | 
2218  | 81.7k  |         *out_len = params[0].return_size;  | 
2219  | 163k  |     return ret1 && ret2;  | 
2220  | 163k  | }  | 
2221  |  |  | 
2222  |  | int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name,  | 
2223  |  |                                     char *str, size_t max_buf_sz,  | 
2224  |  |                                     size_t *out_len)  | 
2225  | 29.9k  | { | 
2226  | 29.9k  |     OSSL_PARAM params[2];  | 
2227  | 29.9k  |     int ret1 = 0, ret2 = 0;  | 
2228  |  |  | 
2229  | 29.9k  |     if (key_name == NULL)  | 
2230  | 0  |         return 0;  | 
2231  |  |  | 
2232  | 29.9k  |     params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);  | 
2233  | 29.9k  |     params[1] = OSSL_PARAM_construct_end();  | 
2234  | 29.9k  |     if ((ret1 = EVP_PKEY_get_params(pkey, params)))  | 
2235  | 29.9k  |         ret2 = OSSL_PARAM_modified(params);  | 
2236  | 29.9k  |     if (ret2 && out_len != NULL)  | 
2237  | 14.1k  |         *out_len = params[0].return_size;  | 
2238  |  |  | 
2239  | 29.9k  |     if (ret2 && params[0].return_size == max_buf_sz)  | 
2240  |  |         /* There was no space for a NUL byte */  | 
2241  | 0  |         return 0;  | 
2242  |  |     /* Add a terminating NUL byte for good measure */  | 
2243  | 29.9k  |     if (ret2 && str != NULL)  | 
2244  | 29.9k  |         str[params[0].return_size] = '\0';  | 
2245  |  |  | 
2246  | 29.9k  |     return ret1 && ret2;  | 
2247  | 29.9k  | }  | 
2248  |  |  | 
2249  |  | int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name,  | 
2250  |  |                            int *out)  | 
2251  | 0  | { | 
2252  | 0  |     OSSL_PARAM params[2];  | 
2253  |  | 
  | 
2254  | 0  |     if (key_name == NULL)  | 
2255  | 0  |         return 0;  | 
2256  |  |  | 
2257  | 0  |     params[0] = OSSL_PARAM_construct_int(key_name, out);  | 
2258  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
2259  | 0  |     return EVP_PKEY_get_params(pkey, params)  | 
2260  | 0  |         && OSSL_PARAM_modified(params);  | 
2261  | 0  | }  | 
2262  |  |  | 
2263  |  | int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name,  | 
2264  |  |                               size_t *out)  | 
2265  | 0  | { | 
2266  | 0  |     OSSL_PARAM params[2];  | 
2267  |  | 
  | 
2268  | 0  |     if (key_name == NULL)  | 
2269  | 0  |         return 0;  | 
2270  |  |  | 
2271  | 0  |     params[0] = OSSL_PARAM_construct_size_t(key_name, out);  | 
2272  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
2273  | 0  |     return EVP_PKEY_get_params(pkey, params)  | 
2274  | 0  |         && OSSL_PARAM_modified(params);  | 
2275  | 0  | }  | 
2276  |  |  | 
2277  |  | int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in)  | 
2278  | 0  | { | 
2279  | 0  |     OSSL_PARAM params[2];  | 
2280  |  | 
  | 
2281  | 0  |     if (key_name == NULL)  | 
2282  | 0  |         return 0;  | 
2283  |  |  | 
2284  | 0  |     params[0] = OSSL_PARAM_construct_int(key_name, &in);  | 
2285  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
2286  | 0  |     return EVP_PKEY_set_params(pkey, params);  | 
2287  | 0  | }  | 
2288  |  |  | 
2289  |  | int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in)  | 
2290  | 0  | { | 
2291  | 0  |     OSSL_PARAM params[2];  | 
2292  |  | 
  | 
2293  | 0  |     if (key_name == NULL)  | 
2294  | 0  |         return 0;  | 
2295  |  |  | 
2296  | 0  |     params[0] = OSSL_PARAM_construct_size_t(key_name, &in);  | 
2297  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
2298  | 0  |     return EVP_PKEY_set_params(pkey, params);  | 
2299  | 0  | }  | 
2300  |  |  | 
2301  |  | int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name,  | 
2302  |  |                           const BIGNUM *bn)  | 
2303  | 0  | { | 
2304  | 0  |     OSSL_PARAM params[2];  | 
2305  | 0  |     unsigned char buffer[2048];  | 
2306  | 0  |     int bsize = 0;  | 
2307  |  | 
  | 
2308  | 0  |     if (key_name == NULL  | 
2309  | 0  |         || bn == NULL  | 
2310  | 0  |         || pkey == NULL  | 
2311  | 0  |         || !evp_pkey_is_assigned(pkey))  | 
2312  | 0  |         return 0;  | 
2313  |  |  | 
2314  | 0  |     bsize = BN_num_bytes(bn);  | 
2315  | 0  |     if (!ossl_assert(bsize <= (int)sizeof(buffer)))  | 
2316  | 0  |         return 0;  | 
2317  |  |  | 
2318  | 0  |     if (BN_bn2nativepad(bn, buffer, bsize) < 0)  | 
2319  | 0  |         return 0;  | 
2320  | 0  |     params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize);  | 
2321  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
2322  | 0  |     return EVP_PKEY_set_params(pkey, params);  | 
2323  | 0  | }  | 
2324  |  |  | 
2325  |  | int EVP_PKEY_set_utf8_string_param(EVP_PKEY *pkey, const char *key_name,  | 
2326  |  |                                    const char *str)  | 
2327  | 0  | { | 
2328  | 0  |     OSSL_PARAM params[2];  | 
2329  |  | 
  | 
2330  | 0  |     if (key_name == NULL)  | 
2331  | 0  |         return 0;  | 
2332  |  |  | 
2333  | 0  |     params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0);  | 
2334  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
2335  | 0  |     return EVP_PKEY_set_params(pkey, params);  | 
2336  | 0  | }  | 
2337  |  |  | 
2338  |  | int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name,  | 
2339  |  |                                     const unsigned char *buf, size_t bsize)  | 
2340  | 24.1k  | { | 
2341  | 24.1k  |     OSSL_PARAM params[2];  | 
2342  |  |  | 
2343  | 24.1k  |     if (key_name == NULL)  | 
2344  | 0  |         return 0;  | 
2345  |  |  | 
2346  | 24.1k  |     params[0] = OSSL_PARAM_construct_octet_string(key_name,  | 
2347  | 24.1k  |                                                   (unsigned char *)buf, bsize);  | 
2348  | 24.1k  |     params[1] = OSSL_PARAM_construct_end();  | 
2349  | 24.1k  |     return EVP_PKEY_set_params(pkey, params);  | 
2350  | 24.1k  | }  | 
2351  |  |  | 
2352  |  | const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey)  | 
2353  | 0  | { | 
2354  | 0  |     return (pkey != NULL && evp_pkey_is_provided(pkey))  | 
2355  | 0  |         ? EVP_KEYMGMT_settable_params(pkey->keymgmt)  | 
2356  | 0  |         : NULL;  | 
2357  | 0  | }  | 
2358  |  |  | 
2359  |  | int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])  | 
2360  | 24.1k  | { | 
2361  | 24.1k  |     if (pkey != NULL) { | 
2362  | 24.1k  |         if (evp_pkey_is_provided(pkey)) { | 
2363  | 24.1k  |             pkey->dirty_cnt++;  | 
2364  | 24.1k  |             return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);  | 
2365  | 24.1k  |         }  | 
2366  | 24.1k  | #ifndef FIPS_MODULE  | 
2367  |  |         /*  | 
2368  |  |          * We will hopefully never find the need to set individual data in  | 
2369  |  |          * EVP_PKEYs with a legacy internal key, but we can't be entirely  | 
2370  |  |          * sure.  This bit of code can be enabled if we find the need.  If  | 
2371  |  |          * not, it can safely be removed when #legacy support is removed.  | 
2372  |  |          */  | 
2373  |  | # if 0  | 
2374  |  |         else if (evp_pkey_is_legacy(pkey)) { | 
2375  |  |             return evp_pkey_set_params_to_ctrl(pkey, params);  | 
2376  |  |         }  | 
2377  |  | # endif  | 
2378  | 24.1k  | #endif  | 
2379  | 24.1k  |     }  | 
2380  | 24.1k  |     ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);  | 
2381  | 0  |     return 0;  | 
2382  | 24.1k  | }  | 
2383  |  |  | 
2384  |  | const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey)  | 
2385  | 0  | { | 
2386  | 0  |     return (pkey != NULL && evp_pkey_is_provided(pkey))  | 
2387  | 0  |         ? EVP_KEYMGMT_gettable_params(pkey->keymgmt)  | 
2388  | 0  |         : NULL;  | 
2389  | 0  | }  | 
2390  |  |  | 
2391  |  | int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[])  | 
2392  | 193k  | { | 
2393  | 193k  |     if (pkey != NULL) { | 
2394  | 193k  |         if (evp_pkey_is_provided(pkey))  | 
2395  | 191k  |             return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params) > 0;  | 
2396  | 2.02k  | #ifndef FIPS_MODULE  | 
2397  | 2.02k  |         else if (evp_pkey_is_legacy(pkey))  | 
2398  | 2.02k  |             return evp_pkey_get_params_to_ctrl(pkey, params) > 0;  | 
2399  | 193k  | #endif  | 
2400  | 193k  |     }  | 
2401  | 193k  |     ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);  | 
2402  | 0  |     return 0;  | 
2403  | 193k  | }  | 
2404  |  |  | 
2405  |  | #ifndef FIPS_MODULE  | 
2406  |  | int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey)  | 
2407  | 14.0k  | { | 
2408  | 14.0k  |     char name[80];  | 
2409  | 14.0k  |     size_t name_len;  | 
2410  |  |  | 
2411  | 14.0k  |     if (pkey == NULL)  | 
2412  | 0  |         return 0;  | 
2413  |  |  | 
2414  | 14.0k  |     if (pkey->keymgmt == NULL  | 
2415  | 14.0k  |             || pkey->keydata == NULL) { | 
2416  | 0  | # ifndef OPENSSL_NO_EC  | 
2417  |  |         /* Might work through the legacy route */  | 
2418  | 0  |         const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);  | 
2419  |  | 
  | 
2420  | 0  |         if (ec == NULL)  | 
2421  | 0  |             return 0;  | 
2422  |  |  | 
2423  | 0  |         return EC_KEY_get_conv_form(ec);  | 
2424  |  | # else  | 
2425  |  |         return 0;  | 
2426  |  | # endif  | 
2427  | 0  |     }  | 
2428  |  |  | 
2429  | 14.0k  |     if (!EVP_PKEY_get_utf8_string_param(pkey,  | 
2430  | 14.0k  |                                         OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,  | 
2431  | 14.0k  |                                         name, sizeof(name), &name_len))  | 
2432  | 0  |         return 0;  | 
2433  |  |  | 
2434  | 14.0k  |     if (strcmp(name, "uncompressed") == 0)  | 
2435  | 14.0k  |         return POINT_CONVERSION_UNCOMPRESSED;  | 
2436  |  |  | 
2437  | 58  |     if (strcmp(name, "compressed") == 0)  | 
2438  | 10  |         return POINT_CONVERSION_COMPRESSED;  | 
2439  |  |  | 
2440  | 48  |     if (strcmp(name, "hybrid") == 0)  | 
2441  | 48  |         return POINT_CONVERSION_HYBRID;  | 
2442  |  |  | 
2443  | 0  |     return 0;  | 
2444  | 48  | }  | 
2445  |  |  | 
2446  |  | int EVP_PKEY_get_field_type(const EVP_PKEY *pkey)  | 
2447  | 58  | { | 
2448  | 58  |     char fstr[80];  | 
2449  | 58  |     size_t fstrlen;  | 
2450  |  |  | 
2451  | 58  |     if (pkey == NULL)  | 
2452  | 0  |         return 0;  | 
2453  |  |  | 
2454  | 58  |     if (pkey->keymgmt == NULL  | 
2455  | 58  |             || pkey->keydata == NULL) { | 
2456  | 0  | # ifndef OPENSSL_NO_EC  | 
2457  |  |         /* Might work through the legacy route */  | 
2458  | 0  |         const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);  | 
2459  | 0  |         const EC_GROUP *grp;  | 
2460  |  | 
  | 
2461  | 0  |         if (ec == NULL)  | 
2462  | 0  |             return 0;  | 
2463  | 0  |         grp = EC_KEY_get0_group(ec);  | 
2464  | 0  |         if (grp == NULL)  | 
2465  | 0  |             return 0;  | 
2466  |  |  | 
2467  | 0  |         return EC_GROUP_get_field_type(grp);  | 
2468  |  | # else  | 
2469  |  |         return 0;  | 
2470  |  | # endif  | 
2471  | 0  |     }  | 
2472  |  |  | 
2473  | 58  |     if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE,  | 
2474  | 58  |                                         fstr, sizeof(fstr), &fstrlen))  | 
2475  | 0  |         return 0;  | 
2476  |  |  | 
2477  | 58  |     if (strcmp(fstr, SN_X9_62_prime_field) == 0)  | 
2478  | 54  |         return NID_X9_62_prime_field;  | 
2479  | 4  |     else if (strcmp(fstr, SN_X9_62_characteristic_two_field))  | 
2480  | 0  |         return NID_X9_62_characteristic_two_field;  | 
2481  |  |  | 
2482  | 4  |     return 0;  | 
2483  | 58  | }  | 
2484  |  | #endif  |