ecdsa.com

Verify a signature · OpenSSL

How to verify an ECDSA signature with OpenSSL

One command verifies an ECDSA signature with the OpenSSL CLI — provided the inputs are what dgst expects: an SPKI public key PEM, the original message file, and the signature in ASN.1 DER.

Tested with OpenSSL 3.6.

Same recipe in:Node.jsPythonGoOpenSSLWebCrypto
verify a DER signature against a public key
openssl dgst -sha256 -verify pub.pem -signature sig.der message.txt
# "Verified OK" (exit 0) or "Verification failure" (exit 1)
only have a private key or a certificate? extract the public key
# derive the public key from a private key
openssl pkey -in key.pem -pubout -out pub.pem
 
# or extract it from a certificate
openssl x509 -in cert.pem -pubkey -noout > pub.pem
inspect a signature that refuses to verify
openssl asn1parse -inform DER -in sig.der
# a well-formed ECDSA signature shows: SEQUENCE, INTEGER (r), INTEGER (s)

How it works

  1. -sha256 must repeat the hash the signer used — OpenSSL cannot infer it from the signature bytes.
  2. -verify takes the public key; the similarly named -prverify verifies using a private key by deriving its public half.
  3. The exit code mirrors the verdict (0 verified, 1 failure), which makes the command easy to use in scripts and CI.

Gotchas

  • dgst consumes only DER signatures. A 64-byte raw r‖s signature (WebCrypto, JWS) fails with asn1 encoding routines errors — convert it to DER first; the browser converter does this without any code.
  • openssl dgst -sha256 message.txt alone just prints a hash — verification requires both -verify and -signature.
  • A wrong key file fails identically to a corrupted signature. When in doubt, re-derive the public key from the signer's private key and diff the two PEMs.

Related recipes