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

Use SubtleCrypto API on browser DOM scenarios #65966

Merged
merged 22 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
6035e71
Use SubtleCrypto API on browser DOM scenarios
layomia Feb 23, 2022
2f99382
Add sync over async implementation
layomia Apr 12, 2022
f7e68f9
Address misc feedback and make fixes
layomia Apr 13, 2022
f1653c5
Address pinvoke errors
layomia Apr 21, 2022
d1da46a
[Attempt] Correct execution of native digest API call at wasm layer
layomia Apr 22, 2022
c23607f
[Fix up] Correct execution of native digest API call at wasm layer
layomia Apr 26, 2022
9cf4875
Update src/tests/BuildWasmApps/Wasm.Build.Tests/BuildTestBase.cs
ericstj May 5, 2022
807a566
Address feedback and clean up
layomia May 5, 2022
6a74390
Re-implement the crypto worker in ts
layomia May 6, 2022
0f85951
Address feedback
layomia May 7, 2022
72e47b8
Revert "Re-implement the crypto worker in ts"
layomia May 7, 2022
cf7cf37
* moved stuff around and renamed it
pavelsavara May 9, 2022
3f14a3a
Add code to handle errors in worker (particularly on init)
layomia May 11, 2022
0f11bca
Clean up
layomia May 12, 2022
7d964be
Add crypto dll to wasm native project
layomia May 12, 2022
797220e
Merge remote-tracking branch 'upstream/main' into WasmCrypto
layomia May 12, 2022
c7901c2
Add e2e test
layomia May 14, 2022
e7f89f5
Adjust test to reflect lack of SharedArrayBuffer for Chrome in test h…
layomia May 17, 2022
a22037b
Enable Chrome test and validate hashed value in tests
layomia May 17, 2022
3b71f3e
Merge remote-tracking branch 'upstream/main' into WasmCrypto
layomia May 18, 2022
7b1a026
Merge branch 'main' into WasmCrypto
ericstj May 24, 2022
c2ec92f
fix merge to track assert being renamed to mono_assert
radical May 24, 2022
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
2 changes: 1 addition & 1 deletion eng/SignCheckExclusionsFile.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
*apphosttemplateapphostexe.exe;;Template, DO-NOT-SIGN, https://github.com/dotnet/core-setup/pull/7549
*comhosttemplatecomhostdll.dll;;Template, DO-NOT-SIGN, https://github.com/dotnet/core-setup/pull/7549
*staticapphosttemplateapphostexe.exe;;Template, DO-NOT-SIGN, https://github.com/dotnet/core-setup/pull/7549
*dotnet.js;;Workaround, https://github.com/dotnet/core-eng/issues/9933
*dotnet.js;;Workaround, https://github.com/dotnet/core-eng/issues/9933
1 change: 1 addition & 0 deletions eng/liveBuilds.targets
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@
<LibrariesRuntimeFiles Condition="'$(TargetOS)' == 'Browser'"
Include="
$(LibrariesNativeArtifactsPath)dotnet.js;
$(LibrariesNativeArtifactsPath)src\dotnet-crypto-worker.js;
$(LibrariesNativeArtifactsPath)dotnet.d.ts;
$(LibrariesNativeArtifactsPath)package.json;
$(LibrariesNativeArtifactsPath)dotnet.wasm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<PlatformManifestFileEntry Include="libSystem.Security.Cryptography.Native.Apple.dylib" IsNative="true" />
<PlatformManifestFileEntry Include="libSystem.Security.Cryptography.Native.Android.a" IsNative="true" />
<PlatformManifestFileEntry Include="libSystem.Security.Cryptography.Native.Android.so" IsNative="true" />
<PlatformManifestFileEntry Include="libSystem.Security.Cryptography.Native.Browser.a" IsNative="true" />
<PlatformManifestFileEntry Include="libSystem.Security.Cryptography.Native.OpenSsl.a" IsNative="true" />
<PlatformManifestFileEntry Include="libSystem.Security.Cryptography.Native.OpenSsl.dylib" IsNative="true" />
<PlatformManifestFileEntry Include="libSystem.Security.Cryptography.Native.OpenSsl.so" IsNative="true" />
Expand Down Expand Up @@ -210,6 +211,7 @@
<PlatformManifestFileEntry Include="libmono-wasm-eh-js.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-wasm.a" IsNative="true" />
<PlatformManifestFileEntry Include="System.Private.Runtime.InteropServices.Javascript.dll" />
<PlatformManifestFileEntry Include="dotnet-crypto-worker.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.worker.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.js.symbols" IsNative="true" />
Expand All @@ -223,6 +225,7 @@
<PlatformManifestFileEntry Include="icudt_optimal.dat" IsNative="true" />
<PlatformManifestFileEntry Include="icudt_optimal_no_CJK.dat" IsNative="true" />
<PlatformManifestFileEntry Include="package.json" IsNative="true" />
<PlatformManifestFileEntry Include="pal_crypto_webworker.js" IsNative="true" />
<PlatformManifestFileEntry Include="pal_random.lib.js" IsNative="true" />
<PlatformManifestFileEntry Include="runtime.cjs.iffe.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.cjs.lib.js" IsNative="true" />
Expand Down
12 changes: 12 additions & 0 deletions src/libraries/Common/src/Interop/Browser/Interop.Libraries.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

internal static partial class Interop
{
internal static partial class Libraries
{
// Shims
internal const string SystemNative = "libSystem.Native";
internal const string CryptoNative = "libSystem.Security.Cryptography.Native.Browser";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// 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;

internal static partial class Interop
{
internal static partial class BrowserCrypto
{
internal enum SimpleDigest
layomia marked this conversation as resolved.
Show resolved Hide resolved
{
Sha1,
Sha256,
Sha384,
Sha512,
};

[LibraryImport(Libraries.CryptoNative, EntryPoint = "SystemCryptoNativeBrowser_CanUseSimpleDigestHash")]
internal static partial int CanUseSimpleDigestHash();

[LibraryImport(Libraries.CryptoNative, EntryPoint = "SystemCryptoNativeBrowser_SimpleDigestHash")]
internal static unsafe partial int SimpleDigestHash(
SimpleDigest hash,
byte* input_buffer,
int input_len,
byte* output_buffer,
int output_len);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<DefineConstants>$(DefineConstants);INTERNAL_ASYMMETRIC_IMPLEMENTATIONS</DefineConstants>
Expand Down Expand Up @@ -529,12 +529,15 @@
</Compile>
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Browser'">
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Link="Common\Interop\Unix\Interop.Libraries.cs" />
<!-- GetRandomBytes is identical on Unix and Browser -->
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.GetRandomBytes.cs"
Link="Common\Interop\Unix\System.Native\Interop.GetRandomBytes.cs" />
<Compile Include="$(CommonPath)Interop\Browser\Interop.Libraries.cs"
Link="Common\Interop\Browser\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)System\Sha1ForNonSecretPurposes.cs"
Link="Common\System\Sha1ForNonSecretPurposes.cs" />
<Compile Include="$(CommonPath)Interop\Browser\System.Security.Cryptography.Native.Browser\Interop.SimpleDigestHash.cs"
Link="Common\Interop\Browser\System.Security.Cryptography.Native.Browser\Interop.SimpleDigestHash.cs" />
<Compile Include="System\Security\Cryptography\AesCcm.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\AesGcm.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\AesImplementation.NotSupported.cs" />
Expand All @@ -560,7 +563,8 @@
<Compile Include="System\Security\Cryptography\RC2Implementation.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\RSACryptoServiceProvider.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\RSA.Create.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\SHAHashProvider.Browser.cs" />
<Compile Include="System\Security\Cryptography\SHAHashProvider.Browser.Managed.cs" />
<Compile Include="System\Security\Cryptography\SHAHashProvider.Browser.Native.cs" />
<Compile Include="System\Security\Cryptography\TripleDESCryptoServiceProvider.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\TripleDesImplementation.NotSupported.cs" />
<Compile Include="System\Security\Cryptography\X509Certificates\CertificatePal.NotSupported.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
// 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;
using Internal.Cryptography;

namespace System.Security.Cryptography
{
internal static partial class HashProviderDispenser
{
internal static readonly bool CanUseSubtleCryptoImpl = Interop.BrowserCrypto.CanUseSimpleDigestHash() == 1;
layomia marked this conversation as resolved.
Show resolved Hide resolved

public static HashProvider CreateHashProvider(string hashAlgorithmId)
{
switch (hashAlgorithmId)
Expand All @@ -20,7 +17,9 @@ public static HashProvider CreateHashProvider(string hashAlgorithmId)
case HashAlgorithmNames.SHA256:
case HashAlgorithmNames.SHA384:
case HashAlgorithmNames.SHA512:
return new SHAHashProvider(hashAlgorithmId);
return CanUseSubtleCryptoImpl
? new SHANativeHashProvider(hashAlgorithmId)
: new SHAManagedHashProvider(hashAlgorithmId);
}
throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmId));
}
Expand All @@ -38,7 +37,7 @@ public static unsafe int MacData(

public static int HashData(string hashAlgorithmId, ReadOnlySpan<byte> source, Span<byte> destination)
{
HashProvider provider = HashProviderDispenser.CreateHashProvider(hashAlgorithmId);
HashProvider provider = CreateHashProvider(hashAlgorithmId);
provider.AppendHashData(source);
return provider.FinalizeHashAndReset(destination);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Licensed to the .NET Foundation under one or more agreements.
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;
Expand All @@ -9,13 +9,13 @@

namespace System.Security.Cryptography
{
internal sealed class SHAHashProvider : HashProvider
internal sealed class SHAManagedHashProvider : HashProvider
{
private int hashSizeInBytes;
private SHAManagedImplementationBase impl;
private MemoryStream? buffer;

public SHAHashProvider(string hashAlgorithmId)
public SHAManagedHashProvider(string hashAlgorithmId)
{
switch (hashAlgorithmId)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// 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.IO;
using System.Diagnostics;
using System.Security.Cryptography;

using SimpleDigest = Interop.BrowserCrypto.SimpleDigest;

namespace Internal.Cryptography
layomia marked this conversation as resolved.
Show resolved Hide resolved
{
internal sealed class SHANativeHashProvider : HashProvider
{
private readonly int hashSizeInBytes;
private readonly SimpleDigest impl;
private MemoryStream? buffer;
layomia marked this conversation as resolved.
Show resolved Hide resolved

public SHANativeHashProvider(string hashAlgorithmId)
{
Debug.Assert(HashProviderDispenser.CanUseSubtleCryptoImpl);

switch (hashAlgorithmId)
{
case HashAlgorithmNames.SHA1:
impl = SimpleDigest.Sha1;
hashSizeInBytes = 20;
break;
case HashAlgorithmNames.SHA256:
impl = SimpleDigest.Sha256;
hashSizeInBytes = 32;
break;
case HashAlgorithmNames.SHA384:
impl = SimpleDigest.Sha384;
hashSizeInBytes = 48;
break;
case HashAlgorithmNames.SHA512:
impl = SimpleDigest.Sha512;
hashSizeInBytes = 64;
break;
default:
throw new CryptographicException(SR.Format(SR.Cryptography_UnknownHashAlgorithm, hashAlgorithmId));
}
}

public override void AppendHashData(ReadOnlySpan<byte> data)
{
buffer ??= new MemoryStream(1000);
layomia marked this conversation as resolved.
Show resolved Hide resolved
buffer.Write(data);
}

public override int FinalizeHashAndReset(Span<byte> destination)
{
GetCurrentHash(destination);
buffer = null;

return hashSizeInBytes;
}

public override int GetCurrentHash(Span<byte> destination)
{
Debug.Assert(destination.Length >= hashSizeInBytes);

byte[] srcArray = Array.Empty<byte>();
int srcLength = 0;
if (buffer != null)
{
srcArray = buffer.GetBuffer();
srcLength = (int)buffer.Length;
}

unsafe
{
fixed (byte* src = srcArray)
fixed (byte* dest = destination)
{
int res = Interop.BrowserCrypto.SimpleDigestHash(impl, src, srcLength, dest, destination.Length);
Debug.Assert(res != 0);
}
}

return hashSizeInBytes;
}

public override int HashSizeInBytes => hashSizeInBytes;

public override void Dispose(bool disposing)
{
}

public override void Reset()
{
buffer = null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public void InvalidHashCoreArgumentsFromDerivedType()
[Fact]
public void InvalidHashCoreArgumentsFromStream()
{
//Diagnostics.Debugger.Launch();
layomia marked this conversation as resolved.
Show resolved Hide resolved
using (SHA1 sha1 = SHA1.Create())
{
AssertExtensions.Throws<ArgumentException>(null, () => sha1.ComputeHash(new BadReadStream(BadReadStream.ErrorCondition.TooLargeValueFromRead)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<!-- DesignTimeBuild requires all the TargetFramework Derived Properties to not be present in the first property group. -->
<PropertyGroup>
<TargetPlatformIdentifier>$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)'))</TargetPlatformIdentifier>
<WasmXHarnessArgs>$(WasmXHarnessArgs) --web-server-use-cop</WasmXHarnessArgs>
</PropertyGroup>
<PropertyGroup Condition="'$(TargetPlatformIdentifier)' == 'Android'">
<UseAndroidCrypto>true</UseAndroidCrypto>
Expand Down
2 changes: 2 additions & 0 deletions src/mono/wasm/build/WasmApp.targets
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@

<_HasDotnetWasm Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.wasm'">true</_HasDotnetWasm>
<_HasDotnetJsWorker Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.worker.js'">true</_HasDotnetJsWorker>
<_HasDotnetJsCryptoWorker Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet-crypto-worker.js'">true</_HasDotnetJsCryptoWorker>
<_HasDotnetJsSymbols Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.js.symbols'">true</_HasDotnetJsSymbols>
<_HasDotnetJs Condition="'%(WasmNativeAsset.FileName)%(WasmNativeAsset.Extension)' == 'dotnet.js'">true</_HasDotnetJs>
</PropertyGroup>
Expand All @@ -270,6 +271,7 @@
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.wasm" Condition="'$(_HasDotnetWasm)' != 'true'" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js" Condition="'$(_HasDotnetJs)' != 'true'" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.worker.js" Condition="'$(_HasDotnetJsWorker)' != 'true' and Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.worker.js')" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet-crypto-worker.js" Condition="'$(_HasDotnetJsCryptoWorker)' != 'true' and Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet-crypto-worker.js')" />
<WasmNativeAsset Include="$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js.symbols"
Condition="'$(WasmEmitSymbolMap)' == 'true' and '$(_HasDotnetJs)' != 'true' and Exists('$(MicrosoftNetCoreAppRuntimePackRidNativeDir)dotnet.js.symbols')" />

Expand Down
7 changes: 4 additions & 3 deletions src/mono/wasm/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ target_link_libraries(dotnet
${MONO_ARTIFACTS_DIR}/libmono-wasm-eh-js.a
${MONO_ARTIFACTS_DIR}/libmono-profiler-aot.a
${NATIVE_BIN_DIR}/libSystem.Native.a
${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a)
${NATIVE_BIN_DIR}/libSystem.IO.Compression.Native.a
${NATIVE_BIN_DIR}/libSystem.Security.Cryptography.Native.Browser.a)

set_target_properties(dotnet PROPERTIES
LINK_DEPENDS "${NATIVE_BIN_DIR}/src/emcc-default.rsp;${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.pre.js;${NATIVE_BIN_DIR}/src/cjs/runtime.cjs.iffe.js;${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.lib.js;${NATIVE_BIN_DIR}/src/pal_random.lib.js;${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.post.js;${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.extpost.js;"
LINK_FLAGS "@${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-link.rsp ${CONFIGURATION_LINK_FLAGS} --extern-pre-js ${NATIVE_BIN_DIR}/src/cjs/runtime.cjs.iffe.js --pre-js ${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.pre.js --js-library ${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.lib.js --js-library ${NATIVE_BIN_DIR}/src/pal_random.lib.js --post-js ${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.post.js --extern-post-js ${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.extpost.js "
LINK_DEPENDS "${NATIVE_BIN_DIR}/src/emcc-default.rsp;${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.pre.js;${NATIVE_BIN_DIR}/src/cjs/runtime.cjs.iffe.js;${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.lib.js;${NATIVE_BIN_DIR}/src/pal_random.lib.js;${NATIVE_BIN_DIR}/src/pal_crypto_webworker.js;${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.post.js;${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.extpost.js;"
LINK_FLAGS "@${NATIVE_BIN_DIR}/src/emcc-default.rsp @${NATIVE_BIN_DIR}/src/emcc-link.rsp ${CONFIGURATION_LINK_FLAGS} --extern-pre-js ${NATIVE_BIN_DIR}/src/cjs/runtime.cjs.iffe.js --pre-js ${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.pre.js --js-library ${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.lib.js --js-library ${NATIVE_BIN_DIR}/src/pal_random.lib.js --js-library ${NATIVE_BIN_DIR}/src/pal_crypto_webworker.js --post-js ${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.post.js --extern-post-js ${NATIVE_BIN_DIR}/src/cjs/dotnet.cjs.extpost.js "
RUNTIME_OUTPUT_DIRECTORY "${NATIVE_BIN_DIR}")

if(CMAKE_BUILD_TYPE STREQUAL "Release")
Expand Down
Loading