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