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