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