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