SubtleCrypto.cpp 3.3 KB

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