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