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 #ifndef MEDIA_CRYPTO_AES_DECRYPTOR_H_ 6 #define MEDIA_CRYPTO_AES_DECRYPTOR_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/containers/hash_tables.h" 12 #include "base/memory/ref_counted.h" 13 #include "base/memory/scoped_ptr.h" 14 #include "base/strings/string_piece.h" 15 #include "base/synchronization/lock.h" 16 #include "media/base/decryptor.h" 17 #include "media/base/media_export.h" 18 #include "media/base/media_keys.h" 19 20 namespace crypto { 21 class SymmetricKey; 22 } 23 24 namespace media { 25 26 // Decrypts an AES encrypted buffer into an unencrypted buffer. The AES 27 // encryption must be CTR with a key size of 128bits. 28 class MEDIA_EXPORT AesDecryptor : public MediaKeys, public Decryptor { 29 public: 30 AesDecryptor(const KeyAddedCB& key_added_cb, 31 const KeyErrorCB& key_error_cb, 32 const KeyMessageCB& key_message_cb); 33 virtual ~AesDecryptor(); 34 35 // MediaKeys implementation. 36 virtual bool GenerateKeyRequest(const std::string& type, 37 const uint8* init_data, 38 int init_data_length) OVERRIDE; 39 virtual void AddKey(const uint8* key, int key_length, 40 const uint8* init_data, int init_data_length, 41 const std::string& session_id) OVERRIDE; 42 virtual void CancelKeyRequest(const std::string& session_id) OVERRIDE; 43 virtual Decryptor* GetDecryptor() OVERRIDE; 44 45 // Decryptor implementation. 46 virtual void RegisterNewKeyCB(StreamType stream_type, 47 const NewKeyCB& key_added_cb) OVERRIDE; 48 virtual void Decrypt(StreamType stream_type, 49 const scoped_refptr<DecoderBuffer>& encrypted, 50 const DecryptCB& decrypt_cb) OVERRIDE; 51 virtual void CancelDecrypt(StreamType stream_type) OVERRIDE; 52 virtual void InitializeAudioDecoder(const AudioDecoderConfig& config, 53 const DecoderInitCB& init_cb) OVERRIDE; 54 virtual void InitializeVideoDecoder(const VideoDecoderConfig& config, 55 const DecoderInitCB& init_cb) OVERRIDE; 56 virtual void DecryptAndDecodeAudio( 57 const scoped_refptr<DecoderBuffer>& encrypted, 58 const AudioDecodeCB& audio_decode_cb) OVERRIDE; 59 virtual void DecryptAndDecodeVideo( 60 const scoped_refptr<DecoderBuffer>& encrypted, 61 const VideoDecodeCB& video_decode_cb) OVERRIDE; 62 virtual void ResetDecoder(StreamType stream_type) OVERRIDE; 63 virtual void DeinitializeDecoder(StreamType stream_type) OVERRIDE; 64 65 private: 66 // TODO(fgalligan): Remove this and change KeyMap to use crypto::SymmetricKey 67 // as there are no decryptors that are performing an integrity check. 68 // Helper class that manages the decryption key. 69 class DecryptionKey { 70 public: 71 explicit DecryptionKey(const std::string& secret); 72 ~DecryptionKey(); 73 74 // Creates the encryption key. 75 bool Init(); 76 77 crypto::SymmetricKey* decryption_key() { return decryption_key_.get(); } 78 79 private: 80 // The base secret that is used to create the decryption key. 81 const std::string secret_; 82 83 // The key used to decrypt the data. 84 scoped_ptr<crypto::SymmetricKey> decryption_key_; 85 86 DISALLOW_COPY_AND_ASSIGN(DecryptionKey); 87 }; 88 89 // Sets |key| for |key_id|. The AesDecryptor takes the ownership of the |key|. 90 void SetKey(const std::string& key_id, scoped_ptr<DecryptionKey> key); 91 92 // Gets a DecryptionKey associated with |key_id|. The AesDecryptor still owns 93 // the key. Returns NULL if no key is associated with |key_id|. 94 DecryptionKey* GetKey(const std::string& key_id) const; 95 96 // Callbacks for firing key events. 97 KeyAddedCB key_added_cb_; 98 KeyErrorCB key_error_cb_; 99 KeyMessageCB key_message_cb_; 100 101 // KeyMap owns the DecryptionKey* and must delete them when they are 102 // not needed any more. 103 typedef base::hash_map<std::string, DecryptionKey*> KeyMap; 104 105 // Since only Decrypt() is called off the renderer thread, we only need to 106 // protect |key_map_|, the only member variable that is shared between 107 // Decrypt() and other methods. 108 KeyMap key_map_; // Protected by the |key_map_lock_|. 109 mutable base::Lock key_map_lock_; // Protects the |key_map_|. 110 111 // Make session ID unique per renderer by making it static. 112 // TODO(xhwang): Make session ID more strictly defined if needed: 113 // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16739#c0 114 static uint32 next_session_id_; 115 116 NewKeyCB new_audio_key_cb_; 117 NewKeyCB new_video_key_cb_; 118 119 DISALLOW_COPY_AND_ASSIGN(AesDecryptor); 120 }; 121 122 } // namespace media 123 124 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_ 125