]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/evp/pmeth_lib.c
[PROV][KMGMT][KEXCH][EC] Implement EC keymgtm and ECDH
[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 EVP_PKEY_CTX *EVP_PKEY_CTX_dup(const EVP_PKEY_CTX *pctx)
383 {
384 EVP_PKEY_CTX *rctx;
385
386 if (((pctx->pmeth == NULL) || (pctx->pmeth->copy == NULL))
387 && ((EVP_PKEY_CTX_IS_DERIVE_OP(pctx)
388 && pctx->op.kex.exchprovctx == NULL)
389 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)
390 && pctx->op.sig.sigprovctx == NULL)))
391 return NULL;
392 # ifndef OPENSSL_NO_ENGINE
393 /* Make sure it's safe to copy a pkey context using an ENGINE */
394 if (pctx->engine && !ENGINE_init(pctx->engine)) {
395 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_ENGINE_LIB);
396 return 0;
397 }
398 # endif
399 rctx = OPENSSL_zalloc(sizeof(*rctx));
400 if (rctx == NULL) {
401 EVPerr(EVP_F_EVP_PKEY_CTX_DUP, ERR_R_MALLOC_FAILURE);
402 return NULL;
403 }
404
405 if (pctx->pkey != NULL)
406 EVP_PKEY_up_ref(pctx->pkey);
407 rctx->pkey = pctx->pkey;
408 rctx->operation = pctx->operation;
409 rctx->libctx = pctx->libctx;
410 rctx->keytype = pctx->keytype;
411 rctx->propquery = pctx->propquery;
412
413 if (EVP_PKEY_CTX_IS_DERIVE_OP(pctx)) {
414 if (pctx->op.kex.exchange != NULL) {
415 rctx->op.kex.exchange = pctx->op.kex.exchange;
416 if (!EVP_KEYEXCH_up_ref(rctx->op.kex.exchange)) {
417 OPENSSL_free(rctx);
418 return NULL;
419 }
420 }
421 if (pctx->op.kex.exchprovctx != NULL) {
422 if (!ossl_assert(pctx->op.kex.exchange != NULL))
423 return NULL;
424 rctx->op.kex.exchprovctx
425 = pctx->op.kex.exchange->dupctx(pctx->op.kex.exchprovctx);
426 if (rctx->op.kex.exchprovctx == NULL) {
427 EVP_KEYEXCH_free(rctx->op.kex.exchange);
428 OPENSSL_free(rctx);
429 return NULL;
430 }
431 return rctx;
432 }
433 } else if (EVP_PKEY_CTX_IS_SIGNATURE_OP(pctx)) {
434 if (pctx->op.sig.signature != NULL) {
435 rctx->op.sig.signature = pctx->op.sig.signature;
436 if (!EVP_SIGNATURE_up_ref(rctx->op.sig.signature)) {
437 OPENSSL_free(rctx);
438 return NULL;
439 }
440 }
441 if (pctx->op.sig.sigprovctx != NULL) {
442 if (!ossl_assert(pctx->op.sig.signature != NULL))
443 return NULL;
444 rctx->op.sig.sigprovctx
445 = pctx->op.sig.signature->dupctx(pctx->op.sig.sigprovctx);
446 if (rctx->op.sig.sigprovctx == NULL) {
447 EVP_SIGNATURE_free(rctx->op.sig.signature);
448 OPENSSL_free(rctx);
449 return NULL;
450 }
451 return rctx;
452 }
453 } else if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(pctx)) {
454 if (pctx->op.ciph.cipher != NULL) {
455 rctx->op.ciph.cipher = pctx->op.ciph.cipher;
456 if (!EVP_ASYM_CIPHER_up_ref(rctx->op.ciph.cipher)) {
457 OPENSSL_free(rctx);
458 return NULL;
459 }
460 }
461 if (pctx->op.ciph.ciphprovctx != NULL) {
462 if (!ossl_assert(pctx->op.ciph.cipher != NULL))
463 return NULL;
464 rctx->op.ciph.ciphprovctx
465 = pctx->op.ciph.cipher->dupctx(pctx->op.ciph.ciphprovctx);
466 if (rctx->op.ciph.ciphprovctx == NULL) {
467 EVP_ASYM_CIPHER_free(rctx->op.ciph.cipher);
468 OPENSSL_free(rctx);
469 return NULL;
470 }
471 return rctx;
472 }
473 }
474
475 rctx->pmeth = pctx->pmeth;
476 # ifndef OPENSSL_NO_ENGINE
477 rctx->engine = pctx->engine;
478 # endif
479
480 if (pctx->peerkey)
481 EVP_PKEY_up_ref(pctx->peerkey);
482 rctx->peerkey = pctx->peerkey;
483
484 if (pctx->pmeth->copy(rctx, pctx) > 0)
485 return rctx;
486
487 rctx->pmeth = NULL;
488 EVP_PKEY_CTX_free(rctx);
489 return NULL;
490
491 }
492
493 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD *pmeth)
494 {
495 if (app_pkey_methods == NULL) {
496 app_pkey_methods = sk_EVP_PKEY_METHOD_new(pmeth_cmp);
497 if (app_pkey_methods == NULL){
498 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
499 return 0;
500 }
501 }
502 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods, pmeth)) {
503 EVPerr(EVP_F_EVP_PKEY_METH_ADD0, ERR_R_MALLOC_FAILURE);
504 return 0;
505 }
506 sk_EVP_PKEY_METHOD_sort(app_pkey_methods);
507 return 1;
508 }
509
510 void evp_app_cleanup_int(void)
511 {
512 if (app_pkey_methods != NULL)
513 sk_EVP_PKEY_METHOD_pop_free(app_pkey_methods, EVP_PKEY_meth_free);
514 }
515
516 int EVP_PKEY_meth_remove(const EVP_PKEY_METHOD *pmeth)
517 {
518 const EVP_PKEY_METHOD *ret;
519
520 ret = sk_EVP_PKEY_METHOD_delete_ptr(app_pkey_methods, pmeth);
521
522 return ret == NULL ? 0 : 1;
523 }
524
525 size_t EVP_PKEY_meth_get_count(void)
526 {
527 size_t rv = OSSL_NELEM(standard_methods);
528
529 if (app_pkey_methods)
530 rv += sk_EVP_PKEY_METHOD_num(app_pkey_methods);
531 return rv;
532 }
533
534 const EVP_PKEY_METHOD *EVP_PKEY_meth_get0(size_t idx)
535 {
536 if (idx < OSSL_NELEM(standard_methods))
537 return (standard_methods[idx])();
538 if (app_pkey_methods == NULL)
539 return NULL;
540 idx -= OSSL_NELEM(standard_methods);
541 if (idx >= (size_t)sk_EVP_PKEY_METHOD_num(app_pkey_methods))
542 return NULL;
543 return sk_EVP_PKEY_METHOD_value(app_pkey_methods, idx);
544 }
545 #endif
546
547 int EVP_PKEY_CTX_set_params(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
548 {
549 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
550 && ctx->op.kex.exchprovctx != NULL
551 && ctx->op.kex.exchange != NULL
552 && ctx->op.kex.exchange->set_ctx_params != NULL)
553 return ctx->op.kex.exchange->set_ctx_params(ctx->op.kex.exchprovctx,
554 params);
555 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
556 && ctx->op.sig.sigprovctx != NULL
557 && ctx->op.sig.signature != NULL
558 && ctx->op.sig.signature->set_ctx_params != NULL)
559 return ctx->op.sig.signature->set_ctx_params(ctx->op.sig.sigprovctx,
560 params);
561 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
562 && ctx->op.ciph.ciphprovctx != NULL
563 && ctx->op.ciph.cipher != NULL
564 && ctx->op.ciph.cipher->set_ctx_params != NULL)
565 return ctx->op.ciph.cipher->set_ctx_params(ctx->op.ciph.ciphprovctx,
566 params);
567 return 0;
568 }
569
570 #ifndef FIPS_MODE
571 int EVP_PKEY_CTX_get_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->get_ctx_params != NULL)
577 return ctx->op.kex.exchange->get_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->get_ctx_params != NULL)
583 return ctx->op.sig.signature->get_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->get_ctx_params != NULL)
589 return ctx->op.ciph.cipher->get_ctx_params(ctx->op.ciph.ciphprovctx,
590 params);
591 return 0;
592 }
593
594 const OSSL_PARAM *EVP_PKEY_CTX_gettable_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->gettable_ctx_params != NULL)
599 return ctx->op.kex.exchange->gettable_ctx_params();
600 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
601 && ctx->op.sig.signature != NULL
602 && ctx->op.sig.signature->gettable_ctx_params != NULL)
603 return ctx->op.sig.signature->gettable_ctx_params();
604
605 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
606 && ctx->op.ciph.cipher != NULL
607 && ctx->op.ciph.cipher->gettable_ctx_params != NULL)
608 return ctx->op.ciph.cipher->gettable_ctx_params();
609
610 return NULL;
611 }
612
613 const OSSL_PARAM *EVP_PKEY_CTX_settable_params(EVP_PKEY_CTX *ctx)
614 {
615 if (EVP_PKEY_CTX_IS_DERIVE_OP(ctx)
616 && ctx->op.kex.exchange != NULL
617 && ctx->op.kex.exchange->settable_ctx_params != NULL)
618 return ctx->op.kex.exchange->settable_ctx_params();
619 if (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
620 && ctx->op.sig.signature != NULL
621 && ctx->op.sig.signature->settable_ctx_params != NULL)
622 return ctx->op.sig.signature->settable_ctx_params();
623 if (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
624 && ctx->op.ciph.cipher != NULL
625 && ctx->op.ciph.cipher->settable_ctx_params != NULL)
626 return ctx->op.ciph.cipher->settable_ctx_params();
627
628 return NULL;
629 }
630
631 /*
632 * Internal helpers for stricter EVP_PKEY_CTX_{set,get}_params().
633 *
634 * Return 1 on success, 0 or negative for errors.
635 *
636 * In particular they return -2 if any of the params is not supported.
637 *
638 * They are not available in FIPS_MODE as they depend on
639 * - EVP_PKEY_CTX_{get,set}_params()
640 * - EVP_PKEY_CTX_{gettable,settable}_params()
641 *
642 */
643 int evp_pkey_ctx_set_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
644 {
645 const OSSL_PARAM *p;
646
647 if (ctx == NULL || params == NULL)
648 return 0;
649
650 for (p = params; p->key != NULL; p++) {
651 /* Check the ctx actually understands this parameter */
652 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_settable_params(ctx),
653 p->key) == NULL )
654 return -2;
655 }
656
657 return EVP_PKEY_CTX_set_params(ctx, params);
658 }
659
660 int evp_pkey_ctx_get_params_strict(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
661 {
662 const OSSL_PARAM *p;
663
664 if (ctx == NULL || params == NULL)
665 return 0;
666
667 for (p = params; p->key != NULL; p++ ) {
668 /* Check the ctx actually understands this parameter */
669 if (OSSL_PARAM_locate_const(EVP_PKEY_CTX_gettable_params(ctx),
670 p->key) == NULL )
671 return -2;
672 }
673
674 return EVP_PKEY_CTX_get_params(ctx, params);
675 }
676
677 # ifndef OPENSSL_NO_DH
678 int EVP_PKEY_CTX_set_dh_pad(EVP_PKEY_CTX *ctx, int pad)
679 {
680 OSSL_PARAM dh_pad_params[2];
681 unsigned int upad = pad;
682
683 /* We use EVP_PKEY_CTX_ctrl return values */
684 if (ctx == NULL || !EVP_PKEY_CTX_IS_DERIVE_OP(ctx)) {
685 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
686 return -2;
687 }
688
689 /* TODO(3.0): Remove this eventually when no more legacy */
690 if (ctx->op.kex.exchprovctx == NULL)
691 return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DH, EVP_PKEY_OP_DERIVE,
692 EVP_PKEY_CTRL_DH_PAD, pad, NULL);
693
694 dh_pad_params[0] = OSSL_PARAM_construct_uint(OSSL_EXCHANGE_PARAM_PAD, &upad);
695 dh_pad_params[1] = OSSL_PARAM_construct_end();
696
697 return EVP_PKEY_CTX_set_params(ctx, dh_pad_params);
698 }
699 # endif
700
701 int EVP_PKEY_CTX_get_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
702 {
703 OSSL_PARAM sig_md_params[3], *p = sig_md_params;
704 /* 80 should be big enough */
705 char name[80] = "";
706 const EVP_MD *tmp;
707
708 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
709 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
710 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
711 return -2;
712 }
713
714 /* TODO(3.0): Remove this eventually when no more legacy */
715 if (ctx->op.sig.sigprovctx == NULL)
716 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
717 EVP_PKEY_CTRL_GET_MD, 0, (void *)(md));
718
719 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
720 name,
721 sizeof(name));
722 *p++ = OSSL_PARAM_construct_end();
723
724 if (!EVP_PKEY_CTX_get_params(ctx, sig_md_params))
725 return 0;
726
727 tmp = evp_get_digestbyname_ex(ctx->libctx, name);
728 if (tmp == NULL)
729 return 0;
730
731 *md = tmp;
732
733 return 1;
734 }
735
736 int EVP_PKEY_CTX_set_signature_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
737 {
738 OSSL_PARAM sig_md_params[2], *p = sig_md_params;
739 const char *name;
740
741 if (ctx == NULL || !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)) {
742 ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
743 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
744 return -2;
745 }
746
747 /* TODO(3.0): Remove this eventually when no more legacy */
748 if (ctx->op.sig.sigprovctx == NULL)
749 return EVP_PKEY_CTX_ctrl(ctx, -1, EVP_PKEY_OP_TYPE_SIG,
750 EVP_PKEY_CTRL_MD, 0, (void *)(md));
751
752 if (md == NULL) {
753 name = "";
754 } else {
755 name = EVP_MD_name(md);
756 }
757
758 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
759 /*
760 * Cast away the const. This is read
761 * only so should be safe
762 */
763 (char *)name, 0);
764 *p++ = OSSL_PARAM_construct_end();
765
766 return EVP_PKEY_CTX_set_params(ctx, sig_md_params);
767 }
768
769 static int legacy_ctrl_to_param(EVP_PKEY_CTX *ctx, int keytype, int optype,
770 int cmd, int p1, void *p2)
771 {
772 # ifndef OPENSSL_NO_DH
773 if (keytype == EVP_PKEY_DH) {
774 switch (cmd) {
775 case EVP_PKEY_CTRL_DH_PAD:
776 return EVP_PKEY_CTX_set_dh_pad(ctx, p1);
777 }
778 }
779 # endif
780 # ifndef OPENSSL_NO_EC
781 if (keytype == EVP_PKEY_EC) {
782 switch (cmd) {
783 case EVP_PKEY_CTRL_EC_ECDH_COFACTOR:
784 if (p1 == -2) {
785 return EVP_PKEY_CTX_get_ecdh_cofactor_mode(ctx);
786 } else if (p1 < -1 || p1 > 1) {
787 /* Uses the same return values as EVP_PKEY_CTX_ctrl */
788 return -2;
789 } else {
790 return EVP_PKEY_CTX_set_ecdh_cofactor_mode(ctx, p1);
791 }
792 case EVP_PKEY_CTRL_EC_KDF_TYPE:
793 if (p1 == -2) {
794 return EVP_PKEY_CTX_get_ecdh_kdf_type(ctx);
795 } else {
796 return EVP_PKEY_CTX_set_ecdh_kdf_type(ctx, p1);
797 }
798 case EVP_PKEY_CTRL_GET_EC_KDF_MD:
799 return EVP_PKEY_CTX_get_ecdh_kdf_md(ctx, p2);
800 case EVP_PKEY_CTRL_EC_KDF_MD:
801 return EVP_PKEY_CTX_set_ecdh_kdf_md(ctx, p2);
802 case EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN:
803 return EVP_PKEY_CTX_get_ecdh_kdf_outlen(ctx, p2);
804 case EVP_PKEY_CTRL_EC_KDF_OUTLEN:
805 return EVP_PKEY_CTX_set_ecdh_kdf_outlen(ctx, p1);
806 case EVP_PKEY_CTRL_GET_EC_KDF_UKM:
807 return EVP_PKEY_CTX_get0_ecdh_kdf_ukm(ctx, p2);
808 case EVP_PKEY_CTRL_EC_KDF_UKM:
809 return EVP_PKEY_CTX_set0_ecdh_kdf_ukm(ctx, p2, p1);
810 }
811 }
812 # endif
813 if (keytype == -1) {
814 switch (cmd) {
815 case EVP_PKEY_CTRL_MD:
816 return EVP_PKEY_CTX_set_signature_md(ctx, p2);
817 case EVP_PKEY_CTRL_GET_MD:
818 return EVP_PKEY_CTX_get_signature_md(ctx, p2);
819 case EVP_PKEY_CTRL_RSA_PADDING:
820 return EVP_PKEY_CTX_set_rsa_padding(ctx, p1);
821 case EVP_PKEY_CTRL_GET_RSA_PADDING:
822 return EVP_PKEY_CTX_get_rsa_padding(ctx, p2);
823 case EVP_PKEY_CTRL_RSA_OAEP_MD:
824 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
825 case EVP_PKEY_CTRL_GET_RSA_OAEP_MD:
826 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
827 case EVP_PKEY_CTRL_RSA_MGF1_MD:
828 return EVP_PKEY_CTX_set_rsa_oaep_md(ctx, p2);
829 case EVP_PKEY_CTRL_GET_RSA_MGF1_MD:
830 return EVP_PKEY_CTX_get_rsa_oaep_md(ctx, p2);
831 case EVP_PKEY_CTRL_RSA_OAEP_LABEL:
832 return EVP_PKEY_CTX_set0_rsa_oaep_label(ctx, p2, p1);
833 case EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL:
834 return EVP_PKEY_CTX_get0_rsa_oaep_label(ctx, (unsigned char **)p2);
835 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
836 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
837 # ifndef OPENSSL_NO_CMS
838 case EVP_PKEY_CTRL_CMS_DECRYPT:
839 case EVP_PKEY_CTRL_CMS_ENCRYPT:
840 # endif
841 if (ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
842 return 1;
843 ERR_raise(ERR_LIB_EVP,
844 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
845 return -2;
846 }
847 }
848 return 0;
849 }
850
851 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
852 int cmd, int p1, void *p2)
853 {
854 int ret;
855
856 if (ctx == NULL) {
857 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
858 return -2;
859 }
860
861 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
862 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
863 && ctx->op.sig.sigprovctx != NULL)
864 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
865 && ctx->op.ciph.ciphprovctx != NULL))
866 return legacy_ctrl_to_param(ctx, keytype, optype, cmd, p1, p2);
867
868 if (ctx->pmeth == NULL || ctx->pmeth->ctrl == NULL) {
869 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
870 return -2;
871 }
872 if ((keytype != -1) && (ctx->pmeth->pkey_id != keytype))
873 return -1;
874
875 /* Skip the operation checks since this is called in a very early stage */
876 if (ctx->pmeth->digest_custom != NULL)
877 goto doit;
878
879 if (ctx->operation == EVP_PKEY_OP_UNDEFINED) {
880 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_NO_OPERATION_SET);
881 return -1;
882 }
883
884 if ((optype != -1) && !(ctx->operation & optype)) {
885 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_INVALID_OPERATION);
886 return -1;
887 }
888
889 doit:
890 ret = ctx->pmeth->ctrl(ctx, cmd, p1, p2);
891
892 if (ret == -2)
893 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL, EVP_R_COMMAND_NOT_SUPPORTED);
894
895 return ret;
896 }
897
898 int EVP_PKEY_CTX_ctrl_uint64(EVP_PKEY_CTX *ctx, int keytype, int optype,
899 int cmd, uint64_t value)
900 {
901 return EVP_PKEY_CTX_ctrl(ctx, keytype, optype, cmd, 0, &value);
902 }
903
904 static int legacy_ctrl_str_to_param(EVP_PKEY_CTX *ctx, const char *name,
905 const char *value)
906 {
907 if (strcmp(name, "rsa_padding_mode") == 0)
908 name = OSSL_ASYM_CIPHER_PARAM_PAD_MODE;
909 else if (strcmp(name, "rsa_mgf1_md") == 0)
910 name = OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST;
911 else if (strcmp(name, "rsa_oaep_md") == 0)
912 name = OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST;
913 else if (strcmp(name, "rsa_oaep_label") == 0)
914 name = OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL;
915 # ifndef OPENSSL_NO_DH
916 else if (strcmp(name, "dh_pad") == 0)
917 name = OSSL_EXCHANGE_PARAM_PAD;
918 # endif
919 # ifndef OPENSSL_NO_EC
920 else if (strcmp(name, "ecdh_cofactor_mode") == 0)
921 name = OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE;
922 else if (strcmp(name, "ecdh_kdf_md") == 0)
923 name = OSSL_EXCHANGE_PARAM_KDF_TYPE;
924 # endif
925
926 {
927 /*
928 * TODO(3.0) reduce the code above to only translate known legacy
929 * string to the corresponding core name (see core_names.h), but
930 * otherwise leave it to this code block to do the actual work.
931 */
932 const OSSL_PARAM *settable = EVP_PKEY_CTX_settable_params(ctx);
933 OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
934 int rv = 0;
935
936 if (!OSSL_PARAM_allocate_from_text(&params[0], settable, name, value,
937 strlen(value)))
938 return 0;
939 if (EVP_PKEY_CTX_set_params(ctx, params))
940 rv = 1;
941 OPENSSL_free(params[0].data);
942 return rv;
943 }
944 }
945
946 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX *ctx,
947 const char *name, const char *value)
948 {
949 if (ctx == NULL) {
950 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
951 return -2;
952 }
953
954 if ((EVP_PKEY_CTX_IS_DERIVE_OP(ctx) && ctx->op.kex.exchprovctx != NULL)
955 || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
956 && ctx->op.sig.sigprovctx != NULL)
957 || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
958 && ctx->op.ciph.ciphprovctx != NULL))
959 return legacy_ctrl_str_to_param(ctx, name, value);
960
961 if (!ctx || !ctx->pmeth || !ctx->pmeth->ctrl_str) {
962 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR, EVP_R_COMMAND_NOT_SUPPORTED);
963 return -2;
964 }
965 if (strcmp(name, "digest") == 0)
966 return EVP_PKEY_CTX_md(ctx, EVP_PKEY_OP_TYPE_SIG, EVP_PKEY_CTRL_MD,
967 value);
968 return ctx->pmeth->ctrl_str(ctx, name, value);
969 }
970
971 /* Utility functions to send a string of hex string to a ctrl */
972
973 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *str)
974 {
975 size_t len;
976
977 len = strlen(str);
978 if (len > INT_MAX)
979 return -1;
980 return ctx->pmeth->ctrl(ctx, cmd, len, (void *)str);
981 }
982
983 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX *ctx, int cmd, const char *hex)
984 {
985 unsigned char *bin;
986 long binlen;
987 int rv = -1;
988
989 bin = OPENSSL_hexstr2buf(hex, &binlen);
990 if (bin == NULL)
991 return 0;
992 if (binlen <= INT_MAX)
993 rv = ctx->pmeth->ctrl(ctx, cmd, binlen, bin);
994 OPENSSL_free(bin);
995 return rv;
996 }
997
998 /* Pass a message digest to a ctrl */
999 int EVP_PKEY_CTX_md(EVP_PKEY_CTX *ctx, int optype, int cmd, const char *md)
1000 {
1001 const EVP_MD *m;
1002
1003 if (md == NULL || (m = EVP_get_digestbyname(md)) == NULL) {
1004 EVPerr(EVP_F_EVP_PKEY_CTX_MD, EVP_R_INVALID_DIGEST);
1005 return 0;
1006 }
1007 return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, 0, (void *)m);
1008 }
1009
1010 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX *ctx)
1011 {
1012 return ctx->operation;
1013 }
1014
1015 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX *ctx, int *dat, int datlen)
1016 {
1017 ctx->keygen_info = dat;
1018 ctx->keygen_info_count = datlen;
1019 }
1020
1021 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX *ctx, void *data)
1022 {
1023 ctx->data = data;
1024 }
1025
1026 void *EVP_PKEY_CTX_get_data(const EVP_PKEY_CTX *ctx)
1027 {
1028 return ctx->data;
1029 }
1030
1031 EVP_PKEY *EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX *ctx)
1032 {
1033 return ctx->pkey;
1034 }
1035
1036 EVP_PKEY *EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX *ctx)
1037 {
1038 return ctx->peerkey;
1039 }
1040
1041 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX *ctx, void *data)
1042 {
1043 ctx->app_data = data;
1044 }
1045
1046 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX *ctx)
1047 {
1048 return ctx->app_data;
1049 }
1050
1051 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD *pmeth,
1052 int (*init) (EVP_PKEY_CTX *ctx))
1053 {
1054 pmeth->init = init;
1055 }
1056
1057 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD *pmeth,
1058 int (*copy) (EVP_PKEY_CTX *dst,
1059 const EVP_PKEY_CTX *src))
1060 {
1061 pmeth->copy = copy;
1062 }
1063
1064 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD *pmeth,
1065 void (*cleanup) (EVP_PKEY_CTX *ctx))
1066 {
1067 pmeth->cleanup = cleanup;
1068 }
1069
1070 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD *pmeth,
1071 int (*paramgen_init) (EVP_PKEY_CTX *ctx),
1072 int (*paramgen) (EVP_PKEY_CTX *ctx,
1073 EVP_PKEY *pkey))
1074 {
1075 pmeth->paramgen_init = paramgen_init;
1076 pmeth->paramgen = paramgen;
1077 }
1078
1079 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD *pmeth,
1080 int (*keygen_init) (EVP_PKEY_CTX *ctx),
1081 int (*keygen) (EVP_PKEY_CTX *ctx,
1082 EVP_PKEY *pkey))
1083 {
1084 pmeth->keygen_init = keygen_init;
1085 pmeth->keygen = keygen;
1086 }
1087
1088 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD *pmeth,
1089 int (*sign_init) (EVP_PKEY_CTX *ctx),
1090 int (*sign) (EVP_PKEY_CTX *ctx,
1091 unsigned char *sig, size_t *siglen,
1092 const unsigned char *tbs,
1093 size_t tbslen))
1094 {
1095 pmeth->sign_init = sign_init;
1096 pmeth->sign = sign;
1097 }
1098
1099 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD *pmeth,
1100 int (*verify_init) (EVP_PKEY_CTX *ctx),
1101 int (*verify) (EVP_PKEY_CTX *ctx,
1102 const unsigned char *sig,
1103 size_t siglen,
1104 const unsigned char *tbs,
1105 size_t tbslen))
1106 {
1107 pmeth->verify_init = verify_init;
1108 pmeth->verify = verify;
1109 }
1110
1111 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD *pmeth,
1112 int (*verify_recover_init) (EVP_PKEY_CTX
1113 *ctx),
1114 int (*verify_recover) (EVP_PKEY_CTX
1115 *ctx,
1116 unsigned char
1117 *sig,
1118 size_t *siglen,
1119 const unsigned
1120 char *tbs,
1121 size_t tbslen))
1122 {
1123 pmeth->verify_recover_init = verify_recover_init;
1124 pmeth->verify_recover = verify_recover;
1125 }
1126
1127 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD *pmeth,
1128 int (*signctx_init) (EVP_PKEY_CTX *ctx,
1129 EVP_MD_CTX *mctx),
1130 int (*signctx) (EVP_PKEY_CTX *ctx,
1131 unsigned char *sig,
1132 size_t *siglen,
1133 EVP_MD_CTX *mctx))
1134 {
1135 pmeth->signctx_init = signctx_init;
1136 pmeth->signctx = signctx;
1137 }
1138
1139 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD *pmeth,
1140 int (*verifyctx_init) (EVP_PKEY_CTX *ctx,
1141 EVP_MD_CTX *mctx),
1142 int (*verifyctx) (EVP_PKEY_CTX *ctx,
1143 const unsigned char *sig,
1144 int siglen,
1145 EVP_MD_CTX *mctx))
1146 {
1147 pmeth->verifyctx_init = verifyctx_init;
1148 pmeth->verifyctx = verifyctx;
1149 }
1150
1151 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD *pmeth,
1152 int (*encrypt_init) (EVP_PKEY_CTX *ctx),
1153 int (*encryptfn) (EVP_PKEY_CTX *ctx,
1154 unsigned char *out,
1155 size_t *outlen,
1156 const unsigned char *in,
1157 size_t inlen))
1158 {
1159 pmeth->encrypt_init = encrypt_init;
1160 pmeth->encrypt = encryptfn;
1161 }
1162
1163 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD *pmeth,
1164 int (*decrypt_init) (EVP_PKEY_CTX *ctx),
1165 int (*decrypt) (EVP_PKEY_CTX *ctx,
1166 unsigned char *out,
1167 size_t *outlen,
1168 const unsigned char *in,
1169 size_t inlen))
1170 {
1171 pmeth->decrypt_init = decrypt_init;
1172 pmeth->decrypt = decrypt;
1173 }
1174
1175 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD *pmeth,
1176 int (*derive_init) (EVP_PKEY_CTX *ctx),
1177 int (*derive) (EVP_PKEY_CTX *ctx,
1178 unsigned char *key,
1179 size_t *keylen))
1180 {
1181 pmeth->derive_init = derive_init;
1182 pmeth->derive = derive;
1183 }
1184
1185 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD *pmeth,
1186 int (*ctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1187 void *p2),
1188 int (*ctrl_str) (EVP_PKEY_CTX *ctx,
1189 const char *type,
1190 const char *value))
1191 {
1192 pmeth->ctrl = ctrl;
1193 pmeth->ctrl_str = ctrl_str;
1194 }
1195
1196 void EVP_PKEY_meth_set_digestsign(EVP_PKEY_METHOD *pmeth,
1197 int (*digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1198 const unsigned char *tbs, size_t tbslen))
1199 {
1200 pmeth->digestsign = digestsign;
1201 }
1202
1203 void EVP_PKEY_meth_set_digestverify(EVP_PKEY_METHOD *pmeth,
1204 int (*digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1205 size_t siglen, const unsigned char *tbs,
1206 size_t tbslen))
1207 {
1208 pmeth->digestverify = digestverify;
1209 }
1210
1211 void EVP_PKEY_meth_set_check(EVP_PKEY_METHOD *pmeth,
1212 int (*check) (EVP_PKEY *pkey))
1213 {
1214 pmeth->check = check;
1215 }
1216
1217 void EVP_PKEY_meth_set_public_check(EVP_PKEY_METHOD *pmeth,
1218 int (*check) (EVP_PKEY *pkey))
1219 {
1220 pmeth->public_check = check;
1221 }
1222
1223 void EVP_PKEY_meth_set_param_check(EVP_PKEY_METHOD *pmeth,
1224 int (*check) (EVP_PKEY *pkey))
1225 {
1226 pmeth->param_check = check;
1227 }
1228
1229 void EVP_PKEY_meth_set_digest_custom(EVP_PKEY_METHOD *pmeth,
1230 int (*digest_custom) (EVP_PKEY_CTX *ctx,
1231 EVP_MD_CTX *mctx))
1232 {
1233 pmeth->digest_custom = digest_custom;
1234 }
1235
1236 void EVP_PKEY_meth_get_init(const EVP_PKEY_METHOD *pmeth,
1237 int (**pinit) (EVP_PKEY_CTX *ctx))
1238 {
1239 *pinit = pmeth->init;
1240 }
1241
1242 void EVP_PKEY_meth_get_copy(const EVP_PKEY_METHOD *pmeth,
1243 int (**pcopy) (EVP_PKEY_CTX *dst,
1244 const EVP_PKEY_CTX *src))
1245 {
1246 *pcopy = pmeth->copy;
1247 }
1248
1249 void EVP_PKEY_meth_get_cleanup(const EVP_PKEY_METHOD *pmeth,
1250 void (**pcleanup) (EVP_PKEY_CTX *ctx))
1251 {
1252 *pcleanup = pmeth->cleanup;
1253 }
1254
1255 void EVP_PKEY_meth_get_paramgen(const EVP_PKEY_METHOD *pmeth,
1256 int (**pparamgen_init) (EVP_PKEY_CTX *ctx),
1257 int (**pparamgen) (EVP_PKEY_CTX *ctx,
1258 EVP_PKEY *pkey))
1259 {
1260 if (pparamgen_init)
1261 *pparamgen_init = pmeth->paramgen_init;
1262 if (pparamgen)
1263 *pparamgen = pmeth->paramgen;
1264 }
1265
1266 void EVP_PKEY_meth_get_keygen(const EVP_PKEY_METHOD *pmeth,
1267 int (**pkeygen_init) (EVP_PKEY_CTX *ctx),
1268 int (**pkeygen) (EVP_PKEY_CTX *ctx,
1269 EVP_PKEY *pkey))
1270 {
1271 if (pkeygen_init)
1272 *pkeygen_init = pmeth->keygen_init;
1273 if (pkeygen)
1274 *pkeygen = pmeth->keygen;
1275 }
1276
1277 void EVP_PKEY_meth_get_sign(const EVP_PKEY_METHOD *pmeth,
1278 int (**psign_init) (EVP_PKEY_CTX *ctx),
1279 int (**psign) (EVP_PKEY_CTX *ctx,
1280 unsigned char *sig, size_t *siglen,
1281 const unsigned char *tbs,
1282 size_t tbslen))
1283 {
1284 if (psign_init)
1285 *psign_init = pmeth->sign_init;
1286 if (psign)
1287 *psign = pmeth->sign;
1288 }
1289
1290 void EVP_PKEY_meth_get_verify(const EVP_PKEY_METHOD *pmeth,
1291 int (**pverify_init) (EVP_PKEY_CTX *ctx),
1292 int (**pverify) (EVP_PKEY_CTX *ctx,
1293 const unsigned char *sig,
1294 size_t siglen,
1295 const unsigned char *tbs,
1296 size_t tbslen))
1297 {
1298 if (pverify_init)
1299 *pverify_init = pmeth->verify_init;
1300 if (pverify)
1301 *pverify = pmeth->verify;
1302 }
1303
1304 void EVP_PKEY_meth_get_verify_recover(const EVP_PKEY_METHOD *pmeth,
1305 int (**pverify_recover_init) (EVP_PKEY_CTX
1306 *ctx),
1307 int (**pverify_recover) (EVP_PKEY_CTX
1308 *ctx,
1309 unsigned char
1310 *sig,
1311 size_t *siglen,
1312 const unsigned
1313 char *tbs,
1314 size_t tbslen))
1315 {
1316 if (pverify_recover_init)
1317 *pverify_recover_init = pmeth->verify_recover_init;
1318 if (pverify_recover)
1319 *pverify_recover = pmeth->verify_recover;
1320 }
1321
1322 void EVP_PKEY_meth_get_signctx(const EVP_PKEY_METHOD *pmeth,
1323 int (**psignctx_init) (EVP_PKEY_CTX *ctx,
1324 EVP_MD_CTX *mctx),
1325 int (**psignctx) (EVP_PKEY_CTX *ctx,
1326 unsigned char *sig,
1327 size_t *siglen,
1328 EVP_MD_CTX *mctx))
1329 {
1330 if (psignctx_init)
1331 *psignctx_init = pmeth->signctx_init;
1332 if (psignctx)
1333 *psignctx = pmeth->signctx;
1334 }
1335
1336 void EVP_PKEY_meth_get_verifyctx(const EVP_PKEY_METHOD *pmeth,
1337 int (**pverifyctx_init) (EVP_PKEY_CTX *ctx,
1338 EVP_MD_CTX *mctx),
1339 int (**pverifyctx) (EVP_PKEY_CTX *ctx,
1340 const unsigned char *sig,
1341 int siglen,
1342 EVP_MD_CTX *mctx))
1343 {
1344 if (pverifyctx_init)
1345 *pverifyctx_init = pmeth->verifyctx_init;
1346 if (pverifyctx)
1347 *pverifyctx = pmeth->verifyctx;
1348 }
1349
1350 void EVP_PKEY_meth_get_encrypt(const EVP_PKEY_METHOD *pmeth,
1351 int (**pencrypt_init) (EVP_PKEY_CTX *ctx),
1352 int (**pencryptfn) (EVP_PKEY_CTX *ctx,
1353 unsigned char *out,
1354 size_t *outlen,
1355 const unsigned char *in,
1356 size_t inlen))
1357 {
1358 if (pencrypt_init)
1359 *pencrypt_init = pmeth->encrypt_init;
1360 if (pencryptfn)
1361 *pencryptfn = pmeth->encrypt;
1362 }
1363
1364 void EVP_PKEY_meth_get_decrypt(const EVP_PKEY_METHOD *pmeth,
1365 int (**pdecrypt_init) (EVP_PKEY_CTX *ctx),
1366 int (**pdecrypt) (EVP_PKEY_CTX *ctx,
1367 unsigned char *out,
1368 size_t *outlen,
1369 const unsigned char *in,
1370 size_t inlen))
1371 {
1372 if (pdecrypt_init)
1373 *pdecrypt_init = pmeth->decrypt_init;
1374 if (pdecrypt)
1375 *pdecrypt = pmeth->decrypt;
1376 }
1377
1378 void EVP_PKEY_meth_get_derive(const EVP_PKEY_METHOD *pmeth,
1379 int (**pderive_init) (EVP_PKEY_CTX *ctx),
1380 int (**pderive) (EVP_PKEY_CTX *ctx,
1381 unsigned char *key,
1382 size_t *keylen))
1383 {
1384 if (pderive_init)
1385 *pderive_init = pmeth->derive_init;
1386 if (pderive)
1387 *pderive = pmeth->derive;
1388 }
1389
1390 void EVP_PKEY_meth_get_ctrl(const EVP_PKEY_METHOD *pmeth,
1391 int (**pctrl) (EVP_PKEY_CTX *ctx, int type, int p1,
1392 void *p2),
1393 int (**pctrl_str) (EVP_PKEY_CTX *ctx,
1394 const char *type,
1395 const char *value))
1396 {
1397 if (pctrl)
1398 *pctrl = pmeth->ctrl;
1399 if (pctrl_str)
1400 *pctrl_str = pmeth->ctrl_str;
1401 }
1402
1403 void EVP_PKEY_meth_get_digestsign(EVP_PKEY_METHOD *pmeth,
1404 int (**digestsign) (EVP_MD_CTX *ctx, unsigned char *sig, size_t *siglen,
1405 const unsigned char *tbs, size_t tbslen))
1406 {
1407 if (digestsign)
1408 *digestsign = pmeth->digestsign;
1409 }
1410
1411 void EVP_PKEY_meth_get_digestverify(EVP_PKEY_METHOD *pmeth,
1412 int (**digestverify) (EVP_MD_CTX *ctx, const unsigned char *sig,
1413 size_t siglen, const unsigned char *tbs,
1414 size_t tbslen))
1415 {
1416 if (digestverify)
1417 *digestverify = pmeth->digestverify;
1418 }
1419
1420 void EVP_PKEY_meth_get_check(const EVP_PKEY_METHOD *pmeth,
1421 int (**pcheck) (EVP_PKEY *pkey))
1422 {
1423 if (pcheck != NULL)
1424 *pcheck = pmeth->check;
1425 }
1426
1427 void EVP_PKEY_meth_get_public_check(const EVP_PKEY_METHOD *pmeth,
1428 int (**pcheck) (EVP_PKEY *pkey))
1429 {
1430 if (pcheck != NULL)
1431 *pcheck = pmeth->public_check;
1432 }
1433
1434 void EVP_PKEY_meth_get_param_check(const EVP_PKEY_METHOD *pmeth,
1435 int (**pcheck) (EVP_PKEY *pkey))
1436 {
1437 if (pcheck != NULL)
1438 *pcheck = pmeth->param_check;
1439 }
1440
1441 void EVP_PKEY_meth_get_digest_custom(EVP_PKEY_METHOD *pmeth,
1442 int (**pdigest_custom) (EVP_PKEY_CTX *ctx,
1443 EVP_MD_CTX *mctx))
1444 {
1445 if (pdigest_custom != NULL)
1446 *pdigest_custom = pmeth->digest_custom;
1447 }
1448
1449 #endif /* FIPS_MODE */