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/cdm_context.h"
17 #include "media/base/decryptor.h"
18 #include "media/base/media_export.h"
19 #include "media/base/media_keys.h"
27 // Decrypts an AES encrypted buffer into an unencrypted buffer. The AES
28 // encryption must be CTR with a key size of 128bits.
29 class MEDIA_EXPORT AesDecryptor
: public MediaKeys
,
33 AesDecryptor(const SessionMessageCB
& session_message_cb
,
34 const SessionClosedCB
& session_closed_cb
,
35 const SessionKeysChangeCB
& session_keys_change_cb
);
36 ~AesDecryptor() override
;
38 // MediaKeys implementation.
39 void SetServerCertificate(const uint8
* certificate_data
,
40 int certificate_data_length
,
41 scoped_ptr
<SimpleCdmPromise
> promise
) override
;
42 void CreateSessionAndGenerateRequest(
43 SessionType session_type
,
44 EmeInitDataType init_data_type
,
45 const uint8
* init_data
,
47 scoped_ptr
<NewSessionCdmPromise
> promise
) override
;
48 void LoadSession(SessionType session_type
,
49 const std::string
& session_id
,
50 scoped_ptr
<NewSessionCdmPromise
> promise
) override
;
51 void UpdateSession(const std::string
& session_id
,
52 const uint8
* response
,
54 scoped_ptr
<SimpleCdmPromise
> promise
) override
;
55 void CloseSession(const std::string
& session_id
,
56 scoped_ptr
<SimpleCdmPromise
> promise
) override
;
57 void RemoveSession(const std::string
& session_id
,
58 scoped_ptr
<SimpleCdmPromise
> promise
) override
;
59 CdmContext
* GetCdmContext() override
;
61 // CdmContext implementation.
62 Decryptor
* GetDecryptor() override
;
63 int GetCdmId() const override
;
65 // Decryptor implementation.
66 void RegisterNewKeyCB(StreamType stream_type
,
67 const NewKeyCB
& key_added_cb
) override
;
68 void Decrypt(StreamType stream_type
,
69 const scoped_refptr
<DecoderBuffer
>& encrypted
,
70 const DecryptCB
& decrypt_cb
) override
;
71 void CancelDecrypt(StreamType stream_type
) override
;
72 void InitializeAudioDecoder(const AudioDecoderConfig
& config
,
73 const DecoderInitCB
& init_cb
) override
;
74 void InitializeVideoDecoder(const VideoDecoderConfig
& config
,
75 const DecoderInitCB
& init_cb
) override
;
76 void DecryptAndDecodeAudio(const scoped_refptr
<DecoderBuffer
>& encrypted
,
77 const AudioDecodeCB
& audio_decode_cb
) override
;
78 void DecryptAndDecodeVideo(const scoped_refptr
<DecoderBuffer
>& encrypted
,
79 const VideoDecodeCB
& video_decode_cb
) override
;
80 void ResetDecoder(StreamType stream_type
) override
;
81 void DeinitializeDecoder(StreamType stream_type
) override
;
84 // TODO(fgalligan): Remove this and change KeyMap to use crypto::SymmetricKey
85 // as there are no decryptors that are performing an integrity check.
86 // Helper class that manages the decryption key.
89 explicit DecryptionKey(const std::string
& secret
);
92 // Creates the encryption key.
95 crypto::SymmetricKey
* decryption_key() { return decryption_key_
.get(); }
98 // The base secret that is used to create the decryption key.
99 const std::string secret_
;
101 // The key used to decrypt the data.
102 scoped_ptr
<crypto::SymmetricKey
> decryption_key_
;
104 DISALLOW_COPY_AND_ASSIGN(DecryptionKey
);
107 // Keep track of the keys for a key ID. If multiple sessions specify keys
108 // for the same key ID, then the last key inserted is used. The structure is
109 // optimized so that Decrypt() has fast access, at the cost of slow deletion
110 // of keys when a session is released.
111 class SessionIdDecryptionKeyMap
;
113 // Key ID <-> SessionIdDecryptionKeyMap map.
114 typedef base::ScopedPtrHashMap
<std::string
, SessionIdDecryptionKeyMap
>
115 KeyIdToSessionKeysMap
;
117 // Creates a DecryptionKey using |key_string| and associates it with |key_id|.
118 // Returns true if successful.
119 bool AddDecryptionKey(const std::string
& session_id
,
120 const std::string
& key_id
,
121 const std::string
& key_string
);
123 // Gets a DecryptionKey associated with |key_id|. The AesDecryptor still owns
124 // the key. Returns NULL if no key is associated with |key_id|.
125 DecryptionKey
* GetKey(const std::string
& key_id
) const;
127 // Determines if |key_id| is already specified for |session_id|.
128 bool HasKey(const std::string
& session_id
, const std::string
& key_id
);
130 // Deletes all keys associated with |session_id|.
131 void DeleteKeysForSession(const std::string
& session_id
);
133 // Callbacks for firing session events.
134 SessionMessageCB session_message_cb_
;
135 SessionClosedCB session_closed_cb_
;
136 SessionKeysChangeCB session_keys_change_cb_
;
138 // Since only Decrypt() is called off the renderer thread, we only need to
139 // protect |key_map_|, the only member variable that is shared between
140 // Decrypt() and other methods.
141 KeyIdToSessionKeysMap key_map_
; // Protected by |key_map_lock_|.
142 mutable base::Lock key_map_lock_
; // Protects the |key_map_|.
144 // Keeps track of current valid sessions.
145 std::set
<std::string
> valid_sessions_
;
147 // Make session ID unique per renderer by making it static. Session
148 // IDs seen by the app will be "1", "2", etc.
149 static uint32 next_session_id_
;
151 NewKeyCB new_audio_key_cb_
;
152 NewKeyCB new_video_key_cb_
;
154 // Protect |new_audio_key_cb_| and |new_video_key_cb_| as they are set on the
155 // main thread but called on the media thread.
156 mutable base::Lock new_key_cb_lock_
;
158 DISALLOW_COPY_AND_ASSIGN(AesDecryptor
);
163 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_