OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/crypto/aes_decryptor.h" | 5 #include "media/crypto/aes_decryptor.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/string_number_conversions.h" | 9 #include "base/string_number_conversions.h" |
| 10 #include "base/string_piece.h" |
10 #include "crypto/encryptor.h" | 11 #include "crypto/encryptor.h" |
11 #include "crypto/hmac.h" | |
12 #include "crypto/symmetric_key.h" | 12 #include "crypto/symmetric_key.h" |
13 #include "media/base/decoder_buffer.h" | 13 #include "media/base/decoder_buffer.h" |
14 #include "media/base/decrypt_config.h" | 14 #include "media/base/decrypt_config.h" |
15 #include "media/base/decryptor_client.h" | 15 #include "media/base/decryptor_client.h" |
16 | 16 |
17 namespace media { | 17 namespace media { |
18 | 18 |
19 // The size is from the WebM encrypted specification. Current encrypted WebM | 19 // TODO(xhwang): Get real IV from frames. |
20 // request for comments specification is here | 20 static const char kInitialCounter[] = "0000000000000000"; |
21 // http://wiki.webmproject.org/encryption/webm-encryption-rfc | |
22 static const int kWebmSha1DigestSize = 20; | |
23 static const char kWebmHmacSeed[] = "hmac-key"; | |
24 static const char kWebmEncryptionSeed[] = "encryption-key"; | |
25 | 21 |
26 uint32 AesDecryptor::next_session_id_ = 1; | 22 uint32 AesDecryptor::next_session_id_ = 1; |
27 | 23 |
28 // Derives a key using SHA1 HMAC. |secret| is the base secret to derive | 24 // Decrypt |input| using |key|. |
29 // the key from. |seed| is the known message to the HMAC algorithm. |key_size| | 25 // Return a DecoderBuffer with the decrypted data if decryption succeeded. |
30 // is how many bytes are returned in the key. Returns a string containing the | 26 // Return NULL if decryption failed. |
31 // key on success. Returns an empty string on failure. | 27 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, |
32 static std::string DeriveKey(const base::StringPiece& secret, | 28 crypto::SymmetricKey* key) { |
33 const base::StringPiece& seed, | |
34 int key_size) { | |
35 CHECK(!secret.empty()); | |
36 CHECK(!seed.empty()); | |
37 CHECK_GT(key_size, 0); | |
38 | |
39 crypto::HMAC hmac(crypto::HMAC::SHA1); | |
40 if (!hmac.Init(secret)) { | |
41 DVLOG(1) << "Could not initialize HMAC with secret data."; | |
42 return std::string(); | |
43 } | |
44 | |
45 scoped_array<uint8> calculated_hmac(new uint8[hmac.DigestLength()]); | |
46 if (!hmac.Sign(seed, calculated_hmac.get(), hmac.DigestLength())) { | |
47 DVLOG(1) << "Could not calculate HMAC."; | |
48 return std::string(); | |
49 } | |
50 | |
51 return std::string(reinterpret_cast<const char*>(calculated_hmac.get()), | |
52 key_size); | |
53 } | |
54 | |
55 // Checks data in |input| matches the HMAC in |input|. The check is using the | |
56 // SHA1 algorithm. |hmac_key| is the key of the HMAC algorithm. Returns true if | |
57 // the integrity check passes. | |
58 static bool CheckData(const DecoderBuffer& input, | |
59 const base::StringPiece& hmac_key) { | |
60 CHECK(input.GetDataSize()); | 29 CHECK(input.GetDataSize()); |
61 CHECK(input.GetDecryptConfig()); | |
62 CHECK_GT(input.GetDecryptConfig()->checksum_size(), 0); | |
63 CHECK(!hmac_key.empty()); | |
64 | |
65 crypto::HMAC hmac(crypto::HMAC::SHA1); | |
66 if (!hmac.Init(hmac_key)) | |
67 return false; | |
68 | |
69 // The HMAC covers the IV and the frame data. | |
70 base::StringPiece data_to_check( | |
71 reinterpret_cast<const char*>(input.GetData()), input.GetDataSize()); | |
72 | |
73 scoped_array<uint8> calculated_hmac(new uint8[hmac.DigestLength()]); | |
74 if (!hmac.Sign(data_to_check, calculated_hmac.get(), hmac.DigestLength())) | |
75 return false; | |
76 | |
77 DCHECK(input.GetDecryptConfig()->checksum_size() <= | |
78 static_cast<int>(hmac.DigestLength())); | |
79 if (memcmp(input.GetDecryptConfig()->checksum(), | |
80 calculated_hmac.get(), | |
81 input.GetDecryptConfig()->checksum_size()) != 0) | |
82 return false; | |
83 return true; | |
84 } | |
85 | |
86 // Decrypts |input| using |key|. |encrypted_data_offset| is the number of bytes | |
87 // into |input| that the encrypted data starts. | |
88 // Returns a DecoderBuffer with the decrypted data if decryption succeeded or | |
89 // NULL if decryption failed. | |
90 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, | |
91 crypto::SymmetricKey* key, | |
92 int encrypted_data_offset) { | |
93 CHECK(input.GetDataSize()); | |
94 CHECK(input.GetDecryptConfig()); | |
95 CHECK(key); | 30 CHECK(key); |
96 | 31 |
97 // Initialize decryptor. | 32 // Initialize encryption data. |
| 33 // The IV must be exactly as long as the cipher block size. |
98 crypto::Encryptor encryptor; | 34 crypto::Encryptor encryptor; |
99 if (!encryptor.Init(key, crypto::Encryptor::CTR, "")) { | 35 if (!encryptor.Init(key, crypto::Encryptor::CBC, kInitialCounter)) { |
100 DVLOG(1) << "Could not initialize decryptor."; | 36 DVLOG(1) << "Could not initialize encryptor."; |
101 return NULL; | |
102 } | |
103 | |
104 DCHECK_EQ(input.GetDecryptConfig()->iv_size(), | |
105 DecryptConfig::kDecryptionKeySize); | |
106 // Set the counter block. | |
107 base::StringPiece counter_block( | |
108 reinterpret_cast<const char*>(input.GetDecryptConfig()->iv()), | |
109 input.GetDecryptConfig()->iv_size()); | |
110 if (counter_block.empty()) { | |
111 DVLOG(1) << "Could not generate counter block."; | |
112 return NULL; | |
113 } | |
114 if (!encryptor.SetCounter(counter_block)) { | |
115 DVLOG(1) << "Could not set counter block."; | |
116 return NULL; | 37 return NULL; |
117 } | 38 } |
118 | 39 |
119 std::string decrypted_text; | 40 std::string decrypted_text; |
120 const char* frame = | 41 base::StringPiece encrypted_text( |
121 reinterpret_cast<const char*>(input.GetData() + encrypted_data_offset); | 42 reinterpret_cast<const char*>(input.GetData()), |
122 int frame_size = input.GetDataSize() - encrypted_data_offset; | 43 input.GetDataSize()); |
123 base::StringPiece encrypted_text(frame, frame_size); | |
124 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { | 44 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { |
125 DVLOG(1) << "Could not decrypt data."; | 45 DVLOG(1) << "Could not decrypt data."; |
126 return NULL; | 46 return NULL; |
127 } | 47 } |
128 | 48 |
129 // TODO(xhwang): Find a way to avoid this data copy. | 49 // TODO(xhwang): Find a way to avoid this data copy. |
130 return DecoderBuffer::CopyFrom( | 50 return DecoderBuffer::CopyFrom( |
131 reinterpret_cast<const uint8*>(decrypted_text.data()), | 51 reinterpret_cast<const uint8*>(decrypted_text.data()), |
132 decrypted_text.size()); | 52 decrypted_text.size()); |
133 } | 53 } |
(...skipping 24 matching lines...) Expand all Loading... |
158 const uint8* key, | 78 const uint8* key, |
159 int key_length, | 79 int key_length, |
160 const uint8* init_data, | 80 const uint8* init_data, |
161 int init_data_length, | 81 int init_data_length, |
162 const std::string& session_id) { | 82 const std::string& session_id) { |
163 CHECK(key); | 83 CHECK(key); |
164 CHECK_GT(key_length, 0); | 84 CHECK_GT(key_length, 0); |
165 | 85 |
166 // TODO(xhwang): Add |session_id| check after we figure out how: | 86 // TODO(xhwang): Add |session_id| check after we figure out how: |
167 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16550 | 87 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16550 |
168 if (key_length != DecryptConfig::kDecryptionKeySize) { | 88 |
| 89 const int kSupportedKeyLength = 16; // 128-bit key. |
| 90 if (key_length != kSupportedKeyLength) { |
169 DVLOG(1) << "Invalid key length: " << key_length; | 91 DVLOG(1) << "Invalid key length: " << key_length; |
170 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); | 92 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); |
171 return; | 93 return; |
172 } | 94 } |
173 | 95 |
174 // TODO(xhwang): Fix the decryptor to accept no |init_data|. See | 96 // TODO(xhwang): Fix the decryptor to accept no |init_data|. See |
175 // http://crbug.com/123265. Until then, ensure a non-empty value is passed. | 97 // http://crbug.com/123265. Until then, ensure a non-empty value is passed. |
176 static const uint8 kDummyInitData[1] = { 0 }; | 98 static const uint8 kDummyInitData[1] = { 0 }; |
177 if (!init_data) { | 99 if (!init_data) { |
178 init_data = kDummyInitData; | 100 init_data = kDummyInitData; |
179 init_data_length = arraysize(kDummyInitData); | 101 init_data_length = arraysize(kDummyInitData); |
180 } | 102 } |
181 | 103 |
182 // TODO(xhwang): For now, use |init_data| for key ID. Make this more spec | 104 // TODO(xhwang): For now, use |init_data| for key ID. Make this more spec |
183 // compliant later (http://crbug.com/123262, http://crbug.com/123265). | 105 // compliant later (http://crbug.com/123262, http://crbug.com/123265). |
184 std::string key_id_string(reinterpret_cast<const char*>(init_data), | 106 std::string key_id_string(reinterpret_cast<const char*>(init_data), |
185 init_data_length); | 107 init_data_length); |
186 std::string key_string(reinterpret_cast<const char*>(key) , key_length); | 108 std::string key_string(reinterpret_cast<const char*>(key) , key_length); |
187 scoped_ptr<DecryptionKey> decryption_key(new DecryptionKey(key_string)); | 109 crypto::SymmetricKey* symmetric_key = crypto::SymmetricKey::Import( |
188 if (!decryption_key.get()) { | 110 crypto::SymmetricKey::AES, key_string); |
189 DVLOG(1) << "Could not create key."; | 111 if (!symmetric_key) { |
190 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); | 112 DVLOG(1) << "Could not import key."; |
191 return; | |
192 } | |
193 | |
194 // TODO(fgalligan): When ISO is added we will need to figure out how to | |
195 // detect if the encrypted data will contain an HMAC. | |
196 if (!decryption_key->Init(true)) { | |
197 DVLOG(1) << "Could not initialize decryption key."; | |
198 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); | 113 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); |
199 return; | 114 return; |
200 } | 115 } |
201 | 116 |
202 { | 117 { |
203 base::AutoLock auto_lock(key_map_lock_); | 118 base::AutoLock auto_lock(key_map_lock_); |
204 KeyMap::iterator found = key_map_.find(key_id_string); | 119 KeyMap::iterator found = key_map_.find(key_id_string); |
205 if (found != key_map_.end()) { | 120 if (found != key_map_.end()) { |
206 delete found->second; | 121 delete found->second; |
207 key_map_.erase(found); | 122 key_map_.erase(found); |
208 } | 123 } |
209 key_map_[key_id_string] = decryption_key.release(); | 124 key_map_[key_id_string] = symmetric_key; |
210 } | 125 } |
211 | 126 |
212 client_->KeyAdded(key_system, session_id); | 127 client_->KeyAdded(key_system, session_id); |
213 } | 128 } |
214 | 129 |
215 void AesDecryptor::CancelKeyRequest(const std::string& key_system, | 130 void AesDecryptor::CancelKeyRequest(const std::string& key_system, |
216 const std::string& session_id) { | 131 const std::string& session_id) { |
217 } | 132 } |
218 | 133 |
219 void AesDecryptor::Decrypt(const scoped_refptr<DecoderBuffer>& encrypted, | 134 void AesDecryptor::Decrypt(const scoped_refptr<DecoderBuffer>& encrypted, |
220 const DecryptCB& decrypt_cb) { | 135 const DecryptCB& decrypt_cb) { |
221 CHECK(encrypted->GetDecryptConfig()); | 136 CHECK(encrypted->GetDecryptConfig()); |
222 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); | 137 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); |
223 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); | 138 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); |
224 | 139 |
225 // TODO(xhwang): Avoid always constructing a string with StringPiece? | 140 // TODO(xhwang): Avoid always constructing a string with StringPiece? |
226 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); | 141 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); |
227 | 142 |
228 DecryptionKey* key = NULL; | 143 crypto::SymmetricKey* key = NULL; |
229 { | 144 { |
230 base::AutoLock auto_lock(key_map_lock_); | 145 base::AutoLock auto_lock(key_map_lock_); |
231 KeyMap::const_iterator found = key_map_.find(key_id_string); | 146 KeyMap::const_iterator found = key_map_.find(key_id_string); |
232 if (found != key_map_.end()) | 147 if (found != key_map_.end()) |
233 key = found->second; | 148 key = found->second; |
234 } | 149 } |
235 | 150 |
236 if (!key) { | 151 if (!key) { |
237 // TODO(fgalligan): Fire a need_key event here and add a test. | |
238 DVLOG(1) << "Could not find a matching key for given key ID."; | 152 DVLOG(1) << "Could not find a matching key for given key ID."; |
239 decrypt_cb.Run(kError, NULL); | 153 decrypt_cb.Run(kError, NULL); |
240 return; | 154 return; |
241 } | 155 } |
242 | 156 |
243 int checksum_size = encrypted->GetDecryptConfig()->checksum_size(); | 157 scoped_refptr<DecoderBuffer> decrypted = DecryptData(*encrypted, key); |
244 // According to the WebM encrypted specification, it is an open question | |
245 // what should happen when a frame fails the integrity check. | |
246 // http://wiki.webmproject.org/encryption/webm-encryption-rfc | |
247 if (checksum_size > 0 && | |
248 !key->hmac_key().empty() && | |
249 !CheckData(*encrypted, key->hmac_key())) { | |
250 DVLOG(1) << "Integrity check failed."; | |
251 decrypt_cb.Run(kError, NULL); | |
252 return; | |
253 } | |
254 | 158 |
255 scoped_refptr<DecoderBuffer> decrypted = | |
256 DecryptData(*encrypted, | |
257 key->decryption_key(), | |
258 encrypted->GetDecryptConfig()->encrypted_frame_offset()); | |
259 if (!decrypted) { | 159 if (!decrypted) { |
260 DVLOG(1) << "Decryption failed."; | 160 DVLOG(1) << "Decryption failed."; |
261 decrypt_cb.Run(kError, NULL); | 161 decrypt_cb.Run(kError, NULL); |
262 return; | 162 return; |
263 } | 163 } |
264 | 164 |
265 decrypted->SetTimestamp(encrypted->GetTimestamp()); | 165 decrypted->SetTimestamp(encrypted->GetTimestamp()); |
266 decrypted->SetDuration(encrypted->GetDuration()); | 166 decrypted->SetDuration(encrypted->GetDuration()); |
267 decrypt_cb.Run(kSuccess, decrypted); | 167 decrypt_cb.Run(kSuccess, decrypted); |
268 } | 168 } |
269 | 169 |
270 AesDecryptor::DecryptionKey::DecryptionKey( | |
271 const std::string& secret) | |
272 : secret_(secret) { | |
273 } | |
274 | |
275 AesDecryptor::DecryptionKey::~DecryptionKey() {} | |
276 | |
277 bool AesDecryptor::DecryptionKey::Init(bool derive_webm_keys) { | |
278 CHECK(!secret_.empty()); | |
279 | |
280 if (derive_webm_keys) { | |
281 std::string raw_key = DeriveKey(secret_, | |
282 kWebmEncryptionSeed, | |
283 secret_.length()); | |
284 if (raw_key.empty()) { | |
285 return false; | |
286 } | |
287 decryption_key_.reset( | |
288 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, raw_key)); | |
289 if (!decryption_key_.get()) { | |
290 return false; | |
291 } | |
292 | |
293 hmac_key_ = DeriveKey(secret_, kWebmHmacSeed, kWebmSha1DigestSize); | |
294 if (hmac_key_.empty()) { | |
295 return false; | |
296 } | |
297 return true; | |
298 } | |
299 | |
300 decryption_key_.reset( | |
301 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, secret_)); | |
302 if (!decryption_key_.get()) { | |
303 return false; | |
304 } | |
305 return true; | |
306 } | |
307 | |
308 } // namespace media | 170 } // namespace media |
OLD | NEW |