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" | 10 #include "base/string_piece.h" |
11 #include "crypto/encryptor.h" | 11 #include "crypto/encryptor.h" |
12 #include "crypto/hmac.h" | |
12 #include "crypto/symmetric_key.h" | 13 #include "crypto/symmetric_key.h" |
13 #include "media/base/decoder_buffer.h" | 14 #include "media/base/decoder_buffer.h" |
14 #include "media/base/decrypt_config.h" | 15 #include "media/base/decrypt_config.h" |
15 #include "media/base/decryptor_client.h" | 16 #include "media/base/decryptor_client.h" |
16 | 17 |
17 namespace media { | 18 namespace media { |
18 | 19 |
19 // TODO(xhwang): Get real IV from frames. | |
20 static const char kInitialCounter[] = "0000000000000000"; | |
21 | |
22 uint32 AesDecryptor::next_session_id_ = 1; | 20 uint32 AesDecryptor::next_session_id_ = 1; |
23 | 21 |
24 // Decrypt |input| using |key|. | 22 // Derives a key from an HMAC using SHA1. |secret| is the base secret to derive |
ddorwin
2012/07/10 01:12:20
s/from an HMAC using SHA1/using a SHA1 HMAC/ ?
fgalligan1
2012/07/11 22:06:33
Done.
| |
23 // the key from. |seed| is the kwnon input to the HMAC. |key_size| is how many | |
ddorwin
2012/07/10 01:12:20
known
fgalligan1
2012/07/11 22:06:33
Done.
| |
24 // bytes are returned in the key. Returns a string containing the key on | |
25 // success. Returns an empty string on failure. | |
26 static std::string DeriveKey(const std::string& secret, | |
27 const std::string& seed, | |
28 int key_size) { | |
xhwang
2012/07/10 06:31:25
Can we pass |secret| and |seed| as StringPiece? Th
fgalligan1
2012/07/11 22:06:33
Done.
| |
29 CHECK(!secret.empty()); | |
30 CHECK(!seed.empty()); | |
31 CHECK_GT(key_size, 0); | |
32 | |
33 std::string key; | |
34 crypto::HMAC hmac(crypto::HMAC::SHA1); | |
35 if (!hmac.Init(reinterpret_cast<const uint8*>(secret.data()), | |
36 secret.size())) { | |
37 DVLOG(1) << "Could not initialize HMAC with secret data."; | |
38 return key; | |
ddorwin
2012/07/10 01:12:20
It might be more readable to just return an empty
fgalligan1
2012/07/11 22:06:33
Done.
| |
39 } | |
40 | |
41 uint8 calculated_hmac[AesDecryptor::kSha1DigestSize]; | |
42 if (!hmac.Sign(seed, calculated_hmac, AesDecryptor::kSha1DigestSize)) { | |
43 DVLOG(1) << "Could not calculate HMAC."; | |
44 return key; | |
45 } | |
46 key.assign(reinterpret_cast<const char*>(calculated_hmac), key_size); | |
ddorwin
2012/07/10 01:12:20
empty line for consistency
fgalligan1
2012/07/11 22:06:33
Done.
| |
47 return key; | |
ddorwin
2012/07/10 01:12:20
You can combine 33, 46-47 into
return std::string(
fgalligan1
2012/07/11 22:06:33
Done.
| |
48 } | |
49 | |
50 // Integrity check of |input|'s data. Checks that the first | |
51 // |kWebMIntegrityCheckSize| in bytes of |input| matches the rest of the data | |
ddorwin
2012/07/10 01:12:20
Comment is out of date.
fgalligan1
2012/07/11 22:06:33
Done.
| |
52 // in |input|. The check is using the SHA1 algorithm. |hmac_key| is the key of | |
53 // the HMAC algorithm. Returns true if the integrity check passes. | |
54 static bool CheckData(const DecoderBuffer& input, | |
55 const std::string& hmac_key) { | |
xhwang
2012/07/10 06:31:25
ditto, pass |hmac_key| as string piece.
fgalligan1
2012/07/11 22:06:33
Done.
| |
56 CHECK(input.GetDataSize()); | |
57 CHECK(input.GetDecryptConfig()); | |
58 CHECK(!hmac_key.empty()); | |
59 | |
60 crypto::HMAC hmac(crypto::HMAC::SHA1); | |
61 if (!hmac.Init(reinterpret_cast<const uint8*>(hmac_key.data()), | |
62 hmac_key.size())) { | |
63 DVLOG(1) << "Could not initialize HMAC."; | |
ddorwin
2012/07/10 01:12:20
Are all these errors likely to occur? It seems the
fgalligan1
2012/07/11 22:06:33
I'm not really sure, but I'm guessing these errors
| |
64 return false; | |
65 } | |
66 | |
67 // The HMAC covers the IV and the frame data. | |
68 base::StringPiece data_to_check( | |
69 reinterpret_cast<const char*>(input.GetData()), input.GetDataSize()); | |
70 | |
71 uint8 calculated_hmac[AesDecryptor::kSha1DigestSize]; | |
72 if (!hmac.Sign(data_to_check, | |
73 calculated_hmac, | |
74 AesDecryptor::kSha1DigestSize)) { | |
ddorwin
2012/07/10 01:12:20
arraysize(calculated_hmac)?
fgalligan1
2012/07/11 22:06:33
Done.
| |
75 DVLOG(1) << "Could not calculate HMAC."; | |
76 return false; | |
77 } | |
78 | |
79 if (memcmp(input.GetDecryptConfig()->data_to_verify(), | |
ddorwin
2012/07/10 01:12:20
"data_to_verify" makes me think it is the data I a
fgalligan1
2012/07/11 22:06:33
I changed the name to "checksum". Sound better to
| |
80 calculated_hmac, | |
81 input.GetDecryptConfig()->data_to_verify_size()) != 0) { | |
ddorwin
2012/07/10 01:12:20
Need to ensure that both buffers are the same size
fgalligan1
2012/07/11 22:06:33
Currently the two sizes are different. checksum_si
| |
82 DVLOG(1) << "Integrity check failure."; | |
83 return false; | |
84 } | |
85 return true; | |
86 } | |
87 | |
88 // Generates a 16 byte CTR counter block. The format is | |
89 // | iv | block counter |. |iv| is a CTR IV. |iv_size| is the size | |
xhwang
2012/07/10 06:31:25
Can we have "| iv | block counter |" on a separate
fgalligan1
2012/07/11 22:06:33
Changed too "Generates a 16 byte CTR counter block
| |
90 // of |iv| in bytes. Returns counter block on success. Returns empty string | |
91 // on failure. | |
92 static std::string GenerateCounterBlock(const uint8* iv, int iv_size) { | |
93 std::string counter_block; | |
94 if (iv_size <= 0 || iv_size > AesDecryptor::kKeySize) | |
95 return counter_block; | |
ddorwin
2012/07/10 01:12:20
same - return empty string and return a constructe
fgalligan1
2012/07/11 22:06:33
Done.
| |
96 | |
97 char counter_block_data[AesDecryptor::kKeySize]; | |
98 | |
99 // Set the IV. | |
100 memcpy(counter_block_data, iv, iv_size); | |
101 | |
102 // Set block counter to all 0's. | |
ddorwin
2012/07/10 01:12:20
Is this comment accurate? Or are we just "zero-ext
fgalligan1
2012/07/11 22:06:33
Yes and Yes. It probably is confusing because "blo
| |
103 memset(counter_block_data + iv_size, | |
104 0, | |
105 AesDecryptor::kKeySize - iv_size); | |
ddorwin
2012/07/10 01:12:20
Is it okay for the size to be 0?
fgalligan1
2012/07/11 22:06:33
Should be.
| |
106 | |
107 counter_block.assign(counter_block_data, AesDecryptor::kKeySize); | |
108 return counter_block; | |
109 } | |
110 | |
111 // Decrypt |input| using |key|. |offset| is the number of bytes into |input| | |
ddorwin
2012/07/10 01:12:20
s/offset/encrypted_data_offset/ (or just data_offs
fgalligan1
2012/07/11 22:06:33
Done.
| |
112 // the encrypted data is. | |
ddorwin
2012/07/10 01:12:20
that the encrypted data starts.
fgalligan1
2012/07/11 22:06:33
Done.
| |
25 // Return a DecoderBuffer with the decrypted data if decryption succeeded. | 113 // Return a DecoderBuffer with the decrypted data if decryption succeeded. |
26 // Return NULL if decryption failed. | 114 // Return NULL if decryption failed. |
27 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, | 115 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, |
28 crypto::SymmetricKey* key) { | 116 crypto::SymmetricKey* key, |
117 int offset) { | |
29 CHECK(input.GetDataSize()); | 118 CHECK(input.GetDataSize()); |
119 CHECK(input.GetDecryptConfig()); | |
30 CHECK(key); | 120 CHECK(key); |
31 | 121 |
32 // Initialize encryption data. | 122 // Initialize encryption data. |
33 // The IV must be exactly as long as the cipher block size. | |
34 crypto::Encryptor encryptor; | 123 crypto::Encryptor encryptor; |
35 if (!encryptor.Init(key, crypto::Encryptor::CBC, kInitialCounter)) { | 124 if (!encryptor.Init(key, crypto::Encryptor::CTR, "")) { |
36 DVLOG(1) << "Could not initialize encryptor."; | 125 DVLOG(1) << "Could not initialize encryptor."; |
37 return NULL; | 126 return NULL; |
38 } | 127 } |
39 | 128 |
129 // Set the counter block. | |
130 std::string counter_block = | |
131 GenerateCounterBlock(input.GetDecryptConfig()->iv(), | |
132 input.GetDecryptConfig()->iv_size()); | |
133 if (counter_block.empty()) { | |
134 DVLOG(1) << "Could not generate counter block."; | |
135 return NULL; | |
136 } | |
137 if (!encryptor.SetCounter(counter_block)) { | |
138 DVLOG(1) << "Could not set counter block."; | |
139 return NULL; | |
140 } | |
141 | |
40 std::string decrypted_text; | 142 std::string decrypted_text; |
41 base::StringPiece encrypted_text( | 143 const char* frame = reinterpret_cast<const char*>(input.GetData() + offset); |
42 reinterpret_cast<const char*>(input.GetData()), | 144 int frame_size = input.GetDataSize() - offset; |
43 input.GetDataSize()); | 145 base::StringPiece encrypted_text(frame, frame_size); |
44 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { | 146 if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) { |
45 DVLOG(1) << "Could not decrypt data."; | 147 DVLOG(1) << "Could not decrypt data."; |
46 return NULL; | 148 return NULL; |
47 } | 149 } |
48 | 150 |
49 // TODO(xhwang): Find a way to avoid this data copy. | 151 // TODO(xhwang): Find a way to avoid this data copy. |
50 return DecoderBuffer::CopyFrom( | 152 return DecoderBuffer::CopyFrom( |
51 reinterpret_cast<const uint8*>(decrypted_text.data()), | 153 reinterpret_cast<const uint8*>(decrypted_text.data()), |
52 decrypted_text.size()); | 154 decrypted_text.size()); |
53 } | 155 } |
54 | 156 |
157 const char AesDecryptor::kHmacSeed[] = "hmac-key"; | |
158 const char AesDecryptor::kEncryptionSeed[] = "encryption-key"; | |
159 | |
55 AesDecryptor::AesDecryptor(DecryptorClient* client) | 160 AesDecryptor::AesDecryptor(DecryptorClient* client) |
56 : client_(client) { | 161 : client_(client) { |
57 } | 162 } |
58 | 163 |
59 AesDecryptor::~AesDecryptor() { | 164 AesDecryptor::~AesDecryptor() { |
60 STLDeleteValues(&key_map_); | 165 STLDeleteValues(&keys_map_); |
61 } | 166 } |
62 | 167 |
63 void AesDecryptor::GenerateKeyRequest(const std::string& key_system, | 168 void AesDecryptor::GenerateKeyRequest(const std::string& key_system, |
64 const uint8* init_data, | 169 const uint8* init_data, |
65 int init_data_length) { | 170 int init_data_length) { |
66 std::string session_id_string(base::UintToString(next_session_id_++)); | 171 std::string session_id_string(base::UintToString(next_session_id_++)); |
67 | 172 |
68 // For now, just fire the event with the |init_data| as the request. | 173 // For now, just fire the event with the |init_data| as the request. |
69 int message_length = init_data_length; | 174 int message_length = init_data_length; |
70 scoped_array<uint8> message(new uint8[message_length]); | 175 scoped_array<uint8> message(new uint8[message_length]); |
(...skipping 28 matching lines...) Expand all Loading... | |
99 if (!init_data) { | 204 if (!init_data) { |
100 init_data = kDummyInitData; | 205 init_data = kDummyInitData; |
101 init_data_length = arraysize(kDummyInitData); | 206 init_data_length = arraysize(kDummyInitData); |
102 } | 207 } |
103 | 208 |
104 // TODO(xhwang): For now, use |init_data| for key ID. Make this more spec | 209 // 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). | 210 // compliant later (http://crbug.com/123262, http://crbug.com/123265). |
106 std::string key_id_string(reinterpret_cast<const char*>(init_data), | 211 std::string key_id_string(reinterpret_cast<const char*>(init_data), |
107 init_data_length); | 212 init_data_length); |
108 std::string key_string(reinterpret_cast<const char*>(key) , key_length); | 213 std::string key_string(reinterpret_cast<const char*>(key) , key_length); |
109 crypto::SymmetricKey* symmetric_key = crypto::SymmetricKey::Import( | 214 HmacEncryptionKeys* keys = new HmacEncryptionKeys(key_string); |
ddorwin
2012/07/10 01:12:20
Could we generalize the class right here? WebM doe
xhwang
2012/07/10 06:31:25
Use scoped_ptr here to make ownership clear. Then
fgalligan1
2012/07/11 22:06:33
I can't Pass() to the map. Can I move scoped_ptr<>
fgalligan1
2012/07/11 22:06:33
Changed the variable name to key_map.
I think we
ddorwin
2012/07/13 00:48:00
Pass is for refptr, which doesn't make sense here.
ddorwin
2012/07/13 00:48:00
I think you should do the init() here. It's more n
xhwang
2012/07/13 01:23:21
Pass() is for scoped_ptr, not scoped_refptr. The p
xhwang
2012/07/13 01:28:10
But as ddrowin suggested, you should still use sco
| |
110 crypto::SymmetricKey::AES, key_string); | 215 if (!keys) { |
111 if (!symmetric_key) { | 216 DVLOG(1) << "Could not create keys."; |
112 DVLOG(1) << "Could not import key."; | 217 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); |
218 return; | |
219 } | |
220 if (!keys->Init()) { | |
221 delete keys; | |
222 DVLOG(1) << "Could not create keys."; | |
113 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); | 223 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); |
114 return; | 224 return; |
115 } | 225 } |
116 | 226 |
117 { | 227 { |
118 base::AutoLock auto_lock(key_map_lock_); | 228 base::AutoLock auto_lock(keys_map_lock_); |
119 KeyMap::iterator found = key_map_.find(key_id_string); | 229 KeysMap::iterator found = keys_map_.find(key_id_string); |
120 if (found != key_map_.end()) { | 230 if (found != keys_map_.end()) { |
121 delete found->second; | 231 delete found->second; |
122 key_map_.erase(found); | 232 keys_map_.erase(found); |
123 } | 233 } |
124 key_map_[key_id_string] = symmetric_key; | 234 keys_map_[key_id_string] = keys; |
125 } | 235 } |
126 | 236 |
127 client_->KeyAdded(key_system, session_id); | 237 client_->KeyAdded(key_system, session_id); |
128 } | 238 } |
129 | 239 |
130 void AesDecryptor::CancelKeyRequest(const std::string& key_system, | 240 void AesDecryptor::CancelKeyRequest(const std::string& key_system, |
131 const std::string& session_id) { | 241 const std::string& session_id) { |
132 } | 242 } |
133 | 243 |
134 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt( | 244 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt( |
135 const scoped_refptr<DecoderBuffer>& encrypted) { | 245 const scoped_refptr<DecoderBuffer>& encrypted) { |
136 CHECK(encrypted->GetDecryptConfig()); | 246 CHECK(encrypted->GetDecryptConfig()); |
137 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); | 247 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); |
138 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); | 248 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); |
139 | 249 |
140 // TODO(xhwang): Avoid always constructing a string with StringPiece? | 250 // TODO(xhwang): Avoid always constructing a string with StringPiece? |
141 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); | 251 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); |
142 | 252 |
143 crypto::SymmetricKey* key = NULL; | 253 HmacEncryptionKeys* keys = NULL; |
144 { | 254 { |
145 base::AutoLock auto_lock(key_map_lock_); | 255 base::AutoLock auto_lock(keys_map_lock_); |
146 KeyMap::const_iterator found = key_map_.find(key_id_string); | 256 KeysMap::const_iterator found = keys_map_.find(key_id_string); |
147 if (found == key_map_.end()) { | 257 if (found == keys_map_.end()) { |
148 DVLOG(1) << "Could not find a matching key for given key ID."; | 258 DVLOG(1) << "Could not find a matching key for given key ID."; |
149 return NULL; | 259 return NULL; |
150 } | 260 } |
151 key = found->second; | 261 keys = found->second; |
152 } | 262 } |
153 | 263 |
154 scoped_refptr<DecoderBuffer> decrypted = DecryptData(*encrypted, key); | 264 int verify_size = encrypted->GetDecryptConfig()->data_to_verify_size(); |
265 if (verify_size > 0 && !CheckData(*encrypted, keys->hmac_key())) { | |
ddorwin
2012/07/10 01:12:20
Might have to check that hmac_key() is not NULL he
fgalligan1
2012/07/11 22:06:33
Currently ISO does not define an IC. If they do we
ddorwin
2012/07/13 00:48:00
I think I meant we shouldn't call CheckData() if k
| |
266 DVLOG(1) << "Integrity check failed."; | |
267 // TODO(fgalligan): Should we signal a decryptor error here? | |
ddorwin
2012/07/10 01:12:20
I think so. This was clearly a problem related to
fgalligan1
2012/07/11 22:06:33
I updated the comment with an explanation of what
| |
268 return NULL; | |
269 } | |
155 | 270 |
271 scoped_refptr<DecoderBuffer> decrypted = | |
272 DecryptData(*encrypted, | |
273 keys->encryption_key(), | |
274 encrypted->GetDecryptConfig()->offset_to_data()); | |
156 if (decrypted) { | 275 if (decrypted) { |
157 decrypted->SetTimestamp(encrypted->GetTimestamp()); | 276 decrypted->SetTimestamp(encrypted->GetTimestamp()); |
158 decrypted->SetDuration(encrypted->GetDuration()); | 277 decrypted->SetDuration(encrypted->GetDuration()); |
159 } | 278 } |
160 | 279 |
161 return decrypted; | 280 return decrypted; |
162 } | 281 } |
163 | 282 |
283 AesDecryptor::HmacEncryptionKeys::HmacEncryptionKeys( | |
284 const std::string& secret) | |
285 : secret_(secret) { | |
286 } | |
287 | |
288 AesDecryptor::HmacEncryptionKeys::~HmacEncryptionKeys() {} | |
289 | |
290 bool AesDecryptor::HmacEncryptionKeys::Init() { | |
291 CHECK(!secret_.empty()); | |
292 | |
293 std::string raw_key = DeriveKey(secret_, | |
294 kEncryptionSeed, | |
295 secret_.length()); | |
296 if (raw_key.empty()) { | |
297 DVLOG(1) << "Could not create encryption key."; | |
298 return false; | |
299 } | |
300 encryption_key_.reset(crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, | |
301 raw_key)); | |
302 if (!encryption_key_.get()) { | |
303 DVLOG(1) << "Could not create encryption key."; | |
ddorwin
2012/07/10 01:12:20
"... import decryption key."
but probably just del
fgalligan1
2012/07/11 22:06:33
Done.
| |
304 return false; | |
305 } | |
306 | |
307 hmac_key_ = DeriveKey(secret_, kHmacSeed, kSha1DigestSize); | |
308 if (hmac_key_.empty()) { | |
309 DVLOG(1) << "Could not create HMAC key."; | |
310 return false; | |
311 } | |
xhwang
2012/07/10 06:31:25
Add empty line here to be consistent.
fgalligan1
2012/07/11 22:06:33
Done.
| |
312 return true; | |
313 } | |
314 | |
164 } // namespace media | 315 } // namespace media |
OLD | NEW |