Esempio n. 1
0
        private static void OnConnected()
        {
            if (diffieHellman != null)
            {
                diffieHellman.Dispose();
            }

            diffieHellman = new ECDiffieHellmanCng();
            byte[] servicePublicPart = diffieHellman.PublicKey.ToByteArray();
            using (MemoryStream sendStream = new MemoryStream(servicePublicPart.Length + 2))
            {
                using (BinaryWriter writer = new BinaryWriter(sendStream))
                {
                    writer.Write((ushort)servicePublicPart.Length);
                    writer.Write(servicePublicPart);
                }
                connection.SendObject <byte[]>("Handshake", sendStream.GetBuffer());
                connection.AppendIncomingPacketHandler <byte[]>("HandshakeResponse", (header, connection, bytes) =>
                {
                    connection.RemoveIncomingPacketHandler("HandshakeResponse");
                    using (MemoryStream receiveStream = new MemoryStream(bytes))
                    {
                        using (BinaryReader reader = new BinaryReader(receiveStream))
                        {
                            ushort saltLength      = reader.ReadUInt16();
                            byte[] salt            = reader.ReadBytes(saltLength);
                            ushort keyBlobLength   = reader.ReadUInt16();
                            byte[] keyBlob         = reader.ReadBytes(keyBlobLength);
                            ushort signatureLength = reader.ReadUInt16();
                            byte[] signature       = reader.ReadBytes(signatureLength);

                            using (RSACryptoServiceProvider csp = new RSACryptoServiceProvider())
                            {
                                try
                                {
                                    StringReader stringReader = new StringReader(Properties.Resources.publickey);
                                    XmlSerializer serializer  = new XmlSerializer(typeof(RSAParameters));
                                    RSAParameters rsaParams   = (RSAParameters)serializer.Deserialize(stringReader);
                                    csp.ImportParameters(rsaParams);
                                    if (!csp.VerifyData(keyBlob, new SHA512CryptoServiceProvider(), signature))
                                    {
                                        //Something is wrong here. The public blob does not match the signature. Possible MITM.
                                        connection.CloseConnection(true);
                                        connection = null;
                                    }
                                    else
                                    {
                                        //Connection was fine. Key exchange worked. Let's set the encryption up!
                                        byte[] sharedMaterial = diffieHellman.DeriveKeyMaterial(CngKey.Import(keyBlob, CngKeyBlobFormat.EccPublicBlob));
                                        using (Rfc2898DeriveBytes keyDerive = new Rfc2898DeriveBytes(sharedMaterial, salt, 1000))
                                        {
                                            encryptionKey = keyDerive.GetBytes(32); // 32 bytes = 256 bits, for AES encryption. Salt is generated per message
                                        }
                                        SetupMessageHandlers();
                                    }
                                }
                                finally
                                {
                                    csp.PersistKeyInCsp = false;
                                    if (diffieHellman != null)
                                    {
                                        diffieHellman.Dispose();
                                        diffieHellman = null;
                                    }
                                }
                            }
                        }
                    }
                });
            }
        }