KeyAlgorithms.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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, 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. private:
  28. JS_DECLARE_NATIVE_FUNCTION(name_getter);
  29. String m_name;
  30. JS::Realm& m_realm;
  31. };
  32. // https://w3c.github.io/webcrypto/#RsaKeyAlgorithm-dictionary
  33. class RsaKeyAlgorithm : public KeyAlgorithm {
  34. JS_OBJECT(RsaKeyAlgorithm, KeyAlgorithm);
  35. JS_DECLARE_ALLOCATOR(RsaKeyAlgorithm);
  36. public:
  37. static JS::NonnullGCPtr<RsaKeyAlgorithm> create(JS::Realm&);
  38. virtual ~RsaKeyAlgorithm() override = default;
  39. u32 modulus_length() const { return m_modulus_length; }
  40. void set_modulus_length(u32 modulus_length) { m_modulus_length = modulus_length; }
  41. JS::NonnullGCPtr<JS::Uint8Array> public_exponent() const { return m_public_exponent; }
  42. void set_public_exponent(JS::NonnullGCPtr<JS::Uint8Array> public_exponent) { m_public_exponent = public_exponent; }
  43. WebIDL::ExceptionOr<void> set_public_exponent(::Crypto::UnsignedBigInteger);
  44. protected:
  45. RsaKeyAlgorithm(JS::Realm&);
  46. virtual void initialize(JS::Realm&) override;
  47. virtual void visit_edges(Visitor&) override;
  48. private:
  49. JS_DECLARE_NATIVE_FUNCTION(modulus_length_getter);
  50. JS_DECLARE_NATIVE_FUNCTION(public_exponent_getter);
  51. u32 m_modulus_length { 0 };
  52. JS::NonnullGCPtr<JS::Uint8Array> m_public_exponent;
  53. };
  54. // https://w3c.github.io/webcrypto/#RsaHashedKeyAlgorithm-dictionary
  55. class RsaHashedKeyAlgorithm : public RsaKeyAlgorithm {
  56. JS_OBJECT(RsaHashedKeyAlgorithm, RsaKeyAlgorithm);
  57. JS_DECLARE_ALLOCATOR(RsaHashedKeyAlgorithm);
  58. public:
  59. static JS::NonnullGCPtr<RsaHashedKeyAlgorithm> create(JS::Realm&);
  60. virtual ~RsaHashedKeyAlgorithm() override = default;
  61. HashAlgorithmIdentifier const& hash() const { return m_hash; }
  62. void set_hash(HashAlgorithmIdentifier hash) { m_hash = move(hash); }
  63. protected:
  64. RsaHashedKeyAlgorithm(JS::Realm&);
  65. virtual void initialize(JS::Realm&) override;
  66. private:
  67. JS_DECLARE_NATIVE_FUNCTION(hash_getter);
  68. HashAlgorithmIdentifier m_hash;
  69. };
  70. }