PKCS7_SIGN(3ossl)

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

πŸ’… NAME πŸ’…

okay so bestie, this is the PKCS7_SIGN(3ossl) page from OpenSSL, and it's literally all about PKCS7_sign_ex and PKCS7_sign β€” the two functions that create a PKCS#7 signedData structure. that's the whole vibe. ✨


πŸŽ€ SYNOPSIS πŸŽ€

like, here's the exact shape of it, no cap:

#include <openssl/pkcs7.h>

PKCS7 *PKCS7_sign_ex(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
                     BIO *data, int flags, OSSL_LIB_CTX *libctx,
                     const char *propq);
PKCS7 *PKCS7_sign(X509 *signcert, EVP_PKEY *pkey, STACK_OF(X509) *certs,
                  BIO *data, int flags);

πŸ¦‹ DESCRIPTION πŸ¦‹

so PKCS7_sign_ex() creates and returns a PKCS#7 signedData structure. that's it, that's the job. πŸ‘‘

here's who's who in the cast:

  • signcert β€” the certificate you're signing with (the main character).
  • pkey β€” the corresponding private key (the ride-or-die).
  • certs β€” an optional set of extra certificates to include in the PKCS#7 structure, like, say, any intermediate CAs in the chain. lowkey important.
  • libctx and propq β€” the library context and the property query, and they get used when retrieving algorithms from providers.

the data that's getting signed is read from the BIO data. πŸ“–

flags is an optional set of flags. and bestie, there are a lot of them.

any of the following flags (ored together, iykyk) can be passed in the flags parameter:

  • PKCS7_TEXT β€” so many S/MIME clients expect the signed content to include valid MIME headers. if this one's set, MIME headers for type "text/plain" get prepended to the data. πŸŽ€
  • PKCS7_NOCERTS β€” if set, the signer's certificate and the extra certs will not be included in the PKCS7 structure. but the signer's certificate still has to be supplied in the signcert parameter, periodt. this can reduce the size of the signatures if the signer's certs can be obtained by other means β€” for example, a previously signed message. πŸ’…
  • PKCS7_DETACHED β€” the data being signed is included in the PKCS7 structure unless this is set, in which case it's omitted. this is what's used for PKCS7 detached signatures, which get used in S/MIME plaintext signed messages, for example. 🫢
  • PKCS7_BINARY β€” normally the supplied content is translated into MIME canonical format (as required by the S/MIME specifications). if this is set, no translation occurs. use this if the supplied data is in binary format β€” otherwise the translation will corrupt it. ⚠️ like, fr fr, don't skip this one.
  • PKCS7_NOATTR β€” the signedData structure includes several PKCS#7 authenticatedAttributes, including the signing time, the PKCS#7 content type, and the supported list of ciphers in an SMIMECapabilities attribute. if this flag is set, then no authenticatedAttributes will be used at all. πŸ™ˆ
  • PKCS7_NOSMIMECAP β€” if this one's set, then just the SMIMECapabilities are omitted (not the whole attr situation).
  • PKCS7_STREAM β€” if set, the returned PKCS7 structure is just initialized, ready to perform the signing operation. but the signing is not performed, and the data to be signed is not read from the data parameter. signing is deferred until after the data has been written. in this way, data can be signed in a single pass. ✨
  • PKCS7_PARTIAL β€” if set, a partial PKCS7 structure is output, to which additional signers and capabilities can be added before finalization. 🧚

if present, the SMIMECapabilities attribute indicates support for the following algorithms: triple DES, 128 bit RC2, 64 bit RC2, DES and 40 bit RC2. if any of these algorithms is disabled, then it will not be included.

🚨 okay huge warning time: if the flag PKCS7_STREAM is set, the returned PKCS7 structure is not complete. outputting its contents via a function that doesn't properly finalize the PKCS7 structure will give unpredictable results. like, chaos. πŸ’€

several functions finalize the structure, including SMIME_write_PKCS7(), i2d_PKCS7_bio_stream(), and PEM_write_bio_PKCS7_stream(). alternatively, finalization can be performed by obtaining the streaming ASN1 BIO directly using BIO_new_PKCS7().

if a signer is specified, it will use the default digest for the signing algorithm. this is SHA256 for both RSA and DSA keys. ⭐

the certs, signcert and pkey parameters can all be NULL if the PKCS7_PARTIAL flag is set. one or more signers can be added using the function PKCS7_sign_add_signer(). PKCS7_final() must also be called to finalize the structure if streaming is not enabled. alternative signing digests can also be specified using this method. 🌈

if signcert and pkey are NULL, then a certificates only PKCS#7 structure is output.

in versions of OpenSSL before 1.0.0, the signcert and pkey parameters must not be NULL. πŸ“Œ

PKCS7_sign() is like PKCS7_sign_ex() except that it uses default values of NULL for the library context libctx and the property query propq. this is retained for API backward compatibility. πŸ’œ


πŸ’€ BUGS πŸ’€

some advanced attributes, such as counter signatures, are not supported. that's just how it is, bestie. 😭


πŸ”₯ RETURN VALUES πŸ”₯

PKCS7_sign_ex() and PKCS7_sign() return either a valid PKCS7 structure or NULL if an error occurred. the error can be obtained from ERR_get_error(3). πŸ•΅οΈβ€β™€οΈ


🌸 SEE ALSO 🌸


πŸ‘‘ HISTORY πŸ‘‘

  • the function PKCS7_sign_ex() was added in OpenSSL 3.0. ✨
  • the PKCS7_PARTIAL flag, and the ability for certs, signcert, and pkey parameters to be NULL, were added in OpenSSL 1.0.0.
  • the PKCS7_STREAM flag was added in OpenSSL 1.0.0.

Copyright 2002-2025 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 one's from OpenSSL version 3.5.6, dated 2026-04-07 β€” screenshot it, cite it, sign your data with it, slay. πŸ’…πŸŽ€βœ¨

πŸ“¦ where this page comes from