27 #include "AampUtils.h"
36 #include <openssl/err.h>
38 #include <gst/base/gstbytereader.h>
40 #define AES_CTR_KID_LEN 16
41 #define AES_CTR_IV_LEN 16
42 #define AES_CTR_KEY_LEN 16
44 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
45 #define OPEN_SSL_CONTEXT mOpensslCtx
47 #define OPEN_SSL_CONTEXT &mOpensslCtx
65 pthread_mutex_init(&decryptMutex,NULL);
75 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
76 OPEN_SSL_CONTEXT = EVP_CIPHER_CTX_new();
78 EVP_CIPHER_CTX_init(OPEN_SSL_CONTEXT);
80 AAMPLOG_ERR(
"ClearKeySession: enter ");
92 m_keyId = (
unsigned char*) malloc(
sizeof(
unsigned char) * keyIDLen);
93 memcpy(m_keyId, keyId, keyIDLen);
94 m_keyIdLen = keyIDLen;
110 unsigned char* key_id = NULL;
118 uint32_t header = 32;
120 key_id = (
unsigned char*)malloc(16 + 1);
121 memset(key_id, 0, 16 + 1);
122 strncpy(
reinterpret_cast<char*
>(key_id), psshData + header, 16);
124 AAMPLOG_INFO(
"ck keyid: %s keyIdlen: %d", key_id, 16);
138 uint32_t f_cbInitData, std::string &customData)
140 unsigned char *keyId = NULL;
141 if(f_pbInitData != NULL && f_cbInitData > 0)
144 keyId =
extractKeyIdFromPssh(
reinterpret_cast<const char*
>(f_pbInitData),f_cbInitData, &keyIDLen);
147 AAMPLOG_ERR(
"ClearKeySession: ERROR: Key Id not found in initdata");
152 if(keyIDLen != AES_CTR_KID_LEN)
154 AAMPLOG_ERR(
"ClearKeySession: ERROR: Invalid keyID length %d", keyIDLen);
159 setKeyId(
reinterpret_cast<const char*
>(keyId), keyIDLen);
160 AAMPLOG_ERR(
"ClearKeySession: Session generated for clearkey");
167 AAMPLOG_ERR(
"Invalid init data");
179 pthread_mutex_destroy(&decryptMutex);
180 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
181 if( OPEN_SSL_CONTEXT )
183 EVP_CIPHER_CTX_free(OPEN_SSL_CONTEXT);
184 OPEN_SSL_CONTEXT = NULL;
187 EVP_CIPHER_CTX_cleanup(OPEN_SSL_CONTEXT);
208 DrmData *licenseChallenge = NULL;
209 if(NULL == m_keyId || m_keyIdLen <= 0)
211 AAMPLOG_ERR(
"ClearKeySession: Error generating license request ");
218 cJSON *licenseRequest = cJSON_CreateObject();
221 cJSON *keyIds = cJSON_CreateArray();
224 cJSON_AddItemToArray(keyIds, cJSON_CreateString(urlEncodedkeyId));
225 cJSON_AddItemToObject(licenseRequest,
"kids", keyIds);
226 cJSON_AddItemToObject(licenseRequest,
"type",cJSON_CreateString(
"temporary"));
227 char* requestBody = cJSON_PrintUnformatted(licenseRequest);
230 AAMPLOG_INFO(
"Generated license request : %s", requestBody);
231 licenseChallenge =
new DrmData(
reinterpret_cast<unsigned char*
>(requestBody), strlen(requestBody)+1);
233 cJSON_free(requestBody);
236 cJSON_Delete(licenseRequest);
238 free(urlEncodedkeyId);
241 return licenseChallenge;
253 AAMPLOG_ERR(
"ClearKeySession: Cannot Process Null Key ");
257 std::string keyDataStr = key->
getData();
258 AAMPLOG_INFO(
"ClearKeySession: Processing license response %s", keyDataStr.c_str());
261 cJSON *licenseResponse = cJSON_Parse(keyDataStr.c_str());
264 if (cJSON_HasObjectItem(licenseResponse,
"keys"))
266 cJSON * keyEntry = NULL;
267 cJSON *keyJsonObj = NULL;
268 cJSON *keyIdJsonObj = NULL;
270 cJSON *keysArray = cJSON_GetObjectItem(licenseResponse,
"keys");
273 keyEntry = keysArray->child;
276 keyJsonObj = cJSON_GetObjectItem(keyEntry,
"k");
277 keyIdJsonObj = cJSON_GetObjectItem(keyEntry,
"kid");
281 if (keyJsonObj && keyIdJsonObj)
283 char *keyJsonStr = cJSON_GetStringValue(keyJsonObj);
284 char *keyIdStr = cJSON_GetStringValue(keyIdJsonObj);
285 size_t resKeyIdLen = 0;
286 size_t resKeyLen = 0;
288 if (resKeyIdLen == m_keyIdLen && 0 == memcmp(m_keyId, resKeyId, m_keyIdLen))
290 if (m_keyStr != NULL)
295 if (resKeyLen == AES_CTR_KEY_LEN)
297 m_keyLen = resKeyLen;
299 AAMPLOG_INFO(
"ClearKeySession: Got key from license response keyLength %d", m_keyLen);
304 AAMPLOG_ERR(
"ClearKeySession: ERROR : Failed parse Key from response");
315 AAMPLOG_ERR(
"ClearKeySession: ERROR : Failed parse KeyID/invalid keyID, from response");
325 AAMPLOG_ERR(
"ClearKeySession: ERROR : Failed parse Key info, from response");
329 cJSON_Delete(licenseResponse);
333 AAMPLOG_ERR(
"ClearKeySession: ERROR : Failed parse response JSON");
339 AAMPLOG_ERR(
"ClearKeySession: ERROR : Invalid DRM state : DRMState %d", m_eKeyState);
348 GstBuffer* subSamplesBuffer, GstCaps* caps)
351 uint8_t *pbData = NULL;
353 uint16_t nBytesClear = 0;
354 uint32_t nBytesEncrypted = 0;
357 GstMapInfo subsampleMap = GST_MAP_INFO_INIT;
358 GstMapInfo bufferMap;
359 GstByteReader* reader = NULL;
361 bool ivMapped =
false;
362 bool subSampleMapped =
false;
363 bool bufferMapped =
false;
365 if(!(ivBuffer && buffer && (subSampleCount == 0 || subSamplesBuffer)))
368 "ClearKeySession: ERROR : Ivalid buffer ivBuffer(%p), buffer(%p), subSamplesBuffer(%p) ",
369 ivBuffer, buffer, subSamplesBuffer);
373 bufferMapped = gst_buffer_map(buffer, &bufferMap,
static_cast<GstMapFlags
>(GST_MAP_READWRITE));
376 AAMPLOG_ERR(
"ClearKeySession: ERROR : Failed to map buffer");
379 ivMapped = gst_buffer_map(ivBuffer, &ivMap, GST_MAP_READ);
382 AAMPLOG_ERR(
"ClearKeySession: ERROR : Failed to map ivBuffer");
385 if(subSampleCount > 0)
387 subSampleMapped = gst_buffer_map(subSamplesBuffer, &subsampleMap, GST_MAP_READ);
388 if (!subSampleMapped)
390 AAMPLOG_ERR(
"ClearKeySession: ERROR : Failed to map subSamplesBuffer");
395 if(bufferMapped && ivMapped && (subSampleCount ==0 || subSampleMapped))
397 reader = gst_byte_reader_new(subsampleMap.data, subsampleMap.size);
402 if (subSampleCount > 0)
404 pbData = (uint8_t *) malloc(
sizeof(uint8_t) * bufferMap.size);
405 uint8_t *pbCurrTarget = (uint8_t *) pbData;
406 uint32_t iCurrSource = 0;
408 for (
int i = 0; i < subSampleCount; i++)
410 if (!gst_byte_reader_get_uint16_be(reader, &nBytesClear)
411 || !gst_byte_reader_get_uint32_be(reader, &nBytesEncrypted))
413 AAMPLOG_ERR(
"ClearKeySession: ERROR : Failed to read from subsamples reader");
418 iCurrSource += nBytesClear;
421 memcpy(pbCurrTarget, (uint8_t*) bufferMap.data + iCurrSource, nBytesEncrypted);
424 pbCurrTarget += nBytesEncrypted;
427 iCurrSource += nBytesEncrypted;
428 cbData += nBytesEncrypted;
433 pbData = bufferMap.data;
434 cbData = bufferMap.size;
439 retVal =
decrypt(
static_cast<uint8_t *
>(ivMap.data),
static_cast<uint32_t
>(ivMap.size),
440 pbData, cbData, NULL);
445 if(subSampleCount > 0)
449 gst_byte_reader_set_pos(reader, 0);
450 uint8_t *pbCurrTarget = bufferMap.data;
451 uint32_t iCurrSource = 0;
453 for (
int i = 0; i < subSampleCount; i++)
455 if (!gst_byte_reader_get_uint16_be(reader, &nBytesClear)
456 || !gst_byte_reader_get_uint32_be(reader, &nBytesEncrypted))
458 AAMPLOG_ERR(
"ClearKeySession: ERROR : Failed to read from subsamples reader");
463 pbCurrTarget += nBytesClear;
464 memcpy(pbCurrTarget, pbData + iCurrSource, nBytesEncrypted);
467 pbCurrTarget += nBytesEncrypted;
470 iCurrSource += nBytesEncrypted;
477 AAMPLOG_ERR(
"ClearKeySession: ERROR : Failed to allocate subsample reader");
481 if(subSampleCount > 0 && pbData)
487 gst_buffer_unmap(buffer, &bufferMap);
491 gst_buffer_unmap(ivBuffer, &ivMap);
495 gst_buffer_unmap(subSamplesBuffer, &subsampleMap);
505 const uint8_t *payloadData, uint32_t payloadDataSize, uint8_t **ppOpaqueData=NULL)
508 pthread_mutex_lock(&decryptMutex);
512 uint8_t *decryptedDataBuf = (uint8_t *)malloc(payloadDataSize);
513 uint32_t decryptedDataLen = 0;
514 uint8_t *ivBuff = NULL;
517 memset(decryptedDataBuf, 0, payloadDataSize);
520 ivBuff = (uint8_t *)malloc(
sizeof(uint8_t) * AES_CTR_IV_LEN);
523 memset(ivBuff + 8, 0, 8);
524 memcpy(ivBuff, f_pbIV, 8);
527 else if(f_cbIV == AES_CTR_IV_LEN)
529 ivBuff = (uint8_t *)malloc(
sizeof(uint8_t) * AES_CTR_IV_LEN);
530 memcpy(ivBuff, f_pbIV, AES_CTR_IV_LEN);
534 AAMPLOG_TRACE(
"ClearKeySession: invalid IV size %u", f_cbIV);
538 if (decryptedDataBuf && ivBuff)
540 uint32_t decLen = payloadDataSize;
542 if(!EVP_DecryptInit_ex(OPEN_SSL_CONTEXT, EVP_aes_128_ctr(), NULL, m_keyStr, ivBuff))
544 AAMPLOG_TRACE(
"ClearKeySession: EVP_DecryptInit_ex failed");
548 if (!EVP_DecryptUpdate(OPEN_SSL_CONTEXT,(
unsigned char*) decryptedDataBuf, (
int*) &decLen, (
const unsigned char*) payloadData,
555 decryptedDataLen = decLen;
557 AAMPLOG_TRACE(
"ClearKeySession: EVP_DecryptUpdate success decryptedDataLen = %d payload Data length = %d", (
int) decryptedDataLen, (
int)payloadDataSize);
558 if (!EVP_DecryptFinal_ex(OPEN_SSL_CONTEXT, (
unsigned char*) (decryptedDataBuf + decryptedDataLen), (
int*) &decLen))
560 AAMPLOG_TRACE(
"ClearKeySession: EVP_DecryptFinal_ex failed mDrmState = %d", (
int) m_eKeyState);
564 decryptedDataLen += decLen;
571 memcpy((
void *)payloadData, decryptedDataBuf, payloadDataSize);
580 free(decryptedDataBuf);
581 decryptedDataBuf = NULL;
586 AAMPLOG_ERR(
"ClearKeySession: key not ready! mDrmState = %d", m_eKeyState);
588 pthread_mutex_unlock(&decryptMutex);
618 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
619 if( OPEN_SSL_CONTEXT )
621 EVP_CIPHER_CTX_free(OPEN_SSL_CONTEXT);
622 OPEN_SSL_CONTEXT = NULL;
625 EVP_CIPHER_CTX_cleanup(OPEN_SSL_CONTEXT);