digitalbazaar / forge

A native implementation of TLS in Javascript and tools to write crypto-based and network-heavy webapps
https://digitalbazaar.com/
Other
5.03k stars 777 forks source link

Cannot convert ed25519 keys to OpenSSH format #795

Open JeremiFerre opened 4 years ago

JeremiFerre commented 4 years ago

I try to generate ssh keys with node-forge, using ed25519 key type. I was previously using rsa keys and I was used to generate the key pairs like this:

const generateKeyPairAsync = promisify(pki.rsa.generateKeyPair);

const pair = await generateKeyPairAsync({ bits: 2048 });
const privPem = pki.privateKeyToPem(pair.privateKey);
const pubSSH = ssh.publicKeyToOpenSSH(pair.publicKey);

But when I'm trying to use new curve25519 like that:

const pair = pki.ed25519.generateKeyPair();
const privPem = pki.privateKeyToPem(pair.privateKey);
const pubSSH = ssh.publicKeyToOpenSSH(pair.publicKey);

I get these errors when converting generated key pair to Pem or to OpenSSH:

error for privateKeyToPem

TypeError: Cannot read property 'toString' of undefined
    at _bnToBytes (/Users/jeremiferre/Documents/Projects/Submarine-analytics/submarine-backend/node_modules/node-forge/lib/rsa.js:1737:15)
    at Object.pki.privateKeyToAsn1.pki.privateKeyToRSAPrivateKey (/Users/jeremiferre/Documents/Projects/Submarine-analytics/submarine-backend/node_modules/node-forge/lib/rsa.js:1343:7)
    at Object.pki.privateKeyToPem (/Users/jeremiferre/Documents/Projects/Submarine-analytics/submarine-backend/node_modules/node-forge/lib/pki.js:82:26)
    at SshService.createNewKeyPair (/Users/jeremiferre/Documents/Projects/Submarine-analytics/submarine-backend/dist/structure/company/ssh/ssh.service.js:14:42)

error for publicKeyToOpenSSH

TypeError: Cannot read property 'toString' of undefined
    at _addBigIntegerToBuffer (/Users/jeremiferre/Documents/Projects/Submarine-analytics/submarine-backend/node_modules/node-forge/lib/ssh.js:203:20)
    at Object.ssh.publicKeyToOpenSSH (/Users/jeremiferre/Documents/Projects/Submarine-analytics/submarine-backend/node_modules/node-forge/lib/ssh.js:133:3)
    at SshService.createNewKeyPair (/Users/jeremiferre/Documents/Projects/Submarine-analytics/submarine-backend/dist/structure/company/ssh/ssh.service.js:14:41)

Environment

Node-forge version: 0.9.1

- Node version: 12.14.1
- Platform: Mac
szaboge commented 2 years ago

I have the same problem, do you have any solution for that?

ROTGP commented 2 years ago

This is blocking me too - any progress?

soichih commented 2 years ago

Sadly, the current implementation of forge.ssh.publicKeyToOpenSSH (and privateKeyToOpenSSH) only works with RSA key.

dmitrizagidulin commented 2 years ago

@ROTGP - try https://github.com/EternalDeiwos/keyto, that might work for your use case.