/* Provenance: openssl/openssl pkcs7_bio_add_digest (fixed). * repo: openssl/openssl * commit: 6eebe6c0238178356114a96a7858f36b24172847 * parent: 7880536fe17c2b5450e279155bedd51771d28c9f * commit_url: https://github.com/openssl/openssl/commit/6eebe6c0238178356114a96a7858f36b24172847 * cve: CVE-2023-0401 * license: Apache-2.0 * function: pkcs7_bio_add_digest * relpath: crypto/pkcs7/pk7_doit.c */ static int pkcs7_bio_add_digest(BIO **pbio, X509_ALGOR *alg, const PKCS7_CTX *ctx) { BIO *btmp; char name[OSSL_MAX_NAME_SIZE]; EVP_MD *fetched = NULL; const EVP_MD *md; if ((btmp = BIO_new(BIO_f_md())) == NULL) { ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB); goto err; } OBJ_obj2txt(name, sizeof(name), alg->algorithm, 0); (void)ERR_set_mark(); fetched = EVP_MD_fetch(ossl_pkcs7_ctx_get0_libctx(ctx), name, ossl_pkcs7_ctx_get0_propq(ctx)); if (fetched != NULL) md = fetched; else md = EVP_get_digestbyname(name); if (md == NULL) { (void)ERR_clear_last_mark(); ERR_raise(ERR_LIB_PKCS7, PKCS7_R_UNKNOWN_DIGEST_TYPE); goto err; } (void)ERR_pop_to_mark(); if (BIO_set_md(btmp, md) <= 0) { ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB); EVP_MD_free(fetched); goto err; } EVP_MD_free(fetched); if (*pbio == NULL) *pbio = btmp; else if (!BIO_push(*pbio, btmp)) { ERR_raise(ERR_LIB_PKCS7, ERR_R_BIO_LIB); goto err; } btmp = NULL; return 1; err: BIO_free(btmp); return 0; }