@microsoft/dev-tunnels-ssh
Version:
SSH library for Dev Tunnels
99 lines • 4.1 kB
JavaScript
;
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
Object.defineProperty(exports, "__esModule", { value: true });
exports.NodeECDiffieHellman = exports.NodeDiffieHellman = void 0;
const crypto = require("crypto");
const buffer_1 = require("buffer");
const keyExchangeAlgorithm_1 = require("../keyExchangeAlgorithm");
const nodeHmac_1 = require("./nodeHmac");
const bigInt_1 = require("../../io/bigInt");
class NodeDiffieHellman extends keyExchangeAlgorithm_1.KeyExchangeAlgorithm {
constructor(name, keySizeInBits, hashAlgorithmName) {
super(name, keySizeInBits, hashAlgorithmName, nodeHmac_1.NodeHmac.getHashDigestLength(hashAlgorithmName));
}
createKeyExchange() {
return new NodeDiffieHellmanKex(this.keySizeInBits, nodeHmac_1.NodeHmac.getNodeHashAlgorithmName(this.hashAlgorithmName), this.hashDigestLength);
}
}
exports.NodeDiffieHellman = NodeDiffieHellman;
class NodeDiffieHellmanKex {
constructor(bitLength, hashAlgorithmName, digestLength) {
this.hashAlgorithmName = hashAlgorithmName;
this.digestLength = digestLength;
switch (bitLength) {
case 1024:
this.dh = crypto.getDiffieHellman('modp2');
break;
case 2048:
this.dh = crypto.getDiffieHellman('modp14');
break;
case 4096:
this.dh = crypto.getDiffieHellman('modp16');
break;
default:
throw new Error('Invalid DH bit length.');
}
}
startKeyExchange() {
const exchangeValueKeys = this.dh.generateKeys();
const exchangeValue = bigInt_1.BigInt.fromBytes(exchangeValueKeys, { unsigned: true }).toBytes();
return Promise.resolve(exchangeValue);
}
decryptKeyExchange(exchangeValue) {
const key = this.dh.computeSecret(exchangeValue);
const sharedSecret = bigInt_1.BigInt.fromBytes(key, { unsigned: true }).toBytes();
return Promise.resolve(sharedSecret);
}
async sign(data) {
const hash = crypto.createHash(this.hashAlgorithmName);
hash.update(data);
return buffer_1.Buffer.from(hash.digest());
}
dispose() { }
}
class NodeECDiffieHellman extends keyExchangeAlgorithm_1.KeyExchangeAlgorithm {
constructor(name, keySizeInBits, hashAlgorithmName) {
super(name, keySizeInBits, hashAlgorithmName, nodeHmac_1.NodeHmac.getHashDigestLength(hashAlgorithmName));
}
createKeyExchange() {
return new NodeECDiffieHellmanKex(this.keySizeInBits, nodeHmac_1.NodeHmac.getNodeHashAlgorithmName(this.hashAlgorithmName), this.hashDigestLength);
}
}
exports.NodeECDiffieHellman = NodeECDiffieHellman;
class NodeECDiffieHellmanKex {
constructor(bitLength, hashAlgorithmName, digestLength) {
this.hashAlgorithmName = hashAlgorithmName;
this.digestLength = digestLength;
switch (bitLength) {
case 256:
this.ecdh = crypto.createECDH('prime256v1');
break;
case 384:
this.ecdh = crypto.createECDH('secp384r1');
break;
case 521:
this.ecdh = crypto.createECDH('secp521r1');
break;
default:
throw new Error('Invalid ECDH bit length.');
}
}
startKeyExchange() {
const exchangeValue = this.ecdh.generateKeys();
return Promise.resolve(exchangeValue);
}
decryptKeyExchange(exchangeValue) {
const sharedSecretBytes = this.ecdh.computeSecret(exchangeValue);
const sharedSecret = bigInt_1.BigInt.fromBytes(sharedSecretBytes, { unsigned: true }).toBytes();
return Promise.resolve(sharedSecret);
}
async sign(data) {
const hash = crypto.createHash(this.hashAlgorithmName);
hash.update(data);
return buffer_1.Buffer.from(hash.digest());
}
dispose() { }
}
//# sourceMappingURL=nodeKeyExchange.js.map