2 * Copyright 2019 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/crypto.h>
13 #include <openssl/core_numbers.h>
14 #include <openssl/core_names.h>
15 #include <openssl/err.h>
16 #include <openssl/dsa.h>
17 #include <openssl/params.h>
18 #include <openssl/evp.h>
19 #include <openssl/err.h>
20 #include "internal/nelem.h"
21 #include "internal/sizes.h"
22 #include "prov/providercommonerr.h"
23 #include "prov/implementations.h"
24 #include "prov/providercommonerr.h"
25 #include "prov/provider_ctx.h"
26 #include "crypto/dsa.h"
28 static OSSL_OP_signature_newctx_fn dsa_newctx;
29 static OSSL_OP_signature_sign_init_fn dsa_signature_init;
30 static OSSL_OP_signature_verify_init_fn dsa_signature_init;
31 static OSSL_OP_signature_sign_fn dsa_sign;
32 static OSSL_OP_signature_verify_fn dsa_verify;
33 static OSSL_OP_signature_digest_sign_init_fn dsa_digest_signverify_init;
34 static OSSL_OP_signature_digest_sign_update_fn dsa_digest_signverify_update;
35 static OSSL_OP_signature_digest_sign_final_fn dsa_digest_sign_final;
36 static OSSL_OP_signature_digest_verify_init_fn dsa_digest_signverify_init;
37 static OSSL_OP_signature_digest_verify_update_fn dsa_digest_signverify_update;
38 static OSSL_OP_signature_digest_verify_final_fn dsa_digest_verify_final;
39 static OSSL_OP_signature_freectx_fn dsa_freectx;
40 static OSSL_OP_signature_dupctx_fn dsa_dupctx;
41 static OSSL_OP_signature_get_ctx_params_fn dsa_get_ctx_params;
42 static OSSL_OP_signature_gettable_ctx_params_fn dsa_gettable_ctx_params;
43 static OSSL_OP_signature_set_ctx_params_fn dsa_set_ctx_params;
44 static OSSL_OP_signature_settable_ctx_params_fn dsa_settable_ctx_params;
45 static OSSL_OP_signature_get_ctx_md_params_fn dsa_get_ctx_md_params;
46 static OSSL_OP_signature_gettable_ctx_md_params_fn dsa_gettable_ctx_md_params;
47 static OSSL_OP_signature_set_ctx_md_params_fn dsa_set_ctx_md_params;
48 static OSSL_OP_signature_settable_ctx_md_params_fn dsa_settable_ctx_md_params;
51 * What's passed as an actual key is defined by the KEYMGMT interface.
52 * We happen to know that our KEYMGMT simply passes DSA structures, so
53 * we use that here too.
61 * Flag to determine if the hash function can be changed (1) or not (0)
62 * Because it's dangerous to change during a DigestSign or DigestVerify
63 * operation, this flag is cleared by their Init function, and set again
64 * by their Final function.
66 unsigned int flag_allow_md : 1;
68 char mdname[OSSL_MAX_NAME_SIZE];
70 /* The Algorithm Identifier of the combined signature agorithm */
71 unsigned char aid[OSSL_MAX_ALGORITHM_ID_SIZE];
80 static size_t dsa_get_md_size(const PROV_DSA_CTX *pdsactx)
82 if (pdsactx->md != NULL)
83 return EVP_MD_size(pdsactx->md);
87 static int dsa_get_md_nid(const EVP_MD *md)
90 * Because the DSA library deals with NIDs, we need to translate.
91 * We do so using EVP_MD_is_a(), and therefore need a name to NID
94 static const OSSL_ITEM name_to_nid[] = {
95 { NID_sha1, OSSL_DIGEST_NAME_SHA1 },
96 { NID_sha224, OSSL_DIGEST_NAME_SHA2_224 },
97 { NID_sha256, OSSL_DIGEST_NAME_SHA2_256 },
98 { NID_sha384, OSSL_DIGEST_NAME_SHA2_384 },
99 { NID_sha512, OSSL_DIGEST_NAME_SHA2_512 },
100 { NID_sha3_224, OSSL_DIGEST_NAME_SHA3_224 },
101 { NID_sha3_256, OSSL_DIGEST_NAME_SHA3_256 },
102 { NID_sha3_384, OSSL_DIGEST_NAME_SHA3_384 },
103 { NID_sha3_512, OSSL_DIGEST_NAME_SHA3_512 },
106 int mdnid = NID_undef;
111 for (i = 0; i < OSSL_NELEM(name_to_nid); i++) {
112 if (EVP_MD_is_a(md, name_to_nid[i].ptr)) {
113 mdnid = (int)name_to_nid[i].id;
118 if (mdnid == NID_undef)
119 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
125 static void *dsa_newctx(void *provctx)
127 PROV_DSA_CTX *pdsactx = OPENSSL_zalloc(sizeof(PROV_DSA_CTX));
132 pdsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
133 pdsactx->flag_allow_md = 1;
137 static int dsa_setup_md(PROV_DSA_CTX *ctx,
138 const char *mdname, const char *mdprops)
140 if (mdname != NULL) {
141 EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
142 int md_nid = dsa_get_md_nid(md);
143 size_t algorithmidentifier_len = 0;
144 const unsigned char *algorithmidentifier;
146 EVP_MD_free(ctx->md);
148 ctx->mdname[0] = '\0';
150 algorithmidentifier =
151 dsa_algorithmidentifier_encoding(md_nid, &algorithmidentifier_len);
153 if (algorithmidentifier == NULL) {
159 OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
164 static int dsa_signature_init(void *vpdsactx, void *vdsa)
166 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
168 if (pdsactx == NULL || vdsa == NULL || !DSA_up_ref(vdsa))
170 DSA_free(pdsactx->dsa);
175 static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen,
176 size_t sigsize, const unsigned char *tbs, size_t tbslen)
178 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
181 size_t dsasize = DSA_size(pdsactx->dsa);
182 size_t mdsize = dsa_get_md_size(pdsactx);
189 if (sigsize < (size_t)dsasize)
192 if (mdsize != 0 && tbslen != mdsize)
195 ret = dsa_sign_int(pdsactx->libctx, 0, tbs, tbslen, sig, &sltmp,
204 static int dsa_verify(void *vpdsactx, const unsigned char *sig, size_t siglen,
205 const unsigned char *tbs, size_t tbslen)
207 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
208 size_t mdsize = dsa_get_md_size(pdsactx);
210 if (mdsize != 0 && tbslen != mdsize)
213 return DSA_verify(0, tbs, tbslen, sig, siglen, pdsactx->dsa);
216 static int dsa_digest_signverify_init(void *vpdsactx, const char *mdname,
217 const char *props, void *vdsa)
219 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
221 pdsactx->flag_allow_md = 0;
222 if (!dsa_signature_init(vpdsactx, vdsa))
225 if (!dsa_setup_md(pdsactx, mdname, props))
228 pdsactx->mdctx = EVP_MD_CTX_new();
229 if (pdsactx->mdctx == NULL)
232 if (!EVP_DigestInit_ex(pdsactx->mdctx, pdsactx->md, NULL))
238 EVP_MD_CTX_free(pdsactx->mdctx);
239 EVP_MD_free(pdsactx->md);
240 pdsactx->mdctx = NULL;
245 int dsa_digest_signverify_update(void *vpdsactx, const unsigned char *data,
248 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
250 if (pdsactx == NULL || pdsactx->mdctx == NULL)
253 return EVP_DigestUpdate(pdsactx->mdctx, data, datalen);
256 int dsa_digest_sign_final(void *vpdsactx, unsigned char *sig, size_t *siglen,
259 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
260 unsigned char digest[EVP_MAX_MD_SIZE];
261 unsigned int dlen = 0;
263 if (pdsactx == NULL || pdsactx->mdctx == NULL)
267 * If sig is NULL then we're just finding out the sig size. Other fields
268 * are ignored. Defer to dsa_sign.
272 * TODO(3.0): There is the possibility that some externally provided
273 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
274 * but that problem is much larger than just in DSA.
276 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
280 pdsactx->flag_allow_md = 1;
282 return dsa_sign(vpdsactx, sig, siglen, sigsize, digest, (size_t)dlen);
286 int dsa_digest_verify_final(void *vpdsactx, const unsigned char *sig,
289 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
290 unsigned char digest[EVP_MAX_MD_SIZE];
291 unsigned int dlen = 0;
293 if (pdsactx == NULL || pdsactx->mdctx == NULL)
297 * TODO(3.0): There is the possibility that some externally provided
298 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
299 * but that problem is much larger than just in DSA.
301 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
304 pdsactx->flag_allow_md = 1;
306 return dsa_verify(vpdsactx, sig, siglen, digest, (size_t)dlen);
309 static void dsa_freectx(void *vpdsactx)
311 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
313 DSA_free(pdsactx->dsa);
314 EVP_MD_CTX_free(pdsactx->mdctx);
315 EVP_MD_free(pdsactx->md);
317 OPENSSL_free(pdsactx);
320 static void *dsa_dupctx(void *vpdsactx)
322 PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx;
323 PROV_DSA_CTX *dstctx;
325 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
332 dstctx->mdctx = NULL;
334 if (srcctx->dsa != NULL && !DSA_up_ref(srcctx->dsa))
336 dstctx->dsa = srcctx->dsa;
338 if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
340 dstctx->md = srcctx->md;
342 if (srcctx->mdctx != NULL) {
343 dstctx->mdctx = EVP_MD_CTX_new();
344 if (dstctx->mdctx == NULL
345 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
355 static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params)
357 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
360 if (pdsactx == NULL || params == NULL)
363 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
365 && !OSSL_PARAM_set_octet_string(p, pdsactx->aid, pdsactx->aid_len))
368 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
369 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pdsactx->mdname))
375 static const OSSL_PARAM known_gettable_ctx_params[] = {
376 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
377 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
381 static const OSSL_PARAM *dsa_gettable_ctx_params(void)
383 return known_gettable_ctx_params;
386 static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])
388 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
391 if (pdsactx == NULL || params == NULL)
394 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
395 /* Not allowed during certain operations */
396 if (p != NULL && !pdsactx->flag_allow_md)
399 char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname;
400 char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops;
401 const OSSL_PARAM *propsp =
402 OSSL_PARAM_locate_const(params,
403 OSSL_SIGNATURE_PARAM_PROPERTIES);
405 if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
408 && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops)))
410 if (!dsa_setup_md(pdsactx, mdname, mdprops))
417 static const OSSL_PARAM known_settable_ctx_params[] = {
418 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
422 static const OSSL_PARAM *dsa_settable_ctx_params(void)
425 * TODO(3.0): Should this function return a different set of settable ctx
426 * params if the ctx is being used for a DigestSign/DigestVerify? In that
427 * case it is not allowed to set the digest size/digest name because the
428 * digest is explicitly set as part of the init.
430 return known_settable_ctx_params;
433 static int dsa_get_ctx_md_params(void *vpdsactx, OSSL_PARAM *params)
435 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
437 if (pdsactx->mdctx == NULL)
440 return EVP_MD_CTX_get_params(pdsactx->mdctx, params);
443 static const OSSL_PARAM *dsa_gettable_ctx_md_params(void *vpdsactx)
445 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
447 if (pdsactx->md == NULL)
450 return EVP_MD_gettable_ctx_params(pdsactx->md);
453 static int dsa_set_ctx_md_params(void *vpdsactx, const OSSL_PARAM params[])
455 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
457 if (pdsactx->mdctx == NULL)
460 return EVP_MD_CTX_set_params(pdsactx->mdctx, params);
463 static const OSSL_PARAM *dsa_settable_ctx_md_params(void *vpdsactx)
465 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
467 if (pdsactx->md == NULL)
470 return EVP_MD_settable_ctx_params(pdsactx->md);
473 const OSSL_DISPATCH dsa_signature_functions[] = {
474 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx },
475 { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_signature_init },
476 { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign },
477 { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_signature_init },
478 { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify },
479 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
480 (void (*)(void))dsa_digest_signverify_init },
481 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
482 (void (*)(void))dsa_digest_signverify_update },
483 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
484 (void (*)(void))dsa_digest_sign_final },
485 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
486 (void (*)(void))dsa_digest_signverify_init },
487 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
488 (void (*)(void))dsa_digest_signverify_update },
489 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
490 (void (*)(void))dsa_digest_verify_final },
491 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx },
492 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx },
493 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params },
494 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
495 (void (*)(void))dsa_gettable_ctx_params },
496 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params },
497 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
498 (void (*)(void))dsa_settable_ctx_params },
499 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
500 (void (*)(void))dsa_get_ctx_md_params },
501 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
502 (void (*)(void))dsa_gettable_ctx_md_params },
503 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
504 (void (*)(void))dsa_set_ctx_md_params },
505 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
506 (void (*)(void))dsa_settable_ctx_md_params },