KeyAlgorithms.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. JS_DEFINE_ALLOCATOR(AesKeyAlgorithm);
  18. template<typename T>
  19. static JS::ThrowCompletionOr<T*> impl_from(JS::VM& vm, StringView Name)
  20. {
  21. auto this_value = vm.this_value();
  22. JS::Object* this_object = nullptr;
  23. if (this_value.is_nullish())
  24. this_object = &vm.current_realm()->global_object();
  25. else
  26. this_object = TRY(this_value.to_object(vm));
  27. if (!is<T>(this_object))
  28. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, Name);
  29. return static_cast<T*>(this_object);
  30. }
  31. JS::NonnullGCPtr<KeyAlgorithm> KeyAlgorithm::create(JS::Realm& realm)
  32. {
  33. return realm.create<KeyAlgorithm>(realm);
  34. }
  35. KeyAlgorithm::KeyAlgorithm(JS::Realm& realm)
  36. : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype())
  37. , m_realm(realm)
  38. {
  39. }
  40. void KeyAlgorithm::initialize(JS::Realm& realm)
  41. {
  42. define_native_accessor(realm, "name", name_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  43. Base::initialize(realm);
  44. }
  45. JS_DEFINE_NATIVE_FUNCTION(KeyAlgorithm::name_getter)
  46. {
  47. auto* impl = TRY(impl_from<KeyAlgorithm>(vm, "KeyAlgorithm"sv));
  48. auto name = TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return impl->name(); }));
  49. return JS::PrimitiveString::create(vm, name);
  50. }
  51. void KeyAlgorithm::visit_edges(Visitor& visitor)
  52. {
  53. Base::visit_edges(visitor);
  54. visitor.visit(m_realm);
  55. }
  56. JS::NonnullGCPtr<RsaKeyAlgorithm> RsaKeyAlgorithm::create(JS::Realm& realm)
  57. {
  58. return realm.create<RsaKeyAlgorithm>(realm);
  59. }
  60. RsaKeyAlgorithm::RsaKeyAlgorithm(JS::Realm& realm)
  61. : KeyAlgorithm(realm)
  62. , m_public_exponent(MUST(JS::Uint8Array::create(realm, 0)))
  63. {
  64. }
  65. void RsaKeyAlgorithm::initialize(JS::Realm& realm)
  66. {
  67. Base::initialize(realm);
  68. define_native_accessor(realm, "modulusLength", modulus_length_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  69. define_native_accessor(realm, "publicExponent", public_exponent_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  70. }
  71. void RsaKeyAlgorithm::visit_edges(Visitor& visitor)
  72. {
  73. Base::visit_edges(visitor);
  74. visitor.visit(m_public_exponent);
  75. }
  76. WebIDL::ExceptionOr<void> RsaKeyAlgorithm::set_public_exponent(::Crypto::UnsignedBigInteger exponent)
  77. {
  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_be = 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. if constexpr (AK::HostIsLittleEndian) {
  86. Vector<u8, 32> data_slice_le;
  87. data_slice_le.ensure_capacity(data_size);
  88. for (size_t i = 0; i < data_size; ++i) {
  89. data_slice_le.append(data_slice_be[data_size - i - 1]);
  90. }
  91. m_public_exponent = TRY(JS::Uint8Array::create(realm, data_slice_le.size()));
  92. m_public_exponent->viewed_array_buffer()->buffer().overwrite(0, data_slice_le.data(), data_slice_le.size());
  93. } else {
  94. m_public_exponent = TRY(JS::Uint8Array::create(realm, data_slice_be.size()));
  95. m_public_exponent->viewed_array_buffer()->buffer().overwrite(0, data_slice_be.data(), data_slice_be.size());
  96. }
  97. return {};
  98. }
  99. JS_DEFINE_NATIVE_FUNCTION(RsaKeyAlgorithm::modulus_length_getter)
  100. {
  101. auto* impl = TRY(impl_from<RsaKeyAlgorithm>(vm, "RsaKeyAlgorithm"sv));
  102. return JS::Value(impl->modulus_length());
  103. }
  104. JS_DEFINE_NATIVE_FUNCTION(RsaKeyAlgorithm::public_exponent_getter)
  105. {
  106. auto* impl = TRY(impl_from<RsaKeyAlgorithm>(vm, "RsaKeyAlgorithm"sv));
  107. return impl->public_exponent();
  108. }
  109. JS::NonnullGCPtr<EcKeyAlgorithm> EcKeyAlgorithm::create(JS::Realm& realm)
  110. {
  111. return realm.create<EcKeyAlgorithm>(realm);
  112. }
  113. EcKeyAlgorithm::EcKeyAlgorithm(JS::Realm& realm)
  114. : KeyAlgorithm(realm)
  115. {
  116. }
  117. void EcKeyAlgorithm::initialize(JS::Realm& realm)
  118. {
  119. Base::initialize(realm);
  120. define_native_accessor(realm, "namedCurve", named_curve_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  121. }
  122. JS_DEFINE_NATIVE_FUNCTION(EcKeyAlgorithm::named_curve_getter)
  123. {
  124. auto* impl = TRY(impl_from<EcKeyAlgorithm>(vm, "EcKeyAlgorithm"sv));
  125. return JS::PrimitiveString::create(vm, impl->named_curve());
  126. }
  127. JS::NonnullGCPtr<RsaHashedKeyAlgorithm> RsaHashedKeyAlgorithm::create(JS::Realm& realm)
  128. {
  129. return realm.create<RsaHashedKeyAlgorithm>(realm);
  130. }
  131. RsaHashedKeyAlgorithm::RsaHashedKeyAlgorithm(JS::Realm& realm)
  132. : RsaKeyAlgorithm(realm)
  133. , m_hash(String {})
  134. {
  135. }
  136. void RsaHashedKeyAlgorithm::initialize(JS::Realm& realm)
  137. {
  138. Base::initialize(realm);
  139. define_native_accessor(realm, "hash", hash_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  140. }
  141. JS_DEFINE_NATIVE_FUNCTION(RsaHashedKeyAlgorithm::hash_getter)
  142. {
  143. auto* impl = TRY(impl_from<RsaHashedKeyAlgorithm>(vm, "RsaHashedKeyAlgorithm"sv));
  144. auto hash = TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return impl->hash(); }));
  145. return hash.visit(
  146. [&](String const& hash_string) -> JS::Value {
  147. return JS::PrimitiveString::create(vm, hash_string);
  148. },
  149. [&](JS::Handle<JS::Object> const& hash) -> JS::Value {
  150. return hash;
  151. });
  152. }
  153. JS::NonnullGCPtr<AesKeyAlgorithm> AesKeyAlgorithm::create(JS::Realm& realm)
  154. {
  155. return realm.create<AesKeyAlgorithm>(realm);
  156. }
  157. AesKeyAlgorithm::AesKeyAlgorithm(JS::Realm& realm)
  158. : KeyAlgorithm(realm)
  159. , m_length(0)
  160. {
  161. }
  162. void AesKeyAlgorithm::initialize(JS::Realm& realm)
  163. {
  164. Base::initialize(realm);
  165. define_native_accessor(realm, "length", length_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  166. }
  167. JS_DEFINE_NATIVE_FUNCTION(AesKeyAlgorithm::length_getter)
  168. {
  169. auto* impl = TRY(impl_from<AesKeyAlgorithm>(vm, "AesKeyAlgorithm"sv));
  170. auto length = TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return impl->length(); }));
  171. return length;
  172. }
  173. }