• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Mister PKI

SSL Certificates * SSL Tools * Certificate Decoder

  • PKI Training
  • OpenSSL
  • Keytool
  • PKI Tools
    • Apereo CAS
    • SAML
    • OpenID Connect
  • Get expert help

OpenSSL cms: Sign, Verify, Encrypt, and Decrypt Files

August 17, 2026 by Mister PKI 1 Comment

Overview

TLS protects information while it travels across a network, but sometimes a system administrator needs to protect the file itself.

Consider a configuration export that must be transferred from one administrative environment to another.

You want the recipient to be able to answer two questions:

  1. Did this file really come from the expected sender, and has anyone modified it?
  2. Can anyone other than the intended recipient read it?

The OpenSSL cms command can solve both problems.

CMS stands for Cryptographic Message Syntax. OpenSSL can use CMS to:

  • digitally sign a file;
  • verify a digital signature;
  • encrypt a file for one or more recipients;
  • decrypt an encrypted file;
  • create detached signatures;
  • include or omit signer certificates;
  • add multiple signatures;
  • create S/MIME-compatible messages;
  • process CMS in PEM, DER, or S/MIME format;
  • inspect the underlying CMS structure.

The official OpenSSL cms documentation describes openssl cms as a command for handling CMS data, including S/MIME messages, with operations for encryption, decryption, signing, verification, compression, and other CMS content types.

This tutorial uses a practical system-administration scenario:

A system administrator needs to securely transfer a sensitive configuration export to another administrator. The sender will digitally sign the file so its authenticity and integrity can be verified, then encrypt it so only the intended recipient can read it.

We will use openssl cms to:

  1. inspect the sender and recipient certificates;
  2. create a CMS digital signature;
  3. verify that signature;
  4. demonstrate how tampering breaks verification;
  5. create a detached signature;
  6. encrypt a file for a recipient;
  7. decrypt the file;
  8. encrypt for multiple recipients;
  9. sign and encrypt the same file;
  10. inspect CMS structures;
  11. work with PEM, DER, and S/MIME formats;
  12. troubleshoot common CMS errors.

What is CMS?

CMS stands for Cryptographic Message Syntax.

It is a standardized format for cryptographically protected content such as:

  • signed data;
  • encrypted data;
  • digested data;
  • authenticated data;
  • certificate-containing structures.

CMS grew out of the older PKCS#7 specification and is closely associated with S/MIME.

A useful distinction is:

PKCS#7
   ↓
evolved into
   ↓
CMS

The formats remain closely related, which is why administrators sometimes encounter CMS files with extensions such as:

.p7m
.p7s
.p7b
.der
.pem

File extensions are only hints. The actual encoded content determines how OpenSSL interprets the file.

For more background on the older format, see PKCS#7 Certificates and SignedData.

What does openssl cms do?

The basic command structure is:

openssl cms OPERATION OPTIONS

Common operations include:

-sign
-verify
-encrypt
-decrypt
-resign
-cmsout
-digest_create
-digest_verify
-compress
-uncompress

For most system administrators, the four most useful operations are:

-sign
-verify
-encrypt
-decrypt

These map to two different security goals.

Digital signatures

Sign
 ↓
Authentication + Integrity

A digital signature helps prove:

  • who signed the data;
  • whether the data changed after signing.

Encryption

Encrypt
 ↓
Confidentiality

Encryption prevents unauthorized users from reading the protected content.

Signing does not make a file confidential.

Encryption does not necessarily prove who created the original file.

When both properties are required, sign and encrypt.

OpenSSL version compatibility

This article covers:

  • OpenSSL 3.0;
  • OpenSSL 3.1;
  • OpenSSL 3.2;
  • OpenSSL 3.3;
  • OpenSSL 3.4;
  • OpenSSL 3.5 LTS;
  • OpenSSL 4.0.

Check your installed version:

openssl version

For complete build information:

openssl version -a

The main CMS operations remain consistent across the supported versions:

openssl cms -sign
openssl cms -verify
openssl cms -encrypt
openssl cms -decrypt
openssl cms -cmsout

There are, however, several important differences.

OpenSSL 3.0

OpenSSL 3.0 is the compatibility baseline for this series.

The provider architecture applies to CMS just as it does to other OpenSSL commands.

The older:

-engine

option was deprecated in OpenSSL 3.0.

OpenSSL 3.0 also added the -nameopt option used when displaying certificate names.

OpenSSL 3.2

OpenSSL 3.2 added:

-digest

for signing a precomputed digest rather than calculating the digest directly from input data.

This is a specialized workflow. Normal file signing should continue using the file itself unless there is a specific reason to supply a precomputed digest.

OpenSSL 3.5 LTS

OpenSSL 3.5 is the primary documentation target for this series.

One important behavioral change occurred in 3.5:

The default CMS content-encryption cipher changed from 3DES to AES-256.

For scripts where the encryption algorithm matters, specify it explicitly instead of relying on a version-dependent default.

For example:

-aes-256-cbc

This makes the intended encryption behavior clear on both OpenSSL 3.0 and OpenSSL 3.5.

OpenSSL 4.0

The main CMS functionality remains available in OpenSSL 4.0.

The deprecated ENGINE option was removed.

Modern integrations should use OpenSSL providers rather than ENGINE-based configuration.

For ordinary certificate-based signing and encryption, the workflow in this article remains applicable.

Real-world scenario

Assume two administrators need to transfer:

system-export.json

The file contains sensitive application configuration.

The sender has:

sender.crt
sender.key

The recipient has:

recipient.crt
recipient.key

A CA certificate or chain is also available:

ca-chain.pem

The goal is:

system-export.json
       ↓
Digital signature
       ↓
Verify authenticity
       ↓
Encryption
       ↓
Secure transfer
       ↓
Recipient decrypts
       ↓
Signature verified again

All filenames and identities in this tutorial are generic placeholders.

Step 1: Confirm CMS support

Run:

openssl list -1 -commands | grep '^cms$'

Expected:

cms

Display command help:

openssl cms -help

Because cms has many options, you can search the output.

For signing:

openssl cms -help 2>&1 | grep signer

For encryption:

openssl cms -help 2>&1 | grep recip

For more command-discovery examples, see How to List OpenSSL Commands and Get Command Help.

Step 2: Inspect the sender certificate

Before using a certificate for signing, inspect it:

openssl x509 \
  -in sender.crt \
  -noout \
  -subject \
  -issuer \
  -serial \
  -dates

Inspect its extensions:

openssl x509 \
  -in sender.crt \
  -noout \
  -purpose

You can also review the complete certificate:

openssl x509 \
  -in sender.crt \
  -text \
  -noout

Confirm that:

  • the certificate is not expired;
  • the issuer is expected;
  • the certificate belongs to the intended signer;
  • the certificate has an appropriate purpose for your application.

For more certificate-inspection examples, see How to View a Certificate with OpenSSL.

Step 3: Verify the sender certificate chain

Before relying on a signature, confirm that you trust the certificate that will produce it.

Run:

openssl verify \
  -CAfile ca-chain.pem \
  sender.crt

Expected:

sender.crt: OK

A cryptographically valid signature from an untrusted certificate does not automatically establish a trusted identity.

For certificate chain troubleshooting, see OpenSSL Verify: Certificate, Chain, CRL, and Key Examples.

Step 4: Confirm the private key matches the certificate

Extract the certificate public key:

openssl x509 \
  -in sender.crt \
  -pubkey \
  -noout \
  > sender-cert-public.pem

Extract the public key from the private key:

openssl pkey \
  -in sender.key \
  -pubout \
  > sender-key-public.pem

Compare:

cmp \
  sender-cert-public.pem \
  sender-key-public.pem

No output indicates that the public keys match.

Clean up:

rm \
  sender-cert-public.pem \
  sender-key-public.pem

This check avoids attempting to sign with a private key that does not correspond to the selected signing certificate.

Step 5: Create the file to be protected

Our example input is:

system-export.json

Inspect its size:

ls -lh system-export.json

Calculate a SHA-256 hash before processing:

openssl dgst \
  -sha256 \
  system-export.json

Record the result if your operational procedure requires independent integrity tracking.

Do not display or publish real confidential configuration contents when documenting the workflow.

Step 6: Create a CMS digital signature

To create a CMS-signed file:

openssl cms \
  -sign \
  -in system-export.json \
  -signer sender.crt \
  -inkey sender.key \
  -out signed-message.pem \
  -outform PEM \
  -binary \
  -nodetach

This produces:

signed-message.pem

Important options include:

-sign

Creates a CMS SignedData structure.

-in

Specifies the file being signed.

-signer

Provides the signer’s certificate.

-inkey

Provides the corresponding private key.

-outform PEM

Encodes the CMS object in PEM.

-binary

Prevents S/MIME newline canonicalization from changing the original byte stream.

-nodetach

Includes the original data inside the CMS object.

The result is therefore an opaque or attached signature.

Why use -binary for files?

CMS and S/MIME were historically designed around mail messages.

Without:

-binary

OpenSSL can canonicalize line endings into the CRLF format expected by S/MIME.

That behavior may be appropriate for email.

It is generally undesirable for arbitrary files such as:

.json
.zip
.tar.gz
.conf
.xml
.bin

When protecting arbitrary file bytes, use:

-binary

during both creation and verification.

Step 7: Inspect the signed CMS structure

Use:

openssl cms \
  -cmsout \
  -in signed-message.pem \
  -inform PEM \
  -print \
  -noout

This displays the CMS structure.

You may see elements corresponding to:

SignedData
certificates
digestAlgorithms
encapContentInfo
signerInfos

The output is useful for determining:

  • which digest algorithm was used;
  • which certificate was embedded;
  • which signed attributes exist;
  • whether the content is attached;
  • which signer identifiers appear.

For deeper ASN.1 inspection, the CMS object can also be examined with OpenSSL asn1parse.

Step 8: Verify the CMS signature

Verify:

openssl cms \
  -verify \
  -in signed-message.pem \
  -inform PEM \
  -CAfile ca-chain.pem \
  -binary \
  -out verified-system-export.json

If both the CMS signature and signer certificate validation succeed, OpenSSL reports:

CMS Verification successful

The original content is written to:

verified-system-export.json

Compare it with the original:

cmp \
  system-export.json \
  verified-system-export.json

No output means the files are identical.

Step 9: Save the signer certificate during verification

Use:

-signer

during verification to save the signer certificate:

openssl cms \
  -verify \
  -in signed-message.pem \
  -inform PEM \
  -CAfile ca-chain.pem \
  -binary \
  -signer verified-signer.pem \
  -out verified-system-export.json

Inspect it:

openssl x509 \
  -in verified-signer.pem \
  -noout \
  -subject \
  -issuer \
  -serial \
  -fingerprint \
  -sha256

This is useful when a CMS message arrives from another system and you need to determine exactly which certificate signed it.

Signature verification has two parts

When verifying CMS data, there are two separate questions:

1. Is the digital signature mathematically valid?
2. Do I trust the certificate that created it?

These are not the same thing.

A file can contain a perfectly valid digital signature created by a certificate you do not trust.

That is why the CA trust configuration matters.

The normal verification command:

openssl cms \
  -verify \
  -CAfile ca-chain.pem \
  ...

does both:

signature validation
+
signer certificate validation

Step 10: Verify only the cryptographic signature

For troubleshooting, you can use:

-noverify

Example:

openssl cms \
  -verify \
  -in signed-message.pem \
  -inform PEM \
  -noverify \
  -binary \
  -out verified-system-export.json

This skips signer-certificate validation.

It can answer:

Is the CMS signature structurally and cryptographically valid even though I do not currently have the correct CA chain?

Do not treat a successful -noverify result as proof that the signer is trusted.

Use this option for diagnosis, not as a shortcut around trust validation.

Step 11: Detect file tampering

A digital signature is useful because modification of the protected content causes signature verification to fail.

For an attached CMS object, extract the verified file normally and keep it as the baseline.

For a detached-signature example later in this article, modify the original file:

printf '\n' >> system-export.json

Then attempt verification against the detached signature.

Verification should fail because the input bytes no longer match the bytes that were signed.

This gives the administrator evidence that:

the content has changed

or:

the wrong content file was supplied

Step 12: Create a detached CMS signature

Sometimes you want to distribute the original file normally and provide a separate signature.

For example:

system-export.json
system-export.p7s

Create a DER-formatted detached signature:

openssl cms \
  -sign \
  -in system-export.json \
  -signer sender.crt \
  -inkey sender.key \
  -binary \
  -outform DER \
  -out system-export.p7s

Notice that we did not specify:

-nodetach

The resulting CMS signature is detached.

This means the CMS object contains the signature but not the original file content.

Step 13: Verify a detached signature

Because the content is not embedded in the CMS object, provide it separately:

openssl cms \
  -verify \
  -inform DER \
  -in system-export.p7s \
  -content system-export.json \
  -binary \
  -CAfile ca-chain.pem \
  -out /dev/null

Expected:

CMS Verification successful

This pattern is useful for:

  • software release files;
  • configuration exports;
  • archived reports;
  • firmware images;
  • deployment artifacts;
  • administrative files.

The file remains in its original format while the signature is distributed separately.

Step 14: Demonstrate tamper detection

Create a copy:

cp \
  system-export.json \
  system-export-modified.json

Change it:

printf '\n{"modified":true}\n' \
  >> system-export-modified.json

Now verify using the original signature:

openssl cms \
  -verify \
  -inform DER \
  -in system-export.p7s \
  -content system-export-modified.json \
  -binary \
  -CAfile ca-chain.pem \
  -out /dev/null

Verification should fail.

That is the central integrity property of a digital signature.

A single changed byte can invalidate the signature.

Step 15: Specify the signing digest

OpenSSL normally chooses an appropriate default digest for the signing key.

For many conventional signing keys, that will commonly be SHA-256.

You can specify it explicitly:

openssl cms \
  -sign \
  -in system-export.json \
  -signer sender.crt \
  -inkey sender.key \
  -md sha256 \
  -binary \
  -nodetach \
  -outform PEM \
  -out signed-message.pem

Explicitly setting:

-md sha256

can make automated procedures easier to audit.

Do not select obsolete digest algorithms merely for compatibility without understanding the security consequences.

Step 16: Create a CAdES Basic Electronic Signature

CMS also supports:

-cades

during signing.

For example:

openssl cms \
  -sign \
  -in system-export.json \
  -signer sender.crt \
  -inkey sender.key \
  -cades \
  -binary \
  -nodetach \
  -outform DER \
  -out signed-cades.der

The option adds signing-certificate information required for a CAdES Basic Electronic Signature.

Verify while requiring CAdES behavior:

openssl cms \
  -verify \
  -inform DER \
  -in signed-cades.der \
  -cades \
  -binary \
  -CAfile ca-chain.pem \
  -out verified-output.json

CAdES is primarily relevant when a workflow specifically requires ETSI-style electronic-signature formats.

Do not add it to ordinary administrative workflows simply because the option exists.

Step 17: Inspect the recipient certificate

Before encrypting the file, inspect:

recipient.crt

Run:

openssl x509 \
  -in recipient.crt \
  -noout \
  -subject \
  -issuer \
  -serial \
  -dates

Check its public key:

openssl x509 \
  -in recipient.crt \
  -noout \
  -text

Verify its trust chain if appropriate:

openssl verify \
  -CAfile ca-chain.pem \
  recipient.crt

A CMS encryption operation uses the public key from the recipient’s certificate.

You do not need the recipient’s private key to encrypt the file.

How CMS certificate encryption works

CMS does not normally encrypt the complete file directly with the recipient’s public key.

Conceptually:

Input file
    ↓
Generate random content-encryption key
    ↓
Encrypt file with symmetric cipher
    ↓
Protect content-encryption key for recipient
    ↓
Create CMS EnvelopedData

The recipient later uses their private key to recover the content-encryption key and decrypt the data.

This hybrid model allows public-key cryptography to protect the secret key while efficient symmetric encryption protects the potentially large file.

Step 18: Encrypt the file for one recipient

Use:

openssl cms \
  -encrypt \
  -in system-export.json \
  -out encrypted-message.pem \
  -outform PEM \
  -binary \
  -aes-256-cbc \
  recipient.crt

This creates a CMS:

EnvelopedData

structure.

The recipient certificate appears last:

recipient.crt

because OpenSSL treats trailing certificate filenames as encryption recipients.

The encrypted result:

encrypted-message.pem

can be transferred without exposing the plaintext.

Why explicitly specify AES-256?

OpenSSL 3.5 changed the default CMS encryption cipher from:

3DES

to:

AES-256

If your command simply says:

openssl cms -encrypt ...

its behavior can therefore differ depending on the installed OpenSSL branch.

For repeatable automation, specify the cipher explicitly:

-aes-256-cbc

or another cipher required by your interoperability and security requirements.

This prevents an upgrade from silently changing the encryption algorithm.

Step 19: Decrypt the CMS file

The recipient needs:

recipient.crt
recipient.key

Run:

openssl cms \
  -decrypt \
  -in encrypted-message.pem \
  -inform PEM \
  -recip recipient.crt \
  -inkey recipient.key \
  -binary \
  -out decrypted-system-export.json

Compare:

cmp \
  system-export.json \
  decrypted-system-export.json

No output means the decrypted file is byte-for-byte identical to the original.

Why does decryption need the private key?

Encryption used:

recipient.crt

which contains the recipient’s public key.

Decryption requires:

recipient.key

which contains the matching private key.

The intended security relationship is:

Anyone with recipient certificate
        ↓
can encrypt
Only holder of recipient private key
        ↓
can decrypt

This makes certificate-based CMS encryption useful when a sender must securely send data without already sharing a symmetric password.

Step 20: Encrypt for multiple recipients

A major advantage of CMS is that one encrypted message can support multiple recipients.

For example:

openssl cms \
  -encrypt \
  -in system-export.json \
  -out encrypted-for-team.pem \
  -outform PEM \
  -binary \
  -aes-256-cbc \
  admin-one.crt \
  admin-two.crt \
  archive-service.crt

Each recipient can decrypt the same encrypted CMS object using their own private key.

For example:

openssl cms \
  -decrypt \
  -in encrypted-for-team.pem \
  -inform PEM \
  -recip admin-two.crt \
  -inkey admin-two.key \
  -binary \
  -out decrypted.json

This is preferable to sharing one private decryption key among several administrators.

Each recipient retains control of their own private key.

Step 21: Use -recip explicitly during encryption

OpenSSL also permits:

-recip

during encryption.

For example:

openssl cms \
  -encrypt \
  -in system-export.json \
  -out encrypted-message.pem \
  -outform PEM \
  -binary \
  -aes-256-cbc \
  -recip recipient.crt

The -recip form is especially useful when customized public-key parameters are required.

It can also be repeated for multiple recipients.

Step 22: Use RSA-OAEP for recipient key transport

When an RSA recipient certificate is used, you may need RSA-OAEP rather than older RSA PKCS#1 v1.5 key transport.

Use -keyopt after the associated recipient:

openssl cms \
  -encrypt \
  -in system-export.json \
  -out encrypted-message.der \
  -outform DER \
  -binary \
  -aes-256-cbc \
  -recip recipient.crt \
  -keyopt rsa_padding_mode:oaep

The receiving implementation must support the corresponding CMS key-transport parameters.

Do not change these parameters purely for cosmetic reasons; interoperability with the destination application matters.

Step 23: Sign and encrypt the same file

For sensitive administrative transfers, you may want both:

Authenticity
Integrity
Confidentiality

One straightforward approach is:

Original file
     ↓
Sign
     ↓
CMS SignedData
     ↓
Encrypt
     ↓
CMS EnvelopedData

First sign:

openssl cms \
  -sign \
  -in system-export.json \
  -signer sender.crt \
  -inkey sender.key \
  -binary \
  -nodetach \
  -outform DER \
  -out signed.der

Then encrypt that signed CMS object:

openssl cms \
  -encrypt \
  -in signed.der \
  -binary \
  -aes-256-cbc \
  -outform DER \
  -out signed-and-encrypted.der \
  recipient.crt

The resulting file contains an encrypted CMS object whose plaintext is itself a signed CMS object.

Step 24: Decrypt and verify a signed-and-encrypted file

First decrypt:

openssl cms \
  -decrypt \
  -inform DER \
  -in signed-and-encrypted.der \
  -recip recipient.crt \
  -inkey recipient.key \
  -binary \
  -out recovered-signed.der

Then verify:

openssl cms \
  -verify \
  -inform DER \
  -in recovered-signed.der \
  -binary \
  -CAfile ca-chain.pem \
  -out recovered-system-export.json

Finally compare:

cmp \
  system-export.json \
  recovered-system-export.json

This validates all three desired properties:

Confidentiality → decryption succeeded
Authenticity → trusted sender certificate verified
Integrity → CMS signature verified

Sign then encrypt or encrypt then sign?

Both constructions are possible, but they expose different information.

With:

sign → encrypt

the signature itself is protected inside the encrypted object.

A recipient must decrypt the message before learning who signed it.

This is generally a sensible model for sensitive administrative files.

With:

encrypt → sign

the outer signature can reveal signer-related information even though the underlying data remains encrypted.

Choose the construction based on the application protocol and interoperability requirements.

For a simple secure-file-transfer workflow, signing first and then encrypting is easy to understand and test.

Step 25: Create a CMS object in DER format

PEM is convenient for text-based transport:

-----BEGIN CMS-----
...
-----END CMS-----

DER is binary.

Create a DER signature:

openssl cms \
  -sign \
  -in system-export.json \
  -signer sender.crt \
  -inkey sender.key \
  -binary \
  -nodetach \
  -outform DER \
  -out signed-message.der

Verify it:

openssl cms \
  -verify \
  -inform DER \
  -in signed-message.der \
  -binary \
  -CAfile ca-chain.pem \
  -out verified-system-export.json

DER is useful when:

  • another application requires binary CMS;
  • you are producing .p7m or .p7s artifacts;
  • MIME wrapping is unnecessary;
  • deterministic ASN.1 encoding is desirable.

Step 26: Convert CMS between PEM and DER

Convert a PEM CMS object to DER:

openssl cms \
  -cmsout \
  -inform PEM \
  -in signed-message.pem \
  -outform DER \
  -out signed-message.der

Convert DER to PEM:

openssl cms \
  -cmsout \
  -inform DER \
  -in signed-message.der \
  -outform PEM \
  -out signed-message.pem

This changes the encoding, not the cryptographic signature or encrypted content itself.

Step 27: Work with S/MIME format

CMS is also used by S/MIME.

OpenSSL supports:

-inform SMIME
-outform SMIME

For example:

openssl cms \
  -sign \
  -in message.txt \
  -signer sender.crt \
  -inkey sender.key \
  -text \
  -outform SMIME \
  -out signed-email.msg

The:

-text

option adds plain-text MIME headers.

This is appropriate when creating actual S/MIME-style message content.

For arbitrary binary files, prefer:

-binary

rather than:

-text

CMS is broader than email even though S/MIME is one of its best-known uses.

CMS versus S/MIME

These terms are related but not identical.

CMS defines the cryptographic message structures.

S/MIME uses CMS to provide cryptographic protection for MIME email.

Conceptually:

CMS
 ├── SignedData
 ├── EnvelopedData
 └── other content types
         ↓
S/MIME uses CMS structures
inside MIME messages

For command-line file processing, openssl cms is generally the more capable modern tool.

CMS versus PKCS#7

CMS evolved from PKCS#7 and the formats remain closely related.

The OpenSSL documentation specifically includes a section on compatibility with PKCS#7.

In practice, you may encounter:

.p7b
.p7c
.p7m
.p7s

that contain CMS or PKCS#7-style objects.

For certificate-only PKCS#7 bundles, the dedicated:

openssl pkcs7

command may be simpler.

See PKCS#7 Certificates and SignedData for certificate-bundle examples.

Use openssl cms when you need modern cryptographic message operations such as:

signing
verification
encryption
decryption

CMS versus PKCS#12

PKCS#12 serves a very different purpose.

A PKCS#12 file such as:

certificate.p12
certificate.pfx

is commonly used to store credentials, including:

  • private keys;
  • certificates;
  • certificate chains.

CMS is used to protect content.

A useful distinction is:

PKCS#12
    ↓
stores identities and keys
CMS
    ↓
signs and encrypts content

You might extract a signing certificate and key from a PKCS#12 file and then use those credentials with openssl cms.

See PKCS#12 Certificates and Keystores for PKCS#12 examples.

Step 28: Extract CMS certificates

A CMS object may include certificates.

Extract them:

openssl cms \
  -cmsout \
  -inform PEM \
  -in signed-message.pem \
  -certsout embedded-certificates.pem \
  -out /dev/null

Or during other operations use:

-certsout

as applicable.

Inspect the resulting certificates:

openssl crl2pkcs7 \
  -nocrl \
  -certfile embedded-certificates.pem \
  | openssl pkcs7 \
      -print_certs \
      -noout

For a single known certificate, use openssl x509 directly.

Step 29: Sign without embedding the signer certificate

By default, OpenSSL includes the signer certificate in the CMS message.

Exclude it:

openssl cms \
  -sign \
  -in system-export.json \
  -signer sender.crt \
  -inkey sender.key \
  -nocerts \
  -binary \
  -nodetach \
  -outform DER \
  -out signed-no-cert.der

This makes the output smaller.

However, the verifier must already have the signer certificate.

Verify by supplying it:

openssl cms \
  -verify \
  -inform DER \
  -in signed-no-cert.der \
  -certfile sender.crt \
  -CAfile ca-chain.pem \
  -binary \
  -out verified.json

Embedding the signer certificate is usually more convenient unless size or protocol requirements say otherwise.

Remember: embedding a certificate does not automatically make that certificate trusted.

Step 30: Add additional certificates

Use:

-certfile

to include additional certificates while signing:

openssl cms \
  -sign \
  -in system-export.json \
  -signer sender.crt \
  -inkey sender.key \
  -certfile intermediate-ca.pem \
  -binary \
  -nodetach \
  -outform PEM \
  -out signed-message.pem

This can make it easier for the receiving system to build the signer certificate chain.

Do not assume that including a root CA inside the CMS object makes the root trusted.

Trust anchors must come from the verifier’s trust configuration.

Step 31: Add multiple signers

CMS supports more than one signer.

For example:

openssl cms \
  -sign \
  -in system-export.json \
  -signer admin-one.crt \
  -inkey admin-one.key \
  -signer admin-two.crt \
  -inkey admin-two.key \
  -binary \
  -nodetach \
  -outform DER \
  -out dual-signed.der

This produces a CMS SignedData object containing multiple signer entries.

Possible uses include:

  • administrative approval workflows;
  • release authorization;
  • dual-control processes;
  • multi-party attestations.

Whether your consuming application understands multiple CMS signers is application-specific.

Step 32: Add a signer to an existing CMS object

OpenSSL provides:

-resign

to add additional signers to an existing signed CMS message.

Conceptually:

SignedData
   ↓
existing signer
   +
new signer

A typical workflow resembles:

openssl cms \
  -resign \
  -inform DER \
  -in signed.der \
  -signer additional-signer.crt \
  -inkey additional-signer.key \
  -outform DER \
  -out resigned.der

The exact requirements depend on whether the content is attached or detached.

Use this when you specifically need another signature applied to an existing CMS SignedData object.

Step 33: Use streaming for large files

The:

-stream

and:

-indef

options enable streaming encoding.

For example:

openssl cms \
  -encrypt \
  -stream \
  -binary \
  -aes-256-cbc \
  -in large-backup.tar \
  -out large-backup.cms \
  recipient.crt

Streaming allows OpenSSL to process data without holding the entire content in memory.

This is useful for:

  • large archives;
  • database exports;
  • backup files;
  • large software artifacts.

Not every consuming implementation handles indefinite-length encodings equally well, so test interoperability before depending on streaming output.

Step 34: Create CMS DigestedData

CMS can also create a DigestedData structure:

openssl cms \
  -digest_create \
  -in system-export.json \
  -outform DER \
  -out digested-data.der

Verify it:

openssl cms \
  -digest_verify \
  -inform DER \
  -in digested-data.der \
  -out recovered-system-export.json

This provides integrity checking through a digest but does not provide signer authentication.

If you need to establish who approved or produced a file, use a digital signature rather than only a digest.

Step 35: Check CMS syntax without outputting content

Use:

openssl cms \
  -cmsout \
  -in signed-message.pem \
  -inform PEM \
  -noout

This attempts to parse the CMS structure without dumping it.

For more detail:

openssl cms \
  -cmsout \
  -in signed-message.pem \
  -inform PEM \
  -print \
  -noout

This is useful when troubleshooting:

Is this really CMS?
Is the encoding valid?
What type of CMS object is it?
Which algorithms are present?
Which certificates are embedded?

Step 36: Inspect CMS with asn1parse

A DER CMS object is ASN.1 data.

Inspect it:

openssl asn1parse \
  -inform DER \
  -in signed-message.der \
  -i

Search for OIDs:

openssl asn1parse \
  -inform DER \
  -in signed-message.der \
  -i |
grep OBJECT

This can reveal structures and algorithms that a higher-level CMS display does not make obvious.

See OpenSSL asn1parse: Decode and Troubleshoot ASN.1 Data for a deeper walkthrough.

Common OpenSSL CMS errors

Unable to load signing key file

Confirm the key exists:

ls -l sender.key

Check it:

openssl pkey \
  -in sender.key \
  -check \
  -noout

Confirm that the key matches the signing certificate.

Could not read signer certificate

Test the certificate:

openssl x509 \
  -in sender.crt \
  -noout \
  -subject

Possible causes include:

  • incorrect file path;
  • corrupted PEM data;
  • passing a private key where a certificate was expected;
  • unsupported or incorrect input format.

CMS Verification failure

Possible causes include:

  • content changed after signing;
  • wrong detached content supplied;
  • incorrect -binary handling;
  • signer certificate is untrusted;
  • required intermediate certificate is missing;
  • certificate validation failed;
  • wrong CMS input format was specified.

First distinguish signature failure from trust failure.

For troubleshooting only:

openssl cms \
  -verify \
  ... \
  -noverify

If that succeeds, investigate the certificate trust path.

Certificate verify error

Test the signer separately:

openssl verify \
  -CAfile ca-chain.pem \
  sender.crt

If that fails, CMS verification will not establish a trusted signer through that CA file either.

Error decrypting CMS structure

Possible causes include:

  • wrong private key;
  • wrong recipient certificate;
  • the CMS message was not encrypted for that recipient;
  • unsupported key-transport parameters;
  • corrupted CMS data.

Check the private key:

openssl pkey \
  -in recipient.key \
  -check \
  -noout

Confirm it matches:

recipient.crt

No recipient certificate or key specified

Supply:

-recip recipient.crt

and:

-inkey recipient.key

for decryption.

Example:

openssl cms \
  -decrypt \
  -in encrypted-message.pem \
  -recip recipient.crt \
  -inkey recipient.key \
  -out decrypted.txt

Bad decrypt or wrong recipient

Inspect the encrypted CMS object:

openssl cms \
  -cmsout \
  -in encrypted-message.pem \
  -print \
  -noout

The message may simply not contain recipient information corresponding to your certificate.

Error reading S/MIME message

The default CMS input format is S/MIME.

If the file is actually PEM:

-inform PEM

If it is binary DER:

-inform DER

Do not assume file extensions accurately indicate the encoded format.

Verification works without -binary but extracted file changes

For arbitrary file data, use:

-binary

consistently during signing and verification.

Without it, S/MIME canonicalization rules may alter line endings.

Unsupported cipher

List available cipher algorithms:

openssl list -cipher-algorithms

Also check providers:

openssl list -providers

The requested algorithm may be unavailable because of:

  • OpenSSL version;
  • provider configuration;
  • build options;
  • system cryptographic policy.

Do not use -debug_decrypt casually

OpenSSL provides:

-debug_decrypt

for specialized CMS decryption troubleshooting.

The OpenSSL documentation specifically advises caution with this option because decryption-error behavior can have security implications.

For ordinary administration, diagnose:

  • recipient selection;
  • certificate/key matching;
  • input format;
  • algorithm compatibility;

before resorting to decryption-debug options.

CMS verification and certificate trust

A common mistake is to think:

CMS Verification successful

always means:

I now know exactly who created this file.

That conclusion depends on how certificate validation was performed.

If you use:

-noverify

you have intentionally disabled trust-chain validation.

Likewise, if the CA file contains an inappropriate trust anchor, a valid cryptographic signature can still produce the wrong identity conclusion.

Treat CMS signer trust like any other PKI validation problem:

Signature valid?
      +
Certificate valid?
      +
Chain trusted?
      +
Certificate appropriate for use?

All of those questions matter.

CMS encryption does not verify the recipient certificate for you

When you encrypt data for:

recipient.crt

OpenSSL uses that certificate to construct the encrypted message.

Do not assume this means OpenSSL has independently established:

  • that the recipient certificate is currently trusted;
  • that it has not been revoked;
  • that it belongs to the person or system you intended.

Validate the recipient certificate before encryption when those properties matter.

For example:

openssl verify \
  -CAfile ca-chain.pem \
  recipient.crt

Then confirm its identity and fingerprint:

openssl x509 \
  -in recipient.crt \
  -noout \
  -subject \
  -issuer \
  -fingerprint \
  -sha256

Encrypting to the wrong certificate means encrypting the data for the wrong private-key holder.

Protect private keys

CMS workflows may involve:

sender.key
recipient.key

These are sensitive.

Protect them:

chmod 600 sender.key recipient.key

Do not:

  • email private keys;
  • store them in Git;
  • embed them in documentation;
  • copy them into public support tickets;
  • put private-key passwords directly into scripts;
  • share one private key among multiple recipients simply for convenience.

Where supported, use:

  • HSMs;
  • smart cards;
  • secure key stores;
  • OpenSSL providers;
  • managed secrets infrastructure.

Privacy considerations

CMS objects and certificate output can reveal:

  • email addresses;
  • employee names;
  • internal hostnames;
  • organization names;
  • certificate serial numbers;
  • certificate issuer names;
  • internal PKI architecture;
  • file contents;
  • custom OIDs;
  • signer identities.

Before publishing:

openssl cms -cmsout -print

output or screenshots, replace real environment values with generic placeholders.

This article uses:

sender.crt
recipient.crt
system-export.json
Example Organization

rather than real identities or infrastructure.

Never publish:

sender.key
recipient.key

or their contents.

Useful openssl cms commands

Sign a file with attached content:

openssl cms \
  -sign \
  -in file.bin \
  -signer sender.crt \
  -inkey sender.key \
  -binary \
  -nodetach \
  -outform DER \
  -out signed.der

Verify it:

openssl cms \
  -verify \
  -inform DER \
  -in signed.der \
  -binary \
  -CAfile ca-chain.pem \
  -out verified.bin

Create a detached signature:

openssl cms \
  -sign \
  -in file.bin \
  -signer sender.crt \
  -inkey sender.key \
  -binary \
  -outform DER \
  -out file.p7s

Verify detached content:

openssl cms \
  -verify \
  -inform DER \
  -in file.p7s \
  -content file.bin \
  -binary \
  -CAfile ca-chain.pem \
  -out /dev/null

Encrypt:

openssl cms \
  -encrypt \
  -in file.bin \
  -binary \
  -aes-256-cbc \
  -outform DER \
  -out encrypted.der \
  recipient.crt

Decrypt:

openssl cms \
  -decrypt \
  -inform DER \
  -in encrypted.der \
  -recip recipient.crt \
  -inkey recipient.key \
  -binary \
  -out decrypted.bin

Inspect:

openssl cms \
  -cmsout \
  -inform DER \
  -in encrypted.der \
  -print \
  -noout

Important openssl cms options

OptionPurpose
-signCreates CMS SignedData
-verifyVerifies CMS SignedData
-encryptCreates CMS EnvelopedData
-decryptDecrypts CMS EnvelopedData
-resignAdds another signer
-cmsoutParses or converts a CMS object
-inSpecifies input
-outSpecifies output
-informSets input as PEM, DER, or S/MIME
-outformSets output as PEM, DER, or S/MIME
-signerSpecifies a signing certificate or saves verified signer certificates
-inkeySpecifies the signing or decryption private key
-recipSpecifies a recipient certificate
-certfileSupplies additional certificates
-CAfileSupplies trusted CA certificates
-CApathSupplies a hashed CA directory
-binaryPrevents S/MIME text canonicalization
-nodetachCreates an attached/opaque signature
-contentSupplies detached content during verification
-nocertsOmits signer certificates from SignedData
-noverifySkips signer certificate validation
-mdSelects signing digest
-cadesCreates or verifies CAdES-BES attributes
-streamEnables streaming encoding
-certsoutWrites embedded certificates to a file
-printPrints the CMS structure
-nooutSuppresses encoded CMS output
-verify_retcodeReturns a nonzero exit status on verification failure
-providerLoads a provider
-provider-pathSpecifies provider module location
-propqueryApplies an OpenSSL property query

For the complete list, see the official OpenSSL 3.5 cms documentation.

Use -verify_retcode in automation

When building scripts, add:

-verify_retcode

to make verification failure produce a nonzero exit code.

For example:

if openssl cms \
  -verify \
  -verify_retcode \
  -inform DER \
  -in file.p7s \
  -content file.bin \
  -binary \
  -CAfile ca-chain.pem \
  -out /dev/null
then
    echo "CMS signature is valid"
else
    echo "CMS verification failed" >&2
    exit 1
fi

This is much safer than parsing human-readable output such as:

CMS Verification successful

Automation should make decisions based on the command’s exit status.

Frequently asked questions

What does openssl cms do?

openssl cms processes Cryptographic Message Syntax data. It can digitally sign, verify, encrypt, decrypt, inspect, compress, and perform several other CMS operations.

How do I sign a file with OpenSSL CMS?

Run:

openssl cms \
  -sign \
  -in file.txt \
  -signer sender.crt \
  -inkey sender.key \
  -binary \
  -nodetach \
  -outform DER \
  -out signed.der

How do I verify a CMS signature?

Run:

openssl cms \
  -verify \
  -inform DER \
  -in signed.der \
  -binary \
  -CAfile ca-chain.pem \
  -out verified.txt

How do I create a detached signature?

Omit:

-nodetach

and save the signature separately:

openssl cms \
  -sign \
  -in file.bin \
  -signer sender.crt \
  -inkey sender.key \
  -binary \
  -outform DER \
  -out file.p7s

How do I verify a detached CMS signature?

Supply the original data with:

-content

Example:

openssl cms \
  -verify \
  -inform DER \
  -in file.p7s \
  -content file.bin \
  -binary \
  -CAfile ca-chain.pem \
  -out /dev/null

How do I encrypt a file with a certificate?

Run:

openssl cms \
  -encrypt \
  -in file.bin \
  -binary \
  -aes-256-cbc \
  -outform DER \
  -out encrypted.der \
  recipient.crt

Only the recipient’s certificate is needed for encryption.

How do I decrypt a CMS encrypted file?

Use the recipient certificate and matching private key:

openssl cms \
  -decrypt \
  -inform DER \
  -in encrypted.der \
  -recip recipient.crt \
  -inkey recipient.key \
  -binary \
  -out decrypted.bin

Can I encrypt one CMS message for multiple recipients?

Yes.

Supply multiple recipient certificates:

openssl cms \
  -encrypt \
  -in file.bin \
  -binary \
  -aes-256-cbc \
  -out encrypted.cms \
  admin-one.crt \
  admin-two.crt

Either recipient can decrypt using their own private key.

Can a CMS file have multiple signatures?

Yes.

openssl cms -sign supports multiple signer certificates and keys, and -resign can add a signer to an existing SignedData object.

What is the difference between detached and attached signatures?

An attached signature contains the protected content inside the CMS object.

A detached signature contains only the signature information and requires the original file separately during verification.

Why should I use -binary?

Use -binary for arbitrary file content to prevent S/MIME line-ending canonicalization from changing the byte stream.

This is particularly important for:

ZIP files
archives
executables
configuration files
JSON
binary data

What encryption algorithm does openssl cms use by default?

OpenSSL 3.5 changed the default CMS encryption cipher from 3DES to AES-256.

For consistent scripts across OpenSSL versions, explicitly select the required cipher rather than relying on the default.

Does CMS encryption verify that the recipient certificate is trusted?

Do not assume that it does.

Validate the recipient certificate independently before encrypting sensitive information for it.

Is CMS the same as PKCS#7?

CMS evolved from PKCS#7 and remains highly compatible with it, but CMS is the newer and broader standard.

Is CMS the same as S/MIME?

No.

CMS defines cryptographic message structures. S/MIME uses CMS to sign and encrypt MIME email messages.

Does openssl cms work with OpenSSL 4.0?

Yes.

The main signing, verification, encryption, and decryption workflows remain available. The deprecated ENGINE option has been removed in OpenSSL 4.0.

Practical secure-transfer workflow

For the system-administration scenario in this article, the complete workflow is:

Sender: sign the file

openssl cms \
  -sign \
  -in system-export.json \
  -signer sender.crt \
  -inkey sender.key \
  -binary \
  -nodetach \
  -outform DER \
  -out signed.der

Sender: encrypt the signed object

openssl cms \
  -encrypt \
  -in signed.der \
  -binary \
  -aes-256-cbc \
  -outform DER \
  -out secure-transfer.der \
  recipient.crt

Transfer only:

secure-transfer.der

Recipient: decrypt

openssl cms \
  -decrypt \
  -inform DER \
  -in secure-transfer.der \
  -recip recipient.crt \
  -inkey recipient.key \
  -binary \
  -out recovered-signed.der

Recipient: verify

openssl cms \
  -verify \
  -verify_retcode \
  -inform DER \
  -in recovered-signed.der \
  -binary \
  -CAfile ca-chain.pem \
  -signer verified-signer.pem \
  -out system-export.json

Recipient: inspect the signer

openssl x509 \
  -in verified-signer.pem \
  -noout \
  -subject \
  -issuer \
  -fingerprint \
  -sha256

The recipient now has assurance of:

Confidentiality
     +
Integrity
     +
Signer authentication

assuming the certificate trust and key-management processes are correct.

Related OpenSSL guides

  • Browse the full OpenSSL Commands Guide.
  • Understand the relationship between CMS and PKCS#7 SignedData and certificate bundles.
  • Validate signer certificates with OpenSSL verify.
  • Inspect certificates with OpenSSL x509.
  • Examine the internal CMS encoding with OpenSSL asn1parse.
  • Manage certificates and private keys stored in PKCS#12 keystores.
  • Learn how to list OpenSSL commands and command options.

External references

  • OpenSSL 3.5 cms documentation
  • OpenSSL 3.0 cms documentation
  • OpenSSL 4.0 cms documentation
  • OpenSSL verification options
  • OpenSSL passphrase options
  • OpenSSL asn1parse documentation

Conclusion

The openssl cms command is much more useful than its historical association with encrypted email might suggest.

For system administrators, it can provide a complete file-protection workflow:

Original file
      ↓
Sign with sender private key
      ↓
CMS SignedData
      ↓
Encrypt for recipient certificate
      ↓
CMS EnvelopedData
      ↓
Secure transfer
      ↓
Recipient decrypts
      ↓
Recipient verifies signature
      ↓
Original trusted file

The four commands worth remembering are:

openssl cms -sign
openssl cms -verify
openssl cms -encrypt
openssl cms -decrypt

Use signing when you need:

integrity + authentication

Use encryption when you need:

confidentiality

Use both when a sensitive administrative file must be confidential and its recipient needs to verify who supplied it and whether it was altered.

For scripts that must work consistently across OpenSSL 3.0 through OpenSSL 4.0, explicitly define important behavior such as the content-encryption algorithm instead of depending on defaults. This is particularly important because OpenSSL 3.5 changed the default CMS encryption cipher from 3DES to AES-256.

Most importantly, remember that CMS does not eliminate the need for PKI validation.

Before trusting a signature or encrypting sensitive data for a recipient, verify the certificates involved and protect their private keys appropriately.

linux,  openssl

Reader Interactions

Comments

  1. Jaroslav Chabr says

    June 15, 2023 at 9:44 am

    Hello,

    Look here:
    https://cutt.us/CAdES
    There you have overview of CAdES variants.

    If I have binary data stored in a file named “binar”, a private key in a file named “key”, my certificate in a file named “cer”, a file
    containing complete certificates and revocation links named “CerRevLink”, and a file containing complete certificates and revocation
    data instead of just links named “CerRev”, how can I create a CAdES signature of the “binar” file to generate a “CAdES-X Long” signature?

    I try this:

    openssl cms -sign -binary -nodetach -cades -in binar -out PLAIN.txt -inkey key -signer cer
    Enter pass phrase for key:
    openssl cms -verify -binary -cades -in PLAIN.txt -CAfile CerRev -out Binar
    CAdES Verification successful

    sha512sum binar = sha512sum Binar

    What exactly of this (CAdES-BES CAdES-EPES CAdES-T CAdES-C CAdES-X Long CAdES-X type1 CAdES-X type2 CAdES-A CAdES-LT CAdES-L CAdES-B CAdES-XL) is in file PLAIN.txt ?

    I think, there is CAdES-BES in file PLAIN.txt. I am right?

    If so, how to create CAdES-X Long ?

    While we are at it, could you please also write me what commands I need to create each type of signature from the following list?

    CAdES-EPES
    CAdES-T
    CAdES-C
    CAdES-X Long
    CAdES-X type1
    CAdES-X type2
    CAdES-A
    CAdES-LT
    CAdES-L
    CAdES-B
    CAdES-XL

    Thank you.

    Could I do it using three separate commands?

    First CAdES-T
    openssl cms -cades -sign -in binar -signer cer -inkey key -out Tsignature.txt -nodetach -binary -md sha256 -outform DER -tsa
    -cades because we need to sign attributes ?
    -nodetach because we pipe the output to second command?
    -binary because of no problem with ^M as EOL.
    -md sha256 just possibility
    -outform DER because of binary data output
    -tsa for getting timestamp

    Second CAdES-C
    openssl cms -cades -sign -in Tsignature.txt -signer cer -inkey key -out Csignature.txt -nodetach -binary -md sha256 -outform DER -certfile CerRevLink -noattr -crl_url “http://example.com/revocation-list.crl” -ocsp_url “http://ocsp.example.com”
    -cades use this or not ?
    -in Tsignature.txt Tsignature.txt is the output of above command
    -signer cer or -signer CerRevLink only key is necessary to sign so what file to put here after -signer
    -nodetach because we pipe the output to third command?
    -certfile CerRevLink this contains Issuer certificate, Root certificate and Revocation-URL: http://example.com/revocation-list.crl
    Revocation-URL: http://ocsp.example.com
    Is this the same (-cades -noattr)=(without -cades and without -noattr)
    -crl_url “http://example.com/revocation-list.crl”
    -ocsp_url “http://ocsp.example.com” as an alternative if this is not in CerRevLink

    Third CAdES-X Long
    openssl cms -cades -sign -in Csignature.txt -signer cer -inkey key -out XLongsignature.txt -binary -md sha256 -outform PEM -certfile CerRev -noattr
    -cades use this or not ?
    -signer cer or -signer CerRev ?
    -nodetach is missing we want detached signature
    -outform PEM or MIME ?
    -certfile CerRev file CerRev contains Issuer certificate, Root certificate and —–BEGIN X509 CRL—–
    base64
    —–END X509 CRL—–
    -noattr see the question in second command above

    I have not experimented with this yet. Please tell me your opinion.

    Thank you

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

Popular Posts

PKCS12

openssl s_client

Keytool

Keytool list

ECDSA vs RSA

OpenSSL

PKCS7

Certificate Decoder

PKI for DevOps Engineers – Free Training Series

PKI for DevOps Training Hub

Lesson 1 – How TLS and PKI Work

Lesson 2 – Understanding X.509 Certificates

Lesson 3 – Certificate Chains Explained

Lesson 4 – Debug TLS with OpenSSL

Lesson 5 – Verify Certificate Chains

Lesson 6 – Creating CSRs with OpenSSL

Lesson 7 – Working with PKCS12 Certificates

Lesson 8 – Java Keystores and keytool

Lesson 9 – Certificate Expiration Monitoring

Lesson 10 – Automating Certificate Renewal

Lesson 11 – Common TLS Errors

Lesson 12 – PKI Architecture for DevOps

Recent Posts

  • Apache mod_auth_cas: Protect Web Applications with Apereo CAS and Docker
  • Apache mod_auth_mellon: Configure SAML SSO & Docker
  • Configure OpenID Connect (OIDC) in Apereo CAS 7.3 and 8
  • Certificate Ownership Is an Operating Model Problem, Not a Tooling Problem
  • OpenSSL dgst: Create Checksums and Verify Digital Signatures

Footer

  • Twitter
  • YouTube
Mister PKI

Practical security engineering for the systems that establish trust.

Learn OpenSSL Keytool Free training
Work together PKI consulting CAS consulting Sponsored contributions
Site About Support Mister PKI Cookie policy Search
© 2026 Mister PKI Secure systems. Clear guidance.