/* Provenance: openssl/openssl rsa_cms_decrypt (fixed). * repo: openssl/openssl * commit: af2a5fecd3e71a29e7568f9c1453dec5cebbaff4 * parent: 6e74b1275cbc1fd1cf7ba32f662602d68501f6ca * commit_url: https://github.com/openssl/openssl/commit/af2a5fecd3e71a29e7568f9c1453dec5cebbaff4 * cve: CVE-2026-28389 * license: Apache-2.0 * function: rsa_cms_decrypt * relpath: crypto/cms/cms_rsa.c */ static int rsa_cms_decrypt(CMS_RecipientInfo *ri) { EVP_PKEY_CTX *pkctx; X509_ALGOR *cmsalg; int nid; int rv = -1; const unsigned char *label = NULL; int labellen = 0; const EVP_MD *mgf1md = NULL, *md = NULL; RSA_OAEP_PARAMS *oaep; const ASN1_OBJECT *aoid; const void *parameter = NULL; int ptype = 0; pkctx = CMS_RecipientInfo_get0_pkey_ctx(ri); if (pkctx == NULL) return 0; if (!CMS_RecipientInfo_ktri_get0_algs(ri, NULL, NULL, &cmsalg)) return -1; nid = OBJ_obj2nid(cmsalg->algorithm); if (nid == NID_rsaEncryption) return 1; if (nid != NID_rsaesOaep) { ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_ENCRYPTION_TYPE); return -1; } /* Decode OAEP parameters */ oaep = rsa_oaep_decode(cmsalg); if (oaep == NULL) { ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_OAEP_PARAMETERS); goto err; } mgf1md = ossl_x509_algor_get_md(oaep->maskHash); if (mgf1md == NULL) goto err; md = ossl_x509_algor_get_md(oaep->hashFunc); if (md == NULL) goto err; if (oaep->pSourceFunc != NULL) { X509_ALGOR_get0(&aoid, &ptype, ¶meter, oaep->pSourceFunc); if (OBJ_obj2nid(aoid) != NID_pSpecified) { ERR_raise(ERR_LIB_CMS, CMS_R_UNSUPPORTED_LABEL_SOURCE); goto err; } if (ptype != V_ASN1_OCTET_STRING) { ERR_raise(ERR_LIB_CMS, CMS_R_INVALID_LABEL); goto err; } label = ASN1_STRING_get0_data(parameter); labellen = ASN1_STRING_length(parameter); } if (EVP_PKEY_CTX_set_rsa_padding(pkctx, RSA_PKCS1_OAEP_PADDING) <= 0) goto err; if (EVP_PKEY_CTX_set_rsa_oaep_md(pkctx, md) <= 0) goto err; if (EVP_PKEY_CTX_set_rsa_mgf1_md(pkctx, mgf1md) <= 0) goto err; if (label != NULL) { unsigned char *dup_label = OPENSSL_memdup(label, labellen); if (dup_label == NULL) goto err; if (EVP_PKEY_CTX_set0_rsa_oaep_label(pkctx, dup_label, labellen) <= 0) { OPENSSL_free(dup_label); goto err; } } /* Carry on */ rv = 1; err: RSA_OAEP_PARAMS_free(oaep); return rv; }