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