35 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
36 #define OPEN_SSL_CONTEXT mOpensslCtx
38 #define OPEN_SSL_CONTEXT &mOpensslCtx
40 #define AES_128_KEY_LEN_BYTES 16
42 static pthread_mutex_t instanceLock = PTHREAD_MUTEX_INITIALIZER;
75 AAMPLOG_ERR(
"AesDec::NotifyDRMError: drmState:%d", mDrmState );
84 pthread_mutex_lock(&mMutex);
86 pthread_cond_broadcast(&mCond);
87 pthread_mutex_unlock(&mMutex);
96 AAMPLOG_WARN(
"aamp:AesDRMListener drmState:%d moving to KeyAcquired", mDrmState);
97 pthread_mutex_lock(&mMutex);
99 pthread_cond_broadcast(&mCond);
100 pthread_mutex_unlock(&mMutex);
109 std::string tempEffectiveUrl;
112 double downloadTime = 0.0;
113 bool keyAcquisitionStatus =
false;
116 if (aamp_pthread_setname(pthread_self(),
"aampAesDRM"))
118 AAMPLOG_ERR(
"pthread_setname_np failed");
121 AAMPLOG_WARN(
"Key acquisition start uri = %s", keyURI.c_str());
122 bool fetched = mpAamp->
GetFile(keyURI, &mAesKeyBuf, tempEffectiveUrl, &http_error, &downloadTime, NULL, mCurlInstance,
true,
eMEDIATYPE_LICENCE);
125 if (AES_128_KEY_LEN_BYTES == mAesKeyBuf.
len)
127 AAMPLOG_WARN(
"Key fetch success len = %d", (
int)mAesKeyBuf.
len);
128 keyAcquisitionStatus =
true;
132 AAMPLOG_ERR(
"Error Key fetch - size %d", (
int)mAesKeyBuf.
len);
138 AAMPLOG_ERR(
"Key fetch failed");
139 if (http_error == CURLE_OPERATION_TIMEDOUT)
149 if(keyAcquisitionStatus)
193 pthread_mutex_lock(&mMutex);
202 AAMPLOG_WARN(
"AesDec:: acquiring key in progress");
207 if (!mDrmUrl.empty())
211 AAMPLOG_TRACE(
"AesDec: same url:%s - not acquiring key", mDrmUrl.c_str());
212 pthread_mutex_unlock(&mMutex);
216 mDrmUrl = drmInfo->
keyURI;
219 if (-1 == mCurlInstance)
225 if (licenseAcquisitionThreadStarted)
227 int ret = pthread_join(licenseAcquisitionThreadId, NULL);
230 AAMPLOG_ERR(
"AesDec:: pthread_join failed for license acquisition thread: %d", licenseAcquisitionThreadId);
232 licenseAcquisitionThreadStarted =
false;
235 int ret = pthread_create(&licenseAcquisitionThreadId, NULL,
acquire_key,
this);
238 AAMPLOG_ERR(
"AesDec:: pthread_create failed for acquire_key with errno = %d, %s", errno, strerror(errno));
240 licenseAcquisitionThreadStarted =
false;
245 licenseAcquisitionThreadStarted =
true;
247 pthread_mutex_unlock(&mMutex);
248 AAMPLOG_INFO(
"AesDec: drmState:%d ", mDrmState);
258 AAMPLOG_INFO(
"aamp:waiting for key acquisition to complete,wait time:%d",timeInMs );
261 if(0 != pthread_cond_timedwait(&mCond, &mMutex, &ts))
263 AAMPLOG_WARN(
"AesDec:: wait for key acquisition timed out");
275 pthread_mutex_lock(&mMutex);
282 AAMPLOG_INFO(
"AesDec: Starting decrypt");
283 unsigned char *decryptedDataBuf = (
unsigned char *)malloc(encryptedDataLen);
284 int decryptedDataLen = 0;
285 if (decryptedDataBuf)
287 int decLen = encryptedDataLen;
288 memset(decryptedDataBuf, 0, encryptedDataLen);
290 if(!EVP_DecryptInit_ex(OPEN_SSL_CONTEXT, EVP_aes_128_cbc(), NULL, (
unsigned char*)mAesKeyBuf.
ptr, mDrmInfo.
iv))
292 AAMPLOG_ERR(
"AesDec::EVP_DecryptInit_ex failed mDrmState = %d",(
int)mDrmState);
296 if (!EVP_DecryptUpdate(OPEN_SSL_CONTEXT, decryptedDataBuf, &decLen, (
const unsigned char*) encryptedDataPtr, encryptedDataLen))
298 AAMPLOG_ERR(
"AesDec::EVP_DecryptUpdate failed mDrmState = %d",(
int) mDrmState);
302 decryptedDataLen = decLen;
304 AAMPLOG_INFO(
"AesDec: EVP_DecryptUpdate success decryptedDataLen = %d encryptedDataLen %d", (
int) decryptedDataLen, (
int)encryptedDataLen);
305 if (!EVP_DecryptFinal_ex(OPEN_SSL_CONTEXT, decryptedDataBuf + decryptedDataLen, &decLen))
307 AAMPLOG_ERR(
"AesDec::EVP_DecryptFinal_ex failed mDrmState = %d",
312 decryptedDataLen += decLen;
313 AAMPLOG_INFO(
"AesDec: decrypt success");
320 memcpy(encryptedDataPtr, decryptedDataBuf, encryptedDataLen);
321 free(decryptedDataBuf);
326 AAMPLOG_ERR(
"AesDec::key acquisition failure! mDrmState = %d",(
int)mDrmState);
328 pthread_mutex_unlock(&mMutex);
339 pthread_mutex_lock(&mMutex);
346 if (licenseAcquisitionThreadStarted)
348 int ret = pthread_join(licenseAcquisitionThreadId, NULL);
351 AAMPLOG_ERR(
"AesDec:: pthread_join failed for license acquisition thread: %d",licenseAcquisitionThreadId);
353 licenseAcquisitionThreadStarted =
false;
355 pthread_cond_broadcast(&mCond);
356 if (-1 != mCurlInstance)
366 pthread_mutex_unlock(&mMutex);
375 pthread_mutex_lock(&mMutex);
379 mPrevDrmState = mDrmState;
383 pthread_cond_broadcast(&mCond);
384 pthread_mutex_unlock(&mMutex);
393 pthread_mutex_lock(&mMutex);
397 mDrmState = mPrevDrmState;
399 pthread_mutex_unlock(&mMutex);
402 std::shared_ptr<AesDec> AesDec::mInstance =
nullptr;
409 pthread_mutex_lock(&instanceLock);
410 if (
nullptr == mInstance)
412 mInstance = std::make_shared<AesDec>();
414 pthread_mutex_unlock(&instanceLock);
424 mCond(), mMutex(), mOpensslCtx(),
425 mDrmInfo(), mAesKeyBuf(), mCurlInstance(-1),
426 licenseAcquisitionThreadId(0),
427 licenseAcquisitionThreadStarted(false),
430 pthread_cond_init(&mCond, NULL);
431 pthread_mutex_init(&mMutex, NULL);
432 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
433 OPEN_SSL_CONTEXT = EVP_CIPHER_CTX_new();
435 EVP_CIPHER_CTX_init(OPEN_SSL_CONTEXT);
447 pthread_mutex_destroy(&mMutex);
448 pthread_cond_destroy(&mCond);
449 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
450 EVP_CIPHER_CTX_free(OPEN_SSL_CONTEXT);
452 EVP_CIPHER_CTX_cleanup(OPEN_SSL_CONTEXT);