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