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
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
#include "ClearKeyUtils.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
2014-09-24 02:04:49 +04:00
|
|
|
#include <ctype.h>
|
2017-01-11 23:52:05 +03:00
|
|
|
#include <memory.h>
|
2014-09-24 02:04:49 +04:00
|
|
|
#include <stdarg.h>
|
2020-03-26 23:53:59 +03:00
|
|
|
// This include is required in order for content_decryption_module to work // on
|
|
|
|
// Unix systems.
|
|
|
|
#include <stddef.h>
|
2014-09-24 02:04:49 +04:00
|
|
|
#include <stdint.h>
|
2017-01-11 23:52:05 +03:00
|
|
|
#include <stdio.h>
|
2020-03-26 23:53:59 +03:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cctype>
|
|
|
|
#include <memory>
|
|
|
|
#include <sstream>
|
2014-09-24 02:04:49 +04:00
|
|
|
#include <vector>
|
|
|
|
|
2015-04-02 23:08:27 +03:00
|
|
|
#include "ArrayUtils.h"
|
2016-06-05 20:24:51 +03:00
|
|
|
#include "BigEndian.h"
|
2017-01-11 23:52:05 +03:00
|
|
|
#include "ClearKeyBase64.h"
|
2019-09-05 22:19:06 +03:00
|
|
|
#include "pk11pub.h"
|
|
|
|
#include "prerror.h"
|
2017-01-11 23:52:05 +03:00
|
|
|
#include "psshparser/PsshParser.h"
|
2019-09-05 22:19:06 +03:00
|
|
|
#include "secmodt.h"
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
using namespace cdm;
|
2019-09-03 22:10:42 +03:00
|
|
|
using std::string;
|
|
|
|
using std::stringstream;
|
|
|
|
using std::vector;
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2020-03-26 23:53:59 +03:00
|
|
|
struct DeleteHelper {
|
|
|
|
void operator()(PK11Context* value) { PK11_DestroyContext(value, true); }
|
|
|
|
void operator()(PK11SlotInfo* value) { PK11_FreeSlot(value); }
|
|
|
|
void operator()(PK11SymKey* value) { PK11_FreeSymKey(value); }
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
struct MaybeDeleteHelper {
|
|
|
|
void operator()(T* ptr) {
|
|
|
|
if (ptr) {
|
|
|
|
DeleteHelper del;
|
|
|
|
del(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
void CK_Log(const char* aFmt, ...) {
|
2017-01-11 23:52:05 +03:00
|
|
|
FILE* out = stdout;
|
|
|
|
|
|
|
|
if (getenv("CLEARKEY_LOG_FILE")) {
|
|
|
|
out = fopen(getenv("CLEARKEY_LOG_FILE"), "a");
|
|
|
|
}
|
|
|
|
|
2014-09-24 02:04:49 +04:00
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, aFmt);
|
2017-01-11 23:52:05 +03:00
|
|
|
const size_t len = 1024;
|
|
|
|
char buf[len];
|
|
|
|
vsnprintf(buf, len, aFmt, ap);
|
2014-09-24 02:04:49 +04:00
|
|
|
va_end(ap);
|
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
fprintf(out, "%s\n", buf);
|
|
|
|
fflush(out);
|
|
|
|
|
|
|
|
if (out != stdout) {
|
|
|
|
fclose(out);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool PrintableAsString(const uint8_t* aBytes, uint32_t aLength) {
|
2019-09-03 22:10:42 +03:00
|
|
|
return std::all_of(aBytes, aBytes + aLength,
|
|
|
|
[](uint8_t c) { return isprint(c) == 1; });
|
2017-01-11 23:52:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CK_LogArray(const char* prepend, const uint8_t* aData,
|
|
|
|
const uint32_t aDataSize) {
|
|
|
|
// If the data is valid ascii, use that. Otherwise print the hex
|
|
|
|
string data = PrintableAsString(aData, aDataSize)
|
|
|
|
? string(aData, aData + aDataSize)
|
|
|
|
: ClearKeyUtils::ToHexString(aData, aDataSize);
|
|
|
|
|
|
|
|
CK_LOGD("%s%s", prepend, data.c_str());
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
2020-03-26 23:53:59 +03:00
|
|
|
/* static */
|
|
|
|
bool ClearKeyUtils::DecryptCbcs(const vector<uint8_t>& aKey,
|
|
|
|
const vector<uint8_t>& aIV,
|
|
|
|
mozilla::Span<uint8_t> aSubsample,
|
|
|
|
uint32_t aCryptByteBlock,
|
|
|
|
uint32_t aSkipByteBlock) {
|
|
|
|
assert(aKey.size() == CENC_KEY_LEN);
|
|
|
|
assert(aIV.size() == CENC_KEY_LEN);
|
|
|
|
assert(aCryptByteBlock <= 0xFF);
|
|
|
|
assert(aSkipByteBlock <= 0xFF);
|
|
|
|
|
|
|
|
std::unique_ptr<PK11SlotInfo, MaybeDeleteHelper<PK11SlotInfo>> slot(
|
|
|
|
PK11_GetInternalKeySlot());
|
|
|
|
|
|
|
|
if (!slot.get()) {
|
|
|
|
CK_LOGE("Failed to get internal PK11 slot");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECItem keyItem = {siBuffer, (unsigned char*)&aKey[0], CENC_KEY_LEN};
|
|
|
|
SECItem ivItem = {siBuffer, (unsigned char*)&aIV[0], CENC_KEY_LEN};
|
|
|
|
|
|
|
|
std::unique_ptr<PK11SymKey, MaybeDeleteHelper<PK11SymKey>> key(
|
|
|
|
PK11_ImportSymKey(slot.get(), CKM_AES_CBC, PK11_OriginUnwrap, CKA_DECRYPT,
|
|
|
|
&keyItem, nullptr));
|
|
|
|
|
|
|
|
if (!key.get()) {
|
|
|
|
CK_LOGE("Failed to import sym key");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<PK11Context, MaybeDeleteHelper<PK11Context>> ctx(
|
|
|
|
PK11_CreateContextBySymKey(CKM_AES_CBC, CKA_DECRYPT, key.get(), &ivItem));
|
|
|
|
|
|
|
|
uint8_t* encryptedSubsample = &aSubsample[0];
|
|
|
|
const uint32_t BLOCK_SIZE = 16;
|
|
|
|
const uint32_t skipBytes = aSkipByteBlock * BLOCK_SIZE;
|
|
|
|
const uint32_t totalBlocks = aSubsample.Length() / BLOCK_SIZE;
|
|
|
|
uint32_t blocksProcessed = 0;
|
|
|
|
|
|
|
|
while (blocksProcessed < totalBlocks) {
|
|
|
|
uint32_t blocksToDecrypt = aCryptByteBlock <= totalBlocks - blocksProcessed
|
|
|
|
? aCryptByteBlock
|
|
|
|
: totalBlocks - blocksProcessed;
|
|
|
|
uint32_t bytesToDecrypt = blocksToDecrypt * BLOCK_SIZE;
|
|
|
|
int outLen;
|
|
|
|
SECStatus rv;
|
|
|
|
rv = PK11_CipherOp(ctx.get(), encryptedSubsample, &outLen, bytesToDecrypt,
|
|
|
|
encryptedSubsample, bytesToDecrypt);
|
|
|
|
if (rv != SECSuccess) {
|
|
|
|
CK_LOGE("PK11_CipherOp() failed");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
encryptedSubsample += skipBytes + bytesToDecrypt;
|
|
|
|
blocksProcessed += aSkipByteBlock + blocksToDecrypt;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:55 +03:00
|
|
|
/* static */
|
2019-09-05 22:19:06 +03:00
|
|
|
bool ClearKeyUtils::DecryptAES(const vector<uint8_t>& aKey,
|
2019-02-26 01:09:55 +03:00
|
|
|
vector<uint8_t>& aData, vector<uint8_t>& aIV) {
|
2016-10-07 01:14:04 +03:00
|
|
|
assert(aIV.size() == CENC_KEY_LEN);
|
|
|
|
assert(aKey.size() == CENC_KEY_LEN);
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2019-09-05 22:19:06 +03:00
|
|
|
PK11SlotInfo* slot = PK11_GetInternalKeySlot();
|
|
|
|
if (!slot) {
|
|
|
|
CK_LOGE("Failed to get internal PK11 slot");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SECItem keyItem = {siBuffer, (unsigned char*)&aKey[0], CENC_KEY_LEN};
|
|
|
|
PK11SymKey* key = PK11_ImportSymKey(slot, CKM_AES_CTR, PK11_OriginUnwrap,
|
|
|
|
CKA_ENCRYPT, &keyItem, nullptr);
|
|
|
|
PK11_FreeSlot(slot);
|
|
|
|
if (!key) {
|
|
|
|
CK_LOGE("Failed to import sym key");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CK_AES_CTR_PARAMS params;
|
|
|
|
params.ulCounterBits = 32;
|
|
|
|
memcpy(¶ms.cb, &aIV[0], CENC_KEY_LEN);
|
|
|
|
SECItem paramItem = {siBuffer, (unsigned char*)¶ms,
|
|
|
|
sizeof(CK_AES_CTR_PARAMS)};
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2019-09-05 22:19:06 +03:00
|
|
|
unsigned int outLen = 0;
|
|
|
|
auto rv = PK11_Decrypt(key, CKM_AES_CTR, ¶mItem, &aData[0], &outLen,
|
|
|
|
aData.size(), &aData[0], aData.size());
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2019-09-05 22:19:06 +03:00
|
|
|
aData.resize(outLen);
|
|
|
|
PK11_FreeSymKey(key);
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2019-09-05 22:19:06 +03:00
|
|
|
if (rv != SECSuccess) {
|
|
|
|
CK_LOGE("PK11_Decrypt() failed");
|
|
|
|
return false;
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
2019-09-05 22:19:06 +03:00
|
|
|
return true;
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ClearKey expects all Key IDs to be base64 encoded with non-standard alphabet
|
|
|
|
* and padding.
|
|
|
|
*/
|
|
|
|
static bool EncodeBase64Web(vector<uint8_t> aBinary, string& aEncoded) {
|
|
|
|
const char sAlphabet[] =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
|
|
const uint8_t sMask = 0x3f;
|
|
|
|
|
|
|
|
aEncoded.resize((aBinary.size() * 8 + 5) / 6);
|
|
|
|
|
|
|
|
// Pad binary data in case there's rubbish past the last byte.
|
|
|
|
aBinary.push_back(0);
|
|
|
|
|
|
|
|
// Number of bytes not consumed in the previous character
|
|
|
|
uint32_t shift = 0;
|
|
|
|
|
|
|
|
auto out = aEncoded.begin();
|
|
|
|
auto data = aBinary.begin();
|
2014-10-28 04:12:51 +03:00
|
|
|
for (string::size_type i = 0; i < aEncoded.length(); i++) {
|
2014-09-24 02:04:49 +04:00
|
|
|
if (shift) {
|
|
|
|
out[i] = (*data << (6 - shift)) & sMask;
|
|
|
|
data++;
|
|
|
|
} else {
|
|
|
|
out[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
out[i] += (*data >> (shift + 2)) & sMask;
|
|
|
|
shift = (shift + 2) % 8;
|
|
|
|
|
2014-10-28 09:17:54 +03:00
|
|
|
// Cast idx to size_t before using it as an array-index,
|
|
|
|
// to pacify clang 'Wchar-subscripts' warning:
|
|
|
|
size_t idx = static_cast<size_t>(out[i]);
|
2017-01-11 23:52:05 +03:00
|
|
|
|
|
|
|
// out of bounds index for 'sAlphabet'
|
|
|
|
assert(idx < MOZ_ARRAY_LENGTH(sAlphabet));
|
2014-10-28 09:17:54 +03:00
|
|
|
out[i] = sAlphabet[idx];
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:55 +03:00
|
|
|
/* static */
|
|
|
|
void ClearKeyUtils::MakeKeyRequest(const vector<KeyId>& aKeyIDs,
|
|
|
|
string& aOutRequest,
|
|
|
|
SessionType aSessionType) {
|
2015-04-02 23:08:27 +03:00
|
|
|
assert(aKeyIDs.size() && aOutRequest.empty());
|
2014-09-24 02:04:49 +04:00
|
|
|
|
2015-11-27 07:13:40 +03:00
|
|
|
aOutRequest.append("{\"kids\":[");
|
2014-09-24 02:04:49 +04:00
|
|
|
for (size_t i = 0; i < aKeyIDs.size(); i++) {
|
|
|
|
if (i) {
|
|
|
|
aOutRequest.append(",");
|
|
|
|
}
|
|
|
|
aOutRequest.append("\"");
|
|
|
|
|
|
|
|
string base64key;
|
|
|
|
EncodeBase64Web(aKeyIDs[i], base64key);
|
|
|
|
aOutRequest.append(base64key);
|
|
|
|
|
|
|
|
aOutRequest.append("\"");
|
|
|
|
}
|
2015-11-27 07:13:40 +03:00
|
|
|
aOutRequest.append("],\"type\":");
|
2014-12-18 23:54:34 +03:00
|
|
|
|
|
|
|
aOutRequest.append("\"");
|
|
|
|
aOutRequest.append(SessionTypeToString(aSessionType));
|
|
|
|
aOutRequest.append("\"}");
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#define EXPECT_SYMBOL(CTX, X) \
|
|
|
|
do { \
|
|
|
|
if (GetNextSymbol(CTX) != (X)) { \
|
|
|
|
CK_LOGE("Unexpected symbol in JWK parser"); \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
} while (false)
|
|
|
|
|
|
|
|
struct ParserContext {
|
|
|
|
const uint8_t* mIter;
|
|
|
|
const uint8_t* mEnd;
|
|
|
|
};
|
|
|
|
|
|
|
|
static uint8_t PeekSymbol(ParserContext& aCtx) {
|
|
|
|
for (; aCtx.mIter < aCtx.mEnd; (aCtx.mIter)++) {
|
|
|
|
if (!isspace(*aCtx.mIter)) {
|
|
|
|
return *aCtx.mIter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t GetNextSymbol(ParserContext& aCtx) {
|
|
|
|
uint8_t sym = PeekSymbol(aCtx);
|
|
|
|
aCtx.mIter++;
|
|
|
|
return sym;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool SkipToken(ParserContext& aCtx);
|
|
|
|
|
|
|
|
static bool SkipString(ParserContext& aCtx) {
|
|
|
|
EXPECT_SYMBOL(aCtx, '"');
|
|
|
|
for (uint8_t sym = GetNextSymbol(aCtx); sym; sym = GetNextSymbol(aCtx)) {
|
|
|
|
if (sym == '\\') {
|
|
|
|
sym = GetNextSymbol(aCtx);
|
2017-08-10 10:34:34 +03:00
|
|
|
if (!sym) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-24 02:04:49 +04:00
|
|
|
} else if (sym == '"') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skip whole object and values it contains.
|
|
|
|
*/
|
|
|
|
static bool SkipObject(ParserContext& aCtx) {
|
|
|
|
EXPECT_SYMBOL(aCtx, '{');
|
|
|
|
|
|
|
|
if (PeekSymbol(aCtx) == '}') {
|
|
|
|
GetNextSymbol(aCtx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (!SkipString(aCtx)) return false;
|
|
|
|
EXPECT_SYMBOL(aCtx, ':');
|
|
|
|
if (!SkipToken(aCtx)) return false;
|
|
|
|
|
|
|
|
if (PeekSymbol(aCtx) == '}') {
|
|
|
|
GetNextSymbol(aCtx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
EXPECT_SYMBOL(aCtx, ',');
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skip array value and the values it contains.
|
|
|
|
*/
|
|
|
|
static bool SkipArray(ParserContext& aCtx) {
|
|
|
|
EXPECT_SYMBOL(aCtx, '[');
|
|
|
|
|
|
|
|
if (PeekSymbol(aCtx) == ']') {
|
|
|
|
GetNextSymbol(aCtx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (SkipToken(aCtx)) {
|
|
|
|
if (PeekSymbol(aCtx) == ']') {
|
|
|
|
GetNextSymbol(aCtx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
EXPECT_SYMBOL(aCtx, ',');
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skip unquoted literals like numbers, |true|, and |null|.
|
|
|
|
* (XXX and anything else that matches /([:alnum:]|[+-.])+/)
|
|
|
|
*/
|
|
|
|
static bool SkipLiteral(ParserContext& aCtx) {
|
|
|
|
for (; aCtx.mIter < aCtx.mEnd; aCtx.mIter++) {
|
|
|
|
if (!isalnum(*aCtx.mIter) && *aCtx.mIter != '.' && *aCtx.mIter != '-' &&
|
|
|
|
*aCtx.mIter != '+') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool SkipToken(ParserContext& aCtx) {
|
|
|
|
uint8_t startSym = PeekSymbol(aCtx);
|
|
|
|
if (startSym == '"') {
|
|
|
|
CK_LOGD("JWK parser skipping string");
|
|
|
|
return SkipString(aCtx);
|
|
|
|
} else if (startSym == '{') {
|
|
|
|
CK_LOGD("JWK parser skipping object");
|
|
|
|
return SkipObject(aCtx);
|
|
|
|
} else if (startSym == '[') {
|
|
|
|
CK_LOGD("JWK parser skipping array");
|
|
|
|
return SkipArray(aCtx);
|
|
|
|
} else {
|
|
|
|
CK_LOGD("JWK parser skipping literal");
|
|
|
|
return SkipLiteral(aCtx);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool GetNextLabel(ParserContext& aCtx, string& aOutLabel) {
|
|
|
|
EXPECT_SYMBOL(aCtx, '"');
|
|
|
|
|
|
|
|
const uint8_t* start = aCtx.mIter;
|
|
|
|
for (uint8_t sym = GetNextSymbol(aCtx); sym; sym = GetNextSymbol(aCtx)) {
|
|
|
|
if (sym == '\\') {
|
|
|
|
GetNextSymbol(aCtx);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sym == '"') {
|
|
|
|
aOutLabel.assign(start, aCtx.mIter - 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-12-15 04:24:30 +03:00
|
|
|
static bool ParseKeyObject(ParserContext& aCtx, KeyIdPair& aOutKey) {
|
2014-09-24 02:04:49 +04:00
|
|
|
EXPECT_SYMBOL(aCtx, '{');
|
|
|
|
|
2014-12-15 04:24:30 +03:00
|
|
|
// Reject empty objects as invalid licenses.
|
2014-09-24 02:04:49 +04:00
|
|
|
if (PeekSymbol(aCtx) == '}') {
|
|
|
|
GetNextSymbol(aCtx);
|
2014-12-15 04:24:30 +03:00
|
|
|
return false;
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
string keyId;
|
|
|
|
string key;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
string label;
|
|
|
|
string value;
|
|
|
|
|
|
|
|
if (!GetNextLabel(aCtx, label)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_SYMBOL(aCtx, ':');
|
|
|
|
if (label == "kty") {
|
|
|
|
if (!GetNextLabel(aCtx, value)) return false;
|
2014-12-15 04:24:30 +03:00
|
|
|
// By spec, type must be "oct".
|
|
|
|
if (value != "oct") return false;
|
2014-09-24 02:04:49 +04:00
|
|
|
} else if (label == "k" && PeekSymbol(aCtx) == '"') {
|
|
|
|
// if this isn't a string we will fall through to the SkipToken() path.
|
|
|
|
if (!GetNextLabel(aCtx, key)) return false;
|
|
|
|
} else if (label == "kid" && PeekSymbol(aCtx) == '"') {
|
|
|
|
if (!GetNextLabel(aCtx, keyId)) return false;
|
|
|
|
} else {
|
|
|
|
if (!SkipToken(aCtx)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t sym = PeekSymbol(aCtx);
|
|
|
|
if (!sym || sym == '}') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
EXPECT_SYMBOL(aCtx, ',');
|
|
|
|
}
|
|
|
|
|
2014-12-15 04:24:30 +03:00
|
|
|
return !key.empty() && !keyId.empty() &&
|
2016-08-01 07:28:10 +03:00
|
|
|
DecodeBase64(keyId, aOutKey.mKeyId) &&
|
|
|
|
DecodeBase64(key, aOutKey.mKey) && GetNextSymbol(aCtx) == '}';
|
2014-09-24 02:04:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool ParseKeys(ParserContext& aCtx, vector<KeyIdPair>& aOutKeys) {
|
|
|
|
// Consume start of array.
|
|
|
|
EXPECT_SYMBOL(aCtx, '[');
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
KeyIdPair key;
|
2014-12-15 04:24:30 +03:00
|
|
|
if (!ParseKeyObject(aCtx, key)) {
|
2014-09-24 02:04:49 +04:00
|
|
|
CK_LOGE("Failed to parse key object");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-02 23:08:27 +03:00
|
|
|
assert(!key.mKey.empty() && !key.mKeyId.empty());
|
2014-12-15 04:24:30 +03:00
|
|
|
aOutKeys.push_back(key);
|
2014-09-24 02:04:49 +04:00
|
|
|
|
|
|
|
uint8_t sym = PeekSymbol(aCtx);
|
|
|
|
if (!sym || sym == ']') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_SYMBOL(aCtx, ',');
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetNextSymbol(aCtx) == ']';
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:55 +03:00
|
|
|
/* static */
|
|
|
|
bool ClearKeyUtils::ParseJWK(const uint8_t* aKeyData, uint32_t aKeyDataSize,
|
|
|
|
vector<KeyIdPair>& aOutKeys,
|
|
|
|
SessionType aSessionType) {
|
2014-09-24 02:04:49 +04:00
|
|
|
ParserContext ctx;
|
|
|
|
ctx.mIter = aKeyData;
|
|
|
|
ctx.mEnd = aKeyData + aKeyDataSize;
|
|
|
|
|
|
|
|
// Consume '{' from start of object.
|
|
|
|
EXPECT_SYMBOL(ctx, '{');
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
string label;
|
|
|
|
// Consume member key.
|
|
|
|
if (!GetNextLabel(ctx, label)) return false;
|
|
|
|
EXPECT_SYMBOL(ctx, ':');
|
|
|
|
|
|
|
|
if (label == "keys") {
|
|
|
|
// Parse "keys" array.
|
|
|
|
if (!ParseKeys(ctx, aOutKeys)) return false;
|
|
|
|
} else if (label == "type") {
|
|
|
|
// Consume type string.
|
|
|
|
string type;
|
|
|
|
if (!GetNextLabel(ctx, type)) return false;
|
2014-12-18 23:54:34 +03:00
|
|
|
if (type != SessionTypeToString(aSessionType)) {
|
2014-09-24 02:04:49 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SkipToken(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for end of object.
|
|
|
|
if (PeekSymbol(ctx) == '}') {
|
|
|
|
break;
|
|
|
|
}
|
2015-11-27 07:13:40 +03:00
|
|
|
|
|
|
|
// Consume ',' between object members.
|
|
|
|
EXPECT_SYMBOL(ctx, ',');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume '}' from end of object.
|
|
|
|
EXPECT_SYMBOL(ctx, '}');
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ParseKeyIds(ParserContext& aCtx, vector<KeyId>& aOutKeyIds) {
|
|
|
|
// Consume start of array.
|
|
|
|
EXPECT_SYMBOL(aCtx, '[');
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
string label;
|
|
|
|
vector<uint8_t> keyId;
|
2016-08-01 07:28:10 +03:00
|
|
|
if (!GetNextLabel(aCtx, label) || !DecodeBase64(label, keyId)) {
|
2015-11-27 07:13:40 +03:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-16 22:24:49 +03:00
|
|
|
if (!keyId.empty() && keyId.size() <= kMaxKeyIdsLength) {
|
2016-08-01 07:28:10 +03:00
|
|
|
aOutKeyIds.push_back(keyId);
|
|
|
|
}
|
2015-11-27 07:13:40 +03:00
|
|
|
|
|
|
|
uint8_t sym = PeekSymbol(aCtx);
|
|
|
|
if (!sym || sym == ']') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_SYMBOL(aCtx, ',');
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetNextSymbol(aCtx) == ']';
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:55 +03:00
|
|
|
/* static */
|
|
|
|
bool ClearKeyUtils::ParseKeyIdsInitData(const uint8_t* aInitData,
|
|
|
|
uint32_t aInitDataSize,
|
|
|
|
vector<KeyId>& aOutKeyIds) {
|
2015-11-27 07:13:40 +03:00
|
|
|
ParserContext ctx;
|
|
|
|
ctx.mIter = aInitData;
|
|
|
|
ctx.mEnd = aInitData + aInitDataSize;
|
|
|
|
|
|
|
|
// Consume '{' from start of object.
|
|
|
|
EXPECT_SYMBOL(ctx, '{');
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
string label;
|
|
|
|
// Consume member kids.
|
|
|
|
if (!GetNextLabel(ctx, label)) return false;
|
|
|
|
EXPECT_SYMBOL(ctx, ':');
|
|
|
|
|
|
|
|
if (label == "kids") {
|
|
|
|
// Parse "kids" array.
|
2016-08-01 07:28:10 +03:00
|
|
|
if (!ParseKeyIds(ctx, aOutKeyIds) || aOutKeyIds.empty()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-27 07:13:40 +03:00
|
|
|
} else {
|
|
|
|
SkipToken(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for end of object.
|
|
|
|
if (PeekSymbol(ctx) == '}') {
|
|
|
|
break;
|
|
|
|
}
|
2014-09-24 02:04:49 +04:00
|
|
|
|
|
|
|
// Consume ',' between object members.
|
|
|
|
EXPECT_SYMBOL(ctx, ',');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Consume '}' from end of object.
|
|
|
|
EXPECT_SYMBOL(ctx, '}');
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2014-12-18 23:54:34 +03:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
/* static */ const char* ClearKeyUtils::SessionTypeToString(
|
|
|
|
SessionType aSessionType) {
|
2014-12-18 23:54:34 +03:00
|
|
|
switch (aSessionType) {
|
2017-01-11 23:52:05 +03:00
|
|
|
case SessionType::kTemporary:
|
|
|
|
return "temporary";
|
|
|
|
case SessionType::kPersistentLicense:
|
|
|
|
return "persistent-license";
|
|
|
|
default: {
|
|
|
|
// We don't support any other license types.
|
|
|
|
assert(false);
|
|
|
|
return "invalid";
|
|
|
|
}
|
2014-12-18 23:54:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 01:09:55 +03:00
|
|
|
/* static */
|
|
|
|
bool ClearKeyUtils::IsValidSessionId(const char* aBuff, uint32_t aLength) {
|
2014-12-18 23:54:34 +03:00
|
|
|
if (aLength > 10) {
|
|
|
|
// 10 is the max number of characters in UINT32_MAX when
|
|
|
|
// represented as a string; ClearKey session ids are integers.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < aLength; i++) {
|
|
|
|
if (!isdigit(aBuff[i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-05-06 02:40:42 +03:00
|
|
|
|
2017-01-11 23:52:05 +03:00
|
|
|
string ClearKeyUtils::ToHexString(const uint8_t* aBytes, uint32_t aLength) {
|
|
|
|
stringstream ss;
|
|
|
|
ss << std::showbase << std::uppercase << std::hex;
|
|
|
|
for (uint32_t i = 0; i < aLength; ++i) {
|
|
|
|
ss << std::hex << static_cast<uint32_t>(aBytes[i]);
|
|
|
|
ss << " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return ss.str();
|
2015-05-06 02:40:42 +03:00
|
|
|
}
|