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