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