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 "crypto/encryptor.h" | 10 #include "crypto/encryptor.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 52 key_size); | 52 key_size); |
| 53 } | 53 } |
| 54 | 54 |
| 55 // Checks data in |input| matches the HMAC in |input|. The check is using the | 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 | 56 // SHA1 algorithm. |hmac_key| is the key of the HMAC algorithm. Returns true if |
| 57 // the integrity check passes. | 57 // the integrity check passes. |
| 58 static bool CheckData(const DecoderBuffer& input, | 58 static bool CheckData(const DecoderBuffer& input, |
| 59 const base::StringPiece& hmac_key) { | 59 const base::StringPiece& hmac_key) { |
| 60 CHECK(input.GetDataSize()); | 60 CHECK(input.GetDataSize()); |
| 61 CHECK(input.GetDecryptConfig()); | 61 CHECK(input.GetDecryptConfig()); |
| 62 CHECK_GT(input.GetDecryptConfig()->checksum_size(), 0); | 62 CHECK_GT(input.GetDecryptConfig()->checksum().size(), 0u); |
| 63 CHECK(!hmac_key.empty()); | 63 CHECK(!hmac_key.empty()); |
| 64 | 64 |
| 65 crypto::HMAC hmac(crypto::HMAC::SHA1); | 65 crypto::HMAC hmac(crypto::HMAC::SHA1); |
| 66 if (!hmac.Init(hmac_key)) | 66 if (!hmac.Init(hmac_key)) |
| 67 return false; | 67 return false; |
| 68 | 68 |
| 69 // The HMAC covers the IV and the frame data. | 69 // The HMAC covers the IV and the frame data. |
| 70 base::StringPiece data_to_check( | 70 base::StringPiece data_to_check( |
| 71 reinterpret_cast<const char*>(input.GetData()), input.GetDataSize()); | 71 reinterpret_cast<const char*>(input.GetData()), input.GetDataSize()); |
| 72 | 72 |
| 73 scoped_array<uint8> calculated_hmac(new uint8[hmac.DigestLength()]); | 73 scoped_array<uint8> calculated_hmac(new uint8[hmac.DigestLength()]); |
| 74 if (!hmac.Sign(data_to_check, calculated_hmac.get(), hmac.DigestLength())) | 74 if (!hmac.Sign(data_to_check, calculated_hmac.get(), hmac.DigestLength())) |
| 75 return false; | 75 return false; |
| 76 | 76 |
| 77 DCHECK(input.GetDecryptConfig()->checksum_size() <= | 77 DCHECK(input.GetDecryptConfig()->checksum().size() <= hmac.DigestLength()); |
| 78 static_cast<int>(hmac.DigestLength())); | 78 // TODO(fgalligan): Use hmac.Verify(). |
|
ddorwin
2012/07/25 07:13:47
Thanks. I think this is now landed. I didn't reali
| |
| 79 if (memcmp(input.GetDecryptConfig()->checksum(), | 79 if (memcmp(input.GetDecryptConfig()->checksum().data(), |
| 80 calculated_hmac.get(), | 80 calculated_hmac.get(), |
| 81 input.GetDecryptConfig()->checksum_size()) != 0) | 81 input.GetDecryptConfig()->checksum().size()) != 0) |
| 82 return false; | 82 return false; |
| 83 return true; | 83 return true; |
| 84 } | 84 } |
| 85 | 85 |
| 86 // Decrypts |input| using |key|. |encrypted_data_offset| is the number of bytes | 86 enum ClearBytesBufferSel { |
| 87 // into |input| that the encrypted data starts. | 87 kSrcContainsClearBytes, |
| 88 // Returns a DecoderBuffer with the decrypted data if decryption succeeded or | 88 kDstContainsClearBytes, |
| 89 // NULL if decryption failed. | 89 }; |
| 90 | |
| 91 static void CopySubsamples(const std::vector<SubsampleEntry>& subsamples, | |
| 92 const ClearBytesBufferSel sel, | |
| 93 const uint8* src, | |
| 94 uint8* dst) { | |
| 95 for (size_t i = 0; i < subsamples.size(); i++) { | |
| 96 const SubsampleEntry& subsample = subsamples[i]; | |
| 97 if (sel == kSrcContainsClearBytes) { | |
| 98 src += subsample.clear_bytes; | |
| 99 } else { | |
| 100 dst += subsample.clear_bytes; | |
| 101 } | |
| 102 memcpy(dst, src, subsample.cypher_bytes); | |
| 103 src += subsample.cypher_bytes; | |
| 104 dst += subsample.cypher_bytes; | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 // Decrypts |input| using |key|. Returns a DecoderBuffer with the decrypted | |
| 109 // data if decryption succeeded or NULL if decryption failed. | |
| 90 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, | 110 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, |
| 91 crypto::SymmetricKey* key, | 111 crypto::SymmetricKey* key) { |
| 92 int encrypted_data_offset) { | |
| 93 CHECK(input.GetDataSize()); | 112 CHECK(input.GetDataSize()); |
| 94 CHECK(input.GetDecryptConfig()); | 113 CHECK(input.GetDecryptConfig()); |
| 95 CHECK(key); | 114 CHECK(key); |
| 96 | 115 |
| 97 // Initialize decryptor. | |
| 98 crypto::Encryptor encryptor; | 116 crypto::Encryptor encryptor; |
| 99 if (!encryptor.Init(key, crypto::Encryptor::CTR, "")) { | 117 if (!encryptor.Init(key, crypto::Encryptor::CTR, "")) { |
| 100 DVLOG(1) << "Could not initialize decryptor."; | 118 DVLOG(1) << "Could not initialize decryptor."; |
| 101 return NULL; | 119 return NULL; |
| 102 } | 120 } |
| 103 | 121 |
| 104 DCHECK_EQ(input.GetDecryptConfig()->iv_size(), | 122 DCHECK_EQ(input.GetDecryptConfig()->iv().size(), |
| 105 DecryptConfig::kDecryptionKeySize); | 123 static_cast<size_t>(DecryptConfig::kDecryptionKeySize)); |
| 106 // Set the counter block. | 124 if (!encryptor.SetCounter(input.GetDecryptConfig()->iv())) { |
| 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."; | 125 DVLOG(1) << "Could not set counter block."; |
| 116 return NULL; | 126 return NULL; |
| 117 } | 127 } |
| 118 | 128 |
| 129 const int data_offset = input.GetDecryptConfig()->data_offset(); | |
| 130 const char* sample = | |
| 131 reinterpret_cast<const char*>(input.GetData() + data_offset); | |
| 132 int sample_size = input.GetDataSize() - data_offset; | |
| 133 | |
| 134 if (input.GetDecryptConfig()->subsamples().empty()) { | |
| 135 std::string decrypted_text; | |
| 136 base::StringPiece encrypted_text(sample, sample_size); | |
| 137 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { | |
| 138 DVLOG(1) << "Could not decrypt data."; | |
| 139 return NULL; | |
| 140 } | |
| 141 | |
| 142 // TODO(xhwang): Find a way to avoid this data copy. | |
| 143 return DecoderBuffer::CopyFrom( | |
| 144 reinterpret_cast<const uint8*>(decrypted_text.data()), | |
| 145 decrypted_text.size()); | |
| 146 } | |
| 147 | |
| 148 const std::vector<SubsampleEntry>& subsamples = | |
| 149 input.GetDecryptConfig()->subsamples(); | |
| 150 | |
| 151 int total_clear_size = 0; | |
| 152 int total_encrypted_size = 0; | |
| 153 for (size_t i = 0; i < subsamples.size(); i++) { | |
| 154 total_clear_size += subsamples[i].clear_bytes; | |
| 155 total_encrypted_size += subsamples[i].cypher_bytes; | |
| 156 } | |
| 157 if (total_clear_size + total_encrypted_size != sample_size) { | |
| 158 DVLOG(1) << "Subsample sizes do not equal input size"; | |
| 159 return NULL; | |
| 160 } | |
| 161 | |
| 162 // The encrypted portions of all subsamples must form a contiguous block, | |
| 163 // such that an encrypted subsample that ends away from a block boundary is | |
| 164 // immediately followed by the start of the next encrypted subsample. We | |
| 165 // copy all encrypted subsamples to a contiguous buffer, decrypt them, then | |
| 166 // copy the decrypted bytes over the encrypted bytes in the output. | |
| 167 // TODO(strobe): attempt to reduce number of memory copies | |
| 168 scoped_array<uint8> encrypted_bytes(new uint8[total_encrypted_size]); | |
| 169 CopySubsamples(subsamples, kSrcContainsClearBytes, | |
| 170 reinterpret_cast<const uint8*>(sample), encrypted_bytes.get()); | |
| 171 | |
| 172 base::StringPiece encrypted_text( | |
| 173 reinterpret_cast<const char*>(encrypted_bytes.get()), | |
| 174 total_encrypted_size); | |
| 119 std::string decrypted_text; | 175 std::string decrypted_text; |
| 120 const char* frame = | |
| 121 reinterpret_cast<const char*>(input.GetData() + encrypted_data_offset); | |
| 122 int frame_size = input.GetDataSize() - encrypted_data_offset; | |
| 123 base::StringPiece encrypted_text(frame, frame_size); | |
| 124 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { | 176 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { |
| 125 DVLOG(1) << "Could not decrypt data."; | 177 DVLOG(1) << "Could not decrypt data."; |
| 126 return NULL; | 178 return NULL; |
| 127 } | 179 } |
| 128 | 180 |
| 129 // TODO(xhwang): Find a way to avoid this data copy. | 181 scoped_refptr<DecoderBuffer> output = DecoderBuffer::CopyFrom( |
| 130 return DecoderBuffer::CopyFrom( | 182 reinterpret_cast<const uint8*>(sample), sample_size); |
| 131 reinterpret_cast<const uint8*>(decrypted_text.data()), | 183 CopySubsamples(subsamples, kDstContainsClearBytes, |
| 132 decrypted_text.size()); | 184 reinterpret_cast<const uint8*>(decrypted_text.data()), |
| 185 output->GetWritableData()); | |
| 186 return output; | |
| 133 } | 187 } |
| 134 | 188 |
| 135 AesDecryptor::AesDecryptor(DecryptorClient* client) | 189 AesDecryptor::AesDecryptor(DecryptorClient* client) |
| 136 : client_(client) { | 190 : client_(client) { |
| 137 } | 191 } |
| 138 | 192 |
| 139 AesDecryptor::~AesDecryptor() { | 193 AesDecryptor::~AesDecryptor() { |
| 140 STLDeleteValues(&key_map_); | 194 STLDeleteValues(&key_map_); |
| 141 } | 195 } |
| 142 | 196 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 std::string key_id_string(reinterpret_cast<const char*>(init_data), | 238 std::string key_id_string(reinterpret_cast<const char*>(init_data), |
| 185 init_data_length); | 239 init_data_length); |
| 186 std::string key_string(reinterpret_cast<const char*>(key) , key_length); | 240 std::string key_string(reinterpret_cast<const char*>(key) , key_length); |
| 187 scoped_ptr<DecryptionKey> decryption_key(new DecryptionKey(key_string)); | 241 scoped_ptr<DecryptionKey> decryption_key(new DecryptionKey(key_string)); |
| 188 if (!decryption_key.get()) { | 242 if (!decryption_key.get()) { |
| 189 DVLOG(1) << "Could not create key."; | 243 DVLOG(1) << "Could not create key."; |
| 190 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); | 244 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); |
| 191 return; | 245 return; |
| 192 } | 246 } |
| 193 | 247 |
| 194 // TODO(fgalligan): When ISO is added we will need to figure out how to | 248 if (!decryption_key->Init()) { |
| 195 // detect if the encrypted data will contain an HMAC. | |
| 196 if (!decryption_key->Init(true)) { | |
| 197 DVLOG(1) << "Could not initialize decryption key."; | 249 DVLOG(1) << "Could not initialize decryption key."; |
| 198 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); | 250 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); |
| 199 return; | 251 return; |
| 200 } | 252 } |
| 201 | 253 |
| 202 { | 254 { |
| 203 base::AutoLock auto_lock(key_map_lock_); | 255 base::AutoLock auto_lock(key_map_lock_); |
| 204 KeyMap::iterator found = key_map_.find(key_id_string); | 256 KeyMap::iterator found = key_map_.find(key_id_string); |
| 205 if (found != key_map_.end()) { | 257 if (found != key_map_.end()) { |
| 206 delete found->second; | 258 delete found->second; |
| 207 key_map_.erase(found); | 259 key_map_.erase(found); |
| 208 } | 260 } |
| 209 key_map_[key_id_string] = decryption_key.release(); | 261 key_map_[key_id_string] = decryption_key.release(); |
| 210 } | 262 } |
| 211 | 263 |
| 212 client_->KeyAdded(key_system, session_id); | 264 client_->KeyAdded(key_system, session_id); |
| 213 } | 265 } |
| 214 | 266 |
| 215 void AesDecryptor::CancelKeyRequest(const std::string& key_system, | 267 void AesDecryptor::CancelKeyRequest(const std::string& key_system, |
| 216 const std::string& session_id) { | 268 const std::string& session_id) { |
| 217 } | 269 } |
| 218 | 270 |
| 219 void AesDecryptor::Decrypt(const scoped_refptr<DecoderBuffer>& encrypted, | 271 void AesDecryptor::Decrypt(const scoped_refptr<DecoderBuffer>& encrypted, |
| 220 const DecryptCB& decrypt_cb) { | 272 const DecryptCB& decrypt_cb) { |
| 221 CHECK(encrypted->GetDecryptConfig()); | 273 CHECK(encrypted->GetDecryptConfig()); |
| 222 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); | 274 const std::string& key_id = encrypted->GetDecryptConfig()->key_id(); |
| 223 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); | |
| 224 | |
| 225 // TODO(xhwang): Avoid always constructing a string with StringPiece? | |
| 226 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); | |
| 227 | 275 |
| 228 DecryptionKey* key = NULL; | 276 DecryptionKey* key = NULL; |
| 229 { | 277 { |
| 230 base::AutoLock auto_lock(key_map_lock_); | 278 base::AutoLock auto_lock(key_map_lock_); |
| 231 KeyMap::const_iterator found = key_map_.find(key_id_string); | 279 KeyMap::const_iterator found = key_map_.find(key_id); |
| 232 if (found != key_map_.end()) | 280 if (found != key_map_.end()) |
| 233 key = found->second; | 281 key = found->second; |
| 234 } | 282 } |
| 235 | 283 |
| 236 if (!key) { | 284 if (!key) { |
| 237 // TODO(fgalligan): Fire a need_key event here and add a test. | 285 // 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."; | 286 DVLOG(1) << "Could not find a matching key for given key ID."; |
| 239 decrypt_cb.Run(kError, NULL); | 287 decrypt_cb.Run(kError, NULL); |
| 240 return; | 288 return; |
| 241 } | 289 } |
| 242 | 290 |
| 243 int checksum_size = encrypted->GetDecryptConfig()->checksum_size(); | 291 int checksum_size = encrypted->GetDecryptConfig()->checksum().size(); |
| 244 // According to the WebM encrypted specification, it is an open question | 292 // According to the WebM encrypted specification, it is an open question |
| 245 // what should happen when a frame fails the integrity check. | 293 // what should happen when a frame fails the integrity check. |
| 246 // http://wiki.webmproject.org/encryption/webm-encryption-rfc | 294 // http://wiki.webmproject.org/encryption/webm-encryption-rfc |
| 247 if (checksum_size > 0 && | 295 if (checksum_size > 0 && |
| 248 !key->hmac_key().empty() && | 296 !key->hmac_key().empty() && |
| 249 !CheckData(*encrypted, key->hmac_key())) { | 297 !CheckData(*encrypted, key->hmac_key())) { |
| 250 DVLOG(1) << "Integrity check failed."; | 298 DVLOG(1) << "Integrity check failed."; |
| 251 decrypt_cb.Run(kError, NULL); | 299 decrypt_cb.Run(kError, NULL); |
| 252 return; | 300 return; |
| 253 } | 301 } |
| 254 | 302 |
| 303 // TODO(strobe): Currently, presence of checksum is used to indicate the use | |
| 304 // of normal or WebM decryption keys. Consider a more explicit signaling | |
| 305 // mechanism and the removal of the webm_decryption_key member. | |
| 306 crypto::SymmetricKey* decryption_key = (checksum_size > 0) ? | |
| 307 key->webm_decryption_key() : key->decryption_key(); | |
| 255 scoped_refptr<DecoderBuffer> decrypted = | 308 scoped_refptr<DecoderBuffer> decrypted = |
| 256 DecryptData(*encrypted, | 309 DecryptData(*encrypted, decryption_key); |
| 257 key->decryption_key(), | |
| 258 encrypted->GetDecryptConfig()->encrypted_frame_offset()); | |
| 259 if (!decrypted) { | 310 if (!decrypted) { |
| 260 DVLOG(1) << "Decryption failed."; | 311 DVLOG(1) << "Decryption failed."; |
| 261 decrypt_cb.Run(kError, NULL); | 312 decrypt_cb.Run(kError, NULL); |
| 262 return; | 313 return; |
| 263 } | 314 } |
| 264 | 315 |
| 265 decrypted->SetTimestamp(encrypted->GetTimestamp()); | 316 decrypted->SetTimestamp(encrypted->GetTimestamp()); |
| 266 decrypted->SetDuration(encrypted->GetDuration()); | 317 decrypted->SetDuration(encrypted->GetDuration()); |
| 267 decrypt_cb.Run(kSuccess, decrypted); | 318 decrypt_cb.Run(kSuccess, decrypted); |
| 268 } | 319 } |
| 269 | 320 |
| 270 AesDecryptor::DecryptionKey::DecryptionKey( | 321 AesDecryptor::DecryptionKey::DecryptionKey( |
| 271 const std::string& secret) | 322 const std::string& secret) |
| 272 : secret_(secret) { | 323 : secret_(secret) { |
| 273 } | 324 } |
| 274 | 325 |
| 275 AesDecryptor::DecryptionKey::~DecryptionKey() {} | 326 AesDecryptor::DecryptionKey::~DecryptionKey() {} |
| 276 | 327 |
| 277 bool AesDecryptor::DecryptionKey::Init(bool derive_webm_keys) { | 328 bool AesDecryptor::DecryptionKey::Init() { |
| 278 CHECK(!secret_.empty()); | 329 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( | 330 decryption_key_.reset( |
| 301 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, secret_)); | 331 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, secret_)); |
| 302 if (!decryption_key_.get()) { | 332 if (!decryption_key_.get()) { |
| 303 return false; | 333 return false; |
| 304 } | 334 } |
| 335 | |
| 336 std::string raw_key = DeriveKey(secret_, | |
| 337 kWebmEncryptionSeed, | |
| 338 secret_.length()); | |
| 339 if (raw_key.empty()) { | |
| 340 return false; | |
| 341 } | |
| 342 webm_decryption_key_.reset( | |
| 343 crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, raw_key)); | |
| 344 if (!webm_decryption_key_.get()) { | |
| 345 return false; | |
| 346 } | |
| 347 | |
| 348 hmac_key_ = DeriveKey(secret_, kWebmHmacSeed, kWebmSha1DigestSize); | |
| 349 if (hmac_key_.empty()) { | |
| 350 return false; | |
| 351 } | |
| 352 | |
| 305 return true; | 353 return true; |
| 306 } | 354 } |
| 307 | 355 |
| 308 } // namespace media | 356 } // namespace media |
| OLD | NEW |