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,
41 * Signature functions cannot work without a key, and key management
42 * from the same provider to manage its keys. We therefore fetch
43 * a key management method using the same algorithm and properties
44 * and pass that down to evp_generic_fetch to be passed on to our
45 * evp_signature_from_dispatch, which will attach the key management
46 * method to the newly created key exchange method as long as the
49 struct keymgmt_data_st *keymgmt_data = vkeymgmt_data;
50 EVP_KEYMGMT *keymgmt =
51 evp_keymgmt_fetch_by_number(keymgmt_data->ctx, name_id,
52 keymgmt_data->properties);
53 EVP_SIGNATURE *signature = NULL;
54 int ctxfncnt = 0, signfncnt = 0, verifyfncnt = 0, verifyrecfncnt = 0;
55 int digsignfncnt = 0, digverifyfncnt = 0;
56 int gparamfncnt = 0, sparamfncnt = 0, gmdparamfncnt = 0, smdparamfncnt = 0;
58 if (keymgmt == NULL || EVP_KEYMGMT_provider(keymgmt) != prov) {
59 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEYMGMT_AVAILABLE);
63 if ((signature = evp_signature_new(prov)) == NULL) {
64 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
68 signature->name_id = name_id;
69 signature->keymgmt = keymgmt;
70 keymgmt = NULL; /* avoid double free on failure below */
72 for (; fns->function_id != 0; fns++) {
73 switch (fns->function_id) {
74 case OSSL_FUNC_SIGNATURE_NEWCTX:
75 if (signature->newctx != NULL)
77 signature->newctx = OSSL_get_OP_signature_newctx(fns);
80 case OSSL_FUNC_SIGNATURE_SIGN_INIT:
81 if (signature->sign_init != NULL)
83 signature->sign_init = OSSL_get_OP_signature_sign_init(fns);
86 case OSSL_FUNC_SIGNATURE_SIGN:
87 if (signature->sign != NULL)
89 signature->sign = OSSL_get_OP_signature_sign(fns);
92 case OSSL_FUNC_SIGNATURE_VERIFY_INIT:
93 if (signature->verify_init != NULL)
95 signature->verify_init = OSSL_get_OP_signature_verify_init(fns);
98 case OSSL_FUNC_SIGNATURE_VERIFY:
99 if (signature->verify != NULL)
101 signature->verify = OSSL_get_OP_signature_verify(fns);
104 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT:
105 if (signature->verify_recover_init != NULL)
107 signature->verify_recover_init
108 = OSSL_get_OP_signature_verify_recover_init(fns);
111 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER:
112 if (signature->verify_recover != NULL)
114 signature->verify_recover
115 = OSSL_get_OP_signature_verify_recover(fns);
118 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT:
119 if (signature->digest_sign_init != NULL)
121 signature->digest_sign_init
122 = OSSL_get_OP_signature_digest_sign_init(fns);
125 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE:
126 if (signature->digest_sign_update != NULL)
128 signature->digest_sign_update
129 = OSSL_get_OP_signature_digest_sign_update(fns);
132 case OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL:
133 if (signature->digest_sign_final != NULL)
135 signature->digest_sign_final
136 = OSSL_get_OP_signature_digest_sign_final(fns);
139 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT:
140 if (signature->digest_verify_init != NULL)
142 signature->digest_verify_init
143 = OSSL_get_OP_signature_digest_verify_init(fns);
146 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE:
147 if (signature->digest_verify_update != NULL)
149 signature->digest_verify_update
150 = OSSL_get_OP_signature_digest_verify_update(fns);
153 case OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL:
154 if (signature->digest_verify_final != NULL)
156 signature->digest_verify_final
157 = OSSL_get_OP_signature_digest_verify_final(fns);
160 case OSSL_FUNC_SIGNATURE_FREECTX:
161 if (signature->freectx != NULL)
163 signature->freectx = OSSL_get_OP_signature_freectx(fns);
166 case OSSL_FUNC_SIGNATURE_DUPCTX:
167 if (signature->dupctx != NULL)
169 signature->dupctx = OSSL_get_OP_signature_dupctx(fns);
171 case OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS:
172 if (signature->get_ctx_params != NULL)
174 signature->get_ctx_params
175 = OSSL_get_OP_signature_get_ctx_params(fns);
178 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS:
179 if (signature->gettable_ctx_params != NULL)
181 signature->gettable_ctx_params
182 = OSSL_get_OP_signature_gettable_ctx_params(fns);
185 case OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS:
186 if (signature->set_ctx_params != NULL)
188 signature->set_ctx_params
189 = OSSL_get_OP_signature_set_ctx_params(fns);
192 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS:
193 if (signature->settable_ctx_params != NULL)
195 signature->settable_ctx_params
196 = OSSL_get_OP_signature_settable_ctx_params(fns);
199 case OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS:
200 if (signature->get_ctx_md_params != NULL)
202 signature->get_ctx_md_params
203 = OSSL_get_OP_signature_get_ctx_md_params(fns);
206 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS:
207 if (signature->gettable_ctx_md_params != NULL)
209 signature->gettable_ctx_md_params
210 = OSSL_get_OP_signature_gettable_ctx_md_params(fns);
213 case OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS:
214 if (signature->set_ctx_md_params != NULL)
216 signature->set_ctx_md_params
217 = OSSL_get_OP_signature_set_ctx_md_params(fns);
220 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS:
221 if (signature->settable_ctx_md_params != NULL)
223 signature->settable_ctx_md_params
224 = OSSL_get_OP_signature_settable_ctx_md_params(fns);
232 && verifyrecfncnt == 0
234 && digverifyfncnt == 0)
235 || (signfncnt != 0 && signfncnt != 2)
236 || (verifyfncnt != 0 && verifyfncnt != 2)
237 || (verifyrecfncnt != 0 && verifyrecfncnt != 2)
238 || (digsignfncnt != 0 && digsignfncnt != 3)
239 || (digverifyfncnt != 0 && digverifyfncnt != 3)
240 || (gparamfncnt != 0 && gparamfncnt != 2)
241 || (sparamfncnt != 0 && sparamfncnt != 2)
242 || (gmdparamfncnt != 0 && gmdparamfncnt != 2)
243 || (smdparamfncnt != 0 && smdparamfncnt != 2)) {
245 * In order to be a consistent set of functions we must have at least
246 * a set of context functions (newctx and freectx) as well as a set of
247 * "signature" functions:
248 * (sign_init, sign) or
249 * (verify_init verify) or
250 * (verify_recover_init, verify_recover) or
251 * (digest_sign_init, digest_sign_update, digest_sign_final) or
252 * (digest_verify_init, digest_verify_update, digest_verify_final).
254 * set_ctx_params and settable_ctx_params are optional, but if one of
255 * them is present then the other one must also be present. The same
256 * applies to get_ctx_params and gettable_ctx_params. The same rules
257 * apply to the "md_params" functions. The dupctx function is optional.
259 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
265 EVP_SIGNATURE_free(signature);
266 EVP_KEYMGMT_free(keymgmt);
270 void EVP_SIGNATURE_free(EVP_SIGNATURE *signature)
272 if (signature != NULL) {
275 CRYPTO_DOWN_REF(&signature->refcnt, &i, signature->lock);
278 EVP_KEYMGMT_free(signature->keymgmt);
279 ossl_provider_free(signature->prov);
280 CRYPTO_THREAD_lock_free(signature->lock);
281 OPENSSL_free(signature);
285 int EVP_SIGNATURE_up_ref(EVP_SIGNATURE *signature)
289 CRYPTO_UP_REF(&signature->refcnt, &ref, signature->lock);
293 OSSL_PROVIDER *EVP_SIGNATURE_provider(const EVP_SIGNATURE *signature)
295 return signature->prov;
298 EVP_SIGNATURE *EVP_SIGNATURE_fetch(OPENSSL_CTX *ctx, const char *algorithm,
299 const char *properties)
301 struct keymgmt_data_st keymgmt_data;
304 * A signature operation cannot work without a key, so we need key
305 * management from the same provider to manage its keys.
307 keymgmt_data.ctx = ctx;
308 keymgmt_data.properties = properties;
309 return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties,
310 evp_signature_from_dispatch, &keymgmt_data,
311 (int (*)(void *))EVP_SIGNATURE_up_ref,
312 (void (*)(void *))EVP_SIGNATURE_free);
315 int EVP_SIGNATURE_is_a(const EVP_SIGNATURE *signature, const char *name)
317 return evp_is_a(signature->prov, signature->name_id, name);
320 void EVP_SIGNATURE_do_all_provided(OPENSSL_CTX *libctx,
321 void (*fn)(EVP_SIGNATURE *signature,
325 struct keymgmt_data_st keymgmt_data;
327 keymgmt_data.ctx = libctx;
328 keymgmt_data.properties = NULL;
329 evp_generic_do_all(libctx, OSSL_OP_SIGNATURE,
330 (void (*)(void *, void *))fn, arg,
331 evp_signature_from_dispatch, &keymgmt_data,
332 (void (*)(void *))EVP_SIGNATURE_free);
335 static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature,
339 void *provkey = NULL;
342 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
346 evp_pkey_ctx_free_old_ops(ctx);
347 ctx->operation = operation;
349 if (ctx->engine != NULL)
352 if (signature != NULL) {
353 if (!EVP_SIGNATURE_up_ref(signature))
356 int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
359 * TODO(3.0): Check for legacy handling. Remove this once all all
360 * algorithms are moved to providers.
362 if (ctx->pkey != NULL) {
363 switch (ctx->pkey->type) {
369 signature = EVP_SIGNATURE_fetch(NULL, OBJ_nid2sn(nid), NULL);
374 if (signature == NULL) {
375 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
380 ctx->op.sig.signature = signature;
381 if (ctx->pkey != NULL) {
382 provkey = evp_keymgmt_export_to_provider(ctx->pkey, signature->keymgmt);
383 if (provkey == NULL) {
384 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
388 ctx->op.sig.sigprovctx = signature->newctx(ossl_provider_ctx(signature->prov));
389 if (ctx->op.sig.sigprovctx == NULL) {
390 /* The provider key can stay in the cache */
391 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
396 case EVP_PKEY_OP_SIGN:
397 if (signature->sign_init == NULL) {
398 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
402 ret = signature->sign_init(ctx->op.sig.sigprovctx, provkey);
404 case EVP_PKEY_OP_VERIFY:
405 if (signature->verify_init == NULL) {
406 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
410 ret = signature->verify_init(ctx->op.sig.sigprovctx, provkey);
412 case EVP_PKEY_OP_VERIFYRECOVER:
413 if (signature->verify_recover_init == NULL) {
414 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
418 ret = signature->verify_recover_init(ctx->op.sig.sigprovctx, provkey);
421 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
426 signature->freectx(ctx->op.sig.sigprovctx);
427 ctx->op.sig.sigprovctx = NULL;
433 if (ctx->pmeth == NULL
434 || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
435 || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
436 || (operation == EVP_PKEY_OP_VERIFYRECOVER
437 && ctx->pmeth->verify_recover == NULL)) {
438 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
443 case EVP_PKEY_OP_SIGN:
444 if (ctx->pmeth->sign_init == NULL)
446 ret = ctx->pmeth->sign_init(ctx);
448 case EVP_PKEY_OP_VERIFY:
449 if (ctx->pmeth->verify_init == NULL)
451 ret = ctx->pmeth->verify_init(ctx);
453 case EVP_PKEY_OP_VERIFYRECOVER:
454 if (ctx->pmeth->verify_recover_init == NULL)
456 ret = ctx->pmeth->verify_recover_init(ctx);
459 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
467 ctx->operation = EVP_PKEY_OP_UNDEFINED;
471 int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
473 return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_SIGN);
476 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
478 return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_SIGN);
481 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
482 unsigned char *sig, size_t *siglen,
483 const unsigned char *tbs, size_t tbslen)
488 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
492 if (ctx->operation != EVP_PKEY_OP_SIGN) {
493 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
497 if (ctx->op.sig.sigprovctx == NULL)
500 ret = ctx->op.sig.signature->sign(ctx->op.sig.sigprovctx, sig, siglen,
501 SIZE_MAX, tbs, tbslen);
506 if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
507 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
511 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
512 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
515 int EVP_PKEY_verify_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
517 return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_VERIFY);
520 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
522 return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFY);
525 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
526 const unsigned char *sig, size_t siglen,
527 const unsigned char *tbs, size_t tbslen)
532 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
536 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
537 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
541 if (ctx->op.sig.sigprovctx == NULL)
544 ret = ctx->op.sig.signature->verify(ctx->op.sig.sigprovctx, sig, siglen,
549 if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
550 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
554 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
557 int EVP_PKEY_verify_recover_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
559 return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_VERIFYRECOVER);
562 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
564 return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFYRECOVER);
567 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
568 unsigned char *rout, size_t *routlen,
569 const unsigned char *sig, size_t siglen)
574 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
578 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
579 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
583 if (ctx->op.sig.sigprovctx == NULL)
586 ret = ctx->op.sig.signature->verify_recover(ctx->op.sig.sigprovctx, rout,
588 (rout == NULL ? 0 : *routlen),
592 if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
593 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
596 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
597 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
600 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
603 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
604 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
605 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
608 ctx->operation = EVP_PKEY_OP_ENCRYPT;
609 if (!ctx->pmeth->encrypt_init)
611 ret = ctx->pmeth->encrypt_init(ctx);
613 ctx->operation = EVP_PKEY_OP_UNDEFINED;
617 int EVP_PKEY_encrypt(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->encrypt) {
622 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
623 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
626 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
627 EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
630 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
631 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
634 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
637 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
638 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
639 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
642 ctx->operation = EVP_PKEY_OP_DECRYPT;
643 if (!ctx->pmeth->decrypt_init)
645 ret = ctx->pmeth->decrypt_init(ctx);
647 ctx->operation = EVP_PKEY_OP_UNDEFINED;
651 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
652 unsigned char *out, size_t *outlen,
653 const unsigned char *in, size_t inlen)
655 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
656 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
657 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
660 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
661 EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
664 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
665 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);