Chromium Code Reviews| 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(), 16); | |
|
ddorwin
2012/07/14 00:50:31
magic #
fgalligan1
2012/07/16 23:51:42
Done.
| |
| 105 // Set the counter block. | |
| 106 base::StringPiece counter_block( | |
| 107 reinterpret_cast<const char*>(input.GetDecryptConfig()->iv()), | |
| 108 input.GetDecryptConfig()->iv_size()); | |
| 109 if (counter_block.empty()) { | |
| 110 DVLOG(1) << "Could not generate counter block."; | |
| 111 return NULL; | |
| 112 } | |
| 113 if (!encryptor.SetCounter(counter_block)) { | |
| 114 DVLOG(1) << "Could not set counter block."; | |
| 37 return NULL; | 115 return NULL; |
| 38 } | 116 } |
| 39 | 117 |
| 40 std::string decrypted_text; | 118 std::string decrypted_text; |
| 41 base::StringPiece encrypted_text( | 119 const char* frame = |
| 42 reinterpret_cast<const char*>(input.GetData()), | 120 reinterpret_cast<const char*>(input.GetData() + encrypted_data_offset); |
| 43 input.GetDataSize()); | 121 int frame_size = input.GetDataSize() - encrypted_data_offset; |
| 122 base::StringPiece encrypted_text(frame, frame_size); | |
| 44 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { | 123 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { |
| 45 DVLOG(1) << "Could not decrypt data."; | 124 DVLOG(1) << "Could not decrypt data."; |
| 46 return NULL; | 125 return NULL; |
| 47 } | 126 } |
| 48 | 127 |
| 49 // TODO(xhwang): Find a way to avoid this data copy. | 128 // TODO(xhwang): Find a way to avoid this data copy. |
| 50 return DecoderBuffer::CopyFrom( | 129 return DecoderBuffer::CopyFrom( |
| 51 reinterpret_cast<const uint8*>(decrypted_text.data()), | 130 reinterpret_cast<const uint8*>(decrypted_text.data()), |
| 52 decrypted_text.size()); | 131 decrypted_text.size()); |
| 53 } | 132 } |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 if (!init_data) { | 178 if (!init_data) { |
| 100 init_data = kDummyInitData; | 179 init_data = kDummyInitData; |
| 101 init_data_length = arraysize(kDummyInitData); | 180 init_data_length = arraysize(kDummyInitData); |
| 102 } | 181 } |
| 103 | 182 |
| 104 // TODO(xhwang): For now, use |init_data| for key ID. Make this more spec | 183 // 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). | 184 // compliant later (http://crbug.com/123262, http://crbug.com/123265). |
| 106 std::string key_id_string(reinterpret_cast<const char*>(init_data), | 185 std::string key_id_string(reinterpret_cast<const char*>(init_data), |
| 107 init_data_length); | 186 init_data_length); |
| 108 std::string key_string(reinterpret_cast<const char*>(key) , key_length); | 187 std::string key_string(reinterpret_cast<const char*>(key) , key_length); |
| 109 crypto::SymmetricKey* symmetric_key = crypto::SymmetricKey::Import( | 188 scoped_ptr<DecryptionKey> decryption_key(new DecryptionKey(key_string)); |
| 110 crypto::SymmetricKey::AES, key_string); | 189 if (!decryption_key.get()) { |
| 111 if (!symmetric_key) { | 190 DVLOG(1) << "Could not create key."; |
| 112 DVLOG(1) << "Could not import key."; | |
| 113 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); | 191 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); |
| 114 return; | 192 return; |
| 115 } | 193 } |
| 194 | |
| 195 // TODO(fgalligan): When ISO is added we will need to figure out how to | |
| 196 // detect if the encrypted data will contain an HMAC. | |
| 197 if (!decryption_key->Init(true)) { | |
| 198 DVLOG(1) << "Could not initialize decryption key."; | |
| 199 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); | |
| 200 return; | |
| 201 } | |
| 116 | 202 |
| 117 { | 203 { |
| 118 base::AutoLock auto_lock(key_map_lock_); | 204 base::AutoLock auto_lock(key_map_lock_); |
| 119 KeyMap::iterator found = key_map_.find(key_id_string); | 205 KeyMap::iterator found = key_map_.find(key_id_string); |
| 120 if (found != key_map_.end()) { | 206 if (found != key_map_.end()) { |
| 121 delete found->second; | 207 delete found->second; |
| 122 key_map_.erase(found); | 208 key_map_.erase(found); |
| 123 } | 209 } |
| 124 key_map_[key_id_string] = symmetric_key; | 210 key_map_[key_id_string] = decryption_key.release(); |
| 125 } | 211 } |
| 126 | 212 |
| 127 client_->KeyAdded(key_system, session_id); | 213 client_->KeyAdded(key_system, session_id); |
| 128 } | 214 } |
| 129 | 215 |
| 130 void AesDecryptor::CancelKeyRequest(const std::string& key_system, | 216 void AesDecryptor::CancelKeyRequest(const std::string& key_system, |
| 131 const std::string& session_id) { | 217 const std::string& session_id) { |
| 132 } | 218 } |
| 133 | 219 |
| 134 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt( | 220 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt( |
| 135 const scoped_refptr<DecoderBuffer>& encrypted) { | 221 const scoped_refptr<DecoderBuffer>& encrypted) { |
| 136 CHECK(encrypted->GetDecryptConfig()); | 222 CHECK(encrypted->GetDecryptConfig()); |
| 137 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); | 223 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); |
| 138 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); | 224 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); |
| 139 | 225 |
| 140 // TODO(xhwang): Avoid always constructing a string with StringPiece? | 226 // TODO(xhwang): Avoid always constructing a string with StringPiece? |
| 141 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); | 227 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); |
| 142 | 228 |
| 143 crypto::SymmetricKey* key = NULL; | 229 DecryptionKey* key = NULL; |
| 144 { | 230 { |
| 145 base::AutoLock auto_lock(key_map_lock_); | 231 base::AutoLock auto_lock(key_map_lock_); |
| 146 KeyMap::const_iterator found = key_map_.find(key_id_string); | 232 KeyMap::const_iterator found = key_map_.find(key_id_string); |
| 147 if (found == key_map_.end()) { | 233 if (found == key_map_.end()) { |
| 234 // TODO(fgalligan): Fire a need_key event here. | |
|
ddorwin
2012/07/14 00:50:31
Hmm, I just realized that for this event, we _only
fgalligan1
2012/07/16 23:51:42
Done.
| |
| 148 DVLOG(1) << "Could not find a matching key for given key ID."; | 235 DVLOG(1) << "Could not find a matching key for given key ID."; |
| 149 return NULL; | 236 return NULL; |
| 150 } | 237 } |
| 151 key = found->second; | 238 key = found->second; |
| 152 } | 239 } |
| 153 | 240 |
| 154 scoped_refptr<DecoderBuffer> decrypted = DecryptData(*encrypted, key); | 241 int checksum_size = encrypted->GetDecryptConfig()->checksum_size(); |
| 242 // According to the WebM encrypted specification, it is an open question | |
| 243 // what should happen when a frame fails the integrity check. | |
| 244 // http://wiki.webmproject.org/encryption/webm-encryption-rfc | |
| 245 if (checksum_size > 0 && | |
| 246 !key->hmac_key().empty() && | |
| 247 !CheckData(*encrypted, key->hmac_key())) { | |
| 248 DVLOG(1) << "Integrity check failed."; | |
| 249 return NULL; | |
|
ddorwin
2012/07/14 00:50:31
Until the open question above is resolved, let's f
xhwang
2012/07/14 01:02:31
Currently when the decoder sees NULL it will fire
fgalligan1
2012/07/16 23:51:42
Is this fine for now?
| |
| 250 } | |
| 155 | 251 |
| 252 scoped_refptr<DecoderBuffer> decrypted = | |
| 253 DecryptData(*encrypted, | |
| 254 key->decryption_key(), | |
| 255 encrypted->GetDecryptConfig()->encrypted_frame_offset()); | |
| 156 if (decrypted) { | 256 if (decrypted) { |
| 157 decrypted->SetTimestamp(encrypted->GetTimestamp()); | 257 decrypted->SetTimestamp(encrypted->GetTimestamp()); |
| 158 decrypted->SetDuration(encrypted->GetDuration()); | 258 decrypted->SetDuration(encrypted->GetDuration()); |
| 159 } | 259 } |
| 160 | 260 |
| 161 return decrypted; | 261 return decrypted; |
| 162 } | 262 } |
| 163 | 263 |
| 264 AesDecryptor::DecryptionKey::DecryptionKey( | |
| 265 const std::string& secret) | |
| 266 : secret_(secret) { | |
| 267 } | |
| 268 | |
| 269 AesDecryptor::DecryptionKey::~DecryptionKey() {} | |
| 270 | |
| 271 bool AesDecryptor::DecryptionKey::Init(bool derive_webm_keys) { | |
| 272 CHECK(!secret_.empty()); | |
| 273 | |
| 274 if (derive_webm_keys) { | |
| 275 std::string raw_key = DeriveKey(secret_, | |
| 276 kWebmEncryptionSeed, | |
| 277 secret_.length()); | |
| 278 if (raw_key.empty()) { | |
| 279 return false; | |
| 280 } | |
| 281 decryption_key_.reset( | |
| 282 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, raw_key)); | |
| 283 if (!decryption_key_.get()) { | |
| 284 return false; | |
| 285 } | |
| 286 | |
| 287 hmac_key_ = DeriveKey(secret_, kWebmHmacSeed, kWebmSha1DigestSize); | |
| 288 if (hmac_key_.empty()) { | |
| 289 return false; | |
| 290 } | |
|
xhwang
2012/07/14 00:05:22
nit: return true here and eliminate the following
fgalligan1
2012/07/16 23:51:42
Done.
| |
| 291 } else { | |
| 292 decryption_key_.reset( | |
| 293 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, secret_)); | |
| 294 if (!decryption_key_.get()) { | |
| 295 return false; | |
| 296 } | |
| 297 } | |
| 298 | |
| 299 return true; | |
| 300 } | |
| 301 | |
| 164 } // namespace media | 302 } // namespace media |
| OLD | NEW |