Debugging shows that the client disconnects after my proxy sends the SKE packet. I did not alter my code so I checked the packet that the server sends me initially and was surprised to see that the server's public key (SPK) is only 126 bytes (which translates to a 63-byte BigNumber).
Now the client's public key is 128 bytes, as expected, so I'd like to know if the SPK was always 126 bytes. Strangely enough though when my proxy does send a SKE packet which is 2 bytes longer than what I received to the client, it disconnects all the same.
Code:
private void HandleServerKeyExchangePacket(byte[] packet)
{
int keyOffset;
using (BinaryReader br = new BinaryReader(new MemoryStream(packet)))
{
byte[] padding = br.ReadBytes(11);
int realSize = br.ReadInt32(); // size - padding
byte[] junk = br.ReadBytes(br.ReadInt32());
this.ClientIV = br.ReadBytes(br.ReadInt32());
this.ServerIV = br.ReadBytes(br.ReadInt32());
this.P = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt32()));
this.G = Encoding.ASCII.GetString(br.ReadBytes(br.ReadInt32()));
var keyLength = br.ReadInt32();
keyOffset = (int)(br.BaseStream.Position);
this.ServerPublicKey = Encoding.ASCII.GetString(br.ReadBytes(keyLength));
var seal = Encoding.UTF7.GetString(br.ReadBytes(8));
}
this.ServerProxyExchange.P = BigNumber.FromHexString(P);
this.ServerProxyExchange.G = BigNumber.FromHexString(G);
this.ServerProxyExchange.GenerateKeys(); // generate Proxy public Key (PPK) and Secret Key (PSK)
this.ServerProxySharedKey = ServerProxyExchange.ComputeKey(BigNumber.FromHexString(this.ServerPublicKey));
var ppk = Encoding.ASCII.GetBytes(this.ProxyPublicKey);
Array.Copy(ppk, 0, packet, keyOffset, ppk.Length);
packet = this.ServerCrypto.Encrypt(packet);
this.ServerCrypto.SetKey(this.ServerProxySharedKey);
this.ServerCrypto.EncryptIV = this.ServerIV;
this.ServerCrypto.DecryptIV = this.ClientIV;
this.ClientState.Socket.Send(packet);
}






