2021-12-14 01:09:55 +03:00
|
|
|
/*
|
2022-08-22 20:31:08 +03:00
|
|
|
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
|
2021-12-14 01:09:55 +03:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibCrypto/Hash/HashManager.h>
|
|
|
|
#include <LibJS/Runtime/ArrayBuffer.h>
|
|
|
|
#include <LibJS/Runtime/Promise.h>
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-26 03:11:21 +03:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2021-12-14 01:09:55 +03:00
|
|
|
#include <LibWeb/Crypto/SubtleCrypto.h>
|
2022-09-24 18:14:37 +03:00
|
|
|
#include <LibWeb/WebIDL/AbstractOperations.h>
|
2023-02-12 23:25:57 +03:00
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2021-12-14 01:09:55 +03:00
|
|
|
|
|
|
|
namespace Web::Crypto {
|
|
|
|
|
2023-11-19 21:47:52 +03:00
|
|
|
JS_DEFINE_ALLOCATOR(SubtleCrypto);
|
|
|
|
|
2023-08-13 14:05:26 +03:00
|
|
|
JS::NonnullGCPtr<SubtleCrypto> SubtleCrypto::create(JS::Realm& realm)
|
2022-09-03 20:59:53 +03:00
|
|
|
{
|
2023-08-13 14:05:26 +03:00
|
|
|
return realm.heap().allocate<SubtleCrypto>(realm, realm);
|
2022-09-03 20:59:53 +03:00
|
|
|
}
|
|
|
|
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-26 03:11:21 +03:00
|
|
|
SubtleCrypto::SubtleCrypto(JS::Realm& realm)
|
|
|
|
: PlatformObject(realm)
|
2022-09-03 20:59:53 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SubtleCrypto::~SubtleCrypto() = default;
|
|
|
|
|
2023-08-07 09:41:28 +03:00
|
|
|
void SubtleCrypto::initialize(JS::Realm& realm)
|
2023-01-10 14:28:20 +03:00
|
|
|
{
|
2023-08-07 09:41:28 +03:00
|
|
|
Base::initialize(realm);
|
2023-01-10 14:28:20 +03:00
|
|
|
set_prototype(&Bindings::ensure_web_prototype<Bindings::SubtleCryptoPrototype>(realm, "SubtleCrypto"));
|
|
|
|
}
|
|
|
|
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-26 03:11:21 +03:00
|
|
|
// https://w3c.github.io/webcrypto/#dfn-SubtleCrypto-method-digest
|
2023-02-24 14:18:43 +03:00
|
|
|
JS::NonnullGCPtr<JS::Promise> SubtleCrypto::digest(String const& algorithm, JS::Handle<JS::Object> const& data)
|
2021-12-14 01:09:55 +03:00
|
|
|
{
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-26 03:11:21 +03:00
|
|
|
auto& realm = this->realm();
|
2021-12-14 01:09:55 +03:00
|
|
|
|
|
|
|
// 1. Let algorithm be the algorithm parameter passed to the digest() method.
|
|
|
|
|
|
|
|
// 2. Let data be the result of getting a copy of the bytes held by the data parameter passed to the digest() method.
|
2022-09-24 18:14:37 +03:00
|
|
|
auto data_buffer_or_error = WebIDL::get_buffer_source_copy(*data.cell());
|
2022-07-22 22:01:36 +03:00
|
|
|
if (data_buffer_or_error.is_error()) {
|
2023-09-06 07:03:01 +03:00
|
|
|
auto error = WebIDL::OperationError::create(realm, "Failed to copy bytes from ArrayBuffer"_fly_string);
|
2022-12-13 23:49:50 +03:00
|
|
|
auto promise = JS::Promise::create(realm);
|
2022-09-04 22:48:54 +03:00
|
|
|
promise->reject(error.ptr());
|
2021-12-14 01:09:55 +03:00
|
|
|
return promise;
|
|
|
|
}
|
2022-07-22 22:01:36 +03:00
|
|
|
auto& data_buffer = data_buffer_or_error.value();
|
2021-12-14 01:09:55 +03:00
|
|
|
|
|
|
|
// 3. Let normalizedAlgorithm be the result of normalizing an algorithm, with alg set to algorithm and op set to "digest".
|
|
|
|
// FIXME: This is way more generic than it needs to be right now, so we simplify it.
|
|
|
|
::Crypto::Hash::HashKind hash_kind;
|
2023-02-23 23:31:57 +03:00
|
|
|
auto algorithm_as_string_view = algorithm.bytes_as_string_view();
|
2023-03-10 10:48:54 +03:00
|
|
|
if (algorithm_as_string_view.equals_ignoring_ascii_case("SHA-1"sv)) {
|
2021-12-14 01:09:55 +03:00
|
|
|
hash_kind = ::Crypto::Hash::HashKind::SHA1;
|
2023-03-10 10:48:54 +03:00
|
|
|
} else if (algorithm_as_string_view.equals_ignoring_ascii_case("SHA-256"sv)) {
|
2021-12-14 01:09:55 +03:00
|
|
|
hash_kind = ::Crypto::Hash::HashKind::SHA256;
|
2023-03-10 10:48:54 +03:00
|
|
|
} else if (algorithm_as_string_view.equals_ignoring_ascii_case("SHA-384"sv)) {
|
2021-12-14 01:09:55 +03:00
|
|
|
hash_kind = ::Crypto::Hash::HashKind::SHA384;
|
2023-03-10 10:48:54 +03:00
|
|
|
} else if (algorithm_as_string_view.equals_ignoring_ascii_case("SHA-512"sv)) {
|
2021-12-14 01:09:55 +03:00
|
|
|
hash_kind = ::Crypto::Hash::HashKind::SHA512;
|
|
|
|
}
|
|
|
|
// 4. If an error occurred, return a Promise rejected with normalizedAlgorithm.
|
|
|
|
else {
|
2023-09-06 07:03:01 +03:00
|
|
|
auto error = WebIDL::NotSupportedError::create(realm, MUST(String::formatted("Invalid hash function '{}'", algorithm)));
|
2022-12-13 23:49:50 +03:00
|
|
|
auto promise = JS::Promise::create(realm);
|
2022-09-04 22:48:54 +03:00
|
|
|
promise->reject(error.ptr());
|
2021-12-14 01:09:55 +03:00
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. Let promise be a new Promise.
|
2022-12-13 23:49:50 +03:00
|
|
|
auto promise = JS::Promise::create(realm);
|
2021-12-14 01:09:55 +03:00
|
|
|
|
|
|
|
// 6. Return promise and perform the remaining steps in parallel.
|
|
|
|
// FIXME: We don't have a good abstraction for this yet, so we do it in sync.
|
|
|
|
|
|
|
|
// 7. If the following steps or referenced procedures say to throw an error, reject promise with the returned error and then terminate the algorithm.
|
|
|
|
|
|
|
|
// 8. Let result be the result of performing the digest operation specified by normalizedAlgorithm using algorithm, with data as message.
|
2021-12-15 15:36:07 +03:00
|
|
|
::Crypto::Hash::Manager hash { hash_kind };
|
2022-07-22 22:01:36 +03:00
|
|
|
hash.update(data_buffer);
|
2021-12-15 15:36:07 +03:00
|
|
|
|
2021-12-14 01:09:55 +03:00
|
|
|
auto digest = hash.digest();
|
2021-12-15 15:36:07 +03:00
|
|
|
auto result_buffer = ByteBuffer::copy(digest.immutable_data(), hash.digest_size());
|
2022-01-20 20:47:39 +03:00
|
|
|
if (result_buffer.is_error()) {
|
2023-09-06 07:03:01 +03:00
|
|
|
auto error = WebIDL::OperationError::create(realm, "Failed to create result buffer"_fly_string);
|
2022-09-04 22:48:54 +03:00
|
|
|
promise->reject(error.ptr());
|
2021-12-14 01:09:55 +03:00
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
2022-12-13 23:49:50 +03:00
|
|
|
auto result = JS::ArrayBuffer::create(realm, result_buffer.release_value());
|
2021-12-14 01:09:55 +03:00
|
|
|
|
|
|
|
// 9. Resolve promise with result.
|
|
|
|
promise->fulfill(result);
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|