Skip to content

Commit

Permalink
Workaround broken RSA loading on Windows for .NET Core 3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
natemcmaster committed Sep 20, 2019
1 parent b0750f8 commit 73055da
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 34 deletions.
10 changes: 3 additions & 7 deletions dotnet-serve.sln
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26124.0
# Visual Studio Version 16
VisualStudioVersion = 16.0.29230.61
MinimumVisualStudioVersion = 15.0.26124.0
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C769ED8F-FD49-48BF-AE78-1F6ABCEAD42B}"
EndProject
Expand All @@ -16,17 +16,13 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
CHANGELOG.md = CHANGELOG.md
CONTRIBUTING.md = CONTRIBUTING.md
Directory.Build.props = Directory.Build.props
Directory.Build.targets = Directory.Build.targets
dotnet-serve.sln.licenseheader = dotnet-serve.sln.licenseheader
LICENSE.txt = LICENSE.txt
NuGet.config = NuGet.config
README.md = README.md
version.props = version.props
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{2266ACA2-C30A-43CC-8C29-55D8EFA74BA1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "dotnet-serve.Tests", "test\dotnet-serve.Tests\dotnet-serve.Tests.csproj", "{4F5C8689-CAC3-4301-A48B-132E195C5EFB}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "dotnet-serve.Tests", "test\dotnet-serve.Tests\dotnet-serve.Tests.csproj", "{4F5C8689-CAC3-4301-A48B-132E195C5EFB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
35 changes: 24 additions & 11 deletions src/dotnet-serve/Security/CertificateLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,30 @@ private static X509Certificate2 LoadFromPem(string certPath, string keyPath)

private static RSA CreateRSA(RsaPrivateCrtKeyParameters rsaParams)
{
return RSA.Create(new RSAParameters
{
Modulus = rsaParams.Modulus.ToByteArray(),
Exponent = rsaParams.PublicExponent.ToByteArray(),
D = rsaParams.Exponent.ToByteArray(),
P = rsaParams.P.ToByteArray(),
Q = rsaParams.Q.ToByteArray(),
DP = rsaParams.DP.ToByteArray(),
DQ = rsaParams.DQ.ToByteArray(),
InverseQ = rsaParams.QInv.ToByteArray(),
});
try
{
return RSA.Create(new RSAParameters
{
Modulus = rsaParams.Modulus.ToByteArray(),
Exponent = rsaParams.PublicExponent.ToByteArray(),
D = rsaParams.Exponent.ToByteArray(),
P = rsaParams.P.ToByteArray(),
Q = rsaParams.Q.ToByteArray(),
DP = rsaParams.DP.ToByteArray(),
DQ = rsaParams.DQ.ToByteArray(),
InverseQ = rsaParams.QInv.ToByteArray(),
});
}
catch (CryptographicException)
{
#if NETCOREAPP3_0
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Console.Error.WriteLine("There are known issues loading PEM certificates on Windows with .NET Core 3.0. See https://github.com/natemcmaster/dotnet-serve/issues/27.");
}
#endif
throw;
}
}

private static X509Certificate2 LoadDeveloperCertificate()
Expand Down
11 changes: 9 additions & 2 deletions test/dotnet-serve.Tests/CertLoaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Net;
using System.Threading.Tasks;
using McMaster.Extensions.Xunit;
using Moq;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -45,7 +46,10 @@ public async Task ItDoesNotServePfxFile()
Assert.Equal(HttpStatusCode.Forbidden, resp.StatusCode);
}

[Theory]
[SkippableTheory]
#if NETCOREAPP3_0
[SkipOnOS(OS.Windows)] // see https://github.com/dotnet/corefx/issues/41226
#endif
[InlineData("rsa", "E8481D606B15080024C806EFE89B00F0976BD906")]
public void ItLoadsPemAndKeyFileByDefault(string keyFormat, string thumbprint)
{
Expand All @@ -58,7 +62,10 @@ public void ItLoadsPemAndKeyFileByDefault(string keyFormat, string thumbprint)
Assert.True(x509.HasPrivateKey, "Cert should have private key");
}

[Theory]
[SkippableTheory]
#if NETCOREAPP3_0
[SkipOnOS(OS.Windows)] // see https://github.com/dotnet/corefx/issues/41226
#endif
[InlineData("rsa")]
public async Task ItDoesNotServePemFiles(string keyFormat)
{
Expand Down
12 changes: 1 addition & 11 deletions test/dotnet-serve.Tests/DotNetServe.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
Expand All @@ -14,17 +13,8 @@ namespace McMaster.DotNet.Serve.Tests
{
class DotNetServe : IDisposable
{
private const string TargetFramework
#if NETCOREAPP2_1
= "netcoreapp2.1";
#elif NETCOREAPP3_0
= "netcoreapp3.0";
#else
#error Update target frameworks
#endif

private static readonly string s_dotnetServe
= Path.Combine(AppContext.BaseDirectory, "tool", TargetFramework, "dotnet-serve.dll");
= Path.Combine(AppContext.BaseDirectory, "tool", "dotnet-serve.dll");

private static int s_nextPort
#if NETCOREAPP2_1 // avoid conflicts if tests for both target frameworks run at the same time.
Expand Down
7 changes: 4 additions & 3 deletions test/dotnet-serve.Tests/dotnet-serve.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;netcoreapp3.0</TargetFrameworks>
<TargetFrameworks>netcoreapp3.0;netcoreapp2.1</TargetFrameworks>
<DefaultItemExcludes>$(DefaultItemExcludes);TestAssets\**\*</DefaultItemExcludes>
<RootNamespace>McMaster.DotNet.Server.Tests</RootNamespace>
<WarningsNotAsErrors>$(WarningsNotAsErrors);AD0001</WarningsNotAsErrors>
Expand All @@ -23,16 +23,17 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="McMaster.Extensions.XUnit" Version="0.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.0" />
<PackageReference Include="Moq" Version="4.10.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1" />
</ItemGroup>

<Target Name="PublishDotNetServe" BeforeTargets="CoreCompile">
<Target Name="PublishDotNetServe" AfterTargets="ResolveProjectReferences">
<MSBuild Projects="..\..\src\dotnet-serve\dotnet-serve.csproj"
Targets="Publish"
Properties="PublishDir=$(TargetDir)\tool\$(TargetFramework)\;Configuration=$(Configuration);TargetFramework=$(TargetFramework)" />
Properties="PublishDir=$(TargetDir)\tool\;Configuration=$(Configuration);TargetFramework=$(TargetFramework)" />
</Target>

</Project>

0 comments on commit 73055da

Please sign in to comment.