2 * Copyright 2019-2021 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_dispatch.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 <openssl/proverr.h>
27 #include "internal/nelem.h"
28 #include "internal/sizes.h"
29 #include "internal/cryptlib.h"
30 #include "prov/providercommon.h"
31 #include "prov/implementations.h"
32 #include "prov/provider_ctx.h"
33 #include "prov/securitycheck.h"
34 #include "crypto/dsa.h"
35 #include "prov/der_dsa.h"
37 static OSSL_FUNC_signature_newctx_fn dsa_newctx;
38 static OSSL_FUNC_signature_sign_init_fn dsa_sign_init;
39 static OSSL_FUNC_signature_verify_init_fn dsa_verify_init;
40 static OSSL_FUNC_signature_sign_fn dsa_sign;
41 static OSSL_FUNC_signature_verify_fn dsa_verify;
42 static OSSL_FUNC_signature_digest_sign_init_fn dsa_digest_sign_init;
43 static OSSL_FUNC_signature_digest_sign_update_fn dsa_digest_signverify_update;
44 static OSSL_FUNC_signature_digest_sign_final_fn dsa_digest_sign_final;
45 static OSSL_FUNC_signature_digest_verify_init_fn dsa_digest_verify_init;
46 static OSSL_FUNC_signature_digest_verify_update_fn dsa_digest_signverify_update;
47 static OSSL_FUNC_signature_digest_verify_final_fn dsa_digest_verify_final;
48 static OSSL_FUNC_signature_freectx_fn dsa_freectx;
49 static OSSL_FUNC_signature_dupctx_fn dsa_dupctx;
50 static OSSL_FUNC_signature_get_ctx_params_fn dsa_get_ctx_params;
51 static OSSL_FUNC_signature_gettable_ctx_params_fn dsa_gettable_ctx_params;
52 static OSSL_FUNC_signature_set_ctx_params_fn dsa_set_ctx_params;
53 static OSSL_FUNC_signature_settable_ctx_params_fn dsa_settable_ctx_params;
54 static OSSL_FUNC_signature_get_ctx_md_params_fn dsa_get_ctx_md_params;
55 static OSSL_FUNC_signature_gettable_ctx_md_params_fn dsa_gettable_ctx_md_params;
56 static OSSL_FUNC_signature_set_ctx_md_params_fn dsa_set_ctx_md_params;
57 static OSSL_FUNC_signature_settable_ctx_md_params_fn dsa_settable_ctx_md_params;
60 * What's passed as an actual key is defined by the KEYMGMT interface.
61 * We happen to know that our KEYMGMT simply passes DSA structures, so
62 * we use that here too.
71 * Flag to determine if the hash function can be changed (1) or not (0)
72 * Because it's dangerous to change during a DigestSign or DigestVerify
73 * operation, this flag is cleared by their Init function, and set again
74 * by their Final function.
76 unsigned int flag_allow_md : 1;
78 char mdname[OSSL_MAX_NAME_SIZE];
80 /* The Algorithm Identifier of the combined signature algorithm */
81 unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
92 static size_t dsa_get_md_size(const PROV_DSA_CTX *pdsactx)
94 if (pdsactx->md != NULL)
95 return EVP_MD_size(pdsactx->md);
99 static void *dsa_newctx(void *provctx, const char *propq)
101 PROV_DSA_CTX *pdsactx;
103 if (!ossl_prov_is_running())
106 pdsactx = OPENSSL_zalloc(sizeof(PROV_DSA_CTX));
110 pdsactx->libctx = PROV_LIBCTX_OF(provctx);
111 pdsactx->flag_allow_md = 1;
112 if (propq != NULL && (pdsactx->propq = OPENSSL_strdup(propq)) == NULL) {
113 OPENSSL_free(pdsactx);
115 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
120 static int dsa_setup_md(PROV_DSA_CTX *ctx,
121 const char *mdname, const char *mdprops)
124 mdprops = ctx->propq;
126 if (mdname != NULL) {
127 int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
129 EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
130 int md_nid = ossl_digest_get_approved_nid_with_sha1(ctx->libctx, md,
132 size_t mdname_len = strlen(mdname);
134 if (md == NULL || md_nid == NID_undef) {
136 ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
137 "%s could not be fetched", mdname);
138 if (md_nid == NID_undef)
139 ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
140 "digest=%s", mdname);
141 if (mdname_len >= sizeof(ctx->mdname))
142 ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
143 "%s exceeds name buffer length", mdname);
148 EVP_MD_CTX_free(ctx->mdctx);
149 EVP_MD_free(ctx->md);
152 * We do not care about DER writing errors.
153 * All it really means is that for some reason, there's no
154 * AlgorithmIdentifier to be had, but the operation itself is
155 * still valid, just as long as it's not used to construct
156 * anything that needs an AlgorithmIdentifier.
159 if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
160 && ossl_DER_w_algorithmIdentifier_DSA_with_MD(&pkt, -1, ctx->dsa,
162 && WPACKET_finish(&pkt)) {
163 WPACKET_get_total_written(&pkt, &ctx->aid_len);
164 ctx->aid = WPACKET_get_curr(&pkt);
166 WPACKET_cleanup(&pkt);
170 OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
175 static int dsa_signverify_init(void *vpdsactx, void *vdsa,
176 const OSSL_PARAM params[], int operation)
178 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
180 if (!ossl_prov_is_running()
183 || !DSA_up_ref(vdsa))
185 DSA_free(pdsactx->dsa);
187 pdsactx->operation = operation;
189 if (!dsa_set_ctx_params(pdsactx, params))
192 if (!ossl_dsa_check_key(pdsactx->libctx, vdsa,
193 operation == EVP_PKEY_OP_SIGN)) {
194 ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
200 static int dsa_sign_init(void *vpdsactx, void *vdsa, const OSSL_PARAM params[])
202 return dsa_signverify_init(vpdsactx, vdsa, params, EVP_PKEY_OP_SIGN);
205 static int dsa_verify_init(void *vpdsactx, void *vdsa,
206 const OSSL_PARAM params[])
208 return dsa_signverify_init(vpdsactx, vdsa, params, EVP_PKEY_OP_VERIFY);
211 static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen,
212 size_t sigsize, const unsigned char *tbs, size_t tbslen)
214 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
217 size_t dsasize = DSA_size(pdsactx->dsa);
218 size_t mdsize = dsa_get_md_size(pdsactx);
220 if (!ossl_prov_is_running())
228 if (sigsize < (size_t)dsasize)
231 if (mdsize != 0 && tbslen != mdsize)
234 ret = ossl_dsa_sign_int(0, tbs, tbslen, sig, &sltmp, pdsactx->dsa);
242 static int dsa_verify(void *vpdsactx, const unsigned char *sig, size_t siglen,
243 const unsigned char *tbs, size_t tbslen)
245 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
246 size_t mdsize = dsa_get_md_size(pdsactx);
248 if (!ossl_prov_is_running() || (mdsize != 0 && tbslen != mdsize))
251 return DSA_verify(0, tbs, tbslen, sig, siglen, pdsactx->dsa);
254 static int dsa_digest_signverify_init(void *vpdsactx, const char *mdname,
255 void *vdsa, const OSSL_PARAM params[],
258 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
260 if (!ossl_prov_is_running())
263 pdsactx->flag_allow_md = 0;
264 if (!dsa_signverify_init(vpdsactx, vdsa, params, operation))
267 if (!dsa_setup_md(pdsactx, mdname, NULL))
270 pdsactx->mdctx = EVP_MD_CTX_new();
271 if (pdsactx->mdctx == NULL)
274 if (!EVP_DigestInit_ex2(pdsactx->mdctx, pdsactx->md, params))
280 EVP_MD_CTX_free(pdsactx->mdctx);
281 EVP_MD_free(pdsactx->md);
282 pdsactx->mdctx = NULL;
287 static int dsa_digest_sign_init(void *vpdsactx, const char *mdname,
288 void *vdsa, const OSSL_PARAM params[])
290 return dsa_digest_signverify_init(vpdsactx, mdname, vdsa, params,
294 static int dsa_digest_verify_init(void *vpdsactx, const char *mdname,
295 void *vdsa, const OSSL_PARAM params[])
297 return dsa_digest_signverify_init(vpdsactx, mdname, vdsa, params,
301 int dsa_digest_signverify_update(void *vpdsactx, const unsigned char *data,
304 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
306 if (pdsactx == NULL || pdsactx->mdctx == NULL)
309 return EVP_DigestUpdate(pdsactx->mdctx, data, datalen);
312 int dsa_digest_sign_final(void *vpdsactx, unsigned char *sig, size_t *siglen,
315 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
316 unsigned char digest[EVP_MAX_MD_SIZE];
317 unsigned int dlen = 0;
319 if (!ossl_prov_is_running() || pdsactx == NULL || pdsactx->mdctx == NULL)
323 * If sig is NULL then we're just finding out the sig size. Other fields
324 * are ignored. Defer to dsa_sign.
328 * There is the possibility that some externally provided
329 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
330 * but that problem is much larger than just in DSA.
332 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
336 pdsactx->flag_allow_md = 1;
338 return dsa_sign(vpdsactx, sig, siglen, sigsize, digest, (size_t)dlen);
342 int dsa_digest_verify_final(void *vpdsactx, const unsigned char *sig,
345 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
346 unsigned char digest[EVP_MAX_MD_SIZE];
347 unsigned int dlen = 0;
349 if (!ossl_prov_is_running() || pdsactx == NULL || pdsactx->mdctx == NULL)
353 * There is the possibility that some externally provided
354 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
355 * but that problem is much larger than just in DSA.
357 if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
360 pdsactx->flag_allow_md = 1;
362 return dsa_verify(vpdsactx, sig, siglen, digest, (size_t)dlen);
365 static void dsa_freectx(void *vpdsactx)
367 PROV_DSA_CTX *ctx = (PROV_DSA_CTX *)vpdsactx;
369 OPENSSL_free(ctx->propq);
370 EVP_MD_CTX_free(ctx->mdctx);
371 EVP_MD_free(ctx->md);
379 static void *dsa_dupctx(void *vpdsactx)
381 PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx;
382 PROV_DSA_CTX *dstctx;
384 if (!ossl_prov_is_running())
387 dstctx = OPENSSL_zalloc(sizeof(*srcctx));
394 dstctx->mdctx = NULL;
395 dstctx->propq = NULL;
397 if (srcctx->dsa != NULL && !DSA_up_ref(srcctx->dsa))
399 dstctx->dsa = srcctx->dsa;
401 if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
403 dstctx->md = srcctx->md;
405 if (srcctx->mdctx != NULL) {
406 dstctx->mdctx = EVP_MD_CTX_new();
407 if (dstctx->mdctx == NULL
408 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
411 if (srcctx->propq != NULL) {
412 dstctx->propq = OPENSSL_strdup(srcctx->propq);
413 if (dstctx->propq == NULL)
423 static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params)
425 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
431 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
433 && !OSSL_PARAM_set_octet_string(p, pdsactx->aid, pdsactx->aid_len))
436 p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
437 if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pdsactx->mdname))
443 static const OSSL_PARAM known_gettable_ctx_params[] = {
444 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
445 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
449 static const OSSL_PARAM *dsa_gettable_ctx_params(ossl_unused void *ctx,
450 ossl_unused void *provctx)
452 return known_gettable_ctx_params;
455 static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])
457 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
465 p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
466 /* Not allowed during certain operations */
467 if (p != NULL && !pdsactx->flag_allow_md)
470 char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname;
471 char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops;
472 const OSSL_PARAM *propsp =
473 OSSL_PARAM_locate_const(params,
474 OSSL_SIGNATURE_PARAM_PROPERTIES);
476 if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
479 && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops)))
481 if (!dsa_setup_md(pdsactx, mdname, mdprops))
488 static const OSSL_PARAM settable_ctx_params[] = {
489 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
490 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),
494 static const OSSL_PARAM settable_ctx_params_no_digest[] = {
498 static const OSSL_PARAM *dsa_settable_ctx_params(void *vpdsactx,
499 ossl_unused void *provctx)
501 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
503 if (pdsactx != NULL && !pdsactx->flag_allow_md)
504 return settable_ctx_params_no_digest;
505 return settable_ctx_params;
508 static int dsa_get_ctx_md_params(void *vpdsactx, OSSL_PARAM *params)
510 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
512 if (pdsactx->mdctx == NULL)
515 return EVP_MD_CTX_get_params(pdsactx->mdctx, params);
518 static const OSSL_PARAM *dsa_gettable_ctx_md_params(void *vpdsactx)
520 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
522 if (pdsactx->md == NULL)
525 return EVP_MD_gettable_ctx_params(pdsactx->md);
528 static int dsa_set_ctx_md_params(void *vpdsactx, const OSSL_PARAM params[])
530 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
532 if (pdsactx->mdctx == NULL)
535 return EVP_MD_CTX_set_params(pdsactx->mdctx, params);
538 static const OSSL_PARAM *dsa_settable_ctx_md_params(void *vpdsactx)
540 PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
542 if (pdsactx->md == NULL)
545 return EVP_MD_settable_ctx_params(pdsactx->md);
548 const OSSL_DISPATCH ossl_dsa_signature_functions[] = {
549 { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx },
550 { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_sign_init },
551 { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign },
552 { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_verify_init },
553 { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify },
554 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
555 (void (*)(void))dsa_digest_sign_init },
556 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
557 (void (*)(void))dsa_digest_signverify_update },
558 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
559 (void (*)(void))dsa_digest_sign_final },
560 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
561 (void (*)(void))dsa_digest_verify_init },
562 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
563 (void (*)(void))dsa_digest_signverify_update },
564 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
565 (void (*)(void))dsa_digest_verify_final },
566 { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx },
567 { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx },
568 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params },
569 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
570 (void (*)(void))dsa_gettable_ctx_params },
571 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params },
572 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
573 (void (*)(void))dsa_settable_ctx_params },
574 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
575 (void (*)(void))dsa_get_ctx_md_params },
576 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
577 (void (*)(void))dsa_gettable_ctx_md_params },
578 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
579 (void (*)(void))dsa_set_ctx_md_params },
580 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
581 (void (*)(void))dsa_settable_ctx_md_params },