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