Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit abaeaa8

Browse files
committed
Rename crypto shim API following discussion in #2606
Interop.Libraries.CryptoInterop -> Interop.Libraries.CryptoNative Interop.NativeCrypto -> Interop.Crypto
1 parent c2aadf2 commit abaeaa8

File tree

11 files changed

+63
-61
lines changed

11 files changed

+63
-61
lines changed

src/Common/src/Interop/Unix/Interop.Libraries.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ internal static partial class Interop
55
{
66
private static partial class Libraries
77
{
8-
internal const string CryptoInterop = "System.Security.Cryptography.Native";
98
internal const string Libc = "libc"; // C library
109
internal const string LibCoreClr= "libcoreclr"; // CoreCLR runtime
1110
internal const string LibCrypto = "libcrypto"; // OpenSSL crypto library
1211
internal const string LibCurl = "libcurl"; // Curl HTTP client library
1312
internal const string Zlib = "libz"; // zlib compression library
13+
14+
// Shims
1415
internal const string SystemNative = "System.Native";
16+
internal const string CryptoNative = "System.Security.Cryptography.Native";
1517
}
1618
}

src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.NativeCrypto.cs renamed to src/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Crypto.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,85 +8,85 @@
88

99
internal static partial class Interop
1010
{
11-
internal static partial class NativeCrypto
11+
internal static partial class Crypto
1212
{
1313
private delegate int NegativeSizeReadMethod<in THandle>(THandle handle, byte[] buf, int cBuf);
1414

15-
[DllImport(Libraries.CryptoInterop)]
15+
[DllImport(Libraries.CryptoNative)]
1616
internal static extern int BioTell(SafeBioHandle bio);
1717

18-
[DllImport(Libraries.CryptoInterop)]
18+
[DllImport(Libraries.CryptoNative)]
1919
internal static extern int BioSeek(SafeBioHandle bio, int pos);
2020

21-
[DllImport(Libraries.CryptoInterop)]
21+
[DllImport(Libraries.CryptoNative)]
2222
private static extern int GetX509Thumbprint(SafeX509Handle x509, byte[] buf, int cBuf);
2323

24-
[DllImport(Libraries.CryptoInterop)]
24+
[DllImport(Libraries.CryptoNative)]
2525
private static extern int GetX509NameRawBytes(IntPtr x509Name, byte[] buf, int cBuf);
2626

27-
[DllImport(Libraries.CryptoInterop)]
27+
[DllImport(Libraries.CryptoNative)]
2828
internal static extern SafeX509Handle ReadX509AsDerFromBio(SafeBioHandle bio);
2929

30-
[DllImport(Libraries.CryptoInterop)]
30+
[DllImport(Libraries.CryptoNative)]
3131
internal static extern IntPtr GetX509NotBefore(SafeX509Handle x509);
3232

33-
[DllImport(Libraries.CryptoInterop)]
33+
[DllImport(Libraries.CryptoNative)]
3434
internal static extern IntPtr GetX509NotAfter(SafeX509Handle x509);
3535

36-
[DllImport(Libraries.CryptoInterop)]
36+
[DllImport(Libraries.CryptoNative)]
3737
internal static extern int GetX509Version(SafeX509Handle x509);
3838

39-
[DllImport(Libraries.CryptoInterop)]
39+
[DllImport(Libraries.CryptoNative)]
4040
internal static extern IntPtr GetX509SignatureAlgorithm(SafeX509Handle x509);
4141

42-
[DllImport(Libraries.CryptoInterop)]
42+
[DllImport(Libraries.CryptoNative)]
4343
internal static extern IntPtr GetX509PublicKeyAlgorithm(SafeX509Handle x509);
4444

45-
[DllImport(Libraries.CryptoInterop)]
45+
[DllImport(Libraries.CryptoNative)]
4646
private static extern int GetX509PublicKeyParameterBytes(SafeX509Handle x509, byte[] buf, int cBuf);
4747

48-
[DllImport(Libraries.CryptoInterop)]
48+
[DllImport(Libraries.CryptoNative)]
4949
internal static extern IntPtr GetX509PublicKeyBytes(SafeX509Handle x509);
5050

51-
[DllImport(Libraries.CryptoInterop)]
51+
[DllImport(Libraries.CryptoNative)]
5252
internal static extern int GetX509EkuFieldCount(SafeEkuExtensionHandle eku);
5353

54-
[DllImport(Libraries.CryptoInterop)]
54+
[DllImport(Libraries.CryptoNative)]
5555
internal static extern IntPtr GetX509EkuField(SafeEkuExtensionHandle eku, int loc);
5656

57-
[DllImport(Libraries.CryptoInterop)]
57+
[DllImport(Libraries.CryptoNative)]
5858
internal static extern SafeBioHandle GetX509NameInfo(SafeX509Handle x509, int nameType, [MarshalAs(UnmanagedType.Bool)] bool forIssuer);
5959

60-
[DllImport(Libraries.CryptoInterop)]
60+
[DllImport(Libraries.CryptoNative)]
6161
private static extern int GetAsn1StringBytes(IntPtr asn1, byte[] buf, int cBuf);
6262

63-
[DllImport(Libraries.CryptoInterop)]
63+
[DllImport(Libraries.CryptoNative)]
6464
internal static extern SafeX509StackHandle NewX509Stack();
6565

66-
[DllImport(Libraries.CryptoInterop)]
66+
[DllImport(Libraries.CryptoNative)]
6767
internal static extern int GetX509StackFieldCount(SafeX509StackHandle stack);
6868

6969
/// <summary>
7070
/// Gets a pointer to a certificate within a STACK_OF(X509). This pointer will later
7171
/// be freed, so it should be cloned via new X509Certificate2(IntPtr)
7272
/// </summary>
73-
[DllImport(Libraries.CryptoInterop)]
73+
[DllImport(Libraries.CryptoNative)]
7474
internal static extern IntPtr GetX509StackField(SafeX509StackHandle stack, int loc);
7575

76-
[DllImport(Libraries.CryptoInterop)]
76+
[DllImport(Libraries.CryptoNative)]
7777
[return: MarshalAs(UnmanagedType.Bool)]
7878
internal static extern bool PushX509StackField(SafeX509StackHandle stack, SafeX509Handle x509);
7979

80-
[DllImport(Libraries.CryptoInterop)]
80+
[DllImport(Libraries.CryptoNative)]
8181
internal static extern void RecursiveFreeX509Stack(IntPtr stack);
8282

83-
[DllImport(Libraries.CryptoInterop, CharSet = CharSet.Ansi)]
83+
[DllImport(Libraries.CryptoNative, CharSet = CharSet.Ansi)]
8484
internal static extern string GetX509RootStorePath();
8585

86-
[DllImport(Libraries.CryptoInterop)]
86+
[DllImport(Libraries.CryptoNative)]
8787
internal static extern int UpRefEvpPkey(SafeEvpPkeyHandle handle);
8888

89-
[DllImport(Libraries.CryptoInterop)]
89+
[DllImport(Libraries.CryptoNative)]
9090
private static extern int SetX509ChainVerifyTime(
9191
SafeX509StoreCtxHandle ctx,
9292
int year,

src/Common/src/Microsoft/Win32/SafeHandles/SafeEvpPkeyHandle.Unix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ internal static SafeEvpPkeyHandle DuplicateHandle(SafeEvpPkeyHandle handle)
3636
// that we don't lose a tracked reference in low-memory situations.
3737
SafeEvpPkeyHandle safeHandle = new SafeEvpPkeyHandle();
3838

39-
int newRefCount = Interop.NativeCrypto.UpRefEvpPkey(handle);
39+
int newRefCount = Interop.Crypto.UpRefEvpPkey(handle);
4040

4141
// UpRefEvpPkey returns the number of references to this key, if it's less than 2
4242
// (the incoming handle, and this one) then someone has already Disposed() this key

src/Common/src/Microsoft/Win32/SafeHandles/SafeX509Handles.Unix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ private SafeX509StackHandle() :
8282

8383
protected override bool ReleaseHandle()
8484
{
85-
Interop.NativeCrypto.RecursiveFreeX509Stack(handle);
85+
Interop.Crypto.RecursiveFreeX509Stack(handle);
8686
SetHandle(IntPtr.Zero);
8787
return true;
8888
}

src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/CollectionBackedStoreProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ private byte[] ExportX509Der()
6161

6262
private byte[] ExportPfx(string password)
6363
{
64-
using (SafeX509StackHandle publicCerts = Interop.NativeCrypto.NewX509Stack())
64+
using (SafeX509StackHandle publicCerts = Interop.Crypto.NewX509Stack())
6565
{
6666
X509Certificate2 privateCert = null;
6767

@@ -88,7 +88,7 @@ private byte[] ExportPfx(string password)
8888
{
8989
using (SafeX509Handle certHandle = Interop.libcrypto.X509_dup(cert.Handle))
9090
{
91-
if (!Interop.NativeCrypto.PushX509StackField(publicCerts, certHandle))
91+
if (!Interop.Crypto.PushX509StackField(publicCerts, certHandle))
9292
{
9393
throw Interop.libcrypto.CreateOpenSslCryptographicException();
9494
}

src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslPkcs12Reader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,11 @@ public List<OpenSslX509CertificateReader> ReadCertificates()
9898

9999
if (_caStackHandle != null && !_caStackHandle.IsInvalid)
100100
{
101-
int caCertCount = Interop.NativeCrypto.GetX509StackFieldCount(_caStackHandle);
101+
int caCertCount = Interop.Crypto.GetX509StackFieldCount(_caStackHandle);
102102

103103
for (int i = 0; i < caCertCount; i++)
104104
{
105-
IntPtr certPtr = Interop.NativeCrypto.GetX509StackField(_caStackHandle, i);
105+
IntPtr certPtr = Interop.Crypto.GetX509StackField(_caStackHandle, i);
106106

107107
if (certPtr != IntPtr.Zero)
108108
{

src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509CertificateReader.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ internal OpenSslX509CertificateReader(SafeX509Handle handle)
3939
internal static ICertificatePal FromBio(SafeBioHandle bio, string password)
4040
{
4141
// Try reading the value as: PEM-X509, DER-X509, DER-PKCS12.
42-
int bioPosition = Interop.NativeCrypto.BioTell(bio);
42+
int bioPosition = Interop.Crypto.BioTell(bio);
4343

4444
Debug.Assert(bioPosition >= 0);
4545

@@ -51,16 +51,16 @@ internal static ICertificatePal FromBio(SafeBioHandle bio, string password)
5151
}
5252

5353
// Rewind, try again.
54-
Interop.NativeCrypto.BioSeek(bio, bioPosition);
55-
cert = Interop.NativeCrypto.ReadX509AsDerFromBio(bio);
54+
Interop.Crypto.BioSeek(bio, bioPosition);
55+
cert = Interop.Crypto.ReadX509AsDerFromBio(bio);
5656

5757
if (cert != null && !cert.IsInvalid)
5858
{
5959
return new OpenSslX509CertificateReader(cert);
6060
}
6161

6262
// Rewind, try again.
63-
Interop.NativeCrypto.BioSeek(bio, bioPosition);
63+
Interop.Crypto.BioSeek(bio, bioPosition);
6464

6565
OpenSslPkcs12Reader pfx;
6666

@@ -97,7 +97,7 @@ internal static ICertificatePal FromBio(SafeBioHandle bio, string password)
9797
// OpenSSL error in case the last BioSeek would change it.
9898
Exception openSslException = Interop.libcrypto.CreateOpenSslCryptographicException();
9999

100-
Interop.NativeCrypto.BioSeek(bio, bioPosition);
100+
Interop.Crypto.BioSeek(bio, bioPosition);
101101

102102
throw openSslException;
103103
}
@@ -131,15 +131,15 @@ public byte[] Thumbprint
131131
{
132132
get
133133
{
134-
return Interop.NativeCrypto.GetX509Thumbprint(_cert);
134+
return Interop.Crypto.GetX509Thumbprint(_cert);
135135
}
136136
}
137137

138138
public string KeyAlgorithm
139139
{
140140
get
141141
{
142-
IntPtr oidPtr = Interop.NativeCrypto.GetX509PublicKeyAlgorithm(_cert);
142+
IntPtr oidPtr = Interop.Crypto.GetX509PublicKeyAlgorithm(_cert);
143143
return Interop.libcrypto.OBJ_obj2txt_helper(oidPtr);
144144
}
145145
}
@@ -148,16 +148,16 @@ public byte[] KeyAlgorithmParameters
148148
{
149149
get
150150
{
151-
return Interop.NativeCrypto.GetX509PublicKeyParameterBytes(_cert);
151+
return Interop.Crypto.GetX509PublicKeyParameterBytes(_cert);
152152
}
153153
}
154154

155155
public byte[] PublicKeyValue
156156
{
157157
get
158158
{
159-
IntPtr keyBytesPtr = Interop.NativeCrypto.GetX509PublicKeyBytes(_cert);
160-
return Interop.NativeCrypto.GetAsn1StringBytes(keyBytesPtr);
159+
IntPtr keyBytesPtr = Interop.Crypto.GetX509PublicKeyBytes(_cert);
160+
return Interop.Crypto.GetAsn1StringBytes(keyBytesPtr);
161161
}
162162
}
163163

@@ -166,7 +166,7 @@ public byte[] SerialNumber
166166
get
167167
{
168168
IntPtr serialNumberPtr = Interop.libcrypto.X509_get_serialNumber(_cert);
169-
byte[] serial = Interop.NativeCrypto.GetAsn1StringBytes(serialNumberPtr);
169+
byte[] serial = Interop.Crypto.GetAsn1StringBytes(serialNumberPtr);
170170

171171
// Windows returns this in BigInteger Little-Endian,
172172
// OpenSSL returns this in BigInteger Big-Endian.
@@ -179,7 +179,7 @@ public string SignatureAlgorithm
179179
{
180180
get
181181
{
182-
IntPtr oidPtr = Interop.NativeCrypto.GetX509SignatureAlgorithm(_cert);
182+
IntPtr oidPtr = Interop.Crypto.GetX509SignatureAlgorithm(_cert);
183183
return Interop.libcrypto.OBJ_obj2txt_helper(oidPtr);
184184
}
185185
}
@@ -188,15 +188,15 @@ public DateTime NotAfter
188188
{
189189
get
190190
{
191-
return ExtractValidityDateTime(Interop.NativeCrypto.GetX509NotAfter(_cert));
191+
return ExtractValidityDateTime(Interop.Crypto.GetX509NotAfter(_cert));
192192
}
193193
}
194194

195195
public DateTime NotBefore
196196
{
197197
get
198198
{
199-
return ExtractValidityDateTime(Interop.NativeCrypto.GetX509NotBefore(_cert));
199+
return ExtractValidityDateTime(Interop.Crypto.GetX509NotBefore(_cert));
200200
}
201201
}
202202

@@ -209,7 +209,7 @@ public int Version
209209
{
210210
get
211211
{
212-
int version = Interop.NativeCrypto.GetX509Version(_cert);
212+
int version = Interop.Crypto.GetX509Version(_cert);
213213

214214
if (version < 0)
215215
{
@@ -284,7 +284,7 @@ public IEnumerable<X509Extension> Extensions
284284

285285
Interop.libcrypto.CheckValidOpenSslHandle(dataPtr);
286286

287-
byte[] extData = Interop.NativeCrypto.GetAsn1StringBytes(dataPtr);
287+
byte[] extData = Interop.Crypto.GetAsn1StringBytes(dataPtr);
288288
bool critical = Interop.libcrypto.X509_EXTENSION_get_critical(ext);
289289
X509Extension extension = new X509Extension(oid, extData, critical);
290290

@@ -320,7 +320,7 @@ public RSA GetRSAPrivateKey()
320320

321321
public string GetNameInfo(X509NameType nameType, bool forIssuer)
322322
{
323-
using (SafeBioHandle bioHandle = Interop.NativeCrypto.GetX509NameInfo(_cert, (int)nameType, forIssuer))
323+
using (SafeBioHandle bioHandle = Interop.Crypto.GetX509NameInfo(_cert, (int)nameType, forIssuer))
324324
{
325325
if (bioHandle.IsInvalid)
326326
{
@@ -387,13 +387,13 @@ private static X500DistinguishedName LoadX500Name(IntPtr namePtr)
387387
{
388388
Interop.libcrypto.CheckValidOpenSslHandle(namePtr);
389389

390-
byte[] buf = Interop.NativeCrypto.GetX509NameRawBytes(namePtr);
390+
byte[] buf = Interop.Crypto.GetX509NameRawBytes(namePtr);
391391
return new X500DistinguishedName(buf);
392392
}
393393

394394
private static DateTime ExtractValidityDateTime(IntPtr validityDatePtr)
395395
{
396-
byte[] bytes = Interop.NativeCrypto.GetAsn1StringBytes(validityDatePtr);
396+
byte[] bytes = Interop.Crypto.GetAsn1StringBytes(validityDatePtr);
397397

398398
// RFC 5280 (X509v3 - https://tools.ietf.org/html/rfc5280)
399399
// states that the validity times are either UTCTime:YYMMDDHHMMSSZ (13 bytes)

src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509ChainProcessor.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static IChainPal BuildChain(
7676
throw Interop.libcrypto.CreateOpenSslCryptographicException();
7777
}
7878

79-
Interop.NativeCrypto.SetX509ChainVerifyTime(storeCtx, verificationTime);
79+
Interop.Crypto.SetX509ChainVerifyTime(storeCtx, verificationTime);
8080

8181
int verify = Interop.libcrypto.X509_verify_cert(storeCtx);
8282

@@ -87,7 +87,7 @@ public static IChainPal BuildChain(
8787

8888
using (SafeX509StackHandle chainStack = Interop.libcrypto.X509_STORE_CTX_get1_chain(storeCtx))
8989
{
90-
int chainSize = Interop.NativeCrypto.GetX509StackFieldCount(chainStack);
90+
int chainSize = Interop.Crypto.GetX509StackFieldCount(chainStack);
9191
int errorDepth = -1;
9292
Interop.libcrypto.X509VerifyStatusCode errorCode = 0;
9393
string errorMsg = null;
@@ -117,7 +117,7 @@ public static IChainPal BuildChain(
117117
AddUniqueStatus(overallStatus, ref chainStatus);
118118
}
119119

120-
IntPtr elementCertPtr = Interop.NativeCrypto.GetX509StackField(chainStack, i);
120+
IntPtr elementCertPtr = Interop.Crypto.GetX509StackField(chainStack, i);
121121

122122
if (elementCertPtr == IntPtr.Zero)
123123
{

src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/OpenSslX509Encoder.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public unsafe void DecodeX509KeyUsageExtension(byte[] encoded, out X509KeyUsageF
9090
{
9191
Interop.libcrypto.CheckValidOpenSslHandle(bitString);
9292

93-
byte[] decoded = Interop.NativeCrypto.GetAsn1StringBytes(bitString.DangerousGetHandle());
93+
byte[] decoded = Interop.Crypto.GetAsn1StringBytes(bitString.DangerousGetHandle());
9494

9595
// Only 9 bits are defined.
9696
if (decoded.Length > 2)
@@ -193,11 +193,11 @@ public unsafe void DecodeX509EnhancedKeyUsageExtension(byte[] encoded, out OidCo
193193
{
194194
Interop.libcrypto.CheckValidOpenSslHandle(eku);
195195

196-
int count = Interop.NativeCrypto.GetX509EkuFieldCount(eku);
196+
int count = Interop.Crypto.GetX509EkuFieldCount(eku);
197197

198198
for (int i = 0; i < count; i++)
199199
{
200-
IntPtr oidPtr = Interop.NativeCrypto.GetX509EkuField(eku, i);
200+
IntPtr oidPtr = Interop.Crypto.GetX509EkuField(eku, i);
201201

202202
if (oidPtr == IntPtr.Zero)
203203
{
@@ -224,7 +224,7 @@ public unsafe void DecodeX509SubjectKeyIdentifierExtension(byte[] encoded, out b
224224
{
225225
Interop.libcrypto.CheckValidOpenSslHandle(octetString);
226226

227-
subjectKeyIdentifier = Interop.NativeCrypto.GetAsn1StringBytes(octetString.DangerousGetHandle());
227+
subjectKeyIdentifier = Interop.Crypto.GetAsn1StringBytes(octetString.DangerousGetHandle());
228228
}
229229
}
230230

src/System.Security.Cryptography.X509Certificates/src/Internal/Cryptography/Pal.Unix/StorePal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static void LoadMachineStores()
145145

146146
try
147147
{
148-
directoryInfo = new DirectoryInfo(Interop.NativeCrypto.GetX509RootStorePath());
148+
directoryInfo = new DirectoryInfo(Interop.Crypto.GetX509RootStorePath());
149149
}
150150
catch (ArgumentException)
151151
{

0 commit comments

Comments
 (0)