| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "bindings/modules/v8/ScriptValueSerializerForModules.h" | |
| 6 | |
| 7 #include "bindings/core/v8/SerializationTag.h" | |
| 8 #include "bindings/core/v8/V8Binding.h" | |
| 9 #include "bindings/modules/v8/V8CryptoKey.h" | |
| 10 #include "bindings/modules/v8/V8DOMFileSystem.h" | |
| 11 #include "bindings/modules/v8/V8RTCCertificate.h" | |
| 12 #include "bindings/modules/v8/serialization/WebCryptoSubTags.h" | |
| 13 #include "modules/filesystem/DOMFileSystem.h" | |
| 14 #include "modules/peerconnection/RTCCertificate.h" | |
| 15 #include "public/platform/Platform.h" | |
| 16 #include "public/platform/WebRTCCertificate.h" | |
| 17 #include "public/platform/WebRTCCertificateGenerator.h" | |
| 18 #include "wtf/PtrUtil.h" | |
| 19 #include <memory> | |
| 20 | |
| 21 namespace blink { | |
| 22 | |
| 23 ScriptValueSerializerForModules::ScriptValueSerializerForModules( | |
| 24 SerializedScriptValueWriter& writer, | |
| 25 WebBlobInfoArray* blobInfo, | |
| 26 ScriptState* scriptState) | |
| 27 : ScriptValueSerializer(writer, blobInfo, scriptState) {} | |
| 28 | |
| 29 ScriptValueSerializer::StateBase* | |
| 30 ScriptValueSerializerForModules::writeDOMFileSystem(v8::Local<v8::Value> value, | |
| 31 StateBase* next) { | |
| 32 DOMFileSystem* fs = V8DOMFileSystem::toImpl(value.As<v8::Object>()); | |
| 33 if (!fs) | |
| 34 return 0; | |
| 35 if (!fs->clonable()) | |
| 36 return handleError(Status::DataCloneError, | |
| 37 "A FileSystem object could not be cloned.", next); | |
| 38 | |
| 39 toSerializedScriptValueWriterForModules(writer()).writeDOMFileSystem( | |
| 40 fs->type(), fs->name(), fs->rootURL().getString()); | |
| 41 return 0; | |
| 42 } | |
| 43 | |
| 44 bool ScriptValueSerializerForModules::writeCryptoKey( | |
| 45 v8::Local<v8::Value> value) { | |
| 46 CryptoKey* key = V8CryptoKey::toImpl(value.As<v8::Object>()); | |
| 47 if (!key) | |
| 48 return false; | |
| 49 return toSerializedScriptValueWriterForModules(writer()).writeCryptoKey( | |
| 50 key->key()); | |
| 51 } | |
| 52 | |
| 53 ScriptValueSerializer::StateBase* | |
| 54 ScriptValueSerializerForModules::writeRTCCertificate(v8::Local<v8::Value> value, | |
| 55 StateBase* next) { | |
| 56 RTCCertificate* certificate = | |
| 57 V8RTCCertificate::toImpl(value.As<v8::Object>()); | |
| 58 if (!certificate) | |
| 59 return handleError(Status::DataCloneError, | |
| 60 "An RTCCertificate object could not be cloned.", next); | |
| 61 toSerializedScriptValueWriterForModules(writer()).writeRTCCertificate( | |
| 62 *certificate); | |
| 63 return nullptr; | |
| 64 } | |
| 65 | |
| 66 void SerializedScriptValueWriterForModules::writeDOMFileSystem( | |
| 67 int type, | |
| 68 const String& name, | |
| 69 const String& url) { | |
| 70 append(DOMFileSystemTag); | |
| 71 doWriteUint32(type); | |
| 72 doWriteWebCoreString(name); | |
| 73 doWriteWebCoreString(url); | |
| 74 } | |
| 75 | |
| 76 bool SerializedScriptValueWriterForModules::writeCryptoKey( | |
| 77 const WebCryptoKey& key) { | |
| 78 append(static_cast<uint8_t>(CryptoKeyTag)); | |
| 79 | |
| 80 switch (key.algorithm().paramsType()) { | |
| 81 case WebCryptoKeyAlgorithmParamsTypeAes: | |
| 82 doWriteAesKey(key); | |
| 83 break; | |
| 84 case WebCryptoKeyAlgorithmParamsTypeHmac: | |
| 85 doWriteHmacKey(key); | |
| 86 break; | |
| 87 case WebCryptoKeyAlgorithmParamsTypeRsaHashed: | |
| 88 doWriteRsaHashedKey(key); | |
| 89 break; | |
| 90 case WebCryptoKeyAlgorithmParamsTypeEc: | |
| 91 doWriteEcKey(key); | |
| 92 break; | |
| 93 case WebCryptoKeyAlgorithmParamsTypeNone: | |
| 94 doWriteKeyWithoutParams(key); | |
| 95 break; | |
| 96 } | |
| 97 | |
| 98 doWriteKeyUsages(key.usages(), key.extractable()); | |
| 99 | |
| 100 WebVector<uint8_t> keyData; | |
| 101 if (!Platform::current()->crypto()->serializeKeyForClone(key, keyData)) | |
| 102 return false; | |
| 103 | |
| 104 doWriteUint32(keyData.size()); | |
| 105 append(keyData.data(), keyData.size()); | |
| 106 return true; | |
| 107 } | |
| 108 | |
| 109 void SerializedScriptValueWriterForModules::writeRTCCertificate( | |
| 110 const RTCCertificate& certificate) { | |
| 111 append(RTCCertificateTag); | |
| 112 | |
| 113 WebRTCCertificatePEM pem = certificate.certificateShallowCopy()->toPEM(); | |
| 114 doWriteWebCoreString(pem.privateKey()); | |
| 115 doWriteWebCoreString(pem.certificate()); | |
| 116 } | |
| 117 | |
| 118 void SerializedScriptValueWriterForModules::doWriteHmacKey( | |
| 119 const WebCryptoKey& key) { | |
| 120 ASSERT(key.algorithm().paramsType() == WebCryptoKeyAlgorithmParamsTypeHmac); | |
| 121 | |
| 122 append(static_cast<uint8_t>(HmacKeyTag)); | |
| 123 ASSERT(!(key.algorithm().hmacParams()->lengthBits() % 8)); | |
| 124 doWriteUint32(key.algorithm().hmacParams()->lengthBits() / 8); | |
| 125 doWriteAlgorithmId(key.algorithm().hmacParams()->hash().id()); | |
| 126 } | |
| 127 | |
| 128 void SerializedScriptValueWriterForModules::doWriteAesKey( | |
| 129 const WebCryptoKey& key) { | |
| 130 ASSERT(key.algorithm().paramsType() == WebCryptoKeyAlgorithmParamsTypeAes); | |
| 131 | |
| 132 append(static_cast<uint8_t>(AesKeyTag)); | |
| 133 doWriteAlgorithmId(key.algorithm().id()); | |
| 134 // Converting the key length from bits to bytes is lossless and makes | |
| 135 // it fit in 1 byte. | |
| 136 ASSERT(!(key.algorithm().aesParams()->lengthBits() % 8)); | |
| 137 doWriteUint32(key.algorithm().aesParams()->lengthBits() / 8); | |
| 138 } | |
| 139 | |
| 140 void SerializedScriptValueWriterForModules::doWriteRsaHashedKey( | |
| 141 const WebCryptoKey& key) { | |
| 142 ASSERT(key.algorithm().rsaHashedParams()); | |
| 143 append(static_cast<uint8_t>(RsaHashedKeyTag)); | |
| 144 | |
| 145 doWriteAlgorithmId(key.algorithm().id()); | |
| 146 doWriteAsymmetricKeyType(key.type()); | |
| 147 | |
| 148 const WebCryptoRsaHashedKeyAlgorithmParams* params = | |
| 149 key.algorithm().rsaHashedParams(); | |
| 150 doWriteUint32(params->modulusLengthBits()); | |
| 151 doWriteUint32(params->publicExponent().size()); | |
| 152 append(params->publicExponent().data(), params->publicExponent().size()); | |
| 153 doWriteAlgorithmId(params->hash().id()); | |
| 154 } | |
| 155 | |
| 156 void SerializedScriptValueWriterForModules::doWriteEcKey( | |
| 157 const WebCryptoKey& key) { | |
| 158 ASSERT(key.algorithm().ecParams()); | |
| 159 append(static_cast<uint8_t>(EcKeyTag)); | |
| 160 | |
| 161 doWriteAlgorithmId(key.algorithm().id()); | |
| 162 doWriteAsymmetricKeyType(key.type()); | |
| 163 doWriteNamedCurve(key.algorithm().ecParams()->namedCurve()); | |
| 164 } | |
| 165 | |
| 166 void SerializedScriptValueWriterForModules::doWriteKeyWithoutParams( | |
| 167 const WebCryptoKey& key) { | |
| 168 ASSERT(WebCryptoAlgorithm::isKdf(key.algorithm().id())); | |
| 169 append(static_cast<uint8_t>(NoParamsKeyTag)); | |
| 170 | |
| 171 doWriteAlgorithmId(key.algorithm().id()); | |
| 172 } | |
| 173 | |
| 174 void SerializedScriptValueWriterForModules::doWriteAlgorithmId( | |
| 175 WebCryptoAlgorithmId id) { | |
| 176 switch (id) { | |
| 177 case WebCryptoAlgorithmIdAesCbc: | |
| 178 return doWriteUint32(AesCbcTag); | |
| 179 case WebCryptoAlgorithmIdHmac: | |
| 180 return doWriteUint32(HmacTag); | |
| 181 case WebCryptoAlgorithmIdRsaSsaPkcs1v1_5: | |
| 182 return doWriteUint32(RsaSsaPkcs1v1_5Tag); | |
| 183 case WebCryptoAlgorithmIdSha1: | |
| 184 return doWriteUint32(Sha1Tag); | |
| 185 case WebCryptoAlgorithmIdSha256: | |
| 186 return doWriteUint32(Sha256Tag); | |
| 187 case WebCryptoAlgorithmIdSha384: | |
| 188 return doWriteUint32(Sha384Tag); | |
| 189 case WebCryptoAlgorithmIdSha512: | |
| 190 return doWriteUint32(Sha512Tag); | |
| 191 case WebCryptoAlgorithmIdAesGcm: | |
| 192 return doWriteUint32(AesGcmTag); | |
| 193 case WebCryptoAlgorithmIdRsaOaep: | |
| 194 return doWriteUint32(RsaOaepTag); | |
| 195 case WebCryptoAlgorithmIdAesCtr: | |
| 196 return doWriteUint32(AesCtrTag); | |
| 197 case WebCryptoAlgorithmIdAesKw: | |
| 198 return doWriteUint32(AesKwTag); | |
| 199 case WebCryptoAlgorithmIdRsaPss: | |
| 200 return doWriteUint32(RsaPssTag); | |
| 201 case WebCryptoAlgorithmIdEcdsa: | |
| 202 return doWriteUint32(EcdsaTag); | |
| 203 case WebCryptoAlgorithmIdEcdh: | |
| 204 return doWriteUint32(EcdhTag); | |
| 205 case WebCryptoAlgorithmIdHkdf: | |
| 206 return doWriteUint32(HkdfTag); | |
| 207 case WebCryptoAlgorithmIdPbkdf2: | |
| 208 return doWriteUint32(Pbkdf2Tag); | |
| 209 } | |
| 210 ASSERT_NOT_REACHED(); | |
| 211 } | |
| 212 | |
| 213 void SerializedScriptValueWriterForModules::doWriteAsymmetricKeyType( | |
| 214 WebCryptoKeyType keyType) { | |
| 215 switch (keyType) { | |
| 216 case WebCryptoKeyTypePublic: | |
| 217 doWriteUint32(PublicKeyType); | |
| 218 break; | |
| 219 case WebCryptoKeyTypePrivate: | |
| 220 doWriteUint32(PrivateKeyType); | |
| 221 break; | |
| 222 case WebCryptoKeyTypeSecret: | |
| 223 ASSERT_NOT_REACHED(); | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 void SerializedScriptValueWriterForModules::doWriteNamedCurve( | |
| 228 WebCryptoNamedCurve namedCurve) { | |
| 229 switch (namedCurve) { | |
| 230 case WebCryptoNamedCurveP256: | |
| 231 return doWriteUint32(P256Tag); | |
| 232 case WebCryptoNamedCurveP384: | |
| 233 return doWriteUint32(P384Tag); | |
| 234 case WebCryptoNamedCurveP521: | |
| 235 return doWriteUint32(P521Tag); | |
| 236 } | |
| 237 ASSERT_NOT_REACHED(); | |
| 238 } | |
| 239 | |
| 240 void SerializedScriptValueWriterForModules::doWriteKeyUsages( | |
| 241 const WebCryptoKeyUsageMask usages, | |
| 242 bool extractable) { | |
| 243 // Reminder to update this when adding new key usages. | |
| 244 static_assert(EndOfWebCryptoKeyUsage == (1 << 7) + 1, | |
| 245 "update required when adding new key usages"); | |
| 246 | |
| 247 uint32_t value = 0; | |
| 248 | |
| 249 if (extractable) | |
| 250 value |= ExtractableUsage; | |
| 251 | |
| 252 if (usages & WebCryptoKeyUsageEncrypt) | |
| 253 value |= EncryptUsage; | |
| 254 if (usages & WebCryptoKeyUsageDecrypt) | |
| 255 value |= DecryptUsage; | |
| 256 if (usages & WebCryptoKeyUsageSign) | |
| 257 value |= SignUsage; | |
| 258 if (usages & WebCryptoKeyUsageVerify) | |
| 259 value |= VerifyUsage; | |
| 260 if (usages & WebCryptoKeyUsageDeriveKey) | |
| 261 value |= DeriveKeyUsage; | |
| 262 if (usages & WebCryptoKeyUsageWrapKey) | |
| 263 value |= WrapKeyUsage; | |
| 264 if (usages & WebCryptoKeyUsageUnwrapKey) | |
| 265 value |= UnwrapKeyUsage; | |
| 266 if (usages & WebCryptoKeyUsageDeriveBits) | |
| 267 value |= DeriveBitsUsage; | |
| 268 | |
| 269 doWriteUint32(value); | |
| 270 } | |
| 271 | |
| 272 ScriptValueSerializer::StateBase* | |
| 273 ScriptValueSerializerForModules::doSerializeObject( | |
| 274 v8::Local<v8::Object> jsObject, | |
| 275 StateBase* next) { | |
| 276 DCHECK(!jsObject.IsEmpty()); | |
| 277 | |
| 278 if (V8DOMFileSystem::hasInstance(jsObject, isolate())) { | |
| 279 greyObject(jsObject); | |
| 280 return writeDOMFileSystem(jsObject, next); | |
| 281 } | |
| 282 if (V8CryptoKey::hasInstance(jsObject, isolate())) { | |
| 283 greyObject(jsObject); | |
| 284 if (!writeCryptoKey(jsObject)) | |
| 285 return handleError(Status::DataCloneError, "Couldn't serialize key data", | |
| 286 next); | |
| 287 return nullptr; | |
| 288 } | |
| 289 if (V8RTCCertificate::hasInstance(jsObject, isolate())) { | |
| 290 greyObject(jsObject); | |
| 291 return writeRTCCertificate(jsObject, next); | |
| 292 } | |
| 293 | |
| 294 return ScriptValueSerializer::doSerializeObject(jsObject, next); | |
| 295 } | |
| 296 | |
| 297 bool SerializedScriptValueReaderForModules::read( | |
| 298 v8::Local<v8::Value>* value, | |
| 299 ScriptValueDeserializer& deserializer) { | |
| 300 SerializationTag tag; | |
| 301 if (!readTag(&tag)) | |
| 302 return false; | |
| 303 switch (tag) { | |
| 304 case DOMFileSystemTag: | |
| 305 if (!readDOMFileSystem(value)) | |
| 306 return false; | |
| 307 deserializer.pushObjectReference(*value); | |
| 308 break; | |
| 309 case CryptoKeyTag: | |
| 310 if (!readCryptoKey(value)) | |
| 311 return false; | |
| 312 deserializer.pushObjectReference(*value); | |
| 313 break; | |
| 314 case RTCCertificateTag: | |
| 315 if (!readRTCCertificate(value)) | |
| 316 return false; | |
| 317 deserializer.pushObjectReference(*value); | |
| 318 break; | |
| 319 default: | |
| 320 return SerializedScriptValueReader::readWithTag(tag, value, deserializer); | |
| 321 } | |
| 322 return !value->IsEmpty(); | |
| 323 } | |
| 324 | |
| 325 bool SerializedScriptValueReaderForModules::readDOMFileSystem( | |
| 326 v8::Local<v8::Value>* value) { | |
| 327 uint32_t type; | |
| 328 String name; | |
| 329 String url; | |
| 330 if (!doReadUint32(&type)) | |
| 331 return false; | |
| 332 if (!readWebCoreString(&name)) | |
| 333 return false; | |
| 334 if (!readWebCoreString(&url)) | |
| 335 return false; | |
| 336 DOMFileSystem* fs = DOMFileSystem::create( | |
| 337 getScriptState()->getExecutionContext(), name, | |
| 338 static_cast<FileSystemType>(type), KURL(ParsedURLString, url)); | |
| 339 *value = ToV8(fs, getScriptState()->context()->Global(), isolate()); | |
| 340 return !value->IsEmpty(); | |
| 341 } | |
| 342 | |
| 343 bool SerializedScriptValueReaderForModules::readCryptoKey( | |
| 344 v8::Local<v8::Value>* value) { | |
| 345 uint32_t rawKeyType; | |
| 346 if (!doReadUint32(&rawKeyType)) | |
| 347 return false; | |
| 348 | |
| 349 WebCryptoKeyAlgorithm algorithm; | |
| 350 WebCryptoKeyType type = WebCryptoKeyTypeSecret; | |
| 351 | |
| 352 switch (static_cast<CryptoKeySubTag>(rawKeyType)) { | |
| 353 case AesKeyTag: | |
| 354 if (!doReadAesKey(algorithm, type)) | |
| 355 return false; | |
| 356 break; | |
| 357 case HmacKeyTag: | |
| 358 if (!doReadHmacKey(algorithm, type)) | |
| 359 return false; | |
| 360 break; | |
| 361 case RsaHashedKeyTag: | |
| 362 if (!doReadRsaHashedKey(algorithm, type)) | |
| 363 return false; | |
| 364 break; | |
| 365 case EcKeyTag: | |
| 366 if (!doReadEcKey(algorithm, type)) | |
| 367 return false; | |
| 368 break; | |
| 369 case NoParamsKeyTag: | |
| 370 if (!doReadKeyWithoutParams(algorithm, type)) | |
| 371 return false; | |
| 372 break; | |
| 373 default: | |
| 374 return false; | |
| 375 } | |
| 376 | |
| 377 WebCryptoKeyUsageMask usages; | |
| 378 bool extractable; | |
| 379 if (!doReadKeyUsages(usages, extractable)) | |
| 380 return false; | |
| 381 | |
| 382 uint32_t keyDataLength; | |
| 383 if (!doReadUint32(&keyDataLength)) | |
| 384 return false; | |
| 385 | |
| 386 if (position() + keyDataLength > length()) | |
| 387 return false; | |
| 388 | |
| 389 const uint8_t* keyData = allocate(keyDataLength); | |
| 390 WebCryptoKey key = WebCryptoKey::createNull(); | |
| 391 if (!Platform::current()->crypto()->deserializeKeyForClone( | |
| 392 algorithm, type, extractable, usages, keyData, keyDataLength, key)) { | |
| 393 return false; | |
| 394 } | |
| 395 | |
| 396 *value = ToV8(CryptoKey::create(key), getScriptState()->context()->Global(), | |
| 397 isolate()); | |
| 398 return !value->IsEmpty(); | |
| 399 } | |
| 400 | |
| 401 bool SerializedScriptValueReaderForModules::readRTCCertificate( | |
| 402 v8::Local<v8::Value>* value) { | |
| 403 String pemPrivateKey; | |
| 404 if (!readWebCoreString(&pemPrivateKey)) | |
| 405 return false; | |
| 406 String pemCertificate; | |
| 407 if (!readWebCoreString(&pemCertificate)) | |
| 408 return false; | |
| 409 | |
| 410 std::unique_ptr<WebRTCCertificateGenerator> certificateGenerator = | |
| 411 WTF::wrapUnique(Platform::current()->createRTCCertificateGenerator()); | |
| 412 | |
| 413 std::unique_ptr<WebRTCCertificate> certificate( | |
| 414 certificateGenerator->fromPEM(pemPrivateKey, pemCertificate)); | |
| 415 if (!certificate) | |
| 416 return false; | |
| 417 RTCCertificate* jsCertificate = new RTCCertificate(std::move(certificate)); | |
| 418 | |
| 419 *value = | |
| 420 ToV8(jsCertificate, getScriptState()->context()->Global(), isolate()); | |
| 421 return !value->IsEmpty(); | |
| 422 } | |
| 423 | |
| 424 bool SerializedScriptValueReaderForModules::doReadHmacKey( | |
| 425 WebCryptoKeyAlgorithm& algorithm, | |
| 426 WebCryptoKeyType& type) { | |
| 427 uint32_t lengthBytes; | |
| 428 if (!doReadUint32(&lengthBytes)) | |
| 429 return false; | |
| 430 WebCryptoAlgorithmId hash; | |
| 431 if (!doReadAlgorithmId(hash)) | |
| 432 return false; | |
| 433 algorithm = WebCryptoKeyAlgorithm::createHmac(hash, lengthBytes * 8); | |
| 434 type = WebCryptoKeyTypeSecret; | |
| 435 return !algorithm.isNull(); | |
| 436 } | |
| 437 | |
| 438 bool SerializedScriptValueReaderForModules::doReadAesKey( | |
| 439 WebCryptoKeyAlgorithm& algorithm, | |
| 440 WebCryptoKeyType& type) { | |
| 441 WebCryptoAlgorithmId id; | |
| 442 if (!doReadAlgorithmId(id)) | |
| 443 return false; | |
| 444 uint32_t lengthBytes; | |
| 445 if (!doReadUint32(&lengthBytes)) | |
| 446 return false; | |
| 447 algorithm = WebCryptoKeyAlgorithm::createAes(id, lengthBytes * 8); | |
| 448 type = WebCryptoKeyTypeSecret; | |
| 449 return !algorithm.isNull(); | |
| 450 } | |
| 451 | |
| 452 bool SerializedScriptValueReaderForModules::doReadRsaHashedKey( | |
| 453 WebCryptoKeyAlgorithm& algorithm, | |
| 454 WebCryptoKeyType& type) { | |
| 455 WebCryptoAlgorithmId id; | |
| 456 if (!doReadAlgorithmId(id)) | |
| 457 return false; | |
| 458 | |
| 459 if (!doReadAsymmetricKeyType(type)) | |
| 460 return false; | |
| 461 | |
| 462 uint32_t modulusLengthBits; | |
| 463 if (!doReadUint32(&modulusLengthBits)) | |
| 464 return false; | |
| 465 | |
| 466 uint32_t publicExponentSize; | |
| 467 if (!doReadUint32(&publicExponentSize)) | |
| 468 return false; | |
| 469 | |
| 470 if (position() + publicExponentSize > length()) | |
| 471 return false; | |
| 472 | |
| 473 const uint8_t* publicExponent = allocate(publicExponentSize); | |
| 474 WebCryptoAlgorithmId hash; | |
| 475 if (!doReadAlgorithmId(hash)) | |
| 476 return false; | |
| 477 algorithm = WebCryptoKeyAlgorithm::createRsaHashed( | |
| 478 id, modulusLengthBits, publicExponent, publicExponentSize, hash); | |
| 479 | |
| 480 return !algorithm.isNull(); | |
| 481 } | |
| 482 | |
| 483 bool SerializedScriptValueReaderForModules::doReadEcKey( | |
| 484 WebCryptoKeyAlgorithm& algorithm, | |
| 485 WebCryptoKeyType& type) { | |
| 486 WebCryptoAlgorithmId id; | |
| 487 if (!doReadAlgorithmId(id)) | |
| 488 return false; | |
| 489 | |
| 490 if (!doReadAsymmetricKeyType(type)) | |
| 491 return false; | |
| 492 | |
| 493 WebCryptoNamedCurve namedCurve; | |
| 494 if (!doReadNamedCurve(namedCurve)) | |
| 495 return false; | |
| 496 | |
| 497 algorithm = WebCryptoKeyAlgorithm::createEc(id, namedCurve); | |
| 498 return !algorithm.isNull(); | |
| 499 } | |
| 500 | |
| 501 bool SerializedScriptValueReaderForModules::doReadKeyWithoutParams( | |
| 502 WebCryptoKeyAlgorithm& algorithm, | |
| 503 WebCryptoKeyType& type) { | |
| 504 WebCryptoAlgorithmId id; | |
| 505 if (!doReadAlgorithmId(id)) | |
| 506 return false; | |
| 507 algorithm = WebCryptoKeyAlgorithm::createWithoutParams(id); | |
| 508 type = WebCryptoKeyTypeSecret; | |
| 509 return !algorithm.isNull(); | |
| 510 } | |
| 511 | |
| 512 bool SerializedScriptValueReaderForModules::doReadAlgorithmId( | |
| 513 WebCryptoAlgorithmId& id) { | |
| 514 uint32_t rawId; | |
| 515 if (!doReadUint32(&rawId)) | |
| 516 return false; | |
| 517 | |
| 518 switch (static_cast<CryptoKeyAlgorithmTag>(rawId)) { | |
| 519 case AesCbcTag: | |
| 520 id = WebCryptoAlgorithmIdAesCbc; | |
| 521 return true; | |
| 522 case HmacTag: | |
| 523 id = WebCryptoAlgorithmIdHmac; | |
| 524 return true; | |
| 525 case RsaSsaPkcs1v1_5Tag: | |
| 526 id = WebCryptoAlgorithmIdRsaSsaPkcs1v1_5; | |
| 527 return true; | |
| 528 case Sha1Tag: | |
| 529 id = WebCryptoAlgorithmIdSha1; | |
| 530 return true; | |
| 531 case Sha256Tag: | |
| 532 id = WebCryptoAlgorithmIdSha256; | |
| 533 return true; | |
| 534 case Sha384Tag: | |
| 535 id = WebCryptoAlgorithmIdSha384; | |
| 536 return true; | |
| 537 case Sha512Tag: | |
| 538 id = WebCryptoAlgorithmIdSha512; | |
| 539 return true; | |
| 540 case AesGcmTag: | |
| 541 id = WebCryptoAlgorithmIdAesGcm; | |
| 542 return true; | |
| 543 case RsaOaepTag: | |
| 544 id = WebCryptoAlgorithmIdRsaOaep; | |
| 545 return true; | |
| 546 case AesCtrTag: | |
| 547 id = WebCryptoAlgorithmIdAesCtr; | |
| 548 return true; | |
| 549 case AesKwTag: | |
| 550 id = WebCryptoAlgorithmIdAesKw; | |
| 551 return true; | |
| 552 case RsaPssTag: | |
| 553 id = WebCryptoAlgorithmIdRsaPss; | |
| 554 return true; | |
| 555 case EcdsaTag: | |
| 556 id = WebCryptoAlgorithmIdEcdsa; | |
| 557 return true; | |
| 558 case EcdhTag: | |
| 559 id = WebCryptoAlgorithmIdEcdh; | |
| 560 return true; | |
| 561 case HkdfTag: | |
| 562 id = WebCryptoAlgorithmIdHkdf; | |
| 563 return true; | |
| 564 case Pbkdf2Tag: | |
| 565 id = WebCryptoAlgorithmIdPbkdf2; | |
| 566 return true; | |
| 567 } | |
| 568 | |
| 569 return false; | |
| 570 } | |
| 571 | |
| 572 bool SerializedScriptValueReaderForModules::doReadAsymmetricKeyType( | |
| 573 WebCryptoKeyType& type) { | |
| 574 uint32_t rawType; | |
| 575 if (!doReadUint32(&rawType)) | |
| 576 return false; | |
| 577 | |
| 578 switch (static_cast<AsymmetricCryptoKeyType>(rawType)) { | |
| 579 case PublicKeyType: | |
| 580 type = WebCryptoKeyTypePublic; | |
| 581 return true; | |
| 582 case PrivateKeyType: | |
| 583 type = WebCryptoKeyTypePrivate; | |
| 584 return true; | |
| 585 } | |
| 586 | |
| 587 return false; | |
| 588 } | |
| 589 | |
| 590 bool SerializedScriptValueReaderForModules::doReadNamedCurve( | |
| 591 WebCryptoNamedCurve& namedCurve) { | |
| 592 uint32_t rawName; | |
| 593 if (!doReadUint32(&rawName)) | |
| 594 return false; | |
| 595 | |
| 596 switch (static_cast<NamedCurveTag>(rawName)) { | |
| 597 case P256Tag: | |
| 598 namedCurve = WebCryptoNamedCurveP256; | |
| 599 return true; | |
| 600 case P384Tag: | |
| 601 namedCurve = WebCryptoNamedCurveP384; | |
| 602 return true; | |
| 603 case P521Tag: | |
| 604 namedCurve = WebCryptoNamedCurveP521; | |
| 605 return true; | |
| 606 } | |
| 607 | |
| 608 return false; | |
| 609 } | |
| 610 | |
| 611 bool SerializedScriptValueReaderForModules::doReadKeyUsages( | |
| 612 WebCryptoKeyUsageMask& usages, | |
| 613 bool& extractable) { | |
| 614 // Reminder to update this when adding new key usages. | |
| 615 static_assert(EndOfWebCryptoKeyUsage == (1 << 7) + 1, | |
| 616 "update required when adding new key usages"); | |
| 617 const uint32_t allPossibleUsages = | |
| 618 ExtractableUsage | EncryptUsage | DecryptUsage | SignUsage | VerifyUsage | | |
| 619 DeriveKeyUsage | WrapKeyUsage | UnwrapKeyUsage | DeriveBitsUsage; | |
| 620 | |
| 621 uint32_t rawUsages; | |
| 622 if (!doReadUint32(&rawUsages)) | |
| 623 return false; | |
| 624 | |
| 625 // Make sure it doesn't contain an unrecognized usage value. | |
| 626 if (rawUsages & ~allPossibleUsages) | |
| 627 return false; | |
| 628 | |
| 629 usages = 0; | |
| 630 | |
| 631 extractable = rawUsages & ExtractableUsage; | |
| 632 | |
| 633 if (rawUsages & EncryptUsage) | |
| 634 usages |= WebCryptoKeyUsageEncrypt; | |
| 635 if (rawUsages & DecryptUsage) | |
| 636 usages |= WebCryptoKeyUsageDecrypt; | |
| 637 if (rawUsages & SignUsage) | |
| 638 usages |= WebCryptoKeyUsageSign; | |
| 639 if (rawUsages & VerifyUsage) | |
| 640 usages |= WebCryptoKeyUsageVerify; | |
| 641 if (rawUsages & DeriveKeyUsage) | |
| 642 usages |= WebCryptoKeyUsageDeriveKey; | |
| 643 if (rawUsages & WrapKeyUsage) | |
| 644 usages |= WebCryptoKeyUsageWrapKey; | |
| 645 if (rawUsages & UnwrapKeyUsage) | |
| 646 usages |= WebCryptoKeyUsageUnwrapKey; | |
| 647 if (rawUsages & DeriveBitsUsage) | |
| 648 usages |= WebCryptoKeyUsageDeriveBits; | |
| 649 | |
| 650 return true; | |
| 651 } | |
| 652 | |
| 653 ScriptValueDeserializerForModules::ScriptValueDeserializerForModules( | |
| 654 SerializedScriptValueReaderForModules& reader, | |
| 655 MessagePortArray* messagePorts, | |
| 656 ArrayBufferContentsArray* arrayBufferContents, | |
| 657 ImageBitmapContentsArray* imageBitmapContents) | |
| 658 : ScriptValueDeserializer(reader, | |
| 659 messagePorts, | |
| 660 arrayBufferContents, | |
| 661 imageBitmapContents) {} | |
| 662 | |
| 663 bool ScriptValueDeserializerForModules::read(v8::Local<v8::Value>* value) { | |
| 664 return toSerializedScriptValueReaderForModules(reader()).read(value, *this); | |
| 665 } | |
| 666 | |
| 667 } // namespace blink | |
| OLD | NEW |