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