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

Commit d5377b4

Browse files
committed
Use CommonCrypto CSPRNG for GetTempFileName on macOS
In the Unix build use the existing calculation of IsMac to determine if Interop.Crypto (powered by OpenSSL) or CommonCrypto should be used for the CSPRNG. GetTempFileName is the most often reported hit of a failure in Interop+Crypto.cctor , but that didn't benefit from when the macOS implementation of System.Security.Cryptography.RandomNumberGenerator changed to use CommonCrypto (via the new Apple-specific shim). This rectifies that, and should take a significant burden off of the first run experiences for macOS users who have not installed OpenSSL in a manner where we can find it.
1 parent bdd952e commit d5377b4

File tree

4 files changed

+56
-1
lines changed

4 files changed

+56
-1
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Runtime.InteropServices;
6+
7+
internal static partial class Interop
8+
{
9+
// Direct support on top of Apple CommonCrypto.
10+
// In general, this should not be used, the System.Security.Cryptography.Native.Apple shim
11+
// being preferred. But when there is a layering complication, or other compelling reason,
12+
// then this can be used directly.
13+
internal static partial class CommonCrypto
14+
{
15+
[DllImport(Libraries.LibSystemCommonCrypto)]
16+
internal static unsafe extern int CCRandomGenerateBytes(byte* bytes, int byteCount);
17+
}
18+
}

src/Common/src/Interop/OSX/Interop.Libraries.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private static partial class Libraries
99
internal const string CoreFoundationLibrary = "/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation";
1010
internal const string CoreServicesLibrary = "/System/Library/Frameworks/CoreServices.framework/CoreServices";
1111
internal const string libproc = "libproc";
12+
internal const string LibSystemCommonCrypto = "/usr/lib/system/libcommonCrypto";
1213
internal const string LibSystemKernel = "/usr/lib/system/libsystem_kernel";
1314
internal const string SystemConfigurationLibrary = "/System/Library/Frameworks/SystemConfiguration.framework/SystemConfiguration";
1415
internal const string AppleCryptoNative = "System.Security.Cryptography.Native.Apple";

src/System.Runtime.Extensions/src/System.Runtime.Extensions.csproj

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,12 @@
117117
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' ">
118118
<Compile Include="System\Diagnostics\Stopwatch.Unix.cs" />
119119
<Compile Include="System\IO\Path.Unix.cs" />
120+
<Compile Include="$(CommonPath)\Interop\OSX\Interop.Libraries.cs">
121+
<Link>Common\Interop\OSX\Interop.Libraries.cs</Link>
122+
</Compile>
123+
<Compile Include="$(CommonPath)\Interop\OSX\Interop.CommonCrypto.cs">
124+
<Link>Common\Interop\OSX\Interop.CommonCrypto.cs</Link>
125+
</Compile>
120126
<Compile Include="$(CommonPath)\Interop\Unix\Interop.Libraries.cs">
121127
<Link>Common\Interop\Unix\Interop.Libraries.cs</Link>
122128
</Compile>
@@ -135,6 +141,9 @@
135141
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.GetTimestamp.cs">
136142
<Link>Common\Interop\Unix\System.Native\Interop.GetTimestamp.cs</Link>
137143
</Compile>
144+
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.GetUnixName.cs">
145+
<Link>Common\Interop\Unix\System.Native\Interop.GetUnixName.cs</Link>
146+
</Compile>
138147
<Compile Include="$(CommonPath)\Interop\Unix\System.Native\Interop.MksTemps.cs">
139148
<Link>Common\Interop\Unix\System.Native\Interop.MksTemps.cs</Link>
140149
</Compile>
@@ -188,4 +197,4 @@
188197
<None Include="project.json" />
189198
</ItemGroup>
190199
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
191-
</Project>
200+
</Project>

src/System.Runtime.Extensions/src/System/IO/Path.Unix.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,29 @@ public static string GetPathRoot(string path)
205205
}
206206

207207
private static unsafe void GetCryptoRandomBytes(byte* bytes, int byteCount)
208+
{
209+
if (IsMac)
210+
{
211+
GetCryptoRandomBytesApple(bytes, byteCount);
212+
}
213+
else
214+
{
215+
GetCryptoRandomBytesOpenSsl(bytes, byteCount);
216+
}
217+
}
218+
219+
private static unsafe void GetCryptoRandomBytesApple(byte* bytes, int byteCount)
220+
{
221+
Debug.Assert(bytes != null);
222+
Debug.Assert(byteCount >= 0);
223+
224+
if (Interop.CommonCrypto.CCRandomGenerateBytes(bytes, byteCount) != 0)
225+
{
226+
throw new InvalidOperationException(SR.InvalidOperation_Cryptography);
227+
}
228+
}
229+
230+
private static unsafe void GetCryptoRandomBytesOpenSsl(byte* bytes, int byteCount)
208231
{
209232
Debug.Assert(bytes != null);
210233
Debug.Assert(byteCount >= 0);
@@ -214,5 +237,9 @@ private static unsafe void GetCryptoRandomBytes(byte* bytes, int byteCount)
214237
throw new InvalidOperationException(SR.InvalidOperation_Cryptography);
215238
}
216239
}
240+
241+
// This lives as an internal property on Environment in master after forking for release/1.1.0
242+
// It is copied here only for the backport and release branch fix.
243+
private static readonly bool IsMac = Interop.Sys.GetUnixName() == "OSX";
217244
}
218245
}

0 commit comments

Comments
 (0)