Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / cdm / aes_decryptor.cc
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/cdm/aes_decryptor.h"
6
7 #include <list>
8 #include <vector>
9
10 #include "base/logging.h"
11 #include "base/stl_util.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "crypto/encryptor.h"
14 #include "crypto/symmetric_key.h"
15 #include "media/base/audio_decoder_config.h"
16 #include "media/base/decoder_buffer.h"
17 #include "media/base/decrypt_config.h"
18 #include "media/base/video_decoder_config.h"
19 #include "media/base/video_frame.h"
20 #include "media/cdm/json_web_key.h"
21
22 namespace media {
23
24 // Keeps track of the session IDs and DecryptionKeys. The keys are ordered by
25 // insertion time (last insertion is first). It takes ownership of the
26 // DecryptionKeys.
27 class AesDecryptor::SessionIdDecryptionKeyMap {
28   // Use a std::list to actually hold the data. Insertion is always done
29   // at the front, so the "latest" decryption key is always the first one
30   // in the list.
31   typedef std::list<std::pair<uint32, DecryptionKey*> > KeyList;
32
33  public:
34   SessionIdDecryptionKeyMap() {}
35   ~SessionIdDecryptionKeyMap() { STLDeleteValues(&key_list_); }
36
37   // Replaces value if |session_id| is already present, or adds it if not.
38   // This |decryption_key| becomes the latest until another insertion or
39   // |session_id| is erased.
40   void Insert(uint32 session_id, scoped_ptr<DecryptionKey> decryption_key);
41
42   // Deletes the entry for |session_id| if present.
43   void Erase(const uint32 session_id);
44
45   // Returns whether the list is empty
46   bool Empty() const { return key_list_.empty(); }
47
48   // Returns the last inserted DecryptionKey.
49   DecryptionKey* LatestDecryptionKey() {
50     DCHECK(!key_list_.empty());
51     return key_list_.begin()->second;
52   }
53
54  private:
55   // Searches the list for an element with |session_id|.
56   KeyList::iterator Find(const uint32 session_id);
57
58   // Deletes the entry pointed to by |position|.
59   void Erase(KeyList::iterator position);
60
61   KeyList key_list_;
62
63   DISALLOW_COPY_AND_ASSIGN(SessionIdDecryptionKeyMap);
64 };
65
66 void AesDecryptor::SessionIdDecryptionKeyMap::Insert(
67     uint32 session_id,
68     scoped_ptr<DecryptionKey> decryption_key) {
69   KeyList::iterator it = Find(session_id);
70   if (it != key_list_.end())
71     Erase(it);
72   DecryptionKey* raw_ptr = decryption_key.release();
73   key_list_.push_front(std::make_pair(session_id, raw_ptr));
74 }
75
76 void AesDecryptor::SessionIdDecryptionKeyMap::Erase(const uint32 session_id) {
77   KeyList::iterator it = Find(session_id);
78   if (it == key_list_.end())
79     return;
80   Erase(it);
81 }
82
83 AesDecryptor::SessionIdDecryptionKeyMap::KeyList::iterator
84 AesDecryptor::SessionIdDecryptionKeyMap::Find(const uint32 session_id) {
85   for (KeyList::iterator it = key_list_.begin(); it != key_list_.end(); ++it) {
86     if (it->first == session_id)
87       return it;
88   }
89   return key_list_.end();
90 }
91
92 void AesDecryptor::SessionIdDecryptionKeyMap::Erase(
93     KeyList::iterator position) {
94   DCHECK(position->second);
95   delete position->second;
96   key_list_.erase(position);
97 }
98
99 uint32 AesDecryptor::next_web_session_id_ = 1;
100
101 enum ClearBytesBufferSel {
102   kSrcContainsClearBytes,
103   kDstContainsClearBytes
104 };
105
106 static void CopySubsamples(const std::vector<SubsampleEntry>& subsamples,
107                            const ClearBytesBufferSel sel,
108                            const uint8* src,
109                            uint8* dst) {
110   for (size_t i = 0; i < subsamples.size(); i++) {
111     const SubsampleEntry& subsample = subsamples[i];
112     if (sel == kSrcContainsClearBytes) {
113       src += subsample.clear_bytes;
114     } else {
115       dst += subsample.clear_bytes;
116     }
117     memcpy(dst, src, subsample.cypher_bytes);
118     src += subsample.cypher_bytes;
119     dst += subsample.cypher_bytes;
120   }
121 }
122
123 // Decrypts |input| using |key|.  Returns a DecoderBuffer with the decrypted
124 // data if decryption succeeded or NULL if decryption failed.
125 static scoped_refptr<DecoderBuffer> DecryptData(const DecoderBuffer& input,
126                                                 crypto::SymmetricKey* key) {
127   CHECK(input.data_size());
128   CHECK(input.decrypt_config());
129   CHECK(key);
130
131   crypto::Encryptor encryptor;
132   if (!encryptor.Init(key, crypto::Encryptor::CTR, "")) {
133     DVLOG(1) << "Could not initialize decryptor.";
134     return NULL;
135   }
136
137   DCHECK_EQ(input.decrypt_config()->iv().size(),
138             static_cast<size_t>(DecryptConfig::kDecryptionKeySize));
139   if (!encryptor.SetCounter(input.decrypt_config()->iv())) {
140     DVLOG(1) << "Could not set counter block.";
141     return NULL;
142   }
143
144   const char* sample = reinterpret_cast<const char*>(input.data());
145   size_t sample_size = static_cast<size_t>(input.data_size());
146
147   DCHECK_GT(sample_size, 0U) << "No sample data to be decrypted.";
148   if (sample_size == 0)
149     return NULL;
150
151   if (input.decrypt_config()->subsamples().empty()) {
152     std::string decrypted_text;
153     base::StringPiece encrypted_text(sample, sample_size);
154     if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) {
155       DVLOG(1) << "Could not decrypt data.";
156       return NULL;
157     }
158
159     // TODO(xhwang): Find a way to avoid this data copy.
160     return DecoderBuffer::CopyFrom(
161         reinterpret_cast<const uint8*>(decrypted_text.data()),
162         decrypted_text.size());
163   }
164
165   const std::vector<SubsampleEntry>& subsamples =
166       input.decrypt_config()->subsamples();
167
168   size_t total_clear_size = 0;
169   size_t total_encrypted_size = 0;
170   for (size_t i = 0; i < subsamples.size(); i++) {
171     total_clear_size += subsamples[i].clear_bytes;
172     total_encrypted_size += subsamples[i].cypher_bytes;
173     // Check for overflow. This check is valid because *_size is unsigned.
174     DCHECK(total_clear_size >= subsamples[i].clear_bytes);
175     if (total_encrypted_size < subsamples[i].cypher_bytes)
176       return NULL;
177   }
178   size_t total_size = total_clear_size + total_encrypted_size;
179   if (total_size < total_clear_size || total_size != sample_size) {
180     DVLOG(1) << "Subsample sizes do not equal input size";
181     return NULL;
182   }
183
184   // No need to decrypt if there is no encrypted data.
185   if (total_encrypted_size <= 0) {
186     return DecoderBuffer::CopyFrom(reinterpret_cast<const uint8*>(sample),
187                                    sample_size);
188   }
189
190   // The encrypted portions of all subsamples must form a contiguous block,
191   // such that an encrypted subsample that ends away from a block boundary is
192   // immediately followed by the start of the next encrypted subsample. We
193   // copy all encrypted subsamples to a contiguous buffer, decrypt them, then
194   // copy the decrypted bytes over the encrypted bytes in the output.
195   // TODO(strobe): attempt to reduce number of memory copies
196   scoped_ptr<uint8[]> encrypted_bytes(new uint8[total_encrypted_size]);
197   CopySubsamples(subsamples, kSrcContainsClearBytes,
198                  reinterpret_cast<const uint8*>(sample), encrypted_bytes.get());
199
200   base::StringPiece encrypted_text(
201       reinterpret_cast<const char*>(encrypted_bytes.get()),
202       total_encrypted_size);
203   std::string decrypted_text;
204   if (!encryptor.Decrypt(encrypted_text, &decrypted_text)) {
205     DVLOG(1) << "Could not decrypt data.";
206     return NULL;
207   }
208   DCHECK_EQ(decrypted_text.size(), encrypted_text.size());
209
210   scoped_refptr<DecoderBuffer> output = DecoderBuffer::CopyFrom(
211       reinterpret_cast<const uint8*>(sample), sample_size);
212   CopySubsamples(subsamples, kDstContainsClearBytes,
213                  reinterpret_cast<const uint8*>(decrypted_text.data()),
214                  output->writable_data());
215   return output;
216 }
217
218 AesDecryptor::AesDecryptor(const SessionCreatedCB& session_created_cb,
219                            const SessionMessageCB& session_message_cb,
220                            const SessionReadyCB& session_ready_cb,
221                            const SessionClosedCB& session_closed_cb,
222                            const SessionErrorCB& session_error_cb)
223     : session_created_cb_(session_created_cb),
224       session_message_cb_(session_message_cb),
225       session_ready_cb_(session_ready_cb),
226       session_closed_cb_(session_closed_cb),
227       session_error_cb_(session_error_cb) {}
228
229 AesDecryptor::~AesDecryptor() {
230   key_map_.clear();
231 }
232
233 bool AesDecryptor::CreateSession(uint32 session_id,
234                                  const std::string& content_type,
235                                  const uint8* init_data,
236                                  int init_data_length) {
237   // Validate that this is a new session.
238   DCHECK(valid_sessions_.find(session_id) == valid_sessions_.end());
239   valid_sessions_.insert(session_id);
240
241   std::string web_session_id_string(base::UintToString(next_web_session_id_++));
242
243   // For now, the AesDecryptor does not care about |content_type|;
244   // just fire the event with the |init_data| as the request.
245   std::vector<uint8> message;
246   if (init_data && init_data_length)
247     message.assign(init_data, init_data + init_data_length);
248
249   session_created_cb_.Run(session_id, web_session_id_string);
250   session_message_cb_.Run(session_id, message, std::string());
251   return true;
252 }
253
254 void AesDecryptor::LoadSession(uint32 session_id,
255                                const std::string& web_session_id) {
256   // TODO(xhwang): Change this to NOTREACHED() when blink checks for key systems
257   // that do not support loadSession. See http://crbug.com/342481
258   session_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0);
259 }
260
261 void AesDecryptor::UpdateSession(uint32 session_id,
262                                  const uint8* response,
263                                  int response_length) {
264   CHECK(response);
265   CHECK_GT(response_length, 0);
266   DCHECK(valid_sessions_.find(session_id) != valid_sessions_.end());
267
268   std::string key_string(reinterpret_cast<const char*>(response),
269                          response_length);
270   KeyIdAndKeyPairs keys;
271   if (!ExtractKeysFromJWKSet(key_string, &keys)) {
272     session_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0);
273     return;
274   }
275
276   // Make sure that at least one key was extracted.
277   if (keys.empty()) {
278     session_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0);
279     return;
280   }
281
282   for (KeyIdAndKeyPairs::iterator it = keys.begin(); it != keys.end(); ++it) {
283     if (it->second.length() !=
284         static_cast<size_t>(DecryptConfig::kDecryptionKeySize)) {
285       DVLOG(1) << "Invalid key length: " << key_string.length();
286       session_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0);
287       return;
288     }
289     if (!AddDecryptionKey(session_id, it->first, it->second)) {
290       session_error_cb_.Run(session_id, MediaKeys::kUnknownError, 0);
291       return;
292     }
293   }
294
295   {
296     base::AutoLock auto_lock(new_key_cb_lock_);
297
298     if (!new_audio_key_cb_.is_null())
299       new_audio_key_cb_.Run();
300
301     if (!new_video_key_cb_.is_null())
302       new_video_key_cb_.Run();
303   }
304
305   session_ready_cb_.Run(session_id);
306 }
307
308 void AesDecryptor::ReleaseSession(uint32 session_id) {
309   // Validate that this is a reference to an active session and then forget it.
310   std::set<uint32>::iterator it = valid_sessions_.find(session_id);
311   DCHECK(it != valid_sessions_.end());
312   valid_sessions_.erase(it);
313
314   DeleteKeysForSession(session_id);
315   session_closed_cb_.Run(session_id);
316 }
317
318 Decryptor* AesDecryptor::GetDecryptor() {
319   return this;
320 }
321
322 void AesDecryptor::RegisterNewKeyCB(StreamType stream_type,
323                                     const NewKeyCB& new_key_cb) {
324   base::AutoLock auto_lock(new_key_cb_lock_);
325
326   switch (stream_type) {
327     case kAudio:
328       new_audio_key_cb_ = new_key_cb;
329       break;
330     case kVideo:
331       new_video_key_cb_ = new_key_cb;
332       break;
333     default:
334       NOTREACHED();
335   }
336 }
337
338 void AesDecryptor::Decrypt(StreamType stream_type,
339                            const scoped_refptr<DecoderBuffer>& encrypted,
340                            const DecryptCB& decrypt_cb) {
341   CHECK(encrypted->decrypt_config());
342
343   scoped_refptr<DecoderBuffer> decrypted;
344   // An empty iv string signals that the frame is unencrypted.
345   if (encrypted->decrypt_config()->iv().empty()) {
346     decrypted = DecoderBuffer::CopyFrom(encrypted->data(),
347                                         encrypted->data_size());
348   } else {
349     const std::string& key_id = encrypted->decrypt_config()->key_id();
350     DecryptionKey* key = GetKey(key_id);
351     if (!key) {
352       DVLOG(1) << "Could not find a matching key for the given key ID.";
353       decrypt_cb.Run(kNoKey, NULL);
354       return;
355     }
356
357     crypto::SymmetricKey* decryption_key = key->decryption_key();
358     decrypted = DecryptData(*encrypted.get(), decryption_key);
359     if (!decrypted.get()) {
360       DVLOG(1) << "Decryption failed.";
361       decrypt_cb.Run(kError, NULL);
362       return;
363     }
364   }
365
366   decrypted->set_timestamp(encrypted->timestamp());
367   decrypted->set_duration(encrypted->duration());
368   decrypt_cb.Run(kSuccess, decrypted);
369 }
370
371 void AesDecryptor::CancelDecrypt(StreamType stream_type) {
372   // Decrypt() calls the DecryptCB synchronously so there's nothing to cancel.
373 }
374
375 void AesDecryptor::InitializeAudioDecoder(const AudioDecoderConfig& config,
376                                           const DecoderInitCB& init_cb) {
377   // AesDecryptor does not support audio decoding.
378   init_cb.Run(false);
379 }
380
381 void AesDecryptor::InitializeVideoDecoder(const VideoDecoderConfig& config,
382                                           const DecoderInitCB& init_cb) {
383   // AesDecryptor does not support video decoding.
384   init_cb.Run(false);
385 }
386
387 void AesDecryptor::DecryptAndDecodeAudio(
388     const scoped_refptr<DecoderBuffer>& encrypted,
389     const AudioDecodeCB& audio_decode_cb) {
390   NOTREACHED() << "AesDecryptor does not support audio decoding";
391 }
392
393 void AesDecryptor::DecryptAndDecodeVideo(
394     const scoped_refptr<DecoderBuffer>& encrypted,
395     const VideoDecodeCB& video_decode_cb) {
396   NOTREACHED() << "AesDecryptor does not support video decoding";
397 }
398
399 void AesDecryptor::ResetDecoder(StreamType stream_type) {
400   NOTREACHED() << "AesDecryptor does not support audio/video decoding";
401 }
402
403 void AesDecryptor::DeinitializeDecoder(StreamType stream_type) {
404   NOTREACHED() << "AesDecryptor does not support audio/video decoding";
405 }
406
407 bool AesDecryptor::AddDecryptionKey(const uint32 session_id,
408                                     const std::string& key_id,
409                                     const std::string& key_string) {
410   scoped_ptr<DecryptionKey> decryption_key(new DecryptionKey(key_string));
411   if (!decryption_key) {
412     DVLOG(1) << "Could not create key.";
413     return false;
414   }
415
416   if (!decryption_key->Init()) {
417     DVLOG(1) << "Could not initialize decryption key.";
418     return false;
419   }
420
421   base::AutoLock auto_lock(key_map_lock_);
422   KeyIdToSessionKeysMap::iterator key_id_entry = key_map_.find(key_id);
423   if (key_id_entry != key_map_.end()) {
424     key_id_entry->second->Insert(session_id, decryption_key.Pass());
425     return true;
426   }
427
428   // |key_id| not found, so need to create new entry.
429   scoped_ptr<SessionIdDecryptionKeyMap> inner_map(
430       new SessionIdDecryptionKeyMap());
431   inner_map->Insert(session_id, decryption_key.Pass());
432   key_map_.add(key_id, inner_map.Pass());
433   return true;
434 }
435
436 AesDecryptor::DecryptionKey* AesDecryptor::GetKey(
437     const std::string& key_id) const {
438   base::AutoLock auto_lock(key_map_lock_);
439   KeyIdToSessionKeysMap::const_iterator key_id_found = key_map_.find(key_id);
440   if (key_id_found == key_map_.end())
441     return NULL;
442
443   // Return the key from the "latest" session_id entry.
444   return key_id_found->second->LatestDecryptionKey();
445 }
446
447 void AesDecryptor::DeleteKeysForSession(const uint32 session_id) {
448   base::AutoLock auto_lock(key_map_lock_);
449
450   // Remove all keys associated with |session_id|. Since the data is optimized
451   // for access in GetKey(), we need to look at each entry in |key_map_|.
452   KeyIdToSessionKeysMap::iterator it = key_map_.begin();
453   while (it != key_map_.end()) {
454     it->second->Erase(session_id);
455     if (it->second->Empty()) {
456       // Need to get rid of the entry for this key_id. This will mess up the
457       // iterator, so we need to increment it first.
458       KeyIdToSessionKeysMap::iterator current = it;
459       ++it;
460       key_map_.erase(current);
461     } else {
462       ++it;
463     }
464   }
465 }
466
467 AesDecryptor::DecryptionKey::DecryptionKey(const std::string& secret)
468     : secret_(secret) {
469 }
470
471 AesDecryptor::DecryptionKey::~DecryptionKey() {}
472
473 bool AesDecryptor::DecryptionKey::Init() {
474   CHECK(!secret_.empty());
475   decryption_key_.reset(crypto::SymmetricKey::Import(
476       crypto::SymmetricKey::AES, secret_));
477   if (!decryption_key_)
478     return false;
479   return true;
480 }
481
482 }  // namespace media