Commit 71e905e8 authored by eroman@chromium.org's avatar eroman@chromium.org

[webcrypto] Allow both ArrayBuffer and ArrayBufferView as inputs to crypto.subtle methods.

This is in line with the webcrypto spec which takes CryptoOperationData as inputs throughout, and defines it as:

  typedef (ArrayBuffer or ArrayBufferView) CryptoOperationData;

BUG=369179,245025

Review URL: https://codereview.chromium.org/267133002

git-svn-id: svn://svn.chromium.org/blink/trunk@173601 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 6957a467
Tests the digest() method using ArrayBuffer
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
PASS: sha-256 of [0] should be [6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d] and was
PASS successfullyParsed is true
TEST COMPLETE
<!DOCTYPE html>
<html>
<head>
<script src="../resources/js-test.js"></script>
<script src="resources/common.js"></script>
</head>
<body>
<p id="description"></p>
<div id="console"></div>
<script>
description("Tests the digest() method using ArrayBuffer");
jsTestIsAsync = true;
var algorithmName = "sha-256";
var inputHex = "00";
var expectedOutputHex = "6e340b9cffb37a989ca544e6bb780a2c78901d3fb33738768511a30617afa01d";
// Most of the crypto LayouTests use ArrayBufferView for data input. This one
// passes an ArrayBuffer instead.
var input = hexStringToUint8Array(inputHex).buffer;
crypto.subtle.digest({name : algorithmName}, input).then(function(result) {
bytesShouldMatchHexString("sha-256 of [0]", expectedOutputHex, result);
}).then(finishJSTest, failAndFinishJSTest);
</script>
</body>
</html>
......@@ -4,9 +4,9 @@ On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE
error is: TypeError: Failed to execute 'digest' on 'SubtleCrypto': 2 arguments required, but only 1 present.
error is: DataError: Invalid dataBuffer argument
error is: DataError: Invalid dataBuffer argument
error is: SyntaxError: Algorithm: Not an object
error is: TypeError: Failed to execute 'digest' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: TypeError: Failed to execute 'digest' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: TypeError: Failed to execute 'digest' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: NotSupportedError: Algorithm: Unrecognized name
error is: SyntaxError: Algorithm: name: Missing or not a string
PASS successfullyParsed is true
......
......@@ -3,9 +3,9 @@ Tests calling cypto.subtle.importKey with bad parameters
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
error is: DataError: Invalid keyData argument
error is: DataError: Invalid keyData argument
error is: SyntaxError: Algorithm: Not an object
error is: TypeError: Failed to execute 'importKey' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: TypeError: Failed to execute 'importKey' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: TypeError: Failed to execute 'importKey' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: SyntaxError: Invalid keyFormat argument
error is: SyntaxError: Invalid keyUsages argument
error is: SyntaxError: Invalid keyFormat argument
......
......@@ -3,9 +3,9 @@ Tests calling cypto.subtle.sign and crypto.subtle.verify with incorrect inputs
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
error is: DataError: Invalid signature argument
error is: DataError: Invalid signature argument
error is: DataError: Invalid signature argument
error is: TypeError: Failed to execute 'verify' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: TypeError: Failed to execute 'verify' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: TypeError: Failed to execute 'verify' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: NotSupportedError: SHA-1: Unsupported operation: sign
error is: NotSupportedError: RSAES-PKCS1-v1_5: Unsupported operation: sign
PASS successfullyParsed is true
......
......@@ -3,11 +3,11 @@ Tests calls to unwrapKey() with bad inputs.
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
error is: DataError: Invalid wrappedKey argument
error is: DataError: Invalid unwrappingKey argument
error is: TypeError: The 7th argument is neither an array, nor does it have indexed properties.
error is: SyntaxError: Algorithm: Not an object
error is: TypeError: Failed to execute 'unwrapKey' on 'SubtleCrypto': parameter 5 ('unwrappedKeyAlgorithm') is not an object.
error is: TypeError: Failed to execute 'unwrapKey' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: TypeError: Failed to execute 'unwrapKey' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: TypeError: Failed to execute 'unwrapKey' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: TypeError: Failed to execute 'unwrapKey' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: TypeError: Failed to execute 'unwrapKey' on 'SubtleCrypto': No function was found that matched the signature provided.
error is: SyntaxError: Invalid keyFormat argument
error is: NotSupportedError: SHA-1: Unsupported operation: unwrapKey
error is: InvalidAccessError: key.algorithm does not match that of operation
......
......@@ -46,10 +46,9 @@ namespace {
// Seems like the generated bindings should take care of these however it
// currently doesn't. See also http://crbug.com/264520
template <typename T>
bool ensureNotNull(T* x, const char* paramName, CryptoResult* result)
bool ensureNotNull(const ArrayPiece& x, const char* paramName, CryptoResult* result)
{
if (!x) {
if (x.isNull()) {
String message = String("Invalid ") + paramName + String(" argument");
result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebString(message));
return false;
......@@ -57,7 +56,17 @@ bool ensureNotNull(T* x, const char* paramName, CryptoResult* result)
return true;
}
ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, AlgorithmOperation operationType, ArrayBufferView* signature, ArrayBufferView* dataBuffer)
bool ensureNotNull(Key* key, const char* paramName, CryptoResult* result)
{
if (!key) {
String message = String("Invalid ") + paramName + String(" argument");
result->completeWithError(blink::WebCryptoErrorTypeType, blink::WebString(message));
return false;
}
return true;
}
ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, AlgorithmOperation operationType, const ArrayPiece& signature, const ArrayPiece& dataBuffer)
{
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
ScriptPromise promise = result->promise();
......@@ -78,8 +87,8 @@ ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg
if (requiresKey && !key->canBeUsedForAlgorithm(algorithm, operationType, result.get()))
return promise;
const unsigned char* data = static_cast<const unsigned char*>(dataBuffer->baseAddress());
unsigned dataSize = dataBuffer->byteLength();
const unsigned char* data = dataBuffer.bytes();
unsigned dataSize = dataBuffer.byteLength();
switch (operationType) {
case Encrypt:
......@@ -92,7 +101,7 @@ ScriptPromise startCryptoOperation(const Dictionary& rawAlgorithm, Key* key, Alg
blink::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result());
break;
case Verify:
blink::Platform::current()->crypto()->verifySignature(algorithm, key->key(), reinterpret_cast<const unsigned char*>(signature->baseAddress()), signature->byteLength(), data, dataSize, result->result());
blink::Platform::current()->crypto()->verifySignature(algorithm, key->key(), signature.bytes(), signature.byteLength(), data, dataSize, result->result());
break;
case Digest:
blink::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result());
......@@ -112,29 +121,29 @@ SubtleCrypto::SubtleCrypto()
ScriptWrappable::init(this);
}
ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* data)
ScriptPromise SubtleCrypto::encrypt(const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
{
return startCryptoOperation(rawAlgorithm, key, Encrypt, 0, data);
return startCryptoOperation(rawAlgorithm, key, Encrypt, ArrayPiece(), data);
}
ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* data)
ScriptPromise SubtleCrypto::decrypt(const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
{
return startCryptoOperation(rawAlgorithm, key, Decrypt, 0, data);
return startCryptoOperation(rawAlgorithm, key, Decrypt, ArrayPiece(), data);
}
ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* data)
ScriptPromise SubtleCrypto::sign(const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
{
return startCryptoOperation(rawAlgorithm, key, Sign, 0, data);
return startCryptoOperation(rawAlgorithm, key, Sign, ArrayPiece(), data);
}
ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key* key, ArrayBufferView* signature, ArrayBufferView* data)
ScriptPromise SubtleCrypto::verifySignature(const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& signature, const ArrayPiece& data)
{
return startCryptoOperation(rawAlgorithm, key, Verify, signature, data);
}
ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, ArrayBufferView* data)
ScriptPromise SubtleCrypto::digest(const Dictionary& rawAlgorithm, const ArrayPiece& data)
{
return startCryptoOperation(rawAlgorithm, 0, Digest, 0, data);
return startCryptoOperation(rawAlgorithm, 0, Digest, ArrayPiece(), data);
}
ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
......@@ -154,7 +163,7 @@ ScriptPromise SubtleCrypto::generateKey(const Dictionary& rawAlgorithm, bool ext
return promise;
}
ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView* keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
ScriptPromise SubtleCrypto::importKey(const String& rawFormat, const ArrayPiece& keyData, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
{
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
ScriptPromise promise = result->promise();
......@@ -174,9 +183,7 @@ ScriptPromise SubtleCrypto::importKey(const String& rawFormat, ArrayBufferView*
if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get()))
return promise;
const unsigned char* keyDataBytes = static_cast<unsigned char*>(keyData->baseAddress());
blink::Platform::current()->crypto()->importKey(format, keyDataBytes, keyData->byteLength(), algorithm, extractable, keyUsages, result->result());
blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), keyData.byteLength(), algorithm, extractable, keyUsages, result->result());
return promise;
}
......@@ -232,7 +239,7 @@ ScriptPromise SubtleCrypto::wrapKey(const String& rawFormat, Key* key, Key* wrap
return promise;
}
ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, ArrayBufferView* wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dictionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, const ArrayPiece& wrappedKey, Key* unwrappingKey, const Dictionary& rawUnwrapAlgorithm, const Dictionary& rawUnwrappedKeyAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
{
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create();
ScriptPromise promise = result->promise();
......@@ -261,10 +268,7 @@ ScriptPromise SubtleCrypto::unwrapKey(const String& rawFormat, ArrayBufferView*
if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result.get()))
return promise;
const unsigned char* wrappedKeyData = static_cast<const unsigned char*>(wrappedKey->baseAddress());
unsigned wrappedKeyDataSize = wrappedKey->byteLength();
blink::Platform::current()->crypto()->unwrapKey(format, wrappedKeyData, wrappedKeyDataSize, unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages, result->result());
blink::Platform::current()->crypto()->unwrapKey(format, wrappedKey.bytes(), wrappedKey.byteLength(), unwrappingKey->key(), unwrapAlgorithm, unwrappedKeyAlgorithm, extractable, keyUsages, result->result());
return promise;
}
......
......@@ -34,6 +34,7 @@
#include "bindings/v8/ScriptPromise.h"
#include "bindings/v8/ScriptWrappable.h"
#include "platform/heap/Handle.h"
#include "wtf/ArrayPiece.h"
#include "wtf/Forward.h"
#include "wtf/PassRefPtr.h"
#include "wtf/RefCounted.h"
......@@ -43,26 +44,26 @@ namespace WebCore {
class Dictionary;
class Key;
class SubtleCrypto : public GarbageCollectedFinalized<SubtleCrypto>, public ScriptWrappable {
class SubtleCrypto : public GarbageCollectedFinalized<SubtleCrypto>, public ScriptWrappable {
public:
static SubtleCrypto* create()
{
return new SubtleCrypto();
}
ScriptPromise encrypt(const Dictionary&, Key*, ArrayBufferView* data);
ScriptPromise decrypt(const Dictionary&, Key*, ArrayBufferView* data);
ScriptPromise sign(const Dictionary&, Key*, ArrayBufferView* data);
ScriptPromise encrypt(const Dictionary&, Key*, const ArrayPiece&);
ScriptPromise decrypt(const Dictionary&, Key*, const ArrayPiece&);
ScriptPromise sign(const Dictionary&, Key*, const ArrayPiece&);
// Note that this is not named "verify" because when compiling on Mac that expands to a macro and breaks.
ScriptPromise verifySignature(const Dictionary&, Key*, ArrayBufferView* signature, ArrayBufferView* data);
ScriptPromise digest(const Dictionary&, ArrayBufferView* data);
ScriptPromise verifySignature(const Dictionary&, Key*, const ArrayPiece& signature, const ArrayPiece& data);
ScriptPromise digest(const Dictionary&, const ArrayPiece& data);
ScriptPromise generateKey(const Dictionary&, bool extractable, const Vector<String>& keyUsages);
ScriptPromise importKey(const String&, ArrayBufferView*, const Dictionary&, bool extractable, const Vector<String>& keyUsages);
ScriptPromise importKey(const String&, const ArrayPiece&, const Dictionary&, bool extractable, const Vector<String>& keyUsages);
ScriptPromise exportKey(const String&, Key*);
ScriptPromise wrapKey(const String&, Key*, Key*, const Dictionary&);
ScriptPromise unwrapKey(const String&, ArrayBufferView*, Key*, const Dictionary&, const Dictionary&, bool, const Vector<String>&);
ScriptPromise unwrapKey(const String&, const ArrayPiece&, Key*, const Dictionary&, const Dictionary&, bool, const Vector<String>&);
void trace(Visitor*) { }
......
......@@ -33,16 +33,32 @@
NoInterfaceObject
] interface SubtleCrypto {
Promise encrypt(Dictionary algorithm, Key key, ArrayBufferView data);
Promise encrypt(Dictionary algorithm, Key key, ArrayBuffer data);
Promise decrypt(Dictionary algorithm, Key key, ArrayBufferView data);
Promise decrypt(Dictionary algorithm, Key key, ArrayBuffer data);
Promise sign(Dictionary algorithm, Key key, ArrayBufferView data);
Promise sign(Dictionary algorithm, Key key, ArrayBuffer data);
[ImplementedAs=verifySignature] Promise verify(Dictionary algorithm, Key key, ArrayBufferView signature, ArrayBufferView data);
[ImplementedAs=verifySignature] Promise verify(Dictionary algorithm, Key key, ArrayBufferView signature, ArrayBuffer data);
[ImplementedAs=verifySignature] Promise verify(Dictionary algorithm, Key key, ArrayBuffer signature, ArrayBuffer data);
[ImplementedAs=verifySignature] Promise verify(Dictionary algorithm, Key key, ArrayBuffer signature, ArrayBufferView data);
Promise digest(Dictionary algorithm, ArrayBufferView data);
Promise digest(Dictionary algorithm, ArrayBuffer data);
Promise generateKey(Dictionary algorithm, boolean extractable, DOMString[] keyUsages);
Promise importKey(DOMString format, ArrayBufferView keyData, Dictionary algorithm, boolean extractable, DOMString[] keyUsages);
Promise importKey(DOMString format, ArrayBuffer keyData, Dictionary algorithm, boolean extractable, DOMString[] keyUsages);
Promise exportKey(DOMString format, Key key);
Promise wrapKey(DOMString format, Key key, Key wrappingKey, Dictionary wrapAlgorithm);
Promise unwrapKey(DOMString format, ArrayBufferView wrappedKey, Key unwrappingKey, Dictionary unwrapAlgorithm, Dictionary unwrappedKeyAlgorithm, boolean extractable, DOMString[] keyUsages);
Promise unwrapKey(DOMString format, ArrayBuffer wrappedKey, Key unwrappingKey, Dictionary unwrapAlgorithm, Dictionary unwrappedKeyAlgorithm, boolean extractable, DOMString[] keyUsages);
};
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "config.h"
#include "wtf/ArrayPiece.h"
#include "wtf/ArrayBuffer.h"
#include "wtf/ArrayBufferView.h"
#include "wtf/Assertions.h"
namespace WTF {
ArrayPiece::ArrayPiece()
{
initNull();
}
ArrayPiece::ArrayPiece(void* data, unsigned byteLength)
{
initWithData(data, byteLength);
}
ArrayPiece::ArrayPiece(ArrayBuffer* buffer)
{
if (buffer) {
initWithData(buffer->data(), buffer->byteLength());
} else {
initNull();
}
}
ArrayPiece::ArrayPiece(ArrayBufferView* buffer)
{
if (buffer) {
initWithData(buffer->baseAddress(), buffer->byteLength());
} else {
initNull();
}
}
bool ArrayPiece::isNull() const
{
return m_isNull;
}
void* ArrayPiece::data() const
{
ASSERT(!isNull());
return m_data;
}
unsigned char* ArrayPiece::bytes() const
{
return static_cast<unsigned char*>(data());
}
unsigned ArrayPiece::byteLength() const
{
ASSERT(!isNull());
return m_byteLength;
}
void ArrayPiece::initWithData(void* data, unsigned byteLength)
{
m_byteLength = byteLength;
m_data = data;
m_isNull = false;
}
void ArrayPiece::initNull()
{
m_byteLength = 0;
m_data = 0;
m_isNull = true;
}
} // namespace WTF
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef ArrayPiece_h
#define ArrayPiece_h
#include "wtf/Forward.h"
#include "wtf/WTFExport.h"
namespace WTF {
// This class is for passing around un-owned bytes as a pointer + length.
// It supports implicit conversion from several other data types.
//
// ArrayPiece has the concept of being "null". This is different from an empty
// byte range. It is invalid to call methods other than isNull() on such
// instances.
//
// IMPORTANT: The data contained by ArrayPiece is NOT OWNED, so caution must be
// taken to ensure it is kept alive.
class WTF_EXPORT ArrayPiece {
public:
// Constructs a "null" ArrayPiece object.
ArrayPiece();
ArrayPiece(void* data, unsigned byteLength);
// Constructs an ArrayPiece from the given ArrayBuffer. If the input is a
// nullptr, then the constructed instance will be isNull().
ArrayPiece(ArrayBuffer*);
ArrayPiece(ArrayBufferView*);
bool isNull() const;
void* data() const;
unsigned char* bytes() const;
unsigned byteLength() const;
private:
void initWithData(void* data, unsigned byteLength);
void initNull();
void* m_data;
unsigned m_byteLength;
bool m_isNull;
};
} // namespace WTF
using WTF::ArrayPiece;
#endif // ArrayPiece_h
......@@ -33,6 +33,7 @@ namespace WTF {
class ArrayBuffer;
class ArrayBufferView;
class ArrayPiece;
class AtomicString;
class CString;
class Float32Array;
......@@ -61,6 +62,7 @@ using WTF::Vector;
using WTF::ArrayBuffer;
using WTF::ArrayBufferView;
using WTF::ArrayPiece;
using WTF::AtomicString;
using WTF::CString;
using WTF::Float32Array;
......
......@@ -13,6 +13,8 @@
'ArrayBufferDeallocationObserver.h',
'ArrayBufferView.cpp',
'ArrayBufferView.h',
'ArrayPiece.cpp',
'ArrayPiece.h',
'Assertions.cpp',
'Assertions.h',
'Atomics.h',
......
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