Commit 125bd5c7 authored by xhwang@chromium.org's avatar xhwang@chromium.org

Code clean-up in AesDecryptor and test.

- Added GetKey() and SetKey() in AesDecryptor.
- Moved static functions out of AesDecryptorTest to make them file scope functions.

BUG=none
TEST=media_unittests

Review URL: https://chromiumcodereview.appspot.com/10822013

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@148984 0039d316-1c4b-4281-b951-d872f2087c98
parent 48288a5e
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "media/crypto/aes_decryptor.h" #include "media/crypto/aes_decryptor.h"
#include <vector>
#include "base/logging.h" #include "base/logging.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/string_number_conversions.h" #include "base/string_number_conversions.h"
...@@ -84,7 +86,7 @@ static bool CheckData(const DecoderBuffer& input, ...@@ -84,7 +86,7 @@ static bool CheckData(const DecoderBuffer& input,
enum ClearBytesBufferSel { enum ClearBytesBufferSel {
kSrcContainsClearBytes, kSrcContainsClearBytes,
kDstContainsClearBytes, kDstContainsClearBytes
}; };
static void CopySubsamples(const std::vector<SubsampleEntry>& subsamples, static void CopySubsamples(const std::vector<SubsampleEntry>& subsamples,
...@@ -250,16 +252,7 @@ void AesDecryptor::AddKey(const std::string& key_system, ...@@ -250,16 +252,7 @@ void AesDecryptor::AddKey(const std::string& key_system,
return; return;
} }
{ SetKey(key_id_string, decryption_key.Pass());
base::AutoLock auto_lock(key_map_lock_);
KeyMap::iterator found = key_map_.find(key_id_string);
if (found != key_map_.end()) {
delete found->second;
key_map_.erase(found);
}
key_map_[key_id_string] = decryption_key.release();
}
client_->KeyAdded(key_system, session_id); client_->KeyAdded(key_system, session_id);
} }
...@@ -272,17 +265,10 @@ void AesDecryptor::Decrypt(const scoped_refptr<DecoderBuffer>& encrypted, ...@@ -272,17 +265,10 @@ void AesDecryptor::Decrypt(const scoped_refptr<DecoderBuffer>& encrypted,
CHECK(encrypted->GetDecryptConfig()); CHECK(encrypted->GetDecryptConfig());
const std::string& key_id = encrypted->GetDecryptConfig()->key_id(); const std::string& key_id = encrypted->GetDecryptConfig()->key_id();
DecryptionKey* key = NULL; DecryptionKey* key = GetKey(key_id);
{
base::AutoLock auto_lock(key_map_lock_);
KeyMap::const_iterator found = key_map_.find(key_id);
if (found != key_map_.end())
key = found->second;
}
if (!key) { if (!key) {
// TODO(fgalligan): Fire a need_key event here and add a test. // TODO(xhwang): Fire a need_key event here and add a test.
DVLOG(1) << "Could not find a matching key for given key ID."; DVLOG(1) << "Could not find a matching key for the given key ID.";
decrypt_cb.Run(kError, NULL); decrypt_cb.Run(kError, NULL);
return; return;
} }
...@@ -317,8 +303,28 @@ void AesDecryptor::Decrypt(const scoped_refptr<DecoderBuffer>& encrypted, ...@@ -317,8 +303,28 @@ void AesDecryptor::Decrypt(const scoped_refptr<DecoderBuffer>& encrypted,
decrypt_cb.Run(kSuccess, decrypted); decrypt_cb.Run(kSuccess, decrypted);
} }
AesDecryptor::DecryptionKey::DecryptionKey( void AesDecryptor::SetKey(const std::string& key_id,
const std::string& secret) scoped_ptr<DecryptionKey> decryption_key) {
base::AutoLock auto_lock(key_map_lock_);
KeyMap::iterator found = key_map_.find(key_id);
if (found != key_map_.end()) {
delete found->second;
key_map_.erase(found);
}
key_map_[key_id] = decryption_key.release();
}
AesDecryptor::DecryptionKey* AesDecryptor::GetKey(
const std::string& key_id) const {
base::AutoLock auto_lock(key_map_lock_);
KeyMap::const_iterator found = key_map_.find(key_id);
if (found == key_map_.end())
return NULL;
return found->second;
}
AesDecryptor::DecryptionKey::DecryptionKey(const std::string& secret)
: secret_(secret) { : secret_(secret) {
} }
...@@ -326,28 +332,25 @@ AesDecryptor::DecryptionKey::~DecryptionKey() {} ...@@ -326,28 +332,25 @@ AesDecryptor::DecryptionKey::~DecryptionKey() {}
bool AesDecryptor::DecryptionKey::Init() { bool AesDecryptor::DecryptionKey::Init() {
CHECK(!secret_.empty()); CHECK(!secret_.empty());
decryption_key_.reset( decryption_key_.reset(crypto::SymmetricKey::Import(
crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, secret_)); crypto::SymmetricKey::AES, secret_));
if (!decryption_key_.get()) { if (!decryption_key_.get())
return false; return false;
}
std::string raw_key = DeriveKey(secret_, std::string raw_key = DeriveKey(secret_,
kWebmEncryptionSeed, kWebmEncryptionSeed,
secret_.length()); secret_.length());
if (raw_key.empty()) { if (raw_key.empty())
return false; return false;
}
webm_decryption_key_.reset( webm_decryption_key_.reset(crypto::SymmetricKey::Import(
crypto::SymmetricKey::Import(crypto::SymmetricKey::AES, raw_key)); crypto::SymmetricKey::AES, raw_key));
if (!webm_decryption_key_.get()) { if (!webm_decryption_key_.get())
return false; return false;
}
hmac_key_ = DeriveKey(secret_, kWebmHmacSeed, kWebmSha1DigestSize); hmac_key_ = DeriveKey(secret_, kWebmHmacSeed, kWebmSha1DigestSize);
if (hmac_key_.empty()) { if (hmac_key_.empty())
return false; return false;
}
return true; return true;
} }
......
...@@ -89,6 +89,13 @@ class MEDIA_EXPORT AesDecryptor : public Decryptor { ...@@ -89,6 +89,13 @@ class MEDIA_EXPORT AesDecryptor : public Decryptor {
DISALLOW_COPY_AND_ASSIGN(DecryptionKey); DISALLOW_COPY_AND_ASSIGN(DecryptionKey);
}; };
// Sets |key| for |key_id|. The AesDecryptor takes the ownership of the |key|.
void SetKey(const std::string& key_id, scoped_ptr<DecryptionKey> key);
// Gets a DecryptionKey associated with |key_id|. The AesDecryptor still owns
// the key. Returns NULL if no key is associated with |key_id|.
DecryptionKey* GetKey(const std::string& key_id) const;
// KeyMap owns the DecryptionKey* and must delete them when they are // KeyMap owns the DecryptionKey* and must delete them when they are
// not needed any more. // not needed any more.
typedef base::hash_map<std::string, DecryptionKey*> KeyMap; typedef base::hash_map<std::string, DecryptionKey*> KeyMap;
...@@ -97,7 +104,7 @@ class MEDIA_EXPORT AesDecryptor : public Decryptor { ...@@ -97,7 +104,7 @@ class MEDIA_EXPORT AesDecryptor : public Decryptor {
// protect |key_map_|, the only member variable that is shared between // protect |key_map_|, the only member variable that is shared between
// Decrypt() and other methods. // Decrypt() and other methods.
KeyMap key_map_; // Protected by the |key_map_lock_|. KeyMap key_map_; // Protected by the |key_map_lock_|.
base::Lock key_map_lock_; // Protects the |key_map_|. mutable base::Lock key_map_lock_; // Protects the |key_map_|.
// Make session ID unique per renderer by making it static. // Make session ID unique per renderer by making it static.
// TODO(xhwang): Make session ID more strictly defined if needed: // TODO(xhwang): Make session ID more strictly defined if needed:
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment