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