ladybird/Libraries/LibWeb/Crypto/SubtleCrypto.h
Shannon Booth 97abc707c7 LibWeb/Bindings: Generate buffer typedefs as variants
Represent BufferSource and ArrayBufferView as ordinary IDL typedefs over
their underlying union types, instead of special casing in the IDL
generator. This allows the union conversion/return machinery handle
these types consistently with other typedefs, which removes buffer
specific paths from the IDL generator.

This necessitates changing the WebIDL::BufferSource and
WebIDL::ArrayBufferView classes as views over these variants. This
replaces the old GC backed BufferableObject wrapper structure and
provide convenience helpers to determine things such as the byte length,
byte offset, backing buffer, and typed-array APIs.
2026-05-30 11:22:08 +02:00

65 lines
3.8 KiB
C++

/*
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibJS/Forward.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Bindings/SubtleCrypto.h>
#include <LibWeb/Crypto/CryptoAlgorithms.h>
#include <LibWeb/Crypto/CryptoKey.h>
namespace Web::Crypto {
class SubtleCrypto final : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(SubtleCrypto, Bindings::PlatformObject);
GC_DECLARE_ALLOCATOR(SubtleCrypto);
public:
using ImportKeyData = FlattenVariant<WebIDL::BufferSourceVariant, Variant<Bindings::JsonWebKey>>;
[[nodiscard]] static GC::Ref<SubtleCrypto> create(JS::Realm&);
virtual ~SubtleCrypto() override;
GC::Ref<WebIDL::Promise> encrypt(AlgorithmIdentifier const& algorithm, GC::Ref<CryptoKey> key, WebIDL::BufferSource data_parameter);
GC::Ref<WebIDL::Promise> decrypt(AlgorithmIdentifier const& algorithm, GC::Ref<CryptoKey> key, WebIDL::BufferSource data_parameter);
GC::Ref<WebIDL::Promise> sign(AlgorithmIdentifier const& algorithm, GC::Ref<CryptoKey> key, WebIDL::BufferSource data_parameter);
GC::Ref<WebIDL::Promise> verify(AlgorithmIdentifier const& algorithm, GC::Ref<CryptoKey> key, WebIDL::BufferSource signature, WebIDL::BufferSource data_parameter);
GC::Ref<WebIDL::Promise> digest(AlgorithmIdentifier const& algorithm, WebIDL::BufferSource data);
GC::Ref<WebIDL::Promise> generate_key(AlgorithmIdentifier algorithm, bool extractable, Vector<Bindings::KeyUsage> key_usages);
GC::Ref<WebIDL::Promise> derive_bits(AlgorithmIdentifier algorithm, GC::Ref<CryptoKey> base_key, Optional<u32> length_optional);
GC::Ref<WebIDL::Promise> derive_key(AlgorithmIdentifier algorithm, GC::Ref<CryptoKey> base_key, AlgorithmIdentifier derived_key_type, bool extractable, Vector<Bindings::KeyUsage> key_usages);
JS::ThrowCompletionOr<GC::Ref<WebIDL::Promise>> import_key(Bindings::KeyFormat format, ImportKeyData key_data, AlgorithmIdentifier algorithm, bool extractable, Vector<Bindings::KeyUsage> key_usages);
GC::Ref<WebIDL::Promise> export_key(Bindings::KeyFormat format, GC::Ref<CryptoKey> key);
GC::Ref<WebIDL::Promise> wrap_key(Bindings::KeyFormat format, GC::Ref<CryptoKey> key, GC::Ref<CryptoKey> wrapping_key, AlgorithmIdentifier wrap_algorithm);
GC::Ref<WebIDL::Promise> unwrap_key(Bindings::KeyFormat format, WebIDL::BufferSource wrapped_key, GC::Ref<CryptoKey> unwrapping_key, AlgorithmIdentifier unwrap_algorithm, AlgorithmIdentifier unwrapped_key_algorithm, bool extractable, Vector<Bindings::KeyUsage> key_usages);
GC::Ref<WebIDL::Promise> encapsulate_key(AlgorithmIdentifier encapsulation_algorithm, GC::Ref<CryptoKey> encapsulation_key, AlgorithmIdentifier shared_key_algorithm, bool extractable, Vector<Bindings::KeyUsage> key_usages);
GC::Ref<WebIDL::Promise> encapsulate_bits(AlgorithmIdentifier encapsulation_algorithm, GC::Ref<CryptoKey> encapsulation_key);
GC::Ref<WebIDL::Promise> decapsulate_key(AlgorithmIdentifier decapsulation_algorithm, GC::Ref<CryptoKey> decapsulation_key, WebIDL::BufferSource ciphertext, AlgorithmIdentifier shared_key_algorithm, bool extractable, Vector<Bindings::KeyUsage> const& usages);
GC::Ref<WebIDL::Promise> decapsulate_bits(AlgorithmIdentifier decapsulation_algorithm, GC::Ref<CryptoKey> decapsulation_key, WebIDL::BufferSource ciphertext);
private:
explicit SubtleCrypto(JS::Realm&);
virtual void initialize(JS::Realm&) override;
};
struct NormalizedAlgorithmAndParameter {
NonnullOwnPtr<AlgorithmMethods> methods;
NonnullOwnPtr<AlgorithmParams> parameter;
};
WebIDL::ExceptionOr<NormalizedAlgorithmAndParameter> normalize_an_algorithm(JS::Realm&, AlgorithmIdentifier const& algorithm, String operation);
}