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

OpenSSL providers support #104961

Merged
merged 17 commits into from
Jul 20, 2024
Merged
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

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using Microsoft.Win32.SafeHandles;

internal static partial class Interop
{
internal static partial class Crypto
{
[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_EcDsaSignHash(
SafeEvpPKeyHandle pkey,
IntPtr extraHandle,
ref byte hash,
int hashLength,
ref byte destination,
int destinationLength);

internal static int EcDsaSignHash(
SafeEvpPKeyHandle pkey,
ReadOnlySpan<byte> hash,
Span<byte> destination)
{
int written = CryptoNative_EcDsaSignHash(
pkey,
pkey.ExtraHandle,
ref MemoryMarshal.GetReference(hash),
hash.Length,
ref MemoryMarshal.GetReference(destination),
destination.Length);

if (written < 0)
{
Debug.Assert(written == -1);
throw CreateOpenSslCryptographicException();
}

return written;
}

[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_EcDsaVerifyHash(
SafeEvpPKeyHandle pkey,
IntPtr extraHandle,
ref byte hash,
int hashLength,
ref byte signature,
int signatureLength);

internal static bool EcDsaVerifyHash(
SafeEvpPKeyHandle pkey,
ReadOnlySpan<byte> hash,
ReadOnlySpan<byte> signature)
{
int ret = CryptoNative_EcDsaVerifyHash(
pkey,
pkey.ExtraHandle,
ref MemoryMarshal.GetReference(hash),
hash.Length,
ref MemoryMarshal.GetReference(signature),
signature.Length);

if (ret == 1)
{
return true;
}

if (ret == 0)
{
return false;
}

Debug.Assert(ret == -1);
throw CreateOpenSslCryptographicException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,21 @@ internal static partial class Crypto
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeyGetEcKey")]
internal static partial SafeEcKeyHandle EvpPkeyGetEcKey(SafeEvpPKeyHandle pkey);

[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeySetEcKey")]
[LibraryImport(Libraries.CryptoNative)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static partial bool EvpPkeySetEcKey(SafeEvpPKeyHandle pkey, SafeEcKeyHandle key);
private static partial bool CryptoNative_EvpPkeySetEcKey(SafeEvpPKeyHandle pkey, SafeEcKeyHandle key);

// Calls EVP_PKEY_set1_EC_KEY therefore the key will be duplicated
internal static SafeEvpPKeyHandle CreateEvpPkeyFromEcKey(SafeEcKeyHandle key)
{
SafeEvpPKeyHandle pkey = Interop.Crypto.EvpPkeyCreate();
if (!CryptoNative_EvpPkeySetEcKey(pkey, key))
{
pkey.Dispose();
throw Interop.Crypto.CreateOpenSslCryptographicException();
}

return pkey;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,35 @@ internal static partial class Interop
{
internal static partial class Crypto
{
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyCtxCreate")]
internal static partial SafeEvpPKeyCtxHandle EvpPKeyCtxCreate(SafeEvpPKeyHandle pkey, SafeEvpPKeyHandle peerkey, out uint secretLength);

[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyDeriveSecretAgreement")]
private static partial int EvpPKeyDeriveSecretAgreement(
SafeEvpPKeyHandle pkey,
IntPtr extraHandle,
SafeEvpPKeyHandle peerKey,
ref byte secret,
uint secretLength,
SafeEvpPKeyCtxHandle ctx);

[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyCtxDestroy")]
internal static partial void EvpPKeyCtxDestroy(IntPtr ctx);
uint secretLength);

internal static void EvpPKeyDeriveSecretAgreement(SafeEvpPKeyCtxHandle ctx, Span<byte> destination)
internal static int EvpPKeyDeriveSecretAgreement(SafeEvpPKeyHandle pkey, SafeEvpPKeyHandle peerKey, Span<byte> destination)
{
Debug.Assert(ctx != null);
Debug.Assert(!ctx.IsInvalid);
Debug.Assert(pkey != null);
Debug.Assert(!pkey.IsInvalid);
Debug.Assert(peerKey != null);
Debug.Assert(!peerKey.IsInvalid);

int ret = EvpPKeyDeriveSecretAgreement(
int written = EvpPKeyDeriveSecretAgreement(
pkey,
pkey.ExtraHandle,
peerKey,
ref MemoryMarshal.GetReference(destination),
(uint)destination.Length,
ctx);
(uint)destination.Length);

if (ret != 1)
if (written <= 0)
{
Debug.Assert(written == 0);
throw CreateOpenSslCryptographicException();
}

return written;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ internal static SafeEvpPKeyHandle RsaGenerateKey(int keySize)
[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_RsaDecrypt(
SafeEvpPKeyHandle pkey,
IntPtr extraHandle,
ref byte source,
int sourceLength,
RSAEncryptionPaddingMode paddingMode,
Expand All @@ -64,6 +65,7 @@ internal static int RsaDecrypt(
{
int written = CryptoNative_RsaDecrypt(
pkey,
pkey.ExtraHandle,
ref MemoryMarshal.GetReference(source),
source.Length,
paddingMode,
Expand All @@ -83,6 +85,7 @@ ref MemoryMarshal.GetReference(destination),
[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_RsaEncrypt(
SafeEvpPKeyHandle pkey,
IntPtr extraHandle,
ref byte source,
int sourceLength,
RSAEncryptionPaddingMode paddingMode,
Expand All @@ -99,6 +102,7 @@ internal static int RsaEncrypt(
{
int written = CryptoNative_RsaEncrypt(
pkey,
pkey.ExtraHandle,
ref MemoryMarshal.GetReference(source),
source.Length,
paddingMode,
Expand All @@ -118,6 +122,7 @@ ref MemoryMarshal.GetReference(destination),
[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_RsaSignHash(
SafeEvpPKeyHandle pkey,
IntPtr extraHandle,
RSASignaturePaddingMode paddingMode,
IntPtr digestAlgorithm,
ref byte hash,
Expand All @@ -128,14 +133,19 @@ private static partial int CryptoNative_RsaSignHash(
internal static int RsaSignHash(
SafeEvpPKeyHandle pkey,
RSASignaturePaddingMode paddingMode,
IntPtr digestAlgorithm,
HashAlgorithmName digestAlgorithm,
ReadOnlySpan<byte> hash,
Span<byte> destination)
{
ArgumentNullException.ThrowIfNull(digestAlgorithm.Name, nameof(digestAlgorithm));

IntPtr digestAlgorithmPtr = Interop.Crypto.HashAlgorithmToEvp(digestAlgorithm.Name);

int written = CryptoNative_RsaSignHash(
pkey,
pkey.ExtraHandle,
paddingMode,
digestAlgorithm,
digestAlgorithmPtr,
ref MemoryMarshal.GetReference(hash),
hash.Length,
ref MemoryMarshal.GetReference(destination),
Expand All @@ -153,6 +163,7 @@ ref MemoryMarshal.GetReference(destination),
[LibraryImport(Libraries.CryptoNative)]
private static partial int CryptoNative_RsaVerifyHash(
SafeEvpPKeyHandle pkey,
IntPtr extraHandle,
RSASignaturePaddingMode paddingMode,
IntPtr digestAlgorithm,
ref byte hash,
Expand All @@ -163,14 +174,19 @@ private static partial int CryptoNative_RsaVerifyHash(
internal static bool RsaVerifyHash(
SafeEvpPKeyHandle pkey,
RSASignaturePaddingMode paddingMode,
IntPtr digestAlgorithm,
HashAlgorithmName digestAlgorithm,
ReadOnlySpan<byte> hash,
ReadOnlySpan<byte> signature)
{
ArgumentNullException.ThrowIfNull(digestAlgorithm.Name, nameof(digestAlgorithm));

IntPtr digestAlgorithmPtr = Interop.Crypto.HashAlgorithmToEvp(digestAlgorithm.Name);

int ret = CryptoNative_RsaVerifyHash(
pkey,
pkey.ExtraHandle,
paddingMode,
digestAlgorithm,
digestAlgorithmPtr,
ref MemoryMarshal.GetReference(hash),
hash.Length,
ref MemoryMarshal.GetReference(signature),
Expand Down
Loading
Loading