SubtleCrypto.cpp 4.0 KB

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