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