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