1*b077aed3SPierre Pronchery /*
2*b077aed3SPierre Pronchery  * Copyright 2019-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b077aed3SPierre Pronchery  *
4*b077aed3SPierre Pronchery  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b077aed3SPierre Pronchery  * this file except in compliance with the License.  You can obtain a copy
6*b077aed3SPierre Pronchery  * in the file LICENSE in the source distribution or at
7*b077aed3SPierre Pronchery  * https://www.openssl.org/source/license.html
8*b077aed3SPierre Pronchery  */
9*b077aed3SPierre Pronchery 
10*b077aed3SPierre Pronchery /*
11*b077aed3SPierre Pronchery  * DSA low level APIs are deprecated for public use, but still ok for
12*b077aed3SPierre Pronchery  * internal use.
13*b077aed3SPierre Pronchery  */
14*b077aed3SPierre Pronchery #include "internal/deprecated.h"
15*b077aed3SPierre Pronchery 
16*b077aed3SPierre Pronchery #include <string.h>
17*b077aed3SPierre Pronchery 
18*b077aed3SPierre Pronchery #include <openssl/crypto.h>
19*b077aed3SPierre Pronchery #include <openssl/core_dispatch.h>
20*b077aed3SPierre Pronchery #include <openssl/core_names.h>
21*b077aed3SPierre Pronchery #include <openssl/err.h>
22*b077aed3SPierre Pronchery #include <openssl/dsa.h>
23*b077aed3SPierre Pronchery #include <openssl/params.h>
24*b077aed3SPierre Pronchery #include <openssl/evp.h>
25*b077aed3SPierre Pronchery #include <openssl/err.h>
26*b077aed3SPierre Pronchery #include <openssl/proverr.h>
27*b077aed3SPierre Pronchery #include "internal/nelem.h"
28*b077aed3SPierre Pronchery #include "internal/sizes.h"
29*b077aed3SPierre Pronchery #include "internal/cryptlib.h"
30*b077aed3SPierre Pronchery #include "prov/providercommon.h"
31*b077aed3SPierre Pronchery #include "prov/implementations.h"
32*b077aed3SPierre Pronchery #include "prov/provider_ctx.h"
33*b077aed3SPierre Pronchery #include "prov/securitycheck.h"
34*b077aed3SPierre Pronchery #include "crypto/dsa.h"
35*b077aed3SPierre Pronchery #include "prov/der_dsa.h"
36*b077aed3SPierre Pronchery 
37*b077aed3SPierre Pronchery static OSSL_FUNC_signature_newctx_fn dsa_newctx;
38*b077aed3SPierre Pronchery static OSSL_FUNC_signature_sign_init_fn dsa_sign_init;
39*b077aed3SPierre Pronchery static OSSL_FUNC_signature_verify_init_fn dsa_verify_init;
40*b077aed3SPierre Pronchery static OSSL_FUNC_signature_sign_fn dsa_sign;
41*b077aed3SPierre Pronchery static OSSL_FUNC_signature_verify_fn dsa_verify;
42*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_sign_init_fn dsa_digest_sign_init;
43*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_sign_update_fn dsa_digest_signverify_update;
44*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_sign_final_fn dsa_digest_sign_final;
45*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_verify_init_fn dsa_digest_verify_init;
46*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_verify_update_fn dsa_digest_signverify_update;
47*b077aed3SPierre Pronchery static OSSL_FUNC_signature_digest_verify_final_fn dsa_digest_verify_final;
48*b077aed3SPierre Pronchery static OSSL_FUNC_signature_freectx_fn dsa_freectx;
49*b077aed3SPierre Pronchery static OSSL_FUNC_signature_dupctx_fn dsa_dupctx;
50*b077aed3SPierre Pronchery static OSSL_FUNC_signature_get_ctx_params_fn dsa_get_ctx_params;
51*b077aed3SPierre Pronchery static OSSL_FUNC_signature_gettable_ctx_params_fn dsa_gettable_ctx_params;
52*b077aed3SPierre Pronchery static OSSL_FUNC_signature_set_ctx_params_fn dsa_set_ctx_params;
53*b077aed3SPierre Pronchery static OSSL_FUNC_signature_settable_ctx_params_fn dsa_settable_ctx_params;
54*b077aed3SPierre Pronchery static OSSL_FUNC_signature_get_ctx_md_params_fn dsa_get_ctx_md_params;
55*b077aed3SPierre Pronchery static OSSL_FUNC_signature_gettable_ctx_md_params_fn dsa_gettable_ctx_md_params;
56*b077aed3SPierre Pronchery static OSSL_FUNC_signature_set_ctx_md_params_fn dsa_set_ctx_md_params;
57*b077aed3SPierre Pronchery static OSSL_FUNC_signature_settable_ctx_md_params_fn dsa_settable_ctx_md_params;
58*b077aed3SPierre Pronchery 
59*b077aed3SPierre Pronchery /*
60*b077aed3SPierre Pronchery  * What's passed as an actual key is defined by the KEYMGMT interface.
61*b077aed3SPierre Pronchery  * We happen to know that our KEYMGMT simply passes DSA structures, so
62*b077aed3SPierre Pronchery  * we use that here too.
63*b077aed3SPierre Pronchery  */
64*b077aed3SPierre Pronchery 
65*b077aed3SPierre Pronchery typedef struct {
66*b077aed3SPierre Pronchery     OSSL_LIB_CTX *libctx;
67*b077aed3SPierre Pronchery     char *propq;
68*b077aed3SPierre Pronchery     DSA *dsa;
69*b077aed3SPierre Pronchery 
70*b077aed3SPierre Pronchery     /*
71*b077aed3SPierre Pronchery      * Flag to determine if the hash function can be changed (1) or not (0)
72*b077aed3SPierre Pronchery      * Because it's dangerous to change during a DigestSign or DigestVerify
73*b077aed3SPierre Pronchery      * operation, this flag is cleared by their Init function, and set again
74*b077aed3SPierre Pronchery      * by their Final function.
75*b077aed3SPierre Pronchery      */
76*b077aed3SPierre Pronchery     unsigned int flag_allow_md : 1;
77*b077aed3SPierre Pronchery 
78*b077aed3SPierre Pronchery     char mdname[OSSL_MAX_NAME_SIZE];
79*b077aed3SPierre Pronchery 
80*b077aed3SPierre Pronchery     /* The Algorithm Identifier of the combined signature algorithm */
81*b077aed3SPierre Pronchery     unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE];
82*b077aed3SPierre Pronchery     unsigned char *aid;
83*b077aed3SPierre Pronchery     size_t  aid_len;
84*b077aed3SPierre Pronchery 
85*b077aed3SPierre Pronchery     /* main digest */
86*b077aed3SPierre Pronchery     EVP_MD *md;
87*b077aed3SPierre Pronchery     EVP_MD_CTX *mdctx;
88*b077aed3SPierre Pronchery     int operation;
89*b077aed3SPierre Pronchery } PROV_DSA_CTX;
90*b077aed3SPierre Pronchery 
91*b077aed3SPierre Pronchery 
dsa_get_md_size(const PROV_DSA_CTX * pdsactx)92*b077aed3SPierre Pronchery static size_t dsa_get_md_size(const PROV_DSA_CTX *pdsactx)
93*b077aed3SPierre Pronchery {
94*b077aed3SPierre Pronchery     if (pdsactx->md != NULL)
95*b077aed3SPierre Pronchery         return EVP_MD_get_size(pdsactx->md);
96*b077aed3SPierre Pronchery     return 0;
97*b077aed3SPierre Pronchery }
98*b077aed3SPierre Pronchery 
dsa_newctx(void * provctx,const char * propq)99*b077aed3SPierre Pronchery static void *dsa_newctx(void *provctx, const char *propq)
100*b077aed3SPierre Pronchery {
101*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx;
102*b077aed3SPierre Pronchery 
103*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
104*b077aed3SPierre Pronchery         return NULL;
105*b077aed3SPierre Pronchery 
106*b077aed3SPierre Pronchery     pdsactx = OPENSSL_zalloc(sizeof(PROV_DSA_CTX));
107*b077aed3SPierre Pronchery     if (pdsactx == NULL)
108*b077aed3SPierre Pronchery         return NULL;
109*b077aed3SPierre Pronchery 
110*b077aed3SPierre Pronchery     pdsactx->libctx = PROV_LIBCTX_OF(provctx);
111*b077aed3SPierre Pronchery     pdsactx->flag_allow_md = 1;
112*b077aed3SPierre Pronchery     if (propq != NULL && (pdsactx->propq = OPENSSL_strdup(propq)) == NULL) {
113*b077aed3SPierre Pronchery         OPENSSL_free(pdsactx);
114*b077aed3SPierre Pronchery         pdsactx = NULL;
115*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
116*b077aed3SPierre Pronchery     }
117*b077aed3SPierre Pronchery     return pdsactx;
118*b077aed3SPierre Pronchery }
119*b077aed3SPierre Pronchery 
dsa_setup_md(PROV_DSA_CTX * ctx,const char * mdname,const char * mdprops)120*b077aed3SPierre Pronchery static int dsa_setup_md(PROV_DSA_CTX *ctx,
121*b077aed3SPierre Pronchery                         const char *mdname, const char *mdprops)
122*b077aed3SPierre Pronchery {
123*b077aed3SPierre Pronchery     if (mdprops == NULL)
124*b077aed3SPierre Pronchery         mdprops = ctx->propq;
125*b077aed3SPierre Pronchery 
126*b077aed3SPierre Pronchery     if (mdname != NULL) {
127*b077aed3SPierre Pronchery         int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN);
128*b077aed3SPierre Pronchery         WPACKET pkt;
129*b077aed3SPierre Pronchery         EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
130*b077aed3SPierre Pronchery         int md_nid = ossl_digest_get_approved_nid_with_sha1(ctx->libctx, md,
131*b077aed3SPierre Pronchery                                                             sha1_allowed);
132*b077aed3SPierre Pronchery         size_t mdname_len = strlen(mdname);
133*b077aed3SPierre Pronchery 
134*b077aed3SPierre Pronchery         if (md == NULL || md_nid < 0) {
135*b077aed3SPierre Pronchery             if (md == NULL)
136*b077aed3SPierre Pronchery                 ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
137*b077aed3SPierre Pronchery                                "%s could not be fetched", mdname);
138*b077aed3SPierre Pronchery             if (md_nid < 0)
139*b077aed3SPierre Pronchery                 ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
140*b077aed3SPierre Pronchery                                "digest=%s", mdname);
141*b077aed3SPierre Pronchery             if (mdname_len >= sizeof(ctx->mdname))
142*b077aed3SPierre Pronchery                 ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST,
143*b077aed3SPierre Pronchery                                "%s exceeds name buffer length", mdname);
144*b077aed3SPierre Pronchery             EVP_MD_free(md);
145*b077aed3SPierre Pronchery             return 0;
146*b077aed3SPierre Pronchery         }
147*b077aed3SPierre Pronchery 
148*b077aed3SPierre Pronchery         if (!ctx->flag_allow_md) {
149*b077aed3SPierre Pronchery             if (ctx->mdname[0] != '\0' && !EVP_MD_is_a(md, ctx->mdname)) {
150*b077aed3SPierre Pronchery                 ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED,
151*b077aed3SPierre Pronchery                                "digest %s != %s", mdname, ctx->mdname);
152*b077aed3SPierre Pronchery                 EVP_MD_free(md);
153*b077aed3SPierre Pronchery                 return 0;
154*b077aed3SPierre Pronchery             }
155*b077aed3SPierre Pronchery             EVP_MD_free(md);
156*b077aed3SPierre Pronchery             return 1;
157*b077aed3SPierre Pronchery         }
158*b077aed3SPierre Pronchery 
159*b077aed3SPierre Pronchery         EVP_MD_CTX_free(ctx->mdctx);
160*b077aed3SPierre Pronchery         EVP_MD_free(ctx->md);
161*b077aed3SPierre Pronchery 
162*b077aed3SPierre Pronchery         /*
163*b077aed3SPierre Pronchery          * We do not care about DER writing errors.
164*b077aed3SPierre Pronchery          * All it really means is that for some reason, there's no
165*b077aed3SPierre Pronchery          * AlgorithmIdentifier to be had, but the operation itself is
166*b077aed3SPierre Pronchery          * still valid, just as long as it's not used to construct
167*b077aed3SPierre Pronchery          * anything that needs an AlgorithmIdentifier.
168*b077aed3SPierre Pronchery          */
169*b077aed3SPierre Pronchery         ctx->aid_len = 0;
170*b077aed3SPierre Pronchery         if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf))
171*b077aed3SPierre Pronchery             && ossl_DER_w_algorithmIdentifier_DSA_with_MD(&pkt, -1, ctx->dsa,
172*b077aed3SPierre Pronchery                                                           md_nid)
173*b077aed3SPierre Pronchery             && WPACKET_finish(&pkt)) {
174*b077aed3SPierre Pronchery             WPACKET_get_total_written(&pkt, &ctx->aid_len);
175*b077aed3SPierre Pronchery             ctx->aid = WPACKET_get_curr(&pkt);
176*b077aed3SPierre Pronchery         }
177*b077aed3SPierre Pronchery         WPACKET_cleanup(&pkt);
178*b077aed3SPierre Pronchery 
179*b077aed3SPierre Pronchery         ctx->mdctx = NULL;
180*b077aed3SPierre Pronchery         ctx->md = md;
181*b077aed3SPierre Pronchery         OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
182*b077aed3SPierre Pronchery     }
183*b077aed3SPierre Pronchery     return 1;
184*b077aed3SPierre Pronchery }
185*b077aed3SPierre Pronchery 
dsa_signverify_init(void * vpdsactx,void * vdsa,const OSSL_PARAM params[],int operation)186*b077aed3SPierre Pronchery static int dsa_signverify_init(void *vpdsactx, void *vdsa,
187*b077aed3SPierre Pronchery                                const OSSL_PARAM params[], int operation)
188*b077aed3SPierre Pronchery {
189*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
190*b077aed3SPierre Pronchery 
191*b077aed3SPierre Pronchery     if (!ossl_prov_is_running()
192*b077aed3SPierre Pronchery             || pdsactx == NULL)
193*b077aed3SPierre Pronchery         return 0;
194*b077aed3SPierre Pronchery 
195*b077aed3SPierre Pronchery     if (vdsa == NULL && pdsactx->dsa == NULL) {
196*b077aed3SPierre Pronchery         ERR_raise(ERR_LIB_PROV, PROV_R_NO_KEY_SET);
197*b077aed3SPierre Pronchery         return 0;
198*b077aed3SPierre Pronchery     }
199*b077aed3SPierre Pronchery 
200*b077aed3SPierre Pronchery     if (vdsa != NULL) {
201*b077aed3SPierre Pronchery         if (!ossl_dsa_check_key(pdsactx->libctx, vdsa,
202*b077aed3SPierre Pronchery                                 operation == EVP_PKEY_OP_SIGN)) {
203*b077aed3SPierre Pronchery             ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH);
204*b077aed3SPierre Pronchery             return 0;
205*b077aed3SPierre Pronchery         }
206*b077aed3SPierre Pronchery         if (!DSA_up_ref(vdsa))
207*b077aed3SPierre Pronchery             return 0;
208*b077aed3SPierre Pronchery         DSA_free(pdsactx->dsa);
209*b077aed3SPierre Pronchery         pdsactx->dsa = vdsa;
210*b077aed3SPierre Pronchery     }
211*b077aed3SPierre Pronchery 
212*b077aed3SPierre Pronchery     pdsactx->operation = operation;
213*b077aed3SPierre Pronchery 
214*b077aed3SPierre Pronchery     if (!dsa_set_ctx_params(pdsactx, params))
215*b077aed3SPierre Pronchery         return 0;
216*b077aed3SPierre Pronchery 
217*b077aed3SPierre Pronchery     return 1;
218*b077aed3SPierre Pronchery }
219*b077aed3SPierre Pronchery 
dsa_sign_init(void * vpdsactx,void * vdsa,const OSSL_PARAM params[])220*b077aed3SPierre Pronchery static int dsa_sign_init(void *vpdsactx, void *vdsa, const OSSL_PARAM params[])
221*b077aed3SPierre Pronchery {
222*b077aed3SPierre Pronchery     return dsa_signverify_init(vpdsactx, vdsa, params, EVP_PKEY_OP_SIGN);
223*b077aed3SPierre Pronchery }
224*b077aed3SPierre Pronchery 
dsa_verify_init(void * vpdsactx,void * vdsa,const OSSL_PARAM params[])225*b077aed3SPierre Pronchery static int dsa_verify_init(void *vpdsactx, void *vdsa,
226*b077aed3SPierre Pronchery                            const OSSL_PARAM params[])
227*b077aed3SPierre Pronchery {
228*b077aed3SPierre Pronchery     return dsa_signverify_init(vpdsactx, vdsa, params, EVP_PKEY_OP_VERIFY);
229*b077aed3SPierre Pronchery }
230*b077aed3SPierre Pronchery 
dsa_sign(void * vpdsactx,unsigned char * sig,size_t * siglen,size_t sigsize,const unsigned char * tbs,size_t tbslen)231*b077aed3SPierre Pronchery static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen,
232*b077aed3SPierre Pronchery                     size_t sigsize, const unsigned char *tbs, size_t tbslen)
233*b077aed3SPierre Pronchery {
234*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
235*b077aed3SPierre Pronchery     int ret;
236*b077aed3SPierre Pronchery     unsigned int sltmp;
237*b077aed3SPierre Pronchery     size_t dsasize = DSA_size(pdsactx->dsa);
238*b077aed3SPierre Pronchery     size_t mdsize = dsa_get_md_size(pdsactx);
239*b077aed3SPierre Pronchery 
240*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
241*b077aed3SPierre Pronchery         return 0;
242*b077aed3SPierre Pronchery 
243*b077aed3SPierre Pronchery     if (sig == NULL) {
244*b077aed3SPierre Pronchery         *siglen = dsasize;
245*b077aed3SPierre Pronchery         return 1;
246*b077aed3SPierre Pronchery     }
247*b077aed3SPierre Pronchery 
248*b077aed3SPierre Pronchery     if (sigsize < (size_t)dsasize)
249*b077aed3SPierre Pronchery         return 0;
250*b077aed3SPierre Pronchery 
251*b077aed3SPierre Pronchery     if (mdsize != 0 && tbslen != mdsize)
252*b077aed3SPierre Pronchery         return 0;
253*b077aed3SPierre Pronchery 
254*b077aed3SPierre Pronchery     ret = ossl_dsa_sign_int(0, tbs, tbslen, sig, &sltmp, pdsactx->dsa);
255*b077aed3SPierre Pronchery     if (ret <= 0)
256*b077aed3SPierre Pronchery         return 0;
257*b077aed3SPierre Pronchery 
258*b077aed3SPierre Pronchery     *siglen = sltmp;
259*b077aed3SPierre Pronchery     return 1;
260*b077aed3SPierre Pronchery }
261*b077aed3SPierre Pronchery 
dsa_verify(void * vpdsactx,const unsigned char * sig,size_t siglen,const unsigned char * tbs,size_t tbslen)262*b077aed3SPierre Pronchery static int dsa_verify(void *vpdsactx, const unsigned char *sig, size_t siglen,
263*b077aed3SPierre Pronchery                       const unsigned char *tbs, size_t tbslen)
264*b077aed3SPierre Pronchery {
265*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
266*b077aed3SPierre Pronchery     size_t mdsize = dsa_get_md_size(pdsactx);
267*b077aed3SPierre Pronchery 
268*b077aed3SPierre Pronchery     if (!ossl_prov_is_running() || (mdsize != 0 && tbslen != mdsize))
269*b077aed3SPierre Pronchery         return 0;
270*b077aed3SPierre Pronchery 
271*b077aed3SPierre Pronchery     return DSA_verify(0, tbs, tbslen, sig, siglen, pdsactx->dsa);
272*b077aed3SPierre Pronchery }
273*b077aed3SPierre Pronchery 
dsa_digest_signverify_init(void * vpdsactx,const char * mdname,void * vdsa,const OSSL_PARAM params[],int operation)274*b077aed3SPierre Pronchery static int dsa_digest_signverify_init(void *vpdsactx, const char *mdname,
275*b077aed3SPierre Pronchery                                       void *vdsa, const OSSL_PARAM params[],
276*b077aed3SPierre Pronchery                                       int operation)
277*b077aed3SPierre Pronchery {
278*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
279*b077aed3SPierre Pronchery 
280*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
281*b077aed3SPierre Pronchery         return 0;
282*b077aed3SPierre Pronchery 
283*b077aed3SPierre Pronchery     if (!dsa_signverify_init(vpdsactx, vdsa, params, operation))
284*b077aed3SPierre Pronchery         return 0;
285*b077aed3SPierre Pronchery 
286*b077aed3SPierre Pronchery     if (!dsa_setup_md(pdsactx, mdname, NULL))
287*b077aed3SPierre Pronchery         return 0;
288*b077aed3SPierre Pronchery 
289*b077aed3SPierre Pronchery     pdsactx->flag_allow_md = 0;
290*b077aed3SPierre Pronchery 
291*b077aed3SPierre Pronchery     if (pdsactx->mdctx == NULL) {
292*b077aed3SPierre Pronchery         pdsactx->mdctx = EVP_MD_CTX_new();
293*b077aed3SPierre Pronchery         if (pdsactx->mdctx == NULL)
294*b077aed3SPierre Pronchery             goto error;
295*b077aed3SPierre Pronchery     }
296*b077aed3SPierre Pronchery 
297*b077aed3SPierre Pronchery     if (!EVP_DigestInit_ex2(pdsactx->mdctx, pdsactx->md, params))
298*b077aed3SPierre Pronchery         goto error;
299*b077aed3SPierre Pronchery 
300*b077aed3SPierre Pronchery     return 1;
301*b077aed3SPierre Pronchery 
302*b077aed3SPierre Pronchery  error:
303*b077aed3SPierre Pronchery     EVP_MD_CTX_free(pdsactx->mdctx);
304*b077aed3SPierre Pronchery     pdsactx->mdctx = NULL;
305*b077aed3SPierre Pronchery     return 0;
306*b077aed3SPierre Pronchery }
307*b077aed3SPierre Pronchery 
dsa_digest_sign_init(void * vpdsactx,const char * mdname,void * vdsa,const OSSL_PARAM params[])308*b077aed3SPierre Pronchery static int dsa_digest_sign_init(void *vpdsactx, const char *mdname,
309*b077aed3SPierre Pronchery                                 void *vdsa, const OSSL_PARAM params[])
310*b077aed3SPierre Pronchery {
311*b077aed3SPierre Pronchery     return dsa_digest_signverify_init(vpdsactx, mdname, vdsa, params,
312*b077aed3SPierre Pronchery                                       EVP_PKEY_OP_SIGN);
313*b077aed3SPierre Pronchery }
314*b077aed3SPierre Pronchery 
dsa_digest_verify_init(void * vpdsactx,const char * mdname,void * vdsa,const OSSL_PARAM params[])315*b077aed3SPierre Pronchery static int dsa_digest_verify_init(void *vpdsactx, const char *mdname,
316*b077aed3SPierre Pronchery                                   void *vdsa, const OSSL_PARAM params[])
317*b077aed3SPierre Pronchery {
318*b077aed3SPierre Pronchery     return dsa_digest_signverify_init(vpdsactx, mdname, vdsa, params,
319*b077aed3SPierre Pronchery                                       EVP_PKEY_OP_VERIFY);
320*b077aed3SPierre Pronchery }
321*b077aed3SPierre Pronchery 
dsa_digest_signverify_update(void * vpdsactx,const unsigned char * data,size_t datalen)322*b077aed3SPierre Pronchery int dsa_digest_signverify_update(void *vpdsactx, const unsigned char *data,
323*b077aed3SPierre Pronchery                                  size_t datalen)
324*b077aed3SPierre Pronchery {
325*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
326*b077aed3SPierre Pronchery 
327*b077aed3SPierre Pronchery     if (pdsactx == NULL || pdsactx->mdctx == NULL)
328*b077aed3SPierre Pronchery         return 0;
329*b077aed3SPierre Pronchery 
330*b077aed3SPierre Pronchery     return EVP_DigestUpdate(pdsactx->mdctx, data, datalen);
331*b077aed3SPierre Pronchery }
332*b077aed3SPierre Pronchery 
dsa_digest_sign_final(void * vpdsactx,unsigned char * sig,size_t * siglen,size_t sigsize)333*b077aed3SPierre Pronchery int dsa_digest_sign_final(void *vpdsactx, unsigned char *sig, size_t *siglen,
334*b077aed3SPierre Pronchery                           size_t sigsize)
335*b077aed3SPierre Pronchery {
336*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
337*b077aed3SPierre Pronchery     unsigned char digest[EVP_MAX_MD_SIZE];
338*b077aed3SPierre Pronchery     unsigned int dlen = 0;
339*b077aed3SPierre Pronchery 
340*b077aed3SPierre Pronchery     if (!ossl_prov_is_running() || pdsactx == NULL || pdsactx->mdctx == NULL)
341*b077aed3SPierre Pronchery         return 0;
342*b077aed3SPierre Pronchery 
343*b077aed3SPierre Pronchery     /*
344*b077aed3SPierre Pronchery      * If sig is NULL then we're just finding out the sig size. Other fields
345*b077aed3SPierre Pronchery      * are ignored. Defer to dsa_sign.
346*b077aed3SPierre Pronchery      */
347*b077aed3SPierre Pronchery     if (sig != NULL) {
348*b077aed3SPierre Pronchery         /*
349*b077aed3SPierre Pronchery          * There is the possibility that some externally provided
350*b077aed3SPierre Pronchery          * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
351*b077aed3SPierre Pronchery          * but that problem is much larger than just in DSA.
352*b077aed3SPierre Pronchery          */
353*b077aed3SPierre Pronchery         if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
354*b077aed3SPierre Pronchery             return 0;
355*b077aed3SPierre Pronchery     }
356*b077aed3SPierre Pronchery 
357*b077aed3SPierre Pronchery     pdsactx->flag_allow_md = 1;
358*b077aed3SPierre Pronchery 
359*b077aed3SPierre Pronchery     return dsa_sign(vpdsactx, sig, siglen, sigsize, digest, (size_t)dlen);
360*b077aed3SPierre Pronchery }
361*b077aed3SPierre Pronchery 
362*b077aed3SPierre Pronchery 
dsa_digest_verify_final(void * vpdsactx,const unsigned char * sig,size_t siglen)363*b077aed3SPierre Pronchery int dsa_digest_verify_final(void *vpdsactx, const unsigned char *sig,
364*b077aed3SPierre Pronchery                             size_t siglen)
365*b077aed3SPierre Pronchery {
366*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
367*b077aed3SPierre Pronchery     unsigned char digest[EVP_MAX_MD_SIZE];
368*b077aed3SPierre Pronchery     unsigned int dlen = 0;
369*b077aed3SPierre Pronchery 
370*b077aed3SPierre Pronchery     if (!ossl_prov_is_running() || pdsactx == NULL || pdsactx->mdctx == NULL)
371*b077aed3SPierre Pronchery         return 0;
372*b077aed3SPierre Pronchery 
373*b077aed3SPierre Pronchery     /*
374*b077aed3SPierre Pronchery      * There is the possibility that some externally provided
375*b077aed3SPierre Pronchery      * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow -
376*b077aed3SPierre Pronchery      * but that problem is much larger than just in DSA.
377*b077aed3SPierre Pronchery      */
378*b077aed3SPierre Pronchery     if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen))
379*b077aed3SPierre Pronchery         return 0;
380*b077aed3SPierre Pronchery 
381*b077aed3SPierre Pronchery     pdsactx->flag_allow_md = 1;
382*b077aed3SPierre Pronchery 
383*b077aed3SPierre Pronchery     return dsa_verify(vpdsactx, sig, siglen, digest, (size_t)dlen);
384*b077aed3SPierre Pronchery }
385*b077aed3SPierre Pronchery 
dsa_freectx(void * vpdsactx)386*b077aed3SPierre Pronchery static void dsa_freectx(void *vpdsactx)
387*b077aed3SPierre Pronchery {
388*b077aed3SPierre Pronchery     PROV_DSA_CTX *ctx = (PROV_DSA_CTX *)vpdsactx;
389*b077aed3SPierre Pronchery 
390*b077aed3SPierre Pronchery     OPENSSL_free(ctx->propq);
391*b077aed3SPierre Pronchery     EVP_MD_CTX_free(ctx->mdctx);
392*b077aed3SPierre Pronchery     EVP_MD_free(ctx->md);
393*b077aed3SPierre Pronchery     ctx->propq = NULL;
394*b077aed3SPierre Pronchery     ctx->mdctx = NULL;
395*b077aed3SPierre Pronchery     ctx->md = NULL;
396*b077aed3SPierre Pronchery     DSA_free(ctx->dsa);
397*b077aed3SPierre Pronchery     OPENSSL_free(ctx);
398*b077aed3SPierre Pronchery }
399*b077aed3SPierre Pronchery 
dsa_dupctx(void * vpdsactx)400*b077aed3SPierre Pronchery static void *dsa_dupctx(void *vpdsactx)
401*b077aed3SPierre Pronchery {
402*b077aed3SPierre Pronchery     PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx;
403*b077aed3SPierre Pronchery     PROV_DSA_CTX *dstctx;
404*b077aed3SPierre Pronchery 
405*b077aed3SPierre Pronchery     if (!ossl_prov_is_running())
406*b077aed3SPierre Pronchery         return NULL;
407*b077aed3SPierre Pronchery 
408*b077aed3SPierre Pronchery     dstctx = OPENSSL_zalloc(sizeof(*srcctx));
409*b077aed3SPierre Pronchery     if (dstctx == NULL)
410*b077aed3SPierre Pronchery         return NULL;
411*b077aed3SPierre Pronchery 
412*b077aed3SPierre Pronchery     *dstctx = *srcctx;
413*b077aed3SPierre Pronchery     dstctx->dsa = NULL;
414*b077aed3SPierre Pronchery     dstctx->md = NULL;
415*b077aed3SPierre Pronchery     dstctx->mdctx = NULL;
416*b077aed3SPierre Pronchery     dstctx->propq = NULL;
417*b077aed3SPierre Pronchery 
418*b077aed3SPierre Pronchery     if (srcctx->dsa != NULL && !DSA_up_ref(srcctx->dsa))
419*b077aed3SPierre Pronchery         goto err;
420*b077aed3SPierre Pronchery     dstctx->dsa = srcctx->dsa;
421*b077aed3SPierre Pronchery 
422*b077aed3SPierre Pronchery     if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
423*b077aed3SPierre Pronchery         goto err;
424*b077aed3SPierre Pronchery     dstctx->md = srcctx->md;
425*b077aed3SPierre Pronchery 
426*b077aed3SPierre Pronchery     if (srcctx->mdctx != NULL) {
427*b077aed3SPierre Pronchery         dstctx->mdctx = EVP_MD_CTX_new();
428*b077aed3SPierre Pronchery         if (dstctx->mdctx == NULL
429*b077aed3SPierre Pronchery                 || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
430*b077aed3SPierre Pronchery             goto err;
431*b077aed3SPierre Pronchery     }
432*b077aed3SPierre Pronchery     if (srcctx->propq != NULL) {
433*b077aed3SPierre Pronchery         dstctx->propq = OPENSSL_strdup(srcctx->propq);
434*b077aed3SPierre Pronchery         if (dstctx->propq == NULL)
435*b077aed3SPierre Pronchery             goto err;
436*b077aed3SPierre Pronchery     }
437*b077aed3SPierre Pronchery 
438*b077aed3SPierre Pronchery     return dstctx;
439*b077aed3SPierre Pronchery  err:
440*b077aed3SPierre Pronchery     dsa_freectx(dstctx);
441*b077aed3SPierre Pronchery     return NULL;
442*b077aed3SPierre Pronchery }
443*b077aed3SPierre Pronchery 
dsa_get_ctx_params(void * vpdsactx,OSSL_PARAM * params)444*b077aed3SPierre Pronchery static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params)
445*b077aed3SPierre Pronchery {
446*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
447*b077aed3SPierre Pronchery     OSSL_PARAM *p;
448*b077aed3SPierre Pronchery 
449*b077aed3SPierre Pronchery     if (pdsactx == NULL)
450*b077aed3SPierre Pronchery         return 0;
451*b077aed3SPierre Pronchery 
452*b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
453*b077aed3SPierre Pronchery     if (p != NULL
454*b077aed3SPierre Pronchery         && !OSSL_PARAM_set_octet_string(p, pdsactx->aid, pdsactx->aid_len))
455*b077aed3SPierre Pronchery         return 0;
456*b077aed3SPierre Pronchery 
457*b077aed3SPierre Pronchery     p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
458*b077aed3SPierre Pronchery     if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pdsactx->mdname))
459*b077aed3SPierre Pronchery         return 0;
460*b077aed3SPierre Pronchery 
461*b077aed3SPierre Pronchery     return 1;
462*b077aed3SPierre Pronchery }
463*b077aed3SPierre Pronchery 
464*b077aed3SPierre Pronchery static const OSSL_PARAM known_gettable_ctx_params[] = {
465*b077aed3SPierre Pronchery     OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
466*b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
467*b077aed3SPierre Pronchery     OSSL_PARAM_END
468*b077aed3SPierre Pronchery };
469*b077aed3SPierre Pronchery 
dsa_gettable_ctx_params(ossl_unused void * ctx,ossl_unused void * provctx)470*b077aed3SPierre Pronchery static const OSSL_PARAM *dsa_gettable_ctx_params(ossl_unused void *ctx,
471*b077aed3SPierre Pronchery                                                  ossl_unused void *provctx)
472*b077aed3SPierre Pronchery {
473*b077aed3SPierre Pronchery     return known_gettable_ctx_params;
474*b077aed3SPierre Pronchery }
475*b077aed3SPierre Pronchery 
dsa_set_ctx_params(void * vpdsactx,const OSSL_PARAM params[])476*b077aed3SPierre Pronchery static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[])
477*b077aed3SPierre Pronchery {
478*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
479*b077aed3SPierre Pronchery     const OSSL_PARAM *p;
480*b077aed3SPierre Pronchery 
481*b077aed3SPierre Pronchery     if (pdsactx == NULL)
482*b077aed3SPierre Pronchery         return 0;
483*b077aed3SPierre Pronchery     if (params == NULL)
484*b077aed3SPierre Pronchery         return 1;
485*b077aed3SPierre Pronchery 
486*b077aed3SPierre Pronchery     p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
487*b077aed3SPierre Pronchery     if (p != NULL) {
488*b077aed3SPierre Pronchery         char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname;
489*b077aed3SPierre Pronchery         char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops;
490*b077aed3SPierre Pronchery         const OSSL_PARAM *propsp =
491*b077aed3SPierre Pronchery             OSSL_PARAM_locate_const(params,
492*b077aed3SPierre Pronchery                                     OSSL_SIGNATURE_PARAM_PROPERTIES);
493*b077aed3SPierre Pronchery 
494*b077aed3SPierre Pronchery         if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
495*b077aed3SPierre Pronchery             return 0;
496*b077aed3SPierre Pronchery         if (propsp != NULL
497*b077aed3SPierre Pronchery             && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops)))
498*b077aed3SPierre Pronchery             return 0;
499*b077aed3SPierre Pronchery         if (!dsa_setup_md(pdsactx, mdname, mdprops))
500*b077aed3SPierre Pronchery             return 0;
501*b077aed3SPierre Pronchery     }
502*b077aed3SPierre Pronchery 
503*b077aed3SPierre Pronchery     return 1;
504*b077aed3SPierre Pronchery }
505*b077aed3SPierre Pronchery 
506*b077aed3SPierre Pronchery static const OSSL_PARAM settable_ctx_params[] = {
507*b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
508*b077aed3SPierre Pronchery     OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),
509*b077aed3SPierre Pronchery     OSSL_PARAM_END
510*b077aed3SPierre Pronchery };
511*b077aed3SPierre Pronchery 
512*b077aed3SPierre Pronchery static const OSSL_PARAM settable_ctx_params_no_digest[] = {
513*b077aed3SPierre Pronchery     OSSL_PARAM_END
514*b077aed3SPierre Pronchery };
515*b077aed3SPierre Pronchery 
dsa_settable_ctx_params(void * vpdsactx,ossl_unused void * provctx)516*b077aed3SPierre Pronchery static const OSSL_PARAM *dsa_settable_ctx_params(void *vpdsactx,
517*b077aed3SPierre Pronchery                                                  ossl_unused void *provctx)
518*b077aed3SPierre Pronchery {
519*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
520*b077aed3SPierre Pronchery 
521*b077aed3SPierre Pronchery     if (pdsactx != NULL && !pdsactx->flag_allow_md)
522*b077aed3SPierre Pronchery         return settable_ctx_params_no_digest;
523*b077aed3SPierre Pronchery     return settable_ctx_params;
524*b077aed3SPierre Pronchery }
525*b077aed3SPierre Pronchery 
dsa_get_ctx_md_params(void * vpdsactx,OSSL_PARAM * params)526*b077aed3SPierre Pronchery static int dsa_get_ctx_md_params(void *vpdsactx, OSSL_PARAM *params)
527*b077aed3SPierre Pronchery {
528*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
529*b077aed3SPierre Pronchery 
530*b077aed3SPierre Pronchery     if (pdsactx->mdctx == NULL)
531*b077aed3SPierre Pronchery         return 0;
532*b077aed3SPierre Pronchery 
533*b077aed3SPierre Pronchery     return EVP_MD_CTX_get_params(pdsactx->mdctx, params);
534*b077aed3SPierre Pronchery }
535*b077aed3SPierre Pronchery 
dsa_gettable_ctx_md_params(void * vpdsactx)536*b077aed3SPierre Pronchery static const OSSL_PARAM *dsa_gettable_ctx_md_params(void *vpdsactx)
537*b077aed3SPierre Pronchery {
538*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
539*b077aed3SPierre Pronchery 
540*b077aed3SPierre Pronchery     if (pdsactx->md == NULL)
541*b077aed3SPierre Pronchery         return 0;
542*b077aed3SPierre Pronchery 
543*b077aed3SPierre Pronchery     return EVP_MD_gettable_ctx_params(pdsactx->md);
544*b077aed3SPierre Pronchery }
545*b077aed3SPierre Pronchery 
dsa_set_ctx_md_params(void * vpdsactx,const OSSL_PARAM params[])546*b077aed3SPierre Pronchery static int dsa_set_ctx_md_params(void *vpdsactx, const OSSL_PARAM params[])
547*b077aed3SPierre Pronchery {
548*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
549*b077aed3SPierre Pronchery 
550*b077aed3SPierre Pronchery     if (pdsactx->mdctx == NULL)
551*b077aed3SPierre Pronchery         return 0;
552*b077aed3SPierre Pronchery 
553*b077aed3SPierre Pronchery     return EVP_MD_CTX_set_params(pdsactx->mdctx, params);
554*b077aed3SPierre Pronchery }
555*b077aed3SPierre Pronchery 
dsa_settable_ctx_md_params(void * vpdsactx)556*b077aed3SPierre Pronchery static const OSSL_PARAM *dsa_settable_ctx_md_params(void *vpdsactx)
557*b077aed3SPierre Pronchery {
558*b077aed3SPierre Pronchery     PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx;
559*b077aed3SPierre Pronchery 
560*b077aed3SPierre Pronchery     if (pdsactx->md == NULL)
561*b077aed3SPierre Pronchery         return 0;
562*b077aed3SPierre Pronchery 
563*b077aed3SPierre Pronchery     return EVP_MD_settable_ctx_params(pdsactx->md);
564*b077aed3SPierre Pronchery }
565*b077aed3SPierre Pronchery 
566*b077aed3SPierre Pronchery const OSSL_DISPATCH ossl_dsa_signature_functions[] = {
567*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx },
568*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_sign_init },
569*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign },
570*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_verify_init },
571*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify },
572*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
573*b077aed3SPierre Pronchery       (void (*)(void))dsa_digest_sign_init },
574*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
575*b077aed3SPierre Pronchery       (void (*)(void))dsa_digest_signverify_update },
576*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
577*b077aed3SPierre Pronchery       (void (*)(void))dsa_digest_sign_final },
578*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
579*b077aed3SPierre Pronchery       (void (*)(void))dsa_digest_verify_init },
580*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
581*b077aed3SPierre Pronchery       (void (*)(void))dsa_digest_signverify_update },
582*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
583*b077aed3SPierre Pronchery       (void (*)(void))dsa_digest_verify_final },
584*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx },
585*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx },
586*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params },
587*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
588*b077aed3SPierre Pronchery       (void (*)(void))dsa_gettable_ctx_params },
589*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params },
590*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
591*b077aed3SPierre Pronchery       (void (*)(void))dsa_settable_ctx_params },
592*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
593*b077aed3SPierre Pronchery       (void (*)(void))dsa_get_ctx_md_params },
594*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
595*b077aed3SPierre Pronchery       (void (*)(void))dsa_gettable_ctx_md_params },
596*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
597*b077aed3SPierre Pronchery       (void (*)(void))dsa_set_ctx_md_params },
598*b077aed3SPierre Pronchery     { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
599*b077aed3SPierre Pronchery       (void (*)(void))dsa_settable_ctx_md_params },
600*b077aed3SPierre Pronchery     { 0, NULL }
601*b077aed3SPierre Pronchery };
602