Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(559)

Side by Side Diff: media/crypto/aes_decryptor.cc

Issue 10534096: Generalize AesDecryptor to make it more spec compliant. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated to pass layout tests. Created 8 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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
22 uint32_t AesDecryptor::next_session_id_ = 1;
23
20 // Decrypt |input| using |key|. 24 // Decrypt |input| using |key|.
21 // Return a DecoderBuffer with the decrypted data if decryption succeeded. 25 // Return a DecoderBuffer with the decrypted data if decryption succeeded.
22 // Return NULL if decryption failed. 26 // Return NULL if decryption failed.
23 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input, 27 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input,
24 crypto::SymmetricKey* key) { 28 crypto::SymmetricKey* key) {
25 CHECK(input.GetDataSize()); 29 CHECK(input.GetDataSize());
26 CHECK(key); 30 CHECK(key);
27 31
28 // Initialize encryption data. 32 // Initialize encryption data.
29 // The IV must be exactly as long as the cipher block size. 33 // The IV must be exactly as long as the cipher block size.
(...skipping 11 matching lines...) Expand all
41 DVLOG(1) << "Could not decrypt data."; 45 DVLOG(1) << "Could not decrypt data.";
42 return NULL; 46 return NULL;
43 } 47 }
44 48
45 // TODO(xhwang): Find a way to avoid this data copy. 49 // TODO(xhwang): Find a way to avoid this data copy.
46 return DecoderBuffer::CopyFrom( 50 return DecoderBuffer::CopyFrom(
47 reinterpret_cast<const uint8*>(decrypted_text.data()), 51 reinterpret_cast<const uint8*>(decrypted_text.data()),
48 decrypted_text.size()); 52 decrypted_text.size());
49 } 53 }
50 54
51 AesDecryptor::AesDecryptor() {} 55 AesDecryptor::AesDecryptor(DecryptorClient* client)
56 : client_(client) {
57 }
52 58
53 AesDecryptor::~AesDecryptor() { 59 AesDecryptor::~AesDecryptor() {
54 STLDeleteValues(&key_map_); 60 STLDeleteValues(&key_map_);
55 } 61 }
56 62
57 void AesDecryptor::AddKey(const uint8* key_id, int key_id_size, 63 void AesDecryptor::GenerateKeyRequest(const std::string& key_system,
58 const uint8* key, int key_size) { 64 const uint8* init_data,
59 CHECK(key_id && key); 65 int init_data_length) {
60 CHECK_GT(key_id_size, 0); 66 std::string session_id_string(base::UintToString(next_session_id_++));
61 CHECK_GT(key_size, 0);
62 67
63 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); 68 // For now, just fire the event with the |init_data| as the request.
64 std::string key_string(reinterpret_cast<const char*>(key) , key_size); 69 int message_length = init_data_length;
70 scoped_array<uint8> message(new uint8[message_length]);
71 memcpy(message.get(), init_data, message_length);
65 72
73 client_->KeyMessage(key_system, session_id_string,
74 message.Pass(), message_length, "");
75 }
76
77 void AesDecryptor::AddKey(const std::string& key_system,
78 const uint8* key,
79 int key_length,
80 const uint8* init_data,
81 int init_data_length,
82 const std::string& session_id) {
83 CHECK(key);
84 CHECK_GT(key_length, 0);
85
86 // TODO(xhwang): Add |session_id| check after we figure out how:
87 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16550
88
89 const int kSupportedKeyLength = 16; // 128-bit key.
90 if (key_length != kSupportedKeyLength) {
91 DVLOG(1) << "Invalid key length: " << key_length;
92 client_->KeyError(key_system, session_id, kUnknownError, 0);
93 return;
94 }
95
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.
98 static const uint8 kDummyInitData[1] = { 0 };
99 if (!init_data) {
100 init_data = kDummyInitData;
101 init_data_length = arraysize(kDummyInitData);
102 }
103
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).
106 std::string key_id_string(reinterpret_cast<const char*>(init_data),
107 init_data_length);
108 std::string key_string(reinterpret_cast<const char*>(key) , key_length);
66 crypto::SymmetricKey* symmetric_key = crypto::SymmetricKey::Import( 109 crypto::SymmetricKey* symmetric_key = crypto::SymmetricKey::Import(
67 crypto::SymmetricKey::AES, key_string); 110 crypto::SymmetricKey::AES, key_string);
68 if (!symmetric_key) { 111 if (!symmetric_key) {
69 DVLOG(1) << "Could not import key."; 112 DVLOG(1) << "Could not import key.";
113 client_->KeyError(key_system, session_id, kUnknownError, 0);
70 return; 114 return;
71 } 115 }
72 116
73 base::AutoLock auto_lock(lock_); 117 {
74 KeyMap::iterator found = key_map_.find(key_id_string); 118 base::AutoLock auto_lock(key_map_lock_);
75 if (found != key_map_.end()) { 119 KeyMap::iterator found = key_map_.find(key_id_string);
76 delete found->second; 120 if (found != key_map_.end()) {
77 key_map_.erase(found); 121 delete found->second;
122 key_map_.erase(found);
123 }
124 key_map_[key_id_string] = symmetric_key;
78 } 125 }
79 key_map_[key_id_string] = symmetric_key; 126
127 client_->KeyAdded(key_system, session_id);
128 }
129
130 void AesDecryptor::CancelKeyRequest(const std::string& /* key_system */,
scherkus (not reviewing) 2012/06/13 23:35:08 nit: don't bother w/ /* */ also is this NOTIMPLEM
ddorwin 2012/06/14 21:04:04 It just doesn't do anything ATM. I think NOTIMPLEM
xhwang 2012/06/15 01:41:04 Done with /* */. Can't be NOTIMPLEMENTED() as ddor
131 const std::string& /* session_id */) {
80 } 132 }
81 133
82 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt( 134 scoped_refptr<DecoderBuffer> AesDecryptor::Decrypt(
83 const scoped_refptr<DecoderBuffer>& encrypted) { 135 const scoped_refptr<DecoderBuffer>& encrypted) {
84 CHECK(encrypted->GetDecryptConfig()); 136 CHECK(encrypted->GetDecryptConfig());
85 const uint8* key_id = encrypted->GetDecryptConfig()->key_id(); 137 const uint8* key_id = encrypted->GetDecryptConfig()->key_id();
86 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size(); 138 const int key_id_size = encrypted->GetDecryptConfig()->key_id_size();
87 139
88 // TODO(xhwang): Avoid always constructing a string with StringPiece? 140 // TODO(xhwang): Avoid always constructing a string with StringPiece?
89 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size); 141 std::string key_id_string(reinterpret_cast<const char*>(key_id), key_id_size);
90 142
91 crypto::SymmetricKey* key = NULL; 143 crypto::SymmetricKey* key = NULL;
92 { 144 {
93 base::AutoLock auto_lock(lock_); 145 base::AutoLock auto_lock(key_map_lock_);
94 KeyMap::const_iterator found = key_map_.find(key_id_string); 146 KeyMap::const_iterator found = key_map_.find(key_id_string);
95 if (found == key_map_.end()) { 147 if (found == key_map_.end()) {
96 DVLOG(1) << "Could not find a matching key for given key ID."; 148 DVLOG(1) << "Could not find a matching key for given key ID.";
97 return NULL; 149 return NULL;
98 } 150 }
99 key = found->second; 151 key = found->second;
100 } 152 }
101 153
102 scoped_refptr<DecoderBuffer> decrypted = DecryptData(*encrypted, key); 154 scoped_refptr<DecoderBuffer> decrypted = DecryptData(*encrypted, key);
103 155
104 if (decrypted) { 156 if (decrypted) {
105 decrypted->SetTimestamp(encrypted->GetTimestamp()); 157 decrypted->SetTimestamp(encrypted->GetTimestamp());
106 decrypted->SetDuration(encrypted->GetDuration()); 158 decrypted->SetDuration(encrypted->GetDuration());
107 } 159 }
108 160
109 return decrypted; 161 return decrypted;
110 } 162 }
111 163
112 } // namespace media 164 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698