Esempio n. 1
0
        public void SetUp()
        {
            _trueCryptoRandom = new CryptoRandom();

            // WARN: order reflects the internal implementation of the service (tests may fail after any refactoring)
            _testRandom = new TestRandom(
                NetTestVectors.NonceA,
                NetTestVectors.EphemeralKeyA.KeyBytes,
                _trueCryptoRandom.GenerateRandomBytes(100),
                NetTestVectors.NonceB,
                NetTestVectors.EphemeralKeyB.KeyBytes,
                _trueCryptoRandom.GenerateRandomBytes(100));

            _messageSerializationService = new MessageSerializationService();
            _messageSerializationService.Register(new AuthMessageSerializer());
            _messageSerializationService.Register(new AuthEip8MessageSerializer(new Eip8MessagePad(_testRandom)));
            _messageSerializationService.Register(new AckMessageSerializer());
            _messageSerializationService.Register(new AckEip8MessageSerializer(new Eip8MessagePad(_testRandom)));

            _eciesCipher = new EciesCipher(_trueCryptoRandom); // TODO: provide a separate test random with specific IV and epehemeral key for testing

            _initiatorService = new HandshakeService(_messageSerializationService, _eciesCipher, _testRandom, _ecdsa, NetTestVectors.StaticKeyA, NullLogManager.Instance);
            _recipientService = new HandshakeService(_messageSerializationService, _eciesCipher, _testRandom, _ecdsa, NetTestVectors.StaticKeyB, NullLogManager.Instance);

            _initiatorHandshake = new EncryptionHandshake();
            _recipientHandshake = new EncryptionHandshake();

            _auth = null;
            _ack  = null;
        }
        public void SetUp()
        {
            _trueCryptoRandom = new CryptoRandom();

            _testRandom = new BenchmarkTestRandom(_trueCryptoRandom, 6);

            _messageSerializationService = new MessageSerializationService();
            _messageSerializationService.Register(new AuthMessageSerializer());
            _messageSerializationService.Register(new AuthEip8MessageSerializer(new Eip8MessagePad(_testRandom)));
            _messageSerializationService.Register(new AckMessageSerializer());
            _messageSerializationService.Register(new AckEip8MessageSerializer(new Eip8MessagePad(_testRandom)));

            _eciesCipher = new EciesCipher(_trueCryptoRandom); // TODO: provide a separate test random with specific IV and ephemeral key for testing

            _initiatorService = new HandshakeService(_messageSerializationService, _eciesCipher, _testRandom, _ecdsa, NetTestVectors.StaticKeyA, LimboLogs.Instance);
            _recipientService = new HandshakeService(_messageSerializationService, _eciesCipher, _testRandom, _ecdsa, NetTestVectors.StaticKeyB, LimboLogs.Instance);
        }
Esempio n. 3
0
        public static (EncryptionSecrets A, EncryptionSecrets B) GetSecretsPair()
        {
            EncryptionHandshake handshakeA = new EncryptionHandshake();

            handshakeA.InitiatorNonce           = TestItem.KeccakA.Bytes;
            handshakeA.RecipientNonce           = TestItem.KeccakB.Bytes;
            handshakeA.EphemeralPrivateKey      = TestItem.PrivateKeyA;
            handshakeA.RemoteEphemeralPublicKey = TestItem.PrivateKeyB.PublicKey;
            handshakeA.AckPacket  = new Packet(new byte[128]);
            handshakeA.AuthPacket = new Packet(new byte[128]);

            EncryptionHandshake handshakeB = new EncryptionHandshake();

            handshakeB.InitiatorNonce           = TestItem.KeccakA.Bytes;
            handshakeB.RecipientNonce           = TestItem.KeccakB.Bytes;
            handshakeB.EphemeralPrivateKey      = TestItem.PrivateKeyB;
            handshakeB.RemoteEphemeralPublicKey = TestItem.PrivateKeyA.PublicKey;
            handshakeB.AckPacket  = new Packet(new byte[128]);
            handshakeB.AuthPacket = new Packet(new byte[128]);

            HandshakeService.SetSecrets(handshakeA, HandshakeRole.Initiator);
            HandshakeService.SetSecrets(handshakeB, HandshakeRole.Recipient);

            Assert.AreEqual(handshakeA.Secrets.AesSecret, handshakeB.Secrets.AesSecret, "aes");
            Assert.AreEqual(handshakeA.Secrets.MacSecret, handshakeB.Secrets.MacSecret, "mac");

            KeccakDigest aIngress = handshakeA.Secrets.IngressMac.Copy() as KeccakDigest;
            KeccakDigest bIngress = handshakeB.Secrets.IngressMac.Copy() as KeccakDigest;
            KeccakDigest aEgress  = handshakeA.Secrets.EgressMac.Copy() as KeccakDigest;
            KeccakDigest bEgress  = handshakeB.Secrets.EgressMac.Copy() as KeccakDigest;

            byte[] aIngressFinal = new byte[32];
            byte[] bIngressFinal = new byte[32];
            byte[] aEgressFinal  = new byte[32];
            byte[] bEgressFinal  = new byte[32];

            aIngress.DoFinal(aIngressFinal, 0);
            bIngress.DoFinal(bIngressFinal, 0);
            aEgress.DoFinal(aEgressFinal, 0);
            bEgress.DoFinal(bEgressFinal, 0);

            Assert.AreEqual(aIngressFinal.ToHexString(), bEgressFinal.ToHexString());
            Assert.AreEqual(aEgressFinal.ToHexString(), bIngressFinal.ToHexString());

            return(handshakeA.Secrets, handshakeB.Secrets);
        }
Esempio n. 4
0
        private void Connect()
        {
            VerifyStatus(HostStatus.WatingToConnect);
            VerifyHostLibrary();
            VerifyJavaRuntime();

            _status = HostStatus.WatingToHandshaking;
            _event.Reset();

            try
            {
                _handshakeServer = new Server
                {
                    Ports =
                    {
                        new ServerPort("localhost", 0, ServerCredentials.Insecure)
                    },
                    Services =
                    {
                        HandshakeService.BindService(new HandshakeRpc(HostProcessOnAck))
                    }
                };

                _handshakeServer.Start();

                _status = HostStatus.Handshaking;

                _handshakeTimeoutTokenSource = new CancellationTokenSource();
                _handshakeTimeoutTokenSource.CancelAfter(TimeSpan.FromSeconds(5));

                _handshakeTimeoutTokenRegistration = _handshakeTimeoutTokenSource.Token.Register(HandshakeTimeout);

                var boundPort = _handshakeServer.Ports.First().BoundPort;
                SpawnHostProcess(boundPort);
            }
            catch (Exception)
            {
                _handshakeServer?.KillAsync().Wait();
                _handshakeServer = null;

                throw new PhoenixSqlHostException("Host handshake failed.");
            }
        }
        public void SetUp()
        {
            _trueCryptoRandom = new CryptoRandom();

            _testRandom = new TestRandom();

            _messageSerializationService = new MessageSerializationService();
            _messageSerializationService.Register(new AuthMessageSerializer());
            _messageSerializationService.Register(new AuthEip8MessageSerializer(new Eip8MessagePad(_testRandom)));
            _messageSerializationService.Register(new AckMessageSerializer());
            _messageSerializationService.Register(new AckEip8MessageSerializer(new Eip8MessagePad(_testRandom)));

            _eciesCipher = new EciesCipher(_trueCryptoRandom); // TODO: provide a separate test random with specific IV and epehemeral key for testing

            _initiatorService = new HandshakeService(_messageSerializationService, _eciesCipher, _testRandom, _ecdsa, NetTestVectors.StaticKeyA, NullLogManager.Instance);
            _recipientService = new HandshakeService(_messageSerializationService, _eciesCipher, _testRandom, _ecdsa, NetTestVectors.StaticKeyB, NullLogManager.Instance);

            _initiatorHandshake = new EncryptionHandshake();
            _recipientHandshake = new EncryptionHandshake();

            _auth = null;
            _ack  = null;
        }
Esempio n. 6
0
        private async Task InitPeer()
        {
            if (_ctx.DbProvider == null)
            {
                throw new StepDependencyException(nameof(_ctx.DbProvider));
            }
            if (_ctx.BlockTree == null)
            {
                throw new StepDependencyException(nameof(_ctx.BlockTree));
            }
            if (_ctx.ReceiptStorage == null)
            {
                throw new StepDependencyException(nameof(_ctx.ReceiptStorage));
            }
            if (_ctx.BlockValidator == null)
            {
                throw new StepDependencyException(nameof(_ctx.BlockValidator));
            }
            if (_ctx.SyncPeerPool == null)
            {
                throw new StepDependencyException(nameof(_ctx.SyncPeerPool));
            }
            if (_ctx.Synchronizer == null)
            {
                throw new StepDependencyException(nameof(_ctx.Synchronizer));
            }
            if (_ctx.Enode == null)
            {
                throw new StepDependencyException(nameof(_ctx.Enode));
            }
            if (_ctx.NodeKey == null)
            {
                throw new StepDependencyException(nameof(_ctx.NodeKey));
            }
            if (_ctx.MainBlockProcessor == null)
            {
                throw new StepDependencyException(nameof(_ctx.MainBlockProcessor));
            }
            if (_ctx.NodeStatsManager == null)
            {
                throw new StepDependencyException(nameof(_ctx.NodeStatsManager));
            }
            if (_ctx.KeyStore == null)
            {
                throw new StepDependencyException(nameof(_ctx.KeyStore));
            }
            if (_ctx.RpcModuleProvider == null)
            {
                throw new StepDependencyException(nameof(_ctx.RpcModuleProvider));
            }
            if (_ctx.Wallet == null)
            {
                throw new StepDependencyException(nameof(_ctx.Wallet));
            }
            if (_ctx.EthereumEcdsa == null)
            {
                throw new StepDependencyException(nameof(_ctx.EthereumEcdsa));
            }
            if (_ctx.SpecProvider == null)
            {
                throw new StepDependencyException(nameof(_ctx.SpecProvider));
            }
            if (_ctx.TxPool == null)
            {
                throw new StepDependencyException(nameof(_ctx.TxPool));
            }
            if (_ctx.EthereumJsonSerializer == null)
            {
                throw new StepDependencyException(nameof(_ctx.EthereumJsonSerializer));
            }

            /* rlpx */
            EciesCipher    eciesCipher = new EciesCipher(_ctx.CryptoRandom);
            Eip8MessagePad eip8Pad     = new Eip8MessagePad(_ctx.CryptoRandom);

            _ctx._messageSerializationService.Register(new AuthEip8MessageSerializer(eip8Pad));
            _ctx._messageSerializationService.Register(new AckEip8MessageSerializer(eip8Pad));
            _ctx._messageSerializationService.Register(Assembly.GetAssembly(typeof(HelloMessageSerializer)));
            _ctx._messageSerializationService.Register(new ReceiptsMessageSerializer(_ctx.SpecProvider));

            HandshakeService encryptionHandshakeServiceA = new HandshakeService(_ctx._messageSerializationService, eciesCipher,
                                                                                _ctx.CryptoRandom, new Ecdsa(), _ctx.NodeKey.Unprotect(), _ctx.LogManager);

            _ctx._messageSerializationService.Register(Assembly.GetAssembly(typeof(HiMessageSerializer)));

            IDiscoveryConfig discoveryConfig = _ctx.Config <IDiscoveryConfig>();
            IInitConfig      initConfig      = _ctx.Config <IInitConfig>();

            _ctx.SessionMonitor = new SessionMonitor(_networkConfig, _ctx.LogManager);
            _ctx.RlpxPeer       = new RlpxPeer(
                _ctx._messageSerializationService,
                _ctx.NodeKey.PublicKey,
                _networkConfig.P2PPort,
                encryptionHandshakeServiceA,
                _ctx.LogManager,
                _ctx.SessionMonitor);

            await _ctx.RlpxPeer.Init();

            _ctx.StaticNodesManager = new StaticNodesManager(initConfig.StaticNodesPath, _ctx.LogManager);
            await _ctx.StaticNodesManager.InitAsync();

            var     dbName  = "PeersDB";
            IFullDb peersDb = initConfig.DiagnosticMode == DiagnosticMode.MemDb
                ? (IFullDb) new MemDb(dbName)
                : new SimpleFilePublicKeyDb(dbName, PeersDbPath.GetApplicationResourcePath(initConfig.BaseDbPath), _ctx.LogManager);

            NetworkStorage peerStorage = new NetworkStorage(peersDb, _ctx.LogManager);

            ProtocolValidator protocolValidator = new ProtocolValidator(_ctx.NodeStatsManager, _ctx.BlockTree, _ctx.LogManager);

            _ctx.ProtocolsManager = new ProtocolsManager(_ctx.SyncPeerPool, _ctx.SyncServer, _ctx.TxPool, _ctx.DiscoveryApp, _ctx._messageSerializationService, _ctx.RlpxPeer, _ctx.NodeStatsManager, protocolValidator, peerStorage, _ctx.SpecProvider, _ctx.LogManager);

            if (!(_ctx.NdmInitializer is null))
            {
                if (_ctx.WebSocketsManager == null)
                {
                    throw new StepDependencyException(nameof(_ctx.WebSocketsManager));
                }
                if (_ctx.GrpcServer == null)
                {
                    throw new StepDependencyException(nameof(_ctx.GrpcServer));
                }
                if (_ctx.NdmDataPublisher == null)
                {
                    throw new StepDependencyException(nameof(_ctx.NdmDataPublisher));
                }
                if (_ctx.NdmConsumerChannelManager == null)
                {
                    throw new StepDependencyException(nameof(_ctx.NdmConsumerChannelManager));
                }
                if (_ctx.BloomStorage == null)
                {
                    throw new StepDependencyException(nameof(_ctx.BloomStorage));
                }
                if (_ctx.ReceiptFinder == null)
                {
                    throw new StepDependencyException(nameof(_ctx.ReceiptFinder));
                }

                if (_logger.IsInfo)
                {
                    _logger.Info($"Initializing NDM...");
                }
                _ctx.HttpClient = new DefaultHttpClient(new HttpClient(), _ctx.EthereumJsonSerializer, _ctx.LogManager);
                INdmConfig ndmConfig = _ctx.Config <INdmConfig>();
                if (ndmConfig.ProxyEnabled)
                {
                    _ctx.JsonRpcClientProxy = new JsonRpcClientProxy(_ctx.HttpClient, ndmConfig.JsonRpcUrlProxies,
                                                                     _ctx.LogManager);
                    _ctx.EthJsonRpcClientProxy = new EthJsonRpcClientProxy(_ctx.JsonRpcClientProxy);
                }

                FilterStore             filterStore         = new FilterStore();
                FilterManager           filterManager       = new FilterManager(filterStore, _ctx.MainBlockProcessor, _ctx.TxPool, _ctx.LogManager);
                INdmCapabilityConnector capabilityConnector = await _ctx.NdmInitializer.InitAsync(
                    _ctx.ConfigProvider,
                    _ctx.DbProvider,
                    initConfig.BaseDbPath,
                    _ctx.BlockTree,
                    _ctx.TxPool,
                    _ctx.SpecProvider,
                    _ctx.ReceiptFinder,
                    _ctx.Wallet,
                    filterStore,
                    filterManager,
                    _ctx.Timestamper,
                    _ctx.EthereumEcdsa,
                    _ctx.RpcModuleProvider,
                    _ctx.KeyStore,
                    _ctx.EthereumJsonSerializer,
                    _ctx.CryptoRandom,
                    _ctx.Enode,
                    _ctx.NdmConsumerChannelManager,
                    _ctx.NdmDataPublisher,
                    _ctx.GrpcServer,
                    _ctx.NodeStatsManager,
                    _ctx.ProtocolsManager,
                    protocolValidator,
                    _ctx._messageSerializationService,
                    initConfig.EnableUnsecuredDevWallet,
                    _ctx.WebSocketsManager,
                    _ctx.LogManager,
                    _ctx.MainBlockProcessor,
                    _ctx.JsonRpcClientProxy,
                    _ctx.EthJsonRpcClientProxy,
                    _ctx.HttpClient,
                    _ctx.MonitoringService,
                    _ctx.BloomStorage);

                capabilityConnector.Init();
                if (_logger.IsInfo)
                {
                    _logger.Info($"NDM initialized.");
                }
            }

            PeerLoader peerLoader = new PeerLoader(_networkConfig, discoveryConfig, _ctx.NodeStatsManager, peerStorage, _ctx.LogManager);

            _ctx.PeerManager = new PeerManager(_ctx.RlpxPeer, _ctx.DiscoveryApp, _ctx.NodeStatsManager, peerStorage, peerLoader, _networkConfig, _ctx.LogManager, _ctx.StaticNodesManager);
            _ctx.PeerManager.Init();
        }
Esempio n. 7
0
        private async Task InitPeer()
        {
            /* rlpx */
            var eciesCipher = new EciesCipher(_cryptoRandom);
            var eip8Pad     = new Eip8MessagePad(_cryptoRandom);

            _messageSerializationService.Register(new AuthEip8MessageSerializer(eip8Pad));
            _messageSerializationService.Register(new AckEip8MessageSerializer(eip8Pad));
            _messageSerializationService.Register(Assembly.GetAssembly(typeof(HelloMessageSerializer)));
            _messageSerializationService.Register(new ReceiptsMessageSerializer(_specProvider));

            var encryptionHandshakeServiceA = new HandshakeService(_messageSerializationService, eciesCipher,
                                                                   _cryptoRandom, new Ecdsa(), _nodeKey, _logManager);

            _messageSerializationService.Register(Assembly.GetAssembly(typeof(HiMessageSerializer)));

            var discoveryConfig = _configProvider.GetConfig <IDiscoveryConfig>();

            _sessionMonitor = new SessionMonitor(_networkConfig, _logManager);
            _rlpxPeer       = new RlpxPeer(
                _messageSerializationService,
                _nodeKey.PublicKey,
                _networkConfig.P2PPort,
                encryptionHandshakeServiceA,
                _logManager,
                _sessionMonitor);

            await _rlpxPeer.Init();

            _staticNodesManager = new StaticNodesManager(_initConfig.StaticNodesPath, _logManager);
            await _staticNodesManager.InitAsync();

            var peersDb     = new SimpleFilePublicKeyDb("PeersDB", Path.Combine(_initConfig.BaseDbPath, PeersDbPath), _logManager);
            var peerStorage = new NetworkStorage(peersDb, _logManager);

            ProtocolValidator protocolValidator = new ProtocolValidator(_nodeStatsManager, _blockTree, _logManager);

            _protocolsManager = new ProtocolsManager(_syncPeerPool, _syncServer, _txPool, _discoveryApp, _messageSerializationService, _rlpxPeer, _nodeStatsManager, protocolValidator, peerStorage, _perfService, _logManager);

            if (!(_ndmInitializer is null))
            {
                if (_logger.IsInfo)
                {
                    _logger.Info($"Initializing NDM...");
                }
                var filterStore         = new FilterStore();
                var filterManager       = new FilterManager(filterStore, _blockProcessor, _txPool, _logManager);
                var capabilityConnector = await _ndmInitializer.InitAsync(_configProvider, _dbProvider,
                                                                          _initConfig.BaseDbPath, _blockTree, _txPool, _specProvider, _receiptStorage, _wallet, filterStore,
                                                                          filterManager, _timestamper, _ethereumEcdsa, _rpcModuleProvider, _keyStore, _ethereumJsonSerializer,
                                                                          _cryptoRandom, _enode, _ndmConsumerChannelManager, _ndmDataPublisher, _grpcServer,
                                                                          _nodeStatsManager, _protocolsManager, protocolValidator, _messageSerializationService,
                                                                          _initConfig.EnableUnsecuredDevWallet, _webSocketsManager, _logManager, _blockProcessor);

                capabilityConnector.Init();
                if (_logger.IsInfo)
                {
                    _logger.Info($"NDM initialized.");
                }
            }

            PeerLoader peerLoader = new PeerLoader(_networkConfig, discoveryConfig, _nodeStatsManager, peerStorage, _logManager);

            _peerManager = new PeerManager(_rlpxPeer, _discoveryApp, _nodeStatsManager, peerStorage, peerLoader, _networkConfig, _logManager, _staticNodesManager);
            _peerManager.Init();
        }