Example #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddSingleton <IQueue, AzureQueue>((serviceProvider) =>
            {
                const string queueName = "somequeue";
                KV = new KeyVault(Configuration["AzureKeyVaultUri"],
                                  Configuration["applicationId"], Configuration["applicationSecret"]);
                var encryptionKeyName = Configuration["EncryptionKeyName"];
                var decryptionKeyName = Configuration["DecryptionKeyName"];
                var signKeyName       = Configuration["SignKeyName"];
                var verifyKeyName     = Configuration["VerifyKeyName"];

                var encryptionCertPassword = Configuration["EncryptionCertPassword"];
                var decryptionCertPassword = Configuration["DecryptionCertPassword"];
                var signCertPassword       = Configuration["SignCertPassword"];
                var verifyCertPassword     = Configuration["VerifyCertPassword"];

                var secretsMgmnt = new KeyVaultCryptoActions(
                    new CertificateInfo(encryptionKeyName, encryptionCertPassword),
                    new CertificateInfo(decryptionKeyName, decryptionCertPassword),
                    new CertificateInfo(signKeyName, signCertPassword),
                    new CertificateInfo(verifyKeyName, verifyCertPassword),
                    KV,
                    KV);
                secretsMgmnt.InitializeAsync().Wait();
                //var securedComm = new RabbitMQBusImpl(config["rabbitMqUri"], secretsMgmnt, true, "securedCommExchange");
                var queueClient = new CloudQueueClientWrapper(Configuration["AzureStorageConnectionString"]);
                securedComm     = new AzureQueue(queueName, queueClient, secretsMgmnt, true);
                securedComm.Initialize().Wait();

                return(securedComm);
            });
        }
Example #2
0
        public async void Sanity_Decryption()
        {
            var kvUri        = "http://dummyKvUri";
            var rawData      = "Some data !!!";
            var kvInfo       = new DatabaseMock(kvUri);
            var secretsMgmnt = new KeyVaultCryptoActions(
                new CertificateInfo(c_encKeyName, string.Empty),
                new CertificateInfo(c_decKeyName, string.Empty),
                new CertificateInfo(c_signKeyName, string.Empty),
                new CertificateInfo(c_verifyKeyName, string.Empty),
                kvInfo,
                kvInfo);
            await secretsMgmnt.InitializeAsync();

            // Encrypt
            var encryptedData = secretsMgmnt.Encrypt(Communication.Utils.ToByteArray(rawData));

            // Decrypt
            var decryptedData = secretsMgmnt.Decrypt(encryptedData);

            // Verify the process ended successfully and the data is plain text
            Assert.IsType <byte[]>(encryptedData);
            Assert.Equal(256, encryptedData.Length);
            Assert.Equal(decryptedData, Communication.Utils.ToByteArray(rawData));
        }
Example #3
0
        public async Task Test_AzureImpl_Enqueue_Dequeue()
        {
            // Init
            var queueMock         = new CloudQueueClientWrapperMock();
            var keyVaultMock      = new DatabaseMock("url");
            var encryptionManager = new KeyVaultCryptoActions(
                new CertificateInfo("emc", string.Empty),
                new CertificateInfo("emc", string.Empty),
                new CertificateInfo("emc", string.Empty),
                new CertificateInfo("emc", string.Empty),
                keyVaultMock,
                keyVaultMock);
            await encryptionManager.InitializeAsync();

            var queueName  = "queueName";
            var azureQueue = new AzureQueue(queueName, queueMock, encryptionManager, true);
            await azureQueue.InitializeAsync();

            // Enqueue Message
            var msg = "new message";
            await azureQueue.EnqueueAsync(Communication.Utils.ToByteArray(msg));

            var task = azureQueue.DequeueAsync(decrypted =>
            {
                // Verify that the decrypted message equals to the original
                Assert.Equal(msg, Communication.Utils.FromByteArray <string>(decrypted));
            }, (message) => { Console.WriteLine("Verification failure, doing nothing"); },
                                               TimeSpan.FromMilliseconds(1));

            Thread.Sleep(10000);
            azureQueue.CancelListeningOnQueue();

            await task;
        }
Example #4
0
        public async Task Test_Enqueue_Message_Happy_flow()
        {
            // Init
            var queueMock         = new CloudQueueClientWrapperMock();
            var keyVaultMock      = new DatabaseMock("url");
            var encryptionManager = new KeyVaultCryptoActions(
                new CertificateInfo("emc", string.Empty),
                new CertificateInfo("emc", string.Empty),
                new CertificateInfo("emc", string.Empty),
                new CertificateInfo("emc", string.Empty),
                keyVaultMock,
                keyVaultMock);
            await encryptionManager.InitializeAsync();

            var queueName  = "queueName";
            var azureQueue = new AzureQueue(queueName, queueMock, encryptionManager, true);
            await azureQueue.InitializeAsync();

            // Enqueue message
            var msg = "new message";
            await azureQueue.EnqueueAsync(Communication.Utils.ToByteArray(msg));

            var queueRefernce = queueMock.GetQueueReference(queueName);

            var result = await queueRefernce.GetMessageAsync(TimeSpan.FromSeconds(10),
                                                             new QueueRequestOptions(), new OperationContext());

            var encryptedMessage = Communication.Utils.FromByteArray <Message>(result.AsBytes);

            // String is encrypted, check it value
            Assert.Equal(256, encryptedMessage.Data.Length);
        }
Example #5
0
        public void Sanity_VerifyCanBeCreated()
        {
            var kvInfo       = new DatabaseMock("http://dummyKvUri");
            var secretsMgmnt = new KeyVaultCryptoActions(
                new CertificateInfo(c_encKeyName, string.Empty),
                new CertificateInfo(c_decKeyName, string.Empty),
                new CertificateInfo(c_signKeyName, string.Empty),
                new CertificateInfo(c_verifyKeyName, string.Empty),
                kvInfo,
                kvInfo);

            secretsMgmnt.InitializeAsync().Wait();
        }
Example #6
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc();

            services.AddSingleton((serviceProvider) =>
            {
                var sqlDb = new SqlConnector(Configuration["SqlUserID"],
                                             Configuration["SqlPassword"],
                                             Configuration["SqlInitialCatalog"],
                                             Configuration["SqlDataSource"],
                                             Configuration["applicationId"],
                                             Configuration["applicationSecret"]);
                sqlDb.Initialize().Wait();

                return(new EthereumAccount(sqlDb, Configuration["EthereumNodeUrl"]));
            });

            services.AddSingleton <IQueue, AzureQueue>((serviceProvider) =>
            {
                const string queueName = "transactions";
                KV = new KeyVault(Configuration["AzureKeyVaultUri"],
                                  Configuration["applicationId"], Configuration["applicationSecret"]);
                var encryptionKeyName = Configuration["EncryptionKeyName"];
                var decryptionKeyName = Configuration["DecryptionKeyName"];
                var signKeyName       = Configuration["SignKeyName"];
                var verifyKeyName     = Configuration["VerifyKeyName"];

                var encryptionCertPassword = Configuration["EncryptionCertPassword"];
                var decryptionCertPassword = Configuration["DecryptionCertPassword"];
                var signCertPassword       = Configuration["SignCertPassword"];
                var verifyCertPassword     = Configuration["VerifyCertPassword"];

                var secretsMgmnt = new KeyVaultCryptoActions(
                    new CertificateInfo(encryptionKeyName, encryptionCertPassword),
                    new CertificateInfo(decryptionKeyName, decryptionCertPassword),
                    new CertificateInfo(signKeyName, signCertPassword),
                    new CertificateInfo(verifyKeyName, verifyCertPassword),
                    KV,
                    KV);
                secretsMgmnt.InitializeAsync().Wait();
                var queueClient = new CloudQueueClientWrapper(Configuration["AzureStorageConnectionString"]);
                azureQueue      = new AzureQueue(queueName, queueClient, secretsMgmnt, true);
                azureQueue.InitializeAsync().Wait();

                return(azureQueue);
            });
        }
Example #7
0
        public async void Sanity_Verify()
        {
            var kvUri        = "http://dummyKvUri";
            var rawData      = "Some data !!!";
            var kvInfo       = new DatabaseMock(kvUri);
            var secretsMgmnt = new KeyVaultCryptoActions(
                new CertificateInfo(c_encKeyName, string.Empty),
                new CertificateInfo(c_decKeyName, string.Empty),
                new CertificateInfo(c_signKeyName, string.Empty),
                new CertificateInfo(c_verifyKeyName, string.Empty),
                kvInfo,
                kvInfo);
            await secretsMgmnt.InitializeAsync();

            // Sign the data
            var signature = secretsMgmnt.Sign(Communication.Utils.ToByteArray(rawData));

            Assert.True(secretsMgmnt.Verify(Communication.Utils.ToByteArray(rawData), signature));
        }
Example #8
0
        public async void Sanity_Encryption()
        {
            var kvUri        = "http://dummyKvUri";
            var rawData      = "Some data !!!";
            var kvInfo       = new DatabaseMock(kvUri);
            var secretsMgmnt = new KeyVaultCryptoActions(
                new CertificateInfo(c_encKeyName, string.Empty),
                new CertificateInfo(c_decKeyName, string.Empty),
                new CertificateInfo(c_signKeyName, string.Empty),
                new CertificateInfo(c_verifyKeyName, string.Empty),
                kvInfo,
                kvInfo);
            await secretsMgmnt.InitializeAsync();

            var encryptedData = secretsMgmnt.Encrypt(Communication.Utils.ToByteArray(rawData));


            Assert.IsType <byte[]>(encryptedData);
        }
Example #9
0
        static void Main(string[] args)
        {
            Console.WriteLine("TransactionEngine - I do as I told");

            // Init
            var unitConverion = new Nethereum.Util.UnitConversion();

            var kv = new KeyVault(ConfigurationManager.AppSettings["AzureKeyVaultUri"],
                                  ConfigurationManager.AppSettings["applicationId"],
                                  ConfigurationManager.AppSettings["applicationSecret"]);

            var encryptionKeyName = ConfigurationManager.AppSettings["EncryptionKeyName"];
            var decryptionKeyName = ConfigurationManager.AppSettings["DecryptionKeyName"];
            var signKeyName       = ConfigurationManager.AppSettings["SignKeyName"];
            var verifyKeyName     = ConfigurationManager.AppSettings["VerifyKeyName"];

            var encryptionCertPassword = ConfigurationManager.AppSettings["EncryptionCertPassword"];
            var decryptionCertPassword = ConfigurationManager.AppSettings["DecryptionCertPassword"];
            var signCertPassword       = ConfigurationManager.AppSettings["SignCertPassword"];
            var verifyCertPassword     = ConfigurationManager.AppSettings["VerifyCertPassword"];

            var secretsMgmnt =
                new KeyVaultCryptoActions(
                    new CertificateInfo(encryptionKeyName, encryptionCertPassword),
                    new CertificateInfo(decryptionKeyName, decryptionCertPassword),
                    new CertificateInfo(signKeyName, signCertPassword),
                    new CertificateInfo(verifyKeyName, verifyCertPassword),
                    kv,
                    kv);

            secretsMgmnt.InitializeAsync().Wait();

            //var securedComm = new RabbitMQBusImpl(ConfigurationManager.AppSettings["rabbitMqUri"], secretsMgmnt, true, "securedCommExchange");
            var queueClient =
                new CloudQueueClientWrapper(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);

            var securedCommForTransactions  = new AzureQueue("transactions", queueClient, secretsMgmnt, true);
            var securedCommForNotifications = new AzureQueue("notifications", queueClient, secretsMgmnt, true);
            var taskInitTransactions        = securedCommForTransactions.InitializeAsync();
            var taskInitNotifications       = securedCommForNotifications.InitializeAsync();

            Task.WhenAll(taskInitTransactions, taskInitNotifications).Wait();

            var sqlDb = new SqlConnector(ConfigurationManager.AppSettings["SqlUserID"],
                                         ConfigurationManager.AppSettings["SqlPassword"],
                                         ConfigurationManager.AppSettings["SqlInitialCatalog"],
                                         ConfigurationManager.AppSettings["SqlDataSource"],
                                         ConfigurationManager.AppSettings["applicationId"],
                                         ConfigurationManager.AppSettings["applicationSecret"]);

            sqlDb.Initialize().Wait();
            var ethereumNodeWrapper = new EthereumAccount(sqlDb, ConfigurationManager.AppSettings["EthereumNodeUrl"]);

            // Listen on transactions requests, process them and notify the users when done
            securedCommForTransactions.DequeueAsync(
                msg =>
            {
                Console.WriteLine("Got work!");

                var data           = Communication.Utils.FromByteArray <string>(msg);
                var msgArray       = data.Split(';');
                var amount         = unitConverion.ToWei(msgArray[0]);
                var senderName     = msgArray[1];
                var reciverAddress = msgArray[2];

                try
                {
                    var transactionHash = ethereumNodeWrapper
                                          .SignTransactionAsync(senderName, reciverAddress, amount).Result;
                    var transactionResult = ethereumNodeWrapper.SendRawTransactionAsync(transactionHash).Result;
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    throw;
                }

                // Wait for miner
                Thread.Sleep(3000);

                // notify a user about his balance change
                securedCommForNotifications.EnqueueAsync(Communication.Utils.ToByteArray(reciverAddress)).Wait();
            },
                (message) => { Console.WriteLine("Verification failure, doing nothing"); },
                TimeSpan.FromSeconds(3)).Wait();
        }
Example #10
0
        private static void SendCoins(EthereumAccount ethereumAccount)
        {
            Console.WriteLine("Sender - Happy to transfer my crypto coins!");

            // Init
            var senderAddress  = ethereumAccount.GetPublicAddressAsync(c_senderId).Result;
            var reciverAddress = ethereumAccount.GetPublicAddressAsync(c_ReciverId).Result;
            var balance        = ethereumAccount.GetCurrentBalance(senderAddress).Result;

            PrintCurrentBalance(senderAddress, balance);

            var encryptionKeyName = ConfigurationManager.AppSettings["EncryptionKeyName"];
            var decryptionKeyName = ConfigurationManager.AppSettings["DecryptionKeyName"];
            var signKeyName       = ConfigurationManager.AppSettings["SignKeyName"];
            var verifyKeyName     = ConfigurationManager.AppSettings["VerifyKeyName"];

            var encryptionCertPassword = ConfigurationManager.AppSettings["EncryptionCertPassword"];
            var decryptionCertPassword = ConfigurationManager.AppSettings["DecryptionCertPassword"];
            var signCertPassword       = ConfigurationManager.AppSettings["SignCertPassword"];
            var verifyCertPassword     = ConfigurationManager.AppSettings["VerifyCertPassword"];

            var kv = new KeyVault(ConfigurationManager.AppSettings["AzureKeyVaultUri"],
                                  ConfigurationManager.AppSettings["applicationId"],
                                  ConfigurationManager.AppSettings["applicationSecret"]);
            var secretsMgmnt =
                new KeyVaultCryptoActions(
                    new CertificateInfo(encryptionKeyName, encryptionCertPassword),
                    new CertificateInfo(decryptionKeyName, decryptionCertPassword),
                    new CertificateInfo(signKeyName, signCertPassword),
                    new CertificateInfo(verifyKeyName, verifyCertPassword),
                    kv,
                    kv);

            secretsMgmnt.InitializeAsync().Wait();
            //var securedComm = new RabbitMQBusImpl(ConfigurationManager.AppSettings["rabbitMqUri"], secretsMgmnt, true, "securedCommExchange");

            var queueClient =
                new CloudQueueClientWrapper(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
            var securedComm = new AzureQueue("transactions", queueClient, secretsMgmnt, true);

            securedComm.InitializeAsync().Wait();

            // While there are sufficient funds, transfer some...
            while (balance >= 0)
            {
                var amountToSend = 0.001;
                // Message structure: {amountToSend};{senderName};{reciverAddress}
                var message = $"{amountToSend};{c_senderId};{reciverAddress}";
                securedComm.EnqueueAsync(Communication.Utils.ToByteArray(message)).Wait();

                // Sleep 1 minute
                Thread.Sleep(60000);

                var newBalance = ethereumAccount.GetCurrentBalance(senderAddress).Result;
                PrintCurrentBalance(senderAddress, newBalance);

                // Wait for mining..
                while (newBalance.Equals(balance))
                {
                    newBalance = ethereumAccount.GetCurrentBalance(senderAddress).Result;
                }

                balance = newBalance;
            }
        }
Example #11
0
        static void Main(string[] args)
        {
            // Init
            var kv = new KeyVault(ConfigurationManager.AppSettings["AzureKeyVaultUri"],
                                  ConfigurationManager.AppSettings["applicationId"],
                                  ConfigurationManager.AppSettings["applicationSecret"]);
            var sqlDb = new SqlConnector(ConfigurationManager.AppSettings["SqlUserID"],
                                         ConfigurationManager.AppSettings["SqlPassword"],
                                         ConfigurationManager.AppSettings["SqlInitialCatalog"],
                                         ConfigurationManager.AppSettings["SqlDataSource"],
                                         ConfigurationManager.AppSettings["applicationId"],
                                         ConfigurationManager.AppSettings["applicationSecret"]);

            sqlDb.Initialize().Wait();

            var ethereumAccount = new EthereumAccount(sqlDb, ConfigurationManager.AppSettings["EthereumNodeUrl"]);

            Console.WriteLine("Receiver - I just love getting new crypto coins");

            var reciverAddress = ethereumAccount.GetPublicAddressAsync(c_ReciverId).Result;

            PrintCurrentBalance(reciverAddress, ethereumAccount.GetCurrentBalance(reciverAddress).Result);

            var encryptionKeyName = ConfigurationManager.AppSettings["EncryptionKeyName"];
            var decryptionKeyName = ConfigurationManager.AppSettings["DecryptionKeyName"];
            var signKeyName       = ConfigurationManager.AppSettings["SignKeyName"];
            var verifyKeyName     = ConfigurationManager.AppSettings["VerifyKeyName"];

            var encryptionCertPassword = ConfigurationManager.AppSettings["EncryptionCertPassword"];
            var decryptionCertPassword = ConfigurationManager.AppSettings["DecryptionCertPassword"];
            var signCertPassword       = ConfigurationManager.AppSettings["SignCertPassword"];
            var verifyCertPassword     = ConfigurationManager.AppSettings["VerifyCertPassword"];

            var secretsMgmnt =
                new KeyVaultCryptoActions(
                    new CertificateInfo(encryptionKeyName, encryptionCertPassword),
                    new CertificateInfo(decryptionKeyName, decryptionCertPassword),
                    new CertificateInfo(signKeyName, signCertPassword),
                    new CertificateInfo(verifyKeyName, verifyCertPassword),
                    kv,
                    kv);

            secretsMgmnt.InitializeAsync().Wait();
            //var securedComm = new RabbitMQBusImpl(ConfigurationManager.AppSettings["rabbitMqUri"], secretsMgmnt, true, "securedCommExchange");
            var queueClient =
                new CloudQueueClientWrapper(ConfigurationManager.AppSettings["AzureStorageConnectionString"]);
            var securedComm = new AzureQueue("notifications", queueClient, secretsMgmnt, true);

            securedComm.InitializeAsync().Wait();

            // Listen on the notifications queue, check balance when a notification arrives
            var consumerTag =
                securedComm.DequeueAsync(
                    msg =>
            {
                var data = Communication.Utils.FromByteArray <string>(msg);
                if (data.Equals(reciverAddress, StringComparison.OrdinalIgnoreCase))
                {
                    Console.WriteLine("Great, Balance change!");
                    PrintCurrentBalance(reciverAddress,
                                        ethereumAccount.GetCurrentBalance(reciverAddress).Result);
                }
                else
                {
                    Console.WriteLine("Not my balance!");
                    Console.WriteLine(msg);
                }
            }, (message) => { Console.WriteLine("Verification failure, doing nothing"); },
                    TimeSpan.FromSeconds(3));

            // wait 30 minutes
            Thread.Sleep(30 * 1000 * 60);

            // switch based on the chosen queue
            //securedComm.CancelListeningOnQueue(consumerTag.Result);
            securedComm.CancelListeningOnQueue();
        }