SubtleCrypto.cpp 3.9 KB

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