CryptoKey.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 <AK/Memory.h>
  8. #include <LibJS/Runtime/Array.h>
  9. #include <LibWeb/Bindings/ExceptionOrUtils.h>
  10. #include <LibWeb/Crypto/CryptoKey.h>
  11. namespace Web::Crypto {
  12. JS_DEFINE_ALLOCATOR(CryptoKey);
  13. JS_DEFINE_ALLOCATOR(CryptoKeyPair);
  14. JS::NonnullGCPtr<CryptoKey> CryptoKey::create(JS::Realm& realm, InternalKeyData key_data)
  15. {
  16. return realm.heap().allocate<CryptoKey>(realm, realm, move(key_data));
  17. }
  18. JS::NonnullGCPtr<CryptoKey> CryptoKey::create(JS::Realm& realm)
  19. {
  20. return realm.heap().allocate<CryptoKey>(realm, realm);
  21. }
  22. CryptoKey::CryptoKey(JS::Realm& realm, InternalKeyData key_data)
  23. : PlatformObject(realm)
  24. , m_algorithm(Object::create(realm, nullptr))
  25. , m_usages(Object::create(realm, nullptr))
  26. , m_key_data(move(key_data))
  27. {
  28. }
  29. CryptoKey::CryptoKey(JS::Realm& realm)
  30. : PlatformObject(realm)
  31. , m_algorithm(Object::create(realm, nullptr))
  32. , m_usages(Object::create(realm, nullptr))
  33. , m_key_data(MUST(ByteBuffer::create_uninitialized(0)))
  34. {
  35. }
  36. CryptoKey::~CryptoKey()
  37. {
  38. m_key_data.visit(
  39. [](ByteBuffer& data) { secure_zero(data.data(), data.size()); },
  40. [](auto& data) { secure_zero(reinterpret_cast<u8*>(&data), sizeof(data)); });
  41. }
  42. void CryptoKey::initialize(JS::Realm& realm)
  43. {
  44. Base::initialize(realm);
  45. WEB_SET_PROTOTYPE_FOR_INTERFACE(CryptoKey);
  46. }
  47. void CryptoKey::visit_edges(Visitor& visitor)
  48. {
  49. Base::visit_edges(visitor);
  50. visitor.visit(m_algorithm);
  51. visitor.visit(m_usages);
  52. }
  53. void CryptoKey::set_usages(Vector<Bindings::KeyUsage> usages)
  54. {
  55. m_key_usages = move(usages);
  56. auto& realm = this->realm();
  57. m_usages = JS::Array::create_from<Bindings::KeyUsage>(realm, m_key_usages.span(), [&](auto& key_usage) -> JS::Value {
  58. return JS::PrimitiveString::create(realm.vm(), Bindings::idl_enum_to_string(key_usage));
  59. });
  60. }
  61. JS::NonnullGCPtr<CryptoKeyPair> CryptoKeyPair::create(JS::Realm& realm, JS::NonnullGCPtr<CryptoKey> public_key, JS::NonnullGCPtr<CryptoKey> private_key)
  62. {
  63. return realm.heap().allocate<CryptoKeyPair>(realm, realm, public_key, private_key);
  64. }
  65. CryptoKeyPair::CryptoKeyPair(JS::Realm& realm, JS::NonnullGCPtr<CryptoKey> public_key, JS::NonnullGCPtr<CryptoKey> private_key)
  66. : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype())
  67. , m_public_key(public_key)
  68. , m_private_key(private_key)
  69. {
  70. }
  71. void CryptoKeyPair::initialize(JS::Realm& realm)
  72. {
  73. define_native_accessor(realm, "publicKey", public_key_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  74. define_native_accessor(realm, "privateKey", private_key_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
  75. Base::initialize(realm);
  76. }
  77. void CryptoKeyPair::visit_edges(Visitor& visitor)
  78. {
  79. Base::visit_edges(visitor);
  80. visitor.visit(m_public_key);
  81. visitor.visit(m_private_key);
  82. }
  83. static JS::ThrowCompletionOr<CryptoKeyPair*> impl_from(JS::VM& vm)
  84. {
  85. auto this_value = vm.this_value();
  86. JS::Object* this_object = nullptr;
  87. if (this_value.is_nullish())
  88. this_object = &vm.current_realm()->global_object();
  89. else
  90. this_object = TRY(this_value.to_object(vm));
  91. if (!is<CryptoKeyPair>(this_object))
  92. return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "CryptoKeyPair");
  93. return static_cast<CryptoKeyPair*>(this_object);
  94. }
  95. JS_DEFINE_NATIVE_FUNCTION(CryptoKeyPair::public_key_getter)
  96. {
  97. auto* impl = TRY(impl_from(vm));
  98. return TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return impl->public_key(); }));
  99. }
  100. JS_DEFINE_NATIVE_FUNCTION(CryptoKeyPair::private_key_getter)
  101. {
  102. auto* impl = TRY(impl_from(vm));
  103. return TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return impl->private_key(); }));
  104. }
  105. WebIDL::ExceptionOr<void> CryptoKey::serialization_steps(HTML::SerializationRecord& serialized, bool for_storage, HTML::SerializationMemory& memory)
  106. {
  107. auto& vm = this->vm();
  108. // 1. Set serialized.[[Type]] to the [[type]] internal slot of value.
  109. HTML::serialize_primitive_type(serialized, static_cast<u32>(m_type));
  110. // 2. Set serialized.[[Extractable]] to the [[extractable]] internal slot of value.
  111. HTML::serialize_primitive_type(serialized, m_extractable);
  112. // 3. Set serialized.[[Algorithm]] to the sub-serialization of the [[algorithm]] internal slot of value.
  113. auto serialized_algorithm = TRY(HTML::structured_serialize_internal(vm, m_algorithm, for_storage, memory));
  114. serialized.extend(move(serialized_algorithm));
  115. // 4. Set serialized.[[Usages]] to the sub-serialization of the [[usages]] internal slot of value.
  116. auto serialized_usages = TRY(HTML::structured_serialize_internal(vm, m_usages, for_storage, memory));
  117. serialized.extend(move(serialized_usages));
  118. // FIXME: 5. Set serialized.[[Handle]] to the [[handle]] internal slot of value.
  119. return {};
  120. }
  121. WebIDL::ExceptionOr<void> CryptoKey::deserialization_steps(ReadonlySpan<u32> const& serialized, size_t& position, HTML::DeserializationMemory& memory)
  122. {
  123. auto& vm = this->vm();
  124. auto& realm = this->realm();
  125. // 1. Initialize the [[type]] internal slot of value to serialized.[[Type]].
  126. m_type = static_cast<Bindings::KeyType>(HTML::deserialize_primitive_type<u32>(serialized, position));
  127. // 2. Initialize the [[extractable]] internal slot of value to serialized.[[Extractable]].
  128. m_extractable = HTML::deserialize_primitive_type<bool>(serialized, position);
  129. // 3. Initialize the [[algorithm]] internal slot of value to the sub-deserialization of serialized.[[Algorithm]].
  130. auto deserialized_record = TRY(HTML::structured_deserialize_internal(vm, serialized, realm, memory, position));
  131. if (deserialized_record.value.has_value())
  132. m_algorithm = deserialized_record.value.release_value().as_object();
  133. position = deserialized_record.position;
  134. // 4. Initialize the [[usages]] internal slot of value to the sub-deserialization of serialized.[[Usages]].
  135. deserialized_record = TRY(HTML::structured_deserialize_internal(vm, serialized, realm, memory, position));
  136. if (deserialized_record.value.has_value())
  137. m_usages = deserialized_record.value.release_value().as_object();
  138. position = deserialized_record.position;
  139. // FIXME: 5. Initialize the [[handle]] internal slot of value to serialized.[[Handle]].
  140. return {};
  141. }
  142. }