/src/openssl30/crypto/evp/m_sigver.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2006-2022 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 |  | #ifndef FIPS_MODULE | 
| 20 |  |  | 
| 21 |  | static int update(EVP_MD_CTX *ctx, const void *data, size_t datalen) | 
| 22 | 0 | { | 
| 23 | 0 |     ERR_raise(ERR_LIB_EVP, EVP_R_ONLY_ONESHOT_SUPPORTED); | 
| 24 | 0 |     return 0; | 
| 25 | 0 | } | 
| 26 |  |  | 
| 27 |  | /* | 
| 28 |  |  * If we get the "NULL" md then the name comes back as "UNDEF". We want to use | 
| 29 |  |  * NULL for this. | 
| 30 |  |  */ | 
| 31 |  | static const char *canon_mdname(const char *mdname) | 
| 32 | 2.53k | { | 
| 33 | 2.53k |     if (mdname != NULL && strcmp(mdname, "UNDEF") == 0) | 
| 34 | 0 |         return NULL; | 
| 35 |  |  | 
| 36 | 2.53k |     return mdname; | 
| 37 | 2.53k | } | 
| 38 |  |  | 
| 39 |  | static int do_sigver_init(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, | 
| 40 |  |                           const EVP_MD *type, const char *mdname, | 
| 41 |  |                           OSSL_LIB_CTX *libctx, const char *props, | 
| 42 |  |                           ENGINE *e, EVP_PKEY *pkey, int ver, | 
| 43 |  |                           const OSSL_PARAM params[]) | 
| 44 | 13.7k | { | 
| 45 | 13.7k |     EVP_PKEY_CTX *locpctx = NULL; | 
| 46 | 13.7k |     EVP_SIGNATURE *signature = NULL; | 
| 47 | 13.7k |     EVP_KEYMGMT *tmp_keymgmt = NULL; | 
| 48 | 13.7k |     const OSSL_PROVIDER *tmp_prov = NULL; | 
| 49 | 13.7k |     const char *supported_sig = NULL; | 
| 50 | 13.7k |     char locmdname[80] = "";     /* 80 chars should be enough */ | 
| 51 | 13.7k |     void *provkey = NULL; | 
| 52 | 13.7k |     int ret, iter, reinit = 1; | 
| 53 |  |  | 
| 54 | 13.7k |     if (ctx->algctx != NULL) { | 
| 55 | 11 |         if (!ossl_assert(ctx->digest != NULL)) { | 
| 56 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 57 | 0 |             return 0; | 
| 58 | 0 |         } | 
| 59 | 11 |         if (ctx->digest->freectx != NULL) | 
| 60 | 11 |             ctx->digest->freectx(ctx->algctx); | 
| 61 | 11 |         ctx->algctx = NULL; | 
| 62 | 11 |     } | 
| 63 |  |  | 
| 64 | 13.7k |     if (ctx->pctx == NULL) { | 
| 65 | 12.9k |         reinit = 0; | 
| 66 | 12.9k |         if (e == NULL) | 
| 67 | 12.9k |             ctx->pctx = EVP_PKEY_CTX_new_from_pkey(libctx, pkey, props); | 
| 68 | 0 |         else | 
| 69 | 0 |             ctx->pctx = EVP_PKEY_CTX_new(pkey, e); | 
| 70 | 12.9k |     } | 
| 71 | 13.7k |     if (ctx->pctx == NULL) | 
| 72 | 0 |         return 0; | 
| 73 |  |  | 
| 74 | 13.7k |     locpctx = ctx->pctx; | 
| 75 | 13.7k |     ERR_set_mark(); | 
| 76 |  |  | 
| 77 | 13.7k |     if (evp_pkey_ctx_is_legacy(locpctx)) | 
| 78 | 0 |         goto legacy; | 
| 79 |  |  | 
| 80 |  |     /* do not reinitialize if pkey is set or operation is different */ | 
| 81 | 13.7k |     if (reinit | 
| 82 | 13.7k |         && (pkey != NULL | 
| 83 | 750 |             || locpctx->operation != (ver ? EVP_PKEY_OP_VERIFYCTX | 
| 84 | 0 |                                           : EVP_PKEY_OP_SIGNCTX) | 
| 85 | 750 |             || (signature = locpctx->op.sig.signature) == NULL | 
| 86 | 750 |             || locpctx->op.sig.algctx == NULL)) | 
| 87 | 750 |         reinit = 0; | 
| 88 |  |  | 
| 89 | 13.7k |     if (props == NULL) | 
| 90 | 13.7k |         props = locpctx->propquery; | 
| 91 |  |  | 
| 92 | 13.7k |     if (locpctx->pkey == NULL) { | 
| 93 | 0 |         ERR_clear_last_mark(); | 
| 94 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_KEY_SET); | 
| 95 | 0 |         goto err; | 
| 96 | 0 |     } | 
| 97 |  |  | 
| 98 | 13.7k |     if (!reinit) { | 
| 99 | 13.7k |         evp_pkey_ctx_free_old_ops(locpctx); | 
| 100 | 13.7k |     } else { | 
| 101 | 0 |         if (mdname == NULL && type == NULL) | 
| 102 | 0 |             mdname = canon_mdname(EVP_MD_get0_name(ctx->reqdigest)); | 
| 103 | 0 |         goto reinitialize; | 
| 104 | 0 |     } | 
| 105 |  |  | 
| 106 |  |     /* | 
| 107 |  |      * Try to derive the supported signature from |locpctx->keymgmt|. | 
| 108 |  |      */ | 
| 109 | 13.7k |     if (!ossl_assert(locpctx->pkey->keymgmt == NULL | 
| 110 | 13.7k |                      || locpctx->pkey->keymgmt == locpctx->keymgmt)) { | 
| 111 | 0 |         ERR_clear_last_mark(); | 
| 112 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR); | 
| 113 | 0 |         goto err; | 
| 114 | 0 |     } | 
| 115 | 13.7k |     supported_sig = evp_keymgmt_util_query_operation_name(locpctx->keymgmt, | 
| 116 | 13.7k |                                                           OSSL_OP_SIGNATURE); | 
| 117 | 13.7k |     if (supported_sig == NULL) { | 
| 118 | 0 |         ERR_clear_last_mark(); | 
| 119 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 120 | 0 |         goto err; | 
| 121 | 0 |     } | 
| 122 |  |  | 
| 123 |  |     /* | 
| 124 |  |      * We perform two iterations: | 
| 125 |  |      * | 
| 126 |  |      * 1.  Do the normal signature fetch, using the fetching data given by | 
| 127 |  |      *     the EVP_PKEY_CTX. | 
| 128 |  |      * 2.  Do the provider specific signature fetch, from the same provider | 
| 129 |  |      *     as |ctx->keymgmt| | 
| 130 |  |      * | 
| 131 |  |      * We then try to fetch the keymgmt from the same provider as the | 
| 132 |  |      * signature, and try to export |ctx->pkey| to that keymgmt (when | 
| 133 |  |      * this keymgmt happens to be the same as |ctx->keymgmt|, the export | 
| 134 |  |      * is a no-op, but we call it anyway to not complicate the code even | 
| 135 |  |      * more). | 
| 136 |  |      * If the export call succeeds (returns a non-NULL provider key pointer), | 
| 137 |  |      * we're done and can perform the operation itself.  If not, we perform | 
| 138 |  |      * the second iteration, or jump to legacy. | 
| 139 |  |      */ | 
| 140 | 27.4k |     for (iter = 1, provkey = NULL; iter < 3 && provkey == NULL; iter++) { | 
| 141 | 13.7k |         EVP_KEYMGMT *tmp_keymgmt_tofree = NULL; | 
| 142 |  |  | 
| 143 |  |         /* | 
| 144 |  |          * If we're on the second iteration, free the results from the first. | 
| 145 |  |          * They are NULL on the first iteration, so no need to check what | 
| 146 |  |          * iteration we're on. | 
| 147 |  |          */ | 
| 148 | 13.7k |         EVP_SIGNATURE_free(signature); | 
| 149 | 13.7k |         EVP_KEYMGMT_free(tmp_keymgmt); | 
| 150 |  |  | 
| 151 | 13.7k |         switch (iter) { | 
| 152 | 13.7k |         case 1: | 
| 153 | 13.7k |             signature = EVP_SIGNATURE_fetch(locpctx->libctx, supported_sig, | 
| 154 | 13.7k |                                             locpctx->propquery); | 
| 155 | 13.7k |             if (signature != NULL) | 
| 156 | 13.7k |                 tmp_prov = EVP_SIGNATURE_get0_provider(signature); | 
| 157 | 13.7k |             break; | 
| 158 | 0 |         case 2: | 
| 159 | 0 |             tmp_prov = EVP_KEYMGMT_get0_provider(locpctx->keymgmt); | 
| 160 | 0 |             signature = | 
| 161 | 0 |                 evp_signature_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, | 
| 162 | 0 |                                               supported_sig, locpctx->propquery); | 
| 163 | 0 |             if (signature == NULL) | 
| 164 | 0 |                 goto legacy; | 
| 165 | 0 |             break; | 
| 166 | 13.7k |         } | 
| 167 | 13.7k |         if (signature == NULL) | 
| 168 | 0 |             continue; | 
| 169 |  |  | 
| 170 |  |         /* | 
| 171 |  |          * Ensure that the key is provided, either natively, or as a cached | 
| 172 |  |          * export.  We start by fetching the keymgmt with the same name as | 
| 173 |  |          * |locpctx->pkey|, but from the provider of the signature method, using | 
| 174 |  |          * the same property query as when fetching the signature method. | 
| 175 |  |          * With the keymgmt we found (if we did), we try to export |locpctx->pkey| | 
| 176 |  |          * to it (evp_pkey_export_to_provider() is smart enough to only actually | 
| 177 |  |  | 
| 178 |  |          * export it if |tmp_keymgmt| is different from |locpctx->pkey|'s keymgmt) | 
| 179 |  |          */ | 
| 180 | 13.7k |         tmp_keymgmt_tofree = tmp_keymgmt = | 
| 181 | 13.7k |             evp_keymgmt_fetch_from_prov((OSSL_PROVIDER *)tmp_prov, | 
| 182 | 13.7k |                                         EVP_KEYMGMT_get0_name(locpctx->keymgmt), | 
| 183 | 13.7k |                                         locpctx->propquery); | 
| 184 | 13.7k |         if (tmp_keymgmt != NULL) | 
| 185 | 13.7k |             provkey = evp_pkey_export_to_provider(locpctx->pkey, locpctx->libctx, | 
| 186 | 13.7k |                                                   &tmp_keymgmt, locpctx->propquery); | 
| 187 | 13.7k |         if (tmp_keymgmt == NULL) | 
| 188 | 0 |             EVP_KEYMGMT_free(tmp_keymgmt_tofree); | 
| 189 | 13.7k |     } | 
| 190 |  |  | 
| 191 | 13.7k |     if (provkey == NULL) { | 
| 192 | 0 |         EVP_SIGNATURE_free(signature); | 
| 193 | 0 |         ERR_clear_last_mark(); | 
| 194 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 195 | 0 |         goto err; | 
| 196 | 0 |     } | 
| 197 |  |  | 
| 198 | 13.7k |     ERR_pop_to_mark(); | 
| 199 |  |  | 
| 200 |  |     /* No more legacy from here down to legacy: */ | 
| 201 |  |  | 
| 202 | 13.7k |     locpctx->op.sig.signature = signature; | 
| 203 | 13.7k |     locpctx->operation = ver ? EVP_PKEY_OP_VERIFYCTX | 
| 204 | 13.7k |                              : EVP_PKEY_OP_SIGNCTX; | 
| 205 | 13.7k |     locpctx->op.sig.algctx | 
| 206 | 13.7k |         = signature->newctx(ossl_provider_ctx(signature->prov), props); | 
| 207 | 13.7k |     if (locpctx->op.sig.algctx == NULL) { | 
| 208 | 0 |         ERR_raise(ERR_LIB_EVP,  EVP_R_INITIALIZATION_ERROR); | 
| 209 | 0 |         goto err; | 
| 210 | 0 |     } | 
| 211 |  |  | 
| 212 | 13.7k |  reinitialize: | 
| 213 | 13.7k |     if (pctx != NULL) | 
| 214 | 2.48k |         *pctx = locpctx; | 
| 215 |  |  | 
| 216 | 13.7k |     if (type != NULL) { | 
| 217 | 743 |         ctx->reqdigest = type; | 
| 218 | 743 |         if (mdname == NULL) | 
| 219 | 743 |             mdname = canon_mdname(EVP_MD_get0_name(type)); | 
| 220 | 12.9k |     } else { | 
| 221 | 12.9k |         if (mdname == NULL && !reinit) { | 
| 222 | 7 |             if (evp_keymgmt_util_get_deflt_digest_name(tmp_keymgmt, provkey, | 
| 223 | 7 |                                                        locmdname, | 
| 224 | 7 |                                                        sizeof(locmdname)) > 0) { | 
| 225 | 7 |                 mdname = canon_mdname(locmdname); | 
| 226 | 7 |             } | 
| 227 | 7 |         } | 
| 228 |  |  | 
| 229 | 12.9k |         if (mdname != NULL) { | 
| 230 |  |             /* | 
| 231 |  |              * We're about to get a new digest so clear anything associated with | 
| 232 |  |              * an old digest. | 
| 233 |  |              */ | 
| 234 | 12.9k |             evp_md_ctx_clear_digest(ctx, 1, 0); | 
| 235 |  |  | 
| 236 |  |             /* legacy code support for engines */ | 
| 237 | 12.9k |             ERR_set_mark(); | 
| 238 |  |             /* | 
| 239 |  |              * This might be requested by a later call to EVP_MD_CTX_get0_md(). | 
| 240 |  |              * In that case the "explicit fetch" rules apply for that | 
| 241 |  |              * function (as per man pages), i.e. the ref count is not updated | 
| 242 |  |              * so the EVP_MD should not be used beyound the lifetime of the | 
| 243 |  |              * EVP_MD_CTX. | 
| 244 |  |              */ | 
| 245 | 12.9k |             ctx->fetched_digest = EVP_MD_fetch(locpctx->libctx, mdname, props); | 
| 246 | 12.9k |             if (ctx->fetched_digest != NULL) { | 
| 247 | 12.9k |                 ctx->digest = ctx->reqdigest = ctx->fetched_digest; | 
| 248 | 12.9k |             } else { | 
| 249 |  |                 /* legacy engine support : remove the mark when this is deleted */ | 
| 250 | 0 |                 ctx->reqdigest = ctx->digest = EVP_get_digestbyname(mdname); | 
| 251 | 0 |                 if (ctx->digest == NULL) { | 
| 252 | 0 |                     (void)ERR_clear_last_mark(); | 
| 253 | 0 |                     ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 254 | 0 |                     goto err; | 
| 255 | 0 |                 } | 
| 256 | 0 |             } | 
| 257 | 12.9k |             (void)ERR_pop_to_mark(); | 
| 258 | 12.9k |         } | 
| 259 | 12.9k |     } | 
| 260 |  |  | 
| 261 | 13.7k |     if (ver) { | 
| 262 | 1.52k |         if (signature->digest_verify_init == NULL) { | 
| 263 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 264 | 0 |             goto err; | 
| 265 | 0 |         } | 
| 266 | 1.52k |         ret = signature->digest_verify_init(locpctx->op.sig.algctx, | 
| 267 | 1.52k |                                             mdname, provkey, params); | 
| 268 | 12.1k |     } else { | 
| 269 | 12.1k |         if (signature->digest_sign_init == NULL) { | 
| 270 | 0 |             ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 271 | 0 |             goto err; | 
| 272 | 0 |         } | 
| 273 | 12.1k |         ret = signature->digest_sign_init(locpctx->op.sig.algctx, | 
| 274 | 12.1k |                                           mdname, provkey, params); | 
| 275 | 12.1k |     } | 
| 276 |  |  | 
| 277 |  |     /* | 
| 278 |  |      * If the operation was not a success and no digest was found, an error | 
| 279 |  |      * needs to be raised. | 
| 280 |  |      */ | 
| 281 | 13.7k |     if (ret > 0 || mdname != NULL) | 
| 282 | 13.7k |         goto end; | 
| 283 | 0 |     if (type == NULL)   /* This check is redundant but clarifies matters */ | 
| 284 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_NO_DEFAULT_DIGEST); | 
| 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 |  | 
 | 
| 364 | 13.7k |  end: | 
| 365 | 13.7k | #ifndef FIPS_MODULE | 
| 366 | 13.7k |     if (ret > 0) | 
| 367 | 13.7k |         ret = evp_pkey_ctx_use_cached_data(locpctx); | 
| 368 | 13.7k | #endif | 
| 369 |  |  | 
| 370 | 13.7k |     EVP_KEYMGMT_free(tmp_keymgmt); | 
| 371 | 13.7k |     return ret > 0 ? 1 : 0; | 
| 372 | 0 | } | 
| 373 |  |  | 
| 374 |  | int EVP_DigestSignInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, | 
| 375 |  |                           const char *mdname, OSSL_LIB_CTX *libctx, | 
| 376 |  |                           const char *props, EVP_PKEY *pkey, | 
| 377 |  |                           const OSSL_PARAM params[]) | 
| 378 | 25.7k | { | 
| 379 | 25.7k |     return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 0, | 
| 380 | 25.7k |                           params); | 
| 381 | 25.7k | } | 
| 382 |  |  | 
| 383 |  | int EVP_DigestSignInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, | 
| 384 |  |                        const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) | 
| 385 | 12.2k | { | 
| 386 | 12.2k |     return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 0, | 
| 387 | 12.2k |                           NULL); | 
| 388 | 12.2k | } | 
| 389 |  |  | 
| 390 |  | int EVP_DigestVerifyInit_ex(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, | 
| 391 |  |                             const char *mdname, OSSL_LIB_CTX *libctx, | 
| 392 |  |                             const char *props, EVP_PKEY *pkey, | 
| 393 |  |                             const OSSL_PARAM params[]) | 
| 394 | 1.39k | { | 
| 395 | 1.39k |     return do_sigver_init(ctx, pctx, NULL, mdname, libctx, props, NULL, pkey, 1, | 
| 396 | 1.39k |                           params); | 
| 397 | 1.39k | } | 
| 398 |  |  | 
| 399 |  | int EVP_DigestVerifyInit(EVP_MD_CTX *ctx, EVP_PKEY_CTX **pctx, | 
| 400 |  |                          const EVP_MD *type, ENGINE *e, EVP_PKEY *pkey) | 
| 401 | 3.27k | { | 
| 402 | 3.27k |     return do_sigver_init(ctx, pctx, type, NULL, NULL, NULL, e, pkey, 1, | 
| 403 | 3.27k |                           NULL); | 
| 404 | 3.27k | } | 
| 405 |  | #endif /* FIPS_MDOE */ | 
| 406 |  |  | 
| 407 |  | int EVP_DigestSignUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize) | 
| 408 | 3.33k | { | 
| 409 | 3.33k |     EVP_PKEY_CTX *pctx = ctx->pctx; | 
| 410 |  |  | 
| 411 | 3.33k |     if (pctx == NULL | 
| 412 | 3.33k |             || pctx->operation != EVP_PKEY_OP_SIGNCTX | 
| 413 | 3.33k |             || pctx->op.sig.algctx == NULL | 
| 414 | 3.33k |             || pctx->op.sig.signature == NULL) | 
| 415 | 0 |         goto legacy; | 
| 416 |  |  | 
| 417 | 3.33k |     if (pctx->op.sig.signature->digest_sign_update == NULL) { | 
| 418 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | 
| 419 | 0 |         return 0; | 
| 420 | 0 |     } | 
| 421 |  |  | 
| 422 | 3.33k |     return pctx->op.sig.signature->digest_sign_update(pctx->op.sig.algctx, | 
| 423 | 3.33k |                                                       data, dsize); | 
| 424 |  |  | 
| 425 | 0 |  legacy: | 
| 426 | 0 |     if (pctx != NULL) { | 
| 427 |  |         /* do_sigver_init() checked that |digest_custom| is non-NULL */ | 
| 428 | 0 |         if (pctx->flag_call_digest_custom | 
| 429 | 0 |             && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx)) | 
| 430 | 0 |             return 0; | 
| 431 | 0 |         pctx->flag_call_digest_custom = 0; | 
| 432 | 0 |     } | 
| 433 |  |  | 
| 434 | 0 |     return EVP_DigestUpdate(ctx, data, dsize); | 
| 435 | 0 | } | 
| 436 |  |  | 
| 437 |  | int EVP_DigestVerifyUpdate(EVP_MD_CTX *ctx, const void *data, size_t dsize) | 
| 438 | 1.52k | { | 
| 439 | 1.52k |     EVP_PKEY_CTX *pctx = ctx->pctx; | 
| 440 |  |  | 
| 441 | 1.52k |     if (pctx == NULL | 
| 442 | 1.52k |             || pctx->operation != EVP_PKEY_OP_VERIFYCTX | 
| 443 | 1.52k |             || pctx->op.sig.algctx == NULL | 
| 444 | 1.52k |             || pctx->op.sig.signature == NULL) | 
| 445 | 0 |         goto legacy; | 
| 446 |  |  | 
| 447 | 1.52k |     if (pctx->op.sig.signature->digest_verify_update == NULL) { | 
| 448 | 0 |         ERR_raise(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); | 
| 449 | 0 |         return 0; | 
| 450 | 0 |     } | 
| 451 |  |  | 
| 452 | 1.52k |     return pctx->op.sig.signature->digest_verify_update(pctx->op.sig.algctx, | 
| 453 | 1.52k |                                                         data, dsize); | 
| 454 |  |  | 
| 455 | 0 |  legacy: | 
| 456 | 0 |     if (pctx != NULL) { | 
| 457 |  |         /* do_sigver_init() checked that |digest_custom| is non-NULL */ | 
| 458 | 0 |         if (pctx->flag_call_digest_custom | 
| 459 | 0 |             && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx)) | 
| 460 | 0 |             return 0; | 
| 461 | 0 |         pctx->flag_call_digest_custom = 0; | 
| 462 | 0 |     } | 
| 463 |  |  | 
| 464 | 0 |     return EVP_DigestUpdate(ctx, data, dsize); | 
| 465 | 0 | } | 
| 466 |  |  | 
| 467 |  | #ifndef FIPS_MODULE | 
| 468 |  | int EVP_DigestSignFinal(EVP_MD_CTX *ctx, unsigned char *sigret, | 
| 469 |  |                         size_t *siglen) | 
| 470 | 3.50k | { | 
| 471 | 3.50k |     int sctx = 0, r = 0; | 
| 472 | 3.50k |     EVP_PKEY_CTX *dctx, *pctx = ctx->pctx; | 
| 473 |  |  | 
| 474 | 3.50k |     if (pctx == NULL | 
| 475 | 3.50k |             || pctx->operation != EVP_PKEY_OP_SIGNCTX | 
| 476 | 3.50k |             || pctx->op.sig.algctx == NULL | 
| 477 | 3.50k |             || pctx->op.sig.signature == NULL) | 
| 478 | 0 |         goto legacy; | 
| 479 |  |  | 
| 480 | 3.50k |     if (sigret == NULL || (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0) | 
| 481 | 1.22k |         return pctx->op.sig.signature->digest_sign_final(pctx->op.sig.algctx, | 
| 482 | 1.22k |                                                          sigret, siglen, | 
| 483 | 1.22k |                                                          sigret == NULL ? 0 : *siglen); | 
| 484 | 2.28k |     dctx = EVP_PKEY_CTX_dup(pctx); | 
| 485 | 2.28k |     if (dctx == NULL) | 
| 486 | 0 |         return 0; | 
| 487 |  |  | 
| 488 | 2.28k |     r = dctx->op.sig.signature->digest_sign_final(dctx->op.sig.algctx, | 
| 489 | 2.28k |                                                   sigret, siglen, | 
| 490 | 2.28k |                                                   *siglen); | 
| 491 | 2.28k |     EVP_PKEY_CTX_free(dctx); | 
| 492 | 2.28k |     return r; | 
| 493 |  |  | 
| 494 | 0 |  legacy: | 
| 495 | 0 |     if (pctx == NULL || pctx->pmeth == NULL) { | 
| 496 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 497 | 0 |         return 0; | 
| 498 | 0 |     } | 
| 499 |  |  | 
| 500 |  |     /* do_sigver_init() checked that |digest_custom| is non-NULL */ | 
| 501 | 0 |     if (pctx->flag_call_digest_custom | 
| 502 | 0 |         && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx)) | 
| 503 | 0 |         return 0; | 
| 504 | 0 |     pctx->flag_call_digest_custom = 0; | 
| 505 |  | 
 | 
| 506 | 0 |     if (pctx->pmeth->flags & EVP_PKEY_FLAG_SIGCTX_CUSTOM) { | 
| 507 | 0 |         if (sigret == NULL) | 
| 508 | 0 |             return pctx->pmeth->signctx(pctx, sigret, siglen, ctx); | 
| 509 | 0 |         if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) | 
| 510 | 0 |             r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx); | 
| 511 | 0 |         else { | 
| 512 | 0 |             dctx = EVP_PKEY_CTX_dup(pctx); | 
| 513 | 0 |             if (dctx == NULL) | 
| 514 | 0 |                 return 0; | 
| 515 | 0 |             r = dctx->pmeth->signctx(dctx, sigret, siglen, ctx); | 
| 516 | 0 |             EVP_PKEY_CTX_free(dctx); | 
| 517 | 0 |         } | 
| 518 | 0 |         return r; | 
| 519 | 0 |     } | 
| 520 | 0 |     if (pctx->pmeth->signctx != NULL) | 
| 521 | 0 |         sctx = 1; | 
| 522 | 0 |     else | 
| 523 | 0 |         sctx = 0; | 
| 524 | 0 |     if (sigret != NULL) { | 
| 525 | 0 |         unsigned char md[EVP_MAX_MD_SIZE]; | 
| 526 | 0 |         unsigned int mdlen = 0; | 
| 527 |  | 
 | 
| 528 | 0 |         if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) { | 
| 529 | 0 |             if (sctx) | 
| 530 | 0 |                 r = pctx->pmeth->signctx(pctx, sigret, siglen, ctx); | 
| 531 | 0 |             else | 
| 532 | 0 |                 r = EVP_DigestFinal_ex(ctx, md, &mdlen); | 
| 533 | 0 |         } else { | 
| 534 | 0 |             EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new(); | 
| 535 |  | 
 | 
| 536 | 0 |             if (tmp_ctx == NULL) | 
| 537 | 0 |                 return 0; | 
| 538 | 0 |             if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) { | 
| 539 | 0 |                 EVP_MD_CTX_free(tmp_ctx); | 
| 540 | 0 |                 return 0; | 
| 541 | 0 |             } | 
| 542 | 0 |             if (sctx) | 
| 543 | 0 |                 r = tmp_ctx->pctx->pmeth->signctx(tmp_ctx->pctx, | 
| 544 | 0 |                                                   sigret, siglen, tmp_ctx); | 
| 545 | 0 |             else | 
| 546 | 0 |                 r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen); | 
| 547 | 0 |             EVP_MD_CTX_free(tmp_ctx); | 
| 548 | 0 |         } | 
| 549 | 0 |         if (sctx || !r) | 
| 550 | 0 |             return r; | 
| 551 | 0 |         if (EVP_PKEY_sign(pctx, sigret, siglen, md, mdlen) <= 0) | 
| 552 | 0 |             return 0; | 
| 553 | 0 |     } else { | 
| 554 | 0 |         if (sctx) { | 
| 555 | 0 |             if (pctx->pmeth->signctx(pctx, sigret, siglen, ctx) <= 0) | 
| 556 | 0 |                 return 0; | 
| 557 | 0 |         } else { | 
| 558 | 0 |             int s = EVP_MD_get_size(ctx->digest); | 
| 559 |  | 
 | 
| 560 | 0 |             if (s < 0 || EVP_PKEY_sign(pctx, sigret, siglen, NULL, s) <= 0) | 
| 561 | 0 |                 return 0; | 
| 562 | 0 |         } | 
| 563 | 0 |     } | 
| 564 | 0 |     return 1; | 
| 565 | 0 | } | 
| 566 |  |  | 
| 567 |  | int EVP_DigestSign(EVP_MD_CTX *ctx, unsigned char *sigret, size_t *siglen, | 
| 568 |  |                    const unsigned char *tbs, size_t tbslen) | 
| 569 | 2.44k | { | 
| 570 | 2.44k |     EVP_PKEY_CTX *pctx = ctx->pctx; | 
| 571 |  |  | 
| 572 | 2.44k |     if (pctx != NULL | 
| 573 | 2.44k |             && pctx->operation == EVP_PKEY_OP_SIGNCTX | 
| 574 | 2.44k |             && pctx->op.sig.algctx != NULL | 
| 575 | 2.44k |             && pctx->op.sig.signature != NULL) { | 
| 576 | 2.44k |         if (pctx->op.sig.signature->digest_sign != NULL) | 
| 577 | 0 |             return pctx->op.sig.signature->digest_sign(pctx->op.sig.algctx, | 
| 578 | 0 |                                                        sigret, siglen, | 
| 579 | 0 |                                                        sigret == NULL ? 0 : *siglen, | 
| 580 | 0 |                                                        tbs, tbslen); | 
| 581 | 2.44k |     } else { | 
| 582 |  |         /* legacy */ | 
| 583 | 0 |         if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestsign != NULL) | 
| 584 | 0 |             return ctx->pctx->pmeth->digestsign(ctx, sigret, siglen, tbs, tbslen); | 
| 585 | 0 |     } | 
| 586 |  |  | 
| 587 | 2.44k |     if (sigret != NULL && EVP_DigestSignUpdate(ctx, tbs, tbslen) <= 0) | 
| 588 | 0 |         return 0; | 
| 589 | 2.44k |     return EVP_DigestSignFinal(ctx, sigret, siglen); | 
| 590 | 2.44k | } | 
| 591 |  |  | 
| 592 |  | int EVP_DigestVerifyFinal(EVP_MD_CTX *ctx, const unsigned char *sig, | 
| 593 |  |                           size_t siglen) | 
| 594 | 1.52k | { | 
| 595 | 1.52k |     unsigned char md[EVP_MAX_MD_SIZE]; | 
| 596 | 1.52k |     int r = 0; | 
| 597 | 1.52k |     unsigned int mdlen = 0; | 
| 598 | 1.52k |     int vctx = 0; | 
| 599 | 1.52k |     EVP_PKEY_CTX *dctx, *pctx = ctx->pctx; | 
| 600 |  |  | 
| 601 | 1.52k |     if (pctx == NULL | 
| 602 | 1.52k |             || pctx->operation != EVP_PKEY_OP_VERIFYCTX | 
| 603 | 1.52k |             || pctx->op.sig.algctx == NULL | 
| 604 | 1.52k |             || pctx->op.sig.signature == NULL) | 
| 605 | 0 |         goto legacy; | 
| 606 |  |  | 
| 607 | 1.52k |     if ((ctx->flags & EVP_MD_CTX_FLAG_FINALISE) != 0) | 
| 608 | 0 |         return pctx->op.sig.signature->digest_verify_final(pctx->op.sig.algctx, | 
| 609 | 0 |                                                            sig, siglen); | 
| 610 | 1.52k |     dctx = EVP_PKEY_CTX_dup(pctx); | 
| 611 | 1.52k |     if (dctx == NULL) | 
| 612 | 0 |         return 0; | 
| 613 |  |  | 
| 614 | 1.52k |     r = dctx->op.sig.signature->digest_verify_final(dctx->op.sig.algctx, | 
| 615 | 1.52k |                                                     sig, siglen); | 
| 616 | 1.52k |     EVP_PKEY_CTX_free(dctx); | 
| 617 | 1.52k |     return r; | 
| 618 |  |  | 
| 619 | 0 |  legacy: | 
| 620 | 0 |     if (pctx == NULL || pctx->pmeth == NULL) { | 
| 621 | 0 |         ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR); | 
| 622 | 0 |         return 0; | 
| 623 | 0 |     } | 
| 624 |  |  | 
| 625 |  |     /* do_sigver_init() checked that |digest_custom| is non-NULL */ | 
| 626 | 0 |     if (pctx->flag_call_digest_custom | 
| 627 | 0 |         && !ctx->pctx->pmeth->digest_custom(ctx->pctx, ctx)) | 
| 628 | 0 |         return 0; | 
| 629 | 0 |     pctx->flag_call_digest_custom = 0; | 
| 630 |  | 
 | 
| 631 | 0 |     if (pctx->pmeth->verifyctx != NULL) | 
| 632 | 0 |         vctx = 1; | 
| 633 | 0 |     else | 
| 634 | 0 |         vctx = 0; | 
| 635 | 0 |     if (ctx->flags & EVP_MD_CTX_FLAG_FINALISE) { | 
| 636 | 0 |         if (vctx) | 
| 637 | 0 |             r = pctx->pmeth->verifyctx(pctx, sig, siglen, ctx); | 
| 638 | 0 |         else | 
| 639 | 0 |             r = EVP_DigestFinal_ex(ctx, md, &mdlen); | 
| 640 | 0 |     } else { | 
| 641 | 0 |         EVP_MD_CTX *tmp_ctx = EVP_MD_CTX_new(); | 
| 642 | 0 |         if (tmp_ctx == NULL) | 
| 643 | 0 |             return -1; | 
| 644 | 0 |         if (!EVP_MD_CTX_copy_ex(tmp_ctx, ctx)) { | 
| 645 | 0 |             EVP_MD_CTX_free(tmp_ctx); | 
| 646 | 0 |             return -1; | 
| 647 | 0 |         } | 
| 648 | 0 |         if (vctx) | 
| 649 | 0 |             r = tmp_ctx->pctx->pmeth->verifyctx(tmp_ctx->pctx, | 
| 650 | 0 |                                                 sig, siglen, tmp_ctx); | 
| 651 | 0 |         else | 
| 652 | 0 |             r = EVP_DigestFinal_ex(tmp_ctx, md, &mdlen); | 
| 653 | 0 |         EVP_MD_CTX_free(tmp_ctx); | 
| 654 | 0 |     } | 
| 655 | 0 |     if (vctx || !r) | 
| 656 | 0 |         return r; | 
| 657 | 0 |     return EVP_PKEY_verify(pctx, sig, siglen, md, mdlen); | 
| 658 | 0 | } | 
| 659 |  |  | 
| 660 |  | int EVP_DigestVerify(EVP_MD_CTX *ctx, const unsigned char *sigret, | 
| 661 |  |                      size_t siglen, const unsigned char *tbs, size_t tbslen) | 
| 662 | 1.52k | { | 
| 663 | 1.52k |     EVP_PKEY_CTX *pctx = ctx->pctx; | 
| 664 |  |  | 
| 665 | 1.52k |     if (pctx != NULL | 
| 666 | 1.52k |             && pctx->operation == EVP_PKEY_OP_VERIFYCTX | 
| 667 | 1.52k |             && pctx->op.sig.algctx != NULL | 
| 668 | 1.52k |             && pctx->op.sig.signature != NULL) { | 
| 669 | 1.52k |         if (pctx->op.sig.signature->digest_verify != NULL) | 
| 670 | 0 |             return pctx->op.sig.signature->digest_verify(pctx->op.sig.algctx, | 
| 671 | 0 |                                                          sigret, siglen, | 
| 672 | 0 |                                                          tbs, tbslen); | 
| 673 | 1.52k |     } else { | 
| 674 |  |         /* legacy */ | 
| 675 | 0 |         if (ctx->pctx->pmeth != NULL && ctx->pctx->pmeth->digestverify != NULL) | 
| 676 | 0 |             return ctx->pctx->pmeth->digestverify(ctx, sigret, siglen, tbs, tbslen); | 
| 677 | 0 |     } | 
| 678 |  |  | 
| 679 | 1.52k |     if (EVP_DigestVerifyUpdate(ctx, tbs, tbslen) <= 0) | 
| 680 | 0 |         return -1; | 
| 681 | 1.52k |     return EVP_DigestVerifyFinal(ctx, sigret, siglen); | 
| 682 | 1.52k | } | 
| 683 |  | #endif /* FIPS_MODULE */ |