Commit d4745597 authored by pneubeck@chromium.org's avatar pneubeck@chromium.org

Expose WebCrypto's algorithm normalization.

This exposes the WebCore::parseAlgorithm function through  blink::WebScriptBindings.

This will allow reusing the normalization in the implementation of the Chrome extension API enterprise.platformKeys.

To keep the exposure of types minimal, the dependency of parseAlgorithm on CryptoResult was removed.
On the way, renames parseAlgorithm to normalizeAlgorithm in order to match the WebCrypto spec and the filename.

BUG=364435

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

git-svn-id: svn://svn.chromium.org/blink/trunk@175657 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent a15c15c6
...@@ -47,9 +47,7 @@ ...@@ -47,9 +47,7 @@
namespace WebCore { namespace WebCore {
namespace { ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType errorType)
ExceptionCode toExceptionCode(blink::WebCryptoErrorType errorType)
{ {
switch (errorType) { switch (errorType) {
case blink::WebCryptoErrorTypeNotSupported: case blink::WebCryptoErrorTypeNotSupported:
...@@ -77,8 +75,6 @@ ExceptionCode toExceptionCode(blink::WebCryptoErrorType errorType) ...@@ -77,8 +75,6 @@ ExceptionCode toExceptionCode(blink::WebCryptoErrorType errorType)
return 0; return 0;
} }
} // namespace
// The PromiseState class contains all the state which is tied to an // The PromiseState class contains all the state which is tied to an
// ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread, // ExecutionContext. Whereas CryptoResultImpl can be deleted from any thread,
// PromiseState is not thread safe and must only be accessed and deleted from // PromiseState is not thread safe and must only be accessed and deleted from
...@@ -109,7 +105,7 @@ public: ...@@ -109,7 +105,7 @@ public:
void completeWithError(blink::WebCryptoErrorType errorType, const blink::WebString& errorDetails) void completeWithError(blink::WebCryptoErrorType errorType, const blink::WebString& errorDetails)
{ {
m_promiseResolver->reject(DOMException::create(toExceptionCode(errorType), errorDetails)); m_promiseResolver->reject(DOMException::create(webCryptoErrorToExceptionCode(errorType), errorDetails));
delete this; delete this;
} }
......
...@@ -32,6 +32,7 @@ ...@@ -32,6 +32,7 @@
#define CryptoResultImpl_h #define CryptoResultImpl_h
#include "bindings/v8/ScriptPromise.h" #include "bindings/v8/ScriptPromise.h"
#include "core/dom/ExceptionCode.h"
#include "platform/CryptoResult.h" #include "platform/CryptoResult.h"
#include "public/platform/WebCrypto.h" #include "public/platform/WebCrypto.h"
#include "wtf/Forward.h" #include "wtf/Forward.h"
...@@ -39,6 +40,8 @@ ...@@ -39,6 +40,8 @@
namespace WebCore { namespace WebCore {
ExceptionCode webCryptoErrorToExceptionCode(blink::WebCryptoErrorType);
// Wrapper around a Promise to notify completion of the crypto operation. // Wrapper around a Promise to notify completion of the crypto operation.
// //
// The thread on which CryptoResultImpl was created on is referred to as the // The thread on which CryptoResultImpl was created on is referred to as the
......
...@@ -95,28 +95,28 @@ blink::WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString) ...@@ -95,28 +95,28 @@ blink::WebCryptoKeyUsageMask keyUsageStringToMask(const String& usageString)
return 0; return 0;
} }
blink::WebCryptoKeyUsageMask toKeyUsage(AlgorithmOperation operation) blink::WebCryptoKeyUsageMask toKeyUsage(blink::WebCryptoOperation operation)
{ {
switch (operation) { switch (operation) {
case Encrypt: case blink::WebCryptoOperationEncrypt:
return blink::WebCryptoKeyUsageEncrypt; return blink::WebCryptoKeyUsageEncrypt;
case Decrypt: case blink::WebCryptoOperationDecrypt:
return blink::WebCryptoKeyUsageDecrypt; return blink::WebCryptoKeyUsageDecrypt;
case Sign: case blink::WebCryptoOperationSign:
return blink::WebCryptoKeyUsageSign; return blink::WebCryptoKeyUsageSign;
case Verify: case blink::WebCryptoOperationVerify:
return blink::WebCryptoKeyUsageVerify; return blink::WebCryptoKeyUsageVerify;
case DeriveKey: case blink::WebCryptoOperationDeriveKey:
return blink::WebCryptoKeyUsageDeriveKey; return blink::WebCryptoKeyUsageDeriveKey;
case DeriveBits: case blink::WebCryptoOperationDeriveBits:
return blink::WebCryptoKeyUsageDeriveBits; return blink::WebCryptoKeyUsageDeriveBits;
case WrapKey: case blink::WebCryptoOperationWrapKey:
return blink::WebCryptoKeyUsageWrapKey; return blink::WebCryptoKeyUsageWrapKey;
case UnwrapKey: case blink::WebCryptoOperationUnwrapKey:
return blink::WebCryptoKeyUsageUnwrapKey; return blink::WebCryptoKeyUsageUnwrapKey;
case Digest: case blink::WebCryptoOperationDigest:
case GenerateKey: case blink::WebCryptoOperationGenerateKey:
case ImportKey: case blink::WebCryptoOperationImportKey:
break; break;
} }
...@@ -168,7 +168,7 @@ Vector<String> Key::usages() const ...@@ -168,7 +168,7 @@ Vector<String> Key::usages() const
return result; return result;
} }
bool Key::canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm& algorithm, AlgorithmOperation op, CryptoResult* result) const bool Key::canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm& algorithm, blink::WebCryptoOperation op, CryptoResult* result) const
{ {
if (!(m_key.usages() & toKeyUsage(op))) { if (!(m_key.usages() & toKeyUsage(op))) {
result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key.usages does not permit this operation"); result->completeWithError(blink::WebCryptoErrorTypeInvalidAccess, "key.usages does not permit this operation");
......
...@@ -34,6 +34,7 @@ ...@@ -34,6 +34,7 @@
#include "bindings/v8/ScriptWrappable.h" #include "bindings/v8/ScriptWrappable.h"
#include "modules/crypto/NormalizeAlgorithm.h" #include "modules/crypto/NormalizeAlgorithm.h"
#include "platform/heap/Handle.h" #include "platform/heap/Handle.h"
#include "public/platform/WebCryptoAlgorithm.h"
#include "public/platform/WebCryptoKey.h" #include "public/platform/WebCryptoKey.h"
#include "wtf/Forward.h" #include "wtf/Forward.h"
#include "wtf/RefCounted.h" #include "wtf/RefCounted.h"
...@@ -62,7 +63,7 @@ public: ...@@ -62,7 +63,7 @@ public:
// If the key cannot be used with the indicated algorithm, returns false // If the key cannot be used with the indicated algorithm, returns false
// and completes the CryptoResult with an error. // and completes the CryptoResult with an error.
bool canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm&, AlgorithmOperation, CryptoResult*) const; bool canBeUsedForAlgorithm(const blink::WebCryptoAlgorithm&, blink::WebCryptoOperation, CryptoResult*) const;
// On failure, these return false and complete the CryptoResult with an error. // On failure, these return false and complete the CryptoResult with an error.
static bool parseFormat(const String&, blink::WebCryptoKeyFormat&, CryptoResult*); static bool parseFormat(const String&, blink::WebCryptoKeyFormat&, CryptoResult*);
......
...@@ -32,7 +32,6 @@ ...@@ -32,7 +32,6 @@
#include "modules/crypto/NormalizeAlgorithm.h" #include "modules/crypto/NormalizeAlgorithm.h"
#include "bindings/v8/Dictionary.h" #include "bindings/v8/Dictionary.h"
#include "platform/CryptoResult.h"
#include "platform/NotImplemented.h" #include "platform/NotImplemented.h"
#include "public/platform/WebCryptoAlgorithmParams.h" #include "public/platform/WebCryptoAlgorithmParams.h"
#include "public/platform/WebString.h" #include "public/platform/WebString.h"
...@@ -85,7 +84,7 @@ struct AlgorithmInfo { ...@@ -85,7 +84,7 @@ struct AlgorithmInfo {
// A map from the operation to the expected parameter type of the algorithm. // A map from the operation to the expected parameter type of the algorithm.
// If an operation is not applicable for the algorithm, set to Undefined. // If an operation is not applicable for the algorithm, set to Undefined.
const ParamsTypeOrUndefined operationToParamsType[LastAlgorithmOperation + 1]; const ParamsTypeOrUndefined operationToParamsType[blink::WebCryptoOperationLast + 1];
}; };
// A mapping from the algorithm ID to information about the algorithm. // A mapping from the algorithm ID to information about the algorithm.
...@@ -262,7 +261,7 @@ COMPILE_ASSERT(blink::WebCryptoAlgorithmIdRsaOaep == 8, RsaOaep_idDoesntMatch); ...@@ -262,7 +261,7 @@ COMPILE_ASSERT(blink::WebCryptoAlgorithmIdRsaOaep == 8, RsaOaep_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesCtr == 9, AesCtr_idDoesntMatch); COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesCtr == 9, AesCtr_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesKw == 10, AesKw_idDoesntMatch); COMPILE_ASSERT(blink::WebCryptoAlgorithmIdAesKw == 10, AesKw_idDoesntMatch);
COMPILE_ASSERT(blink::WebCryptoAlgorithmIdLast == 10, Last_idDoesntMatch); COMPILE_ASSERT(blink::WebCryptoAlgorithmIdLast == 10, Last_idDoesntMatch);
COMPILE_ASSERT(10 == LastAlgorithmOperation, UpdateParamsMapping); COMPILE_ASSERT(10 == blink::WebCryptoOperationLast, UpdateParamsMapping);
#if ASSERT_ENABLED #if ASSERT_ENABLED
...@@ -380,19 +379,22 @@ const AlgorithmInfo* lookupAlgorithmInfo(blink::WebCryptoAlgorithmId id) ...@@ -380,19 +379,22 @@ const AlgorithmInfo* lookupAlgorithmInfo(blink::WebCryptoAlgorithmId id)
return &algorithmIdToInfo[id]; return &algorithmIdToInfo[id];
} }
void completeWithSyntaxError(const String& message, CryptoResult* result) void setSyntaxError(const String& message, AlgorithmError* error)
{ {
result->completeWithError(blink::WebCryptoErrorTypeSyntax, message); error->errorType = blink::WebCryptoErrorTypeSyntax;
error->errorDetails = message;
} }
void completeWithNotSupportedError(const String& message, CryptoResult* result) void setNotSupportedError(const String& message, AlgorithmError* error)
{ {
result->completeWithError(blink::WebCryptoErrorTypeNotSupported, message); error->errorType = blink::WebCryptoErrorTypeNotSupported;
error->errorDetails = message;
} }
void completeWithDataError(const String& message, CryptoResult* result) void setDataError(const String& message, AlgorithmError* error)
{ {
result->completeWithError(blink::WebCryptoErrorTypeData, message); error->errorType = blink::WebCryptoErrorTypeData;
error->errorDetails = message;
} }
// ErrorContext holds a stack of string literals which describe what was // ErrorContext holds a stack of string literals which describe what was
...@@ -460,7 +462,7 @@ private: ...@@ -460,7 +462,7 @@ private:
// typedef (ArrayBuffer or ArrayBufferView) CryptoOperationData; // typedef (ArrayBuffer or ArrayBufferView) CryptoOperationData;
// //
// FIXME: Currently only supports ArrayBufferView. // FIXME: Currently only supports ArrayBufferView.
bool getOptionalCryptoOperationData(const Dictionary& raw, const char* propertyName, bool& hasProperty, RefPtr<ArrayBufferView>& buffer, const ErrorContext& context, CryptoResult* result) bool getOptionalCryptoOperationData(const Dictionary& raw, const char* propertyName, bool& hasProperty, RefPtr<ArrayBufferView>& buffer, const ErrorContext& context, AlgorithmError* error)
{ {
if (!raw.get(propertyName, buffer)) { if (!raw.get(propertyName, buffer)) {
hasProperty = false; hasProperty = false;
...@@ -470,7 +472,7 @@ bool getOptionalCryptoOperationData(const Dictionary& raw, const char* propertyN ...@@ -470,7 +472,7 @@ bool getOptionalCryptoOperationData(const Dictionary& raw, const char* propertyN
hasProperty = true; hasProperty = true;
if (!buffer) { if (!buffer) {
completeWithSyntaxError(context.toString(propertyName, "Not an ArrayBufferView"), result); setSyntaxError(context.toString(propertyName, "Not an ArrayBufferView"), error);
return false; return false;
} }
...@@ -482,21 +484,21 @@ bool getOptionalCryptoOperationData(const Dictionary& raw, const char* propertyN ...@@ -482,21 +484,21 @@ bool getOptionalCryptoOperationData(const Dictionary& raw, const char* propertyN
// typedef (ArrayBuffer or ArrayBufferView) CryptoOperationData; // typedef (ArrayBuffer or ArrayBufferView) CryptoOperationData;
// //
// FIXME: Currently only supports ArrayBufferView. // FIXME: Currently only supports ArrayBufferView.
bool getCryptoOperationData(const Dictionary& raw, const char* propertyName, RefPtr<ArrayBufferView>& buffer, const ErrorContext& context, CryptoResult* result) bool getCryptoOperationData(const Dictionary& raw, const char* propertyName, RefPtr<ArrayBufferView>& buffer, const ErrorContext& context, AlgorithmError* error)
{ {
bool hasProperty; bool hasProperty;
bool ok = getOptionalCryptoOperationData(raw, propertyName, hasProperty, buffer, context, result); bool ok = getOptionalCryptoOperationData(raw, propertyName, hasProperty, buffer, context, error);
if (!hasProperty) { if (!hasProperty) {
completeWithSyntaxError(context.toString(propertyName, "Missing required property"), result); setSyntaxError(context.toString(propertyName, "Missing required property"), error);
return false; return false;
} }
return ok; return ok;
} }
bool getUint8Array(const Dictionary& raw, const char* propertyName, RefPtr<Uint8Array>& array, const ErrorContext& context, CryptoResult* result) bool getUint8Array(const Dictionary& raw, const char* propertyName, RefPtr<Uint8Array>& array, const ErrorContext& context, AlgorithmError* error)
{ {
if (!raw.get(propertyName, array) || !array) { if (!raw.get(propertyName, array) || !array) {
completeWithSyntaxError(context.toString(propertyName, "Missing or not a Uint8Array"), result); setSyntaxError(context.toString(propertyName, "Missing or not a Uint8Array"), error);
return false; return false;
} }
return true; return true;
...@@ -505,25 +507,25 @@ bool getUint8Array(const Dictionary& raw, const char* propertyName, RefPtr<Uint8 ...@@ -505,25 +507,25 @@ bool getUint8Array(const Dictionary& raw, const char* propertyName, RefPtr<Uint8
// Defined by the WebCrypto spec as: // Defined by the WebCrypto spec as:
// //
// typedef Uint8Array BigInteger; // typedef Uint8Array BigInteger;
bool getBigInteger(const Dictionary& raw, const char* propertyName, RefPtr<Uint8Array>& array, const ErrorContext& context, CryptoResult* result) bool getBigInteger(const Dictionary& raw, const char* propertyName, RefPtr<Uint8Array>& array, const ErrorContext& context, AlgorithmError* error)
{ {
if (!getUint8Array(raw, propertyName, array, context, result)) if (!getUint8Array(raw, propertyName, array, context, error))
return false; return false;
if (!array->byteLength()) { if (!array->byteLength()) {
completeWithSyntaxError(context.toString(propertyName, "BigInteger should not be empty"), result); setSyntaxError(context.toString(propertyName, "BigInteger should not be empty"), error);
return false; return false;
} }
if (!raw.get(propertyName, array) || !array) { if (!raw.get(propertyName, array) || !array) {
completeWithSyntaxError(context.toString(propertyName, "Missing or not a Uint8Array"), result); setSyntaxError(context.toString(propertyName, "Missing or not a Uint8Array"), error);
return false; return false;
} }
return true; return true;
} }
// Gets an integer according to WebIDL's [EnforceRange]. // Gets an integer according to WebIDL's [EnforceRange].
bool getOptionalInteger(const Dictionary& raw, const char* propertyName, bool& hasProperty, double& value, double minValue, double maxValue, const ErrorContext& context, CryptoResult* result) bool getOptionalInteger(const Dictionary& raw, const char* propertyName, bool& hasProperty, double& value, double minValue, double maxValue, const ErrorContext& context, AlgorithmError* error)
{ {
double number; double number;
bool ok = raw.get(propertyName, number, hasProperty); bool ok = raw.get(propertyName, number, hasProperty);
...@@ -532,14 +534,14 @@ bool getOptionalInteger(const Dictionary& raw, const char* propertyName, bool& h ...@@ -532,14 +534,14 @@ bool getOptionalInteger(const Dictionary& raw, const char* propertyName, bool& h
return true; return true;
if (!ok || std::isnan(number)) { if (!ok || std::isnan(number)) {
completeWithSyntaxError(context.toString(propertyName, "Is not a number"), result); setSyntaxError(context.toString(propertyName, "Is not a number"), error);
return false; return false;
} }
number = trunc(number); number = trunc(number);
if (std::isinf(number) || number < minValue || number > maxValue) { if (std::isinf(number) || number < minValue || number > maxValue) {
completeWithSyntaxError(context.toString(propertyName, "Outside of numeric range"), result); setSyntaxError(context.toString(propertyName, "Outside of numeric range"), error);
return false; return false;
} }
...@@ -547,51 +549,51 @@ bool getOptionalInteger(const Dictionary& raw, const char* propertyName, bool& h ...@@ -547,51 +549,51 @@ bool getOptionalInteger(const Dictionary& raw, const char* propertyName, bool& h
return true; return true;
} }
bool getInteger(const Dictionary& raw, const char* propertyName, double& value, double minValue, double maxValue, const ErrorContext& context, CryptoResult* result) bool getInteger(const Dictionary& raw, const char* propertyName, double& value, double minValue, double maxValue, const ErrorContext& context, AlgorithmError* error)
{ {
bool hasProperty; bool hasProperty;
if (!getOptionalInteger(raw, propertyName, hasProperty, value, minValue, maxValue, context, result)) if (!getOptionalInteger(raw, propertyName, hasProperty, value, minValue, maxValue, context, error))
return false; return false;
if (!hasProperty) { if (!hasProperty) {
completeWithSyntaxError(context.toString(propertyName, "Missing required property"), result); setSyntaxError(context.toString(propertyName, "Missing required property"), error);
return false; return false;
} }
return true; return true;
} }
bool getUint32(const Dictionary& raw, const char* propertyName, uint32_t& value, const ErrorContext& context, CryptoResult* result) bool getUint32(const Dictionary& raw, const char* propertyName, uint32_t& value, const ErrorContext& context, AlgorithmError* error)
{ {
double number; double number;
if (!getInteger(raw, propertyName, number, 0, 0xFFFFFFFF, context, result)) if (!getInteger(raw, propertyName, number, 0, 0xFFFFFFFF, context, error))
return false; return false;
value = number; value = number;
return true; return true;
} }
bool getUint16(const Dictionary& raw, const char* propertyName, uint16_t& value, const ErrorContext& context, CryptoResult* result) bool getUint16(const Dictionary& raw, const char* propertyName, uint16_t& value, const ErrorContext& context, AlgorithmError* error)
{ {
double number; double number;
if (!getInteger(raw, propertyName, number, 0, 0xFFFF, context, result)) if (!getInteger(raw, propertyName, number, 0, 0xFFFF, context, error))
return false; return false;
value = number; value = number;
return true; return true;
} }
bool getUint8(const Dictionary& raw, const char* propertyName, uint8_t& value, const ErrorContext& context, CryptoResult* result) bool getUint8(const Dictionary& raw, const char* propertyName, uint8_t& value, const ErrorContext& context, AlgorithmError* error)
{ {
double number; double number;
if (!getInteger(raw, propertyName, number, 0, 0xFF, context, result)) if (!getInteger(raw, propertyName, number, 0, 0xFF, context, error))
return false; return false;
value = number; value = number;
return true; return true;
} }
bool getOptionalUint32(const Dictionary& raw, const char* propertyName, bool& hasValue, uint32_t& value, const ErrorContext& context, CryptoResult* result) bool getOptionalUint32(const Dictionary& raw, const char* propertyName, bool& hasValue, uint32_t& value, const ErrorContext& context, AlgorithmError* error)
{ {
double number; double number;
if (!getOptionalInteger(raw, propertyName, hasValue, number, 0, 0xFFFFFFFF, context, result)) if (!getOptionalInteger(raw, propertyName, hasValue, number, 0, 0xFFFFFFFF, context, error))
return false; return false;
if (hasValue) if (hasValue)
value = number; value = number;
...@@ -603,14 +605,14 @@ bool getOptionalUint32(const Dictionary& raw, const char* propertyName, bool& ha ...@@ -603,14 +605,14 @@ bool getOptionalUint32(const Dictionary& raw, const char* propertyName, bool& ha
// dictionary AesCbcParams : Algorithm { // dictionary AesCbcParams : Algorithm {
// CryptoOperationData iv; // CryptoOperationData iv;
// }; // };
bool parseAesCbcParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, CryptoResult* result) bool parseAesCbcParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error)
{ {
RefPtr<ArrayBufferView> iv; RefPtr<ArrayBufferView> iv;
if (!getCryptoOperationData(raw, "iv", iv, context, result)) if (!getCryptoOperationData(raw, "iv", iv, context, error))
return false; return false;
if (iv->byteLength() != 16) { if (iv->byteLength() != 16) {
completeWithDataError(context.toString("iv", "Must be 16 bytes"), result); setDataError(context.toString("iv", "Must be 16 bytes"), error);
return false; return false;
} }
...@@ -623,28 +625,28 @@ bool parseAesCbcParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPa ...@@ -623,28 +625,28 @@ bool parseAesCbcParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPa
// dictionary AesKeyGenParams : Algorithm { // dictionary AesKeyGenParams : Algorithm {
// [EnforceRange] unsigned short length; // [EnforceRange] unsigned short length;
// }; // };
bool parseAesKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, CryptoResult* result) bool parseAesKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error)
{ {
uint16_t length; uint16_t length;
if (!getUint16(raw, "length", length, context, result)) if (!getUint16(raw, "length", length, context, error))
return false; return false;
params = adoptPtr(new blink::WebCryptoAesKeyGenParams(length)); params = adoptPtr(new blink::WebCryptoAesKeyGenParams(length));
return true; return true;
} }
bool parseAlgorithm(const Dictionary&, AlgorithmOperation, blink::WebCryptoAlgorithm&, ErrorContext, CryptoResult*); bool parseAlgorithm(const Dictionary&, blink::WebCryptoOperation, blink::WebCryptoAlgorithm&, ErrorContext, AlgorithmError*);
bool parseHash(const Dictionary& raw, blink::WebCryptoAlgorithm& hash, ErrorContext context, CryptoResult* result) bool parseHash(const Dictionary& raw, blink::WebCryptoAlgorithm& hash, ErrorContext context, AlgorithmError* error)
{ {
Dictionary rawHash; Dictionary rawHash;
if (!raw.get("hash", rawHash)) { if (!raw.get("hash", rawHash)) {
completeWithSyntaxError(context.toString("hash", "Missing or not a dictionary"), result); setSyntaxError(context.toString("hash", "Missing or not a dictionary"), error);
return false; return false;
} }
context.add("hash"); context.add("hash");
return parseAlgorithm(rawHash, Digest, hash, context, result); return parseAlgorithm(rawHash, blink::WebCryptoOperationDigest, hash, context, error);
} }
// Defined by the WebCrypto spec as: // Defined by the WebCrypto spec as:
...@@ -652,10 +654,10 @@ bool parseHash(const Dictionary& raw, blink::WebCryptoAlgorithm& hash, ErrorCont ...@@ -652,10 +654,10 @@ bool parseHash(const Dictionary& raw, blink::WebCryptoAlgorithm& hash, ErrorCont
// dictionary HmacImportParams : Algorithm { // dictionary HmacImportParams : Algorithm {
// AlgorithmIdentifier hash; // AlgorithmIdentifier hash;
// }; // };
bool parseHmacImportParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, CryptoResult* result) bool parseHmacImportParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error)
{ {
blink::WebCryptoAlgorithm hash; blink::WebCryptoAlgorithm hash;
if (!parseHash(raw, hash, context, result)) if (!parseHash(raw, hash, context, error))
return false; return false;
params = adoptPtr(new blink::WebCryptoHmacImportParams(hash)); params = adoptPtr(new blink::WebCryptoHmacImportParams(hash));
...@@ -671,15 +673,15 @@ bool parseHmacImportParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorit ...@@ -671,15 +673,15 @@ bool parseHmacImportParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorit
// // size. // // size.
// unsigned long length; // unsigned long length;
// }; // };
bool parseHmacKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, CryptoResult* result) bool parseHmacKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error)
{ {
blink::WebCryptoAlgorithm hash; blink::WebCryptoAlgorithm hash;
if (!parseHash(raw, hash, context, result)) if (!parseHash(raw, hash, context, error))
return false; return false;
bool hasLength; bool hasLength;
uint32_t length = 0; uint32_t length = 0;
if (!getOptionalUint32(raw, "length", hasLength, length, context, result)) if (!getOptionalUint32(raw, "length", hasLength, length, context, error))
return false; return false;
params = adoptPtr(new blink::WebCryptoHmacKeyGenParams(hash, hasLength, length)); params = adoptPtr(new blink::WebCryptoHmacKeyGenParams(hash, hasLength, length));
...@@ -691,10 +693,10 @@ bool parseHmacKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorit ...@@ -691,10 +693,10 @@ bool parseHmacKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorit
// dictionary RsaHashedImportParams { // dictionary RsaHashedImportParams {
// AlgorithmIdentifier hash; // AlgorithmIdentifier hash;
// }; // };
bool parseRsaHashedImportParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, CryptoResult* result) bool parseRsaHashedImportParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error)
{ {
blink::WebCryptoAlgorithm hash; blink::WebCryptoAlgorithm hash;
if (!parseHash(raw, hash, context, result)) if (!parseHash(raw, hash, context, error))
return false; return false;
params = adoptPtr(new blink::WebCryptoRsaHashedImportParams(hash)); params = adoptPtr(new blink::WebCryptoRsaHashedImportParams(hash));
...@@ -711,18 +713,18 @@ bool parseRsaHashedImportParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAl ...@@ -711,18 +713,18 @@ bool parseRsaHashedImportParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAl
// unsigned long modulusLength; // unsigned long modulusLength;
// BigInteger publicExponent; // BigInteger publicExponent;
// }; // };
bool parseRsaHashedKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, CryptoResult* result) bool parseRsaHashedKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error)
{ {
uint32_t modulusLength; uint32_t modulusLength;
if (!getUint32(raw, "modulusLength", modulusLength, context, result)) if (!getUint32(raw, "modulusLength", modulusLength, context, error))
return false; return false;
RefPtr<Uint8Array> publicExponent; RefPtr<Uint8Array> publicExponent;
if (!getBigInteger(raw, "publicExponent", publicExponent, context, result)) if (!getBigInteger(raw, "publicExponent", publicExponent, context, error))
return false; return false;
blink::WebCryptoAlgorithm hash; blink::WebCryptoAlgorithm hash;
if (!parseHash(raw, hash, context, result)) if (!parseHash(raw, hash, context, error))
return false; return false;
params = adoptPtr(new blink::WebCryptoRsaHashedKeyGenParams(hash, modulusLength, static_cast<const unsigned char*>(publicExponent->baseAddress()), publicExponent->byteLength())); params = adoptPtr(new blink::WebCryptoRsaHashedKeyGenParams(hash, modulusLength, static_cast<const unsigned char*>(publicExponent->baseAddress()), publicExponent->byteLength()));
...@@ -735,14 +737,14 @@ bool parseRsaHashedKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAl ...@@ -735,14 +737,14 @@ bool parseRsaHashedKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAl
// CryptoOperationData counter; // CryptoOperationData counter;
// [EnforceRange] octet length; // [EnforceRange] octet length;
// }; // };
bool parseAesCtrParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, CryptoResult* result) bool parseAesCtrParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error)
{ {
RefPtr<ArrayBufferView> counter; RefPtr<ArrayBufferView> counter;
if (!getCryptoOperationData(raw, "counter", counter, context, result)) if (!getCryptoOperationData(raw, "counter", counter, context, error))
return false; return false;
uint8_t length; uint8_t length;
if (!getUint8(raw, "length", length, context, result)) if (!getUint8(raw, "length", length, context, error))
return false; return false;
params = adoptPtr(new blink::WebCryptoAesCtrParams(length, static_cast<const unsigned char*>(counter->baseAddress()), counter->byteLength())); params = adoptPtr(new blink::WebCryptoAesCtrParams(length, static_cast<const unsigned char*>(counter->baseAddress()), counter->byteLength()));
...@@ -756,20 +758,20 @@ bool parseAesCtrParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPa ...@@ -756,20 +758,20 @@ bool parseAesCtrParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPa
// CryptoOperationData? additionalData; // CryptoOperationData? additionalData;
// [EnforceRange] octet? tagLength; // May be 0-128 // [EnforceRange] octet? tagLength; // May be 0-128
// } // }
bool parseAesGcmParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, CryptoResult* result) bool parseAesGcmParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error)
{ {
RefPtr<ArrayBufferView> iv; RefPtr<ArrayBufferView> iv;
if (!getCryptoOperationData(raw, "iv", iv, context, result)) if (!getCryptoOperationData(raw, "iv", iv, context, error))
return false; return false;
bool hasAdditionalData; bool hasAdditionalData;
RefPtr<ArrayBufferView> additionalData; RefPtr<ArrayBufferView> additionalData;
if (!getOptionalCryptoOperationData(raw, "additionalData", hasAdditionalData, additionalData, context, result)) if (!getOptionalCryptoOperationData(raw, "additionalData", hasAdditionalData, additionalData, context, error))
return false; return false;
double tagLength; double tagLength;
bool hasTagLength; bool hasTagLength;
if (!getOptionalInteger(raw, "tagLength", hasTagLength, tagLength, 0, 128, context, result)) if (!getOptionalInteger(raw, "tagLength", hasTagLength, tagLength, 0, 128, context, error))
return false; return false;
const unsigned char* ivStart = static_cast<const unsigned char*>(iv->baseAddress()); const unsigned char* ivStart = static_cast<const unsigned char*>(iv->baseAddress());
...@@ -787,11 +789,11 @@ bool parseAesGcmParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPa ...@@ -787,11 +789,11 @@ bool parseAesGcmParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPa
// dictionary RsaOaepParams : Algorithm { // dictionary RsaOaepParams : Algorithm {
// CryptoOperationData? label; // CryptoOperationData? label;
// }; // };
bool parseRsaOaepParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, CryptoResult* result) bool parseRsaOaepParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error)
{ {
bool hasLabel; bool hasLabel;
RefPtr<ArrayBufferView> label; RefPtr<ArrayBufferView> label;
if (!getOptionalCryptoOperationData(raw, "label", hasLabel, label, context, result)) if (!getOptionalCryptoOperationData(raw, "label", hasLabel, label, context, error))
return false; return false;
const unsigned char* labelStart = hasLabel ? static_cast<const unsigned char*>(label->baseAddress()) : 0; const unsigned char* labelStart = hasLabel ? static_cast<const unsigned char*>(label->baseAddress()) : 0;
...@@ -801,85 +803,85 @@ bool parseRsaOaepParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmP ...@@ -801,85 +803,85 @@ bool parseRsaOaepParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmP
return true; return true;
} }
bool parseAlgorithmParams(const Dictionary& raw, blink::WebCryptoAlgorithmParamsType type, OwnPtr<blink::WebCryptoAlgorithmParams>& params, ErrorContext& context, CryptoResult* result) bool parseAlgorithmParams(const Dictionary& raw, blink::WebCryptoAlgorithmParamsType type, OwnPtr<blink::WebCryptoAlgorithmParams>& params, ErrorContext& context, AlgorithmError* error)
{ {
switch (type) { switch (type) {
case blink::WebCryptoAlgorithmParamsTypeNone: case blink::WebCryptoAlgorithmParamsTypeNone:
return true; return true;
case blink::WebCryptoAlgorithmParamsTypeAesCbcParams: case blink::WebCryptoAlgorithmParamsTypeAesCbcParams:
context.add("AesCbcParams"); context.add("AesCbcParams");
return parseAesCbcParams(raw, params, context, result); return parseAesCbcParams(raw, params, context, error);
case blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams: case blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams:
context.add("AesKeyGenParams"); context.add("AesKeyGenParams");
return parseAesKeyGenParams(raw, params, context, result); return parseAesKeyGenParams(raw, params, context, error);
case blink::WebCryptoAlgorithmParamsTypeHmacImportParams: case blink::WebCryptoAlgorithmParamsTypeHmacImportParams:
context.add("HmacImportParams"); context.add("HmacImportParams");
return parseHmacImportParams(raw, params, context, result); return parseHmacImportParams(raw, params, context, error);
case blink::WebCryptoAlgorithmParamsTypeHmacKeyGenParams: case blink::WebCryptoAlgorithmParamsTypeHmacKeyGenParams:
context.add("HmacKeyGenParams"); context.add("HmacKeyGenParams");
return parseHmacKeyGenParams(raw, params, context, result); return parseHmacKeyGenParams(raw, params, context, error);
case blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams: case blink::WebCryptoAlgorithmParamsTypeRsaHashedKeyGenParams:
context.add("RsaHashedKeyGenParams"); context.add("RsaHashedKeyGenParams");
return parseRsaHashedKeyGenParams(raw, params, context, result); return parseRsaHashedKeyGenParams(raw, params, context, error);
case blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams: case blink::WebCryptoAlgorithmParamsTypeRsaHashedImportParams:
context.add("RsaHashedImportParams"); context.add("RsaHashedImportParams");
return parseRsaHashedImportParams(raw, params, context, result); return parseRsaHashedImportParams(raw, params, context, error);
case blink::WebCryptoAlgorithmParamsTypeAesCtrParams: case blink::WebCryptoAlgorithmParamsTypeAesCtrParams:
context.add("AesCtrParams"); context.add("AesCtrParams");
return parseAesCtrParams(raw, params, context, result); return parseAesCtrParams(raw, params, context, error);
case blink::WebCryptoAlgorithmParamsTypeAesGcmParams: case blink::WebCryptoAlgorithmParamsTypeAesGcmParams:
context.add("AesGcmParams"); context.add("AesGcmParams");
return parseAesGcmParams(raw, params, context, result); return parseAesGcmParams(raw, params, context, error);
case blink::WebCryptoAlgorithmParamsTypeRsaOaepParams: case blink::WebCryptoAlgorithmParamsTypeRsaOaepParams:
context.add("RsaOaepParams"); context.add("RsaOaepParams");
return parseRsaOaepParams(raw, params, context, result); return parseRsaOaepParams(raw, params, context, error);
break; break;
} }
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
return false; return false;
} }
const char* operationToString(AlgorithmOperation op) const char* operationToString(blink::WebCryptoOperation op)
{ {
switch (op) { switch (op) {
case Encrypt: case blink::WebCryptoOperationEncrypt:
return "encrypt"; return "encrypt";
case Decrypt: case blink::WebCryptoOperationDecrypt:
return "decrypt"; return "decrypt";
case Sign: case blink::WebCryptoOperationSign:
return "sign"; return "sign";
case Verify: case blink::WebCryptoOperationVerify:
return "verify"; return "verify";
case Digest: case blink::WebCryptoOperationDigest:
return "digest"; return "digest";
case GenerateKey: case blink::WebCryptoOperationGenerateKey:
return "generateKey"; return "generateKey";
case ImportKey: case blink::WebCryptoOperationImportKey:
return "importKey"; return "importKey";
case DeriveKey: case blink::WebCryptoOperationDeriveKey:
return "deriveKey"; return "deriveKey";
case DeriveBits: case blink::WebCryptoOperationDeriveBits:
return "deriveBits"; return "deriveBits";
case WrapKey: case blink::WebCryptoOperationWrapKey:
return "wrapKey"; return "wrapKey";
case UnwrapKey: case blink::WebCryptoOperationUnwrapKey:
return "unwrapKey"; return "unwrapKey";
} }
return 0; return 0;
} }
bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryptoAlgorithm& algorithm, ErrorContext context, CryptoResult* result) bool parseAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, blink::WebCryptoAlgorithm& algorithm, ErrorContext context, AlgorithmError* error)
{ {
context.add("Algorithm"); context.add("Algorithm");
if (!raw.isObject()) { if (!raw.isObject()) {
completeWithSyntaxError(context.toString("Not an object"), result); setSyntaxError(context.toString("Not an object"), error);
return false; return false;
} }
String algorithmName; String algorithmName;
if (!raw.get("name", algorithmName)) { if (!raw.get("name", algorithmName)) {
completeWithSyntaxError(context.toString("name", "Missing or not a string"), result); setSyntaxError(context.toString("name", "Missing or not a string"), error);
return false; return false;
} }
...@@ -887,7 +889,7 @@ bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryp ...@@ -887,7 +889,7 @@ bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryp
if (!lookupAlgorithmIdByName(algorithmName, algorithmId)) { if (!lookupAlgorithmIdByName(algorithmName, algorithmId)) {
// FIXME: The spec says to return a SyntaxError if the input contains // FIXME: The spec says to return a SyntaxError if the input contains
// any non-ASCII characters. // any non-ASCII characters.
completeWithNotSupportedError(context.toString("Unrecognized name"), result); setNotSupportedError(context.toString("Unrecognized name"), error);
return false; return false;
} }
...@@ -898,14 +900,14 @@ bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryp ...@@ -898,14 +900,14 @@ bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryp
if (algorithmInfo->operationToParamsType[op] == Undefined) { if (algorithmInfo->operationToParamsType[op] == Undefined) {
context.add(algorithmIdToName(algorithmId)); context.add(algorithmIdToName(algorithmId));
completeWithNotSupportedError(context.toString("Unsupported operation", operationToString(op)), result); setNotSupportedError(context.toString("Unsupported operation", operationToString(op)), error);
return false; return false;
} }
blink::WebCryptoAlgorithmParamsType paramsType = static_cast<blink::WebCryptoAlgorithmParamsType>(algorithmInfo->operationToParamsType[op]); blink::WebCryptoAlgorithmParamsType paramsType = static_cast<blink::WebCryptoAlgorithmParamsType>(algorithmInfo->operationToParamsType[op]);
OwnPtr<blink::WebCryptoAlgorithmParams> params; OwnPtr<blink::WebCryptoAlgorithmParams> params;
if (!parseAlgorithmParams(raw, paramsType, params, context, result)) if (!parseAlgorithmParams(raw, paramsType, params, context, error))
return false; return false;
algorithm = blink::WebCryptoAlgorithm(algorithmId, params.release()); algorithm = blink::WebCryptoAlgorithm(algorithmId, params.release());
...@@ -914,9 +916,9 @@ bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryp ...@@ -914,9 +916,9 @@ bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryp
} // namespace } // namespace
bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryptoAlgorithm& algorithm, CryptoResult* result) bool normalizeAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, blink::WebCryptoAlgorithm& algorithm, AlgorithmError* error)
{ {
return parseAlgorithm(raw, op, algorithm, ErrorContext(), result); return parseAlgorithm(raw, op, algorithm, ErrorContext(), error);
} }
const char* algorithmIdToName(blink::WebCryptoAlgorithmId id) const char* algorithmIdToName(blink::WebCryptoAlgorithmId id)
......
...@@ -31,7 +31,9 @@ ...@@ -31,7 +31,9 @@
#ifndef NormalizeAlgorithm_h #ifndef NormalizeAlgorithm_h
#define NormalizeAlgorithm_h #define NormalizeAlgorithm_h
#include "public/platform/WebCrypto.h"
#include "public/platform/WebCryptoAlgorithm.h" #include "public/platform/WebCryptoAlgorithm.h"
#include "public/platform/WebString.h"
#include "wtf/Assertions.h" #include "wtf/Assertions.h"
#include "wtf/Forward.h" #include "wtf/Forward.h"
...@@ -39,23 +41,11 @@ namespace blink { class WebCryptoAlgorithm; } ...@@ -39,23 +41,11 @@ namespace blink { class WebCryptoAlgorithm; }
namespace WebCore { namespace WebCore {
class CryptoResult;
class Dictionary; class Dictionary;
enum AlgorithmOperation { struct AlgorithmError {
Encrypt, blink::WebCryptoErrorType errorType;
Decrypt, blink::WebString errorDetails;
Sign,
Verify,
Digest,
GenerateKey,
ImportKey,
DeriveKey,
DeriveBits,
WrapKey,
UnwrapKey,
// <---- End of list (keep this up-to-date)
LastAlgorithmOperation = UnwrapKey,
}; };
// Converts a javascript Dictionary to a WebCryptoAlgorithm object. // Converts a javascript Dictionary to a WebCryptoAlgorithm object.
...@@ -65,11 +55,11 @@ enum AlgorithmOperation { ...@@ -65,11 +55,11 @@ enum AlgorithmOperation {
// //
// On success returns true and sets the WebCryptoAlgorithm. // On success returns true and sets the WebCryptoAlgorithm.
// //
// On failure parseAlgorithm returns false and completes the CryptoResult // On failure normalizeAlgorithm returns false and sets the AlgorithmError with
// with a (non-localized) debug string. // a error type and a (non-localized) debug string.
// //
// [1] http://www.w3.org/TR/WebCryptoAPI/#algorithm-normalizing-rules // [1] http://www.w3.org/TR/WebCryptoAPI/#algorithm-normalizing-rules
bool parseAlgorithm(const Dictionary&, AlgorithmOperation, blink::WebCryptoAlgorithm&, CryptoResult*) WARN_UNUSED_RETURN; bool normalizeAlgorithm(const Dictionary&, blink::WebCryptoOperation, blink::WebCryptoAlgorithm&, AlgorithmError*) WARN_UNUSED_RETURN;
// Returns a null-terminated C-string literal. Caller can assume the pointer // Returns a null-terminated C-string literal. Caller can assume the pointer
// will be valid for the program's entire runtime. // will be valid for the program's entire runtime.
......
...@@ -64,16 +64,25 @@ static bool ensureNotNull(Key* key, const char* paramName, CryptoResult* result) ...@@ -64,16 +64,25 @@ static bool ensureNotNull(Key* key, const char* paramName, CryptoResult* result)
return true; return true;
} }
static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, AlgorithmOperation operationType, const ArrayPiece& signature, const ArrayPiece& dataBuffer) bool parseAlgorithm(const Dictionary& raw, blink::WebCryptoOperation op, blink::WebCryptoAlgorithm& algorithm, CryptoResult* result)
{
AlgorithmError error;
bool success = normalizeAlgorithm(raw, op, algorithm, &error);
if (!success)
result->completeWithError(error.errorType, error.errorDetails);
return success;
}
static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, blink::WebCryptoOperation operationType, const ArrayPiece& signature, const ArrayPiece& dataBuffer)
{ {
RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState); RefPtr<CryptoResultImpl> result = CryptoResultImpl::create(scriptState);
ScriptPromise promise = result->promise(); ScriptPromise promise = result->promise();
bool requiresKey = operationType != Digest; bool requiresKey = operationType != blink::WebCryptoOperationDigest;
if (requiresKey && !ensureNotNull(key, "key", result.get())) if (requiresKey && !ensureNotNull(key, "key", result.get()))
return promise; return promise;
if (operationType == Verify && !ensureNotNull(signature, "signature", result.get())) if (operationType == blink::WebCryptoOperationVerify && !ensureNotNull(signature, "signature", result.get()))
return promise; return promise;
if (!ensureNotNull(dataBuffer, "dataBuffer", result.get())) if (!ensureNotNull(dataBuffer, "dataBuffer", result.get()))
return promise; return promise;
...@@ -89,19 +98,19 @@ static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictio ...@@ -89,19 +98,19 @@ static ScriptPromise startCryptoOperation(ScriptState* scriptState, const Dictio
unsigned dataSize = dataBuffer.byteLength(); unsigned dataSize = dataBuffer.byteLength();
switch (operationType) { switch (operationType) {
case Encrypt: case blink::WebCryptoOperationEncrypt:
blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), data, dataSize, result->result()); blink::Platform::current()->crypto()->encrypt(algorithm, key->key(), data, dataSize, result->result());
break; break;
case Decrypt: case blink::WebCryptoOperationDecrypt:
blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), data, dataSize, result->result()); blink::Platform::current()->crypto()->decrypt(algorithm, key->key(), data, dataSize, result->result());
break; break;
case Sign: case blink::WebCryptoOperationSign:
blink::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result()); blink::Platform::current()->crypto()->sign(algorithm, key->key(), data, dataSize, result->result());
break; break;
case Verify: case blink::WebCryptoOperationVerify:
blink::Platform::current()->crypto()->verifySignature(algorithm, key->key(), signature.bytes(), signature.byteLength(), data, dataSize, result->result()); blink::Platform::current()->crypto()->verifySignature(algorithm, key->key(), signature.bytes(), signature.byteLength(), data, dataSize, result->result());
break; break;
case Digest: case blink::WebCryptoOperationDigest:
blink::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result()); blink::Platform::current()->crypto()->digest(algorithm, data, dataSize, result->result());
break; break;
default: default:
...@@ -119,27 +128,27 @@ SubtleCrypto::SubtleCrypto() ...@@ -119,27 +128,27 @@ SubtleCrypto::SubtleCrypto()
ScriptPromise SubtleCrypto::encrypt(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data) ScriptPromise SubtleCrypto::encrypt(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
{ {
return startCryptoOperation(scriptState, rawAlgorithm, key, Encrypt, ArrayPiece(), data); return startCryptoOperation(scriptState, rawAlgorithm, key, blink::WebCryptoOperationEncrypt, ArrayPiece(), data);
} }
ScriptPromise SubtleCrypto::decrypt(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data) ScriptPromise SubtleCrypto::decrypt(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
{ {
return startCryptoOperation(scriptState, rawAlgorithm, key, Decrypt, ArrayPiece(), data); return startCryptoOperation(scriptState, rawAlgorithm, key, blink::WebCryptoOperationDecrypt, ArrayPiece(), data);
} }
ScriptPromise SubtleCrypto::sign(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data) ScriptPromise SubtleCrypto::sign(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& data)
{ {
return startCryptoOperation(scriptState, rawAlgorithm, key, Sign, ArrayPiece(), data); return startCryptoOperation(scriptState, rawAlgorithm, key, blink::WebCryptoOperationSign, ArrayPiece(), data);
} }
ScriptPromise SubtleCrypto::verifySignature(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& signature, const ArrayPiece& data) ScriptPromise SubtleCrypto::verifySignature(ScriptState* scriptState, const Dictionary& rawAlgorithm, Key* key, const ArrayPiece& signature, const ArrayPiece& data)
{ {
return startCryptoOperation(scriptState, rawAlgorithm, key, Verify, signature, data); return startCryptoOperation(scriptState, rawAlgorithm, key, blink::WebCryptoOperationVerify, signature, data);
} }
ScriptPromise SubtleCrypto::digest(ScriptState* scriptState, const Dictionary& rawAlgorithm, const ArrayPiece& data) ScriptPromise SubtleCrypto::digest(ScriptState* scriptState, const Dictionary& rawAlgorithm, const ArrayPiece& data)
{ {
return startCryptoOperation(scriptState, rawAlgorithm, 0, Digest, ArrayPiece(), data); return startCryptoOperation(scriptState, rawAlgorithm, 0, blink::WebCryptoOperationDigest, ArrayPiece(), data);
} }
ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages) ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictionary& rawAlgorithm, bool extractable, const Vector<String>& rawKeyUsages)
...@@ -152,7 +161,7 @@ ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictiona ...@@ -152,7 +161,7 @@ ScriptPromise SubtleCrypto::generateKey(ScriptState* scriptState, const Dictiona
return promise; return promise;
blink::WebCryptoAlgorithm algorithm; blink::WebCryptoAlgorithm algorithm;
if (!parseAlgorithm(rawAlgorithm, GenerateKey, algorithm, result.get())) if (!parseAlgorithm(rawAlgorithm, blink::WebCryptoOperationGenerateKey, algorithm, result.get()))
return promise; return promise;
blink::Platform::current()->crypto()->generateKey(algorithm, extractable, keyUsages, result->result()); blink::Platform::current()->crypto()->generateKey(algorithm, extractable, keyUsages, result->result());
...@@ -176,7 +185,7 @@ ScriptPromise SubtleCrypto::importKey(ScriptState* scriptState, const String& ra ...@@ -176,7 +185,7 @@ ScriptPromise SubtleCrypto::importKey(ScriptState* scriptState, const String& ra
return promise; return promise;
blink::WebCryptoAlgorithm algorithm; blink::WebCryptoAlgorithm algorithm;
if (!parseAlgorithm(rawAlgorithm, ImportKey, algorithm, result.get())) if (!parseAlgorithm(rawAlgorithm, blink::WebCryptoOperationImportKey, algorithm, result.get()))
return promise; return promise;
blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), keyData.byteLength(), algorithm, extractable, keyUsages, result->result()); blink::Platform::current()->crypto()->importKey(format, keyData.bytes(), keyData.byteLength(), algorithm, extractable, keyUsages, result->result());
...@@ -220,7 +229,7 @@ ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF ...@@ -220,7 +229,7 @@ ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF
return promise; return promise;
blink::WebCryptoAlgorithm wrapAlgorithm; blink::WebCryptoAlgorithm wrapAlgorithm;
if (!parseAlgorithm(rawWrapAlgorithm, WrapKey, wrapAlgorithm, result.get())) if (!parseAlgorithm(rawWrapAlgorithm, blink::WebCryptoOperationWrapKey, wrapAlgorithm, result.get()))
return promise; return promise;
if (!key->extractable()) { if (!key->extractable()) {
...@@ -228,7 +237,7 @@ ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF ...@@ -228,7 +237,7 @@ ScriptPromise SubtleCrypto::wrapKey(ScriptState* scriptState, const String& rawF
return promise; return promise;
} }
if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, WrapKey, result.get())) if (!wrappingKey->canBeUsedForAlgorithm(wrapAlgorithm, blink::WebCryptoOperationWrapKey, result.get()))
return promise; return promise;
blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKey->key(), wrapAlgorithm, result->result()); blink::Platform::current()->crypto()->wrapKey(format, key->key(), wrappingKey->key(), wrapAlgorithm, result->result());
...@@ -254,14 +263,14 @@ ScriptPromise SubtleCrypto::unwrapKey(ScriptState* scriptState, const String& ra ...@@ -254,14 +263,14 @@ ScriptPromise SubtleCrypto::unwrapKey(ScriptState* scriptState, const String& ra
return promise; return promise;
blink::WebCryptoAlgorithm unwrapAlgorithm; blink::WebCryptoAlgorithm unwrapAlgorithm;
if (!parseAlgorithm(rawUnwrapAlgorithm, UnwrapKey, unwrapAlgorithm, result.get())) if (!parseAlgorithm(rawUnwrapAlgorithm, blink::WebCryptoOperationUnwrapKey, unwrapAlgorithm, result.get()))
return promise; return promise;
blink::WebCryptoAlgorithm unwrappedKeyAlgorithm; blink::WebCryptoAlgorithm unwrappedKeyAlgorithm;
if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, ImportKey, unwrappedKeyAlgorithm, result.get())) if (!parseAlgorithm(rawUnwrappedKeyAlgorithm, blink::WebCryptoOperationImportKey, unwrappedKeyAlgorithm, result.get()))
return promise; return promise;
if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, UnwrapKey, result.get())) if (!unwrappingKey->canBeUsedForAlgorithm(unwrapAlgorithm, blink::WebCryptoOperationUnwrapKey, result.get()))
return promise; return promise;
blink::Platform::current()->crypto()->unwrapKey(format, wrappedKey.bytes(), wrappedKey.byteLength(), 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());
......
/*
* Copyright (C) 2013 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "public/web/WebCryptoNormalize.h"
#include "bindings/v8/Dictionary.h"
#include "modules/crypto/CryptoResultImpl.h"
#include "modules/crypto/NormalizeAlgorithm.h"
#include "platform/CryptoResult.h"
#include "public/platform/WebString.h"
#include <v8.h>
using namespace WebCore;
namespace blink {
WebCryptoAlgorithm normalizeCryptoAlgorithm(v8::Handle<v8::Object> algorithmObject, WebCryptoOperation operation, int* exceptionCode, WebString* errorDetails, v8::Isolate* isolate)
{
WebCore::Dictionary algorithmDictionary(algorithmObject, isolate);
if (!algorithmDictionary.isUndefinedOrNull() && !algorithmDictionary.isObject())
return WebCryptoAlgorithm();
WebCryptoAlgorithm algorithm;
WebCore::AlgorithmError error;
if (!normalizeAlgorithm(algorithmDictionary, operation, algorithm, &error)) {
*exceptionCode = WebCore::webCryptoErrorToExceptionCode(error.errorType);
*errorDetails = error.errorDetails;
return WebCryptoAlgorithm();
}
return algorithm;
}
} // namespace blink
...@@ -112,6 +112,7 @@ ...@@ -112,6 +112,7 @@
'WebCachedURLRequest.cpp', 'WebCachedURLRequest.cpp',
'WebColorName.cpp', 'WebColorName.cpp',
'WebColorSuggestion.cpp', 'WebColorSuggestion.cpp',
'WebCryptoNormalize.cpp',
'WebCustomElement.cpp', 'WebCustomElement.cpp',
'WebDOMActivityLogger.cpp', 'WebDOMActivityLogger.cpp',
'WebDOMCustomEvent.cpp', 'WebDOMCustomEvent.cpp',
......
...@@ -40,6 +40,21 @@ ...@@ -40,6 +40,21 @@
namespace blink { namespace blink {
enum WebCryptoOperation {
WebCryptoOperationEncrypt,
WebCryptoOperationDecrypt,
WebCryptoOperationSign,
WebCryptoOperationVerify,
WebCryptoOperationDigest,
WebCryptoOperationGenerateKey,
WebCryptoOperationImportKey,
WebCryptoOperationDeriveKey,
WebCryptoOperationDeriveBits,
WebCryptoOperationWrapKey,
WebCryptoOperationUnwrapKey,
WebCryptoOperationLast = WebCryptoOperationUnwrapKey,
};
enum WebCryptoAlgorithmId { enum WebCryptoAlgorithmId {
WebCryptoAlgorithmIdAesCbc, WebCryptoAlgorithmIdAesCbc,
WebCryptoAlgorithmIdHmac, WebCryptoAlgorithmIdHmac,
......
/*
* Copyright (C) 2014 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef WebCryptoNormalize_h
#define WebCryptoNormalize_h
#include "../platform/WebCommon.h"
#include "../platform/WebCryptoAlgorithm.h"
namespace v8 {
class Isolate;
class Object;
template <class T> class Handle;
}
namespace blink {
class WebString;
// Converts a javascript Dictionary to a WebCryptoAlgorithm object.
//
// This corresponds with "normalizing" [1] the algorithm, and then validating
// the expected parameters for the algorithm/operation combination.
//
// On failure returns an null WebCryptoAlgorithm, sets the int to the
// ExceptionCode and the WebString to a (non-localized) debug string.
//
// [1] http://www.w3.org/TR/WebCryptoAPI/#algorithm-normalizing-rules
BLINK_EXPORT WebCryptoAlgorithm normalizeCryptoAlgorithm(v8::Handle<v8::Object>, WebCryptoOperation, int* exceptionCode, WebString* errorDetails, v8::Isolate*);
} // namespace blink
#endif
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