[AndroidWebViewShell] Replace rebaseline script with a new version using test_runner.py
[chromium-blink-merge.git] / media / cdm / aes_decryptor.h
blobe2540265af53f338b01d654a2ee3c872f4265789
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_
8 #include <set>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/containers/scoped_ptr_hash_map.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/synchronization/lock.h"
17 #include "media/base/cdm_context.h"
18 #include "media/base/decryptor.h"
19 #include "media/base/media_export.h"
20 #include "media/base/media_keys.h"
22 class GURL;
24 namespace crypto {
25 class SymmetricKey;
28 namespace media {
30 // Decrypts an AES encrypted buffer into an unencrypted buffer. The AES
31 // encryption must be CTR with a key size of 128bits.
32 class MEDIA_EXPORT AesDecryptor : public MediaKeys,
33 public CdmContext,
34 public Decryptor {
35 public:
36 AesDecryptor(const GURL& security_origin,
37 const SessionMessageCB& session_message_cb,
38 const SessionClosedCB& session_closed_cb,
39 const SessionKeysChangeCB& session_keys_change_cb);
40 ~AesDecryptor() override;
42 // MediaKeys implementation.
43 void SetServerCertificate(const std::vector<uint8_t>& certificate,
44 scoped_ptr<SimpleCdmPromise> promise) override;
45 void CreateSessionAndGenerateRequest(
46 SessionType session_type,
47 EmeInitDataType init_data_type,
48 const std::vector<uint8_t>& init_data,
49 scoped_ptr<NewSessionCdmPromise> promise) override;
50 void LoadSession(SessionType session_type,
51 const std::string& session_id,
52 scoped_ptr<NewSessionCdmPromise> promise) override;
53 void UpdateSession(const std::string& session_id,
54 const std::vector<uint8_t>& response,
55 scoped_ptr<SimpleCdmPromise> promise) override;
56 void CloseSession(const std::string& session_id,
57 scoped_ptr<SimpleCdmPromise> promise) override;
58 void RemoveSession(const std::string& session_id,
59 scoped_ptr<SimpleCdmPromise> promise) override;
60 CdmContext* GetCdmContext() override;
62 // CdmContext implementation.
63 Decryptor* GetDecryptor() override;
64 int GetCdmId() const override;
66 // Decryptor implementation.
67 void RegisterNewKeyCB(StreamType stream_type,
68 const NewKeyCB& key_added_cb) override;
69 void Decrypt(StreamType stream_type,
70 const scoped_refptr<DecoderBuffer>& encrypted,
71 const DecryptCB& decrypt_cb) override;
72 void CancelDecrypt(StreamType stream_type) override;
73 void InitializeAudioDecoder(const AudioDecoderConfig& config,
74 const DecoderInitCB& init_cb) override;
75 void InitializeVideoDecoder(const VideoDecoderConfig& config,
76 const DecoderInitCB& init_cb) override;
77 void DecryptAndDecodeAudio(const scoped_refptr<DecoderBuffer>& encrypted,
78 const AudioDecodeCB& audio_decode_cb) override;
79 void DecryptAndDecodeVideo(const scoped_refptr<DecoderBuffer>& encrypted,
80 const VideoDecodeCB& video_decode_cb) override;
81 void ResetDecoder(StreamType stream_type) override;
82 void DeinitializeDecoder(StreamType stream_type) override;
84 private:
85 // TODO(fgalligan): Remove this and change KeyMap to use crypto::SymmetricKey
86 // as there are no decryptors that are performing an integrity check.
87 // Helper class that manages the decryption key.
88 class DecryptionKey {
89 public:
90 explicit DecryptionKey(const std::string& secret);
91 ~DecryptionKey();
93 // Creates the encryption key.
94 bool Init();
96 crypto::SymmetricKey* decryption_key() { return decryption_key_.get(); }
98 private:
99 // The base secret that is used to create the decryption key.
100 const std::string secret_;
102 // The key used to decrypt the data.
103 scoped_ptr<crypto::SymmetricKey> decryption_key_;
105 DISALLOW_COPY_AND_ASSIGN(DecryptionKey);
108 // Keep track of the keys for a key ID. If multiple sessions specify keys
109 // for the same key ID, then the last key inserted is used. The structure is
110 // optimized so that Decrypt() has fast access, at the cost of slow deletion
111 // of keys when a session is released.
112 class SessionIdDecryptionKeyMap;
114 // Key ID <-> SessionIdDecryptionKeyMap map.
115 typedef base::ScopedPtrHashMap<std::string,
116 scoped_ptr<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& 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_Locked(const std::string& key_id) const;
129 // Determines if |key_id| is already specified for |session_id|.
130 bool HasKey(const std::string& session_id, const std::string& key_id);
132 // Deletes all keys associated with |session_id|.
133 void DeleteKeysForSession(const std::string& session_id);
135 // Callbacks for firing session events.
136 SessionMessageCB session_message_cb_;
137 SessionClosedCB session_closed_cb_;
138 SessionKeysChangeCB session_keys_change_cb_;
140 // Since only Decrypt() is called off the renderer thread, we only need to
141 // protect |key_map_|, the only member variable that is shared between
142 // Decrypt() and other methods.
143 KeyIdToSessionKeysMap key_map_; // Protected by |key_map_lock_|.
144 mutable base::Lock key_map_lock_; // Protects the |key_map_|.
146 // Keeps track of current valid sessions.
147 std::set<std::string> valid_sessions_;
149 // Make session ID unique per renderer by making it static. Session
150 // IDs seen by the app will be "1", "2", etc.
151 static uint32_t next_session_id_;
153 NewKeyCB new_audio_key_cb_;
154 NewKeyCB new_video_key_cb_;
156 // Protect |new_audio_key_cb_| and |new_video_key_cb_| as they are set on the
157 // main thread but called on the media thread.
158 mutable base::Lock new_key_cb_lock_;
160 DISALLOW_COPY_AND_ASSIGN(AesDecryptor);
163 } // namespace media
165 #endif // MEDIA_CRYPTO_AES_DECRYPTOR_H_