Diagnose OpenSSL “ASN1_CHECK_TLEN: wrong tag”

Check the object type and encoding before treating an ASN.1 parsing error as a damaged certificate.

Best practices
Illustration of encrypted network connections

An ASN.1 wrong tag error means the parser encountered a type it did not expect. It does not identify the cause on its own. A certificate parser receiving a private key, a CSR, or a different container format can fail even when the input file is intact.

Identify the file before changing it

Keep the original file. Inspect its type and PEM block labels without printing a private key:

file server.crt
grep '^-----BEGIN ' server.crt

A PEM certificate has BEGIN CERTIFICATE and END CERTIFICATE boundaries around Base64 data. Private keys and certificate signing requests have different labels. PKCS#7 and PKCS#12 containers also need the appropriate reader. File extensions are only conventions, and file output such as data does not conclusively establish DER encoding.

Do not repair Base64 by guesswork. A truncated download, copied page markup, or incorrect concatenation should be corrected at the source.

Use the matching OpenSSL command

# Read a PEM certificate.
openssl x509 -in server.crt -text -noout
# Check a private key without printing its private components.
openssl pkey -in server.key -check -noout
# Read a PEM certificate signing request.
openssl req -in server.csr -text -noout

If the certificate is known to be DER-encoded, specify that explicitly:

openssl x509 -inform DER -in server.crt -out server.pem

Successful parsing proves that the selected reader can decode the object. It does not prove that a certificate is trusted, valid for the intended hostname, or paired with the deployed private key.

Check the web server's file assignments

For Apache 2.4.8 and later, the certificate file can contain the leaf certificate followed by intermediate certificates:

SSLCertificateFile /etc/ssl/certs/fullchain.pem
SSLCertificateKeyFile /etc/ssl/private/server.key

SSLCertificateChainFile is obsolete for these versions. Do not point SSLCertificateFile at a private key or a bundle containing only unrelated CA certificates.

Ordinary blank lines and CRLF line endings are not, by themselves, evidence of malformed PEM. Check block boundaries and bytes copied from rich-text sources, but avoid indiscriminately rewriting a valid file.

Validate the deployment separately

Run apachectl configtest after checking the files. If it succeeds, reload the correct service for the distribution and inspect its logs. Ensure the startup identity can read the key and traverse its parent directories; restrictive key permissions such as root-owned mode 600 suit many privileged-startup deployments, but service identity and security policy determine the actual arrangement.

If the original certificate cannot be recovered, obtain a fresh copy from the issuer or reissue it as appropriate. Reissuing a certificate alone does not resolve a lost or compromised private key: use the appropriate key replacement and revocation process.

References: PEM textual encodings, OpenSSL pkey, and Apache mod_ssl.

Related reading

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting