"use strict";
//
//  Copyright (c) Microsoft Corporation. All rights reserved.
//
Object.defineProperty(exports, "__esModule", { value: true });
exports.NewKeysMessage = exports.KeyExchangeDhReplyMessage = exports.KeyExchangeDhInitMessage = exports.KeyExchangeInitMessage = exports.KeyExchangeMessage = void 0;
const buffer_1 = require("buffer");
const sshMessage_1 = require("./sshMessage");
const sshAlgorithms_1 = require("../algorithms/sshAlgorithms");
class KeyExchangeMessage extends sshMessage_1.SshMessage {
}
exports.KeyExchangeMessage = KeyExchangeMessage;
const keyExchangeInitCookieLength = 16;
class KeyExchangeInitMessage extends KeyExchangeMessage {
    get messageType() {
        return 20;
    }
    onRead(reader) {
        this.cookie = reader.read(keyExchangeInitCookieLength);
        this.keyExchangeAlgorithms = reader.readList('ascii');
        this.serverHostKeyAlgorithms = reader.readList('ascii');
        this.encryptionAlgorithmsClientToServer = reader.readList('ascii');
        this.encryptionAlgorithmsServerToClient = reader.readList('ascii');
        this.macAlgorithmsClientToServer = reader.readList('ascii');
        this.macAlgorithmsServerToClient = reader.readList('ascii');
        this.compressionAlgorithmsClientToServer = reader.readList('ascii');
        this.compressionAlgorithmsServerToClient = reader.readList('ascii');
        this.languagesClientToServer = reader.readList('ascii');
        this.languagesServerToClient = reader.readList('ascii');
        this.firstKexPacketFollows = reader.readBoolean();
        this.reserved = reader.readUInt32();
    }
    onWrite(writer) {
        var _a;
        if (!this.cookie) {
            this.cookie = buffer_1.Buffer.alloc(keyExchangeInitCookieLength);
            sshAlgorithms_1.SshAlgorithms.random.getBytes(this.cookie);
        }
        writer.write(this.cookie);
        writer.writeList(this.keyExchangeAlgorithms || [], 'ascii');
        writer.writeList(this.serverHostKeyAlgorithms || [], 'ascii');
        writer.writeList(this.encryptionAlgorithmsClientToServer || [], 'ascii');
        writer.writeList(this.encryptionAlgorithmsServerToClient || [], 'ascii');
        writer.writeList(this.macAlgorithmsClientToServer || [], 'ascii');
        writer.writeList(this.macAlgorithmsServerToClient || [], 'ascii');
        writer.writeList(this.compressionAlgorithmsClientToServer || [], 'ascii');
        writer.writeList(this.compressionAlgorithmsServerToClient || [], 'ascii');
        writer.writeList(this.languagesClientToServer || [], 'ascii');
        writer.writeList(this.languagesServerToClient || [], 'ascii');
        writer.writeBoolean((_a = this.firstKexPacketFollows) !== null && _a !== void 0 ? _a : false);
        writer.writeUInt32(this.reserved || 0);
    }
    static CreateNone() {
        const noneArray = ['none'];
        const emptyArray = [''];
        const message = new KeyExchangeInitMessage();
        message.cookie = buffer_1.Buffer.alloc(keyExchangeInitCookieLength);
        message.keyExchangeAlgorithms = noneArray;
        message.serverHostKeyAlgorithms = noneArray;
        message.encryptionAlgorithmsClientToServer = noneArray;
        message.encryptionAlgorithmsServerToClient = noneArray;
        message.macAlgorithmsClientToServer = noneArray;
        message.macAlgorithmsServerToClient = noneArray;
        message.compressionAlgorithmsClientToServer = noneArray;
        message.compressionAlgorithmsServerToClient = noneArray;
        message.languagesClientToServer = emptyArray;
        message.languagesServerToClient = emptyArray;
        // Save the serialized bytes so that the message doesn't have to be re-serialized every time
        // it is sent.
        message.rawBytes = message.toBuffer();
        return message;
    }
    get allowsNone() {
        const includesNone = (algorithms) => (algorithms === null || algorithms === void 0 ? void 0 : algorithms.includes('none')) === true;
        return (includesNone(this.keyExchangeAlgorithms) &&
            includesNone(this.serverHostKeyAlgorithms) &&
            includesNone(this.encryptionAlgorithmsClientToServer) &&
            includesNone(this.encryptionAlgorithmsServerToClient) &&
            includesNone(this.macAlgorithmsClientToServer) &&
            includesNone(this.macAlgorithmsServerToClient) &&
            includesNone(this.compressionAlgorithmsClientToServer) &&
            includesNone(this.compressionAlgorithmsServerToClient) &&
            this.firstKexPacketFollows !== true);
    }
}
exports.KeyExchangeInitMessage = KeyExchangeInitMessage;
KeyExchangeInitMessage.none = KeyExchangeInitMessage.CreateNone();
class KeyExchangeDhInitMessage extends KeyExchangeMessage {
    get messageType() {
        return 30;
    }
    onRead(reader) {
        this.e = reader.readBinary();
    }
    onWrite(writer) {
        writer.writeBinary(this.validateField(this.e, 'E'));
    }
}
exports.KeyExchangeDhInitMessage = KeyExchangeDhInitMessage;
class KeyExchangeDhReplyMessage extends KeyExchangeMessage {
    get messageType() {
        return 31;
    }
    onRead(reader) {
        this.hostKey = reader.readBinary();
        this.f = reader.readBinary();
        this.signature = reader.readBinary();
    }
    onWrite(writer) {
        writer.writeBinary(this.validateField(this.hostKey, 'host key'));
        writer.writeBinary(this.validateField(this.f, 'F'));
        writer.writeBinary(this.validateField(this.signature, 'signature'));
    }
}
exports.KeyExchangeDhReplyMessage = KeyExchangeDhReplyMessage;
class NewKeysMessage extends KeyExchangeMessage {
    get messageType() {
        return 21;
    }
    onRead(reader) { }
    onWrite(writer) { }
}
exports.NewKeysMessage = NewKeysMessage;
sshMessage_1.SshMessage.index.set(20, KeyExchangeInitMessage);
sshMessage_1.SshMessage.index.set(30, KeyExchangeDhInitMessage);
sshMessage_1.SshMessage.index.set(31, KeyExchangeDhReplyMessage);
sshMessage_1.SshMessage.index.set(21, NewKeysMessage);
//# sourceMappingURL=https://github.com/VSCodium/sourcemaps/releases/download/stable-824c4c46a288b839f13b24022655329c2aeb9f81/node_modules-@microsoft-dev-tunnels-ssh-messages-kexMessages.js.map