Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function genOpenSSLEdPriv(priv) {
var asnWriter = new Ber.Writer();
asnWriter.startSequence();
// version
asnWriter.writeInt(0x00, Ber.Integer);
// algorithm
asnWriter.startSequence();
asnWriter.writeOID('1.3.101.112'); // id-Ed25519
asnWriter.endSequence();
// PrivateKey
asnWriter.startSequence(Ber.OctetString);
asnWriter.writeBuffer(priv, Ber.OctetString);
asnWriter.endSequence();
asnWriter.endSequence();
return makePEM('PRIVATE', asnWriter.buffer);
}
bp += 4;
if (info.pubkey[0] & 0x80)
exchangeBuf[bp++] = 0;
info.pubkey.copy(exchangeBuf, bp); // f
bp += len_spubkey - (info.pubkey[0] & 0x80 ? 1 : 0);
exchangeBuf.writeUInt32BE(len_secret, bp, true);
bp += 4;
if (info.secret[0] & 0x80)
exchangeBuf[bp++] = 0;
info.secret.copy(exchangeBuf, bp); // K
outstate.exchangeHash = new Buffer(hash.update(exchangeBuf)
.digest('binary'), 'binary'); // H
var asnWriter = new Ber.Writer(),
rawsig = readString(info.sig, info.sig._pos, self, callback); // s
if (rawsig === false)
return false;
var algo = (info.sig_format === 'ssh-rsa' ? 'RSA' : 'DSA'),
verifier = crypto.createVerify(algo + '-SHA1');
verifier.update(outstate.exchangeHash, 'binary');
// change bare host key parameters to ASN.1 DER values for OpenSSL
asnWriter.startSequence();
if (algo === 'RSA') {
var e = readString(info.hostkey, info.hostkey._pos, self, callback);
if (e === false)
return false;
var n = readString(info.hostkey, info.hostkey._pos, self, callback);
if (n === false)
return false;
function genOpenSSLEdPub(pub) {
var asnWriter = new Ber.Writer();
asnWriter.startSequence();
// algorithm
asnWriter.startSequence();
asnWriter.writeOID('1.3.101.112'); // id-Ed25519
asnWriter.endSequence();
// PublicKey
asnWriter.startSequence(Ber.BitString);
asnWriter.writeByte(0x00);
// XXX: hack to write a raw buffer without a tag -- yuck
asnWriter._ensure(pub.length);
pub.copy(asnWriter._buf, asnWriter._offset, 0, pub.length);
asnWriter._offset += pub.length;
asnWriter.endSequence();
asnWriter.endSequence();
return makePEM('PUBLIC', asnWriter.buffer);
asnWriter.writeOID('1.2.840.10040.4.1');
asnWriter.startSequence();
asnWriter.writeBuffer(p, Ber.Integer);
asnWriter.writeBuffer(q, Ber.Integer);
asnWriter.writeBuffer(g, Ber.Integer);
asnWriter.endSequence();
asnWriter.endSequence();
asnWriter.startSequence(Ber.BitString);
asnWriter.writeByte(0x00);
asnWriter.writeBuffer(y, Ber.Integer);
asnWriter.endSequence();
if (rawsig.length <= 40) {
// change bare signature r and s values to ASN.1 DER values for OpenSSL
var asnSigWriter = new Ber.Writer();
asnSigWriter.startSequence();
asnSigWriter.writeBuffer(rawsig.slice(0, 20), Ber.Integer);
asnSigWriter.writeBuffer(rawsig.slice(20), Ber.Integer);
asnSigWriter.endSequence();
rawsig = asnSigWriter.buffer;
}
}
asnWriter.endSequence();
debug && debug('DEBUG: Connection: Verifying signature');
var b64key = asnWriter.buffer.toString('base64')
.replace(/(.{64})/g, '$1\n'),
fullkey = '-----BEGIN PUBLIC KEY-----\n'
+ b64key
+ (b64key[b64key.length - 1] === '\n' ? '' : '\n')
function genOpenSSLECDSAPriv(oid, pub, priv) {
var asnWriter = new Ber.Writer();
asnWriter.startSequence();
// version
asnWriter.writeInt(0x01, Ber.Integer);
// privateKey
asnWriter.writeBuffer(priv, Ber.OctetString);
// parameters (optional)
asnWriter.startSequence(0xA0);
asnWriter.writeOID(oid);
asnWriter.endSequence();
// publicKey (optional)
asnWriter.startSequence(0xA1);
asnWriter.startSequence(Ber.BitString);
asnWriter.writeByte(0x00);
// XXX: hack to write a raw buffer without a tag -- yuck
asnWriter._ensure(pub.length);
pub.copy(asnWriter._buf, asnWriter._offset, 0, pub.length);
function DSAKeySSHToASN1(key, self, callback) {
// Convert SSH key parameters to ASN.1 BER values for OpenSSL
var p = readString(key, key._pos, self, callback);
if (p === false)
return false;
var q = readString(key, key._pos, self, callback);
if (q === false)
return false;
var g = readString(key, key._pos, self, callback);
if (g === false)
return false;
var y = readString(key, key._pos, self, callback);
if (y === false)
return false;
var asnWriter = new Ber.Writer();
asnWriter.startSequence();
// algorithm
asnWriter.startSequence();
asnWriter.writeOID('1.2.840.10040.4.1'); // id-dsa
// algorithm parameters
asnWriter.startSequence();
asnWriter.writeBuffer(p, Ber.Integer);
asnWriter.writeBuffer(q, Ber.Integer);
asnWriter.writeBuffer(g, Ber.Integer);
asnWriter.endSequence();
asnWriter.endSequence();
// subjectPublicKey
asnWriter.startSequence(Ber.BitString);
asnWriter.writeByte(0x00);
asnWriter.writeBuffer(y, Ber.Integer);
ecdsaPrivateKeyToASN1(prvKeyHex:string): Buffer {
var Ber = require('asn1').Ber;
var sk = new Ber.Writer();
sk.startSequence();
sk.writeInt(1);
sk.writeBuffer(new Buffer(prvKeyHex, 'hex'), 4);
sk.writeByte(160);
sk.writeByte(7);
if (this.securityLevel == CURVE_P_384_Size ) {
// OID of P384
sk.writeOID('1.3.132.0.34');
} else if (this.securityLevel == CURVE_P_256_Size) {
// OID of P256
sk.writeOID('1.2.840.10045.3.1.7');
} else {
throw Error("Not supported. Level " + this.securityLevel)
}
sk.endSequence();
return sk.buffer;
function ECDSASigSSHToASN1(signature, self, callback) {
// Convert SSH signature parameters to ASN.1 BER values for OpenSSL
var r = readString(signature, 0, self, callback);
if (r === false)
return false;
var s = readString(signature, signature._pos, self, callback);
if (s === false)
return false;
var asnWriter = new Ber.Writer();
asnWriter.startSequence();
asnWriter.writeBuffer(r, Ber.Integer);
asnWriter.writeBuffer(s, Ber.Integer);
asnWriter.endSequence();
return asnWriter.buffer;
}
case 'nistp256':
// prime256v1/secp256r1
ecCurveOID = '1.2.840.10045.3.1.7';
break;
case 'nistp384':
// secp384r1
ecCurveOID = '1.3.132.0.34';
break;
case 'nistp521':
// secp521r1
ecCurveOID = '1.3.132.0.35';
break;
default:
return false;
}
var asnWriter = new Ber.Writer();
asnWriter.startSequence();
// algorithm
asnWriter.startSequence();
asnWriter.writeOID('1.2.840.10045.2.1'); // id-ecPublicKey
// algorithm parameters (namedCurve)
asnWriter.writeOID(ecCurveOID);
asnWriter.endSequence();
// subjectPublicKey
asnWriter.startSequence(Ber.BitString);
asnWriter.writeByte(0x00);
// XXX: hack to write a raw buffer without a tag -- yuck
asnWriter._ensure(Q.length);
Q.copy(asnWriter._buf, asnWriter._offset, 0, Q.length);
asnWriter._offset += Q.length;
// end hack
function genRSAASN1Buf(n, e, d, p, q, dmp1, dmq1, iqmp) {
var asnWriter = new Ber.Writer();
asnWriter.startSequence();
asnWriter.writeInt(0x00, Ber.Integer);
asnWriter.writeBuffer(n, Ber.Integer);
asnWriter.writeBuffer(e, Ber.Integer);
asnWriter.writeBuffer(d, Ber.Integer);
asnWriter.writeBuffer(p, Ber.Integer);
asnWriter.writeBuffer(q, Ber.Integer);
asnWriter.writeBuffer(dmp1, Ber.Integer);
asnWriter.writeBuffer(dmq1, Ber.Integer);
asnWriter.writeBuffer(iqmp, Ber.Integer);
asnWriter.endSequence();
return asnWriter.buffer;
}