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_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 "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_OP_signature_newctx_fn dsa_newctx
;
37 static OSSL_OP_signature_sign_init_fn dsa_signature_init
;
38 static OSSL_OP_signature_verify_init_fn dsa_signature_init
;
39 static OSSL_OP_signature_sign_fn dsa_sign
;
40 static OSSL_OP_signature_verify_fn dsa_verify
;
41 static OSSL_OP_signature_digest_sign_init_fn dsa_digest_signverify_init
;
42 static OSSL_OP_signature_digest_sign_update_fn dsa_digest_signverify_update
;
43 static OSSL_OP_signature_digest_sign_final_fn dsa_digest_sign_final
;
44 static OSSL_OP_signature_digest_verify_init_fn dsa_digest_signverify_init
;
45 static OSSL_OP_signature_digest_verify_update_fn dsa_digest_signverify_update
;
46 static OSSL_OP_signature_digest_verify_final_fn dsa_digest_verify_final
;
47 static OSSL_OP_signature_freectx_fn dsa_freectx
;
48 static OSSL_OP_signature_dupctx_fn dsa_dupctx
;
49 static OSSL_OP_signature_get_ctx_params_fn dsa_get_ctx_params
;
50 static OSSL_OP_signature_gettable_ctx_params_fn dsa_gettable_ctx_params
;
51 static OSSL_OP_signature_set_ctx_params_fn dsa_set_ctx_params
;
52 static OSSL_OP_signature_settable_ctx_params_fn dsa_settable_ctx_params
;
53 static OSSL_OP_signature_get_ctx_md_params_fn dsa_get_ctx_md_params
;
54 static OSSL_OP_signature_gettable_ctx_md_params_fn dsa_gettable_ctx_md_params
;
55 static OSSL_OP_signature_set_ctx_md_params_fn dsa_set_ctx_md_params
;
56 static OSSL_OP_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.
69 * Flag to determine if the hash function can be changed (1) or not (0)
70 * Because it's dangerous to change during a DigestSign or DigestVerify
71 * operation, this flag is cleared by their Init function, and set again
72 * by their Final function.
74 unsigned int flag_allow_md
: 1;
76 char mdname
[OSSL_MAX_NAME_SIZE
];
78 /* The Algorithm Identifier of the combined signature algorithm */
79 unsigned char aid_buf
[OSSL_MAX_ALGORITHM_ID_SIZE
];
89 static size_t dsa_get_md_size(const PROV_DSA_CTX
*pdsactx
)
91 if (pdsactx
->md
!= NULL
)
92 return EVP_MD_size(pdsactx
->md
);
96 static int dsa_get_md_nid(const EVP_MD
*md
)
99 * Because the DSA library deals with NIDs, we need to translate.
100 * We do so using EVP_MD_is_a(), and therefore need a name to NID
103 static const OSSL_ITEM name_to_nid
[] = {
104 { NID_sha1
, OSSL_DIGEST_NAME_SHA1
},
105 { NID_sha224
, OSSL_DIGEST_NAME_SHA2_224
},
106 { NID_sha256
, OSSL_DIGEST_NAME_SHA2_256
},
107 { NID_sha384
, OSSL_DIGEST_NAME_SHA2_384
},
108 { NID_sha512
, OSSL_DIGEST_NAME_SHA2_512
},
109 { NID_sha3_224
, OSSL_DIGEST_NAME_SHA3_224
},
110 { NID_sha3_256
, OSSL_DIGEST_NAME_SHA3_256
},
111 { NID_sha3_384
, OSSL_DIGEST_NAME_SHA3_384
},
112 { NID_sha3_512
, OSSL_DIGEST_NAME_SHA3_512
},
115 int mdnid
= NID_undef
;
120 for (i
= 0; i
< OSSL_NELEM(name_to_nid
); i
++) {
121 if (EVP_MD_is_a(md
, name_to_nid
[i
].ptr
)) {
122 mdnid
= (int)name_to_nid
[i
].id
;
127 if (mdnid
== NID_undef
)
128 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
);
134 static void *dsa_newctx(void *provctx
)
136 PROV_DSA_CTX
*pdsactx
= OPENSSL_zalloc(sizeof(PROV_DSA_CTX
));
141 pdsactx
->libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
142 pdsactx
->flag_allow_md
= 1;
146 static int dsa_setup_md(PROV_DSA_CTX
*ctx
,
147 const char *mdname
, const char *mdprops
)
149 if (mdname
!= NULL
) {
150 EVP_MD
*md
= EVP_MD_fetch(ctx
->libctx
, mdname
, mdprops
);
151 int md_nid
= dsa_get_md_nid(md
);
154 if (md
== NULL
|| md_nid
== NID_undef
) {
159 EVP_MD_CTX_free(ctx
->mdctx
);
160 EVP_MD_free(ctx
->md
);
163 * TODO(3.0) Should we care about DER writing errors?
164 * All it really means is that for some reason, there's no
165 * AlgorithmIdentifier to be had, but the operation itself is
166 * still valid, just as long as it's not used to construct
167 * anything that needs an AlgorithmIdentifier.
170 if (WPACKET_init_der(&pkt
, ctx
->aid_buf
, sizeof(ctx
->aid_buf
))
171 && DER_w_algorithmIdentifier_DSA_with(&pkt
, -1, ctx
->dsa
, md_nid
)
172 && WPACKET_finish(&pkt
)) {
173 WPACKET_get_total_written(&pkt
, &ctx
->aid_len
);
174 ctx
->aid
= WPACKET_get_curr(&pkt
);
176 WPACKET_cleanup(&pkt
);
180 OPENSSL_strlcpy(ctx
->mdname
, mdname
, sizeof(ctx
->mdname
));
185 static int dsa_signature_init(void *vpdsactx
, void *vdsa
)
187 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
189 if (pdsactx
== NULL
|| vdsa
== NULL
|| !DSA_up_ref(vdsa
))
191 DSA_free(pdsactx
->dsa
);
196 static int dsa_sign(void *vpdsactx
, unsigned char *sig
, size_t *siglen
,
197 size_t sigsize
, const unsigned char *tbs
, size_t tbslen
)
199 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
202 size_t dsasize
= DSA_size(pdsactx
->dsa
);
203 size_t mdsize
= dsa_get_md_size(pdsactx
);
210 if (sigsize
< (size_t)dsasize
)
213 if (mdsize
!= 0 && tbslen
!= mdsize
)
216 ret
= dsa_sign_int(0, tbs
, tbslen
, sig
, &sltmp
, pdsactx
->dsa
);
224 static int dsa_verify(void *vpdsactx
, const unsigned char *sig
, size_t siglen
,
225 const unsigned char *tbs
, size_t tbslen
)
227 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
228 size_t mdsize
= dsa_get_md_size(pdsactx
);
230 if (mdsize
!= 0 && tbslen
!= mdsize
)
233 return DSA_verify(0, tbs
, tbslen
, sig
, siglen
, pdsactx
->dsa
);
236 static int dsa_digest_signverify_init(void *vpdsactx
, const char *mdname
,
237 const char *props
, void *vdsa
)
239 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
241 pdsactx
->flag_allow_md
= 0;
242 if (!dsa_signature_init(vpdsactx
, vdsa
))
245 if (!dsa_setup_md(pdsactx
, mdname
, props
))
248 pdsactx
->mdctx
= EVP_MD_CTX_new();
249 if (pdsactx
->mdctx
== NULL
)
252 if (!EVP_DigestInit_ex(pdsactx
->mdctx
, pdsactx
->md
, NULL
))
258 EVP_MD_CTX_free(pdsactx
->mdctx
);
259 EVP_MD_free(pdsactx
->md
);
260 pdsactx
->mdctx
= NULL
;
265 int dsa_digest_signverify_update(void *vpdsactx
, const unsigned char *data
,
268 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
270 if (pdsactx
== NULL
|| pdsactx
->mdctx
== NULL
)
273 return EVP_DigestUpdate(pdsactx
->mdctx
, data
, datalen
);
276 int dsa_digest_sign_final(void *vpdsactx
, unsigned char *sig
, size_t *siglen
,
279 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
280 unsigned char digest
[EVP_MAX_MD_SIZE
];
281 unsigned int dlen
= 0;
283 if (pdsactx
== NULL
|| pdsactx
->mdctx
== NULL
)
287 * If sig is NULL then we're just finding out the sig size. Other fields
288 * are ignored. Defer to dsa_sign.
292 * TODO(3.0): There is the possibility that some externally provided
293 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
294 * but that problem is much larger than just in DSA.
296 if (!EVP_DigestFinal_ex(pdsactx
->mdctx
, digest
, &dlen
))
300 pdsactx
->flag_allow_md
= 1;
302 return dsa_sign(vpdsactx
, sig
, siglen
, sigsize
, digest
, (size_t)dlen
);
306 int dsa_digest_verify_final(void *vpdsactx
, const unsigned char *sig
,
309 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
310 unsigned char digest
[EVP_MAX_MD_SIZE
];
311 unsigned int dlen
= 0;
313 if (pdsactx
== NULL
|| pdsactx
->mdctx
== NULL
)
317 * TODO(3.0): There is the possibility that some externally provided
318 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
319 * but that problem is much larger than just in DSA.
321 if (!EVP_DigestFinal_ex(pdsactx
->mdctx
, digest
, &dlen
))
324 pdsactx
->flag_allow_md
= 1;
326 return dsa_verify(vpdsactx
, sig
, siglen
, digest
, (size_t)dlen
);
329 static void dsa_freectx(void *vpdsactx
)
331 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
333 DSA_free(pdsactx
->dsa
);
334 EVP_MD_CTX_free(pdsactx
->mdctx
);
335 EVP_MD_free(pdsactx
->md
);
337 OPENSSL_free(pdsactx
);
340 static void *dsa_dupctx(void *vpdsactx
)
342 PROV_DSA_CTX
*srcctx
= (PROV_DSA_CTX
*)vpdsactx
;
343 PROV_DSA_CTX
*dstctx
;
345 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
352 dstctx
->mdctx
= NULL
;
354 if (srcctx
->dsa
!= NULL
&& !DSA_up_ref(srcctx
->dsa
))
356 dstctx
->dsa
= srcctx
->dsa
;
358 if (srcctx
->md
!= NULL
&& !EVP_MD_up_ref(srcctx
->md
))
360 dstctx
->md
= srcctx
->md
;
362 if (srcctx
->mdctx
!= NULL
) {
363 dstctx
->mdctx
= EVP_MD_CTX_new();
364 if (dstctx
->mdctx
== NULL
365 || !EVP_MD_CTX_copy_ex(dstctx
->mdctx
, srcctx
->mdctx
))
375 static int dsa_get_ctx_params(void *vpdsactx
, OSSL_PARAM
*params
)
377 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
380 if (pdsactx
== NULL
|| params
== NULL
)
383 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_ALGORITHM_ID
);
385 && !OSSL_PARAM_set_octet_string(p
, pdsactx
->aid
, pdsactx
->aid_len
))
388 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
389 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, pdsactx
->mdname
))
395 static const OSSL_PARAM known_gettable_ctx_params
[] = {
396 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID
, NULL
, 0),
397 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
401 static const OSSL_PARAM
*dsa_gettable_ctx_params(void)
403 return known_gettable_ctx_params
;
406 static int dsa_set_ctx_params(void *vpdsactx
, const OSSL_PARAM params
[])
408 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
411 if (pdsactx
== NULL
|| params
== NULL
)
414 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
415 /* Not allowed during certain operations */
416 if (p
!= NULL
&& !pdsactx
->flag_allow_md
)
419 char mdname
[OSSL_MAX_NAME_SIZE
] = "", *pmdname
= mdname
;
420 char mdprops
[OSSL_MAX_PROPQUERY_SIZE
] = "", *pmdprops
= mdprops
;
421 const OSSL_PARAM
*propsp
=
422 OSSL_PARAM_locate_const(params
,
423 OSSL_SIGNATURE_PARAM_PROPERTIES
);
425 if (!OSSL_PARAM_get_utf8_string(p
, &pmdname
, sizeof(mdname
)))
428 && !OSSL_PARAM_get_utf8_string(propsp
, &pmdprops
, sizeof(mdprops
)))
430 if (!dsa_setup_md(pdsactx
, mdname
, mdprops
))
437 static const OSSL_PARAM known_settable_ctx_params
[] = {
438 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
442 static const OSSL_PARAM
*dsa_settable_ctx_params(void)
445 * TODO(3.0): Should this function return a different set of settable ctx
446 * params if the ctx is being used for a DigestSign/DigestVerify? In that
447 * case it is not allowed to set the digest size/digest name because the
448 * digest is explicitly set as part of the init.
450 return known_settable_ctx_params
;
453 static int dsa_get_ctx_md_params(void *vpdsactx
, OSSL_PARAM
*params
)
455 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
457 if (pdsactx
->mdctx
== NULL
)
460 return EVP_MD_CTX_get_params(pdsactx
->mdctx
, params
);
463 static const OSSL_PARAM
*dsa_gettable_ctx_md_params(void *vpdsactx
)
465 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
467 if (pdsactx
->md
== NULL
)
470 return EVP_MD_gettable_ctx_params(pdsactx
->md
);
473 static int dsa_set_ctx_md_params(void *vpdsactx
, const OSSL_PARAM params
[])
475 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
477 if (pdsactx
->mdctx
== NULL
)
480 return EVP_MD_CTX_set_params(pdsactx
->mdctx
, params
);
483 static const OSSL_PARAM
*dsa_settable_ctx_md_params(void *vpdsactx
)
485 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
487 if (pdsactx
->md
== NULL
)
490 return EVP_MD_settable_ctx_params(pdsactx
->md
);
493 const OSSL_DISPATCH dsa_signature_functions
[] = {
494 { OSSL_FUNC_SIGNATURE_NEWCTX
, (void (*)(void))dsa_newctx
},
495 { OSSL_FUNC_SIGNATURE_SIGN_INIT
, (void (*)(void))dsa_signature_init
},
496 { OSSL_FUNC_SIGNATURE_SIGN
, (void (*)(void))dsa_sign
},
497 { OSSL_FUNC_SIGNATURE_VERIFY_INIT
, (void (*)(void))dsa_signature_init
},
498 { OSSL_FUNC_SIGNATURE_VERIFY
, (void (*)(void))dsa_verify
},
499 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT
,
500 (void (*)(void))dsa_digest_signverify_init
},
501 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE
,
502 (void (*)(void))dsa_digest_signverify_update
},
503 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL
,
504 (void (*)(void))dsa_digest_sign_final
},
505 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT
,
506 (void (*)(void))dsa_digest_signverify_init
},
507 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE
,
508 (void (*)(void))dsa_digest_signverify_update
},
509 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL
,
510 (void (*)(void))dsa_digest_verify_final
},
511 { OSSL_FUNC_SIGNATURE_FREECTX
, (void (*)(void))dsa_freectx
},
512 { OSSL_FUNC_SIGNATURE_DUPCTX
, (void (*)(void))dsa_dupctx
},
513 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS
, (void (*)(void))dsa_get_ctx_params
},
514 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS
,
515 (void (*)(void))dsa_gettable_ctx_params
},
516 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS
, (void (*)(void))dsa_set_ctx_params
},
517 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS
,
518 (void (*)(void))dsa_settable_ctx_params
},
519 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS
,
520 (void (*)(void))dsa_get_ctx_md_params
},
521 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS
,
522 (void (*)(void))dsa_gettable_ctx_md_params
},
523 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS
,
524 (void (*)(void))dsa_set_ctx_md_params
},
525 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS
,
526 (void (*)(void))dsa_settable_ctx_md_params
},