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

[wasm][crypto] RandomNumberGenerator mapped to Web Crypto getRandomValues #42728

Merged
merged 19 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f0a662c
[wasm][crypto] RandomNumberGenerator mapped to Web Crypto getRandomVa…
kjpou1 Sep 25, 2020
eb46e2f
Remove the emscripten interface code from the driver.c.
kjpou1 Sep 28, 2020
ce149c9
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Sep 28, 2020
878cfcf
remove extraneous code comment
kjpou1 Sep 28, 2020
289efef
Move emscripten definition around.
kjpou1 Sep 28, 2020
b5d372b
Address review comment
kjpou1 Sep 28, 2020
f657f4f
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Sep 29, 2020
cf74a12
Add javascript bridge implementation library to Native source tree.
kjpou1 Sep 29, 2020
88fa846
Change tests to set random values of the buffer instead of return a s…
kjpou1 Sep 29, 2020
2b8987d
Remove old test code
kjpou1 Sep 29, 2020
61f1c19
Remove testing code
kjpou1 Sep 29, 2020
ce13ad1
Incorporate the PAL bridge layer into the `--js-library` build process
kjpou1 Sep 29, 2020
4eb6639
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Sep 30, 2020
5e593bb
Address review comments about directory structure and naming
kjpou1 Sep 30, 2020
76de399
Update src/mono/wasm/runtime-test.js
kjpou1 Oct 1, 2020
75a81ce
Merge branch 'master' of https://github.com/dotnet/runtime into wasm-…
kjpou1 Oct 1, 2020
8077f7c
Add note about insecure code for testing purposes
kjpou1 Oct 1, 2020
ec16f1a
Formatting
kjpou1 Oct 1, 2020
7b7e9d2
Return -1 if crypto does not exist instead of aborting from js. This…
kjpou1 Oct 1, 2020
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
19 changes: 16 additions & 3 deletions src/libraries/Native/Unix/System.Native/pal_random.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,24 @@ Return 0 on success, -1 on failure.
*/
int32_t SystemNative_GetCryptographicallySecureRandomBytes(uint8_t* buffer, int32_t bufferLength)
{
assert(buffer != NULL);

#ifdef __EMSCRIPTEN__
extern int32_t dotnet_browser_entropy(uint8_t* buffer, int32_t bufferLength);
static bool sMissingBrowserCrypto;
marek-safar marked this conversation as resolved.
Show resolved Hide resolved
if (!sMissingBrowserCrypto)
{
int32_t bff = dotnet_browser_entropy(buffer, bufferLength);
if (bff == -1)
sMissingBrowserCrypto = true;
else
return 0;
}
#else
kjpou1 marked this conversation as resolved.
Show resolved Hide resolved

static volatile int rand_des = -1;
static bool sMissingDevURandom;

assert(buffer != NULL);

if (!sMissingDevURandom)
{
if (rand_des == -1)
Expand Down Expand Up @@ -121,6 +134,6 @@ int32_t SystemNative_GetCryptographicallySecureRandomBytes(uint8_t* buffer, int3
return 0;
}
}

#endif
return -1;
}
23 changes: 23 additions & 0 deletions src/libraries/Native/Unix/System.Native/pal_random.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

var DotNetEntropyLib = {
$DOTNETENTROPY: {
},
dotnet_browser_entropy : function (buffer, bufferLength) {
// check that we have crypto available
if (typeof crypto === 'object' && typeof crypto['getRandomValues'] === 'function') {
// for modern web browsers
// map the work array to the memory buffer passed with the length
var wrkArray = new Uint8Array(Module.HEAPU8.buffer, buffer, bufferLength);
crypto.getRandomValues(wrkArray);
return 0;
} else {
// we couldn't find a proper implementation, as Math.random() is not suitable
abort("no cryptographic support found for getRandomValues. Consider polyfilling it if you want to use something insecure like Math.random(), e.g. put this in a --pre-js: var crypto = { getRandomValues: function(array) { for (var i = 0; i < array.length; i++) array[i] = (Math.random()*256)|0 } };");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think we should be suggesting an unsecure polyfill in the error message.

I think we should leave it for the documentation to discuss the polyfill workarounds. The recommended workaround should be something that tries to be secure as much as possible, like the MSR library discussed in the other PR. If somebody wants to use insecure polyfill, I would leave it up to them to figure out how to do it.

cc @GrabYourPitchforks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to let normal managed exception to handle error.

WASM-ERR: 
WASM-ERR: Unhandled Exception:
WASM-ERR: System.Security.Cryptography.CryptographicException: Error occurred during a cryptographic operation.
WASM-ERR:    at Interop.GetCryptographicallySecureRandomBytes(Byte* buffer, Int32 length)
WASM-ERR:    at System.Security.Cryptography.RandomNumberGeneratorImplementation.GetBytes(Byte* pbBuffer, Int32 count)
WASM-ERR:    at System.Security.Cryptography.RandomNumberGeneratorImplementation.GetBytes(Span`1 data)
WASM-ERR:    at System.Security.Cryptography.RandomNumberGeneratorImplementation.GetNonZeroBytes(Span`1 data)
WASM-ERR:    at System.Security.Cryptography.RandomNumberGeneratorImplementation.GetNonZeroBytes(Byte[] data)

Does this work?

}
},
};

autoAddDeps(DotNetEntropyLib, '$DOTNETENTROPY')
mergeInto(LibraryManager.library, DotNetEntropyLib)
5 changes: 3 additions & 2 deletions src/mono/wasm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ PINVOKE_TABLE?=$(TOP)/artifacts/obj/wasm/pinvoke-table.h
MONO_BIN_DIR?=$(BINDIR)/mono/Browser.wasm.$(CONFIG)
NATIVE_BIN_DIR?=$(BINDIR)/native/net5.0-Browser-$(CONFIG)-wasm
ICU_LIBDIR?=
SYSTEM_NATIVE_LIBDIR?=$(TOP)/src/libraries/Native/Unix/System.Native
ENABLE_ES6?=false

all: build-native icu-data
Expand Down Expand Up @@ -81,8 +82,8 @@ $(NATIVE_BIN_DIR):
$(BUILDS_OBJ_DIR):
mkdir -p $$@

$(NATIVE_BIN_DIR)/dotnet.js: $(BUILDS_OBJ_DIR)/driver.o $(BUILDS_OBJ_DIR)/pinvoke.o $(BUILDS_OBJ_DIR)/corebindings.o runtime/library_mono.js runtime/binding_support.js runtime/dotnet_support.js $(2) | $(NATIVE_BIN_DIR)
$(EMCC) $(EMCC_FLAGS) $(1) --js-library runtime/library_mono.js --js-library runtime/binding_support.js --js-library runtime/dotnet_support.js $(BUILDS_OBJ_DIR)/driver.o $(BUILDS_OBJ_DIR)/pinvoke.o $(BUILDS_OBJ_DIR)/corebindings.o $(2) -o $(NATIVE_BIN_DIR)/dotnet.js
$(NATIVE_BIN_DIR)/dotnet.js: $(BUILDS_OBJ_DIR)/driver.o $(BUILDS_OBJ_DIR)/pinvoke.o $(BUILDS_OBJ_DIR)/corebindings.o runtime/library_mono.js runtime/binding_support.js runtime/dotnet_support.js $(SYSTEM_NATIVE_LIBDIR)/pal_random.js $(2) | $(NATIVE_BIN_DIR)
$(EMCC) $(EMCC_FLAGS) $(1) --js-library runtime/library_mono.js --js-library runtime/binding_support.js --js-library runtime/dotnet_support.js --js-library $(SYSTEM_NATIVE_LIBDIR)/pal_random.js $(BUILDS_OBJ_DIR)/driver.o $(BUILDS_OBJ_DIR)/pinvoke.o $(BUILDS_OBJ_DIR)/corebindings.o $(2) -o $(NATIVE_BIN_DIR)/dotnet.js

$(BUILDS_OBJ_DIR)/pinvoke-table.h: $(PINVOKE_TABLE) | $(BUILDS_OBJ_DIR)
if cmp -s $(PINVOKE_TABLE) $$@ ; then : ; else cp $(PINVOKE_TABLE) $$@ ; fi
Expand Down
7 changes: 4 additions & 3 deletions src/mono/wasm/runtime-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,13 @@ if (typeof console !== "undefined") {
console.error = console.log;
}

if (typeof crypto == 'undefined') {
if (typeof crypto === 'undefined') {
lewing marked this conversation as resolved.
Show resolved Hide resolved
// /dev/random doesn't work on js shells, so define our own
// See library_fs.js:createDefaultDevices ()
var crypto = {
getRandomValues: function (buffer) {
kjpou1 marked this conversation as resolved.
Show resolved Hide resolved
buffer[0] = (Math.random()*256)|0;
for (var i = 0; i < buffer.length; i++)
buffer[i] = (Math.random()*256)|0;
kjpou1 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -166,7 +167,7 @@ while (args !== undefined && args.length > 0) {
} else if (args [0].startsWith ("--working-dir=")) {
var arg = args [0].substring ("--working-dir=".length);
working_dir = arg;
args = args.slice (1);
args = args.slice (1);
kjpou1 marked this conversation as resolved.
Show resolved Hide resolved
} else {
break;
}
Expand Down
10 changes: 5 additions & 5 deletions src/mono/wasm/runtime/library_mono.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ var MonoSupportLib = {
},
/** @returns {ManagedPointer} */
get: function (index) {
this._check_in_range (index);
this._check_in_range (index);
return Module.HEAP32[this.get_address_32 (index)];
},
set: function (index, value) {
Expand Down Expand Up @@ -317,7 +317,7 @@ var MonoSupportLib = {
throw new Error ("capacity >= 1");

capacity = capacity | 0;

var capacityBytes = capacity * 4;
var offset = Module._malloc (capacityBytes);
if ((offset % 4) !== 0)
Expand All @@ -328,7 +328,7 @@ var MonoSupportLib = {
var result = Object.create (this._mono_wasm_root_buffer_prototype);
result.__offset = offset;
result.__offset32 = (offset / 4) | 0;
result.__count = capacity;
result.__count = capacity;
result.length = capacity;
result.__handle = this.mono_wasm_register_root (offset, capacityBytes, msg || 0);

Expand All @@ -347,7 +347,7 @@ var MonoSupportLib = {
mono_wasm_new_root: function (value) {
var index = this._mono_wasm_claim_scratch_index ();
var buffer = this._scratch_root_buffer;

var result = Object.create (this._mono_wasm_root_prototype);
result.__buffer = buffer;
result.__index = index;
Expand Down Expand Up @@ -395,7 +395,7 @@ var MonoSupportLib = {
* Multiple objects may be passed on the argument list.
* 'undefined' may be passed as an argument so it is safe to call this method from finally blocks
* even if you are not sure all of your roots have been created yet.
* @param {... WasmRoot} roots
* @param {... WasmRoot} roots
*/
mono_wasm_release_roots: function () {
for (var i = 0; i < arguments.length; i++) {
Expand Down