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,
39 EVP_SIGNATURE *signature = NULL;
40 int ctxfncnt = 0, signfncnt = 0, verifyfncnt = 0, verifyrecfncnt = 0;
41 int digsignfncnt = 0, digverifyfncnt = 0;
42 int gparamfncnt = 0, sparamfncnt = 0, gmdparamfncnt = 0, smdparamfncnt = 0;
44 if ((signature = evp_signature_new(prov)) == NULL) {
45 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
49 signature->name_id = name_id;
51 for (; fns->function_id != 0; fns++) {
52 switch (fns->function_id) {
53 case OSSL_FUNC_SIGNATURE_NEWCTX:
54 if (signature->newctx != NULL)
56 signature->newctx = OSSL_get_OP_signature_newctx(fns);
59 case OSSL_FUNC_SIGNATURE_SIGN_INIT:
60 if (signature->sign_init != NULL)
62 signature->sign_init = OSSL_get_OP_signature_sign_init(fns);
65 case OSSL_FUNC_SIGNATURE_SIGN:
66 if (signature->sign != NULL)
68 signature->sign = OSSL_get_OP_signature_sign(fns);
71 case OSSL_FUNC_SIGNATURE_VERIFY_INIT:
72 if (signature->verify_init != NULL)
74 signature->verify_init = OSSL_get_OP_signature_verify_init(fns);
77 case OSSL_FUNC_SIGNATURE_VERIFY:
78 if (signature->verify != NULL)
80 signature->verify = OSSL_get_OP_signature_verify(fns);
83 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT:
84 if (signature->verify_recover_init != NULL)
86 signature->verify_recover_init
87 = OSSL_get_OP_signature_verify_recover_init(fns);
90 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER:
91 if (signature->verify_recover != NULL)
93 signature->verify_recover
94 = OSSL_get_OP_signature_verify_recover(fns);
97 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT:
98 if (signature->digest_sign_init != NULL)
100 signature->digest_sign_init
101 = OSSL_get_OP_signature_digest_sign_init(fns);
104 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE:
105 if (signature->digest_sign_update != NULL)
107 signature->digest_sign_update
108 = OSSL_get_OP_signature_digest_sign_update(fns);
111 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL:
112 if (signature->digest_sign_final != NULL)
114 signature->digest_sign_final
115 = OSSL_get_OP_signature_digest_sign_final(fns);
118 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT:
119 if (signature->digest_verify_init != NULL)
121 signature->digest_verify_init
122 = OSSL_get_OP_signature_digest_verify_init(fns);
125 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE:
126 if (signature->digest_verify_update != NULL)
128 signature->digest_verify_update
129 = OSSL_get_OP_signature_digest_verify_update(fns);
132 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL:
133 if (signature->digest_verify_final != NULL)
135 signature->digest_verify_final
136 = OSSL_get_OP_signature_digest_verify_final(fns);
139 case OSSL_FUNC_SIGNATURE_FREECTX:
140 if (signature->freectx != NULL)
142 signature->freectx = OSSL_get_OP_signature_freectx(fns);
145 case OSSL_FUNC_SIGNATURE_DUPCTX:
146 if (signature->dupctx != NULL)
148 signature->dupctx = OSSL_get_OP_signature_dupctx(fns);
150 case OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS:
151 if (signature->get_ctx_params != NULL)
153 signature->get_ctx_params
154 = OSSL_get_OP_signature_get_ctx_params(fns);
157 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS:
158 if (signature->gettable_ctx_params != NULL)
160 signature->gettable_ctx_params
161 = OSSL_get_OP_signature_gettable_ctx_params(fns);
164 case OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS:
165 if (signature->set_ctx_params != NULL)
167 signature->set_ctx_params
168 = OSSL_get_OP_signature_set_ctx_params(fns);
171 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS:
172 if (signature->settable_ctx_params != NULL)
174 signature->settable_ctx_params
175 = OSSL_get_OP_signature_settable_ctx_params(fns);
178 case OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS:
179 if (signature->get_ctx_md_params != NULL)
181 signature->get_ctx_md_params
182 = OSSL_get_OP_signature_get_ctx_md_params(fns);
185 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS:
186 if (signature->gettable_ctx_md_params != NULL)
188 signature->gettable_ctx_md_params
189 = OSSL_get_OP_signature_gettable_ctx_md_params(fns);
192 case OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS:
193 if (signature->set_ctx_md_params != NULL)
195 signature->set_ctx_md_params
196 = OSSL_get_OP_signature_set_ctx_md_params(fns);
199 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS:
200 if (signature->settable_ctx_md_params != NULL)
202 signature->settable_ctx_md_params
203 = OSSL_get_OP_signature_settable_ctx_md_params(fns);
211 && verifyrecfncnt == 0
213 && digverifyfncnt == 0)
214 || (signfncnt != 0 && signfncnt != 2)
215 || (verifyfncnt != 0 && verifyfncnt != 2)
216 || (verifyrecfncnt != 0 && verifyrecfncnt != 2)
217 || (digsignfncnt != 0 && digsignfncnt != 3)
218 || (digverifyfncnt != 0 && digverifyfncnt != 3)
219 || (gparamfncnt != 0 && gparamfncnt != 2)
220 || (sparamfncnt != 0 && sparamfncnt != 2)
221 || (gmdparamfncnt != 0 && gmdparamfncnt != 2)
222 || (smdparamfncnt != 0 && smdparamfncnt != 2)) {
224 * In order to be a consistent set of functions we must have at least
225 * a set of context functions (newctx and freectx) as well as a set of
226 * "signature" functions:
227 * (sign_init, sign) or
228 * (verify_init verify) or
229 * (verify_recover_init, verify_recover) or
230 * (digest_sign_init, digest_sign_update, digest_sign_final) or
231 * (digest_verify_init, digest_verify_update, digest_verify_final).
233 * set_ctx_params and settable_ctx_params are optional, but if one of
234 * them is present then the other one must also be present. The same
235 * applies to get_ctx_params and gettable_ctx_params. The same rules
236 * apply to the "md_params" functions. The dupctx function is optional.
238 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
244 EVP_SIGNATURE_free(signature);
248 void EVP_SIGNATURE_free(EVP_SIGNATURE *signature)
250 if (signature != NULL) {
253 CRYPTO_DOWN_REF(&signature->refcnt, &i, signature->lock);
256 ossl_provider_free(signature->prov);
257 CRYPTO_THREAD_lock_free(signature->lock);
258 OPENSSL_free(signature);
262 int EVP_SIGNATURE_up_ref(EVP_SIGNATURE *signature)
266 CRYPTO_UP_REF(&signature->refcnt, &ref, signature->lock);
270 OSSL_PROVIDER *EVP_SIGNATURE_provider(const EVP_SIGNATURE *signature)
272 return signature->prov;
275 EVP_SIGNATURE *EVP_SIGNATURE_fetch(OPENSSL_CTX *ctx, const char *algorithm,
276 const char *properties)
278 return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties,
279 evp_signature_from_dispatch,
280 (int (*)(void *))EVP_SIGNATURE_up_ref,
281 (void (*)(void *))EVP_SIGNATURE_free);
284 int EVP_SIGNATURE_is_a(const EVP_SIGNATURE *signature, const char *name)
286 return evp_is_a(signature->prov, signature->name_id, name);
289 int EVP_SIGNATURE_number(const EVP_SIGNATURE *signature)
291 return signature->name_id;
294 void EVP_SIGNATURE_do_all_provided(OPENSSL_CTX *libctx,
295 void (*fn)(EVP_SIGNATURE *signature,
299 evp_generic_do_all(libctx, OSSL_OP_SIGNATURE,
300 (void (*)(void *, void *))fn, arg,
301 evp_signature_from_dispatch,
302 (void (*)(void *))EVP_SIGNATURE_free);
306 void EVP_SIGNATURE_names_do_all(const EVP_SIGNATURE *signature,
307 void (*fn)(const char *name, void *data),
310 if (signature->prov != NULL)
311 evp_names_do_all(signature->prov, signature->name_id, fn, data);
314 static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, int operation)
317 void *provkey = NULL;
318 EVP_SIGNATURE *signature = NULL;
321 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
325 evp_pkey_ctx_free_old_ops(ctx);
326 ctx->operation = operation;
328 if (ctx->algorithm == NULL)
332 * Because we cleared out old ops, we shouldn't need to worry about
333 * checking if signature is already there. Keymgmt is a different
334 * matter, as it isn't tied to a specific EVP_PKEY op.
336 signature = EVP_SIGNATURE_fetch(ctx->libctx, ctx->algorithm,
338 if (signature != NULL && ctx->keymgmt == NULL) {
339 int name_id = EVP_SIGNATURE_number(signature);
341 ctx->keymgmt = evp_keymgmt_fetch_by_number(ctx->libctx, name_id,
345 if (ctx->keymgmt == NULL
347 || (EVP_KEYMGMT_provider(ctx->keymgmt)
348 != EVP_SIGNATURE_provider(signature))) {
350 * We don't have the full support we need with provided methods,
351 * let's go see if legacy does. Also, we don't need to free
352 * ctx->keymgmt here, as it's not necessarily tied to this
353 * operation. It will be freed by EVP_PKEY_CTX_free().
355 EVP_SIGNATURE_free(signature);
359 ctx->op.sig.signature = signature;
361 if (ctx->pkey != NULL) {
363 evp_keymgmt_export_to_provider(ctx->pkey, ctx->keymgmt, 0);
364 if (provkey == NULL) {
365 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
369 ctx->op.sig.sigprovctx = signature->newctx(ossl_provider_ctx(signature->prov));
370 if (ctx->op.sig.sigprovctx == NULL) {
371 /* The provider key can stay in the cache */
372 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
377 case EVP_PKEY_OP_SIGN:
378 if (signature->sign_init == NULL) {
379 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
383 ret = signature->sign_init(ctx->op.sig.sigprovctx, provkey);
385 case EVP_PKEY_OP_VERIFY:
386 if (signature->verify_init == NULL) {
387 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
391 ret = signature->verify_init(ctx->op.sig.sigprovctx, provkey);
393 case EVP_PKEY_OP_VERIFYRECOVER:
394 if (signature->verify_recover_init == NULL) {
395 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
399 ret = signature->verify_recover_init(ctx->op.sig.sigprovctx, provkey);
402 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
407 signature->freectx(ctx->op.sig.sigprovctx);
408 ctx->op.sig.sigprovctx = NULL;
414 if (ctx->pmeth == NULL
415 || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
416 || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
417 || (operation == EVP_PKEY_OP_VERIFYRECOVER
418 && ctx->pmeth->verify_recover == NULL)) {
419 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
424 case EVP_PKEY_OP_SIGN:
425 if (ctx->pmeth->sign_init == NULL)
427 ret = ctx->pmeth->sign_init(ctx);
429 case EVP_PKEY_OP_VERIFY:
430 if (ctx->pmeth->verify_init == NULL)
432 ret = ctx->pmeth->verify_init(ctx);
434 case EVP_PKEY_OP_VERIFYRECOVER:
435 if (ctx->pmeth->verify_recover_init == NULL)
437 ret = ctx->pmeth->verify_recover_init(ctx);
440 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
448 ctx->operation = EVP_PKEY_OP_UNDEFINED;
452 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
454 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_SIGN);
457 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
458 unsigned char *sig, size_t *siglen,
459 const unsigned char *tbs, size_t tbslen)
464 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
468 if (ctx->operation != EVP_PKEY_OP_SIGN) {
469 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
473 if (ctx->op.sig.sigprovctx == NULL)
476 ret = ctx->op.sig.signature->sign(ctx->op.sig.sigprovctx, sig, siglen,
477 SIZE_MAX, tbs, tbslen);
482 if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
483 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
487 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
488 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
491 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
493 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFY);
496 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
497 const unsigned char *sig, size_t siglen,
498 const unsigned char *tbs, size_t tbslen)
503 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
507 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
508 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
512 if (ctx->op.sig.sigprovctx == NULL)
515 ret = ctx->op.sig.signature->verify(ctx->op.sig.sigprovctx, sig, siglen,
520 if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
521 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
525 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
528 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
530 return evp_pkey_signature_init(ctx, EVP_PKEY_OP_VERIFYRECOVER);
533 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
534 unsigned char *rout, size_t *routlen,
535 const unsigned char *sig, size_t siglen)
540 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
544 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
545 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
549 if (ctx->op.sig.sigprovctx == NULL)
552 ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.sigprovctx, rout,
554 (rout == NULL ? 0 : *routlen),
558 if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
559 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
562 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
563 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
566 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
569 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
570 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
571 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
574 ctx->operation = EVP_PKEY_OP_ENCRYPT;
575 if (!ctx->pmeth->encrypt_init)
577 ret = ctx->pmeth->encrypt_init(ctx);
579 ctx->operation = EVP_PKEY_OP_UNDEFINED;
583 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
584 unsigned char *out, size_t *outlen,
585 const unsigned char *in, size_t inlen)
587 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
588 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
589 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
592 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
593 EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
596 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
597 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
600 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
603 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
604 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
605 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
608 ctx->operation = EVP_PKEY_OP_DECRYPT;
609 if (!ctx->pmeth->decrypt_init)
611 ret = ctx->pmeth->decrypt_init(ctx);
613 ctx->operation = EVP_PKEY_OP_UNDEFINED;
617 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
618 unsigned char *out, size_t *outlen,
619 const unsigned char *in, size_t inlen)
621 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
622 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
623 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
626 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
627 EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
630 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
631 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);