CMS_ENCRYPTEDDATA_ENCRYPT(3SSL)

πŸ’– CMS_ENCRYPTEDDATA_ENCRYPT πŸ’–

hey bestie 🌸 so this is a page from the OpenSSL manual, section 3SSL, and it's all about two little functions that make you a CMS EncryptedData blob: CMS_EncryptedData_encrypt_ex and CMS_EncryptedData_encrypt. it's giving "wrap your secrets in a cute lil envelope" energy, and honestly? obsessed. πŸ’…


✨ NAME ✨

CMS_EncryptedData_encrypt_ex, CMS_EncryptedData_encrypt β€” Create CMS EncryptedData πŸŽ€


🌈 SYNOPSIS 🌈

okay so here's the tea, verbatim, no cap:

#include <openssl/cms.h>

CMS_ContentInfo *CMS_EncryptedData_encrypt_ex(BIO *in,
                                              const EVP_CIPHER *cipher,
                                              const unsigned char *key,
                                              size_t keylen,
                                              unsigned int flags,
                                              OSSL_LIB_CTX *ctx,
                                              const char *propq);

CMS_ContentInfo *CMS_EncryptedData_encrypt(BIO *in,
    const EVP_CIPHER *cipher, const unsigned char *key, size_t keylen,
    unsigned int flags);

πŸ’œ DESCRIPTION πŸ’œ

so CMS_EncryptedData_encrypt_ex() builds you a CMS_ContentInfo structure and gives it the type NID_pkcs7_encrypted β€” that's the whole point, that's the vibe. πŸ”₯

  • in is a BIO holding the data you wanna encrypt πŸ“
  • cipher is the cipher you're encrypting it with
  • key is the encryption key, and it's keylen bytes long πŸ”‘
  • the library context libctx (that's the ctx argument in the synopsis, iykyk) and the property query propq get used when pulling algorithms out of providers ✨
  • flags is a set of optional flags β€” the fun little toggles

πŸ¦‹ the flags πŸ¦‹

the flags field supports these options:

  • CMS_DETACHED
  • CMS_STREAM
  • CMS_PARTIAL

and here's the important bit: internally CMS_final() gets called unless CMS_STREAM and/or CMS_PARTIAL is specified. so like, no CMS_STREAM, no CMS_PARTIAL, CMS_final() shows up to the party whether you asked or not πŸ’€

πŸŽ€ the cipher tea πŸŽ€

the algorithm you pass in the cipher parameter must support ASN1 encoding of its parameters. like, that's the requirement, no negotiating. πŸ™ˆ

and AEAD cipher algorithms are not supported β€” periodt. not a thing here, don't even try it.

also, when you're done with your CMS_ContentInfo structure, free it with CMS_ContentInfo_free(3). be a responsible main character 🫢

πŸ’– and the diet version πŸ’–

CMS_EncryptedData_encrypt() is literally the same as CMS_EncryptedData_encrypt_ex(), except it uses default values of NULL for the library context libctx and the property query propq. so if you don't care about that stuff, take the shortcut bestie, it's iconic. ✨


πŸ‘‘ RETURN VALUES πŸ‘‘

  • if the allocation fails, CMS_EncryptedData_encrypt_ex() and CMS_EncryptedData_encrypt() return NULL and set an error code that you can grab with ERR_get_error(3) 😭
  • otherwise they return a pointer to the freshly allocated structure β€” slay πŸ’…

🌸 SEE ALSO 🌸

(go read those too, they're the extended universe 🧚)


⭐ HISTORY ⭐

CMS_EncryptedData_encrypt_ex() was added in OpenSSL 3.0. so it's a newer model, not an OG. still ate tho. πŸ’œ


Copyright 2020-2021 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, you're fully caught up πŸ’– this was OpenSSL 4.0.3, dated 2026-09-29, from the CMS_ENCRYPTEDDATA_ENCRYPT(3SSL) page β€” congrats on finishing, you absolute legend, go encrypt something cute 🌈✨

πŸ“¦ where this page comes from