public async Task ValidateHandshake_Test() { var remoteKeyPair = _peerKeyProvider.AuthorizedKey; var handshake = CreateHandshake(remoteKeyPair); var validationResult = await _handshakeProvider.ValidateHandshakeAsync(handshake); validationResult.ShouldBe(HandshakeValidationResult.Ok); handshake = CreateHandshake(remoteKeyPair); var unauthorizedKeyPair = CryptoHelper.GenerateKeyPair(); handshake.HandshakeData.Pubkey = ByteString.CopyFrom(unauthorizedKeyPair.PublicKey); validationResult = await _handshakeProvider.ValidateHandshakeAsync(handshake); validationResult.ShouldBe(HandshakeValidationResult.Unauthorized); handshake = CreateHandshake(remoteKeyPair); handshake.HandshakeData.ChainId = 1234; validationResult = await _handshakeProvider.ValidateHandshakeAsync(handshake); validationResult.ShouldBe(HandshakeValidationResult.InvalidChainId); handshake = CreateHandshake(remoteKeyPair); handshake.HandshakeData.Version = 0; validationResult = await _handshakeProvider.ValidateHandshakeAsync(handshake); validationResult.ShouldBe(HandshakeValidationResult.InvalidVersion); handshake = CreateHandshake(remoteKeyPair); handshake.HandshakeData.Time = TimestampHelper.GetUtcNow().AddMilliseconds(-(NetworkConstants.HandshakeTimeout + 100)); validationResult = await _handshakeProvider.ValidateHandshakeAsync(handshake); validationResult.ShouldBe(HandshakeValidationResult.HandshakeTimeout); handshake = CreateHandshake(remoteKeyPair); var maliciousPeer = CryptoHelper.GenerateKeyPair(); var signature = CryptoHelper.SignWithPrivateKey(maliciousPeer.PrivateKey, Hash .FromMessage(handshake.HandshakeData) .ToByteArray()); handshake.Signature = ByteString.CopyFrom(signature); validationResult = await _handshakeProvider.ValidateHandshakeAsync(handshake); validationResult.ShouldBe(HandshakeValidationResult.InvalidSignature); var localHandshake = await _handshakeProvider.GetHandshakeAsync(); validationResult = await _handshakeProvider.ValidateHandshakeAsync(localHandshake); validationResult.ShouldBe(HandshakeValidationResult.SelfConnection); }
/// <summary> /// Given an IP address, will create a handshake to the distant node for /// further communications. /// </summary> /// <returns>The created peer</returns> public async Task <GrpcPeer> DialPeerAsync(DnsEndPoint remoteEndpoint) { var client = await CreateClientAsync(remoteEndpoint); if (client == null) { return(null); } var handshake = await _handshakeProvider.GetHandshakeAsync(); var handshakeReply = await CallDoHandshakeAsync(client, remoteEndpoint, handshake); // verify handshake if (handshakeReply.Error != HandshakeError.HandshakeOk) { Logger.LogWarning($"Handshake error: {remoteEndpoint} {handshakeReply.Error}."); await client.Channel.ShutdownAsync(); return(null); } if (await _handshakeProvider.ValidateHandshakeAsync(handshakeReply.Handshake) != HandshakeValidationResult.Ok) { Logger.LogWarning($"Connect error: {remoteEndpoint} {handshakeReply}."); await client.Channel.ShutdownAsync(); return(null); } var peer = new GrpcPeer(client, remoteEndpoint, new PeerConnectionInfo { Pubkey = handshakeReply.Handshake.HandshakeData.Pubkey.ToHex(), ConnectionTime = TimestampHelper.GetUtcNow(), ProtocolVersion = handshakeReply.Handshake.HandshakeData.Version, SessionId = handshakeReply.Handshake.SessionId.ToByteArray(), IsInbound = false }); peer.UpdateLastReceivedHandshake(handshakeReply.Handshake); peer.InboundSessionId = handshake.SessionId.ToByteArray(); peer.UpdateLastSentHandshake(handshake); return(peer); }
public async Task <HandshakeReply> DoHandshakeAsync(DnsEndPoint endpoint, Handshake handshake) { // validate the handshake (signature, chain id...) var handshakeValidationResult = await _handshakeProvider.ValidateHandshakeAsync(handshake); if (handshakeValidationResult != HandshakeValidationResult.Ok) { var handshakeError = GetHandshakeError(handshakeValidationResult); return(new HandshakeReply { Error = handshakeError }); } var pubkey = handshake.HandshakeData.Pubkey.ToHex(); // remove any remaining connection to the peer (before the check // that we have room for more connections) var currentPeer = _peerPool.FindPeerByPublicKey(pubkey); if (currentPeer != null) { _peerPool.RemovePeer(pubkey); await currentPeer.DisconnectAsync(false); } try { // mark the (IP; pubkey) pair as currently handshaking if (!_peerPool.AddHandshakingPeer(endpoint.Host, pubkey)) { return new HandshakeReply { Error = HandshakeError.ConnectionRefused } } ; // create the connection to the peer var peerEndpoint = new AElfPeerEndpoint(endpoint.Host, handshake.HandshakeData.ListeningPort); var grpcPeer = await _peerDialer.DialBackPeerAsync(peerEndpoint, handshake); // add the new peer to the pool if (!_peerPool.TryAddPeer(grpcPeer)) { Logger.LogWarning($"Stopping connection, peer already in the pool {grpcPeer.Info.Pubkey}."); await grpcPeer.DisconnectAsync(false); return(new HandshakeReply { Error = HandshakeError.RepeatedConnection }); } Logger.LogDebug($"Added to pool {grpcPeer.RemoteEndpoint} - {grpcPeer.Info.Pubkey}."); // send back our handshake var replyHandshake = await _handshakeProvider.GetHandshakeAsync(); grpcPeer.InboundSessionId = replyHandshake.SessionId.ToByteArray(); grpcPeer.UpdateLastSentHandshake(replyHandshake); return(new HandshakeReply { Handshake = replyHandshake, Error = HandshakeError.HandshakeOk }); } finally { // remove the handshaking mark (IP; pubkey) _peerPool.RemoveHandshakingPeer(endpoint.Host, pubkey); } }