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