Пример #1
0
    static NWConnection CreateOutboundConnection(string name, string port)
    {
        NWEndpoint endpoint;

        if (bonjour)
        {
            endpoint = NWEndpoint.CreateBonjourService(name, GetServiceType(), "local");
        }
        else
        {
            endpoint = NWEndpoint.Create(name, port);
        }

        Action <NWProtocolOptions> configureTls = SetupTls();
        NWParameters parameters;

        if (useUdp)
        {
            if (useTls)
            {
                parameters = NWParameters.CreateSecureUdp(configureTls: configureTls, configureUdp: null);
            }
            else
            {
                parameters = NWParameters.CreateUdp(configureUdp: null);
            }
        }
        else
        {
            if (useTls)
            {
                parameters = NWParameters.CreateSecureTcp(configureTls: configureTls, configureTcp: null);
            }
            else
            {
                parameters = NWParameters.CreateTcp(configureTcp: null);
            }
        }

        using (NWProtocolStack protocolStack = parameters.ProtocolStack){
            if (ipv4 || ipv6)
            {
                NWProtocolOptions ipOptions = protocolStack.InternetProtocol;
                ipOptions.IPSetVersion(ipv4 ? NWIPVersion.Version4 : NWIPVersion.Version6);
            }
        }

        if (localAddr != null || localPort != null)
        {
            using (NWEndpoint localEndpoint = NWEndpoint.Create(localAddr != null ? localAddr : "::", port == null ? "0" : port))
                parameters.LocalEndpoint = localEndpoint;
        }

        var connection = new NWConnection(endpoint, parameters);

        endpoint.Dispose();
        parameters.Dispose();
        return(connection);
    }
        public void CreateSecureTcpTestDoNotSetUpTls()
        {
            var setUpProtocol = CreateConfigureProtocolHandler();

            using (var parameters = NWParameters.CreateSecureTcp(configureTls: null, configureTcp: setUpProtocol))
                using (var endpoint = NWEndpoint.Create(NetworkResources.MicrosoftUri.Host, "80")) {
                    configureEvent.WaitOne();
                    Assert.False(secureConnectionWasSet, "Configure TLS handler was called.");
                    Assert.True(protocolConfigured, "Protocol configure handler was not called.");
                }
        }
Пример #3
0
        public void CreateSecureTcpTestDoNotSetUpTls()
        {
            var setUpProtocol = CreateConfigureProtocolHandler();

            using (var parameters = NWParameters.CreateSecureTcp(configureTls: null, configureTcp: setUpProtocol))
                using (var endpoint = NWEndpoint.Create("wwww.google.com", "80"))
                    using (var connection = new NWConnection(endpoint, parameters)) {
                        connection.SetQueue(DispatchQueue.MainQueue);
                        connection.Start();
                        configureEvent.WaitOne();
                        connection.Cancel();
                        Assert.False(secureConnectionWasSet, "Configure TLS handler was called.");
                        Assert.True(protocolConfigured, "Protocol configure handler was not called.");
                    }
        }
        public void TlsDefaults()
        {
            using (var ep = NWEndpoint.Create("www.microsoft.com", "https"))
                using (var parameters = NWParameters.CreateSecureTcp())
                    using (var queue = new DispatchQueue(GetType().FullName)) {
                        var connection = new NWConnection(ep, parameters);

                        var ready = new ManualResetEvent(false);
                        connection.SetStateChangeHandler((state, error) => {
                            Console.WriteLine(state);
                            switch (state)
                            {
                            case NWConnectionState.Cancelled:
                            case NWConnectionState.Failed:
                                // We can't dispose until the connection has been closed or it failed.
                                connection.Dispose();
                                break;

                            case NWConnectionState.Invalid:
                            case NWConnectionState.Preparing:
                            case NWConnectionState.Waiting:
                                break;

                            case NWConnectionState.Ready:
                                ready.Set();
                                break;

                            default:
                                break;
                            }
                        });

                        connection.SetQueue(queue);
                        connection.Start();

                        // Wait until the connection is ready.
                        Assert.True(ready.WaitOne(TimeSpan.FromSeconds(10)), "Connection is ready");

                        using (var m = connection.GetProtocolMetadata(NWProtocolDefinition.TlsDefinition)) {
                            var s = m.TlsSecProtocolMetadata;
                            Assert.False(s.EarlyDataAccepted, "EarlyDataAccepted");
                            Assert.That(s.NegotiatedCipherSuite, Is.Not.EqualTo(SslCipherSuite.SSL_NULL_WITH_NULL_NULL), "NegotiatedCipherSuite");
                            Assert.Null(s.NegotiatedProtocol, "NegotiatedProtocol");
                            Assert.That(s.NegotiatedProtocolVersion, Is.EqualTo(SslProtocol.Tls_1_2).Or.EqualTo(SslProtocol.Tls_1_3), "NegotiatedProtocolVersion");
                            Assert.NotNull(s.PeerPublicKey, "PeerPublicKey");

                            Assert.True(SecProtocolMetadata.ChallengeParametersAreEqual(s, s), "ChallengeParametersAreEqual");
                            Assert.True(SecProtocolMetadata.PeersAreEqual(s, s), "PeersAreEqual");

                            if (TestRuntime.CheckXcodeVersion(11, 0))
                            {
                                using (var d = s.CreateSecret("Xamarin", 128)) {
                                    Assert.That(d.Size, Is.EqualTo((nuint)128), "CreateSecret-1");
                                }
                                using (var d = s.CreateSecret("Microsoft", new byte [1], 256)) {
                                    Assert.That(d.Size, Is.EqualTo((nuint)256), "CreateSecret-2");
                                }

                                Assert.That(s.NegotiatedTlsProtocolVersion, Is.EqualTo(TlsProtocolVersion.Tls12).Or.EqualTo(TlsProtocolVersion.Tls13), "NegotiatedTlsProtocolVersion");
                                // we want to test the binding/API - not the exact value which can vary depending on the negotiation between the client (OS) and server...
                                Assert.That(s.NegotiatedTlsCipherSuite, Is.Not.EqualTo(0), "NegotiatedTlsCipherSuite");
                                Assert.That(s.ServerName, Is.EqualTo("www.microsoft.com"), "ServerName");
                                // we don't have a TLS-PSK enabled server to test this
                                Assert.False(s.AccessPreSharedKeys((psk, pskId) => { }), "AccessPreSharedKeys");
                            }
                        }

                        connection.Cancel();
                    }
        }
Пример #5
0
    static NWListener CreateAndStartListener(string host, string port)
    {
        Action <NWProtocolOptions> configureTls = SetupTls();
        NWParameters parameters;

        // Create the parameters, either TLS or no TLS, and with UDP or no UDP
        if (useUdp)
        {
            if (useTls)
            {
                parameters = NWParameters.CreateSecureUdp(configureTls: configureTls, configureUdp: null);
            }
            else
            {
                parameters = NWParameters.CreateUdp(configureUdp: null);
            }
        }
        else
        {
            if (useTls)
            {
                parameters = NWParameters.CreateSecureTcp(configureTls: configureTls, configureTcp: null);
            }
            else
            {
                parameters = NWParameters.CreateTcp(configureTcp: null);
            }
        }

        // If specified, set the IP version
        using (NWProtocolStack protocolStack = parameters.ProtocolStack){
            if (ipv4 || ipv6)
            {
                NWProtocolOptions ipOptions = protocolStack.InternetProtocol;
                ipOptions.IPSetVersion(ipv4 ? NWIPVersion.Version4 : NWIPVersion.Version6);
            }
        }

        // Bind to local address and port
        string address = bonjour ? null : host;

        if (address != null || port != null)
        {
            NWEndpoint localEndpoint = NWEndpoint.Create(address != null ? address : "::", port != null ? port : "0");
            Console.WriteLine("Getting {0} and {1}", address != null ? address : "::", port != null ? port : "0");
            parameters.LocalEndpoint = localEndpoint;
            Console.WriteLine("With port: " + localEndpoint.Port);
        }

        var listener = NWListener.Create(parameters);

        if (bonjour && host != null)
        {
            listener.SetAdvertiseDescriptor(NWAdvertiseDescriptor.CreateBonjourService(host, GetServiceType(), "local"));
            listener.SetAdvertisedEndpointChangedHandler((NWEndpoint advertisedEndpoint, bool added) => {
                if (verbose)
                {
                    var astr = added ? "added" : "removed";
                    warn($"Listener {astr} on {advertisedEndpoint.BonjourServiceName} on ({advertisedEndpoint.BonjourServiceName}.{GetServiceType()}.local");
                }
            });
        }

        listener.SetQueue(DispatchQueue.MainQueue);
        listener.SetStateChangedHandler((listenerState, error) => {
            var errno = (SslStatus)(error == null ? 0 : error.ErrorCode);
            switch (listenerState)
            {
            case NWListenerState.Waiting:
                if (verbose)
                {
                    warn($"Listener on port {listener.Port} udp={useUdp} waiting");
                }
                break;

            case NWListenerState.Failed:
                warn($"Listener on port {listener.Port} udp={useUdp} failed");
                break;

            case NWListenerState.Ready:
                if (verbose)
                {
                    warn($"Listener on port {listener.Port} udp={useUdp} ready");
                }
                break;

            case NWListenerState.Cancelled:
                listener = null;
                break;
            }
        });

        listener.SetNewConnectionHandler((connection) => {
            if (inboundConnection != null)
            {
                // We only support one connection at a time, so if we already
                // have one, reject the incoming connection.
                connection.Cancel();
            }
            else
            {
                if (verbose)
                {
                    warn($"New Connection on {connection.Handle} with {connection.Endpoint}");
                }
                // Accept the incoming connection and start sending  and receiving on it
                inboundConnection = connection;
                StartConnection(inboundConnection);
                StartSendReceiveLoop(inboundConnection);
            }
        });
        listener.Start();

        return(listener);
    }