SubtleCrypto.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  3. * Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashMap.h>
  9. #include <AK/String.h>
  10. #include <AK/Vector.h>
  11. #include <LibJS/Forward.h>
  12. #include <LibWeb/Bindings/PlatformObject.h>
  13. #include <LibWeb/Bindings/SubtleCryptoPrototype.h>
  14. #include <LibWeb/Crypto/CryptoBindings.h>
  15. #include <LibWeb/Crypto/CryptoKey.h>
  16. namespace Web::Crypto {
  17. class SubtleCrypto final : public Bindings::PlatformObject {
  18. WEB_PLATFORM_OBJECT(SubtleCrypto, Bindings::PlatformObject);
  19. JS_DECLARE_ALLOCATOR(SubtleCrypto);
  20. using SupportedAlgorithmsMap = HashMap<String, HashMap<String, String, AK::ASCIICaseInsensitiveStringTraits>>;
  21. using KeyDataType = Variant<JS::Handle<WebIDL::BufferSource>, Bindings::JsonWebKey>;
  22. using AlgorithmIdentifier = Variant<JS::Handle<JS::Object>, String>;
  23. public:
  24. [[nodiscard]] static JS::NonnullGCPtr<SubtleCrypto> create(JS::Realm&);
  25. virtual ~SubtleCrypto() override;
  26. JS::NonnullGCPtr<JS::Promise> digest(AlgorithmIdentifier const& algorithm, JS::Handle<WebIDL::BufferSource> const& data);
  27. JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> import_key(Bindings::KeyFormat format, KeyDataType keyData, AlgorithmIdentifier algorithm, bool extractable, Vector<Bindings::KeyUsage> keyUsages);
  28. private:
  29. explicit SubtleCrypto(JS::Realm&);
  30. virtual void initialize(JS::Realm&) override;
  31. JS::ThrowCompletionOr<Bindings::Algorithm> normalize_an_algorithm(AlgorithmIdentifier const& algorithm, String operation);
  32. WebIDL::ExceptionOr<JS::NonnullGCPtr<CryptoKey>> pbkdf2_import_key(Variant<ByteBuffer, Bindings::JsonWebKey, Empty> key_data, AlgorithmIdentifier algorithm, Bindings::KeyFormat format, bool extractable, Vector<Bindings::KeyUsage> usages);
  33. static SubtleCrypto::SupportedAlgorithmsMap& supported_algorithms_internal();
  34. static SubtleCrypto::SupportedAlgorithmsMap supported_algorithms();
  35. static void define_an_algorithm(String op, String algorithm, String type);
  36. };
  37. }