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