Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
devgianlu committed Dec 13, 2024
1 parent f796167 commit 7b5005b
Show file tree
Hide file tree
Showing 6 changed files with 392 additions and 2 deletions.
10 changes: 10 additions & 0 deletions Libraries/LibWeb/Crypto/CryptoAlgorithms.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,16 @@ class AlgorithmMethods {
return WebIDL::NotSupportedError::create(m_realm, "getKeyLength is not supported"_string);
}

virtual WebIDL::ExceptionOr<GC::Ref<JS::ArrayBuffer>> wrap_key(AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&)
{
return WebIDL::NotSupportedError::create(m_realm, "wrapKey is not supported"_string);
}

virtual WebIDL::ExceptionOr<GC::Ref<JS::ArrayBuffer>> unwrap_key(AlgorithmParams const&, GC::Ref<CryptoKey>, ByteBuffer const&)
{
return WebIDL::NotSupportedError::create(m_realm, "unwwrapKey is not supported"_string);
}

static NonnullOwnPtr<AlgorithmMethods> create(JS::Realm& realm) { return adopt_own(*new AlgorithmMethods(realm)); }

protected:
Expand Down
79 changes: 79 additions & 0 deletions Libraries/LibWeb/Crypto/CryptoBindings.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,94 @@
/*
* Copyright (c) 2024, Andrew Kaster <[email protected]>
* Copyright (c) 2024, Altomani Gianluca <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#include <AK/JsonObject.h>
#include <AK/JsonValue.h>
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Object.h>
#include <LibJS/Runtime/PromiseCapability.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Crypto/CryptoBindings.h>
#include <LibWeb/WebIDL/DOMException.h>
#include <LibWeb/WebIDL/Types.h>

namespace Web::Bindings {
#define JWK_PARSE_STRING_PROPERTY(name) \
if (json_object.has_string(#name##sv)) { \
key.name = TRY_OR_THROW_OOM(vm, String::from_byte_string(*json_object.get_byte_string(#name##sv))); \
}

JS::ThrowCompletionOr<JsonWebKey> JsonWebKey::parse(JS::Realm& realm, ReadonlyBytes data)
{
auto& vm = realm.vm();
// 1. Let data be the sequence of bytes to be parsed.

// 2. Let json be the Unicode string that results from interpreting data according to UTF-8.
// 3. Convert json to UTF-16.
auto json = TRY_OR_THROW_OOM(vm, String::from_utf8(data));
dbgln("JSON {}", json);

// 4. Let result be the object literal that results from executing the JSON.parse internal function
// in the context of a new global object, with text argument set to a JavaScript String containing json.
auto maybe_json_value = JsonValue::from_string(json);
if (maybe_json_value.is_error())
return vm.throw_completion<WebIDL::SyntaxError>(JS::ErrorType::JsonMalformed);

auto json_value = maybe_json_value.release_value();
if (!json_value.is_object()) {
return vm.throw_completion<WebIDL::SyntaxError>("JSON value is not an object"_string);
}

auto& json_object = json_value.as_object();

// 5. Let key be the result of converting result to the IDL dictionary type of JsonWebKey.
JsonWebKey key {};
JWK_PARSE_STRING_PROPERTY(kty);
JWK_PARSE_STRING_PROPERTY(use);
JWK_PARSE_STRING_PROPERTY(alg);
JWK_PARSE_STRING_PROPERTY(crv);
JWK_PARSE_STRING_PROPERTY(x);
JWK_PARSE_STRING_PROPERTY(y);
JWK_PARSE_STRING_PROPERTY(d);
JWK_PARSE_STRING_PROPERTY(n);
JWK_PARSE_STRING_PROPERTY(e);
JWK_PARSE_STRING_PROPERTY(p);
JWK_PARSE_STRING_PROPERTY(q);
JWK_PARSE_STRING_PROPERTY(dp);
JWK_PARSE_STRING_PROPERTY(dq);
JWK_PARSE_STRING_PROPERTY(qi);
JWK_PARSE_STRING_PROPERTY(k);

key.ext = json_object.get_bool("ext"sv);

if (json_object.has_array("key_ops"sv)) {
auto key_ops = *json_object.get_array("key_ops"sv);
key.key_ops = Vector<String> {};
TRY_OR_THROW_OOM(vm, key.key_ops->try_ensure_capacity(key_ops.size()));

TRY(key_ops.try_for_each([&key, &vm](auto value) -> JS::Completion {
key.key_ops->append(TRY_OR_THROW_OOM(vm, String::from_byte_string(value.as_string())));
return {};
}));
}

if (json_object.has("oth"sv))
TODO();

dbgln("KTY {}", key.kty);

// 6. If the kty field of key is not defined, then throw a DataError.
if (!key.kty.has_value())
return vm.throw_completion<WebIDL::DataError>("kty field is not defined"_string);

// 7. Return key.
return key;
}

#undef JWK_PARSE_STRING_PROPERTY

JS::ThrowCompletionOr<GC::Ref<JS::Object>> JsonWebKey::to_object(JS::Realm& realm)
{
Expand Down
2 changes: 2 additions & 0 deletions Libraries/LibWeb/Crypto/CryptoBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ struct JsonWebKey {
Optional<String> k;

JS::ThrowCompletionOr<GC::Ref<JS::Object>> to_object(JS::Realm&);

static JS::ThrowCompletionOr<JsonWebKey> parse(JS::Realm& realm, ReadonlyBytes data);
};

}
Loading

0 comments on commit 7b5005b

Please sign in to comment.