ecdsa.com
Go encoding/asn1Go

Error message

asn1: structure error: tags don't match

The full message appends the expected and found tags plus field context, e.g. "tags don't match (16 vs {class:0 tag:2 ...}) ... AlgorithmIdentifier @2". Truncated inputs produce the sibling "asn1: syntax error: data truncated".

Go encoding/asn1 — surfaced through crypto/x509 parsers

What it means

Go's DER decoder found a different ASN.1 tag than the target structure requires — you are parsing valid-ish DER into the wrong Go structure, or parsing something that is not DER at all. The numbers in the message are ASN.1 universal tags (16 = SEQUENCE, 2 = INTEGER, 19 = PrintableString), which usually identify the mixup precisely.

Why it happens

How to fix it

  1. 1.

    Always pem.Decode first

    Extract the DER block (and check the label) before calling any parser.

    go
    block, _ := pem.Decode(pemBytes)
    if block == nil {
        return fmt.Errorf("input is not PEM")
    }
    cert, err := x509.ParseCertificate(block.Bytes)   // block.Type == "CERTIFICATE"
  2. 2.

    Try the container parsers in order

    When the private-key container is unknown, attempt PKCS#8, then SEC1, then PKCS#1 — the standard triage for keys from mixed sources.

    go
    if k, err := x509.ParsePKCS8PrivateKey(der); err == nil { return k, nil }
    if k, err := x509.ParseECPrivateKey(der); err == nil { return k, nil }
    if k, err := x509.ParsePKCS1PrivateKey(der); err == nil { return k, nil }
    return nil, errors.New("unrecognized private key format")
  3. 3.

    Look at the first bytes

    One hexdump line tells you whether you even hold DER, and openssl asn1parse shows the structure to match against the parser you chose.

    bash
    head -c 16 key.der | xxd          # 30 81/82 ... → SEQUENCE, plausibly DER
    openssl asn1parse -inform der -in key.der | head -3

Related errors

← Browse the full signature error database