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
12 #include <openssl/crypto.h>
13 #include <openssl/core_numbers.h>
14 #include <openssl/core_names.h>
15 #include <openssl/err.h>
16 #include <openssl/dsa.h>
17 #include <openssl/params.h>
18 #include <openssl/evp.h>
19 #include "internal/nelem.h"
20 #include "internal/sizes.h"
21 #include "prov/providercommonerr.h"
22 #include "prov/implementations.h"
23 #include "prov/provider_ctx.h"
24 #include "crypto/dsa.h"
26 static OSSL_OP_signature_newctx_fn dsa_newctx
;
27 static OSSL_OP_signature_sign_init_fn dsa_signature_init
;
28 static OSSL_OP_signature_verify_init_fn dsa_signature_init
;
29 static OSSL_OP_signature_sign_fn dsa_sign
;
30 static OSSL_OP_signature_verify_fn dsa_verify
;
31 static OSSL_OP_signature_digest_sign_init_fn dsa_digest_signverify_init
;
32 static OSSL_OP_signature_digest_sign_update_fn dsa_digest_signverify_update
;
33 static OSSL_OP_signature_digest_sign_final_fn dsa_digest_sign_final
;
34 static OSSL_OP_signature_digest_verify_init_fn dsa_digest_signverify_init
;
35 static OSSL_OP_signature_digest_verify_update_fn dsa_digest_signverify_update
;
36 static OSSL_OP_signature_digest_verify_final_fn dsa_digest_verify_final
;
37 static OSSL_OP_signature_freectx_fn dsa_freectx
;
38 static OSSL_OP_signature_dupctx_fn dsa_dupctx
;
39 static OSSL_OP_signature_get_ctx_params_fn dsa_get_ctx_params
;
40 static OSSL_OP_signature_gettable_ctx_params_fn dsa_gettable_ctx_params
;
41 static OSSL_OP_signature_set_ctx_params_fn dsa_set_ctx_params
;
42 static OSSL_OP_signature_settable_ctx_params_fn dsa_settable_ctx_params
;
43 static OSSL_OP_signature_get_ctx_md_params_fn dsa_get_ctx_md_params
;
44 static OSSL_OP_signature_gettable_ctx_md_params_fn dsa_gettable_ctx_md_params
;
45 static OSSL_OP_signature_set_ctx_md_params_fn dsa_set_ctx_md_params
;
46 static OSSL_OP_signature_settable_ctx_md_params_fn dsa_settable_ctx_md_params
;
49 * What's passed as an actual key is defined by the KEYMGMT interface.
50 * We happen to know that our KEYMGMT simply passes DSA structures, so
51 * we use that here too.
58 char mdname
[OSSL_MAX_NAME_SIZE
];
60 /* The Algorithm Identifier of the combined signature agorithm */
61 unsigned char aid
[OSSL_MAX_ALGORITHM_ID_SIZE
];
69 static void *dsa_newctx(void *provctx
)
71 PROV_DSA_CTX
*pdsactx
= OPENSSL_zalloc(sizeof(PROV_DSA_CTX
));
76 pdsactx
->libctx
= PROV_LIBRARY_CONTEXT_OF(provctx
);
80 static int dsa_signature_init(void *vpdsactx
, void *vdsa
)
82 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
84 if (pdsactx
== NULL
|| vdsa
== NULL
|| !DSA_up_ref(vdsa
))
86 DSA_free(pdsactx
->dsa
);
91 static int dsa_sign(void *vpdsactx
, unsigned char *sig
, size_t *siglen
,
92 size_t sigsize
, const unsigned char *tbs
, size_t tbslen
)
94 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
97 size_t dsasize
= DSA_size(pdsactx
->dsa
);
104 if (sigsize
< (size_t)dsasize
)
107 if (pdsactx
->mdsize
!= 0 && tbslen
!= pdsactx
->mdsize
)
110 ret
= dsa_sign_int(pdsactx
->libctx
, 0, tbs
, tbslen
, sig
, &sltmp
,
119 static int dsa_verify(void *vpdsactx
, const unsigned char *sig
, size_t siglen
,
120 const unsigned char *tbs
, size_t tbslen
)
122 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
124 if (pdsactx
->mdsize
!= 0 && tbslen
!= pdsactx
->mdsize
)
127 return DSA_verify(0, tbs
, tbslen
, sig
, siglen
, pdsactx
->dsa
);
130 static int get_md_nid(const EVP_MD
*md
)
133 * Because the DSA library deals with NIDs, we need to translate.
134 * We do so using EVP_MD_is_a(), and therefore need a name to NID
137 static const OSSL_ITEM name_to_nid
[] = {
138 { NID_sha1
, OSSL_DIGEST_NAME_SHA1
},
139 { NID_sha224
, OSSL_DIGEST_NAME_SHA2_224
},
140 { NID_sha256
, OSSL_DIGEST_NAME_SHA2_256
},
141 { NID_sha384
, OSSL_DIGEST_NAME_SHA2_384
},
142 { NID_sha512
, OSSL_DIGEST_NAME_SHA2_512
},
143 { NID_sha3_224
, OSSL_DIGEST_NAME_SHA3_224
},
144 { NID_sha3_256
, OSSL_DIGEST_NAME_SHA3_256
},
145 { NID_sha3_384
, OSSL_DIGEST_NAME_SHA3_384
},
146 { NID_sha3_512
, OSSL_DIGEST_NAME_SHA3_512
},
149 int mdnid
= NID_undef
;
154 for (i
= 0; i
< OSSL_NELEM(name_to_nid
); i
++) {
155 if (EVP_MD_is_a(md
, name_to_nid
[i
].ptr
)) {
156 mdnid
= (int)name_to_nid
[i
].id
;
161 if (mdnid
== NID_undef
)
162 ERR_raise(ERR_LIB_PROV
, PROV_R_INVALID_DIGEST
);
168 static int dsa_digest_signverify_init(void *vpdsactx
, const char *mdname
,
169 const char *props
, void *vdsa
)
171 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
172 size_t algorithmidentifier_len
= 0;
173 const unsigned char *algorithmidentifier
;
175 EVP_MD_CTX_free(pdsactx
->mdctx
);
176 EVP_MD_free(pdsactx
->md
);
177 pdsactx
->mdctx
= NULL
;
181 if (!dsa_signature_init(vpdsactx
, vdsa
))
184 pdsactx
->md
= EVP_MD_fetch(pdsactx
->libctx
, mdname
, props
);
185 algorithmidentifier
=
186 dsa_algorithmidentifier_encoding(get_md_nid(pdsactx
->md
),
187 &algorithmidentifier_len
);
189 if (algorithmidentifier
== NULL
)
192 pdsactx
->mdsize
= EVP_MD_size(pdsactx
->md
);
193 pdsactx
->mdctx
= EVP_MD_CTX_new();
194 if (pdsactx
->mdctx
== NULL
)
197 memcpy(pdsactx
->aid
, algorithmidentifier
, algorithmidentifier_len
);
198 pdsactx
->aid_len
= algorithmidentifier_len
;
200 if (!EVP_DigestInit_ex(pdsactx
->mdctx
, pdsactx
->md
, NULL
))
206 EVP_MD_CTX_free(pdsactx
->mdctx
);
207 EVP_MD_free(pdsactx
->md
);
208 pdsactx
->mdctx
= NULL
;
213 int dsa_digest_signverify_update(void *vpdsactx
, const unsigned char *data
,
216 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
218 if (pdsactx
== NULL
|| pdsactx
->mdctx
== NULL
)
221 return EVP_DigestUpdate(pdsactx
->mdctx
, data
, datalen
);
224 int dsa_digest_sign_final(void *vpdsactx
, unsigned char *sig
, size_t *siglen
,
227 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
228 unsigned char digest
[EVP_MAX_MD_SIZE
];
229 unsigned int dlen
= 0;
231 if (pdsactx
== NULL
|| pdsactx
->mdctx
== NULL
)
235 * If sig is NULL then we're just finding out the sig size. Other fields
236 * are ignored. Defer to dsa_sign.
240 * TODO(3.0): There is the possibility that some externally provided
241 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
242 * but that problem is much larger than just in DSA.
244 if (!EVP_DigestFinal_ex(pdsactx
->mdctx
, digest
, &dlen
))
248 return dsa_sign(vpdsactx
, sig
, siglen
, sigsize
, digest
, (size_t)dlen
);
252 int dsa_digest_verify_final(void *vpdsactx
, const unsigned char *sig
,
255 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
256 unsigned char digest
[EVP_MAX_MD_SIZE
];
257 unsigned int dlen
= 0;
259 if (pdsactx
== NULL
|| pdsactx
->mdctx
== NULL
)
263 * TODO(3.0): There is the possibility that some externally provided
264 * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
265 * but that problem is much larger than just in DSA.
267 if (!EVP_DigestFinal_ex(pdsactx
->mdctx
, digest
, &dlen
))
270 return dsa_verify(vpdsactx
, sig
, siglen
, digest
, (size_t)dlen
);
273 static void dsa_freectx(void *vpdsactx
)
275 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
277 DSA_free(pdsactx
->dsa
);
278 EVP_MD_CTX_free(pdsactx
->mdctx
);
279 EVP_MD_free(pdsactx
->md
);
281 OPENSSL_free(pdsactx
);
284 static void *dsa_dupctx(void *vpdsactx
)
286 PROV_DSA_CTX
*srcctx
= (PROV_DSA_CTX
*)vpdsactx
;
287 PROV_DSA_CTX
*dstctx
;
289 dstctx
= OPENSSL_zalloc(sizeof(*srcctx
));
296 dstctx
->mdctx
= NULL
;
298 if (srcctx
->dsa
!= NULL
&& !DSA_up_ref(srcctx
->dsa
))
300 dstctx
->dsa
= srcctx
->dsa
;
302 if (srcctx
->md
!= NULL
&& !EVP_MD_up_ref(srcctx
->md
))
304 dstctx
->md
= srcctx
->md
;
306 if (srcctx
->mdctx
!= NULL
) {
307 dstctx
->mdctx
= EVP_MD_CTX_new();
308 if (dstctx
->mdctx
== NULL
309 || !EVP_MD_CTX_copy_ex(dstctx
->mdctx
, srcctx
->mdctx
))
319 static int dsa_get_ctx_params(void *vpdsactx
, OSSL_PARAM
*params
)
321 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
324 if (pdsactx
== NULL
|| params
== NULL
)
327 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_ALGORITHM_ID
);
329 && !OSSL_PARAM_set_octet_string(p
, pdsactx
->aid
, pdsactx
->aid_len
))
332 p
= OSSL_PARAM_locate(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
333 if (p
!= NULL
&& !OSSL_PARAM_set_utf8_string(p
, pdsactx
->md
== NULL
335 : EVP_MD_name(pdsactx
->md
)))
341 static const OSSL_PARAM known_gettable_ctx_params
[] = {
342 OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID
, NULL
, 0),
343 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
347 static const OSSL_PARAM
*dsa_gettable_ctx_params(void)
349 return known_gettable_ctx_params
;
352 static int dsa_set_ctx_params(void *vpdsactx
, const OSSL_PARAM params
[])
354 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
358 if (pdsactx
== NULL
|| params
== NULL
)
361 if (pdsactx
->md
!= NULL
) {
363 * You cannot set the digest name/size when doing a DigestSign or
370 * We never actually use the mdname, but we do support getting it later.
371 * This can be useful for applications that want to know the MD that they
374 p
= OSSL_PARAM_locate_const(params
, OSSL_SIGNATURE_PARAM_DIGEST
);
375 mdname
= pdsactx
->mdname
;
377 && !OSSL_PARAM_get_utf8_string(p
, &mdname
, sizeof(pdsactx
->mdname
)))
383 static const OSSL_PARAM known_settable_ctx_params
[] = {
384 OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST
, NULL
, 0),
388 static const OSSL_PARAM
*dsa_settable_ctx_params(void)
391 * TODO(3.0): Should this function return a different set of settable ctx
392 * params if the ctx is being used for a DigestSign/DigestVerify? In that
393 * case it is not allowed to set the digest size/digest name because the
394 * digest is explicitly set as part of the init.
396 return known_settable_ctx_params
;
399 static int dsa_get_ctx_md_params(void *vpdsactx
, OSSL_PARAM
*params
)
401 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
403 if (pdsactx
->mdctx
== NULL
)
406 return EVP_MD_CTX_get_params(pdsactx
->mdctx
, params
);
409 static const OSSL_PARAM
*dsa_gettable_ctx_md_params(void *vpdsactx
)
411 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
413 if (pdsactx
->md
== NULL
)
416 return EVP_MD_gettable_ctx_params(pdsactx
->md
);
419 static int dsa_set_ctx_md_params(void *vpdsactx
, const OSSL_PARAM params
[])
421 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
423 if (pdsactx
->mdctx
== NULL
)
426 return EVP_MD_CTX_set_params(pdsactx
->mdctx
, params
);
429 static const OSSL_PARAM
*dsa_settable_ctx_md_params(void *vpdsactx
)
431 PROV_DSA_CTX
*pdsactx
= (PROV_DSA_CTX
*)vpdsactx
;
433 if (pdsactx
->md
== NULL
)
436 return EVP_MD_settable_ctx_params(pdsactx
->md
);
439 const OSSL_DISPATCH dsa_signature_functions
[] = {
440 { OSSL_FUNC_SIGNATURE_NEWCTX
, (void (*)(void))dsa_newctx
},
441 { OSSL_FUNC_SIGNATURE_SIGN_INIT
, (void (*)(void))dsa_signature_init
},
442 { OSSL_FUNC_SIGNATURE_SIGN
, (void (*)(void))dsa_sign
},
443 { OSSL_FUNC_SIGNATURE_VERIFY_INIT
, (void (*)(void))dsa_signature_init
},
444 { OSSL_FUNC_SIGNATURE_VERIFY
, (void (*)(void))dsa_verify
},
445 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT
,
446 (void (*)(void))dsa_digest_signverify_init
},
447 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE
,
448 (void (*)(void))dsa_digest_signverify_update
},
449 { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL
,
450 (void (*)(void))dsa_digest_sign_final
},
451 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT
,
452 (void (*)(void))dsa_digest_signverify_init
},
453 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE
,
454 (void (*)(void))dsa_digest_signverify_update
},
455 { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL
,
456 (void (*)(void))dsa_digest_verify_final
},
457 { OSSL_FUNC_SIGNATURE_FREECTX
, (void (*)(void))dsa_freectx
},
458 { OSSL_FUNC_SIGNATURE_DUPCTX
, (void (*)(void))dsa_dupctx
},
459 { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS
, (void (*)(void))dsa_get_ctx_params
},
460 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS
,
461 (void (*)(void))dsa_gettable_ctx_params
},
462 { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS
, (void (*)(void))dsa_set_ctx_params
},
463 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS
,
464 (void (*)(void))dsa_settable_ctx_params
},
465 { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS
,
466 (void (*)(void))dsa_get_ctx_md_params
},
467 { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS
,
468 (void (*)(void))dsa_gettable_ctx_md_params
},
469 { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS
,
470 (void (*)(void))dsa_set_ctx_md_params
},
471 { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS
,
472 (void (*)(void))dsa_settable_ctx_md_params
},