Skip to content

Commit 7205f69

Browse files
author
Alex C
committed
keygen: fix over-stripping of ED25519 public key bytes
parseDERs() removed *all* leading 0x00 bytes from the ED25519 SubjectPublicKeyInfo BIT STRING content instead of exactly the one mandatory 'unused bits' marker byte. Whenever the raw 32-byte public key itself happened to start with 0x00 (~1/256 keys), the loop over-stripped real key material, producing a truncated key that parseKey() then correctly rejected as 'Malformed OpenSSH public key'. Fixes #1514.
1 parent a3a39d7 commit 7205f69

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

lib/keygen.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,11 +289,13 @@ function parseDERs(keyType, pub, priv) {
289289

290290
let pubBin = reader.readString(Ber.BitString, true);
291291
{
292-
// Remove leading zero bytes
293-
let i = 0;
294-
for (; i < pubBin.length && pubBin[i] === 0x00; ++i);
295-
if (i > 0)
296-
pubBin = pubBin.slice(i);
292+
// The BIT STRING content is exactly one "unused bits" count byte
293+
// (always 0x00 here, since an ED25519 key is byte-aligned) followed
294+
// by the raw 32-byte public key. Strip exactly that one byte --
295+
// NOT a variable-length run of zero bytes (the raw key itself can
296+
// legitimately start with 0x00).
297+
if (pubBin.length > 0 && pubBin[0] === 0x00)
298+
pubBin = pubBin.slice(1);
297299
}
298300

299301
// Parse private key

0 commit comments

Comments
 (0)