/src/openssl32/providers/implementations/signature/dsa_sig.c
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | /*  | 
2  |  |  * Copyright 2019-2023 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 "crypto/dsa.h"  | 
34  |  | #include "prov/der_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_verify_fn dsa_verify;  | 
41  |  | static OSSL_FUNC_signature_digest_sign_init_fn dsa_digest_sign_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_verify_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;  | 
57  |  |  | 
58  |  | /*  | 
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.  | 
62  |  |  */  | 
63  |  |  | 
64  |  | typedef struct { | 
65  |  |     OSSL_LIB_CTX *libctx;  | 
66  |  |     char *propq;  | 
67  |  |     DSA *dsa;  | 
68  |  |  | 
69  |  |     /*  | 
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.  | 
74  |  |      */  | 
75  |  |     unsigned int flag_allow_md : 1;  | 
76  |  |  | 
77  |  |     /* If this is set to 1 then the generated k is not random */  | 
78  |  |     unsigned int nonce_type;  | 
79  |  |  | 
80  |  |     char mdname[OSSL_MAX_NAME_SIZE];  | 
81  |  |  | 
82  |  |     /* The Algorithm Identifier of the combined signature algorithm */  | 
83  |  |     unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];  | 
84  |  |     unsigned char *aid;  | 
85  |  |     size_t  aid_len;  | 
86  |  |  | 
87  |  |     /* main digest */  | 
88  |  |     EVP_MD *md;  | 
89  |  |     EVP_MD_CTX *mdctx;  | 
90  |  |     int operation;  | 
91  |  | } PROV_DSA_CTX;  | 
92  |  |  | 
93  |  |  | 
94  |  | static size_t dsa_get_md_size(const PROV_DSA_CTX *pdsactx)  | 
95  | 942  | { | 
96  | 942  |     if (pdsactx->md != NULL)  | 
97  | 942  |         return EVP_MD_get_size(pdsactx->md);  | 
98  | 0  |     return 0;  | 
99  | 942  | }  | 
100  |  |  | 
101  |  | static void *dsa_newctx(void *provctx, const char *propq)  | 
102  | 1.28k  | { | 
103  | 1.28k  |     PROV_DSA_CTX *pdsactx;  | 
104  |  |  | 
105  | 1.28k  |     if (!ossl_prov_is_running())  | 
106  | 0  |         return NULL;  | 
107  |  |  | 
108  | 1.28k  |     pdsactx = OPENSSL_zalloc(sizeof(PROV_DSA_CTX));  | 
109  | 1.28k  |     if (pdsactx == NULL)  | 
110  | 0  |         return NULL;  | 
111  |  |  | 
112  | 1.28k  |     pdsactx->libctx = PROV_LIBCTX_OF(provctx);  | 
113  | 1.28k  |     pdsactx->flag_allow_md = 1;  | 
114  | 1.28k  |     if (propq != NULL && (pdsactx->propq = OPENSSL_strdup(propq)) == NULL) { | 
115  | 0  |         OPENSSL_free(pdsactx);  | 
116  | 0  |         pdsactx = NULL;  | 
117  | 0  |     }  | 
118  | 1.28k  |     return pdsactx;  | 
119  | 1.28k  | }  | 
120  |  |  | 
121  |  | static int dsa_setup_md(PROV_DSA_CTX *ctx,  | 
122  |  |                         const char *mdname, const char *mdprops)  | 
123  | 942  | { | 
124  | 942  |     if (mdprops == NULL)  | 
125  | 942  |         mdprops = ctx->propq;  | 
126  |  |  | 
127  | 942  |     if (mdname != NULL) { | 
128  | 942  |         int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);  | 
129  | 942  |         WPACKET pkt;  | 
130  | 942  |         EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);  | 
131  | 942  |         int md_nid = ossl_digest_get_approved_nid_with_sha1(ctx->libctx, md,  | 
132  | 942  |                                                             sha1_allowed);  | 
133  | 942  |         size_t mdname_len = strlen(mdname);  | 
134  |  |  | 
135  | 942  |         if (md == NULL || md_nid < 0) { | 
136  | 0  |             if (md == NULL)  | 
137  | 0  |                 ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,  | 
138  | 0  |                                "%s could not be fetched", mdname);  | 
139  | 0  |             if (md_nid < 0)  | 
140  | 0  |                 ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,  | 
141  | 0  |                                "digest=%s", mdname);  | 
142  | 0  |             if (mdname_len >= sizeof(ctx->mdname))  | 
143  | 0  |                 ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,  | 
144  | 0  |                                "%s exceeds name buffer length", mdname);  | 
145  | 0  |             EVP_MD_free(md);  | 
146  | 0  |             return 0;  | 
147  | 0  |         }  | 
148  |  |  | 
149  | 942  |         if (!ctx->flag_allow_md) { | 
150  | 0  |             if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) { | 
151  | 0  |                 ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,  | 
152  | 0  |                                "digest %s != %s", mdname, ctx->mdname);  | 
153  | 0  |                 EVP_MD_free(md);  | 
154  | 0  |                 return 0;  | 
155  | 0  |             }  | 
156  | 0  |             EVP_MD_free(md);  | 
157  | 0  |             return 1;  | 
158  | 0  |         }  | 
159  |  |  | 
160  | 942  |         EVP_MD_CTX_free(ctx->mdctx);  | 
161  | 942  |         EVP_MD_free(ctx->md);  | 
162  |  |  | 
163  |  |         /*  | 
164  |  |          * We do not care about DER writing errors.  | 
165  |  |          * All it really means is that for some reason, there's no  | 
166  |  |          * AlgorithmIdentifier to be had, but the operation itself is  | 
167  |  |          * still valid, just as long as it's not used to construct  | 
168  |  |          * anything that needs an AlgorithmIdentifier.  | 
169  |  |          */  | 
170  | 942  |         ctx->aid_len = 0;  | 
171  | 942  |         if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))  | 
172  | 942  |             && ossl_DER_w_algorithmIdentifier_DSA_with_MD(&pkt, -1, ctx->dsa,  | 
173  | 942  |                                                           md_nid)  | 
174  | 942  |             && WPACKET_finish(&pkt)) { | 
175  | 942  |             WPACKET_get_total_written(&pkt, &ctx->aid_len);  | 
176  | 942  |             ctx->aid = WPACKET_get_curr(&pkt);  | 
177  | 942  |         }  | 
178  | 942  |         WPACKET_cleanup(&pkt);  | 
179  |  |  | 
180  | 942  |         ctx->mdctx = NULL;  | 
181  | 942  |         ctx->md = md;  | 
182  | 942  |         OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));  | 
183  | 942  |     }  | 
184  | 942  |     return 1;  | 
185  | 942  | }  | 
186  |  |  | 
187  |  | static int dsa_signverify_init(void *vpdsactx, void *vdsa,  | 
188  |  |                                const OSSL_PARAM params[], int operation)  | 
189  | 942  | { | 
190  | 942  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
191  |  |  | 
192  | 942  |     if (!ossl_prov_is_running()  | 
193  | 942  |             || pdsactx == NULL)  | 
194  | 0  |         return 0;  | 
195  |  |  | 
196  | 942  |     if (vdsa == NULL && pdsactx->dsa == NULL) { | 
197  | 0  |         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);  | 
198  | 0  |         return 0;  | 
199  | 0  |     }  | 
200  |  |  | 
201  | 942  |     if (vdsa != NULL) { | 
202  | 942  |         if (!ossl_dsa_check_key(pdsactx->libctx, vdsa,  | 
203  | 942  |                                 operation == EVP_PKEY_OP_SIGN)) { | 
204  | 0  |             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);  | 
205  | 0  |             return 0;  | 
206  | 0  |         }  | 
207  | 942  |         if (!DSA_up_ref(vdsa))  | 
208  | 0  |             return 0;  | 
209  | 942  |         DSA_free(pdsactx->dsa);  | 
210  | 942  |         pdsactx->dsa = vdsa;  | 
211  | 942  |     }  | 
212  |  |  | 
213  | 942  |     pdsactx->operation = operation;  | 
214  |  |  | 
215  | 942  |     if (!dsa_set_ctx_params(pdsactx, params))  | 
216  | 0  |         return 0;  | 
217  |  |  | 
218  | 942  |     return 1;  | 
219  | 942  | }  | 
220  |  |  | 
221  |  | static int dsa_sign_init(void *vpdsactx, void *vdsa, const OSSL_PARAM params[])  | 
222  | 0  | { | 
223  | 0  |     return dsa_signverify_init(vpdsactx, vdsa, params, EVP_PKEY_OP_SIGN);  | 
224  | 0  | }  | 
225  |  |  | 
226  |  | static int dsa_verify_init(void *vpdsactx, void *vdsa,  | 
227  |  |                            const OSSL_PARAM params[])  | 
228  | 0  | { | 
229  | 0  |     return dsa_signverify_init(vpdsactx, vdsa, params, EVP_PKEY_OP_VERIFY);  | 
230  | 0  | }  | 
231  |  |  | 
232  |  | static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen,  | 
233  |  |                     size_t sigsize, const unsigned char *tbs, size_t tbslen)  | 
234  | 0  | { | 
235  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
236  | 0  |     int ret;  | 
237  | 0  |     unsigned int sltmp;  | 
238  | 0  |     size_t dsasize = DSA_size(pdsactx->dsa);  | 
239  | 0  |     size_t mdsize = dsa_get_md_size(pdsactx);  | 
240  |  | 
  | 
241  | 0  |     if (!ossl_prov_is_running())  | 
242  | 0  |         return 0;  | 
243  |  |  | 
244  | 0  |     if (sig == NULL) { | 
245  | 0  |         *siglen = dsasize;  | 
246  | 0  |         return 1;  | 
247  | 0  |     }  | 
248  |  |  | 
249  | 0  |     if (sigsize < (size_t)dsasize)  | 
250  | 0  |         return 0;  | 
251  |  |  | 
252  | 0  |     if (mdsize != 0 && tbslen != mdsize)  | 
253  | 0  |         return 0;  | 
254  |  |  | 
255  | 0  |     ret = ossl_dsa_sign_int(0, tbs, tbslen, sig, &sltmp, pdsactx->dsa,  | 
256  | 0  |                             pdsactx->nonce_type, pdsactx->mdname,  | 
257  | 0  |                             pdsactx->libctx, pdsactx->propq);  | 
258  | 0  |     if (ret <= 0)  | 
259  | 0  |         return 0;  | 
260  |  |  | 
261  | 0  |     *siglen = sltmp;  | 
262  | 0  |     return 1;  | 
263  | 0  | }  | 
264  |  |  | 
265  |  | static int dsa_verify(void *vpdsactx, const unsigned char *sig, size_t siglen,  | 
266  |  |                       const unsigned char *tbs, size_t tbslen)  | 
267  | 942  | { | 
268  | 942  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
269  | 942  |     size_t mdsize = dsa_get_md_size(pdsactx);  | 
270  |  |  | 
271  | 942  |     if (!ossl_prov_is_running() || (mdsize != 0 && tbslen != mdsize))  | 
272  | 0  |         return 0;  | 
273  |  |  | 
274  | 942  |     return DSA_verify(0, tbs, tbslen, sig, siglen, pdsactx->dsa);  | 
275  | 942  | }  | 
276  |  |  | 
277  |  | static int dsa_digest_signverify_init(void *vpdsactx, const char *mdname,  | 
278  |  |                                       void *vdsa, const OSSL_PARAM params[],  | 
279  |  |                                       int operation)  | 
280  | 942  | { | 
281  | 942  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
282  |  |  | 
283  | 942  |     if (!ossl_prov_is_running())  | 
284  | 0  |         return 0;  | 
285  |  |  | 
286  | 942  |     if (!dsa_signverify_init(vpdsactx, vdsa, params, operation))  | 
287  | 0  |         return 0;  | 
288  |  |  | 
289  | 942  |     if (!dsa_setup_md(pdsactx, mdname, NULL))  | 
290  | 0  |         return 0;  | 
291  |  |  | 
292  | 942  |     pdsactx->flag_allow_md = 0;  | 
293  |  |  | 
294  | 942  |     if (pdsactx->mdctx == NULL) { | 
295  | 942  |         pdsactx->mdctx = EVP_MD_CTX_new();  | 
296  | 942  |         if (pdsactx->mdctx == NULL)  | 
297  | 0  |             goto error;  | 
298  | 942  |     }  | 
299  |  |  | 
300  | 942  |     if (!EVP_DigestInit_ex2(pdsactx->mdctx, pdsactx->md, params))  | 
301  | 0  |         goto error;  | 
302  |  |  | 
303  | 942  |     return 1;  | 
304  |  |  | 
305  | 0  |  error:  | 
306  | 0  |     EVP_MD_CTX_free(pdsactx->mdctx);  | 
307  | 0  |     pdsactx->mdctx = NULL;  | 
308  | 0  |     return 0;  | 
309  | 942  | }  | 
310  |  |  | 
311  |  | static int dsa_digest_sign_init(void *vpdsactx, const char *mdname,  | 
312  |  |                                 void *vdsa, const OSSL_PARAM params[])  | 
313  | 0  | { | 
314  | 0  |     return dsa_digest_signverify_init(vpdsactx, mdname, vdsa, params,  | 
315  | 0  |                                       EVP_PKEY_OP_SIGN);  | 
316  | 0  | }  | 
317  |  |  | 
318  |  | static int dsa_digest_verify_init(void *vpdsactx, const char *mdname,  | 
319  |  |                                   void *vdsa, const OSSL_PARAM params[])  | 
320  | 1.28k  | { | 
321  | 1.28k  |     return dsa_digest_signverify_init(vpdsactx, mdname, vdsa, params,  | 
322  | 1.28k  |                                       EVP_PKEY_OP_VERIFY);  | 
323  | 1.28k  | }  | 
324  |  |  | 
325  |  | int dsa_digest_signverify_update(void *vpdsactx, const unsigned char *data,  | 
326  |  |                                  size_t datalen)  | 
327  | 942  | { | 
328  | 942  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
329  |  |  | 
330  | 942  |     if (pdsactx == NULL || pdsactx->mdctx == NULL)  | 
331  | 0  |         return 0;  | 
332  |  |  | 
333  | 942  |     return EVP_DigestUpdate(pdsactx->mdctx, data, datalen);  | 
334  | 942  | }  | 
335  |  |  | 
336  |  | int dsa_digest_sign_final(void *vpdsactx, unsigned char *sig, size_t *siglen,  | 
337  |  |                           size_t sigsize)  | 
338  | 0  | { | 
339  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
340  | 0  |     unsigned char digest[EVP_MAX_MD_SIZE];  | 
341  | 0  |     unsigned int dlen = 0;  | 
342  |  | 
  | 
343  | 0  |     if (!ossl_prov_is_running() || pdsactx == NULL || pdsactx->mdctx == NULL)  | 
344  | 0  |         return 0;  | 
345  |  |  | 
346  |  |     /*  | 
347  |  |      * If sig is NULL then we're just finding out the sig size. Other fields  | 
348  |  |      * are ignored. Defer to dsa_sign.  | 
349  |  |      */  | 
350  | 0  |     if (sig != NULL) { | 
351  |  |         /*  | 
352  |  |          * There is the possibility that some externally provided  | 
353  |  |          * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -  | 
354  |  |          * but that problem is much larger than just in DSA.  | 
355  |  |          */  | 
356  | 0  |         if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))  | 
357  | 0  |             return 0;  | 
358  | 0  |     }  | 
359  |  |  | 
360  | 0  |     pdsactx->flag_allow_md = 1;  | 
361  |  | 
  | 
362  | 0  |     return dsa_sign(vpdsactx, sig, siglen, sigsize, digest, (size_t)dlen);  | 
363  | 0  | }  | 
364  |  |  | 
365  |  |  | 
366  |  | int dsa_digest_verify_final(void *vpdsactx, const unsigned char *sig,  | 
367  |  |                             size_t siglen)  | 
368  | 942  | { | 
369  | 942  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
370  | 942  |     unsigned char digest[EVP_MAX_MD_SIZE];  | 
371  | 942  |     unsigned int dlen = 0;  | 
372  |  |  | 
373  | 942  |     if (!ossl_prov_is_running() || pdsactx == NULL || pdsactx->mdctx == NULL)  | 
374  | 0  |         return 0;  | 
375  |  |  | 
376  |  |     /*  | 
377  |  |      * There is the possibility that some externally provided  | 
378  |  |      * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -  | 
379  |  |      * but that problem is much larger than just in DSA.  | 
380  |  |      */  | 
381  | 942  |     if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))  | 
382  | 0  |         return 0;  | 
383  |  |  | 
384  | 942  |     pdsactx->flag_allow_md = 1;  | 
385  |  |  | 
386  | 942  |     return dsa_verify(vpdsactx, sig, siglen, digest, (size_t)dlen);  | 
387  | 942  | }  | 
388  |  |  | 
389  |  | static void dsa_freectx(void *vpdsactx)  | 
390  | 2.57k  | { | 
391  | 2.57k  |     PROV_DSA_CTX *ctx = (PROV_DSA_CTX *)vpdsactx;  | 
392  |  |  | 
393  | 2.57k  |     OPENSSL_free(ctx->propq);  | 
394  | 2.57k  |     EVP_MD_CTX_free(ctx->mdctx);  | 
395  | 2.57k  |     EVP_MD_free(ctx->md);  | 
396  | 2.57k  |     ctx->propq = NULL;  | 
397  | 2.57k  |     ctx->mdctx = NULL;  | 
398  | 2.57k  |     ctx->md = NULL;  | 
399  | 2.57k  |     DSA_free(ctx->dsa);  | 
400  | 2.57k  |     OPENSSL_free(ctx);  | 
401  | 2.57k  | }  | 
402  |  |  | 
403  |  | static void *dsa_dupctx(void *vpdsactx)  | 
404  | 1.28k  | { | 
405  | 1.28k  |     PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx;  | 
406  | 1.28k  |     PROV_DSA_CTX *dstctx;  | 
407  |  |  | 
408  | 1.28k  |     if (!ossl_prov_is_running())  | 
409  | 0  |         return NULL;  | 
410  |  |  | 
411  | 1.28k  |     dstctx = OPENSSL_zalloc(sizeof(*srcctx));  | 
412  | 1.28k  |     if (dstctx == NULL)  | 
413  | 0  |         return NULL;  | 
414  |  |  | 
415  | 1.28k  |     *dstctx = *srcctx;  | 
416  | 1.28k  |     dstctx->dsa = NULL;  | 
417  | 1.28k  |     dstctx->md = NULL;  | 
418  | 1.28k  |     dstctx->mdctx = NULL;  | 
419  | 1.28k  |     dstctx->propq = NULL;  | 
420  |  |  | 
421  | 1.28k  |     if (srcctx->dsa != NULL && !DSA_up_ref(srcctx->dsa))  | 
422  | 0  |         goto err;  | 
423  | 1.28k  |     dstctx->dsa = srcctx->dsa;  | 
424  |  |  | 
425  | 1.28k  |     if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))  | 
426  | 0  |         goto err;  | 
427  | 1.28k  |     dstctx->md = srcctx->md;  | 
428  |  |  | 
429  | 1.28k  |     if (srcctx->mdctx != NULL) { | 
430  | 1.28k  |         dstctx->mdctx = EVP_MD_CTX_new();  | 
431  | 1.28k  |         if (dstctx->mdctx == NULL  | 
432  | 1.28k  |                 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))  | 
433  | 0  |             goto err;  | 
434  | 1.28k  |     }  | 
435  | 1.28k  |     if (srcctx->propq != NULL) { | 
436  | 0  |         dstctx->propq = OPENSSL_strdup(srcctx->propq);  | 
437  | 0  |         if (dstctx->propq == NULL)  | 
438  | 0  |             goto err;  | 
439  | 0  |     }  | 
440  |  |  | 
441  | 1.28k  |     return dstctx;  | 
442  | 0  |  err:  | 
443  | 0  |     dsa_freectx(dstctx);  | 
444  | 0  |     return NULL;  | 
445  | 1.28k  | }  | 
446  |  |  | 
447  |  | static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params)  | 
448  | 0  | { | 
449  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
450  | 0  |     OSSL_PARAM *p;  | 
451  |  | 
  | 
452  | 0  |     if (pdsactx == NULL)  | 
453  | 0  |         return 0;  | 
454  |  |  | 
455  | 0  |     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);  | 
456  | 0  |     if (p != NULL  | 
457  | 0  |         && !OSSL_PARAM_set_octet_string(p, pdsactx->aid, pdsactx->aid_len))  | 
458  | 0  |         return 0;  | 
459  |  |  | 
460  | 0  |     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);  | 
461  | 0  |     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pdsactx->mdname))  | 
462  | 0  |         return 0;  | 
463  |  |  | 
464  | 0  |     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_NONCE_TYPE);  | 
465  | 0  |     if (p != NULL && !OSSL_PARAM_set_uint(p, pdsactx->nonce_type))  | 
466  | 0  |         return 0;  | 
467  |  |  | 
468  | 0  |     return 1;  | 
469  | 0  | }  | 
470  |  |  | 
471  |  | static const OSSL_PARAM known_gettable_ctx_params[] = { | 
472  |  |     OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),  | 
473  |  |     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),  | 
474  |  |     OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, NULL),  | 
475  |  |     OSSL_PARAM_END  | 
476  |  | };  | 
477  |  |  | 
478  |  | static const OSSL_PARAM *dsa_gettable_ctx_params(ossl_unused void *ctx,  | 
479  |  |                                                  ossl_unused void *provctx)  | 
480  | 0  | { | 
481  | 0  |     return known_gettable_ctx_params;  | 
482  | 0  | }  | 
483  |  |  | 
484  |  | static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])  | 
485  | 325  | { | 
486  | 325  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
487  | 325  |     const OSSL_PARAM *p;  | 
488  |  |  | 
489  | 325  |     if (pdsactx == NULL)  | 
490  | 0  |         return 0;  | 
491  | 325  |     if (params == NULL)  | 
492  | 325  |         return 1;  | 
493  |  |  | 
494  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);  | 
495  | 0  |     if (p != NULL) { | 
496  | 0  |         char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname;  | 
497  | 0  |         char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops;  | 
498  | 0  |         const OSSL_PARAM *propsp =  | 
499  | 0  |             OSSL_PARAM_locate_const(params,  | 
500  | 0  |                                     OSSL_SIGNATURE_PARAM_PROPERTIES);  | 
501  |  | 
  | 
502  | 0  |         if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))  | 
503  | 0  |             return 0;  | 
504  | 0  |         if (propsp != NULL  | 
505  | 0  |             && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops)))  | 
506  | 0  |             return 0;  | 
507  | 0  |         if (!dsa_setup_md(pdsactx, mdname, mdprops))  | 
508  | 0  |             return 0;  | 
509  | 0  |     }  | 
510  | 0  |     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_NONCE_TYPE);  | 
511  | 0  |     if (p != NULL  | 
512  | 0  |         && !OSSL_PARAM_get_uint(p, &pdsactx->nonce_type))  | 
513  | 0  |         return 0;  | 
514  |  |  | 
515  | 0  |     return 1;  | 
516  | 0  | }  | 
517  |  |  | 
518  |  | static const OSSL_PARAM settable_ctx_params[] = { | 
519  |  |     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),  | 
520  |  |     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),  | 
521  |  |     OSSL_PARAM_uint(OSSL_SIGNATURE_PARAM_NONCE_TYPE, NULL),  | 
522  |  |     OSSL_PARAM_END  | 
523  |  | };  | 
524  |  |  | 
525  |  | static const OSSL_PARAM settable_ctx_params_no_digest[] = { | 
526  |  |     OSSL_PARAM_END  | 
527  |  | };  | 
528  |  |  | 
529  |  | static const OSSL_PARAM *dsa_settable_ctx_params(void *vpdsactx,  | 
530  |  |                                                  ossl_unused void *provctx)  | 
531  | 1  | { | 
532  | 1  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
533  |  |  | 
534  | 1  |     if (pdsactx != NULL && !pdsactx->flag_allow_md)  | 
535  | 0  |         return settable_ctx_params_no_digest;  | 
536  | 1  |     return settable_ctx_params;  | 
537  | 1  | }  | 
538  |  |  | 
539  |  | static int dsa_get_ctx_md_params(void *vpdsactx, OSSL_PARAM *params)  | 
540  | 0  | { | 
541  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
542  |  | 
  | 
543  | 0  |     if (pdsactx->mdctx == NULL)  | 
544  | 0  |         return 0;  | 
545  |  |  | 
546  | 0  |     return EVP_MD_CTX_get_params(pdsactx->mdctx, params);  | 
547  | 0  | }  | 
548  |  |  | 
549  |  | static const OSSL_PARAM *dsa_gettable_ctx_md_params(void *vpdsactx)  | 
550  | 0  | { | 
551  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
552  |  | 
  | 
553  | 0  |     if (pdsactx->md == NULL)  | 
554  | 0  |         return 0;  | 
555  |  |  | 
556  | 0  |     return EVP_MD_gettable_ctx_params(pdsactx->md);  | 
557  | 0  | }  | 
558  |  |  | 
559  |  | static int dsa_set_ctx_md_params(void *vpdsactx, const OSSL_PARAM params[])  | 
560  | 0  | { | 
561  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
562  |  | 
  | 
563  | 0  |     if (pdsactx->mdctx == NULL)  | 
564  | 0  |         return 0;  | 
565  |  |  | 
566  | 0  |     return EVP_MD_CTX_set_params(pdsactx->mdctx, params);  | 
567  | 0  | }  | 
568  |  |  | 
569  |  | static const OSSL_PARAM *dsa_settable_ctx_md_params(void *vpdsactx)  | 
570  | 0  | { | 
571  | 0  |     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;  | 
572  |  | 
  | 
573  | 0  |     if (pdsactx->md == NULL)  | 
574  | 0  |         return 0;  | 
575  |  |  | 
576  | 0  |     return EVP_MD_settable_ctx_params(pdsactx->md);  | 
577  | 0  | }  | 
578  |  |  | 
579  |  | const OSSL_DISPATCH ossl_dsa_signature_functions[] = { | 
580  |  |     { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx }, | 
581  |  |     { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_sign_init }, | 
582  |  |     { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign }, | 
583  |  |     { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_verify_init }, | 
584  |  |     { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify }, | 
585  |  |     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, | 
586  |  |       (void (*)(void))dsa_digest_sign_init },  | 
587  |  |     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, | 
588  |  |       (void (*)(void))dsa_digest_signverify_update },  | 
589  |  |     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, | 
590  |  |       (void (*)(void))dsa_digest_sign_final },  | 
591  |  |     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, | 
592  |  |       (void (*)(void))dsa_digest_verify_init },  | 
593  |  |     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, | 
594  |  |       (void (*)(void))dsa_digest_signverify_update },  | 
595  |  |     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, | 
596  |  |       (void (*)(void))dsa_digest_verify_final },  | 
597  |  |     { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx }, | 
598  |  |     { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx }, | 
599  |  |     { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params }, | 
600  |  |     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, | 
601  |  |       (void (*)(void))dsa_gettable_ctx_params },  | 
602  |  |     { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params }, | 
603  |  |     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, | 
604  |  |       (void (*)(void))dsa_settable_ctx_params },  | 
605  |  |     { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, | 
606  |  |       (void (*)(void))dsa_get_ctx_md_params },  | 
607  |  |     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, | 
608  |  |       (void (*)(void))dsa_gettable_ctx_md_params },  | 
609  |  |     { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, | 
610  |  |       (void (*)(void))dsa_set_ctx_md_params },  | 
611  |  |     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, | 
612  |  |       (void (*)(void))dsa_settable_ctx_md_params },  | 
613  |  |     OSSL_DISPATCH_END  | 
614  |  | };  |