protected virtual void OnHandshakeResponse(HandshakeResponse response) { try { if (_state != ConnectionState.HandshakeResponseWait) { throw new InvalidOperationException("Connection must be in HandshakeResponseWait state"); } var remoteCertificate = new X509Certificate2(response.RawX509Certificate); if (remoteCertificate.HasPrivateKey) { throw new InvalidOperationException("Remote certificate has private key"); } if (!ValidateCertificate(remoteCertificate)) { throw new InvalidOperationException("Remote certiticate not validated"); } _remoteCertificate = remoteCertificate; byte[] clearKey; var alg = _localCertificate.PrivateKey; if (alg is RSACryptoServiceProvider rsa) { clearKey = rsa.Decrypt(response.EncryptedKey, false); } else { throw new InvalidOperationException("not supported key algorithm"); } SendMessage(HandshakeAccepted); _packer.SetKey(clearKey); _state = ConnectionState.Connected; } catch (Exception e) { _remoteCertificate = null; OnHandshakeException(e); } }
protected override void OnHandshakeResponse(HandshakeResponse response) { base.OnHandshakeResponse(response); _notifier.Connected(new ConnectEventArgs()); }