Esempio n. 1
0
        private void SendClientKeyExchange()
        {
            var preMasterSecret = TLS12.GetPreMasterSecret();

            _session.MasterSecret = TLS12.GetMasterSecret(preMasterSecret, _session.ClientRandom, _session.ServerRandom);
            var clientKeyExchange = new ClientKeyExchange
            {
                SessionId       = _session.Id,
                PreMasterSecret = preMasterSecret
            };
            var serverRsa = Rsa.CreateFromPublicPEM(_session.RSAPublicPem);
            var request   = new SecurityLayerMessage
            {
                Type = SecurityMessageType.ClientKeyExchange,
                Data = serverRsa.Encrypt(_serializer.Serialize <ClientKeyExchange>(clientKeyExchange))
            };
            var requestBytes = _serializer.Serialize <SecurityLayerMessage>(request);

            Send(requestBytes);

            var responseBytes  = Receive();
            var message        = _serializer.Deserialize <SecurityLayerMessage>(responseBytes);
            var serverFinished = _serializer.Deserialize <ServerFinished>(message.Data);

            _session.Id = serverFinished.SessionId;
            var keys = TLS12.GetKeys(_session.MasterSecret, _session.ClientRandom, _session.ServerRandom);

            _session.ClientWriteMACKey = TLS12.GetClientWriteMACKey(keys);
            _session.ServerWriteMACKey = TLS12.GetServerWriteMACKey(keys);
            _session.ClientWriteKey    = TLS12.GetClientWriteKey(keys);
            _session.ServerWriteKey    = TLS12.GetServerWriteKey(keys);
            _session.IsAuthenticated   = true;
        }
Esempio n. 2
0
        private void HandleClientKeyExchange(byte[] message)
        {
            var decryptedMessage  = _rsa.Decrypt(message);
            var clientKeyExchange = _serializer.Deserialize <ClientKeyExchange>(decryptedMessage);
            var session           = _sessionCache.Get(clientKeyExchange.SessionId);

            if (session == null)
            {
                // TODO: Send error
            }
            else
            {
                session.MasterSecret = TLS12.GetMasterSecret(clientKeyExchange.PreMasterSecret, session.ClientRandom, session.ServerRandom);
                var keys = TLS12.GetKeys(session.MasterSecret, session.ClientRandom, session.ServerRandom);
                session.ClientWriteMACKey = TLS12.GetClientWriteMACKey(keys);
                session.ServerWriteMACKey = TLS12.GetServerWriteMACKey(keys);
                session.ClientWriteKey    = TLS12.GetClientWriteKey(keys);
                session.ServerWriteKey    = TLS12.GetServerWriteKey(keys);
                session.IsAuthenticated   = true;
            }
            var serverFinished = new ServerFinished
            {
                SessionId = session.Id
            };
            var response = new SecurityLayerMessage
            {
                Type = SecurityMessageType.ServerFinished,
                Data = _serializer.Serialize <ServerFinished>(serverFinished)
            };
            var responseBytes = _serializer.Serialize <SecurityLayerMessage>(response);

            Send(responseBytes);
        }