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 "internal/evp_int.h"
16 #include "internal/provider.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(const char *name,
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 = EVP_KEYMGMT_fetch(keymgmt_data->ctx, name,
51 keymgmt_data->properties);
52 EVP_SIGNATURE *signature = NULL;
53 int ctxfncnt = 0, signfncnt = 0, verifyfncnt = 0, verifyrecfncnt = 0;
55 if (keymgmt == NULL || EVP_KEYMGMT_provider(keymgmt) != prov) {
56 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEYMGMT_AVAILABLE);
60 if ((signature = evp_signature_new(prov)) == NULL
61 || (signature->name = OPENSSL_strdup(name)) == NULL) {
62 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
66 signature->keymgmt = keymgmt;
67 keymgmt = NULL; /* avoid double free on failure below */
69 for (; fns->function_id != 0; fns++) {
70 switch (fns->function_id) {
71 case OSSL_FUNC_SIGNATURE_NEWCTX:
72 if (signature->newctx != NULL)
74 signature->newctx = OSSL_get_OP_signature_newctx(fns);
77 case OSSL_FUNC_SIGNATURE_SIGN_INIT:
78 if (signature->sign_init != NULL)
80 signature->sign_init = OSSL_get_OP_signature_sign_init(fns);
83 case OSSL_FUNC_SIGNATURE_SIGN:
84 if (signature->sign != NULL)
86 signature->sign = OSSL_get_OP_signature_sign(fns);
89 case OSSL_FUNC_SIGNATURE_VERIFY_INIT:
90 if (signature->verify_init != NULL)
92 signature->verify_init = OSSL_get_OP_signature_verify_init(fns);
95 case OSSL_FUNC_SIGNATURE_VERIFY:
96 if (signature->verify != NULL)
98 signature->verify = OSSL_get_OP_signature_verify(fns);
101 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT:
102 if (signature->verify_recover_init != NULL)
104 signature->verify_recover_init
105 = OSSL_get_OP_signature_verify_recover_init(fns);
108 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER:
109 if (signature->verify_recover != NULL)
111 signature->verify_recover
112 = OSSL_get_OP_signature_verify_recover(fns);
115 case OSSL_FUNC_SIGNATURE_FREECTX:
116 if (signature->freectx != NULL)
118 signature->freectx = OSSL_get_OP_signature_freectx(fns);
121 case OSSL_FUNC_SIGNATURE_DUPCTX:
122 if (signature->dupctx != NULL)
124 signature->dupctx = OSSL_get_OP_signature_dupctx(fns);
126 case OSSL_FUNC_SIGNATURE_SET_PARAMS:
127 if (signature->set_params != NULL)
129 signature->set_params = OSSL_get_OP_signature_set_params(fns);
134 || (signfncnt != 2 && verifyfncnt != 2 && verifyrecfncnt != 2)) {
136 * In order to be a consistent set of functions we must have at least
137 * a set of context functions (newctx and freectx) as well as a pair of
138 * "signature" functions: (sign_init, sign) or (verify_init verify) or
139 * (verify_recover_init, verify_recover). The dupctx and set_params
140 * functions are optional.
142 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
148 EVP_SIGNATURE_free(signature);
149 EVP_KEYMGMT_free(keymgmt);
153 void EVP_SIGNATURE_free(EVP_SIGNATURE *signature)
155 if (signature != NULL) {
158 CRYPTO_DOWN_REF(&signature->refcnt, &i, signature->lock);
161 EVP_KEYMGMT_free(signature->keymgmt);
162 ossl_provider_free(signature->prov);
163 OPENSSL_free(signature->name);
164 CRYPTO_THREAD_lock_free(signature->lock);
165 OPENSSL_free(signature);
169 int EVP_SIGNATURE_up_ref(EVP_SIGNATURE *signature)
173 CRYPTO_UP_REF(&signature->refcnt, &ref, signature->lock);
177 OSSL_PROVIDER *EVP_SIGNATURE_provider(const EVP_SIGNATURE *signature)
179 return signature->prov;
182 EVP_SIGNATURE *EVP_SIGNATURE_fetch(OPENSSL_CTX *ctx, const char *algorithm,
183 const char *properties)
185 struct keymgmt_data_st keymgmt_data;
188 * A signature operation cannot work without a key, so we need key
189 * management from the same provider to manage its keys.
191 keymgmt_data.ctx = ctx;
192 keymgmt_data.properties = properties;
193 return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties,
194 evp_signature_from_dispatch, &keymgmt_data,
195 (int (*)(void *))EVP_SIGNATURE_up_ref,
196 (void (*)(void *))EVP_SIGNATURE_free);
199 static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature,
203 void *provkey = NULL;
206 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
210 ctx->operation = operation;
212 if (ctx->engine != NULL)
215 if (signature != NULL) {
216 if (!EVP_SIGNATURE_up_ref(signature))
219 int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
222 * TODO(3.0): Check for legacy handling. Remove this once all all
223 * algorithms are moved to providers.
225 if (ctx->pkey != NULL) {
226 switch (ctx->pkey->type) {
232 signature = EVP_SIGNATURE_fetch(NULL, OBJ_nid2sn(nid), NULL);
237 if (signature == NULL) {
238 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
243 if (ctx->sigprovctx != NULL && ctx->signature != NULL)
244 ctx->signature->freectx(ctx->sigprovctx);
245 EVP_SIGNATURE_free(ctx->signature);
246 ctx->signature = signature;
247 if (ctx->pkey != NULL) {
248 provkey = evp_keymgmt_export_to_provider(ctx->pkey, signature->keymgmt);
249 if (provkey == NULL) {
250 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
254 ctx->sigprovctx = signature->newctx(ossl_provider_ctx(signature->prov));
255 if (ctx->sigprovctx == NULL) {
256 /* The provider key can stay in the cache */
257 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
262 case EVP_PKEY_OP_SIGN:
263 if (signature->sign_init == NULL) {
264 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
268 ret = signature->sign_init(ctx->sigprovctx, provkey);
270 case EVP_PKEY_OP_VERIFY:
271 if (signature->verify_init == NULL) {
272 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
276 ret = signature->verify_init(ctx->sigprovctx, provkey);
278 case EVP_PKEY_OP_VERIFYRECOVER:
279 if (signature->verify_recover_init == NULL) {
280 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
284 ret = signature->verify_recover_init(ctx->sigprovctx, provkey);
287 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
292 signature->freectx(ctx->sigprovctx);
293 ctx->sigprovctx = NULL;
299 if (ctx->pmeth == NULL
300 || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
301 || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
302 || (operation == EVP_PKEY_OP_VERIFYRECOVER
303 && ctx->pmeth->verify_recover == NULL)) {
304 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
309 case EVP_PKEY_OP_SIGN:
310 if (ctx->pmeth->sign_init == NULL)
312 ret = ctx->pmeth->sign_init(ctx);
314 case EVP_PKEY_OP_VERIFY:
315 if (ctx->pmeth->verify_init == NULL)
317 ret = ctx->pmeth->verify_init(ctx);
319 case EVP_PKEY_OP_VERIFYRECOVER:
320 if (ctx->pmeth->verify_recover_init == NULL)
322 ret = ctx->pmeth->verify_recover_init(ctx);
325 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
333 ctx->operation = EVP_PKEY_OP_UNDEFINED;
337 int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
339 return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_SIGN);
342 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
344 return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_SIGN);
347 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
348 unsigned char *sig, size_t *siglen,
349 const unsigned char *tbs, size_t tbslen)
354 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
358 if (ctx->operation != EVP_PKEY_OP_SIGN) {
359 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
363 if (ctx->sigprovctx == NULL)
366 ret = ctx->signature->sign(ctx->sigprovctx, sig, siglen, SIZE_MAX,
372 if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
373 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
377 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
378 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
381 int EVP_PKEY_verify_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
383 return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_VERIFY);
386 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
388 return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFY);
391 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
392 const unsigned char *sig, size_t siglen,
393 const unsigned char *tbs, size_t tbslen)
398 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
402 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
403 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
407 if (ctx->sigprovctx == NULL)
410 ret = ctx->signature->verify(ctx->sigprovctx, sig, siglen, tbs, tbslen);
414 if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
415 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
419 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
422 int EVP_PKEY_verify_recover_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
424 return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_VERIFYRECOVER);
427 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
429 return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFYRECOVER);
432 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
433 unsigned char *rout, size_t *routlen,
434 const unsigned char *sig, size_t siglen)
439 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
443 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
444 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
448 if (ctx->sigprovctx == NULL)
451 ret = ctx->signature->verify_recover(ctx->sigprovctx, rout, routlen,
452 (rout == NULL ? 0 : *routlen),
456 if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
457 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
460 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
461 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
464 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
467 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
468 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
469 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
472 ctx->operation = EVP_PKEY_OP_ENCRYPT;
473 if (!ctx->pmeth->encrypt_init)
475 ret = ctx->pmeth->encrypt_init(ctx);
477 ctx->operation = EVP_PKEY_OP_UNDEFINED;
481 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
482 unsigned char *out, size_t *outlen,
483 const unsigned char *in, size_t inlen)
485 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
486 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
487 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
490 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
491 EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
494 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
495 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
498 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
501 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
502 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
503 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
506 ctx->operation = EVP_PKEY_OP_DECRYPT;
507 if (!ctx->pmeth->decrypt_init)
509 ret = ctx->pmeth->decrypt_init(ctx);
511 ctx->operation = EVP_PKEY_OP_UNDEFINED;
515 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
516 unsigned char *out, size_t *outlen,
517 const unsigned char *in, size_t inlen)
519 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
520 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
521 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
524 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
525 EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
528 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
529 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);