CMS_DECRYPT(3ossl)

πŸ“¦
version 15.1 Β· 3 versions
, in freebsd 15.1

πŸ’… CMS_DECRYPT(3ossl) β€” or, how to open a CMS envelopedData like it's a group chat invite πŸŽ€

ok so this is the OpenSSL manpage for the <openssl/cms.h> girlies β€” the CMS_decrypt(3ossl) page, bestie. it's all about pulling the actual content out of a CMS EnvelopedData / AuthEnvelopedData structure, and honestly? it's giving cryptographic lockbox energy πŸ”₯


✨ NAME ✨

CMS_decrypt, CMS_decrypt_set1_pkey_and_peer, CMS_decrypt_set1_pkey, CMS_decrypt_set1_password β€” these four icons 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 🌸

main character: CMS_decrypt()

CMS_decrypt() extracts the decrypted content from a CMS EnvelopedData or AuthEnvelopedData structure, periodt. it uses CMS_decrypt_set1_pkey() to decrypt the content with the recipient private key pkey β€” but only if pkey is not NULL. when that's the case, you're recommended to also hand over the associated certificate in cert β€” the NOTES section below explains why, iykyk πŸ‘€

out is a BIO where the content gets written, and flags is your optional little set of flags. if pkey is NULL, the function just assumes decryption already happened (like, say, via CMS_decrypt_set1_pkey() or CMS_decrypt_set1_password()) and it simply provides the content β€” unless cert, dcont, and out are all NULL too.

and dcont? that one's for the rare case where the encrypted content is detached. normally you just set it to NULL and move on πŸ’…

CMS_decrypt_set1_pkey_and_peer()

this one decrypts the CMS_ContentInfo structure cms using the private key pkey, the corresponding certificate cert (recommended bestie, but NULL is allowed), and the optional originator certificate peer. on success it also records the decryption key pkey inside cms, which means 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()

literally the same as CMS_decrypt_set1_pkey_and_peer() except peer is NULL. simple as that 🫢

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 inside cms, so again β€” 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 ⭐

ok so even though the recipient's certificate is not needed to decrypt the data, it is needed to locate the appropriate recipient in the CMS structure (there might be several of them, lowkey). so like… bring it anyway.

if cert is set to NULL, all possible recipients get tried. and this case? it's problematic, real talk. to thwart the MMA attack (that's Bleichenbacher's attack on PKCS #1 v1.5 RSA padding, for the uninitiated πŸ‘‘) all recipients are tried whether they succeed or not. if no recipient succeeds, then 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.

why, you ask? because if CMS_decrypt() just returned an error whenever all recipient encrypted keys failed to decrypt, an attacker could use that in a timing attack. timing attack said no ❌

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. like, genuinely, be careful out there 😭

it is possible to determine the correct recipient key by other means (for example, looking them up in a database) and setting 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. 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() all return either 1 for success or 0 for failure. the error can be obtained from ERR_get_error(3) β€” go get her πŸƒβ€β™€οΈ


πŸ’€ BUGS πŸ’€

the set1_ part of these function names is misleading and should better read: with_. like… ok whoever named these, we need to talk πŸ™ˆ

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. new drop, iconic behaviour ✨


Copyright 2008-2020 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 🫢 β€” with love from version 3.5.6, dated 2026-04-07, signed sealed delivered CMS_DECRYPT(3ossl) style πŸ’– slay ✨

πŸ“¦ where this page comes from