Commit 8b7b75b1 authored by Eric Roman's avatar Eric Roman Committed by Commit Bot

Make CryptoKey bindings return an object instead of DomString[].

Bug: 740871
Change-Id: I8571bf9448da2a257c45da7f1943226095ab08b7
Reviewed-on: https://chromium-review.googlesource.com/567638
Commit-Queue: Eric Roman <eroman@chromium.org>
Reviewed-by: default avatarKentaro Hara <haraken@chromium.org>
Reviewed-by: default avatarJeremy Roman <jbroman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#485877}
parent ed744dda
...@@ -402,7 +402,8 @@ TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyAES) { ...@@ -402,7 +402,8 @@ TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyAES) {
CryptoKey* new_key = V8CryptoKey::toImpl(result.As<v8::Object>()); CryptoKey* new_key = V8CryptoKey::toImpl(result.As<v8::Object>());
EXPECT_EQ("secret", new_key->type()); EXPECT_EQ("secret", new_key->type());
EXPECT_TRUE(new_key->extractable()); EXPECT_TRUE(new_key->extractable());
EXPECT_THAT(new_key->usages(), UnorderedElementsAre("encrypt", "decrypt")); EXPECT_EQ(kWebCryptoKeyUsageEncrypt | kWebCryptoKeyUsageDecrypt,
new_key->Key().Usages());
// Check that the keys have the same raw representation. // Check that the keys have the same raw representation.
WebVector<uint8_t> key_raw = WebVector<uint8_t> key_raw =
...@@ -438,7 +439,7 @@ TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyAES) { ...@@ -438,7 +439,7 @@ TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyAES) {
CryptoKey* new_key = V8CryptoKey::toImpl(result.As<v8::Object>()); CryptoKey* new_key = V8CryptoKey::toImpl(result.As<v8::Object>());
EXPECT_EQ("secret", new_key->type()); EXPECT_EQ("secret", new_key->type());
EXPECT_FALSE(new_key->extractable()); EXPECT_FALSE(new_key->extractable());
EXPECT_THAT(new_key->usages(), UnorderedElementsAre("decrypt")); EXPECT_EQ(kWebCryptoKeyUsageDecrypt, new_key->Key().Usages());
// Check that it can successfully decrypt data. // Check that it can successfully decrypt data.
Vector<uint8_t> iv(16, 0); Vector<uint8_t> iv(16, 0);
...@@ -473,7 +474,8 @@ TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyHMAC) { ...@@ -473,7 +474,8 @@ TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyHMAC) {
CryptoKey* new_key = V8CryptoKey::toImpl(result.As<v8::Object>()); CryptoKey* new_key = V8CryptoKey::toImpl(result.As<v8::Object>());
EXPECT_EQ("secret", new_key->type()); EXPECT_EQ("secret", new_key->type());
EXPECT_TRUE(new_key->extractable()); EXPECT_TRUE(new_key->extractable());
EXPECT_THAT(new_key->usages(), UnorderedElementsAre("sign", "verify")); EXPECT_EQ(kWebCryptoKeyUsageSign | kWebCryptoKeyUsageVerify,
new_key->Key().Usages());
// Check that the keys have the same raw representation. // Check that the keys have the same raw representation.
WebVector<uint8_t> key_raw = WebVector<uint8_t> key_raw =
...@@ -510,7 +512,7 @@ TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyHMAC) { ...@@ -510,7 +512,7 @@ TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyHMAC) {
CryptoKey* new_key = V8CryptoKey::toImpl(result.As<v8::Object>()); CryptoKey* new_key = V8CryptoKey::toImpl(result.As<v8::Object>());
EXPECT_EQ("secret", new_key->type()); EXPECT_EQ("secret", new_key->type());
EXPECT_FALSE(new_key->extractable()); EXPECT_FALSE(new_key->extractable());
EXPECT_THAT(new_key->usages(), UnorderedElementsAre("verify")); EXPECT_EQ(kWebCryptoKeyUsageVerify, new_key->Key().Usages());
// Check that it can successfully verify a signature. // Check that it can successfully verify a signature.
Vector<uint8_t> message{1, 2, 3}; Vector<uint8_t> message{1, 2, 3};
...@@ -547,7 +549,7 @@ TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyRSAHashed) { ...@@ -547,7 +549,7 @@ TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyRSAHashed) {
CryptoKey* new_private_key = V8CryptoKey::toImpl(result.As<v8::Object>()); CryptoKey* new_private_key = V8CryptoKey::toImpl(result.As<v8::Object>());
EXPECT_EQ("private", new_private_key->type()); EXPECT_EQ("private", new_private_key->type());
EXPECT_TRUE(new_private_key->extractable()); EXPECT_TRUE(new_private_key->extractable());
EXPECT_THAT(new_private_key->usages(), UnorderedElementsAre("sign")); EXPECT_EQ(kWebCryptoKeyUsageSign, new_private_key->Key().Usages());
// Check that the keys have the same PKCS8 representation. // Check that the keys have the same PKCS8 representation.
WebVector<uint8_t> key_raw = WebVector<uint8_t> key_raw =
...@@ -593,7 +595,7 @@ TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyRSAHashed) { ...@@ -593,7 +595,7 @@ TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyRSAHashed) {
CryptoKey* new_public_key = V8CryptoKey::toImpl(result.As<v8::Object>()); CryptoKey* new_public_key = V8CryptoKey::toImpl(result.As<v8::Object>());
EXPECT_EQ("public", new_public_key->type()); EXPECT_EQ("public", new_public_key->type());
EXPECT_TRUE(new_public_key->extractable()); EXPECT_TRUE(new_public_key->extractable());
EXPECT_THAT(new_public_key->usages(), UnorderedElementsAre("verify")); EXPECT_EQ(kWebCryptoKeyUsageVerify, new_public_key->Key().Usages());
// Check that it can successfully verify a signature. // Check that it can successfully verify a signature.
Vector<uint8_t> message{1, 2, 3}; Vector<uint8_t> message{1, 2, 3};
...@@ -638,7 +640,7 @@ TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyEC) { ...@@ -638,7 +640,7 @@ TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyEC) {
CryptoKey* new_private_key = V8CryptoKey::toImpl(result.As<v8::Object>()); CryptoKey* new_private_key = V8CryptoKey::toImpl(result.As<v8::Object>());
EXPECT_EQ("private", new_private_key->type()); EXPECT_EQ("private", new_private_key->type());
EXPECT_TRUE(new_private_key->extractable()); EXPECT_TRUE(new_private_key->extractable());
EXPECT_THAT(new_private_key->usages(), UnorderedElementsAre("sign")); EXPECT_EQ(kWebCryptoKeyUsageSign, new_private_key->Key().Usages());
// Check that the keys have the same PKCS8 representation. // Check that the keys have the same PKCS8 representation.
WebVector<uint8_t> key_raw = WebVector<uint8_t> key_raw =
...@@ -679,7 +681,7 @@ TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyEC) { ...@@ -679,7 +681,7 @@ TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyEC) {
CryptoKey* new_public_key = V8CryptoKey::toImpl(result.As<v8::Object>()); CryptoKey* new_public_key = V8CryptoKey::toImpl(result.As<v8::Object>());
EXPECT_EQ("public", new_public_key->type()); EXPECT_EQ("public", new_public_key->type());
EXPECT_TRUE(new_public_key->extractable()); EXPECT_TRUE(new_public_key->extractable());
EXPECT_THAT(new_public_key->usages(), UnorderedElementsAre("verify")); EXPECT_EQ(kWebCryptoKeyUsageVerify, new_public_key->Key().Usages());
// Check that it can successfully verify a signature. // Check that it can successfully verify a signature.
Vector<uint8_t> message{1, 2, 3}; Vector<uint8_t> message{1, 2, 3};
...@@ -714,7 +716,7 @@ TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyNoParams) { ...@@ -714,7 +716,7 @@ TEST(V8ScriptValueSerializerForModulesTest, RoundTripCryptoKeyNoParams) {
CryptoKey* new_key = V8CryptoKey::toImpl(result.As<v8::Object>()); CryptoKey* new_key = V8CryptoKey::toImpl(result.As<v8::Object>());
EXPECT_EQ("secret", new_key->type()); EXPECT_EQ("secret", new_key->type());
EXPECT_FALSE(new_key->extractable()); EXPECT_FALSE(new_key->extractable());
EXPECT_THAT(new_key->usages(), UnorderedElementsAre("deriveBits")); EXPECT_EQ(kWebCryptoKeyUsageDeriveBits, new_key->Key().Usages());
// Check that the keys derive the same bits. // Check that the keys derive the same bits.
WebCryptoAlgorithm hash(kWebCryptoAlgorithmIdSha256, nullptr); WebCryptoAlgorithm hash(kWebCryptoAlgorithmIdSha256, nullptr);
...@@ -744,8 +746,8 @@ TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyNoParams) { ...@@ -744,8 +746,8 @@ TEST(V8ScriptValueSerializerForModulesTest, DecodeCryptoKeyNoParams) {
CryptoKey* new_key = V8CryptoKey::toImpl(result.As<v8::Object>()); CryptoKey* new_key = V8CryptoKey::toImpl(result.As<v8::Object>());
EXPECT_EQ("secret", new_key->type()); EXPECT_EQ("secret", new_key->type());
EXPECT_FALSE(new_key->extractable()); EXPECT_FALSE(new_key->extractable());
EXPECT_THAT(new_key->usages(), EXPECT_EQ(kWebCryptoKeyUsageDeriveKey | kWebCryptoKeyUsageDeriveBits,
UnorderedElementsAre("deriveKey", "deriveBits")); new_key->Key().Usages());
// Check that it derives the right bits. // Check that it derives the right bits.
WebCryptoAlgorithm hash(kWebCryptoAlgorithmIdSha256, nullptr); WebCryptoAlgorithm hash(kWebCryptoAlgorithmIdSha256, nullptr);
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "bindings/core/v8/V8ObjectBuilder.h" #include "bindings/core/v8/V8ObjectBuilder.h"
#include "bindings/core/v8/V8Uint8Array.h" #include "bindings/core/v8/V8Uint8Array.h"
#include "platform/CryptoResult.h" #include "platform/CryptoResult.h"
#include "platform/bindings/ToV8.h"
#include "public/platform/WebCryptoAlgorithmParams.h" #include "public/platform/WebCryptoAlgorithmParams.h"
#include "public/platform/WebCryptoKeyAlgorithm.h" #include "public/platform/WebCryptoKeyAlgorithm.h"
#include "public/platform/WebString.h" #include "public/platform/WebString.h"
...@@ -153,14 +154,17 @@ ScriptValue CryptoKey::algorithm(ScriptState* script_state) { ...@@ -153,14 +154,17 @@ ScriptValue CryptoKey::algorithm(ScriptState* script_state) {
// instead is return the same (immutable) array. (Javascript callers can // instead is return the same (immutable) array. (Javascript callers can
// distinguish this by doing an == test on the arrays and seeing they are // distinguish this by doing an == test on the arrays and seeing they are
// different). // different).
Vector<String> CryptoKey::usages() const { ScriptValue CryptoKey::usages(ScriptState* script_state) {
Vector<String> result; Vector<String> result;
for (size_t i = 0; i < WTF_ARRAY_LENGTH(kKeyUsageMappings); ++i) { for (size_t i = 0; i < WTF_ARRAY_LENGTH(kKeyUsageMappings); ++i) {
WebCryptoKeyUsage usage = kKeyUsageMappings[i].value; WebCryptoKeyUsage usage = kKeyUsageMappings[i].value;
if (key_.Usages() & usage) if (key_.Usages() & usage)
result.push_back(KeyUsageToString(usage)); result.push_back(KeyUsageToString(usage));
} }
return result;
return ScriptValue(script_state,
ToV8(result, script_state->GetContext()->Global(),
script_state->GetIsolate()));
} }
bool CryptoKey::CanBeUsedForAlgorithm(const WebCryptoAlgorithm& algorithm, bool CryptoKey::CanBeUsedForAlgorithm(const WebCryptoAlgorithm& algorithm,
......
...@@ -58,7 +58,7 @@ class MODULES_EXPORT CryptoKey final ...@@ -58,7 +58,7 @@ class MODULES_EXPORT CryptoKey final
String type() const; String type() const;
bool extractable() const; bool extractable() const;
ScriptValue algorithm(ScriptState*); ScriptValue algorithm(ScriptState*);
Vector<String> usages() const; ScriptValue usages(ScriptState*);
const WebCryptoKey& Key() const { return key_; } const WebCryptoKey& Key() const { return key_; }
......
...@@ -36,6 +36,5 @@ ...@@ -36,6 +36,5 @@
readonly attribute DOMString type; readonly attribute DOMString type;
readonly attribute boolean extractable; readonly attribute boolean extractable;
[CallWith=ScriptState] readonly attribute object algorithm; [CallWith=ScriptState] readonly attribute object algorithm;
// TODO(jyasskin): |usages| should be of type object. [CallWith=ScriptState] readonly attribute object usages;
readonly attribute DOMString[] usages;
}; };
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment