Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 256e486

Browse files
author
Cesar Blum Silveira
committed
Use CertificateManager to find development certificate.
1 parent 3ac6ff8 commit 256e486

File tree

2 files changed

+11
-47
lines changed

2 files changed

+11
-47
lines changed

src/Kestrel/DefaultHttpsProvider.cs

Lines changed: 9 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Collections.Generic;
65
using System.Linq;
76
using System.Security.Cryptography.X509Certificates;
7+
using Microsoft.AspNetCore.Certificates.Generation;
88
using Microsoft.AspNetCore.Hosting;
99
using Microsoft.AspNetCore.Server.Kestrel.Core;
1010
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal;
@@ -13,7 +13,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel
1313
{
1414
internal class DefaultHttpsProvider : IDefaultHttpsProvider
1515
{
16-
private const string AspNetHttpsOid = "1.3.6.1.4.1.311.84.1.1";
16+
private static readonly CertificateManager _certificateManager = new CertificateManager();
1717

1818
public void ConfigureHttps(ListenOptions listenOptions)
1919
{
@@ -22,54 +22,16 @@ public void ConfigureHttps(ListenOptions listenOptions)
2222

2323
private static X509Certificate2 FindDevelopmentCertificate()
2424
{
25-
// TODO: replace this with call to
26-
// CertificateManager.FindCertificates(CertificatePurpose.HTTPS, StoreName.My, StoreLocation.CurrentUser, isValid: true)
27-
// when that becomes available.
28-
using (var store = new X509Store(StoreName.My, StoreLocation.CurrentUser))
29-
{
30-
store.Open(OpenFlags.ReadOnly);
31-
32-
var certificates = store.Certificates.OfType<X509Certificate2>();
33-
var certificate = certificates
34-
.FirstOrDefault(c => HasOid(c, AspNetHttpsOid) && !IsExpired(c) && HasPrivateKey(c));
35-
36-
if (certificate == null)
37-
{
38-
throw new InvalidOperationException("Unable to find ASP.NET Core development certificate.");
39-
}
40-
41-
DisposeCertificates(certificates.Except(new[] { certificate }));
42-
43-
return certificate;
44-
}
45-
}
25+
var certificate = _certificateManager
26+
.ListCertificates(CertificatePurpose.HTTPS, StoreName.My, StoreLocation.CurrentUser, isValid: true, requireExportable: false)
27+
.FirstOrDefault();
4628

47-
private static bool HasOid(X509Certificate2 certificate, string oid) =>
48-
certificate.Extensions
49-
.OfType<X509Extension>()
50-
.Any(e => string.Equals(oid, e.Oid.Value, StringComparison.Ordinal));
51-
52-
private static bool IsExpired(X509Certificate2 certificate)
53-
{
54-
var now = DateTimeOffset.Now;
55-
return now < certificate.NotBefore || now > certificate.NotAfter;
56-
}
57-
58-
private static bool HasPrivateKey(X509Certificate2 certificate)
59-
=> certificate.GetRSAPrivateKey() != null;
60-
61-
private static void DisposeCertificates(IEnumerable<X509Certificate2> certificates)
62-
{
63-
foreach (var certificate in certificates)
29+
if (certificate == null)
6430
{
65-
try
66-
{
67-
certificate.Dispose();
68-
}
69-
catch
70-
{
71-
}
31+
throw new InvalidOperationException("Unable to find ASP.NET Core development certificate.");
7232
}
33+
34+
return certificate;
7335
}
7436
}
7537
}

src/Kestrel/Kestrel.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="Microsoft.AspNetCore.Hosting" />
15+
<PackageReference Include="Microsoft.AspNetCore.Certificates.Generation.Sources" PrivateAssets="All" />
16+
<PackageReference Include="System.Security.Cryptography.Cng" />
1517
</ItemGroup>
1618

1719
<ItemGroup>

0 commit comments

Comments
 (0)