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