]>
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; | |
864b89ce MC |
240 | } |
241 | } | |
242 | ||
0f113f3e MC |
243 | EVP_PKEY_METHOD *EVP_PKEY_meth_new(int id, int flags) |
244 | { | |
245 | EVP_PKEY_METHOD *pmeth; | |
b4faea50 | 246 | |
b51bce94 | 247 | pmeth = OPENSSL_zalloc(sizeof(*pmeth)); |
3484236d F |
248 | if (pmeth == NULL) { |
249 | EVPerr(EVP_F_EVP_PKEY_METH_NEW, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 250 | return NULL; |
3484236d | 251 | } |
0f113f3e | 252 | |
0f113f3e MC |
253 | pmeth->pkey_id = id; |
254 | pmeth->flags = flags | EVP_PKEY_FLAG_DYNAMIC; | |
0f113f3e MC |
255 | return pmeth; |
256 | } | |
ba30bad5 | 257 | |
f830c68f | 258 | void EVP_PKEY_meth_get0_info(int *ppkey_id, int *pflags, |
0f113f3e MC |
259 | const EVP_PKEY_METHOD *meth) |
260 | { | |
261 | if (ppkey_id) | |
262 | *ppkey_id = meth->pkey_id; | |
263 | if (pflags) | |
264 | *pflags = meth->flags; | |
265 | } | |
f830c68f DSH |
266 | |
267 | void EVP_PKEY_meth_copy(EVP_PKEY_METHOD *dst, const EVP_PKEY_METHOD *src) | |
0f113f3e | 268 | { |
f830c68f | 269 | |
0f113f3e MC |
270 | dst->init = src->init; |
271 | dst->copy = src->copy; | |
272 | dst->cleanup = src->cleanup; | |
f830c68f | 273 | |
0f113f3e MC |
274 | dst->paramgen_init = src->paramgen_init; |
275 | dst->paramgen = src->paramgen; | |
f830c68f | 276 | |
0f113f3e MC |
277 | dst->keygen_init = src->keygen_init; |
278 | dst->keygen = src->keygen; | |
f830c68f | 279 | |
0f113f3e MC |
280 | dst->sign_init = src->sign_init; |
281 | dst->sign = src->sign; | |
f830c68f | 282 | |
0f113f3e MC |
283 | dst->verify_init = src->verify_init; |
284 | dst->verify = src->verify; | |
f830c68f | 285 | |
0f113f3e MC |
286 | dst->verify_recover_init = src->verify_recover_init; |
287 | dst->verify_recover = src->verify_recover; | |
f830c68f | 288 | |
0f113f3e MC |
289 | dst->signctx_init = src->signctx_init; |
290 | dst->signctx = src->signctx; | |
f830c68f | 291 | |
0f113f3e MC |
292 | dst->verifyctx_init = src->verifyctx_init; |
293 | dst->verifyctx = src->verifyctx; | |
f830c68f | 294 | |
0f113f3e MC |
295 | dst->encrypt_init = src->encrypt_init; |
296 | dst->encrypt = src->encrypt; | |
f830c68f | 297 | |
0f113f3e MC |
298 | dst->decrypt_init = src->decrypt_init; |
299 | dst->decrypt = src->decrypt; | |
f830c68f | 300 | |
0f113f3e MC |
301 | dst->derive_init = src->derive_init; |
302 | dst->derive = src->derive; | |
f830c68f | 303 | |
0f113f3e MC |
304 | dst->ctrl = src->ctrl; |
305 | dst->ctrl_str = src->ctrl_str; | |
2aee35d3 PY |
306 | |
307 | dst->check = src->check; | |
0f113f3e | 308 | } |
f830c68f | 309 | |
ba30bad5 | 310 | void EVP_PKEY_meth_free(EVP_PKEY_METHOD *pmeth) |
0f113f3e MC |
311 | { |
312 | if (pmeth && (pmeth->flags & EVP_PKEY_FLAG_DYNAMIC)) | |
313 | OPENSSL_free(pmeth); | |
314 | } | |
ba30bad5 | 315 | |
f5cda4cb | 316 | EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey, ENGINE *e) |
0f113f3e | 317 | { |
3ee348b0 | 318 | return int_ctx_new(NULL, pkey, e, NULL, NULL, -1); |
0f113f3e | 319 | } |
f5cda4cb DSH |
320 | |
321 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id, ENGINE *e) | |
0f113f3e | 322 | { |
3ee348b0 | 323 | return int_ctx_new(NULL, NULL, e, NULL, NULL, id); |
a07c17ef RL |
324 | } |
325 | ||
3ee348b0 RL |
326 | EVP_PKEY_CTX *EVP_PKEY_CTX_new_provided(OPENSSL_CTX *libctx, |
327 | const char *name, | |
a07c17ef RL |
328 | const char *propquery) |
329 | { | |
3ee348b0 | 330 | return int_ctx_new(libctx, NULL, NULL, name, propquery, -1); |
0f113f3e | 331 | } |
f5cda4cb | 332 | |
9fdcc21f | 333 | EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx) |
0f113f3e MC |
334 | { |
335 | EVP_PKEY_CTX *rctx; | |
ff64702b MC |
336 | |
337 | if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL)) | |
864b89ce MC |
338 | && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx) |
339 | && pctx->op.kex.exchprovctx == NULL) | |
340 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx) | |
341 | && pctx->op.sig.sigprovctx == NULL))) | |
0f113f3e | 342 | return NULL; |
c9777d26 | 343 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e MC |
344 | /* Make sure it's safe to copy a pkey context using an ENGINE */ |
345 | if (pctx->engine && !ENGINE_init(pctx->engine)) { | |
346 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB); | |
347 | return 0; | |
348 | } | |
c9777d26 | 349 | #endif |
ff64702b | 350 | rctx = OPENSSL_zalloc(sizeof(*rctx)); |
3484236d F |
351 | if (rctx == NULL) { |
352 | EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 353 | return NULL; |
3484236d | 354 | } |
8bdcef40 | 355 | |
ff64702b MC |
356 | if (pctx->pkey != NULL) |
357 | EVP_PKEY_up_ref(pctx->pkey); | |
358 | rctx->pkey = pctx->pkey; | |
359 | rctx->operation = pctx->operation; | |
3ee348b0 | 360 | rctx->libctx = pctx->libctx; |
a07c17ef RL |
361 | rctx->algorithm = pctx->algorithm; |
362 | rctx->propquery = pctx->propquery; | |
ff64702b | 363 | |
864b89ce MC |
364 | if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) { |
365 | if (pctx->op.kex.exchange != NULL) { | |
366 | rctx->op.kex.exchange = pctx->op.kex.exchange; | |
367 | if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) { | |
368 | OPENSSL_free(rctx); | |
369 | return NULL; | |
370 | } | |
ff64702b | 371 | } |
864b89ce MC |
372 | if (pctx->op.kex.exchprovctx != NULL) { |
373 | if (!ossl_assert(pctx->op.kex.exchange != NULL)) | |
374 | return NULL; | |
375 | rctx->op.kex.exchprovctx | |
376 | = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx); | |
377 | if (rctx->op.kex.exchprovctx == NULL) { | |
378 | EVP_KEYEXCH_free(rctx->op.kex.exchange); | |
379 | OPENSSL_free(rctx); | |
380 | return NULL; | |
381 | } | |
382 | return rctx; | |
383 | } | |
384 | } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) { | |
385 | if (pctx->op.sig.signature != NULL) { | |
386 | rctx->op.sig.signature = pctx->op.sig.signature; | |
387 | if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) { | |
388 | OPENSSL_free(rctx); | |
389 | return NULL; | |
390 | } | |
391 | } | |
392 | if (pctx->op.sig.sigprovctx != NULL) { | |
393 | if (!ossl_assert(pctx->op.sig.signature != NULL)) | |
394 | return NULL; | |
395 | rctx->op.sig.sigprovctx | |
396 | = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx); | |
397 | if (rctx->op.sig.sigprovctx == NULL) { | |
398 | EVP_SIGNATURE_free(rctx->op.sig.signature); | |
399 | OPENSSL_free(rctx); | |
400 | return NULL; | |
401 | } | |
402 | return rctx; | |
ff64702b | 403 | } |
ff64702b MC |
404 | } |
405 | ||
0f113f3e | 406 | rctx->pmeth = pctx->pmeth; |
c9777d26 | 407 | #ifndef OPENSSL_NO_ENGINE |
0f113f3e | 408 | rctx->engine = pctx->engine; |
c9777d26 | 409 | #endif |
8bdcef40 | 410 | |
0f113f3e | 411 | if (pctx->peerkey) |
03273d61 | 412 | EVP_PKEY_up_ref(pctx->peerkey); |
0f113f3e | 413 | rctx->peerkey = pctx->peerkey; |
8bdcef40 | 414 | |
0f113f3e MC |
415 | if (pctx->pmeth->copy(rctx, pctx) > 0) |
416 | return rctx; | |
8bdcef40 | 417 | |
83b4049a | 418 | rctx->pmeth = NULL; |
0f113f3e MC |
419 | EVP_PKEY_CTX_free(rctx); |
420 | return NULL; | |
8bdcef40 | 421 | |
0f113f3e | 422 | } |
8bdcef40 | 423 | |
ba30bad5 | 424 | int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth) |
0f113f3e MC |
425 | { |
426 | if (app_pkey_methods == NULL) { | |
427 | app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp); | |
3484236d F |
428 | if (app_pkey_methods == NULL){ |
429 | EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 430 | return 0; |
3484236d | 431 | } |
0f113f3e | 432 | } |
3484236d F |
433 | if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) { |
434 | EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE); | |
0f113f3e | 435 | return 0; |
3484236d | 436 | } |
0f113f3e MC |
437 | sk_EVP_PKEY_METHOD_sort(app_pkey_methods); |
438 | return 1; | |
439 | } | |
ba30bad5 | 440 | |
0822e89a PY |
441 | void evp_app_cleanup_int(void) |
442 | { | |
443 | if (app_pkey_methods != NULL) | |
444 | sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free); | |
445 | } | |
446 | ||
447 | int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth) | |
448 | { | |
449 | const EVP_PKEY_METHOD *ret; | |
450 | ||
451 | ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth); | |
452 | ||
453 | return ret == NULL ? 0 : 1; | |
454 | } | |
455 | ||
48ed9c23 DSH |
456 | size_t EVP_PKEY_meth_get_count(void) |
457 | { | |
458 | size_t rv = OSSL_NELEM(standard_methods); | |
459 | ||
460 | if (app_pkey_methods) | |
461 | rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods); | |
462 | return rv; | |
463 | } | |
464 | ||
465 | const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx) | |
466 | { | |
467 | if (idx < OSSL_NELEM(standard_methods)) | |
19bd1fa1 | 468 | return (standard_methods[idx])(); |
48ed9c23 DSH |
469 | if (app_pkey_methods == NULL) |
470 | return NULL; | |
471 | idx -= OSSL_NELEM(standard_methods); | |
472 | if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods)) | |
473 | return NULL; | |
474 | return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx); | |
475 | } | |
476 | ||
5da98aa6 | 477 | void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
478 | { |
479 | if (ctx == NULL) | |
480 | return; | |
481 | if (ctx->pmeth && ctx->pmeth->cleanup) | |
482 | ctx->pmeth->cleanup(ctx); | |
ff64702b | 483 | |
864b89ce | 484 | evp_pkey_ctx_free_old_ops(ctx); |
c0e0984f | 485 | EVP_KEYMGMT_free(ctx->keymgmt); |
dfcb5d29 | 486 | |
c5ba2d99 RS |
487 | EVP_PKEY_free(ctx->pkey); |
488 | EVP_PKEY_free(ctx->peerkey); | |
c9777d26 | 489 | #ifndef OPENSSL_NO_ENGINE |
7c96dbcd | 490 | ENGINE_finish(ctx->engine); |
c9777d26 | 491 | #endif |
0f113f3e MC |
492 | OPENSSL_free(ctx); |
493 | } | |
5da98aa6 | 494 | |
9c45222d MC |
495 | int EVP_PKEY_CTX_get_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
496 | { | |
864b89ce MC |
497 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
498 | && ctx->op.sig.sigprovctx != NULL | |
499 | && ctx->op.sig.signature != NULL | |
500 | && ctx->op.sig.signature->get_ctx_params != NULL) | |
501 | return ctx->op.sig.signature->get_ctx_params(ctx->op.sig.sigprovctx, | |
502 | params); | |
9c45222d MC |
503 | return 0; |
504 | } | |
505 | ||
506 | const OSSL_PARAM *EVP_PKEY_CTX_gettable_params(EVP_PKEY_CTX *ctx) | |
507 | { | |
864b89ce MC |
508 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
509 | && ctx->op.sig.signature != NULL | |
510 | && ctx->op.sig.signature->gettable_ctx_params != NULL) | |
511 | return ctx->op.sig.signature->gettable_ctx_params(); | |
9c45222d MC |
512 | |
513 | return NULL; | |
514 | } | |
515 | ||
35aca9ec MC |
516 | int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params) |
517 | { | |
864b89ce MC |
518 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
519 | && ctx->op.kex.exchprovctx != NULL | |
520 | && ctx->op.kex.exchange != NULL | |
521 | && ctx->op.kex.exchange->set_ctx_params != NULL) | |
522 | return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx, | |
523 | params); | |
524 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
525 | && ctx->op.sig.sigprovctx != NULL | |
526 | && ctx->op.sig.signature != NULL | |
527 | && ctx->op.sig.signature->set_ctx_params != NULL) | |
528 | return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx, | |
529 | params); | |
35aca9ec MC |
530 | return 0; |
531 | } | |
532 | ||
9c45222d MC |
533 | const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx) |
534 | { | |
864b89ce MC |
535 | if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx) |
536 | && ctx->op.kex.exchange != NULL | |
537 | && ctx->op.kex.exchange->settable_ctx_params != NULL) | |
538 | return ctx->op.kex.exchange->settable_ctx_params(); | |
539 | if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
540 | && ctx->op.sig.signature != NULL | |
541 | && ctx->op.sig.signature->settable_ctx_params != NULL) | |
542 | return ctx->op.sig.signature->settable_ctx_params(); | |
9c45222d MC |
543 | |
544 | return NULL; | |
545 | } | |
546 | ||
76ca35e7 | 547 | #ifndef OPENSSL_NO_DH |
35aca9ec MC |
548 | int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad) |
549 | { | |
550 | OSSL_PARAM dh_pad_params[2]; | |
1c3ace68 | 551 | unsigned int upad = pad; |
35aca9ec | 552 | |
864b89ce MC |
553 | /* We use EVP_PKEY_CTX_ctrl return values */ |
554 | if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) { | |
555 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
556 | return -2; | |
557 | } | |
558 | ||
35aca9ec | 559 | /* TODO(3.0): Remove this eventually when no more legacy */ |
864b89ce | 560 | if (ctx->op.kex.exchprovctx == NULL) |
35aca9ec MC |
561 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE, |
562 | EVP_PKEY_CTRL_DH_PAD, pad, NULL); | |
563 | ||
1c3ace68 | 564 | dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad); |
35aca9ec MC |
565 | dh_pad_params[1] = OSSL_PARAM_construct_end(); |
566 | ||
567 | return EVP_PKEY_CTX_set_params(ctx, dh_pad_params); | |
568 | } | |
76ca35e7 | 569 | #endif |
35aca9ec | 570 | |
9c45222d MC |
571 | int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md) |
572 | { | |
573 | OSSL_PARAM sig_md_params[3], *p = sig_md_params; | |
574 | /* 80 should be big enough */ | |
575 | char name[80] = ""; | |
576 | const EVP_MD *tmp; | |
577 | ||
864b89ce | 578 | if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
9c45222d MC |
579 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
580 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
581 | return -2; | |
582 | } | |
583 | ||
584 | /* TODO(3.0): Remove this eventually when no more legacy */ | |
864b89ce | 585 | if (ctx->op.sig.sigprovctx == NULL) |
9c45222d MC |
586 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, |
587 | EVP_PKEY_CTRL_GET_MD, 0, (void *)(md)); | |
588 | ||
589 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, | |
590 | name, | |
591 | sizeof(name)); | |
592 | *p++ = OSSL_PARAM_construct_end(); | |
593 | ||
594 | if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params)) | |
595 | return 0; | |
596 | ||
7606bed9 | 597 | tmp = evp_get_digestbyname_ex(ctx->libctx, name); |
9c45222d MC |
598 | if (tmp == NULL) |
599 | return 0; | |
600 | ||
601 | *md = tmp; | |
602 | ||
603 | return 1; | |
604 | } | |
605 | ||
4889dadc MC |
606 | int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
607 | { | |
9c45222d | 608 | OSSL_PARAM sig_md_params[3], *p = sig_md_params; |
4889dadc MC |
609 | size_t mdsize; |
610 | const char *name; | |
611 | ||
864b89ce | 612 | if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) { |
9c45222d MC |
613 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
614 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
615 | return -2; | |
616 | } | |
617 | ||
4889dadc | 618 | /* TODO(3.0): Remove this eventually when no more legacy */ |
864b89ce | 619 | if (ctx->op.sig.sigprovctx == NULL) |
4889dadc MC |
620 | return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG, |
621 | EVP_PKEY_CTRL_MD, 0, (void *)(md)); | |
622 | ||
9c45222d MC |
623 | if (md == NULL) { |
624 | name = ""; | |
625 | mdsize = 0; | |
626 | } else { | |
627 | mdsize = EVP_MD_size(md); | |
628 | name = EVP_MD_name(md); | |
629 | } | |
4889dadc | 630 | |
9c45222d MC |
631 | *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, |
632 | /* | |
633 | * Cast away the const. This is read | |
634 | * only so should be safe | |
635 | */ | |
636 | (char *)name, | |
637 | strlen(name) + 1); | |
638 | *p++ = OSSL_PARAM_construct_size_t(OSSL_SIGNATURE_PARAM_DIGEST_SIZE, | |
639 | &mdsize); | |
640 | *p++ = OSSL_PARAM_construct_end(); | |
4889dadc | 641 | |
9c45222d | 642 | return EVP_PKEY_CTX_set_params(ctx, sig_md_params); |
4889dadc MC |
643 | } |
644 | ||
35aca9ec MC |
645 | static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype, |
646 | int cmd, int p1, void *p2) | |
647 | { | |
648 | switch (cmd) { | |
76ca35e7 | 649 | #ifndef OPENSSL_NO_DH |
35aca9ec MC |
650 | case EVP_PKEY_CTRL_DH_PAD: |
651 | return EVP_PKEY_CTX_set_dh_pad(ctx, p1); | |
76ca35e7 | 652 | #endif |
390acbeb MC |
653 | case EVP_PKEY_CTRL_MD: |
654 | return EVP_PKEY_CTX_set_signature_md(ctx, p2); | |
864b89ce MC |
655 | case EVP_PKEY_CTRL_GET_MD: |
656 | return EVP_PKEY_CTX_get_signature_md(ctx, p2); | |
35aca9ec MC |
657 | } |
658 | return 0; | |
659 | } | |
660 | ||
0b6f3c66 | 661 | int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype, |
0f113f3e MC |
662 | int cmd, int p1, void *p2) |
663 | { | |
664 | int ret; | |
4803717f | 665 | |
35aca9ec MC |
666 | if (ctx == NULL) { |
667 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
668 | return -2; | |
669 | } | |
670 | ||
864b89ce | 671 | if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL) |
48bb9792 | 672 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) |
864b89ce | 673 | && ctx->op.sig.sigprovctx != NULL)) |
35aca9ec MC |
674 | return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2); |
675 | ||
676 | if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) { | |
0f113f3e MC |
677 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); |
678 | return -2; | |
679 | } | |
680 | if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype)) | |
681 | return -1; | |
682 | ||
4803717f PY |
683 | /* Skip the operation checks since this is called in a very early stage */ |
684 | if (ctx->pmeth->digest_custom != NULL) | |
685 | goto doit; | |
686 | ||
0f113f3e MC |
687 | if (ctx->operation == EVP_PKEY_OP_UNDEFINED) { |
688 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET); | |
689 | return -1; | |
690 | } | |
691 | ||
692 | if ((optype != -1) && !(ctx->operation & optype)) { | |
693 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION); | |
694 | return -1; | |
695 | } | |
696 | ||
4803717f | 697 | doit: |
0f113f3e MC |
698 | ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2); |
699 | ||
700 | if (ret == -2) | |
701 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED); | |
702 | ||
703 | return ret; | |
0f113f3e | 704 | } |
0b6f3c66 | 705 | |
cefa762e | 706 | int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype, |
64bf1016 | 707 | int cmd, uint64_t value) |
cefa762e JB |
708 | { |
709 | return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value); | |
710 | } | |
711 | ||
35aca9ec MC |
712 | static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name, |
713 | const char *value) | |
714 | { | |
76ca35e7 | 715 | #ifndef OPENSSL_NO_DH |
35aca9ec MC |
716 | if (strcmp(name, "dh_pad") == 0) { |
717 | int pad; | |
718 | ||
719 | pad = atoi(value); | |
720 | return EVP_PKEY_CTX_set_dh_pad(ctx, pad); | |
721 | } | |
76ca35e7 | 722 | #endif |
390acbeb MC |
723 | if (strcmp(name, "digest") == 0) { |
724 | int ret; | |
864b89ce MC |
725 | EVP_MD *md; |
726 | ||
727 | if (!EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) || ctx->op.sig.signature == NULL) | |
728 | return 0; | |
729 | md = EVP_MD_fetch(ossl_provider_library_context(ctx->op.sig.signature->prov), | |
730 | value, NULL); | |
390acbeb MC |
731 | if (md == NULL) |
732 | return 0; | |
733 | ret = EVP_PKEY_CTX_set_signature_md(ctx, md); | |
734 | EVP_MD_meth_free(md); | |
735 | return ret; | |
736 | } | |
737 | ||
35aca9ec MC |
738 | return 0; |
739 | } | |
740 | ||
4a3dc3c0 | 741 | int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx, |
0f113f3e MC |
742 | const char *name, const char *value) |
743 | { | |
35aca9ec MC |
744 | if (ctx == NULL) { |
745 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | |
746 | return -2; | |
747 | } | |
748 | ||
864b89ce MC |
749 | if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL) |
750 | || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx) | |
751 | && ctx->op.sig.sigprovctx != NULL)) | |
35aca9ec MC |
752 | return legacy_ctrl_str_to_param(ctx, name, value); |
753 | ||
0f113f3e MC |
754 | if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) { |
755 | EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED); | |
756 | return -2; | |
757 | } | |
410877ba DSH |
758 | if (strcmp(name, "digest") == 0) |
759 | return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD, | |
760 | value); | |
0f113f3e MC |
761 | return ctx->pmeth->ctrl_str(ctx, name, value); |
762 | } | |
f5cda4cb | 763 | |
99119000 DSH |
764 | /* Utility functions to send a string of hex string to a ctrl */ |
765 | ||
766 | int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str) | |
767 | { | |
768 | size_t len; | |
769 | ||
770 | len = strlen(str); | |
771 | if (len > INT_MAX) | |
772 | return -1; | |
773 | return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str); | |
774 | } | |
775 | ||
776 | int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex) | |
777 | { | |
778 | unsigned char *bin; | |
779 | long binlen; | |
780 | int rv = -1; | |
781 | ||
14f051a0 | 782 | bin = OPENSSL_hexstr2buf(hex, &binlen); |
99119000 DSH |
783 | if (bin == NULL) |
784 | return 0; | |
785 | if (binlen <= INT_MAX) | |
786 | rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin); | |
787 | OPENSSL_free(bin); | |
788 | return rv; | |
789 | } | |
52ad523c | 790 | |
410877ba DSH |
791 | /* Pass a message digest to a ctrl */ |
792 | int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md) | |
793 | { | |
794 | const EVP_MD *m; | |
c82bafc5 | 795 | |
410877ba DSH |
796 | if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) { |
797 | EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST); | |
798 | return 0; | |
799 | } | |
800 | return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m); | |
801 | } | |
99119000 | 802 | |
b28dea4e | 803 | int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
804 | { |
805 | return ctx->operation; | |
806 | } | |
b28dea4e DSH |
807 | |
808 | void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen) | |
0f113f3e MC |
809 | { |
810 | ctx->keygen_info = dat; | |
811 | ctx->keygen_info_count = datlen; | |
812 | } | |
b28dea4e | 813 | |
f5cda4cb | 814 | void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data) |
0f113f3e MC |
815 | { |
816 | ctx->data = data; | |
817 | } | |
f5cda4cb | 818 | |
9fdcc21f | 819 | void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx) |
0f113f3e MC |
820 | { |
821 | return ctx->data; | |
822 | } | |
f5cda4cb | 823 | |
81cebb8b | 824 | EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
825 | { |
826 | return ctx->pkey; | |
827 | } | |
81cebb8b | 828 | |
0e1dba93 | 829 | EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx) |
0f113f3e MC |
830 | { |
831 | return ctx->peerkey; | |
832 | } | |
833 | ||
f5cda4cb | 834 | void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data) |
0f113f3e MC |
835 | { |
836 | ctx->app_data = data; | |
837 | } | |
f5cda4cb DSH |
838 | |
839 | void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx) | |
0f113f3e MC |
840 | { |
841 | return ctx->app_data; | |
842 | } | |
ba30bad5 DSH |
843 | |
844 | void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
845 | int (*init) (EVP_PKEY_CTX *ctx)) |
846 | { | |
847 | pmeth->init = init; | |
848 | } | |
8bdcef40 DSH |
849 | |
850 | void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth, | |
0f113f3e | 851 | int (*copy) (EVP_PKEY_CTX *dst, |
9fdcc21f | 852 | const EVP_PKEY_CTX *src)) |
0f113f3e MC |
853 | { |
854 | pmeth->copy = copy; | |
855 | } | |
ba30bad5 DSH |
856 | |
857 | void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
858 | void (*cleanup) (EVP_PKEY_CTX *ctx)) |
859 | { | |
860 | pmeth->cleanup = cleanup; | |
861 | } | |
ba30bad5 DSH |
862 | |
863 | void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
864 | int (*paramgen_init) (EVP_PKEY_CTX *ctx), |
865 | int (*paramgen) (EVP_PKEY_CTX *ctx, | |
866 | EVP_PKEY *pkey)) | |
867 | { | |
868 | pmeth->paramgen_init = paramgen_init; | |
869 | pmeth->paramgen = paramgen; | |
870 | } | |
ba30bad5 DSH |
871 | |
872 | void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
873 | int (*keygen_init) (EVP_PKEY_CTX *ctx), |
874 | int (*keygen) (EVP_PKEY_CTX *ctx, | |
875 | EVP_PKEY *pkey)) | |
876 | { | |
877 | pmeth->keygen_init = keygen_init; | |
878 | pmeth->keygen = keygen; | |
879 | } | |
ba30bad5 DSH |
880 | |
881 | void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
882 | int (*sign_init) (EVP_PKEY_CTX *ctx), |
883 | int (*sign) (EVP_PKEY_CTX *ctx, | |
884 | unsigned char *sig, size_t *siglen, | |
885 | const unsigned char *tbs, | |
886 | size_t tbslen)) | |
887 | { | |
888 | pmeth->sign_init = sign_init; | |
889 | pmeth->sign = sign; | |
890 | } | |
ba30bad5 DSH |
891 | |
892 | void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
893 | int (*verify_init) (EVP_PKEY_CTX *ctx), |
894 | int (*verify) (EVP_PKEY_CTX *ctx, | |
895 | const unsigned char *sig, | |
896 | size_t siglen, | |
897 | const unsigned char *tbs, | |
898 | size_t tbslen)) | |
899 | { | |
900 | pmeth->verify_init = verify_init; | |
901 | pmeth->verify = verify; | |
902 | } | |
ba30bad5 DSH |
903 | |
904 | void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
905 | int (*verify_recover_init) (EVP_PKEY_CTX |
906 | *ctx), | |
907 | int (*verify_recover) (EVP_PKEY_CTX | |
908 | *ctx, | |
909 | unsigned char | |
910 | *sig, | |
911 | size_t *siglen, | |
912 | const unsigned | |
913 | char *tbs, | |
914 | size_t tbslen)) | |
915 | { | |
916 | pmeth->verify_recover_init = verify_recover_init; | |
917 | pmeth->verify_recover = verify_recover; | |
918 | } | |
ba30bad5 DSH |
919 | |
920 | void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
921 | int (*signctx_init) (EVP_PKEY_CTX *ctx, |
922 | EVP_MD_CTX *mctx), | |
923 | int (*signctx) (EVP_PKEY_CTX *ctx, | |
924 | unsigned char *sig, | |
925 | size_t *siglen, | |
926 | EVP_MD_CTX *mctx)) | |
927 | { | |
928 | pmeth->signctx_init = signctx_init; | |
929 | pmeth->signctx = signctx; | |
930 | } | |
ba30bad5 DSH |
931 | |
932 | void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
933 | int (*verifyctx_init) (EVP_PKEY_CTX *ctx, |
934 | EVP_MD_CTX *mctx), | |
935 | int (*verifyctx) (EVP_PKEY_CTX *ctx, | |
936 | const unsigned char *sig, | |
937 | int siglen, | |
938 | EVP_MD_CTX *mctx)) | |
939 | { | |
940 | pmeth->verifyctx_init = verifyctx_init; | |
941 | pmeth->verifyctx = verifyctx; | |
942 | } | |
ba30bad5 DSH |
943 | |
944 | void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
945 | int (*encrypt_init) (EVP_PKEY_CTX *ctx), |
946 | int (*encryptfn) (EVP_PKEY_CTX *ctx, | |
947 | unsigned char *out, | |
948 | size_t *outlen, | |
949 | const unsigned char *in, | |
950 | size_t inlen)) | |
951 | { | |
952 | pmeth->encrypt_init = encrypt_init; | |
953 | pmeth->encrypt = encryptfn; | |
954 | } | |
ba30bad5 DSH |
955 | |
956 | void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
957 | int (*decrypt_init) (EVP_PKEY_CTX *ctx), |
958 | int (*decrypt) (EVP_PKEY_CTX *ctx, | |
959 | unsigned char *out, | |
960 | size_t *outlen, | |
961 | const unsigned char *in, | |
962 | size_t inlen)) | |
963 | { | |
964 | pmeth->decrypt_init = decrypt_init; | |
965 | pmeth->decrypt = decrypt; | |
966 | } | |
ba30bad5 DSH |
967 | |
968 | void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
969 | int (*derive_init) (EVP_PKEY_CTX *ctx), |
970 | int (*derive) (EVP_PKEY_CTX *ctx, | |
971 | unsigned char *key, | |
972 | size_t *keylen)) | |
973 | { | |
974 | pmeth->derive_init = derive_init; | |
975 | pmeth->derive = derive; | |
976 | } | |
ba30bad5 DSH |
977 | |
978 | void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth, | |
0f113f3e MC |
979 | int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
980 | void *p2), | |
981 | int (*ctrl_str) (EVP_PKEY_CTX *ctx, | |
982 | const char *type, | |
983 | const char *value)) | |
984 | { | |
985 | pmeth->ctrl = ctrl; | |
986 | pmeth->ctrl_str = ctrl_str; | |
987 | } | |
e7451ed1 | 988 | |
2aee35d3 PY |
989 | void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth, |
990 | int (*check) (EVP_PKEY *pkey)) | |
991 | { | |
992 | pmeth->check = check; | |
993 | } | |
994 | ||
b0004708 PY |
995 | void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth, |
996 | int (*check) (EVP_PKEY *pkey)) | |
997 | { | |
998 | pmeth->public_check = check; | |
999 | } | |
1000 | ||
1001 | void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth, | |
1002 | int (*check) (EVP_PKEY *pkey)) | |
1003 | { | |
1004 | pmeth->param_check = check; | |
1005 | } | |
1006 | ||
0a8fdef7 PY |
1007 | void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth, |
1008 | int (*digest_custom) (EVP_PKEY_CTX *ctx, | |
1009 | EVP_MD_CTX *mctx)) | |
1010 | { | |
1011 | pmeth->digest_custom = digest_custom; | |
1012 | } | |
1013 | ||
693be9a2 | 1014 | void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1015 | int (**pinit) (EVP_PKEY_CTX *ctx)) |
1016 | { | |
1017 | *pinit = pmeth->init; | |
1018 | } | |
1019 | ||
693be9a2 | 1020 | void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 | 1021 | int (**pcopy) (EVP_PKEY_CTX *dst, |
9fdcc21f | 1022 | const EVP_PKEY_CTX *src)) |
e7451ed1 DSH |
1023 | { |
1024 | *pcopy = pmeth->copy; | |
1025 | } | |
1026 | ||
693be9a2 | 1027 | void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1028 | void (**pcleanup) (EVP_PKEY_CTX *ctx)) |
1029 | { | |
1030 | *pcleanup = pmeth->cleanup; | |
1031 | } | |
1032 | ||
693be9a2 | 1033 | void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1034 | int (**pparamgen_init) (EVP_PKEY_CTX *ctx), |
1035 | int (**pparamgen) (EVP_PKEY_CTX *ctx, | |
1036 | EVP_PKEY *pkey)) | |
1037 | { | |
1038 | if (pparamgen_init) | |
1039 | *pparamgen_init = pmeth->paramgen_init; | |
1040 | if (pparamgen) | |
1041 | *pparamgen = pmeth->paramgen; | |
1042 | } | |
1043 | ||
693be9a2 | 1044 | void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1045 | int (**pkeygen_init) (EVP_PKEY_CTX *ctx), |
1046 | int (**pkeygen) (EVP_PKEY_CTX *ctx, | |
1047 | EVP_PKEY *pkey)) | |
1048 | { | |
1049 | if (pkeygen_init) | |
1050 | *pkeygen_init = pmeth->keygen_init; | |
1051 | if (pkeygen) | |
1052 | *pkeygen = pmeth->keygen; | |
1053 | } | |
1054 | ||
693be9a2 | 1055 | void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1056 | int (**psign_init) (EVP_PKEY_CTX *ctx), |
1057 | int (**psign) (EVP_PKEY_CTX *ctx, | |
1058 | unsigned char *sig, size_t *siglen, | |
1059 | const unsigned char *tbs, | |
1060 | size_t tbslen)) | |
1061 | { | |
1062 | if (psign_init) | |
1063 | *psign_init = pmeth->sign_init; | |
1064 | if (psign) | |
1065 | *psign = pmeth->sign; | |
1066 | } | |
1067 | ||
693be9a2 | 1068 | void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1069 | int (**pverify_init) (EVP_PKEY_CTX *ctx), |
1070 | int (**pverify) (EVP_PKEY_CTX *ctx, | |
1071 | const unsigned char *sig, | |
1072 | size_t siglen, | |
1073 | const unsigned char *tbs, | |
1074 | size_t tbslen)) | |
1075 | { | |
1076 | if (pverify_init) | |
1077 | *pverify_init = pmeth->verify_init; | |
1078 | if (pverify) | |
1079 | *pverify = pmeth->verify; | |
1080 | } | |
1081 | ||
693be9a2 | 1082 | void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1083 | int (**pverify_recover_init) (EVP_PKEY_CTX |
1084 | *ctx), | |
1085 | int (**pverify_recover) (EVP_PKEY_CTX | |
1086 | *ctx, | |
1087 | unsigned char | |
1088 | *sig, | |
1089 | size_t *siglen, | |
1090 | const unsigned | |
1091 | char *tbs, | |
1092 | size_t tbslen)) | |
1093 | { | |
1094 | if (pverify_recover_init) | |
1095 | *pverify_recover_init = pmeth->verify_recover_init; | |
1096 | if (pverify_recover) | |
1097 | *pverify_recover = pmeth->verify_recover; | |
1098 | } | |
1099 | ||
693be9a2 | 1100 | void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1101 | int (**psignctx_init) (EVP_PKEY_CTX *ctx, |
1102 | EVP_MD_CTX *mctx), | |
1103 | int (**psignctx) (EVP_PKEY_CTX *ctx, | |
1104 | unsigned char *sig, | |
1105 | size_t *siglen, | |
1106 | EVP_MD_CTX *mctx)) | |
1107 | { | |
1108 | if (psignctx_init) | |
1109 | *psignctx_init = pmeth->signctx_init; | |
1110 | if (psignctx) | |
1111 | *psignctx = pmeth->signctx; | |
1112 | } | |
1113 | ||
693be9a2 | 1114 | void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1115 | int (**pverifyctx_init) (EVP_PKEY_CTX *ctx, |
1116 | EVP_MD_CTX *mctx), | |
1117 | int (**pverifyctx) (EVP_PKEY_CTX *ctx, | |
1118 | const unsigned char *sig, | |
1119 | int siglen, | |
1120 | EVP_MD_CTX *mctx)) | |
1121 | { | |
1122 | if (pverifyctx_init) | |
1123 | *pverifyctx_init = pmeth->verifyctx_init; | |
1124 | if (pverifyctx) | |
1125 | *pverifyctx = pmeth->verifyctx; | |
1126 | } | |
1127 | ||
693be9a2 | 1128 | void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1129 | int (**pencrypt_init) (EVP_PKEY_CTX *ctx), |
1130 | int (**pencryptfn) (EVP_PKEY_CTX *ctx, | |
1131 | unsigned char *out, | |
1132 | size_t *outlen, | |
1133 | const unsigned char *in, | |
1134 | size_t inlen)) | |
1135 | { | |
1136 | if (pencrypt_init) | |
1137 | *pencrypt_init = pmeth->encrypt_init; | |
1138 | if (pencryptfn) | |
1139 | *pencryptfn = pmeth->encrypt; | |
1140 | } | |
1141 | ||
693be9a2 | 1142 | void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1143 | int (**pdecrypt_init) (EVP_PKEY_CTX *ctx), |
1144 | int (**pdecrypt) (EVP_PKEY_CTX *ctx, | |
1145 | unsigned char *out, | |
1146 | size_t *outlen, | |
1147 | const unsigned char *in, | |
1148 | size_t inlen)) | |
1149 | { | |
1150 | if (pdecrypt_init) | |
1151 | *pdecrypt_init = pmeth->decrypt_init; | |
1152 | if (pdecrypt) | |
1153 | *pdecrypt = pmeth->decrypt; | |
1154 | } | |
1155 | ||
693be9a2 | 1156 | void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1157 | int (**pderive_init) (EVP_PKEY_CTX *ctx), |
1158 | int (**pderive) (EVP_PKEY_CTX *ctx, | |
1159 | unsigned char *key, | |
1160 | size_t *keylen)) | |
1161 | { | |
1162 | if (pderive_init) | |
1163 | *pderive_init = pmeth->derive_init; | |
1164 | if (pderive) | |
1165 | *pderive = pmeth->derive; | |
1166 | } | |
1167 | ||
693be9a2 | 1168 | void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth, |
e7451ed1 DSH |
1169 | int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1, |
1170 | void *p2), | |
1171 | int (**pctrl_str) (EVP_PKEY_CTX *ctx, | |
1172 | const char *type, | |
1173 | const char *value)) | |
1174 | { | |
1175 | if (pctrl) | |
1176 | *pctrl = pmeth->ctrl; | |
1177 | if (pctrl_str) | |
1178 | *pctrl_str = pmeth->ctrl_str; | |
1179 | } | |
2aee35d3 | 1180 | |
693be9a2 | 1181 | void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth, |
2aee35d3 PY |
1182 | int (**pcheck) (EVP_PKEY *pkey)) |
1183 | { | |
34f5c8b1 | 1184 | if (pcheck != NULL) |
2aee35d3 PY |
1185 | *pcheck = pmeth->check; |
1186 | } | |
b0004708 | 1187 | |
693be9a2 | 1188 | void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth, |
b0004708 PY |
1189 | int (**pcheck) (EVP_PKEY *pkey)) |
1190 | { | |
34f5c8b1 | 1191 | if (pcheck != NULL) |
b0004708 PY |
1192 | *pcheck = pmeth->public_check; |
1193 | } | |
1194 | ||
693be9a2 | 1195 | void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth, |
b0004708 PY |
1196 | int (**pcheck) (EVP_PKEY *pkey)) |
1197 | { | |
34f5c8b1 | 1198 | if (pcheck != NULL) |
b0004708 PY |
1199 | *pcheck = pmeth->param_check; |
1200 | } | |
0a8fdef7 PY |
1201 | |
1202 | void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth, | |
1203 | int (**pdigest_custom) (EVP_PKEY_CTX *ctx, | |
1204 | EVP_MD_CTX *mctx)) | |
1205 | { | |
675f4cee | 1206 | if (pdigest_custom != NULL) |
0a8fdef7 PY |
1207 | *pdigest_custom = pmeth->digest_custom; |
1208 | } |