]>
Commit | Line | Data |
---|---|---|
4889dadc | 1 | /* |
33388b44 | 2 | * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. |
4889dadc MC |
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 | ||
f41ac0ee P |
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 | ||
505b41fc RL |
16 | #include <string.h> |
17 | ||
4889dadc | 18 | #include <openssl/crypto.h> |
23c48d94 | 19 | #include <openssl/core_dispatch.h> |
4889dadc | 20 | #include <openssl/core_names.h> |
505b41fc | 21 | #include <openssl/err.h> |
4889dadc MC |
22 | #include <openssl/dsa.h> |
23 | #include <openssl/params.h> | |
45a845e4 | 24 | #include <openssl/evp.h> |
8bee6512 | 25 | #include <openssl/err.h> |
505b41fc | 26 | #include "internal/nelem.h" |
c24937d5 | 27 | #include "internal/sizes.h" |
8c555803 | 28 | #include "internal/cryptlib.h" |
f590a5ea | 29 | #include "prov/providercommon.h" |
af3e7e1b | 30 | #include "prov/implementations.h" |
8bee6512 | 31 | #include "prov/providercommonerr.h" |
ddd21319 | 32 | #include "prov/provider_ctx.h" |
7a810fac | 33 | #include "prov/securitycheck.h" |
e683582b | 34 | #include "crypto/dsa.h" |
8c555803 | 35 | #include "prov/der_dsa.h" |
4889dadc | 36 | |
363b1e5d | 37 | static OSSL_FUNC_signature_newctx_fn dsa_newctx; |
e43b4482 SL |
38 | static OSSL_FUNC_signature_sign_init_fn dsa_sign_init; |
39 | static OSSL_FUNC_signature_verify_init_fn dsa_verify_init; | |
363b1e5d DMSP |
40 | static OSSL_FUNC_signature_sign_fn dsa_sign; |
41 | static OSSL_FUNC_signature_verify_fn dsa_verify; | |
e43b4482 | 42 | static OSSL_FUNC_signature_digest_sign_init_fn dsa_digest_sign_init; |
363b1e5d DMSP |
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; | |
e43b4482 | 45 | static OSSL_FUNC_signature_digest_verify_init_fn dsa_digest_verify_init; |
363b1e5d DMSP |
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; | |
4889dadc MC |
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 { | |
b4250010 | 66 | OSSL_LIB_CTX *libctx; |
2c6094ba | 67 | char *propq; |
4889dadc | 68 | DSA *dsa; |
8bee6512 RL |
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 | ||
c24937d5 | 78 | char mdname[OSSL_MAX_NAME_SIZE]; |
505b41fc | 79 | |
edd3b7a3 | 80 | /* The Algorithm Identifier of the combined signature algorithm */ |
8c555803 RL |
81 | unsigned char aid_buf[OSSL_MAX_ALGORITHM_ID_SIZE]; |
82 | unsigned char *aid; | |
505b41fc RL |
83 | size_t aid_len; |
84 | ||
85 | /* main digest */ | |
45a845e4 MC |
86 | EVP_MD *md; |
87 | EVP_MD_CTX *mdctx; | |
8bee6512 | 88 | size_t mdsize; |
e43b4482 | 89 | int operation; |
4889dadc MC |
90 | } PROV_DSA_CTX; |
91 | ||
e43b4482 | 92 | |
8bee6512 RL |
93 | static size_t dsa_get_md_size(const PROV_DSA_CTX *pdsactx) |
94 | { | |
95 | if (pdsactx->md != NULL) | |
96 | return EVP_MD_size(pdsactx->md); | |
97 | return 0; | |
98 | } | |
99 | ||
2c6094ba | 100 | static void *dsa_newctx(void *provctx, const char *propq) |
4889dadc | 101 | { |
f590a5ea P |
102 | PROV_DSA_CTX *pdsactx; |
103 | ||
104 | if (!ossl_prov_is_running()) | |
105 | return NULL; | |
45a845e4 | 106 | |
f590a5ea | 107 | pdsactx = OPENSSL_zalloc(sizeof(PROV_DSA_CTX)); |
45a845e4 MC |
108 | if (pdsactx == NULL) |
109 | return NULL; | |
110 | ||
111 | pdsactx->libctx = PROV_LIBRARY_CONTEXT_OF(provctx); | |
8bee6512 | 112 | pdsactx->flag_allow_md = 1; |
2c6094ba RL |
113 | if (propq != NULL && (pdsactx->propq = OPENSSL_strdup(propq)) == NULL) { |
114 | OPENSSL_free(pdsactx); | |
115 | pdsactx = NULL; | |
116 | ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE); | |
117 | } | |
45a845e4 | 118 | return pdsactx; |
4889dadc MC |
119 | } |
120 | ||
8bee6512 RL |
121 | static int dsa_setup_md(PROV_DSA_CTX *ctx, |
122 | const char *mdname, const char *mdprops) | |
123 | { | |
2c6094ba RL |
124 | if (mdprops == NULL) |
125 | mdprops = ctx->propq; | |
126 | ||
8bee6512 | 127 | if (mdname != NULL) { |
49ed5ba8 | 128 | int sha1_allowed = (ctx->operation != EVP_PKEY_OP_SIGN); |
8c555803 | 129 | WPACKET pkt; |
e43b4482 | 130 | EVP_MD *md = EVP_MD_fetch(ctx->libctx, mdname, mdprops); |
49ed5ba8 | 131 | int md_nid = digest_get_approved_nid_with_sha1(md, sha1_allowed); |
e43b4482 | 132 | size_t mdname_len = strlen(mdname); |
8bee6512 | 133 | |
8c555803 | 134 | if (md == NULL || md_nid == NID_undef) { |
e43b4482 SL |
135 | if (md == NULL) |
136 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, | |
137 | "%s could not be fetched", mdname); | |
138 | if (md_nid == NID_undef) | |
139 | ERR_raise_data(ERR_LIB_PROV, PROV_R_DIGEST_NOT_ALLOWED, | |
140 | "digest=%s", mdname); | |
141 | if (mdname_len >= sizeof(ctx->mdname)) | |
142 | ERR_raise_data(ERR_LIB_PROV, PROV_R_INVALID_DIGEST, | |
143 | "%s exceeds name buffer length", mdname); | |
8bee6512 RL |
144 | EVP_MD_free(md); |
145 | return 0; | |
146 | } | |
147 | ||
8c555803 RL |
148 | EVP_MD_CTX_free(ctx->mdctx); |
149 | EVP_MD_free(ctx->md); | |
150 | ||
151 | /* | |
152 | * TODO(3.0) Should we care about DER writing errors? | |
153 | * All it really means is that for some reason, there's no | |
154 | * AlgorithmIdentifier to be had, but the operation itself is | |
155 | * still valid, just as long as it's not used to construct | |
156 | * anything that needs an AlgorithmIdentifier. | |
157 | */ | |
158 | ctx->aid_len = 0; | |
159 | if (WPACKET_init_der(&pkt, ctx->aid_buf, sizeof(ctx->aid_buf)) | |
a55b00bd P |
160 | && ossl_DER_w_algorithmIdentifier_DSA_with_MD(&pkt, -1, ctx->dsa, |
161 | md_nid) | |
8c555803 RL |
162 | && WPACKET_finish(&pkt)) { |
163 | WPACKET_get_total_written(&pkt, &ctx->aid_len); | |
164 | ctx->aid = WPACKET_get_curr(&pkt); | |
165 | } | |
166 | WPACKET_cleanup(&pkt); | |
167 | ||
168 | ctx->mdctx = NULL; | |
8bee6512 RL |
169 | ctx->md = md; |
170 | OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname)); | |
171 | } | |
172 | return 1; | |
173 | } | |
174 | ||
e43b4482 | 175 | static int dsa_signverify_init(void *vpdsactx, void *vdsa, int operation) |
4889dadc MC |
176 | { |
177 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
178 | ||
f590a5ea P |
179 | if (!ossl_prov_is_running() |
180 | || pdsactx == NULL | |
181 | || vdsa == NULL | |
182 | || !DSA_up_ref(vdsa)) | |
4889dadc MC |
183 | return 0; |
184 | DSA_free(pdsactx->dsa); | |
185 | pdsactx->dsa = vdsa; | |
e43b4482 | 186 | pdsactx->operation = operation; |
49ed5ba8 | 187 | if (!dsa_check_key(vdsa, operation == EVP_PKEY_OP_SIGN)) { |
e43b4482 SL |
188 | ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_KEY_LENGTH); |
189 | return 0; | |
190 | } | |
4889dadc MC |
191 | return 1; |
192 | } | |
193 | ||
e43b4482 SL |
194 | static int dsa_sign_init(void *vpdsactx, void *vdsa) |
195 | { | |
196 | return dsa_signverify_init(vpdsactx, vdsa, EVP_PKEY_OP_SIGN); | |
197 | } | |
198 | ||
199 | static int dsa_verify_init(void *vpdsactx, void *vdsa) | |
200 | { | |
201 | return dsa_signverify_init(vpdsactx, vdsa, EVP_PKEY_OP_VERIFY); | |
202 | } | |
203 | ||
4889dadc MC |
204 | static int dsa_sign(void *vpdsactx, unsigned char *sig, size_t *siglen, |
205 | size_t sigsize, const unsigned char *tbs, size_t tbslen) | |
206 | { | |
207 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
208 | int ret; | |
209 | unsigned int sltmp; | |
210 | size_t dsasize = DSA_size(pdsactx->dsa); | |
8bee6512 | 211 | size_t mdsize = dsa_get_md_size(pdsactx); |
4889dadc | 212 | |
f590a5ea P |
213 | if (!ossl_prov_is_running()) |
214 | return 0; | |
215 | ||
4889dadc MC |
216 | if (sig == NULL) { |
217 | *siglen = dsasize; | |
218 | return 1; | |
219 | } | |
220 | ||
221 | if (sigsize < (size_t)dsasize) | |
222 | return 0; | |
223 | ||
8bee6512 | 224 | if (mdsize != 0 && tbslen != mdsize) |
4889dadc MC |
225 | return 0; |
226 | ||
8083fd3a | 227 | ret = dsa_sign_int(0, tbs, tbslen, sig, &sltmp, pdsactx->dsa); |
4889dadc MC |
228 | if (ret <= 0) |
229 | return 0; | |
230 | ||
231 | *siglen = sltmp; | |
232 | return 1; | |
233 | } | |
234 | ||
390acbeb MC |
235 | static int dsa_verify(void *vpdsactx, const unsigned char *sig, size_t siglen, |
236 | const unsigned char *tbs, size_t tbslen) | |
237 | { | |
238 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
8bee6512 | 239 | size_t mdsize = dsa_get_md_size(pdsactx); |
390acbeb | 240 | |
f590a5ea | 241 | if (!ossl_prov_is_running() || (mdsize != 0 && tbslen != mdsize)) |
390acbeb MC |
242 | return 0; |
243 | ||
244 | return DSA_verify(0, tbs, tbslen, sig, siglen, pdsactx->dsa); | |
245 | } | |
246 | ||
45a845e4 | 247 | static int dsa_digest_signverify_init(void *vpdsactx, const char *mdname, |
e43b4482 | 248 | void *vdsa, int operation) |
45a845e4 MC |
249 | { |
250 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
45a845e4 | 251 | |
f590a5ea P |
252 | if (!ossl_prov_is_running()) |
253 | return 0; | |
254 | ||
8bee6512 | 255 | pdsactx->flag_allow_md = 0; |
e43b4482 | 256 | if (!dsa_signverify_init(vpdsactx, vdsa, operation)) |
45a845e4 MC |
257 | return 0; |
258 | ||
2c6094ba | 259 | if (!dsa_setup_md(pdsactx, mdname, NULL)) |
8bee6512 | 260 | return 0; |
505b41fc | 261 | |
45a845e4 MC |
262 | pdsactx->mdctx = EVP_MD_CTX_new(); |
263 | if (pdsactx->mdctx == NULL) | |
505b41fc | 264 | goto error; |
45a845e4 | 265 | |
505b41fc RL |
266 | if (!EVP_DigestInit_ex(pdsactx->mdctx, pdsactx->md, NULL)) |
267 | goto error; | |
45a845e4 MC |
268 | |
269 | return 1; | |
505b41fc RL |
270 | |
271 | error: | |
272 | EVP_MD_CTX_free(pdsactx->mdctx); | |
273 | EVP_MD_free(pdsactx->md); | |
274 | pdsactx->mdctx = NULL; | |
505b41fc RL |
275 | pdsactx->md = NULL; |
276 | return 0; | |
45a845e4 MC |
277 | } |
278 | ||
e43b4482 SL |
279 | static int dsa_digest_sign_init(void *vpdsactx, const char *mdname, |
280 | void *vdsa) | |
281 | { | |
282 | return dsa_digest_signverify_init(vpdsactx, mdname, vdsa, EVP_PKEY_OP_SIGN); | |
283 | } | |
284 | ||
285 | static int dsa_digest_verify_init(void *vpdsactx, const char *mdname, void *vdsa) | |
286 | { | |
287 | return dsa_digest_signverify_init(vpdsactx, mdname, vdsa, EVP_PKEY_OP_VERIFY); | |
288 | } | |
289 | ||
45a845e4 MC |
290 | int dsa_digest_signverify_update(void *vpdsactx, const unsigned char *data, |
291 | size_t datalen) | |
292 | { | |
293 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
294 | ||
295 | if (pdsactx == NULL || pdsactx->mdctx == NULL) | |
296 | return 0; | |
297 | ||
298 | return EVP_DigestUpdate(pdsactx->mdctx, data, datalen); | |
299 | } | |
300 | ||
301 | int dsa_digest_sign_final(void *vpdsactx, unsigned char *sig, size_t *siglen, | |
302 | size_t sigsize) | |
303 | { | |
304 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
305 | unsigned char digest[EVP_MAX_MD_SIZE]; | |
306 | unsigned int dlen = 0; | |
307 | ||
f590a5ea | 308 | if (!ossl_prov_is_running() || pdsactx == NULL || pdsactx->mdctx == NULL) |
45a845e4 MC |
309 | return 0; |
310 | ||
311 | /* | |
312 | * If sig is NULL then we're just finding out the sig size. Other fields | |
313 | * are ignored. Defer to dsa_sign. | |
314 | */ | |
315 | if (sig != NULL) { | |
316 | /* | |
317 | * TODO(3.0): There is the possibility that some externally provided | |
318 | * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow - | |
319 | * but that problem is much larger than just in DSA. | |
320 | */ | |
321 | if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen)) | |
322 | return 0; | |
323 | } | |
324 | ||
8bee6512 RL |
325 | pdsactx->flag_allow_md = 1; |
326 | ||
45a845e4 MC |
327 | return dsa_sign(vpdsactx, sig, siglen, sigsize, digest, (size_t)dlen); |
328 | } | |
329 | ||
330 | ||
331 | int dsa_digest_verify_final(void *vpdsactx, const unsigned char *sig, | |
332 | size_t siglen) | |
333 | { | |
334 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
335 | unsigned char digest[EVP_MAX_MD_SIZE]; | |
336 | unsigned int dlen = 0; | |
337 | ||
f590a5ea | 338 | if (!ossl_prov_is_running() || pdsactx == NULL || pdsactx->mdctx == NULL) |
45a845e4 MC |
339 | return 0; |
340 | ||
341 | /* | |
342 | * TODO(3.0): There is the possibility that some externally provided | |
343 | * digests exceed EVP_MAX_MD_SIZE. We should probably handle that somehow - | |
344 | * but that problem is much larger than just in DSA. | |
345 | */ | |
346 | if (!EVP_DigestFinal_ex(pdsactx->mdctx, digest, &dlen)) | |
347 | return 0; | |
348 | ||
8bee6512 RL |
349 | pdsactx->flag_allow_md = 1; |
350 | ||
45a845e4 MC |
351 | return dsa_verify(vpdsactx, sig, siglen, digest, (size_t)dlen); |
352 | } | |
390acbeb | 353 | |
4889dadc MC |
354 | static void dsa_freectx(void *vpdsactx) |
355 | { | |
4f2271d5 SL |
356 | PROV_DSA_CTX *ctx = (PROV_DSA_CTX *)vpdsactx; |
357 | ||
358 | OPENSSL_free(ctx->propq); | |
359 | EVP_MD_CTX_free(ctx->mdctx); | |
360 | EVP_MD_free(ctx->md); | |
361 | ctx->propq = NULL; | |
362 | ctx->mdctx = NULL; | |
363 | ctx->md = NULL; | |
364 | ctx->mdsize = 0; | |
365 | DSA_free(ctx->dsa); | |
366 | OPENSSL_free(ctx); | |
4889dadc MC |
367 | } |
368 | ||
369 | static void *dsa_dupctx(void *vpdsactx) | |
370 | { | |
371 | PROV_DSA_CTX *srcctx = (PROV_DSA_CTX *)vpdsactx; | |
372 | PROV_DSA_CTX *dstctx; | |
373 | ||
f590a5ea P |
374 | if (!ossl_prov_is_running()) |
375 | return NULL; | |
376 | ||
4889dadc MC |
377 | dstctx = OPENSSL_zalloc(sizeof(*srcctx)); |
378 | if (dstctx == NULL) | |
379 | return NULL; | |
380 | ||
381 | *dstctx = *srcctx; | |
45a845e4 MC |
382 | dstctx->dsa = NULL; |
383 | dstctx->md = NULL; | |
384 | dstctx->mdctx = NULL; | |
385 | ||
386 | if (srcctx->dsa != NULL && !DSA_up_ref(srcctx->dsa)) | |
387 | goto err; | |
388 | dstctx->dsa = srcctx->dsa; | |
389 | ||
390 | if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md)) | |
391 | goto err; | |
392 | dstctx->md = srcctx->md; | |
393 | ||
394 | if (srcctx->mdctx != NULL) { | |
395 | dstctx->mdctx = EVP_MD_CTX_new(); | |
396 | if (dstctx->mdctx == NULL | |
397 | || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx)) | |
398 | goto err; | |
4889dadc MC |
399 | } |
400 | ||
401 | return dstctx; | |
45a845e4 MC |
402 | err: |
403 | dsa_freectx(dstctx); | |
404 | return NULL; | |
4889dadc MC |
405 | } |
406 | ||
9c45222d MC |
407 | static int dsa_get_ctx_params(void *vpdsactx, OSSL_PARAM *params) |
408 | { | |
409 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
410 | OSSL_PARAM *p; | |
411 | ||
412 | if (pdsactx == NULL || params == NULL) | |
413 | return 0; | |
414 | ||
505b41fc RL |
415 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID); |
416 | if (p != NULL | |
417 | && !OSSL_PARAM_set_octet_string(p, pdsactx->aid, pdsactx->aid_len)) | |
418 | return 0; | |
419 | ||
9c45222d | 420 | p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST); |
8bee6512 | 421 | if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pdsactx->mdname)) |
9c45222d MC |
422 | return 0; |
423 | ||
424 | return 1; | |
425 | } | |
426 | ||
427 | static const OSSL_PARAM known_gettable_ctx_params[] = { | |
505b41fc | 428 | OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0), |
9c45222d MC |
429 | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), |
430 | OSSL_PARAM_END | |
431 | }; | |
432 | ||
e43b4482 | 433 | static const OSSL_PARAM *dsa_gettable_ctx_params(ossl_unused void *vctx) |
9c45222d MC |
434 | { |
435 | return known_gettable_ctx_params; | |
436 | } | |
437 | ||
438 | static int dsa_set_ctx_params(void *vpdsactx, const OSSL_PARAM params[]) | |
4889dadc MC |
439 | { |
440 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
441 | const OSSL_PARAM *p; | |
4889dadc MC |
442 | |
443 | if (pdsactx == NULL || params == NULL) | |
444 | return 0; | |
445 | ||
9c45222d | 446 | p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST); |
8bee6512 RL |
447 | /* Not allowed during certain operations */ |
448 | if (p != NULL && !pdsactx->flag_allow_md) | |
9c45222d | 449 | return 0; |
8bee6512 RL |
450 | if (p != NULL) { |
451 | char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname; | |
452 | char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops; | |
453 | const OSSL_PARAM *propsp = | |
454 | OSSL_PARAM_locate_const(params, | |
455 | OSSL_SIGNATURE_PARAM_PROPERTIES); | |
456 | ||
457 | if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname))) | |
458 | return 0; | |
459 | if (propsp != NULL | |
460 | && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops))) | |
461 | return 0; | |
462 | if (!dsa_setup_md(pdsactx, mdname, mdprops)) | |
463 | return 0; | |
464 | } | |
4889dadc MC |
465 | |
466 | return 1; | |
467 | } | |
468 | ||
9c45222d | 469 | static const OSSL_PARAM known_settable_ctx_params[] = { |
9c45222d | 470 | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0), |
b8086652 | 471 | OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0), |
9c45222d MC |
472 | OSSL_PARAM_END |
473 | }; | |
474 | ||
1017ab21 | 475 | static const OSSL_PARAM *dsa_settable_ctx_params(ossl_unused void *provctx) |
9c45222d | 476 | { |
45a845e4 MC |
477 | /* |
478 | * TODO(3.0): Should this function return a different set of settable ctx | |
479 | * params if the ctx is being used for a DigestSign/DigestVerify? In that | |
480 | * case it is not allowed to set the digest size/digest name because the | |
481 | * digest is explicitly set as part of the init. | |
e43b4482 SL |
482 | * NOTE: Ideally we would check pdsactx->flag_allow_md, but this is |
483 | * problematic because there is no nice way of passing the | |
484 | * PROV_DSA_CTX down to this function... | |
485 | * Because we have API's that dont know about their parent.. | |
486 | * e.g: EVP_SIGNATURE_gettable_ctx_params(const EVP_SIGNATURE *sig). | |
487 | * We could pass NULL for that case (but then how useful is the check?). | |
45a845e4 | 488 | */ |
9c45222d MC |
489 | return known_settable_ctx_params; |
490 | } | |
491 | ||
45a845e4 MC |
492 | static int dsa_get_ctx_md_params(void *vpdsactx, OSSL_PARAM *params) |
493 | { | |
494 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
495 | ||
496 | if (pdsactx->mdctx == NULL) | |
497 | return 0; | |
498 | ||
499 | return EVP_MD_CTX_get_params(pdsactx->mdctx, params); | |
500 | } | |
501 | ||
502 | static const OSSL_PARAM *dsa_gettable_ctx_md_params(void *vpdsactx) | |
503 | { | |
504 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
505 | ||
506 | if (pdsactx->md == NULL) | |
507 | return 0; | |
508 | ||
509 | return EVP_MD_gettable_ctx_params(pdsactx->md); | |
510 | } | |
511 | ||
512 | static int dsa_set_ctx_md_params(void *vpdsactx, const OSSL_PARAM params[]) | |
513 | { | |
514 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
515 | ||
516 | if (pdsactx->mdctx == NULL) | |
517 | return 0; | |
518 | ||
519 | return EVP_MD_CTX_set_params(pdsactx->mdctx, params); | |
520 | } | |
521 | ||
522 | static const OSSL_PARAM *dsa_settable_ctx_md_params(void *vpdsactx) | |
523 | { | |
524 | PROV_DSA_CTX *pdsactx = (PROV_DSA_CTX *)vpdsactx; | |
525 | ||
526 | if (pdsactx->md == NULL) | |
527 | return 0; | |
528 | ||
529 | return EVP_MD_settable_ctx_params(pdsactx->md); | |
530 | } | |
531 | ||
1be63951 | 532 | const OSSL_DISPATCH ossl_dsa_signature_functions[] = { |
4889dadc | 533 | { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))dsa_newctx }, |
e43b4482 | 534 | { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))dsa_sign_init }, |
4889dadc | 535 | { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))dsa_sign }, |
e43b4482 | 536 | { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))dsa_verify_init }, |
390acbeb | 537 | { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))dsa_verify }, |
45a845e4 | 538 | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT, |
e43b4482 | 539 | (void (*)(void))dsa_digest_sign_init }, |
45a845e4 MC |
540 | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE, |
541 | (void (*)(void))dsa_digest_signverify_update }, | |
542 | { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL, | |
543 | (void (*)(void))dsa_digest_sign_final }, | |
544 | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT, | |
e43b4482 | 545 | (void (*)(void))dsa_digest_verify_init }, |
45a845e4 MC |
546 | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE, |
547 | (void (*)(void))dsa_digest_signverify_update }, | |
548 | { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL, | |
549 | (void (*)(void))dsa_digest_verify_final }, | |
4889dadc MC |
550 | { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))dsa_freectx }, |
551 | { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))dsa_dupctx }, | |
9c45222d MC |
552 | { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))dsa_get_ctx_params }, |
553 | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS, | |
554 | (void (*)(void))dsa_gettable_ctx_params }, | |
555 | { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))dsa_set_ctx_params }, | |
556 | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS, | |
557 | (void (*)(void))dsa_settable_ctx_params }, | |
45a845e4 MC |
558 | { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS, |
559 | (void (*)(void))dsa_get_ctx_md_params }, | |
560 | { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS, | |
561 | (void (*)(void))dsa_gettable_ctx_md_params }, | |
562 | { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS, | |
563 | (void (*)(void))dsa_set_ctx_md_params }, | |
564 | { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS, | |
565 | (void (*)(void))dsa_settable_ctx_md_params }, | |
4889dadc MC |
566 | { 0, NULL } |
567 | }; |