Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(414)

Side by Side Diff: Source/bindings/v8/SerializedScriptValue.cpp

Issue 195543002: [webcrypto] Implement structured clone of keys (blink-side). (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Fixes for jsbell Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Google Inc. All rights reserved. 2 * Copyright (C) 2010 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 18 matching lines...) Expand all
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "bindings/v8/SerializedScriptValue.h" 32 #include "bindings/v8/SerializedScriptValue.h"
33 33
34 #include "V8Blob.h" 34 #include "V8Blob.h"
35 #include "V8DOMFileSystem.h" 35 #include "V8DOMFileSystem.h"
36 #include "V8File.h" 36 #include "V8File.h"
37 #include "V8FileList.h" 37 #include "V8FileList.h"
38 #include "V8ImageData.h" 38 #include "V8ImageData.h"
39 #include "V8Key.h"
39 #include "V8MessagePort.h" 40 #include "V8MessagePort.h"
40 #include "bindings/v8/ExceptionState.h" 41 #include "bindings/v8/ExceptionState.h"
41 #include "bindings/v8/ScriptScope.h" 42 #include "bindings/v8/ScriptScope.h"
42 #include "bindings/v8/ScriptState.h" 43 #include "bindings/v8/ScriptState.h"
43 #include "bindings/v8/V8Binding.h" 44 #include "bindings/v8/V8Binding.h"
44 #include "bindings/v8/WorkerScriptController.h" 45 #include "bindings/v8/WorkerScriptController.h"
45 #include "bindings/v8/custom/V8ArrayBufferCustom.h" 46 #include "bindings/v8/custom/V8ArrayBufferCustom.h"
46 #include "bindings/v8/custom/V8ArrayBufferViewCustom.h" 47 #include "bindings/v8/custom/V8ArrayBufferViewCustom.h"
47 #include "bindings/v8/custom/V8DataViewCustom.h" 48 #include "bindings/v8/custom/V8DataViewCustom.h"
48 #include "bindings/v8/custom/V8Float32ArrayCustom.h" 49 #include "bindings/v8/custom/V8Float32ArrayCustom.h"
49 #include "bindings/v8/custom/V8Float64ArrayCustom.h" 50 #include "bindings/v8/custom/V8Float64ArrayCustom.h"
50 #include "bindings/v8/custom/V8Int16ArrayCustom.h" 51 #include "bindings/v8/custom/V8Int16ArrayCustom.h"
51 #include "bindings/v8/custom/V8Int32ArrayCustom.h" 52 #include "bindings/v8/custom/V8Int32ArrayCustom.h"
52 #include "bindings/v8/custom/V8Int8ArrayCustom.h" 53 #include "bindings/v8/custom/V8Int8ArrayCustom.h"
53 #include "bindings/v8/custom/V8Uint16ArrayCustom.h" 54 #include "bindings/v8/custom/V8Uint16ArrayCustom.h"
54 #include "bindings/v8/custom/V8Uint32ArrayCustom.h" 55 #include "bindings/v8/custom/V8Uint32ArrayCustom.h"
55 #include "bindings/v8/custom/V8Uint8ArrayCustom.h" 56 #include "bindings/v8/custom/V8Uint8ArrayCustom.h"
56 #include "bindings/v8/custom/V8Uint8ClampedArrayCustom.h" 57 #include "bindings/v8/custom/V8Uint8ClampedArrayCustom.h"
57 #include "core/dom/ExceptionCode.h" 58 #include "core/dom/ExceptionCode.h"
58 #include "core/dom/MessagePort.h" 59 #include "core/dom/MessagePort.h"
59 #include "core/fileapi/Blob.h" 60 #include "core/fileapi/Blob.h"
60 #include "core/fileapi/File.h" 61 #include "core/fileapi/File.h"
61 #include "core/fileapi/FileList.h" 62 #include "core/fileapi/FileList.h"
62 #include "core/html/ImageData.h" 63 #include "core/html/ImageData.h"
63 #include "core/html/canvas/DataView.h" 64 #include "core/html/canvas/DataView.h"
64 #include "heap/Handle.h" 65 #include "heap/Handle.h"
65 #include "platform/SharedBuffer.h" 66 #include "platform/SharedBuffer.h"
67 #include "public/platform/Platform.h"
68 #include "public/platform/WebCrypto.h"
69 #include "public/platform/WebCryptoKey.h"
70 #include "public/platform/WebCryptoKeyAlgorithm.h"
66 #include "wtf/ArrayBuffer.h" 71 #include "wtf/ArrayBuffer.h"
67 #include "wtf/ArrayBufferContents.h" 72 #include "wtf/ArrayBufferContents.h"
68 #include "wtf/ArrayBufferView.h" 73 #include "wtf/ArrayBufferView.h"
69 #include "wtf/Assertions.h" 74 #include "wtf/Assertions.h"
70 #include "wtf/ByteOrder.h" 75 #include "wtf/ByteOrder.h"
71 #include "wtf/Float32Array.h" 76 #include "wtf/Float32Array.h"
72 #include "wtf/Float64Array.h" 77 #include "wtf/Float64Array.h"
73 #include "wtf/Int16Array.h" 78 #include "wtf/Int16Array.h"
74 #include "wtf/Int32Array.h" 79 #include "wtf/Int32Array.h"
75 #include "wtf/Int8Array.h" 80 #include "wtf/Int8Array.h"
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
202 ObjectTag = '{', // numProperties:uint32_t -> pops the last object from the open stack; 207 ObjectTag = '{', // numProperties:uint32_t -> pops the last object from the open stack;
203 // fills it with the last numProp erties name,value pairs pushed onto the deserialization stack 208 // fills it with the last numProp erties name,value pairs pushed onto the deserialization stack
204 SparseArrayTag = '@', // numProperties:uint32_t, length:uint32_t -> pops the last object from the open stack; 209 SparseArrayTag = '@', // numProperties:uint32_t, length:uint32_t -> pops the last object from the open stack;
205 // fills it with the last numProperties name,value pairs pushed onto the deserialization st ack 210 // fills it with the last numProperties name,value pairs pushed onto the deserialization st ack
206 DenseArrayTag = '$', // numProperties:uint32_t, length:uint32_t -> pops the last object from the open stack; 211 DenseArrayTag = '$', // numProperties:uint32_t, length:uint32_t -> pops the last object from the open stack;
207 // fills it with the last length elements and numProperties name,value pairs pushed onto des erialization stack 212 // fills it with the last length elements and numProperties name,value pairs pushed onto des erialization stack
208 RegExpTag = 'R', // pattern:RawString, flags:uint32_t -> RegExp (ref) 213 RegExpTag = 'R', // pattern:RawString, flags:uint32_t -> RegExp (ref)
209 ArrayBufferTag = 'B', // byteLength:uint32_t, data:byte[byteLength] -> Array Buffer (ref) 214 ArrayBufferTag = 'B', // byteLength:uint32_t, data:byte[byteLength] -> Array Buffer (ref)
210 ArrayBufferTransferTag = 't', // index:uint32_t -> ArrayBuffer. For ArrayBuf fer transfer 215 ArrayBufferTransferTag = 't', // index:uint32_t -> ArrayBuffer. For ArrayBuf fer transfer
211 ArrayBufferViewTag = 'V', // subtag:byte, byteOffset:uint32_t, byteLength:ui nt32_t -> ArrayBufferView (ref). Consumes an ArrayBuffer from the top of the des erialization stack. 216 ArrayBufferViewTag = 'V', // subtag:byte, byteOffset:uint32_t, byteLength:ui nt32_t -> ArrayBufferView (ref). Consumes an ArrayBuffer from the top of the des erialization stack.
217 CryptoKeyTag = 'K', // subtag:byte, props, usages:uint32_t, keyDataLength:ui nt32_t, keyData:byte[keyDataLength]
218 // If subtag=AesKeyTag:
219 // props = keyLengthBytes:uint32_t, algorithmId:ui nt32_t
220 // If subtag=HmacKeyTag:
221 // props = hashId:uint32_t
222 // If subtag=RsaKeyTag:
223 // props = algorithmId:uint32_t, type:uint32_t, mo dulusLengthBits:uint32_t, publicExponentLength:uint32_t, publicExponent:byte[pub licExponentLength]
224 // If subtag=RsaHashedKeyTag:
225 // props = <same as for RsaKeyTag>, hashId:uint32_ t
212 ObjectReferenceTag = '^', // ref:uint32_t -> reference table[ref] 226 ObjectReferenceTag = '^', // ref:uint32_t -> reference table[ref]
213 GenerateFreshObjectTag = 'o', // -> empty object allocated an object ID and pushed onto the open stack (ref) 227 GenerateFreshObjectTag = 'o', // -> empty object allocated an object ID and pushed onto the open stack (ref)
214 GenerateFreshSparseArrayTag = 'a', // length:uint32_t -> empty array[length] allocated an object ID and pushed onto the open stack (ref) 228 GenerateFreshSparseArrayTag = 'a', // length:uint32_t -> empty array[length] allocated an object ID and pushed onto the open stack (ref)
215 GenerateFreshDenseArrayTag = 'A', // length:uint32_t -> empty array[length] allocated an object ID and pushed onto the open stack (ref) 229 GenerateFreshDenseArrayTag = 'A', // length:uint32_t -> empty array[length] allocated an object ID and pushed onto the open stack (ref)
216 ReferenceCountTag = '?', // refTableSize:uint32_t -> If the reference table is not refTableSize big, fails. 230 ReferenceCountTag = '?', // refTableSize:uint32_t -> If the reference table is not refTableSize big, fails.
217 StringObjectTag = 's', // string:RawString -> new String(string) (ref) 231 StringObjectTag = 's', // string:RawString -> new String(string) (ref)
218 NumberObjectTag = 'n', // value:double -> new Number(value) (ref) 232 NumberObjectTag = 'n', // value:double -> new Number(value) (ref)
219 TrueObjectTag = 'y', // new Boolean(true) (ref) 233 TrueObjectTag = 'y', // new Boolean(true) (ref)
220 FalseObjectTag = 'x', // new Boolean(false) (ref) 234 FalseObjectTag = 'x', // new Boolean(false) (ref)
221 VersionTag = 0xFF // version:uint32_t -> Uses this as the file version. 235 VersionTag = 0xFF // version:uint32_t -> Uses this as the file version.
222 }; 236 };
223 237
224 enum ArrayBufferViewSubTag { 238 enum ArrayBufferViewSubTag {
225 ByteArrayTag = 'b', 239 ByteArrayTag = 'b',
226 UnsignedByteArrayTag = 'B', 240 UnsignedByteArrayTag = 'B',
227 UnsignedByteClampedArrayTag = 'C', 241 UnsignedByteClampedArrayTag = 'C',
228 ShortArrayTag = 'w', 242 ShortArrayTag = 'w',
229 UnsignedShortArrayTag = 'W', 243 UnsignedShortArrayTag = 'W',
230 IntArrayTag = 'd', 244 IntArrayTag = 'd',
231 UnsignedIntArrayTag = 'D', 245 UnsignedIntArrayTag = 'D',
232 FloatArrayTag = 'f', 246 FloatArrayTag = 'f',
233 DoubleArrayTag = 'F', 247 DoubleArrayTag = 'F',
234 DataViewTag = '?' 248 DataViewTag = '?'
235 }; 249 };
236 250
251 enum CryptoKeySubTag {
252 AesKeyTag = 1,
253 HmacKeyTag = 2,
254 RsaKeyTag = 3,
255 RsaHashedKeyTag = 4,
256 // Maximum allowed value is 255
257 };
258
259 enum AssymetricCryptoKeyType {
260 PublicKeyType = 1,
261 PrivateKeyType = 2,
262 // Maximum allowed value is 2^32-1
263 };
264
265 enum CryptoKeyAlgorithmTag {
266 AesCbcTag = 1,
267 HmacTag = 2,
268 RsaSsaPkcs1v1_5Tag = 3,
269 RsaEsPkcs1v1_5Tag = 4,
270 Sha1Tag = 5,
271 Sha224Tag = 6,
272 Sha256Tag = 7,
273 Sha384Tag = 8,
274 Sha512Tag = 9,
275 AesGcmTag = 10,
276 RsaOaepTag = 11,
277 AesCtrTag = 12,
278 AesKwTag = 13,
279 // Maximum allowed value is 2^32-1
280 };
281
282 enum CryptoKeyUsage {
283 // Extractability is not a "usage" in the WebCryptoKeyUsages sense, however
284 // it fits conveniently into this bitfield.
285 ExtractableUsage = 1 << 0,
286
287 EncryptUsage = 1 << 1,
288 DecryptUsage = 1 << 2,
289 SignUsage = 1 << 3,
290 VerifyUsage = 1 << 4,
291 DeriveKeyUsage = 1 << 5,
292 WrapKeyUsage = 1 << 6,
293 UnwrapKeyUsage = 1 << 7,
294 // Maximum allowed value is 1 << 31
295 };
296
237 static bool shouldCheckForCycles(int depth) 297 static bool shouldCheckForCycles(int depth)
238 { 298 {
239 ASSERT(depth >= 0); 299 ASSERT(depth >= 0);
240 // Since we are not required to spot the cycle as soon as it 300 // Since we are not required to spot the cycle as soon as it
241 // happens we can check for cycles only when the current depth 301 // happens we can check for cycles only when the current depth
242 // is a power of two. 302 // is a power of two.
243 return !(depth & (depth - 1)); 303 return !(depth & (depth - 1));
244 } 304 }
245 305
246 static const int maxDepth = 20000; 306 static const int maxDepth = 20000;
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
415 475
416 void writeFileList(const FileList& fileList) 476 void writeFileList(const FileList& fileList)
417 { 477 {
418 append(FileListTag); 478 append(FileListTag);
419 uint32_t length = fileList.length(); 479 uint32_t length = fileList.length();
420 doWriteUint32(length); 480 doWriteUint32(length);
421 for (unsigned i = 0; i < length; ++i) 481 for (unsigned i = 0; i < length; ++i)
422 doWriteFile(*fileList.item(i)); 482 doWriteFile(*fileList.item(i));
423 } 483 }
424 484
485 bool writeCryptoKey(const blink::WebCryptoKey& key)
486 {
487 append(static_cast<uint8_t>(CryptoKeyTag));
488
489 switch (key.algorithm().paramsType()) {
490 case blink::WebCryptoKeyAlgorithmParamsTypeAes:
491 doWriteAesKey(key);
492 break;
493 case blink::WebCryptoKeyAlgorithmParamsTypeHmac:
494 doWriteHmacKey(key);
495 break;
496 case blink::WebCryptoKeyAlgorithmParamsTypeRsa:
497 case blink::WebCryptoKeyAlgorithmParamsTypeRsaHashed:
498 doWriteRsaKey(key);
499 break;
500 case blink::WebCryptoKeyAlgorithmParamsTypeNone:
501 ASSERT_NOT_REACHED();
502 return false;
503 }
504
505 doWriteKeyUsages(key.usages(), key.extractable());
506
507 blink::WebVector<uint8_t> keyData;
508 if (!blink::Platform::current()->crypto()->serializeKeyForClone(key, key Data))
509 return false;
510
511 doWriteUint32(keyData.size());
512 append(keyData.data(), keyData.size());
513 return true;
514 }
515
425 void writeArrayBuffer(const ArrayBuffer& arrayBuffer) 516 void writeArrayBuffer(const ArrayBuffer& arrayBuffer)
426 { 517 {
427 append(ArrayBufferTag); 518 append(ArrayBufferTag);
428 doWriteArrayBuffer(arrayBuffer); 519 doWriteArrayBuffer(arrayBuffer);
429 } 520 }
430 521
431 void writeArrayBufferView(const ArrayBufferView& arrayBufferView) 522 void writeArrayBufferView(const ArrayBufferView& arrayBufferView)
432 { 523 {
433 append(ArrayBufferViewTag); 524 append(ArrayBufferViewTag);
434 #ifndef NDEBUG 525 #ifndef NDEBUG
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after
586 doWriteUint32(static_cast<uint32_t>(length)); 677 doWriteUint32(static_cast<uint32_t>(length));
587 append(reinterpret_cast<const uint8_t*>(data), length); 678 append(reinterpret_cast<const uint8_t*>(data), length);
588 } 679 }
589 680
590 void doWriteWebCoreString(const String& string) 681 void doWriteWebCoreString(const String& string)
591 { 682 {
592 StringUTF8Adaptor stringUTF8(string); 683 StringUTF8Adaptor stringUTF8(string);
593 doWriteString(stringUTF8.data(), stringUTF8.length()); 684 doWriteString(stringUTF8.data(), stringUTF8.length());
594 } 685 }
595 686
687 void doWriteHmacKey(const blink::WebCryptoKey& key)
688 {
689 ASSERT(key.algorithm().paramsType() == blink::WebCryptoKeyAlgorithmParam sTypeHmac);
690
691 append(static_cast<uint8_t>(HmacKeyTag));
692 doWriteAlgorithmId(key.algorithm().hmacParams()->hash().id());
693 }
694
695 void doWriteAesKey(const blink::WebCryptoKey& key)
696 {
697 ASSERT(key.algorithm().paramsType() == blink::WebCryptoKeyAlgorithmParam sTypeAes);
698
699 append(static_cast<uint8_t>(AesKeyTag));
700 doWriteAlgorithmId(key.algorithm().id());
701 // Converting the key length from bits to bytes is lossless and makes
702 // it fit in 1 byte.
703 ASSERT(!(key.algorithm().aesParams()->lengthBits() % 8));
704 doWriteUint32(static_cast<uint8_t>(key.algorithm().aesParams()->lengthBi ts() / 8));
705 }
706
707 void doWriteRsaKey(const blink::WebCryptoKey& key)
708 {
709 if (key.algorithm().rsaHashedParams())
710 append(static_cast<uint8_t>(RsaHashedKeyTag));
711 else
712 append(static_cast<uint8_t>(RsaKeyTag));
713
714 doWriteAlgorithmId(key.algorithm().id());
715
716 switch (key.type()) {
717 case blink::WebCryptoKeyTypePublic:
718 doWriteUint32(PublicKeyType);
719 break;
720 case blink::WebCryptoKeyTypePrivate:
721 doWriteUint32(PrivateKeyType);
722 break;
723 case blink::WebCryptoKeyTypeSecret:
724 ASSERT_NOT_REACHED();
725 }
726
727 const blink::WebCryptoRsaKeyAlgorithmParams* params = key.algorithm().rs aParams();
728 doWriteUint32(params->modulusLengthBits());
729 doWriteUint32(params->publicExponent().size());
730 append(params->publicExponent().data(), params->publicExponent().size()) ;
731
732 if (key.algorithm().rsaHashedParams())
733 doWriteAlgorithmId(key.algorithm().rsaHashedParams()->hash().id());
734 }
735
736 void doWriteAlgorithmId(blink::WebCryptoAlgorithmId id)
737 {
738 switch (id) {
739 case blink::WebCryptoAlgorithmIdAesCbc:
740 return doWriteUint32(AesCbcTag);
741 case blink::WebCryptoAlgorithmIdHmac:
742 return doWriteUint32(HmacTag);
743 case blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5:
744 return doWriteUint32(RsaSsaPkcs1v1_5Tag);
745 case blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5:
746 return doWriteUint32(RsaEsPkcs1v1_5Tag);
747 case blink::WebCryptoAlgorithmIdSha1:
748 return doWriteUint32(Sha1Tag);
749 case blink::WebCryptoAlgorithmIdSha224:
750 return doWriteUint32(Sha224Tag);
751 case blink::WebCryptoAlgorithmIdSha256:
752 return doWriteUint32(Sha256Tag);
753 case blink::WebCryptoAlgorithmIdSha384:
754 return doWriteUint32(Sha384Tag);
755 case blink::WebCryptoAlgorithmIdSha512:
756 return doWriteUint32(Sha512Tag);
757 case blink::WebCryptoAlgorithmIdAesGcm:
758 return doWriteUint32(AesGcmTag);
759 case blink::WebCryptoAlgorithmIdRsaOaep:
760 return doWriteUint32(RsaOaepTag);
761 case blink::WebCryptoAlgorithmIdAesCtr:
762 return doWriteUint32(AesCtrTag);
763 case blink::WebCryptoAlgorithmIdAesKw:
764 return doWriteUint32(AesKwTag);
765 }
766 ASSERT_NOT_REACHED();
767 }
768
769 void doWriteKeyUsages(const blink::WebCryptoKeyUsageMask usages, bool extrac table)
770 {
771 // Reminder to update this when adding new key usages.
772 COMPILE_ASSERT(blink::EndOfWebCryptoKeyUsage == (1 << 7) + 1, UpdateMe);
773
774 uint32_t value = 0;
775
776 if (extractable)
777 value |= ExtractableUsage;
778
779 if (usages & blink::WebCryptoKeyUsageEncrypt)
780 value |= EncryptUsage;
781 if (usages & blink::WebCryptoKeyUsageDecrypt)
782 value |= DecryptUsage;
783 if (usages & blink::WebCryptoKeyUsageSign)
784 value |= SignUsage;
785 if (usages & blink::WebCryptoKeyUsageVerify)
786 value |= VerifyUsage;
787 if (usages & blink::WebCryptoKeyUsageDeriveKey)
788 value |= DeriveKeyUsage;
789 if (usages & blink::WebCryptoKeyUsageWrapKey)
790 value |= WrapKeyUsage;
791 if (usages & blink::WebCryptoKeyUsageUnwrapKey)
792 value |= UnwrapKeyUsage;
793
794 doWriteUint32(value);
795 }
796
596 int bytesNeededToWireEncode(uint32_t value) 797 int bytesNeededToWireEncode(uint32_t value)
597 { 798 {
598 int bytes = 1; 799 int bytes = 1;
599 while (true) { 800 while (true) {
600 value >>= varIntShift; 801 value >>= varIntShift;
601 if (!value) 802 if (!value)
602 break; 803 break;
603 ++bytes; 804 ++bytes;
604 } 805 }
605 806
(...skipping 485 matching lines...) Expand 10 before | Expand all | Expand 10 after
1091 { 1292 {
1092 FileList* fileList = V8FileList::toNative(value.As<v8::Object>()); 1293 FileList* fileList = V8FileList::toNative(value.As<v8::Object>());
1093 if (!fileList) 1294 if (!fileList)
1094 return; 1295 return;
1095 m_writer.writeFileList(*fileList); 1296 m_writer.writeFileList(*fileList);
1096 unsigned length = fileList->length(); 1297 unsigned length = fileList->length();
1097 for (unsigned i = 0; i < length; ++i) 1298 for (unsigned i = 0; i < length; ++i)
1098 m_blobDataHandles.add(fileList->item(i)->uuid(), fileList->item(i)-> blobDataHandle()); 1299 m_blobDataHandles.add(fileList->item(i)->uuid(), fileList->item(i)-> blobDataHandle());
1099 } 1300 }
1100 1301
1302 bool writeCryptoKey(v8::Handle<v8::Value> value)
1303 {
1304 Key* key = V8Key::toNative(value.As<v8::Object>());
1305 if (!key)
1306 return 0;
1307 return m_writer.writeCryptoKey(key->key());
1308 }
1309
1101 void writeImageData(v8::Handle<v8::Value> value) 1310 void writeImageData(v8::Handle<v8::Value> value)
1102 { 1311 {
1103 ImageData* imageData = V8ImageData::toNative(value.As<v8::Object>()); 1312 ImageData* imageData = V8ImageData::toNative(value.As<v8::Object>());
1104 if (!imageData) 1313 if (!imageData)
1105 return; 1314 return;
1106 Uint8ClampedArray* pixelArray = imageData->data(); 1315 Uint8ClampedArray* pixelArray = imageData->data();
1107 m_writer.writeImageData(imageData->width(), imageData->height(), pixelAr ray->data(), pixelArray->length()); 1316 m_writer.writeImageData(imageData->width(), imageData->height(), pixelAr ray->data(), pixelArray->length());
1108 } 1317 }
1109 1318
1110 void writeRegExp(v8::Handle<v8::Value> value) 1319 void writeRegExp(v8::Handle<v8::Value> value)
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after
1286 else if (value->IsArray()) { 1495 else if (value->IsArray()) {
1287 return startArrayState(value.As<v8::Array>(), next); 1496 return startArrayState(value.As<v8::Array>(), next);
1288 } else if (V8File::hasInstance(value, m_isolate)) 1497 } else if (V8File::hasInstance(value, m_isolate))
1289 return writeFile(value, next); 1498 return writeFile(value, next);
1290 else if (V8Blob::hasInstance(value, m_isolate)) 1499 else if (V8Blob::hasInstance(value, m_isolate))
1291 return writeBlob(value, next); 1500 return writeBlob(value, next);
1292 else if (V8DOMFileSystem::hasInstance(value, m_isolate)) 1501 else if (V8DOMFileSystem::hasInstance(value, m_isolate))
1293 return writeDOMFileSystem(value, next); 1502 return writeDOMFileSystem(value, next);
1294 else if (V8FileList::hasInstance(value, m_isolate)) 1503 else if (V8FileList::hasInstance(value, m_isolate))
1295 writeFileList(value); 1504 writeFileList(value);
1296 else if (V8ImageData::hasInstance(value, m_isolate)) 1505 else if (V8Key::hasInstance(value, m_isolate)) {
1506 if (!writeCryptoKey(value))
1507 return handleError(DataCloneError, "Couldn't serialize key data" , next);
1508 } else if (V8ImageData::hasInstance(value, m_isolate))
1297 writeImageData(value); 1509 writeImageData(value);
1298 else if (value->IsRegExp()) 1510 else if (value->IsRegExp())
1299 writeRegExp(value); 1511 writeRegExp(value);
1300 else if (V8ArrayBuffer::hasInstance(value, m_isolate)) 1512 else if (V8ArrayBuffer::hasInstance(value, m_isolate))
1301 return writeArrayBuffer(value, next); 1513 return writeArrayBuffer(value, next);
1302 else if (value->IsObject()) { 1514 else if (value->IsObject()) {
1303 if (isHostObject(jsObject) || jsObject->IsCallable() || value->IsNat iveError()) 1515 if (isHostObject(jsObject) || jsObject->IsCallable() || value->IsNat iveError())
1304 return handleError(DataCloneError, "An object could not be clone d.", next); 1516 return handleError(DataCloneError, "An object could not be clone d.", next);
1305 return startObjectState(jsObject, next); 1517 return startObjectState(jsObject, next);
1306 } else 1518 } else
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
1439 case DOMFileSystemTag: 1651 case DOMFileSystemTag:
1440 if (!readDOMFileSystem(value)) 1652 if (!readDOMFileSystem(value))
1441 return false; 1653 return false;
1442 creator.pushObjectReference(*value); 1654 creator.pushObjectReference(*value);
1443 break; 1655 break;
1444 case FileListTag: 1656 case FileListTag:
1445 if (!readFileList(value)) 1657 if (!readFileList(value))
1446 return false; 1658 return false;
1447 creator.pushObjectReference(*value); 1659 creator.pushObjectReference(*value);
1448 break; 1660 break;
1661 case CryptoKeyTag:
1662 if (!readCryptoKey(value))
1663 return false;
1664 creator.pushObjectReference(*value);
1665 break;
1449 case ImageDataTag: 1666 case ImageDataTag:
1450 if (!readImageData(value)) 1667 if (!readImageData(value))
1451 return false; 1668 return false;
1452 creator.pushObjectReference(*value); 1669 creator.pushObjectReference(*value);
1453 break; 1670 break;
1454 1671
1455 case RegExpTag: 1672 case RegExpTag:
1456 if (!readRegExp(value)) 1673 if (!readRegExp(value))
1457 return false; 1674 return false;
1458 creator.pushObjectReference(*value); 1675 creator.pushObjectReference(*value);
(...skipping 443 matching lines...) Expand 10 before | Expand all | Expand 10 after
1902 for (unsigned i = 0; i < length; ++i) { 2119 for (unsigned i = 0; i < length; ++i) {
1903 RefPtrWillBeRawPtr<File> file = doReadFileHelper(); 2120 RefPtrWillBeRawPtr<File> file = doReadFileHelper();
1904 if (!file) 2121 if (!file)
1905 return false; 2122 return false;
1906 fileList->append(file.release()); 2123 fileList->append(file.release());
1907 } 2124 }
1908 *value = toV8(fileList.release(), v8::Handle<v8::Object>(), m_isolate); 2125 *value = toV8(fileList.release(), v8::Handle<v8::Object>(), m_isolate);
1909 return true; 2126 return true;
1910 } 2127 }
1911 2128
2129 bool readCryptoKey(v8::Handle<v8::Value>* value)
2130 {
2131 uint32_t rawKeyType;
2132 if (!doReadUint32(&rawKeyType))
2133 return false;
2134
2135 blink::WebCryptoKeyAlgorithm algorithm;
2136 blink::WebCryptoKeyType type;
2137
2138 switch (static_cast<CryptoKeySubTag>(rawKeyType)) {
2139 case AesKeyTag:
2140 if (!doReadAesKey(algorithm, type))
2141 return false;
2142 break;
2143 case HmacKeyTag:
2144 if (!doReadHmacKey(algorithm, type))
2145 return false;
2146 break;
2147 case RsaKeyTag:
2148 if (!doReadRsaKey(false, algorithm, type))
2149 return false;
2150 break;
2151 case RsaHashedKeyTag:
2152 if (!doReadRsaKey(true, algorithm, type))
2153 return false;
2154 break;
2155 default:
2156 return false;
2157 }
2158
2159 blink::WebCryptoKeyUsageMask usages;
2160 bool extractable;
2161 if (!doReadKeyUsages(usages, extractable))
2162 return false;
2163
2164 uint32_t keyDataLength;
2165 if (!doReadUint32(&keyDataLength))
2166 return false;
2167
2168 if (m_position + keyDataLength > m_length)
2169 return false;
2170
2171 const uint8_t* keyData = m_buffer + m_position;
2172 m_position += keyDataLength;
2173
2174 blink::WebCryptoKey key = blink::WebCryptoKey::createNull();
2175 if (!blink::Platform::current()->crypto()->deserializeKeyForClone(
2176 algorithm, type, extractable, usages, keyData, keyDataLength, key)) {
2177 return false;
2178 }
2179
2180 RefPtrWillBeRawPtr<Key> k = Key::create(key);
2181 *value = toV8(k.release(), v8::Handle<v8::Object>(), m_isolate);
2182 return true;
2183 }
2184
1912 PassRefPtrWillBeRawPtr<File> doReadFileHelper() 2185 PassRefPtrWillBeRawPtr<File> doReadFileHelper()
1913 { 2186 {
1914 if (m_version < 3) 2187 if (m_version < 3)
1915 return nullptr; 2188 return nullptr;
1916 String path; 2189 String path;
1917 String name; 2190 String name;
1918 String relativePath; 2191 String relativePath;
1919 String uuid; 2192 String uuid;
1920 String type; 2193 String type;
1921 uint32_t hasSnapshot = 0; 2194 uint32_t hasSnapshot = 0;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
1991 // the blob in the src process happens to still exist at the time the de st process is deserializing. 2264 // the blob in the src process happens to still exist at the time the de st process is deserializing.
1992 // For example in sharedWorker.postMesssage(...). 2265 // For example in sharedWorker.postMesssage(...).
1993 BlobDataHandleMap::const_iterator it = m_blobDataHandles.find(uuid); 2266 BlobDataHandleMap::const_iterator it = m_blobDataHandles.find(uuid);
1994 if (it != m_blobDataHandles.end()) { 2267 if (it != m_blobDataHandles.end()) {
1995 // make assertions about type and size? 2268 // make assertions about type and size?
1996 return it->value; 2269 return it->value;
1997 } 2270 }
1998 return BlobDataHandle::create(uuid, type, size); 2271 return BlobDataHandle::create(uuid, type, size);
1999 } 2272 }
2000 2273
2274 bool doReadHmacKey(blink::WebCryptoKeyAlgorithm& algorithm, blink::WebCrypto KeyType& type)
2275 {
2276 blink::WebCryptoAlgorithmId hash;
2277 if (!doReadAlgorithmId(hash))
2278 return false;
2279 algorithm = blink::WebCryptoKeyAlgorithm::createHmac(hash);
2280 type = blink::WebCryptoKeyTypeSecret;
2281 return !algorithm.isNull();
2282 }
2283
2284 bool doReadAesKey(blink::WebCryptoKeyAlgorithm& algorithm, blink::WebCryptoK eyType& type)
2285 {
2286 blink::WebCryptoAlgorithmId id;
2287 if (!doReadAlgorithmId(id))
2288 return false;
2289 uint32_t lengthBytes;
2290 if (!doReadUint32(&lengthBytes))
2291 return false;
2292 algorithm = blink::WebCryptoKeyAlgorithm::createAes(id, lengthBytes * 8) ;
2293 type = blink::WebCryptoKeyTypeSecret;
2294 return !algorithm.isNull();
2295 }
2296
2297 bool doReadRsaKey(bool hasHash, blink::WebCryptoKeyAlgorithm& algorithm, bli nk::WebCryptoKeyType& type)
2298 {
2299 blink::WebCryptoAlgorithmId id;
2300 if (!doReadAlgorithmId(id))
2301 return false;
2302
2303 uint32_t rawType;
2304 if (!doReadUint32(&rawType))
2305 return false;
2306
2307 switch (static_cast<AssymetricCryptoKeyType>(rawType)) {
2308 case PublicKeyType:
2309 type = blink::WebCryptoKeyTypePublic;
2310 break;
2311 case PrivateKeyType:
2312 type = blink::WebCryptoKeyTypePrivate;
2313 break;
2314 default:
2315 return false;
2316 }
2317
2318 uint32_t modulusLengthBits;
2319 if (!doReadUint32(&modulusLengthBits))
2320 return false;
2321
2322 uint32_t publicExponentSize;
2323 if (!doReadUint32(&publicExponentSize))
2324 return false;
2325
2326 if (m_position + publicExponentSize > m_length)
2327 return false;
2328
2329 const uint8_t* publicExponent = m_buffer + m_position;
2330 m_position += publicExponentSize;
2331
2332 if (hasHash) {
2333 blink::WebCryptoAlgorithmId hash;
2334 if (!doReadAlgorithmId(hash))
2335 return false;
2336 algorithm = blink::WebCryptoKeyAlgorithm::createRsaHashed(id, modulu sLengthBits, publicExponent, publicExponentSize, hash);
2337 } else {
2338 algorithm = blink::WebCryptoKeyAlgorithm::createRsa(id, modulusLengt hBits, publicExponent, publicExponentSize);
2339 }
2340
2341 return !algorithm.isNull();
2342 }
2343
2344 bool doReadAlgorithmId(blink::WebCryptoAlgorithmId& id)
2345 {
2346 uint32_t rawId;
2347 if (!doReadUint32(&rawId))
2348 return false;
2349
2350 switch (static_cast<CryptoKeyAlgorithmTag>(rawId)) {
2351 case AesCbcTag:
2352 id = blink::WebCryptoAlgorithmIdAesCbc;
2353 return true;
2354 case HmacTag:
2355 id = blink::WebCryptoAlgorithmIdHmac;
2356 return true;
2357 case RsaSsaPkcs1v1_5Tag:
2358 id = blink::WebCryptoAlgorithmIdRsaSsaPkcs1v1_5;
2359 return true;
2360 case RsaEsPkcs1v1_5Tag:
2361 id = blink::WebCryptoAlgorithmIdRsaEsPkcs1v1_5;
2362 return true;
2363 case Sha1Tag:
2364 id = blink::WebCryptoAlgorithmIdSha1;
2365 return true;
2366 case Sha224Tag:
2367 id = blink::WebCryptoAlgorithmIdSha224;
2368 return true;
2369 case Sha256Tag:
2370 id = blink::WebCryptoAlgorithmIdSha256;
2371 return true;
2372 case Sha384Tag:
2373 id = blink::WebCryptoAlgorithmIdSha384;
2374 return true;
2375 case Sha512Tag:
2376 id = blink::WebCryptoAlgorithmIdSha512;
2377 return true;
2378 case AesGcmTag:
2379 id = blink::WebCryptoAlgorithmIdAesGcm;
2380 return true;
2381 case RsaOaepTag:
2382 id = blink::WebCryptoAlgorithmIdRsaOaep;
2383 return true;
2384 case AesCtrTag:
2385 id = blink::WebCryptoAlgorithmIdAesCtr;
2386 return true;
2387 case AesKwTag:
2388 id = blink::WebCryptoAlgorithmIdAesKw;
2389 return true;
2390 }
2391
2392 return false;
2393 }
2394
2395 bool doReadKeyUsages(blink::WebCryptoKeyUsageMask& usages, bool& extractable )
2396 {
2397 // Reminder to update this when adding new key usages.
2398 COMPILE_ASSERT(blink::EndOfWebCryptoKeyUsage == (1 << 7) + 1, UpdateMe);
2399 const uint32_t allPossibleUsages = ExtractableUsage | EncryptUsage | Dec ryptUsage | SignUsage | VerifyUsage | DeriveKeyUsage | WrapKeyUsage | UnwrapKeyU sage;
2400
2401 uint32_t rawUsages;
2402 if (!doReadUint32(&rawUsages))
2403 return false;
2404
2405 // Make sure it doesn't contain an unrecognized usage value.
2406 if (rawUsages & ~allPossibleUsages)
2407 return false;
2408
2409 usages = 0;
2410
2411 extractable = rawUsages & ExtractableUsage;
2412
2413 if (rawUsages & EncryptUsage)
2414 usages |= blink::WebCryptoKeyUsageEncrypt;
2415 if (rawUsages & DecryptUsage)
2416 usages |= blink::WebCryptoKeyUsageDecrypt;
2417 if (rawUsages & SignUsage)
2418 usages |= blink::WebCryptoKeyUsageSign;
2419 if (rawUsages & VerifyUsage)
2420 usages |= blink::WebCryptoKeyUsageVerify;
2421 if (rawUsages & DeriveKeyUsage)
2422 usages |= blink::WebCryptoKeyUsageDeriveKey;
2423 if (rawUsages & WrapKeyUsage)
2424 usages |= blink::WebCryptoKeyUsageWrapKey;
2425 if (rawUsages & UnwrapKeyUsage)
2426 usages |= blink::WebCryptoKeyUsageUnwrapKey;
2427
2428 return true;
2429 }
2430
2001 const uint8_t* m_buffer; 2431 const uint8_t* m_buffer;
2002 const unsigned m_length; 2432 const unsigned m_length;
2003 unsigned m_position; 2433 unsigned m_position;
2004 uint32_t m_version; 2434 uint32_t m_version;
2005 v8::Isolate* m_isolate; 2435 v8::Isolate* m_isolate;
2006 const BlobDataHandleMap& m_blobDataHandles; 2436 const BlobDataHandleMap& m_blobDataHandles;
2007 }; 2437 };
2008 2438
2009 2439
2010 typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray; 2440 typedef Vector<WTF::ArrayBufferContents, 1> ArrayBufferContentsArray;
(...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after
2510 // If the allocated memory was not registered before, then this class is lik ely 2940 // If the allocated memory was not registered before, then this class is lik ely
2511 // used in a context other then Worker's onmessage environment and the prese nce of 2941 // used in a context other then Worker's onmessage environment and the prese nce of
2512 // current v8 context is not guaranteed. Avoid calling v8 then. 2942 // current v8 context is not guaranteed. Avoid calling v8 then.
2513 if (m_externallyAllocatedMemory) { 2943 if (m_externallyAllocatedMemory) {
2514 ASSERT(v8::Isolate::GetCurrent()); 2944 ASSERT(v8::Isolate::GetCurrent());
2515 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-m_exte rnallyAllocatedMemory); 2945 v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(-m_exte rnallyAllocatedMemory);
2516 } 2946 }
2517 } 2947 }
2518 2948
2519 } // namespace WebCore 2949 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698