Esempio n. 1
0
        public void Setup()
        {
            //EneterTrace.DetailLevel = EneterTrace.EDetailLevel.Debug;
            //EneterTrace.TraceLog = new StreamWriter("d:/tracefile.txt");
            //EneterTrace.StartProfiler();


            // Generate random number for the port.
            string aPort = RandomPortGenerator.Generate();

            TcpMessagingSystemFactory anUnderlyingMessaging = new TcpMessagingSystemFactory();

            //ChannelId = "tcp://127.0.0.1:" + aPort + "/";
            ChannelId = "tcp://[::1]:" + aPort + "/";

            MessagingSystemFactory = new AuthenticatedMessagingFactory(anUnderlyingMessaging,
                                                                       GetLoginMessage,
                                                                       GetHandshakeResponseMessage,
                                                                       GetHandshakeMessage, VerifyHandshakeResponseMessage, HandleAuthenticationCancelled)
            {
                AuthenticationTimeout = TimeSpan.FromMilliseconds(2000)
            };

            myHandshakeSerializer = new AesSerializer("Password123");
        }
Esempio n. 2
0
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType <Mapper>().As <IMapper>().SingleInstance();

            builder.RegisterMsSql(
                _connectionString,
                connString => new DatabaseContext(connString, false),
                dbConn => new DatabaseContext(dbConn));

            builder.RegisterType <PushNotificationRegistrationRepository>()
            .As <IPushNotificationRegistrationRepository>()
            .SingleInstance();

            builder.RegisterType <NotificationMessageRepository>()
            .As <INotificationMessageRepository>()
            .SingleInstance();

            var encryptionKey = Environment.GetEnvironmentVariable("EncryptionKey");
            var encryptionIv  = Environment.GetEnvironmentVariable("EncryptionIV");

            var serializer = new AesSerializer(encryptionKey, encryptionIv);

            builder.RegisterInstance(serializer)
            .As <IAesSerializer>()
            .SingleInstance();

            builder.RegisterType <EncryptionService>()
            .As <IEncryptionService>()
            .SingleInstance();
        }
Esempio n. 3
0
        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType <PushNotificationRegistrationService>()
            .As <IPushNotificationRegistrationService>()
            .SingleInstance();

            builder.RegisterType <NotificationMessageService>()
            .As <INotificationMessageService>()
            .SingleInstance();

            builder.RegisterType <Mapper>().As <IMapper>().SingleInstance();

            var encryptionKey = Environment.GetEnvironmentVariable("EncryptionKey");
            var encryptionIv  = Environment.GetEnvironmentVariable("EncryptionIV");

            var serializer = new AesSerializer(encryptionKey, encryptionIv);

            builder.RegisterInstance(serializer)
            .As <IAesSerializer>()
            .SingleInstance();

            builder.RegisterType <EncryptionService>()
            .As <IEncryptionService>()
            .SingleInstance();
        }
Esempio n. 4
0
        public bool TrySetKey(string key, out string error)
        {
            error = null;

            if (HasKey)
            {
                error = "Key is already installed.";
                return(false);
            }

            ICryptographicSerializer serializer;

            try
            {
                serializer = new AesSerializer(key);
            }
            catch (Exception ex)
            {
                error = $"Wrong key format. {ex.Message}";
                return(false);
            }
            var encryptedStorage = EncryptedTableStorageDecorator <EncryptionInitModel> .Create(_storage, serializer);

            if (WasEncryptionSet())
            {
                try
                {
                    var existingValue = encryptedStorage.GetDataAsync(InitKey, InitKey).GetAwaiter().GetResult();
                    if (existingValue.Data == InitKey)
                    {
                        Serializer = serializer;
                        return(true);
                    }
                    else
                    {
                        error = "The specified key is incorrect.";
                        return(false);
                    }
                }
                catch (System.Security.Cryptography.CryptographicException)
                {
                    error = "The specified key is incorrect.";
                    return(false);
                }
            }
            else
            {
                // this is a new and the only one key
                encryptedStorage.InsertAsync(new EncryptionInitModel {
                    PartitionKey = InitKey, RowKey = InitKey, Data = InitKey
                }).GetAwaiter().GetResult();
                Serializer = serializer;
                return(true);
            }
        }
Esempio n. 5
0
        public void IncorrectPassword()
        {
            string aData = "Hello world.";

            // Serialize.
            object aSerializedData = TestedSerializer.Serialize <string>(aData);

            // Serializer with incorrect password.
            ISerializer anIncorrectSerializer = new AesSerializer("mytestpassword1");

            // Try to deserialize.
            Assert.Throws <CryptographicException>(() => anIncorrectSerializer.Deserialize <string>(aSerializedData));
        }
        public void Setup()
        {
            //EneterTrace.DetailLevel = EneterTrace.EDetailLevel.Debug;
            //EneterTrace.TraceLog = new StreamWriter("d:/tracefile.txt");

            SynchronousMessagingSystemFactory anUnderlyingMessaging = new SynchronousMessagingSystemFactory();

            ChannelId = "MyChannel1";

            MessagingSystemFactory = new AuthenticatedMessagingFactory(anUnderlyingMessaging,
                                                                       GetLoginMessage,
                                                                       GetHandshakeResponseMessage,
                                                                       GetHandshakeMessage, VerifyHandshakeResponseMessage, HandleAuthenticationCancelled)
            {
                AuthenticationTimeout = TimeSpan.FromMilliseconds(2000)
            };

            myHandshakeSerializer = new AesSerializer("Password123");
        }
Esempio n. 7
0
        // Callback which is called when a client sends the login message.
        // It shall verify the login and return the handshake message.
        private static object GetHandshakeMessage(string channelId,
                                                  string responseReceiverId,
                                                  object loginMessage)
        {
            // Find the login name and password in "database"
            // and encrypt the handshake message.
            if (loginMessage is string)
            {
                string aLoginName = (string)loginMessage;

                Console.WriteLine("Received login: "******"Login was not ok. The connection will be closed.");
                        return(null);
                    }
                }
            }
            else
            {
                return(null);
            }
        }
 public void Setup()
 {
     TestedSerializer  = new AesSerializer("mytestpassword", new BinarySerializer());
     TestedSerializer2 = new AesSerializer("mytestpassword", new BinarySerializer());
 }
Esempio n. 9
0
        // Callback which is called when a client sends the handshake response message.
        private static bool Authenticate(string channelId,
                                         string responseReceiverId,
                                         object loginMessage,
                                         object handshakeMessage,
                                         object handshakeResponseMessage)
        {
            string aPassword;

            if (loginMessage is string)
            {
                using (LogstorOEEEntities db = new TestLogin.LogstorOEEEntities())
                {
                    string aLoginName = (string)loginMessage;

                    Users_Security users_Security = db.Users_Security.Where((x) => x.Login == aLoginName).FirstOrDefault();
                    if (!String.IsNullOrEmpty(users_Security.Password))
                    {
                        aPassword = users_Security.Password;
                    }
                    else
                    {
                        return(false);
                    }
                }
                // Get the password associated with the user.



                // Decrypt the handshake response message.
                // Handshake response message is one more time encrypted handshake message.
                // Therefore if the handshake response is decrypted two times it should be
                // the originaly generated GUID.
                try
                {
                    ISerializer aSerializer = new AesSerializer(aPassword);

                    // Decrypt handshake response to get original GUID.
                    string aDecodedHandshakeResponse1 = aSerializer.Deserialize <string>(handshakeResponseMessage);
                    byte[] temp = ConvertHandshakeToBytes(aDecodedHandshakeResponse1);
                    string aDecodedHandshakeResponse2 = aSerializer.Deserialize <string>(temp);

                    // Decrypt original handshake message.
                    string anOriginalGuid = aSerializer.Deserialize <string>(handshakeMessage);

                    // If GUIDs are equal then the identity of the client is verified.
                    if (anOriginalGuid == aDecodedHandshakeResponse2)
                    {
                        Console.WriteLine("Client authenticated.");

                        // The handshake response is correct so the connection can be established.
                        return(true);
                    }
                }
                catch (Exception err)
                {
                    // Decoding of the response message failed.
                    // The authentication will not pass.
                    Console.WriteLine("Decoding handshake message failed.", err);
                }
            }

            // Authentication did not pass.
            Console.WriteLine("Authentication did not pass. The connection will be closed.");
            return(false);
        }
Esempio n. 10
0
 public void Setup()
 {
     TestedSerializer  = new AesSerializer("mytestpassword", new XmlStringSerializer());
     TestedSerializer2 = new AesSerializer("mytestpassword", new XmlStringSerializer());
 }