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