]>
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> |
35aca9ec | 24 | #include "internal/cryptlib.h" |
25f2138b DMSP |
25 | #include "crypto/asn1.h" |
26 | #include "crypto/evp.h" | |
7165593c SL |
27 | #include "crypto/dh.h" |
28 | #include "internal/ffc.h" | |
99119000 | 29 | #include "internal/numbers.h" |
390acbeb | 30 | #include "internal/provider.h" |
706457b7 | 31 | #include "evp_local.h" |
0b6f3c66 | 32 | |
e683582b SL |
33 | #ifndef FIPS_MODE |
34 | ||
19bd1fa1 | 35 | typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void); |
0f113f3e | 36 | typedef int sk_cmp_fn_type(const char *const *a, const char *const *b); |
5ce278a7 | 37 | |
df2ee0e2 | 38 | static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL; |
0b6f3c66 | 39 | |
cefa762e | 40 | /* This array needs to be in order of NIDs */ |
19bd1fa1 | 41 | static pmeth_fn standard_methods[] = { |
e683582b | 42 | # ifndef OPENSSL_NO_RSA |
19bd1fa1 | 43 | rsa_pkey_method, |
e683582b SL |
44 | # endif |
45 | # ifndef OPENSSL_NO_DH | |
19bd1fa1 | 46 | dh_pkey_method, |
e683582b SL |
47 | # endif |
48 | # ifndef OPENSSL_NO_DSA | |
19bd1fa1 | 49 | dsa_pkey_method, |
e683582b SL |
50 | # endif |
51 | # ifndef OPENSSL_NO_EC | |
19bd1fa1 | 52 | ec_pkey_method, |
e683582b | 53 | # endif |
19bd1fa1 | 54 | hmac_pkey_method, |
e683582b | 55 | # ifndef OPENSSL_NO_CMAC |
19bd1fa1 | 56 | cmac_pkey_method, |
e683582b SL |
57 | # endif |
58 | # ifndef OPENSSL_NO_RSA | |
19bd1fa1 | 59 | rsa_pss_pkey_method, |
e683582b SL |
60 | # endif |
61 | # ifndef OPENSSL_NO_DH | |
19bd1fa1 | 62 | dhx_pkey_method, |
e683582b SL |
63 | # endif |
64 | # ifndef OPENSSL_NO_SCRYPT | |
19bd1fa1 | 65 | scrypt_pkey_method, |
e683582b | 66 | # endif |
19bd1fa1 | 67 | tls1_prf_pkey_method, |
e683582b | 68 | # ifndef OPENSSL_NO_EC |
19bd1fa1 PS |
69 | ecx25519_pkey_method, |
70 | ecx448_pkey_method, | |
e683582b | 71 | # endif |
19bd1fa1 | 72 | hkdf_pkey_method, |
e683582b | 73 | # ifndef OPENSSL_NO_POLY1305 |
19bd1fa1 | 74 | poly1305_pkey_method, |
e683582b SL |
75 | # endif |
76 | # ifndef OPENSSL_NO_SIPHASH | |
19bd1fa1 | 77 | siphash_pkey_method, |
e683582b SL |
78 | # endif |
79 | # ifndef OPENSSL_NO_EC | |
19bd1fa1 PS |
80 | ed25519_pkey_method, |
81 | ed448_pkey_method, | |
e683582b SL |
82 | # endif |
83 | # ifndef OPENSSL_NO_SM2 | |
19bd1fa1 | 84 | sm2_pkey_method, |
e683582b | 85 | # endif |
0f113f3e | 86 | }; |
0b6f3c66 | 87 | |
19bd1fa1 PS |
88 | DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func); |
89 | ||
90 | static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b) | |
91 | { | |
92 | return ((*a)->pkey_id - ((**b)())->pkey_id); | |
93 | } | |
94 | ||
95 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func); | |
babb3798 | 96 | |
0f113f3e MC |
97 | static int pmeth_cmp(const EVP_PKEY_METHOD *const *a, |
98 | const EVP_PKEY_METHOD *const *b) | |
99 | { | |
100 | return ((*a)->pkey_id - (*b)->pkey_id); | |
101 | } | |
0b6f3c66 | 102 | |
c9777d26 | 103 | const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type) |
0f113f3e | 104 | { |
19bd1fa1 | 105 | pmeth_fn *ret; |
0f113f3e | 106 | EVP_PKEY_METHOD tmp; |
19bd1fa1 | 107 | const EVP_PKEY_METHOD *t = &tmp; |
12a765a5 | 108 | |
0f113f3e MC |
109 | tmp.pkey_id = type; |
110 | if (app_pkey_methods) { | |
111 | int idx; | |
112 | idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp); | |
113 | if (idx >= 0) | |
114 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); | |
115 | } | |
19bd1fa1 PS |
116 | ret = OBJ_bsearch_pmeth_func(&t, standard_methods, |
117 | sizeof(standard_methods) / | |
118 | sizeof(pmeth_fn)); | |
12a765a5 | 119 | if (ret == NULL || *ret == NULL) |
0f113f3e | 120 | return NULL; |
19bd1fa1 | 121 | return (**ret)(); |
0f113f3e | 122 | } |
0b6f3c66 | 123 | |
e683582b SL |
124 | EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags) |
125 | { | |
126 | EVP_PKEY_METHOD *pmeth; | |
127 | ||
128 | pmeth = OPENSSL_zalloc(sizeof(*pmeth)); | |
129 | if (pmeth == NULL) { | |
130 | EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE); | |
131 | return NULL; | |
132 | } | |
133 | ||
134 | pmeth->pkey_id = id; | |
135 | pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC; | |
136 | return pmeth; | |
137 | } | |
138 | #endif /* FIPS_MODE */ | |
139 | ||
3ee348b0 RL |
140 | static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx, |
141 | EVP_PKEY *pkey, ENGINE *e, | |
4b9e90f4 | 142 | const char *keytype, const char *propquery, |
a07c17ef | 143 | int id) |
e683582b | 144 | |
0f113f3e MC |
145 | { |
146 | EVP_PKEY_CTX *ret; | |
d0ea49a8 | 147 | const EVP_PKEY_METHOD *pmeth = NULL; |
4b9e90f4 | 148 | EVP_KEYMGMT *keymgmt = NULL; |
d0ea49a8 RL |
149 | |
150 | /* | |
151 | * When using providers, the context is bound to the algo implementation | |
152 | * later. | |
153 | */ | |
154 | if (pkey == NULL && e == NULL && id == -1) | |
155 | goto common; | |
2f2e6b62 | 156 | |
982efd77 RL |
157 | /* |
158 | * If the key doesn't contain anything legacy, then it must be provided, | |
159 | * so we extract the necessary information and use that. | |
160 | */ | |
adc9f731 | 161 | if (pkey != NULL && pkey->type == EVP_PKEY_NONE) { |
982efd77 RL |
162 | /* If we have an engine, something went wrong somewhere... */ |
163 | if (!ossl_assert(e == NULL)) | |
164 | return NULL; | |
4b9e90f4 | 165 | keytype = evp_first_name(pkey->keymgmt->prov, pkey->keymgmt->name_id); |
982efd77 RL |
166 | goto common; |
167 | } | |
e683582b | 168 | #ifndef FIPS_MODE |
d0ea49a8 RL |
169 | /* TODO(3.0) Legacy code should be removed when all is provider based */ |
170 | /* BEGIN legacy */ | |
0f113f3e | 171 | if (id == -1) { |
a6465b3f | 172 | if (pkey == NULL) |
982efd77 | 173 | return NULL; |
2f2e6b62 | 174 | id = pkey->type; |
0f113f3e | 175 | } |
60653e5b RL |
176 | |
177 | /* | |
178 | * Here, we extract what information we can for the purpose of | |
179 | * supporting usage with implementations from providers, to make | |
180 | * for a smooth transition from legacy stuff to provider based stuff. | |
181 | * | |
182 | * If an engine is given, this is entirely legacy, and we should not | |
183 | * pretend anything else, so we only set the name when no engine is | |
184 | * given. If both are already given, someone made a mistake, and | |
185 | * since that can only happen internally, it's safe to make an | |
186 | * assertion. | |
187 | */ | |
4b9e90f4 | 188 | if (!ossl_assert(e == NULL || keytype == NULL)) |
60653e5b RL |
189 | return NULL; |
190 | if (e == NULL) | |
4b9e90f4 | 191 | keytype = OBJ_nid2sn(id); |
60653e5b | 192 | |
e683582b | 193 | # ifndef OPENSSL_NO_ENGINE |
c2976edf | 194 | if (e == NULL && pkey != NULL) |
d19b01ad | 195 | e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine; |
0f113f3e MC |
196 | /* Try to find an ENGINE which implements this method */ |
197 | if (e) { | |
198 | if (!ENGINE_init(e)) { | |
199 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB); | |
200 | return NULL; | |
201 | } | |
c2976edf | 202 | } else { |
0f113f3e | 203 | e = ENGINE_get_pkey_meth_engine(id); |
c2976edf | 204 | } |
0f113f3e MC |
205 | |
206 | /* | |
0d4fb843 | 207 | * If an ENGINE handled this method look it up. Otherwise use internal |
0f113f3e MC |
208 | * tables. |
209 | */ | |
0f113f3e MC |
210 | if (e) |
211 | pmeth = ENGINE_get_pkey_meth(e, id); | |
212 | else | |
e683582b | 213 | # endif |
0f113f3e | 214 | pmeth = EVP_PKEY_meth_find(id); |
c9777d26 | 215 | |
0f113f3e | 216 | if (pmeth == NULL) { |
e683582b | 217 | # ifndef OPENSSL_NO_ENGINE |
918a27fa | 218 | ENGINE_finish(e); |
e683582b | 219 | # endif |
0f113f3e MC |
220 | EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM); |
221 | return NULL; | |
222 | } | |
d0ea49a8 | 223 | /* END legacy */ |
e683582b | 224 | #endif /* FIPS_MODE */ |
d0ea49a8 | 225 | common: |
4b9e90f4 RL |
226 | /* |
227 | * If there's no engine and there's a name, we try fetching a provider | |
228 | * implementation. | |
229 | */ | |
5fcb97c6 MC |
230 | if (e == NULL && keytype != NULL) { |
231 | /* This could fail so ignore errors */ | |
232 | ERR_set_mark(); | |
4b9e90f4 | 233 | keymgmt = EVP_KEYMGMT_fetch(libctx, keytype, propquery); |
5fcb97c6 MC |
234 | ERR_pop_to_mark(); |
235 | } | |
4b9e90f4 | 236 | |
64b25758 | 237 | ret = OPENSSL_zalloc(sizeof(*ret)); |
90945fa3 | 238 | if (ret == NULL) { |
b5bcc053 | 239 | EVP_KEYMGMT_free(keymgmt); |
e683582b | 240 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE) |
7c96dbcd | 241 | ENGINE_finish(e); |
a63bf2c5 | 242 | #endif |
0f113f3e MC |
243 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE); |
244 | return NULL; | |
245 | } | |
3ee348b0 | 246 | ret->libctx = libctx; |
a07c17ef | 247 | ret->propquery = propquery; |
4b9e90f4 RL |
248 | ret->keytype = keytype; |
249 | ret->keymgmt = keymgmt; | |
0f113f3e MC |
250 | ret->engine = e; |
251 | ret->pmeth = pmeth; | |
252 | ret->operation = EVP_PKEY_OP_UNDEFINED; | |
253 | ret->pkey = pkey; | |
a6465b3f | 254 | if (pkey != NULL) |
03273d61 | 255 | EVP_PKEY_up_ref(pkey); |
0f113f3e | 256 | |
8b84b075 | 257 | if (pmeth != NULL && pmeth->init != NULL) { |
0f113f3e | 258 | if (pmeth->init(ret) <= 0) { |
83b4049a | 259 | ret->pmeth = NULL; |
0f113f3e MC |
260 | EVP_PKEY_CTX_free(ret); |
261 | return NULL; | |
262 | } | |
263 | } | |
264 | ||
265 | return ret; | |
266 | } | |
267 | ||
e683582b SL |
268 | /*- All methods below can also be used in FIPS_MODE */ |
269 | ||
270 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_name(OPENSSL_CTX *libctx, | |
271 | const char *name, | |
272 | const char *propquery) | |
273 | { | |
274 | return int_ctx_new(libctx, NULL, NULL, name, propquery, -1); | |
275 | } | |
276 | ||
2ee4a50a MC |
277 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_from_pkey(OPENSSL_CTX *libctx, EVP_PKEY *pkey, |
278 | const char *propquery) | |
e683582b | 279 | { |
2ee4a50a | 280 | return int_ctx_new(libctx, pkey, NULL, NULL, propquery, -1); |
e683582b SL |
281 | } |
282 | ||
864b89ce MC |
283 | void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx) |
284 | { | |
e683582b | 285 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
864b89ce MC |
286 | if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL) |
287 | ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx); | |
288 | EVP_SIGNATURE_free(ctx->op.sig.signature); | |
fb1ecf85 RL |
289 | ctx->op.sig.sigprovctx = NULL; |
290 | ctx->op.sig.signature = NULL; | |
62f49b90 | 291 | } else if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { |
e683582b SL |
292 | if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL) |
293 | ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx); | |
294 | EVP_KEYEXCH_free(ctx->op.kex.exchange); | |
295 | ctx->op.kex.exchprovctx = NULL; | |
296 | ctx->op.kex.exchange = NULL; | |
62f49b90 SL |
297 | } |
298 | /* TODO(3.0): add dependancies and uncomment this when available for fips mode */ | |
299 | #ifndef FIPS_MODE | |
300 | else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { | |
2c938e2e MC |
301 | if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL) |
302 | ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx); | |
303 | EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher); | |
304 | ctx->op.ciph.ciphprovctx = NULL; | |
305 | ctx->op.ciph.cipher = NULL; | |
62924755 RL |
306 | } else if (EVP_PKEY_CTX_IS_GEN_OP(ctx)) { |
307 | if (ctx->op.keymgmt.genctx != NULL && ctx->keymgmt != NULL) | |
308 | evp_keymgmt_gen_cleanup(ctx->keymgmt, ctx->op.keymgmt.genctx); | |
864b89ce | 309 | } |
e683582b | 310 | #endif |
864b89ce MC |
311 | } |
312 | ||
e683582b | 313 | void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) |
0f113f3e | 314 | { |
e683582b SL |
315 | if (ctx == NULL) |
316 | return; | |
317 | if (ctx->pmeth && ctx->pmeth->cleanup) | |
318 | ctx->pmeth->cleanup(ctx); | |
b4faea50 | 319 | |
e683582b SL |
320 | evp_pkey_ctx_free_old_ops(ctx); |
321 | EVP_KEYMGMT_free(ctx->keymgmt); | |
0f113f3e | 322 | |
e683582b SL |
323 | EVP_PKEY_free(ctx->pkey); |
324 | EVP_PKEY_free(ctx->peerkey); | |
325 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODE) | |
326 | ENGINE_finish(ctx->engine); | |
327 | #endif | |
328 | OPENSSL_free(ctx); | |
0f113f3e | 329 | } |
ba30bad5 | 330 | |
e683582b SL |
331 | #ifndef FIPS_MODE |
332 | ||
f830c68f | 333 | void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, |
0f113f3e MC |
334 | const EVP_PKEY_METHOD *meth) |
335 | { | |
336 | if (ppkey_id) | |
337 | *ppkey_id = meth->pkey_id; | |
338 | if (pflags) | |
339 | *pflags = meth->flags; | |
340 | } | |
f830c68f DSH |
341 | |
342 | void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src) | |
0f113f3e | 343 | { |
f830c68f | 344 | |
0f113f3e MC |
345 | dst->init = src->init; |
346 | dst->copy = src->copy; | |
347 | dst->cleanup = src->cleanup; | |
f830c68f | 348 | |
0f113f3e MC |
349 | dst->paramgen_init = src->paramgen_init; |
350 | dst->paramgen = src->paramgen; | |
f830c68f | 351 | |
0f113f3e MC |
352 | dst->keygen_init = src->keygen_init; |
353 | dst->keygen = src->keygen; | |
f830c68f | 354 | |
0f113f3e MC |
355 | dst->sign_init = src->sign_init; |
356 | dst->sign = src->sign; | |
f830c68f | 357 | |
0f113f3e MC |
358 | dst->verify_init = src->verify_init; |
359 | dst->verify = src->verify; | |
f830c68f | 360 | |
0f113f3e MC |
361 | dst->verify_recover_init = src->verify_recover_init; |
362 | dst->verify_recover = src->verify_recover; | |
f830c68f | 363 | |
0f113f3e MC |
364 | dst->signctx_init = src->signctx_init; |
365 | dst->signctx = src->signctx; | |
f830c68f | 366 | |
0f113f3e MC |
367 | dst->verifyctx_init = src->verifyctx_init; |
368 | dst->verifyctx = src->verifyctx; | |
f830c68f | 369 | |
0f113f3e MC |
370 | dst->encrypt_init = src->encrypt_init; |
371 | dst->encrypt = src->encrypt; | |
f830c68f | 372 | |
0f113f3e MC |
373 | dst->decrypt_init = src->decrypt_init; |
374 | dst->decrypt = src->decrypt; | |
f830c68f | 375 | |
0f113f3e MC |
376 | dst->derive_init = src->derive_init; |
377 | dst->derive = src->derive; | |
f830c68f | 378 | |
0f113f3e MC |
379 | dst->ctrl = src->ctrl; |
380 | dst->ctrl_str = src->ctrl_str; | |
2aee35d3 PY |
381 | |
382 | dst->check = src->check; | |
0f113f3e | 383 | } |
f830c68f | 384 | |
ba30bad5 | 385 | void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth) |
0f113f3e MC |
386 | { |
387 | if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC)) | |
388 | OPENSSL_free(pmeth); | |
389 | } | |
ba30bad5 | 390 | |
f5cda4cb | 391 | EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) |
0f113f3e | 392 | { |
3ee348b0 | 393 | return int_ctx_new(NULL, pkey, e, NULL, NULL, -1); |
0f113f3e | 394 | } |
f5cda4cb DSH |
395 | |
396 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) | |
0f113f3e | 397 | { |
3ee348b0 | 398 | return int_ctx_new(NULL, NULL, e, NULL, NULL, id); |
a07c17ef RL |
399 | } |
400 | ||
9fdcc21f | 401 | EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx) |
0f113f3e MC |
402 | { |
403 | EVP_PKEY_CTX *rctx; | |
ff64702b MC |
404 | |
405 | if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL)) | |
864b89ce MC |
406 | && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx) |
407 | && pctx->op.kex.exchprovctx == NULL) | |
408 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx) | |
409 | && pctx->op.sig.sigprovctx == NULL))) | |
0f113f3e | 410 | return NULL; |
e683582b | 411 | # ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
412 | /* Make sure it's safe to copy a pkey context using an ENGINE */ |
413 | if (pctx->engine && !ENGINE_init(pctx->engine)) { | |
414 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB); | |
415 | return 0; | |
416 | } | |
e683582b | 417 | # endif |
ff64702b | 418 | rctx = OPENSSL_zalloc(sizeof(*rctx)); |
3484236d F |
419 | if (rctx == NULL) { |
420 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 421 | return NULL; |
3484236d | 422 | } |
8bdcef40 | 423 | |
ff64702b MC |
424 | if (pctx->pkey != NULL) |
425 | EVP_PKEY_up_ref(pctx->pkey); | |
426 | rctx->pkey = pctx->pkey; | |
427 | rctx->operation = pctx->operation; | |
3ee348b0 | 428 | rctx->libctx = pctx->libctx; |
f23bc0b7 | 429 | rctx->keytype = pctx->keytype; |
a07c17ef | 430 | rctx->propquery = pctx->propquery; |
ff64702b | 431 | |
864b89ce MC |
432 | if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) { |
433 | if (pctx->op.kex.exchange != NULL) { | |
434 | rctx->op.kex.exchange = pctx->op.kex.exchange; | |
435 | if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) { | |
436 | OPENSSL_free(rctx); | |
437 | return NULL; | |
438 | } | |
ff64702b | 439 | } |
864b89ce MC |
440 | if (pctx->op.kex.exchprovctx != NULL) { |
441 | if (!ossl_assert(pctx->op.kex.exchange != NULL)) | |
442 | return NULL; | |
443 | rctx->op.kex.exchprovctx | |
444 | = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx); | |
445 | if (rctx->op.kex.exchprovctx == NULL) { | |
446 | EVP_KEYEXCH_free(rctx->op.kex.exchange); | |
447 | OPENSSL_free(rctx); | |
448 | return NULL; | |
449 | } | |
450 | return rctx; | |
451 | } | |
452 | } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) { | |
453 | if (pctx->op.sig.signature != NULL) { | |
454 | rctx->op.sig.signature = pctx->op.sig.signature; | |
455 | if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) { | |
456 | OPENSSL_free(rctx); | |
457 | return NULL; | |
458 | } | |
459 | } | |
460 | if (pctx->op.sig.sigprovctx != NULL) { | |
461 | if (!ossl_assert(pctx->op.sig.signature != NULL)) | |
462 | return NULL; | |
463 | rctx->op.sig.sigprovctx | |
464 | = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx); | |
465 | if (rctx->op.sig.sigprovctx == NULL) { | |
466 | EVP_SIGNATURE_free(rctx->op.sig.signature); | |
467 | OPENSSL_free(rctx); | |
468 | return NULL; | |
469 | } | |
470 | return rctx; | |
ff64702b | 471 | } |
2c938e2e MC |
472 | } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) { |
473 | if (pctx->op.ciph.cipher != NULL) { | |
474 | rctx->op.ciph.cipher = pctx->op.ciph.cipher; | |
475 | if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) { | |
476 | OPENSSL_free(rctx); | |
477 | return NULL; | |
478 | } | |
479 | } | |
480 | if (pctx->op.ciph.ciphprovctx != NULL) { | |
481 | if (!ossl_assert(pctx->op.ciph.cipher != NULL)) | |
482 | return NULL; | |
483 | rctx->op.ciph.ciphprovctx | |
484 | = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx); | |
485 | if (rctx->op.ciph.ciphprovctx == NULL) { | |
486 | EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher); | |
487 | OPENSSL_free(rctx); | |
488 | return NULL; | |
489 | } | |
490 | return rctx; | |
491 | } | |
ff64702b MC |
492 | } |
493 | ||
0f113f3e | 494 | rctx->pmeth = pctx->pmeth; |
e683582b | 495 | # ifndef OPENSSL_NO_ENGINE |
0f113f3e | 496 | rctx->engine = pctx->engine; |
e683582b | 497 | # endif |
8bdcef40 | 498 | |
0f113f3e | 499 | if (pctx->peerkey) |
03273d61 | 500 | EVP_PKEY_up_ref(pctx->peerkey); |
0f113f3e | 501 | rctx->peerkey = pctx->peerkey; |
8bdcef40 | 502 | |
0f113f3e MC |
503 | if (pctx->pmeth->copy(rctx, pctx) > 0) |
504 | return rctx; | |
8bdcef40 | 505 | |
83b4049a | 506 | rctx->pmeth = NULL; |
0f113f3e MC |
507 | EVP_PKEY_CTX_free(rctx); |
508 | return NULL; | |
8bdcef40 | 509 | |
0f113f3e | 510 | } |
8bdcef40 | 511 | |
ba30bad5 | 512 | int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth) |
0f113f3e MC |
513 | { |
514 | if (app_pkey_methods == NULL) { | |
515 | app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp); | |
3484236d F |
516 | if (app_pkey_methods == NULL){ |
517 | EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 518 | return 0; |
3484236d | 519 | } |
0f113f3e | 520 | } |
3484236d F |
521 | if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) { |
522 | EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 523 | return 0; |
3484236d | 524 | } |
0f113f3e MC |
525 | sk_EVP_PKEY_METHOD_sort(app_pkey_methods); |
526 | return 1; | |
527 | } | |
ba30bad5 | 528 | |
0822e89a PY |
529 | void evp_app_cleanup_int(void) |
530 | { | |
531 | if (app_pkey_methods != NULL) | |
532 | sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free); | |
533 | } | |
534 | ||
535 | int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth) | |
536 | { | |
537 | const EVP_PKEY_METHOD *ret; | |
538 | ||
539 | ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth); | |
540 | ||
541 | return ret == NULL ? 0 : 1; | |
542 | } | |
543 | ||
48ed9c23 DSH |
544 | size_t EVP_PKEY_meth_get_count(void) |
545 | { | |
546 | size_t rv = OSSL_NELEM(standard_methods); | |
547 | ||
548 | if (app_pkey_methods) | |
549 | rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods); | |
550 | return rv; | |
551 | } | |
552 | ||
553 | const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx) | |
554 | { | |
555 | if (idx < OSSL_NELEM(standard_methods)) | |
19bd1fa1 | 556 | return (standard_methods[idx])(); |
48ed9c23 DSH |
557 | if (app_pkey_methods == NULL) |
558 | return NULL; | |
559 | idx -= OSSL_NELEM(standard_methods); | |
560 | if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods)) | |
561 | return NULL; | |
562 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); | |
563 | } | |
e683582b | 564 | #endif |
48ed9c23 | 565 | |
e683582b | 566 | int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
0f113f3e | 567 | { |
e683582b SL |
568 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
569 | && ctx->op.kex.exchprovctx != NULL | |
570 | && ctx->op.kex.exchange != NULL | |
571 | && ctx->op.kex.exchange->set_ctx_params != NULL) | |
572 | return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx, | |
573 | params); | |
574 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
575 | && ctx->op.sig.sigprovctx != NULL | |
576 | && ctx->op.sig.signature != NULL | |
577 | && ctx->op.sig.signature->set_ctx_params != NULL) | |
578 | return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx, | |
579 | params); | |
580 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
581 | && ctx->op.ciph.ciphprovctx != NULL | |
582 | && ctx->op.ciph.cipher != NULL | |
583 | && ctx->op.ciph.cipher->set_ctx_params != NULL) | |
584 | return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx, | |
585 | params); | |
62924755 RL |
586 | if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
587 | && ctx->op.keymgmt.genctx != NULL | |
588 | && ctx->keymgmt != NULL | |
589 | && ctx->keymgmt->gen_set_params != NULL) | |
590 | return evp_keymgmt_gen_set_params(ctx->keymgmt, ctx->op.keymgmt.genctx, | |
591 | params); | |
e683582b | 592 | return 0; |
0f113f3e | 593 | } |
5da98aa6 | 594 | |
e683582b | 595 | #ifndef FIPS_MODE |
9c45222d MC |
596 | int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
597 | { | |
4fe54d67 NT |
598 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
599 | && ctx->op.kex.exchprovctx != NULL | |
600 | && ctx->op.kex.exchange != NULL | |
601 | && ctx->op.kex.exchange->get_ctx_params != NULL) | |
602 | return ctx->op.kex.exchange->get_ctx_params(ctx->op.kex.exchprovctx, | |
603 | params); | |
864b89ce MC |
604 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
605 | && ctx->op.sig.sigprovctx != NULL | |
606 | && ctx->op.sig.signature != NULL | |
607 | && ctx->op.sig.signature->get_ctx_params != NULL) | |
608 | return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx, | |
609 | params); | |
2c938e2e MC |
610 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
611 | && ctx->op.ciph.ciphprovctx != NULL | |
612 | && ctx->op.ciph.cipher != NULL | |
613 | && ctx->op.ciph.cipher->get_ctx_params != NULL) | |
614 | return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx, | |
615 | params); | |
2b9add69 RL |
616 | if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
617 | && ctx->op.keymgmt.genctx != NULL | |
618 | && ctx->keymgmt != NULL | |
619 | && ctx->keymgmt->gen_get_params != NULL) | |
620 | return evp_keymgmt_gen_get_params(ctx->keymgmt, ctx->op.keymgmt.genctx, | |
621 | params); | |
9c45222d MC |
622 | return 0; |
623 | } | |
624 | ||
625 | const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx) | |
626 | { | |
4fe54d67 NT |
627 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
628 | && ctx->op.kex.exchange != NULL | |
629 | && ctx->op.kex.exchange->gettable_ctx_params != NULL) | |
630 | return ctx->op.kex.exchange->gettable_ctx_params(); | |
864b89ce MC |
631 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
632 | && ctx->op.sig.signature != NULL | |
633 | && ctx->op.sig.signature->gettable_ctx_params != NULL) | |
634 | return ctx->op.sig.signature->gettable_ctx_params(); | |
9c45222d | 635 | |
2c938e2e MC |
636 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
637 | && ctx->op.ciph.cipher != NULL | |
638 | && ctx->op.ciph.cipher->gettable_ctx_params != NULL) | |
639 | return ctx->op.ciph.cipher->gettable_ctx_params(); | |
640 | ||
9c45222d MC |
641 | return NULL; |
642 | } | |
643 | ||
9c45222d MC |
644 | const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx) |
645 | { | |
864b89ce MC |
646 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
647 | && ctx->op.kex.exchange != NULL | |
648 | && ctx->op.kex.exchange->settable_ctx_params != NULL) | |
649 | return ctx->op.kex.exchange->settable_ctx_params(); | |
650 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
651 | && ctx->op.sig.signature != NULL | |
652 | && ctx->op.sig.signature->settable_ctx_params != NULL) | |
653 | return ctx->op.sig.signature->settable_ctx_params(); | |
2c938e2e MC |
654 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
655 | && ctx->op.ciph.cipher != NULL | |
656 | && ctx->op.ciph.cipher->settable_ctx_params != NULL) | |
657 | return ctx->op.ciph.cipher->settable_ctx_params(); | |
62924755 RL |
658 | if (EVP_PKEY_CTX_IS_GEN_OP(ctx) |
659 | && ctx->keymgmt != NULL | |
660 | && ctx->keymgmt->gen_settable_params != NULL) | |
661 | return evp_keymgmt_gen_settable_params(ctx->keymgmt); | |
9c45222d MC |
662 | |
663 | return NULL; | |
664 | } | |
665 | ||
4fe54d67 NT |
666 | /* |
667 | * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params(). | |
668 | * | |
669 | * Return 1 on success, 0 or negative for errors. | |
670 | * | |
671 | * In particular they return -2 if any of the params is not supported. | |
672 | * | |
673 | * They are not available in FIPS_MODE as they depend on | |
674 | * - EVP_PKEY_CTX_{get,set}_params() | |
675 | * - EVP_PKEY_CTX_{gettable,settable}_params() | |
676 | * | |
677 | */ | |
678 | int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) | |
679 | { | |
680 | const OSSL_PARAM *p; | |
681 | ||
682 | if (ctx == NULL || params == NULL) | |
683 | return 0; | |
684 | ||
685 | for (p = params; p->key != NULL; p++) { | |
686 | /* Check the ctx actually understands this parameter */ | |
687 | if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx), | |
688 | p->key) == NULL ) | |
689 | return -2; | |
690 | } | |
691 | ||
692 | return EVP_PKEY_CTX_set_params(ctx, params); | |
693 | } | |
694 | ||
695 | int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) | |
696 | { | |
697 | const OSSL_PARAM *p; | |
698 | ||
699 | if (ctx == NULL || params == NULL) | |
700 | return 0; | |
701 | ||
702 | for (p = params; p->key != NULL; p++ ) { | |
703 | /* Check the ctx actually understands this parameter */ | |
704 | if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx), | |
705 | p->key) == NULL ) | |
706 | return -2; | |
707 | } | |
708 | ||
709 | return EVP_PKEY_CTX_get_params(ctx, params); | |
710 | } | |
711 | ||
e683582b | 712 | # ifndef OPENSSL_NO_DH |
35aca9ec MC |
713 | int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad) |
714 | { | |
715 | OSSL_PARAM dh_pad_params[2]; | |
1c3ace68 | 716 | unsigned int upad = pad; |
35aca9ec | 717 | |
864b89ce MC |
718 | /* We use EVP_PKEY_CTX_ctrl return values */ |
719 | if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { | |
720 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
721 | return -2; | |
722 | } | |
723 | ||
35aca9ec | 724 | /* TODO(3.0): Remove this eventually when no more legacy */ |
864b89ce | 725 | if (ctx->op.kex.exchprovctx == NULL) |
35aca9ec MC |
726 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE, |
727 | EVP_PKEY_CTRL_DH_PAD, pad, NULL); | |
728 | ||
1c3ace68 | 729 | dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad); |
35aca9ec MC |
730 | dh_pad_params[1] = OSSL_PARAM_construct_end(); |
731 | ||
732 | return EVP_PKEY_CTX_set_params(ctx, dh_pad_params); | |
733 | } | |
e683582b | 734 | # endif |
35aca9ec | 735 | |
9c45222d MC |
736 | int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md) |
737 | { | |
738 | OSSL_PARAM sig_md_params[3], *p = sig_md_params; | |
739 | /* 80 should be big enough */ | |
740 | char name[80] = ""; | |
741 | const EVP_MD *tmp; | |
742 | ||
864b89ce | 743 | if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
9c45222d MC |
744 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
745 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
746 | return -2; | |
747 | } | |
748 | ||
749 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
864b89ce | 750 | if (ctx->op.sig.sigprovctx == NULL) |
9c45222d MC |
751 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, |
752 | EVP_PKEY_CTRL_GET_MD, 0, (void *)(md)); | |
753 | ||
754 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, | |
755 | name, | |
756 | sizeof(name)); | |
757 | *p++ = OSSL_PARAM_construct_end(); | |
758 | ||
759 | if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params)) | |
760 | return 0; | |
761 | ||
7606bed9 | 762 | tmp = evp_get_digestbyname_ex(ctx->libctx, name); |
9c45222d MC |
763 | if (tmp == NULL) |
764 | return 0; | |
765 | ||
766 | *md = tmp; | |
767 | ||
768 | return 1; | |
769 | } | |
770 | ||
4889dadc MC |
771 | int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
772 | { | |
00bc1ad9 | 773 | OSSL_PARAM sig_md_params[2], *p = sig_md_params; |
4889dadc MC |
774 | const char *name; |
775 | ||
864b89ce | 776 | if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
9c45222d MC |
777 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
778 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
779 | return -2; | |
780 | } | |
781 | ||
4889dadc | 782 | /* TODO(3.0): Remove this eventually when no more legacy */ |
864b89ce | 783 | if (ctx->op.sig.sigprovctx == NULL) |
4889dadc MC |
784 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, |
785 | EVP_PKEY_CTRL_MD, 0, (void *)(md)); | |
786 | ||
9c45222d MC |
787 | if (md == NULL) { |
788 | name = ""; | |
9c45222d | 789 | } else { |
9c45222d MC |
790 | name = EVP_MD_name(md); |
791 | } | |
4889dadc | 792 | |
9c45222d MC |
793 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, |
794 | /* | |
795 | * Cast away the const. This is read | |
796 | * only so should be safe | |
797 | */ | |
8b6ffd40 | 798 | (char *)name, 0); |
9c45222d | 799 | *p++ = OSSL_PARAM_construct_end(); |
4889dadc | 800 | |
9c45222d | 801 | return EVP_PKEY_CTX_set_params(ctx, sig_md_params); |
4889dadc MC |
802 | } |
803 | ||
35aca9ec MC |
804 | static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype, |
805 | int cmd, int p1, void *p2) | |
806 | { | |
71434aed DB |
807 | /* |
808 | * GOST CMS format is different for different cipher algorithms. | |
809 | * Most of other algorithms don't have such a difference | |
810 | * so this ctrl is just ignored. | |
811 | */ | |
812 | if (cmd == EVP_PKEY_CTRL_CIPHER) | |
813 | return -2; | |
7165593c | 814 | |
e683582b | 815 | # ifndef OPENSSL_NO_DH |
4fe54d67 NT |
816 | if (keytype == EVP_PKEY_DH) { |
817 | switch (cmd) { | |
818 | case EVP_PKEY_CTRL_DH_PAD: | |
819 | return EVP_PKEY_CTX_set_dh_pad(ctx, p1); | |
7165593c SL |
820 | case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN: |
821 | return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, p1); | |
822 | case EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN: | |
823 | return EVP_PKEY_CTX_set_dh_paramgen_subprime_len(ctx, p1); | |
824 | case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR: | |
825 | return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, p1); | |
826 | case EVP_PKEY_CTRL_DH_PARAMGEN_TYPE: | |
827 | return EVP_PKEY_CTX_set_dh_paramgen_type(ctx, p1); | |
828 | case EVP_PKEY_CTRL_DH_RFC5114: | |
829 | return EVP_PKEY_CTX_set_dh_rfc5114(ctx, p1); | |
4fe54d67 NT |
830 | } |
831 | } | |
832 | # endif | |
b03ec3b5 SL |
833 | # ifndef OPENSSL_NO_DSA |
834 | if (keytype == EVP_PKEY_DSA) { | |
835 | switch (cmd) { | |
836 | case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS: | |
837 | return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, p1); | |
838 | case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS: | |
839 | return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, p1); | |
840 | case EVP_PKEY_CTRL_DSA_PARAMGEN_MD: | |
841 | return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, p2); | |
842 | } | |
843 | } | |
844 | # endif | |
4fe54d67 NT |
845 | # ifndef OPENSSL_NO_EC |
846 | if (keytype == EVP_PKEY_EC) { | |
847 | switch (cmd) { | |
10d756a7 RL |
848 | case EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID: |
849 | return EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx, p1); | |
6f4b7663 RL |
850 | case EVP_PKEY_CTRL_EC_ECDH_COFACTOR: |
851 | if (p1 == -2) { | |
852 | return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx); | |
853 | } else if (p1 < -1 || p1 > 1) { | |
854 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
855 | return -2; | |
856 | } else { | |
857 | return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, p1); | |
858 | } | |
859 | case EVP_PKEY_CTRL_EC_KDF_TYPE: | |
860 | if (p1 == -2) { | |
861 | return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx); | |
862 | } else { | |
863 | return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, p1); | |
864 | } | |
865 | case EVP_PKEY_CTRL_GET_EC_KDF_MD: | |
866 | return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, p2); | |
867 | case EVP_PKEY_CTRL_EC_KDF_MD: | |
868 | return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, p2); | |
869 | case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN: | |
870 | return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, p2); | |
871 | case EVP_PKEY_CTRL_EC_KDF_OUTLEN: | |
872 | return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, p1); | |
873 | case EVP_PKEY_CTRL_GET_EC_KDF_UKM: | |
874 | return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p2); | |
875 | case EVP_PKEY_CTRL_EC_KDF_UKM: | |
876 | return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p2, p1); | |
4fe54d67 NT |
877 | } |
878 | } | |
e683582b | 879 | # endif |
2decdad3 RL |
880 | if (keytype == EVP_PKEY_RSA) { |
881 | switch (cmd) { | |
882 | case EVP_PKEY_CTRL_RSA_OAEP_MD: | |
883 | return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2); | |
884 | case EVP_PKEY_CTRL_GET_RSA_OAEP_MD: | |
885 | return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2); | |
886 | case EVP_PKEY_CTRL_RSA_MGF1_MD: | |
887 | return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2); | |
888 | case EVP_PKEY_CTRL_RSA_OAEP_LABEL: | |
889 | return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1); | |
890 | case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL: | |
891 | return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2); | |
892 | case EVP_PKEY_CTRL_RSA_KEYGEN_BITS: | |
893 | return EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, p1); | |
894 | case EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP: | |
895 | return EVP_PKEY_CTX_set_rsa_keygen_pubexp(ctx, p2); | |
896 | case EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES: | |
897 | return EVP_PKEY_CTX_set_rsa_keygen_primes(ctx, p1); | |
898 | } | |
899 | } | |
900 | /* | |
901 | * keytype == -1 is used when several key types share the same structure, | |
902 | * or for generic controls that are the same across multiple key types. | |
903 | */ | |
4fe54d67 NT |
904 | if (keytype == -1) { |
905 | switch (cmd) { | |
6f4b7663 RL |
906 | case EVP_PKEY_CTRL_MD: |
907 | return EVP_PKEY_CTX_set_signature_md(ctx, p2); | |
908 | case EVP_PKEY_CTRL_GET_MD: | |
909 | return EVP_PKEY_CTX_get_signature_md(ctx, p2); | |
910 | case EVP_PKEY_CTRL_RSA_PADDING: | |
911 | return EVP_PKEY_CTX_set_rsa_padding(ctx, p1); | |
912 | case EVP_PKEY_CTRL_GET_RSA_PADDING: | |
913 | return EVP_PKEY_CTX_get_rsa_padding(ctx, p2); | |
6f4b7663 RL |
914 | case EVP_PKEY_CTRL_GET_RSA_MGF1_MD: |
915 | return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2); | |
6f4b7663 RL |
916 | case EVP_PKEY_CTRL_RSA_PSS_SALTLEN: |
917 | return EVP_PKEY_CTX_set_rsa_pss_saltlen(ctx, p1); | |
918 | case EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN: | |
919 | return EVP_PKEY_CTX_get_rsa_pss_saltlen(ctx, p2); | |
920 | case EVP_PKEY_CTRL_PKCS7_ENCRYPT: | |
921 | case EVP_PKEY_CTRL_PKCS7_DECRYPT: | |
e683582b | 922 | # ifndef OPENSSL_NO_CMS |
6f4b7663 RL |
923 | case EVP_PKEY_CTRL_CMS_DECRYPT: |
924 | case EVP_PKEY_CTRL_CMS_ENCRYPT: | |
e683582b | 925 | # endif |
cc572c25 RL |
926 | /* TODO (3.0) Temporary hack, this should probe */ |
927 | if (!EVP_PKEY_is_a(EVP_PKEY_CTX_get0_pkey(ctx), "RSASSA-PSS")) | |
6f4b7663 RL |
928 | return 1; |
929 | ERR_raise(ERR_LIB_EVP, | |
930 | EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); | |
931 | return -2; | |
4fe54d67 | 932 | } |
35aca9ec MC |
933 | } |
934 | return 0; | |
935 | } | |
936 | ||
0b6f3c66 | 937 | int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, |
0f113f3e MC |
938 | int cmd, int p1, void *p2) |
939 | { | |
940 | int ret; | |
4803717f | 941 | |
35aca9ec MC |
942 | if (ctx == NULL) { |
943 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
944 | return -2; | |
945 | } | |
946 | ||
864b89ce | 947 | if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL) |
48bb9792 | 948 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
2c938e2e MC |
949 | && ctx->op.sig.sigprovctx != NULL) |
950 | || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
62924755 RL |
951 | && ctx->op.ciph.ciphprovctx != NULL) |
952 | || (EVP_PKEY_CTX_IS_GEN_OP(ctx) | |
953 | && ctx->op.keymgmt.genctx != NULL)) | |
35aca9ec MC |
954 | return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2); |
955 | ||
956 | if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) { | |
0f113f3e MC |
957 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); |
958 | return -2; | |
959 | } | |
960 | if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype)) | |
961 | return -1; | |
962 | ||
4803717f PY |
963 | /* Skip the operation checks since this is called in a very early stage */ |
964 | if (ctx->pmeth->digest_custom != NULL) | |
965 | goto doit; | |
966 | ||
0f113f3e MC |
967 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { |
968 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET); | |
969 | return -1; | |
970 | } | |
971 | ||
972 | if ((optype != -1) && !(ctx->operation & optype)) { | |
973 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION); | |
974 | return -1; | |
975 | } | |
976 | ||
4803717f | 977 | doit: |
0f113f3e MC |
978 | ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2); |
979 | ||
980 | if (ret == -2) | |
981 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
982 | ||
983 | return ret; | |
0f113f3e | 984 | } |
0b6f3c66 | 985 | |
cefa762e | 986 | int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype, |
64bf1016 | 987 | int cmd, uint64_t value) |
cefa762e JB |
988 | { |
989 | return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value); | |
990 | } | |
991 | ||
35aca9ec MC |
992 | static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name, |
993 | const char *value) | |
994 | { | |
10d756a7 RL |
995 | |
996 | /* Special cases that we intercept */ | |
997 | # ifndef OPENSSL_NO_EC | |
998 | /* | |
999 | * We don't support encoding settings for providers, i.e. the only | |
1000 | * possible encoding is "named_curve", so we simply fail when something | |
1001 | * else is given, and otherwise just pretend all is fine. | |
1002 | */ | |
1003 | if (strcmp(name, "ec_param_enc") == 0) { | |
1004 | if (strcmp(value, "named_curve") == 0) { | |
1005 | return 1; | |
1006 | } else { | |
1007 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1008 | return -2; | |
1009 | } | |
1010 | } | |
1011 | # endif | |
1012 | ||
972fa318 RL |
1013 | if (strcmp(name, "rsa_padding_mode") == 0) |
1014 | name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE; | |
1015 | else if (strcmp(name, "rsa_mgf1_md") == 0) | |
1016 | name = OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST; | |
1017 | else if (strcmp(name, "rsa_oaep_md") == 0) | |
1018 | name = OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST; | |
1019 | else if (strcmp(name, "rsa_oaep_label") == 0) | |
1020 | name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL; | |
6f4b7663 RL |
1021 | else if (strcmp(name, "rsa_pss_saltlen") == 0) |
1022 | name = OSSL_SIGNATURE_PARAM_PSS_SALTLEN; | |
2972af10 RL |
1023 | else if (strcmp(name, "rsa_keygen_bits") == 0) |
1024 | name = OSSL_PKEY_PARAM_RSA_BITS; | |
1025 | else if (strcmp(name, "rsa_keygen_pubexp") == 0) | |
1026 | name = OSSL_PKEY_PARAM_RSA_E; | |
1027 | else if (strcmp(name, "rsa_keygen_primes") == 0) | |
1028 | name = OSSL_PKEY_PARAM_RSA_PRIMES; | |
b03ec3b5 SL |
1029 | # ifndef OPENSSL_NO_DSA |
1030 | else if (strcmp(name, "dsa_paramgen_bits") == 0) | |
1031 | name = OSSL_PKEY_PARAM_FFC_PBITS; | |
1032 | else if (strcmp(name, "dsa_paramgen_q_bits") == 0) | |
1033 | name = OSSL_PKEY_PARAM_FFC_QBITS; | |
1034 | else if (strcmp(name, "dsa_paramgen_md") == 0) | |
1035 | name = OSSL_PKEY_PARAM_FFC_DIGEST; | |
1036 | # endif | |
e683582b | 1037 | # ifndef OPENSSL_NO_DH |
7165593c SL |
1038 | else if (strcmp(name, "dh_paramgen_generator") == 0) |
1039 | name = OSSL_PKEY_PARAM_FFC_GENERATOR; | |
1040 | else if (strcmp(name, "dh_paramgen_prime_len") == 0) | |
1041 | name = OSSL_PKEY_PARAM_FFC_PBITS; | |
1042 | else if (strcmp(name, "dh_paramgen_subprime_len") == 0) | |
1043 | name = OSSL_PKEY_PARAM_FFC_QBITS; | |
1044 | else if (strcmp(name, "dh_paramgen_type") == 0) { | |
1045 | name = OSSL_PKEY_PARAM_FFC_TYPE; | |
1046 | value = dh_gen_type_id2name(atoi(value)); | |
1047 | } else if (strcmp(name, "dh_param") == 0) | |
1048 | name = OSSL_PKEY_PARAM_FFC_GROUP; | |
1049 | else if (strcmp(name, "dh_rfc5114") == 0) { | |
1050 | name = OSSL_PKEY_PARAM_FFC_GROUP; | |
1051 | value = ffc_named_group_from_uid(atoi(value)); | |
1052 | } else if (strcmp(name, "dh_pad") == 0) | |
972fa318 | 1053 | name = OSSL_EXCHANGE_PARAM_PAD; |
e683582b | 1054 | # endif |
4fe54d67 | 1055 | # ifndef OPENSSL_NO_EC |
10d756a7 RL |
1056 | else if (strcmp(name, "ec_paramgen_curve") == 0) |
1057 | name = OSSL_PKEY_PARAM_EC_NAME; | |
4fe54d67 NT |
1058 | else if (strcmp(name, "ecdh_cofactor_mode") == 0) |
1059 | name = OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE; | |
1060 | else if (strcmp(name, "ecdh_kdf_md") == 0) | |
f552d900 | 1061 | name = OSSL_EXCHANGE_PARAM_KDF_DIGEST; |
4fe54d67 | 1062 | # endif |
89abd1b6 | 1063 | |
972fa318 RL |
1064 | { |
1065 | /* | |
1066 | * TODO(3.0) reduce the code above to only translate known legacy | |
1067 | * string to the corresponding core name (see core_names.h), but | |
1068 | * otherwise leave it to this code block to do the actual work. | |
1069 | */ | |
1070 | const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx); | |
1071 | OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END }; | |
1072 | int rv = 0; | |
292c8bdc | 1073 | int exists = 0; |
89abd1b6 | 1074 | |
972fa318 | 1075 | if (!OSSL_PARAM_allocate_from_text(¶ms[0], settable, name, value, |
292c8bdc P |
1076 | strlen(value), &exists)) { |
1077 | if (!exists) { | |
1078 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
1079 | return -2; | |
1080 | } | |
89abd1b6 | 1081 | return 0; |
292c8bdc | 1082 | } |
972fa318 RL |
1083 | if (EVP_PKEY_CTX_set_params(ctx, params)) |
1084 | rv = 1; | |
1085 | OPENSSL_free(params[0].data); | |
1086 | return rv; | |
89abd1b6 | 1087 | } |
35aca9ec MC |
1088 | } |
1089 | ||
4a3dc3c0 | 1090 | int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, |
0f113f3e MC |
1091 | const char *name, const char *value) |
1092 | { | |
35aca9ec MC |
1093 | if (ctx == NULL) { |
1094 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | |
1095 | return -2; | |
1096 | } | |
1097 | ||
864b89ce MC |
1098 | if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL) |
1099 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
2c938e2e MC |
1100 | && ctx->op.sig.sigprovctx != NULL) |
1101 | || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
62924755 RL |
1102 | && ctx->op.ciph.ciphprovctx != NULL) |
1103 | || (EVP_PKEY_CTX_IS_GEN_OP(ctx) | |
1104 | && ctx->op.keymgmt.genctx != NULL)) | |
35aca9ec MC |
1105 | return legacy_ctrl_str_to_param(ctx, name, value); |
1106 | ||
0f113f3e MC |
1107 | if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) { |
1108 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | |
1109 | return -2; | |
1110 | } | |
410877ba DSH |
1111 | if (strcmp(name, "digest") == 0) |
1112 | return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, | |
1113 | value); | |
0f113f3e MC |
1114 | return ctx->pmeth->ctrl_str(ctx, name, value); |
1115 | } | |
f5cda4cb | 1116 | |
99119000 DSH |
1117 | /* Utility functions to send a string of hex string to a ctrl */ |
1118 | ||
1119 | int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str) | |
1120 | { | |
1121 | size_t len; | |
1122 | ||
1123 | len = strlen(str); | |
1124 | if (len > INT_MAX) | |
1125 | return -1; | |
1126 | return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str); | |
1127 | } | |
1128 | ||
1129 | int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex) | |
1130 | { | |
1131 | unsigned char *bin; | |
1132 | long binlen; | |
1133 | int rv = -1; | |
1134 | ||
14f051a0 | 1135 | bin = OPENSSL_hexstr2buf(hex, &binlen); |
99119000 DSH |
1136 | if (bin == NULL) |
1137 | return 0; | |
1138 | if (binlen <= INT_MAX) | |
1139 | rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin); | |
1140 | OPENSSL_free(bin); | |
1141 | return rv; | |
1142 | } | |
52ad523c | 1143 | |
410877ba DSH |
1144 | /* Pass a message digest to a ctrl */ |
1145 | int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md) | |
1146 | { | |
1147 | const EVP_MD *m; | |
c82bafc5 | 1148 | |
410877ba DSH |
1149 | if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) { |
1150 | EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST); | |
1151 | return 0; | |
1152 | } | |
1153 | return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m); | |
1154 | } | |
99119000 | 1155 | |
b28dea4e | 1156 | int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
1157 | { |
1158 | return ctx->operation; | |
1159 | } | |
b28dea4e DSH |
1160 | |
1161 | void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen) | |
0f113f3e MC |
1162 | { |
1163 | ctx->keygen_info = dat; | |
1164 | ctx->keygen_info_count = datlen; | |
1165 | } | |
b28dea4e | 1166 | |
f5cda4cb | 1167 | void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data) |
0f113f3e MC |
1168 | { |
1169 | ctx->data = data; | |
1170 | } | |
f5cda4cb | 1171 | |
9fdcc21f | 1172 | void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx) |
0f113f3e MC |
1173 | { |
1174 | return ctx->data; | |
1175 | } | |
f5cda4cb | 1176 | |
81cebb8b | 1177 | EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
1178 | { |
1179 | return ctx->pkey; | |
1180 | } | |
81cebb8b | 1181 | |
0e1dba93 | 1182 | EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
1183 | { |
1184 | return ctx->peerkey; | |
1185 | } | |
1186 | ||
f5cda4cb | 1187 | void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) |
0f113f3e MC |
1188 | { |
1189 | ctx->app_data = data; | |
1190 | } | |
f5cda4cb DSH |
1191 | |
1192 | void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
1193 | { |
1194 | return ctx->app_data; | |
1195 | } | |
ba30bad5 DSH |
1196 | |
1197 | void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1198 | int (*init) (EVP_PKEY_CTX *ctx)) |
1199 | { | |
1200 | pmeth->init = init; | |
1201 | } | |
8bdcef40 DSH |
1202 | |
1203 | void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth, | |
0f113f3e | 1204 | int (*copy) (EVP_PKEY_CTX *dst, |
9fdcc21f | 1205 | const EVP_PKEY_CTX *src)) |
0f113f3e MC |
1206 | { |
1207 | pmeth->copy = copy; | |
1208 | } | |
ba30bad5 DSH |
1209 | |
1210 | void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1211 | void (*cleanup) (EVP_PKEY_CTX *ctx)) |
1212 | { | |
1213 | pmeth->cleanup = cleanup; | |
1214 | } | |
ba30bad5 DSH |
1215 | |
1216 | void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1217 | int (*paramgen_init) (EVP_PKEY_CTX *ctx), |
1218 | int (*paramgen) (EVP_PKEY_CTX *ctx, | |
1219 | EVP_PKEY *pkey)) | |
1220 | { | |
1221 | pmeth->paramgen_init = paramgen_init; | |
1222 | pmeth->paramgen = paramgen; | |
1223 | } | |
ba30bad5 DSH |
1224 | |
1225 | void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1226 | int (*keygen_init) (EVP_PKEY_CTX *ctx), |
1227 | int (*keygen) (EVP_PKEY_CTX *ctx, | |
1228 | EVP_PKEY *pkey)) | |
1229 | { | |
1230 | pmeth->keygen_init = keygen_init; | |
1231 | pmeth->keygen = keygen; | |
1232 | } | |
ba30bad5 DSH |
1233 | |
1234 | void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1235 | int (*sign_init) (EVP_PKEY_CTX *ctx), |
1236 | int (*sign) (EVP_PKEY_CTX *ctx, | |
1237 | unsigned char *sig, size_t *siglen, | |
1238 | const unsigned char *tbs, | |
1239 | size_t tbslen)) | |
1240 | { | |
1241 | pmeth->sign_init = sign_init; | |
1242 | pmeth->sign = sign; | |
1243 | } | |
ba30bad5 DSH |
1244 | |
1245 | void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1246 | int (*verify_init) (EVP_PKEY_CTX *ctx), |
1247 | int (*verify) (EVP_PKEY_CTX *ctx, | |
1248 | const unsigned char *sig, | |
1249 | size_t siglen, | |
1250 | const unsigned char *tbs, | |
1251 | size_t tbslen)) | |
1252 | { | |
1253 | pmeth->verify_init = verify_init; | |
1254 | pmeth->verify = verify; | |
1255 | } | |
ba30bad5 DSH |
1256 | |
1257 | void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1258 | int (*verify_recover_init) (EVP_PKEY_CTX |
1259 | *ctx), | |
1260 | int (*verify_recover) (EVP_PKEY_CTX | |
1261 | *ctx, | |
1262 | unsigned char | |
1263 | *sig, | |
1264 | size_t *siglen, | |
1265 | const unsigned | |
1266 | char *tbs, | |
1267 | size_t tbslen)) | |
1268 | { | |
1269 | pmeth->verify_recover_init = verify_recover_init; | |
1270 | pmeth->verify_recover = verify_recover; | |
1271 | } | |
ba30bad5 DSH |
1272 | |
1273 | void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1274 | int (*signctx_init) (EVP_PKEY_CTX *ctx, |
1275 | EVP_MD_CTX *mctx), | |
1276 | int (*signctx) (EVP_PKEY_CTX *ctx, | |
1277 | unsigned char *sig, | |
1278 | size_t *siglen, | |
1279 | EVP_MD_CTX *mctx)) | |
1280 | { | |
1281 | pmeth->signctx_init = signctx_init; | |
1282 | pmeth->signctx = signctx; | |
1283 | } | |
ba30bad5 DSH |
1284 | |
1285 | void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1286 | int (*verifyctx_init) (EVP_PKEY_CTX *ctx, |
1287 | EVP_MD_CTX *mctx), | |
1288 | int (*verifyctx) (EVP_PKEY_CTX *ctx, | |
1289 | const unsigned char *sig, | |
1290 | int siglen, | |
1291 | EVP_MD_CTX *mctx)) | |
1292 | { | |
1293 | pmeth->verifyctx_init = verifyctx_init; | |
1294 | pmeth->verifyctx = verifyctx; | |
1295 | } | |
ba30bad5 DSH |
1296 | |
1297 | void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1298 | int (*encrypt_init) (EVP_PKEY_CTX *ctx), |
1299 | int (*encryptfn) (EVP_PKEY_CTX *ctx, | |
1300 | unsigned char *out, | |
1301 | size_t *outlen, | |
1302 | const unsigned char *in, | |
1303 | size_t inlen)) | |
1304 | { | |
1305 | pmeth->encrypt_init = encrypt_init; | |
1306 | pmeth->encrypt = encryptfn; | |
1307 | } | |
ba30bad5 DSH |
1308 | |
1309 | void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1310 | int (*decrypt_init) (EVP_PKEY_CTX *ctx), |
1311 | int (*decrypt) (EVP_PKEY_CTX *ctx, | |
1312 | unsigned char *out, | |
1313 | size_t *outlen, | |
1314 | const unsigned char *in, | |
1315 | size_t inlen)) | |
1316 | { | |
1317 | pmeth->decrypt_init = decrypt_init; | |
1318 | pmeth->decrypt = decrypt; | |
1319 | } | |
ba30bad5 DSH |
1320 | |
1321 | void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1322 | int (*derive_init) (EVP_PKEY_CTX *ctx), |
1323 | int (*derive) (EVP_PKEY_CTX *ctx, | |
1324 | unsigned char *key, | |
1325 | size_t *keylen)) | |
1326 | { | |
1327 | pmeth->derive_init = derive_init; | |
1328 | pmeth->derive = derive; | |
1329 | } | |
ba30bad5 DSH |
1330 | |
1331 | void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1332 | int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
1333 | void *p2), | |
1334 | int (*ctrl_str) (EVP_PKEY_CTX *ctx, | |
1335 | const char *type, | |
1336 | const char *value)) | |
1337 | { | |
1338 | pmeth->ctrl = ctrl; | |
1339 | pmeth->ctrl_str = ctrl_str; | |
1340 | } | |
e7451ed1 | 1341 | |
2555285f AH |
1342 | void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth, |
1343 | int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, | |
1344 | const unsigned char *tbs, size_t tbslen)) | |
1345 | { | |
1346 | pmeth->digestsign = digestsign; | |
1347 | } | |
1348 | ||
1349 | void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth, | |
1350 | int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig, | |
1351 | size_t siglen, const unsigned char *tbs, | |
1352 | size_t tbslen)) | |
1353 | { | |
1354 | pmeth->digestverify = digestverify; | |
1355 | } | |
1356 | ||
2aee35d3 PY |
1357 | void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, |
1358 | int (*check) (EVP_PKEY *pkey)) | |
1359 | { | |
1360 | pmeth->check = check; | |
1361 | } | |
1362 | ||
b0004708 PY |
1363 | void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth, |
1364 | int (*check) (EVP_PKEY *pkey)) | |
1365 | { | |
1366 | pmeth->public_check = check; | |
1367 | } | |
1368 | ||
1369 | void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth, | |
1370 | int (*check) (EVP_PKEY *pkey)) | |
1371 | { | |
1372 | pmeth->param_check = check; | |
1373 | } | |
1374 | ||
0a8fdef7 PY |
1375 | void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth, |
1376 | int (*digest_custom) (EVP_PKEY_CTX *ctx, | |
1377 | EVP_MD_CTX *mctx)) | |
1378 | { | |
1379 | pmeth->digest_custom = digest_custom; | |
1380 | } | |
1381 | ||
693be9a2 | 1382 | void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1383 | int (**pinit) (EVP_PKEY_CTX *ctx)) |
1384 | { | |
1385 | *pinit = pmeth->init; | |
1386 | } | |
1387 | ||
693be9a2 | 1388 | void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 | 1389 | int (**pcopy) (EVP_PKEY_CTX *dst, |
9fdcc21f | 1390 | const EVP_PKEY_CTX *src)) |
e7451ed1 DSH |
1391 | { |
1392 | *pcopy = pmeth->copy; | |
1393 | } | |
1394 | ||
693be9a2 | 1395 | void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1396 | void (**pcleanup) (EVP_PKEY_CTX *ctx)) |
1397 | { | |
1398 | *pcleanup = pmeth->cleanup; | |
1399 | } | |
1400 | ||
693be9a2 | 1401 | void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1402 | int (**pparamgen_init) (EVP_PKEY_CTX *ctx), |
1403 | int (**pparamgen) (EVP_PKEY_CTX *ctx, | |
1404 | EVP_PKEY *pkey)) | |
1405 | { | |
1406 | if (pparamgen_init) | |
1407 | *pparamgen_init = pmeth->paramgen_init; | |
1408 | if (pparamgen) | |
1409 | *pparamgen = pmeth->paramgen; | |
1410 | } | |
1411 | ||
693be9a2 | 1412 | void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1413 | int (**pkeygen_init) (EVP_PKEY_CTX *ctx), |
1414 | int (**pkeygen) (EVP_PKEY_CTX *ctx, | |
1415 | EVP_PKEY *pkey)) | |
1416 | { | |
1417 | if (pkeygen_init) | |
1418 | *pkeygen_init = pmeth->keygen_init; | |
1419 | if (pkeygen) | |
1420 | *pkeygen = pmeth->keygen; | |
1421 | } | |
1422 | ||
693be9a2 | 1423 | void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1424 | int (**psign_init) (EVP_PKEY_CTX *ctx), |
1425 | int (**psign) (EVP_PKEY_CTX *ctx, | |
1426 | unsigned char *sig, size_t *siglen, | |
1427 | const unsigned char *tbs, | |
1428 | size_t tbslen)) | |
1429 | { | |
1430 | if (psign_init) | |
1431 | *psign_init = pmeth->sign_init; | |
1432 | if (psign) | |
1433 | *psign = pmeth->sign; | |
1434 | } | |
1435 | ||
693be9a2 | 1436 | void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1437 | int (**pverify_init) (EVP_PKEY_CTX *ctx), |
1438 | int (**pverify) (EVP_PKEY_CTX *ctx, | |
1439 | const unsigned char *sig, | |
1440 | size_t siglen, | |
1441 | const unsigned char *tbs, | |
1442 | size_t tbslen)) | |
1443 | { | |
1444 | if (pverify_init) | |
1445 | *pverify_init = pmeth->verify_init; | |
1446 | if (pverify) | |
1447 | *pverify = pmeth->verify; | |
1448 | } | |
1449 | ||
693be9a2 | 1450 | void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1451 | int (**pverify_recover_init) (EVP_PKEY_CTX |
1452 | *ctx), | |
1453 | int (**pverify_recover) (EVP_PKEY_CTX | |
1454 | *ctx, | |
1455 | unsigned char | |
1456 | *sig, | |
1457 | size_t *siglen, | |
1458 | const unsigned | |
1459 | char *tbs, | |
1460 | size_t tbslen)) | |
1461 | { | |
1462 | if (pverify_recover_init) | |
1463 | *pverify_recover_init = pmeth->verify_recover_init; | |
1464 | if (pverify_recover) | |
1465 | *pverify_recover = pmeth->verify_recover; | |
1466 | } | |
1467 | ||
693be9a2 | 1468 | void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1469 | int (**psignctx_init) (EVP_PKEY_CTX *ctx, |
1470 | EVP_MD_CTX *mctx), | |
1471 | int (**psignctx) (EVP_PKEY_CTX *ctx, | |
1472 | unsigned char *sig, | |
1473 | size_t *siglen, | |
1474 | EVP_MD_CTX *mctx)) | |
1475 | { | |
1476 | if (psignctx_init) | |
1477 | *psignctx_init = pmeth->signctx_init; | |
1478 | if (psignctx) | |
1479 | *psignctx = pmeth->signctx; | |
1480 | } | |
1481 | ||
693be9a2 | 1482 | void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1483 | int (**pverifyctx_init) (EVP_PKEY_CTX *ctx, |
1484 | EVP_MD_CTX *mctx), | |
1485 | int (**pverifyctx) (EVP_PKEY_CTX *ctx, | |
1486 | const unsigned char *sig, | |
1487 | int siglen, | |
1488 | EVP_MD_CTX *mctx)) | |
1489 | { | |
1490 | if (pverifyctx_init) | |
1491 | *pverifyctx_init = pmeth->verifyctx_init; | |
1492 | if (pverifyctx) | |
1493 | *pverifyctx = pmeth->verifyctx; | |
1494 | } | |
1495 | ||
693be9a2 | 1496 | void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1497 | int (**pencrypt_init) (EVP_PKEY_CTX *ctx), |
1498 | int (**pencryptfn) (EVP_PKEY_CTX *ctx, | |
1499 | unsigned char *out, | |
1500 | size_t *outlen, | |
1501 | const unsigned char *in, | |
1502 | size_t inlen)) | |
1503 | { | |
1504 | if (pencrypt_init) | |
1505 | *pencrypt_init = pmeth->encrypt_init; | |
1506 | if (pencryptfn) | |
1507 | *pencryptfn = pmeth->encrypt; | |
1508 | } | |
1509 | ||
693be9a2 | 1510 | void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1511 | int (**pdecrypt_init) (EVP_PKEY_CTX *ctx), |
1512 | int (**pdecrypt) (EVP_PKEY_CTX *ctx, | |
1513 | unsigned char *out, | |
1514 | size_t *outlen, | |
1515 | const unsigned char *in, | |
1516 | size_t inlen)) | |
1517 | { | |
1518 | if (pdecrypt_init) | |
1519 | *pdecrypt_init = pmeth->decrypt_init; | |
1520 | if (pdecrypt) | |
1521 | *pdecrypt = pmeth->decrypt; | |
1522 | } | |
1523 | ||
693be9a2 | 1524 | void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1525 | int (**pderive_init) (EVP_PKEY_CTX *ctx), |
1526 | int (**pderive) (EVP_PKEY_CTX *ctx, | |
1527 | unsigned char *key, | |
1528 | size_t *keylen)) | |
1529 | { | |
1530 | if (pderive_init) | |
1531 | *pderive_init = pmeth->derive_init; | |
1532 | if (pderive) | |
1533 | *pderive = pmeth->derive; | |
1534 | } | |
1535 | ||
693be9a2 | 1536 | void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1537 | int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
1538 | void *p2), | |
1539 | int (**pctrl_str) (EVP_PKEY_CTX *ctx, | |
1540 | const char *type, | |
1541 | const char *value)) | |
1542 | { | |
1543 | if (pctrl) | |
1544 | *pctrl = pmeth->ctrl; | |
1545 | if (pctrl_str) | |
1546 | *pctrl_str = pmeth->ctrl_str; | |
1547 | } | |
2aee35d3 | 1548 | |
2555285f AH |
1549 | void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth, |
1550 | int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen, | |
1551 | const unsigned char *tbs, size_t tbslen)) | |
1552 | { | |
1553 | if (digestsign) | |
1554 | *digestsign = pmeth->digestsign; | |
1555 | } | |
1556 | ||
1557 | void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth, | |
1558 | int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig, | |
1559 | size_t siglen, const unsigned char *tbs, | |
1560 | size_t tbslen)) | |
1561 | { | |
1562 | if (digestverify) | |
1563 | *digestverify = pmeth->digestverify; | |
1564 | } | |
1565 | ||
693be9a2 | 1566 | void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth, |
2aee35d3 PY |
1567 | int (**pcheck) (EVP_PKEY *pkey)) |
1568 | { | |
34f5c8b1 | 1569 | if (pcheck != NULL) |
2aee35d3 PY |
1570 | *pcheck = pmeth->check; |
1571 | } | |
b0004708 | 1572 | |
693be9a2 | 1573 | void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth, |
b0004708 PY |
1574 | int (**pcheck) (EVP_PKEY *pkey)) |
1575 | { | |
34f5c8b1 | 1576 | if (pcheck != NULL) |
b0004708 PY |
1577 | *pcheck = pmeth->public_check; |
1578 | } | |
1579 | ||
693be9a2 | 1580 | void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth, |
b0004708 PY |
1581 | int (**pcheck) (EVP_PKEY *pkey)) |
1582 | { | |
34f5c8b1 | 1583 | if (pcheck != NULL) |
b0004708 PY |
1584 | *pcheck = pmeth->param_check; |
1585 | } | |
0a8fdef7 PY |
1586 | |
1587 | void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth, | |
1588 | int (**pdigest_custom) (EVP_PKEY_CTX *ctx, | |
1589 | EVP_MD_CTX *mctx)) | |
1590 | { | |
675f4cee | 1591 | if (pdigest_custom != NULL) |
0a8fdef7 PY |
1592 | *pdigest_custom = pmeth->digest_custom; |
1593 | } | |
e683582b SL |
1594 | |
1595 | #endif /* FIPS_MODE */ |