KeyAlgorithms.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include <LibJS/Runtime/GlobalObject.h>
  8. #include <LibJS/Runtime/PrimitiveString.h>
  9. #include <LibJS/Runtime/TypedArray.h>
  10. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  11. #include <LibWeb/Crypto/KeyAlgorithms.h>
  12. namespace Web::Crypto {
  13. JS_DEFINE_ALLOCATOR(KeyAlgorithm);
  14. JS_DEFINE_ALLOCATOR(RsaKeyAlgorithm);
  15. JS_DEFINE_ALLOCATOR(RsaHashedKeyAlgorithm);
  16. JS_DEFINE_ALLOCATOR(EcKeyAlgorithm);
  17. template<typename T>
  18. static JS::ThrowCompletionOr<T*> impl_from(JS::VM& vm, StringView Name)
  19. {
  20. auto this_value = vm.this_value();
  21. JS::Object* this_object = nullptr;
  22. if (this_value.is_nullish())
  23. this_object = &vm.current_realm()->global_object();
  24. else
  25. this_object = TRY(this_value.to_object(vm));
  26. if (!is<T>(this_object))
  27. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, Name);
  28. return static_cast<T*>(this_object);
  29. }
  30. JS::NonnullGCPtr<KeyAlgorithm> KeyAlgorithm::create(JS::Realm& realm)
  31. {
  32. return realm.heap().allocate<KeyAlgorithm>(realm, realm);
  33. }
  34. KeyAlgorithm::KeyAlgorithm(JS::Realm& realm)
  35. : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype())
  36. , m_realm(realm)
  37. {
  38. }
  39. void KeyAlgorithm::initialize(JS::Realm& realm)
  40. {
  41. define_native_accessor(realm, "name", name_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  42. Base::initialize(realm);
  43. }
  44. JS_DEFINE_NATIVE_FUNCTION(KeyAlgorithm::name_getter)
  45. {
  46. auto* impl = TRY(impl_from<KeyAlgorithm>(vm, "KeyAlgorithm"sv));
  47. auto name = TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return impl->name(); }));
  48. return JS::PrimitiveString::create(vm, name);
  49. }
  50. void KeyAlgorithm::visit_edges(Visitor& visitor)
  51. {
  52. Base::visit_edges(visitor);
  53. visitor.visit(m_realm);
  54. }
  55. JS::NonnullGCPtr<RsaKeyAlgorithm> RsaKeyAlgorithm::create(JS::Realm& realm)
  56. {
  57. return realm.heap().allocate<RsaKeyAlgorithm>(realm, realm);
  58. }
  59. RsaKeyAlgorithm::RsaKeyAlgorithm(JS::Realm& realm)
  60. : KeyAlgorithm(realm)
  61. , m_public_exponent(MUST(JS::Uint8Array::create(realm, 0)))
  62. {
  63. }
  64. void RsaKeyAlgorithm::initialize(JS::Realm& realm)
  65. {
  66. Base::initialize(realm);
  67. define_native_accessor(realm, "modulusLength", modulus_length_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  68. define_native_accessor(realm, "publicExponent", public_exponent_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  69. }
  70. void RsaKeyAlgorithm::visit_edges(Visitor& visitor)
  71. {
  72. Base::visit_edges(visitor);
  73. visitor.visit(m_public_exponent);
  74. }
  75. WebIDL::ExceptionOr<void> RsaKeyAlgorithm::set_public_exponent(::Crypto::UnsignedBigInteger exponent)
  76. {
  77. static_assert(AK::HostIsLittleEndian, "This code assumes a little endian host");
  78. auto& realm = this->realm();
  79. auto& vm = this->vm();
  80. auto bytes = TRY_OR_THROW_OOM(vm, ByteBuffer::create_uninitialized(exponent.trimmed_byte_length()));
  81. bool const remove_leading_zeroes = true;
  82. auto data_size = exponent.export_data(bytes.span(), remove_leading_zeroes);
  83. auto data_slice = bytes.bytes().slice(bytes.size() - data_size, data_size);
  84. // The BigInteger typedef from the WebCrypto spec requires the bytes in the Uint8Array be ordered in Big Endian
  85. Vector<u8, 32> byte_swapped_data;
  86. byte_swapped_data.ensure_capacity(data_size);
  87. for (size_t i = 0; i < data_size; ++i)
  88. byte_swapped_data.append(data_slice[data_size - i - 1]);
  89. m_public_exponent = TRY(JS::Uint8Array::create(realm, byte_swapped_data.size()));
  90. m_public_exponent->viewed_array_buffer()->buffer().overwrite(0, byte_swapped_data.data(), byte_swapped_data.size());
  91. return {};
  92. }
  93. JS_DEFINE_NATIVE_FUNCTION(RsaKeyAlgorithm::modulus_length_getter)
  94. {
  95. auto* impl = TRY(impl_from<RsaKeyAlgorithm>(vm, "RsaKeyAlgorithm"sv));
  96. return JS::Value(impl->modulus_length());
  97. }
  98. JS_DEFINE_NATIVE_FUNCTION(RsaKeyAlgorithm::public_exponent_getter)
  99. {
  100. auto* impl = TRY(impl_from<RsaKeyAlgorithm>(vm, "RsaKeyAlgorithm"sv));
  101. return impl->public_exponent();
  102. }
  103. JS::NonnullGCPtr<EcKeyAlgorithm> EcKeyAlgorithm::create(JS::Realm& realm)
  104. {
  105. return realm.heap().allocate<EcKeyAlgorithm>(realm, realm);
  106. }
  107. EcKeyAlgorithm::EcKeyAlgorithm(JS::Realm& realm)
  108. : KeyAlgorithm(realm)
  109. {
  110. }
  111. void EcKeyAlgorithm::initialize(JS::Realm& realm)
  112. {
  113. Base::initialize(realm);
  114. define_native_accessor(realm, "namedCurve", named_curve_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  115. }
  116. JS_DEFINE_NATIVE_FUNCTION(EcKeyAlgorithm::named_curve_getter)
  117. {
  118. auto* impl = TRY(impl_from<EcKeyAlgorithm>(vm, "EcKeyAlgorithm"sv));
  119. return JS::PrimitiveString::create(vm, impl->named_curve());
  120. }
  121. JS::NonnullGCPtr<RsaHashedKeyAlgorithm> RsaHashedKeyAlgorithm::create(JS::Realm& realm)
  122. {
  123. return realm.heap().allocate<RsaHashedKeyAlgorithm>(realm, realm);
  124. }
  125. RsaHashedKeyAlgorithm::RsaHashedKeyAlgorithm(JS::Realm& realm)
  126. : RsaKeyAlgorithm(realm)
  127. , m_hash(String {})
  128. {
  129. }
  130. void RsaHashedKeyAlgorithm::initialize(JS::Realm& realm)
  131. {
  132. Base::initialize(realm);
  133. define_native_accessor(realm, "hash", hash_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  134. }
  135. JS_DEFINE_NATIVE_FUNCTION(RsaHashedKeyAlgorithm::hash_getter)
  136. {
  137. auto* impl = TRY(impl_from<RsaHashedKeyAlgorithm>(vm, "RsaHashedKeyAlgorithm"sv));
  138. auto hash = TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return impl->hash(); }));
  139. return hash.visit(
  140. [&](String const& hash_string) -> JS::Value {
  141. return JS::PrimitiveString::create(vm, hash_string);
  142. },
  143. [&](JS::Handle<JS::Object> const& hash) -> JS::Value {
  144. return hash;
  145. });
  146. }
  147. }