aes_decryptor.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. #ifndef MEDIA_CDM_AES_DECRYPTOR_H_
  5. #define MEDIA_CDM_AES_DECRYPTOR_H_
  6. #include <stdint.h>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <unordered_map>
  11. #include <vector>
  12. #include "base/memory/ref_counted.h"
  13. #include "base/synchronization/lock.h"
  14. #include "base/thread_annotations.h"
  15. #include "media/base/callback_registry.h"
  16. #include "media/base/cdm_context.h"
  17. #include "media/base/cdm_key_information.h"
  18. #include "media/base/cdm_promise.h"
  19. #include "media/base/content_decryption_module.h"
  20. #include "media/base/decryptor.h"
  21. #include "media/base/media_export.h"
  22. #include "media/cdm/json_web_key.h"
  23. namespace crypto {
  24. class SymmetricKey;
  25. }
  26. namespace media {
  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 ContentDecryptionModule,
  30. public CdmContext,
  31. public Decryptor {
  32. public:
  33. AesDecryptor(const SessionMessageCB& session_message_cb,
  34. const SessionClosedCB& session_closed_cb,
  35. const SessionKeysChangeCB& session_keys_change_cb,
  36. const SessionExpirationUpdateCB& session_expiration_update_cb);
  37. AesDecryptor(const AesDecryptor&) = delete;
  38. AesDecryptor& operator=(const AesDecryptor&) = delete;
  39. // ContentDecryptionModule implementation.
  40. void SetServerCertificate(const std::vector<uint8_t>& certificate,
  41. std::unique_ptr<SimpleCdmPromise> promise) override;
  42. void CreateSessionAndGenerateRequest(
  43. CdmSessionType session_type,
  44. EmeInitDataType init_data_type,
  45. const std::vector<uint8_t>& init_data,
  46. std::unique_ptr<NewSessionCdmPromise> promise) override;
  47. void LoadSession(CdmSessionType session_type,
  48. const std::string& session_id,
  49. std::unique_ptr<NewSessionCdmPromise> promise) override;
  50. void UpdateSession(const std::string& session_id,
  51. const std::vector<uint8_t>& response,
  52. std::unique_ptr<SimpleCdmPromise> promise) override;
  53. void CloseSession(const std::string& session_id,
  54. std::unique_ptr<SimpleCdmPromise> promise) override;
  55. void RemoveSession(const std::string& session_id,
  56. std::unique_ptr<SimpleCdmPromise> promise) override;
  57. CdmContext* GetCdmContext() override;
  58. // CdmContext implementation.
  59. std::unique_ptr<CallbackRegistration> RegisterEventCB(
  60. EventCB event_cb) override;
  61. Decryptor* GetDecryptor() override;
  62. // Decryptor implementation.
  63. void Decrypt(StreamType stream_type,
  64. scoped_refptr<DecoderBuffer> encrypted,
  65. DecryptCB decrypt_cb) override;
  66. void CancelDecrypt(StreamType stream_type) override;
  67. void InitializeAudioDecoder(const AudioDecoderConfig& config,
  68. DecoderInitCB init_cb) override;
  69. void InitializeVideoDecoder(const VideoDecoderConfig& config,
  70. DecoderInitCB init_cb) override;
  71. void DecryptAndDecodeAudio(scoped_refptr<DecoderBuffer> encrypted,
  72. AudioDecodeCB audio_decode_cb) override;
  73. void DecryptAndDecodeVideo(scoped_refptr<DecoderBuffer> encrypted,
  74. VideoDecodeCB video_decode_cb) override;
  75. void ResetDecoder(StreamType stream_type) override;
  76. void DeinitializeDecoder(StreamType stream_type) override;
  77. bool CanAlwaysDecrypt() override;
  78. private:
  79. // Testing classes that needs to manipulate internal states for testing.
  80. friend class ClearKeyPersistentSessionCdm;
  81. // Internally this class supports persistent license type sessions so that
  82. // it can be used by ClearKeyPersistentSessionCdm. The following methods
  83. // will be used from ClearKeyPersistentSessionCdm to create and update
  84. // persistent sessions. Note that ClearKeyPersistentSessionCdm is only used
  85. // for testing, so persistent sessions will not be available generally.
  86. // Creates a new session with ID |session_id| and type |session_type|, and
  87. // adds it to the list of active sessions. Returns false if the session ID
  88. // is already in the list.
  89. bool CreateSession(const std::string& session_id,
  90. CdmSessionType session_type);
  91. // Gets the state of the session |session_id| as a JWK.
  92. std::string GetSessionStateAsJWK(const std::string& session_id);
  93. // Update session |session_id| with the JWK provided in |json_web_key_set|.
  94. // Returns true and sets |key_added| if successful, otherwise returns false
  95. // and |error_message| is the reason for failure.
  96. bool UpdateSessionWithJWK(const std::string& session_id,
  97. const std::string& json_web_key_set,
  98. bool* key_added,
  99. CdmPromise::Exception* exception,
  100. std::string* error_message);
  101. // Performs the final steps of UpdateSession (notify any listeners for keys
  102. // changed, resolve the promise, and generate a keys change event).
  103. void FinishUpdate(const std::string& session_id,
  104. bool key_added,
  105. std::unique_ptr<SimpleCdmPromise> promise);
  106. // TODO(fgalligan): Remove this and change KeyMap to use crypto::SymmetricKey
  107. // as there are no decryptors that are performing an integrity check.
  108. // Helper class that manages the decryption key.
  109. class DecryptionKey {
  110. public:
  111. explicit DecryptionKey(const std::string& secret);
  112. DecryptionKey(const DecryptionKey&) = delete;
  113. DecryptionKey& operator=(const DecryptionKey&) = delete;
  114. ~DecryptionKey();
  115. // Creates the encryption key.
  116. bool Init();
  117. const std::string& secret() { return secret_; }
  118. crypto::SymmetricKey* decryption_key() { return decryption_key_.get(); }
  119. private:
  120. // The base secret that is used to create the decryption key.
  121. const std::string secret_;
  122. // The key used to decrypt the data.
  123. std::unique_ptr<crypto::SymmetricKey> decryption_key_;
  124. };
  125. // Keep track of the keys for a key ID. If multiple sessions specify keys
  126. // for the same key ID, then the last key inserted is used. The structure is
  127. // optimized so that Decrypt() has fast access, at the cost of slow deletion
  128. // of keys when a session is released.
  129. class SessionIdDecryptionKeyMap;
  130. // Key ID <-> SessionIdDecryptionKeyMap map.
  131. using KeyIdToSessionKeysMap =
  132. std::unordered_map<std::string,
  133. std::unique_ptr<SessionIdDecryptionKeyMap>>;
  134. ~AesDecryptor() override;
  135. // Creates a DecryptionKey using |key_string| and associates it with |key_id|.
  136. // Returns true if successful.
  137. bool AddDecryptionKey(const std::string& session_id,
  138. const std::string& key_id,
  139. const std::string& key_string);
  140. // Gets a DecryptionKey associated with |key_id|. The AesDecryptor still owns
  141. // the key. Returns NULL if no key is associated with |key_id|.
  142. DecryptionKey* GetKey_Locked(const std::string& key_id) const
  143. EXCLUSIVE_LOCKS_REQUIRED(key_map_lock_);
  144. // Determines if |key_id| is already specified for |session_id|.
  145. bool HasKey(const std::string& session_id, const std::string& key_id);
  146. // Deletes all keys associated with |session_id|.
  147. void DeleteKeysForSession(const std::string& session_id);
  148. CdmKeysInfo GenerateKeysInfoList(const std::string& session_id,
  149. CdmKeyInformation::KeyStatus status);
  150. // Callbacks for firing session events. No SessionExpirationUpdateCB since
  151. // the keys never expire.
  152. SessionMessageCB session_message_cb_;
  153. SessionClosedCB session_closed_cb_;
  154. SessionKeysChangeCB session_keys_change_cb_;
  155. // Since only Decrypt() is called off the renderer thread, we only need to
  156. // protect |key_map_|, the only member variable that is shared between
  157. // Decrypt() and other methods.
  158. mutable base::Lock key_map_lock_;
  159. KeyIdToSessionKeysMap key_map_ GUARDED_BY(key_map_lock_);
  160. // Keeps track of current open sessions and their type. Although publicly
  161. // AesDecryptor only supports temporary sessions, ClearKeyPersistentSessionCdm
  162. // uses this class to also support persistent sessions, so save the
  163. // CdmSessionType for each session.
  164. std::map<std::string, CdmSessionType> open_sessions_;
  165. CallbackRegistry<EventCB::RunType> event_callbacks_;
  166. };
  167. } // namespace media
  168. #endif // MEDIA_CDM_AES_DECRYPTOR_H_