ecdsa.com
Node.js cryptoNode.js crypto

Error message

Failed to read asymmetric key

Node.js crypto — e.g. code ERR_OSSL_ASN1_WRONG_TAG

What it means

createPublicKey()/createPrivateKey() got DER (or mis-labeled PEM) whose ASN.1 structure does not match the type you declared — for example type: "spki" while the bytes are actually a SEC1 EC private key. OpenSSL parsed far enough to know the tags are wrong, then stopped. The accompanying code (ERR_OSSL_ASN1_WRONG_TAG and relatives) names the ASN.1-level mismatch. Unlike the DECODER-routines "unsupported" error, which means nothing parseable was found at all, this one means the bytes were recognizably the wrong structure for the declared type.

Why it happens

How to fix it

  1. 1.

    Identify the structure, then declare it truthfully

    asn1parse prints the outer structure. An SPKI starts with a SEQUENCE containing an AlgorithmIdentifier; PKCS#8 starts with a version INTEGER 0; SEC1 EC keys with INTEGER 1.

    bash
    openssl asn1parse -inform der -in key.der | head -5
  2. 2.

    Match Node options to the container

    Once you know the container, the mapping is mechanical.

    js
    // SPKI public key
    createPublicKey({ key: der, format: "der", type: "spki" });
    // PKCS#8 private key
    createPrivateKey({ key: der, format: "der", type: "pkcs8" });
    // SEC1 (BEGIN EC PRIVATE KEY) private key
    createPrivateKey({ key: der, format: "der", type: "sec1" });
  3. 3.

    Convert legacy containers once, at the edge

    Standardize on PKCS#8/SPKI in storage so application code never guesses.

    bash
    openssl pkey -in legacy-ec.pem -out key-pkcs8.pem            # any private → PKCS#8
    openssl pkey -in key-pkcs8.pem -pubout -out pub-spki.pem      # derive SPKI public

Related errors

← Browse the full signature error database