{"record":{"id":"ac8f62de4abc5687","repo":"ipfs/kubo","slug":"invalid-ec-private-key-w","errorCode":null,"errorMessage":"invalid EC private key: %w","messagePattern":"invalid EC private key: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/commands/keystore.go","lineNumber":1068,"sourceCode":"\t}\n\tif !wrapper.Algo.Algorithm.Equal(oidPublicKeyECDSA) {\n\t\treturn false\n\t}\n\tvar curve asn1.ObjectIdentifier\n\tif _, err := asn1.Unmarshal(wrapper.Algo.Parameters.FullBytes, &curve); err != nil {\n\t\treturn false\n\t}\n\treturn curve.Equal(oidNamedCurveSecp256k1)\n}\n\nfunc parseSecp256k1PrivateKey(der []byte) (*secp256k1.PrivateKey, error) {\n\tvar wrapper pkcs8Key\n\tif _, err := asn1.Unmarshal(der, &wrapper); err != nil {\n\t\treturn nil, err\n\t}\n\tvar ec ecPrivateKey\n\tif _, err := asn1.Unmarshal(wrapper.PrivateKey, &ec); err != nil {\n\t\treturn nil, fmt.Errorf(\"invalid EC private key: %w\", err)\n\t}\n\tif ec.Version != 1 {\n\t\treturn nil, fmt.Errorf(\"unsupported EC private key version %d\", ec.Version)\n\t}\n\tif len(ec.PrivateKey) > 32 {\n\t\treturn nil, errors.New(\"invalid EC private key length\")\n\t}\n\tvar buf [32]byte\n\tcopy(buf[32-len(ec.PrivateKey):], ec.PrivateKey)\n\tvar scalar secp256k1.ModNScalar\n\tif overflow := scalar.SetBytes(&buf); overflow != 0 || scalar.IsZero() {\n\t\treturn nil, errors.New(\"EC private key not in the valid range for secp256k1\")\n\t}\n\treturn secp256k1.NewPrivateKey(&scalar), nil\n}\n","sourceCodeStart":1050,"sourceCodeEnd":1084,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/core/commands/keystore.go#L1050-L1084","documentation":"parseSecp256k1PrivateKey parses a DER/PKCS#8-encoded secp256k1 private key. After unwrapping the PKCS#8 structure, it ASN.1-decodes the inner EC private key structure; if that decode fails (malformed DER, wrong structure), the error is wrapped as \"invalid EC private key: %w\". This is part of local key import/export parsing for keystore keys.","triggerScenarios":"Importing a key file (`ipfs key import`) whose bytes are not valid ASN.1 EC private key structure; truncated or corrupted PEM/DER file; passing an Ed25519 or RSA PKCS#8 blob where an EC secp256k1 body is expected.","commonSituations":"Hand-converted key files (openssl output in wrong format, e.g. `openssl ec` vs secp256k1 curve mismatch); exporting from another tool with different encoding; corrupted backup file.","solutions":["Re-export the key correctly: `openssl ecparam -name secp256k1 -genkey` then convert to PKCS#8 with `openssl pkcs8 -topk8 -nocrypt`","Check the input file is not truncated (`openssl asn1parse -in key.pem`)","Ensure the curve is secp256k1, not prime256v1 or another EC curve","If importing into ipfs, try `ipfs key import <name> key.pem` with the PEM-wrapped PKCS#8 form"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// validate key before ipfs key import\nif openssl asn1parse -in key.pem > /dev/null 2>&1; then\n  echo \"DER structure OK\"\nelse\n  echo \"not valid ASN.1/DER\"\nfi","typeGuard":null,"tryCatchPattern":"_, err := parsePKCS8PrivateKey(der)\nif err != nil {\n    var perr error\n    if _, err2 := parseSecp256k1PrivateKey(der); err2 != nil {\n        // input is not a parseable secp256k1 PKCS#8 key: re-export with openssl\n    }\n    _ = perr\n}","preventionTips":["Export keys as PKCS#8 PEM with openssl pkcs8 -topk8 -nocrypt","Confirm curve is secp256k1, not prime256v1","Test-parse with openssl asn1parse before importing"],"tags":["crypto","asn1","secp256k1","key-import"],"backgroundTag":"invalid-private-key-format","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}