Skip to content

Developers targeting browser-wasm can use Web Crypto APIs #40074

Closed as not planned

Description

@marek-safar

We want to avoid shipping OpenSSL for Browser as that’s not something that aligns well with web nature of WebAssembly as well as it has noticeable impacts on the size of the final app. This would also save us from having to deal with zero-day vulnerabilities as well as support for crypto algorithms which are only secure if they use special CPU instructions (RNG and AES for example).

Using the platform native crypto functions is the preferred solution as it does not have any noticeable size and it’s also the most performant solution. All browser have nowadays support for Crypto APIs which we should be able to use to implement the core crypto functions required by BCL.

Relevant documentation can be found at https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto. The tricky part will be to deal with the async nature of these APIs but we could introduce new async APIs to make the integration easier.

.NET has support for old/obscure algorithms and also other features like certificates which are not relevant in browser space and for them, we would keep throwing PNSE.

Design Proposal

The design proposal for how we will enable cryptographic algorithms in .NET for WebAssembly can be found here:
https://github.com/dotnet/designs/blob/main/accepted/2021/blazor-wasm-crypto.md

We intend on providing the most secure implementations of cryptographic algorithms when available. On browsers which support SharedArrayBuffer, we will utilize that as a synchronization mechanism to perform a sync-over-async operation, letting the browser's secure SubtleCrypto implementation act as our cryptographic primitive. On browsers which do not support SharedArrayBuffer, we will fall back to in-box managed algorithm implementations of these primitives.

Where managed algorithm implementations are needed, they can be migrated from the .NET Framework Reference Source.

Work Items

  • SHA1
  • SHA256
  • SHA384
  • SHA512
  • HMACSHA1
  • HMACSHA256
  • HMACSHA384
  • HMACSHA512
  • AES-CBC
    • AES-ECB and AES-CFB should throw PlatformNotSupportedException, as they are not available in webcrypto
  • PBKDF2 (via Rfc2898DeriveBytes)
  • HKDF

Acceptance Criteria

  • Integrate into arch-wasm infrastructure
  • Ensure a productive development/testing workflow with WebAssembly
  • Get all existing unit tests for the algorithms passing in WebAssembly
  • Look for opportunities to improve argument validation
  • Look for opportunities to modernize the code, such as using Span
  • Validate end-to-end scenarios in desktop browsers that support SharedArrayBuffer
  • Validate end-to-end scenarios in mobile browsers that support SharedArrayBuffer
  • Validate end-to-end scenarios in desktop browsers that do not support SharedArrayBuffer
  • Validate end-to-end scenarios in mobile browsers that do not support SharedArrayBuffer

Issues to address

Activity

  1. added this to the 6.0.0 milestone on Jul 29, 2020
  2. removed
    untriagedNew issue has not been triaged by the area owner
    on Jul 29, 2020
  3. jaykrell commented on Aug 6, 2020

    @jaykrell
    Contributor

    only secure if they use special CPU instructions (RNG and AES for example

    Really "only secure"? Or just faster?

  4. bartonjs commented on Aug 7, 2020

    @bartonjs
    Member

    Really "only secure"? Or just faster?

    AES is literally only secure with dedicated hardware instructions on modern CPUs. All software implementations are subject to various timing problems that ultimately leak the key to a dedicated attack (http://cr.yp.to/antiforgery/cachetiming-20050414.pdf).

  5. GrabYourPitchforks commented on Aug 31, 2020

    @GrabYourPitchforks
    Member

    As part of this work item, we should also verify that RandomNumberGenerator is correctly plumbed down to crypto.getRandomValues. Right now it's plumbed through /dev/random, which I think the Mono wasm engine ties through correctly, but it's always good to remove indirection and points of failure where possible.

  6. kjpou1 commented on Oct 14, 2020

    @kjpou1
    Contributor

    Looking into support and providing a prototype that works with browser os.

  7. added
    User StoryA single user-facing feature. Can be grouped under an epic.
    on Nov 5, 2020
  8. changed the title [-]Using Web Crypto APIs for browser-wasm cryptography implementation[/-] [+]Web Crypto APIs are available to users targeting browser-wasm[/+] on Nov 5, 2020
  9. added
    Cost:LWork that requires one engineer up to 4 weeks
    and removed on Nov 5, 2020
  10. 80 remaining items

  11. modified the milestones: 7.0.0, 8.0.0 on Aug 12, 2022
  12. modified the milestones: 8.0.0, 7.0.0 on Aug 13, 2022
  13. jeffhandley commented on Aug 13, 2022

    @jeffhandley
    Member

    We've concluded that using the SharedArrayBuffer approach via a worker is not yielding the end-to-end experience or quality necessary to release this integration. As a result, we've changed the scope of what will be included in .NET 7 to no longer include AES-CBC. Additionally, instead of using SubtleCrypto via the SharedArrayBuffer to reach the native implementations of other algorithms, we will rely wholly on managed implementations.

    Please see #73858 for more information and refer to the design document for more information about the managed implementation expectations.

    Where native implementations need to be accessed, they will need to be called via JavaScript interop.

  14. ghost locked as resolved and limited conversation to collaborators on Sep 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Cost:LWork that requires one engineer up to 4 weeksPriority:1Work that is critical for the release, but we could probably ship withoutTeam:LibrariesUser StoryA single user-facing feature. Can be grouped under an epic.arch-wasmWebAssembly architecturearea-System.Security

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions