public void ProcessMessage(HandshakeMessage message)
        {
            byte[] msgData = message.Encode();
            _handshakeStream.Write(msgData, 0, msgData.Length);

            switch (message.Type)
            {
                case HandshakeMessageType.HelloRequest:
                    ProcessHelloRequest();
                    break;
                case HandshakeMessageType.ClientHello:
                    ProcessClientHello((HandshakeClientHello)message);
                    break;
                case HandshakeMessageType.ServerHello:
                    ProcessServerHello((HandshakeServerHello)message);
                    break;
                case HandshakeMessageType.NewSessionTicket:
                    ProcessNewSessionTicket(message);
                    break;
                case HandshakeMessageType.Certificate:
                    ProcessCertificate((HandshakeCertificate)message);
                    break;
                case HandshakeMessageType.ServerKeyExchange:
                    ProcessServerKeyExchange(message);
                    break;
                case HandshakeMessageType.CertificateRequest:
                    ProcessCertificateRequest((HandshakeCertificateRequest)message);
                    break;
                case HandshakeMessageType.ServerHelloDone:
                    ProcessServerHelloDone(message);
                    break;
                case HandshakeMessageType.CertificateVerify:
                    ProcessCertificateVerify(message);
                    break;
                case HandshakeMessageType.ClientKeyExchange:
                    ProcessClientKeyExchange(message);
                    break;
                case HandshakeMessageType.Finished:
                    ProcessFinished(message);
                    break;
            }
        }
        protected override void ProcessFinished(HandshakeMessage finished)
        {
            if (_state != HandshakeState.ReceivedChangeCipherSpec)
            {
                throw new AlertException(AlertDescription.UnexpectedMessage,
                                         "Finished received at the wrong time");
            }

            byte[] allMessages = _handshakeStream.ToArray();
            byte[] handshakeMessages = new byte[allMessages.Length - finished.Encode().Length];
            Buffer.BlockCopy(allMessages, 0, handshakeMessages, 0, handshakeMessages.Length);

            PseudoRandomFunction prf = _cipherSuite.PseudoRandomFunction;
            HashAlgorithm verifyHash = prf.CreateVerifyHashAlgorithm(_connectionState.MasterSecret);
            verifyHash.TransformBlock(handshakeMessages, 0, handshakeMessages.Length, handshakeMessages, 0);

            byte[] ourVerifyData;
            if (_version == ProtocolVersion.SSL3_0)
            {
                byte[] senderBytes = Encoding.ASCII.GetBytes("CLNT");
                verifyHash.TransformFinalBlock(senderBytes, 0, senderBytes.Length);
                ourVerifyData = verifyHash.Hash;
            }
            else
            {
                verifyHash.TransformFinalBlock(new byte[0], 0, 0);
                byte[] hash = verifyHash.Hash;

                int length = _cipherSuite.PseudoRandomFunction.VerifyDataLength;
                ourVerifyData = _cipherSuite.PseudoRandomFunction.GetBytes(_connectionState.MasterSecret, "client finished", hash, length);
            }

            // Verify that the received data matches ours
            bool verifyOk = false;
            if (ourVerifyData.Length == finished.Data.Length)
            {
                verifyOk = true;
                for (int i = 0; i < ourVerifyData.Length; i++)
                {
                    if (ourVerifyData[i] != finished.Data[i])
                    {
                        verifyOk = false;
                    }
                }
            }

            if (!verifyOk)
            {
                throw new AlertException(AlertDescription.DecryptError,
                                         "Finished packet contents incorrect");
            }
            _connectionState.ClientVerifyData = ourVerifyData;

            // Send our own change cipher spec
            _state = HandshakeState.SendChangeCipherSpec;
        }
        protected void OutputMessage(HandshakeMessage message)
        {
            byte[] data = message.Encode();
            _handshakeStream.Write(data, 0, data.Length);

            _outputMessages.Add(message);
        }
        protected override void ProcessCertificateVerify(HandshakeMessage verify)
        {
            if (_state != HandshakeState.ReceivedClientKeyExchange)
            {
                throw new AlertException(AlertDescription.UnexpectedMessage,
                                         "Certificate verify received at the wrong time");
            }
            if (!_clientCertificateReceived)
            {
                throw new AlertException(AlertDescription.UnexpectedMessage,
                                         "Certificate verify received even though client certificate not received");
            }

            // Get all handshake messages up to, but not including, this one
            byte[] allMessages = _handshakeStream.ToArray();
            byte[] handshakeMessages = new byte[allMessages.Length - verify.Encode().Length];
            Buffer.BlockCopy(allMessages, 0, handshakeMessages, 0, handshakeMessages.Length);

            // Verify the signature of handshake messages
            CertificatePublicKey publicKey = new CertificatePublicKey(_clientCertificates[0]);
            bool signatureOk = VerifySignature(publicKey, handshakeMessages, verify.Data);
            if (!signatureOk)
            {
                throw new AlertException(AlertDescription.DecodeError, "Signature from client incorrect");
            }

            // Wait for changecipherspec+finished
            _state = HandshakeState.ReceivedCertificateVerify;
        }