/src/openssl40/crypto/evp/m_sigver.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2006-2026 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 "internal/common.h" |
18 | | #include "evp_local.h" |
19 | | |
20 | | /* |
21 | | * If we get the "NULL" md then the name comes back as "UNDEF". We want to use |
22 | | * NULL for this. |
23 | | */ |
24 | | static const char *canon_mdname(const char *mdname) |
25 | 9.90k | { |
26 | 9.90k | if (mdname != NULL && strcmp(mdname, "UNDEF") == 0) |
27 | 714 | return NULL; |
28 | | |
29 | 9.19k | return mdname; |
30 | 9.90k | } |
31 | | |
32 | | static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
33 | | const EVP_MD *type, const char *mdname, |
34 | | OSSL_LIB_CTX *libctx, const char *props, |
35 | | EVP_PKEY *pkey, int ver, |
36 | | const OSSL_PARAM params[]) |
37 | 33.2k | { |
38 | 33.2k | EVP_PKEY_CTX *locpctx = NULL; |
39 | 33.2k | EVP_SIGNATURE *signature = NULL; |
40 | 33.2k | const char *desc; |
41 | 33.2k | EVP_KEYMGMT *tmp_keymgmt = NULL; |
42 | 33.2k | const OSSL_PROVIDER *tmp_prov = NULL; |
43 | 33.2k | const char *supported_sig = NULL; |
44 | 33.2k | char locmdname[80] = ""; /* 80 chars should be enough */ |
45 | 33.2k | void *provkey = NULL; |
46 | 33.2k | int ret, iter, reinit = 1; |
47 | | |
48 | 33.2k | if (!evp_md_ctx_free_algctx(ctx)) |
49 | 0 | return 0; |
50 | | |
51 | 33.2k | if (ctx->pctx == NULL) { |
52 | 29.5k | reinit = 0; |
53 | 29.5k | ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props); |
54 | 29.5k | } |
55 | 33.2k | if (ctx->pctx == NULL) |
56 | 0 | return 0; |
57 | | |
58 | 33.2k | EVP_MD_CTX_clear_flags(ctx, EVP_MD_CTX_FLAG_FINALISED); |
59 | | |
60 | 33.2k | locpctx = ctx->pctx; |
61 | 33.2k | ERR_set_mark(); |
62 | | |
63 | 33.2k | if (evp_pkey_ctx_is_legacy(locpctx)) |
64 | 0 | goto notsupported; |
65 | | |
66 | | /* do not reinitialize if pkey is set or operation is different */ |
67 | 33.2k | if (reinit |
68 | 3.68k | && (pkey != NULL |
69 | 0 | || locpctx->operation != (ver ? EVP_PKEY_OP_VERIFYCTX : EVP_PKEY_OP_SIGNCTX) |
70 | 0 | || (signature = locpctx->op.sig.signature) == NULL |
71 | 0 | || locpctx->op.sig.algctx == NULL)) |
72 | 3.68k | reinit = 0; |
73 | | |
74 | 33.2k | if (props == NULL) |
75 | 32.8k | props = locpctx->propquery; |
76 | | |
77 | 33.2k | if (locpctx->pkey == NULL) { |
78 | 0 | ERR_clear_last_mark(); |
79 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); |
80 | 0 | goto err; |
81 | 0 | } |
82 | | |
83 | 33.2k | if (!reinit) { |
84 | 33.2k | evp_pkey_ctx_free_old_ops(locpctx); |
85 | 33.2k | } else { |
86 | 0 | if (mdname == NULL && type == NULL) |
87 | 0 | mdname = canon_mdname(EVP_MD_get0_name(ctx->reqdigest)); |
88 | 0 | goto reinitialize; |
89 | 0 | } |
90 | | |
91 | | /* |
92 | | * Try to derive the supported signature from |locpctx->keymgmt|. |
93 | | */ |
94 | 33.2k | if (!ossl_assert(locpctx->pkey->keymgmt == NULL |
95 | 33.2k | || locpctx->pkey->keymgmt == locpctx->keymgmt)) { |
96 | 0 | ERR_clear_last_mark(); |
97 | 0 | ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); |
98 | 0 | goto err; |
99 | 0 | } |
100 | 33.2k | supported_sig = evp_keymgmt_util_query_operation_name(locpctx->keymgmt, |
101 | 33.2k | OSSL_OP_SIGNATURE); |
102 | 33.2k | if (supported_sig == NULL) { |
103 | 0 | ERR_clear_last_mark(); |
104 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
105 | 0 | goto err; |
106 | 0 | } |
107 | | |
108 | | /* |
109 | | * We perform two iterations: |
110 | | * |
111 | | * 1. Do the normal signature fetch, using the fetching data given by |
112 | | * the EVP_PKEY_CTX. |
113 | | * 2. Do the provider specific signature fetch, from the same provider |
114 | | * as |ctx->keymgmt| |
115 | | * |
116 | | * We then try to fetch the keymgmt from the same provider as the |
117 | | * signature, and try to export |ctx->pkey| to that keymgmt (when |
118 | | * this keymgmt happens to be the same as |ctx->keymgmt|, the export |
119 | | * is a no-op, but we call it anyway to not complicate the code even |
120 | | * more). |
121 | | * If the export call succeeds (returns a non-NULL provider key pointer), |
122 | | * we're done and can perform the operation itself. If not, we perform |
123 | | * the second iteration, or jump to legacy. |
124 | | */ |
125 | 66.5k | for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) { |
126 | 33.2k | EVP_KEYMGMT *tmp_keymgmt_tofree = NULL; |
127 | | |
128 | | /* |
129 | | * If we're on the second iteration, free the results from the first. |
130 | | * They are NULL on the first iteration, so no need to check what |
131 | | * iteration we're on. |
132 | | */ |
133 | 33.2k | EVP_SIGNATURE_free(signature); |
134 | 33.2k | signature = NULL; |
135 | 33.2k | EVP_KEYMGMT_free(tmp_keymgmt); |
136 | 33.2k | tmp_keymgmt = NULL; |
137 | | |
138 | 33.2k | switch (iter) { |
139 | 33.2k | case 1: |
140 | 33.2k | signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig, |
141 | 33.2k | locpctx->propquery); |
142 | 33.2k | if (signature != NULL) |
143 | 33.2k | tmp_prov = EVP_SIGNATURE_get0_provider(signature); |
144 | 33.2k | break; |
145 | 0 | case 2: |
146 | 0 | tmp_prov = EVP_KEYMGMT_get0_provider(locpctx->keymgmt); |
147 | 0 | signature = evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
148 | 0 | supported_sig, locpctx->propquery); |
149 | 0 | if (signature == NULL) |
150 | 0 | goto notsupported; |
151 | 0 | break; |
152 | 33.2k | } |
153 | 33.2k | if (signature == NULL) |
154 | 0 | continue; |
155 | | |
156 | | /* |
157 | | * Ensure that the key is provided, either natively, or as a cached |
158 | | * export. We start by fetching the keymgmt with the same name as |
159 | | * |locpctx->pkey|, but from the provider of the signature method, using |
160 | | * the same property query as when fetching the signature method. |
161 | | * With the keymgmt we found (if we did), we try to export |locpctx->pkey| |
162 | | * to it (evp_pkey_export_to_provider() is smart enough to only actually |
163 | | |
164 | | * export it if |tmp_keymgmt| is different from |locpctx->pkey|'s keymgmt) |
165 | | */ |
166 | 33.2k | tmp_keymgmt_tofree = tmp_keymgmt = evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, |
167 | 33.2k | EVP_KEYMGMT_get0_name(locpctx->keymgmt), |
168 | 33.2k | locpctx->propquery); |
169 | 33.2k | if (tmp_keymgmt != NULL) |
170 | 33.2k | provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx, |
171 | 33.2k | &tmp_keymgmt, locpctx->propquery); |
172 | 33.2k | if (tmp_keymgmt == NULL) |
173 | 0 | EVP_KEYMGMT_free(tmp_keymgmt_tofree); |
174 | 33.2k | } |
175 | | |
176 | 33.2k | if (provkey == NULL) { |
177 | 0 | EVP_SIGNATURE_free(signature); |
178 | 0 | ERR_clear_last_mark(); |
179 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
180 | 0 | goto err; |
181 | 0 | } |
182 | | |
183 | 33.2k | ERR_pop_to_mark(); |
184 | | |
185 | | /* No more legacy from here down to legacy: */ |
186 | | |
187 | 33.2k | locpctx->op.sig.signature = signature; |
188 | 33.2k | locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX |
189 | 33.2k | : EVP_PKEY_OP_SIGNCTX; |
190 | 33.2k | locpctx->op.sig.algctx |
191 | 33.2k | = signature->newctx(ossl_provider_ctx(signature->prov), props); |
192 | 33.2k | if (locpctx->op.sig.algctx == NULL) { |
193 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
194 | 0 | goto err; |
195 | 0 | } |
196 | | |
197 | 33.2k | reinitialize: |
198 | 33.2k | if (pctx != NULL) |
199 | 12.3k | *pctx = locpctx; |
200 | | |
201 | 33.2k | if (type != NULL) { |
202 | 3.66k | ctx->reqdigest = type; |
203 | 3.66k | if (mdname == NULL) |
204 | 3.66k | mdname = canon_mdname(EVP_MD_get0_name(type)); |
205 | 29.5k | } else { |
206 | 29.5k | if (mdname == NULL && !reinit) { |
207 | 402 | if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey, |
208 | 402 | locmdname, |
209 | 402 | sizeof(locmdname)) |
210 | 402 | > 0) { |
211 | 402 | mdname = canon_mdname(locmdname); |
212 | 402 | } |
213 | 402 | } |
214 | | |
215 | 29.5k | if (mdname != NULL) { |
216 | | /* |
217 | | * We're about to get a new digest so clear anything associated with |
218 | | * an old digest. |
219 | | */ |
220 | 29.2k | evp_md_ctx_clear_digest(ctx, 1, 0); |
221 | | |
222 | | /* |
223 | | * This might be requested by a later call to EVP_MD_CTX_get0_md(). |
224 | | * In that case the "explicit fetch" rules apply for that |
225 | | * function (as per man pages), i.e. the ref count is not updated |
226 | | * so the EVP_MD should not be used beyond the lifetime of the |
227 | | * EVP_MD_CTX. |
228 | | */ |
229 | 29.2k | ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props); |
230 | 29.2k | if (ctx->fetched_digest != NULL) { |
231 | 29.2k | ctx->digest = ctx->reqdigest = ctx->fetched_digest; |
232 | 29.2k | if (ctx->digest == NULL) { |
233 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
234 | 0 | goto err; |
235 | 0 | } |
236 | 29.2k | } |
237 | 29.2k | } |
238 | 29.5k | } |
239 | | |
240 | 33.2k | desc = signature->description != NULL ? signature->description : ""; |
241 | 33.2k | if (ver) { |
242 | 10.2k | if (signature->digest_verify_init == NULL) { |
243 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
244 | 0 | "%s digest_verify_init:%s", signature->type_name, desc); |
245 | 0 | goto err; |
246 | 0 | } |
247 | 10.2k | ret = signature->digest_verify_init(locpctx->op.sig.algctx, |
248 | 10.2k | mdname, provkey, params); |
249 | 23.0k | } else { |
250 | 23.0k | if (signature->digest_sign_init == NULL) { |
251 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
252 | 0 | "%s digest_sign_init:%s", signature->type_name, desc); |
253 | 0 | goto err; |
254 | 0 | } |
255 | 23.0k | ret = signature->digest_sign_init(locpctx->op.sig.algctx, |
256 | 23.0k | mdname, provkey, params); |
257 | 23.0k | } |
258 | | |
259 | | /* |
260 | | * If the operation was not a success and no digest was found, an error |
261 | | * needs to be raised. |
262 | | */ |
263 | 33.2k | if (ret > 0 || mdname != NULL) { |
264 | 33.2k | if (ret > 0) |
265 | 33.2k | ret = evp_pkey_ctx_use_cached_data(locpctx); |
266 | | |
267 | 33.2k | EVP_KEYMGMT_free(tmp_keymgmt); |
268 | 33.2k | return ret > 0 ? 1 : 0; |
269 | 33.2k | } |
270 | 0 | if (type == NULL) /* This check is redundant but clarifies matters */ |
271 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST); |
272 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
273 | 0 | ver ? "%s digest_verify_init:%s" : "%s digest_sign_init:%s", |
274 | 0 | signature->type_name, desc); |
275 | |
|
276 | 0 | err: |
277 | 0 | evp_pkey_ctx_free_old_ops(locpctx); |
278 | 0 | locpctx->operation = EVP_PKEY_OP_UNDEFINED; |
279 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
280 | 0 | return 0; |
281 | | |
282 | 0 | notsupported: |
283 | 0 | ERR_pop_to_mark(); |
284 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
285 | |
|
286 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE, |
287 | 0 | ver ? "%s digest_verify_init" : "%s digest_sign_init", |
288 | 0 | EVP_PKEY_get0_type_name(locpctx->pkey)); |
289 | 0 | return 0; |
290 | 0 | } |
291 | | |
292 | | int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
293 | | const char *mdname, OSSL_LIB_CTX *libctx, |
294 | | const char *props, EVP_PKEY *pkey, |
295 | | const OSSL_PARAM params[]) |
296 | 56.5k | { |
297 | 56.5k | return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, pkey, 0, |
298 | 56.5k | params); |
299 | 56.5k | } |
300 | | |
301 | | int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
302 | | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) |
303 | 0 | { |
304 | 0 | if (!ossl_assert(e == NULL)) |
305 | 0 | return 0; |
306 | 0 | return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, pkey, 0, |
307 | 0 | NULL); |
308 | 0 | } |
309 | | |
310 | | int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
311 | | const char *mdname, OSSL_LIB_CTX *libctx, |
312 | | const char *props, EVP_PKEY *pkey, |
313 | | const OSSL_PARAM params[]) |
314 | 17.2k | { |
315 | 17.2k | return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, pkey, 1, |
316 | 17.2k | params); |
317 | 17.2k | } |
318 | | |
319 | | int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
320 | | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) |
321 | 3.68k | { |
322 | 3.68k | if (!ossl_assert(e == NULL)) |
323 | 0 | return 0; |
324 | 3.68k | return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, pkey, 1, |
325 | 3.68k | NULL); |
326 | 3.68k | } |
327 | | |
328 | | int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize) |
329 | 454k | { |
330 | 454k | EVP_SIGNATURE *signature; |
331 | 454k | const char *desc; |
332 | 454k | EVP_PKEY_CTX *pctx = ctx->pctx; |
333 | 454k | int ret; |
334 | | |
335 | 454k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
336 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
337 | 0 | return 0; |
338 | 0 | } |
339 | | |
340 | 454k | if (pctx == NULL) |
341 | 0 | return EVP_DigestUpdate(ctx, data, dsize); |
342 | | |
343 | 454k | if (pctx->operation != EVP_PKEY_OP_SIGNCTX |
344 | 454k | || pctx->op.sig.algctx == NULL |
345 | 454k | || pctx->op.sig.signature == NULL) { |
346 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
347 | 0 | return 0; |
348 | 0 | } |
349 | | |
350 | 454k | signature = pctx->op.sig.signature; |
351 | 454k | desc = signature->description != NULL ? signature->description : ""; |
352 | 454k | if (signature->digest_sign_update == NULL) { |
353 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
354 | 0 | "%s digest_sign_update:%s", signature->type_name, desc); |
355 | 0 | return 0; |
356 | 0 | } |
357 | | |
358 | 454k | ERR_set_mark(); |
359 | 454k | ret = signature->digest_sign_update(pctx->op.sig.algctx, data, dsize); |
360 | 454k | if (ret <= 0 && ERR_count_to_mark() == 0) |
361 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
362 | 0 | "%s digest_sign_update:%s", signature->type_name, desc); |
363 | 454k | ERR_clear_last_mark(); |
364 | 454k | return ret; |
365 | 454k | } |
366 | | |
367 | | int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize) |
368 | 9.99k | { |
369 | 9.99k | EVP_SIGNATURE *signature; |
370 | 9.99k | const char *desc; |
371 | 9.99k | EVP_PKEY_CTX *pctx = ctx->pctx; |
372 | 9.99k | int ret; |
373 | | |
374 | 9.99k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
375 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
376 | 0 | return 0; |
377 | 0 | } |
378 | | |
379 | 9.99k | if (pctx == NULL |
380 | 9.99k | || pctx->operation != EVP_PKEY_OP_VERIFYCTX |
381 | 9.99k | || pctx->op.sig.algctx == NULL |
382 | 9.99k | || pctx->op.sig.signature == NULL) |
383 | 0 | return EVP_DigestUpdate(ctx, data, dsize); |
384 | | |
385 | 9.99k | signature = pctx->op.sig.signature; |
386 | 9.99k | desc = signature->description != NULL ? signature->description : ""; |
387 | 9.99k | if (signature->digest_verify_update == NULL) { |
388 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
389 | 0 | "%s digest_verify_update:%s", signature->type_name, desc); |
390 | 0 | return 0; |
391 | 0 | } |
392 | | |
393 | 9.99k | ERR_set_mark(); |
394 | 9.99k | ret = signature->digest_verify_update(pctx->op.sig.algctx, data, dsize); |
395 | 9.99k | if (ret <= 0 && ERR_count_to_mark() == 0) |
396 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
397 | 0 | "%s digest_verify_update:%s", signature->type_name, desc); |
398 | 9.99k | ERR_clear_last_mark(); |
399 | 9.99k | return ret; |
400 | 9.99k | } |
401 | | |
402 | | int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, |
403 | | size_t *siglen) |
404 | 235k | { |
405 | 235k | EVP_SIGNATURE *signature; |
406 | 235k | const char *desc; |
407 | 235k | int r = 0; |
408 | 235k | EVP_PKEY_CTX *dctx = NULL, *pctx = ctx->pctx; |
409 | | |
410 | 235k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
411 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
412 | 0 | return 0; |
413 | 0 | } |
414 | | |
415 | 235k | if (pctx == NULL |
416 | 235k | || pctx->operation != EVP_PKEY_OP_SIGNCTX |
417 | 235k | || pctx->op.sig.algctx == NULL |
418 | 235k | || pctx->op.sig.signature == NULL) { |
419 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
420 | 0 | return 0; |
421 | 0 | } |
422 | | |
423 | 235k | signature = pctx->op.sig.signature; |
424 | 235k | desc = signature->description != NULL ? signature->description : ""; |
425 | 235k | if (signature->digest_sign_final == NULL) { |
426 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
427 | 0 | "%s digest_sign_final:%s", signature->type_name, desc); |
428 | 0 | return 0; |
429 | 0 | } |
430 | | |
431 | 235k | if (sigret != NULL && (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) == 0) { |
432 | | /* try dup */ |
433 | 230k | dctx = EVP_PKEY_CTX_dup(pctx); |
434 | 230k | if (dctx != NULL) |
435 | 230k | pctx = dctx; |
436 | 230k | } |
437 | | |
438 | 235k | ERR_set_mark(); |
439 | 235k | r = signature->digest_sign_final(pctx->op.sig.algctx, sigret, siglen, |
440 | 235k | sigret == NULL ? 0 : *siglen); |
441 | 235k | if (!r && ERR_count_to_mark() == 0) |
442 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
443 | 0 | "%s digest_sign_final:%s", signature->type_name, desc); |
444 | 235k | ERR_clear_last_mark(); |
445 | 235k | if (dctx == NULL && sigret != NULL) |
446 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
447 | 235k | else |
448 | 235k | EVP_PKEY_CTX_free(dctx); |
449 | 235k | return r; |
450 | 235k | } |
451 | | |
452 | | int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen, |
453 | | const unsigned char *tbs, size_t tbslen) |
454 | 11.1k | { |
455 | 11.1k | EVP_PKEY_CTX *pctx = ctx->pctx; |
456 | 11.1k | int ret; |
457 | | |
458 | 11.1k | if (pctx == NULL) { |
459 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
460 | 0 | return 0; |
461 | 0 | } |
462 | | |
463 | 11.1k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
464 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
465 | 0 | return 0; |
466 | 0 | } |
467 | | |
468 | 11.1k | if (pctx->operation == EVP_PKEY_OP_SIGNCTX |
469 | 11.1k | && pctx->op.sig.algctx != NULL |
470 | 11.1k | && pctx->op.sig.signature != NULL) { |
471 | 11.1k | EVP_SIGNATURE *signature = pctx->op.sig.signature; |
472 | | |
473 | 11.1k | if (signature->digest_sign != NULL) { |
474 | 388 | const char *desc = signature->description != NULL ? signature->description : ""; |
475 | | |
476 | 388 | if (sigret != NULL) |
477 | 194 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
478 | 388 | ERR_set_mark(); |
479 | 388 | ret = signature->digest_sign(pctx->op.sig.algctx, sigret, siglen, |
480 | 388 | sigret == NULL ? 0 : *siglen, tbs, tbslen); |
481 | 388 | if (ret <= 0 && ERR_count_to_mark() == 0) |
482 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
483 | 0 | "%s digest_sign:%s", signature->type_name, desc); |
484 | 388 | ERR_clear_last_mark(); |
485 | 388 | return ret; |
486 | 388 | } |
487 | 11.1k | } |
488 | | |
489 | 10.7k | if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0) |
490 | 0 | return 0; |
491 | 10.7k | return EVP_DigestSignFinal(ctx, sigret, siglen); |
492 | 10.7k | } |
493 | | |
494 | | int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, |
495 | | size_t siglen) |
496 | 9.99k | { |
497 | 9.99k | EVP_SIGNATURE *signature; |
498 | 9.99k | const char *desc; |
499 | 9.99k | int r = 0; |
500 | 9.99k | EVP_PKEY_CTX *dctx = NULL, *pctx = ctx->pctx; |
501 | | |
502 | 9.99k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
503 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
504 | 0 | return 0; |
505 | 0 | } |
506 | | |
507 | 9.99k | if (pctx == NULL |
508 | 9.99k | || pctx->operation != EVP_PKEY_OP_VERIFYCTX |
509 | 9.99k | || pctx->op.sig.algctx == NULL |
510 | 9.99k | || pctx->op.sig.signature == NULL) { |
511 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
512 | 0 | return 0; |
513 | 0 | } |
514 | | |
515 | 9.99k | signature = pctx->op.sig.signature; |
516 | 9.99k | desc = signature->description != NULL ? signature->description : ""; |
517 | 9.99k | if (signature->digest_verify_final == NULL) { |
518 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
519 | 0 | "%s digest_verify_final:%s", signature->type_name, desc); |
520 | 0 | return 0; |
521 | 0 | } |
522 | | |
523 | 9.99k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) == 0) { |
524 | | /* try dup */ |
525 | 9.99k | dctx = EVP_PKEY_CTX_dup(pctx); |
526 | 9.99k | if (dctx != NULL) |
527 | 9.99k | pctx = dctx; |
528 | 9.99k | } |
529 | | |
530 | 9.99k | ERR_set_mark(); |
531 | 9.99k | r = signature->digest_verify_final(pctx->op.sig.algctx, sig, siglen); |
532 | 9.99k | if (!r && ERR_count_to_mark() == 0) |
533 | 983 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
534 | 983 | "%s digest_verify_final:%s", signature->type_name, desc); |
535 | 9.99k | ERR_clear_last_mark(); |
536 | 9.99k | if (dctx == NULL) |
537 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
538 | 9.99k | else |
539 | 9.99k | EVP_PKEY_CTX_free(dctx); |
540 | 9.99k | return r; |
541 | 9.99k | } |
542 | | |
543 | | int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret, |
544 | | size_t siglen, const unsigned char *tbs, size_t tbslen) |
545 | 10.1k | { |
546 | 10.1k | EVP_PKEY_CTX *pctx = ctx->pctx; |
547 | | |
548 | 10.1k | if (pctx == NULL) { |
549 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
550 | 0 | return -1; |
551 | 0 | } |
552 | | |
553 | 10.1k | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
554 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
555 | 0 | return 0; |
556 | 0 | } |
557 | | |
558 | 10.1k | if (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
559 | 10.1k | && pctx->op.sig.algctx != NULL |
560 | 10.1k | && pctx->op.sig.signature != NULL) { |
561 | 10.1k | if (pctx->op.sig.signature->digest_verify != NULL) { |
562 | 194 | EVP_SIGNATURE *signature = pctx->op.sig.signature; |
563 | 194 | const char *desc = signature->description != NULL ? signature->description : ""; |
564 | 194 | int ret; |
565 | | |
566 | 194 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
567 | 194 | ERR_set_mark(); |
568 | 194 | ret = signature->digest_verify(pctx->op.sig.algctx, sigret, siglen, tbs, tbslen); |
569 | 194 | if (ret <= 0 && ERR_count_to_mark() == 0) |
570 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
571 | 0 | "%s digest_verify:%s", signature->type_name, desc); |
572 | 194 | ERR_clear_last_mark(); |
573 | 194 | return ret; |
574 | 194 | } |
575 | 10.1k | } |
576 | | |
577 | 9.99k | if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0) |
578 | 0 | return -1; |
579 | 9.99k | return EVP_DigestVerifyFinal(ctx, sigret, siglen); |
580 | 9.99k | } |