CryptoKey.h 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibCrypto/PK/RSA.h>
  8. #include <LibJS/Forward.h>
  9. #include <LibJS/Heap/GCPtr.h>
  10. #include <LibWeb/Bindings/CryptoKeyPrototype.h>
  11. #include <LibWeb/Bindings/Intrinsics.h>
  12. #include <LibWeb/Bindings/PlatformObject.h>
  13. #include <LibWeb/Bindings/Serializable.h>
  14. #include <LibWeb/Crypto/CryptoBindings.h>
  15. namespace Web::Crypto {
  16. class CryptoKey final
  17. : public Bindings::PlatformObject
  18. , public Bindings::Serializable {
  19. WEB_PLATFORM_OBJECT(CryptoKey, Bindings::PlatformObject);
  20. JS_DECLARE_ALLOCATOR(CryptoKey);
  21. public:
  22. using InternalKeyData = Variant<ByteBuffer, Bindings::JsonWebKey, ::Crypto::PK::RSAPublicKey<>, ::Crypto::PK::RSAPrivateKey<>>;
  23. [[nodiscard]] static JS::NonnullGCPtr<CryptoKey> create(JS::Realm&, InternalKeyData);
  24. [[nodiscard]] static JS::NonnullGCPtr<CryptoKey> create(JS::Realm&);
  25. virtual ~CryptoKey() override;
  26. bool extractable() const { return m_extractable; }
  27. Bindings::KeyType type() const { return m_type; }
  28. JS::Object const* algorithm() const { return m_algorithm; }
  29. JS::Object const* usages() const { return m_usages; }
  30. Vector<Bindings::KeyUsage> internal_usages() const { return m_key_usages; }
  31. void set_extractable(bool extractable) { m_extractable = extractable; }
  32. void set_type(Bindings::KeyType type) { m_type = type; }
  33. void set_algorithm(JS::NonnullGCPtr<Object> algorithm) { m_algorithm = move(algorithm); }
  34. void set_usages(Vector<Bindings::KeyUsage>);
  35. InternalKeyData const& handle() const { return m_key_data; }
  36. String algorithm_name() const;
  37. virtual StringView interface_name() const override { return "CryptoKey"sv; }
  38. virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::SerializationRecord& record, bool for_storage, HTML::SerializationMemory&) override;
  39. virtual WebIDL::ExceptionOr<void> deserialization_steps(ReadonlySpan<u32> const& record, size_t& position, HTML::DeserializationMemory&) override;
  40. private:
  41. CryptoKey(JS::Realm&, InternalKeyData);
  42. explicit CryptoKey(JS::Realm&);
  43. virtual void initialize(JS::Realm&) override;
  44. virtual void visit_edges(Visitor&) override;
  45. Bindings::KeyType m_type;
  46. bool m_extractable { false };
  47. JS::NonnullGCPtr<Object> m_algorithm;
  48. JS::NonnullGCPtr<Object> m_usages;
  49. Vector<Bindings::KeyUsage> m_key_usages;
  50. InternalKeyData m_key_data; // [[handle]]
  51. mutable String m_algorithm_name;
  52. };
  53. // https://w3c.github.io/webcrypto/#ref-for-dfn-CryptoKeyPair-2
  54. class CryptoKeyPair : public JS::Object {
  55. JS_OBJECT(CryptoKeyPair, JS::Object);
  56. JS_DECLARE_ALLOCATOR(CryptoKeyPair);
  57. public:
  58. static JS::NonnullGCPtr<CryptoKeyPair> create(JS::Realm&, JS::NonnullGCPtr<CryptoKey> public_key, JS::NonnullGCPtr<CryptoKey> private_key);
  59. virtual ~CryptoKeyPair() override = default;
  60. JS::NonnullGCPtr<CryptoKey> public_key() const { return m_public_key; }
  61. JS::NonnullGCPtr<CryptoKey> private_key() const { return m_private_key; }
  62. private:
  63. CryptoKeyPair(JS::Realm&, JS::NonnullGCPtr<CryptoKey> public_key, JS::NonnullGCPtr<CryptoKey> private_key);
  64. virtual void initialize(JS::Realm&) override;
  65. virtual void visit_edges(Visitor&) override;
  66. JS_DECLARE_NATIVE_FUNCTION(public_key_getter);
  67. JS_DECLARE_NATIVE_FUNCTION(private_key_getter);
  68. JS::NonnullGCPtr<CryptoKey> m_public_key;
  69. JS::NonnullGCPtr<CryptoKey> m_private_key;
  70. };
  71. }