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

Side by Side Diff: third_party/WebKit/Source/bindings/modules/v8/ScriptValueSerializerForModules.cpp

Issue 1949033002: Ability to persist RTCCertificate in IndexedDB (enables cloning). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "bindings/modules/v8/ScriptValueSerializerForModules.h" 5 #include "bindings/modules/v8/ScriptValueSerializerForModules.h"
6 6
7 #include "bindings/core/v8/SerializationTag.h" 7 #include "bindings/core/v8/SerializationTag.h"
8 #include "bindings/core/v8/V8Binding.h" 8 #include "bindings/core/v8/V8Binding.h"
9 #include "bindings/modules/v8/V8CryptoKey.h" 9 #include "bindings/modules/v8/V8CryptoKey.h"
10 #include "bindings/modules/v8/V8DOMFileSystem.h" 10 #include "bindings/modules/v8/V8DOMFileSystem.h"
11 #include "bindings/modules/v8/V8RTCCertificate.h"
11 #include "modules/filesystem/DOMFileSystem.h" 12 #include "modules/filesystem/DOMFileSystem.h"
13 #include "modules/mediastream/RTCCertificate.h"
12 #include "public/platform/Platform.h" 14 #include "public/platform/Platform.h"
15 #include "public/platform/WebRTCCertificate.h"
16 #include "public/platform/WebRTCCertificateGenerator.h"
13 17
14 namespace blink { 18 namespace blink {
15 19
16 enum CryptoKeyAlgorithmTag { 20 enum CryptoKeyAlgorithmTag {
17 AesCbcTag = 1, 21 AesCbcTag = 1,
18 HmacTag = 2, 22 HmacTag = 2,
19 RsaSsaPkcs1v1_5Tag = 3, 23 RsaSsaPkcs1v1_5Tag = 3,
20 // ID 4 was used by RsaEs, while still behind experimental flag. 24 // ID 4 was used by RsaEs, while still behind experimental flag.
21 Sha1Tag = 5, 25 Sha1Tag = 5,
22 Sha256Tag = 6, 26 Sha256Tag = 6,
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 } 95 }
92 96
93 bool ScriptValueSerializerForModules::writeCryptoKey(v8::Local<v8::Value> value) 97 bool ScriptValueSerializerForModules::writeCryptoKey(v8::Local<v8::Value> value)
94 { 98 {
95 CryptoKey* key = V8CryptoKey::toImpl(value.As<v8::Object>()); 99 CryptoKey* key = V8CryptoKey::toImpl(value.As<v8::Object>());
96 if (!key) 100 if (!key)
97 return false; 101 return false;
98 return toSerializedScriptValueWriterForModules(writer()).writeCryptoKey(key- >key()); 102 return toSerializedScriptValueWriterForModules(writer()).writeCryptoKey(key- >key());
99 } 103 }
100 104
105 ScriptValueSerializer::StateBase* ScriptValueSerializerForModules::writeRTCCerti ficate(v8::Local<v8::Value> value, ScriptValueSerializer::StateBase* next)
106 {
107 RTCCertificate* certificate = V8RTCCertificate::toImpl(value.As<v8::Object>( ));
108 if (!certificate)
109 return handleError(DataCloneError, "An RTCCertificate object could not b e cloned.", next);
110 toSerializedScriptValueWriterForModules(writer()).writeRTCCertificate(*certi ficate);
111 return 0;
112 }
113
101 void SerializedScriptValueWriterForModules::writeDOMFileSystem(int type, const S tring& name, const String& url) 114 void SerializedScriptValueWriterForModules::writeDOMFileSystem(int type, const S tring& name, const String& url)
102 { 115 {
103 append(DOMFileSystemTag); 116 append(DOMFileSystemTag);
104 doWriteUint32(type); 117 doWriteUint32(type);
105 doWriteWebCoreString(name); 118 doWriteWebCoreString(name);
106 doWriteWebCoreString(url); 119 doWriteWebCoreString(url);
107 } 120 }
108 121
109 bool SerializedScriptValueWriterForModules::writeCryptoKey(const WebCryptoKey& k ey) 122 bool SerializedScriptValueWriterForModules::writeCryptoKey(const WebCryptoKey& k ey)
110 { 123 {
(...skipping 21 matching lines...) Expand all
132 145
133 WebVector<uint8_t> keyData; 146 WebVector<uint8_t> keyData;
134 if (!Platform::current()->crypto()->serializeKeyForClone(key, keyData)) 147 if (!Platform::current()->crypto()->serializeKeyForClone(key, keyData))
135 return false; 148 return false;
136 149
137 doWriteUint32(keyData.size()); 150 doWriteUint32(keyData.size());
138 append(keyData.data(), keyData.size()); 151 append(keyData.data(), keyData.size());
139 return true; 152 return true;
140 } 153 }
141 154
155 void SerializedScriptValueWriterForModules::writeRTCCertificate(const RTCCertifi cate& certificate)
156 {
157 append(RTCCertificateTag);
158
159 WebRTCCertificatePEM pem = certificate.certificateShallowCopy()->toPEM();
160 doWriteWebCoreString(String(pem.privateKey().c_str()));
161 doWriteWebCoreString(String(pem.certificate().c_str()));
162 }
163
142 void SerializedScriptValueWriterForModules::doWriteHmacKey(const WebCryptoKey& k ey) 164 void SerializedScriptValueWriterForModules::doWriteHmacKey(const WebCryptoKey& k ey)
143 { 165 {
144 ASSERT(key.algorithm().paramsType() == WebCryptoKeyAlgorithmParamsTypeHmac); 166 ASSERT(key.algorithm().paramsType() == WebCryptoKeyAlgorithmParamsTypeHmac);
145 167
146 append(static_cast<uint8_t>(HmacKeyTag)); 168 append(static_cast<uint8_t>(HmacKeyTag));
147 ASSERT(!(key.algorithm().hmacParams()->lengthBits() % 8)); 169 ASSERT(!(key.algorithm().hmacParams()->lengthBits() % 8));
148 doWriteUint32(key.algorithm().hmacParams()->lengthBits() / 8); 170 doWriteUint32(key.algorithm().hmacParams()->lengthBits() / 8);
149 doWriteAlgorithmId(key.algorithm().hmacParams()->hash().id()); 171 doWriteAlgorithmId(key.algorithm().hmacParams()->hash().id());
150 } 172 }
151 173
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after
286 value |= UnwrapKeyUsage; 308 value |= UnwrapKeyUsage;
287 if (usages & WebCryptoKeyUsageDeriveBits) 309 if (usages & WebCryptoKeyUsageDeriveBits)
288 value |= DeriveBitsUsage; 310 value |= DeriveBitsUsage;
289 311
290 doWriteUint32(value); 312 doWriteUint32(value);
291 } 313 }
292 314
293 ScriptValueSerializer::StateBase* ScriptValueSerializerForModules::doSerializeVa lue(v8::Local<v8::Value> value, ScriptValueSerializer::StateBase* next) 315 ScriptValueSerializer::StateBase* ScriptValueSerializerForModules::doSerializeVa lue(v8::Local<v8::Value> value, ScriptValueSerializer::StateBase* next)
294 { 316 {
295 bool isDOMFileSystem = V8DOMFileSystem::hasInstance(value, isolate()); 317 bool isDOMFileSystem = V8DOMFileSystem::hasInstance(value, isolate());
296 if (isDOMFileSystem || V8CryptoKey::hasInstance(value, isolate())) { 318 bool isCryptoKey = V8CryptoKey::hasInstance(value, isolate());
319 bool isRTCCertificate = V8RTCCertificate::hasInstance(value, isolate());
320 if (isDOMFileSystem || isCryptoKey || isRTCCertificate) {
297 v8::Local<v8::Object> jsObject = value.As<v8::Object>(); 321 v8::Local<v8::Object> jsObject = value.As<v8::Object>();
298 if (jsObject.IsEmpty()) 322 if (jsObject.IsEmpty())
299 return handleError(DataCloneError, "An object could not be cloned.", next); 323 return handleError(DataCloneError, "An object could not be cloned.", next);
300 greyObject(jsObject); 324 greyObject(jsObject);
301 325
302 if (isDOMFileSystem) 326 if (isDOMFileSystem)
303 return writeDOMFileSystem(value, next); 327 return writeDOMFileSystem(value, next);
304 328 if (isCryptoKey) {
305 if (!writeCryptoKey(value)) 329 if (!writeCryptoKey(value))
306 return handleError(DataCloneError, "Couldn't serialize key data", ne xt); 330 return handleError(DataCloneError, "Couldn't serialize key data" , next);
307 return 0; 331 return 0;
332 }
333 if (isRTCCertificate)
334 return writeRTCCertificate(value, next);
335 ASSERT_NOT_REACHED();
308 } 336 }
309 return ScriptValueSerializer::doSerializeValue(value, next); 337 return ScriptValueSerializer::doSerializeValue(value, next);
310 } 338 }
311 339
312 bool SerializedScriptValueReaderForModules::read(v8::Local<v8::Value>* value, Sc riptValueCompositeCreator& creator) 340 bool SerializedScriptValueReaderForModules::read(v8::Local<v8::Value>* value, Sc riptValueCompositeCreator& creator)
313 { 341 {
314 SerializationTag tag; 342 SerializationTag tag;
315 if (!readTag(&tag)) 343 if (!readTag(&tag))
316 return false; 344 return false;
317 switch (tag) { 345 switch (tag) {
318 case DOMFileSystemTag: 346 case DOMFileSystemTag:
319 if (!readDOMFileSystem(value)) 347 if (!readDOMFileSystem(value))
320 return false; 348 return false;
321 creator.pushObjectReference(*value); 349 creator.pushObjectReference(*value);
322 break; 350 break;
323 case CryptoKeyTag: 351 case CryptoKeyTag:
324 if (!readCryptoKey(value)) 352 if (!readCryptoKey(value))
325 return false; 353 return false;
326 creator.pushObjectReference(*value); 354 creator.pushObjectReference(*value);
327 break; 355 break;
356 case RTCCertificateTag:
357 if (!readRTCCertificate(value))
358 return false;
359 creator.pushObjectReference(*value);
360 break;
328 default: 361 default:
329 return SerializedScriptValueReader::readWithTag(tag, value, creator); 362 return SerializedScriptValueReader::readWithTag(tag, value, creator);
330 } 363 }
331 return !value->IsEmpty(); 364 return !value->IsEmpty();
332 } 365 }
333 366
334 bool SerializedScriptValueReaderForModules::readDOMFileSystem(v8::Local<v8::Valu e>* value) 367 bool SerializedScriptValueReaderForModules::readDOMFileSystem(v8::Local<v8::Valu e>* value)
335 { 368 {
336 uint32_t type; 369 uint32_t type;
337 String name; 370 String name;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 WebCryptoKey key = WebCryptoKey::createNull(); 430 WebCryptoKey key = WebCryptoKey::createNull();
398 if (!Platform::current()->crypto()->deserializeKeyForClone( 431 if (!Platform::current()->crypto()->deserializeKeyForClone(
399 algorithm, type, extractable, usages, keyData, keyDataLength, key)) { 432 algorithm, type, extractable, usages, keyData, keyDataLength, key)) {
400 return false; 433 return false;
401 } 434 }
402 435
403 *value = toV8(CryptoKey::create(key), getScriptState()->context()->Global(), isolate()); 436 *value = toV8(CryptoKey::create(key), getScriptState()->context()->Global(), isolate());
404 return !value->IsEmpty(); 437 return !value->IsEmpty();
405 } 438 }
406 439
440 bool SerializedScriptValueReaderForModules::readRTCCertificate(v8::Local<v8::Val ue>* value)
441 {
442 String pemPrivateKey;
443 if (!readWebCoreString(&pemPrivateKey))
444 return false;
445 String pemCertificate;
446 if (!readWebCoreString(&pemCertificate))
447 return false;
448
449 OwnPtr<WebRTCCertificateGenerator> certificateGenerator = adoptPtr(
450 Platform::current()->createRTCCertificateGenerator());
451
452 std::unique_ptr<WebRTCCertificate> certificate(
453 certificateGenerator->fromPEM(
454 pemPrivateKey.utf8().data(),
455 pemCertificate.utf8().data()));
456 RTCCertificate* jsCertificate = new RTCCertificate(std::move(certificate));
457
458 *value = toV8(jsCertificate, getScriptState()->context()->Global(), isolate( ));
459 return !value->IsEmpty();
460 }
461
407 bool SerializedScriptValueReaderForModules::doReadHmacKey(WebCryptoKeyAlgorithm& algorithm, WebCryptoKeyType& type) 462 bool SerializedScriptValueReaderForModules::doReadHmacKey(WebCryptoKeyAlgorithm& algorithm, WebCryptoKeyType& type)
408 { 463 {
409 uint32_t lengthBytes; 464 uint32_t lengthBytes;
410 if (!doReadUint32(&lengthBytes)) 465 if (!doReadUint32(&lengthBytes))
411 return false; 466 return false;
412 WebCryptoAlgorithmId hash; 467 WebCryptoAlgorithmId hash;
413 if (!doReadAlgorithmId(hash)) 468 if (!doReadAlgorithmId(hash))
414 return false; 469 return false;
415 algorithm = WebCryptoKeyAlgorithm::createHmac(hash, lengthBytes * 8); 470 algorithm = WebCryptoKeyAlgorithm::createHmac(hash, lengthBytes * 8);
416 type = WebCryptoKeyTypeSecret; 471 type = WebCryptoKeyTypeSecret;
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
627 : ScriptValueDeserializer(reader, messagePorts, arrayBufferContents, imageBi tmapContents) 682 : ScriptValueDeserializer(reader, messagePorts, arrayBufferContents, imageBi tmapContents)
628 { 683 {
629 } 684 }
630 685
631 bool ScriptValueDeserializerForModules::read(v8::Local<v8::Value>* value) 686 bool ScriptValueDeserializerForModules::read(v8::Local<v8::Value>* value)
632 { 687 {
633 return toSerializedScriptValueReaderForModules(reader()).read(value, *this); 688 return toSerializedScriptValueReaderForModules(reader()).read(value, *this);
634 } 689 }
635 690
636 } // namespace blink 691 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698