/// <summary>
        /// This is to be called from the concrete communication channel.S
        /// </summary>
        /// <param name="receivedData"></param>
        protected void OnRaisePortRequestReceived(byte[] receivedData, string sender)
        {
            Int32 port = 0;

            try
            {
                var ms = new MemoryStream(receivedData);
                using BinaryReader sr = new BinaryReader(ms);

                var salt = sr.ReadBytes(EncryptionUtils.saltSize);
                port = sr.ReadInt32();
                var encryptedText = sr.ReadBytes(1000);

                //now we should try to decrypt the data.
                var portRule = _configuration.GetRuleFromTcpPort(port);
                if (portRule == null)
                {
                    throw new SecurityException($"Unable to find rule for requested port {port}");
                }
                var decrypted = Encryptor.SimmetricDecrypt(portRule.Secret, salt, encryptedText);
                var decryptedDeserializedText = Encoding.UTF8.GetString(decrypted);
                var request = JsonConvert.DeserializeObject <OpenPortRequest>(decryptedDeserializedText);
                OpenPortRequestReceived?.Invoke(this, new OpenPortRequestEventArgs(request));
            }
            catch (CryptographicException)
            {
                throw new SecurityException($"Wrong password received trying to access port {port} from {sender}");
            }
            catch (Exception ex)
            {
                throw new SecurityException($"Malformed packets received from {sender} - {ex}");
            }
        }
コード例 #2
0
        /// <summary>
        /// This is to be called from the concrete communication channel.S
        /// </summary>
        /// <param name="receivedData"></param>
        protected void OnRaisePortRequestReceived(byte[] receivedData, string sender)
        {
            var transmissionData = JsonConvert.DeserializeObject <TransmissionData>(
                Encoding.UTF8.GetString(receivedData));
            var loadPublicKey = _configuration.LoadPublicKey(transmissionData.Sender);

            Encryptor.Verify(transmissionData.Request, transmissionData.Signature, loadPublicKey);

            //if we reach here, signature is ok, we can simply proceed
            var request = JsonConvert.DeserializeObject <OpenPortRequest>(
                Encoding.UTF8.GetString(transmissionData.Request));

            //port rule exists?
            var rule = _configuration.GetRuleFromTcpPort(request.PortToOpen);

            if (rule == null)
            {
                Log.Error("Received valid message to open port {port} that has no rule", request.PortToOpen);
                throw new SecurityException("Error");
            }

            OpenPortRequestReceived?.Invoke(this, new OpenPortRequestEventArgs(request));
        }