/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 | | /* legacy code support for engines */ |
221 | 0 | ERR_set_mark(); |
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 | 0 | ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props); |
230 | 0 | if (ctx->fetched_digest != NULL) { |
231 | 0 | ctx->digest = ctx->reqdigest = ctx->fetched_digest; |
232 | 0 | } else { |
233 | | /* legacy engine support : remove the mark when this is deleted */ |
234 | 0 | ctx->reqdigest = ctx->digest = EVP_get_digestbyname(mdname); |
235 | 0 | if (ctx->digest == NULL) { |
236 | 0 | (void)ERR_clear_last_mark(); |
237 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
238 | 0 | goto err; |
239 | 0 | } |
240 | 0 | } |
241 | 0 | (void)ERR_pop_to_mark(); |
242 | 0 | } |
243 | 0 | } |
244 | | |
245 | 0 | desc = signature->description != NULL ? signature->description : ""; |
246 | 0 | if (ver) { |
247 | 0 | if (signature->digest_verify_init == NULL) { |
248 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
249 | 0 | "%s digest_verify_init:%s", signature->type_name, desc); |
250 | 0 | goto err; |
251 | 0 | } |
252 | 0 | ret = signature->digest_verify_init(locpctx->op.sig.algctx, |
253 | 0 | mdname, provkey, params); |
254 | 0 | } else { |
255 | 0 | if (signature->digest_sign_init == NULL) { |
256 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
257 | 0 | "%s digest_sign_init:%s", signature->type_name, desc); |
258 | 0 | goto err; |
259 | 0 | } |
260 | 0 | ret = signature->digest_sign_init(locpctx->op.sig.algctx, |
261 | 0 | mdname, provkey, params); |
262 | 0 | } |
263 | | |
264 | | /* |
265 | | * If the operation was not a success and no digest was found, an error |
266 | | * needs to be raised. |
267 | | */ |
268 | 0 | if (ret > 0 || mdname != NULL) { |
269 | 0 | if (ret > 0) |
270 | 0 | ret = evp_pkey_ctx_use_cached_data(locpctx); |
271 | |
|
272 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
273 | 0 | return ret > 0 ? 1 : 0; |
274 | 0 | } |
275 | 0 | if (type == NULL) /* This check is redundant but clarifies matters */ |
276 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST); |
277 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
278 | 0 | ver ? "%s digest_verify_init:%s" : "%s digest_sign_init:%s", |
279 | 0 | signature->type_name, desc); |
280 | |
|
281 | 0 | err: |
282 | 0 | evp_pkey_ctx_free_old_ops(locpctx); |
283 | 0 | locpctx->operation = EVP_PKEY_OP_UNDEFINED; |
284 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
285 | 0 | return 0; |
286 | | |
287 | 0 | notsupported: |
288 | 0 | ERR_pop_to_mark(); |
289 | 0 | EVP_KEYMGMT_free(tmp_keymgmt); |
290 | |
|
291 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE, |
292 | 0 | ver ? "%s digest_verify_init" : "%s digest_sign_init", |
293 | 0 | EVP_PKEY_get0_type_name(locpctx->pkey)); |
294 | 0 | return 0; |
295 | 0 | } |
296 | | |
297 | | int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
298 | | const char *mdname, OSSL_LIB_CTX *libctx, |
299 | | const char *props, EVP_PKEY *pkey, |
300 | | const OSSL_PARAM params[]) |
301 | 0 | { |
302 | 0 | return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, pkey, 0, |
303 | 0 | params); |
304 | 0 | } |
305 | | |
306 | | int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
307 | | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) |
308 | 0 | { |
309 | 0 | if (!ossl_assert(e == NULL)) |
310 | 0 | return 0; |
311 | 0 | return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, pkey, 0, |
312 | 0 | NULL); |
313 | 0 | } |
314 | | |
315 | | int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
316 | | const char *mdname, OSSL_LIB_CTX *libctx, |
317 | | const char *props, EVP_PKEY *pkey, |
318 | | const OSSL_PARAM params[]) |
319 | 0 | { |
320 | 0 | return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, pkey, 1, |
321 | 0 | params); |
322 | 0 | } |
323 | | |
324 | | int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, |
325 | | const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) |
326 | 0 | { |
327 | 0 | if (!ossl_assert(e == NULL)) |
328 | 0 | return 0; |
329 | 0 | return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, pkey, 1, |
330 | 0 | NULL); |
331 | 0 | } |
332 | | |
333 | | int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize) |
334 | 0 | { |
335 | 0 | EVP_SIGNATURE *signature; |
336 | 0 | const char *desc; |
337 | 0 | EVP_PKEY_CTX *pctx = ctx->pctx; |
338 | 0 | int ret; |
339 | |
|
340 | 0 | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
341 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
342 | 0 | return 0; |
343 | 0 | } |
344 | | |
345 | 0 | if (pctx == NULL) |
346 | 0 | return EVP_DigestUpdate(ctx, data, dsize); |
347 | | |
348 | 0 | if (pctx->operation != EVP_PKEY_OP_SIGNCTX |
349 | 0 | || pctx->op.sig.algctx == NULL |
350 | 0 | || pctx->op.sig.signature == NULL) { |
351 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
352 | 0 | return 0; |
353 | 0 | } |
354 | | |
355 | 0 | signature = pctx->op.sig.signature; |
356 | 0 | desc = signature->description != NULL ? signature->description : ""; |
357 | 0 | if (signature->digest_sign_update == NULL) { |
358 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
359 | 0 | "%s digest_sign_update:%s", signature->type_name, desc); |
360 | 0 | return 0; |
361 | 0 | } |
362 | | |
363 | 0 | ERR_set_mark(); |
364 | 0 | ret = signature->digest_sign_update(pctx->op.sig.algctx, data, dsize); |
365 | 0 | if (ret <= 0 && ERR_count_to_mark() == 0) |
366 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
367 | 0 | "%s digest_sign_update:%s", signature->type_name, desc); |
368 | 0 | ERR_clear_last_mark(); |
369 | 0 | return ret; |
370 | 0 | } |
371 | | |
372 | | int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize) |
373 | 0 | { |
374 | 0 | EVP_SIGNATURE *signature; |
375 | 0 | const char *desc; |
376 | 0 | EVP_PKEY_CTX *pctx = ctx->pctx; |
377 | 0 | int ret; |
378 | |
|
379 | 0 | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
380 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_UPDATE_ERROR); |
381 | 0 | return 0; |
382 | 0 | } |
383 | | |
384 | 0 | if (pctx == NULL |
385 | 0 | || pctx->operation != EVP_PKEY_OP_VERIFYCTX |
386 | 0 | || pctx->op.sig.algctx == NULL |
387 | 0 | || pctx->op.sig.signature == NULL) |
388 | 0 | return EVP_DigestUpdate(ctx, data, dsize); |
389 | | |
390 | 0 | signature = pctx->op.sig.signature; |
391 | 0 | desc = signature->description != NULL ? signature->description : ""; |
392 | 0 | if (signature->digest_verify_update == NULL) { |
393 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
394 | 0 | "%s digest_verify_update:%s", signature->type_name, desc); |
395 | 0 | return 0; |
396 | 0 | } |
397 | | |
398 | 0 | ERR_set_mark(); |
399 | 0 | ret = signature->digest_verify_update(pctx->op.sig.algctx, data, dsize); |
400 | 0 | if (ret <= 0 && ERR_count_to_mark() == 0) |
401 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
402 | 0 | "%s digest_verify_update:%s", signature->type_name, desc); |
403 | 0 | ERR_clear_last_mark(); |
404 | 0 | return ret; |
405 | 0 | } |
406 | | |
407 | | int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, |
408 | | size_t *siglen) |
409 | 0 | { |
410 | 0 | EVP_SIGNATURE *signature; |
411 | 0 | const char *desc; |
412 | 0 | int r = 0; |
413 | 0 | EVP_PKEY_CTX *dctx = NULL, *pctx = ctx->pctx; |
414 | |
|
415 | 0 | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
416 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
417 | 0 | return 0; |
418 | 0 | } |
419 | | |
420 | 0 | if (pctx == NULL |
421 | 0 | || pctx->operation != EVP_PKEY_OP_SIGNCTX |
422 | 0 | || pctx->op.sig.algctx == NULL |
423 | 0 | || pctx->op.sig.signature == NULL) { |
424 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
425 | 0 | return 0; |
426 | 0 | } |
427 | | |
428 | 0 | signature = pctx->op.sig.signature; |
429 | 0 | desc = signature->description != NULL ? signature->description : ""; |
430 | 0 | if (signature->digest_sign_final == NULL) { |
431 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
432 | 0 | "%s digest_sign_final:%s", signature->type_name, desc); |
433 | 0 | return 0; |
434 | 0 | } |
435 | | |
436 | 0 | if (sigret != NULL && (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) == 0) { |
437 | | /* try dup */ |
438 | 0 | dctx = EVP_PKEY_CTX_dup(pctx); |
439 | 0 | if (dctx != NULL) |
440 | 0 | pctx = dctx; |
441 | 0 | } |
442 | |
|
443 | 0 | ERR_set_mark(); |
444 | 0 | r = signature->digest_sign_final(pctx->op.sig.algctx, sigret, siglen, |
445 | 0 | sigret == NULL ? 0 : *siglen); |
446 | 0 | if (!r && ERR_count_to_mark() == 0) |
447 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
448 | 0 | "%s digest_sign_final:%s", signature->type_name, desc); |
449 | 0 | ERR_clear_last_mark(); |
450 | 0 | if (dctx == NULL && sigret != NULL) |
451 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
452 | 0 | else |
453 | 0 | EVP_PKEY_CTX_free(dctx); |
454 | 0 | return r; |
455 | 0 | } |
456 | | |
457 | | int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen, |
458 | | const unsigned char *tbs, size_t tbslen) |
459 | 0 | { |
460 | 0 | EVP_PKEY_CTX *pctx = ctx->pctx; |
461 | 0 | int ret; |
462 | |
|
463 | 0 | if (pctx == NULL) { |
464 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
465 | 0 | return 0; |
466 | 0 | } |
467 | | |
468 | 0 | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
469 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
470 | 0 | return 0; |
471 | 0 | } |
472 | | |
473 | 0 | if (pctx->operation == EVP_PKEY_OP_SIGNCTX |
474 | 0 | && pctx->op.sig.algctx != NULL |
475 | 0 | && pctx->op.sig.signature != NULL) { |
476 | 0 | EVP_SIGNATURE *signature = pctx->op.sig.signature; |
477 | |
|
478 | 0 | if (signature->digest_sign != NULL) { |
479 | 0 | const char *desc = signature->description != NULL ? signature->description : ""; |
480 | |
|
481 | 0 | if (sigret != NULL) |
482 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
483 | 0 | ERR_set_mark(); |
484 | 0 | ret = signature->digest_sign(pctx->op.sig.algctx, sigret, siglen, |
485 | 0 | sigret == NULL ? 0 : *siglen, tbs, tbslen); |
486 | 0 | if (ret <= 0 && ERR_count_to_mark() == 0) |
487 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
488 | 0 | "%s digest_sign:%s", signature->type_name, desc); |
489 | 0 | ERR_clear_last_mark(); |
490 | 0 | return ret; |
491 | 0 | } |
492 | 0 | } |
493 | | |
494 | 0 | if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0) |
495 | 0 | return 0; |
496 | 0 | return EVP_DigestSignFinal(ctx, sigret, siglen); |
497 | 0 | } |
498 | | |
499 | | int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, |
500 | | size_t siglen) |
501 | 0 | { |
502 | 0 | EVP_SIGNATURE *signature; |
503 | 0 | const char *desc; |
504 | 0 | int r = 0; |
505 | 0 | EVP_PKEY_CTX *dctx = NULL, *pctx = ctx->pctx; |
506 | |
|
507 | 0 | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
508 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
509 | 0 | return 0; |
510 | 0 | } |
511 | | |
512 | 0 | if (pctx == NULL |
513 | 0 | || pctx->operation != EVP_PKEY_OP_VERIFYCTX |
514 | 0 | || pctx->op.sig.algctx == NULL |
515 | 0 | || pctx->op.sig.signature == NULL) { |
516 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
517 | 0 | return 0; |
518 | 0 | } |
519 | | |
520 | 0 | signature = pctx->op.sig.signature; |
521 | 0 | desc = signature->description != NULL ? signature->description : ""; |
522 | 0 | if (signature->digest_verify_final == NULL) { |
523 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_NOT_SUPPORTED, |
524 | 0 | "%s digest_verify_final:%s", signature->type_name, desc); |
525 | 0 | return 0; |
526 | 0 | } |
527 | | |
528 | 0 | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) == 0) { |
529 | | /* try dup */ |
530 | 0 | dctx = EVP_PKEY_CTX_dup(pctx); |
531 | 0 | if (dctx != NULL) |
532 | 0 | pctx = dctx; |
533 | 0 | } |
534 | |
|
535 | 0 | ERR_set_mark(); |
536 | 0 | r = signature->digest_verify_final(pctx->op.sig.algctx, sig, siglen); |
537 | 0 | if (!r && ERR_count_to_mark() == 0) |
538 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
539 | 0 | "%s digest_verify_final:%s", signature->type_name, desc); |
540 | 0 | ERR_clear_last_mark(); |
541 | 0 | if (dctx == NULL) |
542 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
543 | 0 | else |
544 | 0 | EVP_PKEY_CTX_free(dctx); |
545 | 0 | return r; |
546 | 0 | } |
547 | | |
548 | | int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret, |
549 | | size_t siglen, const unsigned char *tbs, size_t tbslen) |
550 | 0 | { |
551 | 0 | EVP_PKEY_CTX *pctx = ctx->pctx; |
552 | |
|
553 | 0 | if (pctx == NULL) { |
554 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); |
555 | 0 | return -1; |
556 | 0 | } |
557 | | |
558 | 0 | if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISED) != 0) { |
559 | 0 | ERR_raise(ERR_LIB_EVP, EVP_R_FINAL_ERROR); |
560 | 0 | return 0; |
561 | 0 | } |
562 | | |
563 | 0 | if (pctx->operation == EVP_PKEY_OP_VERIFYCTX |
564 | 0 | && pctx->op.sig.algctx != NULL |
565 | 0 | && pctx->op.sig.signature != NULL) { |
566 | 0 | if (pctx->op.sig.signature->digest_verify != NULL) { |
567 | 0 | EVP_SIGNATURE *signature = pctx->op.sig.signature; |
568 | 0 | const char *desc = signature->description != NULL ? signature->description : ""; |
569 | 0 | int ret; |
570 | |
|
571 | 0 | ctx->flags |= EVP_MD_CTX_FLAG_FINALISED; |
572 | 0 | ERR_set_mark(); |
573 | 0 | ret = signature->digest_verify(pctx->op.sig.algctx, sigret, siglen, tbs, tbslen); |
574 | 0 | if (ret <= 0 && ERR_count_to_mark() == 0) |
575 | 0 | ERR_raise_data(ERR_LIB_EVP, EVP_R_PROVIDER_SIGNATURE_FAILURE, |
576 | 0 | "%s digest_verify:%s", signature->type_name, desc); |
577 | 0 | ERR_clear_last_mark(); |
578 | 0 | return ret; |
579 | 0 | } |
580 | 0 | } |
581 | | |
582 | 0 | if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0) |
583 | 0 | return -1; |
584 | 0 | return EVP_DigestVerifyFinal(ctx, sigret, siglen); |
585 | 0 | } |