Skip to content

Commit 8134c62

Browse files
committed
OpenSSL providers support
1 parent 25a5085 commit 8134c62

36 files changed

+1442
-813
lines changed

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EcDsa.cs

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EvpPkey.EcKey.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,21 @@ internal static partial class Crypto
1212
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeyGetEcKey")]
1313
internal static partial SafeEcKeyHandle EvpPkeyGetEcKey(SafeEvpPKeyHandle pkey);
1414

15-
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPkeySetEcKey")]
15+
[LibraryImport(Libraries.CryptoNative)]
1616
[return: MarshalAs(UnmanagedType.Bool)]
17-
internal static partial bool EvpPkeySetEcKey(SafeEvpPKeyHandle pkey, SafeEcKeyHandle key);
17+
private static partial bool CryptoNative_EvpPkeySetEcKey(SafeEvpPKeyHandle pkey, SafeEcKeyHandle key);
18+
19+
// Calls EVP_PKEY_set1_EC_KEY therefore the key will be duplicated
20+
internal static SafeEvpPKeyHandle CreateEvpPkeyFromEcKey(SafeEcKeyHandle key)
21+
{
22+
SafeEvpPKeyHandle pkey = Interop.Crypto.EvpPkeyCreate();
23+
if (!CryptoNative_EvpPkeySetEcKey(pkey, key))
24+
{
25+
pkey.Dispose();
26+
throw Interop.Crypto.CreateOpenSslCryptographicException();
27+
}
28+
29+
return pkey;
30+
}
1831
}
1932
}

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EvpPkey.Ecdh.cs

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,100 @@ internal static partial class Interop
1111
{
1212
internal static partial class Crypto
1313
{
14+
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyCtxCreateFromPKey")]
15+
private static partial SafeEvpPKeyCtxHandle EvpPKeyCtxCreate(SafeEvpPKeyHandle pkey, IntPtr extraHandle);
16+
17+
internal static SafeEvpPKeyCtxHandle EvpPKeyCtxCreate(SafeEvpPKeyHandle pkey)
18+
=> EvpPKeyCtxCreate(pkey, pkey.ExtraHandle);
19+
1420
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyCtxCreate")]
15-
internal static partial SafeEvpPKeyCtxHandle EvpPKeyCtxCreate(SafeEvpPKeyHandle pkey, SafeEvpPKeyHandle peerkey, out uint secretLength);
21+
private static partial SafeEvpPKeyCtxHandle EvpPKeyCtxCreate(SafeEvpPKeyHandle pkey, IntPtr extraHandle, SafeEvpPKeyHandle peerkey, out uint secretLength);
22+
23+
internal static SafeEvpPKeyCtxHandle EvpPKeyCtxCreate(SafeEvpPKeyHandle pkey, SafeEvpPKeyHandle peerkey, out uint secretLength)
24+
=> EvpPKeyCtxCreate(pkey, pkey.ExtraHandle, peerkey, out secretLength);
25+
26+
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyCtxConfigureForECDSASign")]
27+
private static partial int EvpPKeyCtxConfigureForECDSASignCore(SafeEvpPKeyCtxHandle ctx);
28+
29+
internal static void EvpPKeyCtxConfigureForECDSASign(SafeEvpPKeyCtxHandle ctx)
30+
{
31+
Debug.Assert(ctx != null);
32+
Debug.Assert(!ctx.IsInvalid);
33+
34+
if (EvpPKeyCtxConfigureForECDSASignCore(ctx) != 1)
35+
{
36+
throw CreateOpenSslCryptographicException();
37+
}
38+
}
39+
40+
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyCtxConfigureForECDSAVerify")]
41+
private static partial int EvpPKeyCtxConfigureForECDSAVerifyCore(SafeEvpPKeyCtxHandle ctx);
42+
43+
internal static void EvpPKeyCtxConfigureForECDSAVerify(SafeEvpPKeyCtxHandle ctx)
44+
{
45+
Debug.Assert(ctx != null);
46+
Debug.Assert(!ctx.IsInvalid);
47+
48+
if (EvpPKeyCtxConfigureForECDSAVerifyCore(ctx) != 1)
49+
{
50+
throw CreateOpenSslCryptographicException();
51+
}
52+
}
53+
54+
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyCtxSignHash")]
55+
private static unsafe partial int EvpPKeyCtxSignHash(SafeEvpPKeyCtxHandle ctx, byte* hash, int hashLen, byte* destination, ref int destinationLen);
56+
57+
internal static unsafe bool TryEvpPKeyCtxSignHash(SafeEvpPKeyCtxHandle ctx, ReadOnlySpan<byte> hash, Span<byte> destination, out int bytesWritten)
58+
{
59+
Debug.Assert(ctx != null);
60+
Debug.Assert(!ctx.IsInvalid);
61+
62+
if (hash.Length == 0 || destination.Length == 0)
63+
{
64+
bytesWritten = 0;
65+
return false;
66+
}
67+
68+
bytesWritten = destination.Length;
69+
ref byte hashRef = ref MemoryMarshal.GetReference(hash);
70+
ref byte destRef = ref MemoryMarshal.GetReference(destination);
71+
fixed (byte* hashPtr = &hashRef)
72+
fixed (byte* destPtr = &destRef)
73+
{
74+
return EvpPKeyCtxSignHash(ctx, hashPtr, hash.Length, destPtr, ref bytesWritten) == 1;
75+
}
76+
}
77+
78+
internal static unsafe bool TryEvpPKeyCtxSignatureSize(SafeEvpPKeyCtxHandle ctx, ReadOnlySpan<byte> hash, out int bytesWritten)
79+
{
80+
Debug.Assert(ctx != null);
81+
Debug.Assert(!ctx.IsInvalid);
82+
83+
bytesWritten = 0;
84+
85+
if (hash.Length == 0)
86+
{
87+
return false;
88+
}
89+
90+
ref byte hashRef = ref MemoryMarshal.GetReference(hash);
91+
fixed (byte* hashPtr = &hashRef)
92+
{
93+
byte* destPtr = null;
94+
return EvpPKeyCtxSignHash(ctx, hashPtr, hash.Length, destPtr, ref bytesWritten) == 1;
95+
}
96+
}
97+
98+
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyCtxVerifyHash")]
99+
private static partial int EvpPKeyCtxVerifyHash(SafeEvpPKeyCtxHandle ctx, ref byte hash, int hashLen, ref byte signature, int signatureLen);
100+
101+
internal static bool EvpPKeyCtxVerifyHash(SafeEvpPKeyCtxHandle ctx, ReadOnlySpan<byte> hash, ReadOnlySpan<byte> signature)
102+
{
103+
Debug.Assert(ctx != null);
104+
Debug.Assert(!ctx.IsInvalid);
105+
106+
return EvpPKeyCtxVerifyHash(ctx, ref MemoryMarshal.GetReference(hash), hash.Length, ref MemoryMarshal.GetReference(signature), signature.Length) == 1;
107+
}
16108

17109
[LibraryImport(Libraries.CryptoNative, EntryPoint = "CryptoNative_EvpPKeyDeriveSecretAgreement")]
18110
private static partial int EvpPKeyDeriveSecretAgreement(

src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.EvpPkey.Rsa.cs

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ internal static SafeEvpPKeyHandle RsaGenerateKey(int keySize)
4848
[LibraryImport(Libraries.CryptoNative)]
4949
private static partial int CryptoNative_RsaDecrypt(
5050
SafeEvpPKeyHandle pkey,
51+
IntPtr extraHandle,
5152
ref byte source,
5253
int sourceLength,
5354
RSAEncryptionPaddingMode paddingMode,
@@ -64,6 +65,7 @@ internal static int RsaDecrypt(
6465
{
6566
int written = CryptoNative_RsaDecrypt(
6667
pkey,
68+
pkey.ExtraHandle,
6769
ref MemoryMarshal.GetReference(source),
6870
source.Length,
6971
paddingMode,
@@ -83,6 +85,7 @@ ref MemoryMarshal.GetReference(destination),
8385
[LibraryImport(Libraries.CryptoNative)]
8486
private static partial int CryptoNative_RsaEncrypt(
8587
SafeEvpPKeyHandle pkey,
88+
IntPtr extraHandle,
8689
ref byte source,
8790
int sourceLength,
8891
RSAEncryptionPaddingMode paddingMode,
@@ -99,6 +102,7 @@ internal static int RsaEncrypt(
99102
{
100103
int written = CryptoNative_RsaEncrypt(
101104
pkey,
105+
pkey.ExtraHandle,
102106
ref MemoryMarshal.GetReference(source),
103107
source.Length,
104108
paddingMode,
@@ -116,78 +120,53 @@ ref MemoryMarshal.GetReference(destination),
116120
}
117121

118122
[LibraryImport(Libraries.CryptoNative)]
119-
private static partial int CryptoNative_RsaSignHash(
120-
SafeEvpPKeyHandle pkey,
123+
private static partial int CryptoNative_EvpPKeyCtxConfigureForRsaSign(
124+
SafeEvpPKeyCtxHandle ctx,
121125
RSASignaturePaddingMode paddingMode,
122-
IntPtr digestAlgorithm,
123-
ref byte hash,
124-
int hashLength,
125-
ref byte destination,
126-
int destinationLength);
126+
IntPtr digestAlgorithm);
127127

128-
internal static int RsaSignHash(
129-
SafeEvpPKeyHandle pkey,
128+
internal static void CryptoNative_ConfigureForRsaSign(
129+
SafeEvpPKeyCtxHandle ctx,
130130
RSASignaturePaddingMode paddingMode,
131-
IntPtr digestAlgorithm,
132-
ReadOnlySpan<byte> hash,
133-
Span<byte> destination)
131+
HashAlgorithmName digestAlgorithm)
134132
{
135-
int written = CryptoNative_RsaSignHash(
136-
pkey,
137-
paddingMode,
138-
digestAlgorithm,
139-
ref MemoryMarshal.GetReference(hash),
140-
hash.Length,
141-
ref MemoryMarshal.GetReference(destination),
142-
destination.Length);
133+
if (digestAlgorithm.Name == null)
134+
{
135+
throw new ArgumentNullException(nameof(digestAlgorithm));
136+
}
143137

144-
if (written < 0)
138+
IntPtr digestAlgorithmPtr = Interop.Crypto.HashAlgorithmToEvp(digestAlgorithm.Name);
139+
int ret = CryptoNative_EvpPKeyCtxConfigureForRsaSign(ctx, paddingMode, digestAlgorithmPtr);
140+
141+
if (ret != 1)
145142
{
146-
Debug.Assert(written == -1);
147143
throw CreateOpenSslCryptographicException();
148144
}
149-
150-
return written;
151145
}
152146

153147
[LibraryImport(Libraries.CryptoNative)]
154-
private static partial int CryptoNative_RsaVerifyHash(
155-
SafeEvpPKeyHandle pkey,
148+
private static partial int CryptoNative_EvpPKeyCtxConfigureForRsaVerify(
149+
SafeEvpPKeyCtxHandle ctx,
156150
RSASignaturePaddingMode paddingMode,
157-
IntPtr digestAlgorithm,
158-
ref byte hash,
159-
int hashLength,
160-
ref byte signature,
161-
int signatureLength);
151+
IntPtr digestAlgorithm);
162152

163-
internal static bool RsaVerifyHash(
164-
SafeEvpPKeyHandle pkey,
153+
internal static void CryptoNative_ConfigureForRsaVerify(
154+
SafeEvpPKeyCtxHandle ctx,
165155
RSASignaturePaddingMode paddingMode,
166-
IntPtr digestAlgorithm,
167-
ReadOnlySpan<byte> hash,
168-
ReadOnlySpan<byte> signature)
156+
HashAlgorithmName digestAlgorithm)
169157
{
170-
int ret = CryptoNative_RsaVerifyHash(
171-
pkey,
172-
paddingMode,
173-
digestAlgorithm,
174-
ref MemoryMarshal.GetReference(hash),
175-
hash.Length,
176-
ref MemoryMarshal.GetReference(signature),
177-
signature.Length);
178-
179-
if (ret == 1)
158+
if (digestAlgorithm.Name == null)
180159
{
181-
return true;
160+
throw new ArgumentNullException(nameof(digestAlgorithm));
182161
}
183162

184-
if (ret == 0)
163+
IntPtr digestAlgorithmPtr = Interop.Crypto.HashAlgorithmToEvp(digestAlgorithm.Name);
164+
int ret = CryptoNative_EvpPKeyCtxConfigureForRsaVerify(ctx, paddingMode, digestAlgorithmPtr);
165+
166+
if (ret != 1)
185167
{
186-
return false;
168+
throw CreateOpenSslCryptographicException();
187169
}
188-
189-
Debug.Assert(ret == -1);
190-
throw CreateOpenSslCryptographicException();
191170
}
192171
}
193172
}

0 commit comments

Comments
 (0)