| 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" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 return NULL; | 46 return NULL; |
| 47 } | 47 } |
| 48 | 48 |
| 49 // TODO(xhwang): Find a way to avoid this data copy. | 49 // TODO(xhwang): Find a way to avoid this data copy. |
| 50 return DecoderBuffer::CopyFrom( | 50 return DecoderBuffer::CopyFrom( |
| 51 reinterpret_cast<const uint8*>(decrypted_text.data()), | 51 reinterpret_cast<const uint8*>(decrypted_text.data()), |
| 52 decrypted_text.size()); | 52 decrypted_text.size()); |
| 53 } | 53 } |
| 54 | 54 |
| 55 AesDecryptor::AesDecryptor(DecryptorClient* client) | 55 AesDecryptor::AesDecryptor(DecryptorClient* client) |
| 56 : client_(client) { | 56 : Decryptor(client) { |
| 57 } | 57 } |
| 58 | 58 |
| 59 AesDecryptor::~AesDecryptor() { | 59 AesDecryptor::~AesDecryptor() { |
| 60 STLDeleteValues(&key_map_); | 60 STLDeleteValues(&key_map_); |
| 61 } | 61 } |
| 62 | 62 |
| 63 void AesDecryptor::GenerateKeyRequest(const std::string& key_system, | 63 void AesDecryptor::GenerateKeyRequest(const std::string& key_system, |
| 64 const uint8* init_data, | 64 const uint8* init_data, |
| 65 int init_data_length) { | 65 int init_data_length) { |
| 66 std::string session_id_string(base::UintToString(next_session_id_++)); | 66 std::string session_id_string(base::UintToString(next_session_id_++)); |
| 67 | 67 |
| 68 // For now, just fire the event with the |init_data| as the request. | 68 // For now, just fire the event with the |init_data| as the request. |
| 69 int message_length = init_data_length; | 69 int message_length = init_data_length; |
| 70 scoped_array<uint8> message(new uint8[message_length]); | 70 scoped_array<uint8> message(new uint8[message_length]); |
| 71 memcpy(message.get(), init_data, message_length); | 71 memcpy(message.get(), init_data, message_length); |
| 72 | 72 |
| 73 client_->KeyMessage(key_system, session_id_string, | 73 client()->KeyMessage(key_system, session_id_string, |
| 74 message.Pass(), message_length, ""); | 74 message.Pass(), message_length, ""); |
| 75 } | 75 } |
| 76 | 76 |
| 77 void AesDecryptor::AddKey(const std::string& key_system, | 77 void AesDecryptor::AddKey(const std::string& key_system, |
| 78 const uint8* key, | 78 const uint8* key, |
| 79 int key_length, | 79 int key_length, |
| 80 const uint8* init_data, | 80 const uint8* init_data, |
| 81 int init_data_length, | 81 int init_data_length, |
| 82 const std::string& session_id) { | 82 const std::string& session_id) { |
| 83 CHECK(key); | 83 CHECK(key); |
| 84 CHECK_GT(key_length, 0); | 84 CHECK_GT(key_length, 0); |
| 85 | 85 |
| 86 // TODO(xhwang): Add |session_id| check after we figure out how: | 86 // TODO(xhwang): Add |session_id| check after we figure out how: |
| 87 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16550 | 87 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16550 |
| 88 | 88 |
| 89 const int kSupportedKeyLength = 16; // 128-bit key. | 89 const int kSupportedKeyLength = 16; // 128-bit key. |
| 90 if (key_length != kSupportedKeyLength) { | 90 if (key_length != kSupportedKeyLength) { |
| 91 DVLOG(1) << "Invalid key length: " << key_length; | 91 DVLOG(1) << "Invalid key length: " << key_length; |
| 92 client_->KeyError(key_system, session_id, kUnknownError, 0); | 92 client()->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); |
| 93 return; | 93 return; |
| 94 } | 94 } |
| 95 | 95 |
| 96 // TODO(xhwang): Fix the decryptor to accept no |init_data|. See | 96 // TODO(xhwang): Fix the decryptor to accept no |init_data|. See |
| 97 // http://crbug.com/123265. Until then, ensure a non-empty value is passed. | 97 // http://crbug.com/123265. Until then, ensure a non-empty value is passed. |
| 98 static const uint8 kDummyInitData[1] = { 0 }; | 98 static const uint8 kDummyInitData[1] = { 0 }; |
| 99 if (!init_data) { | 99 if (!init_data) { |
| 100 init_data = kDummyInitData; | 100 init_data = kDummyInitData; |
| 101 init_data_length = arraysize(kDummyInitData); | 101 init_data_length = arraysize(kDummyInitData); |
| 102 } | 102 } |
| 103 | 103 |
| 104 // TODO(xhwang): For now, use |init_data| for key ID. Make this more spec | 104 // 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). | 105 // compliant later (http://crbug.com/123262, http://crbug.com/123265). |
| 106 std::string key_id_string(reinterpret_cast<const char*>(init_data), | 106 std::string key_id_string(reinterpret_cast<const char*>(init_data), |
| 107 init_data_length); | 107 init_data_length); |
| 108 std::string key_string(reinterpret_cast<const char*>(key) , key_length); | 108 std::string key_string(reinterpret_cast<const char*>(key) , key_length); |
| 109 crypto::SymmetricKey* symmetric_key = crypto::SymmetricKey::Import( | 109 crypto::SymmetricKey* symmetric_key = crypto::SymmetricKey::Import( |
| 110 crypto::SymmetricKey::AES, key_string); | 110 crypto::SymmetricKey::AES, key_string); |
| 111 if (!symmetric_key) { | 111 if (!symmetric_key) { |
| 112 DVLOG(1) << "Could not import key."; | 112 DVLOG(1) << "Could not import key."; |
| 113 client_->KeyError(key_system, session_id, kUnknownError, 0); | 113 client()->KeyError(key_system, session_id, Decryptor::kUnknownError, 0); |
| 114 return; | 114 return; |
| 115 } | 115 } |
| 116 | 116 |
| 117 { | 117 { |
| 118 base::AutoLock auto_lock(key_map_lock_); | 118 base::AutoLock auto_lock(key_map_lock_); |
| 119 KeyMap::iterator found = key_map_.find(key_id_string); | 119 KeyMap::iterator found = key_map_.find(key_id_string); |
| 120 if (found != key_map_.end()) { | 120 if (found != key_map_.end()) { |
| 121 delete found->second; | 121 delete found->second; |
| 122 key_map_.erase(found); | 122 key_map_.erase(found); |
| 123 } | 123 } |
| 124 key_map_[key_id_string] = symmetric_key; | 124 key_map_[key_id_string] = symmetric_key; |
| 125 } | 125 } |
| 126 | 126 |
| 127 client_->KeyAdded(key_system, session_id); | 127 client()->KeyAdded(key_system, session_id); |
| 128 } | 128 } |
| 129 | 129 |
| 130 void AesDecryptor::CancelKeyRequest(const std::string& key_system, | 130 void AesDecryptor::CancelKeyRequest(const std::string& key_system, |
| 131 const std::string& session_id) { | 131 const std::string& session_id) { |
| 132 } | 132 } |
| 133 | 133 |
| 134 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt( | 134 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt( |
| 135 const scoped_refptr<DecoderBuffer>& encrypted) { | 135 const scoped_refptr<DecoderBuffer>& encrypted) { |
| 136 CHECK(encrypted->GetDecryptConfig()); | 136 CHECK(encrypted->GetDecryptConfig()); |
| 137 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); | 137 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 155 | 155 |
| 156 if (decrypted) { | 156 if (decrypted) { |
| 157 decrypted->SetTimestamp(encrypted->GetTimestamp()); | 157 decrypted->SetTimestamp(encrypted->GetTimestamp()); |
| 158 decrypted->SetDuration(encrypted->GetDuration()); | 158 decrypted->SetDuration(encrypted->GetDuration()); |
| 159 } | 159 } |
| 160 | 160 |
| 161 return decrypted; | 161 return decrypted; |
| 162 } | 162 } |
| 163 | 163 |
| 164 } // namespace media | 164 } // namespace media |
| OLD | NEW |