PKCS7_VERIFY(3)

📦 version 4.4.0, in alpine 3.24, arch rolling

💖 NAME 💖

okay so this is PKCS7_VERIFY(3) from libcrypto, and it’s here to explain PKCS7_verify() and PKCS7_get0_signers() — aka how to verify a PKCS#7 signedData structure. slay. ✨

✨ SYNOPSIS ✨

library “libcrypto” #include <openssl/pkcs7.h>

int
PKCS7_verify(PKCS7 *p7, STACK_OF(X509) *certs, X509_STORE *store,
    BIO *indata, BIO *out, int flags);

STACK_OF(X509) *
PKCS7_get0_signers(PKCS7 *p7, STACK_OF(X509) *certs, int flags);

🦋 DESCRIPTION 🦋

PKCS7_verify() is the main character here: it verifies a PKCS#7 signedData structure. Here’s the cast, bestie:

  • p7 — the PKCS7 structure you’re verifying.
  • certs — a set of certificates to search for the signer’s certificate.
  • store — a trusted certificate store, used for chain verification.
  • indata — the signed data when the content is not present in p7, i.e. when it’s detached.
  • out — if this is not NULL, the content gets written here.
  • flags — an optional set of flags that can modify the verify operation.

PKCS7_get0_signers() retrieves the signer’s certificates from p7. The signers must be freed with sk_X509_free(). It does not check their validity or whether any signatures are valid. The certs and flags parameters have the same meanings as in PKCS7_verify().

Normally the verify process proceeds like this:

  1. First, some sanity checks happen on p7. The type of p7 must be signedData. There must be at least one signature on the data, and if the content is detached, indata cannot be NULL. If the content is not detached and indata is not NULL, then the structure has both embedded and external content. To treat this as an error, use the flag PKCS7_NO_DUAL_CONTENT. The default behavior allows this, for compatibility with other implementations.
  2. An attempt is made to locate all the signer’s certificates, first looking in the certs parameter (if it is not NULL) and then looking in any certificates contained in the p7 structure itself. If any signer’s certificates cannot be located, the operation fails.
  3. Each signer’s certificate is chain verified using the smimesign purpose and the supplied trusted certificate store. Any internal certificates in the message are used as untrusted CAs. If any chain verify fails, an error code is returned.
  4. Finally, the signed content is read (and written to out if it is not NULL) and the signature’s checked.

If all signature’s verify correctly, then the function is successful. periodt. 💅

Any of the following flags (OR’ed together) can be passed in the flags parameter to change the default verify behaviour. Only the flag PKCS7_NOINTERN is meaningful to PKCS7_get0_signers().

  • PKCS7_NO_DUAL_CONTENT — treat dual embedded + external content as an error, instead of allowing it by default for compatibility with other implementations.
  • PKCS7_NOINTERN — do not search the certificates in the message itself when locating the signer’s certificate. This means all the signer’s certificates must be in the certs parameter.
  • PKCS7_TEXT — delete MIME headers for type text/plain from the content. If the content is not of type text/plain, then an error is returned.
  • PKCS7_NOVERIFY — do not chain verify the signer’s certificates.
  • PKCS7_NOCHAIN — do not use the certificates contained in the message as untrusted CAs. This means the whole verify chain (apart from the signer’s certificate) must be contained in the trusted store.
  • PKCS7_NOSIGS — do not check the signatures on the data.

One application of PKCS7_NOINTERN is to only accept messages signed by a small number of certificates. The acceptable certificates would be passed in the certs parameter. In this case, if the signer is not one of the certificates supplied in certs, then the verify will fail because the signer cannot be found.

Careful, bestie: modifying the default verify behaviour is a whole thing. For example, setting PKCS7_NOVERIFY | PKCS7_NOSIGS will totally disable all verification, and any signed message will be considered valid. That combination is however useful if one merely wishes to write the content to out and its validity is not considered important. 🙈

Chain verification should arguably be performed using the signing time rather than the current time. However, since the signing time is supplied by the signer, it cannot be trusted without additional evidence (such as a trusted timestamp).

🎀 RETURN VALUES 🎀

  • PKCS7_verify() returns 1 for a successful verification and 0 or a negative value if an error occurs.
  • PKCS7_get0_signers() returns all signers or NULL if an error occurred. The signers must be freed with sk_X509_free().
  • The error can be obtained from ERR_get_error(3).

🌈 SEE ALSO 🌈

🌸 HISTORY 🌸

PKCS7_verify() and PKCS7_get0_signers() first appeared in OpenSSL 0.9.5 and have been available since OpenBSD 2.7. iconic behavior. 👑

💀 BUGS 💀

The trusted certificate store is not searched for the signer’s certificate. This is primarily due to the inadequacies of the current X509_STORE functionality.

The lack of single pass processing and the need to hold all data in memory as mentioned in PKCS7_sign(3) also applies to PKCS7_verify().


okay that’s the whole page, mwah 💋 — this one comes from Debian, dated December 20, 2025, and the manual itself is PKCS7_VERIFY(3). 🫶

📦 where this page comes from