PKCS7_SIGN(3ossl)
π PKCS7_SIGN π
okay bestie, so this is PKCS7_SIGN(3ossl) β it's from the OpenSSL manual, section 3ossl, and it's all about signing stuff into a PKCS#7 signedData structure. two functions live here: PKCS7_sign_ex() and PKCS7_sign(). let's get into it π
β¨ NAME β¨
PKCS7_sign_ex, PKCS7_sign β create a PKCS#7 signedData structure
πΈ SYNOPSIS πΈ
#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. iconic behavior honestly.
here's who's who in the cast:
signcertis the certificate you're signing with, andpkeyis its corresponding private key. power couple fr πcertsis an optional set of extra certificates to include in the PKCS#7 structure β think any intermediate CAs in the chain.libctx(the library context) andpropq(the property query) get used when retrieving algorithms from providers.
the actual data that's getting signed? that gets read from the BIO data.
and flags is an optional set of flags. any of the following can be passed in, ored together:
- Many S/MIME clients expect the signed content to include valid MIME headers.</> if the
PKCS7_TEXTflag is set, MIME headers for type"text/plain"get prepended to the data. β¨ - if
PKCS7_NOCERTSis set, the signer's certificate and the extra certs will not be included in the PKCS7 structure. but heads up β the signer's certificate must still be supplied in thesigncertparameter though. this can shrink the size of the signatures if the signer's certificates can be grabbed by other means, like from a previously signed message. - the data being signed gets included in the PKCS7 structure, unless
PKCS7_DETACHEDis set β in which case it's omitted. this is what you use for PKCS7 detached signatures, which show up in S/MIME plaintext signed messages for example. - normally the supplied content is translated into MIME canonical format (as required by the S/MIME specifications). but if
PKCS7_BINARYis set, no translation occurs. you should use this option if the supplied data is in binary format, otherwise the translation will corrupt it. no cap, that's a real warning. π - the
signedDatastructure includes several PKCS#7 authenticatedAttributes, including the signing time, the PKCS#7 content type, and the supported list of ciphers in an SMIMECapabilities attribute. ifPKCS7_NOATTRis set, then no authenticatedAttributes get used. ifPKCS7_NOSMIMECAPis set, then just the SMIMECapabilities are omitted. - 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 won't be included. bye felicia π
- if the flag
PKCS7_STREAMis set, then the returned PKCS7 structure is just initialized, ready to perform the signing operation. the signing is however not performed, and the data to be signed is not read from thedataparameter. signing is deferred until after the data has been written. in this way, data can be signed in a single pass. slay, one-pass signing. β - if the
PKCS7_PARTIALflag is set, a partial PKCS7 structure is output, to which additional signers and capabilities can be added before finalization. build-a-bear energy π§Έ
okay so important: if the flag PKCS7_STREAM is set, the returned PKCS7 structure is not complete, and outputting its contents via a function that does not properly finalize the PKCS7 structure will give unpredictable results. π do not do that.
several functions do finalize the structure, including SMIME_write_PKCS7(), i2d_PKCS7_bio_stream(), 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. just the certs, no signing, we love a low-stakes vibe check.
history note: in versions of OpenSSL before1.0.0, thesigncertandpkeyparameters must not beNULL. so if you're time-traveling back there, don't.
and finally: 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. it's giving legacy support, and we respect it. π«Ά
π BUGS π
some advanced attributes such as counter signatures are not supported. that's the whole tea, 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). so if you get NULL, go ask ERR_get_error(3) what went down. π₯
π SEE ALSO π
π HISTORY π
- the function
PKCS7_sign_ex()was added in OpenSSL3.0. - the
PKCS7_PARTIALflag, and the ability forcerts,signcert, andpkeyparameters to beNULL, were added in OpenSSL1.0.0. - the
PKCS7_STREAMflag was added in OpenSSL1.0.0.
π COPYRIGHT π
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.
okay that's the whole page!! this was PKCS7_SIGN(3ossl) from OpenSSL 3.5.9, dated 2026-09-29. go sign something iconic bestie, and remember: PKCS7_BINARY for binary data or the translation will get you π
β¨ bye!! ππΈπ¦
π¦ where this page comes from
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 43: openssl-devel 1:3.5.9-1.fc43 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- fedora 44: openssl-devel 1:3.5.9-1.fc44 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms