Chromium Code Reviews| 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/base64.h" | 7 #include "base/base64.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" |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/string_number_conversions.h" | |
| 13 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 14 #include "base/values.h" | 15 #include "base/values.h" |
| 15 | 16 |
| 16 namespace media { | 17 namespace media { |
| 17 | 18 |
| 18 const char kKeysTag[] = "keys"; | 19 const char kKeysTag[] = "keys"; |
| 19 const char kKeyTypeTag[] = "kty"; | 20 const char kKeyTypeTag[] = "kty"; |
| 20 const char kKeyTypeOct[] = "oct"; // Octet sequence. | 21 const char kKeyTypeOct[] = "oct"; // Octet sequence. |
| 21 const char kAlgTag[] = "alg"; | 22 const char kAlgTag[] = "alg"; |
| 22 const char kAlgA128KW[] = "A128KW"; // AES key wrap using a 128-bit key. | 23 const char kAlgA128KW[] = "A128KW"; // AES key wrap using a 128-bit key. |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 219 } else { | 220 } else { |
| 220 DVLOG(1) << "Invalid '" << kTypeTag << "' value: " << session_type_id; | 221 DVLOG(1) << "Invalid '" << kTypeTag << "' value: " << session_type_id; |
| 221 return false; | 222 return false; |
| 222 } | 223 } |
| 223 | 224 |
| 224 // All done. | 225 // All done. |
| 225 keys->swap(local_keys); | 226 keys->swap(local_keys); |
| 226 return true; | 227 return true; |
| 227 } | 228 } |
| 228 | 229 |
| 230 bool ExtractKeyIdsFromKeyIdsInitData(const std::string& input, | |
| 231 KeyIdList* key_ids, | |
| 232 std::string* error_message) { | |
| 233 if (!base::IsStringASCII(input)) { | |
| 234 error_message->assign("Non ASCII: "); | |
| 235 error_message->append(input); | |
|
sandersd (OOO until July 31)
2015/03/04 01:04:21
Is it wise to have arbitrarily large error message
jrummell
2015/03/04 01:28:52
Good catch. Done.
| |
| 236 return false; | |
| 237 } | |
| 238 | |
| 239 scoped_ptr<base::Value> root(base::JSONReader().ReadToValue(input)); | |
| 240 if (!root.get() || root->GetType() != base::Value::TYPE_DICTIONARY) { | |
| 241 error_message->assign("Not valid JSON: "); | |
| 242 error_message->append(input); | |
| 243 return false; | |
| 244 } | |
| 245 | |
| 246 // Locate the set from the dictionary. | |
| 247 base::DictionaryValue* dictionary = | |
| 248 static_cast<base::DictionaryValue*>(root.get()); | |
| 249 base::ListValue* list_val = NULL; | |
| 250 if (!dictionary->GetList(kKeyIdsTag, &list_val)) { | |
| 251 error_message->assign("Missing '"); | |
| 252 error_message->append(kKeyIdsTag); | |
| 253 error_message->append("' parameter or not a list"); | |
| 254 return false; | |
| 255 } | |
| 256 | |
| 257 // Create a local list of key ids, so that |key_ids| only gets updated on | |
| 258 // success. | |
| 259 KeyIdList local_key_ids; | |
| 260 for (size_t i = 0; i < list_val->GetSize(); ++i) { | |
| 261 std::string encoded_key_id; | |
| 262 if (!list_val->GetString(i, &encoded_key_id)) { | |
| 263 error_message->assign("Unable to access '"); | |
|
sandersd (OOO until July 31)
2015/03/04 01:04:21
Presumably this happens only when the value at tha
jrummell
2015/03/04 01:28:52
Done.
| |
| 264 error_message->append(kKeyIdsTag); | |
| 265 error_message->append("'["); | |
| 266 error_message->append(base::UintToString(i)); | |
| 267 error_message->append("]"); | |
| 268 return false; | |
| 269 } | |
| 270 | |
| 271 // Key ID is a base64-encoded string, so decode it. | |
| 272 std::string raw_key_id = DecodeBase64Url(encoded_key_id); | |
| 273 if (raw_key_id.empty()) { | |
| 274 error_message->assign("'"); | |
| 275 error_message->append(kKeyIdsTag); | |
| 276 error_message->append("'["); | |
| 277 error_message->append(base::UintToString(i)); | |
| 278 error_message->append("] is not valid base64url encoded. Value: "); | |
| 279 error_message->append(encoded_key_id); | |
| 280 return false; | |
| 281 } | |
| 282 | |
| 283 // Add the decoded key ID to the list. | |
| 284 local_key_ids.push_back(std::vector<uint8>( | |
| 285 raw_key_id.data(), raw_key_id.data() + raw_key_id.length())); | |
| 286 } | |
| 287 | |
| 288 // All done. | |
| 289 key_ids->swap(local_key_ids); | |
| 290 error_message->clear(); | |
| 291 return true; | |
| 292 } | |
| 293 | |
| 229 void CreateLicenseRequest(const KeyIdList& key_ids, | 294 void CreateLicenseRequest(const KeyIdList& key_ids, |
| 230 MediaKeys::SessionType session_type, | 295 MediaKeys::SessionType session_type, |
| 231 std::vector<uint8>* license) { | 296 std::vector<uint8>* license) { |
| 232 // Create the license request. | 297 // Create the license request. |
| 233 scoped_ptr<base::DictionaryValue> request(new base::DictionaryValue()); | 298 scoped_ptr<base::DictionaryValue> request(new base::DictionaryValue()); |
| 234 scoped_ptr<base::ListValue> list(new base::ListValue()); | 299 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 235 for (const auto& key_id : key_ids) | 300 for (const auto& key_id : key_ids) |
| 236 list->AppendString(EncodeBase64Url(&key_id[0], key_id.size())); | 301 list->AppendString(EncodeBase64Url(&key_id[0], key_id.size())); |
| 237 request->Set(kKeyIdsTag, list.release()); | 302 request->Set(kKeyIdsTag, list.release()); |
| 238 | 303 |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 DVLOG(1) << "Invalid '" << kKeyIdsTag << "' value: " << encoded_key; | 365 DVLOG(1) << "Invalid '" << kKeyIdsTag << "' value: " << encoded_key; |
| 301 return false; | 366 return false; |
| 302 } | 367 } |
| 303 | 368 |
| 304 std::vector<uint8> result(decoded_string.begin(), decoded_string.end()); | 369 std::vector<uint8> result(decoded_string.begin(), decoded_string.end()); |
| 305 first_key->swap(result); | 370 first_key->swap(result); |
| 306 return true; | 371 return true; |
| 307 } | 372 } |
| 308 | 373 |
| 309 } // namespace media | 374 } // namespace media |
| OLD | NEW |