mscdex / mscdex/ssh2

Generating ed25519 key pair can produce key pairs not readable by parseKey

Open
#1,390 0 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
5.8k
Forks
734
PR merge metrics
No merged PRs in 30d

Description

Hi. First off, thanks for your work on this library.

I'm seeing an issue where generateKeyPairSync can produce key pairs where either the private or public key cannot be successfully parsed by parseKey. This program reproduces the issue:

const utils = require('ssh2').utils;
while (true) {
  const keyPair = utils.generateKeyPairSync('ed25519');
  const parsedPublicKey = utils.parseKey(keyPair.public)
  if (parsedPublicKey instanceof Error) {
    throw parsedPublicKey;
  }
  const parsedPrivateKey = utils.parseKey(keyPair.private)
  if (parsedPrivateKey instanceof Error) {
    throw parsedPrivateKey;
  }
}

Commenting out the block that throws parsedPublicKey may be needed to quickly see cases where parsing the private key also generates an error.

I am not sure whether the issue is exactly in parsing the key or generating it, but I did notice that generateKeyPair will strip leading zero bytes from the public key, if the public key from the Node.js standard library starts with a zero:

https://github.com/mscdex/ssh2/blob/a56e70e68efd6a1c7e665c9a473eb68d25571a55/lib/keygen.js#L290-L297

My brief read of https://datatracker.ietf.org/doc/html/rfc8709#section-4 is that this can be incorrect if it strips the public key length to less than 32 bytes, but my understanding of the specification may be incorrect.

I did notice that feeding the generated keys with the stripped public keys to https://github.com/TritonDataCenter/node-sshpk parses successfully; I am not sure if that is an implementation detail of that library.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/keygen.js around lines 290-297 and trace the utils.generateKeyPairSync and utils.parseKey entry points. Run the supplied reproduction repeatedly, compare the generated public-key lengths with RFC 8709 section 4, and verify that both generated keys remain parseable.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
cryptography
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.