SubtleCrypto.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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/CryptoAlgorithms.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. struct RegisteredAlgorithm {
  21. NonnullOwnPtr<AlgorithmMethods> (*create_methods)(JS::Realm&) = nullptr;
  22. JS::ThrowCompletionOr<NonnullOwnPtr<AlgorithmParams>> (*parameter_from_value)(JS::VM&, JS::Value) = nullptr;
  23. };
  24. using SupportedAlgorithmsMap = HashMap<String, HashMap<String, RegisteredAlgorithm, AK::ASCIICaseInsensitiveStringTraits>>;
  25. public:
  26. [[nodiscard]] static JS::NonnullGCPtr<SubtleCrypto> create(JS::Realm&);
  27. virtual ~SubtleCrypto() override;
  28. JS::NonnullGCPtr<JS::Promise> digest(AlgorithmIdentifier const& algorithm, JS::Handle<WebIDL::BufferSource> const& data);
  29. JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> generate_key(AlgorithmIdentifier algorithm, bool extractable, Vector<Bindings::KeyUsage> key_usages);
  30. JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> import_key(Bindings::KeyFormat format, KeyDataType key_data, AlgorithmIdentifier algorithm, bool extractable, Vector<Bindings::KeyUsage> key_usages);
  31. JS::ThrowCompletionOr<JS::NonnullGCPtr<JS::Promise>> export_key(Bindings::KeyFormat format, JS::NonnullGCPtr<CryptoKey> key);
  32. private:
  33. explicit SubtleCrypto(JS::Realm&);
  34. virtual void initialize(JS::Realm&) override;
  35. struct NormalizedAlgorithmAndParameter {
  36. NonnullOwnPtr<AlgorithmMethods> methods;
  37. NonnullOwnPtr<AlgorithmParams> parameter;
  38. };
  39. WebIDL::ExceptionOr<NormalizedAlgorithmAndParameter> normalize_an_algorithm(AlgorithmIdentifier const& algorithm, String operation);
  40. static SubtleCrypto::SupportedAlgorithmsMap& supported_algorithms_internal();
  41. static SubtleCrypto::SupportedAlgorithmsMap supported_algorithms();
  42. template<typename Methods, typename Param = AlgorithmParams>
  43. static void define_an_algorithm(String op, String algorithm);
  44. };
  45. }