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 return DecoderBuffer::CopyFrom( |
Ami GONE FROM CHROMIUM
2012/05/30 05:19:27
Why drop the spirit of this TODO?
DaleCurtis
2012/05/30 17:33:47
Good point. Changed.
| |
47 // data copying. | |
48 return DataBuffer::CopyFrom( | |
49 reinterpret_cast<const uint8*>(decrypted_text.data()), | 46 reinterpret_cast<const uint8*>(decrypted_text.data()), |
50 decrypted_text.size()); | 47 decrypted_text.size()); |
51 } | 48 } |
52 | 49 |
53 AesDecryptor::AesDecryptor() {} | 50 AesDecryptor::AesDecryptor() {} |
54 | 51 |
55 AesDecryptor::~AesDecryptor() { | 52 AesDecryptor::~AesDecryptor() { |
56 STLDeleteValues(&key_map_); | 53 STLDeleteValues(&key_map_); |
57 } | 54 } |
58 | 55 |
(...skipping 15 matching lines...) Expand all Loading... | |
74 | 71 |
75 base::AutoLock auto_lock(lock_); | 72 base::AutoLock auto_lock(lock_); |
76 KeyMap::iterator found = key_map_.find(key_id_string); | 73 KeyMap::iterator found = key_map_.find(key_id_string); |
77 if (found != key_map_.end()) { | 74 if (found != key_map_.end()) { |
78 delete found->second; | 75 delete found->second; |
79 key_map_.erase(found); | 76 key_map_.erase(found); |
80 } | 77 } |
81 key_map_[key_id_string] = symmetric_key; | 78 key_map_[key_id_string] = symmetric_key; |
82 } | 79 } |
83 | 80 |
84 scoped_refptr<Buffer> AesDecryptor::Decrypt( | 81 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt( |
85 const scoped_refptr<Buffer>& encrypted) { | 82 const scoped_refptr<DecoderBuffer>& encrypted) { |
86 CHECK(encrypted->GetDecryptConfig()); | 83 CHECK(encrypted->GetDecryptConfig()); |
87 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); | 84 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); |
88 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); | 85 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); |
89 | 86 |
90 // TODO(xhwang): Avoid always constructing a string with StringPiece? | 87 // TODO(xhwang): Avoid always constructing a string with StringPiece? |
91 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); | 88 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); |
92 | 89 |
93 crypto::SymmetricKey* key = NULL; | 90 crypto::SymmetricKey* key = NULL; |
94 { | 91 { |
95 base::AutoLock auto_lock(lock_); | 92 base::AutoLock auto_lock(lock_); |
96 KeyMap::const_iterator found = key_map_.find(key_id_string); | 93 KeyMap::const_iterator found = key_map_.find(key_id_string); |
97 if (found == key_map_.end()) { | 94 if (found == key_map_.end()) { |
98 DVLOG(1) << "Could not find a matching key for given key ID."; | 95 DVLOG(1) << "Could not find a matching key for given key ID."; |
99 return NULL; | 96 return NULL; |
100 } | 97 } |
101 key = found->second; | 98 key = found->second; |
102 } | 99 } |
103 | 100 |
104 scoped_refptr<Buffer> decrypted = DecryptData(*encrypted, key); | 101 scoped_refptr<DecoderBuffer> decrypted = DecryptData(*encrypted, key); |
105 | 102 |
106 if (decrypted) { | 103 if (decrypted) { |
107 decrypted->SetTimestamp(encrypted->GetTimestamp()); | 104 decrypted->SetTimestamp(encrypted->GetTimestamp()); |
108 decrypted->SetDuration(encrypted->GetDuration()); | 105 decrypted->SetDuration(encrypted->GetDuration()); |
109 } | 106 } |
110 | 107 |
111 return decrypted; | 108 return decrypted; |
112 } | 109 } |
113 | 110 |
114 } // namespace media | 111 } // namespace media |
OLD | NEW |