?? drmadpapi.c
字號:
/*
// $Header: /I76/I76_Common/I76_Reference/Playcore/Nav_Clips/AviDrm/libDrmDecrypt/DrmAdpApi.c 5 3/10/04 8:40p Lotan $
// Copyright (c) DivXNetworks, Inc. http://www.divxnetworks.com
// All rights reserved.
//
// This software is the confidential and proprietary information of DivXNetworks
// Inc. ("Confidential Information"). You shall not disclose such Confidential
// Information and shall use it only in accordance with the terms of the license
// agreement you entered into with DivXNetworks, Inc.
*/
#include "Config.h" // Global Configuration - do not remove!
#ifdef AVI_DRM_SUPPORT
#include "Services\Memory\malloc.h" // when using far malloc we should add this include
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\DrmAdpApi.h"
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\DrmAdpHelper.h"
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\Bits.h"
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\DrmAdpModelId.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\DrmTypeDefs.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\DrmTypes.h"
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\DrmMessage.h"
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\DrmLocal.h"
#include "Playcore\Nav_Clips\AviDrm\libDrmDecrypt\DrmMemory.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\DrmSizes.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\DrmErrors.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\base64.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\crypt_util.h"
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\DrmTypeDefs.h"
#include "Playcore\AuxCache\AuxCache.h"
#include "Services\Include\_heap.h"
// <<< Robin_0915_2004
#ifndef AVI_DRM_OPTIMIZATION
#ifdef USE_ONE_MASTER_KEY
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\master_key_one.h"
#else
#include "Playcore\Nav_Clips\AviDrm\LibDrmCommon\master_key_all.h"
#endif
#endif
// >>> Robin_0915_2004
#include <string.h>
//#include <memory.h>
#ifdef AVI_MALLOC_SIZE_DOWN
//#ifdef NEW_SC_KEYS
extern BYTE* far g_ScAviDrm_KeysOffset;
#endif
void drmSetRandomSample(void)
{
drmHelperSetRandomSample();
}
uint32_t drmGetRandomSampleCounter(void)
{
return drmHelperGetRandomSampleCounter();
}
uint8_t drmGetRegistrationCodeString(char *registrationCodeString)
{
uint8_t returnCode = DRM_ERROR_NONE;
DrmMemory memory;
DrmMessagePacked messagePacked;
RegistrationRequest request;
memset(&messagePacked,0,sizeof(DrmMessagePacked));
memset((void*)&messagePacked,0,sizeof(DrmMessagePacked));
memset((void*)&memory,0,sizeof(DrmMemory));
returnCode = loadDrmMemory(&memory);
if (returnCode == DRM_ERROR_NONE)
{
tr_printf(("drmGetRegistrationCodeString:: set data.\n"));
/* Set data. */
tr_printf(("Last memory owner guard: %02x%02x%02x\n",((BYTE*)memory.owner.guard)[0],((BYTE*)memory.owner.guard)[1],((BYTE*)memory.owner.guard)[2]));
if (0 == memcmp(memory.owner.guard, nullGuard, OWNER_GUARD_BYTES))
{
uint16_t uiIndex = 0,uiIndexMax;
srand(uiIndex);
drmSetRandomSample();
uiIndexMax = rand() % 10000;
tr_printf(("first loop %d\n",uiIndexMax ));
for (uiIndex=0; uiIndex< uiIndexMax ; uiIndex++)
;//random delay
drmSetRandomSample();
uiIndexMax = rand() % 10000;
for (uiIndex=0; uiIndex< uiIndexMax ; uiIndex++)
;//random delay
drmSetRandomSample();
returnCode = randomizeGuard(&memory);
tr_printf(("randomizeGuard %s\n",(returnCode)?"ERROR":"OK"));
if (returnCode == DRM_ERROR_NEED_MORE_RANDOM_SAMPLE)
{
tr_printf(("Error: DRM_ERROR_NEED_MORE_RANDOM_SAMPLE\n"));
return returnCode;
}
returnCode = saveDrmMemory(&memory);
tr_printf(("saveDrmMemory %s\n",(returnCode)?"ERROR":"OK"));
}
else{
tr_printf(("USING OLD MEMORY OWNER GUARD (OK)\n"));}
//drmGetModelId(request.modelId);
request.modelId[0] = MODEL_ID_LOW_BYTE;
request.modelId[1] = MODEL_ID_TOP_BYTE;
memcpy(request.userIdGuard, memory.owner.guard, OWNER_GUARD_BYTES);
/* Pack, wrap, and convert for human display. */
packRegistrationRequest(request, &messagePacked);
wrapSecureMessage(&messagePacked);
base32Encode40Bits(messagePacked.message, registrationCodeString);
}
else
{
tr_printf(("loadDrmMemory failed. status:%d\n",returnCode));
}
tr_printf(("drmGetRegistrationCodeString %s\n",(returnCode)?"ERROR":"OK"));
return returnCode;
}
uint8_t drmInitPlaybackContext( uint8_t *drmContext )
{
char portableMasterKey[DRM_PORTABLE_KEY_LENGTH];
int masterKeySize;
int i=0;
keyInstance masterKeyInstance;
int decryptedBlockSize = 0;
uint8_t *to;
// uint8_t *from;
cipherInstance cipher;
DrmHeader* drmHeader ;//= {0};
DrmPlaybackContext *context = (DrmPlaybackContext *) drmContext; //it is the same pointer
int result = 0;
char masterKey[33];
uint8_t hardwareSecret[MASTER_KEY_SIZE_BYTES];
// Put in to memory structure.
//memcpy(&drmHeader, drmHeaderInfo, sizeof(DrmHeader));
drmHeader = (DrmHeader*)drmContext;
// Get master key.
if(!get_master_key_from_id((unsigned char *)drmHeader->masterKeyId, (unsigned char *)portableMasterKey))
{
return 1;
}
base64Decode(portableMasterKey, DRM_PORTABLE_KEY_LENGTH, (unsigned int *)&masterKeySize, masterKey);
if(masterKeySize != MASTER_KEY_SIZE_BYTES)
{
return 1;
}
// Handle locally protected master keys.
result = localGetHardwareKey(hardwareSecret);
if (DRM_ERROR_NONE == result)
{
result = getLocalizedMasterKey((unsigned char *)masterKey);
if (DRM_ERROR_NONE != result)
{
return result;
}
}
cipherInit(&cipher, MODE_ECB, NULL);
crypt_util_make_key((unsigned char *)masterKey, DIR_DECRYPT, &masterKeyInstance, MASTER_KEY_SIZE_BYTES);
to = (uint8_t *) MEM_Allocate(SC_POOL, sizeof(DrmAdpTargetHeader));
//from = (uint8_t *) malloc(sizeof(DrmAdpTargetHeader));
//memcpy(from, &drmHeader->adpTarget, sizeof(DrmAdpTargetHeader));
crypt_util_decrypt(&cipher, &masterKeyInstance, (unsigned char *)&drmHeader->adpTarget, sizeof(DrmAdpTargetHeader), to, &decryptedBlockSize);
if(decryptedBlockSize != sizeof(DrmAdpTargetHeader))
{
MEM_Free(SC_POOL, to);
//free(from);
return 1;
}
// Put in xor.
for (i = sizeof(DrmAdpTargetHeader) - 1; i > 0; i--)
{
to[i] = to[i] ^ to[i-1];
}
memcpy(&drmHeader->adpTarget, to, sizeof(DrmAdpTargetHeader));
//free(from);
MEM_Free(SC_POOL, to);
if (!(DRM_TYPE_RENTAL == drmHeader->adpTarget.drmMode ||
DRM_TYPE_PURCHASE == drmHeader->adpTarget.drmMode ||
DRM_TYPE_ACTIVATION_PURCHASE == drmHeader->adpTarget.drmMode ||
DRM_TYPE_ACTIVATION_RENTAL == drmHeader->adpTarget.drmMode ||
DRM_TYPE_BASE == drmHeader->adpTarget.drmMode) )
{
tr_printf(("DRM_TYPE_ILLEGAL"));
return 1;
}
// no need for memcpy, since context and drmInfoHeader are the same!
// memcpy(&(context->clearHeader), &drmHeader, sizeof(DrmHeader));
context->state = DRM_STATE_INITIALIZED;
return 0;
}
uint8_t drmQueryRentalStatus(uint8_t *drmContext, uint8_t *useLimit, uint8_t *useCount)
{
uint8_t returnCode = DRM_ERROR_NONE;
DrmMemory memory;
uint8_t i;
DrmPlaybackContext *context = (DrmPlaybackContext *) drmContext;
uint16_t memorySerialNumber;
uint16_t fileSerialNumber;
*useLimit = 0;
*useCount = 0;
if (DRM_STATE_INITIALIZED != context->state)
{
return DRM_ERROR_NOT_INITIALIZED;
}
context->state = DRM_STATE_RENTAL_QUERIED;
if (!(DRM_TYPE_RENTAL == context->clearHeader.adpTarget.drmMode ||
DRM_TYPE_ACTIVATION_RENTAL == context->clearHeader.adpTarget.drmMode))
{
return DRM_ERROR_NOT_LIMITED_RENTAL_TYPE;
}
if (USE_LIMIT_ID_UNLIMITED == context->clearHeader.adpTarget.rentalRecord.useLimitId)
{
return DRM_ERROR_NOT_LIMITED_RENTAL_TYPE;
}
returnCode = loadDrmMemory(&memory);
if (returnCode == DRM_ERROR_NONE)
{
/* Check if correct user only if not activation record.. */
if ((0 != memcmp(context->clearHeader.adpTarget.userId, memory.owner.userId, OWNER_USER_ID_BYTES)) &&
(DRM_TYPE_RENTAL == context->clearHeader.adpTarget.drmMode ||
DRM_TYPE_PURCHASE == context->clearHeader.adpTarget.drmMode))
{
return DRM_ERROR_NOT_AUTH_USER;
}
/* Ensure slot number is in range. */
if (context->clearHeader.adpTarget.rentalRecord.slotNumber >= TOTAL_PLAY_SLOTS)
{
return DRM_ERROR_BAD_SLOT_NUMBER;
}
memorySerialNumber = pack(memory.slots[context->clearHeader.adpTarget.rentalRecord.slotNumber].serialNumber[0],
memory.slots[context->clearHeader.adpTarget.rentalRecord.slotNumber].serialNumber[1]);
fileSerialNumber = pack(context->clearHeader.adpTarget.rentalRecord.serialNumber[0],
context->clearHeader.adpTarget.rentalRecord.serialNumber[1]);
/* Check if correct serial number. Only check if it is not an activation. */
if (!(DRM_TYPE_ACTIVATION_RENTAL == context->clearHeader.adpTarget.drmMode &&
0 == memcmp(context->clearHeader.adpTarget.activateRecord.memoryGuard, memory.owner.guard, OWNER_GUARD_BYTES)) )
{
if (fileSerialNumber < memorySerialNumber)
{
return DRM_ERROR_RENTAL_EXPIRED;
}
}
/* Find use limit id. */
for (i = 0; i < USE_LIMITS; i++)
{
if (useLimitIdInfo[i].id == context->clearHeader.adpTarget.rentalRecord.useLimitId)
{
*useLimit = useLimitIdInfo[i].uses;
break;
}
}
if (fileSerialNumber == memorySerialNumber)
{
*useCount = memory.slots[context->clearHeader.adpTarget.rentalRecord.slotNumber].counter;
}
else
{
*useCount = 0;
}
/* If a first time, uses will be zero. */
if (fileSerialNumber > memorySerialNumber)
{
*useCount = 0;
}
// when activation, *useCount reset 0, Robin_0623_2004_A
if (DRM_TYPE_ACTIVATION_PURCHASE == context->clearHeader.adpTarget.drmMode ||DRM_TYPE_ACTIVATION_RENTAL == context->clearHeader.adpTarget.drmMode)
if (0 == memcmp(context->clearHeader.adpTarget.activateRecord.memoryGuard, memory.owner.guard, OWNER_GUARD_BYTES))
*useCount = 0;
}
return returnCode;
}
#ifdef AVI_MALLOC_SIZE_DOWN
uint8_t drmCommitPlayback(uint8_t *drmContext, DWORD dwClipStartAddr, DWORD pdwOffset)
#else
uint8_t drmCommitPlayback(uint8_t *drmContext)
#endif
{
uint8_t sessionKey[KEY_SIZE_BYTES];
uint8_t encryptedFrameKey[KEY_SIZE_BYTES];
keyInstance sessionKeyInstance;
int i;
int decryptedFrameKeySize = 0;
cipherInstance cipher;
DrmPlaybackContext *context = (DrmPlaybackContext *) drmContext;
uint8_t result = 0;
#ifdef AVI_MALLOC_SIZE_DOWN
uint8_t FrameKey[KEY_SIZE_BYTES];
UINT16 scratchPadOffset;
#endif
if (DRM_STATE_RENTAL_QUERIED != context->state)
{
return DRM_ERROR_NOT_RENTAL_QUERIED;
}
// Try to get the session key.
result = getSessionKey(context->clearHeader.adpTarget, sessionKey);
if (DRM_ERROR_NONE != result)
{
return result;
}
// Populate the frame key table.
cipherInit(&cipher, MODE_ECB, NULL);
crypt_util_make_key((unsigned char *)sessionKey, DIR_DECRYPT, &sessionKeyInstance, KEY_SIZE_BYTES);
#ifdef AVI_MALLOC_SIZE_DOWN
scratchPadOffset = sizeof( DrmHeader );
pdwOffset += sizeof( DrmHeader );
for (i = 0; i < DRM_FRAME_KEY_COUNT; i++)
{
if (!AuxCache_GetBytes(dwClipStartAddr, pdwOffset, KEY_SIZE_BYTES, FrameKey))
{
return DRM_ERROR_DECRYPTION_FAILED;
}
memcpy(encryptedFrameKey, FrameKey, KEY_SIZE_BYTES);
crypt_util_decrypt(&cipher, &sessionKeyInstance, encryptedFrameKey, KEY_SIZE_BYTES, FrameKey, &decryptedFrameKeySize);
if(decryptedFrameKeySize != KEY_SIZE_BYTES)
{
return DRM_ERROR_DECRYPTION_FAILED;
}
//#ifdef NEW_SC_KEYS
memcpy(g_ScAviDrm_KeysOffset+scratchPadOffset,FrameKey,KEY_SIZE_BYTES);
scratchPadOffset += KEY_SIZE_BYTES;
pdwOffset += KEY_SIZE_BYTES;
}
#else
for (i = 0; i < DRM_FRAME_KEY_COUNT; i++)
{
memcpy(encryptedFrameKey, context->clearHeader.frameKeys[i], KEY_SIZE_BYTES);
crypt_util_decrypt(&cipher, &sessionKeyInstance, encryptedFrameKey, KEY_SIZE_BYTES, context->clearHeader.frameKeys[i], &decryptedFrameKeySize);
if(decryptedFrameKeySize != KEY_SIZE_BYTES)
{
return DRM_ERROR_DECRYPTION_FAILED;
}
}
#endif
context->state = DRM_STATE_COMMITTED;
return result;
}
#endif // AVI_DRM_SUPPORT
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -