-
Notifications
You must be signed in to change notification settings - Fork 5.3k
On Browser DOM scenarios use the SubtleCrypto API #49511
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
1863d35
[WIP] - Start crypto worker and initialize channel.
AaronRobinsonMSFT d5c4199
Make Emscripten minimizer happy by using getters instead of fields.
AaronRobinsonMSFT 4e2d738
Add new System.Security.Cryptography.Native.Browser library.
AaronRobinsonMSFT 5ec3db7
Initialize WebWorker earlier.
AaronRobinsonMSFT c7e3888
Update logging and be less strict for output buffer size.
AaronRobinsonMSFT 47b5e6c
Focus SHA tests for non-browser and browser DOM scenarios.
AaronRobinsonMSFT 891b23c
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT d4ea7d6
Add missing namespaces to test builds.
AaronRobinsonMSFT 8062a2f
Additional locations for that need the crypto_worker.js file.
AaronRobinsonMSFT 2b38fdd
Add new JS and native binaries... everywhere.
AaronRobinsonMSFT 84dd60b
Reference the correct location for the webworker file.
AaronRobinsonMSFT 36b2f56
Specify the correct native directory path for the JS WebWorker file.
AaronRobinsonMSFT 80087a5
Only enable Crypto WebWorker when running in the browser scenario.
AaronRobinsonMSFT 6a3b8a6
Condition more tests based on platform Crypto support needed.
AaronRobinsonMSFT d78f0e2
Rename crypto_worker.js to dotnet_crypto_worker.js
AaronRobinsonMSFT 2e74b1f
Missing installer directory.
AaronRobinsonMSFT 7eb087d
Style
AaronRobinsonMSFT b04d0cc
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT 925dcc0
Change internal API name to use generic "digest" name instead
AaronRobinsonMSFT 753c57b
Add test for not supported platform crypto WRT SHA
AaronRobinsonMSFT 74a4e7e
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT b217fc4
Browser uses Unix binary builds so removing Windows placeholder.
AaronRobinsonMSFT a95e9d6
Add libSystem.Security.Cryptography.Native.Browser.a for Windows build.
AaronRobinsonMSFT ce6d2b1
More Windows build changes.
AaronRobinsonMSFT 42eac88
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT 55dfe74
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT f777cb3
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT c13a76e
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT f2e28fc
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT c749ab5
Merge remote-tracking branch 'upstream/main' into wasm_crypto
AaronRobinsonMSFT 4c5a3e0
Merge remote-tracking branch 'upstream/main' into wasm_crypto
lewing 44ea919
Merge branch 'main' into wasm_crypto
lewing 742cbc3
Fix the js inclusion logic
lewing 76dd72b
Add libSystem.Globalization.Native explicitly
lewing bc7ec42
Merge branch 'main' into wasm_crypto
jeffhandley 3eb7b2f
Merge branch 'main' into wasm_crypto
jeffhandley File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
[WIP] - Start crypto worker and initialize channel.
- Loading branch information
commit 1863d351e6f5277882ae9e86ce6274519f969c6f
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| var ChannelWorker = { | ||
| _impl: class { | ||
| // BEGIN ChannelOwner contract - shared constants. | ||
| STATE_IDX = 0; | ||
| MSG_SIZE_IDX = 1; | ||
|
|
||
| STATE_IDLE = 0; | ||
| STATE_REQ = 1; | ||
| STATE_RESP = 2; | ||
| STATE_REQ_P = 3; // Request has multiple parts | ||
| STATE_RESP_P = 4; // Response has multiple parts | ||
| STATE_AWAIT = 5; // Awaiting the next part | ||
| // END ChannelOwner contract - shared constants. | ||
|
|
||
| constructor(comm_buf, msg_buf, msg_char_len) { | ||
| this.comm = new Int32Array(comm_buf); | ||
| this.msg = new Uint16Array(msg_buf); | ||
| this.msg_char_len = msg_char_len; | ||
| } | ||
|
|
||
| async await_request(async_call) { | ||
| console.log("await_request()"); | ||
|
|
||
| for (;;) { | ||
| // Wait for signal to perform operation | ||
| Atomics.wait(this.comm, this.STATE_IDX, this.STATE_IDLE); | ||
|
|
||
| // Read in request | ||
| var req = this._read_request(); | ||
| console.log("Request: " + req); | ||
|
|
||
| // Perform async action based on request | ||
| var resp = await async_call(req); | ||
|
|
||
| // Send response | ||
| this._send_response(resp); | ||
| } | ||
| } | ||
|
|
||
| _read_request() { | ||
| var request = ""; | ||
| for (;;) { | ||
| // Get the current state and message size | ||
| var state = Atomics.load(this.comm, this.STATE_IDX); | ||
| var size_to_read = Atomics.load(this.comm, this.MSG_SIZE_IDX); | ||
|
|
||
| // Append the latest part of the message. | ||
| request += this._read_from_msg(0, size_to_read); | ||
|
|
||
| // The request is complete. | ||
| if (state === this.STATE_REQ) | ||
| break; | ||
|
|
||
| // Reset the size and transition to await state. | ||
| Atomics.store(this.comm, this.MSG_SIZE_IDX, 0); | ||
| Atomics.store(this.comm, this.STATE_IDX, this.STATE_AWAIT); | ||
| Atomics.wait(this.comm, this.STATE_IDX, this.STATE_AWAIT); | ||
| } | ||
|
|
||
| return request; | ||
| } | ||
|
|
||
| _read_from_msg(begin, end) { | ||
| return String.fromCharCode.apply(null, this.msg.slice(begin, end)); | ||
| } | ||
|
|
||
| _send_response(msg) { | ||
| if (Atomics.load(this.comm, this.STATE_IDX) !== this.STATE_REQ) | ||
| throw "WORKER: Invalid sync communication channel state."; | ||
|
|
||
| var state; // State machine variable | ||
| const msg_len = msg.length; | ||
| var msg_written = 0; | ||
|
|
||
| for (;;) { | ||
| // Write the message and return how much was written. | ||
| var wrote = this._write_to_msg(msg, msg_written, msg_len); | ||
| msg_written += wrote; | ||
|
|
||
| // Indicate how much was written to the this.msg buffer. | ||
| Atomics.store(this.comm, this.MSG_SIZE_IDX, wrote); | ||
|
|
||
| // Indicate if this was the whole message or part of it. | ||
| state = msg_written === msg_len ? this.STATE_RESP : this.STATE_RESP_P; | ||
|
|
||
| // Update the state | ||
| Atomics.store(this.comm, this.STATE_IDX, state); | ||
|
|
||
| // Wait for the transition to know the main thread has | ||
| // received the response by moving onto a new state. | ||
| Atomics.wait(this.comm, this.STATE_IDX, state); | ||
|
|
||
| // Done sending response. | ||
| if (state === this.STATE_RESP) | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| _write_to_msg(input, start, input_len) { | ||
| var mi = 0; | ||
| var ii = start; | ||
| while (mi < this.msg_char_len && ii < input_len) { | ||
| this.msg[mi] = input.charCodeAt(ii); | ||
| ii++; // Next character | ||
| mi++; // Next buffer index | ||
| } | ||
| return ii - start; | ||
| } | ||
| }, | ||
|
|
||
| create: function (comm_buf, msg_buf, msg_char_len) { | ||
| return new this._impl(comm_buf, msg_buf, msg_char_len); | ||
| } | ||
| }; | ||
|
|
||
| // | ||
| // [TODO] Handle crypto calls that uses Promises below. | ||
| // | ||
|
|
||
| // Operation to perform. | ||
| async function async_call(msg) { | ||
| var keyPair = await self.crypto.subtle.generateKey( | ||
| { | ||
| name: "RSA-OAEP", | ||
| modulusLength: 2048, | ||
| publicExponent: new Uint8Array([1, 0, 1]), | ||
| hash: "SHA-256", | ||
| }, | ||
| true, | ||
| ["encrypt", "decrypt"] | ||
| ); | ||
|
|
||
| return msg.split("").reverse().join(""); | ||
| } | ||
|
|
||
| var s_channel; | ||
|
|
||
| // Initialize WebWorker | ||
| onmessage = function (p) { | ||
| console.log(p.data.salutation); | ||
| s_channel = ChannelWorker.create(p.data.comm_buf, p.data.msg_buf, p.data.msg_char_len); | ||
|
|
||
| s_channel.await_request(async_call); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| Module [ 'channel' ] = { | ||
| _impl: class { | ||
| // Index constants for the communication buffer. | ||
| STATE_IDX = 0; | ||
| MSG_SIZE_IDX = 1; | ||
| COMM_LAST_IDX = this.MSG_SIZE_IDX; | ||
|
|
||
| // Communication states. | ||
| STATE_IDLE = 0; | ||
| STATE_REQ = 1; | ||
| STATE_RESP = 2; | ||
| STATE_REQ_P = 3; // Request has multiple parts | ||
| STATE_RESP_P = 4; // Response has multiple parts | ||
| STATE_AWAIT = 5; // Awaiting the next part | ||
|
|
||
| constructor(msg_char_len) { | ||
| this.msg_char_len = msg_char_len; | ||
|
|
||
| const int_bytes = 4; | ||
| const comm_byte_len = int_bytes * (this.COMM_LAST_IDX + 1); | ||
| this.comm_buf = new SharedArrayBuffer(comm_byte_len); | ||
|
|
||
| // JavaScript character encoding is UTF-16. | ||
| const char_bytes = 2; | ||
| const msg_byte_len = char_bytes * this.msg_char_len; | ||
| this.msg_buf = new SharedArrayBuffer(msg_byte_len); | ||
|
|
||
| // Create the local arrays to use. | ||
| this.comm = new Int32Array(this.comm_buf); | ||
| this.msg = new Uint16Array(this.msg_buf); | ||
| } | ||
|
|
||
| get_msg_len() { return this.msg_char_len; } | ||
| get_msg_buffer() { return this.msg_buf; } | ||
| get_comm_buffer() { return this.comm_buf; } | ||
|
|
||
| send_msg(msg) { | ||
| if (Atomics.load(this.comm, this.STATE_IDX) !== this.STATE_IDLE) { | ||
| throw "OWNER: Invalid sync communication channel state."; | ||
| } | ||
| this._send_request(msg); | ||
| return this._read_response(); | ||
| } | ||
|
|
||
| _send_request(msg) { | ||
| var state; | ||
| const msg_len = msg.length; | ||
| var msg_written = 0; | ||
|
|
||
| for (;;) { | ||
| // Write the message and return how much was written. | ||
| var wrote = this._write_to_msg(msg, msg_written, msg_len); | ||
| msg_written += wrote; | ||
|
|
||
| // Indicate how much was written to the this.msg buffer. | ||
| Atomics.store(this.comm, this.MSG_SIZE_IDX, wrote); | ||
|
|
||
| // Indicate if this was the whole message or part of it. | ||
| state = msg_written === msg_len ? this.STATE_REQ : this.STATE_REQ_P; | ||
|
|
||
| // Notify webworker | ||
| Atomics.store(this.comm, this.STATE_IDX, state); | ||
| Atomics.notify(this.comm, this.STATE_IDX); | ||
|
|
||
| // The send message is complete. | ||
| if (state === this.STATE_REQ) | ||
| break; | ||
|
|
||
| // Wait for the worker to be ready for the next part. | ||
| // - Atomics.wait() is not permissible on the main thread. | ||
| do { | ||
| state = Atomics.load(this.comm, this.STATE_IDX); | ||
| } while (state !== this.STATE_AWAIT); | ||
| } | ||
| } | ||
|
|
||
| _write_to_msg(input, start, input_len) { | ||
| var mi = 0; | ||
| var ii = start; | ||
| while (mi < this.msg_char_len && ii < input_len) { | ||
| this.msg[mi] = input.charCodeAt(ii); | ||
| ii++; // Next character | ||
| mi++; // Next buffer index | ||
| } | ||
| return ii - start; | ||
| } | ||
|
|
||
| _read_response() { | ||
| var state; | ||
| var response = ""; | ||
| for (;;) { | ||
| // Wait for webworker response. | ||
| // - Atomics.wait() is not permissible on the main thread. | ||
| do { | ||
| state = Atomics.load(this.comm, this.STATE_IDX); | ||
| } while (state !== this.STATE_RESP && state !== this.STATE_RESP_P); | ||
|
|
||
| var size_to_read = Atomics.load(this.comm, this.MSG_SIZE_IDX); | ||
|
|
||
| // Append the latest part of the message. | ||
| response += this._read_from_msg(0, size_to_read); | ||
|
|
||
| // The response is complete. | ||
| if (state === this.STATE_RESP) | ||
| break; | ||
|
|
||
| // Reset the size and transition to await state. | ||
| Atomics.store(this.comm, this.MSG_SIZE_IDX, 0); | ||
| Atomics.store(this.comm, this.STATE_IDX, this.STATE_AWAIT); | ||
| Atomics.notify(this.comm, this.STATE_IDX); | ||
| } | ||
|
|
||
| // Reset the communication channel's state and let the | ||
| // webworker know we are done. | ||
| Atomics.store(this.comm, this.STATE_IDX, this.STATE_IDLE); | ||
| Atomics.notify(this.comm, this.STATE_IDX); | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| _read_from_msg(begin, end) { | ||
| return String.fromCharCode.apply(null, this.msg.slice(begin, end)); | ||
| } | ||
|
|
||
| }, | ||
|
|
||
| create: function (msg_char_len) { | ||
| if (msg_char_len === undefined) { | ||
| msg_char_len = 1024; // Default size is arbitrary but is in 'char' units (i.e. UTF-16 code points). | ||
| } | ||
| return new this._impl(msg_char_len); | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.