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