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.
|
|
|
|
*/
|
2015-01-12 10:15:55 +03:00
|
|
|
|
|
|
|
#include "ClearKeyDecryptionManager.h"
|
|
|
|
#include "ClearKeySessionManager.h"
|
|
|
|
#include "ClearKeyUtils.h"
|
|
|
|
#include "ClearKeyStorage.h"
|
2016-10-19 12:25:00 +03:00
|
|
|
#include "ClearKeyPersistence.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"
|
|
|
|
#include "psshparser/PsshParser.h"
|
|
|
|
|
2015-04-02 23:08:27 +03:00
|
|
|
#include <assert.h>
|
2017-01-11 23:52:05 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2015-01-12 10:15:55 +03:00
|
|
|
|
|
|
|
using namespace std;
|
2017-01-11 23:52:05 +03:00
|
|
|
using namespace cdm;
|
2015-01-12 10:15:55 +03:00
|
|
|
|
2017-11-23 11:47:13 +03:00
|
|
|
ClearKeySessionManager::ClearKeySessionManager(Host_9* aHost)
|
2015-01-12 10:15:55 +03:00
|
|
|
: mDecryptionManager(ClearKeyDecryptionManager::Get())
|
|
|
|
{
|
2015-01-27 08:35:36 +03:00
|
|
|
CK_LOGD("ClearKeySessionManager ctor %p", this);
|
2015-01-12 10:15:55 +03:00
|
|
|
AddRef();
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
mHost = aHost;
|
|
|
|
mPersistence = new ClearKeyPersistence(mHost);
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ClearKeySessionManager::~ClearKeySessionManager()
|
|
|
|
{
|
2015-01-27 08:35:36 +03:00
|
|
|
CK_LOGD("ClearKeySessionManager dtor %p", this);
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-11 23:52:05 +03:00
|
|
|
ClearKeySessionManager::Init(bool aDistinctiveIdentifierAllowed,
|
2016-07-07 08:26:15 +03:00
|
|
|
bool aPersistentStateAllowed)
|
2015-01-12 10:15:55 +03:00
|
|
|
{
|
|
|
|
CK_LOGD("ClearKeySessionManager::Init");
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
RefPtr<ClearKeySessionManager> self(this);
|
|
|
|
function<void()> onPersistentStateLoaded =
|
|
|
|
[self] ()
|
|
|
|
{
|
|
|
|
while (!self->mDeferredInitialize.empty()) {
|
|
|
|
function<void()> func = self->mDeferredInitialize.front();
|
|
|
|
self->mDeferredInitialize.pop();
|
|
|
|
|
|
|
|
func();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
mPersistence->EnsureInitialized(aPersistentStateAllowed,
|
|
|
|
move(onPersistentStateLoaded));
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-11 23:52:05 +03:00
|
|
|
ClearKeySessionManager::CreateSession(uint32_t aPromiseId,
|
|
|
|
InitDataType aInitDataType,
|
2015-01-12 10:15:55 +03:00
|
|
|
const uint8_t* aInitData,
|
|
|
|
uint32_t aInitDataSize,
|
2017-01-11 23:52:05 +03:00
|
|
|
SessionType aSessionType)
|
2015-01-12 10:15:55 +03:00
|
|
|
{
|
2017-01-18 04:49:58 +03:00
|
|
|
CK_LOGD("ClearKeySessionManager::CreateSession type:%u", aInitDataType);
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
// Copy the init data so it is correctly captured by the lambda
|
|
|
|
vector<uint8_t> initData(aInitData, aInitData + aInitDataSize);
|
|
|
|
|
|
|
|
RefPtr<ClearKeySessionManager> self(this);
|
|
|
|
function<void()> deferrer =
|
|
|
|
[self, aPromiseId, aInitDataType, initData, aSessionType] ()
|
|
|
|
{
|
|
|
|
self->CreateSession(aPromiseId,
|
|
|
|
aInitDataType,
|
|
|
|
initData.data(),
|
|
|
|
initData.size(),
|
|
|
|
aSessionType);
|
|
|
|
};
|
|
|
|
|
|
|
|
// If we haven't loaded, don't do this yet
|
2017-04-27 19:44:57 +03:00
|
|
|
if (MaybeDeferTillInitialized(move(deferrer))) {
|
2017-01-18 04:49:58 +03:00
|
|
|
CK_LOGD("Deferring CreateSession");
|
2017-01-11 23:52:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CK_LOGARRAY("ClearKeySessionManager::CreateSession initdata: ",
|
|
|
|
aInitData,
|
|
|
|
aInitDataSize);
|
|
|
|
|
|
|
|
// If 'DecryptingComplete' has been called mHost will be null so we can't
|
|
|
|
// won't be able to resolve our promise
|
|
|
|
if (!mHost) {
|
2017-01-18 04:49:58 +03:00
|
|
|
CK_LOGD("ClearKeySessionManager::CreateSession: mHost is nullptr");
|
2015-01-12 10:15:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
// initDataType must be "cenc", "keyids", or "webm".
|
|
|
|
if (aInitDataType != InitDataType::kCenc &&
|
|
|
|
aInitDataType != InitDataType::kKeyIds &&
|
|
|
|
aInitDataType != InitDataType::kWebM) {
|
|
|
|
|
|
|
|
string message = "initDataType is not supported by ClearKey";
|
|
|
|
mHost->OnRejectPromise(aPromiseId,
|
2017-11-23 11:47:13 +03:00
|
|
|
Exception::kExceptionNotSupportedError,
|
2017-01-11 23:52:05 +03:00
|
|
|
0,
|
|
|
|
message.c_str(),
|
|
|
|
message.size());
|
|
|
|
|
2016-10-19 12:25:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
string sessionId = mPersistence->GetNewSessionId(aSessionType);
|
2015-04-02 23:08:27 +03:00
|
|
|
assert(mSessions.find(sessionId) == mSessions.end());
|
2015-01-12 10:15:55 +03:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
ClearKeySession* session = new ClearKeySession(sessionId,
|
|
|
|
aSessionType);
|
|
|
|
|
|
|
|
if (!session->Init(aInitDataType, aInitData, aInitDataSize)) {
|
|
|
|
|
|
|
|
CK_LOGD("Failed to initialize session: %s", sessionId.c_str());
|
|
|
|
|
|
|
|
const static char* message = "Failed to initialize session";
|
|
|
|
mHost->OnRejectPromise(aPromiseId,
|
2017-11-23 11:47:13 +03:00
|
|
|
Exception::kExceptionInvalidStateError,
|
2017-01-11 23:52:05 +03:00
|
|
|
0,
|
|
|
|
message,
|
|
|
|
strlen(message));
|
2017-01-26 15:18:07 +03:00
|
|
|
delete session;
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
mSessions[sessionId] = session;
|
|
|
|
|
|
|
|
const vector<KeyId>& sessionKeys = session->GetKeyIds();
|
|
|
|
vector<KeyId> neededKeys;
|
2017-01-11 23:52:05 +03:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
for (auto it = sessionKeys.begin(); it != sessionKeys.end(); it++) {
|
|
|
|
// Need to request this key ID from the client. We always send a key
|
|
|
|
// request, whether or not another session has sent a request with the same
|
|
|
|
// key ID. Otherwise a script can end up waiting for another script to
|
|
|
|
// respond to the request (which may not necessarily happen).
|
|
|
|
neededKeys.push_back(*it);
|
|
|
|
mDecryptionManager->ExpectKeyId(*it);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (neededKeys.empty()) {
|
|
|
|
CK_LOGD("No keys needed from client.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a request for needed key data.
|
|
|
|
string request;
|
|
|
|
ClearKeyUtils::MakeKeyRequest(neededKeys, request, aSessionType);
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
// Resolve the promise with the new session information.
|
|
|
|
mHost->OnResolveNewSessionPromise(aPromiseId,
|
|
|
|
sessionId.c_str(),
|
|
|
|
sessionId.size());
|
|
|
|
|
|
|
|
mHost->OnSessionMessage(sessionId.c_str(),
|
|
|
|
sessionId.size(),
|
|
|
|
MessageType::kLicenseRequest,
|
|
|
|
request.c_str(),
|
2017-11-23 11:47:13 +03:00
|
|
|
request.size());
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClearKeySessionManager::LoadSession(uint32_t aPromiseId,
|
|
|
|
const char* aSessionId,
|
|
|
|
uint32_t aSessionIdLength)
|
|
|
|
{
|
2017-01-18 04:49:58 +03:00
|
|
|
CK_LOGD("ClearKeySessionManager::LoadSession");
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
// Copy the sessionId into a string so the lambda captures it properly.
|
|
|
|
string sessionId(aSessionId, aSessionId + aSessionIdLength);
|
|
|
|
|
|
|
|
// Hold a reference to the SessionManager so that it isn't released before
|
|
|
|
// we try to use it.
|
|
|
|
RefPtr<ClearKeySessionManager> self(this);
|
|
|
|
function<void()> deferrer =
|
|
|
|
[self, aPromiseId, sessionId] ()
|
|
|
|
{
|
|
|
|
self->LoadSession(aPromiseId, sessionId.data(), sessionId.size());
|
|
|
|
};
|
|
|
|
|
2017-04-27 19:44:57 +03:00
|
|
|
if (MaybeDeferTillInitialized(move(deferrer))) {
|
2017-01-18 04:49:58 +03:00
|
|
|
CK_LOGD("Deferring LoadSession");
|
2017-01-11 23:52:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the SessionManager has been shutdown mHost will be null and we won't
|
|
|
|
// be able to resolve the promise.
|
|
|
|
if (!mHost) {
|
2016-10-19 12:25:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
if (!ClearKeyUtils::IsValidSessionId(aSessionId, aSessionIdLength)) {
|
|
|
|
mHost->OnResolveNewSessionPromise(aPromiseId, nullptr, 0);
|
2016-10-19 12:25:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
if (!mPersistence->IsPersistentSessionId(sessionId)) {
|
|
|
|
mHost->OnResolveNewSessionPromise(aPromiseId, nullptr, 0);
|
2016-10-19 12:25:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
function<void(const uint8_t*, uint32_t)> success =
|
|
|
|
[self, sessionId, aPromiseId] (const uint8_t* data, uint32_t size)
|
|
|
|
{
|
|
|
|
self->PersistentSessionDataLoaded(aPromiseId,
|
|
|
|
sessionId,
|
|
|
|
data,
|
|
|
|
size);
|
|
|
|
};
|
|
|
|
|
2017-03-06 21:52:09 +03:00
|
|
|
function<void()> failure = [self, aPromiseId] {
|
2017-01-11 23:52:05 +03:00
|
|
|
if (!self->mHost) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// As per the API described in ContentDecryptionModule_8
|
|
|
|
self->mHost->OnResolveNewSessionPromise(aPromiseId, nullptr, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
ReadData(mHost, sessionId, move(success), move(failure));
|
2016-10-19 12:25:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-11 23:52:05 +03:00
|
|
|
ClearKeySessionManager::PersistentSessionDataLoaded(uint32_t aPromiseId,
|
2016-10-19 12:25:00 +03:00
|
|
|
const string& aSessionId,
|
|
|
|
const uint8_t* aKeyData,
|
|
|
|
uint32_t aKeyDataSize)
|
|
|
|
{
|
|
|
|
CK_LOGD("ClearKeySessionManager::PersistentSessionDataLoaded");
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
// Check that the SessionManager has not been shut down before we try and
|
|
|
|
// resolve any promises.
|
|
|
|
if (!mHost) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Contains(mSessions, aSessionId) ||
|
2016-10-19 12:25:00 +03:00
|
|
|
(aKeyDataSize % (2 * CENC_KEY_LEN)) != 0) {
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
// As per the instructions in ContentDecryptionModule_8
|
|
|
|
mHost->OnResolveNewSessionPromise(aPromiseId, nullptr, 0);
|
2016-10-19 12:25:00 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearKeySession* session = new ClearKeySession(aSessionId,
|
2017-01-11 23:52:05 +03:00
|
|
|
SessionType::kPersistentLicense);
|
|
|
|
|
2016-10-19 12:25:00 +03:00
|
|
|
mSessions[aSessionId] = session;
|
|
|
|
|
|
|
|
uint32_t numKeys = aKeyDataSize / (2 * CENC_KEY_LEN);
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
vector<KeyInformation> keyInfos;
|
2016-10-19 12:25:00 +03:00
|
|
|
vector<KeyIdPair> keyPairs;
|
|
|
|
for (uint32_t i = 0; i < numKeys; i ++) {
|
|
|
|
const uint8_t* base = aKeyData + 2 * CENC_KEY_LEN * i;
|
|
|
|
|
|
|
|
KeyIdPair keyPair;
|
|
|
|
|
|
|
|
keyPair.mKeyId = KeyId(base, base + CENC_KEY_LEN);
|
|
|
|
assert(keyPair.mKeyId.size() == CENC_KEY_LEN);
|
|
|
|
|
|
|
|
keyPair.mKey = Key(base + CENC_KEY_LEN, base + 2 * CENC_KEY_LEN);
|
|
|
|
assert(keyPair.mKey.size() == CENC_KEY_LEN);
|
|
|
|
|
|
|
|
session->AddKeyId(keyPair.mKeyId);
|
|
|
|
|
|
|
|
mDecryptionManager->ExpectKeyId(keyPair.mKeyId);
|
|
|
|
mDecryptionManager->InitKey(keyPair.mKeyId, keyPair.mKey);
|
|
|
|
mKeyIds.insert(keyPair.mKey);
|
2017-01-14 00:23:31 +03:00
|
|
|
keyPairs.push_back(keyPair);
|
2017-01-11 23:52:05 +03:00
|
|
|
|
2018-09-13 17:49:10 +03:00
|
|
|
KeyInformation keyInfo = {};
|
2017-01-11 23:52:05 +03:00
|
|
|
keyInfo.key_id = &keyPairs.back().mKeyId[0];
|
|
|
|
keyInfo.key_id_size = keyPair.mKeyId.size();
|
|
|
|
keyInfo.status = KeyStatus::kUsable;
|
|
|
|
|
|
|
|
keyInfos.push_back(keyInfo);
|
2016-10-19 12:25:00 +03:00
|
|
|
}
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
mHost->OnSessionKeysChange(&aSessionId[0],
|
|
|
|
aSessionId.size(),
|
|
|
|
true,
|
|
|
|
keyInfos.data(),
|
|
|
|
keyInfos.size());
|
|
|
|
|
|
|
|
mHost->OnResolveNewSessionPromise(aPromiseId,
|
|
|
|
aSessionId.c_str(),
|
|
|
|
aSessionId.size());
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClearKeySessionManager::UpdateSession(uint32_t aPromiseId,
|
|
|
|
const char* aSessionId,
|
|
|
|
uint32_t aSessionIdLength,
|
|
|
|
const uint8_t* aResponse,
|
|
|
|
uint32_t aResponseSize)
|
|
|
|
{
|
2017-01-18 04:49:58 +03:00
|
|
|
CK_LOGD("ClearKeySessionManager::UpdateSession");
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
// Copy the method arguments so we can capture them in the lambda
|
2017-01-14 00:23:31 +03:00
|
|
|
string sessionId(aSessionId, aSessionId + aSessionIdLength);
|
2017-01-11 23:52:05 +03:00
|
|
|
vector<uint8_t> response(aResponse, aResponse + aResponseSize);
|
|
|
|
|
|
|
|
// Hold a reference to the SessionManager so it isn't released before we
|
|
|
|
// callback.
|
|
|
|
RefPtr<ClearKeySessionManager> self(this);
|
|
|
|
function<void()> deferrer =
|
|
|
|
[self, aPromiseId, sessionId, response] ()
|
|
|
|
{
|
|
|
|
self->UpdateSession(aPromiseId,
|
|
|
|
sessionId.data(),
|
|
|
|
sessionId.size(),
|
|
|
|
response.data(),
|
|
|
|
response.size());
|
|
|
|
};
|
|
|
|
|
|
|
|
// If we haven't fully loaded, defer calling this method
|
2017-04-27 19:44:57 +03:00
|
|
|
if (MaybeDeferTillInitialized(move(deferrer))) {
|
2017-01-18 04:49:58 +03:00
|
|
|
CK_LOGD("Deferring LoadSession");
|
2017-01-11 23:52:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure the SessionManager has not been shutdown before we try and
|
|
|
|
// resolve any promises.
|
|
|
|
if (!mHost) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CK_LOGD("Updating session: %s", sessionId.c_str());
|
2015-01-12 10:15:55 +03:00
|
|
|
|
|
|
|
auto itr = mSessions.find(sessionId);
|
|
|
|
if (itr == mSessions.end() || !(itr->second)) {
|
|
|
|
CK_LOGW("ClearKey CDM couldn't resolve session ID in UpdateSession.");
|
2017-01-11 23:52:05 +03:00
|
|
|
CK_LOGD("Unable to find session: %s", sessionId.c_str());
|
|
|
|
mHost->OnRejectPromise(aPromiseId,
|
2017-11-23 11:47:13 +03:00
|
|
|
Exception::kExceptionTypeError,
|
2017-01-11 23:52:05 +03:00
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
0);
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
ClearKeySession* session = itr->second;
|
|
|
|
|
2016-10-07 13:16:30 +03:00
|
|
|
// Verify the size of session response.
|
|
|
|
if (aResponseSize >= kMaxSessionResponseLength) {
|
|
|
|
CK_LOGW("Session response size is not within a reasonable size.");
|
2017-01-11 23:52:05 +03:00
|
|
|
CK_LOGD("Failed to parse response for session %s", sessionId.c_str());
|
|
|
|
|
|
|
|
mHost->OnRejectPromise(aPromiseId,
|
2017-11-23 11:47:13 +03:00
|
|
|
Exception::kExceptionTypeError,
|
2017-01-11 23:52:05 +03:00
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
0);
|
|
|
|
|
2016-10-07 13:16:30 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
// Parse the response for any (key ID, key) pairs.
|
|
|
|
vector<KeyIdPair> keyPairs;
|
2017-01-11 23:52:05 +03:00
|
|
|
if (!ClearKeyUtils::ParseJWK(aResponse,
|
|
|
|
aResponseSize,
|
|
|
|
keyPairs,
|
|
|
|
session->Type())) {
|
2015-01-12 10:15:55 +03:00
|
|
|
CK_LOGW("ClearKey CDM failed to parse JSON Web Key.");
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
mHost->OnRejectPromise(aPromiseId,
|
2017-11-23 11:47:13 +03:00
|
|
|
Exception::kExceptionTypeError,
|
2017-01-11 23:52:05 +03:00
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
0);
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
vector<KeyInformation> keyInfos;
|
2016-10-04 12:18:46 +03:00
|
|
|
for (size_t i = 0; i < keyPairs.size(); i++) {
|
|
|
|
KeyIdPair& keyPair = keyPairs[i];
|
|
|
|
mDecryptionManager->InitKey(keyPair.mKeyId, keyPair.mKey);
|
|
|
|
mKeyIds.insert(keyPair.mKeyId);
|
2017-01-11 23:52:05 +03:00
|
|
|
|
2018-09-13 17:49:10 +03:00
|
|
|
KeyInformation keyInfo = {};
|
2017-01-11 23:52:05 +03:00
|
|
|
keyInfo.key_id = &keyPair.mKeyId[0];
|
|
|
|
keyInfo.key_id_size = keyPair.mKeyId.size();
|
|
|
|
keyInfo.status = KeyStatus::kUsable;
|
|
|
|
|
|
|
|
keyInfos.push_back(keyInfo);
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
mHost->OnSessionKeysChange(aSessionId,
|
|
|
|
aSessionIdLength,
|
|
|
|
true,
|
|
|
|
keyInfos.data(),
|
|
|
|
keyInfos.size());
|
|
|
|
|
|
|
|
if (session->Type() != SessionType::kPersistentLicense) {
|
|
|
|
mHost->OnResolvePromise(aPromiseId);
|
2015-01-12 10:15:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the keys on disk. We store a record whose name is the sessionId,
|
|
|
|
// and simply append each keyId followed by its key.
|
|
|
|
vector<uint8_t> keydata;
|
|
|
|
Serialize(session, keydata);
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
function<void()> resolve = [self, aPromiseId] ()
|
|
|
|
{
|
|
|
|
if (!self->mHost) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self->mHost->OnResolvePromise(aPromiseId);
|
|
|
|
};
|
|
|
|
|
|
|
|
function<void()> reject = [self, aPromiseId] ()
|
|
|
|
{
|
|
|
|
if (!self->mHost) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char* message = "Couldn't store cenc key init data";
|
|
|
|
self->mHost->OnRejectPromise(aPromiseId,
|
2017-11-23 11:47:13 +03:00
|
|
|
Exception::kExceptionInvalidStateError,
|
2017-01-11 23:52:05 +03:00
|
|
|
0,
|
|
|
|
message,
|
|
|
|
strlen(message));
|
|
|
|
};
|
|
|
|
|
|
|
|
WriteData(mHost, sessionId, keydata, move(resolve), move(reject));
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClearKeySessionManager::Serialize(const ClearKeySession* aSession,
|
|
|
|
std::vector<uint8_t>& aOutKeyData)
|
|
|
|
{
|
|
|
|
const std::vector<KeyId>& keyIds = aSession->GetKeyIds();
|
|
|
|
for (size_t i = 0; i < keyIds.size(); i++) {
|
|
|
|
const KeyId& keyId = keyIds[i];
|
|
|
|
if (!mDecryptionManager->HasKeyForKeyId(keyId)) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-10-07 01:14:04 +03:00
|
|
|
assert(keyId.size() == CENC_KEY_LEN);
|
2015-01-12 10:15:55 +03:00
|
|
|
aOutKeyData.insert(aOutKeyData.end(), keyId.begin(), keyId.end());
|
|
|
|
const Key& key = mDecryptionManager->GetDecryptionKey(keyId);
|
2016-10-07 01:14:04 +03:00
|
|
|
assert(key.size() == CENC_KEY_LEN);
|
2015-01-12 10:15:55 +03:00
|
|
|
aOutKeyData.insert(aOutKeyData.end(), key.begin(), key.end());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClearKeySessionManager::CloseSession(uint32_t aPromiseId,
|
|
|
|
const char* aSessionId,
|
|
|
|
uint32_t aSessionIdLength)
|
|
|
|
{
|
2017-01-18 04:49:58 +03:00
|
|
|
CK_LOGD("ClearKeySessionManager::CloseSession");
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
// Copy the sessionId into a string so we capture it properly.
|
|
|
|
string sessionId(aSessionId, aSessionId + aSessionIdLength);
|
|
|
|
// Hold a reference to the session manager, so it doesn't get deleted
|
|
|
|
// before we need to use it.
|
|
|
|
RefPtr<ClearKeySessionManager> self(this);
|
|
|
|
function<void()> deferrer =
|
|
|
|
[self, aPromiseId, sessionId] ()
|
|
|
|
{
|
|
|
|
self->CloseSession(aPromiseId, sessionId.data(), sessionId.size());
|
|
|
|
};
|
|
|
|
|
|
|
|
// If we haven't loaded, call this method later.
|
2017-04-27 19:44:57 +03:00
|
|
|
if (MaybeDeferTillInitialized(move(deferrer))) {
|
2017-01-18 04:49:58 +03:00
|
|
|
CK_LOGD("Deferring CloseSession");
|
2017-01-11 23:52:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If DecryptingComplete has been called mHost will be null and we won't
|
|
|
|
// be able to resolve our promise.
|
|
|
|
if (!mHost) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
auto itr = mSessions.find(sessionId);
|
|
|
|
if (itr == mSessions.end()) {
|
|
|
|
CK_LOGW("ClearKey CDM couldn't close non-existent session.");
|
2017-01-11 23:52:05 +03:00
|
|
|
mHost->OnRejectPromise(aPromiseId,
|
2017-11-23 11:47:13 +03:00
|
|
|
Exception::kExceptionTypeError,
|
2017-01-11 23:52:05 +03:00
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
0);
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearKeySession* session = itr->second;
|
2015-04-02 23:08:27 +03:00
|
|
|
assert(session);
|
2015-01-12 10:15:55 +03:00
|
|
|
|
|
|
|
ClearInMemorySessionData(session);
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
mHost->OnSessionClosed(aSessionId, aSessionIdLength);
|
|
|
|
mHost->OnResolvePromise(aPromiseId);
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClearKeySessionManager::ClearInMemorySessionData(ClearKeySession* aSession)
|
|
|
|
{
|
|
|
|
mSessions.erase(aSession->Id());
|
|
|
|
delete aSession;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClearKeySessionManager::RemoveSession(uint32_t aPromiseId,
|
|
|
|
const char* aSessionId,
|
|
|
|
uint32_t aSessionIdLength)
|
|
|
|
{
|
2017-01-18 04:49:58 +03:00
|
|
|
CK_LOGD("ClearKeySessionManager::RemoveSession");
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
// Copy the sessionId into a string so it can be captured for the lambda.
|
2017-01-14 00:23:31 +03:00
|
|
|
string sessionId(aSessionId, aSessionId + aSessionIdLength);
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
// Hold a reference to the SessionManager, so it isn't released before we
|
|
|
|
// try and use it.
|
|
|
|
RefPtr<ClearKeySessionManager> self(this);
|
|
|
|
function<void()> deferrer =
|
|
|
|
[self, aPromiseId, sessionId] ()
|
|
|
|
{
|
|
|
|
self->RemoveSession(aPromiseId, sessionId.data(), sessionId.size());
|
|
|
|
};
|
|
|
|
|
|
|
|
// If we haven't fully loaded, defer calling this method.
|
2017-04-27 19:44:57 +03:00
|
|
|
if (MaybeDeferTillInitialized(move(deferrer))) {
|
2017-01-18 04:49:58 +03:00
|
|
|
CK_LOGD("Deferring RemoveSession");
|
2017-01-11 23:52:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the SessionManager has not been shutdown before we try and
|
|
|
|
// resolve any promises.
|
|
|
|
if (!mHost) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
auto itr = mSessions.find(sessionId);
|
|
|
|
if (itr == mSessions.end()) {
|
|
|
|
CK_LOGW("ClearKey CDM couldn't remove non-existent session.");
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
mHost->OnRejectPromise(aPromiseId,
|
2017-11-23 11:47:13 +03:00
|
|
|
Exception::kExceptionTypeError,
|
2017-01-11 23:52:05 +03:00
|
|
|
0,
|
|
|
|
nullptr,
|
|
|
|
0);
|
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClearKeySession* session = itr->second;
|
2015-04-02 23:08:27 +03:00
|
|
|
assert(session);
|
2015-01-12 10:15:55 +03:00
|
|
|
string sid = session->Id();
|
2017-01-11 23:52:05 +03:00
|
|
|
bool isPersistent = session->Type() == SessionType::kPersistentLicense;
|
2015-01-12 10:15:55 +03:00
|
|
|
ClearInMemorySessionData(session);
|
|
|
|
|
|
|
|
if (!isPersistent) {
|
2017-01-11 23:52:05 +03:00
|
|
|
mHost->OnResolvePromise(aPromiseId);
|
2015-01-12 10:15:55 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
mPersistence->PersistentSessionRemoved(sid);
|
2016-10-19 12:25:00 +03:00
|
|
|
|
2015-01-12 10:15:55 +03:00
|
|
|
vector<uint8_t> emptyKeydata;
|
2017-01-11 23:52:05 +03:00
|
|
|
|
2017-03-06 21:52:09 +03:00
|
|
|
function<void()> resolve = [self, aPromiseId] ()
|
2017-01-11 23:52:05 +03:00
|
|
|
{
|
|
|
|
if (!self->mHost) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
self->mHost->OnResolvePromise(aPromiseId);
|
|
|
|
};
|
|
|
|
|
2017-03-06 21:52:09 +03:00
|
|
|
function<void()> reject = [self, aPromiseId] ()
|
2017-01-11 23:52:05 +03:00
|
|
|
{
|
|
|
|
if (!self->mHost) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
static const char* message = "Could not remove session";
|
|
|
|
self->mHost->OnRejectPromise(aPromiseId,
|
2017-11-23 11:47:13 +03:00
|
|
|
Exception::kExceptionTypeError,
|
2017-01-11 23:52:05 +03:00
|
|
|
0,
|
|
|
|
message,
|
|
|
|
strlen(message));
|
|
|
|
};
|
|
|
|
|
|
|
|
WriteData(mHost, sessionId, emptyKeydata, move(resolve), move(reject));
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClearKeySessionManager::SetServerCertificate(uint32_t aPromiseId,
|
|
|
|
const uint8_t* aServerCert,
|
|
|
|
uint32_t aServerCertSize)
|
|
|
|
{
|
|
|
|
// ClearKey CDM doesn't support this method by spec.
|
|
|
|
CK_LOGD("ClearKeySessionManager::SetServerCertificate");
|
2017-01-11 23:52:05 +03:00
|
|
|
mHost->OnRejectPromise(aPromiseId,
|
2017-11-23 11:47:13 +03:00
|
|
|
Exception::kExceptionNotSupportedError,
|
2017-01-11 23:52:05 +03:00
|
|
|
0,
|
|
|
|
nullptr /* message */,
|
|
|
|
0 /* messageLen */);
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
Status
|
2018-09-13 17:49:10 +03:00
|
|
|
ClearKeySessionManager::Decrypt(const InputBuffer_1& aBuffer,
|
2017-01-11 23:52:05 +03:00
|
|
|
DecryptedBlock* aDecryptedBlock)
|
2015-01-12 10:15:55 +03:00
|
|
|
{
|
|
|
|
CK_LOGD("ClearKeySessionManager::Decrypt");
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
CK_LOGARRAY("Key: ", aBuffer.key_id, aBuffer.key_id_size);
|
2017-01-11 23:52:05 +03:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
Buffer* buffer = mHost->Allocate(aBuffer.data_size);
|
|
|
|
assert(buffer != nullptr);
|
|
|
|
assert(buffer->Data() != nullptr);
|
|
|
|
assert(buffer->Capacity() >= aBuffer.data_size);
|
2015-01-12 10:15:55 +03:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
memcpy(buffer->Data(), aBuffer.data, aBuffer.data_size);
|
|
|
|
|
2017-10-19 11:45:21 +03:00
|
|
|
Status status = Status::kSuccess;
|
|
|
|
// According to the comment `If |iv_size| = 0, the data is unencrypted.`
|
|
|
|
// Use iv_size to determine if the sample is encrypted.
|
|
|
|
if (aBuffer.iv_size != 0) {
|
|
|
|
status = mDecryptionManager->Decrypt(buffer->Data(),
|
|
|
|
buffer->Size(),
|
|
|
|
CryptoMetaData(&aBuffer));
|
|
|
|
}
|
2017-01-11 23:52:05 +03:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
aDecryptedBlock->SetDecryptedBuffer(buffer);
|
|
|
|
aDecryptedBlock->SetTimestamp(aBuffer.timestamp);
|
|
|
|
|
|
|
|
return status;
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-11 23:52:05 +03:00
|
|
|
ClearKeySessionManager::DecryptingComplete()
|
2015-01-12 10:15:55 +03:00
|
|
|
{
|
2017-01-11 23:52:05 +03:00
|
|
|
CK_LOGD("ClearKeySessionManager::DecryptingComplete %p", this);
|
2015-01-12 10:15:55 +03:00
|
|
|
|
|
|
|
for (auto it = mSessions.begin(); it != mSessions.end(); it++) {
|
|
|
|
delete it->second;
|
|
|
|
}
|
|
|
|
mSessions.clear();
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
mDecryptionManager = nullptr;
|
|
|
|
mHost = nullptr;
|
|
|
|
|
|
|
|
Release();
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|
|
|
|
|
2017-04-27 19:44:57 +03:00
|
|
|
bool ClearKeySessionManager::MaybeDeferTillInitialized(function<void()>&& aMaybeDefer)
|
2015-01-12 10:15:55 +03:00
|
|
|
{
|
2017-01-11 23:52:05 +03:00
|
|
|
if (mPersistence->IsLoaded()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-01-14 00:23:31 +03:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
mDeferredInitialize.emplace(move(aMaybeDefer));
|
|
|
|
return true;
|
2015-01-12 10:15:55 +03:00
|
|
|
}
|