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.
5 #ifndef MEDIA_CRYPTO_AES_DECRYPTOR_H_
6 #define MEDIA_CRYPTO_AES_DECRYPTOR_H_
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"
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
{
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();
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
;
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
;
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.
74 explicit DecryptionKey(const std::string
& secret
);
77 // Creates the encryption key.
80 crypto::SymmetricKey
* decryption_key() { return decryption_key_
.get(); }
83 // The base secret that is used to create the decryption key.
84 const std::string secret_
;
86 // The key used to decrypt the data.
87 scoped_ptr
<crypto::SymmetricKey
> decryption_key_
;
89 DISALLOW_COPY_AND_ASSIGN(DecryptionKey
);
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
;
98 // Key ID <-> SessionIdDecryptionKeyMap map.
99 typedef base::ScopedPtrHashMap
<std::string
, SessionIdDecryptionKeyMap
>
100 KeyIdToSessionKeysMap
;
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
);
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;
112 // Deletes all keys associated with |session_id|.
113 void DeleteKeysForSession(const uint32 session_id
);
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_
;
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_|.
128 // Keeps track of current valid session IDs.
129 std::set
<uint32
> valid_sessions_
;
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_
;
135 NewKeyCB new_audio_key_cb_
;
136 NewKeyCB new_video_key_cb_
;
138 // Protect |new_audio_key_cb_| and |new_video_key_cb_| as they are set on the
139 // main thread but called on the media thread.
140 mutable base::Lock new_key_cb_lock_
;
142 DISALLOW_COPY_AND_ASSIGN(AesDecryptor
);
147 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_