2015-04-02 23:08:27 +03:00
|
|
|
/*
|
|
|
|
* Copyright 2015, Mozilla Foundation and contributors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
#ifndef __ClearKeyDecryptionManager_h__
|
|
|
|
#define __ClearKeyDecryptionManager_h__
|
2014-09-24 02:04:49 +04:00
|
|
|
|
|
|
|
#include "ClearKeyUtils.h"
|
2017-01-11 23:52:05 +03:00
|
|
|
// This include is required in order for content_decryption_module to work
|
|
|
|
// on Unix systems.
|
|
|
|
#include "stddef.h"
|
|
|
|
#include "content_decryption_module.h"
|
2014-12-18 23:54:34 +03:00
|
|
|
#include "RefCounted.h"
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
#include <map>
|
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
class ClearKeyDecryptor;
|
2015-01-12 10:15:55 +03:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
class CryptoMetaData {
|
2015-12-01 08:13:58 +03:00
|
|
|
public:
|
2020-03-06 12:21:47 +03:00
|
|
|
CryptoMetaData() = default;
|
2015-12-01 08:13:58 +03:00
|
|
|
|
2019-01-14 19:40:04 +03:00
|
|
|
explicit CryptoMetaData(const cdm::InputBuffer_2* aInputBuffer) {
|
2017-01-11 23:52:05 +03:00
|
|
|
Init(aInputBuffer);
|
2015-12-01 08:13:58 +03:00
|
|
|
}
|
|
|
|
|
2019-01-14 19:40:04 +03:00
|
|
|
void Init(const cdm::InputBuffer_2* aInputBuffer) {
|
2017-01-11 23:52:05 +03:00
|
|
|
if (!aInputBuffer) {
|
2015-12-01 08:13:58 +03:00
|
|
|
assert(!IsValid());
|
|
|
|
return;
|
|
|
|
}
|
2017-01-11 23:52:05 +03:00
|
|
|
|
2020-03-26 23:53:59 +03:00
|
|
|
mEncryptionScheme = aInputBuffer->encryption_scheme;
|
2017-01-11 23:52:05 +03:00
|
|
|
Assign(mKeyId, aInputBuffer->key_id, aInputBuffer->key_id_size);
|
|
|
|
Assign(mIV, aInputBuffer->iv, aInputBuffer->iv_size);
|
2020-03-26 23:53:59 +03:00
|
|
|
mCryptByteBlock = aInputBuffer->pattern.crypt_byte_block;
|
|
|
|
mSkipByteBlock = aInputBuffer->pattern.skip_byte_block;
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aInputBuffer->num_subsamples; ++i) {
|
|
|
|
const cdm::SubsampleEntry& subsample = aInputBuffer->subsamples[i];
|
|
|
|
mClearBytes.push_back(subsample.clear_bytes);
|
2020-03-26 23:53:59 +03:00
|
|
|
mCipherBytes.push_back(subsample.cipher_bytes);
|
2017-01-11 23:52:05 +03:00
|
|
|
}
|
2015-12-01 08:13:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid() const {
|
|
|
|
return !mKeyId.empty() && !mIV.empty() && !mCipherBytes.empty() &&
|
|
|
|
!mClearBytes.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t NumSubsamples() const {
|
|
|
|
assert(mClearBytes.size() == mCipherBytes.size());
|
|
|
|
return mClearBytes.size();
|
|
|
|
}
|
|
|
|
|
2020-03-26 23:53:59 +03:00
|
|
|
cdm::EncryptionScheme mEncryptionScheme;
|
2015-12-01 08:13:58 +03:00
|
|
|
std::vector<uint8_t> mKeyId;
|
|
|
|
std::vector<uint8_t> mIV;
|
2020-03-26 23:53:59 +03:00
|
|
|
uint32_t mCryptByteBlock;
|
|
|
|
uint32_t mSkipByteBlock;
|
2017-01-11 23:52:05 +03:00
|
|
|
std::vector<uint32_t> mClearBytes;
|
2015-12-01 08:13:58 +03:00
|
|
|
std::vector<uint32_t> mCipherBytes;
|
|
|
|
};
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
class ClearKeyDecryptionManager : public RefCounted {
|
|
|
|
private:
|
2014-10-28 04:11:55 +03:00
|
|
|
ClearKeyDecryptionManager();
|
2015-01-12 10:15:55 +03:00
|
|
|
~ClearKeyDecryptionManager();
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
static ClearKeyDecryptionManager* sInstance;
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
public:
|
|
|
|
static ClearKeyDecryptionManager* Get();
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
bool HasSeenKeyId(const KeyId& aKeyId) const;
|
|
|
|
bool HasKeyForKeyId(const KeyId& aKeyId) const;
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
const Key& GetDecryptionKey(const KeyId& aKeyId);
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
// Create a decryptor for the given KeyId if one does not already exist.
|
|
|
|
void InitKey(KeyId aKeyId, Key aKey);
|
|
|
|
void ExpectKeyId(KeyId aKeyId);
|
|
|
|
void ReleaseKeyId(KeyId aKeyId);
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-12-01 08:13:58 +03:00
|
|
|
// Decrypts buffer *in place*.
|
2017-01-11 23:52:05 +03:00
|
|
|
cdm::Status Decrypt(uint8_t* aBuffer, uint32_t aBufferSize,
|
|
|
|
const CryptoMetaData& aMetadata);
|
|
|
|
cdm::Status Decrypt(std::vector<uint8_t>& aBuffer,
|
|
|
|
const CryptoMetaData& aMetadata);
|
2014-12-18 23:54:34 +03:00
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
private:
|
2015-01-12 10:15:55 +03:00
|
|
|
bool IsExpectingKeyForKeyId(const KeyId& aKeyId) const;
|
2014-09-24 02:04:49 +04:00
|
|
|
|
|
|
|
std::map<KeyId, ClearKeyDecryptor*> mDecryptors;
|
|
|
|
};
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
#endif // __ClearKeyDecryptionManager_h__
|