Index: third_party/WebKit/Source/modules/crypto/NormalizeAlgorithm.cpp |
diff --git a/third_party/WebKit/Source/modules/crypto/NormalizeAlgorithm.cpp b/third_party/WebKit/Source/modules/crypto/NormalizeAlgorithm.cpp |
index d09a7b0f0f7fc89a2a87814d0a4e568affd4efd1..592f4dcdc975c70e8db31a7549a13c245ff6d16d 100644 |
--- a/third_party/WebKit/Source/modules/crypto/NormalizeAlgorithm.cpp |
+++ b/third_party/WebKit/Source/modules/crypto/NormalizeAlgorithm.cpp |
@@ -46,6 +46,10 @@ |
#include <algorithm> |
#include <memory> |
+// TODO(eroman): Change the interface for constructing |
+// WebCryptoAlgorithmParams to allow transferring byte |
+// parameters (whereas currently it makes a copy). |
+ |
namespace blink { |
namespace { |
@@ -270,11 +274,18 @@ private: |
Vector<const char*, 10> m_messages; |
}; |
+static Vector<uint8_t> copyBytes(const DOMArrayPiece& source) |
+{ |
+ Vector<uint8_t> result; |
+ result.append(reinterpret_cast<const uint8_t*>(source.data()), source.byteLength()); |
+ return result; |
+} |
+ |
// Defined by the WebCrypto spec as: |
// |
// typedef (ArrayBuffer or ArrayBufferView) BufferSource; |
// |
-bool getOptionalBufferSource(const Dictionary& raw, const char* propertyName, bool& hasProperty, BufferSource& buffer, const ErrorContext& context, AlgorithmError* error) |
+bool getOptionalBufferSource(const Dictionary& raw, const char* propertyName, bool& hasProperty, Vector<uint8_t>& bytes, const ErrorContext& context, AlgorithmError* error) |
{ |
hasProperty = false; |
v8::Local<v8::Value> v8Value; |
@@ -283,12 +294,12 @@ bool getOptionalBufferSource(const Dictionary& raw, const char* propertyName, bo |
hasProperty = true; |
if (v8Value->IsArrayBufferView()) { |
- buffer.setArrayBufferView(V8ArrayBufferView::toImpl(v8::Local<v8::Object>::Cast(v8Value))); |
+ bytes = copyBytes(V8ArrayBufferView::toImpl(v8::Local<v8::Object>::Cast(v8Value))); |
return true; |
} |
if (v8Value->IsArrayBuffer()) { |
- buffer.setArrayBuffer(V8ArrayBuffer::toImpl(v8::Local<v8::Object>::Cast(v8Value))); |
+ bytes = copyBytes(V8ArrayBuffer::toImpl(v8::Local<v8::Object>::Cast(v8Value))); |
return true; |
} |
@@ -299,10 +310,10 @@ bool getOptionalBufferSource(const Dictionary& raw, const char* propertyName, bo |
return true; |
} |
-bool getBufferSource(const Dictionary& raw, const char* propertyName, BufferSource& buffer, const ErrorContext& context, AlgorithmError* error) |
+bool getBufferSource(const Dictionary& raw, const char* propertyName, Vector<uint8_t>& bytes, const ErrorContext& context, AlgorithmError* error) |
{ |
bool hasProperty; |
- bool ok = getOptionalBufferSource(raw, propertyName, hasProperty, buffer, context, error); |
+ bool ok = getOptionalBufferSource(raw, propertyName, hasProperty, bytes, context, error); |
if (!hasProperty) { |
setTypeError(context.toString(propertyName, "Missing required property"), error); |
return false; |
@@ -310,26 +321,28 @@ bool getBufferSource(const Dictionary& raw, const char* propertyName, BufferSour |
return ok; |
} |
-bool getUint8Array(const Dictionary& raw, const char* propertyName, DOMUint8Array*& array, const ErrorContext& context, AlgorithmError* error) |
+bool getUint8Array(const Dictionary& raw, const char* propertyName, Vector<uint8_t>& bytes, const ErrorContext& context, AlgorithmError* error) |
{ |
+ DOMUint8Array* array = nullptr; |
if (!DictionaryHelper::get(raw, propertyName, array) || !array) { |
setTypeError(context.toString(propertyName, "Missing or not a Uint8Array"), error); |
return false; |
} |
+ bytes = copyBytes(array); |
return true; |
} |
// Defined by the WebCrypto spec as: |
// |
// typedef Uint8Array BigInteger; |
-bool getBigInteger(const Dictionary& raw, const char* propertyName, DOMUint8Array*& array, const ErrorContext& context, AlgorithmError* error) |
+bool getBigInteger(const Dictionary& raw, const char* propertyName, Vector<uint8_t>& bytes, const ErrorContext& context, AlgorithmError* error) |
{ |
- if (!getUint8Array(raw, propertyName, array, context, error)) |
+ if (!getUint8Array(raw, propertyName, bytes, context, error)) |
return false; |
- if (!array->byteLength()) { |
+ if (bytes.isEmpty()) { |
// Empty BigIntegers represent 0 according to the spec |
- array = DOMUint8Array::create(1); |
+ bytes.fill(0, 1); |
} |
return true; |
@@ -451,13 +464,11 @@ bool getAlgorithmIdentifier(const Dictionary& raw, const char* propertyName, Alg |
// }; |
bool parseAesCbcParams(const Dictionary& raw, std::unique_ptr<WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error) |
{ |
- BufferSource ivBufferSource; |
- if (!getBufferSource(raw, "iv", ivBufferSource, context, error)) |
+ Vector<uint8_t> iv; |
+ if (!getBufferSource(raw, "iv", iv, context, error)) |
return false; |
- DOMArrayPiece iv(ivBufferSource); |
- |
- params = wrapUnique(new WebCryptoAesCbcParams(iv.bytes(), iv.byteLength())); |
+ params = wrapUnique(new WebCryptoAesCbcParams(iv.data(), iv.size())); |
return true; |
} |
@@ -565,7 +576,7 @@ bool parseRsaHashedKeyGenParams(const Dictionary& raw, std::unique_ptr<WebCrypto |
if (!getUint32(raw, "modulusLength", modulusLength, context, error)) |
return false; |
- DOMUint8Array* publicExponent = nullptr; |
+ Vector<uint8_t> publicExponent; |
if (!getBigInteger(raw, "publicExponent", publicExponent, context, error)) |
return false; |
@@ -573,7 +584,7 @@ bool parseRsaHashedKeyGenParams(const Dictionary& raw, std::unique_ptr<WebCrypto |
if (!parseHash(raw, hash, context, error)) |
return false; |
- params = wrapUnique(new WebCryptoRsaHashedKeyGenParams(hash, modulusLength, static_cast<const unsigned char*>(publicExponent->baseAddress()), publicExponent->byteLength())); |
+ params = wrapUnique(new WebCryptoRsaHashedKeyGenParams(hash, modulusLength, publicExponent.data(), publicExponent.size())); |
return true; |
} |
@@ -585,16 +596,15 @@ bool parseRsaHashedKeyGenParams(const Dictionary& raw, std::unique_ptr<WebCrypto |
// }; |
bool parseAesCtrParams(const Dictionary& raw, std::unique_ptr<WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error) |
{ |
- BufferSource counterBufferSource; |
- if (!getBufferSource(raw, "counter", counterBufferSource, context, error)) |
+ Vector<uint8_t> counter; |
+ if (!getBufferSource(raw, "counter", counter, context, error)) |
return false; |
- DOMArrayPiece counter(counterBufferSource); |
uint8_t length; |
if (!getUint8(raw, "length", length, context, error)) |
return false; |
- params = wrapUnique(new WebCryptoAesCtrParams(length, counter.bytes(), counter.byteLength())); |
+ params = wrapUnique(new WebCryptoAesCtrParams(length, counter.data(), counter.size())); |
return true; |
} |
@@ -607,13 +617,13 @@ bool parseAesCtrParams(const Dictionary& raw, std::unique_ptr<WebCryptoAlgorithm |
// } |
bool parseAesGcmParams(const Dictionary& raw, std::unique_ptr<WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error) |
{ |
- BufferSource ivBufferSource; |
- if (!getBufferSource(raw, "iv", ivBufferSource, context, error)) |
+ Vector<uint8_t> iv; |
+ if (!getBufferSource(raw, "iv", iv, context, error)) |
return false; |
bool hasAdditionalData; |
- BufferSource additionalDataBufferSource; |
- if (!getOptionalBufferSource(raw, "additionalData", hasAdditionalData, additionalDataBufferSource, context, error)) |
+ Vector<uint8_t> additionalData; |
+ if (!getOptionalBufferSource(raw, "additionalData", hasAdditionalData, additionalData, context, error)) |
return false; |
uint8_t tagLength = 0; |
@@ -621,10 +631,7 @@ bool parseAesGcmParams(const Dictionary& raw, std::unique_ptr<WebCryptoAlgorithm |
if (!getOptionalUint8(raw, "tagLength", hasTagLength, tagLength, context, error)) |
return false; |
- DOMArrayPiece iv(ivBufferSource); |
- DOMArrayPiece additionalData(additionalDataBufferSource, DOMArrayPiece::AllowNullPointToNullWithZeroSize); |
- |
- params = wrapUnique(new WebCryptoAesGcmParams(iv.bytes(), iv.byteLength(), hasAdditionalData, additionalData.bytes(), additionalData.byteLength(), hasTagLength, tagLength)); |
+ params = wrapUnique(new WebCryptoAesGcmParams(iv.data(), iv.size(), hasAdditionalData, additionalData.data(), additionalData.size(), hasTagLength, tagLength)); |
return true; |
} |
@@ -636,12 +643,11 @@ bool parseAesGcmParams(const Dictionary& raw, std::unique_ptr<WebCryptoAlgorithm |
bool parseRsaOaepParams(const Dictionary& raw, std::unique_ptr<WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error) |
{ |
bool hasLabel; |
- BufferSource labelBufferSource; |
- if (!getOptionalBufferSource(raw, "label", hasLabel, labelBufferSource, context, error)) |
+ Vector<uint8_t> label; |
+ if (!getOptionalBufferSource(raw, "label", hasLabel, label, context, error)) |
return false; |
- DOMArrayPiece label(labelBufferSource, DOMArrayPiece::AllowNullPointToNullWithZeroSize); |
- params = wrapUnique(new WebCryptoRsaOaepParams(hasLabel, label.bytes(), label.byteLength())); |
+ params = wrapUnique(new WebCryptoRsaOaepParams(hasLabel, label.data(), label.size())); |
return true; |
} |
@@ -770,12 +776,10 @@ bool parseEcdhKeyDeriveParams(const Dictionary& raw, std::unique_ptr<WebCryptoAl |
// }; |
bool parsePbkdf2Params(const Dictionary& raw, std::unique_ptr<WebCryptoAlgorithmParams>& params, const ErrorContext& context, AlgorithmError* error) |
{ |
- BufferSource saltBufferSource; |
- if (!getBufferSource(raw, "salt", saltBufferSource, context, error)) |
+ Vector<uint8_t> salt; |
+ if (!getBufferSource(raw, "salt", salt, context, error)) |
return false; |
- DOMArrayPiece salt(saltBufferSource); |
- |
uint32_t iterations; |
if (!getUint32(raw, "iterations", iterations, context, error)) |
return false; |
@@ -783,7 +787,7 @@ bool parsePbkdf2Params(const Dictionary& raw, std::unique_ptr<WebCryptoAlgorithm |
WebCryptoAlgorithm hash; |
if (!parseHash(raw, hash, context, error)) |
return false; |
- params = wrapUnique(new WebCryptoPbkdf2Params(hash, salt.bytes(), salt.byteLength(), iterations)); |
+ params = wrapUnique(new WebCryptoPbkdf2Params(hash, salt.data(), salt.size(), iterations)); |
return true; |
} |
@@ -819,17 +823,14 @@ bool parseHkdfParams(const Dictionary& raw, std::unique_ptr<WebCryptoAlgorithmPa |
WebCryptoAlgorithm hash; |
if (!parseHash(raw, hash, context, error)) |
return false; |
- BufferSource saltBufferSource; |
- if (!getBufferSource(raw, "salt", saltBufferSource, context, error)) |
+ Vector<uint8_t> salt; |
+ if (!getBufferSource(raw, "salt", salt, context, error)) |
return false; |
- BufferSource infoBufferSource; |
- if (!getBufferSource(raw, "info", infoBufferSource, context, error)) |
+ Vector<uint8_t> info; |
+ if (!getBufferSource(raw, "info", info, context, error)) |
return false; |
- DOMArrayPiece salt(saltBufferSource); |
- DOMArrayPiece info(infoBufferSource); |
- |
- params = wrapUnique(new WebCryptoHkdfParams(hash, salt.bytes(), salt.byteLength(), info.bytes(), info.byteLength())); |
+ params = wrapUnique(new WebCryptoHkdfParams(hash, salt.data(), salt.size(), info.data(), info.size())); |
return true; |
} |