/src/openssl/crypto/evp/pmeth_lib.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2006-2023 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 | | * Low level key APIs (DH etc) are deprecated for public use, but still ok for |
12 | | * internal use. |
13 | | */ |
14 | | #include "internal/deprecated.h" |
15 | | |
16 | | #include <stdio.h> |
17 | | #include <stdlib.h> |
18 | | #ifndef FIPS_MODULE |
19 | | # include <openssl/engine.h> |
20 | | #endif |
21 | | #include <openssl/evp.h> |
22 | | #include <openssl/core_names.h> |
23 | | #include <openssl/dh.h> |
24 | | #include <openssl/rsa.h> |
25 | | #include <openssl/kdf.h> |
26 | | #include "internal/cryptlib.h" |
27 | | #ifndef FIPS_MODULE |
28 | | # include "crypto/asn1.h" |
29 | | #endif |
30 | | #include "crypto/evp.h" |
31 | | #include "crypto/dh.h" |
32 | | #include "crypto/ec.h" |
33 | | #include "internal/ffc.h" |
34 | | #include "internal/numbers.h" |
35 | | #include "internal/provider.h" |
36 | | #include "evp_local.h" |
37 | | |
38 | | #ifndef FIPS_MODULE |
39 | | |
40 | | static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx, |
41 | | int keytype, int optype, |
42 | | int cmd, const char *name, |
43 | | const void *data, size_t data_len); |
44 | | static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx, |
45 | | int cmd, const char *name); |
46 | | static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx); |
47 | | |
48 | | typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void); |
49 | | typedef int sk_cmp_fn_type(const char *const *a, const char *const *b); |
50 | | |
51 | | static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL; |
52 | | |
53 | | /* This array needs to be in order of NIDs */ |
54 | | static pmeth_fn standard_methods[] = { |
55 | | ossl_rsa_pkey_method, |
56 | | # ifndef OPENSSL_NO_DH |
57 | | ossl_dh_pkey_method, |
58 | | # endif |
59 | | # ifndef OPENSSL_NO_DSA |
60 | | ossl_dsa_pkey_method, |
61 | | # endif |
62 | | # ifndef OPENSSL_NO_EC |
63 | | ossl_ec_pkey_method, |
64 | | # endif |
65 | | ossl_rsa_pss_pkey_method, |
66 | | # ifndef OPENSSL_NO_DH |
67 | | ossl_dhx_pkey_method, |
68 | | # endif |
69 | | # ifndef OPENSSL_NO_ECX |
70 | | ossl_ecx25519_pkey_method, |
71 | | ossl_ecx448_pkey_method, |
72 | | ossl_ed25519_pkey_method, |
73 | | ossl_ed448_pkey_method, |
74 | | # endif |
75 | | }; |
76 | | |
77 | | DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func); |
78 | | |
79 | | static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b) |
80 | 0 | { |
81 | 0 | return ((*a)->pkey_id - ((**b)())->pkey_id); |
82 | 0 | } |
83 | | |
84 | | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func); |
85 | | |
86 | | static int pmeth_cmp(const EVP_PKEY_METHOD *const *a, |
87 | | const EVP_PKEY_METHOD *const *b) |
88 | 0 | { |
89 | 0 | return ((*a)->pkey_id - (*b)->pkey_id); |
90 | 0 | } |
91 | | |
92 | | static const EVP_PKEY_METHOD *evp_pkey_meth_find_added_by_application(int type) |
93 | 0 | { |
94 | 0 | if (app_pkey_methods != NULL) { |
95 | 0 | int idx; |
96 | 0 | EVP_PKEY_METHOD tmp; |
97 | |
|
98 | 0 | tmp.pkey_id = type; |
99 | 0 | idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp); |
100 | 0 | if (idx >= 0) |
101 | 0 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); |
102 | 0 | } |
103 | 0 | return NULL; |
104 | 0 | } |
105 | | |
106 | | const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type) |
107 | 0 | { |
108 | 0 | pmeth_fn *ret; |
109 | 0 | EVP_PKEY_METHOD tmp; |
110 | 0 | const EVP_PKEY_METHOD *t; |
111 | |
|
112 | 0 | if ((t = evp_pkey_meth_find_added_by_application(type)) != NULL) |
113 | 0 | return t; |
114 | | |
115 | 0 | tmp.pkey_id = type; |
116 | 0 | t = &tmp; |
117 | 0 | ret = OBJ_bsearch_pmeth_func(&t, standard_methods, |
118 | 0 | OSSL_NELEM(standard_methods)); |
119 | 0 | if (ret == NULL || *ret == NULL) |
120 | 0 | return NULL; |
121 | 0 | return (**ret)(); |
122 | 0 | } |
123 | | |
124 | | EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags) |
125 | 0 | { |
126 | 0 | EVP_PKEY_METHOD *pmeth; |
127 | |
|
128 | 0 | pmeth = OPENSSL_zalloc(sizeof(*pmeth)); |
129 | 0 | if (pmeth == NULL) |
130 | 0 | return NULL; |
131 | | |
132 | 0 | pmeth->pkey_id = id; |
133 | 0 | pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC; |
134 | 0 | return pmeth; |
135 | 0 | } |
136 | | |
137 | | static void help_get_legacy_alg_type_from_keymgmt(const char *keytype, |
138 | | void *arg) |
139 | 0 | { |
140 | 0 | int *type = arg; |
141 | |
|
142 | 0 | if (*type == NID_undef) |
143 | 0 | *type = evp_pkey_name2type(keytype); |
144 | 0 | } |
145 | | |
146 | | static int get_legacy_alg_type_from_keymgmt(const EVP_KEYMGMT *keymgmt) |
147 | 0 | { |
148 | 0 | int type = NID_undef; |
149 | |
|
150 | 0 | EVP_KEYMGMT_names_do_all(keymgmt, help_get_legacy_alg_type_from_keymgmt, |
151 | 0 | &type); |
152 | 0 | return type; |
153 | 0 | } |
154 | | #endif /* FIPS_MODULE */ |
155 | | |
156 | | int evp_pkey_ctx_state(const EVP_PKEY_CTX *ctx) |
157 | 0 | { |
158 | 0 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) |
159 | 0 | return EVP_PKEY_STATE_UNKNOWN; |
160 | | |
161 | 0 | if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
162 | 0 | && ctx->op.kex.algctx != NULL) |
163 | 0 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
164 | 0 | && ctx->op.sig.algctx != NULL) |
165 | 0 | || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
166 | 0 | && ctx->op.ciph.algctx != NULL) |
167 | 0 | || (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
168 | 0 | && ctx->op.keymgmt.genctx != NULL) |
169 | 0 | || (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
170 | 0 | && ctx->op.encap.algctx != NULL)) |
171 | 0 | return EVP_PKEY_STATE_PROVIDER; |
172 | | |
173 | 0 | return EVP_PKEY_STATE_LEGACY; |
174 | 0 | } |
175 | | |
176 | | static EVP_PKEY_CTX *int_ctx_new(OSSL_LIB_CTX *libctx, |
177 | | EVP_PKEY *pkey, ENGINE *e, |
178 | | const char *keytype, const char *propquery, |
179 | | int id) |
180 | | |
181 | 0 | { |
182 | 0 | EVP_PKEY_CTX *ret = NULL; |
183 | 0 | const EVP_PKEY_METHOD *pmeth = NULL, *app_pmeth = NULL; |
184 | 0 | EVP_KEYMGMT *keymgmt = NULL; |
185 | | |
186 | | /* Code below to be removed when legacy support is dropped. */ |
187 | | /* BEGIN legacy */ |
188 | 0 | if (id == -1) { |
189 | 0 | if (pkey != NULL && !evp_pkey_is_provided(pkey)) { |
190 | 0 | id = pkey->type; |
191 | 0 | } else { |
192 | 0 | if (pkey != NULL) { |
193 | | /* Must be provided if we get here */ |
194 | 0 | keytype = EVP_KEYMGMT_get0_name(pkey->keymgmt); |
195 | 0 | } |
196 | 0 | #ifndef FIPS_MODULE |
197 | 0 | if (keytype != NULL) { |
198 | 0 | id = evp_pkey_name2type(keytype); |
199 | 0 | if (id == NID_undef) |
200 | 0 | id = -1; |
201 | 0 | } |
202 | 0 | #endif |
203 | 0 | } |
204 | 0 | } |
205 | | /* If no ID was found here, we can only resort to find a keymgmt */ |
206 | 0 | if (id == -1) { |
207 | 0 | #ifndef FIPS_MODULE |
208 | | /* Using engine with a key without id will not work */ |
209 | 0 | if (e != NULL) { |
210 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
211 | 0 | return NULL; |
212 | 0 | } |
213 | 0 | #endif |
214 | 0 | goto common; |
215 | 0 | } |
216 | | |
217 | 0 | #ifndef FIPS_MODULE |
218 | | /* |
219 | | * Here, we extract what information we can for the purpose of |
220 | | * supporting usage with implementations from providers, to make |
221 | | * for a smooth transition from legacy stuff to provider based stuff. |
222 | | * |
223 | | * If an engine is given, this is entirely legacy, and we should not |
224 | | * pretend anything else, so we clear the name. |
225 | | */ |
226 | 0 | if (e != NULL) |
227 | 0 | keytype = NULL; |
228 | 0 | if (e == NULL && (pkey == NULL || pkey->foreign == 0)) |
229 | 0 | keytype = OBJ_nid2sn(id); |
230 | |
|
231 | 0 | # ifndef OPENSSL_NO_ENGINE |
232 | 0 | if (e == NULL && pkey != NULL) |
233 | 0 | e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine; |
234 | | /* Try to find an ENGINE which implements this method */ |
235 | 0 | if (e != NULL) { |
236 | 0 | if (!ENGINE_init(e)) { |
237 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB); |
238 | 0 | return NULL; |
239 | 0 | } |
240 | 0 | } else { |
241 | 0 | e = ENGINE_get_pkey_meth_engine(id); |
242 | 0 | } |
243 | | |
244 | | /* |
245 | | * If an ENGINE handled this method look it up. Otherwise use internal |
246 | | * tables. |
247 | | */ |
248 | 0 | if (e != NULL) |
249 | 0 | pmeth = ENGINE_get_pkey_meth(e, id); |
250 | 0 | else |
251 | 0 | # endif /* OPENSSL_NO_ENGINE */ |
252 | 0 | if (pkey != NULL && pkey->foreign) |
253 | 0 | pmeth = EVP_PKEY_meth_find(id); |
254 | 0 | else |
255 | 0 | app_pmeth = pmeth = evp_pkey_meth_find_added_by_application(id); |
256 | | |
257 | | /* END legacy */ |
258 | 0 | #endif /* FIPS_MODULE */ |
259 | 0 | common: |
260 | | /* |
261 | | * If there's no engine and no app supplied pmeth and there's a name, we try |
262 | | * fetching a provider implementation. |
263 | | */ |
264 | 0 | if (e == NULL && app_pmeth == NULL && keytype != NULL) { |
265 | | /* |
266 | | * If |pkey| is given and is provided, we take a reference to its |
267 | | * keymgmt. Otherwise, we fetch one for the keytype we got. This |
268 | | * is to ensure that operation init functions can access what they |
269 | | * need through this single pointer. |
270 | | */ |
271 | 0 | if (pkey != NULL && pkey->keymgmt != NULL) { |
272 | 0 | if (!EVP_KEYMGMT_up_ref(pkey->keymgmt)) |
273 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
274 | 0 | else |
275 | 0 | keymgmt = pkey->keymgmt; |
276 | 0 | } else { |
277 | 0 | keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery); |
278 | 0 | } |
279 | 0 | if (keymgmt == NULL) |
280 | 0 | return NULL; /* EVP_KEYMGMT_fetch() recorded an error */ |
281 | | |
282 | 0 | #ifndef FIPS_MODULE |
283 | | /* |
284 | | * Chase down the legacy NID, as that might be needed for diverse |
285 | | * purposes, such as ensure that EVP_PKEY_type() can return sensible |
286 | | * values. We go through all keymgmt names, because the keytype |
287 | | * that's passed to this function doesn't necessarily translate |
288 | | * directly. |
289 | | */ |
290 | 0 | if (keymgmt != NULL) { |
291 | 0 | int tmp_id = get_legacy_alg_type_from_keymgmt(keymgmt); |
292 | |
|
293 | 0 | if (tmp_id != NID_undef) { |
294 | 0 | if (id == -1) { |
295 | 0 | id = tmp_id; |
296 | 0 | } else { |
297 | | /* |
298 | | * It really really shouldn't differ. If it still does, |
299 | | * something is very wrong. |
300 | | */ |
301 | 0 | if (!ossl_assert(id == tmp_id)) { |
302 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
303 | 0 | EVP_KEYMGMT_free(keymgmt); |
304 | 0 | return NULL; |
305 | 0 | } |
306 | 0 | } |
307 | 0 | } |
308 | 0 | } |
309 | 0 | #endif |
310 | 0 | } |
311 | | |
312 | 0 | if (pmeth == NULL && keymgmt == NULL) { |
313 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM); |
314 | 0 | } else { |
315 | 0 | ret = OPENSSL_zalloc(sizeof(*ret)); |
316 | 0 | } |
317 | |
|
318 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
319 | 0 | if ((ret == NULL || pmeth == NULL) && e != NULL) |
320 | 0 | ENGINE_finish(e); |
321 | 0 | #endif |
322 | |
|
323 | 0 | if (ret == NULL) { |
324 | 0 | EVP_KEYMGMT_free(keymgmt); |
325 | 0 | return NULL; |
326 | 0 | } |
327 | 0 | if (propquery != NULL) { |
328 | 0 | ret->propquery = OPENSSL_strdup(propquery); |
329 | 0 | if (ret->propquery == NULL) { |
330 | 0 | OPENSSL_free(ret); |
331 | 0 | EVP_KEYMGMT_free(keymgmt); |
332 | 0 | return NULL; |
333 | 0 | } |
334 | 0 | } |
335 | 0 | ret->libctx = libctx; |
336 | 0 | ret->keytype = keytype; |
337 | 0 | ret->keymgmt = keymgmt; |
338 | 0 | ret->legacy_keytype = id; |
339 | 0 | ret->engine = e; |
340 | 0 | ret->pmeth = pmeth; |
341 | 0 | ret->operation = EVP_PKEY_OP_UNDEFINED; |
342 | 0 | ret->pkey = pkey; |
343 | 0 | if (pkey != NULL) |
344 | 0 | EVP_PKEY_up_ref(pkey); |
345 | |
|
346 | 0 | if (pmeth != NULL && pmeth->init != NULL) { |
347 | 0 | if (pmeth->init(ret) <= 0) { |
348 | 0 | ret->pmeth = NULL; |
349 | 0 | EVP_PKEY_CTX_free(ret); |
350 | 0 | return NULL; |
351 | 0 | } |
352 | 0 | } |
353 | | |
354 | 0 | return ret; |
355 | 0 | } |
356 | | |
357 | | /*- All methods below can also be used in FIPS_MODULE */ |
358 | | |
359 | | EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OSSL_LIB_CTX *libctx, |
360 | | const char *name, |
361 | | const char *propquery) |
362 | 0 | { |
363 | 0 | return int_ctx_new(libctx, NULL, NULL, name, propquery, -1); |
364 | 0 | } |
365 | | |
366 | | EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OSSL_LIB_CTX *libctx, EVP_PKEY *pkey, |
367 | | const char *propquery) |
368 | 0 | { |
369 | 0 | return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1); |
370 | 0 | } |
371 | | |
372 | | void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx) |
373 | 0 | { |
374 | 0 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
375 | 0 | if (ctx->op.sig.algctx != NULL && ctx->op.sig.signature != NULL) |
376 | 0 | ctx->op.sig.signature->freectx(ctx->op.sig.algctx); |
377 | 0 | EVP_SIGNATURE_free(ctx->op.sig.signature); |
378 | 0 | ctx->op.sig.algctx = NULL; |
379 | 0 | ctx->op.sig.signature = NULL; |
380 | 0 | } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
381 | 0 | if (ctx->op.kex.algctx != NULL && ctx->op.kex.exchange != NULL) |
382 | 0 | ctx->op.kex.exchange->freectx(ctx->op.kex.algctx); |
383 | 0 | EVP_KEYEXCH_free(ctx->op.kex.exchange); |
384 | 0 | ctx->op.kex.algctx = NULL; |
385 | 0 | ctx->op.kex.exchange = NULL; |
386 | 0 | } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) { |
387 | 0 | if (ctx->op.encap.algctx != NULL && ctx->op.encap.kem != NULL) |
388 | 0 | ctx->op.encap.kem->freectx(ctx->op.encap.algctx); |
389 | 0 | EVP_KEM_free(ctx->op.encap.kem); |
390 | 0 | ctx->op.encap.algctx = NULL; |
391 | 0 | ctx->op.encap.kem = NULL; |
392 | 0 | } |
393 | 0 | else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { |
394 | 0 | if (ctx->op.ciph.algctx != NULL && ctx->op.ciph.cipher != NULL) |
395 | 0 | ctx->op.ciph.cipher->freectx(ctx->op.ciph.algctx); |
396 | 0 | EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher); |
397 | 0 | ctx->op.ciph.algctx = NULL; |
398 | 0 | ctx->op.ciph.cipher = NULL; |
399 | 0 | } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) { |
400 | 0 | if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL) |
401 | 0 | evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx); |
402 | 0 | } |
403 | 0 | } |
404 | | |
405 | | void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) |
406 | 8.77k | { |
407 | 8.77k | if (ctx == NULL) |
408 | 8.77k | return; |
409 | 0 | if (ctx->pmeth && ctx->pmeth->cleanup) |
410 | 0 | ctx->pmeth->cleanup(ctx); |
411 | |
|
412 | 0 | evp_pkey_ctx_free_old_ops(ctx); |
413 | 0 | #ifndef FIPS_MODULE |
414 | 0 | evp_pkey_ctx_free_all_cached_data(ctx); |
415 | 0 | #endif |
416 | 0 | EVP_KEYMGMT_free(ctx->keymgmt); |
417 | |
|
418 | 0 | OPENSSL_free(ctx->propquery); |
419 | 0 | EVP_PKEY_free(ctx->pkey); |
420 | 0 | EVP_PKEY_free(ctx->peerkey); |
421 | 0 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
422 | 0 | ENGINE_finish(ctx->engine); |
423 | 0 | #endif |
424 | 0 | BN_free(ctx->rsa_pubexp); |
425 | 0 | OPENSSL_free(ctx); |
426 | 0 | } |
427 | | |
428 | | #ifndef FIPS_MODULE |
429 | | |
430 | | void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, |
431 | | const EVP_PKEY_METHOD *meth) |
432 | 0 | { |
433 | 0 | if (ppkey_id) |
434 | 0 | *ppkey_id = meth->pkey_id; |
435 | 0 | if (pflags) |
436 | 0 | *pflags = meth->flags; |
437 | 0 | } |
438 | | |
439 | | void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src) |
440 | 0 | { |
441 | 0 | int pkey_id = dst->pkey_id; |
442 | 0 | int flags = dst->flags; |
443 | |
|
444 | 0 | *dst = *src; |
445 | | |
446 | | /* We only copy the function pointers so restore the other values */ |
447 | 0 | dst->pkey_id = pkey_id; |
448 | 0 | dst->flags = flags; |
449 | 0 | } |
450 | | |
451 | | void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth) |
452 | 0 | { |
453 | 0 | if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC)) |
454 | 0 | OPENSSL_free(pmeth); |
455 | 0 | } |
456 | | |
457 | | EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) |
458 | 0 | { |
459 | 0 | return int_ctx_new(NULL, pkey, e, NULL, NULL, -1); |
460 | 0 | } |
461 | | |
462 | | EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) |
463 | 0 | { |
464 | 0 | return int_ctx_new(NULL, NULL, e, NULL, NULL, id); |
465 | 0 | } |
466 | | |
467 | | EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx) |
468 | 0 | { |
469 | 0 | EVP_PKEY_CTX *rctx; |
470 | |
|
471 | 0 | # ifndef OPENSSL_NO_ENGINE |
472 | | /* Make sure it's safe to copy a pkey context using an ENGINE */ |
473 | 0 | if (pctx->engine && !ENGINE_init(pctx->engine)) { |
474 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB); |
475 | 0 | return 0; |
476 | 0 | } |
477 | 0 | # endif |
478 | 0 | rctx = OPENSSL_zalloc(sizeof(*rctx)); |
479 | 0 | if (rctx == NULL) |
480 | 0 | return NULL; |
481 | | |
482 | 0 | if (pctx->pkey != NULL) |
483 | 0 | EVP_PKEY_up_ref(pctx->pkey); |
484 | 0 | rctx->pkey = pctx->pkey; |
485 | 0 | rctx->operation = pctx->operation; |
486 | 0 | rctx->libctx = pctx->libctx; |
487 | 0 | rctx->keytype = pctx->keytype; |
488 | 0 | rctx->propquery = NULL; |
489 | 0 | if (pctx->propquery != NULL) { |
490 | 0 | rctx->propquery = OPENSSL_strdup(pctx->propquery); |
491 | 0 | if (rctx->propquery == NULL) |
492 | 0 | goto err; |
493 | 0 | } |
494 | 0 | rctx->legacy_keytype = pctx->legacy_keytype; |
495 | |
|
496 | 0 | if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) { |
497 | 0 | if (pctx->op.kex.exchange != NULL) { |
498 | 0 | rctx->op.kex.exchange = pctx->op.kex.exchange; |
499 | 0 | if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) |
500 | 0 | goto err; |
501 | 0 | } |
502 | 0 | if (pctx->op.kex.algctx != NULL) { |
503 | 0 | if (!ossl_assert(pctx->op.kex.exchange != NULL)) |
504 | 0 | goto err; |
505 | | |
506 | 0 | if (pctx->op.kex.exchange->dupctx != NULL) |
507 | 0 | rctx->op.kex.algctx |
508 | 0 | = pctx->op.kex.exchange->dupctx(pctx->op.kex.algctx); |
509 | |
|
510 | 0 | if (rctx->op.kex.algctx == NULL) { |
511 | 0 | EVP_KEYEXCH_free(rctx->op.kex.exchange); |
512 | 0 | rctx->op.kex.exchange = NULL; |
513 | 0 | goto err; |
514 | 0 | } |
515 | 0 | return rctx; |
516 | 0 | } |
517 | 0 | } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) { |
518 | 0 | if (pctx->op.sig.signature != NULL) { |
519 | 0 | rctx->op.sig.signature = pctx->op.sig.signature; |
520 | 0 | if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) |
521 | 0 | goto err; |
522 | 0 | } |
523 | 0 | if (pctx->op.sig.algctx != NULL) { |
524 | 0 | if (!ossl_assert(pctx->op.sig.signature != NULL)) |
525 | 0 | goto err; |
526 | | |
527 | 0 | if (pctx->op.sig.signature->dupctx != NULL) |
528 | 0 | rctx->op.sig.algctx |
529 | 0 | = pctx->op.sig.signature->dupctx(pctx->op.sig.algctx); |
530 | |
|
531 | 0 | if (rctx->op.sig.algctx == NULL) { |
532 | 0 | EVP_SIGNATURE_free(rctx->op.sig.signature); |
533 | 0 | rctx->op.sig.signature = NULL; |
534 | 0 | goto err; |
535 | 0 | } |
536 | 0 | return rctx; |
537 | 0 | } |
538 | 0 | } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) { |
539 | 0 | if (pctx->op.ciph.cipher != NULL) { |
540 | 0 | rctx->op.ciph.cipher = pctx->op.ciph.cipher; |
541 | 0 | if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) |
542 | 0 | goto err; |
543 | 0 | } |
544 | 0 | if (pctx->op.ciph.algctx != NULL) { |
545 | 0 | if (!ossl_assert(pctx->op.ciph.cipher != NULL)) |
546 | 0 | goto err; |
547 | | |
548 | 0 | if (pctx->op.ciph.cipher->dupctx != NULL) |
549 | 0 | rctx->op.ciph.algctx |
550 | 0 | = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.algctx); |
551 | |
|
552 | 0 | if (rctx->op.ciph.algctx == NULL) { |
553 | 0 | EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher); |
554 | 0 | rctx->op.ciph.cipher = NULL; |
555 | 0 | goto err; |
556 | 0 | } |
557 | 0 | return rctx; |
558 | 0 | } |
559 | 0 | } else if (EVP_PKEY_CTX_IS_KEM_OP(pctx)) { |
560 | 0 | if (pctx->op.encap.kem != NULL) { |
561 | 0 | rctx->op.encap.kem = pctx->op.encap.kem; |
562 | 0 | if (!EVP_KEM_up_ref(rctx->op.encap.kem)) |
563 | 0 | goto err; |
564 | 0 | } |
565 | 0 | if (pctx->op.encap.algctx != NULL) { |
566 | 0 | if (!ossl_assert(pctx->op.encap.kem != NULL)) |
567 | 0 | goto err; |
568 | | |
569 | 0 | if (pctx->op.encap.kem->dupctx != NULL) |
570 | 0 | rctx->op.encap.algctx |
571 | 0 | = pctx->op.encap.kem->dupctx(pctx->op.encap.algctx); |
572 | |
|
573 | 0 | if (rctx->op.encap.algctx == NULL) { |
574 | 0 | EVP_KEM_free(rctx->op.encap.kem); |
575 | 0 | rctx->op.encap.kem = NULL; |
576 | 0 | goto err; |
577 | 0 | } |
578 | 0 | return rctx; |
579 | 0 | } |
580 | 0 | } else if (EVP_PKEY_CTX_IS_GEN_OP(pctx)) { |
581 | | /* Not supported - This would need a gen_dupctx() to work */ |
582 | 0 | goto err; |
583 | 0 | } |
584 | | |
585 | 0 | rctx->pmeth = pctx->pmeth; |
586 | 0 | # ifndef OPENSSL_NO_ENGINE |
587 | 0 | rctx->engine = pctx->engine; |
588 | 0 | # endif |
589 | |
|
590 | 0 | if (pctx->peerkey != NULL) |
591 | 0 | EVP_PKEY_up_ref(pctx->peerkey); |
592 | 0 | rctx->peerkey = pctx->peerkey; |
593 | |
|
594 | 0 | if (pctx->pmeth == NULL) { |
595 | 0 | if (rctx->operation == EVP_PKEY_OP_UNDEFINED) { |
596 | 0 | EVP_KEYMGMT *tmp_keymgmt = pctx->keymgmt; |
597 | 0 | void *provkey; |
598 | |
|
599 | 0 | provkey = evp_pkey_export_to_provider(pctx->pkey, pctx->libctx, |
600 | 0 | &tmp_keymgmt, pctx->propquery); |
601 | 0 | if (provkey == NULL) |
602 | 0 | goto err; |
603 | 0 | if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) |
604 | 0 | goto err; |
605 | 0 | EVP_KEYMGMT_free(rctx->keymgmt); |
606 | 0 | rctx->keymgmt = tmp_keymgmt; |
607 | 0 | return rctx; |
608 | 0 | } |
609 | 0 | } else if (pctx->pmeth->copy(rctx, pctx) > 0) { |
610 | 0 | return rctx; |
611 | 0 | } |
612 | 0 | err: |
613 | 0 | rctx->pmeth = NULL; |
614 | 0 | EVP_PKEY_CTX_free(rctx); |
615 | 0 | return NULL; |
616 | 0 | } |
617 | | |
618 | | int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth) |
619 | 0 | { |
620 | 0 | if (app_pkey_methods == NULL) { |
621 | 0 | app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp); |
622 | 0 | if (app_pkey_methods == NULL) { |
623 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB); |
624 | 0 | return 0; |
625 | 0 | } |
626 | 0 | } |
627 | 0 | if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) { |
628 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB); |
629 | 0 | return 0; |
630 | 0 | } |
631 | 0 | sk_EVP_PKEY_METHOD_sort(app_pkey_methods); |
632 | 0 | return 1; |
633 | 0 | } |
634 | | |
635 | | void evp_app_cleanup_int(void) |
636 | 2 | { |
637 | 2 | if (app_pkey_methods != NULL) |
638 | 0 | sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free); |
639 | 2 | } |
640 | | |
641 | | int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth) |
642 | 0 | { |
643 | 0 | const EVP_PKEY_METHOD *ret; |
644 | |
|
645 | 0 | ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth); |
646 | |
|
647 | 0 | return ret == NULL ? 0 : 1; |
648 | 0 | } |
649 | | |
650 | | size_t EVP_PKEY_meth_get_count(void) |
651 | 0 | { |
652 | 0 | size_t rv = OSSL_NELEM(standard_methods); |
653 | |
|
654 | 0 | if (app_pkey_methods) |
655 | 0 | rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods); |
656 | 0 | return rv; |
657 | 0 | } |
658 | | |
659 | | const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx) |
660 | 0 | { |
661 | 0 | if (idx < OSSL_NELEM(standard_methods)) |
662 | 0 | return (standard_methods[idx])(); |
663 | 0 | if (app_pkey_methods == NULL) |
664 | 0 | return NULL; |
665 | 0 | idx -= OSSL_NELEM(standard_methods); |
666 | 0 | if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods)) |
667 | 0 | return NULL; |
668 | 0 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); |
669 | 0 | } |
670 | | #endif |
671 | | |
672 | | int EVP_PKEY_CTX_is_a(EVP_PKEY_CTX *ctx, const char *keytype) |
673 | 0 | { |
674 | 0 | #ifndef FIPS_MODULE |
675 | 0 | if (evp_pkey_ctx_is_legacy(ctx)) |
676 | 0 | return (ctx->pmeth->pkey_id == evp_pkey_name2type(keytype)); |
677 | 0 | #endif |
678 | 0 | return EVP_KEYMGMT_is_a(ctx->keymgmt, keytype); |
679 | 0 | } |
680 | | |
681 | | int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params) |
682 | 0 | { |
683 | 0 | switch (evp_pkey_ctx_state(ctx)) { |
684 | 0 | case EVP_PKEY_STATE_PROVIDER: |
685 | 0 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
686 | 0 | && ctx->op.kex.exchange != NULL |
687 | 0 | && ctx->op.kex.exchange->set_ctx_params != NULL) |
688 | 0 | return |
689 | 0 | ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.algctx, |
690 | 0 | params); |
691 | 0 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
692 | 0 | && ctx->op.sig.signature != NULL |
693 | 0 | && ctx->op.sig.signature->set_ctx_params != NULL) |
694 | 0 | return |
695 | 0 | ctx->op.sig.signature->set_ctx_params(ctx->op.sig.algctx, |
696 | 0 | params); |
697 | 0 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
698 | 0 | && ctx->op.ciph.cipher != NULL |
699 | 0 | && ctx->op.ciph.cipher->set_ctx_params != NULL) |
700 | 0 | return |
701 | 0 | ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.algctx, |
702 | 0 | params); |
703 | 0 | if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
704 | 0 | && ctx->keymgmt != NULL |
705 | 0 | && ctx->keymgmt->gen_set_params != NULL) |
706 | 0 | return |
707 | 0 | evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx, |
708 | 0 | params); |
709 | 0 | if (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
710 | 0 | && ctx->op.encap.kem != NULL |
711 | 0 | && ctx->op.encap.kem->set_ctx_params != NULL) |
712 | 0 | return |
713 | 0 | ctx->op.encap.kem->set_ctx_params(ctx->op.encap.algctx, |
714 | 0 | params); |
715 | 0 | break; |
716 | 0 | #ifndef FIPS_MODULE |
717 | 0 | case EVP_PKEY_STATE_UNKNOWN: |
718 | 0 | case EVP_PKEY_STATE_LEGACY: |
719 | 0 | return evp_pkey_ctx_set_params_to_ctrl(ctx, params); |
720 | 0 | #endif |
721 | 0 | } |
722 | 0 | return 0; |
723 | 0 | } |
724 | | |
725 | | int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
726 | 0 | { |
727 | 0 | switch (evp_pkey_ctx_state(ctx)) { |
728 | 0 | case EVP_PKEY_STATE_PROVIDER: |
729 | 0 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
730 | 0 | && ctx->op.kex.exchange != NULL |
731 | 0 | && ctx->op.kex.exchange->get_ctx_params != NULL) |
732 | 0 | return |
733 | 0 | ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.algctx, |
734 | 0 | params); |
735 | 0 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
736 | 0 | && ctx->op.sig.signature != NULL |
737 | 0 | && ctx->op.sig.signature->get_ctx_params != NULL) |
738 | 0 | return |
739 | 0 | ctx->op.sig.signature->get_ctx_params(ctx->op.sig.algctx, |
740 | 0 | params); |
741 | 0 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
742 | 0 | && ctx->op.ciph.cipher != NULL |
743 | 0 | && ctx->op.ciph.cipher->get_ctx_params != NULL) |
744 | 0 | return |
745 | 0 | ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.algctx, |
746 | 0 | params); |
747 | 0 | if (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
748 | 0 | && ctx->op.encap.kem != NULL |
749 | 0 | && ctx->op.encap.kem->get_ctx_params != NULL) |
750 | 0 | return |
751 | 0 | ctx->op.encap.kem->get_ctx_params(ctx->op.encap.algctx, |
752 | 0 | params); |
753 | 0 | break; |
754 | 0 | #ifndef FIPS_MODULE |
755 | 0 | case EVP_PKEY_STATE_UNKNOWN: |
756 | 0 | case EVP_PKEY_STATE_LEGACY: |
757 | 0 | return evp_pkey_ctx_get_params_to_ctrl(ctx, params); |
758 | 0 | #endif |
759 | 0 | } |
760 | 0 | return 0; |
761 | 0 | } |
762 | | |
763 | | #ifndef FIPS_MODULE |
764 | | const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(const EVP_PKEY_CTX *ctx) |
765 | 0 | { |
766 | 0 | void *provctx; |
767 | |
|
768 | 0 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
769 | 0 | && ctx->op.kex.exchange != NULL |
770 | 0 | && ctx->op.kex.exchange->gettable_ctx_params != NULL) { |
771 | 0 | provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange)); |
772 | 0 | return ctx->op.kex.exchange->gettable_ctx_params(ctx->op.kex.algctx, |
773 | 0 | provctx); |
774 | 0 | } |
775 | 0 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
776 | 0 | && ctx->op.sig.signature != NULL |
777 | 0 | && ctx->op.sig.signature->gettable_ctx_params != NULL) { |
778 | 0 | provctx = ossl_provider_ctx( |
779 | 0 | EVP_SIGNATURE_get0_provider(ctx->op.sig.signature)); |
780 | 0 | return ctx->op.sig.signature->gettable_ctx_params(ctx->op.sig.algctx, |
781 | 0 | provctx); |
782 | 0 | } |
783 | 0 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
784 | 0 | && ctx->op.ciph.cipher != NULL |
785 | 0 | && ctx->op.ciph.cipher->gettable_ctx_params != NULL) { |
786 | 0 | provctx = ossl_provider_ctx( |
787 | 0 | EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher)); |
788 | 0 | return ctx->op.ciph.cipher->gettable_ctx_params(ctx->op.ciph.algctx, |
789 | 0 | provctx); |
790 | 0 | } |
791 | 0 | if (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
792 | 0 | && ctx->op.encap.kem != NULL |
793 | 0 | && ctx->op.encap.kem->gettable_ctx_params != NULL) { |
794 | 0 | provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem)); |
795 | 0 | return ctx->op.encap.kem->gettable_ctx_params(ctx->op.encap.algctx, |
796 | 0 | provctx); |
797 | 0 | } |
798 | 0 | return NULL; |
799 | 0 | } |
800 | | |
801 | | const OSSL_PARAM *EVP_PKEY_CTX_settable_params(const EVP_PKEY_CTX *ctx) |
802 | 0 | { |
803 | 0 | void *provctx; |
804 | |
|
805 | 0 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
806 | 0 | && ctx->op.kex.exchange != NULL |
807 | 0 | && ctx->op.kex.exchange->settable_ctx_params != NULL) { |
808 | 0 | provctx = ossl_provider_ctx(EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange)); |
809 | 0 | return ctx->op.kex.exchange->settable_ctx_params(ctx->op.kex.algctx, |
810 | 0 | provctx); |
811 | 0 | } |
812 | 0 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
813 | 0 | && ctx->op.sig.signature != NULL |
814 | 0 | && ctx->op.sig.signature->settable_ctx_params != NULL) { |
815 | 0 | provctx = ossl_provider_ctx( |
816 | 0 | EVP_SIGNATURE_get0_provider(ctx->op.sig.signature)); |
817 | 0 | return ctx->op.sig.signature->settable_ctx_params(ctx->op.sig.algctx, |
818 | 0 | provctx); |
819 | 0 | } |
820 | 0 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
821 | 0 | && ctx->op.ciph.cipher != NULL |
822 | 0 | && ctx->op.ciph.cipher->settable_ctx_params != NULL) { |
823 | 0 | provctx = ossl_provider_ctx( |
824 | 0 | EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher)); |
825 | 0 | return ctx->op.ciph.cipher->settable_ctx_params(ctx->op.ciph.algctx, |
826 | 0 | provctx); |
827 | 0 | } |
828 | 0 | if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
829 | 0 | && ctx->keymgmt != NULL |
830 | 0 | && ctx->keymgmt->gen_settable_params != NULL) { |
831 | 0 | provctx = ossl_provider_ctx(EVP_KEYMGMT_get0_provider(ctx->keymgmt)); |
832 | 0 | return ctx->keymgmt->gen_settable_params(ctx->op.keymgmt.genctx, |
833 | 0 | provctx); |
834 | 0 | } |
835 | 0 | if (EVP_PKEY_CTX_IS_KEM_OP(ctx) |
836 | 0 | && ctx->op.encap.kem != NULL |
837 | 0 | && ctx->op.encap.kem->settable_ctx_params != NULL) { |
838 | 0 | provctx = ossl_provider_ctx(EVP_KEM_get0_provider(ctx->op.encap.kem)); |
839 | 0 | return ctx->op.encap.kem->settable_ctx_params(ctx->op.encap.algctx, |
840 | 0 | provctx); |
841 | 0 | } |
842 | 0 | return NULL; |
843 | 0 | } |
844 | | |
845 | | /* |
846 | | * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params(). |
847 | | * |
848 | | * Return 1 on success, 0 or negative for errors. |
849 | | * |
850 | | * In particular they return -2 if any of the params is not supported. |
851 | | * |
852 | | * They are not available in FIPS_MODULE as they depend on |
853 | | * - EVP_PKEY_CTX_{get,set}_params() |
854 | | * - EVP_PKEY_CTX_{gettable,settable}_params() |
855 | | * |
856 | | */ |
857 | | int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
858 | 0 | { |
859 | 0 | if (ctx == NULL || params == NULL) |
860 | 0 | return 0; |
861 | | |
862 | | /* |
863 | | * We only check for provider side EVP_PKEY_CTX. For #legacy, we |
864 | | * depend on the translation that happens in EVP_PKEY_CTX_set_params() |
865 | | * call, and that the resulting ctrl call will return -2 if it doesn't |
866 | | * known the ctrl command number. |
867 | | */ |
868 | 0 | if (evp_pkey_ctx_is_provided(ctx)) { |
869 | 0 | const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx); |
870 | 0 | const OSSL_PARAM *p; |
871 | |
|
872 | 0 | for (p = params; p->key != NULL; p++) { |
873 | | /* Check the ctx actually understands this parameter */ |
874 | 0 | if (OSSL_PARAM_locate_const(settable, p->key) == NULL) |
875 | 0 | return -2; |
876 | 0 | } |
877 | 0 | } |
878 | | |
879 | 0 | return EVP_PKEY_CTX_set_params(ctx, params); |
880 | 0 | } |
881 | | |
882 | | int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
883 | 0 | { |
884 | 0 | if (ctx == NULL || params == NULL) |
885 | 0 | return 0; |
886 | | |
887 | | /* |
888 | | * We only check for provider side EVP_PKEY_CTX. For #legacy, we |
889 | | * depend on the translation that happens in EVP_PKEY_CTX_get_params() |
890 | | * call, and that the resulting ctrl call will return -2 if it doesn't |
891 | | * known the ctrl command number. |
892 | | */ |
893 | 0 | if (evp_pkey_ctx_is_provided(ctx)) { |
894 | 0 | const OSSL_PARAM *gettable = EVP_PKEY_CTX_gettable_params(ctx); |
895 | 0 | const OSSL_PARAM *p; |
896 | |
|
897 | 0 | for (p = params; p->key != NULL; p++) { |
898 | | /* Check the ctx actually understands this parameter */ |
899 | 0 | if (OSSL_PARAM_locate_const(gettable, p->key) == NULL) |
900 | 0 | return -2; |
901 | 0 | } |
902 | 0 | } |
903 | | |
904 | 0 | return EVP_PKEY_CTX_get_params(ctx, params); |
905 | 0 | } |
906 | | |
907 | | int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md) |
908 | 0 | { |
909 | 0 | OSSL_PARAM sig_md_params[2], *p = sig_md_params; |
910 | | /* 80 should be big enough */ |
911 | 0 | char name[80] = ""; |
912 | 0 | const EVP_MD *tmp; |
913 | |
|
914 | 0 | if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
915 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
916 | | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
917 | 0 | return -2; |
918 | 0 | } |
919 | | |
920 | 0 | if (ctx->op.sig.algctx == NULL) |
921 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, |
922 | 0 | EVP_PKEY_CTRL_GET_MD, 0, (void *)(md)); |
923 | | |
924 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, |
925 | 0 | name, |
926 | 0 | sizeof(name)); |
927 | 0 | *p = OSSL_PARAM_construct_end(); |
928 | |
|
929 | 0 | if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params)) |
930 | 0 | return 0; |
931 | | |
932 | 0 | tmp = evp_get_digestbyname_ex(ctx->libctx, name); |
933 | 0 | if (tmp == NULL) |
934 | 0 | return 0; |
935 | | |
936 | 0 | *md = tmp; |
937 | |
|
938 | 0 | return 1; |
939 | 0 | } |
940 | | |
941 | | static int evp_pkey_ctx_set_md(EVP_PKEY_CTX *ctx, const EVP_MD *md, |
942 | | int fallback, const char *param, int op, |
943 | | int ctrl) |
944 | 0 | { |
945 | 0 | OSSL_PARAM md_params[2], *p = md_params; |
946 | 0 | const char *name; |
947 | |
|
948 | 0 | if (ctx == NULL || (ctx->operation & op) == 0) { |
949 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
950 | | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
951 | 0 | return -2; |
952 | 0 | } |
953 | | |
954 | 0 | if (fallback) |
955 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, 0, (void *)(md)); |
956 | | |
957 | 0 | if (md == NULL) { |
958 | 0 | name = ""; |
959 | 0 | } else { |
960 | 0 | name = EVP_MD_get0_name(md); |
961 | 0 | } |
962 | |
|
963 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(param, |
964 | | /* |
965 | | * Cast away the const. This is read |
966 | | * only so should be safe |
967 | | */ |
968 | 0 | (char *)name, 0); |
969 | 0 | *p = OSSL_PARAM_construct_end(); |
970 | |
|
971 | 0 | return EVP_PKEY_CTX_set_params(ctx, md_params); |
972 | 0 | } |
973 | | |
974 | | int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
975 | 0 | { |
976 | 0 | return evp_pkey_ctx_set_md(ctx, md, ctx->op.sig.algctx == NULL, |
977 | 0 | OSSL_SIGNATURE_PARAM_DIGEST, |
978 | 0 | EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD); |
979 | 0 | } |
980 | | |
981 | | int EVP_PKEY_CTX_set_tls1_prf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
982 | 0 | { |
983 | 0 | return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL, |
984 | 0 | OSSL_KDF_PARAM_DIGEST, |
985 | 0 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_TLS_MD); |
986 | 0 | } |
987 | | |
988 | | static int evp_pkey_ctx_set1_octet_string(EVP_PKEY_CTX *ctx, int fallback, |
989 | | const char *param, int op, int ctrl, |
990 | | const unsigned char *data, |
991 | | int datalen) |
992 | 0 | { |
993 | 0 | OSSL_PARAM octet_string_params[2], *p = octet_string_params; |
994 | |
|
995 | 0 | if (ctx == NULL || (ctx->operation & op) == 0) { |
996 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
997 | | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
998 | 0 | return -2; |
999 | 0 | } |
1000 | | |
1001 | | /* Code below to be removed when legacy support is dropped. */ |
1002 | 0 | if (fallback) |
1003 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, op, ctrl, datalen, (void *)(data)); |
1004 | | /* end of legacy support */ |
1005 | | |
1006 | 0 | if (datalen < 0) { |
1007 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_LENGTH); |
1008 | 0 | return 0; |
1009 | 0 | } |
1010 | | |
1011 | 0 | *p++ = OSSL_PARAM_construct_octet_string(param, |
1012 | | /* |
1013 | | * Cast away the const. This is read |
1014 | | * only so should be safe |
1015 | | */ |
1016 | 0 | (unsigned char *)data, |
1017 | 0 | (size_t)datalen); |
1018 | 0 | *p = OSSL_PARAM_construct_end(); |
1019 | |
|
1020 | 0 | return EVP_PKEY_CTX_set_params(ctx, octet_string_params); |
1021 | 0 | } |
1022 | | |
1023 | | int EVP_PKEY_CTX_set1_tls1_prf_secret(EVP_PKEY_CTX *ctx, |
1024 | | const unsigned char *sec, int seclen) |
1025 | 0 | { |
1026 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
1027 | 0 | OSSL_KDF_PARAM_SECRET, |
1028 | 0 | EVP_PKEY_OP_DERIVE, |
1029 | 0 | EVP_PKEY_CTRL_TLS_SECRET, |
1030 | 0 | sec, seclen); |
1031 | 0 | } |
1032 | | |
1033 | | int EVP_PKEY_CTX_add1_tls1_prf_seed(EVP_PKEY_CTX *ctx, |
1034 | | const unsigned char *seed, int seedlen) |
1035 | 0 | { |
1036 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
1037 | 0 | OSSL_KDF_PARAM_SEED, |
1038 | 0 | EVP_PKEY_OP_DERIVE, |
1039 | 0 | EVP_PKEY_CTRL_TLS_SEED, |
1040 | 0 | seed, seedlen); |
1041 | 0 | } |
1042 | | |
1043 | | int EVP_PKEY_CTX_set_hkdf_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
1044 | 0 | { |
1045 | 0 | return evp_pkey_ctx_set_md(ctx, md, ctx->op.kex.algctx == NULL, |
1046 | 0 | OSSL_KDF_PARAM_DIGEST, |
1047 | 0 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_HKDF_MD); |
1048 | 0 | } |
1049 | | |
1050 | | int EVP_PKEY_CTX_set1_hkdf_salt(EVP_PKEY_CTX *ctx, |
1051 | | const unsigned char *salt, int saltlen) |
1052 | 0 | { |
1053 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
1054 | 0 | OSSL_KDF_PARAM_SALT, |
1055 | 0 | EVP_PKEY_OP_DERIVE, |
1056 | 0 | EVP_PKEY_CTRL_HKDF_SALT, |
1057 | 0 | salt, saltlen); |
1058 | 0 | } |
1059 | | |
1060 | | int EVP_PKEY_CTX_set1_hkdf_key(EVP_PKEY_CTX *ctx, |
1061 | | const unsigned char *key, int keylen) |
1062 | 0 | { |
1063 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
1064 | 0 | OSSL_KDF_PARAM_KEY, |
1065 | 0 | EVP_PKEY_OP_DERIVE, |
1066 | 0 | EVP_PKEY_CTRL_HKDF_KEY, |
1067 | 0 | key, keylen); |
1068 | 0 | } |
1069 | | |
1070 | | int EVP_PKEY_CTX_add1_hkdf_info(EVP_PKEY_CTX *ctx, |
1071 | | const unsigned char *info, int infolen) |
1072 | 0 | { |
1073 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
1074 | 0 | OSSL_KDF_PARAM_INFO, |
1075 | 0 | EVP_PKEY_OP_DERIVE, |
1076 | 0 | EVP_PKEY_CTRL_HKDF_INFO, |
1077 | 0 | info, infolen); |
1078 | 0 | } |
1079 | | |
1080 | | int EVP_PKEY_CTX_set_hkdf_mode(EVP_PKEY_CTX *ctx, int mode) |
1081 | 0 | { |
1082 | 0 | OSSL_PARAM int_params[2], *p = int_params; |
1083 | |
|
1084 | 0 | if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
1085 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1086 | | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
1087 | 0 | return -2; |
1088 | 0 | } |
1089 | | |
1090 | | /* Code below to be removed when legacy support is dropped. */ |
1091 | 0 | if (ctx->op.kex.algctx == NULL) |
1092 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_DERIVE, |
1093 | 0 | EVP_PKEY_CTRL_HKDF_MODE, mode, NULL); |
1094 | | /* end of legacy support */ |
1095 | | |
1096 | 0 | if (mode < 0) { |
1097 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE); |
1098 | 0 | return 0; |
1099 | 0 | } |
1100 | | |
1101 | 0 | *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode); |
1102 | 0 | *p = OSSL_PARAM_construct_end(); |
1103 | |
|
1104 | 0 | return EVP_PKEY_CTX_set_params(ctx, int_params); |
1105 | 0 | } |
1106 | | |
1107 | | int EVP_PKEY_CTX_set1_pbe_pass(EVP_PKEY_CTX *ctx, const char *pass, |
1108 | | int passlen) |
1109 | 0 | { |
1110 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
1111 | 0 | OSSL_KDF_PARAM_PASSWORD, |
1112 | 0 | EVP_PKEY_OP_DERIVE, |
1113 | 0 | EVP_PKEY_CTRL_PASS, |
1114 | 0 | (const unsigned char *)pass, passlen); |
1115 | 0 | } |
1116 | | |
1117 | | int EVP_PKEY_CTX_set1_scrypt_salt(EVP_PKEY_CTX *ctx, |
1118 | | const unsigned char *salt, int saltlen) |
1119 | 0 | { |
1120 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.kex.algctx == NULL, |
1121 | 0 | OSSL_KDF_PARAM_SALT, |
1122 | 0 | EVP_PKEY_OP_DERIVE, |
1123 | 0 | EVP_PKEY_CTRL_SCRYPT_SALT, |
1124 | 0 | salt, saltlen); |
1125 | 0 | } |
1126 | | |
1127 | | static int evp_pkey_ctx_set_uint64(EVP_PKEY_CTX *ctx, const char *param, |
1128 | | int op, int ctrl, uint64_t val) |
1129 | 0 | { |
1130 | 0 | OSSL_PARAM uint64_params[2], *p = uint64_params; |
1131 | |
|
1132 | 0 | if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
1133 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1134 | | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ |
1135 | 0 | return -2; |
1136 | 0 | } |
1137 | | |
1138 | | /* Code below to be removed when legacy support is dropped. */ |
1139 | 0 | if (ctx->op.kex.algctx == NULL) |
1140 | 0 | return EVP_PKEY_CTX_ctrl_uint64(ctx, -1, op, ctrl, val); |
1141 | | /* end of legacy support */ |
1142 | | |
1143 | 0 | *p++ = OSSL_PARAM_construct_uint64(param, &val); |
1144 | 0 | *p = OSSL_PARAM_construct_end(); |
1145 | |
|
1146 | 0 | return EVP_PKEY_CTX_set_params(ctx, uint64_params); |
1147 | 0 | } |
1148 | | |
1149 | | int EVP_PKEY_CTX_set_scrypt_N(EVP_PKEY_CTX *ctx, uint64_t n) |
1150 | 0 | { |
1151 | 0 | return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_N, |
1152 | 0 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_N, |
1153 | 0 | n); |
1154 | 0 | } |
1155 | | |
1156 | | int EVP_PKEY_CTX_set_scrypt_r(EVP_PKEY_CTX *ctx, uint64_t r) |
1157 | 0 | { |
1158 | 0 | return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_R, |
1159 | 0 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_R, |
1160 | 0 | r); |
1161 | 0 | } |
1162 | | |
1163 | | int EVP_PKEY_CTX_set_scrypt_p(EVP_PKEY_CTX *ctx, uint64_t p) |
1164 | 0 | { |
1165 | 0 | return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_P, |
1166 | 0 | EVP_PKEY_OP_DERIVE, EVP_PKEY_CTRL_SCRYPT_P, |
1167 | 0 | p); |
1168 | 0 | } |
1169 | | |
1170 | | int EVP_PKEY_CTX_set_scrypt_maxmem_bytes(EVP_PKEY_CTX *ctx, |
1171 | | uint64_t maxmem_bytes) |
1172 | 0 | { |
1173 | 0 | return evp_pkey_ctx_set_uint64(ctx, OSSL_KDF_PARAM_SCRYPT_MAXMEM, |
1174 | 0 | EVP_PKEY_OP_DERIVE, |
1175 | 0 | EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, |
1176 | 0 | maxmem_bytes); |
1177 | 0 | } |
1178 | | |
1179 | | int EVP_PKEY_CTX_set_mac_key(EVP_PKEY_CTX *ctx, const unsigned char *key, |
1180 | | int keylen) |
1181 | 0 | { |
1182 | 0 | return evp_pkey_ctx_set1_octet_string(ctx, ctx->op.keymgmt.genctx == NULL, |
1183 | 0 | OSSL_PKEY_PARAM_PRIV_KEY, |
1184 | 0 | EVP_PKEY_OP_KEYGEN, |
1185 | 0 | EVP_PKEY_CTRL_SET_MAC_KEY, |
1186 | 0 | key, keylen); |
1187 | 0 | } |
1188 | | |
1189 | | int EVP_PKEY_CTX_set_kem_op(EVP_PKEY_CTX *ctx, const char *op) |
1190 | 0 | { |
1191 | 0 | OSSL_PARAM params[2], *p = params; |
1192 | |
|
1193 | 0 | if (ctx == NULL || op == NULL) { |
1194 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE); |
1195 | 0 | return 0; |
1196 | 0 | } |
1197 | 0 | if (!EVP_PKEY_CTX_IS_KEM_OP(ctx)) { |
1198 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1199 | 0 | return -2; |
1200 | 0 | } |
1201 | 0 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KEM_PARAM_OPERATION, |
1202 | 0 | (char *)op, 0); |
1203 | 0 | *p = OSSL_PARAM_construct_end(); |
1204 | 0 | return EVP_PKEY_CTX_set_params(ctx, params); |
1205 | 0 | } |
1206 | | |
1207 | | int EVP_PKEY_CTX_set1_id(EVP_PKEY_CTX *ctx, const void *id, int len) |
1208 | 0 | { |
1209 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, -1, |
1210 | 0 | EVP_PKEY_CTRL_SET1_ID, (int)len, (void*)(id)); |
1211 | 0 | } |
1212 | | |
1213 | | int EVP_PKEY_CTX_get1_id(EVP_PKEY_CTX *ctx, void *id) |
1214 | 0 | { |
1215 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET1_ID, 0, (void*)id); |
1216 | 0 | } |
1217 | | |
1218 | | int EVP_PKEY_CTX_get1_id_len(EVP_PKEY_CTX *ctx, size_t *id_len) |
1219 | 0 | { |
1220 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, -1, |
1221 | 0 | EVP_PKEY_CTRL_GET1_ID_LEN, 0, (void*)id_len); |
1222 | 0 | } |
1223 | | |
1224 | | static int evp_pkey_ctx_ctrl_int(EVP_PKEY_CTX *ctx, int keytype, int optype, |
1225 | | int cmd, int p1, void *p2) |
1226 | 0 | { |
1227 | 0 | int ret = 0; |
1228 | | |
1229 | | /* |
1230 | | * If the method has a |digest_custom| function, we can relax the |
1231 | | * operation type check, since this can be called before the operation |
1232 | | * is initialized. |
1233 | | */ |
1234 | 0 | if (ctx->pmeth == NULL || ctx->pmeth->digest_custom == NULL) { |
1235 | 0 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { |
1236 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_OPERATION_SET); |
1237 | 0 | return -1; |
1238 | 0 | } |
1239 | | |
1240 | 0 | if ((optype != -1) && !(ctx->operation & optype)) { |
1241 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
1242 | 0 | return -1; |
1243 | 0 | } |
1244 | 0 | } |
1245 | | |
1246 | 0 | switch (evp_pkey_ctx_state(ctx)) { |
1247 | 0 | case EVP_PKEY_STATE_PROVIDER: |
1248 | 0 | return evp_pkey_ctx_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2); |
1249 | 0 | case EVP_PKEY_STATE_UNKNOWN: |
1250 | 0 | case EVP_PKEY_STATE_LEGACY: |
1251 | 0 | if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) { |
1252 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1253 | 0 | return -2; |
1254 | 0 | } |
1255 | 0 | if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype)) |
1256 | 0 | return -1; |
1257 | | |
1258 | 0 | ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2); |
1259 | |
|
1260 | 0 | if (ret == -2) |
1261 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1262 | 0 | break; |
1263 | 0 | } |
1264 | 0 | return ret; |
1265 | 0 | } |
1266 | | |
1267 | | int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, |
1268 | | int cmd, int p1, void *p2) |
1269 | 0 | { |
1270 | 0 | int ret = 0; |
1271 | |
|
1272 | 0 | if (ctx == NULL) { |
1273 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1274 | 0 | return -2; |
1275 | 0 | } |
1276 | | /* If unsupported, we don't want that reported here */ |
1277 | 0 | ERR_set_mark(); |
1278 | 0 | ret = evp_pkey_ctx_store_cached_data(ctx, keytype, optype, |
1279 | 0 | cmd, NULL, p2, p1); |
1280 | 0 | if (ret == -2) { |
1281 | 0 | ERR_pop_to_mark(); |
1282 | 0 | } else { |
1283 | 0 | ERR_clear_last_mark(); |
1284 | | /* |
1285 | | * If there was an error, there was an error. |
1286 | | * If the operation isn't initialized yet, we also return, as |
1287 | | * the saved values will be used then anyway. |
1288 | | */ |
1289 | 0 | if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED) |
1290 | 0 | return ret; |
1291 | 0 | } |
1292 | 0 | return evp_pkey_ctx_ctrl_int(ctx, keytype, optype, cmd, p1, p2); |
1293 | 0 | } |
1294 | | |
1295 | | int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype, |
1296 | | int cmd, uint64_t value) |
1297 | 0 | { |
1298 | 0 | return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value); |
1299 | 0 | } |
1300 | | |
1301 | | |
1302 | | static int evp_pkey_ctx_ctrl_str_int(EVP_PKEY_CTX *ctx, |
1303 | | const char *name, const char *value) |
1304 | 0 | { |
1305 | 0 | int ret = 0; |
1306 | |
|
1307 | 0 | if (ctx == NULL) { |
1308 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1309 | 0 | return -2; |
1310 | 0 | } |
1311 | | |
1312 | 0 | switch (evp_pkey_ctx_state(ctx)) { |
1313 | 0 | case EVP_PKEY_STATE_PROVIDER: |
1314 | 0 | return evp_pkey_ctx_ctrl_str_to_param(ctx, name, value); |
1315 | 0 | case EVP_PKEY_STATE_UNKNOWN: |
1316 | 0 | case EVP_PKEY_STATE_LEGACY: |
1317 | 0 | if (ctx == NULL || ctx->pmeth == NULL || ctx->pmeth->ctrl_str == NULL) { |
1318 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1319 | 0 | return -2; |
1320 | 0 | } |
1321 | 0 | if (strcmp(name, "digest") == 0) |
1322 | 0 | ret = EVP_PKEY_CTX_md(ctx, |
1323 | 0 | EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, |
1324 | 0 | EVP_PKEY_CTRL_MD, value); |
1325 | 0 | else |
1326 | 0 | ret = ctx->pmeth->ctrl_str(ctx, name, value); |
1327 | 0 | break; |
1328 | 0 | } |
1329 | | |
1330 | 0 | return ret; |
1331 | 0 | } |
1332 | | |
1333 | | int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, |
1334 | | const char *name, const char *value) |
1335 | 0 | { |
1336 | 0 | int ret = 0; |
1337 | | |
1338 | | /* If unsupported, we don't want that reported here */ |
1339 | 0 | ERR_set_mark(); |
1340 | 0 | ret = evp_pkey_ctx_store_cached_data(ctx, -1, -1, -1, |
1341 | 0 | name, value, strlen(value) + 1); |
1342 | 0 | if (ret == -2) { |
1343 | 0 | ERR_pop_to_mark(); |
1344 | 0 | } else { |
1345 | 0 | ERR_clear_last_mark(); |
1346 | | /* |
1347 | | * If there was an error, there was an error. |
1348 | | * If the operation isn't initialized yet, we also return, as |
1349 | | * the saved values will be used then anyway. |
1350 | | */ |
1351 | 0 | if (ret < 1 || ctx->operation == EVP_PKEY_OP_UNDEFINED) |
1352 | 0 | return ret; |
1353 | 0 | } |
1354 | | |
1355 | 0 | return evp_pkey_ctx_ctrl_str_int(ctx, name, value); |
1356 | 0 | } |
1357 | | |
1358 | | static int decode_cmd(int cmd, const char *name) |
1359 | 0 | { |
1360 | 0 | if (cmd == -1) { |
1361 | | /* |
1362 | | * The consequence of the assertion not being true is that this |
1363 | | * function will return -1, which will cause the calling functions |
1364 | | * to signal that the command is unsupported... in non-debug mode. |
1365 | | */ |
1366 | 0 | if (ossl_assert(name != NULL)) |
1367 | 0 | if (strcmp(name, "distid") == 0 || strcmp(name, "hexdistid") == 0) |
1368 | 0 | cmd = EVP_PKEY_CTRL_SET1_ID; |
1369 | 0 | } |
1370 | |
|
1371 | 0 | return cmd; |
1372 | 0 | } |
1373 | | |
1374 | | static int evp_pkey_ctx_store_cached_data(EVP_PKEY_CTX *ctx, |
1375 | | int keytype, int optype, |
1376 | | int cmd, const char *name, |
1377 | | const void *data, size_t data_len) |
1378 | 0 | { |
1379 | | /* |
1380 | | * Check that it's one of the supported commands. The ctrl commands |
1381 | | * number cases here must correspond to the cases in the bottom switch |
1382 | | * in this function. |
1383 | | */ |
1384 | 0 | switch (cmd = decode_cmd(cmd, name)) { |
1385 | 0 | case EVP_PKEY_CTRL_SET1_ID: |
1386 | 0 | break; |
1387 | 0 | default: |
1388 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1389 | 0 | return -2; |
1390 | 0 | } |
1391 | | |
1392 | 0 | if (keytype != -1) { |
1393 | 0 | switch (evp_pkey_ctx_state(ctx)) { |
1394 | 0 | case EVP_PKEY_STATE_PROVIDER: |
1395 | 0 | if (ctx->keymgmt == NULL) { |
1396 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1397 | 0 | return -2; |
1398 | 0 | } |
1399 | 0 | if (!EVP_KEYMGMT_is_a(ctx->keymgmt, |
1400 | 0 | evp_pkey_type2name(keytype))) { |
1401 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
1402 | 0 | return -1; |
1403 | 0 | } |
1404 | 0 | break; |
1405 | 0 | case EVP_PKEY_STATE_UNKNOWN: |
1406 | 0 | case EVP_PKEY_STATE_LEGACY: |
1407 | 0 | if (ctx->pmeth == NULL) { |
1408 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
1409 | 0 | return -2; |
1410 | 0 | } |
1411 | 0 | if (EVP_PKEY_type(ctx->pmeth->pkey_id) != EVP_PKEY_type(keytype)) { |
1412 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
1413 | 0 | return -1; |
1414 | 0 | } |
1415 | 0 | break; |
1416 | 0 | } |
1417 | 0 | } |
1418 | 0 | if (optype != -1 && (ctx->operation & optype) == 0) { |
1419 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION); |
1420 | 0 | return -1; |
1421 | 0 | } |
1422 | | |
1423 | 0 | switch (cmd) { |
1424 | 0 | case EVP_PKEY_CTRL_SET1_ID: |
1425 | 0 | evp_pkey_ctx_free_cached_data(ctx, cmd, name); |
1426 | 0 | if (name != NULL) { |
1427 | 0 | ctx->cached_parameters.dist_id_name = OPENSSL_strdup(name); |
1428 | 0 | if (ctx->cached_parameters.dist_id_name == NULL) |
1429 | 0 | return 0; |
1430 | 0 | } |
1431 | 0 | if (data_len > 0) { |
1432 | 0 | ctx->cached_parameters.dist_id = OPENSSL_memdup(data, data_len); |
1433 | 0 | if (ctx->cached_parameters.dist_id == NULL) |
1434 | 0 | return 0; |
1435 | 0 | } |
1436 | 0 | ctx->cached_parameters.dist_id_set = 1; |
1437 | 0 | ctx->cached_parameters.dist_id_len = data_len; |
1438 | 0 | break; |
1439 | 0 | } |
1440 | 0 | return 1; |
1441 | 0 | } |
1442 | | |
1443 | | static void evp_pkey_ctx_free_cached_data(EVP_PKEY_CTX *ctx, |
1444 | | int cmd, const char *name) |
1445 | 0 | { |
1446 | 0 | cmd = decode_cmd(cmd, name); |
1447 | 0 | switch (cmd) { |
1448 | 0 | case EVP_PKEY_CTRL_SET1_ID: |
1449 | 0 | OPENSSL_free(ctx->cached_parameters.dist_id); |
1450 | 0 | OPENSSL_free(ctx->cached_parameters.dist_id_name); |
1451 | 0 | ctx->cached_parameters.dist_id = NULL; |
1452 | 0 | ctx->cached_parameters.dist_id_name = NULL; |
1453 | 0 | break; |
1454 | 0 | } |
1455 | 0 | } |
1456 | | |
1457 | | static void evp_pkey_ctx_free_all_cached_data(EVP_PKEY_CTX *ctx) |
1458 | 0 | { |
1459 | 0 | evp_pkey_ctx_free_cached_data(ctx, EVP_PKEY_CTRL_SET1_ID, NULL); |
1460 | 0 | } |
1461 | | |
1462 | | int evp_pkey_ctx_use_cached_data(EVP_PKEY_CTX *ctx) |
1463 | 0 | { |
1464 | 0 | int ret = 1; |
1465 | |
|
1466 | 0 | if (ret && ctx->cached_parameters.dist_id_set) { |
1467 | 0 | const char *name = ctx->cached_parameters.dist_id_name; |
1468 | 0 | const void *val = ctx->cached_parameters.dist_id; |
1469 | 0 | size_t len = ctx->cached_parameters.dist_id_len; |
1470 | |
|
1471 | 0 | if (name != NULL) |
1472 | 0 | ret = evp_pkey_ctx_ctrl_str_int(ctx, name, val); |
1473 | 0 | else |
1474 | 0 | ret = evp_pkey_ctx_ctrl_int(ctx, -1, ctx->operation, |
1475 | 0 | EVP_PKEY_CTRL_SET1_ID, |
1476 | 0 | (int)len, (void *)val); |
1477 | 0 | } |
1478 | |
|
1479 | 0 | return ret; |
1480 | 0 | } |
1481 | | |
1482 | | OSSL_LIB_CTX *EVP_PKEY_CTX_get0_libctx(EVP_PKEY_CTX *ctx) |
1483 | 0 | { |
1484 | 0 | return ctx->libctx; |
1485 | 0 | } |
1486 | | |
1487 | | const char *EVP_PKEY_CTX_get0_propq(const EVP_PKEY_CTX *ctx) |
1488 | 0 | { |
1489 | 0 | return ctx->propquery; |
1490 | 0 | } |
1491 | | |
1492 | | const OSSL_PROVIDER *EVP_PKEY_CTX_get0_provider(const EVP_PKEY_CTX *ctx) |
1493 | 0 | { |
1494 | 0 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
1495 | 0 | if (ctx->op.sig.signature != NULL) |
1496 | 0 | return EVP_SIGNATURE_get0_provider(ctx->op.sig.signature); |
1497 | 0 | } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
1498 | 0 | if (ctx->op.kex.exchange != NULL) |
1499 | 0 | return EVP_KEYEXCH_get0_provider(ctx->op.kex.exchange); |
1500 | 0 | } else if (EVP_PKEY_CTX_IS_KEM_OP(ctx)) { |
1501 | 0 | if (ctx->op.encap.kem != NULL) |
1502 | 0 | return EVP_KEM_get0_provider(ctx->op.encap.kem); |
1503 | 0 | } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { |
1504 | 0 | if (ctx->op.ciph.cipher != NULL) |
1505 | 0 | return EVP_ASYM_CIPHER_get0_provider(ctx->op.ciph.cipher); |
1506 | 0 | } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) { |
1507 | 0 | if (ctx->keymgmt != NULL) |
1508 | 0 | return EVP_KEYMGMT_get0_provider(ctx->keymgmt); |
1509 | 0 | } |
1510 | | |
1511 | 0 | return NULL; |
1512 | 0 | } |
1513 | | |
1514 | | /* Utility functions to send a string of hex string to a ctrl */ |
1515 | | |
1516 | | int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str) |
1517 | 0 | { |
1518 | 0 | size_t len; |
1519 | |
|
1520 | 0 | len = strlen(str); |
1521 | 0 | if (len > INT_MAX) |
1522 | 0 | return -1; |
1523 | 0 | return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str); |
1524 | 0 | } |
1525 | | |
1526 | | int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex) |
1527 | 0 | { |
1528 | 0 | unsigned char *bin; |
1529 | 0 | long binlen; |
1530 | 0 | int rv = -1; |
1531 | |
|
1532 | 0 | bin = OPENSSL_hexstr2buf(hex, &binlen); |
1533 | 0 | if (bin == NULL) |
1534 | 0 | return 0; |
1535 | 0 | if (binlen <= INT_MAX) |
1536 | 0 | rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin); |
1537 | 0 | OPENSSL_free(bin); |
1538 | 0 | return rv; |
1539 | 0 | } |
1540 | | |
1541 | | /* Pass a message digest to a ctrl */ |
1542 | | int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md) |
1543 | 0 | { |
1544 | 0 | const EVP_MD *m; |
1545 | |
|
1546 | 0 | if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) { |
1547 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_DIGEST); |
1548 | 0 | return 0; |
1549 | 0 | } |
1550 | 0 | return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m); |
1551 | 0 | } |
1552 | | |
1553 | | int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx) |
1554 | 0 | { |
1555 | 0 | return ctx->operation; |
1556 | 0 | } |
1557 | | |
1558 | | void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen) |
1559 | 0 | { |
1560 | 0 | ctx->keygen_info = dat; |
1561 | 0 | ctx->keygen_info_count = datlen; |
1562 | 0 | } |
1563 | | |
1564 | | void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data) |
1565 | 0 | { |
1566 | 0 | ctx->data = data; |
1567 | 0 | } |
1568 | | |
1569 | | void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx) |
1570 | 0 | { |
1571 | 0 | return ctx->data; |
1572 | 0 | } |
1573 | | |
1574 | | EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) |
1575 | 0 | { |
1576 | 0 | return ctx->pkey; |
1577 | 0 | } |
1578 | | |
1579 | | EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx) |
1580 | 0 | { |
1581 | 0 | return ctx->peerkey; |
1582 | 0 | } |
1583 | | |
1584 | | void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) |
1585 | 0 | { |
1586 | 0 | ctx->app_data = data; |
1587 | 0 | } |
1588 | | |
1589 | | void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) |
1590 | 0 | { |
1591 | 0 | return ctx->app_data; |
1592 | 0 | } |
1593 | | |
1594 | | void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth, |
1595 | | int (*init) (EVP_PKEY_CTX *ctx)) |
1596 | 0 | { |
1597 | 0 | pmeth->init = init; |
1598 | 0 | } |
1599 | | |
1600 | | void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth, |
1601 | | int (*copy) (EVP_PKEY_CTX *dst, |
1602 | | const EVP_PKEY_CTX *src)) |
1603 | 0 | { |
1604 | 0 | pmeth->copy = copy; |
1605 | 0 | } |
1606 | | |
1607 | | void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth, |
1608 | | void (*cleanup) (EVP_PKEY_CTX *ctx)) |
1609 | 0 | { |
1610 | 0 | pmeth->cleanup = cleanup; |
1611 | 0 | } |
1612 | | |
1613 | | void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth, |
1614 | | int (*paramgen_init) (EVP_PKEY_CTX *ctx), |
1615 | | int (*paramgen) (EVP_PKEY_CTX *ctx, |
1616 | | EVP_PKEY *pkey)) |
1617 | 0 | { |
1618 | 0 | pmeth->paramgen_init = paramgen_init; |
1619 | 0 | pmeth->paramgen = paramgen; |
1620 | 0 | } |
1621 | | |
1622 | | void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth, |
1623 | | int (*keygen_init) (EVP_PKEY_CTX *ctx), |
1624 | | int (*keygen) (EVP_PKEY_CTX *ctx, |
1625 | | EVP_PKEY *pkey)) |
1626 | 0 | { |
1627 | 0 | pmeth->keygen_init = keygen_init; |
1628 | 0 | pmeth->keygen = keygen; |
1629 | 0 | } |
1630 | | |
1631 | | void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth, |
1632 | | int (*sign_init) (EVP_PKEY_CTX *ctx), |
1633 | | int (*sign) (EVP_PKEY_CTX *ctx, |
1634 | | unsigned char *sig, size_t *siglen, |
1635 | | const unsigned char *tbs, |
1636 | | size_t tbslen)) |
1637 | 0 | { |
1638 | 0 | pmeth->sign_init = sign_init; |
1639 | 0 | pmeth->sign = sign; |
1640 | 0 | } |
1641 | | |
1642 | | void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth, |
1643 | | int (*verify_init) (EVP_PKEY_CTX *ctx), |
1644 | | int (*verify) (EVP_PKEY_CTX *ctx, |
1645 | | const unsigned char *sig, |
1646 | | size_t siglen, |
1647 | | const unsigned char *tbs, |
1648 | | size_t tbslen)) |
1649 | 0 | { |
1650 | 0 | pmeth->verify_init = verify_init; |
1651 | 0 | pmeth->verify = verify; |
1652 | 0 | } |
1653 | | |
1654 | | void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth, |
1655 | | int (*verify_recover_init) (EVP_PKEY_CTX |
1656 | | *ctx), |
1657 | | int (*verify_recover) (EVP_PKEY_CTX |
1658 | | *ctx, |
1659 | | unsigned char |
1660 | | *sig, |
1661 | | size_t *siglen, |
1662 | | const unsigned |
1663 | | char *tbs, |
1664 | | size_t tbslen)) |
1665 | 0 | { |
1666 | 0 | pmeth->verify_recover_init = verify_recover_init; |
1667 | 0 | pmeth->verify_recover = verify_recover; |
1668 | 0 | } |
1669 | | |
1670 | | void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth, |
1671 | | int (*signctx_init) (EVP_PKEY_CTX *ctx, |
1672 | | EVP_MD_CTX *mctx), |
1673 | | int (*signctx) (EVP_PKEY_CTX *ctx, |
1674 | | unsigned char *sig, |
1675 | | size_t *siglen, |
1676 | | EVP_MD_CTX *mctx)) |
1677 | 0 | { |
1678 | 0 | pmeth->signctx_init = signctx_init; |
1679 | 0 | pmeth->signctx = signctx; |
1680 | 0 | } |
1681 | | |
1682 | | void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth, |
1683 | | int (*verifyctx_init) (EVP_PKEY_CTX *ctx, |
1684 | | EVP_MD_CTX *mctx), |
1685 | | int (*verifyctx) (EVP_PKEY_CTX *ctx, |
1686 | | const unsigned char *sig, |
1687 | | int siglen, |
1688 | | EVP_MD_CTX *mctx)) |
1689 | 0 | { |
1690 | 0 | pmeth->verifyctx_init = verifyctx_init; |
1691 | 0 | pmeth->verifyctx = verifyctx; |
1692 | 0 | } |
1693 | | |
1694 | | void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth, |
1695 | | int (*encrypt_init) (EVP_PKEY_CTX *ctx), |
1696 | | int (*encryptfn) (EVP_PKEY_CTX *ctx, |
1697 | | unsigned char *out, |
1698 | | size_t *outlen, |
1699 | | const unsigned char *in, |
1700 | | size_t inlen)) |
1701 | 0 | { |
1702 | 0 | pmeth->encrypt_init = encrypt_init; |
1703 | 0 | pmeth->encrypt = encryptfn; |
1704 | 0 | } |
1705 | | |
1706 | | void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth, |
1707 | | int (*decrypt_init) (EVP_PKEY_CTX *ctx), |
1708 | | int (*decrypt) (EVP_PKEY_CTX *ctx, |
1709 | | unsigned char *out, |
1710 | | size_t *outlen, |
1711 | | const unsigned char *in, |
1712 | | size_t inlen)) |
1713 | 0 | { |
1714 | 0 | pmeth->decrypt_init = decrypt_init; |
1715 | 0 | pmeth->decrypt = decrypt; |
1716 | 0 | } |
1717 | | |
1718 | | void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth, |
1719 | | int (*derive_init) (EVP_PKEY_CTX *ctx), |
1720 | | int (*derive) (EVP_PKEY_CTX *ctx, |
1721 | | unsigned char *key, |
1722 | | size_t *keylen)) |
1723 | 0 | { |
1724 | 0 | pmeth->derive_init = derive_init; |
1725 | 0 | pmeth->derive = derive; |
1726 | 0 | } |
1727 | | |
1728 | | void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, |
1729 | | int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
1730 | | void *p2), |
1731 | | int (*ctrl_str) (EVP_PKEY_CTX *ctx, |
1732 | | const char *type, |
1733 | | const char *value)) |
1734 | 0 | { |
1735 | 0 | pmeth->ctrl = ctrl; |
1736 | 0 | pmeth->ctrl_str = ctrl_str; |
1737 | 0 | } |
1738 | | |
1739 | | void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth, |
1740 | | int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, |
1741 | | const unsigned char *tbs, size_t tbslen)) |
1742 | 0 | { |
1743 | 0 | pmeth->digestsign = digestsign; |
1744 | 0 | } |
1745 | | |
1746 | | void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth, |
1747 | | int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig, |
1748 | | size_t siglen, const unsigned char *tbs, |
1749 | | size_t tbslen)) |
1750 | 0 | { |
1751 | 0 | pmeth->digestverify = digestverify; |
1752 | 0 | } |
1753 | | |
1754 | | void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, |
1755 | | int (*check) (EVP_PKEY *pkey)) |
1756 | 0 | { |
1757 | 0 | pmeth->check = check; |
1758 | 0 | } |
1759 | | |
1760 | | void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth, |
1761 | | int (*check) (EVP_PKEY *pkey)) |
1762 | 0 | { |
1763 | 0 | pmeth->public_check = check; |
1764 | 0 | } |
1765 | | |
1766 | | void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth, |
1767 | | int (*check) (EVP_PKEY *pkey)) |
1768 | 0 | { |
1769 | 0 | pmeth->param_check = check; |
1770 | 0 | } |
1771 | | |
1772 | | void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth, |
1773 | | int (*digest_custom) (EVP_PKEY_CTX *ctx, |
1774 | | EVP_MD_CTX *mctx)) |
1775 | 0 | { |
1776 | 0 | pmeth->digest_custom = digest_custom; |
1777 | 0 | } |
1778 | | |
1779 | | void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth, |
1780 | | int (**pinit) (EVP_PKEY_CTX *ctx)) |
1781 | 0 | { |
1782 | 0 | *pinit = pmeth->init; |
1783 | 0 | } |
1784 | | |
1785 | | void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth, |
1786 | | int (**pcopy) (EVP_PKEY_CTX *dst, |
1787 | | const EVP_PKEY_CTX *src)) |
1788 | 0 | { |
1789 | 0 | *pcopy = pmeth->copy; |
1790 | 0 | } |
1791 | | |
1792 | | void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth, |
1793 | | void (**pcleanup) (EVP_PKEY_CTX *ctx)) |
1794 | 0 | { |
1795 | 0 | *pcleanup = pmeth->cleanup; |
1796 | 0 | } |
1797 | | |
1798 | | void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth, |
1799 | | int (**pparamgen_init) (EVP_PKEY_CTX *ctx), |
1800 | | int (**pparamgen) (EVP_PKEY_CTX *ctx, |
1801 | | EVP_PKEY *pkey)) |
1802 | 0 | { |
1803 | 0 | if (pparamgen_init) |
1804 | 0 | *pparamgen_init = pmeth->paramgen_init; |
1805 | 0 | if (pparamgen) |
1806 | 0 | *pparamgen = pmeth->paramgen; |
1807 | 0 | } |
1808 | | |
1809 | | void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth, |
1810 | | int (**pkeygen_init) (EVP_PKEY_CTX *ctx), |
1811 | | int (**pkeygen) (EVP_PKEY_CTX *ctx, |
1812 | | EVP_PKEY *pkey)) |
1813 | 0 | { |
1814 | 0 | if (pkeygen_init) |
1815 | 0 | *pkeygen_init = pmeth->keygen_init; |
1816 | 0 | if (pkeygen) |
1817 | 0 | *pkeygen = pmeth->keygen; |
1818 | 0 | } |
1819 | | |
1820 | | void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth, |
1821 | | int (**psign_init) (EVP_PKEY_CTX *ctx), |
1822 | | int (**psign) (EVP_PKEY_CTX *ctx, |
1823 | | unsigned char *sig, size_t *siglen, |
1824 | | const unsigned char *tbs, |
1825 | | size_t tbslen)) |
1826 | 0 | { |
1827 | 0 | if (psign_init) |
1828 | 0 | *psign_init = pmeth->sign_init; |
1829 | 0 | if (psign) |
1830 | 0 | *psign = pmeth->sign; |
1831 | 0 | } |
1832 | | |
1833 | | void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth, |
1834 | | int (**pverify_init) (EVP_PKEY_CTX *ctx), |
1835 | | int (**pverify) (EVP_PKEY_CTX *ctx, |
1836 | | const unsigned char *sig, |
1837 | | size_t siglen, |
1838 | | const unsigned char *tbs, |
1839 | | size_t tbslen)) |
1840 | 0 | { |
1841 | 0 | if (pverify_init) |
1842 | 0 | *pverify_init = pmeth->verify_init; |
1843 | 0 | if (pverify) |
1844 | 0 | *pverify = pmeth->verify; |
1845 | 0 | } |
1846 | | |
1847 | | void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth, |
1848 | | int (**pverify_recover_init) (EVP_PKEY_CTX |
1849 | | *ctx), |
1850 | | int (**pverify_recover) (EVP_PKEY_CTX |
1851 | | *ctx, |
1852 | | unsigned char |
1853 | | *sig, |
1854 | | size_t *siglen, |
1855 | | const unsigned |
1856 | | char *tbs, |
1857 | | size_t tbslen)) |
1858 | 0 | { |
1859 | 0 | if (pverify_recover_init) |
1860 | 0 | *pverify_recover_init = pmeth->verify_recover_init; |
1861 | 0 | if (pverify_recover) |
1862 | 0 | *pverify_recover = pmeth->verify_recover; |
1863 | 0 | } |
1864 | | |
1865 | | void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth, |
1866 | | int (**psignctx_init) (EVP_PKEY_CTX *ctx, |
1867 | | EVP_MD_CTX *mctx), |
1868 | | int (**psignctx) (EVP_PKEY_CTX *ctx, |
1869 | | unsigned char *sig, |
1870 | | size_t *siglen, |
1871 | | EVP_MD_CTX *mctx)) |
1872 | 0 | { |
1873 | 0 | if (psignctx_init) |
1874 | 0 | *psignctx_init = pmeth->signctx_init; |
1875 | 0 | if (psignctx) |
1876 | 0 | *psignctx = pmeth->signctx; |
1877 | 0 | } |
1878 | | |
1879 | | void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth, |
1880 | | int (**pverifyctx_init) (EVP_PKEY_CTX *ctx, |
1881 | | EVP_MD_CTX *mctx), |
1882 | | int (**pverifyctx) (EVP_PKEY_CTX *ctx, |
1883 | | const unsigned char *sig, |
1884 | | int siglen, |
1885 | | EVP_MD_CTX *mctx)) |
1886 | 0 | { |
1887 | 0 | if (pverifyctx_init) |
1888 | 0 | *pverifyctx_init = pmeth->verifyctx_init; |
1889 | 0 | if (pverifyctx) |
1890 | 0 | *pverifyctx = pmeth->verifyctx; |
1891 | 0 | } |
1892 | | |
1893 | | void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth, |
1894 | | int (**pencrypt_init) (EVP_PKEY_CTX *ctx), |
1895 | | int (**pencryptfn) (EVP_PKEY_CTX *ctx, |
1896 | | unsigned char *out, |
1897 | | size_t *outlen, |
1898 | | const unsigned char *in, |
1899 | | size_t inlen)) |
1900 | 0 | { |
1901 | 0 | if (pencrypt_init) |
1902 | 0 | *pencrypt_init = pmeth->encrypt_init; |
1903 | 0 | if (pencryptfn) |
1904 | 0 | *pencryptfn = pmeth->encrypt; |
1905 | 0 | } |
1906 | | |
1907 | | void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth, |
1908 | | int (**pdecrypt_init) (EVP_PKEY_CTX *ctx), |
1909 | | int (**pdecrypt) (EVP_PKEY_CTX *ctx, |
1910 | | unsigned char *out, |
1911 | | size_t *outlen, |
1912 | | const unsigned char *in, |
1913 | | size_t inlen)) |
1914 | 0 | { |
1915 | 0 | if (pdecrypt_init) |
1916 | 0 | *pdecrypt_init = pmeth->decrypt_init; |
1917 | 0 | if (pdecrypt) |
1918 | 0 | *pdecrypt = pmeth->decrypt; |
1919 | 0 | } |
1920 | | |
1921 | | void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth, |
1922 | | int (**pderive_init) (EVP_PKEY_CTX *ctx), |
1923 | | int (**pderive) (EVP_PKEY_CTX *ctx, |
1924 | | unsigned char *key, |
1925 | | size_t *keylen)) |
1926 | 0 | { |
1927 | 0 | if (pderive_init) |
1928 | 0 | *pderive_init = pmeth->derive_init; |
1929 | 0 | if (pderive) |
1930 | 0 | *pderive = pmeth->derive; |
1931 | 0 | } |
1932 | | |
1933 | | void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth, |
1934 | | int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
1935 | | void *p2), |
1936 | | int (**pctrl_str) (EVP_PKEY_CTX *ctx, |
1937 | | const char *type, |
1938 | | const char *value)) |
1939 | 0 | { |
1940 | 0 | if (pctrl) |
1941 | 0 | *pctrl = pmeth->ctrl; |
1942 | 0 | if (pctrl_str) |
1943 | 0 | *pctrl_str = pmeth->ctrl_str; |
1944 | 0 | } |
1945 | | |
1946 | | void EVP_PKEY_meth_get_digestsign(const EVP_PKEY_METHOD *pmeth, |
1947 | | int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, |
1948 | | const unsigned char *tbs, size_t tbslen)) |
1949 | 0 | { |
1950 | 0 | if (digestsign) |
1951 | 0 | *digestsign = pmeth->digestsign; |
1952 | 0 | } |
1953 | | |
1954 | | void EVP_PKEY_meth_get_digestverify(const EVP_PKEY_METHOD *pmeth, |
1955 | | int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig, |
1956 | | size_t siglen, const unsigned char *tbs, |
1957 | | size_t tbslen)) |
1958 | 0 | { |
1959 | 0 | if (digestverify) |
1960 | 0 | *digestverify = pmeth->digestverify; |
1961 | 0 | } |
1962 | | |
1963 | | void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth, |
1964 | | int (**pcheck) (EVP_PKEY *pkey)) |
1965 | 0 | { |
1966 | 0 | if (pcheck != NULL) |
1967 | 0 | *pcheck = pmeth->check; |
1968 | 0 | } |
1969 | | |
1970 | | void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth, |
1971 | | int (**pcheck) (EVP_PKEY *pkey)) |
1972 | 0 | { |
1973 | 0 | if (pcheck != NULL) |
1974 | 0 | *pcheck = pmeth->public_check; |
1975 | 0 | } |
1976 | | |
1977 | | void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth, |
1978 | | int (**pcheck) (EVP_PKEY *pkey)) |
1979 | 0 | { |
1980 | 0 | if (pcheck != NULL) |
1981 | 0 | *pcheck = pmeth->param_check; |
1982 | 0 | } |
1983 | | |
1984 | | void EVP_PKEY_meth_get_digest_custom(const EVP_PKEY_METHOD *pmeth, |
1985 | | int (**pdigest_custom) (EVP_PKEY_CTX *ctx, |
1986 | | EVP_MD_CTX *mctx)) |
1987 | 0 | { |
1988 | 0 | if (pdigest_custom != NULL) |
1989 | 0 | *pdigest_custom = pmeth->digest_custom; |
1990 | 0 | } |
1991 | | |
1992 | | #endif /* FIPS_MODULE */ |