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); }
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"); } } }
public void ReceivePublicKeyTransmission(Windows.COPYDATASTRUCT cds) { byte[] remotePublicKey = new byte[cds.cbData]; Marshal.Copy(cds.lpData, remotePublicKey, 0, cds.cbData); SetRemotePublicKey(remotePublicKey); }
public void TransmitEncryptedMasterKey(Windows.MainWindow localWindow) { Windows.COPYDATASTRUCT cds = GetEncryptedMasterKeyTransmission(); HwndSource source = PresentationSource.FromVisual(localWindow) as HwndSource; Windows.MainWindow.SendMessage(this.RemoteWindow, Windows.MainWindow.WM_COPYDATA, source.Handle, ref cds); Marshal.FreeHGlobal(cds.lpData); }
Windows.COPYDATASTRUCT GetPublicKeyTransmission(bool isRequest) { Windows.COPYDATASTRUCT cds = new Windows.COPYDATASTRUCT(); cds.cbData = LocalPublicKey.Bytes.Length; cds.lpData = Marshal.AllocHGlobal(cds.cbData); Marshal.Copy(LocalPublicKey.Bytes, 0, cds.lpData, LocalPublicKey.Bytes.Length); if (isRequest) { cds.dwData = new IntPtr(10); } else { cds.dwData = new IntPtr(12); } // caller needs to Marshal.FreeHGlobal(cds.lpData); return(cds); }
public static bool ReceiveTransmission(Windows.MainWindow window, IntPtr remoteWindow, System.Diagnostics.Process remoteProcess, Windows.COPYDATASTRUCT cds) { Session session = Sessions.FirstOrDefault(q => q.RemoteWindow == remoteWindow && q.Process.Id == remoteProcess.Id); if (session == null) { session = new Session(remoteWindow, remoteProcess); Sessions.Add(session); } switch ((long)cds.dwData) { case 10: if (!App.Settings.HasPasswordMasterKey) { return(false); } session.ReceivePublicKeyTransmission(cds); session.TransmitPublicKey(window, false); session.TransmitEncryptedMasterKey(window); return(true); case 11: session.ReceiveEncryptedMasterKeyTransmission(cds); return(true); case 12: session.ReceivePublicKeyTransmission(cds); return(true); } return(false); }