summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Crypto/SubtleCrypto.cpp
blob: 575339a254ebda5af2982c12ae00cb9309af9a83 (plain)
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
/*
 * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCrypto/Hash/HashManager.h>
#include <LibJS/Runtime/ArrayBuffer.h>
#include <LibJS/Runtime/Promise.h>
#include <LibWeb/Bindings/IDLAbstractOperations.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Crypto/SubtleCrypto.h>
#include <LibWeb/DOM/DOMException.h>

namespace Web::Crypto {

JS::NonnullGCPtr<SubtleCrypto> SubtleCrypto::create(HTML::Window& window)
{
    return *window.heap().allocate<SubtleCrypto>(window.realm(), window);
}

SubtleCrypto::SubtleCrypto(HTML::Window& window)
    : PlatformObject(window.realm())
{
    set_prototype(&window.cached_web_prototype("SubtleCrypto"));
}

SubtleCrypto::~SubtleCrypto() = default;

JS::Promise* SubtleCrypto::digest(String const& algorithm, JS::Handle<JS::Object> const& data)
{
    auto& vm = Bindings::main_thread_vm();
    auto& realm = *vm.current_realm();

    // 1. Let algorithm be the algorithm parameter passed to the digest() method.

    // 2. Let data be the result of getting a copy of the bytes held by the data parameter passed to the digest() method.
    auto data_buffer_or_error = Bindings::IDL::get_buffer_source_copy(*data.cell());
    if (data_buffer_or_error.is_error()) {
        auto* error = wrap(realm, DOM::OperationError::create(global_object(), "Failed to copy bytes from ArrayBuffer"));
        auto* promise = JS::Promise::create(realm);
        promise->reject(error);
        return promise;
    }
    auto& data_buffer = data_buffer_or_error.value();

    // 3. Let normalizedAlgorithm be the result of normalizing an algorithm, with alg set to algorithm and op set to "digest".
    // FIXME: This is way more generic than it needs to be right now, so we simplify it.
    ::Crypto::Hash::HashKind hash_kind;
    if (algorithm.equals_ignoring_case("SHA-1"sv)) {
        hash_kind = ::Crypto::Hash::HashKind::SHA1;
    } else if (algorithm.equals_ignoring_case("SHA-256"sv)) {
        hash_kind = ::Crypto::Hash::HashKind::SHA256;
    } else if (algorithm.equals_ignoring_case("SHA-384"sv)) {
        hash_kind = ::Crypto::Hash::HashKind::SHA384;
    } else if (algorithm.equals_ignoring_case("SHA-512"sv)) {
        hash_kind = ::Crypto::Hash::HashKind::SHA512;
    }
    // 4. If an error occurred, return a Promise rejected with normalizedAlgorithm.
    else {
        auto* error = wrap(realm, DOM::NotSupportedError::create(global_object(), String::formatted("Invalid hash function '{}'", algorithm)));
        auto* promise = JS::Promise::create(realm);
        promise->reject(error);
        return promise;
    }

    // 5. Let promise be a new Promise.
    auto* promise = JS::Promise::create(realm);

    // 6. Return promise and perform the remaining steps in parallel.
    // FIXME: We don't have a good abstraction for this yet, so we do it in sync.

    // 7. If the following steps or referenced procedures say to throw an error, reject promise with the returned error and then terminate the algorithm.

    // 8. Let result be the result of performing the digest operation specified by normalizedAlgorithm using algorithm, with data as message.
    ::Crypto::Hash::Manager hash { hash_kind };
    hash.update(data_buffer);

    auto digest = hash.digest();
    auto result_buffer = ByteBuffer::copy(digest.immutable_data(), hash.digest_size());
    if (result_buffer.is_error()) {
        auto* error = wrap(realm, DOM::OperationError::create(global_object(), "Failed to create result buffer"));
        promise->reject(error);
        return promise;
    }

    auto* result = JS::ArrayBuffer::create(realm, result_buffer.release_value());

    // 9. Resolve promise with result.
    promise->fulfill(result);
    return promise;
}

}