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 <set> 9 #include <string> 10 11 #include "base/basictypes.h" 12 #include "base/containers/scoped_ptr_hash_map.h" 13 #include "base/memory/ref_counted.h" 14 #include "base/memory/scoped_ptr.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 SessionCreatedCB& session_created_cb, 31 const SessionMessageCB& session_message_cb, 32 const SessionReadyCB& session_ready_cb, 33 const SessionClosedCB& session_closed_cb, 34 const SessionErrorCB& session_error_cb); 35 virtual ~AesDecryptor(); 36 37 // MediaKeys implementation. 38 virtual bool CreateSession(uint32 session_id, 39 const std::string& type, 40 const uint8* init_data, 41 int init_data_length) OVERRIDE; 42 virtual void UpdateSession(uint32 session_id, 43 const uint8* response, 44 int response_length) OVERRIDE; 45 virtual void ReleaseSession(uint32 session_id) OVERRIDE; 46 virtual Decryptor* GetDecryptor() OVERRIDE; 47 48 // Decryptor implementation. 49 virtual void RegisterNewKeyCB(StreamType stream_type, 50 const NewKeyCB& key_added_cb) OVERRIDE; 51 virtual void Decrypt(StreamType stream_type, 52 const scoped_refptr<DecoderBuffer>& encrypted, 53 const DecryptCB& decrypt_cb) OVERRIDE; 54 virtual void CancelDecrypt(StreamType stream_type) OVERRIDE; 55 virtual void InitializeAudioDecoder(const AudioDecoderConfig& config, 56 const DecoderInitCB& init_cb) OVERRIDE; 57 virtual void InitializeVideoDecoder(const VideoDecoderConfig& config, 58 const DecoderInitCB& init_cb) OVERRIDE; 59 virtual void DecryptAndDecodeAudio( 60 const scoped_refptr<DecoderBuffer>& encrypted, 61 const AudioDecodeCB& audio_decode_cb) OVERRIDE; 62 virtual void DecryptAndDecodeVideo( 63 const scoped_refptr<DecoderBuffer>& encrypted, 64 const VideoDecodeCB& video_decode_cb) OVERRIDE; 65 virtual void ResetDecoder(StreamType stream_type) OVERRIDE; 66 virtual void DeinitializeDecoder(StreamType stream_type) OVERRIDE; 67 68 private: 69 // TODO(fgalligan): Remove this and change KeyMap to use crypto::SymmetricKey 70 // as there are no decryptors that are performing an integrity check. 71 // Helper class that manages the decryption key. 72 class DecryptionKey { 73 public: 74 explicit DecryptionKey(const std::string& secret); 75 ~DecryptionKey(); 76 77 // Creates the encryption key. 78 bool Init(); 79 80 crypto::SymmetricKey* decryption_key() { return decryption_key_.get(); } 81 82 private: 83 // The base secret that is used to create the decryption key. 84 const std::string secret_; 85 86 // The key used to decrypt the data. 87 scoped_ptr<crypto::SymmetricKey> decryption_key_; 88 89 DISALLOW_COPY_AND_ASSIGN(DecryptionKey); 90 }; 91 92 // Keep track of the keys for a key ID. If multiple sessions specify keys 93 // for the same key ID, then the last key inserted is used. The structure is 94 // optimized so that Decrypt() has fast access, at the cost of slow deletion 95 // of keys when a session is released. 96 class SessionIdDecryptionKeyMap; 97 98 // Key ID <-> SessionIdDecryptionKeyMap map. 99 typedef base::ScopedPtrHashMap<std::string, SessionIdDecryptionKeyMap> 100 KeyIdToSessionKeysMap; 101 102 // Creates a DecryptionKey using |key_string| and associates it with |key_id|. 103 // Returns true if successful. 104 bool AddDecryptionKey(const uint32 session_id, 105 const std::string& key_id, 106 const std::string& key_string); 107 108 // Gets a DecryptionKey associated with |key_id|. The AesDecryptor still owns 109 // the key. Returns NULL if no key is associated with |key_id|. 110 DecryptionKey* GetKey(const std::string& key_id) const; 111 112 // Deletes all keys associated with |session_id|. 113 void DeleteKeysForSession(const uint32 session_id); 114 115 // Callbacks for firing session events. 116 SessionCreatedCB session_created_cb_; 117 SessionMessageCB session_message_cb_; 118 SessionReadyCB session_ready_cb_; 119 SessionClosedCB session_closed_cb_; 120 SessionErrorCB session_error_cb_; 121 122 // Since only Decrypt() is called off the renderer thread, we only need to 123 // protect |key_map_|, the only member variable that is shared between 124 // Decrypt() and other methods. 125 KeyIdToSessionKeysMap key_map_; // Protected by |key_map_lock_|. 126 mutable base::Lock key_map_lock_; // Protects the |key_map_|. 127 128 // Keeps track of current valid session IDs. 129 std::set<uint32> valid_sessions_; 130 131 // Make web session ID unique per renderer by making it static. Web session 132 // IDs seen by the app will be "1", "2", etc. 133 static uint32 next_web_session_id_; 134 135 NewKeyCB new_audio_key_cb_; 136 NewKeyCB new_video_key_cb_; 137 138 DISALLOW_COPY_AND_ASSIGN(AesDecryptor); 139 }; 140 141 } // namespace media 142 143 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_ 144