Coverage Report

Created: 2026-07-25 07:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/Crypto/KeyAlgorithms.h
Line
Count
Source
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
8
#pragma once
9
10
#include <AK/String.h>
11
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
12
#include <LibJS/Runtime/Object.h>
13
#include <LibWeb/Crypto/CryptoAlgorithms.h>
14
#include <LibWeb/WebIDL/ExceptionOr.h>
15
16
namespace Web::Crypto {
17
18
// https://w3c.github.io/webcrypto/#key-algorithm-dictionary
19
class KeyAlgorithm : public JS::Object {
20
    JS_OBJECT(KeyAlgorithm, JS::Object);
21
    JS_DECLARE_ALLOCATOR(KeyAlgorithm);
22
23
public:
24
    static JS::NonnullGCPtr<KeyAlgorithm> create(JS::Realm&);
25
0
    virtual ~KeyAlgorithm() override = default;
26
27
0
    String const& name() const { return m_name; }
28
0
    void set_name(String name) { m_name = move(name); }
29
30
0
    JS::Realm& realm() const { return m_realm; }
31
32
protected:
33
    KeyAlgorithm(JS::Realm&);
34
35
    virtual void initialize(JS::Realm&) override;
36
    virtual void visit_edges(Visitor&) override;
37
38
private:
39
    JS_DECLARE_NATIVE_FUNCTION(name_getter);
40
41
    String m_name;
42
    JS::NonnullGCPtr<JS::Realm> m_realm;
43
};
44
45
// https://w3c.github.io/webcrypto/#RsaKeyAlgorithm-dictionary
46
class RsaKeyAlgorithm : public KeyAlgorithm {
47
    JS_OBJECT(RsaKeyAlgorithm, KeyAlgorithm);
48
    JS_DECLARE_ALLOCATOR(RsaKeyAlgorithm);
49
50
public:
51
    static JS::NonnullGCPtr<RsaKeyAlgorithm> create(JS::Realm&);
52
53
    virtual ~RsaKeyAlgorithm() override = default;
54
55
0
    u32 modulus_length() const { return m_modulus_length; }
56
0
    void set_modulus_length(u32 modulus_length) { m_modulus_length = modulus_length; }
57
58
0
    JS::NonnullGCPtr<JS::Uint8Array> public_exponent() const { return m_public_exponent; }
59
0
    void set_public_exponent(JS::NonnullGCPtr<JS::Uint8Array> public_exponent) { m_public_exponent = public_exponent; }
60
    WebIDL::ExceptionOr<void> set_public_exponent(::Crypto::UnsignedBigInteger);
61
62
protected:
63
    RsaKeyAlgorithm(JS::Realm&);
64
65
    virtual void initialize(JS::Realm&) override;
66
    virtual void visit_edges(Visitor&) override;
67
68
private:
69
    JS_DECLARE_NATIVE_FUNCTION(modulus_length_getter);
70
    JS_DECLARE_NATIVE_FUNCTION(public_exponent_getter);
71
72
    u32 m_modulus_length { 0 };
73
    JS::NonnullGCPtr<JS::Uint8Array> m_public_exponent;
74
};
75
76
// https://w3c.github.io/webcrypto/#RsaHashedKeyAlgorithm-dictionary
77
class RsaHashedKeyAlgorithm : public RsaKeyAlgorithm {
78
    JS_OBJECT(RsaHashedKeyAlgorithm, RsaKeyAlgorithm);
79
    JS_DECLARE_ALLOCATOR(RsaHashedKeyAlgorithm);
80
81
public:
82
    static JS::NonnullGCPtr<RsaHashedKeyAlgorithm> create(JS::Realm&);
83
84
0
    virtual ~RsaHashedKeyAlgorithm() override = default;
85
86
0
    HashAlgorithmIdentifier const& hash() const { return m_hash; }
87
0
    void set_hash(HashAlgorithmIdentifier hash) { m_hash = move(hash); }
88
89
protected:
90
    RsaHashedKeyAlgorithm(JS::Realm&);
91
92
    virtual void initialize(JS::Realm&) override;
93
94
private:
95
    JS_DECLARE_NATIVE_FUNCTION(hash_getter);
96
97
    HashAlgorithmIdentifier m_hash;
98
};
99
100
// https://w3c.github.io/webcrypto/#EcKeyAlgorithm-dictionary
101
class EcKeyAlgorithm : public KeyAlgorithm {
102
    JS_OBJECT(EcKeyAlgorithm, KeyAlgorithm);
103
    JS_DECLARE_ALLOCATOR(EcKeyAlgorithm);
104
105
public:
106
    static JS::NonnullGCPtr<EcKeyAlgorithm> create(JS::Realm&);
107
108
0
    virtual ~EcKeyAlgorithm() override = default;
109
110
0
    NamedCurve named_curve() const { return m_named_curve; }
111
0
    void set_named_curve(NamedCurve named_curve) { m_named_curve = named_curve; }
112
113
protected:
114
    EcKeyAlgorithm(JS::Realm&);
115
116
    virtual void initialize(JS::Realm&) override;
117
118
private:
119
    JS_DECLARE_NATIVE_FUNCTION(named_curve_getter);
120
121
    NamedCurve m_named_curve;
122
};
123
124
}