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