{"record":{"id":"f5e227d21e0953b1","repo":"gravitational/teleport","slug":"public-key-representation-has-unexpected-length","errorCode":null,"errorMessage":"public key representation has unexpected length (%v bytes)","messagePattern":"public key representation has unexpected length \\((.+?) bytes\\)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"lib/darwin/pub_key.go","lineNumber":39,"sourceCode":"import (\n\t\"crypto/ecdsa\"\n\t\"crypto/elliptic\"\n\t\"fmt\"\n\t\"math/big\"\n)\n\n// ECDSAPublicKeyFromRaw reads an ECDSA public key from a raw Apple public key,\n// as returned by SecKeyCopyExternalRepresentation.\nfunc ECDSAPublicKeyFromRaw(pubKeyRaw []byte) (*ecdsa.PublicKey, error) {\n\t// Verify key length to avoid a potential panic below.\n\t// 3 is the smallest number that clears it, but in practice 65 is the more\n\t// common length.\n\t// Apple's docs make no guarantees, hence no assumptions are made here.\n\tswitch l := len(pubKeyRaw); {\n\tcase l < 3:\n\t\treturn nil, fmt.Errorf(\"public key representation too small (%v bytes)\", l)\n\tcase l%2 != 1: // 0x4+keyLen+keyLen is always odd, see explanation below.\n\t\treturn nil, fmt.Errorf(\"public key representation has unexpected length (%v bytes)\", l)\n\tcase pubKeyRaw[0] != 0x04: // See explanation below.\n\t\treturn nil, fmt.Errorf(\"public key representation starts with unexpected byte (%#x vs 0x4)\", pubKeyRaw[0])\n\t}\n\n\t// \"For an elliptic curve public key, the format follows the ANSI X9.63\n\t// standard using a byte string of 04 || X || Y. (...) All of these\n\t// representations use constant size integers, including leading zeros as\n\t// needed.\"\n\t// https://developer.apple.com/documentation/security/1643698-seckeycopyexternalrepresentation?language=objc\n\tpubKeyRaw = pubKeyRaw[1:] // skip 0x4\n\tl := len(pubKeyRaw) / 2\n\tx := pubKeyRaw[:l]\n\ty := pubKeyRaw[l:]\n\n\treturn &ecdsa.PublicKey{\n\t\tCurve: elliptic.P256(),\n\t\tX:     (&big.Int{}).SetBytes(x),\n\t\tY:     (&big.Int{}).SetBytes(y),","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/gravitational/teleport/blob/1283425b60ec5f60d509ba4c791183d452923ff7/lib/darwin/pub_key.go#L21-L57","documentation":"ECDSAPublicKeyFromRaw validates the raw Apple public key bytes: the length is not a multiple of 2 plus 1 (the expected '0x04 || X || Y' uncompressed-point layout), so the representation does not match an uncompressed ECDSA point.","triggerScenarios":"Thrown at lib/darwin/pub_key.go:39 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Verify the credential is an ECDSA public key obtained via SecKeyCopyExternalRepresentation","Re-register the Touch ID/Apple credential to obtain a well-formed raw representation"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"1283425b60ec5f60d509ba4c791183d452923ff7","analyzedAt":"2026-09-02T04:06:41.601Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}