Serenity Operating System
1/*
2 * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
3 * Copyright (c) 2022, stelar7 <dudedbz@gmail.com>
4 *
5 * SPDX-License-Identifier: BSD-2-Clause
6 */
7
8#include <AK/Random.h>
9#include <AK/StringBuilder.h>
10#include <LibJS/Runtime/TypedArray.h>
11#include <LibWeb/Bindings/ExceptionOrUtils.h>
12#include <LibWeb/Bindings/Intrinsics.h>
13#include <LibWeb/Crypto/Crypto.h>
14#include <LibWeb/Crypto/SubtleCrypto.h>
15
16namespace Web::Crypto {
17
18WebIDL::ExceptionOr<JS::NonnullGCPtr<Crypto>> Crypto::create(JS::Realm& realm)
19{
20 return MUST_OR_THROW_OOM(realm.heap().allocate<Crypto>(realm, realm));
21}
22
23Crypto::Crypto(JS::Realm& realm)
24 : PlatformObject(realm)
25{
26}
27
28Crypto::~Crypto() = default;
29
30JS::ThrowCompletionOr<void> Crypto::initialize(JS::Realm& realm)
31{
32 MUST_OR_THROW_OOM(Base::initialize(realm));
33 set_prototype(&Bindings::ensure_web_prototype<Bindings::CryptoPrototype>(realm, "Crypto"));
34 m_subtle = TRY(Bindings::throw_dom_exception_if_needed(realm.vm(), [&]() {
35 return SubtleCrypto::create(realm);
36 }));
37
38 return {};
39}
40
41JS::NonnullGCPtr<SubtleCrypto> Crypto::subtle() const
42{
43 return *m_subtle;
44}
45
46// https://w3c.github.io/webcrypto/#dfn-Crypto-method-getRandomValues
47WebIDL::ExceptionOr<JS::Value> Crypto::get_random_values(JS::Value array) const
48{
49 // 1. If array is not an Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array, then throw a TypeMismatchError and terminate the algorithm.
50 if (!array.is_object() || !(is<JS::Int8Array>(array.as_object()) || is<JS::Uint8Array>(array.as_object()) || is<JS::Uint8ClampedArray>(array.as_object()) || is<JS::Int16Array>(array.as_object()) || is<JS::Uint16Array>(array.as_object()) || is<JS::Int32Array>(array.as_object()) || is<JS::Uint32Array>(array.as_object()) || is<JS::BigInt64Array>(array.as_object()) || is<JS::BigUint64Array>(array.as_object())))
51 return WebIDL::TypeMismatchError::create(realm(), "array must be one of Int8Array, Uint8Array, Uint8ClampedArray, Int16Array, Uint16Array, Int32Array, Uint32Array, BigInt64Array, or BigUint64Array");
52 auto& typed_array = static_cast<JS::TypedArrayBase&>(array.as_object());
53
54 // 2. If the byteLength of array is greater than 65536, throw a QuotaExceededError and terminate the algorithm.
55 if (typed_array.byte_length() > 65536)
56 return WebIDL::QuotaExceededError::create(realm(), "array's byteLength may not be greater than 65536");
57
58 // IMPLEMENTATION DEFINED: If the viewed array buffer is detached, throw a InvalidStateError and terminate the algorithm.
59 if (typed_array.viewed_array_buffer()->is_detached())
60 return WebIDL::InvalidStateError::create(realm(), "array is detached");
61 // FIXME: Handle SharedArrayBuffers
62
63 // 3. Overwrite all elements of array with cryptographically strong random values of the appropriate type.
64 fill_with_random(typed_array.viewed_array_buffer()->buffer().data(), typed_array.viewed_array_buffer()->buffer().size());
65
66 // 4. Return array.
67 return array;
68}
69
70// https://w3c.github.io/webcrypto/#dfn-Crypto-method-randomUUID
71WebIDL::ExceptionOr<String> Crypto::random_uuid() const
72{
73 auto& vm = realm().vm();
74
75 // 1. Let bytes be a byte sequence of length 16.
76 u8 bytes[16];
77
78 // 2. Fill bytes with cryptographically secure random bytes.
79 fill_with_random(bytes, 16);
80
81 // 3. Set the 4 most significant bits of bytes[6], which represent the UUID version, to 0100.
82 bytes[6] &= ~(1 << 7);
83 bytes[6] |= 1 << 6;
84 bytes[6] &= ~(1 << 5);
85 bytes[6] &= ~(1 << 4);
86
87 // 4. Set the 2 most significant bits of bytes[8], which represent the UUID variant, to 10.
88 bytes[8] |= 1 << 7;
89 bytes[8] &= ~(1 << 6);
90
91 /* 5. Return the string concatenation of
92 «
93 hexadecimal representation of bytes[0],
94 hexadecimal representation of bytes[1],
95 hexadecimal representation of bytes[2],
96 hexadecimal representation of bytes[3],
97 "-",
98 hexadecimal representation of bytes[4],
99 hexadecimal representation of bytes[5],
100 "-",
101 hexadecimal representation of bytes[6],
102 hexadecimal representation of bytes[7],
103 "-",
104 hexadecimal representation of bytes[8],
105 hexadecimal representation of bytes[9],
106 "-",
107 hexadecimal representation of bytes[10],
108 hexadecimal representation of bytes[11],
109 hexadecimal representation of bytes[12],
110 hexadecimal representation of bytes[13],
111 hexadecimal representation of bytes[14],
112 hexadecimal representation of bytes[15]
113 ».
114 */
115 StringBuilder builder;
116 TRY_OR_THROW_OOM(vm, builder.try_appendff("{:02x}{:02x}{:02x}{:02x}-", bytes[0], bytes[1], bytes[2], bytes[3]));
117 TRY_OR_THROW_OOM(vm, builder.try_appendff("{:02x}{:02x}-", bytes[4], bytes[5]));
118 TRY_OR_THROW_OOM(vm, builder.try_appendff("{:02x}{:02x}-", bytes[6], bytes[7]));
119 TRY_OR_THROW_OOM(vm, builder.try_appendff("{:02x}{:02x}-", bytes[8], bytes[9]));
120 TRY_OR_THROW_OOM(vm, builder.try_appendff("{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}", bytes[10], bytes[11], bytes[12], bytes[13], bytes[14], bytes[15]));
121 return TRY_OR_THROW_OOM(vm, builder.to_string());
122}
123
124void Crypto::visit_edges(Cell::Visitor& visitor)
125{
126 Base::visit_edges(visitor);
127 visitor.visit(m_subtle.ptr());
128}
129
130}