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