Coverage Report

Created: 2025-08-28 06:26

/src/serenity/Userland/Libraries/LibWeb/Crypto/CryptoKey.h
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright (c) 2023, stelar7 <dudedbz@gmail.com>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <LibCrypto/PK/RSA.h>
10
#include <LibJS/Forward.h>
11
#include <LibJS/Heap/GCPtr.h>
12
#include <LibWeb/Bindings/CryptoKeyPrototype.h>
13
#include <LibWeb/Bindings/Intrinsics.h>
14
#include <LibWeb/Bindings/PlatformObject.h>
15
#include <LibWeb/Bindings/Serializable.h>
16
#include <LibWeb/Crypto/CryptoBindings.h>
17
18
namespace Web::Crypto {
19
20
class CryptoKey final
21
    : public Bindings::PlatformObject
22
    , public Bindings::Serializable {
23
    WEB_PLATFORM_OBJECT(CryptoKey, Bindings::PlatformObject);
24
    JS_DECLARE_ALLOCATOR(CryptoKey);
25
26
public:
27
    using InternalKeyData = Variant<ByteBuffer, Bindings::JsonWebKey, ::Crypto::PK::RSAPublicKey<>, ::Crypto::PK::RSAPrivateKey<>>;
28
29
    [[nodiscard]] static JS::NonnullGCPtr<CryptoKey> create(JS::Realm&, InternalKeyData);
30
    [[nodiscard]] static JS::NonnullGCPtr<CryptoKey> create(JS::Realm&);
31
32
    virtual ~CryptoKey() override;
33
34
0
    bool extractable() const { return m_extractable; }
35
0
    Bindings::KeyType type() const { return m_type; }
36
0
    JS::Object const* algorithm() const { return m_algorithm; }
37
0
    JS::Object const* usages() const { return m_usages; }
38
39
0
    Vector<Bindings::KeyUsage> internal_usages() const { return m_key_usages; }
40
41
0
    void set_extractable(bool extractable) { m_extractable = extractable; }
42
0
    void set_type(Bindings::KeyType type) { m_type = type; }
43
0
    void set_algorithm(JS::NonnullGCPtr<Object> algorithm) { m_algorithm = move(algorithm); }
44
    void set_usages(Vector<Bindings::KeyUsage>);
45
46
0
    InternalKeyData const& handle() const { return m_key_data; }
47
    String algorithm_name() const;
48
49
0
    virtual StringView interface_name() const override { return "CryptoKey"sv; }
50
    virtual WebIDL::ExceptionOr<void> serialization_steps(HTML::SerializationRecord& record, bool for_storage, HTML::SerializationMemory&) override;
51
    virtual WebIDL::ExceptionOr<void> deserialization_steps(ReadonlySpan<u32> const& record, size_t& position, HTML::DeserializationMemory&) override;
52
53
private:
54
    CryptoKey(JS::Realm&, InternalKeyData);
55
    explicit CryptoKey(JS::Realm&);
56
57
    virtual void initialize(JS::Realm&) override;
58
    virtual void visit_edges(Visitor&) override;
59
60
    Bindings::KeyType m_type;
61
    bool m_extractable { false };
62
    JS::NonnullGCPtr<Object> m_algorithm;
63
    JS::NonnullGCPtr<Object> m_usages;
64
65
    Vector<Bindings::KeyUsage> m_key_usages;
66
    InternalKeyData m_key_data; // [[handle]]
67
    mutable String m_algorithm_name;
68
};
69
70
// https://w3c.github.io/webcrypto/#ref-for-dfn-CryptoKeyPair-2
71
class CryptoKeyPair : public JS::Object {
72
    JS_OBJECT(CryptoKeyPair, JS::Object);
73
    JS_DECLARE_ALLOCATOR(CryptoKeyPair);
74
75
public:
76
    static JS::NonnullGCPtr<CryptoKeyPair> create(JS::Realm&, JS::NonnullGCPtr<CryptoKey> public_key, JS::NonnullGCPtr<CryptoKey> private_key);
77
    virtual ~CryptoKeyPair() override = default;
78
79
0
    JS::NonnullGCPtr<CryptoKey> public_key() const { return m_public_key; }
80
0
    JS::NonnullGCPtr<CryptoKey> private_key() const { return m_private_key; }
81
82
private:
83
    CryptoKeyPair(JS::Realm&, JS::NonnullGCPtr<CryptoKey> public_key, JS::NonnullGCPtr<CryptoKey> private_key);
84
    virtual void initialize(JS::Realm&) override;
85
    virtual void visit_edges(Visitor&) override;
86
87
    JS_DECLARE_NATIVE_FUNCTION(public_key_getter);
88
    JS_DECLARE_NATIVE_FUNCTION(private_key_getter);
89
90
    JS::NonnullGCPtr<CryptoKey> m_public_key;
91
    JS::NonnullGCPtr<CryptoKey> m_private_key;
92
};
93
94
}