Skip to content

Commit

Permalink
Merge pull request #4 from dlmelendez/rel/vnext
Browse files Browse the repository at this point in the history
Rel/vnext
  • Loading branch information
dlmelendez authored Mar 13, 2024
2 parents 0c1dac4 + 9f780b0 commit 9ac6b23
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 43 deletions.
3 changes: 3 additions & 0 deletions src/ACMESharp/Crypto/JOSE/IJwsTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,8 @@ string JwsAlg
byte[] Sign(byte[] raw);

bool Verify(byte[] raw, byte[] sig);

string ExportSubjectPublicKeyInfoPem();

}
}
11 changes: 8 additions & 3 deletions src/ACMESharp/Crypto/JOSE/Impl/ESJwsTool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Security.Cryptography;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -120,7 +120,12 @@ public byte[] Sign(byte[] raw)

public bool Verify(byte[] raw, byte[] sig)
{
return _dsa.VerifyData(raw, sig, _shaName);
return _dsa.VerifyData(raw, sig, _shaName);
}

public string ExportSubjectPublicKeyInfoPem()
{
return _dsa.ExportSubjectPublicKeyInfoPem();
}

/// <summary>
Expand Down Expand Up @@ -154,4 +159,4 @@ class ESJwk
public string y { get; set; }
}
}
}
}
7 changes: 6 additions & 1 deletion src/ACMESharp/Crypto/JOSE/Impl/RSJwsTool.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Security.Cryptography;
using System.Text.Json;
using System.Text.Json.Serialization;
Expand Down Expand Up @@ -119,6 +119,11 @@ public bool Verify(byte[] raw, byte[] sig)
return _rsa.VerifyData(raw, _sha, sig);
}

public string ExportSubjectPublicKeyInfoPem()
{
return _rsa.ExportSubjectPublicKeyInfoPem();
}

// As per RFC 7638 Section 3, these are the *required* elements of the
// JWK and are sorted in lexicographic order to produce a canonical form
class RSJwk
Expand Down
13 changes: 12 additions & 1 deletion src/ACMESharp/Protocol/AcmeProtocolClient.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
Expand Down Expand Up @@ -47,6 +47,17 @@ public AcmeProtocolClient(HttpClient http, ServiceDirectory? dir = null,
_usePostAsGet = usePostAsGet;
}

/// <summary>
/// https://learn.microsoft.com/en-us/dotnet/fundamentals/networking/http/httpclient-guidelines#recommended-use
/// Uses a bad pattern for short lived HttpClients
/// </summary>
/// <param name="baseUri"></param>
/// <param name="dir"></param>
/// <param name="acct"></param>
/// <param name="signer"></param>
/// <param name="logger"></param>
/// <param name="usePostAsGet"></param>
[System.Obsolete("Uses a poor pattern for instantiating a new HttpClient() object, use the IHttpClientFactory.CreateClient() to create the HttpClient and set disposeHttpClient = false ")]
public AcmeProtocolClient(Uri baseUri,
ServiceDirectory? dir = null,
AccountDetails? acct = null, IJwsTool? signer = null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -15,7 +15,7 @@
<PackageReference Include="DnsClient" Version="1.0.7" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0-dev-00023" />
<PackageReference Include="Serilog.Extensions.Logging.File" Version="3.0.0" />
<PackageReference Include="xunit" Version="2.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7">
<PrivateAssets>all</PrivateAssets>
Expand Down
4 changes: 3 additions & 1 deletion test/ACMESharp.IntegrationTests/AcmeAccountTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public async Task TestDuplicateCreateAccount()
{
var testCtx = SetTestContext();

var oldAcct = LoadObject<AcmeAccount>("acct.json");
var oldAcct = LoadObject<AccountDetails>("acct.json");
var dupAcct = await Clients.Acme.CreateAccountAsync(_contactsInit, true);

// For a duplicate account, the returned object is not complete...
Expand Down Expand Up @@ -187,6 +187,8 @@ public async Task TestUpdateAccountAfterDeactivation()
{
var testCtx = SetTestContext();

await Task.Delay(5000);

var ex = await Assert.ThrowsAnyAsync<AcmeProtocolException>(
() => Clients.Acme.UpdateAccountAsync(_contactsUpdate));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="8.0.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions test/ACMESharp.UnitTests/ACMESharp.UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@

<ItemGroup>
<!-- VSTest: https://github.com/microsoft/vstest/ -->
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<!-- MSTest v2: https://github.com/microsoft/testfx -->
<PackageReference Include="MSTest.TestAdapter" Version="3.2.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
</ItemGroup>

<ItemGroup>
Expand Down
35 changes: 10 additions & 25 deletions test/ACMESharp.UnitTests/JwsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using System.Text.RegularExpressions;
using ACMESharp.Crypto;
using ACMESharp.Crypto.JOSE;
using ACMESharp.Crypto.JOSE.Impl;
using Microsoft.VisualStudio.TestTools.UnitTesting;

Expand Down Expand Up @@ -307,45 +308,29 @@ public void TestRfc7515Example_A_2_1()
}

[TestMethod]
public void SerDesEC()
[DataRow(typeof(RSJwsTool), 100)]
[DataRow(typeof(ESJwsTool), 1000)]
public void SerDes(Type jwsTool, int max)
{
var rng = RandomNumberGenerator.Create();
for (var i = 0; i < 1000; i++) {
var original = new ESJwsTool(); // Default for ISigner
original.Init();
var rawX = new byte[8034];
rng.GetBytes(rawX);
var sigX = original.Sign(rawX);

var exported = original.Export();
var copy = new ESJwsTool();
copy.Init();
copy.Import(exported);
var verified = copy.Verify(rawX, sigX);

Assert.AreEqual(true, verified);
}
}

[TestMethod]
public void SerDesRSA()
{
var rng = RandomNumberGenerator.Create();
for (var i = 0; i < 100; i++)
for (var i = 0; i < max; i++)
{
var original = new RSJwsTool(); // Default for ISigner
var original = Activator.CreateInstance(jwsTool) as IJwsTool; // Default for ISigner
original.Init();
var rawX = new byte[8034];
rng.GetBytes(rawX);
var sigX = original.Sign(rawX);
string originalPubKeyPem = original.ExportSubjectPublicKeyInfoPem();

var exported = original.Export();
var copy = new RSJwsTool();
var copy = Activator.CreateInstance(jwsTool) as IJwsTool;
copy.Init();
copy.Import(exported);
var verified = copy.Verify(rawX, sigX);
string copyPubKeyPem = copy.ExportSubjectPublicKeyInfoPem();

Assert.AreEqual(true, verified);
Assert.AreEqual(originalPubKeyPem, copyPubKeyPem);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.0">
<PackageReference Include="coverlet.collector" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.1" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.1" />
<PackageReference Include="MSTest.TestAdapter" Version="3.2.2" />
<PackageReference Include="MSTest.TestFramework" Version="3.2.2" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<BuildNumber Condition="'$(BuildNumber)' == ''">$(APPVEYOR_BUILD_NUMBER)</BuildNumber>
<BuildNumber Condition="'$(BuildNumber)' == ''">0</BuildNumber>
<VersionPrefix>1.0.1.$(BuildNumber)</VersionPrefix>
<VersionPrefix>1.0.2.$(BuildNumber)</VersionPrefix>
<!--
<VersionSuffix>beta1</VersionSuffix>
-->
Expand Down

0 comments on commit 9ac6b23

Please sign in to comment.