SubtleCrypto.cpp 3.7 KB

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