Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
keyType = 'dss';
if (keyType === 'ec' && semver.lt(process.version, '5.2.0')) {
return new Error(
'EC private keys are not supported in this version of node'
);
}
if (!RE_HEADER_OPENSSH.test(data[1])) {
// unencrypted, no headers
var privData = new Buffer(data.slice(1, -1).join(''), 'base64');
if (keyType !== 'ec') {
ret.fulltype = 'ssh-' + keyType;
} else {
// ECDSA
var asnReader = new Ber.Reader(privData);
asnReader.readSequence();
asnReader.readInt();
asnReader.readString(Ber.OctetString, true);
asnReader.readByte(); // Skip "complex" context type byte
var offset = asnReader.readLength(); // Skip context length
if (offset !== null) {
asnReader._offset = offset;
switch (asnReader.readOID()) {
case '1.2.840.10045.3.1.7':
// prime256v1/secp256r1
ret.fulltype = 'ecdsa-sha2-nistp256';
break;
case '1.3.132.0.34':
// secp384r1
ret.fulltype = 'ecdsa-sha2-nistp384';
break;
function DSASigBERToBare(signature) {
if (signature.length <= 40)
return signature;
// This is a quick and dirty way to get from BER encoded r and s that
// OpenSSL gives us, to just the bare values back to back (40 bytes
// total) like OpenSSH (and possibly others) are expecting
var asnReader = new Ber.Reader(signature);
asnReader.readSequence();
var r = asnReader.readString(Ber.Integer, true);
var s = asnReader.readString(Ber.Integer, true);
var rOffset = 0;
var sOffset = 0;
if (r.length < 20) {
var rNew = Buffer.allocUnsafe(20);
r.copy(rNew, 1);
r = rNew;
r[0] = 0;
}
if (s.length < 20) {
var sNew = Buffer.allocUnsafe(20);
s.copy(sNew, 1);
s = sNew;
s[0] = 0;
var type;
var privPEM;
var pubPEM;
var pubSSH;
var algo;
var reader;
var errMsg = 'Malformed OpenSSH private key';
if (decrypted)
errMsg += '. Bad passphrase?';
switch (m[1]) {
case 'RSA':
type = 'ssh-rsa';
privPEM = makePEM('RSA PRIVATE', privBlob);
try {
reader = new Ber.Reader(privBlob);
reader.readSequence();
reader.readInt(); // skip version
var n = reader.readString(Ber.Integer, true);
if (n === null)
return new Error(errMsg);
var e = reader.readString(Ber.Integer, true);
if (e === null)
return new Error(errMsg);
pubPEM = genOpenSSLRSAPub(n, e);
pubSSH = genOpenSSHRSAPub(n, e);
} catch (ex) {
return new Error(errMsg);
}
algo = 'sha1';
break;
case 'DSA':
// DSA
var p;
var q;
var g;
var y;
// ECDSA
var d;
var Q;
var ecCurveOID;
var ecCurveName;
if (keyInfo.private) {
// parsing private key in ASN.1 format in order to generate a public key
var privKey = keyInfo.private;
var asnReader = new Ber.Reader(privKey);
var errMsg;
if (asnReader.readSequence() === null) {
errMsg = 'Malformed private key (expected sequence)';
if (keyInfo._decrypted)
errMsg += '. Bad passphrase?';
throw new Error(errMsg);
}
// version (ignored)
if (asnReader.readInt() === null) {
errMsg = 'Malformed private key (expected version)';
if (keyInfo._decrypted)
errMsg += '. Bad passphrase?';
throw new Error(errMsg);
}
}
} else if (Buffer.isBuffer(data)) {
buffer = data;
} else {
throw Error('Unsupported key format');
}
var reader = new ber.Reader(buffer);
reader.readSequence();
var header = new ber.Reader(reader.readString(0x30, true));
if (header.readOID(0x06, true) !== PUBLIC_RSA_OID) {
throw Error('Invalid Public key format');
}
var body = new ber.Reader(reader.readString(0x03, true));
body.readByte();
body.readSequence();
key.setPublic(
body.readString(0x02, true), // modulus
body.readString(0x02, true) // publicExponent
);
},
function ECDSASigASN1ToSSH(signature) {
if (signature[0] === 0x00)
return signature;
// Convert SSH signature parameters to ASN.1 BER values for OpenSSL
var asnReader = new Ber.Reader(signature);
asnReader.readSequence();
var r = asnReader.readString(Ber.Integer, true);
var s = asnReader.readString(Ber.Integer, true);
if (r === null || s === null)
return false;
var newSig = Buffer.allocUnsafe(4 + r.length + 4 + s.length);
writeUInt32BE(newSig, r.length, 0);
r.copy(newSig, 4);
writeUInt32BE(newSig, s.length, 4 + r.length);
s.copy(newSig, 4 + 4 + r.length);
return newSig;
}
return new Error(errMsg);
var y = reader.readString(Ber.Integer, true);
if (y === null)
return new Error(errMsg);
pubPEM = genOpenSSLDSAPub(p, q, g, y);
pubSSH = genOpenSSHDSAPub(p, q, g, y);
} catch (ex) {
return new Error(errMsg);
}
algo = 'sha1';
break;
case 'EC':
var ecSSLName;
var ecPriv;
try {
reader = new Ber.Reader(privBlob);
reader.readSequence();
reader.readInt(); // skip version
ecPriv = reader.readString(Ber.OctetString, true);
reader.readByte(); // Skip "complex" context type byte
var offset = reader.readLength(); // Skip context length
if (offset !== null) {
reader._offset = offset;
var oid = reader.readOID();
if (oid === null)
return new Error(errMsg);
switch (oid) {
case '1.2.840.10045.3.1.7':
// prime256v1/secp256r1
ecSSLName = 'prime256v1';
type = 'ecdsa-sha2-nistp256';
algo = 'sha256';
} else if (Buffer.isBuffer(data)) {
buffer = data;
} else {
throw Error('Unsupported key format');
}
var reader = new ber.Reader(buffer);
reader.readSequence();
reader.readInt(0);
var header = new ber.Reader(reader.readString(0x30, true));
if (header.readOID(0x06, true) !== PUBLIC_RSA_OID) {
throw Error('Invalid Public key format');
}
var body = new ber.Reader(reader.readString(0x04, true));
body.readSequence();
body.readString(2, true); // just zero
key.setPrivate(
body.readString(2, true), // modulus
body.readString(2, true), // publicExponent
body.readString(2, true), // privateExponent
body.readString(2, true), // prime1
body.readString(2, true), // prime2
body.readString(2, true), // exponent1 -- d mod (p1)
body.readString(2, true), // exponent2 -- d mod (q-1)
body.readString(2, true) // coefficient -- (inverse of q) mod p
);
},
if (Buffer.isBuffer(data)) {
data = data.toString('utf8');
}
if (_.isString(data)) {
var pem = utils.trimSurroundingText(data, PUBLIC_OPENING_BOUNDARY, PUBLIC_CLOSING_BOUNDARY)
.replace(/\s+|\n\r|\n|\r$/gm, '');
buffer = Buffer.from(pem, 'base64');
}
} else if (Buffer.isBuffer(data)) {
buffer = data;
} else {
throw Error('Unsupported key format');
}
var reader = new ber.Reader(buffer);
reader.readSequence();
var header = new ber.Reader(reader.readString(0x30, true));
if (header.readOID(0x06, true) !== PUBLIC_RSA_OID) {
throw Error('Invalid Public key format');
}
var body = new ber.Reader(reader.readString(0x03, true));
body.readByte();
body.readSequence();
key.setPublic(
body.readString(0x02, true), // modulus
body.readString(0x02, true) // publicExponent
);
},
if (_.isString(data)) {
var pem = utils.trimSurroundingText(data, PRIVATE_OPENING_BOUNDARY, PRIVATE_CLOSING_BOUNDARY)
.replace('-----END PRIVATE KEY-----', '')
.replace(/\s+|\n\r|\n|\r$/gm, '');
buffer = Buffer.from(pem, 'base64');
} else {
throw Error('Unsupported key format');
}
} else if (Buffer.isBuffer(data)) {
buffer = data;
} else {
throw Error('Unsupported key format');
}
var reader = new ber.Reader(buffer);
reader.readSequence();
reader.readInt(0);
var header = new ber.Reader(reader.readString(0x30, true));
if (header.readOID(0x06, true) !== PUBLIC_RSA_OID) {
throw Error('Invalid Public key format');
}
var body = new ber.Reader(reader.readString(0x04, true));
body.readSequence();
body.readString(2, true); // just zero
key.setPrivate(
body.readString(2, true), // modulus
body.readString(2, true), // publicExponent
body.readString(2, true), // privateExponent
body.readString(2, true), // prime1