Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added net5/net6 targets. Added SupportedOSPlatform attrubutes for windows-specific code. #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Cipher/AesFactories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ public static class AesFactories
{
internal static readonly Func<Aes> ManagedAes = () => new AesManaged();
internal static readonly Func<Aes> FipsAes = Environment.OSVersion.Platform == PlatformID.Win32NT ?
(Func<Aes>)(() => new AesCng()) : // Windows
() => new AesCryptoServiceProvider(); // non-Windows
#pragma warning disable CA1416 // Validate platform compatibility
(Func<Aes>)(() => new AesCng()) : // Windows
#pragma warning restore CA1416 // Validate platform compatibility
() => new AesCryptoServiceProvider(); // non-Windows

public static readonly Func<Aes> Aes = Utils.AllowOnlyFipsAlgorithms ? FipsAes : ManagedAes;
}//class AesFactories
Expand Down
63 changes: 62 additions & 1 deletion Extensions/CngKeyExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,85 @@ namespace SecurityDriven.Inferno.Extensions
{
public static class CngKeyExtensions
{
#if NET5_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#elif NETSTANDARD2_0_OR_GREATER || NET461_OR_GREATER || NETCOREAPP3_1
#else
#error Target Framework is not supported
#endif
static readonly CngKeyCreationParameters cngKeyCreationParameters = new CngKeyCreationParameters { ExportPolicy = CngExportPolicies.AllowPlaintextExport };

#if NET5_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#elif NETSTANDARD2_0_OR_GREATER || NET461_OR_GREATER || NETCOREAPP3_1
#else
#error Target Framework is not supported
#endif
static readonly CngProperty exportPolicy_AllowPlaintextExport = new CngProperty("Export Policy", BitConverter.GetBytes((int)CngExportPolicies.AllowPlaintextExport), CngPropertyOptions.None);

#if NET5_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#elif NETSTANDARD2_0_OR_GREATER || NET461_OR_GREATER || NETCOREAPP3_1
#else
#error Target Framework is not supported
#endif
public static CngKey CreateNewDhmKey(string name = null)
{
return CngKey.Create(CngAlgorithm.ECDiffieHellmanP384, name, cngKeyCreationParameters);
}

#if NET5_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#elif NETSTANDARD2_0_OR_GREATER || NET461_OR_GREATER || NETCOREAPP3_1
#else
#error Target Framework is not supported
#endif
public static CngKey CreateNewDsaKey(string name = null)
{
return CngKey.Create(CngAlgorithm.ECDsaP384, name, cngKeyCreationParameters);
}

#if NET5_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#elif NETSTANDARD2_0_OR_GREATER || NET461_OR_GREATER || NETCOREAPP3_1
#else
#error Target Framework is not supported
#endif
public static byte[] GetPrivateBlob(this CngKey key)
{
return key.Export(CngKeyBlobFormat.EccPrivateBlob);
}

#if NET5_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#elif NETSTANDARD2_0_OR_GREATER || NET461_OR_GREATER || NETCOREAPP3_1
#else
#error Target Framework is not supported
#endif
public static byte[] GetPublicBlob(this CngKey key)
{
return key.Export(CngKeyBlobFormat.EccPublicBlob);
}

#if NET5_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#elif NETSTANDARD2_0_OR_GREATER || NET461_OR_GREATER || NETCOREAPP3_1
#else
#error Target Framework is not supported
#endif
public static CngKey ToPrivateKeyFromBlob(this byte[] privateBlob)
{
var key = CngKey.Import(privateBlob, CngKeyBlobFormat.EccPrivateBlob);
key.SetProperty(exportPolicy_AllowPlaintextExport);
return key;
}

#if NET5_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#elif NETSTANDARD2_0_OR_GREATER || NET461_OR_GREATER || NETCOREAPP3_1
#else
#error Target Framework is not supported
#endif
public static CngKey ToPublicKeyFromBlob(this byte[] publicBlob)
{
return CngKey.Import(publicBlob, CngKeyBlobFormat.EccPublicBlob);
Expand All @@ -44,9 +93,15 @@ public static CngKey ToPublicKeyFromBlob(this byte[] publicBlob)
/// <summary>
/// Both parties are static and authenticated.
/// </summary>
#if NET5_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#elif NETSTANDARD2_0_OR_GREATER || NET461_OR_GREATER || NETCOREAPP3_1
#else
#error Target Framework is not supported
#endif
public static byte[] GetSharedDhmSecret(this CngKey privateDhmKey, CngKey publicDhmKey, byte[] contextAppend = null, byte[] contextPrepend = null)
{
#if (NET462 || NETCOREAPP3_1)
#if (NET462 || NETCOREAPP3_1 || NET5_0 || NET6_0)
using (var ecdh = new ECDiffieHellmanCng(privateDhmKey) { HashAlgorithm = CngAlgorithm.Sha384, SecretAppend = contextAppend, SecretPrepend = contextPrepend })
return ecdh.DeriveKeyMaterial(publicDhmKey);
#elif NETSTANDARD2_0
Expand All @@ -60,6 +115,12 @@ public static byte[] GetSharedDhmSecret(this CngKey privateDhmKey, CngKey public
/// Sender is anonymous and keyless.
/// Receiver is static and authenticated.
/// </summary>
#if NET5_0_OR_GREATER
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
#elif NETSTANDARD2_0_OR_GREATER || NET461_OR_GREATER || NETCOREAPP3_1
#else
#error Target Framework is not supported
#endif
public static SharedEphemeralBundle GetSharedEphemeralDhmSecret(this CngKey receiverDhmPublicKey, byte[] contextAppend = null, byte[] contextPrepend = null)
{
using (var sender = CreateNewDhmKey())
Expand Down
3 changes: 2 additions & 1 deletion SecurityDriven.Inferno.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netcoreapp3.1;net462;netstandard2.0</TargetFrameworks>
<TargetFrameworks>net5;net6;netcoreapp3.1;net462;netstandard2.0</TargetFrameworks>
<LangVersion>latest</LangVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RootNamespace>SecurityDriven.Inferno</RootNamespace>
Expand All @@ -26,6 +26,7 @@
<AssemblyOriginatorKeyFile>Inferno.snk</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
<Nullable>warnings</Nullable>
<!--<NoWarn>$(NoWarn);CS8602;CS8604;CS8629;NU5048;CS8601;CS8625;SYSLIB0021</NoWarn>-->
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
Expand Down