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_piece.h" | 10 #include "base/string_piece.h" |
| 10 #include "crypto/encryptor.h" | 11 #include "crypto/encryptor.h" |
| 11 #include "crypto/symmetric_key.h" | 12 #include "crypto/symmetric_key.h" |
| 12 #include "media/base/decoder_buffer.h" | 13 #include "media/base/decoder_buffer.h" |
| 13 #include "media/base/decrypt_config.h" | 14 #include "media/base/decrypt_config.h" |
| 15 #include "media/crypto/decryptor_client.h" | |
| 14 | 16 |
| 15 namespace media { | 17 namespace media { |
| 16 | 18 |
| 17 // TODO(xhwang): Get real IV from frames. | 19 // TODO(xhwang): Get real IV from frames. |
| 18 static const char kInitialCounter[] = "0000000000000000"; | 20 static const char kInitialCounter[] = "0000000000000000"; |
| 19 | 21 |
| 20 // Decrypt |input| using |key|. | 22 // Decrypt |input| using |key|. |
| 21 // Return a DecoderBuffer with the decrypted data if decryption succeeded. | 23 // Return a DecoderBuffer with the decrypted data if decryption succeeded. |
| 22 // Return NULL if decryption failed. | 24 // Return NULL if decryption failed. |
| 23 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, | 25 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 41 DVLOG(1) << "Could not decrypt data."; | 43 DVLOG(1) << "Could not decrypt data."; |
| 42 return NULL; | 44 return NULL; |
| 43 } | 45 } |
| 44 | 46 |
| 45 // TODO(xhwang): Find a way to avoid this data copy. | 47 // TODO(xhwang): Find a way to avoid this data copy. |
| 46 return DecoderBuffer::CopyFrom( | 48 return DecoderBuffer::CopyFrom( |
| 47 reinterpret_cast<const uint8*>(decrypted_text.data()), | 49 reinterpret_cast<const uint8*>(decrypted_text.data()), |
| 48 decrypted_text.size()); | 50 decrypted_text.size()); |
| 49 } | 51 } |
| 50 | 52 |
| 51 AesDecryptor::AesDecryptor() {} | 53 AesDecryptor::AesDecryptor() |
| 54 : client_(NULL), | |
| 55 session_id_(0) { | |
| 56 } | |
| 52 | 57 |
| 53 AesDecryptor::~AesDecryptor() { | 58 AesDecryptor::~AesDecryptor() { |
| 54 STLDeleteValues(&key_map_); | 59 STLDeleteValues(&key_map_); |
| 55 } | 60 } |
| 56 | 61 |
| 57 void AesDecryptor::AddKey(const uint8* key_id, int key_id_size, | 62 void AesDecryptor::Init(DecryptorClient* client) { |
| 58 const uint8* key, int key_size) { | 63 client_ = client; |
| 59 CHECK(key_id && key); | 64 } |
| 60 CHECK_GT(key_id_size, 0); | |
| 61 CHECK_GT(key_size, 0); | |
| 62 | 65 |
| 63 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); | 66 void AesDecryptor::GenerateKeyRequest(const std::string& key_system, |
| 64 std::string key_string(reinterpret_cast<const char*>(key) , key_size); | 67 const uint8* init_data, |
| 68 int init_data_length) { | |
| 69 session_id_++; | |
| 70 std::string session_id_string(base::UintToString(session_id_)); | |
| 65 | 71 |
| 72 // For now, just fire the event with the init_data as the request. | |
|
scherkus (not reviewing)
2012/06/12 03:15:58
|init_data|
xhwang
2012/06/12 19:01:15
Done.
| |
| 73 int message_length = init_data_length; | |
| 74 scoped_array<uint8> message(new uint8[message_length]); | |
| 75 memcpy(message.get(), init_data, message_length); | |
| 76 | |
| 77 client_->KeyMessage(key_system, session_id_string, | |
| 78 message.Pass(), message_length, ""); | |
| 79 } | |
| 80 | |
| 81 void AesDecryptor::AddKey(const std::string& key_system, | |
| 82 const uint8* key, | |
| 83 int key_length, | |
| 84 const uint8* init_data, | |
| 85 int init_data_length, | |
| 86 const std::string& session_id) { | |
| 87 CHECK(init_data && key); | |
|
ddorwin
2012/06/11 21:02:40
I think this will fail layout tests because of htt
xhwang
2012/06/12 19:01:15
Done.
| |
| 88 CHECK_GT(init_data_length, 0); | |
| 89 CHECK_GT(key_length, 0); | |
| 90 | |
| 91 // TODO(xhwang): Added |session_id| check after we figure out how: | |
|
ddorwin
2012/06/11 21:02:40
s/Added/Add
xhwang
2012/06/12 19:01:15
Done.
| |
| 92 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16550 | |
| 93 | |
| 94 const int kSupportedKeyLength = 16; // 128-bit key. | |
| 95 if (key_length != kSupportedKeyLength) { | |
| 96 DVLOG(1) << "Invalid key length: " << key_length; | |
| 97 client_->KeyError(key_system, session_id, UnknownError, 0); | |
| 98 return; | |
| 99 } | |
| 100 | |
| 101 // TODO(xhwang): For now, use |init_data| for key ID. Make this more spec | |
| 102 // compliant later (http://crbug.com/123262, http://crbug.com/123265). | |
| 103 std::string key_id_string(reinterpret_cast<const char*>(init_data), | |
| 104 init_data_length); | |
| 105 std::string key_string(reinterpret_cast<const char*>(key) , key_length); | |
| 66 crypto::SymmetricKey* symmetric_key = crypto::SymmetricKey::Import( | 106 crypto::SymmetricKey* symmetric_key = crypto::SymmetricKey::Import( |
| 67 crypto::SymmetricKey::AES, key_string); | 107 crypto::SymmetricKey::AES, key_string); |
| 68 if (!symmetric_key) { | 108 if (!symmetric_key) { |
| 69 DVLOG(1) << "Could not import key."; | 109 DVLOG(1) << "Could not import key."; |
| 110 client_->KeyError(key_system, session_id, UnknownError, 0); | |
| 70 return; | 111 return; |
| 71 } | 112 } |
| 72 | 113 |
| 73 base::AutoLock auto_lock(lock_); | 114 { |
| 74 KeyMap::iterator found = key_map_.find(key_id_string); | 115 base::AutoLock auto_lock(lock_); |
| 75 if (found != key_map_.end()) { | 116 KeyMap::iterator found = key_map_.find(key_id_string); |
| 76 delete found->second; | 117 if (found != key_map_.end()) { |
| 77 key_map_.erase(found); | 118 delete found->second; |
| 119 key_map_.erase(found); | |
| 120 } | |
| 121 key_map_[key_id_string] = symmetric_key; | |
| 78 } | 122 } |
| 79 key_map_[key_id_string] = symmetric_key; | 123 |
| 124 client_->KeyAdded(key_system, session_id); | |
| 125 } | |
| 126 | |
| 127 void AesDecryptor::CancelKeyRequest(const std::string& /* key_system */, | |
| 128 const std::string& /* session_id */) { | |
| 80 } | 129 } |
| 81 | 130 |
| 82 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt( | 131 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt( |
| 83 const scoped_refptr<DecoderBuffer>& encrypted) { | 132 const scoped_refptr<DecoderBuffer>& encrypted) { |
| 84 CHECK(encrypted->GetDecryptConfig()); | 133 CHECK(encrypted->GetDecryptConfig()); |
| 85 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); | 134 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); |
| 86 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); | 135 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); |
| 87 | 136 |
| 88 // TODO(xhwang): Avoid always constructing a string with StringPiece? | 137 // TODO(xhwang): Avoid always constructing a string with StringPiece? |
| 89 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); | 138 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 103 | 152 |
| 104 if (decrypted) { | 153 if (decrypted) { |
| 105 decrypted->SetTimestamp(encrypted->GetTimestamp()); | 154 decrypted->SetTimestamp(encrypted->GetTimestamp()); |
| 106 decrypted->SetDuration(encrypted->GetDuration()); | 155 decrypted->SetDuration(encrypted->GetDuration()); |
| 107 } | 156 } |
| 108 | 157 |
| 109 return decrypted; | 158 return decrypted; |
| 110 } | 159 } |
| 111 | 160 |
| 112 } // namespace media | 161 } // namespace media |
| OLD | NEW |