Bug Summary

File:out/../deps/openssl/openssl/crypto/evp/p_lib.c
Warning:line 1312, column 9
Access to field 'ameth' results in a dereference of a null pointer (loaded from variable 'pkey')

Annotated Source Code

Press '?' to see keyboard shortcuts

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