CryptoKey.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. virtual StringView interface_name() const override { return "CryptoKey"sv; }
  37. virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::SerializationRecord& record, bool for_storage, HTML::SerializationMemory&) override;
  38. virtual WebIDL::ExceptionOr<void> deserialization_steps(ReadonlySpan<u32> const& record, size_t& position, HTML::DeserializationMemory&) override;
  39. private:
  40. CryptoKey(JS::Realm&, InternalKeyData);
  41. explicit CryptoKey(JS::Realm&);
  42. virtual void initialize(JS::Realm&) override;
  43. virtual void visit_edges(Visitor&) override;
  44. Bindings::KeyType m_type;
  45. bool m_extractable { false };
  46. JS::NonnullGCPtr<Object> m_algorithm;
  47. JS::NonnullGCPtr<Object> m_usages;
  48. Vector<Bindings::KeyUsage> m_key_usages;
  49. InternalKeyData m_key_data; // [[handle]]
  50. };
  51. // https://w3c.github.io/webcrypto/#ref-for-dfn-CryptoKeyPair-2
  52. class CryptoKeyPair : public JS::Object {
  53. JS_OBJECT(CryptoKeyPair, Object);
  54. JS_DECLARE_ALLOCATOR(CryptoKeyPair);
  55. public:
  56. static JS::NonnullGCPtr<CryptoKeyPair> create(JS::Realm&, JS::NonnullGCPtr<CryptoKey> public_key, JS::NonnullGCPtr<CryptoKey> private_key);
  57. virtual ~CryptoKeyPair() override = default;
  58. JS::NonnullGCPtr<CryptoKey> public_key() const { return m_public_key; }
  59. JS::NonnullGCPtr<CryptoKey> private_key() const { return m_private_key; }
  60. private:
  61. CryptoKeyPair(JS::Realm&, JS::NonnullGCPtr<CryptoKey> public_key, JS::NonnullGCPtr<CryptoKey> private_key);
  62. virtual void initialize(JS::Realm&) override;
  63. virtual void visit_edges(Visitor&) override;
  64. JS_DECLARE_NATIVE_FUNCTION(public_key_getter);
  65. JS_DECLARE_NATIVE_FUNCTION(private_key_getter);
  66. JS::NonnullGCPtr<CryptoKey> m_public_key;
  67. JS::NonnullGCPtr<CryptoKey> m_private_key;
  68. };
  69. }