Server Key Exchange Packet (SKE)

01/11/2011 01:10 Belth#1
Something bizarre has happened. My private proxy which was working perfectly has developed a bug seemingly overnight.

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);
}
01/11/2011 03:05 shitboi#2
always 128 from what i see.
01/11/2011 04:06 gabrola#3
TQ sometimes randomly does that on some servers, tell me which server you are trying this on.
01/11/2011 05:17 Belth#4
Quote:
Originally Posted by gabrola View Post
TQ sometimes randomly does that on some servers, tell me which server you are trying this on.
Virgo

Edit:

Ok you were right it is happening on virgo and I fixed the code to create a new packet instead of just copying over the key. The randomness of the event just got to me at first.