CryptoKey.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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/Crypto/CryptoBindings.h>
  14. namespace Web::Crypto {
  15. class CryptoKey final : public Bindings::PlatformObject {
  16. WEB_PLATFORM_OBJECT(CryptoKey, Bindings::PlatformObject);
  17. JS_DECLARE_ALLOCATOR(CryptoKey);
  18. public:
  19. using InternalKeyData = Variant<ByteBuffer, Bindings::JsonWebKey, ::Crypto::PK::RSAPublicKey<>, ::Crypto::PK::RSAPrivateKey<>>;
  20. [[nodiscard]] static JS::NonnullGCPtr<CryptoKey> create(JS::Realm&, InternalKeyData);
  21. virtual ~CryptoKey() override;
  22. bool extractable() const { return m_extractable; }
  23. Bindings::KeyType type() const { return m_type; }
  24. JS::Object const* algorithm() const { return m_algorithm; }
  25. JS::Object const* usages() const { return m_usages; }
  26. Vector<Bindings::KeyUsage> internal_usages() const { return m_key_usages; }
  27. void set_extractable(bool extractable) { m_extractable = extractable; }
  28. void set_type(Bindings::KeyType type) { m_type = type; }
  29. void set_algorithm(JS::NonnullGCPtr<Object> algorithm) { m_algorithm = move(algorithm); }
  30. void set_usages(Vector<Bindings::KeyUsage>);
  31. InternalKeyData const& handle() const { return m_key_data; }
  32. private:
  33. CryptoKey(JS::Realm&, InternalKeyData);
  34. virtual void initialize(JS::Realm&) override;
  35. virtual void visit_edges(Visitor&) override;
  36. Bindings::KeyType m_type;
  37. bool m_extractable { false };
  38. JS::NonnullGCPtr<Object> m_algorithm;
  39. JS::NonnullGCPtr<Object> m_usages;
  40. Vector<Bindings::KeyUsage> m_key_usages;
  41. InternalKeyData m_key_data; // [[handle]]
  42. };
  43. // https://w3c.github.io/webcrypto/#ref-for-dfn-CryptoKeyPair-2
  44. class CryptoKeyPair : public JS::Object {
  45. JS_OBJECT(CryptoKeyPair, Object);
  46. JS_DECLARE_ALLOCATOR(CryptoKeyPair);
  47. public:
  48. static JS::NonnullGCPtr<CryptoKeyPair> create(JS::Realm&, JS::NonnullGCPtr<CryptoKey> public_key, JS::NonnullGCPtr<CryptoKey> private_key);
  49. virtual ~CryptoKeyPair() override = default;
  50. JS::NonnullGCPtr<CryptoKey> public_key() const { return m_public_key; }
  51. JS::NonnullGCPtr<CryptoKey> private_key() const { return m_private_key; }
  52. private:
  53. CryptoKeyPair(JS::Realm&, JS::NonnullGCPtr<CryptoKey> public_key, JS::NonnullGCPtr<CryptoKey> private_key);
  54. virtual void initialize(JS::Realm&) override;
  55. virtual void visit_edges(Visitor&) override;
  56. JS_DECLARE_NATIVE_FUNCTION(public_key_getter);
  57. JS_DECLARE_NATIVE_FUNCTION(private_key_getter);
  58. JS::NonnullGCPtr<CryptoKey> m_public_key;
  59. JS::NonnullGCPtr<CryptoKey> m_private_key;
  60. };
  61. }