/src/openssl/providers/implementations/signature/dsa_sig.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2019-2024 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
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  | 
8  |  |  */  | 
9  |  |  | 
10  |  | /*  | 
11  |  |  * DSA low level APIs are deprecated for public use, but still ok for  | 
12  |  |  * internal use.  | 
13  |  |  */  | 
14  |  | #include "internal/deprecated.h"  | 
15  |  |  | 
16  |  | #include <string.h>  | 
17  |  |  | 
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/proverr.h>  | 
26  |  | #include "internal/nelem.h"  | 
27  |  | #include "internal/sizes.h"  | 
28  |  | #include "internal/cryptlib.h"  | 
29  |  | #include "prov/providercommon.h"  | 
30  |  | #include "prov/implementations.h"  | 
31  |  | #include "prov/provider_ctx.h"  | 
32  |  | #include "prov/securitycheck.h"  | 
33  |  | #include "prov/der_dsa.h"  | 
34  |  | #include "crypto/dsa.h"  | 
35  |  |  | 
36  |  | static OSSL_FUNC_signature_newctx_fn dsa_newctx;  | 
37  |  | static OSSL_FUNC_signature_sign_init_fn dsa_sign_init;  | 
38  |  | static OSSL_FUNC_signature_verify_init_fn dsa_verify_init;  | 
39  |  | static OSSL_FUNC_signature_sign_fn dsa_sign;  | 
40  |  | static OSSL_FUNC_signature_sign_message_update_fn dsa_signverify_message_update;  | 
41  |  | static OSSL_FUNC_signature_sign_message_final_fn dsa_sign_message_final;  | 
42  |  | static OSSL_FUNC_signature_verify_fn dsa_verify;  | 
43  |  | static OSSL_FUNC_signature_verify_message_update_fn dsa_signverify_message_update;  | 
44  |  | static OSSL_FUNC_signature_verify_message_final_fn dsa_verify_message_final;  | 
45  |  | static OSSL_FUNC_signature_digest_sign_init_fn dsa_digest_sign_init;  | 
46  |  | static OSSL_FUNC_signature_digest_sign_update_fn dsa_digest_signverify_update;  | 
47  |  | static OSSL_FUNC_signature_digest_sign_final_fn dsa_digest_sign_final;  | 
48  |  | static OSSL_FUNC_signature_digest_verify_init_fn dsa_digest_verify_init;  | 
49  |  | static OSSL_FUNC_signature_digest_verify_update_fn dsa_digest_signverify_update;  | 
50  |  | static OSSL_FUNC_signature_digest_verify_final_fn dsa_digest_verify_final;  | 
51  |  | static OSSL_FUNC_signature_freectx_fn dsa_freectx;  | 
52  |  | static OSSL_FUNC_signature_dupctx_fn dsa_dupctx;  | 
53  |  | static OSSL_FUNC_signature_query_key_types_fn dsa_sigalg_query_key_types;  | 
54  |  | static OSSL_FUNC_signature_get_ctx_params_fn dsa_get_ctx_params;  | 
55  |  | static OSSL_FUNC_signature_gettable_ctx_params_fn dsa_gettable_ctx_params;  | 
56  |  | static OSSL_FUNC_signature_set_ctx_params_fn dsa_set_ctx_params;  | 
57  |  | static OSSL_FUNC_signature_settable_ctx_params_fn dsa_settable_ctx_params;  | 
58  |  | static OSSL_FUNC_signature_get_ctx_md_params_fn dsa_get_ctx_md_params;  | 
59  |  | static OSSL_FUNC_signature_gettable_ctx_md_params_fn dsa_gettable_ctx_md_params;  | 
60  |  | static OSSL_FUNC_signature_set_ctx_md_params_fn dsa_set_ctx_md_params;  | 
61  |  | static OSSL_FUNC_signature_settable_ctx_md_params_fn dsa_settable_ctx_md_params;  | 
62  |  | static OSSL_FUNC_signature_set_ctx_params_fn dsa_sigalg_set_ctx_params;  | 
63  |  | static OSSL_FUNC_signature_settable_ctx_params_fn dsa_sigalg_settable_ctx_params;  | 
64  |  |  | 
65  |  | /*  | 
66  |  |  * What's passed as an actual key is defined by the KEYMGMT interface.  | 
67  |  |  * We happen to know that our KEYMGMT simply passes DSA structures, so  | 
68  |  |  * we use that here too.  | 
69  |  |  */  | 
70  |  |  | 
71  |  | typedef struct { | 
72  |  |     OSSL_LIB_CTX *libctx;  | 
73  |  |     char *propq;  | 
74  |  |     DSA *dsa;  | 
75  |  |     /* |operation| reuses EVP's operation bitfield */  | 
76  |  |     int operation;  | 
77  |  |  | 
78  |  |     /*  | 
79  |  |      * Flag to determine if a full sigalg is run (1) or if a composable  | 
80  |  |      * signature algorithm is run (0).  | 
81  |  |      *  | 
82  |  |      * When a full sigalg is run (1), this currently affects the following  | 
83  |  |      * other flags, which are to remain untouched after their initialization:  | 
84  |  |      *  | 
85  |  |      * - flag_allow_md (initialized to 0)  | 
86  |  |      */  | 
87  |  |     unsigned int flag_sigalg : 1;  | 
88  |  |     /*  | 
89  |  |      * Flag to determine if the hash function can be changed (1) or not (0)  | 
90  |  |      * Because it's dangerous to change during a DigestSign or DigestVerify  | 
91  |  |      * operation, this flag is cleared by their Init function, and set again  | 
92  |  |      * by their Final function.  | 
93  |  |      */  | 
94  |  |     unsigned int flag_allow_md : 1;  | 
95  |  |  | 
96  |  |     /* If this is set to 1 then the generated k is not random */  | 
97  |  |     unsigned int nonce_type;  | 
98  |  |  | 
99  |  |     /* The Algorithm Identifier of the combined signature algorithm */  | 
100  |  |     unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];  | 
101  |  |     size_t  aid_len;  | 
102  |  |  | 
103  |  |     /* main digest */  | 
104  |  |     char mdname[OSSL_MAX_NAME_SIZE];  | 
105  |  |     EVP_MD *md;  | 
106  |  |     EVP_MD_CTX *mdctx;  | 
107  |  |  | 
108  |  |     /* Signature, for verification */  | 
109  |  |     unsigned char *sig;  | 
110  |  |     size_t siglen;  | 
111  |  |  | 
112  |  |     OSSL_FIPS_IND_DECLARE  | 
113  |  | } PROV_DSA_CTX;  | 
114  |  |  | 
115  |  | static size_t dsa_get_md_size(const PROV_DSA_CTX *pdsactx)  | 
116  | 0  | { | 
117  | 0  |     int md_size;  | 
118  |  | 
  | 
119  | 0  |     if (pdsactx->md != NULL) { | 
120  | 0  |         md_size = EVP_MD_get_size(pdsactx->md);  | 
121  | 0  |         if (md_size <= 0)  | 
122  | 0  |             return 0;  | 
123  | 0  |         return (size_t)md_size;  | 
124  | 0  |     }  | 
125  | 0  |     return 0;  | 
126  | 0  | }  | 
127  |  |  | 
128  |  | static void *dsa_newctx(void *provctx, const char *propq)  | 
129  | 0  | { | 
130  | 0  |     PROV_DSA_CTX *pdsactx;  | 
131  |  | 
  | 
132  | 0  |     if (!ossl_prov_is_running())  | 
133  | 0  |         return NULL;  | 
134  |  |  | 
135  | 0  |     pdsactx = OPENSSL_zalloc(sizeof(PROV_DSA_CTX));  | 
136  | 0  |     if (pdsactx == NULL)  | 
137  | 0  |         return NULL;  | 
138  |  |  | 
139  | 0  |     pdsactx->libctx = PROV_LIBCTX_OF(provctx);  | 
140  | 0  |     pdsactx->flag_allow_md = 1;  | 
141  | 0  |     OSSL_FIPS_IND_INIT(pdsactx)  | 
142  | 0  |     if (propq != NULL && (pdsactx->propq = OPENSSL_strdup(propq)) == NULL) { | 
143  | 0  |         OPENSSL_free(pdsactx);  | 
144  | 0  |         pdsactx = NULL;  | 
145  | 0  |     }  | 
146  | 0  |     return pdsactx;  | 
147  | 0  | }  | 
148  |  |  | 
149  |  | static int dsa_setup_md(PROV_DSA_CTX *ctx,  | 
150  |  |                         const char *mdname, const char *mdprops,  | 
151  |  |                         const char *desc)  | 
152  | 0  | { | 
153  | 0  |     EVP_MD *md = NULL;  | 
154  |  | 
  | 
155  | 0  |     if (mdprops == NULL)  | 
156  | 0  |         mdprops = ctx->propq;  | 
157  |  | 
  | 
158  | 0  |     if (mdname != NULL) { | 
159  | 0  |         WPACKET pkt;  | 
160  | 0  |         int md_nid;  | 
161  | 0  |         size_t mdname_len = strlen(mdname);  | 
162  | 0  |         unsigned char *aid = NULL;  | 
163  |  | 
  | 
164  | 0  |         md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);  | 
165  | 0  |         md_nid = ossl_digest_get_approved_nid(md);  | 
166  |  | 
  | 
167  | 0  |         if (md == NULL) { | 
168  | 0  |             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,  | 
169  | 0  |                            "%s could not be fetched", mdname);  | 
170  | 0  |             goto err;  | 
171  | 0  |         }  | 
172  | 0  |         if (md_nid == NID_undef) { | 
173  | 0  |             ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,  | 
174  | 0  |                            "digest=%s", mdname);  | 
175  | 0  |             goto err;  | 
176  | 0  |         }  | 
177  | 0  |         if (mdname_len >= sizeof(ctx->mdname)) { | 
178  | 0  |             ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,  | 
179  | 0  |                            "%s exceeds name buffer length", mdname);  | 
180  | 0  |             goto err;  | 
181  | 0  |         }  | 
182  |  |         /* XOF digests don't work */  | 
183  | 0  |         if (EVP_MD_xof(md)) { | 
184  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_XOF_DIGESTS_NOT_ALLOWED);  | 
185  | 0  |             goto err;  | 
186  | 0  |         }  | 
187  |  | #ifdef FIPS_MODULE  | 
188  |  |         { | 
189  |  |             int sha1_allowed  | 
190  |  |                 = ((ctx->operation  | 
191  |  |                     & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG)) == 0);  | 
192  |  |  | 
193  |  |             if (!ossl_fips_ind_digest_sign_check(OSSL_FIPS_IND_GET(ctx),  | 
194  |  |                                                  OSSL_FIPS_IND_SETTABLE1,  | 
195  |  |                                                  ctx->libctx,  | 
196  |  |                                                  md_nid, sha1_allowed, desc,  | 
197  |  |                                                  ossl_fips_config_signature_digest_check))  | 
198  |  |                 goto err;  | 
199  |  |         }  | 
200  |  | #endif  | 
201  |  |  | 
202  | 0  |         if (!ctx->flag_allow_md) { | 
203  | 0  |             if (ctx->mdname[0] != '\0'  | 
204  | 0  |                 && !EVP_MD_is_a(md, ctx->mdname)) { | 
205  | 0  |                 ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,  | 
206  | 0  |                                "digest %s != %s", mdname, ctx->mdname);  | 
207  | 0  |                 goto err;  | 
208  | 0  |             }  | 
209  | 0  |             EVP_MD_free(md);  | 
210  | 0  |             return 1;  | 
211  | 0  |         }  | 
212  |  |  | 
213  | 0  |         EVP_MD_CTX_free(ctx->mdctx);  | 
214  | 0  |         EVP_MD_free(ctx->md);  | 
215  |  |  | 
216  |  |         /*  | 
217  |  |          * We do not care about DER writing errors.  | 
218  |  |          * All it really means is that for some reason, there's no  | 
219  |  |          * AlgorithmIdentifier to be had, but the operation itself is  | 
220  |  |          * still valid, just as long as it's not used to construct  | 
221  |  |          * anything that needs an AlgorithmIdentifier.  | 
222  |  |          */  | 
223  | 0  |         ctx->aid_len = 0;  | 
224  | 0  |         if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))  | 
225  | 0  |             && ossl_DER_w_algorithmIdentifier_DSA_with_MD(&pkt, -1, ctx->dsa,  | 
226  | 0  |                                                           md_nid)  | 
227  | 0  |             && WPACKET_finish(&pkt)) { | 
228  | 0  |             WPACKET_get_total_written(&pkt, &ctx->aid_len);  | 
229  | 0  |             aid = WPACKET_get_curr(&pkt);  | 
230  | 0  |         }  | 
231  | 0  |         WPACKET_cleanup(&pkt);  | 
232  | 0  |         if (aid != NULL && ctx->aid_len != 0)  | 
233  | 0  |             memmove(ctx->aid_buf, aid, ctx->aid_len);  | 
234  |  | 
  | 
235  | 0  |         ctx->mdctx = NULL;  | 
236  | 0  |         ctx->md = md;  | 
237  | 0  |         OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));  | 
238  | 0  |     }  | 
239  |  |  | 
240  | 0  |     return 1;  | 
241  | 0  |  err:  | 
242  | 0  |     EVP_MD_free(md);  | 
243  | 0  |     return 0;  | 
244  | 0  | }  | 
245  |  |  | 
246  |  | #ifdef FIPS_MODULE  | 
247  |  |  | 
248  |  | static int dsa_sign_check_approved(PROV_DSA_CTX *ctx, int signing,  | 
249  |  |                                    const char *desc)  | 
250  |  | { | 
251  |  |     /* DSA Signing is not approved in FIPS 140-3 */  | 
252  |  |     if (signing  | 
253  |  |         && !OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE2,  | 
254  |  |                                         ctx->libctx, desc, "DSA",  | 
255  |  |                                         ossl_fips_config_dsa_sign_disallowed))  | 
256  |  |         return 0;  | 
257  |  |     return 1;  | 
258  |  | }  | 
259  |  |  | 
260  |  | static int dsa_check_key(PROV_DSA_CTX *ctx, int sign, const char *desc)  | 
261  |  | { | 
262  |  |     int approved = ossl_dsa_check_key(ctx->dsa, sign);  | 
263  |  |  | 
264  |  |     if (!approved) { | 
265  |  |         if (!OSSL_FIPS_IND_ON_UNAPPROVED(ctx, OSSL_FIPS_IND_SETTABLE0,  | 
266  |  |                                          ctx->libctx, desc, "DSA Key",  | 
267  |  |                                          ossl_fips_config_signature_digest_check)) { | 
268  |  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
269  |  |             return 0;  | 
270  |  |         }  | 
271  |  |     }  | 
272  |  |     return 1;  | 
273  |  | }  | 
274  |  | #endif  | 
275  |  |  | 
276  |  | static int  | 
277  |  | dsa_signverify_init(void *vpdsactx, void *vdsa,  | 
278  |  |                     OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params,  | 
279  |  |                     const OSSL_PARAM params[], int operation,  | 
280  |  |                     const char *desc)  | 
281  | 0  | { | 
282  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
283  |  | 
  | 
284  | 0  |     if (!ossl_prov_is_running()  | 
285  | 0  |             || pdsactx == NULL)  | 
286  | 0  |         return 0;  | 
287  |  |  | 
288  | 0  |     if (vdsa == NULL && pdsactx->dsa == NULL) { | 
289  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);  | 
290  | 0  |         return 0;  | 
291  | 0  |     }  | 
292  |  |  | 
293  | 0  |     if (vdsa != NULL) { | 
294  | 0  |         if (!DSA_up_ref(vdsa))  | 
295  | 0  |             return 0;  | 
296  | 0  |         DSA_free(pdsactx->dsa);  | 
297  | 0  |         pdsactx->dsa = vdsa;  | 
298  | 0  |     }  | 
299  |  |  | 
300  | 0  |     pdsactx->operation = operation;  | 
301  |  | 
  | 
302  | 0  |     OSSL_FIPS_IND_SET_APPROVED(pdsactx)  | 
303  | 0  |     if (!set_ctx_params(pdsactx, params))  | 
304  | 0  |         return 0;  | 
305  |  | #ifdef FIPS_MODULE  | 
306  |  |     { | 
307  |  |         int operation_is_sign  | 
308  |  |             = (operation & (EVP_PKEY_OP_SIGN | EVP_PKEY_OP_SIGNMSG)) != 0;  | 
309  |  |  | 
310  |  |         if (!dsa_sign_check_approved(pdsactx, operation_is_sign, desc))  | 
311  |  |             return 0;  | 
312  |  |         if (!dsa_check_key(pdsactx, operation_is_sign, desc))  | 
313  |  |             return 0;  | 
314  |  |     }  | 
315  |  | #endif  | 
316  | 0  |     return 1;  | 
317  | 0  | }  | 
318  |  |  | 
319  |  | static int dsa_sign_init(void *vpdsactx, void *vdsa, const OSSL_PARAM params[])  | 
320  | 0  | { | 
321  | 0  |     return dsa_signverify_init(vpdsactx, vdsa, dsa_set_ctx_params, params,  | 
322  | 0  |                                EVP_PKEY_OP_SIGN, "DSA Sign Init");  | 
323  | 0  | }  | 
324  |  |  | 
325  |  | /*  | 
326  |  |  * Sign tbs without digesting it first.  This is suitable for "primitive"  | 
327  |  |  * signing and signing the digest of a message, i.e. should be used with  | 
328  |  |  * implementations of the keytype related algorithms.  | 
329  |  |  */  | 
330  |  | static int dsa_sign_directly(void *vpdsactx,  | 
331  |  |                              unsigned char *sig, size_t *siglen, size_t sigsize,  | 
332  |  |                              const unsigned char *tbs, size_t tbslen)  | 
333  | 0  | { | 
334  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
335  | 0  |     int ret;  | 
336  | 0  |     unsigned int sltmp;  | 
337  | 0  |     size_t dsasize = DSA_size(pdsactx->dsa);  | 
338  | 0  |     size_t mdsize = dsa_get_md_size(pdsactx);  | 
339  |  | 
  | 
340  | 0  |     if (!ossl_prov_is_running())  | 
341  | 0  |         return 0;  | 
342  |  |  | 
343  |  | #ifdef FIPS_MODULE  | 
344  |  |     if (!dsa_sign_check_approved(pdsactx, 1, "Sign"))  | 
345  |  |         return 0;  | 
346  |  | #endif  | 
347  |  |  | 
348  | 0  |     if (sig == NULL) { | 
349  | 0  |         *siglen = dsasize;  | 
350  | 0  |         return 1;  | 
351  | 0  |     }  | 
352  |  |  | 
353  | 0  |     if (sigsize < dsasize)  | 
354  | 0  |         return 0;  | 
355  |  |  | 
356  | 0  |     if (mdsize != 0 && tbslen != mdsize)  | 
357  | 0  |         return 0;  | 
358  |  |  | 
359  | 0  |     ret = ossl_dsa_sign_int(0, tbs, tbslen, sig, &sltmp, pdsactx->dsa,  | 
360  | 0  |                             pdsactx->nonce_type, pdsactx->mdname,  | 
361  | 0  |                             pdsactx->libctx, pdsactx->propq);  | 
362  | 0  |     if (ret <= 0)  | 
363  | 0  |         return 0;  | 
364  |  |  | 
365  | 0  |     *siglen = sltmp;  | 
366  | 0  |     return 1;  | 
367  | 0  | }  | 
368  |  |  | 
369  |  | static int dsa_signverify_message_update(void *vpdsactx,  | 
370  |  |                                          const unsigned char *data,  | 
371  |  |                                          size_t datalen)  | 
372  | 0  | { | 
373  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
374  |  | 
  | 
375  | 0  |     if (pdsactx == NULL)  | 
376  | 0  |         return 0;  | 
377  |  |  | 
378  | 0  |     return EVP_DigestUpdate(pdsactx->mdctx, data, datalen);  | 
379  | 0  | }  | 
380  |  |  | 
381  |  | static int dsa_sign_message_final(void *vpdsactx, unsigned char *sig,  | 
382  |  |                                   size_t *siglen, size_t sigsize)  | 
383  | 0  | { | 
384  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
385  | 0  |     unsigned char digest[EVP_MAX_MD_SIZE];  | 
386  | 0  |     unsigned int dlen = 0;  | 
387  |  | 
  | 
388  | 0  |     if (!ossl_prov_is_running() || pdsactx == NULL || pdsactx->mdctx == NULL)  | 
389  | 0  |         return 0;  | 
390  |  |     /*  | 
391  |  |      * If sig is NULL then we're just finding out the sig size. Other fields  | 
392  |  |      * are ignored. Defer to dsa_sign.  | 
393  |  |      */  | 
394  | 0  |     if (sig != NULL) { | 
395  |  |         /*  | 
396  |  |          * When this function is used through dsa_digest_sign_final(),  | 
397  |  |          * there is the possibility that some externally provided digests  | 
398  |  |          * exceed EVP_MAX_MD_SIZE. We should probably handle that  | 
399  |  |          * somehow but that problem is much larger than just in DSA.  | 
400  |  |          */  | 
401  | 0  |         if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))  | 
402  | 0  |             return 0;  | 
403  | 0  |     }  | 
404  |  |  | 
405  | 0  |     return dsa_sign_directly(vpdsactx, sig, siglen, sigsize, digest, dlen);  | 
406  | 0  | }  | 
407  |  |  | 
408  |  | /*  | 
409  |  |  * If signing a message, digest tbs and sign the result.  | 
410  |  |  * Otherwise, sign tbs directly.  | 
411  |  |  */  | 
412  |  | static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen,  | 
413  |  |                     size_t sigsize, const unsigned char *tbs, size_t tbslen)  | 
414  | 0  | { | 
415  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
416  |  | 
  | 
417  | 0  |     if (pdsactx->operation == EVP_PKEY_OP_SIGNMSG) { | 
418  |  |         /*  | 
419  |  |          * If |sig| is NULL, the caller is only looking for the sig length.  | 
420  |  |          * DO NOT update the input in this case.  | 
421  |  |          */  | 
422  | 0  |         if (sig == NULL)  | 
423  | 0  |             return dsa_sign_message_final(pdsactx, sig, siglen, sigsize);  | 
424  |  |  | 
425  | 0  |         if (dsa_signverify_message_update(pdsactx, tbs, tbslen) <= 0)  | 
426  | 0  |             return 0;  | 
427  | 0  |         return dsa_sign_message_final(pdsactx, sig, siglen, sigsize);  | 
428  | 0  |     }  | 
429  | 0  |     return dsa_sign_directly(pdsactx, sig, siglen, sigsize, tbs, tbslen);  | 
430  | 0  | }  | 
431  |  |  | 
432  |  | static int dsa_verify_init(void *vpdsactx, void *vdsa,  | 
433  |  |                            const OSSL_PARAM params[])  | 
434  | 0  | { | 
435  | 0  |     return dsa_signverify_init(vpdsactx, vdsa, dsa_set_ctx_params, params,  | 
436  | 0  |                                EVP_PKEY_OP_VERIFY, "DSA Verify Init");  | 
437  | 0  | }  | 
438  |  |  | 
439  |  | static int dsa_verify_directly(void *vpdsactx,  | 
440  |  |                                const unsigned char *sig, size_t siglen,  | 
441  |  |                                const unsigned char *tbs, size_t tbslen)  | 
442  | 0  | { | 
443  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
444  | 0  |     size_t mdsize = dsa_get_md_size(pdsactx);  | 
445  |  | 
  | 
446  | 0  |     if (!ossl_prov_is_running() || (mdsize != 0 && tbslen != mdsize))  | 
447  | 0  |         return 0;  | 
448  |  |  | 
449  | 0  |     return DSA_verify(0, tbs, tbslen, sig, siglen, pdsactx->dsa);  | 
450  | 0  | }  | 
451  |  |  | 
452  |  | static int dsa_verify_set_sig(void *vpdsactx,  | 
453  |  |                               const unsigned char *sig, size_t siglen)  | 
454  | 0  | { | 
455  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
456  | 0  |     OSSL_PARAM params[2];  | 
457  |  | 
  | 
458  | 0  |     params[0] =  | 
459  | 0  |         OSSL_PARAM_construct_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE,  | 
460  | 0  |                                           (unsigned char *)sig, siglen);  | 
461  | 0  |     params[1] = OSSL_PARAM_construct_end();  | 
462  | 0  |     return dsa_sigalg_set_ctx_params(pdsactx, params);  | 
463  | 0  | }  | 
464  |  |  | 
465  |  | static int dsa_verify_message_final(void *vpdsactx)  | 
466  | 0  | { | 
467  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
468  | 0  |     unsigned char digest[EVP_MAX_MD_SIZE];  | 
469  | 0  |     unsigned int dlen = 0;  | 
470  |  | 
  | 
471  | 0  |     if (!ossl_prov_is_running())  | 
472  | 0  |         return 0;  | 
473  |  |  | 
474  | 0  |     if (pdsactx == NULL || pdsactx->mdctx == NULL)  | 
475  | 0  |         return 0;  | 
476  |  |  | 
477  |  |     /*  | 
478  |  |      * The digests used here are all known (see dsa_get_md_nid()), so they  | 
479  |  |      * should not exceed the internal buffer size of EVP_MAX_MD_SIZE.  | 
480  |  |      */  | 
481  | 0  |     if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))  | 
482  | 0  |         return 0;  | 
483  |  |  | 
484  | 0  |     return dsa_verify_directly(vpdsactx, pdsactx->sig, pdsactx->siglen,  | 
485  | 0  |                                digest, dlen);  | 
486  | 0  | }  | 
487  |  |  | 
488  |  | /*  | 
489  |  |  * If verifying a message, digest tbs and verify the result.  | 
490  |  |  * Otherwise, verify tbs directly.  | 
491  |  |  */  | 
492  |  | static int dsa_verify(void *vpdsactx,  | 
493  |  |                       const unsigned char *sig, size_t siglen,  | 
494  |  |                       const unsigned char *tbs, size_t tbslen)  | 
495  | 0  | { | 
496  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
497  |  | 
  | 
498  | 0  |     if (pdsactx->operation == EVP_PKEY_OP_VERIFYMSG) { | 
499  | 0  |         if (dsa_verify_set_sig(pdsactx, sig, siglen) <= 0)  | 
500  | 0  |             return 0;  | 
501  | 0  |         if (dsa_signverify_message_update(pdsactx, tbs, tbslen) <= 0)  | 
502  | 0  |             return 0;  | 
503  | 0  |         return dsa_verify_message_final(pdsactx);  | 
504  | 0  |     }  | 
505  | 0  |     return dsa_verify_directly(pdsactx, sig, siglen, tbs, tbslen);  | 
506  | 0  | }  | 
507  |  |  | 
508  |  | /* DigestSign/DigestVerify wrappers */  | 
509  |  |  | 
510  |  | static int dsa_digest_signverify_init(void *vpdsactx, const char *mdname,  | 
511  |  |                                       void *vdsa, const OSSL_PARAM params[],  | 
512  |  |                                       int operation, const char *desc)  | 
513  | 0  | { | 
514  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
515  |  | 
  | 
516  | 0  |     if (!ossl_prov_is_running())  | 
517  | 0  |         return 0;  | 
518  |  |  | 
519  | 0  |     if (!dsa_signverify_init(vpdsactx, vdsa, dsa_set_ctx_params, params,  | 
520  | 0  |                              operation, desc))  | 
521  | 0  |         return 0;  | 
522  |  |  | 
523  | 0  |     if (mdname != NULL  | 
524  |  |         /* was dsa_setup_md already called in dsa_signverify_init()? */  | 
525  | 0  |         && (mdname[0] == '\0' || OPENSSL_strcasecmp(pdsactx->mdname, mdname) != 0)  | 
526  | 0  |         && !dsa_setup_md(pdsactx, mdname, NULL, desc))  | 
527  | 0  |         return 0;  | 
528  |  |  | 
529  | 0  |     pdsactx->flag_allow_md = 0;  | 
530  |  | 
  | 
531  | 0  |     if (pdsactx->mdctx == NULL) { | 
532  | 0  |         pdsactx->mdctx = EVP_MD_CTX_new();  | 
533  | 0  |         if (pdsactx->mdctx == NULL)  | 
534  | 0  |             goto error;  | 
535  | 0  |     }  | 
536  |  |  | 
537  | 0  |     if (!EVP_DigestInit_ex2(pdsactx->mdctx, pdsactx->md, params))  | 
538  | 0  |         goto error;  | 
539  |  |  | 
540  | 0  |     return 1;  | 
541  |  |  | 
542  | 0  |  error:  | 
543  | 0  |     EVP_MD_CTX_free(pdsactx->mdctx);  | 
544  | 0  |     pdsactx->mdctx = NULL;  | 
545  | 0  |     return 0;  | 
546  | 0  | }  | 
547  |  |  | 
548  |  | static int dsa_digest_sign_init(void *vpdsactx, const char *mdname,  | 
549  |  |                                 void *vdsa, const OSSL_PARAM params[])  | 
550  | 0  | { | 
551  | 0  |     return dsa_digest_signverify_init(vpdsactx, mdname, vdsa, params,  | 
552  | 0  |                                       EVP_PKEY_OP_SIGNMSG,  | 
553  | 0  |                                       "DSA Digest Sign Init");  | 
554  | 0  | }  | 
555  |  |  | 
556  |  | static int dsa_digest_signverify_update(void *vpdsactx, const unsigned char *data,  | 
557  |  |                                         size_t datalen)  | 
558  | 0  | { | 
559  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
560  |  | 
  | 
561  | 0  |     if (pdsactx == NULL)  | 
562  | 0  |         return 0;  | 
563  |  |     /* Sigalg implementations shouldn't do digest_sign */  | 
564  | 0  |     if (pdsactx->flag_sigalg)  | 
565  | 0  |         return 0;  | 
566  |  |  | 
567  | 0  |     return dsa_signverify_message_update(vpdsactx, data, datalen);  | 
568  | 0  | }  | 
569  |  |  | 
570  |  | static int dsa_digest_sign_final(void *vpdsactx, unsigned char *sig,  | 
571  |  |                                  size_t *siglen, size_t sigsize)  | 
572  | 0  | { | 
573  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
574  | 0  |     int ok = 0;  | 
575  |  | 
  | 
576  | 0  |     if (pdsactx == NULL)  | 
577  | 0  |         return 0;  | 
578  |  |     /* Sigalg implementations shouldn't do digest_sign */  | 
579  | 0  |     if (pdsactx->flag_sigalg)  | 
580  | 0  |         return 0;  | 
581  |  |  | 
582  | 0  |     ok = dsa_sign_message_final(pdsactx, sig, siglen, sigsize);  | 
583  |  | 
  | 
584  | 0  |     pdsactx->flag_allow_md = 1;  | 
585  |  | 
  | 
586  | 0  |     return ok;  | 
587  | 0  | }  | 
588  |  |  | 
589  |  | static int dsa_digest_verify_init(void *vpdsactx, const char *mdname,  | 
590  |  |                                   void *vdsa, const OSSL_PARAM params[])  | 
591  | 0  | { | 
592  | 0  |     return dsa_digest_signverify_init(vpdsactx, mdname, vdsa, params,  | 
593  | 0  |                                       EVP_PKEY_OP_VERIFYMSG,  | 
594  | 0  |                                       "DSA Digest Verify Init");  | 
595  | 0  | }  | 
596  |  |  | 
597  |  | int dsa_digest_verify_final(void *vpdsactx, const unsigned char *sig,  | 
598  |  |                             size_t siglen)  | 
599  | 0  | { | 
600  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
601  | 0  |     int ok = 0;  | 
602  |  | 
  | 
603  | 0  |     if (pdsactx == NULL)  | 
604  | 0  |         return 0;  | 
605  |  |     /* Sigalg implementations shouldn't do digest_verify */  | 
606  | 0  |     if (pdsactx->flag_sigalg)  | 
607  | 0  |         return 0;  | 
608  |  |  | 
609  | 0  |     if (dsa_verify_set_sig(pdsactx, sig, siglen))  | 
610  | 0  |         ok = dsa_verify_message_final(vpdsactx);  | 
611  |  | 
  | 
612  | 0  |     pdsactx->flag_allow_md = 1;  | 
613  |  | 
  | 
614  | 0  |     return ok;  | 
615  | 0  | }  | 
616  |  |  | 
617  |  | static void dsa_freectx(void *vpdsactx)  | 
618  | 0  | { | 
619  | 0  |     PROV_DSA_CTX *ctx = (PROV_DSA_CTX *)vpdsactx;  | 
620  |  | 
  | 
621  | 0  |     EVP_MD_CTX_free(ctx->mdctx);  | 
622  | 0  |     EVP_MD_free(ctx->md);  | 
623  | 0  |     OPENSSL_free(ctx->sig);  | 
624  | 0  |     OPENSSL_free(ctx->propq);  | 
625  | 0  |     DSA_free(ctx->dsa);  | 
626  | 0  |     OPENSSL_free(ctx);  | 
627  | 0  | }  | 
628  |  |  | 
629  |  | static void *dsa_dupctx(void *vpdsactx)  | 
630  | 0  | { | 
631  | 0  |     PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx;  | 
632  | 0  |     PROV_DSA_CTX *dstctx;  | 
633  |  | 
  | 
634  | 0  |     if (!ossl_prov_is_running())  | 
635  | 0  |         return NULL;  | 
636  |  |  | 
637  | 0  |     dstctx = OPENSSL_zalloc(sizeof(*srcctx));  | 
638  | 0  |     if (dstctx == NULL)  | 
639  | 0  |         return NULL;  | 
640  |  |  | 
641  | 0  |     *dstctx = *srcctx;  | 
642  | 0  |     dstctx->dsa = NULL;  | 
643  | 0  |     dstctx->propq = NULL;  | 
644  |  | 
  | 
645  | 0  |     if (srcctx->dsa != NULL && !DSA_up_ref(srcctx->dsa))  | 
646  | 0  |         goto err;  | 
647  | 0  |     dstctx->dsa = srcctx->dsa;  | 
648  |  | 
  | 
649  | 0  |     if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))  | 
650  | 0  |         goto err;  | 
651  | 0  |     dstctx->md = srcctx->md;  | 
652  |  | 
  | 
653  | 0  |     if (srcctx->mdctx != NULL) { | 
654  | 0  |         dstctx->mdctx = EVP_MD_CTX_new();  | 
655  | 0  |         if (dstctx->mdctx == NULL  | 
656  | 0  |                 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))  | 
657  | 0  |             goto err;  | 
658  | 0  |     }  | 
659  |  |  | 
660  | 0  |     if (srcctx->propq != NULL) { | 
661  | 0  |         dstctx->propq = OPENSSL_strdup(srcctx->propq);  | 
662  | 0  |         if (dstctx->propq == NULL)  | 
663  | 0  |             goto err;  | 
664  | 0  |     }  | 
665  |  |  | 
666  | 0  |     return dstctx;  | 
667  | 0  |  err:  | 
668  | 0  |     dsa_freectx(dstctx);  | 
669  | 0  |     return NULL;  | 
670  | 0  | }  | 
671  |  |  | 
672  |  | static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params)  | 
673  | 0  | { | 
674  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
675  | 0  |     OSSL_PARAM *p;  | 
676  |  | 
  | 
677  | 0  |     if (pdsactx == NULL)  | 
678  | 0  |         return 0;  | 
679  |  |  | 
680  | 0  |     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);  | 
681  | 0  |     if (p != NULL  | 
682  | 0  |         && !OSSL_PARAM_set_octet_string(p,  | 
683  | 0  |                                         pdsactx->aid_len == 0 ? NULL : pdsactx->aid_buf,  | 
684  | 0  |                                         pdsactx->aid_len))  | 
685  | 0  |         return 0;  | 
686  |  |  | 
687  | 0  |     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);  | 
688  | 0  |     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pdsactx->mdname))  | 
689  | 0  |         return 0;  | 
690  |  |  | 
691  | 0  |     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_NONCE_TYPE);  | 
692  | 0  |     if (p != NULL && !OSSL_PARAM_set_uint(p, pdsactx->nonce_type))  | 
693  | 0  |         return 0;  | 
694  | 0  |     if (!OSSL_FIPS_IND_GET_CTX_PARAM(pdsactx, params))  | 
695  | 0  |         return 0;  | 
696  |  |  | 
697  | 0  |     return 1;  | 
698  | 0  | }  | 
699  |  |  | 
700  |  | static const OSSL_PARAM known_gettable_ctx_params[] = { | 
701  |  |     OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),  | 
702  |  |     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),  | 
703  |  |     OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, NULL),  | 
704  |  |     OSSL_FIPS_IND_GETTABLE_CTX_PARAM()  | 
705  |  |     OSSL_PARAM_END  | 
706  |  | };  | 
707  |  |  | 
708  |  | static const OSSL_PARAM *dsa_gettable_ctx_params(ossl_unused void *ctx,  | 
709  |  |                                                  ossl_unused void *provctx)  | 
710  | 0  | { | 
711  | 0  |     return known_gettable_ctx_params;  | 
712  | 0  | }  | 
713  |  |  | 
714  |  | /**  | 
715  |  |  * @brief Setup common params for dsa_set_ctx_params and dsa_sigalg_set_ctx_params  | 
716  |  |  * The caller is responsible for checking |vpdsactx| is not NULL and |params|  | 
717  |  |  * is not empty.  | 
718  |  |  */  | 
719  |  | static int dsa_common_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])  | 
720  | 0  | { | 
721  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
722  | 0  |     const OSSL_PARAM *p;  | 
723  |  | 
  | 
724  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_PARAM(pdsactx, OSSL_FIPS_IND_SETTABLE0, params,  | 
725  | 0  |                                      OSSL_SIGNATURE_PARAM_FIPS_KEY_CHECK))  | 
726  | 0  |         return 0;  | 
727  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_PARAM(pdsactx, OSSL_FIPS_IND_SETTABLE1, params,  | 
728  | 0  |                                      OSSL_SIGNATURE_PARAM_FIPS_DIGEST_CHECK))  | 
729  | 0  |         return 0;  | 
730  | 0  |     if (!OSSL_FIPS_IND_SET_CTX_PARAM(pdsactx, OSSL_FIPS_IND_SETTABLE2, params,  | 
731  | 0  |                                      OSSL_SIGNATURE_PARAM_FIPS_SIGN_CHECK))  | 
732  | 0  |         return 0;  | 
733  |  |  | 
734  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_NONCE_TYPE);  | 
735  | 0  |     if (p != NULL  | 
736  | 0  |         && !OSSL_PARAM_get_uint(p, &pdsactx->nonce_type))  | 
737  | 0  |         return 0;  | 
738  | 0  |     return 1;  | 
739  | 0  | }  | 
740  |  |  | 
741  |  | #define DSA_COMMON_SETTABLE_CTX_PARAMS                                        \  | 
742  |  |     OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, NULL),                   \  | 
743  |  |     OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_SIGNATURE_PARAM_FIPS_KEY_CHECK)     \  | 
744  |  |     OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_SIGNATURE_PARAM_FIPS_DIGEST_CHECK)  \  | 
745  |  |     OSSL_FIPS_IND_SETTABLE_CTX_PARAM(OSSL_SIGNATURE_PARAM_FIPS_SIGN_CHECK)    \  | 
746  |  |     OSSL_PARAM_END  | 
747  |  |  | 
748  |  | static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])  | 
749  | 0  | { | 
750  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
751  | 0  |     const OSSL_PARAM *p;  | 
752  | 0  |     int ret;  | 
753  |  | 
  | 
754  | 0  |     if (pdsactx == NULL)  | 
755  | 0  |         return 0;  | 
756  | 0  |     if (ossl_param_is_empty(params))  | 
757  | 0  |         return 1;  | 
758  |  |  | 
759  | 0  |     if ((ret = dsa_common_set_ctx_params(pdsactx, params)) <= 0)  | 
760  | 0  |         return ret;  | 
761  |  |  | 
762  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);  | 
763  | 0  |     if (p != NULL) { | 
764  | 0  |         char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname;  | 
765  | 0  |         char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops;  | 
766  | 0  |         const OSSL_PARAM *propsp =  | 
767  | 0  |             OSSL_PARAM_locate_const(params,  | 
768  | 0  |                                     OSSL_SIGNATURE_PARAM_PROPERTIES);  | 
769  |  | 
  | 
770  | 0  |         if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))  | 
771  | 0  |             return 0;  | 
772  | 0  |         if (propsp != NULL  | 
773  | 0  |             && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops)))  | 
774  | 0  |             return 0;  | 
775  | 0  |         if (!dsa_setup_md(pdsactx, mdname, mdprops, "DSA Set Ctx"))  | 
776  | 0  |             return 0;  | 
777  | 0  |     }  | 
778  | 0  |     return 1;  | 
779  | 0  | }  | 
780  |  |  | 
781  |  | static const OSSL_PARAM settable_ctx_params[] = { | 
782  |  |     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),  | 
783  |  |     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),  | 
784  |  |     DSA_COMMON_SETTABLE_CTX_PARAMS  | 
785  |  | };  | 
786  |  |  | 
787  |  | static const OSSL_PARAM settable_ctx_params_no_digest[] = { | 
788  |  |     OSSL_PARAM_END  | 
789  |  | };  | 
790  |  |  | 
791  |  | static const OSSL_PARAM *dsa_settable_ctx_params(void *vpdsactx,  | 
792  |  |                                                  ossl_unused void *provctx)  | 
793  | 0  | { | 
794  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
795  |  | 
  | 
796  | 0  |     if (pdsactx != NULL && !pdsactx->flag_allow_md)  | 
797  | 0  |         return settable_ctx_params_no_digest;  | 
798  | 0  |     return settable_ctx_params;  | 
799  | 0  | }  | 
800  |  |  | 
801  |  | static int dsa_get_ctx_md_params(void *vpdsactx, OSSL_PARAM *params)  | 
802  | 0  | { | 
803  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
804  |  | 
  | 
805  | 0  |     if (pdsactx->mdctx == NULL)  | 
806  | 0  |         return 0;  | 
807  |  |  | 
808  | 0  |     return EVP_MD_CTX_get_params(pdsactx->mdctx, params);  | 
809  | 0  | }  | 
810  |  |  | 
811  |  | static const OSSL_PARAM *dsa_gettable_ctx_md_params(void *vpdsactx)  | 
812  | 0  | { | 
813  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
814  |  | 
  | 
815  | 0  |     if (pdsactx->md == NULL)  | 
816  | 0  |         return 0;  | 
817  |  |  | 
818  | 0  |     return EVP_MD_gettable_ctx_params(pdsactx->md);  | 
819  | 0  | }  | 
820  |  |  | 
821  |  | static int dsa_set_ctx_md_params(void *vpdsactx, const OSSL_PARAM params[])  | 
822  | 0  | { | 
823  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
824  |  | 
  | 
825  | 0  |     if (pdsactx->mdctx == NULL)  | 
826  | 0  |         return 0;  | 
827  |  |  | 
828  | 0  |     return EVP_MD_CTX_set_params(pdsactx->mdctx, params);  | 
829  | 0  | }  | 
830  |  |  | 
831  |  | static const OSSL_PARAM *dsa_settable_ctx_md_params(void *vpdsactx)  | 
832  | 0  | { | 
833  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
834  |  | 
  | 
835  | 0  |     if (pdsactx->md == NULL)  | 
836  | 0  |         return 0;  | 
837  |  |  | 
838  | 0  |     return EVP_MD_settable_ctx_params(pdsactx->md);  | 
839  | 0  | }  | 
840  |  |  | 
841  |  | const OSSL_DISPATCH ossl_dsa_signature_functions[] = { | 
842  |  |     { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx }, | 
843  |  |     { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_sign_init }, | 
844  |  |     { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign }, | 
845  |  |     { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_verify_init }, | 
846  |  |     { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify }, | 
847  |  |     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, | 
848  |  |       (void (*)(void))dsa_digest_sign_init },  | 
849  |  |     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, | 
850  |  |       (void (*)(void))dsa_digest_signverify_update },  | 
851  |  |     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, | 
852  |  |       (void (*)(void))dsa_digest_sign_final },  | 
853  |  |     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, | 
854  |  |       (void (*)(void))dsa_digest_verify_init },  | 
855  |  |     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, | 
856  |  |       (void (*)(void))dsa_digest_signverify_update },  | 
857  |  |     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, | 
858  |  |       (void (*)(void))dsa_digest_verify_final },  | 
859  |  |     { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx }, | 
860  |  |     { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx }, | 
861  |  |     { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params }, | 
862  |  |     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, | 
863  |  |       (void (*)(void))dsa_gettable_ctx_params },  | 
864  |  |     { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params }, | 
865  |  |     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, | 
866  |  |       (void (*)(void))dsa_settable_ctx_params },  | 
867  |  |     { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, | 
868  |  |       (void (*)(void))dsa_get_ctx_md_params },  | 
869  |  |     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, | 
870  |  |       (void (*)(void))dsa_gettable_ctx_md_params },  | 
871  |  |     { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, | 
872  |  |       (void (*)(void))dsa_set_ctx_md_params },  | 
873  |  |     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, | 
874  |  |       (void (*)(void))dsa_settable_ctx_md_params },  | 
875  |  |     OSSL_DISPATCH_END  | 
876  |  | };  | 
877  |  |  | 
878  |  | /* ------------------------------------------------------------------ */  | 
879  |  |  | 
880  |  | /*  | 
881  |  |  * So called sigalgs (composite DSA+hash) implemented below.  They  | 
882  |  |  * are pretty much hard coded.  | 
883  |  |  */  | 
884  |  |  | 
885  |  | static OSSL_FUNC_signature_query_key_types_fn dsa_sigalg_query_key_types;  | 
886  |  | static OSSL_FUNC_signature_settable_ctx_params_fn dsa_sigalg_settable_ctx_params;  | 
887  |  | static OSSL_FUNC_signature_set_ctx_params_fn dsa_sigalg_set_ctx_params;  | 
888  |  |  | 
889  |  | /*  | 
890  |  |  * dsa_sigalg_signverify_init() is almost like dsa_digest_signverify_init(),  | 
891  |  |  * just doesn't allow fetching an MD from whatever the user chooses.  | 
892  |  |  */  | 
893  |  | static int dsa_sigalg_signverify_init(void *vpdsactx, void *vdsa,  | 
894  |  |                                       OSSL_FUNC_signature_set_ctx_params_fn *set_ctx_params,  | 
895  |  |                                       const OSSL_PARAM params[],  | 
896  |  |                                       const char *mdname,  | 
897  |  |                                       int operation, const char *desc)  | 
898  | 0  | { | 
899  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
900  |  | 
  | 
901  | 0  |     if (!ossl_prov_is_running())  | 
902  | 0  |         return 0;  | 
903  |  |  | 
904  | 0  |     if (!dsa_signverify_init(vpdsactx, vdsa, set_ctx_params, params, operation,  | 
905  | 0  |                              desc))  | 
906  | 0  |         return 0;  | 
907  |  |  | 
908  | 0  |     if (!dsa_setup_md(pdsactx, mdname, NULL, desc))  | 
909  | 0  |         return 0;  | 
910  |  |  | 
911  | 0  |     pdsactx->flag_sigalg = 1;  | 
912  | 0  |     pdsactx->flag_allow_md = 0;  | 
913  |  | 
  | 
914  | 0  |     if (pdsactx->mdctx == NULL) { | 
915  | 0  |         pdsactx->mdctx = EVP_MD_CTX_new();  | 
916  | 0  |         if (pdsactx->mdctx == NULL)  | 
917  | 0  |             goto error;  | 
918  | 0  |     }  | 
919  |  |  | 
920  | 0  |     if (!EVP_DigestInit_ex2(pdsactx->mdctx, pdsactx->md, params))  | 
921  | 0  |         goto error;  | 
922  |  |  | 
923  | 0  |     return 1;  | 
924  |  |  | 
925  | 0  |  error:  | 
926  | 0  |     EVP_MD_CTX_free(pdsactx->mdctx);  | 
927  | 0  |     pdsactx->mdctx = NULL;  | 
928  | 0  |     return 0;  | 
929  | 0  | }  | 
930  |  |  | 
931  |  | static const char **dsa_sigalg_query_key_types(void)  | 
932  | 0  | { | 
933  | 0  |     static const char *keytypes[] = { "DSA", NULL }; | 
934  |  | 
  | 
935  | 0  |     return keytypes;  | 
936  | 0  | }  | 
937  |  |  | 
938  |  | static const OSSL_PARAM settable_sigalg_ctx_params[] = { | 
939  |  |     OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_SIGNATURE, NULL, 0),  | 
940  |  |     DSA_COMMON_SETTABLE_CTX_PARAMS  | 
941  |  | };  | 
942  |  |  | 
943  |  | static const OSSL_PARAM *dsa_sigalg_settable_ctx_params(void *vpdsactx,  | 
944  |  |                                                         ossl_unused void *provctx)  | 
945  | 0  | { | 
946  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
947  |  | 
  | 
948  | 0  |     if (pdsactx != NULL && pdsactx->operation == EVP_PKEY_OP_VERIFYMSG)  | 
949  | 0  |         return settable_sigalg_ctx_params;  | 
950  | 0  |     return NULL;  | 
951  | 0  | }  | 
952  |  |  | 
953  |  | static int dsa_sigalg_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])  | 
954  | 0  | { | 
955  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
956  | 0  |     const OSSL_PARAM *p;  | 
957  | 0  |     int ret;  | 
958  |  | 
  | 
959  | 0  |     if (pdsactx == NULL)  | 
960  | 0  |         return 0;  | 
961  | 0  |     if (ossl_param_is_empty(params))  | 
962  | 0  |         return 1;  | 
963  |  |  | 
964  | 0  |     if ((ret = dsa_common_set_ctx_params(pdsactx, params)) <= 0)  | 
965  | 0  |         return ret;  | 
966  |  |  | 
967  | 0  |     if (pdsactx->operation == EVP_PKEY_OP_VERIFYMSG) { | 
968  | 0  |         p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_SIGNATURE);  | 
969  | 0  |         if (p != NULL) { | 
970  | 0  |             OPENSSL_free(pdsactx->sig);  | 
971  | 0  |             pdsactx->sig = NULL;  | 
972  | 0  |             pdsactx->siglen = 0;  | 
973  | 0  |             if (!OSSL_PARAM_get_octet_string(p, (void **)&pdsactx->sig,  | 
974  | 0  |                                              0, &pdsactx->siglen))  | 
975  | 0  |                 return 0;  | 
976  | 0  |         }  | 
977  | 0  |     }  | 
978  | 0  |     return 1;  | 
979  | 0  | }  | 
980  |  |  | 
981  |  | #define IMPL_DSA_SIGALG(md, MD)                                         \  | 
982  |  |     static OSSL_FUNC_signature_sign_init_fn dsa_##md##_sign_init;       \  | 
983  |  |     static OSSL_FUNC_signature_sign_message_init_fn                     \  | 
984  |  |         dsa_##md##_sign_message_init;                                   \  | 
985  |  |     static OSSL_FUNC_signature_verify_init_fn dsa_##md##_verify_init;   \  | 
986  |  |     static OSSL_FUNC_signature_verify_message_init_fn                   \  | 
987  |  |         dsa_##md##_verify_message_init;                                 \  | 
988  |  |                                                                         \  | 
989  |  |     static int                                                          \  | 
990  |  |     dsa_##md##_sign_init(void *vpdsactx, void *vdsa,                    \  | 
991  |  |                          const OSSL_PARAM params[])                     \  | 
992  | 0  |     {                                                                   \ | 
993  | 0  |         static const char desc[] = "DSA-" MD " Sign Init";             \  | 
994  | 0  |                                                                         \  | 
995  | 0  |         return dsa_sigalg_signverify_init(vpdsactx, vdsa,               \  | 
996  | 0  |                                           dsa_sigalg_set_ctx_params,    \  | 
997  | 0  |                                           params, MD,                  \  | 
998  | 0  |                                           EVP_PKEY_OP_SIGN,             \  | 
999  | 0  |                                           desc);                        \  | 
1000  | 0  |     }                                                                   \ Unexecuted instantiation: dsa_sig.c:dsa_sha1_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha224_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha256_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha384_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha512_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_224_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_256_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_384_sign_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_512_sign_init  | 
1001  |  |                                                                         \  | 
1002  |  |     static int                                                          \  | 
1003  |  |     dsa_##md##_sign_message_init(void *vpdsactx, void *vdsa,            \  | 
1004  |  |                                  const OSSL_PARAM params[])             \  | 
1005  | 0  |     {                                                                   \ | 
1006  | 0  |         static const char desc[] = "DSA-" MD " Sign Message Init";     \  | 
1007  | 0  |                                                                         \  | 
1008  | 0  |         return dsa_sigalg_signverify_init(vpdsactx, vdsa,               \  | 
1009  | 0  |                                           dsa_sigalg_set_ctx_params,    \  | 
1010  | 0  |                                           params, MD,                  \  | 
1011  | 0  |                                           EVP_PKEY_OP_SIGNMSG,          \  | 
1012  | 0  |                                           desc);                        \  | 
1013  | 0  |     }                                                                   \ Unexecuted instantiation: dsa_sig.c:dsa_sha1_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha224_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha256_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha384_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha512_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_224_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_256_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_384_sign_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_512_sign_message_init  | 
1014  |  |                                                                         \  | 
1015  |  |     static int                                                          \  | 
1016  |  |     dsa_##md##_verify_init(void *vpdsactx, void *vdsa,                  \  | 
1017  |  |                            const OSSL_PARAM params[])                   \  | 
1018  | 0  |     {                                                                   \ | 
1019  | 0  |         static const char desc[] = "DSA-" MD " Verify Init";           \  | 
1020  | 0  |                                                                         \  | 
1021  | 0  |         return dsa_sigalg_signverify_init(vpdsactx, vdsa,               \  | 
1022  | 0  |                                           dsa_sigalg_set_ctx_params,    \  | 
1023  | 0  |                                           params, MD,                  \  | 
1024  | 0  |                                           EVP_PKEY_OP_VERIFY,           \  | 
1025  | 0  |                                           desc);                        \  | 
1026  | 0  |     }                                                                   \ Unexecuted instantiation: dsa_sig.c:dsa_sha1_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha224_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha256_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha384_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha512_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_224_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_256_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_384_verify_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_512_verify_init  | 
1027  |  |                                                                         \  | 
1028  |  |     static int                                                          \  | 
1029  |  |     dsa_##md##_verify_message_init(void *vpdsactx, void *vdsa,          \  | 
1030  |  |                                    const OSSL_PARAM params[])           \  | 
1031  | 0  |     {                                                                   \ | 
1032  | 0  |         static const char desc[] = "DSA-" MD " Verify Message Init";   \  | 
1033  | 0  |                                                                         \  | 
1034  | 0  |         return dsa_sigalg_signverify_init(vpdsactx, vdsa,               \  | 
1035  | 0  |                                           dsa_sigalg_set_ctx_params,    \  | 
1036  | 0  |                                           params, MD,                  \  | 
1037  | 0  |                                           EVP_PKEY_OP_VERIFYMSG,        \  | 
1038  | 0  |                                           desc);                        \  | 
1039  | 0  |     }                                                                   \ Unexecuted instantiation: dsa_sig.c:dsa_sha1_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha224_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha256_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha384_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha512_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_224_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_256_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_384_verify_message_init Unexecuted instantiation: dsa_sig.c:dsa_sha3_512_verify_message_init  | 
1040  |  |                                                                         \  | 
1041  |  |     const OSSL_DISPATCH ossl_dsa_##md##_signature_functions[] = {       \ | 
1042  |  |         { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx },     \ | 
1043  |  |         { OSSL_FUNC_SIGNATURE_SIGN_INIT,                                \ | 
1044  |  |           (void (*)(void))dsa_##md##_sign_init },                       \  | 
1045  |  |         { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign },         \ | 
1046  |  |         { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_INIT,                        \ | 
1047  |  |           (void (*)(void))dsa_##md##_sign_message_init },               \  | 
1048  |  |         { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_UPDATE,                      \ | 
1049  |  |           (void (*)(void))dsa_signverify_message_update },              \  | 
1050  |  |         { OSSL_FUNC_SIGNATURE_SIGN_MESSAGE_FINAL,                       \ | 
1051  |  |           (void (*)(void))dsa_sign_message_final },                     \  | 
1052  |  |         { OSSL_FUNC_SIGNATURE_VERIFY_INIT,                              \ | 
1053  |  |           (void (*)(void))dsa_##md##_verify_init },                     \  | 
1054  |  |         { OSSL_FUNC_SIGNATURE_VERIFY,                                   \ | 
1055  |  |           (void (*)(void))dsa_verify },                                 \  | 
1056  |  |         { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_INIT,                      \ | 
1057  |  |           (void (*)(void))dsa_##md##_verify_message_init },             \  | 
1058  |  |         { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_UPDATE,                    \ | 
1059  |  |           (void (*)(void))dsa_signverify_message_update },              \  | 
1060  |  |         { OSSL_FUNC_SIGNATURE_VERIFY_MESSAGE_FINAL,                     \ | 
1061  |  |           (void (*)(void))dsa_verify_message_final },                   \  | 
1062  |  |         { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx },   \ | 
1063  |  |         { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx },     \ | 
1064  |  |         { OSSL_FUNC_SIGNATURE_QUERY_KEY_TYPES,                          \ | 
1065  |  |           (void (*)(void))dsa_sigalg_query_key_types },                 \  | 
1066  |  |         { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS,                           \ | 
1067  |  |           (void (*)(void))dsa_get_ctx_params },                         \  | 
1068  |  |         { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,                      \ | 
1069  |  |           (void (*)(void))dsa_gettable_ctx_params },                    \  | 
1070  |  |         { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS,                           \ | 
1071  |  |           (void (*)(void))dsa_sigalg_set_ctx_params },                  \  | 
1072  |  |         { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,                      \ | 
1073  |  |           (void (*)(void))dsa_sigalg_settable_ctx_params },             \  | 
1074  |  |         OSSL_DISPATCH_END                                               \  | 
1075  |  |     }  | 
1076  |  |  | 
1077  |  | IMPL_DSA_SIGALG(sha1, "SHA1");  | 
1078  |  | IMPL_DSA_SIGALG(sha224, "SHA2-224");  | 
1079  |  | IMPL_DSA_SIGALG(sha256, "SHA2-256");  | 
1080  |  | IMPL_DSA_SIGALG(sha384, "SHA2-384");  | 
1081  |  | IMPL_DSA_SIGALG(sha512, "SHA2-512");  | 
1082  |  | IMPL_DSA_SIGALG(sha3_224, "SHA3-224");  | 
1083  |  | IMPL_DSA_SIGALG(sha3_256, "SHA3-256");  | 
1084  |  | IMPL_DSA_SIGALG(sha3_384, "SHA3-384");  | 
1085  |  | IMPL_DSA_SIGALG(sha3_512, "SHA3-512");  |