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> |
| 10 |
9 #include "base/base64url.h" | 11 #include "base/base64url.h" |
10 #include "base/json/json_reader.h" | 12 #include "base/json/json_reader.h" |
11 #include "base/json/json_string_value_serializer.h" | 13 #include "base/json/json_string_value_serializer.h" |
12 #include "base/json/string_escape.h" | 14 #include "base/json/string_escape.h" |
13 #include "base/logging.h" | 15 #include "base/logging.h" |
14 #include "base/macros.h" | 16 #include "base/macros.h" |
15 #include "base/memory/scoped_ptr.h" | |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_piece.h" | 18 #include "base/strings/string_piece.h" |
18 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
19 #include "base/values.h" | 20 #include "base/values.h" |
20 | 21 |
21 namespace media { | 22 namespace media { |
22 | 23 |
23 const char kKeysTag[] = "keys"; | 24 const char kKeysTag[] = "keys"; |
24 const char kKeyTypeTag[] = "kty"; | 25 const char kKeyTypeTag[] = "kty"; |
25 const char kKeyTypeOct[] = "oct"; // Octet sequence. | 26 const char kKeyTypeOct[] = "oct"; // Octet sequence. |
(...skipping 13 matching lines...) Expand all Loading... |
39 std::string escaped_str = | 40 std::string escaped_str = |
40 base::EscapeBytesAsInvalidJSONString(input.substr(0, 65), false); | 41 base::EscapeBytesAsInvalidJSONString(input.substr(0, 65), false); |
41 if (escaped_str.length() <= 64u) | 42 if (escaped_str.length() <= 64u) |
42 return escaped_str; | 43 return escaped_str; |
43 | 44 |
44 // This may end up truncating an escaped character, but the first part of | 45 // This may end up truncating an escaped character, but the first part of |
45 // the string should provide enough information. | 46 // the string should provide enough information. |
46 return escaped_str.substr(0, 61).append("..."); | 47 return escaped_str.substr(0, 61).append("..."); |
47 } | 48 } |
48 | 49 |
49 static scoped_ptr<base::DictionaryValue> CreateJSONDictionary( | 50 static std::unique_ptr<base::DictionaryValue> CreateJSONDictionary( |
50 const uint8_t* key, | 51 const uint8_t* key, |
51 int key_length, | 52 int key_length, |
52 const uint8_t* key_id, | 53 const uint8_t* key_id, |
53 int key_id_length) { | 54 int key_id_length) { |
54 std::string key_string, key_id_string; | 55 std::string key_string, key_id_string; |
55 base::Base64UrlEncode( | 56 base::Base64UrlEncode( |
56 base::StringPiece(reinterpret_cast<const char*>(key), key_length), | 57 base::StringPiece(reinterpret_cast<const char*>(key), key_length), |
57 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_string); | 58 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_string); |
58 base::Base64UrlEncode( | 59 base::Base64UrlEncode( |
59 base::StringPiece(reinterpret_cast<const char*>(key_id), key_id_length), | 60 base::StringPiece(reinterpret_cast<const char*>(key_id), key_id_length), |
60 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); | 61 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); |
61 | 62 |
62 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue()); | 63 std::unique_ptr<base::DictionaryValue> jwk(new base::DictionaryValue()); |
63 jwk->SetString(kKeyTypeTag, kKeyTypeOct); | 64 jwk->SetString(kKeyTypeTag, kKeyTypeOct); |
64 jwk->SetString(kKeyTag, key_string); | 65 jwk->SetString(kKeyTag, key_string); |
65 jwk->SetString(kKeyIdTag, key_id_string); | 66 jwk->SetString(kKeyIdTag, key_id_string); |
66 return jwk; | 67 return jwk; |
67 } | 68 } |
68 | 69 |
69 std::string GenerateJWKSet(const uint8_t* key, | 70 std::string GenerateJWKSet(const uint8_t* key, |
70 int key_length, | 71 int key_length, |
71 const uint8_t* key_id, | 72 const uint8_t* key_id, |
72 int key_id_length) { | 73 int key_id_length) { |
73 // Create the JWK, and wrap it into a JWK Set. | 74 // Create the JWK, and wrap it into a JWK Set. |
74 scoped_ptr<base::ListValue> list(new base::ListValue()); | 75 std::unique_ptr<base::ListValue> list(new base::ListValue()); |
75 list->Append( | 76 list->Append( |
76 CreateJSONDictionary(key, key_length, key_id, key_id_length).release()); | 77 CreateJSONDictionary(key, key_length, key_id, key_id_length).release()); |
77 base::DictionaryValue jwk_set; | 78 base::DictionaryValue jwk_set; |
78 jwk_set.Set(kKeysTag, list.release()); | 79 jwk_set.Set(kKeysTag, list.release()); |
79 | 80 |
80 // Finally serialize |jwk_set| into a string and return it. | 81 // Finally serialize |jwk_set| into a string and return it. |
81 std::string serialized_jwk; | 82 std::string serialized_jwk; |
82 JSONStringValueSerializer serializer(&serialized_jwk); | 83 JSONStringValueSerializer serializer(&serialized_jwk); |
83 serializer.Serialize(jwk_set); | 84 serializer.Serialize(jwk_set); |
84 return serialized_jwk; | 85 return serialized_jwk; |
85 } | 86 } |
86 | 87 |
87 std::string GenerateJWKSet(const KeyIdAndKeyPairs& keys, | 88 std::string GenerateJWKSet(const KeyIdAndKeyPairs& keys, |
88 MediaKeys::SessionType session_type) { | 89 MediaKeys::SessionType session_type) { |
89 scoped_ptr<base::ListValue> list(new base::ListValue()); | 90 std::unique_ptr<base::ListValue> list(new base::ListValue()); |
90 for (const auto& key_pair : keys) { | 91 for (const auto& key_pair : keys) { |
91 list->Append(CreateJSONDictionary( | 92 list->Append(CreateJSONDictionary( |
92 reinterpret_cast<const uint8_t*>(key_pair.second.data()), | 93 reinterpret_cast<const uint8_t*>(key_pair.second.data()), |
93 key_pair.second.length(), | 94 key_pair.second.length(), |
94 reinterpret_cast<const uint8_t*>(key_pair.first.data()), | 95 reinterpret_cast<const uint8_t*>(key_pair.first.data()), |
95 key_pair.first.length()) | 96 key_pair.first.length()) |
96 .release()); | 97 .release()); |
97 } | 98 } |
98 | 99 |
99 base::DictionaryValue jwk_set; | 100 base::DictionaryValue jwk_set; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
164 } | 165 } |
165 | 166 |
166 bool ExtractKeysFromJWKSet(const std::string& jwk_set, | 167 bool ExtractKeysFromJWKSet(const std::string& jwk_set, |
167 KeyIdAndKeyPairs* keys, | 168 KeyIdAndKeyPairs* keys, |
168 MediaKeys::SessionType* session_type) { | 169 MediaKeys::SessionType* session_type) { |
169 if (!base::IsStringASCII(jwk_set)) { | 170 if (!base::IsStringASCII(jwk_set)) { |
170 DVLOG(1) << "Non ASCII JWK Set: " << jwk_set; | 171 DVLOG(1) << "Non ASCII JWK Set: " << jwk_set; |
171 return false; | 172 return false; |
172 } | 173 } |
173 | 174 |
174 scoped_ptr<base::Value> root(base::JSONReader().ReadToValue(jwk_set)); | 175 std::unique_ptr<base::Value> root(base::JSONReader().ReadToValue(jwk_set)); |
175 if (!root.get() || root->GetType() != base::Value::TYPE_DICTIONARY) { | 176 if (!root.get() || root->GetType() != base::Value::TYPE_DICTIONARY) { |
176 DVLOG(1) << "Not valid JSON: " << jwk_set << ", root: " << root.get(); | 177 DVLOG(1) << "Not valid JSON: " << jwk_set << ", root: " << root.get(); |
177 return false; | 178 return false; |
178 } | 179 } |
179 | 180 |
180 // Locate the set from the dictionary. | 181 // Locate the set from the dictionary. |
181 base::DictionaryValue* dictionary = | 182 base::DictionaryValue* dictionary = |
182 static_cast<base::DictionaryValue*>(root.get()); | 183 static_cast<base::DictionaryValue*>(root.get()); |
183 base::ListValue* list_val = NULL; | 184 base::ListValue* list_val = NULL; |
184 if (!dictionary->GetList(kKeysTag, &list_val)) { | 185 if (!dictionary->GetList(kKeysTag, &list_val)) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 | 234 |
234 bool ExtractKeyIdsFromKeyIdsInitData(const std::string& input, | 235 bool ExtractKeyIdsFromKeyIdsInitData(const std::string& input, |
235 KeyIdList* key_ids, | 236 KeyIdList* key_ids, |
236 std::string* error_message) { | 237 std::string* error_message) { |
237 if (!base::IsStringASCII(input)) { | 238 if (!base::IsStringASCII(input)) { |
238 error_message->assign("Non ASCII: "); | 239 error_message->assign("Non ASCII: "); |
239 error_message->append(ShortenTo64Characters(input)); | 240 error_message->append(ShortenTo64Characters(input)); |
240 return false; | 241 return false; |
241 } | 242 } |
242 | 243 |
243 scoped_ptr<base::Value> root(base::JSONReader().ReadToValue(input)); | 244 std::unique_ptr<base::Value> root(base::JSONReader().ReadToValue(input)); |
244 if (!root.get() || root->GetType() != base::Value::TYPE_DICTIONARY) { | 245 if (!root.get() || root->GetType() != base::Value::TYPE_DICTIONARY) { |
245 error_message->assign("Not valid JSON: "); | 246 error_message->assign("Not valid JSON: "); |
246 error_message->append(ShortenTo64Characters(input)); | 247 error_message->append(ShortenTo64Characters(input)); |
247 return false; | 248 return false; |
248 } | 249 } |
249 | 250 |
250 // Locate the set from the dictionary. | 251 // Locate the set from the dictionary. |
251 base::DictionaryValue* dictionary = | 252 base::DictionaryValue* dictionary = |
252 static_cast<base::DictionaryValue*>(root.get()); | 253 static_cast<base::DictionaryValue*>(root.get()); |
253 base::ListValue* list_val = NULL; | 254 base::ListValue* list_val = NULL; |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 // All done. | 296 // All done. |
296 key_ids->swap(local_key_ids); | 297 key_ids->swap(local_key_ids); |
297 error_message->clear(); | 298 error_message->clear(); |
298 return true; | 299 return true; |
299 } | 300 } |
300 | 301 |
301 void CreateLicenseRequest(const KeyIdList& key_ids, | 302 void CreateLicenseRequest(const KeyIdList& key_ids, |
302 MediaKeys::SessionType session_type, | 303 MediaKeys::SessionType session_type, |
303 std::vector<uint8_t>* license) { | 304 std::vector<uint8_t>* license) { |
304 // Create the license request. | 305 // Create the license request. |
305 scoped_ptr<base::DictionaryValue> request(new base::DictionaryValue()); | 306 std::unique_ptr<base::DictionaryValue> request(new base::DictionaryValue()); |
306 scoped_ptr<base::ListValue> list(new base::ListValue()); | 307 std::unique_ptr<base::ListValue> list(new base::ListValue()); |
307 for (const auto& key_id : key_ids) { | 308 for (const auto& key_id : key_ids) { |
308 std::string key_id_string; | 309 std::string key_id_string; |
309 base::Base64UrlEncode( | 310 base::Base64UrlEncode( |
310 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), | 311 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), |
311 key_id.size()), | 312 key_id.size()), |
312 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); | 313 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); |
313 | 314 |
314 list->AppendString(key_id_string); | 315 list->AppendString(key_id_string); |
315 } | 316 } |
316 request->Set(kKeyIdsTag, list.release()); | 317 request->Set(kKeyIdsTag, list.release()); |
(...skipping 16 matching lines...) Expand all Loading... |
333 serializer.Serialize(*request); | 334 serializer.Serialize(*request); |
334 | 335 |
335 // Convert the serialized license request into std::vector and return it. | 336 // Convert the serialized license request into std::vector and return it. |
336 std::vector<uint8_t> result(json.begin(), json.end()); | 337 std::vector<uint8_t> result(json.begin(), json.end()); |
337 license->swap(result); | 338 license->swap(result); |
338 } | 339 } |
339 | 340 |
340 void CreateKeyIdsInitData(const KeyIdList& key_ids, | 341 void CreateKeyIdsInitData(const KeyIdList& key_ids, |
341 std::vector<uint8_t>* init_data) { | 342 std::vector<uint8_t>* init_data) { |
342 // Create the init_data. | 343 // Create the init_data. |
343 scoped_ptr<base::DictionaryValue> dictionary(new base::DictionaryValue()); | 344 std::unique_ptr<base::DictionaryValue> dictionary( |
344 scoped_ptr<base::ListValue> list(new base::ListValue()); | 345 new base::DictionaryValue()); |
| 346 std::unique_ptr<base::ListValue> list(new base::ListValue()); |
345 for (const auto& key_id : key_ids) { | 347 for (const auto& key_id : key_ids) { |
346 std::string key_id_string; | 348 std::string key_id_string; |
347 base::Base64UrlEncode( | 349 base::Base64UrlEncode( |
348 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), | 350 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), |
349 key_id.size()), | 351 key_id.size()), |
350 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); | 352 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); |
351 | 353 |
352 list->AppendString(key_id_string); | 354 list->AppendString(key_id_string); |
353 } | 355 } |
354 dictionary->Set(kKeyIdsTag, list.release()); | 356 dictionary->Set(kKeyIdsTag, list.release()); |
(...skipping 11 matching lines...) Expand all Loading... |
366 bool ExtractFirstKeyIdFromLicenseRequest(const std::vector<uint8_t>& license, | 368 bool ExtractFirstKeyIdFromLicenseRequest(const std::vector<uint8_t>& license, |
367 std::vector<uint8_t>* first_key) { | 369 std::vector<uint8_t>* first_key) { |
368 const std::string license_as_str( | 370 const std::string license_as_str( |
369 reinterpret_cast<const char*>(!license.empty() ? &license[0] : NULL), | 371 reinterpret_cast<const char*>(!license.empty() ? &license[0] : NULL), |
370 license.size()); | 372 license.size()); |
371 if (!base::IsStringASCII(license_as_str)) { | 373 if (!base::IsStringASCII(license_as_str)) { |
372 DVLOG(1) << "Non ASCII license: " << license_as_str; | 374 DVLOG(1) << "Non ASCII license: " << license_as_str; |
373 return false; | 375 return false; |
374 } | 376 } |
375 | 377 |
376 scoped_ptr<base::Value> root(base::JSONReader().ReadToValue(license_as_str)); | 378 std::unique_ptr<base::Value> root( |
| 379 base::JSONReader().ReadToValue(license_as_str)); |
377 if (!root.get() || root->GetType() != base::Value::TYPE_DICTIONARY) { | 380 if (!root.get() || root->GetType() != base::Value::TYPE_DICTIONARY) { |
378 DVLOG(1) << "Not valid JSON: " << license_as_str; | 381 DVLOG(1) << "Not valid JSON: " << license_as_str; |
379 return false; | 382 return false; |
380 } | 383 } |
381 | 384 |
382 // Locate the set from the dictionary. | 385 // Locate the set from the dictionary. |
383 base::DictionaryValue* dictionary = | 386 base::DictionaryValue* dictionary = |
384 static_cast<base::DictionaryValue*>(root.get()); | 387 static_cast<base::DictionaryValue*>(root.get()); |
385 base::ListValue* list_val = NULL; | 388 base::ListValue* list_val = NULL; |
386 if (!dictionary->GetList(kKeyIdsTag, &list_val)) { | 389 if (!dictionary->GetList(kKeyIdsTag, &list_val)) { |
(...skipping 21 matching lines...) Expand all Loading... |
408 DVLOG(1) << "Invalid '" << kKeyIdsTag << "' value: " << encoded_key; | 411 DVLOG(1) << "Invalid '" << kKeyIdsTag << "' value: " << encoded_key; |
409 return false; | 412 return false; |
410 } | 413 } |
411 | 414 |
412 std::vector<uint8_t> result(decoded_string.begin(), decoded_string.end()); | 415 std::vector<uint8_t> result(decoded_string.begin(), decoded_string.end()); |
413 first_key->swap(result); | 416 first_key->swap(result); |
414 return true; | 417 return true; |
415 } | 418 } |
416 | 419 |
417 } // namespace media | 420 } // namespace media |
OLD | NEW |