Esempio n. 1
0
        public void ItLoadsPemAndKeyFileByDefault(string keyFormat, string thumbprint)
        {
            var path    = Path.Combine(AppContext.BaseDirectory, "TestAssets", "Https", keyFormat);
            var options = new Mock <CommandLineOptions>();

            options.SetupGet(o => o.UseTls).Returns(true);
            Assert.True(CertificateLoader.TryLoadCertificate(options.Object, path, out var x509, out _));
            Assert.NotNull(x509);
            Assert.Equal(thumbprint, x509.Thumbprint);
            Assert.True(x509.HasPrivateKey, "Cert should have private key");
        }
Esempio n. 2
0
        public static void When_load_certificate_thumb_empty()
        {
            const string thumbprint = "";

            var norm = CertificateLoader.NormalizeThumbprint(thumbprint);

            Assert.Equal(string.Empty, norm);

            Assert.Throws <ArgumentNullException>(() => CertificateLoader.TryLoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false, out _));
            Assert.Throws <ArgumentNullException>(() => CertificateLoader.LoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false));
        }
Esempio n. 3
0
        public static void When_load_certificate_thumb_noisy_only()
        {
            var thumbprint = new string('?', CertificateLoader.Sha1Length); // Special characters only

            var norm = CertificateLoader.NormalizeThumbprint(thumbprint);

            Assert.Equal(0, norm.Length);
            Assert.Throws <ArgumentNullException>(() => CertificateLoader.TryLoadCertificate(StoreName.My, StoreLocation.CurrentUser, norm, false, out _));

            Assert.Throws <FormatException>(() => CertificateLoader.TryLoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false, out _));
            Assert.Throws <FormatException>(() => CertificateLoader.LoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false));
        }
Esempio n. 4
0
        public static void When_load_certificate_thumb_short_by_N(int n)
        {
            var thumbprint = s_existingCertificate.Thumbprint.Substring(n); // Too short

            var norm = CertificateLoader.NormalizeThumbprint(thumbprint);

            Assert.Equal(thumbprint.Length, norm.Length);
            Assert.NotEqual(CertificateLoader.Sha1Length, norm.Length);

            Assert.Throws <FormatException>(() => CertificateLoader.TryLoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false, out _));
            Assert.Throws <FormatException>(() => CertificateLoader.LoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false));
        }
Esempio n. 5
0
        public void ItLoadsCertPfxFileByDefault()
        {
            var path    = Path.Combine(AppContext.BaseDirectory, "TestAssets", "Https", "pfx");
            var options = new Mock <CommandLineOptions>();

            options.SetupGet(o => o.CertificatePassword).Returns("testPassword");
            options.SetupGet(o => o.UseTls).Returns(true);
            Assert.True(CertificateLoader.TryLoadCertificate(options.Object, path, out var x509, out _));
            Assert.NotNull(x509);
            Assert.Equal("E8481D606B15080024C806EFE89B00F0976BD906", x509.Thumbprint);
            Assert.True(x509.HasPrivateKey, "Cert should have private key");
        }
Esempio n. 6
0
        public static void When_load_certificate_thumb_noisy_short_0()
        {
            const string thumbprint = "\r\n"; // 0 chars after removing special chars

            var norm = CertificateLoader.NormalizeThumbprint(thumbprint);

            Assert.NotEqual(thumbprint.Length, norm.Length);
            Assert.Equal(0, norm.Length);
            Assert.Throws <ArgumentNullException>(() => CertificateLoader.TryLoadCertificate(StoreName.My, StoreLocation.CurrentUser, norm, false, out _));

            Assert.Throws <ArgumentNullException>(() => CertificateLoader.TryLoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false, out _));
            Assert.Throws <ArgumentNullException>(() => CertificateLoader.LoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false));
        }
Esempio n. 7
0
        public static void When_load_certificate_thumb_noisy_short(int n)
        {
            var thumbprint = "\n" + s_existingCertificate.Thumbprint.Substring(n) + "\t"; // Too short after removing special chars

            var norm = CertificateLoader.NormalizeThumbprint(thumbprint);

            Assert.NotEqual(thumbprint.Length, norm.Length);
            Assert.NotEqual(CertificateLoader.Sha1Length, norm.Length);
            Assert.Throws <FormatException>(() => CertificateLoader.TryLoadCertificate(StoreName.My, StoreLocation.CurrentUser, norm, false, out _));

            Assert.Throws <FormatException>(() => CertificateLoader.TryLoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false, out _));
            Assert.Throws <FormatException>(() => CertificateLoader.LoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false));
        }
Esempio n. 8
0
        public static void When_load_certificate_thumb_nonexistent()
        {
            var thumbprint = "00000" + s_existingCertificate.Thumbprint.Substring(10) + "00000"; // Valid format but unlikely to exist

            var norm = CertificateLoader.NormalizeThumbprint(thumbprint);

            Assert.Equal(thumbprint.Length, norm.Length);
            Assert.Equal(CertificateLoader.Sha1Length, norm.Length);

            var found = CertificateLoader.TryLoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false, out _);

            Assert.False(found);

            Assert.Throws <InvalidOperationException>(() => CertificateLoader.LoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false));
        }
Esempio n. 9
0
        public static void When_load_certificate_thumb_valid()
        {
            var thumbprint = s_existingCertificate.Thumbprint; // Valid in all respects (given that we already retrieved it locally)

            var norm = CertificateLoader.NormalizeThumbprint(thumbprint);

            Assert.Equal(CertificateLoader.Sha1Length, norm.Length);

            var found = CertificateLoader.TryLoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false, out X509Certificate2 actual);

            Assert.True(found);
            Assert.Equal(s_existingCertificate.SerialNumber, actual.SerialNumber);

            actual = CertificateLoader.LoadCertificate(StoreName.My, StoreLocation.CurrentUser, thumbprint, false);
            Assert.Equal(s_existingCertificate.SerialNumber, actual.SerialNumber);
        }
Esempio n. 10
0
    public async Task <int> RunAsync(CancellationToken cancellationToken)
    {
        var directory = Path.GetFullPath(_options.Directory ?? _currentDirectory);
        var port      = _options.Port;

        if (!CertificateLoader.TryLoadCertificate(_options, _currentDirectory, out var cert, out var certLoadError))
        {
            _reporter.Verbose(certLoadError.ToString());
            _reporter.Error(certLoadError.Message);
            return(1);
        }

        if (cert != null)
        {
            _reporter.Verbose($"Using certificate {cert.SubjectName.Name} ({cert.Thumbprint})");
        }

        void ConfigureHttps(ListenOptions options)
        {
            if (cert != null)
            {
                options.UseHttps(cert);
            }
        }

        var host = new WebHostBuilder()
                   .ConfigureLogging(l =>
        {
            l.SetMinimumLevel(_options.MinLogLevel);
            l.AddConsole();
        })
                   .PreferHostingUrls(false)
                   .UseKestrel(o =>
        {
            if (_options.ShouldUseLocalhost())
            {
                if (port.GetValueOrDefault() == 0)
                {
                    o.ListenAnyIP(0, ConfigureHttps);
                }
                else
                {
                    o.ListenLocalhost(port.GetValueOrDefault(), ConfigureHttps);
                }
            }
            else
            {
                foreach (var a in _options.Addresses)
                {
                    o.Listen(a, port.GetValueOrDefault(), ConfigureHttps);
                }
            }
        })
                   .UseWebRoot(directory)
                   .UseContentRoot(directory)
                   .UseEnvironment("Production")
                   .SuppressStatusMessages(true)
                   .UseStartup <Startup>()
                   .ConfigureServices(s => s.AddSingleton(_options))
                   .Build();

        _console.Write(ConsoleColor.DarkYellow, "Starting server, serving ");
        _console.WriteLine(Path.GetRelativePath(_currentDirectory, directory));

        var defaultExtensions = _options.GetDefaultExtensions();

        if (defaultExtensions != null)
        {
            _console.WriteLine(ConsoleColor.DarkYellow, $"Using default extensions " + string.Join(", ", defaultExtensions));
        }

        await host.StartAsync(cancellationToken);

        AfterServerStart(host);
        await host.WaitForShutdownAsync(cancellationToken);

        return(0);
    }