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;
54 int gparamfncnt = 0, sparamfncnt = 0;
56 if (keymgmt == NULL || EVP_KEYMGMT_provider(keymgmt) != prov) {
57 ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEYMGMT_AVAILABLE);
61 if ((signature = evp_signature_new(prov)) == NULL
62 || (signature->name = OPENSSL_strdup(name)) == NULL) {
63 ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
67 signature->keymgmt = keymgmt;
68 keymgmt = NULL; /* avoid double free on failure below */
70 for (; fns->function_id != 0; fns++) {
71 switch (fns->function_id) {
72 case OSSL_FUNC_SIGNATURE_NEWCTX:
73 if (signature->newctx != NULL)
75 signature->newctx = OSSL_get_OP_signature_newctx(fns);
78 case OSSL_FUNC_SIGNATURE_SIGN_INIT:
79 if (signature->sign_init != NULL)
81 signature->sign_init = OSSL_get_OP_signature_sign_init(fns);
84 case OSSL_FUNC_SIGNATURE_SIGN:
85 if (signature->sign != NULL)
87 signature->sign = OSSL_get_OP_signature_sign(fns);
90 case OSSL_FUNC_SIGNATURE_VERIFY_INIT:
91 if (signature->verify_init != NULL)
93 signature->verify_init = OSSL_get_OP_signature_verify_init(fns);
96 case OSSL_FUNC_SIGNATURE_VERIFY:
97 if (signature->verify != NULL)
99 signature->verify = OSSL_get_OP_signature_verify(fns);
102 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER_INIT:
103 if (signature->verify_recover_init != NULL)
105 signature->verify_recover_init
106 = OSSL_get_OP_signature_verify_recover_init(fns);
109 case OSSL_FUNC_SIGNATURE_VERIFY_RECOVER:
110 if (signature->verify_recover != NULL)
112 signature->verify_recover
113 = OSSL_get_OP_signature_verify_recover(fns);
116 case OSSL_FUNC_SIGNATURE_FREECTX:
117 if (signature->freectx != NULL)
119 signature->freectx = OSSL_get_OP_signature_freectx(fns);
122 case OSSL_FUNC_SIGNATURE_DUPCTX:
123 if (signature->dupctx != NULL)
125 signature->dupctx = OSSL_get_OP_signature_dupctx(fns);
127 case OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS:
128 if (signature->get_ctx_params != NULL)
130 signature->get_ctx_params
131 = OSSL_get_OP_signature_get_ctx_params(fns);
134 case OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS:
135 if (signature->gettable_ctx_params != NULL)
137 signature->gettable_ctx_params
138 = OSSL_get_OP_signature_gettable_ctx_params(fns);
141 case OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS:
142 if (signature->set_ctx_params != NULL)
144 signature->set_ctx_params
145 = OSSL_get_OP_signature_set_ctx_params(fns);
148 case OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS:
149 if (signature->settable_ctx_params != NULL)
151 signature->settable_ctx_params
152 = OSSL_get_OP_signature_settable_ctx_params(fns);
158 || (signfncnt != 2 && verifyfncnt != 2 && verifyrecfncnt != 2)
159 || (gparamfncnt != 0 && gparamfncnt != 2)
160 || (sparamfncnt != 0 && sparamfncnt != 2)) {
162 * In order to be a consistent set of functions we must have at least
163 * a set of context functions (newctx and freectx) as well as a pair of
164 * "signature" functions: (sign_init, sign) or (verify_init verify) or
165 * (verify_recover_init, verify_recover). set_ctx_params and
166 * settable_ctx_params are optional, but if one of them is present then
167 * the other one must also be present. The same applies to
168 * get_ctx_params and gettable_ctx_params. The dupctx function is
171 ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_PROVIDER_FUNCTIONS);
177 EVP_SIGNATURE_free(signature);
178 EVP_KEYMGMT_free(keymgmt);
182 void EVP_SIGNATURE_free(EVP_SIGNATURE *signature)
184 if (signature != NULL) {
187 CRYPTO_DOWN_REF(&signature->refcnt, &i, signature->lock);
190 EVP_KEYMGMT_free(signature->keymgmt);
191 ossl_provider_free(signature->prov);
192 OPENSSL_free(signature->name);
193 CRYPTO_THREAD_lock_free(signature->lock);
194 OPENSSL_free(signature);
198 int EVP_SIGNATURE_up_ref(EVP_SIGNATURE *signature)
202 CRYPTO_UP_REF(&signature->refcnt, &ref, signature->lock);
206 OSSL_PROVIDER *EVP_SIGNATURE_provider(const EVP_SIGNATURE *signature)
208 return signature->prov;
211 EVP_SIGNATURE *EVP_SIGNATURE_fetch(OPENSSL_CTX *ctx, const char *algorithm,
212 const char *properties)
214 struct keymgmt_data_st keymgmt_data;
217 * A signature operation cannot work without a key, so we need key
218 * management from the same provider to manage its keys.
220 keymgmt_data.ctx = ctx;
221 keymgmt_data.properties = properties;
222 return evp_generic_fetch(ctx, OSSL_OP_SIGNATURE, algorithm, properties,
223 evp_signature_from_dispatch, &keymgmt_data,
224 (int (*)(void *))EVP_SIGNATURE_up_ref,
225 (void (*)(void *))EVP_SIGNATURE_free);
228 static int evp_pkey_signature_init(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature,
232 void *provkey = NULL;
235 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
239 ctx->operation = operation;
241 if (ctx->engine != NULL)
244 if (signature != NULL) {
245 if (!EVP_SIGNATURE_up_ref(signature))
248 int nid = ctx->pkey != NULL ? ctx->pkey->type : ctx->pmeth->pkey_id;
251 * TODO(3.0): Check for legacy handling. Remove this once all all
252 * algorithms are moved to providers.
254 if (ctx->pkey != NULL) {
255 switch (ctx->pkey->type) {
261 signature = EVP_SIGNATURE_fetch(NULL, OBJ_nid2sn(nid), NULL);
266 if (signature == NULL) {
267 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
272 if (ctx->sigprovctx != NULL && ctx->signature != NULL)
273 ctx->signature->freectx(ctx->sigprovctx);
274 EVP_SIGNATURE_free(ctx->signature);
275 ctx->signature = signature;
276 if (ctx->pkey != NULL) {
277 provkey = evp_keymgmt_export_to_provider(ctx->pkey, signature->keymgmt);
278 if (provkey == NULL) {
279 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
283 ctx->sigprovctx = signature->newctx(ossl_provider_ctx(signature->prov));
284 if (ctx->sigprovctx == NULL) {
285 /* The provider key can stay in the cache */
286 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
291 case EVP_PKEY_OP_SIGN:
292 if (signature->sign_init == NULL) {
293 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
297 ret = signature->sign_init(ctx->sigprovctx, provkey);
299 case EVP_PKEY_OP_VERIFY:
300 if (signature->verify_init == NULL) {
301 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
305 ret = signature->verify_init(ctx->sigprovctx, provkey);
307 case EVP_PKEY_OP_VERIFYRECOVER:
308 if (signature->verify_recover_init == NULL) {
309 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
313 ret = signature->verify_recover_init(ctx->sigprovctx, provkey);
316 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
321 signature->freectx(ctx->sigprovctx);
322 ctx->sigprovctx = NULL;
328 if (ctx->pmeth == NULL
329 || (operation == EVP_PKEY_OP_SIGN && ctx->pmeth->sign == NULL)
330 || (operation == EVP_PKEY_OP_VERIFY && ctx->pmeth->verify == NULL)
331 || (operation == EVP_PKEY_OP_VERIFYRECOVER
332 && ctx->pmeth->verify_recover == NULL)) {
333 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
338 case EVP_PKEY_OP_SIGN:
339 if (ctx->pmeth->sign_init == NULL)
341 ret = ctx->pmeth->sign_init(ctx);
343 case EVP_PKEY_OP_VERIFY:
344 if (ctx->pmeth->verify_init == NULL)
346 ret = ctx->pmeth->verify_init(ctx);
348 case EVP_PKEY_OP_VERIFYRECOVER:
349 if (ctx->pmeth->verify_recover_init == NULL)
351 ret = ctx->pmeth->verify_recover_init(ctx);
354 EVPerr(0, EVP_R_INITIALIZATION_ERROR);
362 ctx->operation = EVP_PKEY_OP_UNDEFINED;
366 int EVP_PKEY_sign_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
368 return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_SIGN);
371 int EVP_PKEY_sign_init(EVP_PKEY_CTX *ctx)
373 return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_SIGN);
376 int EVP_PKEY_sign(EVP_PKEY_CTX *ctx,
377 unsigned char *sig, size_t *siglen,
378 const unsigned char *tbs, size_t tbslen)
383 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
387 if (ctx->operation != EVP_PKEY_OP_SIGN) {
388 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
392 if (ctx->sigprovctx == NULL)
395 ret = ctx->signature->sign(ctx->sigprovctx, sig, siglen, SIZE_MAX,
401 if (ctx->pmeth == NULL || ctx->pmeth->sign == NULL) {
402 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
406 M_check_autoarg(ctx, sig, siglen, EVP_F_EVP_PKEY_SIGN)
407 return ctx->pmeth->sign(ctx, sig, siglen, tbs, tbslen);
410 int EVP_PKEY_verify_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
412 return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_VERIFY);
415 int EVP_PKEY_verify_init(EVP_PKEY_CTX *ctx)
417 return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFY);
420 int EVP_PKEY_verify(EVP_PKEY_CTX *ctx,
421 const unsigned char *sig, size_t siglen,
422 const unsigned char *tbs, size_t tbslen)
427 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
431 if (ctx->operation != EVP_PKEY_OP_VERIFY) {
432 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
436 if (ctx->sigprovctx == NULL)
439 ret = ctx->signature->verify(ctx->sigprovctx, sig, siglen, tbs, tbslen);
443 if (ctx->pmeth == NULL || ctx->pmeth->verify == NULL) {
444 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
448 return ctx->pmeth->verify(ctx, sig, siglen, tbs, tbslen);
451 int EVP_PKEY_verify_recover_init_ex(EVP_PKEY_CTX *ctx, EVP_SIGNATURE *signature)
453 return evp_pkey_signature_init(ctx, signature, EVP_PKEY_OP_VERIFYRECOVER);
456 int EVP_PKEY_verify_recover_init(EVP_PKEY_CTX *ctx)
458 return evp_pkey_signature_init(ctx, NULL, EVP_PKEY_OP_VERIFYRECOVER);
461 int EVP_PKEY_verify_recover(EVP_PKEY_CTX *ctx,
462 unsigned char *rout, size_t *routlen,
463 const unsigned char *sig, size_t siglen)
468 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
472 if (ctx->operation != EVP_PKEY_OP_VERIFYRECOVER) {
473 EVPerr(0, EVP_R_OPERATON_NOT_INITIALIZED);
477 if (ctx->sigprovctx == NULL)
480 ret = ctx->signature->verify_recover(ctx->sigprovctx, rout, routlen,
481 (rout == NULL ? 0 : *routlen),
485 if (ctx->pmeth == NULL || ctx->pmeth->verify_recover == NULL) {
486 EVPerr(0, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
489 M_check_autoarg(ctx, rout, routlen, EVP_F_EVP_PKEY_VERIFY_RECOVER)
490 return ctx->pmeth->verify_recover(ctx, rout, routlen, sig, siglen);
493 int EVP_PKEY_encrypt_init(EVP_PKEY_CTX *ctx)
496 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
497 EVPerr(EVP_F_EVP_PKEY_ENCRYPT_INIT,
498 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
501 ctx->operation = EVP_PKEY_OP_ENCRYPT;
502 if (!ctx->pmeth->encrypt_init)
504 ret = ctx->pmeth->encrypt_init(ctx);
506 ctx->operation = EVP_PKEY_OP_UNDEFINED;
510 int EVP_PKEY_encrypt(EVP_PKEY_CTX *ctx,
511 unsigned char *out, size_t *outlen,
512 const unsigned char *in, size_t inlen)
514 if (!ctx || !ctx->pmeth || !ctx->pmeth->encrypt) {
515 EVPerr(EVP_F_EVP_PKEY_ENCRYPT,
516 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
519 if (ctx->operation != EVP_PKEY_OP_ENCRYPT) {
520 EVPerr(EVP_F_EVP_PKEY_ENCRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
523 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_ENCRYPT)
524 return ctx->pmeth->encrypt(ctx, out, outlen, in, inlen);
527 int EVP_PKEY_decrypt_init(EVP_PKEY_CTX *ctx)
530 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
531 EVPerr(EVP_F_EVP_PKEY_DECRYPT_INIT,
532 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
535 ctx->operation = EVP_PKEY_OP_DECRYPT;
536 if (!ctx->pmeth->decrypt_init)
538 ret = ctx->pmeth->decrypt_init(ctx);
540 ctx->operation = EVP_PKEY_OP_UNDEFINED;
544 int EVP_PKEY_decrypt(EVP_PKEY_CTX *ctx,
545 unsigned char *out, size_t *outlen,
546 const unsigned char *in, size_t inlen)
548 if (!ctx || !ctx->pmeth || !ctx->pmeth->decrypt) {
549 EVPerr(EVP_F_EVP_PKEY_DECRYPT,
550 EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
553 if (ctx->operation != EVP_PKEY_OP_DECRYPT) {
554 EVPerr(EVP_F_EVP_PKEY_DECRYPT, EVP_R_OPERATON_NOT_INITIALIZED);
557 M_check_autoarg(ctx, out, outlen, EVP_F_EVP_PKEY_DECRYPT)
558 return ctx->pmeth->decrypt(ctx, out, outlen, in, inlen);