mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 20:32:44 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			113 lines
		
	
	
	
		
			3.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
	
		
			3.5 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
 | |
|  * Copyright (c) 2024, Andrew Kaster <akaster@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #include <AK/Memory.h>
 | |
| #include <LibJS/Runtime/Array.h>
 | |
| #include <LibWeb/Bindings/ExceptionOrUtils.h>
 | |
| #include <LibWeb/Crypto/CryptoKey.h>
 | |
| 
 | |
| namespace Web::Crypto {
 | |
| 
 | |
| JS_DEFINE_ALLOCATOR(CryptoKey);
 | |
| JS_DEFINE_ALLOCATOR(CryptoKeyPair);
 | |
| 
 | |
| JS::NonnullGCPtr<CryptoKey> CryptoKey::create(JS::Realm& realm, InternalKeyData key_data)
 | |
| {
 | |
|     return realm.heap().allocate<CryptoKey>(realm, realm, move(key_data));
 | |
| }
 | |
| 
 | |
| CryptoKey::CryptoKey(JS::Realm& realm, InternalKeyData key_data)
 | |
|     : PlatformObject(realm)
 | |
|     , m_algorithm(Object::create(realm, nullptr))
 | |
|     , m_usages(Object::create(realm, nullptr))
 | |
|     , m_key_data(move(key_data))
 | |
| {
 | |
| }
 | |
| 
 | |
| CryptoKey::~CryptoKey()
 | |
| {
 | |
|     m_key_data.visit(
 | |
|         [](ByteBuffer& data) { secure_zero(data.data(), data.size()); },
 | |
|         [](auto& data) { secure_zero(reinterpret_cast<u8*>(&data), sizeof(data)); });
 | |
| }
 | |
| 
 | |
| void CryptoKey::initialize(JS::Realm& realm)
 | |
| {
 | |
|     Base::initialize(realm);
 | |
|     set_prototype(&Bindings::ensure_web_prototype<Bindings::CryptoKeyPrototype>(realm, "CryptoKey"_fly_string));
 | |
| }
 | |
| 
 | |
| void CryptoKey::visit_edges(Visitor& visitor)
 | |
| {
 | |
|     Base::visit_edges(visitor);
 | |
|     visitor.visit(m_algorithm);
 | |
|     visitor.visit(m_usages);
 | |
| }
 | |
| 
 | |
| 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));
 | |
|     });
 | |
| }
 | |
| 
 | |
| JS::NonnullGCPtr<CryptoKeyPair> CryptoKeyPair::create(JS::Realm& realm, JS::NonnullGCPtr<CryptoKey> public_key, JS::NonnullGCPtr<CryptoKey> private_key)
 | |
| {
 | |
|     return realm.heap().allocate<CryptoKeyPair>(realm, realm, public_key, private_key);
 | |
| }
 | |
| 
 | |
| CryptoKeyPair::CryptoKeyPair(JS::Realm& realm, JS::NonnullGCPtr<CryptoKey> public_key, JS::NonnullGCPtr<CryptoKey> private_key)
 | |
|     : Object(ConstructWithPrototypeTag::Tag, realm.intrinsics().object_prototype())
 | |
|     , m_public_key(public_key)
 | |
|     , m_private_key(private_key)
 | |
| {
 | |
| }
 | |
| 
 | |
| void CryptoKeyPair::initialize(JS::Realm& realm)
 | |
| {
 | |
|     define_native_accessor(realm, "publicKey", public_key_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
 | |
|     define_native_accessor(realm, "privateKey", private_key_getter, {}, JS::Attribute::Enumerable | JS::Attribute::Configurable);
 | |
| 
 | |
|     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(); }));
 | |
| }
 | |
| 
 | |
| }
 | 
