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