Esempio n. 1
0
        public void DbxUploadCommandTest010()
        {
            if (Directory.Exists($"{_location}/data/client/test-user/dbx"))
            {
                Directory.Delete($"{_location}/data/client/test-user/dbx", true);
            }

            using (var stream1 = new MemoryStream())
                using (var stream2 = new MemoryStream())
                    using (var channel1 = new ServiceChannel())
                        using (var channel2 = new ServiceChannel())
                        {
                            byte[] dbxData = Random.Get(256);
                            channel2.SetReadStream(stream1, canDispose: false);
                            channel2.SetWriteStream(stream2, canDispose: false);
                            channel2.Write(dbxData);

                            stream2.Position = 0;
                            channel1.SetReadStream(stream2, canDispose: false);
                            channel1.SetWriteStream(stream1, canDispose: false);

                            Context ctx = new Context();
                            ctx.ClientIP = IPAddress.None;
                            ctx.Channel  = channel1;

                            ctx.Query = new QueryMessage {
                                Command = "dbx-Upload"
                            };
                            ctx.Query.Params["username"]      = "******";
                            ctx.Query.Params["uuid"]          = "safevault";
                            ctx.Query.Params["password"]      = "******";
                            ctx.Query.Params["md5"]           = Hash.MD5(dbxData);
                            ctx.Query.Params["last-modified"] = "2017-01-01 12:00:00Z";

                            Command.Process(ctx);

                            stream1.Position = 0;
                            channel2.CipherLib["rsa-private"] = RsaCipher
                                                                .LoadFromPEM($"{_location}/data/client/test-user/cer.pem", $"{_location}/data/client/test-user/cer.pem.key");

                            var response = channel2.ReadObject <ResponseMessage>();
                            Assert.AreEqual(200, response.StatusCode);
                            Assert.AreEqual("OK", response.Header["data"]);

                            var data = File.ReadAllBytes($"{_location}/data/client/test-user/dbx/safevault.dbx");
                            Assert.AreEqual(dbxData, data);

                            var fileInfo = new FileInfo($"{_location}/data/client/test-user/dbx/safevault.dbx");
                            Assert.AreEqual(fileInfo.CreationTime, DateTime.Parse(ctx.Query.Params["last-modified"]));
                        }
        }
Esempio n. 2
0
        public void Encrypt(bool reset = false)
        {
            if (reset)
            {
                WriteStream.Cipher = new XorCipher();
                WriteCipher        = null;
            }

            if (CipherLib.ContainsKey("rsa-public"))
            {
                WriteUInt16((ushort)EncryptedStreamControl.EncryptRSA);
                WriteCipher = CipherLib["rsa-public"].Clone();
            }

            byte[] salt = Random.Get(32);
            WriteUInt16((ushort)EncryptedStreamControl.EncryptAES);
            WriteUInt16((ushort)salt.Length);
            WriteData(salt);
            CipherLib["aes"] = new Aes256Cipher(salt);
            WriteCipher      = CipherLib["aes"].Clone();


            if (!CipherLib.ContainsKey("xor"))
            {
                byte[] passw = Random.Get(2048 / 8);
                WriteUInt16((ushort)EncryptedStreamControl.EncryptXOR);
                WriteUInt16((ushort)passw.Length);
                WriteData(passw);
                CipherLib["xor"] = new XorCipher(passw);
            }
            else
            {
                WriteUInt16((ushort)EncryptedStreamControl.EncryptXOR);
                WriteUInt16((ushort)0);
            }
            WriteStream.Cipher = CipherLib["xor"].Clone();
        }
Esempio n. 3
0
        private byte[] Create(KeyProviderQueryContext ctx)
        {
            var vaultConf = new SafeVaultConf(ctx.DatabaseIOInfo);

            var vaultConnectionForm = new VaultConnectionConfigForm();

            vaultConnectionForm.InitEx(vaultConf);

            if (UIUtil.ShowDialogAndDestroy(vaultConnectionForm) != DialogResult.OK)
            {
                return(null);
            }

            VaultKeyCreateForm createForm = new VaultKeyCreateForm();

            createForm.InitEx(vaultConf, ctx);
            if (UIUtil.ShowDialogAndDestroy(createForm) != DialogResult.OK)
            {
                return(null);
            }

            vaultConf.Type    = PROVIDER_TYPE;
            vaultConf.Version = PROVIDER_VERSION;

            var masterKey = Encoding.UTF8.GetBytes(vaultConf.DatabaseKeyA);

            var keyLen = (masterKey.Length > 254) ? masterKey.Length : 254;

            var keyA = new byte[keyLen + 2];

            Array.Copy(BitConverter.GetBytes((ushort)masterKey.Length), keyA, 2);
            Array.Copy(masterKey, 0, keyA, 2, masterKey.Length);

            var keyB = Random.Get(keyA.Length);

            for (int i = 0; i < keyB.Length; i++)
            {
                keyA[i] ^= keyB[i];
            }

            var salt = Random.Get(64);

            using (var aes = new Aes256Cipher())
            {
                aes.SetPassPhrase(salt);
                keyA = aes.Encrypt(keyA);
                keyB = aes.Encrypt(keyB);
            }

            using (var rsa = RsaCipher.LoadFromX509Store(vaultConf.ClientCertificateName))
            {
                salt = rsa.Encrypt(salt);
            }

            vaultConf.Salt         = Convert.ToBase64String(salt);
            vaultConf.DatabaseKeyA = Convert.ToBase64String(keyA);
            vaultConf.VaultKeyname = Guid.NewGuid().ToString();
            var databaseKeyB = Convert.ToBase64String(keyB);

            VaultKeyPromptForm promptForm = new VaultKeyPromptForm();

            promptForm.InitEx("Enter SafeVault Password", "Save KeyB to SafeVault", (oneTimePassword) => {
                string status = "";
                var query     = new SafeVaultWebClient(vaultConf);
                try
                {
                    status = Async.Invoke(() => query.SetDbxKey(vaultConf.VaultKeyname, databaseKeyB, oneTimePassword));
                    if (status == "OK")
                    {
                        return(true);
                    }

                    MessageService.ShowWarning(
                        query.Utc != null ? "DateTime: " + DateTime.Parse(query.Utc).ToLocalTime() : "",
                        status);
                }
                catch (CryptographicException ex)
                {
                    MessageService.ShowWarning(
                        query.Utc != null ? "DateTime: " + DateTime.Parse(query.Utc).ToLocalTime() : "",
                        ex.Message);
                }
                return(false);
            });

            if (UIUtil.ShowDialogAndDestroy(promptForm) != DialogResult.OK)
            {
                return(null);
            }

            try
            {
                vaultConf.Save();
            }
            catch (Exception e)
            {
                MessageService.ShowWarning(e.Message);
                return(null);
            }

            return(masterKey);
        }