2014-09-24 02:04:49 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <vector>
|
2014-09-24 02:04:49 +04:00
|
|
|
|
|
|
|
#include "ClearKeyDecryptionManager.h"
|
2015-01-12 10:15:55 +03:00
|
|
|
#include "gmp-decryption.h"
|
2014-10-29 01:43:18 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2015-01-12 10:15:55 +03:00
|
|
|
#include "mozilla/Attributes.h"
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
class ClearKeyDecryptor : public RefCounted
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
|
|
|
public:
|
2015-01-12 10:15:55 +03:00
|
|
|
MOZ_IMPLICIT ClearKeyDecryptor();
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
void InitKey(const Key& aKey);
|
|
|
|
bool HasKey() const { return !!mKey.size(); }
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
GMPErr Decrypt(uint8_t* aBuffer, uint32_t aBufferSize,
|
2015-01-16 00:37:54 +03:00
|
|
|
const GMPEncryptedBufferMetadata* aMetadata);
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2014-12-18 23:54:34 +03:00
|
|
|
const Key& DecryptionKey() const { return mKey; }
|
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
private:
|
2015-01-12 10:15:55 +03:00
|
|
|
~ClearKeyDecryptor();
|
2014-09-24 02:04:49 +04:00
|
|
|
|
|
|
|
Key mKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
/* static */ ClearKeyDecryptionManager* ClearKeyDecryptionManager::sInstance = nullptr;
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
/* static */ ClearKeyDecryptionManager*
|
|
|
|
ClearKeyDecryptionManager::Get()
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
if (!sInstance) {
|
|
|
|
sInstance = new ClearKeyDecryptionManager();
|
|
|
|
}
|
|
|
|
return sInstance;
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
ClearKeyDecryptionManager::ClearKeyDecryptionManager()
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
CK_LOGD("ClearKeyDecryptionManager::ClearKeyDecryptionManager");
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
ClearKeyDecryptionManager::~ClearKeyDecryptionManager()
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
CK_LOGD("ClearKeyDecryptionManager::~ClearKeyDecryptionManager");
|
2014-12-18 23:54:34 +03:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
sInstance = nullptr;
|
2014-12-18 23:54:34 +03:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
for (auto it = mDecryptors.begin(); it != mDecryptors.end(); it++) {
|
|
|
|
it->second->Release();
|
2014-12-18 23:54:34 +03:00
|
|
|
}
|
2015-01-12 10:15:55 +03:00
|
|
|
mDecryptors.clear();
|
2014-12-18 23:54:34 +03:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
bool
|
|
|
|
ClearKeyDecryptionManager::HasSeenKeyId(const KeyId& aKeyId) const
|
2014-12-18 23:54:34 +03:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
CK_LOGD("ClearKeyDecryptionManager::HasSeenKeyId");
|
|
|
|
return mDecryptors.find(aKeyId) != mDecryptors.end();
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
bool
|
|
|
|
ClearKeyDecryptionManager::IsExpectingKeyForKeyId(const KeyId& aKeyId) const
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
CK_LOGD("ClearKeyDecryptionManager::IsExpectingKeyForKeyId");
|
|
|
|
const auto& decryptor = mDecryptors.find(aKeyId);
|
|
|
|
return decryptor != mDecryptors.end() && !decryptor->second->HasKey();
|
2014-12-18 23:54:34 +03:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
bool
|
|
|
|
ClearKeyDecryptionManager::HasKeyForKeyId(const KeyId& aKeyId) const
|
2014-12-18 23:54:34 +03:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
CK_LOGD("ClearKeyDecryptionManager::HasKeyForKeyId");
|
|
|
|
const auto& decryptor = mDecryptors.find(aKeyId);
|
|
|
|
return decryptor != mDecryptors.end() && decryptor->second->HasKey();
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
const Key&
|
|
|
|
ClearKeyDecryptionManager::GetDecryptionKey(const KeyId& aKeyId)
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
MOZ_ASSERT(HasKeyForKeyId(aKeyId));
|
|
|
|
return mDecryptors[aKeyId]->DecryptionKey();
|
2014-12-18 23:54:34 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-12 10:15:55 +03:00
|
|
|
ClearKeyDecryptionManager::InitKey(KeyId aKeyId, Key aKey)
|
2014-12-18 23:54:34 +03:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
CK_LOGD("ClearKeyDecryptionManager::InitKey");
|
|
|
|
if (IsExpectingKeyForKeyId(aKeyId)) {
|
|
|
|
mDecryptors[aKeyId]->InitKey(aKey);
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-12 10:15:55 +03:00
|
|
|
ClearKeyDecryptionManager::ExpectKeyId(KeyId aKeyId)
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
CK_LOGD("ClearKeyDecryptionManager::ExpectKeyId");
|
|
|
|
if (!HasSeenKeyId(aKeyId)) {
|
|
|
|
mDecryptors[aKeyId] = new ClearKeyDecryptor();
|
2014-12-18 23:54:34 +03:00
|
|
|
}
|
2015-01-12 10:15:55 +03:00
|
|
|
mDecryptors[aKeyId]->AddRef();
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-12 10:15:55 +03:00
|
|
|
ClearKeyDecryptionManager::ReleaseKeyId(KeyId aKeyId)
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
CK_LOGD("ClearKeyDecryptionManager::ReleaseKeyId");
|
|
|
|
ClearKeyDecryptor* decryptor = mDecryptors[aKeyId];
|
|
|
|
if (!decryptor->Release()) {
|
|
|
|
mDecryptors.erase(aKeyId);
|
|
|
|
}
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
GMPErr
|
|
|
|
ClearKeyDecryptionManager::Decrypt(uint8_t* aBuffer, uint32_t aBufferSize,
|
2015-01-16 00:37:54 +03:00
|
|
|
const GMPEncryptedBufferMetadata* aMetadata)
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
|
|
|
CK_LOGD("ClearKeyDecryptionManager::Decrypt");
|
|
|
|
KeyId keyId(aMetadata->KeyId(), aMetadata->KeyId() + aMetadata->KeyIdSize());
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
if (!HasKeyForKeyId(keyId)) {
|
|
|
|
return GMPNoKeyErr;
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
return mDecryptors[keyId]->Decrypt(aBuffer, aBufferSize, aMetadata);
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
ClearKeyDecryptor::ClearKeyDecryptor()
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
CK_LOGD("ClearKeyDecryptor ctor");
|
|
|
|
}
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
ClearKeyDecryptor::~ClearKeyDecryptor()
|
|
|
|
{
|
|
|
|
CK_LOGD("ClearKeyDecryptor dtor; key ID = %08x...", *(uint32_t*)&mKey[0]);
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-12 10:15:55 +03:00
|
|
|
ClearKeyDecryptor::InitKey(const Key& aKey)
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
mKey = aKey;
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
GMPErr
|
|
|
|
ClearKeyDecryptor::Decrypt(uint8_t* aBuffer, uint32_t aBufferSize,
|
2015-01-16 00:37:54 +03:00
|
|
|
const GMPEncryptedBufferMetadata* aMetadata)
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
CK_LOGD("ClearKeyDecryptor::Decrypt");
|
2014-09-24 02:04:49 +04:00
|
|
|
// If the sample is split up into multiple encrypted subsamples, we need to
|
|
|
|
// stitch them into one continuous buffer for decryption.
|
2015-01-12 10:15:55 +03:00
|
|
|
std::vector<uint8_t> tmp(aBufferSize);
|
2014-09-24 02:04:49 +04:00
|
|
|
|
|
|
|
if (aMetadata->NumSubsamples()) {
|
|
|
|
// Take all encrypted parts of subsamples and stitch them into one
|
|
|
|
// continuous encrypted buffer.
|
2015-01-12 10:15:55 +03:00
|
|
|
unsigned char* data = aBuffer;
|
2014-09-24 02:04:49 +04:00
|
|
|
unsigned char* iter = &tmp[0];
|
|
|
|
for (size_t i = 0; i < aMetadata->NumSubsamples(); i++) {
|
|
|
|
data += aMetadata->ClearBytes()[i];
|
|
|
|
uint32_t cipherBytes = aMetadata->CipherBytes()[i];
|
|
|
|
|
|
|
|
memcpy(iter, data, cipherBytes);
|
|
|
|
|
|
|
|
data += cipherBytes;
|
|
|
|
iter += cipherBytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
tmp.resize((size_t)(iter - &tmp[0]));
|
|
|
|
} else {
|
2015-01-12 10:15:55 +03:00
|
|
|
memcpy(&tmp[0], aBuffer, aBufferSize);
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
2014-10-29 01:43:18 +03:00
|
|
|
MOZ_ASSERT(aMetadata->IVSize() == 8 || aMetadata->IVSize() == 16);
|
2015-01-12 10:15:55 +03:00
|
|
|
std::vector<uint8_t> iv(aMetadata->IV(), aMetadata->IV() + aMetadata->IVSize());
|
2014-09-24 02:04:49 +04:00
|
|
|
iv.insert(iv.end(), CLEARKEY_KEY_LEN - aMetadata->IVSize(), 0);
|
|
|
|
|
|
|
|
ClearKeyUtils::DecryptAES(mKey, tmp, iv);
|
|
|
|
|
|
|
|
if (aMetadata->NumSubsamples()) {
|
|
|
|
// Take the decrypted buffer, split up into subsamples, and insert those
|
|
|
|
// subsamples back into their original position in the original buffer.
|
2015-01-12 10:15:55 +03:00
|
|
|
unsigned char* data = aBuffer;
|
2014-09-24 02:04:49 +04:00
|
|
|
unsigned char* iter = &tmp[0];
|
|
|
|
for (size_t i = 0; i < aMetadata->NumSubsamples(); i++) {
|
|
|
|
data += aMetadata->ClearBytes()[i];
|
|
|
|
uint32_t cipherBytes = aMetadata->CipherBytes()[i];
|
|
|
|
|
|
|
|
memcpy(data, iter, cipherBytes);
|
|
|
|
|
|
|
|
data += cipherBytes;
|
|
|
|
iter += cipherBytes;
|
|
|
|
}
|
|
|
|
} else {
|
2015-01-12 10:15:55 +03:00
|
|
|
memcpy(aBuffer, &tmp[0], aBufferSize);
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
return GMPNoErr;
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|