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" |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 std::string escaped_str = | 95 std::string escaped_str = |
96 base::EscapeBytesAsInvalidJSONString(input.substr(0, 65), false); | 96 base::EscapeBytesAsInvalidJSONString(input.substr(0, 65), false); |
97 if (escaped_str.length() <= 64u) | 97 if (escaped_str.length() <= 64u) |
98 return escaped_str; | 98 return escaped_str; |
99 | 99 |
100 // This may end up truncating an escaped character, but the first part of | 100 // This may end up truncating an escaped character, but the first part of |
101 // the string should provide enough information. | 101 // the string should provide enough information. |
102 return escaped_str.substr(0, 61).append("..."); | 102 return escaped_str.substr(0, 61).append("..."); |
103 } | 103 } |
104 | 104 |
| 105 static scoped_ptr<base::DictionaryValue> CreateJSONDictionary( |
| 106 const uint8* key, |
| 107 int key_length, |
| 108 const uint8* key_id, |
| 109 int key_id_length) { |
| 110 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue()); |
| 111 jwk->SetString(kKeyTypeTag, kKeyTypeOct); |
| 112 jwk->SetString(kKeyTag, EncodeBase64Url(key, key_length)); |
| 113 jwk->SetString(kKeyIdTag, EncodeBase64Url(key_id, key_id_length)); |
| 114 return jwk.Pass(); |
| 115 } |
| 116 |
105 std::string GenerateJWKSet(const uint8* key, int key_length, | 117 std::string GenerateJWKSet(const uint8* key, int key_length, |
106 const uint8* key_id, int key_id_length) { | 118 const uint8* key_id, int key_id_length) { |
107 // Both |key| and |key_id| need to be base64 encoded strings in the JWK. | |
108 std::string key_base64 = EncodeBase64Url(key, key_length); | |
109 std::string key_id_base64 = EncodeBase64Url(key_id, key_id_length); | |
110 | |
111 // Create the JWK, and wrap it into a JWK Set. | 119 // Create the JWK, and wrap it into a JWK Set. |
112 scoped_ptr<base::DictionaryValue> jwk(new base::DictionaryValue()); | |
113 jwk->SetString(kKeyTypeTag, kKeyTypeOct); | |
114 jwk->SetString(kKeyTag, key_base64); | |
115 jwk->SetString(kKeyIdTag, key_id_base64); | |
116 scoped_ptr<base::ListValue> list(new base::ListValue()); | 120 scoped_ptr<base::ListValue> list(new base::ListValue()); |
117 list->Append(jwk.release()); | 121 list->Append( |
| 122 CreateJSONDictionary(key, key_length, key_id, key_id_length).release()); |
118 base::DictionaryValue jwk_set; | 123 base::DictionaryValue jwk_set; |
119 jwk_set.Set(kKeysTag, list.release()); | 124 jwk_set.Set(kKeysTag, list.release()); |
120 | 125 |
121 // Finally serialize |jwk_set| into a string and return it. | 126 // Finally serialize |jwk_set| into a string and return it. |
122 std::string serialized_jwk; | 127 std::string serialized_jwk; |
| 128 JSONStringValueSerializer serializer(&serialized_jwk); |
| 129 serializer.Serialize(jwk_set); |
| 130 return serialized_jwk; |
| 131 } |
| 132 |
| 133 std::string GenerateJWKSet(const KeyIdAndKeyPairs& keys, |
| 134 MediaKeys::SessionType session_type) { |
| 135 scoped_ptr<base::ListValue> list(new base::ListValue()); |
| 136 for (const auto& key_pair : keys) { |
| 137 list->Append(CreateJSONDictionary( |
| 138 reinterpret_cast<const uint8*>(key_pair.second.data()), |
| 139 key_pair.second.length(), |
| 140 reinterpret_cast<const uint8*>(key_pair.first.data()), |
| 141 key_pair.first.length()) |
| 142 .release()); |
| 143 } |
| 144 |
| 145 base::DictionaryValue jwk_set; |
| 146 jwk_set.Set(kKeysTag, list.release()); |
| 147 switch (session_type) { |
| 148 case MediaKeys::TEMPORARY_SESSION: |
| 149 jwk_set.SetString(kTypeTag, kTemporarySession); |
| 150 break; |
| 151 case MediaKeys::PERSISTENT_LICENSE_SESSION: |
| 152 jwk_set.SetString(kTypeTag, kPersistentLicenseSession); |
| 153 break; |
| 154 case MediaKeys::PERSISTENT_RELEASE_MESSAGE_SESSION: |
| 155 jwk_set.SetString(kTypeTag, kPersistentReleaseMessageSession); |
| 156 break; |
| 157 } |
| 158 |
| 159 // Finally serialize |jwk_set| into a string and return it. |
| 160 std::string serialized_jwk; |
123 JSONStringValueSerializer serializer(&serialized_jwk); | 161 JSONStringValueSerializer serializer(&serialized_jwk); |
124 serializer.Serialize(jwk_set); | 162 serializer.Serialize(jwk_set); |
125 return serialized_jwk; | 163 return serialized_jwk; |
126 } | 164 } |
127 | 165 |
128 // Processes a JSON Web Key to extract the key id and key value. Sets |jwk_key| | 166 // Processes a JSON Web Key to extract the key id and key value. Sets |jwk_key| |
129 // to the id/value pair and returns true on success. | 167 // to the id/value pair and returns true on success. |
130 static bool ConvertJwkToKeyPair(const base::DictionaryValue& jwk, | 168 static bool ConvertJwkToKeyPair(const base::DictionaryValue& jwk, |
131 KeyIdAndKeyPair* jwk_key) { | 169 KeyIdAndKeyPair* jwk_key) { |
132 std::string type; | 170 std::string type; |
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 DVLOG(1) << "Invalid '" << kKeyIdsTag << "' value: " << encoded_key; | 428 DVLOG(1) << "Invalid '" << kKeyIdsTag << "' value: " << encoded_key; |
391 return false; | 429 return false; |
392 } | 430 } |
393 | 431 |
394 std::vector<uint8> result(decoded_string.begin(), decoded_string.end()); | 432 std::vector<uint8> result(decoded_string.begin(), decoded_string.end()); |
395 first_key->swap(result); | 433 first_key->swap(result); |
396 return true; | 434 return true; |
397 } | 435 } |
398 | 436 |
399 } // namespace media | 437 } // namespace media |
OLD | NEW |