handleMSG_KEXINIT method

void handleMSG_KEXINIT(
  1. MSG_KEXINIT msg,
  2. Uint8List packet
)

Implementation

void handleMSG_KEXINIT(MSG_KEXINIT msg, Uint8List packet) {
  if (tracePrint != null) tracePrint('$hostport: MSG_KEXINIT $msg');

  if (client) {
    guessedS = msg.firstKexPacketFollows;
    kexInitS = packet.sublist(0, packetLen - packetMacLen);
  } else {
    guessedC = msg.firstKexPacketFollows;
    kexInitC = packet.sublist(0, packetLen - packetMacLen);
  }

  /// Make sure we can agree on an algorithm suite.
  if (0 == (kexMethod = KEX.preferenceIntersect(msg.kexAlgorithms, server))) {
    throw FormatException('$hostport: negotiate kex');
  } else if (0 ==
      (hostkeyType =
          Key.preferenceIntersect(msg.serverHostKeyAlgorithms, server))) {
    throw FormatException('$hostport: negotiate hostkey');
  } else if (0 ==
      (cipherIdC2s = Cipher.preferenceIntersect(
          msg.encryptionAlgorithmsClientToServer, server))) {
    throw FormatException('$hostport: negotiate c2s cipher');
  } else if (0 ==
      (cipherIdS2c = Cipher.preferenceIntersect(
          msg.encryptionAlgorithmsServerToClient, server))) {
    throw FormatException('$hostport: negotiate s2c cipher');
  } else if (0 ==
      (macIdC2s =
          MAC.preferenceIntersect(msg.macAlgorithmsClientToServer, server))) {
    throw FormatException('$hostport: negotiate c2s mac');
  } else if (0 ==
      (macIdS2c =
          MAC.preferenceIntersect(msg.macAlgorithmsServerToClient, server))) {
    throw FormatException('$hostport: negotiate s2c mac');
  } else if (0 ==
      (compressIdC2s = Compression.preferenceIntersect(
          msg.compressionAlgorithmsClientToServer,
          server,
          compress ? 0 : 1))) {
    throw FormatException('$hostport: negotiate c2s compression');
  } else if (0 ==
      (compressIdS2c = Compression.preferenceIntersect(
          msg.compressionAlgorithmsServerToClient,
          server,
          compress ? 0 : 1))) {
    throw FormatException('$hostport: negotiate s2c compression');
  }

  /// Setup connection and start Diffie Hellman key exchange.
  guessedRightS = kexMethod == KEX.id(msg.kexAlgorithms.split(',')[0]) &&
      hostkeyType == Key.id(msg.serverHostKeyAlgorithms.split(',')[0]);
  guessedRightC = kexMethod == 1 && hostkeyType == 1;
  encryptBlockSize = Cipher.blockSize(cipherIdC2s);
  decryptBlockSize = Cipher.blockSize(cipherIdS2c);
  macAlgoC2s = MAC.mac(macIdC2s);
  macPrefixC2s = MAC.prefixBytes(macIdC2s);
  macAlgoS2c = MAC.mac(macIdS2c);
  macPrefixS2c = MAC.prefixBytes(macIdS2c);
  sendDiffileHellmanInit();

  if (print != null) {
    print('$hostport: ssh negotiated { kex=${KEX.name(kexMethod)}, hostkey=${Key.name(hostkeyType)}' +
        (cipherIdC2s == cipherIdS2c
            ? ', cipher=${Cipher.name(cipherIdC2s)}'
            : ', cipherC2s=${Cipher.name(cipherIdC2s)}, cipherS2c=${Cipher.name(cipherIdS2c)}') +
        (macIdC2s == macIdS2c
            ? ', mac=${MAC.name(macIdC2s)}'
            : ', macC2s=${MAC.name(macIdC2s)},  macS2c=${MAC.name(macIdS2c)}') +
        (compressIdC2s == compressIdS2c
            ? ', compress=${Compression.name(compressIdC2s)}'
            : ', compressC2s=${Compression.name(compressIdC2s)}, compressS2c=${Compression.name(compressIdS2c)}') +
        " }");
  }
  if (tracePrint != null) {
    tracePrint(
        '$hostport: blockSize=$encryptBlockSize,$decryptBlockSize, macHashLen=$macHashLenC,$macHashLenS');
  }
}