]>
Commit | Line | Data |
---|---|---|
d0ea49a8 | 1 | |
0f113f3e | 2 | /* |
b0edda11 | 3 | * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. |
0b6f3c66 | 4 | * |
4a8b0c55 | 5 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
62867571 RS |
6 | * this file except in compliance with the License. You can obtain a copy |
7 | * in the file LICENSE in the source distribution or at | |
8 | * https://www.openssl.org/source/license.html | |
0b6f3c66 DSH |
9 | */ |
10 | ||
11 | #include <stdio.h> | |
12 | #include <stdlib.h> | |
3c27208f | 13 | #include <openssl/engine.h> |
33bed28b | 14 | #include <openssl/evp.h> |
99119000 | 15 | #include <openssl/x509v3.h> |
35aca9ec MC |
16 | #include <openssl/core_names.h> |
17 | #include <openssl/dh.h> | |
18 | #include "internal/cryptlib.h" | |
25f2138b DMSP |
19 | #include "crypto/asn1.h" |
20 | #include "crypto/evp.h" | |
99119000 | 21 | #include "internal/numbers.h" |
390acbeb | 22 | #include "internal/provider.h" |
706457b7 | 23 | #include "evp_local.h" |
0b6f3c66 | 24 | |
19bd1fa1 | 25 | typedef const EVP_PKEY_METHOD *(*pmeth_fn)(void); |
0f113f3e | 26 | typedef int sk_cmp_fn_type(const char *const *a, const char *const *b); |
5ce278a7 | 27 | |
df2ee0e2 | 28 | static STACK_OF(EVP_PKEY_METHOD) *app_pkey_methods = NULL; |
0b6f3c66 | 29 | |
cefa762e | 30 | /* This array needs to be in order of NIDs */ |
19bd1fa1 | 31 | static pmeth_fn standard_methods[] = { |
d4f0339c | 32 | #ifndef OPENSSL_NO_RSA |
19bd1fa1 | 33 | rsa_pkey_method, |
d4f0339c DSH |
34 | #endif |
35 | #ifndef OPENSSL_NO_DH | |
19bd1fa1 | 36 | dh_pkey_method, |
d4f0339c DSH |
37 | #endif |
38 | #ifndef OPENSSL_NO_DSA | |
19bd1fa1 | 39 | dsa_pkey_method, |
d4f0339c | 40 | #endif |
ef236ec3 | 41 | #ifndef OPENSSL_NO_EC |
19bd1fa1 | 42 | ec_pkey_method, |
ef236ec3 | 43 | #endif |
19bd1fa1 | 44 | hmac_pkey_method, |
b4a3aeeb | 45 | #ifndef OPENSSL_NO_CMAC |
19bd1fa1 | 46 | cmac_pkey_method, |
b4a3aeeb | 47 | #endif |
6577e008 | 48 | #ifndef OPENSSL_NO_RSA |
19bd1fa1 | 49 | rsa_pss_pkey_method, |
6577e008 | 50 | #endif |
afb14cda | 51 | #ifndef OPENSSL_NO_DH |
19bd1fa1 | 52 | dhx_pkey_method, |
cefa762e JB |
53 | #endif |
54 | #ifndef OPENSSL_NO_SCRYPT | |
19bd1fa1 | 55 | scrypt_pkey_method, |
afb14cda | 56 | #endif |
19bd1fa1 | 57 | tls1_prf_pkey_method, |
262bd85f | 58 | #ifndef OPENSSL_NO_EC |
19bd1fa1 PS |
59 | ecx25519_pkey_method, |
60 | ecx448_pkey_method, | |
262bd85f | 61 | #endif |
19bd1fa1 | 62 | hkdf_pkey_method, |
52ad5b60 | 63 | #ifndef OPENSSL_NO_POLY1305 |
19bd1fa1 | 64 | poly1305_pkey_method, |
52ad5b60 | 65 | #endif |
3f5616d7 | 66 | #ifndef OPENSSL_NO_SIPHASH |
19bd1fa1 | 67 | siphash_pkey_method, |
3f5616d7 | 68 | #endif |
42a3008a | 69 | #ifndef OPENSSL_NO_EC |
19bd1fa1 PS |
70 | ed25519_pkey_method, |
71 | ed448_pkey_method, | |
42a3008a | 72 | #endif |
ddb634fe | 73 | #ifndef OPENSSL_NO_SM2 |
19bd1fa1 | 74 | sm2_pkey_method, |
ddb634fe | 75 | #endif |
0f113f3e | 76 | }; |
0b6f3c66 | 77 | |
19bd1fa1 PS |
78 | DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func); |
79 | ||
80 | static int pmeth_func_cmp(const EVP_PKEY_METHOD *const *a, pmeth_fn const *b) | |
81 | { | |
82 | return ((*a)->pkey_id - ((**b)())->pkey_id); | |
83 | } | |
84 | ||
85 | IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD *, pmeth_fn, pmeth_func); | |
babb3798 | 86 | |
0f113f3e MC |
87 | static int pmeth_cmp(const EVP_PKEY_METHOD *const *a, |
88 | const EVP_PKEY_METHOD *const *b) | |
89 | { | |
90 | return ((*a)->pkey_id - (*b)->pkey_id); | |
91 | } | |
0b6f3c66 | 92 | |
c9777d26 | 93 | const EVP_PKEY_METHOD *EVP_PKEY_meth_find(int type) |
0f113f3e | 94 | { |
19bd1fa1 | 95 | pmeth_fn *ret; |
0f113f3e | 96 | EVP_PKEY_METHOD tmp; |
19bd1fa1 | 97 | const EVP_PKEY_METHOD *t = &tmp; |
12a765a5 | 98 | |
0f113f3e MC |
99 | tmp.pkey_id = type; |
100 | if (app_pkey_methods) { | |
101 | int idx; | |
102 | idx = sk_EVP_PKEY_METHOD_find(app_pkey_methods, &tmp); | |
103 | if (idx >= 0) | |
104 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); | |
105 | } | |
19bd1fa1 PS |
106 | ret = OBJ_bsearch_pmeth_func(&t, standard_methods, |
107 | sizeof(standard_methods) / | |
108 | sizeof(pmeth_fn)); | |
12a765a5 | 109 | if (ret == NULL || *ret == NULL) |
0f113f3e | 110 | return NULL; |
19bd1fa1 | 111 | return (**ret)(); |
0f113f3e | 112 | } |
0b6f3c66 | 113 | |
3ee348b0 RL |
114 | static EVP_PKEY_CTX *int_ctx_new(OPENSSL_CTX *libctx, |
115 | EVP_PKEY *pkey, ENGINE *e, | |
a07c17ef RL |
116 | const char *name, const char *propquery, |
117 | int id) | |
0f113f3e MC |
118 | { |
119 | EVP_PKEY_CTX *ret; | |
d0ea49a8 RL |
120 | const EVP_PKEY_METHOD *pmeth = NULL; |
121 | ||
122 | /* | |
123 | * When using providers, the context is bound to the algo implementation | |
124 | * later. | |
125 | */ | |
126 | if (pkey == NULL && e == NULL && id == -1) | |
127 | goto common; | |
2f2e6b62 | 128 | |
d0ea49a8 RL |
129 | /* TODO(3.0) Legacy code should be removed when all is provider based */ |
130 | /* BEGIN legacy */ | |
0f113f3e | 131 | if (id == -1) { |
a6465b3f P |
132 | if (pkey == NULL) |
133 | return 0; | |
2f2e6b62 | 134 | id = pkey->type; |
0f113f3e | 135 | } |
60653e5b RL |
136 | |
137 | /* | |
138 | * Here, we extract what information we can for the purpose of | |
139 | * supporting usage with implementations from providers, to make | |
140 | * for a smooth transition from legacy stuff to provider based stuff. | |
141 | * | |
142 | * If an engine is given, this is entirely legacy, and we should not | |
143 | * pretend anything else, so we only set the name when no engine is | |
144 | * given. If both are already given, someone made a mistake, and | |
145 | * since that can only happen internally, it's safe to make an | |
146 | * assertion. | |
147 | */ | |
148 | if (!ossl_assert(e == NULL || name == NULL)) | |
149 | return NULL; | |
150 | if (e == NULL) | |
151 | name = OBJ_nid2sn(id); | |
a07c17ef | 152 | propquery = NULL; |
3ee348b0 RL |
153 | /* |
154 | * We were called using legacy data, or an EVP_PKEY, but an EVP_PKEY | |
155 | * isn't tied to a specific library context, so we fall back to the | |
156 | * default library context. | |
157 | * TODO(v3.0): an EVP_PKEY that doesn't originate from a leagacy key | |
158 | * structure only has the pkeys[] cache, where the first element is | |
159 | * considered the "origin". Investigate if that could be a suitable | |
160 | * way to find a library context. | |
161 | */ | |
162 | libctx = NULL; | |
60653e5b | 163 | |
a63bf2c5 | 164 | #ifndef OPENSSL_NO_ENGINE |
c2976edf | 165 | if (e == NULL && pkey != NULL) |
d19b01ad | 166 | e = pkey->pmeth_engine != NULL ? pkey->pmeth_engine : pkey->engine; |
0f113f3e MC |
167 | /* Try to find an ENGINE which implements this method */ |
168 | if (e) { | |
169 | if (!ENGINE_init(e)) { | |
170 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_ENGINE_LIB); | |
171 | return NULL; | |
172 | } | |
c2976edf | 173 | } else { |
0f113f3e | 174 | e = ENGINE_get_pkey_meth_engine(id); |
c2976edf | 175 | } |
0f113f3e MC |
176 | |
177 | /* | |
0d4fb843 | 178 | * If an ENGINE handled this method look it up. Otherwise use internal |
0f113f3e MC |
179 | * tables. |
180 | */ | |
0f113f3e MC |
181 | if (e) |
182 | pmeth = ENGINE_get_pkey_meth(e, id); | |
183 | else | |
a63bf2c5 | 184 | #endif |
0f113f3e | 185 | pmeth = EVP_PKEY_meth_find(id); |
c9777d26 | 186 | |
0f113f3e | 187 | if (pmeth == NULL) { |
918a27fa DSH |
188 | #ifndef OPENSSL_NO_ENGINE |
189 | ENGINE_finish(e); | |
190 | #endif | |
0f113f3e MC |
191 | EVPerr(EVP_F_INT_CTX_NEW, EVP_R_UNSUPPORTED_ALGORITHM); |
192 | return NULL; | |
193 | } | |
d0ea49a8 | 194 | /* END legacy */ |
c9777d26 | 195 | |
d0ea49a8 | 196 | common: |
64b25758 | 197 | ret = OPENSSL_zalloc(sizeof(*ret)); |
90945fa3 | 198 | if (ret == NULL) { |
a63bf2c5 | 199 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 200 | ENGINE_finish(e); |
a63bf2c5 | 201 | #endif |
0f113f3e MC |
202 | EVPerr(EVP_F_INT_CTX_NEW, ERR_R_MALLOC_FAILURE); |
203 | return NULL; | |
204 | } | |
3ee348b0 | 205 | ret->libctx = libctx; |
a07c17ef RL |
206 | ret->algorithm = name; |
207 | ret->propquery = propquery; | |
0f113f3e MC |
208 | ret->engine = e; |
209 | ret->pmeth = pmeth; | |
210 | ret->operation = EVP_PKEY_OP_UNDEFINED; | |
211 | ret->pkey = pkey; | |
a6465b3f | 212 | if (pkey != NULL) |
03273d61 | 213 | EVP_PKEY_up_ref(pkey); |
0f113f3e | 214 | |
8b84b075 | 215 | if (pmeth != NULL && pmeth->init != NULL) { |
0f113f3e | 216 | if (pmeth->init(ret) <= 0) { |
83b4049a | 217 | ret->pmeth = NULL; |
0f113f3e MC |
218 | EVP_PKEY_CTX_free(ret); |
219 | return NULL; | |
220 | } | |
221 | } | |
222 | ||
223 | return ret; | |
224 | } | |
225 | ||
864b89ce MC |
226 | void evp_pkey_ctx_free_old_ops(EVP_PKEY_CTX *ctx) |
227 | { | |
228 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { | |
229 | if (ctx->op.kex.exchprovctx != NULL && ctx->op.kex.exchange != NULL) | |
230 | ctx->op.kex.exchange->freectx(ctx->op.kex.exchprovctx); | |
231 | EVP_KEYEXCH_free(ctx->op.kex.exchange); | |
fb1ecf85 RL |
232 | ctx->op.kex.exchprovctx = NULL; |
233 | ctx->op.kex.exchange = NULL; | |
864b89ce MC |
234 | } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
235 | if (ctx->op.sig.sigprovctx != NULL && ctx->op.sig.signature != NULL) | |
236 | ctx->op.sig.signature->freectx(ctx->op.sig.sigprovctx); | |
237 | EVP_SIGNATURE_free(ctx->op.sig.signature); | |
fb1ecf85 RL |
238 | ctx->op.sig.sigprovctx = NULL; |
239 | ctx->op.sig.signature = NULL; | |
2c938e2e MC |
240 | } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { |
241 | if (ctx->op.ciph.ciphprovctx != NULL && ctx->op.ciph.cipher != NULL) | |
242 | ctx->op.ciph.cipher->freectx(ctx->op.ciph.ciphprovctx); | |
243 | EVP_ASYM_CIPHER_free(ctx->op.ciph.cipher); | |
244 | ctx->op.ciph.ciphprovctx = NULL; | |
245 | ctx->op.ciph.cipher = NULL; | |
864b89ce MC |
246 | } |
247 | } | |
248 | ||
0f113f3e MC |
249 | EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags) |
250 | { | |
251 | EVP_PKEY_METHOD *pmeth; | |
b4faea50 | 252 | |
b51bce94 | 253 | pmeth = OPENSSL_zalloc(sizeof(*pmeth)); |
3484236d F |
254 | if (pmeth == NULL) { |
255 | EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 256 | return NULL; |
3484236d | 257 | } |
0f113f3e | 258 | |
0f113f3e MC |
259 | pmeth->pkey_id = id; |
260 | pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC; | |
0f113f3e MC |
261 | return pmeth; |
262 | } | |
ba30bad5 | 263 | |
f830c68f | 264 | void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, |
0f113f3e MC |
265 | const EVP_PKEY_METHOD *meth) |
266 | { | |
267 | if (ppkey_id) | |
268 | *ppkey_id = meth->pkey_id; | |
269 | if (pflags) | |
270 | *pflags = meth->flags; | |
271 | } | |
f830c68f DSH |
272 | |
273 | void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src) | |
0f113f3e | 274 | { |
f830c68f | 275 | |
0f113f3e MC |
276 | dst->init = src->init; |
277 | dst->copy = src->copy; | |
278 | dst->cleanup = src->cleanup; | |
f830c68f | 279 | |
0f113f3e MC |
280 | dst->paramgen_init = src->paramgen_init; |
281 | dst->paramgen = src->paramgen; | |
f830c68f | 282 | |
0f113f3e MC |
283 | dst->keygen_init = src->keygen_init; |
284 | dst->keygen = src->keygen; | |
f830c68f | 285 | |
0f113f3e MC |
286 | dst->sign_init = src->sign_init; |
287 | dst->sign = src->sign; | |
f830c68f | 288 | |
0f113f3e MC |
289 | dst->verify_init = src->verify_init; |
290 | dst->verify = src->verify; | |
f830c68f | 291 | |
0f113f3e MC |
292 | dst->verify_recover_init = src->verify_recover_init; |
293 | dst->verify_recover = src->verify_recover; | |
f830c68f | 294 | |
0f113f3e MC |
295 | dst->signctx_init = src->signctx_init; |
296 | dst->signctx = src->signctx; | |
f830c68f | 297 | |
0f113f3e MC |
298 | dst->verifyctx_init = src->verifyctx_init; |
299 | dst->verifyctx = src->verifyctx; | |
f830c68f | 300 | |
0f113f3e MC |
301 | dst->encrypt_init = src->encrypt_init; |
302 | dst->encrypt = src->encrypt; | |
f830c68f | 303 | |
0f113f3e MC |
304 | dst->decrypt_init = src->decrypt_init; |
305 | dst->decrypt = src->decrypt; | |
f830c68f | 306 | |
0f113f3e MC |
307 | dst->derive_init = src->derive_init; |
308 | dst->derive = src->derive; | |
f830c68f | 309 | |
0f113f3e MC |
310 | dst->ctrl = src->ctrl; |
311 | dst->ctrl_str = src->ctrl_str; | |
2aee35d3 PY |
312 | |
313 | dst->check = src->check; | |
0f113f3e | 314 | } |
f830c68f | 315 | |
ba30bad5 | 316 | void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth) |
0f113f3e MC |
317 | { |
318 | if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC)) | |
319 | OPENSSL_free(pmeth); | |
320 | } | |
ba30bad5 | 321 | |
f5cda4cb | 322 | EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) |
0f113f3e | 323 | { |
3ee348b0 | 324 | return int_ctx_new(NULL, pkey, e, NULL, NULL, -1); |
0f113f3e | 325 | } |
f5cda4cb DSH |
326 | |
327 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) | |
0f113f3e | 328 | { |
3ee348b0 | 329 | return int_ctx_new(NULL, NULL, e, NULL, NULL, id); |
a07c17ef RL |
330 | } |
331 | ||
3ee348b0 RL |
332 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_provided(OPENSSL_CTX *libctx, |
333 | const char *name, | |
a07c17ef RL |
334 | const char *propquery) |
335 | { | |
3ee348b0 | 336 | return int_ctx_new(libctx, NULL, NULL, name, propquery, -1); |
0f113f3e | 337 | } |
f5cda4cb | 338 | |
9fdcc21f | 339 | EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx) |
0f113f3e MC |
340 | { |
341 | EVP_PKEY_CTX *rctx; | |
ff64702b MC |
342 | |
343 | if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL)) | |
864b89ce MC |
344 | && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx) |
345 | && pctx->op.kex.exchprovctx == NULL) | |
346 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx) | |
347 | && pctx->op.sig.sigprovctx == NULL))) | |
0f113f3e | 348 | return NULL; |
c9777d26 | 349 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
350 | /* Make sure it's safe to copy a pkey context using an ENGINE */ |
351 | if (pctx->engine && !ENGINE_init(pctx->engine)) { | |
352 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB); | |
353 | return 0; | |
354 | } | |
c9777d26 | 355 | #endif |
ff64702b | 356 | rctx = OPENSSL_zalloc(sizeof(*rctx)); |
3484236d F |
357 | if (rctx == NULL) { |
358 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 359 | return NULL; |
3484236d | 360 | } |
8bdcef40 | 361 | |
ff64702b MC |
362 | if (pctx->pkey != NULL) |
363 | EVP_PKEY_up_ref(pctx->pkey); | |
364 | rctx->pkey = pctx->pkey; | |
365 | rctx->operation = pctx->operation; | |
3ee348b0 | 366 | rctx->libctx = pctx->libctx; |
a07c17ef RL |
367 | rctx->algorithm = pctx->algorithm; |
368 | rctx->propquery = pctx->propquery; | |
ff64702b | 369 | |
864b89ce MC |
370 | if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) { |
371 | if (pctx->op.kex.exchange != NULL) { | |
372 | rctx->op.kex.exchange = pctx->op.kex.exchange; | |
373 | if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) { | |
374 | OPENSSL_free(rctx); | |
375 | return NULL; | |
376 | } | |
ff64702b | 377 | } |
864b89ce MC |
378 | if (pctx->op.kex.exchprovctx != NULL) { |
379 | if (!ossl_assert(pctx->op.kex.exchange != NULL)) | |
380 | return NULL; | |
381 | rctx->op.kex.exchprovctx | |
382 | = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx); | |
383 | if (rctx->op.kex.exchprovctx == NULL) { | |
384 | EVP_KEYEXCH_free(rctx->op.kex.exchange); | |
385 | OPENSSL_free(rctx); | |
386 | return NULL; | |
387 | } | |
388 | return rctx; | |
389 | } | |
390 | } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) { | |
391 | if (pctx->op.sig.signature != NULL) { | |
392 | rctx->op.sig.signature = pctx->op.sig.signature; | |
393 | if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) { | |
394 | OPENSSL_free(rctx); | |
395 | return NULL; | |
396 | } | |
397 | } | |
398 | if (pctx->op.sig.sigprovctx != NULL) { | |
399 | if (!ossl_assert(pctx->op.sig.signature != NULL)) | |
400 | return NULL; | |
401 | rctx->op.sig.sigprovctx | |
402 | = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx); | |
403 | if (rctx->op.sig.sigprovctx == NULL) { | |
404 | EVP_SIGNATURE_free(rctx->op.sig.signature); | |
405 | OPENSSL_free(rctx); | |
406 | return NULL; | |
407 | } | |
408 | return rctx; | |
ff64702b | 409 | } |
2c938e2e MC |
410 | } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) { |
411 | if (pctx->op.ciph.cipher != NULL) { | |
412 | rctx->op.ciph.cipher = pctx->op.ciph.cipher; | |
413 | if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) { | |
414 | OPENSSL_free(rctx); | |
415 | return NULL; | |
416 | } | |
417 | } | |
418 | if (pctx->op.ciph.ciphprovctx != NULL) { | |
419 | if (!ossl_assert(pctx->op.ciph.cipher != NULL)) | |
420 | return NULL; | |
421 | rctx->op.ciph.ciphprovctx | |
422 | = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx); | |
423 | if (rctx->op.ciph.ciphprovctx == NULL) { | |
424 | EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher); | |
425 | OPENSSL_free(rctx); | |
426 | return NULL; | |
427 | } | |
428 | return rctx; | |
429 | } | |
ff64702b MC |
430 | } |
431 | ||
0f113f3e | 432 | rctx->pmeth = pctx->pmeth; |
c9777d26 | 433 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 434 | rctx->engine = pctx->engine; |
c9777d26 | 435 | #endif |
8bdcef40 | 436 | |
0f113f3e | 437 | if (pctx->peerkey) |
03273d61 | 438 | EVP_PKEY_up_ref(pctx->peerkey); |
0f113f3e | 439 | rctx->peerkey = pctx->peerkey; |
8bdcef40 | 440 | |
0f113f3e MC |
441 | if (pctx->pmeth->copy(rctx, pctx) > 0) |
442 | return rctx; | |
8bdcef40 | 443 | |
83b4049a | 444 | rctx->pmeth = NULL; |
0f113f3e MC |
445 | EVP_PKEY_CTX_free(rctx); |
446 | return NULL; | |
8bdcef40 | 447 | |
0f113f3e | 448 | } |
8bdcef40 | 449 | |
ba30bad5 | 450 | int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth) |
0f113f3e MC |
451 | { |
452 | if (app_pkey_methods == NULL) { | |
453 | app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp); | |
3484236d F |
454 | if (app_pkey_methods == NULL){ |
455 | EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 456 | return 0; |
3484236d | 457 | } |
0f113f3e | 458 | } |
3484236d F |
459 | if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) { |
460 | EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 461 | return 0; |
3484236d | 462 | } |
0f113f3e MC |
463 | sk_EVP_PKEY_METHOD_sort(app_pkey_methods); |
464 | return 1; | |
465 | } | |
ba30bad5 | 466 | |
0822e89a PY |
467 | void evp_app_cleanup_int(void) |
468 | { | |
469 | if (app_pkey_methods != NULL) | |
470 | sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free); | |
471 | } | |
472 | ||
473 | int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth) | |
474 | { | |
475 | const EVP_PKEY_METHOD *ret; | |
476 | ||
477 | ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth); | |
478 | ||
479 | return ret == NULL ? 0 : 1; | |
480 | } | |
481 | ||
48ed9c23 DSH |
482 | size_t EVP_PKEY_meth_get_count(void) |
483 | { | |
484 | size_t rv = OSSL_NELEM(standard_methods); | |
485 | ||
486 | if (app_pkey_methods) | |
487 | rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods); | |
488 | return rv; | |
489 | } | |
490 | ||
491 | const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx) | |
492 | { | |
493 | if (idx < OSSL_NELEM(standard_methods)) | |
19bd1fa1 | 494 | return (standard_methods[idx])(); |
48ed9c23 DSH |
495 | if (app_pkey_methods == NULL) |
496 | return NULL; | |
497 | idx -= OSSL_NELEM(standard_methods); | |
498 | if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods)) | |
499 | return NULL; | |
500 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); | |
501 | } | |
502 | ||
5da98aa6 | 503 | void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
504 | { |
505 | if (ctx == NULL) | |
506 | return; | |
507 | if (ctx->pmeth && ctx->pmeth->cleanup) | |
508 | ctx->pmeth->cleanup(ctx); | |
ff64702b | 509 | |
864b89ce | 510 | evp_pkey_ctx_free_old_ops(ctx); |
c0e0984f | 511 | EVP_KEYMGMT_free(ctx->keymgmt); |
dfcb5d29 | 512 | |
c5ba2d99 RS |
513 | EVP_PKEY_free(ctx->pkey); |
514 | EVP_PKEY_free(ctx->peerkey); | |
c9777d26 | 515 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 516 | ENGINE_finish(ctx->engine); |
c9777d26 | 517 | #endif |
0f113f3e MC |
518 | OPENSSL_free(ctx); |
519 | } | |
5da98aa6 | 520 | |
9c45222d MC |
521 | int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
522 | { | |
864b89ce MC |
523 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
524 | && ctx->op.sig.sigprovctx != NULL | |
525 | && ctx->op.sig.signature != NULL | |
526 | && ctx->op.sig.signature->get_ctx_params != NULL) | |
527 | return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx, | |
528 | params); | |
2c938e2e MC |
529 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
530 | && ctx->op.ciph.ciphprovctx != NULL | |
531 | && ctx->op.ciph.cipher != NULL | |
532 | && ctx->op.ciph.cipher->get_ctx_params != NULL) | |
533 | return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx, | |
534 | params); | |
9c45222d MC |
535 | return 0; |
536 | } | |
537 | ||
538 | const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx) | |
539 | { | |
864b89ce MC |
540 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
541 | && ctx->op.sig.signature != NULL | |
542 | && ctx->op.sig.signature->gettable_ctx_params != NULL) | |
543 | return ctx->op.sig.signature->gettable_ctx_params(); | |
9c45222d | 544 | |
2c938e2e MC |
545 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
546 | && ctx->op.ciph.cipher != NULL | |
547 | && ctx->op.ciph.cipher->gettable_ctx_params != NULL) | |
548 | return ctx->op.ciph.cipher->gettable_ctx_params(); | |
549 | ||
9c45222d MC |
550 | return NULL; |
551 | } | |
552 | ||
35aca9ec MC |
553 | int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
554 | { | |
864b89ce MC |
555 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
556 | && ctx->op.kex.exchprovctx != NULL | |
557 | && ctx->op.kex.exchange != NULL | |
558 | && ctx->op.kex.exchange->set_ctx_params != NULL) | |
559 | return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx, | |
560 | params); | |
561 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
562 | && ctx->op.sig.sigprovctx != NULL | |
563 | && ctx->op.sig.signature != NULL | |
564 | && ctx->op.sig.signature->set_ctx_params != NULL) | |
565 | return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx, | |
566 | params); | |
2c938e2e MC |
567 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
568 | && ctx->op.ciph.ciphprovctx != NULL | |
569 | && ctx->op.ciph.cipher != NULL | |
570 | && ctx->op.ciph.cipher->set_ctx_params != NULL) | |
571 | return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx, | |
572 | params); | |
35aca9ec MC |
573 | return 0; |
574 | } | |
575 | ||
9c45222d MC |
576 | const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx) |
577 | { | |
864b89ce MC |
578 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
579 | && ctx->op.kex.exchange != NULL | |
580 | && ctx->op.kex.exchange->settable_ctx_params != NULL) | |
581 | return ctx->op.kex.exchange->settable_ctx_params(); | |
582 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
583 | && ctx->op.sig.signature != NULL | |
584 | && ctx->op.sig.signature->settable_ctx_params != NULL) | |
585 | return ctx->op.sig.signature->settable_ctx_params(); | |
2c938e2e MC |
586 | if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) |
587 | && ctx->op.ciph.cipher != NULL | |
588 | && ctx->op.ciph.cipher->settable_ctx_params != NULL) | |
589 | return ctx->op.ciph.cipher->settable_ctx_params(); | |
9c45222d MC |
590 | |
591 | return NULL; | |
592 | } | |
593 | ||
76ca35e7 | 594 | #ifndef OPENSSL_NO_DH |
35aca9ec MC |
595 | int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad) |
596 | { | |
597 | OSSL_PARAM dh_pad_params[2]; | |
1c3ace68 | 598 | unsigned int upad = pad; |
35aca9ec | 599 | |
864b89ce MC |
600 | /* We use EVP_PKEY_CTX_ctrl return values */ |
601 | if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { | |
602 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
603 | return -2; | |
604 | } | |
605 | ||
35aca9ec | 606 | /* TODO(3.0): Remove this eventually when no more legacy */ |
864b89ce | 607 | if (ctx->op.kex.exchprovctx == NULL) |
35aca9ec MC |
608 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE, |
609 | EVP_PKEY_CTRL_DH_PAD, pad, NULL); | |
610 | ||
1c3ace68 | 611 | dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad); |
35aca9ec MC |
612 | dh_pad_params[1] = OSSL_PARAM_construct_end(); |
613 | ||
614 | return EVP_PKEY_CTX_set_params(ctx, dh_pad_params); | |
615 | } | |
76ca35e7 | 616 | #endif |
35aca9ec | 617 | |
9c45222d MC |
618 | int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md) |
619 | { | |
620 | OSSL_PARAM sig_md_params[3], *p = sig_md_params; | |
621 | /* 80 should be big enough */ | |
622 | char name[80] = ""; | |
623 | const EVP_MD *tmp; | |
624 | ||
864b89ce | 625 | if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
9c45222d MC |
626 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
627 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
628 | return -2; | |
629 | } | |
630 | ||
631 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
864b89ce | 632 | if (ctx->op.sig.sigprovctx == NULL) |
9c45222d MC |
633 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, |
634 | EVP_PKEY_CTRL_GET_MD, 0, (void *)(md)); | |
635 | ||
636 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, | |
637 | name, | |
638 | sizeof(name)); | |
639 | *p++ = OSSL_PARAM_construct_end(); | |
640 | ||
641 | if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params)) | |
642 | return 0; | |
643 | ||
7606bed9 | 644 | tmp = evp_get_digestbyname_ex(ctx->libctx, name); |
9c45222d MC |
645 | if (tmp == NULL) |
646 | return 0; | |
647 | ||
648 | *md = tmp; | |
649 | ||
650 | return 1; | |
651 | } | |
652 | ||
4889dadc MC |
653 | int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
654 | { | |
9c45222d | 655 | OSSL_PARAM sig_md_params[3], *p = sig_md_params; |
4889dadc MC |
656 | size_t mdsize; |
657 | const char *name; | |
658 | ||
864b89ce | 659 | if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
9c45222d MC |
660 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
661 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
662 | return -2; | |
663 | } | |
664 | ||
4889dadc | 665 | /* TODO(3.0): Remove this eventually when no more legacy */ |
864b89ce | 666 | if (ctx->op.sig.sigprovctx == NULL) |
4889dadc MC |
667 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, |
668 | EVP_PKEY_CTRL_MD, 0, (void *)(md)); | |
669 | ||
9c45222d MC |
670 | if (md == NULL) { |
671 | name = ""; | |
672 | mdsize = 0; | |
673 | } else { | |
674 | mdsize = EVP_MD_size(md); | |
675 | name = EVP_MD_name(md); | |
676 | } | |
4889dadc | 677 | |
9c45222d MC |
678 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, |
679 | /* | |
680 | * Cast away the const. This is read | |
681 | * only so should be safe | |
682 | */ | |
683 | (char *)name, | |
684 | strlen(name) + 1); | |
685 | *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, | |
686 | &mdsize); | |
687 | *p++ = OSSL_PARAM_construct_end(); | |
4889dadc | 688 | |
9c45222d | 689 | return EVP_PKEY_CTX_set_params(ctx, sig_md_params); |
4889dadc MC |
690 | } |
691 | ||
35aca9ec MC |
692 | static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype, |
693 | int cmd, int p1, void *p2) | |
694 | { | |
695 | switch (cmd) { | |
76ca35e7 | 696 | #ifndef OPENSSL_NO_DH |
35aca9ec MC |
697 | case EVP_PKEY_CTRL_DH_PAD: |
698 | return EVP_PKEY_CTX_set_dh_pad(ctx, p1); | |
76ca35e7 | 699 | #endif |
390acbeb MC |
700 | case EVP_PKEY_CTRL_MD: |
701 | return EVP_PKEY_CTX_set_signature_md(ctx, p2); | |
864b89ce MC |
702 | case EVP_PKEY_CTRL_GET_MD: |
703 | return EVP_PKEY_CTX_get_signature_md(ctx, p2); | |
35aca9ec MC |
704 | } |
705 | return 0; | |
706 | } | |
707 | ||
0b6f3c66 | 708 | int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, |
0f113f3e MC |
709 | int cmd, int p1, void *p2) |
710 | { | |
711 | int ret; | |
4803717f | 712 | |
35aca9ec MC |
713 | if (ctx == NULL) { |
714 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
715 | return -2; | |
716 | } | |
717 | ||
864b89ce | 718 | if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL) |
48bb9792 | 719 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
2c938e2e MC |
720 | && ctx->op.sig.sigprovctx != NULL) |
721 | || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
722 | && ctx->op.ciph.ciphprovctx != NULL)) | |
35aca9ec MC |
723 | return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2); |
724 | ||
725 | if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) { | |
0f113f3e MC |
726 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); |
727 | return -2; | |
728 | } | |
729 | if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype)) | |
730 | return -1; | |
731 | ||
4803717f PY |
732 | /* Skip the operation checks since this is called in a very early stage */ |
733 | if (ctx->pmeth->digest_custom != NULL) | |
734 | goto doit; | |
735 | ||
0f113f3e MC |
736 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { |
737 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET); | |
738 | return -1; | |
739 | } | |
740 | ||
741 | if ((optype != -1) && !(ctx->operation & optype)) { | |
742 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION); | |
743 | return -1; | |
744 | } | |
745 | ||
4803717f | 746 | doit: |
0f113f3e MC |
747 | ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2); |
748 | ||
749 | if (ret == -2) | |
750 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
751 | ||
752 | return ret; | |
0f113f3e | 753 | } |
0b6f3c66 | 754 | |
cefa762e | 755 | int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype, |
64bf1016 | 756 | int cmd, uint64_t value) |
cefa762e JB |
757 | { |
758 | return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value); | |
759 | } | |
760 | ||
35aca9ec MC |
761 | static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name, |
762 | const char *value) | |
763 | { | |
76ca35e7 | 764 | #ifndef OPENSSL_NO_DH |
35aca9ec MC |
765 | if (strcmp(name, "dh_pad") == 0) { |
766 | int pad; | |
767 | ||
768 | pad = atoi(value); | |
769 | return EVP_PKEY_CTX_set_dh_pad(ctx, pad); | |
770 | } | |
76ca35e7 | 771 | #endif |
390acbeb MC |
772 | if (strcmp(name, "digest") == 0) { |
773 | int ret; | |
864b89ce MC |
774 | EVP_MD *md; |
775 | ||
776 | if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) || ctx->op.sig.signature == NULL) | |
777 | return 0; | |
778 | md = EVP_MD_fetch(ossl_provider_library_context(ctx->op.sig.signature->prov), | |
779 | value, NULL); | |
390acbeb MC |
780 | if (md == NULL) |
781 | return 0; | |
782 | ret = EVP_PKEY_CTX_set_signature_md(ctx, md); | |
783 | EVP_MD_meth_free(md); | |
784 | return ret; | |
785 | } | |
786 | ||
35aca9ec MC |
787 | return 0; |
788 | } | |
789 | ||
4a3dc3c0 | 790 | int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, |
0f113f3e MC |
791 | const char *name, const char *value) |
792 | { | |
35aca9ec MC |
793 | if (ctx == NULL) { |
794 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | |
795 | return -2; | |
796 | } | |
797 | ||
864b89ce MC |
798 | if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL) |
799 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
2c938e2e MC |
800 | && ctx->op.sig.sigprovctx != NULL) |
801 | || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx) | |
802 | && ctx->op.ciph.ciphprovctx != NULL)) | |
35aca9ec MC |
803 | return legacy_ctrl_str_to_param(ctx, name, value); |
804 | ||
0f113f3e MC |
805 | if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) { |
806 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | |
807 | return -2; | |
808 | } | |
410877ba DSH |
809 | if (strcmp(name, "digest") == 0) |
810 | return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, | |
811 | value); | |
0f113f3e MC |
812 | return ctx->pmeth->ctrl_str(ctx, name, value); |
813 | } | |
f5cda4cb | 814 | |
99119000 DSH |
815 | /* Utility functions to send a string of hex string to a ctrl */ |
816 | ||
817 | int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str) | |
818 | { | |
819 | size_t len; | |
820 | ||
821 | len = strlen(str); | |
822 | if (len > INT_MAX) | |
823 | return -1; | |
824 | return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str); | |
825 | } | |
826 | ||
827 | int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex) | |
828 | { | |
829 | unsigned char *bin; | |
830 | long binlen; | |
831 | int rv = -1; | |
832 | ||
14f051a0 | 833 | bin = OPENSSL_hexstr2buf(hex, &binlen); |
99119000 DSH |
834 | if (bin == NULL) |
835 | return 0; | |
836 | if (binlen <= INT_MAX) | |
837 | rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin); | |
838 | OPENSSL_free(bin); | |
839 | return rv; | |
840 | } | |
52ad523c | 841 | |
410877ba DSH |
842 | /* Pass a message digest to a ctrl */ |
843 | int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md) | |
844 | { | |
845 | const EVP_MD *m; | |
c82bafc5 | 846 | |
410877ba DSH |
847 | if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) { |
848 | EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST); | |
849 | return 0; | |
850 | } | |
851 | return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m); | |
852 | } | |
99119000 | 853 | |
b28dea4e | 854 | int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
855 | { |
856 | return ctx->operation; | |
857 | } | |
b28dea4e DSH |
858 | |
859 | void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen) | |
0f113f3e MC |
860 | { |
861 | ctx->keygen_info = dat; | |
862 | ctx->keygen_info_count = datlen; | |
863 | } | |
b28dea4e | 864 | |
f5cda4cb | 865 | void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data) |
0f113f3e MC |
866 | { |
867 | ctx->data = data; | |
868 | } | |
f5cda4cb | 869 | |
9fdcc21f | 870 | void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx) |
0f113f3e MC |
871 | { |
872 | return ctx->data; | |
873 | } | |
f5cda4cb | 874 | |
81cebb8b | 875 | EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
876 | { |
877 | return ctx->pkey; | |
878 | } | |
81cebb8b | 879 | |
0e1dba93 | 880 | EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
881 | { |
882 | return ctx->peerkey; | |
883 | } | |
884 | ||
f5cda4cb | 885 | void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) |
0f113f3e MC |
886 | { |
887 | ctx->app_data = data; | |
888 | } | |
f5cda4cb DSH |
889 | |
890 | void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
891 | { |
892 | return ctx->app_data; | |
893 | } | |
ba30bad5 DSH |
894 | |
895 | void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
896 | int (*init) (EVP_PKEY_CTX *ctx)) |
897 | { | |
898 | pmeth->init = init; | |
899 | } | |
8bdcef40 DSH |
900 | |
901 | void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth, | |
0f113f3e | 902 | int (*copy) (EVP_PKEY_CTX *dst, |
9fdcc21f | 903 | const EVP_PKEY_CTX *src)) |
0f113f3e MC |
904 | { |
905 | pmeth->copy = copy; | |
906 | } | |
ba30bad5 DSH |
907 | |
908 | void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
909 | void (*cleanup) (EVP_PKEY_CTX *ctx)) |
910 | { | |
911 | pmeth->cleanup = cleanup; | |
912 | } | |
ba30bad5 DSH |
913 | |
914 | void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
915 | int (*paramgen_init) (EVP_PKEY_CTX *ctx), |
916 | int (*paramgen) (EVP_PKEY_CTX *ctx, | |
917 | EVP_PKEY *pkey)) | |
918 | { | |
919 | pmeth->paramgen_init = paramgen_init; | |
920 | pmeth->paramgen = paramgen; | |
921 | } | |
ba30bad5 DSH |
922 | |
923 | void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
924 | int (*keygen_init) (EVP_PKEY_CTX *ctx), |
925 | int (*keygen) (EVP_PKEY_CTX *ctx, | |
926 | EVP_PKEY *pkey)) | |
927 | { | |
928 | pmeth->keygen_init = keygen_init; | |
929 | pmeth->keygen = keygen; | |
930 | } | |
ba30bad5 DSH |
931 | |
932 | void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
933 | int (*sign_init) (EVP_PKEY_CTX *ctx), |
934 | int (*sign) (EVP_PKEY_CTX *ctx, | |
935 | unsigned char *sig, size_t *siglen, | |
936 | const unsigned char *tbs, | |
937 | size_t tbslen)) | |
938 | { | |
939 | pmeth->sign_init = sign_init; | |
940 | pmeth->sign = sign; | |
941 | } | |
ba30bad5 DSH |
942 | |
943 | void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
944 | int (*verify_init) (EVP_PKEY_CTX *ctx), |
945 | int (*verify) (EVP_PKEY_CTX *ctx, | |
946 | const unsigned char *sig, | |
947 | size_t siglen, | |
948 | const unsigned char *tbs, | |
949 | size_t tbslen)) | |
950 | { | |
951 | pmeth->verify_init = verify_init; | |
952 | pmeth->verify = verify; | |
953 | } | |
ba30bad5 DSH |
954 | |
955 | void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
956 | int (*verify_recover_init) (EVP_PKEY_CTX |
957 | *ctx), | |
958 | int (*verify_recover) (EVP_PKEY_CTX | |
959 | *ctx, | |
960 | unsigned char | |
961 | *sig, | |
962 | size_t *siglen, | |
963 | const unsigned | |
964 | char *tbs, | |
965 | size_t tbslen)) | |
966 | { | |
967 | pmeth->verify_recover_init = verify_recover_init; | |
968 | pmeth->verify_recover = verify_recover; | |
969 | } | |
ba30bad5 DSH |
970 | |
971 | void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
972 | int (*signctx_init) (EVP_PKEY_CTX *ctx, |
973 | EVP_MD_CTX *mctx), | |
974 | int (*signctx) (EVP_PKEY_CTX *ctx, | |
975 | unsigned char *sig, | |
976 | size_t *siglen, | |
977 | EVP_MD_CTX *mctx)) | |
978 | { | |
979 | pmeth->signctx_init = signctx_init; | |
980 | pmeth->signctx = signctx; | |
981 | } | |
ba30bad5 DSH |
982 | |
983 | void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
984 | int (*verifyctx_init) (EVP_PKEY_CTX *ctx, |
985 | EVP_MD_CTX *mctx), | |
986 | int (*verifyctx) (EVP_PKEY_CTX *ctx, | |
987 | const unsigned char *sig, | |
988 | int siglen, | |
989 | EVP_MD_CTX *mctx)) | |
990 | { | |
991 | pmeth->verifyctx_init = verifyctx_init; | |
992 | pmeth->verifyctx = verifyctx; | |
993 | } | |
ba30bad5 DSH |
994 | |
995 | void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
996 | int (*encrypt_init) (EVP_PKEY_CTX *ctx), |
997 | int (*encryptfn) (EVP_PKEY_CTX *ctx, | |
998 | unsigned char *out, | |
999 | size_t *outlen, | |
1000 | const unsigned char *in, | |
1001 | size_t inlen)) | |
1002 | { | |
1003 | pmeth->encrypt_init = encrypt_init; | |
1004 | pmeth->encrypt = encryptfn; | |
1005 | } | |
ba30bad5 DSH |
1006 | |
1007 | void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1008 | int (*decrypt_init) (EVP_PKEY_CTX *ctx), |
1009 | int (*decrypt) (EVP_PKEY_CTX *ctx, | |
1010 | unsigned char *out, | |
1011 | size_t *outlen, | |
1012 | const unsigned char *in, | |
1013 | size_t inlen)) | |
1014 | { | |
1015 | pmeth->decrypt_init = decrypt_init; | |
1016 | pmeth->decrypt = decrypt; | |
1017 | } | |
ba30bad5 DSH |
1018 | |
1019 | void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1020 | int (*derive_init) (EVP_PKEY_CTX *ctx), |
1021 | int (*derive) (EVP_PKEY_CTX *ctx, | |
1022 | unsigned char *key, | |
1023 | size_t *keylen)) | |
1024 | { | |
1025 | pmeth->derive_init = derive_init; | |
1026 | pmeth->derive = derive; | |
1027 | } | |
ba30bad5 DSH |
1028 | |
1029 | void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
1030 | int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
1031 | void *p2), | |
1032 | int (*ctrl_str) (EVP_PKEY_CTX *ctx, | |
1033 | const char *type, | |
1034 | const char *value)) | |
1035 | { | |
1036 | pmeth->ctrl = ctrl; | |
1037 | pmeth->ctrl_str = ctrl_str; | |
1038 | } | |
e7451ed1 | 1039 | |
2aee35d3 PY |
1040 | void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, |
1041 | int (*check) (EVP_PKEY *pkey)) | |
1042 | { | |
1043 | pmeth->check = check; | |
1044 | } | |
1045 | ||
b0004708 PY |
1046 | void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth, |
1047 | int (*check) (EVP_PKEY *pkey)) | |
1048 | { | |
1049 | pmeth->public_check = check; | |
1050 | } | |
1051 | ||
1052 | void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth, | |
1053 | int (*check) (EVP_PKEY *pkey)) | |
1054 | { | |
1055 | pmeth->param_check = check; | |
1056 | } | |
1057 | ||
0a8fdef7 PY |
1058 | void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth, |
1059 | int (*digest_custom) (EVP_PKEY_CTX *ctx, | |
1060 | EVP_MD_CTX *mctx)) | |
1061 | { | |
1062 | pmeth->digest_custom = digest_custom; | |
1063 | } | |
1064 | ||
693be9a2 | 1065 | void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1066 | int (**pinit) (EVP_PKEY_CTX *ctx)) |
1067 | { | |
1068 | *pinit = pmeth->init; | |
1069 | } | |
1070 | ||
693be9a2 | 1071 | void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 | 1072 | int (**pcopy) (EVP_PKEY_CTX *dst, |
9fdcc21f | 1073 | const EVP_PKEY_CTX *src)) |
e7451ed1 DSH |
1074 | { |
1075 | *pcopy = pmeth->copy; | |
1076 | } | |
1077 | ||
693be9a2 | 1078 | void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1079 | void (**pcleanup) (EVP_PKEY_CTX *ctx)) |
1080 | { | |
1081 | *pcleanup = pmeth->cleanup; | |
1082 | } | |
1083 | ||
693be9a2 | 1084 | void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1085 | int (**pparamgen_init) (EVP_PKEY_CTX *ctx), |
1086 | int (**pparamgen) (EVP_PKEY_CTX *ctx, | |
1087 | EVP_PKEY *pkey)) | |
1088 | { | |
1089 | if (pparamgen_init) | |
1090 | *pparamgen_init = pmeth->paramgen_init; | |
1091 | if (pparamgen) | |
1092 | *pparamgen = pmeth->paramgen; | |
1093 | } | |
1094 | ||
693be9a2 | 1095 | void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1096 | int (**pkeygen_init) (EVP_PKEY_CTX *ctx), |
1097 | int (**pkeygen) (EVP_PKEY_CTX *ctx, | |
1098 | EVP_PKEY *pkey)) | |
1099 | { | |
1100 | if (pkeygen_init) | |
1101 | *pkeygen_init = pmeth->keygen_init; | |
1102 | if (pkeygen) | |
1103 | *pkeygen = pmeth->keygen; | |
1104 | } | |
1105 | ||
693be9a2 | 1106 | void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1107 | int (**psign_init) (EVP_PKEY_CTX *ctx), |
1108 | int (**psign) (EVP_PKEY_CTX *ctx, | |
1109 | unsigned char *sig, size_t *siglen, | |
1110 | const unsigned char *tbs, | |
1111 | size_t tbslen)) | |
1112 | { | |
1113 | if (psign_init) | |
1114 | *psign_init = pmeth->sign_init; | |
1115 | if (psign) | |
1116 | *psign = pmeth->sign; | |
1117 | } | |
1118 | ||
693be9a2 | 1119 | void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1120 | int (**pverify_init) (EVP_PKEY_CTX *ctx), |
1121 | int (**pverify) (EVP_PKEY_CTX *ctx, | |
1122 | const unsigned char *sig, | |
1123 | size_t siglen, | |
1124 | const unsigned char *tbs, | |
1125 | size_t tbslen)) | |
1126 | { | |
1127 | if (pverify_init) | |
1128 | *pverify_init = pmeth->verify_init; | |
1129 | if (pverify) | |
1130 | *pverify = pmeth->verify; | |
1131 | } | |
1132 | ||
693be9a2 | 1133 | void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1134 | int (**pverify_recover_init) (EVP_PKEY_CTX |
1135 | *ctx), | |
1136 | int (**pverify_recover) (EVP_PKEY_CTX | |
1137 | *ctx, | |
1138 | unsigned char | |
1139 | *sig, | |
1140 | size_t *siglen, | |
1141 | const unsigned | |
1142 | char *tbs, | |
1143 | size_t tbslen)) | |
1144 | { | |
1145 | if (pverify_recover_init) | |
1146 | *pverify_recover_init = pmeth->verify_recover_init; | |
1147 | if (pverify_recover) | |
1148 | *pverify_recover = pmeth->verify_recover; | |
1149 | } | |
1150 | ||
693be9a2 | 1151 | void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1152 | int (**psignctx_init) (EVP_PKEY_CTX *ctx, |
1153 | EVP_MD_CTX *mctx), | |
1154 | int (**psignctx) (EVP_PKEY_CTX *ctx, | |
1155 | unsigned char *sig, | |
1156 | size_t *siglen, | |
1157 | EVP_MD_CTX *mctx)) | |
1158 | { | |
1159 | if (psignctx_init) | |
1160 | *psignctx_init = pmeth->signctx_init; | |
1161 | if (psignctx) | |
1162 | *psignctx = pmeth->signctx; | |
1163 | } | |
1164 | ||
693be9a2 | 1165 | void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1166 | int (**pverifyctx_init) (EVP_PKEY_CTX *ctx, |
1167 | EVP_MD_CTX *mctx), | |
1168 | int (**pverifyctx) (EVP_PKEY_CTX *ctx, | |
1169 | const unsigned char *sig, | |
1170 | int siglen, | |
1171 | EVP_MD_CTX *mctx)) | |
1172 | { | |
1173 | if (pverifyctx_init) | |
1174 | *pverifyctx_init = pmeth->verifyctx_init; | |
1175 | if (pverifyctx) | |
1176 | *pverifyctx = pmeth->verifyctx; | |
1177 | } | |
1178 | ||
693be9a2 | 1179 | void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1180 | int (**pencrypt_init) (EVP_PKEY_CTX *ctx), |
1181 | int (**pencryptfn) (EVP_PKEY_CTX *ctx, | |
1182 | unsigned char *out, | |
1183 | size_t *outlen, | |
1184 | const unsigned char *in, | |
1185 | size_t inlen)) | |
1186 | { | |
1187 | if (pencrypt_init) | |
1188 | *pencrypt_init = pmeth->encrypt_init; | |
1189 | if (pencryptfn) | |
1190 | *pencryptfn = pmeth->encrypt; | |
1191 | } | |
1192 | ||
693be9a2 | 1193 | void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1194 | int (**pdecrypt_init) (EVP_PKEY_CTX *ctx), |
1195 | int (**pdecrypt) (EVP_PKEY_CTX *ctx, | |
1196 | unsigned char *out, | |
1197 | size_t *outlen, | |
1198 | const unsigned char *in, | |
1199 | size_t inlen)) | |
1200 | { | |
1201 | if (pdecrypt_init) | |
1202 | *pdecrypt_init = pmeth->decrypt_init; | |
1203 | if (pdecrypt) | |
1204 | *pdecrypt = pmeth->decrypt; | |
1205 | } | |
1206 | ||
693be9a2 | 1207 | void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1208 | int (**pderive_init) (EVP_PKEY_CTX *ctx), |
1209 | int (**pderive) (EVP_PKEY_CTX *ctx, | |
1210 | unsigned char *key, | |
1211 | size_t *keylen)) | |
1212 | { | |
1213 | if (pderive_init) | |
1214 | *pderive_init = pmeth->derive_init; | |
1215 | if (pderive) | |
1216 | *pderive = pmeth->derive; | |
1217 | } | |
1218 | ||
693be9a2 | 1219 | void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1220 | int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
1221 | void *p2), | |
1222 | int (**pctrl_str) (EVP_PKEY_CTX *ctx, | |
1223 | const char *type, | |
1224 | const char *value)) | |
1225 | { | |
1226 | if (pctrl) | |
1227 | *pctrl = pmeth->ctrl; | |
1228 | if (pctrl_str) | |
1229 | *pctrl_str = pmeth->ctrl_str; | |
1230 | } | |
2aee35d3 | 1231 | |
693be9a2 | 1232 | void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth, |
2aee35d3 PY |
1233 | int (**pcheck) (EVP_PKEY *pkey)) |
1234 | { | |
34f5c8b1 | 1235 | if (pcheck != NULL) |
2aee35d3 PY |
1236 | *pcheck = pmeth->check; |
1237 | } | |
b0004708 | 1238 | |
693be9a2 | 1239 | void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth, |
b0004708 PY |
1240 | int (**pcheck) (EVP_PKEY *pkey)) |
1241 | { | |
34f5c8b1 | 1242 | if (pcheck != NULL) |
b0004708 PY |
1243 | *pcheck = pmeth->public_check; |
1244 | } | |
1245 | ||
693be9a2 | 1246 | void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth, |
b0004708 PY |
1247 | int (**pcheck) (EVP_PKEY *pkey)) |
1248 | { | |
34f5c8b1 | 1249 | if (pcheck != NULL) |
b0004708 PY |
1250 | *pcheck = pmeth->param_check; |
1251 | } | |
0a8fdef7 PY |
1252 | |
1253 | void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth, | |
1254 | int (**pdigest_custom) (EVP_PKEY_CTX *ctx, | |
1255 | EVP_MD_CTX *mctx)) | |
1256 | { | |
675f4cee | 1257 | if (pdigest_custom != NULL) |
0a8fdef7 PY |
1258 | *pdigest_custom = pmeth->digest_custom; |
1259 | } |