handleMSG_NEWKEYS method

void handleMSG_NEWKEYS()

When MSG_NEWKEYS is received, the new keys and algorithms MUST be used for receiving.

Implementation

void handleMSG_NEWKEYS() {
  if (state != SSHTransportState.FIRST_NEWKEYS &&
      state != SSHTransportState.NEWKEYS) {
    throw FormatException('$hostport: unexpected state $state');
  }
  if (tracePrint != null) {
    tracePrint('$hostport: MSG_NEWKEYS');
  }
  int keyLenC = Cipher.keySize(cipherIdC2s),
      keyLenS = Cipher.keySize(cipherIdS2c);
  encrypt = initCipher(
      cipherIdC2s,
      deriveKey(kexHash, sessionId, exH, K, 'A'.codeUnits[0], 24),
      deriveKey(kexHash, sessionId, exH, K, 'C'.codeUnits[0], keyLenC),
      client ? true : false);
  decrypt = initCipher(
      cipherIdS2c,
      deriveKey(kexHash, sessionId, exH, K, 'B'.codeUnits[0], 24),
      deriveKey(kexHash, sessionId, exH, K, 'D'.codeUnits[0], keyLenS),
      client ? false : true);
  if ((macHashLenC = MAC.hashSize(macIdC2s)) <= 0) {
    throw FormatException('$hostport: invalid maclen $encryptBlockSize');
  } else if ((macHashLenS = MAC.hashSize(macIdS2c)) <= 0) {
    throw FormatException('$hostport: invalid maclen $encryptBlockSize');
  }
  integrityC2s =
      deriveKey(kexHash, sessionId, exH, K, 'E'.codeUnits[0], macHashLenC);
  integrityS2c =
      deriveKey(kexHash, sessionId, exH, K, 'F'.codeUnits[0], macHashLenS);
  if (server) {
    BlockCipher tmpBC = encrypt;
    encrypt = decrypt;
    decrypt = tmpBC;

    Uint8List swapUL = integrityC2s;
    integrityC2s = integrityS2c;
    integrityS2c = swapUL;
  }
  state = SSHTransportState.NEWKEYS;
}