SubtleCrypto.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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/Intrinsics.h>
  10. #include <LibWeb/Crypto/SubtleCrypto.h>
  11. #include <LibWeb/WebIDL/AbstractOperations.h>
  12. #include <LibWeb/WebIDL/ExceptionOr.h>
  13. namespace Web::Crypto {
  14. WebIDL::ExceptionOr<JS::NonnullGCPtr<SubtleCrypto>> SubtleCrypto::create(JS::Realm& realm)
  15. {
  16. return MUST_OR_THROW_OOM(realm.heap().allocate<SubtleCrypto>(realm, realm));
  17. }
  18. SubtleCrypto::SubtleCrypto(JS::Realm& realm)
  19. : PlatformObject(realm)
  20. {
  21. }
  22. SubtleCrypto::~SubtleCrypto() = default;
  23. JS::ThrowCompletionOr<void> SubtleCrypto::initialize(JS::Realm& realm)
  24. {
  25. MUST_OR_THROW_OOM(Base::initialize(realm));
  26. set_prototype(&Bindings::ensure_web_prototype<Bindings::SubtleCryptoPrototype>(realm, "SubtleCrypto"));
  27. return {};
  28. }
  29. // https://w3c.github.io/webcrypto/#dfn-SubtleCrypto-method-digest
  30. JS::NonnullGCPtr<JS::Promise> SubtleCrypto::digest(String const& algorithm, JS::Handle<JS::Object> const& data)
  31. {
  32. auto& realm = this->realm();
  33. // 1. Let algorithm be the algorithm parameter passed to the digest() method.
  34. // 2. Let data be the result of getting a copy of the bytes held by the data parameter passed to the digest() method.
  35. auto data_buffer_or_error = WebIDL::get_buffer_source_copy(*data.cell());
  36. if (data_buffer_or_error.is_error()) {
  37. auto error = WebIDL::OperationError::create(realm, "Failed to copy bytes from ArrayBuffer");
  38. auto promise = JS::Promise::create(realm);
  39. promise->reject(error.ptr());
  40. return promise;
  41. }
  42. auto& data_buffer = data_buffer_or_error.value();
  43. // 3. Let normalizedAlgorithm be the result of normalizing an algorithm, with alg set to algorithm and op set to "digest".
  44. // FIXME: This is way more generic than it needs to be right now, so we simplify it.
  45. ::Crypto::Hash::HashKind hash_kind;
  46. auto algorithm_as_string_view = algorithm.bytes_as_string_view();
  47. if (algorithm_as_string_view.equals_ignoring_ascii_case("SHA-1"sv)) {
  48. hash_kind = ::Crypto::Hash::HashKind::SHA1;
  49. } else if (algorithm_as_string_view.equals_ignoring_ascii_case("SHA-256"sv)) {
  50. hash_kind = ::Crypto::Hash::HashKind::SHA256;
  51. } else if (algorithm_as_string_view.equals_ignoring_ascii_case("SHA-384"sv)) {
  52. hash_kind = ::Crypto::Hash::HashKind::SHA384;
  53. } else if (algorithm_as_string_view.equals_ignoring_ascii_case("SHA-512"sv)) {
  54. hash_kind = ::Crypto::Hash::HashKind::SHA512;
  55. }
  56. // 4. If an error occurred, return a Promise rejected with normalizedAlgorithm.
  57. else {
  58. auto error = WebIDL::NotSupportedError::create(realm, DeprecatedString::formatted("Invalid hash function '{}'", algorithm));
  59. auto promise = JS::Promise::create(realm);
  60. promise->reject(error.ptr());
  61. return promise;
  62. }
  63. // 5. Let promise be a new Promise.
  64. auto promise = JS::Promise::create(realm);
  65. // 6. Return promise and perform the remaining steps in parallel.
  66. // FIXME: We don't have a good abstraction for this yet, so we do it in sync.
  67. // 7. If the following steps or referenced procedures say to throw an error, reject promise with the returned error and then terminate the algorithm.
  68. // 8. Let result be the result of performing the digest operation specified by normalizedAlgorithm using algorithm, with data as message.
  69. ::Crypto::Hash::Manager hash { hash_kind };
  70. hash.update(data_buffer);
  71. auto digest = hash.digest();
  72. auto result_buffer = ByteBuffer::copy(digest.immutable_data(), hash.digest_size());
  73. if (result_buffer.is_error()) {
  74. auto error = WebIDL::OperationError::create(realm, "Failed to create result buffer");
  75. promise->reject(error.ptr());
  76. return promise;
  77. }
  78. auto result = JS::ArrayBuffer::create(realm, result_buffer.release_value());
  79. // 9. Resolve promise with result.
  80. promise->fulfill(result);
  81. return promise;
  82. }
  83. }