CMS_DECRYPT(3)
π CMS_decrypt π
okay bestie, welcome to CMS_DECRYPT(3) from the Library Functions Manual β yes we're doing crypto-page vibes and it's giving big-brain energy β¨
πΈ NAME πΈ
CMS_decrypt, CMS_decrypt_set1_pkey, CMS_decrypt_set1_key β decrypt content from a CMS EnvelopedData structure
π SYNOPSIS π
library βlibcryptoβ #include <openssl/cms.h>
int
CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *private_key,
X509 *certificate, BIO *dcont, BIO *out, unsigned int flags);
int
CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *private_key,
X509 *certificate);
int
CMS_decrypt_set1_key(CMS_ContentInfo *cms, unsigned char *symmetric_key,
size_t keylen, const unsigned char *id, size_t idlen);
β¨ DESCRIPTION β¨
okay so CMS_decrypt() is the main character here: it extracts and decrypts the content from the CMS EnvelopedData structure cms, using private_key and the certificate of the recipient. Where does all that decrypted goodness go? Straight into out. π«Ά
In the rare case where the compressed content is detached, pass it in via dcont. For normal use, set dcont to NULL. π
real talk: the recipient's certificate is not needed to decrypt the data, but it is needed to locate the appropriate recipient β and yes, there can possibly be several β inside the CMS structure. π
β οΈ Ifcertificateis set toNULL, all possible recipients are tried. This case however is problematic. To thwart the MMA attack (Bleichenbacher's attack on PKCS #1 v1.5 RSA padding), all recipients are tried whether they succeed or not. If no recipient succeeds, a random symmetric key is used to decrypt the content: this will typically output garbage and may (but is not guaranteed to) ultimately return a padding error only. The reason? IfCMS_decrypt()just returned an error when all recipient encrypted keys failed to decrypt, an attacker could use this in a timing attack. π
If the special flag CMS_DEBUG_DECRYPT is set, the above behaviour is modified and an error is returned if no recipient encrypted key can be decrypted without generating a random content encryption key. Applications should use this flag with extreme caution, especially in automated gateways, as it can leave them open to attack. π¨
You can also determine the correct recipient key by other means (for example by looking them up in a database) and set them in the cms structure in advance using the CMS utility functions such as CMS_decrypt_set1_pkey(). In this case, both certificate and private_key should be set to NULL when calling CMS_decrypt() later on. π
To process KEKRecipientInfo types, CMS_decrypt_set1_key() or CMS_RecipientInfo_set0_key(3) and CMS_RecipientInfo_decrypt(3) should be called before CMS_decrypt(), and certificate and private_key set to NULL when calling CMS_decrypt() later on. π
If the CMS_TEXT bit is set in flags, MIME headers for type text/plain are deleted from the content. If the content is not of type text/plain, an error occurs. π
π RETURN VALUES π
CMS_decrypt(), CMS_decrypt_set1_pkey(), and CMS_decrypt_set1_key() return 1 for success or 0 for failure. The error can be obtained from ERR_get_error(3). One for slay, zero for not today. β¨
π¦ SEE ALSO π¦
π STANDARDS π
RFC 5652: Cryptographic Message Syntax (CMS) is the standard bestie here:
- section 6.1: EnvelopedData Type
- section 6.2.3: KEKRecipientInfo Type
π HISTORY π
CMS_decrypt(), CMS_decrypt_set1_pkey(), and CMS_decrypt_set1_key() first appeared in OpenSSL 0.9.8h and have been available since OpenBSD 6.7. vintage but still iconic π
π BUGS π
The lack of single pass processing and the need to hold all data in memory as mentioned in CMS_verify(3) also applies to CMS_decrypt(). so yeah, memory go brrr in the same way β not a vibe, but it's canon. π
xoxo, Debian π β page dated June 8, 2025 β CMS_DECRYPT(3) out π
π¦ where this page comes from
- alpine 3.24: libressl-doc 4.3.1-r0 Β· custom
- arch rolling: libressl 4.4.0-1 Β· ISC AND custom:OpenSSL
- Copyright (c) 2019 Ingo Schwarze <schwarze@openbsd.org>
- Copyright (c) 2008, 2014 The OpenSSL Project. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms