| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "media/cdm/json_web_key.h" | 5 #include "media/cdm/json_web_key.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <utility> |
| 10 | 11 |
| 11 #include "base/base64url.h" | 12 #include "base/base64url.h" |
| 12 #include "base/json/json_reader.h" | 13 #include "base/json/json_reader.h" |
| 13 #include "base/json/json_string_value_serializer.h" | 14 #include "base/json/json_string_value_serializer.h" |
| 14 #include "base/json/string_escape.h" | 15 #include "base/json/string_escape.h" |
| 15 #include "base/logging.h" | 16 #include "base/logging.h" |
| 16 #include "base/macros.h" | 17 #include "base/macros.h" |
| 18 #include "base/memory/ptr_util.h" |
| 17 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/string_piece.h" | 20 #include "base/strings/string_piece.h" |
| 19 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 20 #include "base/values.h" | 22 #include "base/values.h" |
| 21 #include "media/base/content_decryption_module.h" | 23 #include "media/base/content_decryption_module.h" |
| 22 | 24 |
| 23 namespace media { | 25 namespace media { |
| 24 | 26 |
| 25 const char kKeysTag[] = "keys"; | 27 const char kKeysTag[] = "keys"; |
| 26 const char kKeyTypeTag[] = "kty"; | 28 const char kKeyTypeTag[] = "kty"; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 54 const uint8_t* key_id, | 56 const uint8_t* key_id, |
| 55 int key_id_length) { | 57 int key_id_length) { |
| 56 std::string key_string, key_id_string; | 58 std::string key_string, key_id_string; |
| 57 base::Base64UrlEncode( | 59 base::Base64UrlEncode( |
| 58 base::StringPiece(reinterpret_cast<const char*>(key), key_length), | 60 base::StringPiece(reinterpret_cast<const char*>(key), key_length), |
| 59 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_string); | 61 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_string); |
| 60 base::Base64UrlEncode( | 62 base::Base64UrlEncode( |
| 61 base::StringPiece(reinterpret_cast<const char*>(key_id), key_id_length), | 63 base::StringPiece(reinterpret_cast<const char*>(key_id), key_id_length), |
| 62 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); | 64 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); |
| 63 | 65 |
| 64 std::unique_ptr<base::DictionaryValue> jwk(new base::DictionaryValue()); | 66 auto jwk = base::MakeUnique<base::DictionaryValue>(); |
| 65 jwk->SetString(kKeyTypeTag, kKeyTypeOct); | 67 jwk->SetString(kKeyTypeTag, kKeyTypeOct); |
| 66 jwk->SetString(kKeyTag, key_string); | 68 jwk->SetString(kKeyTag, key_string); |
| 67 jwk->SetString(kKeyIdTag, key_id_string); | 69 jwk->SetString(kKeyIdTag, key_id_string); |
| 68 return jwk; | 70 return jwk; |
| 69 } | 71 } |
| 70 | 72 |
| 71 std::string GenerateJWKSet(const uint8_t* key, | 73 std::string GenerateJWKSet(const uint8_t* key, |
| 72 int key_length, | 74 int key_length, |
| 73 const uint8_t* key_id, | 75 const uint8_t* key_id, |
| 74 int key_id_length) { | 76 int key_id_length) { |
| 75 // Create the JWK, and wrap it into a JWK Set. | 77 // Create the JWK, and wrap it into a JWK Set. |
| 76 std::unique_ptr<base::ListValue> list(new base::ListValue()); | 78 auto list = base::MakeUnique<base::ListValue>(); |
| 77 list->Append(CreateJSONDictionary(key, key_length, key_id, key_id_length)); | 79 list->Append(CreateJSONDictionary(key, key_length, key_id, key_id_length)); |
| 78 base::DictionaryValue jwk_set; | 80 base::DictionaryValue jwk_set; |
| 79 jwk_set.Set(kKeysTag, list.release()); | 81 jwk_set.Set(kKeysTag, std::move(list)); |
| 80 | 82 |
| 81 // Finally serialize |jwk_set| into a string and return it. | 83 // Finally serialize |jwk_set| into a string and return it. |
| 82 std::string serialized_jwk; | 84 std::string serialized_jwk; |
| 83 JSONStringValueSerializer serializer(&serialized_jwk); | 85 JSONStringValueSerializer serializer(&serialized_jwk); |
| 84 serializer.Serialize(jwk_set); | 86 serializer.Serialize(jwk_set); |
| 85 return serialized_jwk; | 87 return serialized_jwk; |
| 86 } | 88 } |
| 87 | 89 |
| 88 std::string GenerateJWKSet(const KeyIdAndKeyPairs& keys, | 90 std::string GenerateJWKSet(const KeyIdAndKeyPairs& keys, |
| 89 CdmSessionType session_type) { | 91 CdmSessionType session_type) { |
| 90 std::unique_ptr<base::ListValue> list(new base::ListValue()); | 92 auto list = base::MakeUnique<base::ListValue>(); |
| 91 for (const auto& key_pair : keys) { | 93 for (const auto& key_pair : keys) { |
| 92 list->Append(CreateJSONDictionary( | 94 list->Append(CreateJSONDictionary( |
| 93 reinterpret_cast<const uint8_t*>(key_pair.second.data()), | 95 reinterpret_cast<const uint8_t*>(key_pair.second.data()), |
| 94 key_pair.second.length(), | 96 key_pair.second.length(), |
| 95 reinterpret_cast<const uint8_t*>(key_pair.first.data()), | 97 reinterpret_cast<const uint8_t*>(key_pair.first.data()), |
| 96 key_pair.first.length())); | 98 key_pair.first.length())); |
| 97 } | 99 } |
| 98 | 100 |
| 99 base::DictionaryValue jwk_set; | 101 base::DictionaryValue jwk_set; |
| 100 jwk_set.Set(kKeysTag, list.release()); | 102 jwk_set.Set(kKeysTag, std::move(list)); |
| 101 switch (session_type) { | 103 switch (session_type) { |
| 102 case CdmSessionType::TEMPORARY_SESSION: | 104 case CdmSessionType::TEMPORARY_SESSION: |
| 103 jwk_set.SetString(kTypeTag, kTemporarySession); | 105 jwk_set.SetString(kTypeTag, kTemporarySession); |
| 104 break; | 106 break; |
| 105 case CdmSessionType::PERSISTENT_LICENSE_SESSION: | 107 case CdmSessionType::PERSISTENT_LICENSE_SESSION: |
| 106 jwk_set.SetString(kTypeTag, kPersistentLicenseSession); | 108 jwk_set.SetString(kTypeTag, kPersistentLicenseSession); |
| 107 break; | 109 break; |
| 108 case CdmSessionType::PERSISTENT_RELEASE_MESSAGE_SESSION: | 110 case CdmSessionType::PERSISTENT_RELEASE_MESSAGE_SESSION: |
| 109 jwk_set.SetString(kTypeTag, kPersistentReleaseMessageSession); | 111 jwk_set.SetString(kTypeTag, kPersistentReleaseMessageSession); |
| 110 break; | 112 break; |
| (...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 // All done. | 297 // All done. |
| 296 key_ids->swap(local_key_ids); | 298 key_ids->swap(local_key_ids); |
| 297 error_message->clear(); | 299 error_message->clear(); |
| 298 return true; | 300 return true; |
| 299 } | 301 } |
| 300 | 302 |
| 301 void CreateLicenseRequest(const KeyIdList& key_ids, | 303 void CreateLicenseRequest(const KeyIdList& key_ids, |
| 302 CdmSessionType session_type, | 304 CdmSessionType session_type, |
| 303 std::vector<uint8_t>* license) { | 305 std::vector<uint8_t>* license) { |
| 304 // Create the license request. | 306 // Create the license request. |
| 305 std::unique_ptr<base::DictionaryValue> request(new base::DictionaryValue()); | 307 auto request = base::MakeUnique<base::DictionaryValue>(); |
| 306 std::unique_ptr<base::ListValue> list(new base::ListValue()); | 308 auto list = base::MakeUnique<base::ListValue>(); |
| 307 for (const auto& key_id : key_ids) { | 309 for (const auto& key_id : key_ids) { |
| 308 std::string key_id_string; | 310 std::string key_id_string; |
| 309 base::Base64UrlEncode( | 311 base::Base64UrlEncode( |
| 310 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), | 312 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), |
| 311 key_id.size()), | 313 key_id.size()), |
| 312 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); | 314 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); |
| 313 | 315 |
| 314 list->AppendString(key_id_string); | 316 list->AppendString(key_id_string); |
| 315 } | 317 } |
| 316 request->Set(kKeyIdsTag, list.release()); | 318 request->Set(kKeyIdsTag, std::move(list)); |
| 317 | 319 |
| 318 switch (session_type) { | 320 switch (session_type) { |
| 319 case CdmSessionType::TEMPORARY_SESSION: | 321 case CdmSessionType::TEMPORARY_SESSION: |
| 320 request->SetString(kTypeTag, kTemporarySession); | 322 request->SetString(kTypeTag, kTemporarySession); |
| 321 break; | 323 break; |
| 322 case CdmSessionType::PERSISTENT_LICENSE_SESSION: | 324 case CdmSessionType::PERSISTENT_LICENSE_SESSION: |
| 323 request->SetString(kTypeTag, kPersistentLicenseSession); | 325 request->SetString(kTypeTag, kPersistentLicenseSession); |
| 324 break; | 326 break; |
| 325 case CdmSessionType::PERSISTENT_RELEASE_MESSAGE_SESSION: | 327 case CdmSessionType::PERSISTENT_RELEASE_MESSAGE_SESSION: |
| 326 request->SetString(kTypeTag, kPersistentReleaseMessageSession); | 328 request->SetString(kTypeTag, kPersistentReleaseMessageSession); |
| 327 break; | 329 break; |
| 328 } | 330 } |
| 329 | 331 |
| 330 // Serialize the license request as a string. | 332 // Serialize the license request as a string. |
| 331 std::string json; | 333 std::string json; |
| 332 JSONStringValueSerializer serializer(&json); | 334 JSONStringValueSerializer serializer(&json); |
| 333 serializer.Serialize(*request); | 335 serializer.Serialize(*request); |
| 334 | 336 |
| 335 // Convert the serialized license request into std::vector and return it. | 337 // Convert the serialized license request into std::vector and return it. |
| 336 std::vector<uint8_t> result(json.begin(), json.end()); | 338 std::vector<uint8_t> result(json.begin(), json.end()); |
| 337 license->swap(result); | 339 license->swap(result); |
| 338 } | 340 } |
| 339 | 341 |
| 340 void CreateKeyIdsInitData(const KeyIdList& key_ids, | 342 void CreateKeyIdsInitData(const KeyIdList& key_ids, |
| 341 std::vector<uint8_t>* init_data) { | 343 std::vector<uint8_t>* init_data) { |
| 342 // Create the init_data. | 344 // Create the init_data. |
| 343 std::unique_ptr<base::DictionaryValue> dictionary( | 345 auto dictionary = base::MakeUnique<base::DictionaryValue>(); |
| 344 new base::DictionaryValue()); | 346 auto list = base::MakeUnique<base::ListValue>(); |
| 345 std::unique_ptr<base::ListValue> list(new base::ListValue()); | |
| 346 for (const auto& key_id : key_ids) { | 347 for (const auto& key_id : key_ids) { |
| 347 std::string key_id_string; | 348 std::string key_id_string; |
| 348 base::Base64UrlEncode( | 349 base::Base64UrlEncode( |
| 349 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), | 350 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), |
| 350 key_id.size()), | 351 key_id.size()), |
| 351 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); | 352 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); |
| 352 | 353 |
| 353 list->AppendString(key_id_string); | 354 list->AppendString(key_id_string); |
| 354 } | 355 } |
| 355 dictionary->Set(kKeyIdsTag, list.release()); | 356 dictionary->Set(kKeyIdsTag, std::move(list)); |
| 356 | 357 |
| 357 // Serialize the dictionary as a string. | 358 // Serialize the dictionary as a string. |
| 358 std::string json; | 359 std::string json; |
| 359 JSONStringValueSerializer serializer(&json); | 360 JSONStringValueSerializer serializer(&json); |
| 360 serializer.Serialize(*dictionary); | 361 serializer.Serialize(*dictionary); |
| 361 | 362 |
| 362 // Convert the serialized data into std::vector and return it. | 363 // Convert the serialized data into std::vector and return it. |
| 363 std::vector<uint8_t> result(json.begin(), json.end()); | 364 std::vector<uint8_t> result(json.begin(), json.end()); |
| 364 init_data->swap(result); | 365 init_data->swap(result); |
| 365 } | 366 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 410 DVLOG(1) << "Invalid '" << kKeyIdsTag << "' value: " << encoded_key; | 411 DVLOG(1) << "Invalid '" << kKeyIdsTag << "' value: " << encoded_key; |
| 411 return false; | 412 return false; |
| 412 } | 413 } |
| 413 | 414 |
| 414 std::vector<uint8_t> result(decoded_string.begin(), decoded_string.end()); | 415 std::vector<uint8_t> result(decoded_string.begin(), decoded_string.end()); |
| 415 first_key->swap(result); | 416 first_key->swap(result); |
| 416 return true; | 417 return true; |
| 417 } | 418 } |
| 418 | 419 |
| 419 } // namespace media | 420 } // namespace media |
| OLD | NEW |