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));
160 memcpy(ctx->aid, algorithmidentifier, algorithmidentifier_len);
161 ctx->aid_len = algorithmidentifier_len;
166 static int dsa_signature_init(void *vpdsactx, void *vdsa)
168 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
170 if (pdsactx == NULL || vdsa == NULL || !DSA_up_ref(vdsa))
172 DSA_free(pdsactx->dsa);
177 static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen,
178 size_t sigsize, const unsigned char *tbs, size_t tbslen)
180 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
183 size_t dsasize = DSA_size(pdsactx->dsa);
184 size_t mdsize = dsa_get_md_size(pdsactx);
191 if (sigsize < (size_t)dsasize)
194 if (mdsize != 0 && tbslen != mdsize)
197 ret = dsa_sign_int(pdsactx->libctx, 0, tbs, tbslen, sig, &sltmp,
206 static int dsa_verify(void *vpdsactx, const unsigned char *sig, size_t siglen,
207 const unsigned char *tbs, size_t tbslen)
209 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
210 size_t mdsize = dsa_get_md_size(pdsactx);
212 if (mdsize != 0 && tbslen != mdsize)
215 return DSA_verify(0, tbs, tbslen, sig, siglen, pdsactx->dsa);
218 static int dsa_digest_signverify_init(void *vpdsactx, const char *mdname,
219 const char *props, void *vdsa)
221 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
223 pdsactx->flag_allow_md = 0;
224 if (!dsa_signature_init(vpdsactx, vdsa))
227 if (!dsa_setup_md(pdsactx, mdname, props))
230 pdsactx->mdctx = EVP_MD_CTX_new();
231 if (pdsactx->mdctx == NULL)
234 if (!EVP_DigestInit_ex(pdsactx->mdctx, pdsactx->md, NULL))
240 EVP_MD_CTX_free(pdsactx->mdctx);
241 EVP_MD_free(pdsactx->md);
242 pdsactx->mdctx = NULL;
247 int dsa_digest_signverify_update(void *vpdsactx, const unsigned char *data,
250 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
252 if (pdsactx == NULL || pdsactx->mdctx == NULL)
255 return EVP_DigestUpdate(pdsactx->mdctx, data, datalen);
258 int dsa_digest_sign_final(void *vpdsactx, unsigned char *sig, size_t *siglen,
261 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
262 unsigned char digest[EVP_MAX_MD_SIZE];
263 unsigned int dlen = 0;
265 if (pdsactx == NULL || pdsactx->mdctx == NULL)
269 * If sig is NULL then we're just finding out the sig size. Other fields
270 * are ignored. Defer to dsa_sign.
274 * TODO(3.0): There is the possibility that some externally provided
275 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
276 * but that problem is much larger than just in DSA.
278 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
282 pdsactx->flag_allow_md = 1;
284 return dsa_sign(vpdsactx, sig, siglen, sigsize, digest, (size_t)dlen);
288 int dsa_digest_verify_final(void *vpdsactx, const unsigned char *sig,
291 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
292 unsigned char digest[EVP_MAX_MD_SIZE];
293 unsigned int dlen = 0;
295 if (pdsactx == NULL || pdsactx->mdctx == NULL)
299 * TODO(3.0): There is the possibility that some externally provided
300 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
301 * but that problem is much larger than just in DSA.
303 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
306 pdsactx->flag_allow_md = 1;
308 return dsa_verify(vpdsactx, sig, siglen, digest, (size_t)dlen);
311 static void dsa_freectx(void *vpdsactx)
313 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
315 DSA_free(pdsactx->dsa);
316 EVP_MD_CTX_free(pdsactx->mdctx);
317 EVP_MD_free(pdsactx->md);
319 OPENSSL_free(pdsactx);
322 static void *dsa_dupctx(void *vpdsactx)
324 PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx;
325 PROV_DSA_CTX *dstctx;
327 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
334 dstctx->mdctx = NULL;
336 if (srcctx->dsa != NULL && !DSA_up_ref(srcctx->dsa))
338 dstctx->dsa = srcctx->dsa;
340 if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
342 dstctx->md = srcctx->md;
344 if (srcctx->mdctx != NULL) {
345 dstctx->mdctx = EVP_MD_CTX_new();
346 if (dstctx->mdctx == NULL
347 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
357 static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params)
359 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
362 if (pdsactx == NULL || params == NULL)
365 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
367 && !OSSL_PARAM_set_octet_string(p, pdsactx->aid, pdsactx->aid_len))
370 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
371 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pdsactx->mdname))
377 static const OSSL_PARAM known_gettable_ctx_params[] = {
378 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
379 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
383 static const OSSL_PARAM *dsa_gettable_ctx_params(void)
385 return known_gettable_ctx_params;
388 static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])
390 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
393 if (pdsactx == NULL || params == NULL)
396 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
397 /* Not allowed during certain operations */
398 if (p != NULL && !pdsactx->flag_allow_md)
401 char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname;
402 char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops;
403 const OSSL_PARAM *propsp =
404 OSSL_PARAM_locate_const(params,
405 OSSL_SIGNATURE_PARAM_PROPERTIES);
407 if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
410 && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops)))
412 if (!dsa_setup_md(pdsactx, mdname, mdprops))
419 static const OSSL_PARAM known_settable_ctx_params[] = {
420 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
424 static const OSSL_PARAM *dsa_settable_ctx_params(void)
427 * TODO(3.0): Should this function return a different set of settable ctx
428 * params if the ctx is being used for a DigestSign/DigestVerify? In that
429 * case it is not allowed to set the digest size/digest name because the
430 * digest is explicitly set as part of the init.
432 return known_settable_ctx_params;
435 static int dsa_get_ctx_md_params(void *vpdsactx, OSSL_PARAM *params)
437 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
439 if (pdsactx->mdctx == NULL)
442 return EVP_MD_CTX_get_params(pdsactx->mdctx, params);
445 static const OSSL_PARAM *dsa_gettable_ctx_md_params(void *vpdsactx)
447 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
449 if (pdsactx->md == NULL)
452 return EVP_MD_gettable_ctx_params(pdsactx->md);
455 static int dsa_set_ctx_md_params(void *vpdsactx, const OSSL_PARAM params[])
457 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
459 if (pdsactx->mdctx == NULL)
462 return EVP_MD_CTX_set_params(pdsactx->mdctx, params);
465 static const OSSL_PARAM *dsa_settable_ctx_md_params(void *vpdsactx)
467 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
469 if (pdsactx->md == NULL)
472 return EVP_MD_settable_ctx_params(pdsactx->md);
475 const OSSL_DISPATCH dsa_signature_functions[] = {
476 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx },
477 { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_signature_init },
478 { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign },
479 { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_signature_init },
480 { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify },
481 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
482 (void (*)(void))dsa_digest_signverify_init },
483 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
484 (void (*)(void))dsa_digest_signverify_update },
485 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
486 (void (*)(void))dsa_digest_sign_final },
487 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
488 (void (*)(void))dsa_digest_signverify_init },
489 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
490 (void (*)(void))dsa_digest_signverify_update },
491 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
492 (void (*)(void))dsa_digest_verify_final },
493 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx },
494 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx },
495 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params },
496 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
497 (void (*)(void))dsa_gettable_ctx_params },
498 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params },
499 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
500 (void (*)(void))dsa_settable_ctx_params },
501 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
502 (void (*)(void))dsa_get_ctx_md_params },
503 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
504 (void (*)(void))dsa_gettable_ctx_md_params },
505 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
506 (void (*)(void))dsa_set_ctx_md_params },
507 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
508 (void (*)(void))dsa_settable_ctx_md_params },