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 const std::string
& init_data_type
,
45 const uint8
* init_data
,
47 scoped_ptr
<NewSessionCdmPromise
> promise
) override
;
48 void LoadSession(SessionType session_type
,
49 const std::string
& web_session_id
,
50 scoped_ptr
<NewSessionCdmPromise
> promise
) override
;
51 void UpdateSession(const std::string
& web_session_id
,
52 const uint8
* response
,
54 scoped_ptr
<SimpleCdmPromise
> promise
) override
;
55 void CloseSession(const std::string
& web_session_id
,
56 scoped_ptr
<SimpleCdmPromise
> promise
) override
;
57 void RemoveSession(const std::string
& web_session_id
,
58 scoped_ptr
<SimpleCdmPromise
> promise
) override
;
59 CdmContext
* GetCdmContext() override
;
61 // CdmContext implementation.
62 Decryptor
* GetDecryptor() override
;
63 #if defined(ENABLE_BROWSER_CDMS)
64 int GetCdmId() const override
;
65 #endif // defined(ENABLE_BROWSER_CDMS)
67 // Decryptor implementation.
68 void RegisterNewKeyCB(StreamType stream_type
,
69 const NewKeyCB
& key_added_cb
) override
;
70 void Decrypt(StreamType stream_type
,
71 const scoped_refptr
<DecoderBuffer
>& encrypted
,
72 const DecryptCB
& decrypt_cb
) override
;
73 void CancelDecrypt(StreamType stream_type
) override
;
74 void InitializeAudioDecoder(const AudioDecoderConfig
& config
,
75 const DecoderInitCB
& init_cb
) override
;
76 void InitializeVideoDecoder(const VideoDecoderConfig
& config
,
77 const DecoderInitCB
& init_cb
) override
;
78 void DecryptAndDecodeAudio(const scoped_refptr
<DecoderBuffer
>& encrypted
,
79 const AudioDecodeCB
& audio_decode_cb
) override
;
80 void DecryptAndDecodeVideo(const scoped_refptr
<DecoderBuffer
>& encrypted
,
81 const VideoDecodeCB
& video_decode_cb
) override
;
82 void ResetDecoder(StreamType stream_type
) override
;
83 void DeinitializeDecoder(StreamType stream_type
) override
;
86 // TODO(fgalligan): Remove this and change KeyMap to use crypto::SymmetricKey
87 // as there are no decryptors that are performing an integrity check.
88 // Helper class that manages the decryption key.
91 explicit DecryptionKey(const std::string
& secret
);
94 // Creates the encryption key.
97 crypto::SymmetricKey
* decryption_key() { return decryption_key_
.get(); }
100 // The base secret that is used to create the decryption key.
101 const std::string secret_
;
103 // The key used to decrypt the data.
104 scoped_ptr
<crypto::SymmetricKey
> decryption_key_
;
106 DISALLOW_COPY_AND_ASSIGN(DecryptionKey
);
109 // Keep track of the keys for a key ID. If multiple sessions specify keys
110 // for the same key ID, then the last key inserted is used. The structure is
111 // optimized so that Decrypt() has fast access, at the cost of slow deletion
112 // of keys when a session is released.
113 class SessionIdDecryptionKeyMap
;
115 // Key ID <-> SessionIdDecryptionKeyMap map.
116 typedef base::ScopedPtrHashMap
<std::string
, SessionIdDecryptionKeyMap
>
117 KeyIdToSessionKeysMap
;
119 // Creates a DecryptionKey using |key_string| and associates it with |key_id|.
120 // Returns true if successful.
121 bool AddDecryptionKey(const std::string
& web_session_id
,
122 const std::string
& key_id
,
123 const std::string
& key_string
);
125 // Gets a DecryptionKey associated with |key_id|. The AesDecryptor still owns
126 // the key. Returns NULL if no key is associated with |key_id|.
127 DecryptionKey
* GetKey(const std::string
& key_id
) const;
129 // Deletes all keys associated with |web_session_id|.
130 void DeleteKeysForSession(const std::string
& web_session_id
);
132 // Callbacks for firing session events.
133 SessionMessageCB session_message_cb_
;
134 SessionClosedCB session_closed_cb_
;
135 SessionKeysChangeCB session_keys_change_cb_
;
137 // Since only Decrypt() is called off the renderer thread, we only need to
138 // protect |key_map_|, the only member variable that is shared between
139 // Decrypt() and other methods.
140 KeyIdToSessionKeysMap key_map_
; // Protected by |key_map_lock_|.
141 mutable base::Lock key_map_lock_
; // Protects the |key_map_|.
143 // Keeps track of current valid sessions.
144 std::set
<std::string
> valid_sessions_
;
146 // Make web session ID unique per renderer by making it static. Web session
147 // IDs seen by the app will be "1", "2", etc.
148 static uint32 next_web_session_id_
;
150 NewKeyCB new_audio_key_cb_
;
151 NewKeyCB new_video_key_cb_
;
153 // Protect |new_audio_key_cb_| and |new_video_key_cb_| as they are set on the
154 // main thread but called on the media thread.
155 mutable base::Lock new_key_cb_lock_
;
157 DISALLOW_COPY_AND_ASSIGN(AesDecryptor
);
162 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_