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
|
|
|
|
2017-01-14 00:23:31 +03:00
|
|
|
#include <map>
|
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
#include "ClearKeyUtils.h"
|
2014-12-18 23:54:34 +03:00
|
|
|
#include "RefCounted.h"
|
2014-09-24 02:04:49 +04:00
|
|
|
|
|
|
|
class ClearKeyDecryptor;
|
2015-01-12 10:15:55 +03:00
|
|
|
|
2017-01-14 00:23:31 +03:00
|
|
|
class CryptoMetaData {
|
2015-12-01 08:13:58 +03:00
|
|
|
public:
|
|
|
|
CryptoMetaData() {}
|
|
|
|
|
2017-01-14 00:23:31 +03:00
|
|
|
explicit CryptoMetaData(const GMPEncryptedBufferMetadata* aCrypto)
|
2015-12-01 08:13:58 +03:00
|
|
|
{
|
2017-01-14 00:23:31 +03:00
|
|
|
Init(aCrypto);
|
2015-12-01 08:13:58 +03:00
|
|
|
}
|
|
|
|
|
2017-01-14 00:23:31 +03:00
|
|
|
void Init(const GMPEncryptedBufferMetadata* aCrypto)
|
2015-12-01 08:13:58 +03:00
|
|
|
{
|
2017-01-14 00:23:31 +03:00
|
|
|
if (!aCrypto) {
|
2015-12-01 08:13:58 +03:00
|
|
|
assert(!IsValid());
|
|
|
|
return;
|
|
|
|
}
|
2017-01-14 00:23:31 +03:00
|
|
|
Assign(mKeyId, aCrypto->KeyId(), aCrypto->KeyIdSize());
|
|
|
|
Assign(mIV, aCrypto->IV(), aCrypto->IVSize());
|
|
|
|
Assign(mClearBytes, aCrypto->ClearBytes(), aCrypto->NumSubsamples());
|
|
|
|
Assign(mCipherBytes, aCrypto->CipherBytes(), aCrypto->NumSubsamples());
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<uint8_t> mKeyId;
|
|
|
|
std::vector<uint8_t> mIV;
|
2017-01-14 00:23:31 +03:00
|
|
|
std::vector<uint16_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
|
2014-09-24 02:04:49 +04:00
|
|
|
{
|
2015-01-12 10:15:55 +03:00
|
|
|
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-14 00:23:31 +03:00
|
|
|
GMPErr Decrypt(uint8_t* aBuffer, uint32_t aBufferSize,
|
|
|
|
const CryptoMetaData& aMetadata);
|
|
|
|
GMPErr Decrypt(std::vector<uint8_t>& aBuffer,
|
|
|
|
const CryptoMetaData& aMetadata);
|
|
|
|
|
|
|
|
void Shutdown();
|
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__
|