File: ASN1Utilities.cpp

package info (click to toggle)
wpewebkit 2.38.6-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 311,508 kB
  • sloc: cpp: 2,653,313; javascript: 289,013; ansic: 121,268; xml: 64,149; python: 35,534; ruby: 17,287; perl: 15,877; asm: 11,072; yacc: 2,326; sh: 1,863; lex: 1,319; java: 937; makefile: 146; pascal: 60
file content (246 lines) | stat: -rw-r--r-- 7,910 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 * Copyright (C) 2021 Apple Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "ASN1Utilities.h"

#include <wtf/UniqueRef.h>
#include <wtf/Vector.h>

namespace ASN1 {

struct Object {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    virtual size_t encodedLengthBytes() const = 0;
    virtual void serializeTo(Vector<uint8_t>&) const = 0;
    virtual ~Object() = default;
protected:
    size_t sizeSerializedSize(size_t size) const
    {
        if (size <= 0x7F)
            return 1;
        ASSERT(size <= std::numeric_limits<uint16_t>::max());
        return 3;
    }
    void serializeSize(Vector<uint8_t>& vector, size_t size) const
    {
        ASSERT(size <= std::numeric_limits<uint16_t>::max());
        if (size > 0x7F) {
            vector.append(0x82);
            vector.append(static_cast<uint8_t>(size >> 8));
        }
        vector.append(static_cast<uint8_t>(size));
    }
};

struct ObjectIdentifier : Object {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    enum class Type : uint8_t {
        RsaEncryption,
        Rsapss,
        Sha384,
        Pkcs1MGF
    };
    ObjectIdentifier(Type type)
        : type(type) { }
private:
    const Type type;
    size_t encodedLengthBytes() const final { return 11; }
    void serializeTo(Vector<uint8_t>& vector) const final
    {
        vector.append(0x6);
        auto oidBytes = bytes();
        vector.append(oidBytes.size());
        vector.append(oidBytes.data(), oidBytes.size());
    }
    std::array<uint8_t, 9> bytes() const
    {
        switch (type) {
        case Type::RsaEncryption:
            return { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x01 };
        case Type::Rsapss:
            return { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0A };
        case Type::Sha384:
            return { 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02 };
        case Type::Pkcs1MGF:
            return { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x08 };
        }
    }
};

struct Sequence : Object {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    template<typename... Types>
    static UniqueRef<Sequence> create(Types... types)
    {
        return makeUniqueRef<Sequence>(Vector<UniqueRef<Object>>::from(std::forward<Types>(types)...));
    }
    Sequence(Vector<UniqueRef<Object>>&& elements)
        : elements(WTFMove(elements)) { }
private:
    const Vector<UniqueRef<Object>> elements;
    size_t encodedLengthBytes() const final
    {
        size_t elementBytes = elementEncodedLengthBytes();
        return 1 + sizeSerializedSize(elementBytes) + elementBytes;
    }
    void serializeTo(Vector<uint8_t>& vector) const final
    {
        vector.append(0x30);
        serializeSize(vector, elementEncodedLengthBytes());
        for (const auto& object : elements)
            object->serializeTo(vector);
    }
    size_t elementEncodedLengthBytes() const
    {
        size_t total = 0;
        for (const auto& element : elements)
            total += element->encodedLengthBytes();
        return total;
    }
};

struct Null : Object {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
private:
    size_t encodedLengthBytes() const final { return 2; }
    void serializeTo(Vector<uint8_t>& vector) const final
    {
        vector.append(0x05);
        vector.append(0x00);
    }
};

struct IndexWrapper : Object {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    IndexWrapper(uint8_t index, UniqueRef<Object>&& wrapped)
        : index(index), wrapped(WTFMove(wrapped)) { }
private:
    const uint8_t index;
    UniqueRef<Object> wrapped;
    size_t encodedLengthBytes() const final
    {
        size_t wrappedBytes = wrapped->encodedLengthBytes();
        return 1 + sizeSerializedSize(wrappedBytes) + wrappedBytes;
    }
    void serializeTo(Vector<uint8_t>& vector) const final
    {
        switch (index) {
        case 0:
            vector.append(0xa0);
            break;
        case 1:
            vector.append(0xa1);
            break;
        case 2:
            vector.append(0xa2);
            break;
        default:
            ASSERT_NOT_REACHED();
        }
        serializeSize(vector, wrapped->encodedLengthBytes());
        wrapped->serializeTo(vector);
    }
};

struct Integer : Object {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    Integer(uint8_t value)
        : value(value) { }
private:
    const uint8_t value;
    size_t encodedLengthBytes() const final
    {
        return 3;
    }
    void serializeTo(Vector<uint8_t>& vector) const final
    {
        vector.append(0x02);
        vector.append(0x01);
        vector.append(value);
    }
};

struct BitString : Object {
    WTF_MAKE_STRUCT_FAST_ALLOCATED;
    BitString(Vector<uint8_t>&& bytes)
        : bytes(WTFMove(bytes)) { }
private:
    const Vector<uint8_t> bytes;
    size_t encodedLengthBytes() const final
    {
        size_t byteSize = bytes.size();
        return 2 + sizeSerializedSize(byteSize) + byteSize;
    }
    void serializeTo(Vector<uint8_t>& vector) const final
    {
        vector.append(0x03);
        serializeSize(vector, bytes.size() + 1);
        vector.append(0x00);
        vector.appendVector(bytes);
    }
};

} // namespace ASN1

namespace TestWebKitAPI {

Vector<uint8_t> wrapPublicKeyWithRSAPSSOID(Vector<uint8_t>&& publicKey)
{
#if HAVE(RSA_PSS_OID)
    auto sequence = ASN1::Sequence::create(
        ASN1::Sequence::create(
            makeUniqueRef<ASN1::ObjectIdentifier>(ASN1::ObjectIdentifier::Type::Rsapss),
            ASN1::Sequence::create(
                makeUniqueRef<ASN1::IndexWrapper>(0, ASN1::Sequence::create(
                    makeUniqueRef<ASN1::ObjectIdentifier>(ASN1::ObjectIdentifier::Type::Sha384)
                )),
                makeUniqueRef<ASN1::IndexWrapper>(1, ASN1::Sequence::create(
                    makeUniqueRef<ASN1::ObjectIdentifier>(ASN1::ObjectIdentifier::Type::Pkcs1MGF),
                    ASN1::Sequence::create(
                        makeUniqueRef<ASN1::ObjectIdentifier>(ASN1::ObjectIdentifier::Type::Sha384)
                    )
                )),
                makeUniqueRef<ASN1::IndexWrapper>(2, makeUniqueRef<ASN1::Integer>(48))
            )
        ),
        makeUniqueRef<ASN1::BitString>(WTFMove(publicKey))
    );
#else
    auto sequence = ASN1::Sequence::create(
        ASN1::Sequence::create(
            makeUniqueRef<ASN1::ObjectIdentifier>(ASN1::ObjectIdentifier::Type::RsaEncryption),
            makeUniqueRef<ASN1::Null>()
        ),
        makeUniqueRef<ASN1::BitString>(WTFMove(publicKey))
    );
#endif

    Vector<uint8_t> wrapped;
    static_cast<ASN1::Object&>(sequence.get()).serializeTo(wrapped);
    return wrapped;
}

}