public void Incoming() {
Random rand = new Random();
byte[] local_cookie = new byte[SecurityControlMessage.CookieLength];
byte[] remote_cookie = new byte[SecurityControlMessage.CookieLength];
byte[] dhe = new byte[144];
byte[] cas = new byte[120];
byte[] cert = new byte[100];
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
HashAlgorithm hash = new SHA1CryptoServiceProvider();
rand.NextBytes(local_cookie);
rand.NextBytes(remote_cookie);
rand.NextBytes(dhe);
rand.NextBytes(cas);
rand.NextBytes(cert);
MemBlock mlocal_cookie = MemBlock.Reference(local_cookie);
MemBlock mremote_cookie = MemBlock.Reference(remote_cookie);
MemBlock mdhe = MemBlock.Reference(dhe);
MemBlock mcert = MemBlock.Reference(cert);
MemBlock mcas = MemBlock.Reference(cas);
List<MemBlock> lcas = new List<MemBlock>();
for(int i = 0; i < cas.Length; i+= SecurityControlMessage.CALength) {
lcas.Add(MemBlock.Reference(mcas.Slice(i, SecurityControlMessage.CALength)));
}
int length = 4 + 4 + 4 + 2 * SecurityControlMessage.CookieLength +
4 + cas.Length + 4 + dhe.Length +
4 + cert.Length;
byte[] b = new byte[length];
int pos = 0;
NumberSerializer.WriteInt(5, b, pos);
pos += 4;
NumberSerializer.WriteInt(12345, b, pos);
pos += 4;
NumberSerializer.WriteInt((int) SecurityControlMessage.MessageType.DHEWithCertificateAndCAs, b, pos);
pos += 4;
local_cookie.CopyTo(b, pos);
pos += SecurityControlMessage.CookieLength;
remote_cookie.CopyTo(b, pos);
pos += SecurityControlMessage.CookieLength;
NumberSerializer.WriteInt(dhe.Length, b, pos);
pos += 4;
dhe.CopyTo(b, pos);
pos += dhe.Length;
NumberSerializer.WriteInt(cert.Length, b, pos);
pos += 4;
cert.CopyTo(b, pos);
pos += cert.Length;
NumberSerializer.WriteInt(cas.Length, b, pos);
pos += 4;
mcas.CopyTo(b, pos);
pos += cas.Length;
byte[] signature = rsa.SignData(b, hash);
byte[] nb = new byte[b.Length + signature.Length];
b.CopyTo(nb, 0);
signature.CopyTo(nb, b.Length);
MemBlock packet = MemBlock.Reference(nb);
// check
SecurityControlMessage scm = new SecurityControlMessage(packet);
Assert.AreEqual(5, scm.Version, "Version");
Assert.AreEqual(12345, scm.SPI, "SPI");
Assert.AreEqual(SecurityControlMessage.MessageType.DHEWithCertificateAndCAs, scm.Type, "Type");
Assert.AreEqual(mlocal_cookie, scm.LocalCookie, "LocalCookie");
Assert.AreEqual(mremote_cookie, scm.RemoteCookie, "RemoteCookie");
Assert.AreEqual(mdhe, scm.DHE, "DHE");
Assert.AreEqual(mcert, scm.Certificate, "Certificate");
int contains = 0;
foreach(MemBlock ca in scm.CAs) {
if(scm.CAs.Contains(ca)) {
contains++;
}
}
Assert.AreEqual(contains, lcas.Count, "Contains CAs");
Assert.IsTrue(scm.Verify(rsa, hash), "Signature");
Assert.AreEqual(packet, scm.Packet, "Packet");
// change a few things around and check again!
scm.Version = 0;
SecurityControlMessage scm1 = new SecurityControlMessage(scm.Packet);
scm1.Sign(rsa, hash);
Assert.AreEqual(scm1.Version, scm.Version, "Version 1");
Assert.AreEqual(scm1.SPI, scm.SPI, "SPI 1");
Assert.AreEqual(scm1.Type, scm.Type, "Type 1");
Assert.AreEqual(scm1.LocalCookie, scm.LocalCookie, "LocalCookie 1");
Assert.AreEqual(scm1.RemoteCookie, scm.RemoteCookie, "RemoteCookie 1");
Assert.AreEqual(mdhe, scm.DHE, "DHE 1");
Assert.AreEqual(mcert, scm.Certificate, "Certificate 1");
contains = 0;
foreach(MemBlock ca in scm.CAs) {
if(scm.CAs.Contains(ca)) {
contains++;
}
}
Assert.AreEqual(contains, lcas.Count, "Contains CAs 1");
Assert.IsTrue(scm1.Signature != scm.Signature, "Signature 1");
Assert.AreEqual(scm1.Packet.Slice(4, scm1.Signature.Length),
scm.Packet.Slice(4, scm.Signature.Length), "Packet 1");
}
}