KeyAlgorithms.cpp 4.9 KB

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