/src/openssl34/crypto/evp/m_sigver.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2006-2024 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 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/evp.h> |
13 | | #include <openssl/objects.h> |
14 | | #include "crypto/evp.h" |
15 | | #include "internal/provider.h" |
16 | | #include "internal/numbers.h" /* includes SIZE_MAX */ |
17 | | #include "evp_local.h" |
18 | | |
19 | | #ifndef FIPS_MODULE |
20 | | static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen) |
21 | 0 | { |
22 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_ONLY_ONESHOT_SUPPORTED); |
23 | 0 | return 0; |
24 | 0 | } |
25 | | #endif |
26 | | |
27 | | /* |
28 | | * If we get the "NULL" md then the name comes back as "UNDEF". We want to use |
29 | | * NULL for this. |
30 | | */ |
31 | | static const char *canon_mdname(const char *mdname) |
32 | 12.6k | { |
33 | 12.6k | if (mdname != NULL && strcmp(mdname, "UNDEF") == 0) |
34 | 402 | return NULL; |
35 | | |
36 | 12.2k | return mdname; |
37 | 12.6k | } |
38 | | |
39 | | static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
40 | | const EVP_MD *type, const char *mdname, |
41 | | OSSL_LIB_CTX *libctx, const char *props, |
42 | | ENGINE *e, EVP_PKEY *pkey, int ver, |
43 | | const OSSL_PARAM params[]) |
44 | 43.4k | { |
45 | 43.4k | EVP_PKEY_CTX *locpctx = NULL; |
46 | 43.4k | EVP_SIGNATURE *signature = NULL; |
47 | 43.4k | EVP_KEYMGMT *tmp_keymgmt = NULL; |
48 | 43.4k | const OSSL_PROVIDER *tmp_prov = NULL; |
49 | 43.4k | const char *supported_sig = NULL; |
50 | 43.4k | char locmdname[80] = ""; /* 80 chars should be enough */ |
51 | 43.4k | void *provkey = NULL; |
52 | 43.4k | int ret, iter, reinit = 1; |
53 | | |
54 | 43.4k | if (!evp_md_ctx_free_algctx(ctx)) |
55 | 0 | return 0; |
56 | | |
57 | 43.4k | if (ctx->pctx == NULL) { |
58 | 37.2k | reinit = 0; |
59 | 37.2k | if (e == NULL) |
60 | 37.2k | ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props); |
61 | 0 | #ifndef FIPS_MODULE |
62 | 0 | else |
63 | 0 | ctx->pctx = EVP_PKEY_CTX_new(pkey, e); |
64 | 37.2k | #endif |
65 | 37.2k | } |
66 | 43.4k | if (ctx->pctx == NULL) |
67 | 0 | return 0; |
68 | | |
69 | 43.4k | EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_FINALISED); |
70 | | |
71 | 43.4k | locpctx = ctx->pctx; |
72 | 43.4k | ERR_set_mark(); |
73 | | |
74 | 43.4k | if (evp_pkey_ctx_is_legacy(locpctx)) |
75 | 0 | goto legacy; |
76 | | |
77 | | /* do not reinitialize if pkey is set or operation is different */ |
78 | 43.4k | if (reinit |
79 | 43.4k | && (pkey != NULL |
80 | 6.27k | || locpctx->operation != (ver ? EVP_PKEY_OP_VERIFYCTX |
81 | 0 | : EVP_PKEY_OP_SIGNCTX) |
82 | 6.27k | || (signature = locpctx->op.sig.signature) == NULL |
83 | 6.27k | || locpctx->op.sig.algctx == NULL)) |
84 | 6.27k | reinit = 0; |
85 | | |
86 | 43.4k | if (props == NULL) |
87 | 43.4k | props = locpctx->propquery; |
88 | | |
89 | 43.4k | if (locpctx->pkey == NULL) { |
90 | 0 | ERR_clear_last_mark(); |
91 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); |
92 | 0 | goto err; |
93 | 0 | } |
94 | | |
95 | 43.4k | if (!reinit) { |
96 | 43.4k | evp_pkey_ctx_free_old_ops(locpctx); |
97 | 43.4k | } else { |
98 | 0 | if (mdname == NULL && type == NULL) |
99 | 0 | mdname = canon_mdname(EVP_MD_get0_name(ctx->reqdigest)); |
100 | 0 | goto reinitialize; |
101 | 0 | } |
102 | | |
103 | | /* |
104 | | * Try to derive the supported signature from |locpctx->keymgmt|. |
105 | | */ |
106 | 43.4k | if (!ossl_assert(locpctx->pkey->keymgmt == NULL |
107 | 43.4k | || locpctx->pkey->keymgmt == locpctx->keymgmt)) { |
108 | 0 | ERR_clear_last_mark(); |
109 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
110 | 0 | goto err; |
111 | 0 | } |
112 | 43.4k | supported_sig = evp_keymgmt_util_query_operation_name(locpctx->keymgmt, |
113 | 43.4k | OSSL_OP_SIGNATURE); |
114 | 43.4k | if (supported_sig == NULL) { |
115 | 0 | ERR_clear_last_mark(); |
116 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
117 | 0 | goto err; |
118 | 0 | } |
119 | | |
120 | | /* |
121 | | * We perform two iterations: |
122 | | * |
123 | | * 1. Do the normal signature fetch, using the fetching data given by |
124 | | * the EVP_PKEY_CTX. |
125 | | * 2. Do the provider specific signature fetch, from the same provider |
126 | | * as |ctx->keymgmt| |
127 | | * |
128 | | * We then try to fetch the keymgmt from the same provider as the |
129 | | * signature, and try to export |ctx->pkey| to that keymgmt (when |
130 | | * this keymgmt happens to be the same as |ctx->keymgmt|, the export |
131 | | * is a no-op, but we call it anyway to not complicate the code even |
132 | | * more). |
133 | | * If the export call succeeds (returns a non-NULL provider key pointer), |
134 | | * we're done and can perform the operation itself. If not, we perform |
135 | | * the second iteration, or jump to legacy. |
136 | | */ |
137 | 86.9k | for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) { |
138 | 43.4k | EVP_KEYMGMT *tmp_keymgmt_tofree = NULL; |
139 | | |
140 | | /* |
141 | | * If we're on the second iteration, free the results from the first. |
142 | | * They are NULL on the first iteration, so no need to check what |
143 | | * iteration we're on. |
144 | | */ |
145 | 43.4k | EVP_SIGNATURE_free(signature); |
146 | 43.4k | EVP_KEYMGMT_free(tmp_keymgmt); |
147 | | |
148 | 43.4k | switch (iter) { |
149 | 43.4k | case 1: |
150 | 43.4k | signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig, |
151 | 43.4k | locpctx->propquery); |
152 | 43.4k | if (signature != NULL) |
153 | 43.4k | tmp_prov = EVP_SIGNATURE_get0_provider(signature); |
154 | 43.4k | break; |
155 | 0 | case 2: |
156 | 0 | tmp_prov = EVP_KEYMGMT_get0_provider(locpctx->keymgmt); |
157 | 0 | signature = |
158 | 0 | evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
159 | 0 | supported_sig, locpctx->propquery); |
160 | 0 | if (signature == NULL) |
161 | 0 | goto legacy; |
162 | 0 | break; |
163 | 43.4k | } |
164 | 43.4k | if (signature == NULL) |
165 | 0 | continue; |
166 | | |
167 | | /* |
168 | | * Ensure that the key is provided, either natively, or as a cached |
169 | | * export. We start by fetching the keymgmt with the same name as |
170 | | * |locpctx->pkey|, but from the provider of the signature method, using |
171 | | * the same property query as when fetching the signature method. |
172 | | * With the keymgmt we found (if we did), we try to export |locpctx->pkey| |
173 | | * to it (evp_pkey_export_to_provider() is smart enough to only actually |
174 | | |
175 | | * export it if |tmp_keymgmt| is different from |locpctx->pkey|'s keymgmt) |
176 | | */ |
177 | 43.4k | tmp_keymgmt_tofree = tmp_keymgmt = |
178 | 43.4k | evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
179 | 43.4k | EVP_KEYMGMT_get0_name(locpctx->keymgmt), |
180 | 43.4k | locpctx->propquery); |
181 | 43.4k | if (tmp_keymgmt != NULL) |
182 | 43.4k | provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx, |
183 | 43.4k | &tmp_keymgmt, locpctx->propquery); |
184 | 43.4k | if (tmp_keymgmt == NULL) |
185 | 0 | EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
186 | 43.4k | } |
187 | | |
188 | 43.4k | if (provkey == NULL) { |
189 | 0 | EVP_SIGNATURE_free(signature); |
190 | 0 | ERR_clear_last_mark(); |
191 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
192 | 0 | goto err; |
193 | 0 | } |
194 | | |
195 | 43.4k | ERR_pop_to_mark(); |
196 | | |
197 | | /* No more legacy from here down to legacy: */ |
198 | | |
199 | 43.4k | locpctx->op.sig.signature = signature; |
200 | 43.4k | locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX |
201 | 43.4k | : EVP_PKEY_OP_SIGNCTX; |
202 | 43.4k | locpctx->op.sig.algctx |
203 | 43.4k | = signature->newctx(ossl_provider_ctx(signature->prov), props); |
204 | 43.4k | if (locpctx->op.sig.algctx == NULL) { |
205 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
206 | 0 | goto err; |
207 | 0 | } |
208 | | |
209 | 43.4k | reinitialize: |
210 | 43.4k | if (pctx != NULL) |
211 | 17.8k | *pctx = locpctx; |
212 | | |
213 | 43.4k | if (type != NULL) { |
214 | 6.20k | ctx->reqdigest = type; |
215 | 6.20k | if (mdname == NULL) |
216 | 6.20k | mdname = canon_mdname(EVP_MD_get0_name(type)); |
217 | 37.2k | } else { |
218 | 37.2k | if (mdname == NULL && !reinit) { |
219 | 74 | if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey, |
220 | 74 | locmdname, |
221 | 74 | sizeof(locmdname)) > 0) { |
222 | 74 | mdname = canon_mdname(locmdname); |
223 | 74 | } |
224 | 74 | } |
225 | | |
226 | 37.2k | if (mdname != NULL) { |
227 | | /* |
228 | | * We're about to get a new digest so clear anything associated with |
229 | | * an old digest. |
230 | | */ |
231 | 37.2k | evp_md_ctx_clear_digest(ctx, 1, 0); |
232 | | |
233 | | /* legacy code support for engines */ |
234 | 37.2k | ERR_set_mark(); |
235 | | /* |
236 | | * This might be requested by a later call to EVP_MD_CTX_get0_md(). |
237 | | * In that case the "explicit fetch" rules apply for that |
238 | | * function (as per man pages), i.e. the ref count is not updated |
239 | | * so the EVP_MD should not be used beyond the lifetime of the |
240 | | * EVP_MD_CTX. |
241 | | */ |
242 | 37.2k | ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props); |
243 | 37.2k | if (ctx->fetched_digest != NULL) { |
244 | 37.2k | ctx->digest = ctx->reqdigest = ctx->fetched_digest; |
245 | 37.2k | } else { |
246 | | #ifdef FIPS_MODULE |
247 | | (void)ERR_clear_last_mark(); |
248 | | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
249 | | goto err; |
250 | | #else |
251 | | /* legacy engine support : remove the mark when this is deleted */ |
252 | 0 | ctx->reqdigest = ctx->digest = EVP_get_digestbyname(mdname); |
253 | 0 | if (ctx->digest == NULL) { |
254 | 0 | (void)ERR_clear_last_mark(); |
255 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
256 | 0 | goto err; |
257 | 0 | } |
258 | 0 | #endif |
259 | 0 | } |
260 | 37.2k | (void)ERR_pop_to_mark(); |
261 | 37.2k | } |
262 | 37.2k | } |
263 | | |
264 | 43.4k | if (ver) { |
265 | 17.4k | if (signature->digest_verify_init == NULL) { |
266 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
267 | 0 | goto err; |
268 | 0 | } |
269 | 17.4k | ret = signature->digest_verify_init(locpctx->op.sig.algctx, |
270 | 17.4k | mdname, provkey, params); |
271 | 26.0k | } else { |
272 | 26.0k | if (signature->digest_sign_init == NULL) { |
273 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
274 | 0 | goto err; |
275 | 0 | } |
276 | 26.0k | ret = signature->digest_sign_init(locpctx->op.sig.algctx, |
277 | 26.0k | mdname, provkey, params); |
278 | 26.0k | } |
279 | | |
280 | | /* |
281 | | * If the operation was not a success and no digest was found, an error |
282 | | * needs to be raised. |
283 | | */ |
284 | 43.4k | if (ret > 0 || mdname != NULL) |
285 | 43.4k | goto end; |
286 | 0 | if (type == NULL) /* This check is redundant but clarifies matters */ |
287 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST); |
288 | |
|
289 | 0 | err: |
290 | 0 | evp_pkey_ctx_free_old_ops(locpctx); |
291 | 0 | locpctx->operation = EVP_PKEY_OP_UNDEFINED; |
292 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
293 | 0 | return 0; |
294 | | |
295 | 0 | legacy: |
296 | | /* |
297 | | * If we don't have the full support we need with provided methods, |
298 | | * let's go see if legacy does. |
299 | | */ |
300 | 0 | ERR_pop_to_mark(); |
301 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
302 | 0 | tmp_keymgmt = NULL; |
303 | |
|
304 | | #ifdef FIPS_MODULE |
305 | | return 0; |
306 | | #else |
307 | 0 | if (type == NULL && mdname != NULL) |
308 | 0 | type = evp_get_digestbyname_ex(locpctx->libctx, mdname); |
309 | |
|
310 | 0 | if (ctx->pctx->pmeth == NULL) { |
311 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE); |
312 | 0 | return 0; |
313 | 0 | } |
314 | | |
315 | 0 | if (!(ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM)) { |
316 | |
|
317 | 0 | if (type == NULL) { |
318 | 0 | int def_nid; |
319 | 0 | if (EVP_PKEY_get_default_digest_nid(pkey, &def_nid) > 0) |
320 | 0 | type = EVP_get_digestbynid(def_nid); |
321 | 0 | } |
322 | |
|
323 | 0 | if (type == NULL) { |
324 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST); |
325 | 0 | return 0; |
326 | 0 | } |
327 | 0 | } |
328 | | |
329 | 0 | if (ver) { |
330 | 0 | if (ctx->pctx->pmeth->verifyctx_init) { |
331 | 0 | if (ctx->pctx->pmeth->verifyctx_init(ctx->pctx, ctx) <= 0) |
332 | 0 | return 0; |
333 | 0 | ctx->pctx->operation = EVP_PKEY_OP_VERIFYCTX; |
334 | 0 | } else if (ctx->pctx->pmeth->digestverify != 0) { |
335 | 0 | ctx->pctx->operation = EVP_PKEY_OP_VERIFY; |
336 | 0 | ctx->update = update; |
337 | 0 | } else if (EVP_PKEY_verify_init(ctx->pctx) <= 0) { |
338 | 0 | return 0; |
339 | 0 | } |
340 | 0 | } else { |
341 | 0 | if (ctx->pctx->pmeth->signctx_init) { |
342 | 0 | if (ctx->pctx->pmeth->signctx_init(ctx->pctx, ctx) <= 0) |
343 | 0 | return 0; |
344 | 0 | ctx->pctx->operation = EVP_PKEY_OP_SIGNCTX; |
345 | 0 | } else if (ctx->pctx->pmeth->digestsign != 0) { |
346 | 0 | ctx->pctx->operation = EVP_PKEY_OP_SIGN; |
347 | 0 | ctx->update = update; |
348 | 0 | } else if (EVP_PKEY_sign_init(ctx->pctx) <= 0) { |
349 | 0 | return 0; |
350 | 0 | } |
351 | 0 | } |
352 | 0 | if (EVP_PKEY_CTX_set_signature_md(ctx->pctx, type) <= 0) |
353 | 0 | return 0; |
354 | 0 | if (pctx) |
355 | 0 | *pctx = ctx->pctx; |
356 | 0 | if (ctx->pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) |
357 | 0 | return 1; |
358 | 0 | if (!EVP_DigestInit_ex(ctx, type, e)) |
359 | 0 | return 0; |
360 | | /* |
361 | | * This indicates the current algorithm requires |
362 | | * special treatment before hashing the tbs-message. |
363 | | */ |
364 | 0 | ctx->pctx->flag_call_digest_custom = 0; |
365 | 0 | if (ctx->pctx->pmeth->digest_custom != NULL) |
366 | 0 | ctx->pctx->flag_call_digest_custom = 1; |
367 | |
|
368 | 0 | ret = 1; |
369 | 0 | #endif |
370 | 43.4k | end: |
371 | 43.4k | #ifndef FIPS_MODULE |
372 | 43.4k | if (ret > 0) |
373 | 43.3k | ret = evp_pkey_ctx_use_cached_data(locpctx); |
374 | 43.4k | #endif |
375 | | |
376 | 43.4k | EVP_KEYMGMT_free(tmp_keymgmt); |
377 | 43.4k | return ret > 0 ? 1 : 0; |
378 | 0 | } |
379 | | |
380 | | int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
381 | | const char *mdname, OSSL_LIB_CTX *libctx, |
382 | | const char *props, EVP_PKEY *pkey, |
383 | | const OSSL_PARAM params[]) |
384 | 64.4k | { |
385 | 64.4k | return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 0, |
386 | 64.4k | params); |
387 | 64.4k | } |
388 | | |
389 | | #ifndef FIPS_MODULE |
390 | | int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
391 | | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) |
392 | 0 | { |
393 | 0 | return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 0, |
394 | 0 | NULL); |
395 | 0 | } |
396 | | #endif |
397 | | |
398 | | int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
399 | | const char *mdname, OSSL_LIB_CTX *libctx, |
400 | | const char *props, EVP_PKEY *pkey, |
401 | | const OSSL_PARAM params[]) |
402 | 19.9k | { |
403 | 19.9k | return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 1, |
404 | 19.9k | params); |
405 | 19.9k | } |
406 | | |
407 | | #ifndef FIPS_MODULE |
408 | | int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
409 | | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) |
410 | 12.4k | { |
411 | 12.4k | return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 1, |
412 | 12.4k | NULL); |
413 | 12.4k | } |
414 | | #endif /* FIPS_MODULE */ |
415 | | |
416 | | int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize) |
417 | 357k | { |
418 | 357k | EVP_PKEY_CTX *pctx = ctx->pctx; |
419 | | |
420 | 357k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
421 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
422 | 0 | return 0; |
423 | 0 | } |
424 | | |
425 | 357k | if (pctx == NULL |
426 | 357k | || pctx->operation != EVP_PKEY_OP_SIGNCTX |
427 | 357k | || pctx->op.sig.algctx == NULL |
428 | 357k | || pctx->op.sig.signature == NULL) |
429 | 0 | goto legacy; |
430 | | |
431 | 357k | if (pctx->op.sig.signature->digest_sign_update == NULL) { |
432 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
433 | 0 | return 0; |
434 | 0 | } |
435 | | |
436 | 357k | return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.algctx, |
437 | 357k | data, dsize); |
438 | | |
439 | 0 | legacy: |
440 | | #ifdef FIPS_MODULE |
441 | | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
442 | | return 0; |
443 | | #else |
444 | 0 | if (pctx != NULL) { |
445 | | /* do_sigver_init() checked that |digest_custom| is non-NULL */ |
446 | 0 | if (pctx->flag_call_digest_custom |
447 | 0 | && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx)) |
448 | 0 | return 0; |
449 | 0 | pctx->flag_call_digest_custom = 0; |
450 | 0 | } |
451 | | |
452 | 0 | return EVP_DigestUpdate(ctx, data, dsize); |
453 | 0 | #endif |
454 | 0 | } |
455 | | |
456 | | int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize) |
457 | 17.3k | { |
458 | 17.3k | EVP_PKEY_CTX *pctx = ctx->pctx; |
459 | | |
460 | 17.3k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
461 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
462 | 0 | return 0; |
463 | 0 | } |
464 | | |
465 | 17.3k | if (pctx == NULL |
466 | 17.3k | || pctx->operation != EVP_PKEY_OP_VERIFYCTX |
467 | 17.3k | || pctx->op.sig.algctx == NULL |
468 | 17.3k | || pctx->op.sig.signature == NULL) |
469 | 0 | goto legacy; |
470 | | |
471 | 17.3k | if (pctx->op.sig.signature->digest_verify_update == NULL) { |
472 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
473 | 0 | return 0; |
474 | 0 | } |
475 | | |
476 | 17.3k | return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.algctx, |
477 | 17.3k | data, dsize); |
478 | | |
479 | 0 | legacy: |
480 | | #ifdef FIPS_MODULE |
481 | | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
482 | | return 0; |
483 | | #else |
484 | 0 | if (pctx != NULL) { |
485 | | /* do_sigver_init() checked that |digest_custom| is non-NULL */ |
486 | 0 | if (pctx->flag_call_digest_custom |
487 | 0 | && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx)) |
488 | 0 | return 0; |
489 | 0 | pctx->flag_call_digest_custom = 0; |
490 | 0 | } |
491 | | |
492 | 0 | return EVP_DigestUpdate(ctx, data, dsize); |
493 | 0 | #endif |
494 | 0 | } |
495 | | |
496 | | int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, |
497 | | size_t *siglen) |
498 | 71.7k | { |
499 | 71.7k | #ifndef FIPS_MODULE |
500 | 71.7k | int sctx = 0; |
501 | 71.7k | #endif |
502 | 71.7k | int r = 0; |
503 | 71.7k | EVP_PKEY_CTX *dctx = NULL, *pctx = ctx->pctx; |
504 | | |
505 | 71.7k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
506 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
507 | 0 | return 0; |
508 | 0 | } |
509 | | |
510 | 71.7k | if (pctx == NULL |
511 | 71.7k | || pctx->operation != EVP_PKEY_OP_SIGNCTX |
512 | 71.7k | || pctx->op.sig.algctx == NULL |
513 | 71.7k | || pctx->op.sig.signature == NULL) |
514 | 0 | goto legacy; |
515 | | |
516 | 71.7k | #ifndef FIPS_MODULE |
517 | 71.7k | if (sigret != NULL && (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) == 0) { |
518 | | /* try dup */ |
519 | 70.3k | dctx = EVP_PKEY_CTX_dup(pctx); |
520 | 70.3k | if (dctx != NULL) |
521 | 70.3k | pctx = dctx; |
522 | 70.3k | } |
523 | 71.7k | #endif |
524 | 71.7k | r = pctx->op.sig.signature->digest_sign_final(pctx->op.sig.algctx, |
525 | 71.7k | sigret, siglen, |
526 | 71.7k | sigret == NULL ? 0 : *siglen); |
527 | 71.7k | if (dctx == NULL && sigret != NULL) |
528 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
529 | 71.7k | else |
530 | 71.7k | EVP_PKEY_CTX_free(dctx); |
531 | 71.7k | return r; |
532 | | |
533 | 0 | legacy: |
534 | | #ifdef FIPS_MODULE |
535 | | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
536 | | return 0; |
537 | | #else |
538 | 0 | if (pctx == NULL || pctx->pmeth == NULL) { |
539 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
540 | 0 | return 0; |
541 | 0 | } |
542 | | |
543 | | /* do_sigver_init() checked that |digest_custom| is non-NULL */ |
544 | 0 | if (pctx->flag_call_digest_custom |
545 | 0 | && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx)) |
546 | 0 | return 0; |
547 | 0 | pctx->flag_call_digest_custom = 0; |
548 | |
|
549 | 0 | if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) { |
550 | 0 | if (sigret == NULL) |
551 | 0 | return pctx->pmeth->signctx(pctx, sigret, siglen, ctx); |
552 | 0 | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0) { |
553 | 0 | r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx); |
554 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
555 | 0 | } else { |
556 | 0 | dctx = EVP_PKEY_CTX_dup(pctx); |
557 | 0 | if (dctx == NULL) |
558 | 0 | return 0; |
559 | 0 | r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx); |
560 | 0 | EVP_PKEY_CTX_free(dctx); |
561 | 0 | } |
562 | 0 | return r; |
563 | 0 | } |
564 | 0 | if (pctx->pmeth->signctx != NULL) |
565 | 0 | sctx = 1; |
566 | 0 | else |
567 | 0 | sctx = 0; |
568 | 0 | if (sigret != NULL) { |
569 | 0 | unsigned char md[EVP_MAX_MD_SIZE]; |
570 | 0 | unsigned int mdlen = 0; |
571 | |
|
572 | 0 | if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) { |
573 | 0 | if (sctx) |
574 | 0 | r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx); |
575 | 0 | else |
576 | 0 | r = EVP_DigestFinal_ex(ctx, md, &mdlen); |
577 | 0 | } else { |
578 | 0 | EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new(); |
579 | |
|
580 | 0 | if (tmp_ctx == NULL) |
581 | 0 | return 0; |
582 | 0 | if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) { |
583 | 0 | EVP_MD_CTX_free(tmp_ctx); |
584 | 0 | return 0; |
585 | 0 | } |
586 | 0 | if (sctx) |
587 | 0 | r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx, |
588 | 0 | sigret, siglen, tmp_ctx); |
589 | 0 | else |
590 | 0 | r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen); |
591 | 0 | EVP_MD_CTX_free(tmp_ctx); |
592 | 0 | } |
593 | 0 | if (sctx || !r) |
594 | 0 | return r; |
595 | 0 | if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0) |
596 | 0 | return 0; |
597 | 0 | } else { |
598 | 0 | if (sctx) { |
599 | 0 | if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0) |
600 | 0 | return 0; |
601 | 0 | } else { |
602 | 0 | int s = EVP_MD_get_size(ctx->digest); |
603 | |
|
604 | 0 | if (s <= 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0) |
605 | 0 | return 0; |
606 | 0 | } |
607 | 0 | } |
608 | 0 | return 1; |
609 | 0 | #endif |
610 | 0 | } |
611 | | |
612 | | int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen, |
613 | | const unsigned char *tbs, size_t tbslen) |
614 | 8.88k | { |
615 | 8.88k | EVP_PKEY_CTX *pctx = ctx->pctx; |
616 | | |
617 | 8.88k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
618 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
619 | 0 | return 0; |
620 | 0 | } |
621 | | |
622 | 8.88k | if (pctx != NULL |
623 | 8.88k | && pctx->operation == EVP_PKEY_OP_SIGNCTX |
624 | 8.88k | && pctx->op.sig.algctx != NULL |
625 | 8.88k | && pctx->op.sig.signature != NULL) { |
626 | 8.88k | if (pctx->op.sig.signature->digest_sign != NULL) { |
627 | 0 | if (sigret != NULL) |
628 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
629 | 0 | return pctx->op.sig.signature->digest_sign(pctx->op.sig.algctx, |
630 | 0 | sigret, siglen, |
631 | 0 | sigret == NULL ? 0 : *siglen, |
632 | 0 | tbs, tbslen); |
633 | 0 | } |
634 | | #ifdef FIPS_MODULE |
635 | | } |
636 | | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
637 | | return 0; |
638 | | #else |
639 | 8.88k | } else { |
640 | | /* legacy */ |
641 | 0 | if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL) |
642 | 0 | return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen); |
643 | 0 | } |
644 | | |
645 | 8.88k | if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0) |
646 | 0 | return 0; |
647 | 8.88k | return EVP_DigestSignFinal(ctx, sigret, siglen); |
648 | 8.88k | #endif |
649 | 8.88k | } |
650 | | |
651 | | int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, |
652 | | size_t siglen) |
653 | 17.3k | { |
654 | 17.3k | #ifndef FIPS_MODULE |
655 | 17.3k | int vctx = 0; |
656 | 17.3k | unsigned int mdlen = 0; |
657 | 17.3k | unsigned char md[EVP_MAX_MD_SIZE]; |
658 | 17.3k | #endif |
659 | 17.3k | int r = 0; |
660 | 17.3k | EVP_PKEY_CTX *dctx = NULL, *pctx = ctx->pctx; |
661 | | |
662 | 17.3k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
663 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
664 | 0 | return 0; |
665 | 0 | } |
666 | | |
667 | 17.3k | if (pctx == NULL |
668 | 17.3k | || pctx->operation != EVP_PKEY_OP_VERIFYCTX |
669 | 17.3k | || pctx->op.sig.algctx == NULL |
670 | 17.3k | || pctx->op.sig.signature == NULL) |
671 | 0 | goto legacy; |
672 | | |
673 | 17.3k | #ifndef FIPS_MODULE |
674 | 17.3k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) == 0) { |
675 | | /* try dup */ |
676 | 17.3k | dctx = EVP_PKEY_CTX_dup(pctx); |
677 | 17.3k | if (dctx != NULL) |
678 | 17.3k | pctx = dctx; |
679 | 17.3k | } |
680 | 17.3k | #endif |
681 | 17.3k | r = pctx->op.sig.signature->digest_verify_final(pctx->op.sig.algctx, |
682 | 17.3k | sig, siglen); |
683 | 17.3k | if (dctx == NULL) |
684 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
685 | 17.3k | else |
686 | 17.3k | EVP_PKEY_CTX_free(dctx); |
687 | 17.3k | return r; |
688 | | |
689 | 0 | legacy: |
690 | | #ifdef FIPS_MODULE |
691 | | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
692 | | return 0; |
693 | | #else |
694 | 0 | if (pctx == NULL || pctx->pmeth == NULL) { |
695 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
696 | 0 | return 0; |
697 | 0 | } |
698 | | |
699 | | /* do_sigver_init() checked that |digest_custom| is non-NULL */ |
700 | 0 | if (pctx->flag_call_digest_custom |
701 | 0 | && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx)) |
702 | 0 | return 0; |
703 | 0 | pctx->flag_call_digest_custom = 0; |
704 | |
|
705 | 0 | if (pctx->pmeth->verifyctx != NULL) |
706 | 0 | vctx = 1; |
707 | 0 | else |
708 | 0 | vctx = 0; |
709 | 0 | if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) { |
710 | 0 | if (vctx) { |
711 | 0 | r = pctx->pmeth->verifyctx(pctx, sig, siglen, ctx); |
712 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
713 | 0 | } else |
714 | 0 | r = EVP_DigestFinal_ex(ctx, md, &mdlen); |
715 | 0 | } else { |
716 | 0 | EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new(); |
717 | 0 | if (tmp_ctx == NULL) |
718 | 0 | return -1; |
719 | 0 | if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) { |
720 | 0 | EVP_MD_CTX_free(tmp_ctx); |
721 | 0 | return -1; |
722 | 0 | } |
723 | 0 | if (vctx) |
724 | 0 | r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx, |
725 | 0 | sig, siglen, tmp_ctx); |
726 | 0 | else |
727 | 0 | r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen); |
728 | 0 | EVP_MD_CTX_free(tmp_ctx); |
729 | 0 | } |
730 | 0 | if (vctx || !r) |
731 | 0 | return r; |
732 | 0 | return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen); |
733 | 0 | #endif |
734 | 0 | } |
735 | | |
736 | | int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret, |
737 | | size_t siglen, const unsigned char *tbs, size_t tbslen) |
738 | 17.3k | { |
739 | 17.3k | EVP_PKEY_CTX *pctx = ctx->pctx; |
740 | | |
741 | 17.3k | if (pctx == NULL) { |
742 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
743 | 0 | return -1; |
744 | 0 | } |
745 | | |
746 | 17.3k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
747 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
748 | 0 | return 0; |
749 | 0 | } |
750 | | |
751 | 17.3k | if (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
752 | 17.3k | && pctx->op.sig.algctx != NULL |
753 | 17.3k | && pctx->op.sig.signature != NULL) { |
754 | 17.3k | if (pctx->op.sig.signature->digest_verify != NULL) { |
755 | 64 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
756 | 64 | return pctx->op.sig.signature->digest_verify(pctx->op.sig.algctx, |
757 | 64 | sigret, siglen, |
758 | 64 | tbs, tbslen); |
759 | 64 | } |
760 | | #ifdef FIPS_MODULE |
761 | | } |
762 | | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
763 | | return 0; |
764 | | #else |
765 | 17.3k | } else { |
766 | | /* legacy */ |
767 | 0 | if (pctx->pmeth != NULL && pctx->pmeth->digestverify != NULL) |
768 | 0 | return pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen); |
769 | 0 | } |
770 | 17.3k | if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0) |
771 | 0 | return -1; |
772 | 17.3k | return EVP_DigestVerifyFinal(ctx, sigret, siglen); |
773 | 17.3k | #endif |
774 | 17.3k | } |