| 
									
										
										
										
											2023-12-15 20:25:35 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2023, stelar7 <dudedbz@gmail.com> | 
					
						
							| 
									
										
										
										
											2024-03-06 16:53:50 -07:00
										 |  |  |  * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-12-15 20:25:35 +01:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 16:53:50 -07:00
										 |  |  | #include <AK/Memory.h>
 | 
					
						
							| 
									
										
										
										
											2024-03-06 19:11:08 -07:00
										 |  |  | #include <LibJS/Runtime/Array.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-27 12:09:58 +12:00
										 |  |  | #include <LibWeb/Bindings/CryptoKeyPrototype.h>
 | 
					
						
							| 
									
										
										
										
											2024-03-06 19:14:08 -07:00
										 |  |  | #include <LibWeb/Bindings/ExceptionOrUtils.h>
 | 
					
						
							| 
									
										
										
										
											2023-12-15 20:25:35 +01:00
										 |  |  | #include <LibWeb/Crypto/CryptoKey.h>
 | 
					
						
							| 
									
										
										
										
											2024-11-22 11:51:40 +01:00
										 |  |  | #include <LibWeb/HTML/StructuredSerialize.h>
 | 
					
						
							| 
									
										
										
										
											2023-12-15 20:25:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Crypto { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(CryptoKey); | 
					
						
							|  |  |  | GC_DEFINE_ALLOCATOR(CryptoKeyPair); | 
					
						
							| 
									
										
										
										
											2023-12-15 20:25:35 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC::Ref<CryptoKey> CryptoKey::create(JS::Realm& realm, InternalKeyData key_data) | 
					
						
							| 
									
										
										
										
											2023-12-15 20:25:35 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     return realm.create<CryptoKey>(realm, move(key_data)); | 
					
						
							| 
									
										
										
										
											2023-12-15 20:25:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC::Ref<CryptoKey> CryptoKey::create(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     return realm.create<CryptoKey>(realm); | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 16:53:50 -07:00
										 |  |  | CryptoKey::CryptoKey(JS::Realm& realm, InternalKeyData key_data) | 
					
						
							| 
									
										
										
										
											2023-12-15 20:25:35 +01:00
										 |  |  |     : PlatformObject(realm) | 
					
						
							|  |  |  |     , m_algorithm(Object::create(realm, nullptr)) | 
					
						
							|  |  |  |     , m_usages(Object::create(realm, nullptr)) | 
					
						
							| 
									
										
										
										
											2024-03-06 16:53:50 -07:00
										 |  |  |     , m_key_data(move(key_data)) | 
					
						
							| 
									
										
										
										
											2023-12-15 20:25:35 +01:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | CryptoKey::CryptoKey(JS::Realm& realm) | 
					
						
							|  |  |  |     : PlatformObject(realm) | 
					
						
							|  |  |  |     , m_algorithm(Object::create(realm, nullptr)) | 
					
						
							|  |  |  |     , m_usages(Object::create(realm, nullptr)) | 
					
						
							|  |  |  |     , m_key_data(MUST(ByteBuffer::create_uninitialized(0))) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 16:53:50 -07:00
										 |  |  | CryptoKey::~CryptoKey() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_key_data.visit( | 
					
						
							|  |  |  |         [](ByteBuffer& data) { secure_zero(data.data(), data.size()); }, | 
					
						
							|  |  |  |         [](auto& data) { secure_zero(reinterpret_cast<u8*>(&data), sizeof(data)); }); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-12-15 20:25:35 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | void CryptoKey::initialize(JS::Realm& realm) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-03-16 13:13:08 +01:00
										 |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(CryptoKey); | 
					
						
							| 
									
										
										
										
											2025-04-20 16:22:57 +02:00
										 |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2023-12-15 20:25:35 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CryptoKey::visit_edges(Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_algorithm); | 
					
						
							|  |  |  |     visitor.visit(m_usages); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-06 19:11:08 -07:00
										 |  |  | void CryptoKey::set_usages(Vector<Bindings::KeyUsage> usages) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_key_usages = move(usages); | 
					
						
							|  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  |     m_usages = JS::Array::create_from<Bindings::KeyUsage>(realm, m_key_usages.span(), [&](auto& key_usage) -> JS::Value { | 
					
						
							|  |  |  |         return JS::PrimitiveString::create(realm.vm(), Bindings::idl_enum_to_string(key_usage)); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-14 22:39:48 -06:00
										 |  |  | String CryptoKey::algorithm_name() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (m_algorithm_name.is_empty()) { | 
					
						
							| 
									
										
										
										
											2025-08-02 19:27:29 -04:00
										 |  |  |         auto name = MUST(m_algorithm->get("name"_utf16_fly_string)); | 
					
						
							| 
									
										
										
										
											2024-03-14 22:39:48 -06:00
										 |  |  |         m_algorithm_name = MUST(name.to_string(vm())); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return m_algorithm_name; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC::Ref<CryptoKeyPair> CryptoKeyPair::create(JS::Realm& realm, GC::Ref<CryptoKey> public_key, GC::Ref<CryptoKey> private_key) | 
					
						
							| 
									
										
										
										
											2024-03-06 19:14:08 -07:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     return realm.create<CryptoKeyPair>(realm, public_key, private_key); | 
					
						
							| 
									
										
										
										
											2024-03-06 19:14:08 -07:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | CryptoKeyPair::CryptoKeyPair(JS::Realm& realm, GC::Ref<CryptoKey> public_key, GC::Ref<CryptoKey> private_key) | 
					
						
							| 
									
										
										
										
											2024-03-06 19:14:08 -07:00
										 |  |  |     : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype()) | 
					
						
							|  |  |  |     , m_public_key(public_key) | 
					
						
							|  |  |  |     , m_private_key(private_key) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CryptoKeyPair::initialize(JS::Realm& realm) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2025-08-02 19:27:29 -04:00
										 |  |  |     define_native_accessor(realm, "publicKey"_utf16_fly_string, public_key_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable); | 
					
						
							|  |  |  |     define_native_accessor(realm, "privateKey"_utf16_fly_string, private_key_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable); | 
					
						
							| 
									
										
										
										
											2024-03-06 19:14:08 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Base::initialize(realm); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void CryptoKeyPair::visit_edges(Visitor& visitor) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     Base::visit_edges(visitor); | 
					
						
							|  |  |  |     visitor.visit(m_public_key); | 
					
						
							|  |  |  |     visitor.visit(m_private_key); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static JS::ThrowCompletionOr<CryptoKeyPair*> impl_from(JS::VM& vm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto this_value = vm.this_value(); | 
					
						
							|  |  |  |     JS::Object* this_object = nullptr; | 
					
						
							|  |  |  |     if (this_value.is_nullish()) | 
					
						
							|  |  |  |         this_object = &vm.current_realm()->global_object(); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  |         this_object = TRY(this_value.to_object(vm)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!is<CryptoKeyPair>(this_object)) | 
					
						
							|  |  |  |         return vm.throw_completion<JS::TypeError>(JS::ErrorType::NotAnObjectOfType, "CryptoKeyPair"); | 
					
						
							|  |  |  |     return static_cast<CryptoKeyPair*>(this_object); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JS_DEFINE_NATIVE_FUNCTION(CryptoKeyPair::public_key_getter) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto* impl = TRY(impl_from(vm)); | 
					
						
							|  |  |  |     return TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return impl->public_key(); })); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | JS_DEFINE_NATIVE_FUNCTION(CryptoKeyPair::private_key_getter) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     auto* impl = TRY(impl_from(vm)); | 
					
						
							|  |  |  |     return TRY(Bindings::throw_dom_exception_if_needed(vm, [&] { return impl->private_key(); })); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-17 09:51:04 -04:00
										 |  |  | WebIDL::ExceptionOr<void> CryptoKey::serialization_steps(HTML::TransferDataEncoder& serialized, bool for_storage, HTML::SerializationMemory& memory) | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. Set serialized.[[Type]] to the [[type]] internal slot of value.
 | 
					
						
							| 
									
										
										
										
											2025-07-17 09:51:04 -04:00
										 |  |  |     serialized.encode(m_type); | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. Set serialized.[[Extractable]] to the [[extractable]] internal slot of value.
 | 
					
						
							| 
									
										
										
										
											2025-07-17 09:51:04 -04:00
										 |  |  |     serialized.encode(m_extractable); | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. Set serialized.[[Algorithm]] to the sub-serialization of the [[algorithm]] internal slot of value.
 | 
					
						
							|  |  |  |     auto serialized_algorithm = TRY(HTML::structured_serialize_internal(vm, m_algorithm, for_storage, memory)); | 
					
						
							| 
									
										
										
										
											2025-07-17 09:51:04 -04:00
										 |  |  |     serialized.append(move(serialized_algorithm)); | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 4. Set serialized.[[Usages]] to the sub-serialization of the [[usages]] internal slot of value.
 | 
					
						
							|  |  |  |     auto serialized_usages = TRY(HTML::structured_serialize_internal(vm, m_usages, for_storage, memory)); | 
					
						
							| 
									
										
										
										
											2025-07-17 09:51:04 -04:00
										 |  |  |     serialized.append(move(serialized_usages)); | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: 5. Set serialized.[[Handle]] to the [[handle]] internal slot of value.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-07-17 09:51:04 -04:00
										 |  |  | WebIDL::ExceptionOr<void> CryptoKey::deserialization_steps(HTML::TransferDataDecoder& serialized, HTML::DeserializationMemory& memory) | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     auto& vm = this->vm(); | 
					
						
							|  |  |  |     auto& realm = this->realm(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // 1. Initialize the [[type]] internal slot of value to serialized.[[Type]].
 | 
					
						
							| 
									
										
										
										
											2025-07-17 09:51:04 -04:00
										 |  |  |     m_type = serialized.decode<Bindings::KeyType>(); | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 2. Initialize the [[extractable]] internal slot of value to serialized.[[Extractable]].
 | 
					
						
							| 
									
										
										
										
											2025-07-17 09:51:04 -04:00
										 |  |  |     m_extractable = serialized.decode<bool>(); | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 3. Initialize the [[algorithm]] internal slot of value to the sub-deserialization of serialized.[[Algorithm]].
 | 
					
						
							| 
									
										
										
										
											2025-07-17 09:51:04 -04:00
										 |  |  |     auto deserialized = TRY(HTML::structured_deserialize_internal(vm, serialized, realm, memory)); | 
					
						
							|  |  |  |     m_algorithm = deserialized.as_object(); | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // 4. Initialize the [[usages]] internal slot of value to the sub-deserialization of serialized.[[Usages]].
 | 
					
						
							| 
									
										
										
										
											2025-07-17 09:51:04 -04:00
										 |  |  |     deserialized = TRY(HTML::structured_deserialize_internal(vm, serialized, realm, memory)); | 
					
						
							|  |  |  |     m_usages = deserialized.as_object(); | 
					
						
							| 
									
										
										
										
											2024-03-10 20:48:00 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     // FIXME: 5. Initialize the [[handle]] internal slot of value to serialized.[[Handle]].
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-12-15 20:25:35 +01:00
										 |  |  | } |