| 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 "base/base64url.h" | 7 #include "base/base64url.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/json/json_string_value_serializer.h" | 9 #include "base/json/json_string_value_serializer.h" |
| 10 #include "base/json/string_escape.h" | 10 #include "base/json/string_escape.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 base::EscapeBytesAsInvalidJSONString(input.substr(0, 65), false); | 37 base::EscapeBytesAsInvalidJSONString(input.substr(0, 65), false); |
| 38 if (escaped_str.length() <= 64u) | 38 if (escaped_str.length() <= 64u) |
| 39 return escaped_str; | 39 return escaped_str; |
| 40 | 40 |
| 41 // This may end up truncating an escaped character, but the first part of | 41 // This may end up truncating an escaped character, but the first part of |
| 42 // the string should provide enough information. | 42 // the string should provide enough information. |
| 43 return escaped_str.substr(0, 61).append("..."); | 43 return escaped_str.substr(0, 61).append("..."); |
| 44 } | 44 } |
| 45 | 45 |
| 46 static scoped_ptr<base::DictionaryValue> CreateJSONDictionary( | 46 static scoped_ptr<base::DictionaryValue> CreateJSONDictionary( |
| 47 const uint8* key, | 47 const uint8_t* key, |
| 48 int key_length, | 48 int key_length, |
| 49 const uint8* key_id, | 49 const uint8_t* key_id, |
| 50 int key_id_length) { | 50 int key_id_length) { |
| 51 std::string key_string, key_id_string; | 51 std::string key_string, key_id_string; |
| 52 base::Base64UrlEncode( | 52 base::Base64UrlEncode( |
| 53 base::StringPiece(reinterpret_cast<const char*>(key), key_length), | 53 base::StringPiece(reinterpret_cast<const char*>(key), key_length), |
| 54 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_string); | 54 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_string); |
| 55 base::Base64UrlEncode( | 55 base::Base64UrlEncode( |
| 56 base::StringPiece(reinterpret_cast<const char*>(key_id), key_id_length), | 56 base::StringPiece(reinterpret_cast<const char*>(key_id), key_id_length), |
| 57 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); | 57 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); |
| 58 | 58 |
| 59 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue()); | 59 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue()); |
| 60 jwk->SetString(kKeyTypeTag, kKeyTypeOct); | 60 jwk->SetString(kKeyTypeTag, kKeyTypeOct); |
| 61 jwk->SetString(kKeyTag, key_string); | 61 jwk->SetString(kKeyTag, key_string); |
| 62 jwk->SetString(kKeyIdTag, key_id_string); | 62 jwk->SetString(kKeyIdTag, key_id_string); |
| 63 return jwk.Pass(); | 63 return jwk.Pass(); |
| 64 } | 64 } |
| 65 | 65 |
| 66 std::string GenerateJWKSet(const uint8* key, int key_length, | 66 std::string GenerateJWKSet(const uint8_t* key, |
| 67 const uint8* key_id, int key_id_length) { | 67 int key_length, |
| 68 const uint8_t* key_id, |
| 69 int key_id_length) { |
| 68 // Create the JWK, and wrap it into a JWK Set. | 70 // Create the JWK, and wrap it into a JWK Set. |
| 69 scoped_ptr<base::ListValue> list(new base::ListValue()); | 71 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 70 list->Append( | 72 list->Append( |
| 71 CreateJSONDictionary(key, key_length, key_id, key_id_length).release()); | 73 CreateJSONDictionary(key, key_length, key_id, key_id_length).release()); |
| 72 base::DictionaryValue jwk_set; | 74 base::DictionaryValue jwk_set; |
| 73 jwk_set.Set(kKeysTag, list.release()); | 75 jwk_set.Set(kKeysTag, list.release()); |
| 74 | 76 |
| 75 // Finally serialize |jwk_set| into a string and return it. | 77 // Finally serialize |jwk_set| into a string and return it. |
| 76 std::string serialized_jwk; | 78 std::string serialized_jwk; |
| 77 JSONStringValueSerializer serializer(&serialized_jwk); | 79 JSONStringValueSerializer serializer(&serialized_jwk); |
| 78 serializer.Serialize(jwk_set); | 80 serializer.Serialize(jwk_set); |
| 79 return serialized_jwk; | 81 return serialized_jwk; |
| 80 } | 82 } |
| 81 | 83 |
| 82 std::string GenerateJWKSet(const KeyIdAndKeyPairs& keys, | 84 std::string GenerateJWKSet(const KeyIdAndKeyPairs& keys, |
| 83 MediaKeys::SessionType session_type) { | 85 MediaKeys::SessionType session_type) { |
| 84 scoped_ptr<base::ListValue> list(new base::ListValue()); | 86 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 85 for (const auto& key_pair : keys) { | 87 for (const auto& key_pair : keys) { |
| 86 list->Append(CreateJSONDictionary( | 88 list->Append(CreateJSONDictionary( |
| 87 reinterpret_cast<const uint8*>(key_pair.second.data()), | 89 reinterpret_cast<const uint8_t*>(key_pair.second.data()), |
| 88 key_pair.second.length(), | 90 key_pair.second.length(), |
| 89 reinterpret_cast<const uint8*>(key_pair.first.data()), | 91 reinterpret_cast<const uint8_t*>(key_pair.first.data()), |
| 90 key_pair.first.length()) | 92 key_pair.first.length()) |
| 91 .release()); | 93 .release()); |
| 92 } | 94 } |
| 93 | 95 |
| 94 base::DictionaryValue jwk_set; | 96 base::DictionaryValue jwk_set; |
| 95 jwk_set.Set(kKeysTag, list.release()); | 97 jwk_set.Set(kKeysTag, list.release()); |
| 96 switch (session_type) { | 98 switch (session_type) { |
| 97 case MediaKeys::TEMPORARY_SESSION: | 99 case MediaKeys::TEMPORARY_SESSION: |
| 98 jwk_set.SetString(kTypeTag, kTemporarySession); | 100 jwk_set.SetString(kTypeTag, kTemporarySession); |
| 99 break; | 101 break; |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 error_message->assign("'"); | 278 error_message->assign("'"); |
| 277 error_message->append(kKeyIdsTag); | 279 error_message->append(kKeyIdsTag); |
| 278 error_message->append("'["); | 280 error_message->append("'["); |
| 279 error_message->append(base::SizeTToString(i)); | 281 error_message->append(base::SizeTToString(i)); |
| 280 error_message->append("] is not valid base64url encoded. Value: "); | 282 error_message->append("] is not valid base64url encoded. Value: "); |
| 281 error_message->append(ShortenTo64Characters(encoded_key_id)); | 283 error_message->append(ShortenTo64Characters(encoded_key_id)); |
| 282 return false; | 284 return false; |
| 283 } | 285 } |
| 284 | 286 |
| 285 // Add the decoded key ID to the list. | 287 // Add the decoded key ID to the list. |
| 286 local_key_ids.push_back(std::vector<uint8>( | 288 local_key_ids.push_back(std::vector<uint8_t>( |
| 287 raw_key_id.data(), raw_key_id.data() + raw_key_id.length())); | 289 raw_key_id.data(), raw_key_id.data() + raw_key_id.length())); |
| 288 } | 290 } |
| 289 | 291 |
| 290 // All done. | 292 // All done. |
| 291 key_ids->swap(local_key_ids); | 293 key_ids->swap(local_key_ids); |
| 292 error_message->clear(); | 294 error_message->clear(); |
| 293 return true; | 295 return true; |
| 294 } | 296 } |
| 295 | 297 |
| 296 void CreateLicenseRequest(const KeyIdList& key_ids, | 298 void CreateLicenseRequest(const KeyIdList& key_ids, |
| 297 MediaKeys::SessionType session_type, | 299 MediaKeys::SessionType session_type, |
| 298 std::vector<uint8>* license) { | 300 std::vector<uint8_t>* license) { |
| 299 // Create the license request. | 301 // Create the license request. |
| 300 scoped_ptr<base::DictionaryValue> request(new base::DictionaryValue()); | 302 scoped_ptr<base::DictionaryValue> request(new base::DictionaryValue()); |
| 301 scoped_ptr<base::ListValue> list(new base::ListValue()); | 303 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 302 for (const auto& key_id : key_ids) { | 304 for (const auto& key_id : key_ids) { |
| 303 std::string key_id_string; | 305 std::string key_id_string; |
| 304 base::Base64UrlEncode( | 306 base::Base64UrlEncode( |
| 305 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), | 307 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), |
| 306 key_id.size()), | 308 key_id.size()), |
| 307 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); | 309 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); |
| 308 | 310 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 321 request->SetString(kTypeTag, kPersistentReleaseMessageSession); | 323 request->SetString(kTypeTag, kPersistentReleaseMessageSession); |
| 322 break; | 324 break; |
| 323 } | 325 } |
| 324 | 326 |
| 325 // Serialize the license request as a string. | 327 // Serialize the license request as a string. |
| 326 std::string json; | 328 std::string json; |
| 327 JSONStringValueSerializer serializer(&json); | 329 JSONStringValueSerializer serializer(&json); |
| 328 serializer.Serialize(*request); | 330 serializer.Serialize(*request); |
| 329 | 331 |
| 330 // Convert the serialized license request into std::vector and return it. | 332 // Convert the serialized license request into std::vector and return it. |
| 331 std::vector<uint8> result(json.begin(), json.end()); | 333 std::vector<uint8_t> result(json.begin(), json.end()); |
| 332 license->swap(result); | 334 license->swap(result); |
| 333 } | 335 } |
| 334 | 336 |
| 335 void CreateKeyIdsInitData(const KeyIdList& key_ids, | 337 void CreateKeyIdsInitData(const KeyIdList& key_ids, |
| 336 std::vector<uint8>* init_data) { | 338 std::vector<uint8_t>* init_data) { |
| 337 // Create the init_data. | 339 // Create the init_data. |
| 338 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue()); | 340 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue()); |
| 339 scoped_ptr<base::ListValue> list(new base::ListValue()); | 341 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 340 for (const auto& key_id : key_ids) { | 342 for (const auto& key_id : key_ids) { |
| 341 std::string key_id_string; | 343 std::string key_id_string; |
| 342 base::Base64UrlEncode( | 344 base::Base64UrlEncode( |
| 343 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), | 345 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), |
| 344 key_id.size()), | 346 key_id.size()), |
| 345 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); | 347 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); |
| 346 | 348 |
| 347 list->AppendString(key_id_string); | 349 list->AppendString(key_id_string); |
| 348 } | 350 } |
| 349 dictionary->Set(kKeyIdsTag, list.release()); | 351 dictionary->Set(kKeyIdsTag, list.release()); |
| 350 | 352 |
| 351 // Serialize the dictionary as a string. | 353 // Serialize the dictionary as a string. |
| 352 std::string json; | 354 std::string json; |
| 353 JSONStringValueSerializer serializer(&json); | 355 JSONStringValueSerializer serializer(&json); |
| 354 serializer.Serialize(*dictionary); | 356 serializer.Serialize(*dictionary); |
| 355 | 357 |
| 356 // Convert the serialized data into std::vector and return it. | 358 // Convert the serialized data into std::vector and return it. |
| 357 std::vector<uint8> result(json.begin(), json.end()); | 359 std::vector<uint8_t> result(json.begin(), json.end()); |
| 358 init_data->swap(result); | 360 init_data->swap(result); |
| 359 } | 361 } |
| 360 | 362 |
| 361 bool ExtractFirstKeyIdFromLicenseRequest(const std::vector<uint8>& license, | 363 bool ExtractFirstKeyIdFromLicenseRequest(const std::vector<uint8_t>& license, |
| 362 std::vector<uint8>* first_key) { | 364 std::vector<uint8_t>* first_key) { |
| 363 const std::string license_as_str( | 365 const std::string license_as_str( |
| 364 reinterpret_cast<const char*>(!license.empty() ? &license[0] : NULL), | 366 reinterpret_cast<const char*>(!license.empty() ? &license[0] : NULL), |
| 365 license.size()); | 367 license.size()); |
| 366 if (!base::IsStringASCII(license_as_str)) { | 368 if (!base::IsStringASCII(license_as_str)) { |
| 367 DVLOG(1) << "Non ASCII license: " << license_as_str; | 369 DVLOG(1) << "Non ASCII license: " << license_as_str; |
| 368 return false; | 370 return false; |
| 369 } | 371 } |
| 370 | 372 |
| 371 scoped_ptr<base::Value> root(base::JSONReader().ReadToValue(license_as_str)); | 373 scoped_ptr<base::Value> root(base::JSONReader().ReadToValue(license_as_str)); |
| 372 if (!root.get() || root->GetType() != base::Value::TYPE_DICTIONARY) { | 374 if (!root.get() || root->GetType() != base::Value::TYPE_DICTIONARY) { |
| (...skipping 24 matching lines...) Expand all Loading... |
| 397 | 399 |
| 398 std::string decoded_string; | 400 std::string decoded_string; |
| 399 if (!base::Base64UrlDecode(encoded_key, | 401 if (!base::Base64UrlDecode(encoded_key, |
| 400 base::Base64UrlDecodePolicy::DISALLOW_PADDING, | 402 base::Base64UrlDecodePolicy::DISALLOW_PADDING, |
| 401 &decoded_string) || | 403 &decoded_string) || |
| 402 decoded_string.empty()) { | 404 decoded_string.empty()) { |
| 403 DVLOG(1) << "Invalid '" << kKeyIdsTag << "' value: " << encoded_key; | 405 DVLOG(1) << "Invalid '" << kKeyIdsTag << "' value: " << encoded_key; |
| 404 return false; | 406 return false; |
| 405 } | 407 } |
| 406 | 408 |
| 407 std::vector<uint8> result(decoded_string.begin(), decoded_string.end()); | 409 std::vector<uint8_t> result(decoded_string.begin(), decoded_string.end()); |
| 408 first_key->swap(result); | 410 first_key->swap(result); |
| 409 return true; | 411 return true; |
| 410 } | 412 } |
| 411 | 413 |
| 412 } // namespace media | 414 } // namespace media |
| OLD | NEW |