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(pdsactx->libctx, 0, tbs, tbslen, sig, &sltmp,
212 static int dsa_verify(void *vpdsactx, const unsigned char *sig, size_t siglen,
213 const unsigned char *tbs, size_t tbslen)
215 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
216 size_t mdsize = dsa_get_md_size(pdsactx);
218 if (mdsize != 0 && tbslen != mdsize)
221 return DSA_verify(0, tbs, tbslen, sig, siglen, pdsactx->dsa);
224 static int dsa_digest_signverify_init(void *vpdsactx, const char *mdname,
225 const char *props, void *vdsa)
227 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
229 pdsactx->flag_allow_md = 0;
230 if (!dsa_signature_init(vpdsactx, vdsa))
233 if (!dsa_setup_md(pdsactx, mdname, props))
236 pdsactx->mdctx = EVP_MD_CTX_new();
237 if (pdsactx->mdctx == NULL)
240 if (!EVP_DigestInit_ex(pdsactx->mdctx, pdsactx->md, NULL))
246 EVP_MD_CTX_free(pdsactx->mdctx);
247 EVP_MD_free(pdsactx->md);
248 pdsactx->mdctx = NULL;
253 int dsa_digest_signverify_update(void *vpdsactx, const unsigned char *data,
256 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
258 if (pdsactx == NULL || pdsactx->mdctx == NULL)
261 return EVP_DigestUpdate(pdsactx->mdctx, data, datalen);
264 int dsa_digest_sign_final(void *vpdsactx, unsigned char *sig, size_t *siglen,
267 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
268 unsigned char digest[EVP_MAX_MD_SIZE];
269 unsigned int dlen = 0;
271 if (pdsactx == NULL || pdsactx->mdctx == NULL)
275 * If sig is NULL then we're just finding out the sig size. Other fields
276 * are ignored. Defer to dsa_sign.
280 * TODO(3.0): There is the possibility that some externally provided
281 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
282 * but that problem is much larger than just in DSA.
284 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
288 pdsactx->flag_allow_md = 1;
290 return dsa_sign(vpdsactx, sig, siglen, sigsize, digest, (size_t)dlen);
294 int dsa_digest_verify_final(void *vpdsactx, const unsigned char *sig,
297 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
298 unsigned char digest[EVP_MAX_MD_SIZE];
299 unsigned int dlen = 0;
301 if (pdsactx == NULL || pdsactx->mdctx == NULL)
305 * TODO(3.0): There is the possibility that some externally provided
306 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
307 * but that problem is much larger than just in DSA.
309 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
312 pdsactx->flag_allow_md = 1;
314 return dsa_verify(vpdsactx, sig, siglen, digest, (size_t)dlen);
317 static void dsa_freectx(void *vpdsactx)
319 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
321 DSA_free(pdsactx->dsa);
322 EVP_MD_CTX_free(pdsactx->mdctx);
323 EVP_MD_free(pdsactx->md);
325 OPENSSL_free(pdsactx);
328 static void *dsa_dupctx(void *vpdsactx)
330 PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx;
331 PROV_DSA_CTX *dstctx;
333 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
340 dstctx->mdctx = NULL;
342 if (srcctx->dsa != NULL && !DSA_up_ref(srcctx->dsa))
344 dstctx->dsa = srcctx->dsa;
346 if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
348 dstctx->md = srcctx->md;
350 if (srcctx->mdctx != NULL) {
351 dstctx->mdctx = EVP_MD_CTX_new();
352 if (dstctx->mdctx == NULL
353 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
363 static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params)
365 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
368 if (pdsactx == NULL || params == NULL)
371 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
373 && !OSSL_PARAM_set_octet_string(p, pdsactx->aid, pdsactx->aid_len))
376 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
377 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pdsactx->mdname))
383 static const OSSL_PARAM known_gettable_ctx_params[] = {
384 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
385 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
389 static const OSSL_PARAM *dsa_gettable_ctx_params(void)
391 return known_gettable_ctx_params;
394 static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])
396 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
399 if (pdsactx == NULL || params == NULL)
402 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
403 /* Not allowed during certain operations */
404 if (p != NULL && !pdsactx->flag_allow_md)
407 char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname;
408 char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops;
409 const OSSL_PARAM *propsp =
410 OSSL_PARAM_locate_const(params,
411 OSSL_SIGNATURE_PARAM_PROPERTIES);
413 if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
416 && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops)))
418 if (!dsa_setup_md(pdsactx, mdname, mdprops))
425 static const OSSL_PARAM known_settable_ctx_params[] = {
426 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
430 static const OSSL_PARAM *dsa_settable_ctx_params(void)
433 * TODO(3.0): Should this function return a different set of settable ctx
434 * params if the ctx is being used for a DigestSign/DigestVerify? In that
435 * case it is not allowed to set the digest size/digest name because the
436 * digest is explicitly set as part of the init.
438 return known_settable_ctx_params;
441 static int dsa_get_ctx_md_params(void *vpdsactx, OSSL_PARAM *params)
443 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
445 if (pdsactx->mdctx == NULL)
448 return EVP_MD_CTX_get_params(pdsactx->mdctx, params);
451 static const OSSL_PARAM *dsa_gettable_ctx_md_params(void *vpdsactx)
453 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
455 if (pdsactx->md == NULL)
458 return EVP_MD_gettable_ctx_params(pdsactx->md);
461 static int dsa_set_ctx_md_params(void *vpdsactx, const OSSL_PARAM params[])
463 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
465 if (pdsactx->mdctx == NULL)
468 return EVP_MD_CTX_set_params(pdsactx->mdctx, params);
471 static const OSSL_PARAM *dsa_settable_ctx_md_params(void *vpdsactx)
473 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
475 if (pdsactx->md == NULL)
478 return EVP_MD_settable_ctx_params(pdsactx->md);
481 const OSSL_DISPATCH dsa_signature_functions[] = {
482 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx },
483 { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_signature_init },
484 { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign },
485 { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_signature_init },
486 { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify },
487 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
488 (void (*)(void))dsa_digest_signverify_init },
489 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
490 (void (*)(void))dsa_digest_signverify_update },
491 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
492 (void (*)(void))dsa_digest_sign_final },
493 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
494 (void (*)(void))dsa_digest_signverify_init },
495 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
496 (void (*)(void))dsa_digest_signverify_update },
497 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
498 (void (*)(void))dsa_digest_verify_final },
499 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx },
500 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx },
501 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params },
502 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
503 (void (*)(void))dsa_gettable_ctx_params },
504 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params },
505 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
506 (void (*)(void))dsa_settable_ctx_params },
507 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
508 (void (*)(void))dsa_get_ctx_md_params },
509 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
510 (void (*)(void))dsa_gettable_ctx_md_params },
511 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
512 (void (*)(void))dsa_set_ctx_md_params },
513 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
514 (void (*)(void))dsa_settable_ctx_md_params },