| 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 | 10 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 78 jwk_set.Set(kKeysTag, list.release()); | 78 jwk_set.Set(kKeysTag, list.release()); |
| 79 | 79 |
| 80 // Finally serialize |jwk_set| into a string and return it. | 80 // Finally serialize |jwk_set| into a string and return it. |
| 81 std::string serialized_jwk; | 81 std::string serialized_jwk; |
| 82 JSONStringValueSerializer serializer(&serialized_jwk); | 82 JSONStringValueSerializer serializer(&serialized_jwk); |
| 83 serializer.Serialize(jwk_set); | 83 serializer.Serialize(jwk_set); |
| 84 return serialized_jwk; | 84 return serialized_jwk; |
| 85 } | 85 } |
| 86 | 86 |
| 87 std::string GenerateJWKSet(const KeyIdAndKeyPairs& keys, | 87 std::string GenerateJWKSet(const KeyIdAndKeyPairs& keys, |
| 88 MediaKeys::SessionType session_type) { | 88 ContentDecryptionModule::SessionType session_type) { |
| 89 std::unique_ptr<base::ListValue> list(new base::ListValue()); | 89 std::unique_ptr<base::ListValue> list(new base::ListValue()); |
| 90 for (const auto& key_pair : keys) { | 90 for (const auto& key_pair : keys) { |
| 91 list->Append(CreateJSONDictionary( | 91 list->Append(CreateJSONDictionary( |
| 92 reinterpret_cast<const uint8_t*>(key_pair.second.data()), | 92 reinterpret_cast<const uint8_t*>(key_pair.second.data()), |
| 93 key_pair.second.length(), | 93 key_pair.second.length(), |
| 94 reinterpret_cast<const uint8_t*>(key_pair.first.data()), | 94 reinterpret_cast<const uint8_t*>(key_pair.first.data()), |
| 95 key_pair.first.length())); | 95 key_pair.first.length())); |
| 96 } | 96 } |
| 97 | 97 |
| 98 base::DictionaryValue jwk_set; | 98 base::DictionaryValue jwk_set; |
| 99 jwk_set.Set(kKeysTag, list.release()); | 99 jwk_set.Set(kKeysTag, list.release()); |
| 100 switch (session_type) { | 100 switch (session_type) { |
| 101 case MediaKeys::TEMPORARY_SESSION: | 101 case ContentDecryptionModule::TEMPORARY_SESSION: |
| 102 jwk_set.SetString(kTypeTag, kTemporarySession); | 102 jwk_set.SetString(kTypeTag, kTemporarySession); |
| 103 break; | 103 break; |
| 104 case MediaKeys::PERSISTENT_LICENSE_SESSION: | 104 case ContentDecryptionModule::PERSISTENT_LICENSE_SESSION: |
| 105 jwk_set.SetString(kTypeTag, kPersistentLicenseSession); | 105 jwk_set.SetString(kTypeTag, kPersistentLicenseSession); |
| 106 break; | 106 break; |
| 107 case MediaKeys::PERSISTENT_RELEASE_MESSAGE_SESSION: | 107 case ContentDecryptionModule::PERSISTENT_RELEASE_MESSAGE_SESSION: |
| 108 jwk_set.SetString(kTypeTag, kPersistentReleaseMessageSession); | 108 jwk_set.SetString(kTypeTag, kPersistentReleaseMessageSession); |
| 109 break; | 109 break; |
| 110 } | 110 } |
| 111 | 111 |
| 112 // Finally serialize |jwk_set| into a string and return it. | 112 // Finally serialize |jwk_set| into a string and return it. |
| 113 std::string serialized_jwk; | 113 std::string serialized_jwk; |
| 114 JSONStringValueSerializer serializer(&serialized_jwk); | 114 JSONStringValueSerializer serializer(&serialized_jwk); |
| 115 serializer.Serialize(jwk_set); | 115 serializer.Serialize(jwk_set); |
| 116 return serialized_jwk; | 116 return serialized_jwk; |
| 117 } | 117 } |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 return false; | 157 return false; |
| 158 } | 158 } |
| 159 | 159 |
| 160 // Add the decoded key ID and the decoded key to the list. | 160 // Add the decoded key ID and the decoded key to the list. |
| 161 *jwk_key = std::make_pair(raw_key_id, raw_key); | 161 *jwk_key = std::make_pair(raw_key_id, raw_key); |
| 162 return true; | 162 return true; |
| 163 } | 163 } |
| 164 | 164 |
| 165 bool ExtractKeysFromJWKSet(const std::string& jwk_set, | 165 bool ExtractKeysFromJWKSet(const std::string& jwk_set, |
| 166 KeyIdAndKeyPairs* keys, | 166 KeyIdAndKeyPairs* keys, |
| 167 MediaKeys::SessionType* session_type) { | 167 ContentDecryptionModule::SessionType* session_type) { |
| 168 if (!base::IsStringASCII(jwk_set)) { | 168 if (!base::IsStringASCII(jwk_set)) { |
| 169 DVLOG(1) << "Non ASCII JWK Set: " << jwk_set; | 169 DVLOG(1) << "Non ASCII JWK Set: " << jwk_set; |
| 170 return false; | 170 return false; |
| 171 } | 171 } |
| 172 | 172 |
| 173 std::unique_ptr<base::Value> root(base::JSONReader().ReadToValue(jwk_set)); | 173 std::unique_ptr<base::Value> root(base::JSONReader().ReadToValue(jwk_set)); |
| 174 if (!root.get() || root->GetType() != base::Value::Type::DICTIONARY) { | 174 if (!root.get() || root->GetType() != base::Value::Type::DICTIONARY) { |
| 175 DVLOG(1) << "Not valid JSON: " << jwk_set << ", root: " << root.get(); | 175 DVLOG(1) << "Not valid JSON: " << jwk_set << ", root: " << root.get(); |
| 176 return false; | 176 return false; |
| 177 } | 177 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 203 } | 203 } |
| 204 local_keys.push_back(key_pair); | 204 local_keys.push_back(key_pair); |
| 205 } | 205 } |
| 206 | 206 |
| 207 // Successfully processed all JWKs in the set. Now check if "type" is | 207 // Successfully processed all JWKs in the set. Now check if "type" is |
| 208 // specified. | 208 // specified. |
| 209 base::Value* value = NULL; | 209 base::Value* value = NULL; |
| 210 std::string session_type_id; | 210 std::string session_type_id; |
| 211 if (!dictionary->Get(kTypeTag, &value)) { | 211 if (!dictionary->Get(kTypeTag, &value)) { |
| 212 // Not specified, so use the default type. | 212 // Not specified, so use the default type. |
| 213 *session_type = MediaKeys::TEMPORARY_SESSION; | 213 *session_type = ContentDecryptionModule::TEMPORARY_SESSION; |
| 214 } else if (!value->GetAsString(&session_type_id)) { | 214 } else if (!value->GetAsString(&session_type_id)) { |
| 215 DVLOG(1) << "Invalid '" << kTypeTag << "' value"; | 215 DVLOG(1) << "Invalid '" << kTypeTag << "' value"; |
| 216 return false; | 216 return false; |
| 217 } else if (session_type_id == kTemporarySession) { | 217 } else if (session_type_id == kTemporarySession) { |
| 218 *session_type = MediaKeys::TEMPORARY_SESSION; | 218 *session_type = ContentDecryptionModule::TEMPORARY_SESSION; |
| 219 } else if (session_type_id == kPersistentLicenseSession) { | 219 } else if (session_type_id == kPersistentLicenseSession) { |
| 220 *session_type = MediaKeys::PERSISTENT_LICENSE_SESSION; | 220 *session_type = ContentDecryptionModule::PERSISTENT_LICENSE_SESSION; |
| 221 } else if (session_type_id == kPersistentReleaseMessageSession) { | 221 } else if (session_type_id == kPersistentReleaseMessageSession) { |
| 222 *session_type = MediaKeys::PERSISTENT_RELEASE_MESSAGE_SESSION; | 222 *session_type = ContentDecryptionModule::PERSISTENT_RELEASE_MESSAGE_SESSION; |
| 223 } else { | 223 } else { |
| 224 DVLOG(1) << "Invalid '" << kTypeTag << "' value: " << session_type_id; | 224 DVLOG(1) << "Invalid '" << kTypeTag << "' value: " << session_type_id; |
| 225 return false; | 225 return false; |
| 226 } | 226 } |
| 227 | 227 |
| 228 // All done. | 228 // All done. |
| 229 keys->swap(local_keys); | 229 keys->swap(local_keys); |
| 230 return true; | 230 return true; |
| 231 } | 231 } |
| 232 | 232 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 291 raw_key_id.data(), raw_key_id.data() + raw_key_id.length())); | 291 raw_key_id.data(), raw_key_id.data() + raw_key_id.length())); |
| 292 } | 292 } |
| 293 | 293 |
| 294 // All done. | 294 // All done. |
| 295 key_ids->swap(local_key_ids); | 295 key_ids->swap(local_key_ids); |
| 296 error_message->clear(); | 296 error_message->clear(); |
| 297 return true; | 297 return true; |
| 298 } | 298 } |
| 299 | 299 |
| 300 void CreateLicenseRequest(const KeyIdList& key_ids, | 300 void CreateLicenseRequest(const KeyIdList& key_ids, |
| 301 MediaKeys::SessionType session_type, | 301 ContentDecryptionModule::SessionType session_type, |
| 302 std::vector<uint8_t>* license) { | 302 std::vector<uint8_t>* license) { |
| 303 // Create the license request. | 303 // Create the license request. |
| 304 std::unique_ptr<base::DictionaryValue> request(new base::DictionaryValue()); | 304 std::unique_ptr<base::DictionaryValue> request(new base::DictionaryValue()); |
| 305 std::unique_ptr<base::ListValue> list(new base::ListValue()); | 305 std::unique_ptr<base::ListValue> list(new base::ListValue()); |
| 306 for (const auto& key_id : key_ids) { | 306 for (const auto& key_id : key_ids) { |
| 307 std::string key_id_string; | 307 std::string key_id_string; |
| 308 base::Base64UrlEncode( | 308 base::Base64UrlEncode( |
| 309 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), | 309 base::StringPiece(reinterpret_cast<const char*>(key_id.data()), |
| 310 key_id.size()), | 310 key_id.size()), |
| 311 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); | 311 base::Base64UrlEncodePolicy::OMIT_PADDING, &key_id_string); |
| 312 | 312 |
| 313 list->AppendString(key_id_string); | 313 list->AppendString(key_id_string); |
| 314 } | 314 } |
| 315 request->Set(kKeyIdsTag, list.release()); | 315 request->Set(kKeyIdsTag, list.release()); |
| 316 | 316 |
| 317 switch (session_type) { | 317 switch (session_type) { |
| 318 case MediaKeys::TEMPORARY_SESSION: | 318 case ContentDecryptionModule::TEMPORARY_SESSION: |
| 319 request->SetString(kTypeTag, kTemporarySession); | 319 request->SetString(kTypeTag, kTemporarySession); |
| 320 break; | 320 break; |
| 321 case MediaKeys::PERSISTENT_LICENSE_SESSION: | 321 case ContentDecryptionModule::PERSISTENT_LICENSE_SESSION: |
| 322 request->SetString(kTypeTag, kPersistentLicenseSession); | 322 request->SetString(kTypeTag, kPersistentLicenseSession); |
| 323 break; | 323 break; |
| 324 case MediaKeys::PERSISTENT_RELEASE_MESSAGE_SESSION: | 324 case ContentDecryptionModule::PERSISTENT_RELEASE_MESSAGE_SESSION: |
| 325 request->SetString(kTypeTag, kPersistentReleaseMessageSession); | 325 request->SetString(kTypeTag, kPersistentReleaseMessageSession); |
| 326 break; | 326 break; |
| 327 } | 327 } |
| 328 | 328 |
| 329 // Serialize the license request as a string. | 329 // Serialize the license request as a string. |
| 330 std::string json; | 330 std::string json; |
| 331 JSONStringValueSerializer serializer(&json); | 331 JSONStringValueSerializer serializer(&json); |
| 332 serializer.Serialize(*request); | 332 serializer.Serialize(*request); |
| 333 | 333 |
| 334 // Convert the serialized license request into std::vector and return it. | 334 // Convert the serialized license request into std::vector and return it. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 409 DVLOG(1) << "Invalid '" << kKeyIdsTag << "' value: " << encoded_key; | 409 DVLOG(1) << "Invalid '" << kKeyIdsTag << "' value: " << encoded_key; |
| 410 return false; | 410 return false; |
| 411 } | 411 } |
| 412 | 412 |
| 413 std::vector<uint8_t> result(decoded_string.begin(), decoded_string.end()); | 413 std::vector<uint8_t> result(decoded_string.begin(), decoded_string.end()); |
| 414 first_key->swap(result); | 414 first_key->swap(result); |
| 415 return true; | 415 return true; |
| 416 } | 416 } |
| 417 | 417 |
| 418 } // namespace media | 418 } // namespace media |
| OLD | NEW |