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
11 * DSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
18 #include <openssl/crypto.h>
19 #include <openssl/core_numbers.h>
20 #include <openssl/core_names.h>
21 #include <openssl/err.h>
22 #include <openssl/dsa.h>
23 #include <openssl/params.h>
24 #include <openssl/evp.h>
25 #include <openssl/err.h>
26 #include "internal/nelem.h"
27 #include "internal/sizes.h"
28 #include "prov/providercommonerr.h"
29 #include "prov/implementations.h"
30 #include "prov/providercommonerr.h"
31 #include "prov/provider_ctx.h"
32 #include "crypto/dsa.h"
34 static OSSL_OP_signature_newctx_fn dsa_newctx;
35 static OSSL_OP_signature_sign_init_fn dsa_signature_init;
36 static OSSL_OP_signature_verify_init_fn dsa_signature_init;
37 static OSSL_OP_signature_sign_fn dsa_sign;
38 static OSSL_OP_signature_verify_fn dsa_verify;
39 static OSSL_OP_signature_digest_sign_init_fn dsa_digest_signverify_init;
40 static OSSL_OP_signature_digest_sign_update_fn dsa_digest_signverify_update;
41 static OSSL_OP_signature_digest_sign_final_fn dsa_digest_sign_final;
42 static OSSL_OP_signature_digest_verify_init_fn dsa_digest_signverify_init;
43 static OSSL_OP_signature_digest_verify_update_fn dsa_digest_signverify_update;
44 static OSSL_OP_signature_digest_verify_final_fn dsa_digest_verify_final;
45 static OSSL_OP_signature_freectx_fn dsa_freectx;
46 static OSSL_OP_signature_dupctx_fn dsa_dupctx;
47 static OSSL_OP_signature_get_ctx_params_fn dsa_get_ctx_params;
48 static OSSL_OP_signature_gettable_ctx_params_fn dsa_gettable_ctx_params;
49 static OSSL_OP_signature_set_ctx_params_fn dsa_set_ctx_params;
50 static OSSL_OP_signature_settable_ctx_params_fn dsa_settable_ctx_params;
51 static OSSL_OP_signature_get_ctx_md_params_fn dsa_get_ctx_md_params;
52 static OSSL_OP_signature_gettable_ctx_md_params_fn dsa_gettable_ctx_md_params;
53 static OSSL_OP_signature_set_ctx_md_params_fn dsa_set_ctx_md_params;
54 static OSSL_OP_signature_settable_ctx_md_params_fn dsa_settable_ctx_md_params;
57 * What's passed as an actual key is defined by the KEYMGMT interface.
58 * We happen to know that our KEYMGMT simply passes DSA structures, so
59 * we use that here too.
67 * Flag to determine if the hash function can be changed (1) or not (0)
68 * Because it's dangerous to change during a DigestSign or DigestVerify
69 * operation, this flag is cleared by their Init function, and set again
70 * by their Final function.
72 unsigned int flag_allow_md : 1;
74 char mdname[OSSL_MAX_NAME_SIZE];
76 /* The Algorithm Identifier of the combined signature agorithm */
77 unsigned char aid[OSSL_MAX_ALGORITHM_ID_SIZE];
86 static size_t dsa_get_md_size(const PROV_DSA_CTX *pdsactx)
88 if (pdsactx->md != NULL)
89 return EVP_MD_size(pdsactx->md);
93 static int dsa_get_md_nid(const EVP_MD *md)
96 * Because the DSA library deals with NIDs, we need to translate.
97 * We do so using EVP_MD_is_a(), and therefore need a name to NID
100 static const OSSL_ITEM name_to_nid[] = {
101 { NID_sha1, OSSL_DIGEST_NAME_SHA1 },
102 { NID_sha224, OSSL_DIGEST_NAME_SHA2_224 },
103 { NID_sha256, OSSL_DIGEST_NAME_SHA2_256 },
104 { NID_sha384, OSSL_DIGEST_NAME_SHA2_384 },
105 { NID_sha512, OSSL_DIGEST_NAME_SHA2_512 },
106 { NID_sha3_224, OSSL_DIGEST_NAME_SHA3_224 },
107 { NID_sha3_256, OSSL_DIGEST_NAME_SHA3_256 },
108 { NID_sha3_384, OSSL_DIGEST_NAME_SHA3_384 },
109 { NID_sha3_512, OSSL_DIGEST_NAME_SHA3_512 },
112 int mdnid = NID_undef;
117 for (i = 0; i < OSSL_NELEM(name_to_nid); i++) {
118 if (EVP_MD_is_a(md, name_to_nid[i].ptr)) {
119 mdnid = (int)name_to_nid[i].id;
124 if (mdnid == NID_undef)
125 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_DIGEST);
131 static void *dsa_newctx(void *provctx)
133 PROV_DSA_CTX *pdsactx = OPENSSL_zalloc(sizeof(PROV_DSA_CTX));
138 pdsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx);
139 pdsactx->flag_allow_md = 1;
143 static int dsa_setup_md(PROV_DSA_CTX *ctx,
144 const char *mdname, const char *mdprops)
146 if (mdname != NULL) {
147 EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
148 int md_nid = dsa_get_md_nid(md);
149 size_t algorithmidentifier_len = 0;
150 const unsigned char *algorithmidentifier;
152 EVP_MD_free(ctx->md);
154 ctx->mdname[0] = '\0';
156 algorithmidentifier =
157 dsa_algorithmidentifier_encoding(md_nid, &algorithmidentifier_len);
159 if (algorithmidentifier == NULL) {
165 OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
166 memcpy(ctx->aid, algorithmidentifier, algorithmidentifier_len);
167 ctx->aid_len = algorithmidentifier_len;
172 static int dsa_signature_init(void *vpdsactx, void *vdsa)
174 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
176 if (pdsactx == NULL || vdsa == NULL || !DSA_up_ref(vdsa))
178 DSA_free(pdsactx->dsa);
183 static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen,
184 size_t sigsize, const unsigned char *tbs, size_t tbslen)
186 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
189 size_t dsasize = DSA_size(pdsactx->dsa);
190 size_t mdsize = dsa_get_md_size(pdsactx);
197 if (sigsize < (size_t)dsasize)
200 if (mdsize != 0 && tbslen != mdsize)
203 ret = dsa_sign_int(0, tbs, tbslen, sig, &sltmp, pdsactx->dsa);
211 static int dsa_verify(void *vpdsactx, const unsigned char *sig, size_t siglen,
212 const unsigned char *tbs, size_t tbslen)
214 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
215 size_t mdsize = dsa_get_md_size(pdsactx);
217 if (mdsize != 0 && tbslen != mdsize)
220 return DSA_verify(0, tbs, tbslen, sig, siglen, pdsactx->dsa);
223 static int dsa_digest_signverify_init(void *vpdsactx, const char *mdname,
224 const char *props, void *vdsa)
226 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
228 pdsactx->flag_allow_md = 0;
229 if (!dsa_signature_init(vpdsactx, vdsa))
232 if (!dsa_setup_md(pdsactx, mdname, props))
235 pdsactx->mdctx = EVP_MD_CTX_new();
236 if (pdsactx->mdctx == NULL)
239 if (!EVP_DigestInit_ex(pdsactx->mdctx, pdsactx->md, NULL))
245 EVP_MD_CTX_free(pdsactx->mdctx);
246 EVP_MD_free(pdsactx->md);
247 pdsactx->mdctx = NULL;
252 int dsa_digest_signverify_update(void *vpdsactx, const unsigned char *data,
255 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
257 if (pdsactx == NULL || pdsactx->mdctx == NULL)
260 return EVP_DigestUpdate(pdsactx->mdctx, data, datalen);
263 int dsa_digest_sign_final(void *vpdsactx, unsigned char *sig, size_t *siglen,
266 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
267 unsigned char digest[EVP_MAX_MD_SIZE];
268 unsigned int dlen = 0;
270 if (pdsactx == NULL || pdsactx->mdctx == NULL)
274 * If sig is NULL then we're just finding out the sig size. Other fields
275 * are ignored. Defer to dsa_sign.
279 * TODO(3.0): There is the possibility that some externally provided
280 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
281 * but that problem is much larger than just in DSA.
283 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
287 pdsactx->flag_allow_md = 1;
289 return dsa_sign(vpdsactx, sig, siglen, sigsize, digest, (size_t)dlen);
293 int dsa_digest_verify_final(void *vpdsactx, const unsigned char *sig,
296 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
297 unsigned char digest[EVP_MAX_MD_SIZE];
298 unsigned int dlen = 0;
300 if (pdsactx == NULL || pdsactx->mdctx == NULL)
304 * TODO(3.0): There is the possibility that some externally provided
305 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
306 * but that problem is much larger than just in DSA.
308 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
311 pdsactx->flag_allow_md = 1;
313 return dsa_verify(vpdsactx, sig, siglen, digest, (size_t)dlen);
316 static void dsa_freectx(void *vpdsactx)
318 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
320 DSA_free(pdsactx->dsa);
321 EVP_MD_CTX_free(pdsactx->mdctx);
322 EVP_MD_free(pdsactx->md);
324 OPENSSL_free(pdsactx);
327 static void *dsa_dupctx(void *vpdsactx)
329 PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx;
330 PROV_DSA_CTX *dstctx;
332 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
339 dstctx->mdctx = NULL;
341 if (srcctx->dsa != NULL && !DSA_up_ref(srcctx->dsa))
343 dstctx->dsa = srcctx->dsa;
345 if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
347 dstctx->md = srcctx->md;
349 if (srcctx->mdctx != NULL) {
350 dstctx->mdctx = EVP_MD_CTX_new();
351 if (dstctx->mdctx == NULL
352 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
362 static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params)
364 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
367 if (pdsactx == NULL || params == NULL)
370 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
372 && !OSSL_PARAM_set_octet_string(p, pdsactx->aid, pdsactx->aid_len))
375 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
376 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pdsactx->mdname))
382 static const OSSL_PARAM known_gettable_ctx_params[] = {
383 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
384 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
388 static const OSSL_PARAM *dsa_gettable_ctx_params(void)
390 return known_gettable_ctx_params;
393 static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])
395 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
398 if (pdsactx == NULL || params == NULL)
401 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
402 /* Not allowed during certain operations */
403 if (p != NULL && !pdsactx->flag_allow_md)
406 char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname;
407 char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops;
408 const OSSL_PARAM *propsp =
409 OSSL_PARAM_locate_const(params,
410 OSSL_SIGNATURE_PARAM_PROPERTIES);
412 if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
415 && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops)))
417 if (!dsa_setup_md(pdsactx, mdname, mdprops))
424 static const OSSL_PARAM known_settable_ctx_params[] = {
425 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
429 static const OSSL_PARAM *dsa_settable_ctx_params(void)
432 * TODO(3.0): Should this function return a different set of settable ctx
433 * params if the ctx is being used for a DigestSign/DigestVerify? In that
434 * case it is not allowed to set the digest size/digest name because the
435 * digest is explicitly set as part of the init.
437 return known_settable_ctx_params;
440 static int dsa_get_ctx_md_params(void *vpdsactx, OSSL_PARAM *params)
442 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
444 if (pdsactx->mdctx == NULL)
447 return EVP_MD_CTX_get_params(pdsactx->mdctx, params);
450 static const OSSL_PARAM *dsa_gettable_ctx_md_params(void *vpdsactx)
452 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
454 if (pdsactx->md == NULL)
457 return EVP_MD_gettable_ctx_params(pdsactx->md);
460 static int dsa_set_ctx_md_params(void *vpdsactx, const OSSL_PARAM params[])
462 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
464 if (pdsactx->mdctx == NULL)
467 return EVP_MD_CTX_set_params(pdsactx->mdctx, params);
470 static const OSSL_PARAM *dsa_settable_ctx_md_params(void *vpdsactx)
472 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
474 if (pdsactx->md == NULL)
477 return EVP_MD_settable_ctx_params(pdsactx->md);
480 const OSSL_DISPATCH dsa_signature_functions[] = {
481 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx },
482 { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_signature_init },
483 { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign },
484 { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_signature_init },
485 { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify },
486 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
487 (void (*)(void))dsa_digest_signverify_init },
488 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
489 (void (*)(void))dsa_digest_signverify_update },
490 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
491 (void (*)(void))dsa_digest_sign_final },
492 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
493 (void (*)(void))dsa_digest_signverify_init },
494 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
495 (void (*)(void))dsa_digest_signverify_update },
496 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
497 (void (*)(void))dsa_digest_verify_final },
498 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx },
499 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx },
500 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params },
501 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
502 (void (*)(void))dsa_gettable_ctx_params },
503 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params },
504 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
505 (void (*)(void))dsa_settable_ctx_params },
506 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
507 (void (*)(void))dsa_get_ctx_md_params },
508 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
509 (void (*)(void))dsa_gettable_ctx_md_params },
510 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
511 (void (*)(void))dsa_set_ctx_md_params },
512 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
513 (void (*)(void))dsa_settable_ctx_md_params },