SubtleCrypto.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/DOMException.h>
  13. namespace Web::Crypto {
  14. JS::NonnullGCPtr<SubtleCrypto> SubtleCrypto::create(JS::Realm& realm)
  15. {
  16. return realm.heap().allocate<SubtleCrypto>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors();
  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::Promise* SubtleCrypto::digest(DeprecatedString 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. if (algorithm.equals_ignoring_case("SHA-1"sv)) {
  47. hash_kind = ::Crypto::Hash::HashKind::SHA1;
  48. } else if (algorithm.equals_ignoring_case("SHA-256"sv)) {
  49. hash_kind = ::Crypto::Hash::HashKind::SHA256;
  50. } else if (algorithm.equals_ignoring_case("SHA-384"sv)) {
  51. hash_kind = ::Crypto::Hash::HashKind::SHA384;
  52. } else if (algorithm.equals_ignoring_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, DeprecatedString::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");
  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. }