aes_decryptor.h revision 58537e28ecd584eab876aee8be7156509866d23a
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
5#ifndef MEDIA_CRYPTO_AES_DECRYPTOR_H_
6#define MEDIA_CRYPTO_AES_DECRYPTOR_H_
7
8#include <string>
9
10#include "base/basictypes.h"
11#include "base/containers/hash_tables.h"
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/strings/string_piece.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"
19
20namespace crypto {
21class SymmetricKey;
22}
23
24namespace media {
25
26// Decrypts an AES encrypted buffer into an unencrypted buffer. The AES
27// encryption must be CTR with a key size of 128bits.
28class MEDIA_EXPORT AesDecryptor : public MediaKeys, public Decryptor {
29 public:
30  AesDecryptor(const KeyAddedCB& key_added_cb,
31               const KeyErrorCB& key_error_cb,
32               const KeyMessageCB& key_message_cb);
33  virtual ~AesDecryptor();
34
35  // MediaKeys implementation.
36  virtual bool GenerateKeyRequest(const std::string& type,
37                                  const uint8* init_data,
38                                  int init_data_length) OVERRIDE;
39  virtual void AddKey(const uint8* key, int key_length,
40                      const uint8* init_data, int init_data_length,
41                      const std::string& session_id) OVERRIDE;
42  virtual void CancelKeyRequest(const std::string& session_id) OVERRIDE;
43  virtual Decryptor* GetDecryptor() OVERRIDE;
44
45  // Decryptor implementation.
46  virtual void RegisterNewKeyCB(StreamType stream_type,
47                                const NewKeyCB& key_added_cb) OVERRIDE;
48  virtual void Decrypt(StreamType stream_type,
49                       const scoped_refptr<DecoderBuffer>& encrypted,
50                       const DecryptCB& decrypt_cb) OVERRIDE;
51  virtual void CancelDecrypt(StreamType stream_type) OVERRIDE;
52  virtual void InitializeAudioDecoder(const AudioDecoderConfig& config,
53                                      const DecoderInitCB& init_cb) OVERRIDE;
54  virtual void InitializeVideoDecoder(const VideoDecoderConfig& config,
55                                      const DecoderInitCB& init_cb) OVERRIDE;
56  virtual void DecryptAndDecodeAudio(
57      const scoped_refptr<DecoderBuffer>& encrypted,
58      const AudioDecodeCB& audio_decode_cb) OVERRIDE;
59  virtual void DecryptAndDecodeVideo(
60      const scoped_refptr<DecoderBuffer>& encrypted,
61      const VideoDecodeCB& video_decode_cb) OVERRIDE;
62  virtual void ResetDecoder(StreamType stream_type) OVERRIDE;
63  virtual void DeinitializeDecoder(StreamType stream_type) OVERRIDE;
64
65 private:
66  // TODO(fgalligan): Remove this and change KeyMap to use crypto::SymmetricKey
67  // as there are no decryptors that are performing an integrity check.
68  // Helper class that manages the decryption key.
69  class DecryptionKey {
70   public:
71    explicit DecryptionKey(const std::string& secret);
72    ~DecryptionKey();
73
74    // Creates the encryption key.
75    bool Init();
76
77    crypto::SymmetricKey* decryption_key() { return decryption_key_.get(); }
78
79   private:
80    // The base secret that is used to create the decryption key.
81    const std::string secret_;
82
83    // The key used to decrypt the data.
84    scoped_ptr<crypto::SymmetricKey> decryption_key_;
85
86    DISALLOW_COPY_AND_ASSIGN(DecryptionKey);
87  };
88
89  // Creates a DecryptionKey using |key_string| and associates it with |key_id|.
90  // Returns true if successful.
91  bool AddDecryptionKey(const std::string& key_id,
92                        const std::string& key_string);
93
94  // Gets a DecryptionKey associated with |key_id|. The AesDecryptor still owns
95  // the key. Returns NULL if no key is associated with |key_id|.
96  DecryptionKey* GetKey(const std::string& key_id) const;
97
98  // Callbacks for firing key events.
99  KeyAddedCB key_added_cb_;
100  KeyErrorCB key_error_cb_;
101  KeyMessageCB key_message_cb_;
102
103  // KeyMap owns the DecryptionKey* and must delete them when they are
104  // not needed any more.
105  typedef base::hash_map<std::string, DecryptionKey*> KeyMap;
106
107  // Since only Decrypt() is called off the renderer thread, we only need to
108  // protect |key_map_|, the only member variable that is shared between
109  // Decrypt() and other methods.
110  KeyMap key_map_;  // Protected by the |key_map_lock_|.
111  mutable base::Lock key_map_lock_;  // Protects the |key_map_|.
112
113  // Make session ID unique per renderer by making it static.
114  // TODO(xhwang): Make session ID more strictly defined if needed:
115  // https://www.w3.org/Bugs/Public/show_bug.cgi?id=16739#c0
116  static uint32 next_session_id_;
117
118  NewKeyCB new_audio_key_cb_;
119  NewKeyCB new_video_key_cb_;
120
121  DISALLOW_COPY_AND_ASSIGN(AesDecryptor);
122};
123
124}  // namespace media
125
126#endif  // MEDIA_CRYPTO_AES_DECRYPTOR_H_
127