diff --git a/lib/formats/putty.js b/lib/formats/putty.js index 9d17c0f..89ed0b6 100644 --- a/lib/formats/putty.js +++ b/lib/formats/putty.js @@ -64,7 +64,7 @@ function read(buf, options) { assert.equal(parts[0].toLowerCase(), 'private-lines'); var privateLines = parseInt(parts[1], 10); if (!isFinite(privateLines) || privateLines < 0 || - privateLines > lines.length) { + privateLines > lines.length) { throw (new Error('Invalid private-lines count')); } @@ -77,9 +77,7 @@ function read(buf, options) { options.filename, 'PEM')); } - var iv = Buffer.from(Array - .from({length: 16}) - .map(function () { return 0; })); + var iv = Buffer.alloc(16, 0); var decipher = crypto.createDecipheriv( 'aes-256-cbc', derivePPK2EncryptionKey(options.passphrase), @@ -95,20 +93,35 @@ function read(buf, options) { } var sshbuf = new SSHBuffer({buffer: privateBuf}); + var privateKeyParts; if (alg === 'ssh-dss') { - key.addPart('x', sshbuf.readPart()); + privateKeyParts = [ { + name: 'x', + data: sshbuf.readBuffer() + }]; } else if (alg === 'ssh-rsa') { - key.addPart('d', sshbuf.readPart()); - key.addPart('p', sshbuf.readPart()); - key.addPart('q', sshbuf.readPart()); - key.addPart('iqmp', sshbuf.readPart()); + privateKeyParts = [ + { name: 'd', data: sshbuf.readBuffer() }, + { name: 'p', data: sshbuf.readBuffer() }, + { name: 'q', data: sshbuf.readBuffer() }, + { name: 'iqmp', data: sshbuf.readBuffer() } + ]; } else if (alg.startsWith('ecdsa-sha2-nistp')) { - key.addPart('d', sshbuf.readPart()); + privateKeyParts = [ { + name: 'd', data: sshbuf.readBuffer() + } ]; } else if (alg === 'ssh-ed25519') { - key.addPart('k', sshbuf.readPart()); + privateKeyParts = [ { + name: 'k', data: sshbuf.readBuffer() + } ]; } else { throw new Error('Unsupported PPK key type: ' + alg); } + + key = new PrivateKey({ + type: key.type, + parts: key.parts.concat(privateKeyParts) + }); } key.comment = comment; diff --git a/lib/key.js b/lib/key.js index e6e892f..706f834 100644 --- a/lib/key.js +++ b/lib/key.js @@ -80,12 +80,6 @@ function Key(opts) { Key.formats = formats; -Key.prototype.addPart = function (name, options) { - var part = Object.assign({ name: name }, options); - this.parts.push(part); - this.part[part.name] = part; -}; - Key.prototype.toBuffer = function (format, options) { if (format === undefined) format = 'ssh';