SubtleCrypto.cpp 3.4 KB

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