SubtleCrypto.cpp 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibCrypto/Hash/HashManager.h>
  7. #include <LibJS/Runtime/ArrayBuffer.h>
  8. #include <LibJS/Runtime/Promise.h>
  9. #include <LibWeb/Bindings/DOMExceptionWrapper.h>
  10. #include <LibWeb/Bindings/IDLAbstractOperations.h>
  11. #include <LibWeb/Bindings/MainThreadVM.h>
  12. #include <LibWeb/Bindings/Wrapper.h>
  13. #include <LibWeb/Crypto/SubtleCrypto.h>
  14. #include <LibWeb/DOM/DOMException.h>
  15. namespace Web::Crypto {
  16. JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handle<JS::Object> const& data)
  17. {
  18. auto& vm = Bindings::main_thread_vm();
  19. auto& realm = *vm.current_realm();
  20. // 1. Let algorithm be the algorithm parameter passed to the digest() method.
  21. // 2. Let data be the result of getting a copy of the bytes held by the data parameter passed to the digest() method.
  22. auto data_buffer_or_error = Bindings::IDL::get_buffer_source_copy(*data.cell());
  23. if (data_buffer_or_error.is_error()) {
  24. auto* error = wrap(realm, DOM::OperationError::create("Failed to copy bytes from ArrayBuffer"));
  25. auto* promise = JS::Promise::create(realm);
  26. promise->reject(error);
  27. return promise;
  28. }
  29. auto& data_buffer = data_buffer_or_error.value();
  30. // 3. Let normalizedAlgorithm be the result of normalizing an algorithm, with alg set to algorithm and op set to "digest".
  31. // FIXME: This is way more generic than it needs to be right now, so we simplify it.
  32. ::Crypto::Hash::HashKind hash_kind;
  33. if (algorithm.equals_ignoring_case("SHA-1"sv)) {
  34. hash_kind = ::Crypto::Hash::HashKind::SHA1;
  35. } else if (algorithm.equals_ignoring_case("SHA-256"sv)) {
  36. hash_kind = ::Crypto::Hash::HashKind::SHA256;
  37. } else if (algorithm.equals_ignoring_case("SHA-384"sv)) {
  38. hash_kind = ::Crypto::Hash::HashKind::SHA384;
  39. } else if (algorithm.equals_ignoring_case("SHA-512"sv)) {
  40. hash_kind = ::Crypto::Hash::HashKind::SHA512;
  41. }
  42. // 4. If an error occurred, return a Promise rejected with normalizedAlgorithm.
  43. else {
  44. auto* error = wrap(realm, DOM::NotSupportedError::create(String::formatted("Invalid hash function '{}'", algorithm)));
  45. auto* promise = JS::Promise::create(realm);
  46. promise->reject(error);
  47. return promise;
  48. }
  49. // 5. Let promise be a new Promise.
  50. auto* promise = JS::Promise::create(realm);
  51. // 6. Return promise and perform the remaining steps in parallel.
  52. // FIXME: We don't have a good abstraction for this yet, so we do it in sync.
  53. // 7. If the following steps or referenced procedures say to throw an error, reject promise with the returned error and then terminate the algorithm.
  54. // 8. Let result be the result of performing the digest operation specified by normalizedAlgorithm using algorithm, with data as message.
  55. ::Crypto::Hash::Manager hash { hash_kind };
  56. hash.update(data_buffer);
  57. auto digest = hash.digest();
  58. auto result_buffer = ByteBuffer::copy(digest.immutable_data(), hash.digest_size());
  59. if (result_buffer.is_error()) {
  60. auto* error = wrap(realm, DOM::OperationError::create("Failed to create result buffer"));
  61. promise->reject(error);
  62. return promise;
  63. }
  64. auto* result = JS::ArrayBuffer::create(realm, result_buffer.release_value());
  65. // 9. Resolve promise with result.
  66. promise->fulfill(result);
  67. return promise;
  68. }
  69. }