2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 <openssl/objects.h>
13 #include <openssl/evp.h>
14 #include "internal/cryptlib.h"
15 #include "crypto/evp.h"
16 #include "internal/provider.h"
17 #include "evp_local.h"
19 static EVP_SIGNATURE *evp_signature_new(OSSL_PROVIDER *prov)
21 EVP_SIGNATURE *signature = OPENSSL_zalloc(sizeof(EVP_SIGNATURE));
23 signature->lock = CRYPTO_THREAD_lock_new();
24 if (signature->lock == NULL) {
25 OPENSSL_free(signature);
28 signature->prov = prov;
29 ossl_provider_up_ref(prov);
30 signature->refcnt = 1;
35 static void *evp_signature_from_dispatch(int name_id,
36 const OSSL_DISPATCH *fns,
40 EVP_SIGNATURE *signature = NULL;
41 int ctxfncnt = 0, signfncnt = 0, verifyfncnt = 0, verifyrecfncnt = 0;
42 int digsignfncnt = 0, digverifyfncnt = 0;
43 int gparamfncnt = 0, sparamfncnt = 0, gmdparamfncnt = 0, smdparamfncnt = 0;
45 if ((signature = evp_signature_new(prov)) == NULL) {
46 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
50 signature->name_id = name_id;
52 for (; fns->function_id != 0; fns++) {
53 switch (fns->function_id) {
54 case OSSL_FUNC_SIGNATURE_NEWCTX:
55 if (signature->newctx != NULL)
57 signature->newctx = OSSL_get_OP_signature_newctx(fns);
60 case OSSL_FUNC_SIGNATURE_SIGN_INIT:
61 if (signature->sign_init != NULL)
63 signature->sign_init = OSSL_get_OP_signature_sign_init(fns);
66 case OSSL_FUNC_SIGNATURE_SIGN:
67 if (signature->sign != NULL)
69 signature->sign = OSSL_get_OP_signature_sign(fns);
72 case OSSL_FUNC_SIGNATURE_VERIFY_INIT:
73 if (signature->verify_init != NULL)
75 signature->verify_init = OSSL_get_OP_signature_verify_init(fns);
78 case OSSL_FUNC_SIGNATURE_VERIFY:
79 if (signature->verify != NULL)
81 signature->verify = OSSL_get_OP_signature_verify(fns);
84 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT:
85 if (signature->verify_recover_init != NULL)
87 signature->verify_recover_init
88 = OSSL_get_OP_signature_verify_recover_init(fns);
91 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER:
92 if (signature->verify_recover != NULL)
94 signature->verify_recover
95 = OSSL_get_OP_signature_verify_recover(fns);
98 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT:
99 if (signature->digest_sign_init != NULL)
101 signature->digest_sign_init
102 = OSSL_get_OP_signature_digest_sign_init(fns);
105 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE:
106 if (signature->digest_sign_update != NULL)
108 signature->digest_sign_update
109 = OSSL_get_OP_signature_digest_sign_update(fns);
112 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL:
113 if (signature->digest_sign_final != NULL)
115 signature->digest_sign_final
116 = OSSL_get_OP_signature_digest_sign_final(fns);
119 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT:
120 if (signature->digest_verify_init != NULL)
122 signature->digest_verify_init
123 = OSSL_get_OP_signature_digest_verify_init(fns);
126 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE:
127 if (signature->digest_verify_update != NULL)
129 signature->digest_verify_update
130 = OSSL_get_OP_signature_digest_verify_update(fns);
133 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL:
134 if (signature->digest_verify_final != NULL)
136 signature->digest_verify_final
137 = OSSL_get_OP_signature_digest_verify_final(fns);
140 case OSSL_FUNC_SIGNATURE_FREECTX:
141 if (signature->freectx != NULL)
143 signature->freectx = OSSL_get_OP_signature_freectx(fns);
146 case OSSL_FUNC_SIGNATURE_DUPCTX:
147 if (signature->dupctx != NULL)
149 signature->dupctx = OSSL_get_OP_signature_dupctx(fns);
151 case OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS:
152 if (signature->get_ctx_params != NULL)
154 signature->get_ctx_params
155 = OSSL_get_OP_signature_get_ctx_params(fns);
158 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS:
159 if (signature->gettable_ctx_params != NULL)
161 signature->gettable_ctx_params
162 = OSSL_get_OP_signature_gettable_ctx_params(fns);
165 case OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS:
166 if (signature->set_ctx_params != NULL)
168 signature->set_ctx_params
169 = OSSL_get_OP_signature_set_ctx_params(fns);
172 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS:
173 if (signature->settable_ctx_params != NULL)
175 signature->settable_ctx_params
176 = OSSL_get_OP_signature_settable_ctx_params(fns);
179 case OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS:
180 if (signature->get_ctx_md_params != NULL)
182 signature->get_ctx_md_params
183 = OSSL_get_OP_signature_get_ctx_md_params(fns);
186 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS:
187 if (signature->gettable_ctx_md_params != NULL)
189 signature->gettable_ctx_md_params
190 = OSSL_get_OP_signature_gettable_ctx_md_params(fns);
193 case OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS:
194 if (signature->set_ctx_md_params != NULL)
196 signature->set_ctx_md_params
197 = OSSL_get_OP_signature_set_ctx_md_params(fns);
200 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS:
201 if (signature->settable_ctx_md_params != NULL)
203 signature->settable_ctx_md_params
204 = OSSL_get_OP_signature_settable_ctx_md_params(fns);
212 && verifyrecfncnt == 0
214 && digverifyfncnt == 0)
215 || (signfncnt != 0 && signfncnt != 2)
216 || (verifyfncnt != 0 && verifyfncnt != 2)
217 || (verifyrecfncnt != 0 && verifyrecfncnt != 2)
218 || (digsignfncnt != 0 && digsignfncnt != 3)
219 || (digverifyfncnt != 0 && digverifyfncnt != 3)
220 || (gparamfncnt != 0 && gparamfncnt != 2)
221 || (sparamfncnt != 0 && sparamfncnt != 2)
222 || (gmdparamfncnt != 0 && gmdparamfncnt != 2)
223 || (smdparamfncnt != 0 && smdparamfncnt != 2)) {
225 * In order to be a consistent set of functions we must have at least
226 * a set of context functions (newctx and freectx) as well as a set of
227 * "signature" functions:
228 * (sign_init, sign) or
229 * (verify_init verify) or
230 * (verify_recover_init, verify_recover) or
231 * (digest_sign_init, digest_sign_update, digest_sign_final) or
232 * (digest_verify_init, digest_verify_update, digest_verify_final).
234 * set_ctx_params and settable_ctx_params are optional, but if one of
235 * them is present then the other one must also be present. The same
236 * applies to get_ctx_params and gettable_ctx_params. The same rules
237 * apply to the "md_params" functions. The dupctx function is optional.
239 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
245 EVP_SIGNATURE_free(signature);
249 void EVP_SIGNATURE_free(EVP_SIGNATURE *signature)
251 if (signature != NULL) {
254 CRYPTO_DOWN_REF(&signature->refcnt, &i, signature->lock);
257 ossl_provider_free(signature->prov);
258 CRYPTO_THREAD_lock_free(signature->lock);
259 OPENSSL_free(signature);
263 int EVP_SIGNATURE_up_ref(EVP_SIGNATURE *signature)
267 CRYPTO_UP_REF(&signature->refcnt, &ref, signature->lock);
271 OSSL_PROVIDER *EVP_SIGNATURE_provider(const EVP_SIGNATURE *signature)
273 return signature->prov;
276 EVP_SIGNATURE *EVP_SIGNATURE_fetch(OPENSSL_CTX *ctx, const char *algorithm,
277 const char *properties)
279 return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties,
280 evp_signature_from_dispatch, NULL,
281 (int (*)(void *))EVP_SIGNATURE_up_ref,
282 (void (*)(void *))EVP_SIGNATURE_free);
285 int EVP_SIGNATURE_is_a(const EVP_SIGNATURE *signature, const char *name)
287 return evp_is_a(signature->prov, signature->name_id, name);
290 int EVP_SIGNATURE_number(const EVP_SIGNATURE *signature)
292 return signature->name_id;
295 void EVP_SIGNATURE_do_all_provided(OPENSSL_CTX *libctx,
296 void (*fn)(EVP_SIGNATURE *signature,
300 evp_generic_do_all(libctx, OSSL_OP_SIGNATURE,
301 (void (*)(void *, void *))fn, arg,
302 evp_signature_from_dispatch, NULL,
303 (void (*)(void *))EVP_SIGNATURE_free);
307 void EVP_SIGNATURE_names_do_all(const EVP_SIGNATURE *signature,
308 void (*fn)(const char *name, void *data),
311 if (signature->prov != NULL)
312 evp_names_do_all(signature->prov, signature->name_id, fn, data);
315 static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, int operation)
318 void *provkey = NULL;
319 EVP_SIGNATURE *signature = NULL;
322 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
326 evp_pkey_ctx_free_old_ops(ctx);
327 ctx->operation = operation;
329 if (ctx->algorithm == NULL)
333 * Because we cleared out old ops, we shouldn't need to worry about
334 * checking if signature is already there. Keymgmt is a different
335 * matter, as it isn't tied to a specific EVP_PKEY op.
337 signature = EVP_SIGNATURE_fetch(ctx->libctx, ctx->algorithm,
339 if (signature != NULL && ctx->keymgmt == NULL) {
340 int name_id = EVP_SIGNATURE_number(signature);
342 ctx->keymgmt = evp_keymgmt_fetch_by_number(ctx->libctx, name_id,
346 if (ctx->keymgmt == NULL
348 || (EVP_KEYMGMT_provider(ctx->keymgmt)
349 != EVP_SIGNATURE_provider(signature))) {
351 * We don't have the full support we need with provided methods,
352 * let's go see if legacy does. Also, we don't need to free
353 * ctx->keymgmt here, as it's not necessarily tied to this
354 * operation. It will be freed by EVP_PKEY_CTX_free().
356 EVP_SIGNATURE_free(signature);
360 ctx->op.sig.signature = signature;
362 if (ctx->pkey != NULL) {
364 evp_keymgmt_export_to_provider(ctx->pkey, ctx->keymgmt, 0);
365 if (provkey == NULL) {
366 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
370 ctx->op.sig.sigprovctx = signature->newctx(ossl_provider_ctx(signature->prov));
371 if (ctx->op.sig.sigprovctx == NULL) {
372 /* The provider key can stay in the cache */
373 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
378 case EVP_PKEY_OP_SIGN:
379 if (signature->sign_init == NULL) {
380 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
384 ret = signature->sign_init(ctx->op.sig.sigprovctx, provkey);
386 case EVP_PKEY_OP_VERIFY:
387 if (signature->verify_init == NULL) {
388 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
392 ret = signature->verify_init(ctx->op.sig.sigprovctx, provkey);
394 case EVP_PKEY_OP_VERIFYRECOVER:
395 if (signature->verify_recover_init == NULL) {
396 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
400 ret = signature->verify_recover_init(ctx->op.sig.sigprovctx, provkey);
403 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
408 signature->freectx(ctx->op.sig.sigprovctx);
409 ctx->op.sig.sigprovctx = NULL;
415 if (ctx->pmeth == NULL
416 || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
417 || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
418 || (operation == EVP_PKEY_OP_VERIFYRECOVER
419 && ctx->pmeth->verify_recover == NULL)) {
420 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
425 case EVP_PKEY_OP_SIGN:
426 if (ctx->pmeth->sign_init == NULL)
428 ret = ctx->pmeth->sign_init(ctx);
430 case EVP_PKEY_OP_VERIFY:
431 if (ctx->pmeth->verify_init == NULL)
433 ret = ctx->pmeth->verify_init(ctx);
435 case EVP_PKEY_OP_VERIFYRECOVER:
436 if (ctx->pmeth->verify_recover_init == NULL)
438 ret = ctx->pmeth->verify_recover_init(ctx);
441 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
449 ctx->operation = EVP_PKEY_OP_UNDEFINED;
453 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
455 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_SIGN);
458 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
459 unsigned char *sig, size_t *siglen,
460 const unsigned char *tbs, size_t tbslen)
465 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
469 if (ctx->operation != EVP_PKEY_OP_SIGN) {
470 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
474 if (ctx->op.sig.sigprovctx == NULL)
477 ret = ctx->op.sig.signature->sign(ctx->op.sig.sigprovctx, sig, siglen,
478 SIZE_MAX, tbs, tbslen);
483 if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
484 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
488 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
489 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
492 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
494 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFY);
497 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
498 const unsigned char *sig, size_t siglen,
499 const unsigned char *tbs, size_t tbslen)
504 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
508 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
509 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
513 if (ctx->op.sig.sigprovctx == NULL)
516 ret = ctx->op.sig.signature->verify(ctx->op.sig.sigprovctx, sig, siglen,
521 if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
522 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
526 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
529 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
531 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFYRECOVER);
534 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
535 unsigned char *rout, size_t *routlen,
536 const unsigned char *sig, size_t siglen)
541 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
545 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
546 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
550 if (ctx->op.sig.sigprovctx == NULL)
553 ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.sigprovctx, rout,
555 (rout == NULL ? 0 : *routlen),
559 if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
560 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
563 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
564 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
567 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
570 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
571 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
572 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
575 ctx->operation = EVP_PKEY_OP_ENCRYPT;
576 if (!ctx->pmeth->encrypt_init)
578 ret = ctx->pmeth->encrypt_init(ctx);
580 ctx->operation = EVP_PKEY_OP_UNDEFINED;
584 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
585 unsigned char *out, size_t *outlen,
586 const unsigned char *in, size_t inlen)
588 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
589 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
590 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
593 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
594 EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
597 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
598 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
601 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
604 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
605 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
606 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
609 ctx->operation = EVP_PKEY_OP_DECRYPT;
610 if (!ctx->pmeth->decrypt_init)
612 ret = ctx->pmeth->decrypt_init(ctx);
614 ctx->operation = EVP_PKEY_OP_UNDEFINED;
618 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
619 unsigned char *out, size_t *outlen,
620 const unsigned char *in, size_t inlen)
622 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
623 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
624 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
627 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
628 EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
631 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
632 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);