コード例 #1
0
        public WebsocketClientObject(IWebSocketConnection connection)//TcpClient tcpClient)//Socket tcpClient)
        {
            //client = tcpClient;
            _applicationProcessor = new ServiceProcessor(UserLoggedIn);
            // _accumulator = new List<byte>();
            _connection = connection;

            _connection.OnMessage = message => { DeconstructPacketAndProcess(message); };
            _connection.OnClose   = () => { Close(); };

            ServiceProcessor.UserLoggedIn userLoggedInAction = UserLoggedIn;
        }
コード例 #2
0
        public void Process(Object stateInfo)
        {
            try
            {
                _stream = client.GetStream();
                byte[] data = new byte[64]; // buffer

                //Need this to know when to pass delegate for updating
                ServiceProcessor.UserLoggedIn userLoggedInAction = UserLoggedIn;
                clientRecognizer = new Recognizer(userLoggedInAction);

                while (!(client.Client.Poll(1000, SelectMode.SelectRead) && client.Available == 0))
                {
                    // Request - Response
                    int bytes = 0;
                    do
                    {
                        bytes = _stream.Read(data, 0, data.Length);                                        //TO DO: handle System.IO.IOException

                        if (clientRecognizer.Process(data, bytes) &&                                       //collection data while response is not ready
                            clientRecognizer.Response.Length > 0)                                          //drop packet in case of some problems
                        {
                            _stream.Write(clientRecognizer.Response, 0, clientRecognizer.Response.Length); //when response ready send it
                        }
                    }while (_stream.DataAvailable);
                }
            }
            catch (IOException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (SocketException ex)
            {
                Console.WriteLine(ex.Message);
            }


            // Removing updating delegate from active users table
            if (!String.IsNullOrEmpty(_userId))
            {
                ServiceProcessor._activeUsersTable.TryRemove(_userId, out _);
            }

            client.Close();
#if DEBUG
            Console.WriteLine($"Connection closed, user id: {_userId}");
#endif
        }
コード例 #3
0
        public void InitDiffieHellman_ShouldGenerateSameDerivedKeys()   //imitating client to test encrypting
        {
            ECDiffieHellmanCng eCDiffie = new ECDiffieHellmanCng(256);

            eCDiffie.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
            eCDiffie.HashAlgorithm         = CngAlgorithm.Sha256;

            byte[] myPublicKey       = eCDiffie.ExportSubjectPublicKeyInfo(); //export in x509 format
            String myPublicKeyBase64 = Convert.ToBase64String(myPublicKey);


            //Passing to project
            var packetToPreoject = new Packet('0', Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Concat("{", String.Format("\"Public_key\": \"{0}\"", myPublicKeyBase64), "}"))));

            ServiceProcessor.UserLoggedIn stubMethod = StubMethod;
            PacketProcessor packetProcessor          = new PacketProcessor(stubMethod);
            Packet          response = packetProcessor.Process(packetToPreoject);

            PublicKeyPayload serverPublicKey = JsonSerializer.Deserialize <PublicKeyPayload>(Convert.FromBase64String(response.Payload));

            byte[]             otherKeyFromBase64 = Convert.FromBase64String(serverPublicKey.Public_key);
            ECDiffieHellmanCng eCDiffie2          = new ECDiffieHellmanCng(256);

            eCDiffie2.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
            eCDiffie2.HashAlgorithm         = CngAlgorithm.Sha256;

            int some = 0;

            eCDiffie2.ImportSubjectPublicKeyInfo(otherKeyFromBase64, out some);

            byte[] otherKeyDecoded = eCDiffie2.PublicKey.ToByteArray();
            CngKey k = CngKey.Import(otherKeyDecoded, CngKeyBlobFormat.EccPublicBlob);

            byte[] derivedKey = eCDiffie.DeriveKeyMaterial(k);

            string actual   = Convert.ToBase64String(packetProcessor.EncryptionModule.DerivedKey);
            string expected = Convert.ToBase64String(derivedKey);


            Assert.Equal(expected, actual);
        }
コード例 #4
0
 public Recognizer(ServiceProcessor.UserLoggedIn action)
 {
     _pktProcessor               = new PacketProcessor(action);
     _applicationPacket          = new List <byte>();
     _applicationPacket.Capacity = 64;
 }