2 * Copyright 2006-2017 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
12 #include "internal/cryptlib.h"
13 #include <openssl/engine.h>
14 #include <openssl/evp.h>
15 #include <openssl/x509v3.h>
16 #include "internal/asn1_int.h"
17 #include "internal/evp_int.h"
18 #include "internal/numbers.h"
20 typedef int sk_cmp_fn_type(const char *const *a
, const char *const *b
);
22 static STACK_OF(EVP_PKEY_METHOD
) *app_pkey_methods
= NULL
;
24 static const EVP_PKEY_METHOD
*standard_methods
[] = {
25 #ifndef OPENSSL_NO_RSA
31 #ifndef OPENSSL_NO_DSA
38 #ifndef OPENSSL_NO_CMAC
41 #ifndef OPENSSL_NO_RSA
52 #ifndef OPENSSL_NO_POLY1305
55 #ifndef OPENSSL_NO_SIPHASH
60 DECLARE_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD
*, const EVP_PKEY_METHOD
*,
63 static int pmeth_cmp(const EVP_PKEY_METHOD
*const *a
,
64 const EVP_PKEY_METHOD
*const *b
)
66 return ((*a
)->pkey_id
- (*b
)->pkey_id
);
69 IMPLEMENT_OBJ_BSEARCH_CMP_FN(const EVP_PKEY_METHOD
*, const EVP_PKEY_METHOD
*,
72 const EVP_PKEY_METHOD
*EVP_PKEY_meth_find(int type
)
75 const EVP_PKEY_METHOD
*t
= &tmp
, **ret
;
77 if (app_pkey_methods
) {
79 idx
= sk_EVP_PKEY_METHOD_find(app_pkey_methods
, &tmp
);
81 return sk_EVP_PKEY_METHOD_value(app_pkey_methods
, idx
);
83 ret
= OBJ_bsearch_pmeth(&t
, standard_methods
,
84 sizeof(standard_methods
) /
85 sizeof(EVP_PKEY_METHOD
*));
91 static EVP_PKEY_CTX
*int_ctx_new(EVP_PKEY
*pkey
, ENGINE
*e
, int id
)
94 const EVP_PKEY_METHOD
*pmeth
;
96 if (!pkey
|| !pkey
->ameth
)
98 id
= pkey
->ameth
->pkey_id
;
100 #ifndef OPENSSL_NO_ENGINE
101 if (pkey
&& pkey
->engine
)
103 /* Try to find an ENGINE which implements this method */
105 if (!ENGINE_init(e
)) {
106 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_ENGINE_LIB
);
110 e
= ENGINE_get_pkey_meth_engine(id
);
113 * If an ENGINE handled this method look it up. Otherwise use internal
118 pmeth
= ENGINE_get_pkey_meth(e
, id
);
121 pmeth
= EVP_PKEY_meth_find(id
);
124 EVPerr(EVP_F_INT_CTX_NEW
, EVP_R_UNSUPPORTED_ALGORITHM
);
128 ret
= OPENSSL_zalloc(sizeof(*ret
));
130 #ifndef OPENSSL_NO_ENGINE
133 EVPerr(EVP_F_INT_CTX_NEW
, ERR_R_MALLOC_FAILURE
);
138 ret
->operation
= EVP_PKEY_OP_UNDEFINED
;
141 EVP_PKEY_up_ref(pkey
);
144 if (pmeth
->init(ret
) <= 0) {
146 EVP_PKEY_CTX_free(ret
);
154 EVP_PKEY_METHOD
*EVP_PKEY_meth_new(int id
, int flags
)
156 EVP_PKEY_METHOD
*pmeth
;
158 pmeth
= OPENSSL_zalloc(sizeof(*pmeth
));
163 pmeth
->flags
= flags
| EVP_PKEY_FLAG_DYNAMIC
;
167 void EVP_PKEY_meth_get0_info(int *ppkey_id
, int *pflags
,
168 const EVP_PKEY_METHOD
*meth
)
171 *ppkey_id
= meth
->pkey_id
;
173 *pflags
= meth
->flags
;
176 void EVP_PKEY_meth_copy(EVP_PKEY_METHOD
*dst
, const EVP_PKEY_METHOD
*src
)
179 dst
->init
= src
->init
;
180 dst
->copy
= src
->copy
;
181 dst
->cleanup
= src
->cleanup
;
183 dst
->paramgen_init
= src
->paramgen_init
;
184 dst
->paramgen
= src
->paramgen
;
186 dst
->keygen_init
= src
->keygen_init
;
187 dst
->keygen
= src
->keygen
;
189 dst
->sign_init
= src
->sign_init
;
190 dst
->sign
= src
->sign
;
192 dst
->verify_init
= src
->verify_init
;
193 dst
->verify
= src
->verify
;
195 dst
->verify_recover_init
= src
->verify_recover_init
;
196 dst
->verify_recover
= src
->verify_recover
;
198 dst
->signctx_init
= src
->signctx_init
;
199 dst
->signctx
= src
->signctx
;
201 dst
->verifyctx_init
= src
->verifyctx_init
;
202 dst
->verifyctx
= src
->verifyctx
;
204 dst
->encrypt_init
= src
->encrypt_init
;
205 dst
->encrypt
= src
->encrypt
;
207 dst
->decrypt_init
= src
->decrypt_init
;
208 dst
->decrypt
= src
->decrypt
;
210 dst
->derive_init
= src
->derive_init
;
211 dst
->derive
= src
->derive
;
213 dst
->ctrl
= src
->ctrl
;
214 dst
->ctrl_str
= src
->ctrl_str
;
217 void EVP_PKEY_meth_free(EVP_PKEY_METHOD
*pmeth
)
219 if (pmeth
&& (pmeth
->flags
& EVP_PKEY_FLAG_DYNAMIC
))
223 EVP_PKEY_CTX
*EVP_PKEY_CTX_new(EVP_PKEY
*pkey
, ENGINE
*e
)
225 return int_ctx_new(pkey
, e
, -1);
228 EVP_PKEY_CTX
*EVP_PKEY_CTX_new_id(int id
, ENGINE
*e
)
230 return int_ctx_new(NULL
, e
, id
);
233 EVP_PKEY_CTX
*EVP_PKEY_CTX_dup(EVP_PKEY_CTX
*pctx
)
236 if (!pctx
->pmeth
|| !pctx
->pmeth
->copy
)
238 #ifndef OPENSSL_NO_ENGINE
239 /* Make sure it's safe to copy a pkey context using an ENGINE */
240 if (pctx
->engine
&& !ENGINE_init(pctx
->engine
)) {
241 EVPerr(EVP_F_EVP_PKEY_CTX_DUP
, ERR_R_ENGINE_LIB
);
245 rctx
= OPENSSL_malloc(sizeof(*rctx
));
249 rctx
->pmeth
= pctx
->pmeth
;
250 #ifndef OPENSSL_NO_ENGINE
251 rctx
->engine
= pctx
->engine
;
255 EVP_PKEY_up_ref(pctx
->pkey
);
257 rctx
->pkey
= pctx
->pkey
;
260 EVP_PKEY_up_ref(pctx
->peerkey
);
262 rctx
->peerkey
= pctx
->peerkey
;
265 rctx
->app_data
= NULL
;
266 rctx
->operation
= pctx
->operation
;
268 if (pctx
->pmeth
->copy(rctx
, pctx
) > 0)
272 EVP_PKEY_CTX_free(rctx
);
277 int EVP_PKEY_meth_add0(const EVP_PKEY_METHOD
*pmeth
)
279 if (app_pkey_methods
== NULL
) {
280 app_pkey_methods
= sk_EVP_PKEY_METHOD_new(pmeth_cmp
);
281 if (app_pkey_methods
== NULL
)
284 if (!sk_EVP_PKEY_METHOD_push(app_pkey_methods
, pmeth
))
286 sk_EVP_PKEY_METHOD_sort(app_pkey_methods
);
290 void EVP_PKEY_CTX_free(EVP_PKEY_CTX
*ctx
)
294 if (ctx
->pmeth
&& ctx
->pmeth
->cleanup
)
295 ctx
->pmeth
->cleanup(ctx
);
296 EVP_PKEY_free(ctx
->pkey
);
297 EVP_PKEY_free(ctx
->peerkey
);
298 #ifndef OPENSSL_NO_ENGINE
299 ENGINE_finish(ctx
->engine
);
304 int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX
*ctx
, int keytype
, int optype
,
305 int cmd
, int p1
, void *p2
)
308 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->ctrl
) {
309 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
312 if ((keytype
!= -1) && (ctx
->pmeth
->pkey_id
!= keytype
))
315 if (ctx
->operation
== EVP_PKEY_OP_UNDEFINED
) {
316 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_NO_OPERATION_SET
);
320 if ((optype
!= -1) && !(ctx
->operation
& optype
)) {
321 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_INVALID_OPERATION
);
325 ret
= ctx
->pmeth
->ctrl(ctx
, cmd
, p1
, p2
);
328 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL
, EVP_R_COMMAND_NOT_SUPPORTED
);
334 int EVP_PKEY_CTX_ctrl_str(EVP_PKEY_CTX
*ctx
,
335 const char *name
, const char *value
)
337 if (!ctx
|| !ctx
->pmeth
|| !ctx
->pmeth
->ctrl_str
) {
338 EVPerr(EVP_F_EVP_PKEY_CTX_CTRL_STR
, EVP_R_COMMAND_NOT_SUPPORTED
);
341 if (strcmp(name
, "digest") == 0)
342 return EVP_PKEY_CTX_md(ctx
, EVP_PKEY_OP_TYPE_SIG
, EVP_PKEY_CTRL_MD
,
344 return ctx
->pmeth
->ctrl_str(ctx
, name
, value
);
347 /* Utility functions to send a string of hex string to a ctrl */
349 int EVP_PKEY_CTX_str2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *str
)
356 return ctx
->pmeth
->ctrl(ctx
, cmd
, len
, (void *)str
);
359 int EVP_PKEY_CTX_hex2ctrl(EVP_PKEY_CTX
*ctx
, int cmd
, const char *hex
)
365 bin
= OPENSSL_hexstr2buf(hex
, &binlen
);
368 if (binlen
<= INT_MAX
)
369 rv
= ctx
->pmeth
->ctrl(ctx
, cmd
, binlen
, bin
);
374 /* Pass a message digest to a ctrl */
375 int EVP_PKEY_CTX_md(EVP_PKEY_CTX
*ctx
, int optype
, int cmd
, const char *md
)
379 if (md
== NULL
|| (m
= EVP_get_digestbyname(md
)) == NULL
) {
380 EVPerr(EVP_F_EVP_PKEY_CTX_MD
, EVP_R_INVALID_DIGEST
);
383 return EVP_PKEY_CTX_ctrl(ctx
, -1, optype
, cmd
, 0, (void *)m
);
386 int EVP_PKEY_CTX_get_operation(EVP_PKEY_CTX
*ctx
)
388 return ctx
->operation
;
391 void EVP_PKEY_CTX_set0_keygen_info(EVP_PKEY_CTX
*ctx
, int *dat
, int datlen
)
393 ctx
->keygen_info
= dat
;
394 ctx
->keygen_info_count
= datlen
;
397 void EVP_PKEY_CTX_set_data(EVP_PKEY_CTX
*ctx
, void *data
)
402 void *EVP_PKEY_CTX_get_data(EVP_PKEY_CTX
*ctx
)
407 EVP_PKEY
*EVP_PKEY_CTX_get0_pkey(EVP_PKEY_CTX
*ctx
)
412 EVP_PKEY
*EVP_PKEY_CTX_get0_peerkey(EVP_PKEY_CTX
*ctx
)
417 void EVP_PKEY_CTX_set_app_data(EVP_PKEY_CTX
*ctx
, void *data
)
419 ctx
->app_data
= data
;
422 void *EVP_PKEY_CTX_get_app_data(EVP_PKEY_CTX
*ctx
)
424 return ctx
->app_data
;
427 void EVP_PKEY_meth_set_init(EVP_PKEY_METHOD
*pmeth
,
428 int (*init
) (EVP_PKEY_CTX
*ctx
))
433 void EVP_PKEY_meth_set_copy(EVP_PKEY_METHOD
*pmeth
,
434 int (*copy
) (EVP_PKEY_CTX
*dst
,
440 void EVP_PKEY_meth_set_cleanup(EVP_PKEY_METHOD
*pmeth
,
441 void (*cleanup
) (EVP_PKEY_CTX
*ctx
))
443 pmeth
->cleanup
= cleanup
;
446 void EVP_PKEY_meth_set_paramgen(EVP_PKEY_METHOD
*pmeth
,
447 int (*paramgen_init
) (EVP_PKEY_CTX
*ctx
),
448 int (*paramgen
) (EVP_PKEY_CTX
*ctx
,
451 pmeth
->paramgen_init
= paramgen_init
;
452 pmeth
->paramgen
= paramgen
;
455 void EVP_PKEY_meth_set_keygen(EVP_PKEY_METHOD
*pmeth
,
456 int (*keygen_init
) (EVP_PKEY_CTX
*ctx
),
457 int (*keygen
) (EVP_PKEY_CTX
*ctx
,
460 pmeth
->keygen_init
= keygen_init
;
461 pmeth
->keygen
= keygen
;
464 void EVP_PKEY_meth_set_sign(EVP_PKEY_METHOD
*pmeth
,
465 int (*sign_init
) (EVP_PKEY_CTX
*ctx
),
466 int (*sign
) (EVP_PKEY_CTX
*ctx
,
467 unsigned char *sig
, size_t *siglen
,
468 const unsigned char *tbs
,
471 pmeth
->sign_init
= sign_init
;
475 void EVP_PKEY_meth_set_verify(EVP_PKEY_METHOD
*pmeth
,
476 int (*verify_init
) (EVP_PKEY_CTX
*ctx
),
477 int (*verify
) (EVP_PKEY_CTX
*ctx
,
478 const unsigned char *sig
,
480 const unsigned char *tbs
,
483 pmeth
->verify_init
= verify_init
;
484 pmeth
->verify
= verify
;
487 void EVP_PKEY_meth_set_verify_recover(EVP_PKEY_METHOD
*pmeth
,
488 int (*verify_recover_init
) (EVP_PKEY_CTX
490 int (*verify_recover
) (EVP_PKEY_CTX
499 pmeth
->verify_recover_init
= verify_recover_init
;
500 pmeth
->verify_recover
= verify_recover
;
503 void EVP_PKEY_meth_set_signctx(EVP_PKEY_METHOD
*pmeth
,
504 int (*signctx_init
) (EVP_PKEY_CTX
*ctx
,
506 int (*signctx
) (EVP_PKEY_CTX
*ctx
,
511 pmeth
->signctx_init
= signctx_init
;
512 pmeth
->signctx
= signctx
;
515 void EVP_PKEY_meth_set_verifyctx(EVP_PKEY_METHOD
*pmeth
,
516 int (*verifyctx_init
) (EVP_PKEY_CTX
*ctx
,
518 int (*verifyctx
) (EVP_PKEY_CTX
*ctx
,
519 const unsigned char *sig
,
523 pmeth
->verifyctx_init
= verifyctx_init
;
524 pmeth
->verifyctx
= verifyctx
;
527 void EVP_PKEY_meth_set_encrypt(EVP_PKEY_METHOD
*pmeth
,
528 int (*encrypt_init
) (EVP_PKEY_CTX
*ctx
),
529 int (*encryptfn
) (EVP_PKEY_CTX
*ctx
,
532 const unsigned char *in
,
535 pmeth
->encrypt_init
= encrypt_init
;
536 pmeth
->encrypt
= encryptfn
;
539 void EVP_PKEY_meth_set_decrypt(EVP_PKEY_METHOD
*pmeth
,
540 int (*decrypt_init
) (EVP_PKEY_CTX
*ctx
),
541 int (*decrypt
) (EVP_PKEY_CTX
*ctx
,
544 const unsigned char *in
,
547 pmeth
->decrypt_init
= decrypt_init
;
548 pmeth
->decrypt
= decrypt
;
551 void EVP_PKEY_meth_set_derive(EVP_PKEY_METHOD
*pmeth
,
552 int (*derive_init
) (EVP_PKEY_CTX
*ctx
),
553 int (*derive
) (EVP_PKEY_CTX
*ctx
,
557 pmeth
->derive_init
= derive_init
;
558 pmeth
->derive
= derive
;
561 void EVP_PKEY_meth_set_ctrl(EVP_PKEY_METHOD
*pmeth
,
562 int (*ctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
564 int (*ctrl_str
) (EVP_PKEY_CTX
*ctx
,
569 pmeth
->ctrl_str
= ctrl_str
;
572 void EVP_PKEY_meth_get_init(EVP_PKEY_METHOD
*pmeth
,
573 int (**pinit
) (EVP_PKEY_CTX
*ctx
))
575 *pinit
= pmeth
->init
;
578 void EVP_PKEY_meth_get_copy(EVP_PKEY_METHOD
*pmeth
,
579 int (**pcopy
) (EVP_PKEY_CTX
*dst
,
582 *pcopy
= pmeth
->copy
;
585 void EVP_PKEY_meth_get_cleanup(EVP_PKEY_METHOD
*pmeth
,
586 void (**pcleanup
) (EVP_PKEY_CTX
*ctx
))
588 *pcleanup
= pmeth
->cleanup
;
591 void EVP_PKEY_meth_get_paramgen(EVP_PKEY_METHOD
*pmeth
,
592 int (**pparamgen_init
) (EVP_PKEY_CTX
*ctx
),
593 int (**pparamgen
) (EVP_PKEY_CTX
*ctx
,
597 *pparamgen_init
= pmeth
->paramgen_init
;
599 *pparamgen
= pmeth
->paramgen
;
602 void EVP_PKEY_meth_get_keygen(EVP_PKEY_METHOD
*pmeth
,
603 int (**pkeygen_init
) (EVP_PKEY_CTX
*ctx
),
604 int (**pkeygen
) (EVP_PKEY_CTX
*ctx
,
608 *pkeygen_init
= pmeth
->keygen_init
;
610 *pkeygen
= pmeth
->keygen
;
613 void EVP_PKEY_meth_get_sign(EVP_PKEY_METHOD
*pmeth
,
614 int (**psign_init
) (EVP_PKEY_CTX
*ctx
),
615 int (**psign
) (EVP_PKEY_CTX
*ctx
,
616 unsigned char *sig
, size_t *siglen
,
617 const unsigned char *tbs
,
621 *psign_init
= pmeth
->sign_init
;
623 *psign
= pmeth
->sign
;
626 void EVP_PKEY_meth_get_verify(EVP_PKEY_METHOD
*pmeth
,
627 int (**pverify_init
) (EVP_PKEY_CTX
*ctx
),
628 int (**pverify
) (EVP_PKEY_CTX
*ctx
,
629 const unsigned char *sig
,
631 const unsigned char *tbs
,
635 *pverify_init
= pmeth
->verify_init
;
637 *pverify
= pmeth
->verify
;
640 void EVP_PKEY_meth_get_verify_recover(EVP_PKEY_METHOD
*pmeth
,
641 int (**pverify_recover_init
) (EVP_PKEY_CTX
643 int (**pverify_recover
) (EVP_PKEY_CTX
652 if (pverify_recover_init
)
653 *pverify_recover_init
= pmeth
->verify_recover_init
;
655 *pverify_recover
= pmeth
->verify_recover
;
658 void EVP_PKEY_meth_get_signctx(EVP_PKEY_METHOD
*pmeth
,
659 int (**psignctx_init
) (EVP_PKEY_CTX
*ctx
,
661 int (**psignctx
) (EVP_PKEY_CTX
*ctx
,
667 *psignctx_init
= pmeth
->signctx_init
;
669 *psignctx
= pmeth
->signctx
;
672 void EVP_PKEY_meth_get_verifyctx(EVP_PKEY_METHOD
*pmeth
,
673 int (**pverifyctx_init
) (EVP_PKEY_CTX
*ctx
,
675 int (**pverifyctx
) (EVP_PKEY_CTX
*ctx
,
676 const unsigned char *sig
,
681 *pverifyctx_init
= pmeth
->verifyctx_init
;
683 *pverifyctx
= pmeth
->verifyctx
;
686 void EVP_PKEY_meth_get_encrypt(EVP_PKEY_METHOD
*pmeth
,
687 int (**pencrypt_init
) (EVP_PKEY_CTX
*ctx
),
688 int (**pencryptfn
) (EVP_PKEY_CTX
*ctx
,
691 const unsigned char *in
,
695 *pencrypt_init
= pmeth
->encrypt_init
;
697 *pencryptfn
= pmeth
->encrypt
;
700 void EVP_PKEY_meth_get_decrypt(EVP_PKEY_METHOD
*pmeth
,
701 int (**pdecrypt_init
) (EVP_PKEY_CTX
*ctx
),
702 int (**pdecrypt
) (EVP_PKEY_CTX
*ctx
,
705 const unsigned char *in
,
709 *pdecrypt_init
= pmeth
->decrypt_init
;
711 *pdecrypt
= pmeth
->decrypt
;
714 void EVP_PKEY_meth_get_derive(EVP_PKEY_METHOD
*pmeth
,
715 int (**pderive_init
) (EVP_PKEY_CTX
*ctx
),
716 int (**pderive
) (EVP_PKEY_CTX
*ctx
,
721 *pderive_init
= pmeth
->derive_init
;
723 *pderive
= pmeth
->derive
;
726 void EVP_PKEY_meth_get_ctrl(EVP_PKEY_METHOD
*pmeth
,
727 int (**pctrl
) (EVP_PKEY_CTX
*ctx
, int type
, int p1
,
729 int (**pctrl_str
) (EVP_PKEY_CTX
*ctx
,
734 *pctrl
= pmeth
->ctrl
;
736 *pctrl_str
= pmeth
->ctrl_str
;