| 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_piece.h" | 9 #include "base/string_piece.h" |
| 10 #include "crypto/encryptor.h" | 10 #include "crypto/encryptor.h" |
| 11 #include "crypto/symmetric_key.h" | 11 #include "crypto/symmetric_key.h" |
| 12 #include "media/base/buffers.h" | 12 #include "media/base/decoder_buffer.h" |
| 13 #include "media/base/data_buffer.h" | |
| 14 #include "media/base/decrypt_config.h" | 13 #include "media/base/decrypt_config.h" |
| 15 | 14 |
| 16 namespace media { | 15 namespace media { |
| 17 | 16 |
| 18 // TODO(xhwang): Get real IV from frames. | 17 // TODO(xhwang): Get real IV from frames. |
| 19 static const char kInitialCounter[] = "0000000000000000"; | 18 static const char kInitialCounter[] = "0000000000000000"; |
| 20 | 19 |
| 21 // Decrypt |input| using |key|. | 20 // Decrypt |input| using |key|. |
| 22 // Return a scoped_refptr to a Buffer object with the decrypted data on success. | 21 // Return a DecoderBuffer with the decrypted data if decryption succeeded. |
| 23 // Return a scoped_refptr to NULL if the data could not be decrypted. | 22 // Return NULL if decryption failed. |
| 24 static scoped_refptr<Buffer> DecryptData(const Buffer& input, | 23 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, |
| 25 crypto::SymmetricKey* key) { | 24 crypto::SymmetricKey* key) { |
| 26 CHECK(input.GetDataSize()); | 25 CHECK(input.GetDataSize()); |
| 27 CHECK(key); | 26 CHECK(key); |
| 28 | 27 |
| 29 // Initialize encryption data. | 28 // Initialize encryption data. |
| 30 // The IV must be exactly as long as the cipher block size. | 29 // The IV must be exactly as long as the cipher block size. |
| 31 crypto::Encryptor encryptor; | 30 crypto::Encryptor encryptor; |
| 32 if (!encryptor.Init(key, crypto::Encryptor::CBC, kInitialCounter)) { | 31 if (!encryptor.Init(key, crypto::Encryptor::CBC, kInitialCounter)) { |
| 33 DVLOG(1) << "Could not initialize encryptor."; | 32 DVLOG(1) << "Could not initialize encryptor."; |
| 34 return NULL; | 33 return NULL; |
| 35 } | 34 } |
| 36 | 35 |
| 37 std::string decrypted_text; | 36 std::string decrypted_text; |
| 38 base::StringPiece encrypted_text( | 37 base::StringPiece encrypted_text( |
| 39 reinterpret_cast<const char*>(input.GetData()), | 38 reinterpret_cast<const char*>(input.GetData()), |
| 40 input.GetDataSize()); | 39 input.GetDataSize()); |
| 41 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { | 40 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { |
| 42 DVLOG(1) << "Could not decrypt data."; | 41 DVLOG(1) << "Could not decrypt data."; |
| 43 return NULL; | 42 return NULL; |
| 44 } | 43 } |
| 45 | 44 |
| 46 // TODO(xhwang): Implement a string based Buffer implementation to avoid | 45 // TODO(xhwang): Find a way to avoid this data copy. |
| 47 // data copying. | 46 return DecoderBuffer::CopyFrom( |
| 48 return DataBuffer::CopyFrom( | |
| 49 reinterpret_cast<const uint8*>(decrypted_text.data()), | 47 reinterpret_cast<const uint8*>(decrypted_text.data()), |
| 50 decrypted_text.size()); | 48 decrypted_text.size()); |
| 51 } | 49 } |
| 52 | 50 |
| 53 AesDecryptor::AesDecryptor() {} | 51 AesDecryptor::AesDecryptor() {} |
| 54 | 52 |
| 55 AesDecryptor::~AesDecryptor() { | 53 AesDecryptor::~AesDecryptor() { |
| 56 STLDeleteValues(&key_map_); | 54 STLDeleteValues(&key_map_); |
| 57 } | 55 } |
| 58 | 56 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 74 | 72 |
| 75 base::AutoLock auto_lock(lock_); | 73 base::AutoLock auto_lock(lock_); |
| 76 KeyMap::iterator found = key_map_.find(key_id_string); | 74 KeyMap::iterator found = key_map_.find(key_id_string); |
| 77 if (found != key_map_.end()) { | 75 if (found != key_map_.end()) { |
| 78 delete found->second; | 76 delete found->second; |
| 79 key_map_.erase(found); | 77 key_map_.erase(found); |
| 80 } | 78 } |
| 81 key_map_[key_id_string] = symmetric_key; | 79 key_map_[key_id_string] = symmetric_key; |
| 82 } | 80 } |
| 83 | 81 |
| 84 scoped_refptr<Buffer> AesDecryptor::Decrypt( | 82 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt( |
| 85 const scoped_refptr<Buffer>& encrypted) { | 83 const scoped_refptr<DecoderBuffer>& encrypted) { |
| 86 CHECK(encrypted->GetDecryptConfig()); | 84 CHECK(encrypted->GetDecryptConfig()); |
| 87 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); | 85 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); |
| 88 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); | 86 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); |
| 89 | 87 |
| 90 // TODO(xhwang): Avoid always constructing a string with StringPiece? | 88 // TODO(xhwang): Avoid always constructing a string with StringPiece? |
| 91 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); | 89 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); |
| 92 | 90 |
| 93 crypto::SymmetricKey* key = NULL; | 91 crypto::SymmetricKey* key = NULL; |
| 94 { | 92 { |
| 95 base::AutoLock auto_lock(lock_); | 93 base::AutoLock auto_lock(lock_); |
| 96 KeyMap::const_iterator found = key_map_.find(key_id_string); | 94 KeyMap::const_iterator found = key_map_.find(key_id_string); |
| 97 if (found == key_map_.end()) { | 95 if (found == key_map_.end()) { |
| 98 DVLOG(1) << "Could not find a matching key for given key ID."; | 96 DVLOG(1) << "Could not find a matching key for given key ID."; |
| 99 return NULL; | 97 return NULL; |
| 100 } | 98 } |
| 101 key = found->second; | 99 key = found->second; |
| 102 } | 100 } |
| 103 | 101 |
| 104 scoped_refptr<Buffer> decrypted = DecryptData(*encrypted, key); | 102 scoped_refptr<DecoderBuffer> decrypted = DecryptData(*encrypted, key); |
| 105 | 103 |
| 106 if (decrypted) { | 104 if (decrypted) { |
| 107 decrypted->SetTimestamp(encrypted->GetTimestamp()); | 105 decrypted->SetTimestamp(encrypted->GetTimestamp()); |
| 108 decrypted->SetDuration(encrypted->GetDuration()); | 106 decrypted->SetDuration(encrypted->GetDuration()); |
| 109 } | 107 } |
| 110 | 108 |
| 111 return decrypted; | 109 return decrypted; |
| 112 } | 110 } |
| 113 | 111 |
| 114 } // namespace media | 112 } // namespace media |
| OLD | NEW |