protected void HandlePunch(IPEndPoint address, BitStream stream) { var firstNonce = new Nonce(); firstNonce.Read(stream); Console.WriteLine("Received punch packet from {0} - {1}", address, Convert.ToBase64String(firstNonce.Data)); var found = false; NetConnection conn = null; foreach (var pair in PendingConnections) { conn = pair.Value; var cParams = conn.GetConnectionParameters(); if (conn.ConnectionState != NetConnectionState.SendingPunchPackets) { continue; } if ((cParams.IsInitiator && firstNonce != cParams.ServerNonce) || (!cParams.IsInitiator && firstNonce != cParams.Nonce)) { continue; } if (cParams.PossibleAddresses.Contains(address)) { if (cParams.IsInitiator) { found = true; break; } continue; } var cont = cParams.PossibleAddresses.Any(ep => ep.Address.Equals(address.Address) && ep.AddressFamily == address.AddressFamily); if (!cont) { continue; } if (cParams.PossibleAddresses.Count < 5) { cParams.PossibleAddresses.Add(address); } if (cParams.IsInitiator) { found = true; break; } } if (!found) { return; } var cParam = conn.GetConnectionParameters(); var theCipher = new SymmetricCipher(cParam.ArrangedSecret); if (stream.DecryptAndCheckHash(NetConnection.MessageSignatureBytes, stream.GetBytePosition(), theCipher)) { return; } var nextNonce = new Nonce(); nextNonce.Read(stream); if (nextNonce != cParam.Nonce) { return; } if (stream.ReadFlag()) { if (stream.ReadFlag()) { cParam.Certificate = new Certificate(stream); if (!cParam.Certificate.IsValid() || !conn.ValidateCertificate(cParam.Certificate, true)) { return; } cParam.PublicKey = cParam.Certificate.GetPublicKey(); } else { cParam.PublicKey = new AsymmetricKey(stream); if (!cParam.PublicKey.IsValid() || !conn.ValidatePublicKey(cParam.PublicKey, true)) { return; } } if (PrivateKey == null || PrivateKey.GetKeySize() != cParam.PublicKey.GetKeySize()) { cParam.PrivateKey = new AsymmetricKey(cParam.PublicKey.GetKeySize()); } else { cParam.PrivateKey = PrivateKey; } cParam.SharedSecret = cParam.PrivateKey.ComputeSharedSecretKey(cParam.PublicKey); RandomUtil.Read(cParam.SymmetricKey, SymmetricCipher.KeySize); cParam.UsingCrypto = true; } }
protected void HandleConnectChallengeResponse(IPEndPoint address, BitStream stream) { var conn = FindPendingConnection(address); if (conn == null || conn.ConnectionState != NetConnectionState.AwaitingChallengeResponse) { return; } var theNonce = new Nonce(); theNonce.Read(stream); var cParams = conn.GetConnectionParameters(); if (theNonce != cParams.Nonce) { return; } stream.Read(out cParams.ClientIdentity); cParams.ServerNonce.Read(stream); stream.Read(out cParams.PuzzleDifficulty); if (cParams.PuzzleDifficulty > ClientPuzzleManager.MaxPuzzleDifficulty) { return; } if (stream.ReadFlag()) { if (stream.ReadFlag()) { cParams.Certificate = new Certificate(stream); if (!cParams.Certificate.IsValid() || !conn.ValidateCertificate(cParams.Certificate, true)) { return; } cParams.PublicKey = cParams.Certificate.GetPublicKey(); } else { cParams.PublicKey = new AsymmetricKey(stream); if (!cParams.PublicKey.IsValid() || !conn.ValidatePublicKey(cParams.PublicKey, true)) { return; } } if (PrivateKey == null || PrivateKey.GetKeySize() != cParams.PublicKey.GetKeySize()) { cParams.PrivateKey = new AsymmetricKey(cParams.PublicKey.GetKeySize()); } else { cParams.PrivateKey = PrivateKey; } cParams.SharedSecret = cParams.PrivateKey.ComputeSharedSecretKey(cParams.PublicKey); RandomUtil.Read(cParams.SymmetricKey, SymmetricCipher.KeySize); cParams.UsingCrypto = true; } conn.ConnectionState = NetConnectionState.ComputingPuzzleSolution; conn.ConnectSendCount = 0; cParams.PuzzleSolution = 0; conn.ConnectLastSendTime = CurrentTime; ContinuePuzzleSolution(conn); }