| 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 <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "crypto/encryptor.h" | 12 #include "crypto/encryptor.h" |
| 13 #include "crypto/symmetric_key.h" | 13 #include "crypto/symmetric_key.h" |
| 14 #include "media/base/audio_decoder_config.h" | 14 #include "media/base/audio_decoder_config.h" |
| 15 #include "media/base/decoder_buffer.h" | 15 #include "media/base/decoder_buffer.h" |
| 16 #include "media/base/decrypt_config.h" | 16 #include "media/base/decrypt_config.h" |
| 17 #include "media/base/decryptor_client.h" | |
| 18 #include "media/base/video_decoder_config.h" | 17 #include "media/base/video_decoder_config.h" |
| 19 #include "media/base/video_frame.h" | 18 #include "media/base/video_frame.h" |
| 20 | 19 |
| 21 namespace media { | 20 namespace media { |
| 22 | 21 |
| 23 uint32 AesDecryptor::next_session_id_ = 1; | 22 uint32 AesDecryptor::next_session_id_ = 1; |
| 24 | 23 |
| 25 enum ClearBytesBufferSel { | 24 enum ClearBytesBufferSel { |
| 26 kSrcContainsClearBytes, | 25 kSrcContainsClearBytes, |
| 27 kDstContainsClearBytes | 26 kDstContainsClearBytes |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 } | 117 } |
| 119 | 118 |
| 120 scoped_refptr<DecoderBuffer> output = DecoderBuffer::CopyFrom( | 119 scoped_refptr<DecoderBuffer> output = DecoderBuffer::CopyFrom( |
| 121 reinterpret_cast<const uint8*>(sample), sample_size); | 120 reinterpret_cast<const uint8*>(sample), sample_size); |
| 122 CopySubsamples(subsamples, kDstContainsClearBytes, | 121 CopySubsamples(subsamples, kDstContainsClearBytes, |
| 123 reinterpret_cast<const uint8*>(decrypted_text.data()), | 122 reinterpret_cast<const uint8*>(decrypted_text.data()), |
| 124 output->GetWritableData()); | 123 output->GetWritableData()); |
| 125 return output; | 124 return output; |
| 126 } | 125 } |
| 127 | 126 |
| 128 AesDecryptor::AesDecryptor(DecryptorClient* client) | 127 AesDecryptor::AesDecryptor(const KeyAddedCB& key_added_cb, |
| 129 : client_(client) { | 128 const KeyErrorCB& key_error_cb, |
| 129 const KeyMessageCB& key_message_cb, |
| 130 const NeedKeyCB& need_key_cb) |
| 131 : key_added_cb_(key_added_cb), |
| 132 key_error_cb_(key_error_cb), |
| 133 key_message_cb_(key_message_cb), |
| 134 need_key_cb_(need_key_cb) { |
| 130 } | 135 } |
| 131 | 136 |
| 132 AesDecryptor::~AesDecryptor() { | 137 AesDecryptor::~AesDecryptor() { |
| 133 STLDeleteValues(&key_map_); | 138 STLDeleteValues(&key_map_); |
| 134 } | 139 } |
| 135 | 140 |
| 136 bool AesDecryptor::GenerateKeyRequest(const std::string& key_system, | 141 bool AesDecryptor::GenerateKeyRequest(const std::string& key_system, |
| 137 const std::string& type, | 142 const std::string& type, |
| 138 const uint8* init_data, | 143 const uint8* init_data, |
| 139 int init_data_length) { | 144 int init_data_length) { |
| 140 std::string session_id_string(base::UintToString(next_session_id_++)); | 145 std::string session_id_string(base::UintToString(next_session_id_++)); |
| 141 | 146 |
| 142 // For now, the AesDecryptor does not care about |key_system| and |type|; | 147 // For now, the AesDecryptor does not care about |key_system| and |type|; |
| 143 // just fire the event with the |init_data| as the request. | 148 // just fire the event with the |init_data| as the request. |
| 144 std::string message; | 149 std::string message; |
| 145 if (init_data && init_data_length) { | 150 if (init_data && init_data_length) { |
| 146 message = std::string(reinterpret_cast<const char*>(init_data), | 151 message = std::string(reinterpret_cast<const char*>(init_data), |
| 147 init_data_length); | 152 init_data_length); |
| 148 } | 153 } |
| 149 | 154 |
| 150 client_->KeyMessage(key_system, session_id_string, message, ""); | 155 key_message_cb_.Run(key_system, session_id_string, message, ""); |
| 151 return true; | 156 return true; |
| 152 } | 157 } |
| 153 | 158 |
| 154 void AesDecryptor::AddKey(const std::string& key_system, | 159 void AesDecryptor::AddKey(const std::string& key_system, |
| 155 const uint8* key, | 160 const uint8* key, |
| 156 int key_length, | 161 int key_length, |
| 157 const uint8* init_data, | 162 const uint8* init_data, |
| 158 int init_data_length, | 163 int init_data_length, |
| 159 const std::string& session_id) { | 164 const std::string& session_id) { |
| 160 CHECK(key); | 165 CHECK(key); |
| 161 CHECK_GT(key_length, 0); | 166 CHECK_GT(key_length, 0); |
| 162 | 167 |
| 163 // TODO(xhwang): Add |session_id| check after we figure out how: | 168 // TODO(xhwang): Add |session_id| check after we figure out how: |
| 164 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16550 | 169 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16550 |
| 165 if (key_length != DecryptConfig::kDecryptionKeySize) { | 170 if (key_length != DecryptConfig::kDecryptionKeySize) { |
| 166 DVLOG(1) << "Invalid key length: " << key_length; | 171 DVLOG(1) << "Invalid key length: " << key_length; |
| 167 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); | 172 key_error_cb_.Run(key_system, session_id, Decryptor::kUnknownError, 0); |
| 168 return; | 173 return; |
| 169 } | 174 } |
| 170 | 175 |
| 171 // TODO(xhwang): Fix the decryptor to accept no |init_data|. See | 176 // TODO(xhwang): Fix the decryptor to accept no |init_data|. See |
| 172 // http://crbug.com/123265. Until then, ensure a non-empty value is passed. | 177 // http://crbug.com/123265. Until then, ensure a non-empty value is passed. |
| 173 static const uint8 kDummyInitData[1] = { 0 }; | 178 static const uint8 kDummyInitData[1] = { 0 }; |
| 174 if (!init_data) { | 179 if (!init_data) { |
| 175 init_data = kDummyInitData; | 180 init_data = kDummyInitData; |
| 176 init_data_length = arraysize(kDummyInitData); | 181 init_data_length = arraysize(kDummyInitData); |
| 177 } | 182 } |
| 178 | 183 |
| 179 // TODO(xhwang): For now, use |init_data| for key ID. Make this more spec | 184 // TODO(xhwang): For now, use |init_data| for key ID. Make this more spec |
| 180 // compliant later (http://crbug.com/123262, http://crbug.com/123265). | 185 // compliant later (http://crbug.com/123262, http://crbug.com/123265). |
| 181 std::string key_id_string(reinterpret_cast<const char*>(init_data), | 186 std::string key_id_string(reinterpret_cast<const char*>(init_data), |
| 182 init_data_length); | 187 init_data_length); |
| 183 std::string key_string(reinterpret_cast<const char*>(key) , key_length); | 188 std::string key_string(reinterpret_cast<const char*>(key) , key_length); |
| 184 scoped_ptr<DecryptionKey> decryption_key(new DecryptionKey(key_string)); | 189 scoped_ptr<DecryptionKey> decryption_key(new DecryptionKey(key_string)); |
| 185 if (!decryption_key.get()) { | 190 if (!decryption_key.get()) { |
| 186 DVLOG(1) << "Could not create key."; | 191 DVLOG(1) << "Could not create key."; |
| 187 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); | 192 key_error_cb_.Run(key_system, session_id, Decryptor::kUnknownError, 0); |
| 188 return; | 193 return; |
| 189 } | 194 } |
| 190 | 195 |
| 191 if (!decryption_key->Init()) { | 196 if (!decryption_key->Init()) { |
| 192 DVLOG(1) << "Could not initialize decryption key."; | 197 DVLOG(1) << "Could not initialize decryption key."; |
| 193 client_->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); | 198 key_error_cb_.Run(key_system, session_id, Decryptor::kUnknownError, 0); |
| 194 return; | 199 return; |
| 195 } | 200 } |
| 196 | 201 |
| 197 SetKey(key_id_string, decryption_key.Pass()); | 202 SetKey(key_id_string, decryption_key.Pass()); |
| 198 | 203 |
| 199 if (!audio_key_added_cb_.is_null()) | 204 if (!new_audio_key_cb_.is_null()) |
| 200 audio_key_added_cb_.Run(); | 205 new_audio_key_cb_.Run(); |
| 201 | 206 |
| 202 if (!video_key_added_cb_.is_null()) | 207 if (!new_video_key_cb_.is_null()) |
| 203 video_key_added_cb_.Run(); | 208 new_video_key_cb_.Run(); |
| 204 | 209 |
| 205 client_->KeyAdded(key_system, session_id); | 210 key_added_cb_.Run(key_system, session_id); |
| 206 } | 211 } |
| 207 | 212 |
| 208 void AesDecryptor::CancelKeyRequest(const std::string& key_system, | 213 void AesDecryptor::CancelKeyRequest(const std::string& key_system, |
| 209 const std::string& session_id) { | 214 const std::string& session_id) { |
| 210 } | 215 } |
| 211 | 216 |
| 212 void AesDecryptor::RegisterKeyAddedCB(StreamType stream_type, | 217 void AesDecryptor::RegisterNewKeyCB(StreamType stream_type, |
| 213 const KeyAddedCB& key_added_cb) { | 218 const NewKeyCB& new_key_cb) { |
| 214 switch (stream_type) { | 219 switch (stream_type) { |
| 215 case kAudio: | 220 case kAudio: |
| 216 audio_key_added_cb_ = key_added_cb; | 221 new_audio_key_cb_ = new_key_cb; |
| 217 break; | 222 break; |
| 218 case kVideo: | 223 case kVideo: |
| 219 video_key_added_cb_ = key_added_cb; | 224 new_video_key_cb_ = new_key_cb; |
| 220 break; | 225 break; |
| 221 default: | 226 default: |
| 222 NOTREACHED(); | 227 NOTREACHED(); |
| 223 } | 228 } |
| 224 } | 229 } |
| 225 | 230 |
| 226 void AesDecryptor::Decrypt(StreamType stream_type, | 231 void AesDecryptor::Decrypt(StreamType stream_type, |
| 227 const scoped_refptr<DecoderBuffer>& encrypted, | 232 const scoped_refptr<DecoderBuffer>& encrypted, |
| 228 const DecryptCB& decrypt_cb) { | 233 const DecryptCB& decrypt_cb) { |
| 229 CHECK(encrypted->GetDecryptConfig()); | 234 CHECK(encrypted->GetDecryptConfig()); |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 bool AesDecryptor::DecryptionKey::Init() { | 328 bool AesDecryptor::DecryptionKey::Init() { |
| 324 CHECK(!secret_.empty()); | 329 CHECK(!secret_.empty()); |
| 325 decryption_key_.reset(crypto::SymmetricKey::Import( | 330 decryption_key_.reset(crypto::SymmetricKey::Import( |
| 326 crypto::SymmetricKey::AES, secret_)); | 331 crypto::SymmetricKey::AES, secret_)); |
| 327 if (!decryption_key_.get()) | 332 if (!decryption_key_.get()) |
| 328 return false; | 333 return false; |
| 329 return true; | 334 return true; |
| 330 } | 335 } |
| 331 | 336 |
| 332 } // namespace media | 337 } // namespace media |
| OLD | NEW |