KeyAlgorithms.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
  3. * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/String.h>
  9. #include <LibCrypto/BigInt/UnsignedBigInteger.h>
  10. #include <LibJS/Runtime/Object.h>
  11. #include <LibWeb/Crypto/CryptoAlgorithms.h>
  12. #include <LibWeb/WebIDL/ExceptionOr.h>
  13. namespace Web::Crypto {
  14. // https://w3c.github.io/webcrypto/#key-algorithm-dictionary
  15. class KeyAlgorithm : public JS::Object {
  16. JS_OBJECT(KeyAlgorithm, JS::Object);
  17. JS_DECLARE_ALLOCATOR(KeyAlgorithm);
  18. public:
  19. static JS::NonnullGCPtr<KeyAlgorithm> create(JS::Realm&);
  20. virtual ~KeyAlgorithm() override = default;
  21. String const& name() const { return m_name; }
  22. void set_name(String name) { m_name = move(name); }
  23. JS::Realm& realm() const { return m_realm; }
  24. protected:
  25. KeyAlgorithm(JS::Realm&);
  26. virtual void initialize(JS::Realm&) override;
  27. virtual void visit_edges(Visitor&) override;
  28. private:
  29. JS_DECLARE_NATIVE_FUNCTION(name_getter);
  30. String m_name;
  31. JS::NonnullGCPtr<JS::Realm> m_realm;
  32. };
  33. // https://w3c.github.io/webcrypto/#RsaKeyAlgorithm-dictionary
  34. class RsaKeyAlgorithm : public KeyAlgorithm {
  35. JS_OBJECT(RsaKeyAlgorithm, KeyAlgorithm);
  36. JS_DECLARE_ALLOCATOR(RsaKeyAlgorithm);
  37. public:
  38. static JS::NonnullGCPtr<RsaKeyAlgorithm> create(JS::Realm&);
  39. virtual ~RsaKeyAlgorithm() override = default;
  40. u32 modulus_length() const { return m_modulus_length; }
  41. void set_modulus_length(u32 modulus_length) { m_modulus_length = modulus_length; }
  42. JS::NonnullGCPtr<JS::Uint8Array> public_exponent() const { return m_public_exponent; }
  43. void set_public_exponent(JS::NonnullGCPtr<JS::Uint8Array> public_exponent) { m_public_exponent = public_exponent; }
  44. WebIDL::ExceptionOr<void> set_public_exponent(::Crypto::UnsignedBigInteger);
  45. protected:
  46. RsaKeyAlgorithm(JS::Realm&);
  47. virtual void initialize(JS::Realm&) override;
  48. virtual void visit_edges(Visitor&) override;
  49. private:
  50. JS_DECLARE_NATIVE_FUNCTION(modulus_length_getter);
  51. JS_DECLARE_NATIVE_FUNCTION(public_exponent_getter);
  52. u32 m_modulus_length { 0 };
  53. JS::NonnullGCPtr<JS::Uint8Array> m_public_exponent;
  54. };
  55. // https://w3c.github.io/webcrypto/#RsaHashedKeyAlgorithm-dictionary
  56. class RsaHashedKeyAlgorithm : public RsaKeyAlgorithm {
  57. JS_OBJECT(RsaHashedKeyAlgorithm, RsaKeyAlgorithm);
  58. JS_DECLARE_ALLOCATOR(RsaHashedKeyAlgorithm);
  59. public:
  60. static JS::NonnullGCPtr<RsaHashedKeyAlgorithm> create(JS::Realm&);
  61. virtual ~RsaHashedKeyAlgorithm() override = default;
  62. HashAlgorithmIdentifier const& hash() const { return m_hash; }
  63. void set_hash(HashAlgorithmIdentifier hash) { m_hash = move(hash); }
  64. protected:
  65. RsaHashedKeyAlgorithm(JS::Realm&);
  66. virtual void initialize(JS::Realm&) override;
  67. private:
  68. JS_DECLARE_NATIVE_FUNCTION(hash_getter);
  69. HashAlgorithmIdentifier m_hash;
  70. };
  71. // https://w3c.github.io/webcrypto/#EcKeyAlgorithm-dictionary
  72. class EcKeyAlgorithm : public KeyAlgorithm {
  73. JS_OBJECT(EcKeyAlgorithm, KeyAlgorithm);
  74. JS_DECLARE_ALLOCATOR(EcKeyAlgorithm);
  75. public:
  76. static JS::NonnullGCPtr<EcKeyAlgorithm> create(JS::Realm&);
  77. virtual ~EcKeyAlgorithm() override = default;
  78. NamedCurve named_curve() const { return m_named_curve; }
  79. void set_named_curve(NamedCurve named_curve) { m_named_curve = named_curve; }
  80. protected:
  81. EcKeyAlgorithm(JS::Realm&);
  82. virtual void initialize(JS::Realm&) override;
  83. private:
  84. JS_DECLARE_NATIVE_FUNCTION(named_curve_getter);
  85. NamedCurve m_named_curve;
  86. };
  87. }