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