CMS_DECRYPT(3SSL)
π CMS_DECRYPT(3SSL) π
okay bestie, we're in OpenSSL territory and this is CMS_DECRYPT, aka the CMS_decrypt, CMS_decrypt_set1_pkey_and_peer, CMS_decrypt_set1_pkey, and CMS_decrypt_set1_password girlies. Their whole thing: decrypt content from a CMS envelopedData structure. πβ¨
πΈ SYNOPSIS πΈ
#include <openssl/cms.h>
int CMS_decrypt(CMS_ContentInfo *cms, EVP_PKEY *pkey, X509 *cert,
BIO *dcont, BIO *out, unsigned int flags);
int CMS_decrypt_set1_pkey_and_peer(CMS_ContentInfo *cms,
EVP_PKEY *pk, X509 *cert, X509 *peer);
int CMS_decrypt_set1_pkey(CMS_ContentInfo *cms, EVP_PKEY *pk, X509 *cert);
int CMS_decrypt_set1_password(CMS_ContentInfo *cms,
unsigned char *pass, ossl_ssize_t passlen);
β¨ DESCRIPTION β¨
So CMS_decrypt() extracts the decrypted content out of a CMS EnvelopedData or AuthEnvelopedData structure. π It uses CMS_decrypt_set1_pkey() to decrypt the content with the recipient private key pkey β but only if pkey is not NULL. In that case, the associated certificate is recommended to be provided in cert β check the NOTES down below, bestie. π out is a BIO to write the content to, and flags is an optional set of flags. If pkey is NULL, the function just assumes decryption was already done (like, via CMS_decrypt_set1_pkey() or CMS_decrypt_set1_password()) and it just hands you the content β unless cert, dcont, and out are NULL as well. And dcont? That parameter is for the rare case where the encrypted content is detached. Normally you set it to NULL. π«Ά
CMS_decrypt_set1_pkey_and_peer() decrypts the CMS_ContentInfo structure cms using the private key pkey, the corresponding certificate cert β which is recommended but may be NULL β and the (optional) originator certificate peer. On success, it also records the decryption key pkey in cms, and then you should follow it up with CMS_decrypt(cms, NULL, NULL, dcont, out, flags). Heads up: this call deallocates any decryption key stored in cms. π
CMS_decrypt_set1_pkey() is literally the same as CMS_decrypt_set1_pkey_and_peer() except peer is NULL. slay, no notes. π
CMS_decrypt_set1_password() decrypts the CMS_ContentInfo structure cms using the secret pass of length passlen. On success, it also records the decryption key used in cms, and then you should follow it up with CMS_decrypt(cms, NULL, NULL, dcont, out, flags). And yep, this call deallocates any decryption key stored in cms too. π
π NOTES π
So even though the recipient's certificate is not needed to decrypt the data, it is needed to locate the appropriate recipient among the possibly several recipients in the CMS structure. iykyk. π¦
If cert is set to NULL, all possible recipients get tried. But this case is problematic, bestie, fr fr. To thwart the MMA attack (Bleichenbacher's attack on PKCS #1 v1.5 RSA padding), all recipients are tried whether they succeed or not. And if no recipient succeeds, a random symmetric key is used to decrypt the content β which will typically output garbage and may (but is not guaranteed to) ultimately return a padding error only. The reason it's built like this? If CMS_decrypt() just returned an error when all recipient encrypted keys failed to decrypt, an attacker could use that in a timing attack. π If the special flag CMS_DEBUG_DECRYPT is set, then 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, because it can leave them open to attack. See EVP_PKEY_decrypt(3) for more details.
You can also figure out the correct recipient key by other means (for example, looking them up in a database) and set them in the CMS structure in advance using the CMS utility functions such as CMS_set1_pkey(), or use CMS_decrypt_set1_password() if the recipient has a symmetric key. In these cases, both cert and pkey should be set to NULL. π
To process KEKRecipientInfo types, CMS_set1_key() or CMS_RecipientInfo_set0_key() and CMS_RecipientInfo_decrypt() should be called before CMS_decrypt(), and cert and pkey set to NULL. π
The following flags can be passed in the flags parameter:
CMS_TEXTβ if this flag is set, MIME headers for type"text/plain"are deleted from the content. But if the content is not of type"text/plain", then an error is returned. π
π RETURN VALUES π
CMS_decrypt(), CMS_decrypt_set1_pkey_and_peer(), CMS_decrypt_set1_pkey(), and CMS_decrypt_set1_password() return either 1 for success or 0 for failure. The error can be obtained from ERR_get_error(3). β
π BUGS π
The set1_ part of these function names is misleading and should better read: with_. not me pointing that out but here we are. π
Also β the lack of single pass processing and the need to hold all data in memory as mentioned in CMS_verify() also applies to CMS_decrypt(). so yeah, memory go brrr. π«
π SEE ALSO π
π§ HISTORY π§
CMS_decrypt_set1_pkey_and_peer() and CMS_decrypt_set1_password() were added in OpenSSL 3.0. iconic era. β¨
π COPYRIGHT π
Copyright 2008-2026 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at <https://www.openssl.org/source/license.html>.
and that's the whole page bestie!! π₯³ this was brought to you by OpenSSL 4.0.3, dated 2026-09-29 β CMS_DECRYPT(3SSL), periodt. π
ππ¦
π¦ where this page comes from
- ubuntu stonking: libssl-doc 4.0.3-1ubuntu1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms