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

Commit 61e0211

Browse files
committed
Merge pull request #2856 from nguerrera/shim-crypto-init
Move EnsureOpenSslInitialized shim from coreclr to corefx
2 parents 3876ac0 + 6fea59e commit 61e0211

File tree

20 files changed

+270
-145
lines changed

20 files changed

+270
-145
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,
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
using System.Security.Cryptography;
7+
8+
internal static partial class Interop
9+
{
10+
// Initialization of libcrypto threading support is done in a static constructor.
11+
// This enables a project simply to include this file, and any usage of any of
12+
// the libcrypto or System.Security.Cryptography.Native functions will trigger
13+
// initialization of the threading support.
14+
15+
// Note that we can collapse everything into Interop.Crypto once we no longer
16+
// have a mix of P/Invokes to libcrypto and System.Security.Crytpography.Native.
17+
// In the meantime, a third Interop.CryptoInitializer class is used to prevent
18+
// a circular initialization dependency between the two.
19+
20+
internal static partial class libcrypto
21+
{
22+
static libcrypto()
23+
{
24+
CryptoInitializer.Initialize();
25+
}
26+
}
27+
28+
internal static partial class Crypto
29+
{
30+
static Crypto()
31+
{
32+
CryptoInitializer.Initialize();
33+
}
34+
}
35+
36+
internal static class CryptoInitializer
37+
{
38+
static CryptoInitializer()
39+
{
40+
if (EnsureOpenSslInitialized() != 0)
41+
{
42+
// Ideally this would be a CryptographicException, but we use
43+
// OpenSSL in libraries lower than System.Security.Cryptography.
44+
// It's not a big deal, though: this will already be wrapped in a
45+
// TypeLoadException, and this failing means something is very
46+
// wrong with the system's configuration and any code using
47+
// these libraries will be unable to operate correctly.
48+
throw new InvalidOperationException();
49+
}
50+
51+
// Load the SHA-2 hash algorithms, and anything else not in the default
52+
// support set.
53+
OPENSSL_add_all_algorithms_conf();
54+
55+
// Ensure that the error message table is loaded.
56+
ERR_load_crypto_strings();
57+
}
58+
59+
internal static void Initialize()
60+
{
61+
// No-op that exists to provide a hook for other static constructors
62+
// to trigger initialization. Once we stop having direct P/Invokes
63+
// to libcrypto and everything is collapsed in to the static constructor
64+
// of Interop.Crypto, this function can be removed.
65+
}
66+
67+
[DllImport(Libraries.CryptoNative)]
68+
private static extern int EnsureOpenSslInitialized();
69+
70+
[DllImport(Libraries.LibCrypto)]
71+
private static extern void ERR_load_crypto_strings();
72+
73+
[DllImport(Libraries.LibCrypto)]
74+
private static extern void OPENSSL_add_all_algorithms_conf();
75+
}
76+
}

src/Common/src/Interop/Unix/libcoreclr/Interop.EnsureOpenSslInitialized.cs

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

src/Common/src/Interop/Unix/libcrypto/Interop.ERR.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ internal static partial class Interop
1010
{
1111
internal static partial class libcrypto
1212
{
13-
[DllImport(Libraries.LibCrypto)]
14-
private static extern void ERR_load_crypto_strings();
15-
1613
[DllImport(Libraries.LibCrypto)]
1714
private static extern uint ERR_get_error();
1815

src/Common/src/Interop/Unix/libcrypto/Interop.Initialization.cs

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

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
}

0 commit comments

Comments
 (0)