| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ | 29 */ |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "modules/crypto/NormalizeAlgorithm.h" | 32 #include "modules/crypto/NormalizeAlgorithm.h" |
| 33 | 33 |
| 34 #include "bindings/v8/Dictionary.h" | 34 #include "bindings/v8/Dictionary.h" |
| 35 #include "bindings/v8/ExceptionState.h" | 35 #include "bindings/v8/ExceptionState.h" |
| 36 #include "bindings/v8/ExceptionStatePlaceholder.h" |
| 36 #include "core/dom/ExceptionCode.h" | 37 #include "core/dom/ExceptionCode.h" |
| 37 #include "platform/NotImplemented.h" | 38 #include "platform/NotImplemented.h" |
| 38 #include "public/platform/WebCryptoAlgorithmParams.h" | 39 #include "public/platform/WebCryptoAlgorithmParams.h" |
| 39 #include "wtf/ArrayBuffer.h" | 40 #include "wtf/ArrayBuffer.h" |
| 40 #include "wtf/ArrayBufferView.h" | 41 #include "wtf/ArrayBufferView.h" |
| 41 #include "wtf/HashMap.h" | 42 #include "wtf/HashMap.h" |
| 42 #include "wtf/MathExtras.h" | 43 #include "wtf/MathExtras.h" |
| 43 #include "wtf/Uint8Array.h" | 44 #include "wtf/Uint8Array.h" |
| 44 #include "wtf/Vector.h" | 45 #include "wtf/Vector.h" |
| 45 #include "wtf/text/StringBuilder.h" | 46 #include "wtf/text/StringBuilder.h" |
| (...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 m_algorithms[mapping.algorithmId].algorithmName = mapping.algorithmName; | 196 m_algorithms[mapping.algorithmId].algorithmName = mapping.algorithmName; |
| 196 m_algorithms[mapping.algorithmId].algorithmId = mapping.algorithmId; | 197 m_algorithms[mapping.algorithmId].algorithmId = mapping.algorithmId; |
| 197 } | 198 } |
| 198 | 199 |
| 199 for (size_t i = 0; i < WTF_ARRAY_LENGTH(operationParamsMappings); ++i) { | 200 for (size_t i = 0; i < WTF_ARRAY_LENGTH(operationParamsMappings); ++i) { |
| 200 const OperationParamsMapping& mapping = operationParamsMappings[i]; | 201 const OperationParamsMapping& mapping = operationParamsMappings[i]; |
| 201 m_algorithms[mapping.algorithmId].paramsForOperation[mapping.operation]
= mapping.params; | 202 m_algorithms[mapping.algorithmId].paramsForOperation[mapping.operation]
= mapping.params; |
| 202 } | 203 } |
| 203 } | 204 } |
| 204 | 205 |
| 205 // ExceptionContext holds a stack of string literals which describe what was | 206 // ErrorContext holds a stack of string literals which describe what was |
| 206 // happening at the time the exception was thrown. This is helpful because | 207 // happening at the time the error occurred. This is helpful because |
| 207 // parsing of the algorithm dictionary can be recursive and it is difficult to | 208 // parsing of the algorithm dictionary can be recursive and it is difficult to |
| 208 // tell what went wrong from the exception type alone (TypeError). | 209 // tell what went wrong from a failure alone. |
| 209 class ExceptionContext { | 210 class ErrorContext { |
| 210 public: | 211 public: |
| 211 explicit ExceptionContext(AlgorithmOperation op) | |
| 212 : m_op(op) | |
| 213 { | |
| 214 } | |
| 215 | |
| 216 void add(const char* message) | 212 void add(const char* message) |
| 217 { | 213 { |
| 218 m_messages.append(message); | 214 m_messages.append(message); |
| 219 } | 215 } |
| 220 | 216 |
| 221 // Join all of the string literals into a single String. | 217 // Join all of the string literals into a single String. |
| 222 String toString() const | 218 String toString() const |
| 223 { | 219 { |
| 224 if (m_messages.isEmpty()) | 220 if (m_messages.isEmpty()) |
| 225 return String(); | 221 return String(); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 236 if (i) | 232 if (i) |
| 237 result.append(Separator, strlen(Separator)); | 233 result.append(Separator, strlen(Separator)); |
| 238 result.append(m_messages[i], strlen(m_messages[i])); | 234 result.append(m_messages[i], strlen(m_messages[i])); |
| 239 } | 235 } |
| 240 | 236 |
| 241 return result.toString(); | 237 return result.toString(); |
| 242 } | 238 } |
| 243 | 239 |
| 244 String toString(const char* message) const | 240 String toString(const char* message) const |
| 245 { | 241 { |
| 246 ExceptionContext stack(*this); | 242 ErrorContext stack(*this); |
| 247 stack.add(message); | 243 stack.add(message); |
| 248 return stack.toString(); | 244 return stack.toString(); |
| 249 } | 245 } |
| 250 | 246 |
| 251 String toString(const char* message1, const char* message2) const | 247 String toString(const char* message1, const char* message2) const |
| 252 { | 248 { |
| 253 ExceptionContext stack(*this); | 249 ErrorContext stack(*this); |
| 254 stack.add(message1); | 250 stack.add(message1); |
| 255 stack.add(message2); | 251 stack.add(message2); |
| 256 return stack.toString(); | 252 return stack.toString(); |
| 257 } | 253 } |
| 258 | 254 |
| 259 private: | 255 private: |
| 260 AlgorithmOperation m_op; | |
| 261 | |
| 262 // This inline size is large enough to avoid having to grow the Vector in | 256 // This inline size is large enough to avoid having to grow the Vector in |
| 263 // the majority of cases (up to 1 nested algorithm identifier). | 257 // the majority of cases (up to 1 nested algorithm identifier). |
| 264 Vector<const char*, 10> m_messages; | 258 Vector<const char*, 10> m_messages; |
| 265 }; | 259 }; |
| 266 | 260 |
| 267 bool getArrayBufferView(const Dictionary& raw, const char* propertyName, RefPtr<
ArrayBufferView>& buffer, const ExceptionContext& context, ExceptionState& excep
tionState) | 261 bool getArrayBufferView(const Dictionary& raw, const char* propertyName, RefPtr<
ArrayBufferView>& buffer, const ErrorContext& context, String& errorDetails) |
| 268 { | 262 { |
| 269 if (!raw.get(propertyName, buffer) || !buffer) { | 263 if (!raw.get(propertyName, buffer) || !buffer) { |
| 270 exceptionState.throwTypeError(context.toString(propertyName, "Missing or
not a ArrayBufferView")); | 264 errorDetails = context.toString(propertyName, "Missing or not a ArrayBuf
ferView"); |
| 271 return false; | 265 return false; |
| 272 } | 266 } |
| 273 return true; | 267 return true; |
| 274 } | 268 } |
| 275 | 269 |
| 276 bool getUint8Array(const Dictionary& raw, const char* propertyName, RefPtr<Uint8
Array>& array, const ExceptionContext& context, ExceptionState& exceptionState) | 270 bool getUint8Array(const Dictionary& raw, const char* propertyName, RefPtr<Uint8
Array>& array, const ErrorContext& context, String& errorDetails) |
| 277 { | 271 { |
| 278 if (!raw.get(propertyName, array) || !array) { | 272 if (!raw.get(propertyName, array) || !array) { |
| 279 exceptionState.throwTypeError(context.toString(propertyName, "Missing or
not a Uint8Array")); | 273 errorDetails = context.toString(propertyName, "Missing or not a Uint8Arr
ay"); |
| 280 return false; | 274 return false; |
| 281 } | 275 } |
| 282 return true; | 276 return true; |
| 283 } | 277 } |
| 284 | 278 |
| 285 // Gets an integer according to WebIDL's [EnforceRange]. | 279 // Gets an integer according to WebIDL's [EnforceRange]. |
| 286 bool getOptionalInteger(const Dictionary& raw, const char* propertyName, bool& h
asProperty, double& value, double minValue, double maxValue, const ExceptionCont
ext& context, ExceptionState& exceptionState) | 280 bool getOptionalInteger(const Dictionary& raw, const char* propertyName, bool& h
asProperty, double& value, double minValue, double maxValue, const ErrorContext&
context, String& errorDetails) |
| 287 { | 281 { |
| 288 double number; | 282 double number; |
| 289 bool ok = raw.get(propertyName, number, hasProperty); | 283 bool ok = raw.get(propertyName, number, hasProperty); |
| 290 | 284 |
| 291 if (!hasProperty) | 285 if (!hasProperty) |
| 292 return true; | 286 return true; |
| 293 | 287 |
| 294 if (!ok || std::isnan(number)) { | 288 if (!ok || std::isnan(number)) { |
| 295 exceptionState.throwTypeError(context.toString(propertyName, "Is not a n
umber")); | 289 errorDetails = context.toString(propertyName, "Is not a number"); |
| 296 return false; | 290 return false; |
| 297 } | 291 } |
| 298 | 292 |
| 299 number = trunc(number); | 293 number = trunc(number); |
| 300 | 294 |
| 301 if (std::isinf(number) || number < minValue || number > maxValue) { | 295 if (std::isinf(number) || number < minValue || number > maxValue) { |
| 302 exceptionState.throwTypeError(context.toString(propertyName, "Outside of
numeric range")); | 296 errorDetails = context.toString(propertyName, "Outside of numeric range"
); |
| 303 return false; | 297 return false; |
| 304 } | 298 } |
| 305 | 299 |
| 306 value = number; | 300 value = number; |
| 307 return true; | 301 return true; |
| 308 } | 302 } |
| 309 | 303 |
| 310 bool getInteger(const Dictionary& raw, const char* propertyName, double& value,
double minValue, double maxValue, const ExceptionContext& context, ExceptionStat
e& exceptionState) | 304 bool getInteger(const Dictionary& raw, const char* propertyName, double& value,
double minValue, double maxValue, const ErrorContext& context, String& errorDeta
ils) |
| 311 { | 305 { |
| 312 bool hasProperty; | 306 bool hasProperty; |
| 313 if (!getOptionalInteger(raw, propertyName, hasProperty, value, minValue, max
Value, context, exceptionState)) | 307 if (!getOptionalInteger(raw, propertyName, hasProperty, value, minValue, max
Value, context, errorDetails)) |
| 314 return false; | 308 return false; |
| 315 | 309 |
| 316 if (!hasProperty) { | 310 if (!hasProperty) { |
| 317 exceptionState.throwTypeError(context.toString(propertyName, "Missing re
quired property")); | 311 errorDetails = context.toString(propertyName, "Missing required property
"); |
| 318 return false; | 312 return false; |
| 319 } | 313 } |
| 320 | 314 |
| 321 return true; | 315 return true; |
| 322 } | 316 } |
| 323 | 317 |
| 324 bool getUint32(const Dictionary& raw, const char* propertyName, uint32_t& value,
const ExceptionContext& context, ExceptionState& exceptionState) | 318 bool getUint32(const Dictionary& raw, const char* propertyName, uint32_t& value,
const ErrorContext& context, String& errorDetails) |
| 325 { | 319 { |
| 326 double number; | 320 double number; |
| 327 if (!getInteger(raw, propertyName, number, 0, 0xFFFFFFFF, context, exception
State)) | 321 if (!getInteger(raw, propertyName, number, 0, 0xFFFFFFFF, context, errorDeta
ils)) |
| 328 return false; | 322 return false; |
| 329 value = number; | 323 value = number; |
| 330 return true; | 324 return true; |
| 331 } | 325 } |
| 332 | 326 |
| 333 bool getUint16(const Dictionary& raw, const char* propertyName, uint16_t& value,
const ExceptionContext& context, ExceptionState& exceptionState) | 327 bool getUint16(const Dictionary& raw, const char* propertyName, uint16_t& value,
const ErrorContext& context, String& errorDetails) |
| 334 { | 328 { |
| 335 double number; | 329 double number; |
| 336 if (!getInteger(raw, propertyName, number, 0, 0xFFFF, context, exceptionStat
e)) | 330 if (!getInteger(raw, propertyName, number, 0, 0xFFFF, context, errorDetails)
) |
| 337 return false; | 331 return false; |
| 338 value = number; | 332 value = number; |
| 339 return true; | 333 return true; |
| 340 } | 334 } |
| 341 | 335 |
| 342 bool getUint8(const Dictionary& raw, const char* propertyName, uint8_t& value, c
onst ExceptionContext& context, ExceptionState& exceptionState) | 336 bool getUint8(const Dictionary& raw, const char* propertyName, uint8_t& value, c
onst ErrorContext& context, String& errorDetails) |
| 343 { | 337 { |
| 344 double number; | 338 double number; |
| 345 if (!getInteger(raw, propertyName, number, 0, 0xFF, context, exceptionState)
) | 339 if (!getInteger(raw, propertyName, number, 0, 0xFF, context, errorDetails)) |
| 346 return false; | 340 return false; |
| 347 value = number; | 341 value = number; |
| 348 return true; | 342 return true; |
| 349 } | 343 } |
| 350 | 344 |
| 351 bool getOptionalUint32(const Dictionary& raw, const char* propertyName, bool& ha
sValue, uint32_t& value, const ExceptionContext& context, ExceptionState& except
ionState) | 345 bool getOptionalUint32(const Dictionary& raw, const char* propertyName, bool& ha
sValue, uint32_t& value, const ErrorContext& context, String& errorDetails) |
| 352 { | 346 { |
| 353 double number; | 347 double number; |
| 354 if (!getOptionalInteger(raw, propertyName, hasValue, number, 0, 0xFFFFFFFF,
context, exceptionState)) | 348 if (!getOptionalInteger(raw, propertyName, hasValue, number, 0, 0xFFFFFFFF,
context, errorDetails)) |
| 355 return false; | 349 return false; |
| 356 if (hasValue) | 350 if (hasValue) |
| 357 value = number; | 351 value = number; |
| 358 return true; | 352 return true; |
| 359 } | 353 } |
| 360 | 354 |
| 361 bool parseAesCbcParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPa
rams>& params, const ExceptionContext& context, ExceptionState& exceptionState) | 355 bool parseAesCbcParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPa
rams>& params, const ErrorContext& context, String& errorDetails) |
| 362 { | 356 { |
| 363 RefPtr<ArrayBufferView> iv; | 357 RefPtr<ArrayBufferView> iv; |
| 364 if (!getArrayBufferView(raw, "iv", iv, context, exceptionState)) | 358 if (!getArrayBufferView(raw, "iv", iv, context, errorDetails)) |
| 365 return false; | 359 return false; |
| 366 | 360 |
| 367 if (iv->byteLength() != 16) { | 361 if (iv->byteLength() != 16) { |
| 368 exceptionState.throwTypeError(context.toString("iv", "Must be 16 bytes")
); | 362 errorDetails = context.toString("iv", "Must be 16 bytes"); |
| 369 return false; | 363 return false; |
| 370 } | 364 } |
| 371 | 365 |
| 372 params = adoptPtr(new blink::WebCryptoAesCbcParams(static_cast<unsigned char
*>(iv->baseAddress()), iv->byteLength())); | 366 params = adoptPtr(new blink::WebCryptoAesCbcParams(static_cast<unsigned char
*>(iv->baseAddress()), iv->byteLength())); |
| 373 return true; | 367 return true; |
| 374 } | 368 } |
| 375 | 369 |
| 376 bool parseAesKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorith
mParams>& params, const ExceptionContext& context, ExceptionState& exceptionStat
e) | 370 bool parseAesKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorith
mParams>& params, const ErrorContext& context, String& errorDetails) |
| 377 { | 371 { |
| 378 uint16_t length; | 372 uint16_t length; |
| 379 if (!getUint16(raw, "length", length, context, exceptionState)) | 373 if (!getUint16(raw, "length", length, context, errorDetails)) |
| 380 return false; | 374 return false; |
| 381 | 375 |
| 382 params = adoptPtr(new blink::WebCryptoAesKeyGenParams(length)); | 376 params = adoptPtr(new blink::WebCryptoAesKeyGenParams(length)); |
| 383 return true; | 377 return true; |
| 384 } | 378 } |
| 385 | 379 |
| 386 bool normalizeAlgorithm(const Dictionary&, AlgorithmOperation, blink::WebCryptoA
lgorithm&, ExceptionContext, ExceptionState&); | 380 bool parseAlgorithm(const Dictionary&, AlgorithmOperation, blink::WebCryptoAlgor
ithm&, ErrorContext, String&, ExceptionState&); |
| 387 | 381 |
| 388 bool parseHash(const Dictionary& raw, blink::WebCryptoAlgorithm& hash, Exception
Context context, ExceptionState& exceptionState) | 382 bool parseHash(const Dictionary& raw, blink::WebCryptoAlgorithm& hash, ErrorCont
ext context, String& errorDetails) |
| 389 { | 383 { |
| 390 Dictionary rawHash; | 384 Dictionary rawHash; |
| 391 if (!raw.get("hash", rawHash)) { | 385 if (!raw.get("hash", rawHash)) { |
| 392 exceptionState.throwTypeError(context.toString("hash", "Missing or not a
dictionary")); | 386 errorDetails = context.toString("hash", "Missing or not a dictionary"); |
| 393 return false; | 387 return false; |
| 394 } | 388 } |
| 395 | 389 |
| 396 context.add("hash"); | 390 context.add("hash"); |
| 397 return normalizeAlgorithm(rawHash, Digest, hash, context, exceptionState); | 391 IgnorableExceptionState exceptionState; |
| 392 return parseAlgorithm(rawHash, Digest, hash, context, errorDetails, exceptio
nState); |
| 398 } | 393 } |
| 399 | 394 |
| 400 bool parseHmacParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPara
ms>& params, const ExceptionContext& context, ExceptionState& exceptionState) | 395 bool parseHmacParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPara
ms>& params, const ErrorContext& context, String& errorDetails) |
| 401 { | 396 { |
| 402 blink::WebCryptoAlgorithm hash; | 397 blink::WebCryptoAlgorithm hash; |
| 403 if (!parseHash(raw, hash, context, exceptionState)) | 398 if (!parseHash(raw, hash, context, errorDetails)) |
| 404 return false; | 399 return false; |
| 405 | 400 |
| 406 params = adoptPtr(new blink::WebCryptoHmacParams(hash)); | 401 params = adoptPtr(new blink::WebCryptoHmacParams(hash)); |
| 407 return true; | 402 return true; |
| 408 } | 403 } |
| 409 | 404 |
| 410 bool parseHmacKeyParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmP
arams>& params, const ExceptionContext& context, ExceptionState& exceptionState) | 405 bool parseHmacKeyParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmP
arams>& params, const ErrorContext& context, String& errorDetails) |
| 411 { | 406 { |
| 412 blink::WebCryptoAlgorithm hash; | 407 blink::WebCryptoAlgorithm hash; |
| 413 if (!parseHash(raw, hash, context, exceptionState)) | 408 if (!parseHash(raw, hash, context, errorDetails)) |
| 414 return false; | 409 return false; |
| 415 | 410 |
| 416 bool hasLength; | 411 bool hasLength; |
| 417 uint32_t length = 0; | 412 uint32_t length = 0; |
| 418 if (!getOptionalUint32(raw, "length", hasLength, length, context, exceptionS
tate)) | 413 if (!getOptionalUint32(raw, "length", hasLength, length, context, errorDetai
ls)) |
| 419 return false; | 414 return false; |
| 420 | 415 |
| 421 params = adoptPtr(new blink::WebCryptoHmacKeyParams(hash, hasLength, length)
); | 416 params = adoptPtr(new blink::WebCryptoHmacKeyParams(hash, hasLength, length)
); |
| 422 return true; | 417 return true; |
| 423 } | 418 } |
| 424 | 419 |
| 425 bool parseRsaSsaParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPa
rams>& params, const ExceptionContext& context, ExceptionState& exceptionState) | 420 bool parseRsaSsaParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPa
rams>& params, const ErrorContext& context, String& errorDetails) |
| 426 { | 421 { |
| 427 blink::WebCryptoAlgorithm hash; | 422 blink::WebCryptoAlgorithm hash; |
| 428 if (!parseHash(raw, hash, context, exceptionState)) | 423 if (!parseHash(raw, hash, context, errorDetails)) |
| 429 return false; | 424 return false; |
| 430 | 425 |
| 431 params = adoptPtr(new blink::WebCryptoRsaSsaParams(hash)); | 426 params = adoptPtr(new blink::WebCryptoRsaSsaParams(hash)); |
| 432 return true; | 427 return true; |
| 433 } | 428 } |
| 434 | 429 |
| 435 bool parseRsaKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorith
mParams>& params, const ExceptionContext& context, ExceptionState& exceptionStat
e) | 430 bool parseRsaKeyGenParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorith
mParams>& params, const ErrorContext& context, String& errorDetails) |
| 436 { | 431 { |
| 437 uint32_t modulusLength; | 432 uint32_t modulusLength; |
| 438 if (!getUint32(raw, "modulusLength", modulusLength, context, exceptionState)
) | 433 if (!getUint32(raw, "modulusLength", modulusLength, context, errorDetails)) |
| 439 return false; | 434 return false; |
| 440 | 435 |
| 441 RefPtr<Uint8Array> publicExponent; | 436 RefPtr<Uint8Array> publicExponent; |
| 442 if (!getUint8Array(raw, "publicExponent", publicExponent, context, exception
State)) | 437 if (!getUint8Array(raw, "publicExponent", publicExponent, context, errorDeta
ils)) |
| 443 return false; | 438 return false; |
| 444 | 439 |
| 445 params = adoptPtr(new blink::WebCryptoRsaKeyGenParams(modulusLength, static_
cast<const unsigned char*>(publicExponent->baseAddress()), publicExponent->byteL
ength())); | 440 params = adoptPtr(new blink::WebCryptoRsaKeyGenParams(modulusLength, static_
cast<const unsigned char*>(publicExponent->baseAddress()), publicExponent->byteL
ength())); |
| 446 return true; | 441 return true; |
| 447 } | 442 } |
| 448 | 443 |
| 449 bool parseAesCtrParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPa
rams>& params, const ExceptionContext& context, ExceptionState& es) | 444 bool parseAesCtrParams(const Dictionary& raw, OwnPtr<blink::WebCryptoAlgorithmPa
rams>& params, const ErrorContext& context, String& es) |
| 450 { | 445 { |
| 451 RefPtr<Uint8Array> counter; | 446 RefPtr<Uint8Array> counter; |
| 452 if (!getUint8Array(raw, "counter", counter, context, es)) | 447 if (!getUint8Array(raw, "counter", counter, context, es)) |
| 453 return false; | 448 return false; |
| 454 | 449 |
| 455 uint8_t length; | 450 uint8_t length; |
| 456 if (!getUint8(raw, "length", length, context, es)) | 451 if (!getUint8(raw, "length", length, context, es)) |
| 457 return false; | 452 return false; |
| 458 | 453 |
| 459 params = adoptPtr(new blink::WebCryptoAesCtrParams(length, static_cast<const
unsigned char*>(counter->baseAddress()), counter->byteLength())); | 454 params = adoptPtr(new blink::WebCryptoAesCtrParams(length, static_cast<const
unsigned char*>(counter->baseAddress()), counter->byteLength())); |
| 460 return true; | 455 return true; |
| 461 } | 456 } |
| 462 | 457 |
| 463 bool parseAlgorithmParams(const Dictionary& raw, blink::WebCryptoAlgorithmParams
Type type, OwnPtr<blink::WebCryptoAlgorithmParams>& params, ExceptionContext& co
ntext, ExceptionState& exceptionState) | 458 bool parseAlgorithmParams(const Dictionary& raw, blink::WebCryptoAlgorithmParams
Type type, OwnPtr<blink::WebCryptoAlgorithmParams>& params, ErrorContext& contex
t, String& errorDetails) |
| 464 { | 459 { |
| 465 switch (type) { | 460 switch (type) { |
| 466 case blink::WebCryptoAlgorithmParamsTypeNone: | 461 case blink::WebCryptoAlgorithmParamsTypeNone: |
| 467 return true; | 462 return true; |
| 468 case blink::WebCryptoAlgorithmParamsTypeAesCbcParams: | 463 case blink::WebCryptoAlgorithmParamsTypeAesCbcParams: |
| 469 context.add("AesCbcParams"); | 464 context.add("AesCbcParams"); |
| 470 return parseAesCbcParams(raw, params, context, exceptionState); | 465 return parseAesCbcParams(raw, params, context, errorDetails); |
| 471 case blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams: | 466 case blink::WebCryptoAlgorithmParamsTypeAesKeyGenParams: |
| 472 context.add("AesKeyGenParams"); | 467 context.add("AesKeyGenParams"); |
| 473 return parseAesKeyGenParams(raw, params, context, exceptionState); | 468 return parseAesKeyGenParams(raw, params, context, errorDetails); |
| 474 case blink::WebCryptoAlgorithmParamsTypeHmacParams: | 469 case blink::WebCryptoAlgorithmParamsTypeHmacParams: |
| 475 context.add("HmacParams"); | 470 context.add("HmacParams"); |
| 476 return parseHmacParams(raw, params, context, exceptionState); | 471 return parseHmacParams(raw, params, context, errorDetails); |
| 477 case blink::WebCryptoAlgorithmParamsTypeHmacKeyParams: | 472 case blink::WebCryptoAlgorithmParamsTypeHmacKeyParams: |
| 478 context.add("HmacKeyParams"); | 473 context.add("HmacKeyParams"); |
| 479 return parseHmacKeyParams(raw, params, context, exceptionState); | 474 return parseHmacKeyParams(raw, params, context, errorDetails); |
| 480 case blink::WebCryptoAlgorithmParamsTypeRsaSsaParams: | 475 case blink::WebCryptoAlgorithmParamsTypeRsaSsaParams: |
| 481 context.add("RsaSSaParams"); | 476 context.add("RsaSSaParams"); |
| 482 return parseRsaSsaParams(raw, params, context, exceptionState); | 477 return parseRsaSsaParams(raw, params, context, errorDetails); |
| 483 case blink::WebCryptoAlgorithmParamsTypeRsaKeyGenParams: | 478 case blink::WebCryptoAlgorithmParamsTypeRsaKeyGenParams: |
| 484 context.add("RsaKeyGenParams"); | 479 context.add("RsaKeyGenParams"); |
| 485 return parseRsaKeyGenParams(raw, params, context, exceptionState); | 480 return parseRsaKeyGenParams(raw, params, context, errorDetails); |
| 486 case blink::WebCryptoAlgorithmParamsTypeAesCtrParams: | 481 case blink::WebCryptoAlgorithmParamsTypeAesCtrParams: |
| 487 context.add("AesCtrParams"); | 482 context.add("AesCtrParams"); |
| 488 return parseAesCtrParams(raw, params, context, exceptionState); | 483 return parseAesCtrParams(raw, params, context, errorDetails); |
| 489 case blink::WebCryptoAlgorithmParamsTypeAesGcmParams: | 484 case blink::WebCryptoAlgorithmParamsTypeAesGcmParams: |
| 490 case blink::WebCryptoAlgorithmParamsTypeRsaOaepParams: | 485 case blink::WebCryptoAlgorithmParamsTypeRsaOaepParams: |
| 491 // TODO | 486 // TODO |
| 492 notImplemented(); | 487 notImplemented(); |
| 493 break; | 488 break; |
| 494 } | 489 } |
| 495 ASSERT_NOT_REACHED(); | 490 ASSERT_NOT_REACHED(); |
| 496 return false; | 491 return false; |
| 497 } | 492 } |
| 498 | 493 |
| 499 const AlgorithmInfo* algorithmInfo(const Dictionary& raw, const ExceptionContext
& context, ExceptionState& exceptionState) | 494 bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryp
toAlgorithm& algorithm, ErrorContext context, String& errorDetails, ExceptionSta
te& exceptionState) |
| 500 { | 495 { |
| 496 context.add("Algorithm"); |
| 497 |
| 501 if (!raw.isObject()) { | 498 if (!raw.isObject()) { |
| 502 exceptionState.throwTypeError(context.toString("Not an object")); | 499 errorDetails = context.toString("Not an object"); |
| 503 return 0; | 500 exceptionState.throwTypeError(errorDetails); |
| 501 return false; |
| 504 } | 502 } |
| 505 | 503 |
| 506 String algorithmName; | 504 String algorithmName; |
| 507 if (!raw.get("name", algorithmName)) { | 505 if (!raw.get("name", algorithmName)) { |
| 508 exceptionState.throwTypeError(context.toString("name", "Missing or not a
string")); | 506 errorDetails = context.toString("name", "Missing or not a string"); |
| 509 return 0; | 507 exceptionState.throwDOMException(NotSupportedError, errorDetails); |
| 508 return false; |
| 510 } | 509 } |
| 511 | 510 |
| 512 const AlgorithmInfo* info = AlgorithmRegistry::instance().lookupAlgorithmByN
ame(algorithmName); | 511 const AlgorithmInfo* info = AlgorithmRegistry::instance().lookupAlgorithmByN
ame(algorithmName); |
| 513 if (!info) { | 512 if (!info) { |
| 514 exceptionState.throwDOMException(NotSupportedError, context.toString("Un
recognized algorithm name")); | 513 errorDetails = context.toString("Unrecognized algorithm name"); |
| 515 return 0; | 514 exceptionState.throwDOMException(NotSupportedError, errorDetails); |
| 515 return false; |
| 516 } | 516 } |
| 517 | 517 |
| 518 return info; | |
| 519 } | |
| 520 | |
| 521 // This implementation corresponds with: | |
| 522 // http://www.w3.org/TR/WebCryptoAPI/#algorithm-normalizing-rules | |
| 523 bool normalizeAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::Web
CryptoAlgorithm& algorithm, ExceptionContext context, ExceptionState& exceptionS
tate) | |
| 524 { | |
| 525 context.add("Algorithm"); | |
| 526 | |
| 527 const AlgorithmInfo* info = algorithmInfo(raw, context, exceptionState); | |
| 528 if (!info) | |
| 529 return false; | |
| 530 | |
| 531 context.add(info->algorithmName); | 518 context.add(info->algorithmName); |
| 532 | 519 |
| 533 if (info->paramsForOperation[op] == UnsupportedOp) { | 520 if (info->paramsForOperation[op] == UnsupportedOp) { |
| 534 exceptionState.throwDOMException(NotSupportedError, context.toString("Un
supported operation")); | 521 errorDetails = context.toString("Unsupported operation"); |
| 522 exceptionState.throwDOMException(NotSupportedError, errorDetails); |
| 535 return false; | 523 return false; |
| 536 } | 524 } |
| 537 | 525 |
| 538 blink::WebCryptoAlgorithmParamsType paramsType = static_cast<blink::WebCrypt
oAlgorithmParamsType>(info->paramsForOperation[op]); | 526 blink::WebCryptoAlgorithmParamsType paramsType = static_cast<blink::WebCrypt
oAlgorithmParamsType>(info->paramsForOperation[op]); |
| 539 OwnPtr<blink::WebCryptoAlgorithmParams> params; | 527 OwnPtr<blink::WebCryptoAlgorithmParams> params; |
| 540 if (!parseAlgorithmParams(raw, paramsType, params, context, exceptionState)) | 528 if (!parseAlgorithmParams(raw, paramsType, params, context, errorDetails)) |
| 541 return false; | 529 return false; |
| 542 | 530 |
| 543 algorithm = blink::WebCryptoAlgorithm(info->algorithmId, params.release()); | 531 algorithm = blink::WebCryptoAlgorithm(info->algorithmId, params.release()); |
| 544 return true; | 532 return true; |
| 545 } | 533 } |
| 546 | 534 |
| 547 } // namespace | 535 } // namespace |
| 548 | 536 |
| 549 bool normalizeAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::Web
CryptoAlgorithm& algorithm, ExceptionState& exceptionState) | 537 bool parseAlgorithm(const Dictionary& raw, AlgorithmOperation op, blink::WebCryp
toAlgorithm& algorithm, String& errorDetails, ExceptionState& exceptionState) |
| 550 { | 538 { |
| 551 return normalizeAlgorithm(raw, op, algorithm, ExceptionContext(op), exceptio
nState); | 539 return parseAlgorithm(raw, op, algorithm, ErrorContext(), errorDetails, exce
ptionState); |
| 552 } | 540 } |
| 553 | 541 |
| 554 const char* algorithmIdToName(blink::WebCryptoAlgorithmId id) | 542 const char* algorithmIdToName(blink::WebCryptoAlgorithmId id) |
| 555 { | 543 { |
| 556 return AlgorithmRegistry::instance().lookupAlgorithmById(id)->algorithmName; | 544 return AlgorithmRegistry::instance().lookupAlgorithmById(id)->algorithmName; |
| 557 } | 545 } |
| 558 | 546 |
| 559 } // namespace WebCore | 547 } // namespace WebCore |
| OLD | NEW |