Skip to content

Commit

Permalink
Merge branch 'release/0.80.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jericho committed May 5, 2021
2 parents 383d576 + 6f76fe8 commit 4179dd1
Show file tree
Hide file tree
Showing 8 changed files with 654 additions and 1,100 deletions.
1 change: 1 addition & 0 deletions Source/StrongGrid.IntegrationTests/Tests/Mail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public async Task RunAsync(IBaseClient client, TextWriter log, CancellationToken
customArgs: customArgs,
mailSettings: mailSettings,
trackingSettings: trackingSettings,
priority: MailPriority.High,
cancellationToken: cancellationToken
).ConfigureAwait(false);
await log.WriteLineAsync($"Email has been sent. Message Id: {messageId}").ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion Source/StrongGrid.UnitTests/StrongGrid.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net461;net472;net5.0</TargetFrameworks>
<TargetFrameworks>net48;netcoreapp3.1;net5.0</TargetFrameworks>
<AssemblyName>StrongGrid.UnitTests</AssemblyName>
<RootNamespace>StrongGrid.UnitTests</RootNamespace>
</PropertyGroup>
Expand Down
602 changes: 602 additions & 0 deletions Source/StrongGrid/Extensions/Public.cs

Large diffs are not rendered by default.

458 changes: 0 additions & 458 deletions Source/StrongGrid/Resources/IMail.cs

Large diffs are not rendered by default.

644 changes: 48 additions & 596 deletions Source/StrongGrid/Resources/Mail.cs

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions Source/StrongGrid/StrongGrid.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net461;net472;netstandard2.0;net5.0</TargetFrameworks>
<TargetFrameworks>net48;netstandard2.1;net5.0</TargetFrameworks>
<PlatformTarget>anycpu</PlatformTarget>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<OutputType>Library</OutputType>
Expand Down Expand Up @@ -53,7 +53,6 @@

<ItemGroup Condition=" $(TargetFramework.StartsWith('netstandard')) ">
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Security.Cryptography.Cng" Version="5.0.0" />
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
</ItemGroup>

Expand Down
26 changes: 0 additions & 26 deletions Source/StrongGrid/Utilities/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,9 @@ namespace StrongGrid.Utilities
internal static class Utils
{
private static readonly byte[] Secp256R1Prefix = Convert.FromBase64String("MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAE");
private static readonly byte[] CngBlobPrefix = { 0x45, 0x43, 0x53, 0x31, 0x20, 0, 0, 0 };

public static RecyclableMemoryStreamManager MemoryStreamManager { get; } = new RecyclableMemoryStreamManager();

/// <summary>
/// Converts a secp256r1/NIST P-256 public key.
/// </summary>
/// <param name="subjectPublicKeyInfo">The public key.</param>
/// <returns>The converted public key.</returns>
/// <remarks>
/// From https://stackoverflow.com/questions/44502331/c-sharp-get-cngkey-object-from-public-key-in-text-file/44527439#44527439 .
/// </remarks>
public static byte[] ConvertSecp256R1PublicKeyToEccPublicBlob(byte[] subjectPublicKeyInfo)
{
if (subjectPublicKeyInfo.Length != 91)
throw new InvalidOperationException();

var prefix = Secp256R1Prefix;

if (!subjectPublicKeyInfo.Take(prefix.Length).SequenceEqual(prefix))
throw new InvalidOperationException();

var cngBlob = new byte[CngBlobPrefix.Length + 64];
Buffer.BlockCopy(CngBlobPrefix, 0, cngBlob, 0, CngBlobPrefix.Length);
Buffer.BlockCopy(subjectPublicKeyInfo, Secp256R1Prefix.Length, cngBlob, CngBlobPrefix.Length, 64);

return cngBlob;
}

/// <summary>
/// Get the 'x' and 'y' values from a secp256r1/NIST P-256 public key.
/// </summary>
Expand Down
18 changes: 1 addition & 17 deletions Source/StrongGrid/WebhookParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ public Event[] ParseSignedEventsWebhook(string requestBody, string publicKey, st
The 'ECDsa.ImportSubjectPublicKeyInfo' method was introduced in .NET core 3.0
and the DSASignatureFormat enum was introduced in .NET 5.0.
We can get rid of the code that relies on ECDsaCng and remove reference to
System.Security.Cryptography.Cng in csproj when we drop support for net461.
We can get rid of the 'ConvertECDSASignature' class and the Utils methods that
convert public keys when we stop suporting .NET framework and .NET standard.
Expand All @@ -120,18 +117,14 @@ NET5_0_OR_GREATER was added to the NET SDK very recently and works fine on
on their Ubuntu image because it's still on NET SDK 5.0.101. That's why I added
the seemingly redundant "NET5_0" in the conditional block below. It will be safe
to remove it when the SDK in AppVeyor's Ubuntu image is upgraded.
Note:
ECDsa is cross-platform and can be used on Windows and Linux/Ubuntu.
ECDsaCng is Windows only.
*/

#if NET5_0 || NET5_0_OR_GREATER
// Verify the signature
var eCDsa = ECDsa.Create();
eCDsa.ImportSubjectPublicKeyInfo(publicKeyBytes, out _);
var verified = eCDsa.VerifyData(data, signatureBytes, HashAlgorithmName.SHA256, DSASignatureFormat.Rfc3279DerSequence);
#elif NET472_OR_GREATER || NETSTANDARD2_0
#elif NET48_OR_GREATER || NETSTANDARD2_1
// Convert the signature and public key provided by SendGrid into formats usable by the ECDsa .net crypto class
var sig = ConvertECDSASignature.LightweightConvertSignatureFromX9_62ToISO7816_8(256, signatureBytes);
var (x, y) = Utils.GetXYFromSecp256r1PublicKey(publicKeyBytes);
Expand All @@ -148,15 +141,6 @@ ECDsaCng is Windows only.
}
});
var verified = eCDsa.VerifyData(data, sig, HashAlgorithmName.SHA256);
#elif NETFRAMEWORK
// Convert the signature and public key provided by SendGrid into formats usable by the ECDsaCng .net crypto class
var sig = ConvertECDSASignature.LightweightConvertSignatureFromX9_62ToISO7816_8(256, signatureBytes);
var cngBlob = Utils.ConvertSecp256R1PublicKeyToEccPublicBlob(publicKeyBytes);

// Verify the signature
var cngKey = CngKey.Import(cngBlob, CngKeyBlobFormat.EccPublicBlob);
var eCDsaCng = new ECDsaCng(cngKey);
var verified = eCDsaCng.VerifyData(data, sig);
#else
#error Unhandled TFM
#endif
Expand Down

0 comments on commit 4179dd1

Please sign in to comment.