Exemple #1
0
            Windows.COPYDATASTRUCT GetEncryptedMasterKeyTransmission()
            {
                byte[] encryptedMessage;
                byte[] iv;

                using (SecureBytesWrapper sbwKey = new SecureBytesWrapper(App.Settings.PasswordMasterKey, true))
                {
                    Encrypt(sbwKey.Bytes, out encryptedMessage, out iv);
                }
                Windows.COPYDATASTRUCT cds = new Windows.COPYDATASTRUCT();
                cds.cbData = sizeof(int) + iv.Length + encryptedMessage.Length;

                byte[] combinedMessage = new byte[cds.cbData];
                byte[] lengthBytes     = BitConverter.GetBytes(iv.Length);

                Buffer.BlockCopy(lengthBytes, 0, combinedMessage, 0, lengthBytes.Length);
                Buffer.BlockCopy(iv, 0, combinedMessage, lengthBytes.Length, iv.Length);
                Buffer.BlockCopy(encryptedMessage, 0, combinedMessage, lengthBytes.Length + iv.Length, encryptedMessage.Length);

                cds.lpData = Marshal.AllocHGlobal(cds.cbData);
                Marshal.Copy(combinedMessage, 0, cds.lpData, combinedMessage.Length);
                cds.dwData = new IntPtr(11);
                // caller needs to Marshal.FreeHGlobal(cds.lpData);
                return(cds);
            }
Exemple #2
0
            public void ReceiveEncryptedMasterKeyTransmission(Windows.COPYDATASTRUCT cds)
            {
                byte[] combinedMessage = new byte[cds.cbData];
                Marshal.Copy(cds.lpData, combinedMessage, 0, cds.cbData);

                int ivLength = BitConverter.ToInt32(combinedMessage, 0);

                // validate length...
                if (ivLength > cds.cbData)
                {
                    // throw exception .. ?
                    throw new ApplicationException("Master Key transmission failed: Initialization Vector received incorrectly");
                }
                byte[] iv = new byte[ivLength];
                Buffer.BlockCopy(combinedMessage, sizeof(int), iv, 0, ivLength);

                byte[] encryptedMessage = new byte[cds.cbData - (ivLength + sizeof(int))];
                Buffer.BlockCopy(combinedMessage, sizeof(int) + ivLength, encryptedMessage, 0, encryptedMessage.Length);

                using (SecureBytesWrapper sbw = new SecureBytesWrapper())
                {
                    Decrypt(encryptedMessage, iv, sbw);
                    if (!App.Settings.TryPasswordMasterKey(sbw.Bytes))
                    {
                        throw new ApplicationException("Master Key transmission failed: Master Key received incorrectly");
                    }
                }
            }
Exemple #3
0
            public void Decrypt(byte[] encryptedMessage, byte[] iv, SecureBytesWrapper decryptedMessage)
            {
                using (Aes aes = new AesCryptoServiceProvider())
                {
                    aes.Key = LocalPrivateKey.Bytes;
                    aes.IV  = iv;

                    // Encrypt the message
                    using (MemoryStream ciphertext = new MemoryStream())
                    {
                        using (CryptoStream cs = new CryptoStream(ciphertext, aes.CreateDecryptor(), CryptoStreamMode.Write))
                        {
                            cs.Write(encryptedMessage, 0, encryptedMessage.Length);
                            cs.Close();
                            decryptedMessage.CopyBytes(ciphertext.ToArray());
                        }
                    }
                }
            }
Exemple #4
0
            public Session(IntPtr window, System.Diagnostics.Process process)
            {
                RemoteWindow = window;
                Process      = process;

                process.Exited += process_Exited;

                DH = new ECDiffieHellmanCng();

                DH.KeyDerivationFunction = ECDiffieHellmanKeyDerivationFunction.Hash;
                DH.HashAlgorithm         = CngAlgorithm.Sha256;
                LocalPublicKey           = new SecureBytesWrapper()
                {
                    Bytes = DH.PublicKey.ToByteArray()
                };

                // this is generated using the remote public key, which we dont have yet
                LocalPrivateKey = new SecureBytesWrapper();
            }
        /// <summary>
        /// Initialize with byte array via the SecureStringWrapper, but possibly convert from hex string...
        /// </summary>
        /// <param name="ssw"></param>
        /// <param name="convertFromHex"></param>
        public SecureBytesWrapper(SecureStringWrapper ssw, bool convertFromHex)
        {
            if (!convertFromHex)
            {
                CopyBytes(ssw.ToByteArray());
                return;
            }
            if (_hexTable == null)
            {
                _hexTable      = new byte[256];
                _hexTable['0'] = 0;
                _hexTable['1'] = 1;
                _hexTable['2'] = 2;
                _hexTable['3'] = 3;
                _hexTable['4'] = 4;
                _hexTable['5'] = 5;
                _hexTable['6'] = 6;
                _hexTable['7'] = 7;
                _hexTable['8'] = 8;
                _hexTable['9'] = 9;
                _hexTable['a'] = 10;
                _hexTable['b'] = 11;
                _hexTable['c'] = 12;
                _hexTable['d'] = 13;
                _hexTable['e'] = 14;
                _hexTable['f'] = 15;
            }

            using (SecureBytesWrapper temp = new SecureBytesWrapper(ssw, false))
            {
                _Bytes = new byte[temp.Bytes.Length / 2];
                for (int i = 0; i < temp.Bytes.Length; i += 2)
                {
                    byte b1 = temp.Bytes[i];
                    byte b2 = temp.Bytes[i + 1];
                    _Bytes[i / 2] = (byte)((_hexTable[b1] * 16) + _hexTable[b2]);
                }
            }
        }