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