2015-04-02 13:08:27 -07: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-23 15:04:49 -07:00
|
|
|
|
2015-01-11 23:15:55 -08:00
|
|
|
#ifndef __ClearKeyDecryptionManager_h__
|
|
|
|
#define __ClearKeyDecryptionManager_h__
|
2014-09-23 15:04:49 -07:00
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "ClearKeyUtils.h"
|
2014-12-18 12:54:34 -08:00
|
|
|
#include "RefCounted.h"
|
2014-09-23 15:04:49 -07:00
|
|
|
|
|
|
|
class ClearKeyDecryptor;
|
2015-01-11 23:15:55 -08:00
|
|
|
|
2015-11-30 21:13:58 -08:00
|
|
|
class CryptoMetaData {
|
|
|
|
public:
|
|
|
|
CryptoMetaData() {}
|
|
|
|
|
|
|
|
explicit CryptoMetaData(const GMPEncryptedBufferMetadata* aCrypto)
|
|
|
|
{
|
|
|
|
Init(aCrypto);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Init(const GMPEncryptedBufferMetadata* aCrypto)
|
|
|
|
{
|
|
|
|
if (!aCrypto) {
|
|
|
|
assert(!IsValid());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Assign(mKeyId, aCrypto->KeyId(), aCrypto->KeyIdSize());
|
|
|
|
Assign(mIV, aCrypto->IV(), aCrypto->IVSize());
|
|
|
|
Assign(mClearBytes, aCrypto->ClearBytes(), aCrypto->NumSubsamples());
|
|
|
|
Assign(mCipherBytes, aCrypto->CipherBytes(), aCrypto->NumSubsamples());
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
std::vector<uint16_t> mClearBytes;
|
|
|
|
std::vector<uint32_t> mCipherBytes;
|
|
|
|
};
|
|
|
|
|
2015-01-11 23:15:55 -08:00
|
|
|
class ClearKeyDecryptionManager : public RefCounted
|
2014-09-23 15:04:49 -07:00
|
|
|
{
|
2015-01-11 23:15:55 -08:00
|
|
|
private:
|
2014-10-27 18:11:55 -07:00
|
|
|
ClearKeyDecryptionManager();
|
2015-01-11 23:15:55 -08:00
|
|
|
~ClearKeyDecryptionManager();
|
2014-09-23 15:04:49 -07:00
|
|
|
|
2015-01-11 23:15:55 -08:00
|
|
|
static ClearKeyDecryptionManager* sInstance;
|
2014-09-23 15:04:49 -07:00
|
|
|
|
2015-01-11 23:15:55 -08:00
|
|
|
public:
|
|
|
|
static ClearKeyDecryptionManager* Get();
|
2014-09-23 15:04:49 -07:00
|
|
|
|
2015-01-11 23:15:55 -08:00
|
|
|
bool HasSeenKeyId(const KeyId& aKeyId) const;
|
|
|
|
bool HasKeyForKeyId(const KeyId& aKeyId) const;
|
2014-09-23 15:04:49 -07:00
|
|
|
|
2015-01-11 23:15:55 -08:00
|
|
|
const Key& GetDecryptionKey(const KeyId& aKeyId);
|
2014-09-23 15:04:49 -07:00
|
|
|
|
2015-01-11 23:15:55 -08: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-23 15:04:49 -07:00
|
|
|
|
2015-11-30 21:13:58 -08:00
|
|
|
// Decrypts buffer *in place*.
|
2015-01-11 23:15:55 -08:00
|
|
|
GMPErr Decrypt(uint8_t* aBuffer, uint32_t aBufferSize,
|
2015-11-30 21:13:58 -08:00
|
|
|
const CryptoMetaData& aMetadata);
|
|
|
|
GMPErr Decrypt(std::vector<uint8_t>& aBuffer,
|
|
|
|
const CryptoMetaData& aMetadata);
|
2014-09-23 15:04:49 -07:00
|
|
|
|
2015-01-11 23:15:55 -08:00
|
|
|
void Shutdown();
|
2014-12-18 12:54:34 -08:00
|
|
|
|
2014-09-23 15:04:49 -07:00
|
|
|
private:
|
2015-01-11 23:15:55 -08:00
|
|
|
bool IsExpectingKeyForKeyId(const KeyId& aKeyId) const;
|
2014-09-23 15:04:49 -07:00
|
|
|
|
|
|
|
std::map<KeyId, ClearKeyDecryptor*> mDecryptors;
|
|
|
|
};
|
|
|
|
|
2015-01-11 23:15:55 -08:00
|
|
|
#endif // __ClearKeyDecryptionManager_h__
|