2 * Copyright 2019-2020 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 "internal/nelem.h"
27 #include "internal/sizes.h"
28 #include "internal/cryptlib.h"
29 #include "prov/providercommonerr.h"
30 #include "prov/implementations.h"
31 #include "prov/providercommonerr.h"
32 #include "prov/provider_ctx.h"
33 #include "crypto/dsa.h"
34 #include "prov/der_dsa.h"
36 static OSSL_FUNC_signature_newctx_fn dsa_newctx
;
37 static OSSL_FUNC_signature_sign_init_fn dsa_signature_init
;
38 static OSSL_FUNC_signature_verify_init_fn dsa_signature_init
;
39 static OSSL_FUNC_signature_sign_fn dsa_sign
;
40 static OSSL_FUNC_signature_verify_fn dsa_verify
;
41 static OSSL_FUNC_signature_digest_sign_init_fn dsa_digest_signverify_init
;
42 static OSSL_FUNC_signature_digest_sign_update_fn dsa_digest_signverify_update
;
43 static OSSL_FUNC_signature_digest_sign_final_fn dsa_digest_sign_final
;
44 static OSSL_FUNC_signature_digest_verify_init_fn dsa_digest_signverify_init
;
45 static OSSL_FUNC_signature_digest_verify_update_fn dsa_digest_signverify_update
;
46 static OSSL_FUNC_signature_digest_verify_final_fn dsa_digest_verify_final
;
47 static OSSL_FUNC_signature_freectx_fn dsa_freectx
;
48 static OSSL_FUNC_signature_dupctx_fn dsa_dupctx
;
49 static OSSL_FUNC_signature_get_ctx_params_fn dsa_get_ctx_params
;
50 static OSSL_FUNC_signature_gettable_ctx_params_fn dsa_gettable_ctx_params
;
51 static OSSL_FUNC_signature_set_ctx_params_fn dsa_set_ctx_params
;
52 static OSSL_FUNC_signature_settable_ctx_params_fn dsa_settable_ctx_params
;
53 static OSSL_FUNC_signature_get_ctx_md_params_fn dsa_get_ctx_md_params
;
54 static OSSL_FUNC_signature_gettable_ctx_md_params_fn dsa_gettable_ctx_md_params
;
55 static OSSL_FUNC_signature_set_ctx_md_params_fn dsa_set_ctx_md_params
;
56 static OSSL_FUNC_signature_settable_ctx_md_params_fn dsa_settable_ctx_md_params
;
59 * What's passed as an actual key is defined by the KEYMGMT interface.
60 * We happen to know that our KEYMGMT simply passes DSA structures, so
61 * we use that here too.
70 * Flag to determine if the hash function can be changed (1) or not (0)
71 * Because it's dangerous to change during a DigestSign or DigestVerify
72 * operation, this flag is cleared by their Init function, and set again
73 * by their Final function.
75 unsigned int flag_allow_md
: 1;
77 char mdname
[OSSL_MAX_NAME_SIZE
];
79 /* The Algorithm Identifier of the combined signature algorithm */
80 unsigned char aid_buf
[OSSL_MAX_ALGORITHM_ID_SIZE
];
90 static size_t dsa_get_md_size(const PROV_DSA_CTX
*pdsactx
)
92 if (pdsactx
->md
!= NULL
)
93 return EVP_MD_size(pdsactx
->md
);
97 static int dsa_get_md_nid(const EVP_MD
*md
)
100 * Because the DSA library deals with NIDs, we need to translate.
101 * We do so using EVP_MD_is_a(), and therefore need a name to NID
104 static const OSSL_ITEM name_to_nid
[] = {
105 { NID_sha1
, OSSL_DIGEST_NAME_SHA1
},
106 { NID_sha224
, OSSL_DIGEST_NAME_SHA2_224
},
107 { NID_sha256
, OSSL_DIGEST_NAME_SHA2_256
},
108 { NID_sha384
, OSSL_DIGEST_NAME_SHA2_384
},
109 { NID_sha512
, OSSL_DIGEST_NAME_SHA2_512
},
110 { NID_sha3_224
, OSSL_DIGEST_NAME_SHA3_224
},
111 { NID_sha3_256
, OSSL_DIGEST_NAME_SHA3_256
},
112 { NID_sha3_384
, OSSL_DIGEST_NAME_SHA3_384
},
113 { NID_sha3_512
, OSSL_DIGEST_NAME_SHA3_512
},
116 int mdnid
= NID_undef
;
121 for (i
= 0; i
< OSSL_NELEM(name_to_nid
); i
++) {
122 if (EVP_MD_is_a(md
, name_to_nid
[i
].ptr
)) {
123 mdnid
= (int)name_to_nid
[i
].id
;
128 if (mdnid
== NID_undef
)
129 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
);
135 static void *dsa_newctx(void *provctx
, const char *propq
)
137 PROV_DSA_CTX
*pdsactx
= OPENSSL_zalloc(sizeof(PROV_DSA_CTX
));
142 pdsactx
->libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
143 pdsactx
->flag_allow_md
= 1;
144 if (propq
!= NULL
&& (pdsactx
->propq
= OPENSSL_strdup(propq
)) == NULL
) {
145 OPENSSL_free(pdsactx
);
147 ERR_raise(ERR_LIB_PROV
, ERR_R_MALLOC_FAILURE
);
152 static int dsa_setup_md(PROV_DSA_CTX
*ctx
,
153 const char *mdname
, const char *mdprops
)
156 mdprops
= ctx
->propq
;
158 if (mdname
!= NULL
) {
159 EVP_MD
*md
= EVP_MD_fetch(ctx
->libctx
, mdname
, mdprops
);
160 int md_nid
= dsa_get_md_nid(md
);
163 if (md
== NULL
|| md_nid
== NID_undef
) {
168 EVP_MD_CTX_free(ctx
->mdctx
);
169 EVP_MD_free(ctx
->md
);
172 * TODO(3.0) Should we care about DER writing errors?
173 * All it really means is that for some reason, there's no
174 * AlgorithmIdentifier to be had, but the operation itself is
175 * still valid, just as long as it's not used to construct
176 * anything that needs an AlgorithmIdentifier.
179 if (WPACKET_init_der(&pkt
, ctx
->aid_buf
, sizeof(ctx
->aid_buf
))
180 && DER_w_algorithmIdentifier_DSA_with_MD(&pkt
, -1, ctx
->dsa
,
182 && WPACKET_finish(&pkt
)) {
183 WPACKET_get_total_written(&pkt
, &ctx
->aid_len
);
184 ctx
->aid
= WPACKET_get_curr(&pkt
);
186 WPACKET_cleanup(&pkt
);
190 OPENSSL_strlcpy(ctx
->mdname
, mdname
, sizeof(ctx
->mdname
));
195 static int dsa_signature_init(void *vpdsactx
, void *vdsa
)
197 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
199 if (pdsactx
== NULL
|| vdsa
== NULL
|| !DSA_up_ref(vdsa
))
201 DSA_free(pdsactx
->dsa
);
206 static int dsa_sign(void *vpdsactx
, unsigned char *sig
, size_t *siglen
,
207 size_t sigsize
, const unsigned char *tbs
, size_t tbslen
)
209 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
212 size_t dsasize
= DSA_size(pdsactx
->dsa
);
213 size_t mdsize
= dsa_get_md_size(pdsactx
);
220 if (sigsize
< (size_t)dsasize
)
223 if (mdsize
!= 0 && tbslen
!= mdsize
)
226 ret
= dsa_sign_int(0, tbs
, tbslen
, sig
, &sltmp
, pdsactx
->dsa
);
234 static int dsa_verify(void *vpdsactx
, const unsigned char *sig
, size_t siglen
,
235 const unsigned char *tbs
, size_t tbslen
)
237 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
238 size_t mdsize
= dsa_get_md_size(pdsactx
);
240 if (mdsize
!= 0 && tbslen
!= mdsize
)
243 return DSA_verify(0, tbs
, tbslen
, sig
, siglen
, pdsactx
->dsa
);
246 static int dsa_digest_signverify_init(void *vpdsactx
, const char *mdname
,
249 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
251 pdsactx
->flag_allow_md
= 0;
252 if (!dsa_signature_init(vpdsactx
, vdsa
))
255 if (!dsa_setup_md(pdsactx
, mdname
, NULL
))
258 pdsactx
->mdctx
= EVP_MD_CTX_new();
259 if (pdsactx
->mdctx
== NULL
)
262 if (!EVP_DigestInit_ex(pdsactx
->mdctx
, pdsactx
->md
, NULL
))
268 EVP_MD_CTX_free(pdsactx
->mdctx
);
269 EVP_MD_free(pdsactx
->md
);
270 pdsactx
->mdctx
= NULL
;
275 int dsa_digest_signverify_update(void *vpdsactx
, const unsigned char *data
,
278 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
280 if (pdsactx
== NULL
|| pdsactx
->mdctx
== NULL
)
283 return EVP_DigestUpdate(pdsactx
->mdctx
, data
, datalen
);
286 int dsa_digest_sign_final(void *vpdsactx
, unsigned char *sig
, size_t *siglen
,
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 * If sig is NULL then we're just finding out the sig size. Other fields
298 * are ignored. Defer to dsa_sign.
302 * TODO(3.0): There is the possibility that some externally provided
303 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
304 * but that problem is much larger than just in DSA.
306 if (!EVP_DigestFinal_ex(pdsactx
->mdctx
, digest
, &dlen
))
310 pdsactx
->flag_allow_md
= 1;
312 return dsa_sign(vpdsactx
, sig
, siglen
, sigsize
, digest
, (size_t)dlen
);
316 int dsa_digest_verify_final(void *vpdsactx
, const unsigned char *sig
,
319 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
320 unsigned char digest
[EVP_MAX_MD_SIZE
];
321 unsigned int dlen
= 0;
323 if (pdsactx
== NULL
|| pdsactx
->mdctx
== NULL
)
327 * TODO(3.0): There is the possibility that some externally provided
328 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
329 * but that problem is much larger than just in DSA.
331 if (!EVP_DigestFinal_ex(pdsactx
->mdctx
, digest
, &dlen
))
334 pdsactx
->flag_allow_md
= 1;
336 return dsa_verify(vpdsactx
, sig
, siglen
, digest
, (size_t)dlen
);
339 static void dsa_freectx(void *vpdsactx
)
341 PROV_DSA_CTX
*ctx
= (PROV_DSA_CTX
*)vpdsactx
;
343 OPENSSL_free(ctx
->propq
);
344 EVP_MD_CTX_free(ctx
->mdctx
);
345 EVP_MD_free(ctx
->md
);
354 static void *dsa_dupctx(void *vpdsactx
)
356 PROV_DSA_CTX
*srcctx
= (PROV_DSA_CTX
*)vpdsactx
;
357 PROV_DSA_CTX
*dstctx
;
359 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
366 dstctx
->mdctx
= NULL
;
368 if (srcctx
->dsa
!= NULL
&& !DSA_up_ref(srcctx
->dsa
))
370 dstctx
->dsa
= srcctx
->dsa
;
372 if (srcctx
->md
!= NULL
&& !EVP_MD_up_ref(srcctx
->md
))
374 dstctx
->md
= srcctx
->md
;
376 if (srcctx
->mdctx
!= NULL
) {
377 dstctx
->mdctx
= EVP_MD_CTX_new();
378 if (dstctx
->mdctx
== NULL
379 || !EVP_MD_CTX_copy_ex(dstctx
->mdctx
, srcctx
->mdctx
))
389 static int dsa_get_ctx_params(void *vpdsactx
, OSSL_PARAM
*params
)
391 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
394 if (pdsactx
== NULL
|| params
== NULL
)
397 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_ALGORITHM_ID
);
399 && !OSSL_PARAM_set_octet_string(p
, pdsactx
->aid
, pdsactx
->aid_len
))
402 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
403 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, pdsactx
->mdname
))
409 static const OSSL_PARAM known_gettable_ctx_params
[] = {
410 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID
, NULL
, 0),
411 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
415 static const OSSL_PARAM
*dsa_gettable_ctx_params(void)
417 return known_gettable_ctx_params
;
420 static int dsa_set_ctx_params(void *vpdsactx
, const OSSL_PARAM params
[])
422 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
425 if (pdsactx
== NULL
|| params
== NULL
)
428 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
429 /* Not allowed during certain operations */
430 if (p
!= NULL
&& !pdsactx
->flag_allow_md
)
433 char mdname
[OSSL_MAX_NAME_SIZE
] = "", *pmdname
= mdname
;
434 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = "", *pmdprops
= mdprops
;
435 const OSSL_PARAM
*propsp
=
436 OSSL_PARAM_locate_const(params
,
437 OSSL_SIGNATURE_PARAM_PROPERTIES
);
439 if (!OSSL_PARAM_get_utf8_string(p
, &pmdname
, sizeof(mdname
)))
442 && !OSSL_PARAM_get_utf8_string(propsp
, &pmdprops
, sizeof(mdprops
)))
444 if (!dsa_setup_md(pdsactx
, mdname
, mdprops
))
451 static const OSSL_PARAM known_settable_ctx_params
[] = {
452 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
453 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES
, NULL
, 0),
457 static const OSSL_PARAM
*dsa_settable_ctx_params(void)
460 * TODO(3.0): Should this function return a different set of settable ctx
461 * params if the ctx is being used for a DigestSign/DigestVerify? In that
462 * case it is not allowed to set the digest size/digest name because the
463 * digest is explicitly set as part of the init.
465 return known_settable_ctx_params
;
468 static int dsa_get_ctx_md_params(void *vpdsactx
, OSSL_PARAM
*params
)
470 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
472 if (pdsactx
->mdctx
== NULL
)
475 return EVP_MD_CTX_get_params(pdsactx
->mdctx
, params
);
478 static const OSSL_PARAM
*dsa_gettable_ctx_md_params(void *vpdsactx
)
480 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
482 if (pdsactx
->md
== NULL
)
485 return EVP_MD_gettable_ctx_params(pdsactx
->md
);
488 static int dsa_set_ctx_md_params(void *vpdsactx
, const OSSL_PARAM params
[])
490 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
492 if (pdsactx
->mdctx
== NULL
)
495 return EVP_MD_CTX_set_params(pdsactx
->mdctx
, params
);
498 static const OSSL_PARAM
*dsa_settable_ctx_md_params(void *vpdsactx
)
500 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
502 if (pdsactx
->md
== NULL
)
505 return EVP_MD_settable_ctx_params(pdsactx
->md
);
508 const OSSL_DISPATCH dsa_signature_functions
[] = {
509 { OSSL_FUNC_SIGNATURE_NEWCTX
, (void (*)(void))dsa_newctx
},
510 { OSSL_FUNC_SIGNATURE_SIGN_INIT
, (void (*)(void))dsa_signature_init
},
511 { OSSL_FUNC_SIGNATURE_SIGN
, (void (*)(void))dsa_sign
},
512 { OSSL_FUNC_SIGNATURE_VERIFY_INIT
, (void (*)(void))dsa_signature_init
},
513 { OSSL_FUNC_SIGNATURE_VERIFY
, (void (*)(void))dsa_verify
},
514 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT
,
515 (void (*)(void))dsa_digest_signverify_init
},
516 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE
,
517 (void (*)(void))dsa_digest_signverify_update
},
518 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL
,
519 (void (*)(void))dsa_digest_sign_final
},
520 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT
,
521 (void (*)(void))dsa_digest_signverify_init
},
522 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE
,
523 (void (*)(void))dsa_digest_signverify_update
},
524 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL
,
525 (void (*)(void))dsa_digest_verify_final
},
526 { OSSL_FUNC_SIGNATURE_FREECTX
, (void (*)(void))dsa_freectx
},
527 { OSSL_FUNC_SIGNATURE_DUPCTX
, (void (*)(void))dsa_dupctx
},
528 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS
, (void (*)(void))dsa_get_ctx_params
},
529 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS
,
530 (void (*)(void))dsa_gettable_ctx_params
},
531 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS
, (void (*)(void))dsa_set_ctx_params
},
532 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS
,
533 (void (*)(void))dsa_settable_ctx_params
},
534 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS
,
535 (void (*)(void))dsa_get_ctx_md_params
},
536 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS
,
537 (void (*)(void))dsa_gettable_ctx_md_params
},
538 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS
,
539 (void (*)(void))dsa_set_ctx_md_params
},
540 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS
,
541 (void (*)(void))dsa_settable_ctx_md_params
},