Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1667 lines
50 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1997.
  5. //
  6. // File: hcpack.c
  7. //
  8. // Contents: Functions that are used to pack and unpack different messages
  9. //
  10. // Classes:
  11. //
  12. // Functions: PackHydraClientNewLicenseRequest
  13. // PackHydraClientKeyExchangeInfo
  14. // PackHydraClientLicenseInfo
  15. // PackHydraClientPlatformInfo
  16. // PackHydraClientPlatformChallengeResponse
  17. // PackLicenseErrorMessage
  18. // UnPackLicenseErrorMessage
  19. // UnpackHydraServerLicenseRequest
  20. // UnPackHydraServerPlatformChallenge
  21. // UnPackHydraServerNewLicense
  22. // UnPackHydraServerUpgradeLicense
  23. // UnpackHydraServerCertificate
  24. //
  25. // History: 12-19-97 v-sbhatt Created
  26. //
  27. //----------------------------------------------------------------------------
  28. //
  29. //Includes
  30. //
  31. #include "windows.h"
  32. #ifndef OS_WINCE
  33. #include "stdio.h"
  34. #endif // OS_WINCE
  35. #include "stdlib.h"
  36. #include <tchar.h>
  37. #ifdef OS_WINCE
  38. #include <wincelic.h>
  39. #endif //OS_WINCE
  40. #include "license.h"
  41. #include "hcpack.h"
  42. #include "licdbg.h"
  43. #define INVALID_INPUT_RETURN lsReturn = LICENSE_STATUS_INVALID_INPUT; LS_LOG_RESULT(lsReturn); goto ErrorReturn
  44. #define EXTENDED_ERROR_CAPABILITY 0x80
  45. //Copies a binary blob into a byte buffer. Does not check for any abnormal
  46. //condition. After copying buffer points to the end of the blob
  47. static VOID CopyBinaryBlob(
  48. BYTE FAR * pbBuffer,
  49. PBinary_Blob pbbBlob,
  50. DWORD FAR * pdwCount
  51. )
  52. {
  53. *pdwCount = 0;
  54. //First copy the wBlobType data;
  55. memcpy(pbBuffer, &pbbBlob->wBlobType, sizeof(WORD));
  56. pbBuffer += sizeof(WORD);
  57. *pdwCount += sizeof(WORD);
  58. //Copy the wBlobLen data
  59. memcpy(pbBuffer, &pbbBlob->wBlobLen, sizeof(WORD));
  60. pbBuffer += sizeof(WORD);
  61. *pdwCount += sizeof(WORD);
  62. if( (pbbBlob->wBlobLen >0) && (pbbBlob->pBlob != NULL) )
  63. {
  64. //Copy the actual data
  65. memcpy(pbBuffer, pbbBlob->pBlob, pbbBlob->wBlobLen);
  66. pbBuffer += pbbBlob->wBlobLen;
  67. *pdwCount += pbbBlob->wBlobLen;
  68. }
  69. }
  70. //Function implementation
  71. /***************************************************************************************
  72. * Function : PackHydraClientNewLicenseRequest
  73. * Purpose : This function takes a pointer to a Hydra_Client_New_License_Request
  74. * structure and copies the data to the buffer pointed by pbBuffer.
  75. * pcbBuffer should point the size of the buffer pointed by pbBuffer.
  76. * After the function returns, pcbBuffer contains the no. of bytes copied
  77. * in the buffer. If pbBuffer is NULL, the fucntion returns the size of
  78. * the pbBuffer to be allocated.
  79. * Returns : LICENSE_STATUS
  80. ****************************************************************************************/
  81. LICENSE_STATUS
  82. PackHydraClientNewLicenseRequest(
  83. IN PHydra_Client_New_License_Request pCanonical,
  84. IN BOOL fExtendedError,
  85. OUT BYTE FAR * pbBuffer,
  86. IN OUT DWORD FAR * pcbBuffer
  87. )
  88. {
  89. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  90. BYTE FAR * pbTemp = NULL;
  91. DWORD dwCount = 0;
  92. Preamble Header;
  93. LS_BEGIN(TEXT("PackHydraClientNewLicenseRequest"));
  94. //Check if the inputs are valid or not!
  95. if(pCanonical == NULL)
  96. {
  97. INVALID_INPUT_RETURN;;
  98. }
  99. if( pbBuffer == NULL && pcbBuffer == NULL )
  100. {
  101. INVALID_INPUT_RETURN;;
  102. }
  103. //Initialize Message Header
  104. Header.bMsgType = HC_NEW_LICENSE_REQUEST;
  105. Header.bVersion = PREAMBLE_VERSION_3_0;
  106. if( fExtendedError == TRUE)
  107. {
  108. Header.bVersion |= EXTENDED_ERROR_CAPABILITY;
  109. }
  110. Header.wMsgSize = 0;
  111. //Calculate the size of the message and place the data in Header.wMsgSize.
  112. //Start with Preamble size
  113. Header.wMsgSize += sizeof(Preamble);
  114. //dwPrefKeyExchangeAlg
  115. Header.wMsgSize += sizeof(pCanonical->dwPrefKeyExchangeAlg);
  116. //dwPlatformID
  117. Header.wMsgSize += sizeof(pCanonical->dwPlatformID);
  118. //Client Random
  119. Header.wMsgSize += LICENSE_RANDOM;
  120. //EncryptedPreMasterSecret
  121. Header.wMsgSize += sizeof(pCanonical->EncryptedPreMasterSecret.wBlobType) +
  122. sizeof(pCanonical->EncryptedPreMasterSecret.wBlobLen) +
  123. pCanonical->EncryptedPreMasterSecret.wBlobLen;
  124. //
  125. // client user name and machine name
  126. //
  127. Header.wMsgSize += sizeof(pCanonical->ClientUserName.wBlobType) +
  128. sizeof(pCanonical->ClientUserName.wBlobLen) +
  129. pCanonical->ClientUserName.wBlobLen;
  130. Header.wMsgSize += sizeof(pCanonical->ClientMachineName.wBlobType) +
  131. sizeof(pCanonical->ClientMachineName.wBlobLen) +
  132. pCanonical->ClientMachineName.wBlobLen;
  133. if(pbBuffer == NULL)
  134. {
  135. *pcbBuffer = (DWORD)Header.wMsgSize;
  136. LS_RETURN(lsReturn);
  137. goto CommonReturn;
  138. }
  139. else if(*pcbBuffer < (DWORD)Header.wMsgSize)
  140. {
  141. lsReturn = LICENSE_STATUS_INSUFFICIENT_BUFFER;
  142. LS_RETURN(lsReturn);
  143. goto ErrorReturn;
  144. }
  145. pbTemp = pbBuffer;
  146. *pcbBuffer = 0;
  147. //Now start copying different members of the New License structure to the
  148. //buffer specified by the caller
  149. //first copy the Header into the buffer
  150. memcpy(pbTemp, &Header, sizeof(Preamble));
  151. pbTemp += sizeof(Preamble);
  152. *pcbBuffer += sizeof(Preamble);
  153. //Copy the dwPrefKeyExchangeAlg parameter
  154. memcpy(pbTemp, &pCanonical->dwPrefKeyExchangeAlg, sizeof(pCanonical->dwPrefKeyExchangeAlg));
  155. pbTemp += sizeof(pCanonical->dwPrefKeyExchangeAlg);
  156. *pcbBuffer += sizeof(pCanonical->dwPrefKeyExchangeAlg);
  157. //Copy PlatformID;
  158. memcpy(pbTemp, &pCanonical->dwPlatformID, sizeof(pCanonical->dwPlatformID));
  159. pbTemp += sizeof(pCanonical->dwPlatformID);
  160. *pcbBuffer += sizeof(pCanonical->dwPlatformID);
  161. //Copy ClientRandom
  162. memcpy(pbTemp, pCanonical->ClientRandom, LICENSE_RANDOM);
  163. pbTemp += LICENSE_RANDOM;
  164. *pcbBuffer += LICENSE_RANDOM;
  165. //Copy EncryptedPreMasterSecret Blob
  166. CopyBinaryBlob(pbTemp, &pCanonical->EncryptedPreMasterSecret, &dwCount);
  167. pbTemp += dwCount;
  168. *pcbBuffer += dwCount;
  169. //
  170. // copy client user name
  171. //
  172. CopyBinaryBlob(pbTemp, &pCanonical->ClientUserName, &dwCount);
  173. pbTemp += dwCount;
  174. *pcbBuffer += dwCount;
  175. //
  176. // copy client machine name
  177. //
  178. CopyBinaryBlob(pbTemp, &pCanonical->ClientMachineName, &dwCount);
  179. pbTemp += dwCount;
  180. *pcbBuffer += dwCount;
  181. LS_LOG_RESULT(lsReturn);
  182. CommonReturn:
  183. //return lsReturn;
  184. LS_RETURN(lsReturn);
  185. ErrorReturn:
  186. goto CommonReturn;
  187. }
  188. /***************************************************************************************
  189. * Function : PackHydraClientLicenseInfo
  190. * Purpose : This function takes a pointer to a Hydra_Client_License_Info structure
  191. * and copies the data to the buffer pointed by pbBuffer. pcbBuffer
  192. * should point the size of the buffer pointed by pbBuffer. After the
  193. * function returns, pcbBuffer contains the no. of bytes copied in the
  194. * buffer. If pbBuffer is NULL, the fucntion returns the size of the
  195. * pbBuffer to be allocated
  196. * Returns : LICENSE_STATUS
  197. ****************************************************************************************/
  198. LICENSE_STATUS
  199. PackHydraClientLicenseInfo(
  200. IN PHydra_Client_License_Info pCanonical,
  201. IN BOOL fExtendedError,
  202. OUT BYTE FAR * pbBuffer,
  203. IN OUT DWORD FAR * pcbBuffer
  204. )
  205. {
  206. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  207. BYTE FAR * pbTemp; //To be used while copying the data
  208. Preamble Header;
  209. DWORD dwCount = 0;
  210. //Check if the inputs are valid or not!
  211. LS_BEGIN(TEXT("PackHydraClientLicenseInfo\n"));
  212. if(pCanonical == NULL)
  213. {
  214. INVALID_INPUT_RETURN;
  215. }
  216. if( pbBuffer == NULL && pcbBuffer == NULL )
  217. {
  218. INVALID_INPUT_RETURN;
  219. }
  220. //Initialize Message Header
  221. Header.bMsgType = HC_LICENSE_INFO;
  222. Header.bVersion = PREAMBLE_VERSION_3_0;
  223. if(fExtendedError == TRUE)
  224. {
  225. Header.bVersion |= EXTENDED_ERROR_CAPABILITY;
  226. }
  227. Header.wMsgSize = 0;
  228. //Calculate the size of the message and place the data in Header.wMsgSize.
  229. //Start with Preamble size
  230. Header.wMsgSize += sizeof(Preamble);
  231. //dwPrefKeyExchangeAlg
  232. Header.wMsgSize += sizeof(pCanonical->dwPrefKeyExchangeAlg);
  233. //dwPlatformID
  234. Header.wMsgSize += sizeof(pCanonical->dwPlatformID);
  235. //ClientRandom
  236. Header.wMsgSize += LICENSE_RANDOM;
  237. //EncryptedPreMasterSecret
  238. Header.wMsgSize += sizeof(pCanonical->EncryptedPreMasterSecret.wBlobType) +
  239. sizeof(pCanonical->EncryptedPreMasterSecret.wBlobLen) +
  240. pCanonical->EncryptedPreMasterSecret.wBlobLen;
  241. //Add the license Info
  242. Header.wMsgSize += sizeof(pCanonical->LicenseInfo.wBlobType) +
  243. sizeof(pCanonical->LicenseInfo.wBlobLen) +
  244. pCanonical->LicenseInfo.wBlobLen;
  245. //Encrypted HWID
  246. Header.wMsgSize += sizeof(pCanonical->EncryptedHWID.wBlobType) +
  247. sizeof(pCanonical->EncryptedHWID.wBlobLen) +
  248. pCanonical->EncryptedHWID.wBlobLen;
  249. //MACData
  250. Header.wMsgSize += LICENSE_MAC_DATA;
  251. //If the input buffer is null, inform the user to allocate a buffer of size
  252. //*pcbBuffer!
  253. if(pbBuffer == NULL)
  254. {
  255. *pcbBuffer = (DWORD)Header.wMsgSize;
  256. LS_LOG_RESULT(lsReturn);
  257. goto CommonReturn;
  258. }
  259. //else, check if the allocated buffer size is more than the required!
  260. else if(*pcbBuffer < (DWORD)Header.wMsgSize)
  261. {
  262. lsReturn = LICENSE_STATUS_INSUFFICIENT_BUFFER;
  263. LS_LOG_RESULT(lsReturn);
  264. goto ErrorReturn;
  265. }
  266. pbTemp = pbBuffer;
  267. *pcbBuffer = 0;
  268. //Now start copying different members of the New License structure to the
  269. //buffer specified by the caller
  270. //first copy the Header into the buffer
  271. memcpy(pbTemp, &Header, sizeof(Preamble));
  272. pbTemp += sizeof(Preamble);
  273. *pcbBuffer += sizeof(Preamble);
  274. //Copy the dwPrefKeyExchangeAlg parameter
  275. memcpy(pbTemp, &pCanonical->dwPrefKeyExchangeAlg, sizeof(pCanonical->dwPrefKeyExchangeAlg));
  276. pbTemp += sizeof(pCanonical->dwPrefKeyExchangeAlg);
  277. *pcbBuffer += sizeof(pCanonical->dwPrefKeyExchangeAlg);
  278. //Copy the dwPlatformID
  279. memcpy(pbTemp, &pCanonical->dwPlatformID, sizeof(pCanonical->dwPlatformID));
  280. pbTemp += sizeof(pCanonical->dwPlatformID);
  281. *pcbBuffer += sizeof(pCanonical->dwPlatformID);
  282. //Copy ClientRandom
  283. memcpy(pbTemp, pCanonical->ClientRandom, LICENSE_RANDOM);
  284. pbTemp += LICENSE_RANDOM;
  285. *pcbBuffer += LICENSE_RANDOM;
  286. //Copy EncryptedPreMasterSecret Blob
  287. CopyBinaryBlob(pbTemp, &pCanonical->EncryptedPreMasterSecret, &dwCount);
  288. pbTemp += dwCount;
  289. *pcbBuffer += dwCount;
  290. //Copy LicenseInfo
  291. CopyBinaryBlob(pbTemp, &pCanonical->LicenseInfo, &dwCount);
  292. pbTemp += dwCount;
  293. *pcbBuffer += dwCount;
  294. //Copy EncryptedHWID
  295. CopyBinaryBlob(pbTemp, &pCanonical->EncryptedHWID, &dwCount);
  296. pbTemp += dwCount;
  297. *pcbBuffer += dwCount;
  298. //Copy MACData
  299. memcpy(pbTemp, pCanonical->MACData, LICENSE_MAC_DATA);
  300. pbTemp += LICENSE_MAC_DATA;
  301. *pcbBuffer += LICENSE_MAC_DATA;
  302. LS_LOG_RESULT(lsReturn);
  303. CommonReturn:
  304. //return lsReturn;
  305. LS_RETURN(lsReturn);
  306. ErrorReturn:
  307. goto CommonReturn;
  308. }
  309. /****************************************************************************************
  310. * Function : PackHydraClientPlatformChallengeResponse
  311. * Purpose : This function takes a pointer to a Hydra_Client_Platform_Info structure
  312. * and copies the data to the buffer pointed by pbBuffer. pcbBuffer should
  313. * point the size of the buffer pointed by pbBuffer. After the function
  314. * returns, pcbBuffer contains the no. of bytes copied in the buffer.
  315. * If pbBuffer is NULL, the fucntion returns the size of the pbBuffer to
  316. * be allocated
  317. * Returns : LICENSE_STATUS
  318. ******************************************************************************************/
  319. LICENSE_STATUS
  320. PackHydraClientPlatformChallengeResponse(
  321. IN PHydra_Client_Platform_Challenge_Response pCanonical,
  322. IN BOOL fExtendedError,
  323. OUT BYTE FAR * pbBuffer,
  324. IN OUT DWORD FAR * pcbBuffer
  325. )
  326. {
  327. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  328. BYTE FAR * pbTemp; //To be used while copying the data
  329. Preamble Header;
  330. DWORD dwCount = 0;
  331. //Check if the inputs are valid or not!
  332. LS_BEGIN(TEXT("PackHydraClientPlatformChallengeResponse\n"));
  333. if(pCanonical == NULL)
  334. {
  335. INVALID_INPUT_RETURN;
  336. }
  337. if( pbBuffer == NULL && pcbBuffer == NULL )
  338. {
  339. INVALID_INPUT_RETURN;
  340. }
  341. //Initialize Message Header
  342. Header.bMsgType = HC_PLATFORM_CHALENGE_RESPONSE;
  343. Header.bVersion = PREAMBLE_VERSION_3_0;
  344. if(fExtendedError == TRUE)
  345. {
  346. Header.bVersion |= EXTENDED_ERROR_CAPABILITY;
  347. }
  348. Header.wMsgSize = 0;
  349. //Calculate the size of the message and place the data in Header.wMsgSize.
  350. //Start with Preamble size
  351. Header.wMsgSize += sizeof(Preamble);
  352. //EncryptedChallengeResponse
  353. Header.wMsgSize += sizeof(pCanonical->EncryptedChallengeResponse.wBlobType) +
  354. sizeof(pCanonical->EncryptedChallengeResponse.wBlobLen) +
  355. pCanonical->EncryptedChallengeResponse.wBlobLen;
  356. //Encrypted HWID
  357. Header.wMsgSize += sizeof(pCanonical->EncryptedHWID.wBlobType) +
  358. sizeof(pCanonical->EncryptedHWID.wBlobLen) +
  359. pCanonical->EncryptedHWID.wBlobLen;
  360. //MACData
  361. Header.wMsgSize += LICENSE_MAC_DATA;
  362. if(pbBuffer == NULL)
  363. {
  364. *pcbBuffer = (DWORD)Header.wMsgSize;
  365. LS_LOG_RESULT(lsReturn);
  366. goto CommonReturn;
  367. }
  368. else if(*pcbBuffer < (DWORD)Header.wMsgSize)
  369. {
  370. lsReturn = LICENSE_STATUS_INSUFFICIENT_BUFFER;
  371. LS_LOG_RESULT(lsReturn);
  372. goto ErrorReturn;
  373. }
  374. pbTemp = pbBuffer;
  375. *pcbBuffer = 0;
  376. //Now start copying different members of the New License structure to the
  377. //buffer specified by the caller
  378. //first copy the Header into the buffer
  379. memcpy(pbTemp, &Header, sizeof(Preamble));
  380. pbTemp += sizeof(Preamble);
  381. *pcbBuffer += sizeof(Preamble);
  382. //Copy LicenseInfo
  383. CopyBinaryBlob(pbTemp, &pCanonical->EncryptedChallengeResponse, &dwCount);
  384. pbTemp += dwCount;
  385. *pcbBuffer += dwCount;
  386. //Copy EncryptedHWID
  387. CopyBinaryBlob(pbTemp, &pCanonical->EncryptedHWID, &dwCount);
  388. pbTemp += dwCount;
  389. *pcbBuffer += dwCount;
  390. //Copy MACData
  391. memcpy(pbTemp, pCanonical->MACData, LICENSE_MAC_DATA);
  392. pbTemp += LICENSE_MAC_DATA;
  393. //CopyBinaryBlob(pbTemp, &pCanonical->MACData, &dwCount);
  394. *pcbBuffer += LICENSE_MAC_DATA;
  395. LS_LOG_RESULT(lsReturn);
  396. CommonReturn:
  397. //return lsReturn;
  398. LS_RETURN(lsReturn);
  399. ErrorReturn:
  400. goto CommonReturn;
  401. }
  402. /****************************************************************************************
  403. * Funtion : PackLicenseErrorMessage
  404. * Purpose : This function takes a pointer to a License_Error_Message structure
  405. * and copies the data to the buffer pointed by pbBuffer. pcbBuffer should
  406. * point the size of the buffer pointed by pbBuffer. After the function
  407. * returns, pcbBuffer contains the no. of bytes copied in the buffer.
  408. * If pbBuffer is NULL, the fucntion returns the size of the pbBuffer to
  409. * be allocated
  410. * Return : LICENSE_STATUS
  411. *****************************************************************************************/
  412. LICENSE_STATUS
  413. PackLicenseErrorMessage(
  414. IN PLicense_Error_Message pCanonical,
  415. IN BOOL fExtendedError,
  416. OUT BYTE FAR * pbBuffer,
  417. IN OUT DWORD FAR * pcbBuffer
  418. )
  419. {
  420. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  421. BYTE FAR * pbTemp; //To be used while copying the data
  422. Preamble Header;
  423. DWORD dwCount = 0;
  424. LS_BEGIN(TEXT("PackLicenseErrorMessage\n"));
  425. //Check if the inputs are valid or not!
  426. if(pCanonical == NULL)
  427. {
  428. INVALID_INPUT_RETURN;
  429. }
  430. if( pbBuffer == NULL && pcbBuffer == NULL )
  431. {
  432. INVALID_INPUT_RETURN;
  433. }
  434. //Initialize Message Header
  435. Header.bMsgType = GM_ERROR_ALERT;
  436. Header.bVersion = PREAMBLE_VERSION_3_0;
  437. if(fExtendedError == TRUE)
  438. {
  439. Header.bVersion |= EXTENDED_ERROR_CAPABILITY;
  440. }
  441. Header.wMsgSize = 0;
  442. //Calculate the size of the message and place the data in Header.wMsgSize.
  443. //Start with Preamble size
  444. Header.wMsgSize += sizeof(Preamble);
  445. //dwErrorCode
  446. Header.wMsgSize += sizeof(pCanonical->dwErrorCode);
  447. //dwStateTransition
  448. Header.wMsgSize += sizeof(pCanonical->dwStateTransition);
  449. //bbErrorInfo
  450. Header.wMsgSize += sizeof(pCanonical->bbErrorInfo.wBlobType) +
  451. sizeof(pCanonical->bbErrorInfo.wBlobLen) +
  452. pCanonical->bbErrorInfo.wBlobLen;
  453. if(pbBuffer == NULL)
  454. {
  455. *pcbBuffer = (DWORD)Header.wMsgSize;
  456. LS_LOG_RESULT(lsReturn);
  457. goto CommonReturn;
  458. }
  459. else if(*pcbBuffer < (DWORD)Header.wMsgSize)
  460. {
  461. lsReturn = LICENSE_STATUS_INSUFFICIENT_BUFFER;
  462. LS_LOG_RESULT(lsReturn);
  463. goto ErrorReturn;
  464. }
  465. pbTemp = pbBuffer;
  466. *pcbBuffer = 0;
  467. //Now start copying different members of the New License structure to the
  468. //buffer specified by the caller
  469. //first copy the Header into the buffer
  470. memcpy(pbTemp, &Header, sizeof(Preamble));
  471. pbTemp += sizeof(Preamble);
  472. *pcbBuffer += sizeof(Preamble);
  473. //Copy dwErrorCode
  474. memcpy(pbTemp, &pCanonical->dwErrorCode, sizeof(pCanonical->dwErrorCode));
  475. pbTemp += sizeof(pCanonical->dwErrorCode);
  476. *pcbBuffer += sizeof(pCanonical->dwErrorCode);
  477. //Copy dwStateTransition
  478. memcpy(pbTemp, &pCanonical->dwStateTransition, sizeof(pCanonical->dwStateTransition));
  479. pbTemp += sizeof(pCanonical->dwStateTransition);
  480. *pcbBuffer += sizeof(pCanonical->dwStateTransition);
  481. //Copy bbErrorInfo
  482. CopyBinaryBlob(pbTemp, &pCanonical->bbErrorInfo, &dwCount);
  483. pbTemp += dwCount;
  484. *pcbBuffer += dwCount;
  485. LS_LOG_RESULT(lsReturn);
  486. CommonReturn:
  487. LS_RETURN(lsReturn);
  488. //return lsReturn;
  489. ErrorReturn:
  490. goto CommonReturn;
  491. }
  492. /****************************************************************************************
  493. * Function : UnpackLicenseErrorMessage
  494. * Purpose : To unpack a binary blob into a License_Error_Message structure.
  495. * Note : The caller should initialize the pointer. All the necessary allocation is
  496. * done by the function itself.
  497. * The caller should free all the memory components once it is no longer needed.
  498. * Return : License_Status
  499. *****************************************************************************************/
  500. LICENSE_STATUS
  501. UnPackLicenseErrorMessage(
  502. IN BYTE FAR * pbMessage,
  503. IN DWORD cbMessage,
  504. OUT PLicense_Error_Message pCanonical
  505. )
  506. {
  507. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  508. BYTE FAR * pbTemp;
  509. DWORD dwTemp;
  510. DWORD dwSize;
  511. LS_BEGIN(TEXT("UnpackLicenseErrorMessage\n"));
  512. if(pbMessage == NULL)
  513. {
  514. INVALID_INPUT_RETURN;
  515. }
  516. if(pCanonical == NULL)
  517. {
  518. INVALID_INPUT_RETURN;
  519. }
  520. //Memset pCanonical structure to zero
  521. memset(pCanonical, 0x00, sizeof(License_Error_Message));
  522. LS_DUMPSTRING(cbMessage, pbMessage);
  523. pbTemp = pbMessage;
  524. dwTemp = cbMessage;
  525. if (dwTemp < 2 * sizeof(DWORD))
  526. {
  527. INVALID_INPUT_RETURN;
  528. }
  529. //Assign dwErrorCode
  530. pCanonical->dwErrorCode = *( UNALIGNED DWORD* )pbTemp;
  531. pbTemp += sizeof(DWORD);
  532. dwTemp -= sizeof(DWORD);
  533. //Assign dwStateTransition
  534. pCanonical->dwStateTransition = *( UNALIGNED DWORD* )pbTemp;
  535. pbTemp += sizeof(DWORD);
  536. dwTemp -= sizeof(DWORD);
  537. if (dwTemp < 2 * sizeof(WORD))
  538. {
  539. INVALID_INPUT_RETURN;
  540. }
  541. pCanonical->bbErrorInfo.wBlobType = *( UNALIGNED WORD* )pbTemp;
  542. pbTemp += sizeof(WORD);
  543. dwTemp -= sizeof(WORD);
  544. pCanonical->bbErrorInfo.wBlobLen = *( UNALIGNED WORD* )pbTemp;
  545. pbTemp += sizeof(WORD);
  546. dwTemp -= sizeof(WORD);
  547. dwSize = pCanonical->bbErrorInfo.wBlobLen;
  548. if(dwSize > dwTemp)
  549. {
  550. INVALID_INPUT_RETURN;
  551. }
  552. if(pCanonical->bbErrorInfo.wBlobLen>0)
  553. {
  554. if( NULL == (pCanonical->bbErrorInfo.pBlob = (BYTE FAR *)malloc(pCanonical->bbErrorInfo.wBlobLen)) )
  555. {
  556. pCanonical->bbErrorInfo.wBlobLen = 0;
  557. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  558. LS_LOG_RESULT(lsReturn);
  559. goto ErrorReturn;
  560. }
  561. memset(pCanonical->bbErrorInfo.pBlob, 0x00, pCanonical->bbErrorInfo.wBlobLen);
  562. memcpy(pCanonical->bbErrorInfo.pBlob, pbTemp, pCanonical->bbErrorInfo.wBlobLen);
  563. }
  564. else
  565. {
  566. pCanonical->bbErrorInfo.pBlob = NULL;
  567. }
  568. LS_LOG_RESULT(lsReturn);
  569. ErrorReturn:
  570. LS_RETURN(lsReturn);
  571. }
  572. /****************************************************************************************
  573. * Function : UnpackHydraServerLicenseRequest
  574. * Purpose : To unpack a binary blob into a Hydra_Server_License_Request structure.
  575. * Note : The caller should initialize the output pointer. All the necessary
  576. * allocation for different structure components is done by the function itself.
  577. * The caller should free all the memory components once it is no longer needed.
  578. * Return : License_Status
  579. *****************************************************************************************/
  580. LICENSE_STATUS
  581. UnpackHydraServerLicenseRequest(
  582. IN BYTE FAR * pbMessage,
  583. IN DWORD cbMessage,
  584. OUT PHydra_Server_License_Request pCanonical
  585. )
  586. {
  587. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  588. BYTE FAR *pbTemp = NULL;
  589. DWORD dwTemp = 0;
  590. DWORD i = 0;
  591. LS_BEGIN(TEXT("UnpackHydraServerLicenseRequest\n"));
  592. if(pbMessage == NULL)
  593. {
  594. INVALID_INPUT_RETURN;
  595. }
  596. if(pCanonical == NULL)
  597. {
  598. INVALID_INPUT_RETURN;
  599. }
  600. LS_DUMPSTRING(cbMessage, pbMessage);
  601. pbTemp = pbMessage;
  602. dwTemp = cbMessage;
  603. if (dwTemp < LICENSE_RANDOM)
  604. {
  605. INVALID_INPUT_RETURN;
  606. }
  607. //Copy Server Random
  608. memcpy(pCanonical->ServerRandom, pbTemp, LICENSE_RANDOM);
  609. pbTemp += LICENSE_RANDOM;
  610. dwTemp -= LICENSE_RANDOM;
  611. if (dwTemp < 2 * sizeof(DWORD))
  612. {
  613. INVALID_INPUT_RETURN;
  614. }
  615. //Copy the ProductInfo structure
  616. pCanonical->ProductInfo.dwVersion = *( UNALIGNED DWORD* )pbTemp;
  617. pbTemp += sizeof(DWORD);
  618. dwTemp -= sizeof(DWORD);
  619. pCanonical->ProductInfo.cbCompanyName = *( UNALIGNED DWORD* )pbTemp;
  620. pbTemp += sizeof(DWORD);
  621. dwTemp -= sizeof(DWORD);
  622. if(pCanonical->ProductInfo.cbCompanyName>0)
  623. {
  624. if(dwTemp < pCanonical->ProductInfo.cbCompanyName)
  625. {
  626. INVALID_INPUT_RETURN;
  627. }
  628. if( NULL == (pCanonical->ProductInfo.pbCompanyName = (BYTE FAR *)malloc(pCanonical->ProductInfo.cbCompanyName)) )
  629. {
  630. pCanonical->ProductInfo.cbCompanyName = 0;
  631. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  632. LS_LOG_RESULT(lsReturn);
  633. goto ErrorReturn;
  634. }
  635. memcpy(pCanonical->ProductInfo.pbCompanyName, pbTemp, pCanonical->ProductInfo.cbCompanyName);
  636. pbTemp += pCanonical->ProductInfo.cbCompanyName;
  637. dwTemp -= pCanonical->ProductInfo.cbCompanyName;
  638. }
  639. if(dwTemp < sizeof(DWORD))
  640. {
  641. INVALID_INPUT_RETURN;
  642. }
  643. pCanonical->ProductInfo.cbProductID = *( UNALIGNED DWORD* )pbTemp;
  644. pbTemp += sizeof(DWORD);
  645. dwTemp -= sizeof(DWORD);
  646. if(pCanonical->ProductInfo.cbProductID>0)
  647. {
  648. if(dwTemp < pCanonical->ProductInfo.cbProductID)
  649. {
  650. INVALID_INPUT_RETURN;
  651. }
  652. if( NULL == (pCanonical->ProductInfo.pbProductID = (BYTE FAR *)malloc(pCanonical->ProductInfo.cbProductID)) )
  653. {
  654. pCanonical->ProductInfo.cbProductID = 0;
  655. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  656. LS_LOG_RESULT(lsReturn);
  657. goto ErrorReturn;
  658. }
  659. memcpy(pCanonical->ProductInfo.pbProductID, pbTemp, pCanonical->ProductInfo.cbProductID);
  660. pbTemp += pCanonical->ProductInfo.cbProductID;
  661. dwTemp -= pCanonical->ProductInfo.cbProductID;
  662. }
  663. if(dwTemp < sizeof(WORD)*2)
  664. {
  665. INVALID_INPUT_RETURN;
  666. }
  667. //Copy KeyExchngList
  668. pCanonical->KeyExchngList.wBlobType = *( UNALIGNED WORD* )pbTemp;
  669. pbTemp += sizeof(WORD);
  670. dwTemp -= sizeof(WORD);
  671. pCanonical->KeyExchngList.wBlobLen = *( UNALIGNED WORD* )pbTemp;
  672. pbTemp += sizeof(WORD);
  673. dwTemp -= sizeof(WORD);
  674. if( pCanonical->KeyExchngList.wBlobLen > 0 )
  675. {
  676. if(dwTemp < pCanonical->KeyExchngList.wBlobLen)
  677. {
  678. INVALID_INPUT_RETURN;
  679. }
  680. if( NULL == (pCanonical->KeyExchngList.pBlob = (BYTE FAR *)malloc(pCanonical->KeyExchngList.wBlobLen)) )
  681. {
  682. pCanonical->KeyExchngList.wBlobLen = 0;
  683. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  684. LS_LOG_RESULT(lsReturn);
  685. goto ErrorReturn;
  686. }
  687. memcpy(pCanonical->KeyExchngList.pBlob, pbTemp, pCanonical->KeyExchngList.wBlobLen);
  688. pbTemp += pCanonical->KeyExchngList.wBlobLen;
  689. dwTemp -= pCanonical->KeyExchngList.wBlobLen;
  690. }
  691. if(dwTemp < sizeof(WORD)*2)
  692. {
  693. INVALID_INPUT_RETURN;
  694. }
  695. //Copy ServerCert
  696. pCanonical->ServerCert.wBlobType = *( UNALIGNED WORD* )pbTemp;
  697. pbTemp += sizeof(WORD);
  698. dwTemp -= sizeof(WORD);
  699. pCanonical->ServerCert.wBlobLen = *( UNALIGNED WORD* )pbTemp;
  700. pbTemp += sizeof(WORD);
  701. dwTemp -= sizeof(WORD);
  702. if(pCanonical->ServerCert.wBlobLen >0)
  703. {
  704. if(dwTemp < pCanonical->ServerCert.wBlobLen)
  705. {
  706. INVALID_INPUT_RETURN;
  707. }
  708. if( NULL == (pCanonical->ServerCert.pBlob = (BYTE FAR *)malloc(pCanonical->ServerCert.wBlobLen)) )
  709. {
  710. pCanonical->ServerCert.wBlobLen = 0;
  711. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  712. LS_LOG_RESULT(lsReturn);
  713. goto ErrorReturn;
  714. }
  715. memcpy(pCanonical->ServerCert.pBlob, pbTemp, pCanonical->ServerCert.wBlobLen);
  716. pbTemp += pCanonical->ServerCert.wBlobLen;
  717. dwTemp -= pCanonical->ServerCert.wBlobLen;
  718. }
  719. if(dwTemp < sizeof(DWORD))
  720. {
  721. INVALID_INPUT_RETURN;
  722. }
  723. //Copy the scopelist
  724. pCanonical->ScopeList.dwScopeCount = *( UNALIGNED DWORD* )pbTemp;
  725. pbTemp += sizeof( DWORD );
  726. dwTemp -= sizeof( DWORD );
  727. if(dwTemp < pCanonical->ScopeList.dwScopeCount*sizeof(Binary_Blob))
  728. {
  729. pCanonical->ScopeList.dwScopeCount = 0;
  730. INVALID_INPUT_RETURN;
  731. }
  732. if( NULL == (pCanonical->ScopeList.Scopes = (PBinary_Blob)malloc(pCanonical->ScopeList.dwScopeCount*sizeof(Binary_Blob))) )
  733. {
  734. pCanonical->ScopeList.dwScopeCount = 0;
  735. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  736. LS_LOG_RESULT(lsReturn);
  737. goto ErrorReturn;
  738. }
  739. memset(pCanonical->ScopeList.Scopes, 0x00, pCanonical->ScopeList.dwScopeCount*sizeof(Binary_Blob));
  740. for(i = 0; i<pCanonical->ScopeList.dwScopeCount; i++ )
  741. {
  742. if(dwTemp < sizeof(WORD)*2)
  743. {
  744. pCanonical->ScopeList.dwScopeCount = i;
  745. INVALID_INPUT_RETURN;
  746. }
  747. pCanonical->ScopeList.Scopes[i].wBlobType = *( UNALIGNED WORD* )pbTemp;
  748. pbTemp += sizeof(WORD);
  749. dwTemp -= sizeof(WORD);
  750. pCanonical->ScopeList.Scopes[i].wBlobLen = *( UNALIGNED WORD* )pbTemp;
  751. pbTemp += sizeof(WORD);
  752. dwTemp -= sizeof(WORD);
  753. if(dwTemp < pCanonical->ScopeList.Scopes[i].wBlobLen)
  754. {
  755. pCanonical->ScopeList.dwScopeCount = i;
  756. INVALID_INPUT_RETURN;
  757. }
  758. if( NULL ==(pCanonical->ScopeList.Scopes[i].pBlob = (BYTE FAR *)malloc(pCanonical->ScopeList.Scopes[i].wBlobLen)) )
  759. {
  760. pCanonical->ScopeList.Scopes[i].wBlobLen = 0;
  761. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  762. LS_LOG_RESULT(lsReturn);
  763. goto ErrorReturn;
  764. }
  765. memcpy(pCanonical->ScopeList.Scopes[i].pBlob, pbTemp, pCanonical->ScopeList.Scopes[i].wBlobLen);
  766. pbTemp += pCanonical->ScopeList.Scopes[i].wBlobLen;
  767. dwTemp -= pCanonical->ScopeList.Scopes[i].wBlobLen;
  768. }
  769. LS_LOG_RESULT(lsReturn);
  770. LS_RETURN(lsReturn);
  771. ErrorReturn:
  772. if (pCanonical)
  773. {
  774. if(pCanonical->ProductInfo.pbCompanyName)
  775. {
  776. free(pCanonical->ProductInfo.pbCompanyName);
  777. pCanonical->ProductInfo.pbCompanyName = NULL;
  778. }
  779. if(pCanonical->ProductInfo.pbProductID)
  780. {
  781. free(pCanonical->ProductInfo.pbProductID);
  782. pCanonical->ProductInfo.pbProductID = NULL;
  783. }
  784. if(pCanonical->KeyExchngList.pBlob)
  785. {
  786. free(pCanonical->KeyExchngList.pBlob);
  787. pCanonical->KeyExchngList.pBlob = NULL;
  788. }
  789. if(pCanonical->ServerCert.pBlob)
  790. {
  791. free(pCanonical->ServerCert.pBlob);
  792. pCanonical->ServerCert.pBlob = NULL;
  793. }
  794. for(i = 0; i<pCanonical->ScopeList.dwScopeCount; i++ )
  795. {
  796. if(pCanonical->ScopeList.Scopes[i].pBlob)
  797. {
  798. free(pCanonical->ScopeList.Scopes[i].pBlob);
  799. pCanonical->ScopeList.Scopes[i].pBlob = NULL;
  800. }
  801. }
  802. if(pCanonical->ScopeList.Scopes)
  803. {
  804. free(pCanonical->ScopeList.Scopes);
  805. pCanonical->ScopeList.Scopes = NULL;
  806. }
  807. }
  808. LS_RETURN(lsReturn);
  809. }
  810. /****************************************************************************************
  811. * Function : UnpackHydraPlatformChallenge
  812. * Purpose : To unpack a binary blob into a Hydra_Server_Platform_Challenge structure.
  813. * Note : The caller should initialize the output pointer. All the necessary
  814. * allocation for different structure components is done by the function itself.
  815. * The caller should free all the memory components once it is no longer needed.
  816. * Return : License_Status
  817. *****************************************************************************************/
  818. LICENSE_STATUS
  819. UnPackHydraServerPlatformChallenge(
  820. IN BYTE FAR * pbMessage,
  821. IN DWORD cbMessage,
  822. OUT PHydra_Server_Platform_Challenge pCanonical
  823. )
  824. {
  825. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  826. BYTE FAR * pbTemp = NULL;
  827. DWORD dwTemp = 0;
  828. LS_BEGIN(TEXT("UnpackHydraServerPlatformChallenge\n"));
  829. if(pbMessage == NULL)
  830. {
  831. INVALID_INPUT_RETURN;
  832. }
  833. if(pCanonical == NULL)
  834. {
  835. INVALID_INPUT_RETURN;
  836. }
  837. LS_DUMPSTRING(cbMessage, pbMessage);
  838. pbTemp = pbMessage;
  839. dwTemp = cbMessage;
  840. if (dwTemp < sizeof(DWORD))
  841. {
  842. INVALID_INPUT_RETURN;
  843. }
  844. //Assign dwConnectFlags
  845. pCanonical->dwConnectFlags = *( UNALIGNED DWORD* )pbTemp;
  846. pbTemp += sizeof(DWORD);
  847. dwTemp -= sizeof(DWORD);
  848. if (dwTemp < 2 * sizeof(WORD))
  849. {
  850. INVALID_INPUT_RETURN;
  851. }
  852. //Assign EncryptedPlatformChallenge
  853. pCanonical->EncryptedPlatformChallenge.wBlobType = *( UNALIGNED WORD* )pbTemp;
  854. pbTemp += sizeof(WORD);
  855. dwTemp -= sizeof(WORD);
  856. pCanonical->EncryptedPlatformChallenge.wBlobLen = *( UNALIGNED WORD* )pbTemp;
  857. pbTemp += sizeof(WORD);
  858. dwTemp -= sizeof(WORD);
  859. if(pCanonical->EncryptedPlatformChallenge.wBlobLen >0)
  860. {
  861. if (dwTemp < pCanonical->EncryptedPlatformChallenge.wBlobLen)
  862. {
  863. INVALID_INPUT_RETURN;
  864. }
  865. if( NULL == (pCanonical->EncryptedPlatformChallenge.pBlob = (BYTE FAR *)malloc(pCanonical->EncryptedPlatformChallenge.wBlobLen)) )
  866. {
  867. pCanonical->EncryptedPlatformChallenge.wBlobLen = 0;
  868. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  869. LS_LOG_RESULT(lsReturn);
  870. goto ErrorReturn;
  871. }
  872. memcpy(pCanonical->EncryptedPlatformChallenge.pBlob, pbTemp, pCanonical->EncryptedPlatformChallenge.wBlobLen);
  873. pbTemp += pCanonical->EncryptedPlatformChallenge.wBlobLen;
  874. dwTemp -= pCanonical->EncryptedPlatformChallenge.wBlobLen;
  875. }
  876. if(dwTemp < LICENSE_MAC_DATA)
  877. {
  878. INVALID_INPUT_RETURN;
  879. }
  880. //Assign MACData
  881. memcpy(pCanonical->MACData, pbTemp, LICENSE_MAC_DATA);
  882. pbTemp += LICENSE_MAC_DATA;
  883. dwTemp -= LICENSE_MAC_DATA;
  884. LS_LOG_RESULT(lsReturn);
  885. LS_RETURN(lsReturn);
  886. ErrorReturn:
  887. if (pCanonical)
  888. {
  889. if(pCanonical->EncryptedPlatformChallenge.pBlob)
  890. {
  891. free(pCanonical->EncryptedPlatformChallenge.pBlob);
  892. pCanonical->EncryptedPlatformChallenge.pBlob = NULL;
  893. }
  894. }
  895. LS_RETURN(lsReturn);
  896. }
  897. /****************************************************************************************
  898. * Function : UnpackHydraServerNewLicense
  899. * Purpose : To unpack a binary blob into a Hydra_Server_New_License structure.
  900. * Note : The caller should initialize the output pointer. All the necessary
  901. * allocation for different structure components is done by the function itself.
  902. * The caller should free all the memory components once it is no longer needed.
  903. * Return : License_Status
  904. *****************************************************************************************/
  905. LICENSE_STATUS
  906. UnPackHydraServerNewLicense(
  907. IN BYTE FAR * pbMessage,
  908. IN DWORD cbMessage,
  909. OUT PHydra_Server_New_License pCanonical
  910. )
  911. {
  912. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  913. BYTE FAR * pbTemp = NULL;
  914. DWORD dwTemp = 0;
  915. LS_BEGIN(TEXT("UnpackHydraServerNewLicense\n"));
  916. if( (pbMessage == NULL) || (pCanonical == NULL ) )
  917. {
  918. INVALID_INPUT_RETURN;
  919. }
  920. memset(pCanonical, 0x00, sizeof(Hydra_Server_New_License));
  921. LS_DUMPSTRING(cbMessage, pbMessage);
  922. pbTemp = pbMessage;
  923. dwTemp = cbMessage;
  924. if (dwTemp < 2 * sizeof(WORD))
  925. {
  926. INVALID_INPUT_RETURN;
  927. }
  928. //Assign EncryptedNewLicenseInfo
  929. pCanonical->EncryptedNewLicenseInfo.wBlobType = *( UNALIGNED WORD* )pbTemp;
  930. pbTemp += sizeof(WORD);
  931. dwTemp -= sizeof(WORD);
  932. pCanonical->EncryptedNewLicenseInfo.wBlobLen = *( UNALIGNED WORD* )pbTemp;
  933. pbTemp += sizeof(WORD);
  934. dwTemp -= sizeof(WORD);
  935. if(pCanonical->EncryptedNewLicenseInfo.wBlobLen > 0)
  936. {
  937. if (dwTemp < pCanonical->EncryptedNewLicenseInfo.wBlobLen)
  938. {
  939. INVALID_INPUT_RETURN;
  940. }
  941. if( NULL == (pCanonical->EncryptedNewLicenseInfo.pBlob = (BYTE FAR *)malloc(pCanonical->EncryptedNewLicenseInfo.wBlobLen)) )
  942. {
  943. pCanonical->EncryptedNewLicenseInfo.wBlobLen = 0;
  944. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  945. LS_LOG_RESULT(lsReturn);
  946. goto ErrorReturn;
  947. }
  948. memcpy(pCanonical->EncryptedNewLicenseInfo.pBlob, pbTemp, pCanonical->EncryptedNewLicenseInfo.wBlobLen);
  949. pbTemp += pCanonical->EncryptedNewLicenseInfo.wBlobLen;
  950. dwTemp -= pCanonical->EncryptedNewLicenseInfo.wBlobLen;
  951. }
  952. if(dwTemp < LICENSE_MAC_DATA)
  953. {
  954. INVALID_INPUT_RETURN;
  955. }
  956. //Copy MACData
  957. memcpy(pCanonical->MACData, pbTemp, LICENSE_MAC_DATA);
  958. pbTemp += LICENSE_MAC_DATA;
  959. dwTemp -= LICENSE_MAC_DATA;
  960. LS_LOG_RESULT(lsReturn);
  961. LS_RETURN(lsReturn);
  962. ErrorReturn:
  963. if (pCanonical)
  964. {
  965. if(pCanonical->EncryptedNewLicenseInfo.pBlob)
  966. {
  967. free(pCanonical->EncryptedNewLicenseInfo.pBlob);
  968. pCanonical->EncryptedNewLicenseInfo.pBlob = NULL;
  969. }
  970. }
  971. LS_RETURN(lsReturn);
  972. }
  973. /****************************************************************************************
  974. * Function : UnpackHydraServerUpgradeLicense
  975. * Purpose : To unpack a binary blob into a Hydra_Server_Upgrade_License structure.
  976. * Note : The caller should initialize the output pointer. All the necessary
  977. * allocation for different structure components is done by the function itself.
  978. * The caller should free all the memory components once it is no longer needed.
  979. * Internally this function calls UnpackHydraServerUpgradeLicense.
  980. * Return : License_Status
  981. *****************************************************************************************/
  982. LICENSE_STATUS
  983. UnPackHydraServerUpgradeLicense(
  984. IN BYTE FAR * pbMessage,
  985. IN DWORD cbMessage,
  986. OUT PHydra_Server_Upgrade_License pCanonical
  987. )
  988. {
  989. //Call UnpackHydraServerNewLicense as both the messages are same
  990. LS_BEGIN(TEXT("UnpackHydraServerUpgradeLicense\n"));
  991. return UnPackHydraServerNewLicense(pbMessage, cbMessage, pCanonical);
  992. }
  993. #if 0
  994. //
  995. // moved to cryptkey.c
  996. //
  997. /****************************************************************************************
  998. * Function : UnpackHydraServerCertificate
  999. * Purpose : To unpack a binary blob into a Hydra_Server_Cert structure.
  1000. * Note : The caller should initialize the output pointer. All the necessary
  1001. * allocation for different structure components is done by the function itself.
  1002. * The caller should free all the memory components once it is no longer needed.
  1003. * Return : License_Status
  1004. *****************************************************************************************/
  1005. LICENSE_STATUS
  1006. UnpackHydraServerCertificate(
  1007. IN BYTE FAR * pbMessage,
  1008. IN DWORD cbMessage,
  1009. OUT PHydra_Server_Cert pCanonical
  1010. )
  1011. {
  1012. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  1013. BYTE FAR * pbTemp = NULL;
  1014. DWORD dwTemp = 0;
  1015. LS_BEGIN(TEXT("UnpackHydraServerCertificate\n"));
  1016. if( (pbMessage == NULL) || (pCanonical == NULL ) )
  1017. {
  1018. INVALID_INPUT_RETURN;
  1019. }
  1020. dwTemp = 3*sizeof(DWORD) + 4*sizeof(WORD);
  1021. if(dwTemp > cbMessage)
  1022. {
  1023. INVALID_INPUT_RETURN;
  1024. }
  1025. memset(pCanonical, 0x00, sizeof(Hydra_Server_Cert));
  1026. LS_DUMPSTRING(cbMessage, pbMessage);
  1027. pbTemp = pbMessage;
  1028. dwTemp = cbMessage;
  1029. //Assign dwVersion
  1030. pCanonical->dwVersion = *( UNALIGNED DWORD* )pbTemp;
  1031. pbTemp += sizeof(DWORD);
  1032. dwTemp -= sizeof(DWORD);
  1033. //Assign dwSigAlgID
  1034. pCanonical->dwSigAlgID = *( UNALIGNED DWORD* )pbTemp;
  1035. pbTemp += sizeof(DWORD);
  1036. dwTemp -= sizeof(DWORD);
  1037. //Assign dwSignID
  1038. pCanonical->dwKeyAlgID = *( UNALIGNED DWORD* )pbTemp;
  1039. pbTemp += sizeof(DWORD);
  1040. dwTemp -= sizeof(DWORD);
  1041. //Assign PublicKeyData
  1042. pCanonical->PublicKeyData.wBlobType = *( UNALIGNED WORD* )pbTemp;
  1043. pbTemp += sizeof(WORD);
  1044. dwTemp -= sizeof(WORD);
  1045. if( pCanonical->PublicKeyData.wBlobType != BB_RSA_KEY_BLOB )
  1046. {
  1047. INVALID_INPUT_RETURN;
  1048. }
  1049. pCanonical->PublicKeyData.wBlobLen = *( UNALIGNED WORD* )pbTemp;
  1050. pbTemp += sizeof(WORD);
  1051. dwTemp -= sizeof(WORD);
  1052. if(pCanonical->PublicKeyData.wBlobLen >0)
  1053. {
  1054. if( NULL ==(pCanonical->PublicKeyData.pBlob = (BYTE FAR *)malloc(pCanonical->PublicKeyData.wBlobLen)) )
  1055. {
  1056. pCanonical->PublicKeyData.wBlobLen = 0;
  1057. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  1058. LS_LOG_RESULT(lsReturn);
  1059. goto ErrorReturn;
  1060. }
  1061. memset(pCanonical->PublicKeyData.pBlob, 0x00, pCanonical->PublicKeyData.wBlobLen);
  1062. memcpy(pCanonical->PublicKeyData.pBlob, pbTemp, pCanonical->PublicKeyData.wBlobLen);
  1063. pbTemp += pCanonical->PublicKeyData.wBlobLen;
  1064. dwTemp -= pCanonical->PublicKeyData.wBlobLen;
  1065. }
  1066. //Assign SignatureBlob
  1067. pCanonical->SignatureBlob.wBlobType = *( UNALIGNED WORD* )pbTemp;
  1068. pbTemp += sizeof(WORD);
  1069. dwTemp -= sizeof(WORD);
  1070. if( pCanonical->SignatureBlob.wBlobType != BB_RSA_SIGNATURE_BLOB )
  1071. {
  1072. INVALID_INPUT_RETURN;
  1073. }
  1074. pCanonical->SignatureBlob.wBlobLen = *( UNALIGNED WORD* )pbTemp;
  1075. pbTemp += sizeof(WORD);
  1076. dwTemp -= sizeof(WORD);
  1077. if(pCanonical->SignatureBlob.wBlobLen >0)
  1078. {
  1079. if( NULL ==(pCanonical->SignatureBlob.pBlob = (BYTE FAR *)malloc(pCanonical->SignatureBlob.wBlobLen)) )
  1080. {
  1081. pCanonical->SignatureBlob.wBlobLen = 0;
  1082. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  1083. LS_LOG_RESULT(lsReturn);
  1084. goto ErrorReturn;
  1085. }
  1086. memset(pCanonical->SignatureBlob.pBlob, 0x00, pCanonical->SignatureBlob.wBlobLen);
  1087. memcpy(pCanonical->SignatureBlob.pBlob, pbTemp, pCanonical->SignatureBlob.wBlobLen);
  1088. pbTemp += pCanonical->SignatureBlob.wBlobLen;
  1089. dwTemp -= pCanonical->SignatureBlob.wBlobLen;
  1090. }
  1091. LS_LOG_RESULT(lsReturn);
  1092. LS_RETURN(lsReturn);
  1093. ErrorReturn:
  1094. if (pCanonical)
  1095. {
  1096. if(pCanonical->PublicKeyData.pBlob)
  1097. {
  1098. free(pCanonical->PublicKeyData.pBlob);
  1099. pCanonical->PublicKeyData.pBlob = NULL;
  1100. }
  1101. if(pCanonical->SignatureBlob.pBlob)
  1102. {
  1103. free(pCanonical->SignatureBlob.pBlob);
  1104. pCanonical->SignatureBlob.pBlob = NULL;
  1105. }
  1106. memset(pCanonical, 0x00, sizeof(Hydra_Server_Cert));
  1107. }
  1108. LS_RETURN(lsReturn);
  1109. }
  1110. #endif
  1111. /****************************************************************************************
  1112. * Function : UnpackNewLicenseInfo
  1113. * Purpose : To unpack a binary blob into a New_license_Info structure
  1114. * Note : The caller should initialize the output pointer. All the necessary
  1115. * allocation for different structure components is done by the function itself.
  1116. * The caller should free all the memory components once it is no longer needed.
  1117. * Return : License_Status
  1118. *****************************************************************************************/
  1119. LICENSE_STATUS
  1120. UnpackNewLicenseInfo(
  1121. BYTE FAR * pbMessage,
  1122. DWORD cbMessage,
  1123. PNew_License_Info pCanonical
  1124. )
  1125. {
  1126. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  1127. BYTE FAR * pbTemp = NULL;
  1128. DWORD dwTemp = 0, dw = 0;
  1129. LS_BEGIN(TEXT("UnpackNewLicenseInfo\n"));
  1130. //Check for the validity of the inputs
  1131. if( (pbMessage == NULL) || (pCanonical == 0) )
  1132. {
  1133. INVALID_INPUT_RETURN;
  1134. }
  1135. dwTemp = 5*sizeof(DWORD);
  1136. if(dwTemp > cbMessage)
  1137. {
  1138. INVALID_INPUT_RETURN;
  1139. }
  1140. memset(pCanonical, 0x00, sizeof(New_License_Info));
  1141. LS_DUMPSTRING(cbMessage, pbMessage);
  1142. dwTemp = cbMessage;
  1143. pbTemp = pbMessage;
  1144. //Assign version
  1145. pCanonical->dwVersion = *( UNALIGNED DWORD* )pbTemp;
  1146. pbTemp += sizeof(DWORD);
  1147. dwTemp -= sizeof(DWORD);
  1148. //Assign Scope Data
  1149. pCanonical->cbScope = *( UNALIGNED DWORD* )pbTemp;
  1150. pbTemp += sizeof(DWORD);
  1151. dwTemp -= sizeof(DWORD);
  1152. dw = pCanonical->cbScope + 3*sizeof(DWORD);
  1153. if( dw>dwTemp )
  1154. {
  1155. INVALID_INPUT_RETURN;
  1156. }
  1157. if( pCanonical->cbScope>0 )
  1158. {
  1159. if( NULL == (pCanonical->pbScope = (BYTE FAR *)malloc(pCanonical->cbScope)) )
  1160. {
  1161. pCanonical->cbScope = 0;
  1162. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  1163. LS_LOG_RESULT(lsReturn);
  1164. goto ErrorReturn;
  1165. }
  1166. memset(pCanonical->pbScope, 0x00, pCanonical->cbScope);
  1167. memcpy(pCanonical->pbScope, pbTemp, pCanonical->cbScope);
  1168. pbTemp += pCanonical->cbScope;
  1169. dwTemp -= pCanonical->cbScope;
  1170. }
  1171. //Assign CompanyName Data
  1172. pCanonical->cbCompanyName = *( UNALIGNED DWORD* )pbTemp;
  1173. pbTemp += sizeof(DWORD);
  1174. dwTemp -= sizeof(DWORD);
  1175. dw = pCanonical->cbCompanyName + 2*sizeof(DWORD);
  1176. if( dw>dwTemp)
  1177. {
  1178. INVALID_INPUT_RETURN;
  1179. }
  1180. if( pCanonical->cbCompanyName>0 )
  1181. {
  1182. if( NULL == (pCanonical->pbCompanyName = (BYTE FAR *)malloc(pCanonical->cbCompanyName)) )
  1183. {
  1184. pCanonical->cbCompanyName = 0;
  1185. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  1186. LS_LOG_RESULT(lsReturn);
  1187. goto ErrorReturn;
  1188. }
  1189. memset(pCanonical->pbCompanyName, 0x00, pCanonical->cbCompanyName);
  1190. memcpy(pCanonical->pbCompanyName, pbTemp, pCanonical->cbCompanyName);
  1191. pbTemp += pCanonical->cbCompanyName;
  1192. dwTemp -= pCanonical->cbCompanyName;
  1193. }
  1194. //Assign ProductID data
  1195. pCanonical->cbProductID = *( UNALIGNED DWORD* )pbTemp;
  1196. pbTemp += sizeof(DWORD);
  1197. dwTemp -= sizeof(DWORD);
  1198. dw = pCanonical->cbProductID + sizeof(DWORD);
  1199. if(dw>dwTemp)
  1200. {
  1201. INVALID_INPUT_RETURN;
  1202. }
  1203. if( pCanonical->cbProductID>0 )
  1204. {
  1205. if( NULL == (pCanonical->pbProductID = (BYTE FAR *)malloc(pCanonical->cbProductID)) )
  1206. {
  1207. pCanonical->cbProductID = 0;
  1208. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  1209. goto ErrorReturn;
  1210. }
  1211. memset(pCanonical->pbProductID, 0x00, pCanonical->cbProductID);
  1212. memcpy(pCanonical->pbProductID, pbTemp, pCanonical->cbProductID);
  1213. pbTemp += pCanonical->cbProductID;
  1214. dwTemp -= pCanonical->cbProductID;
  1215. }
  1216. //Assign LicenseInfo data
  1217. pCanonical->cbLicenseInfo = *( UNALIGNED DWORD* )pbTemp;
  1218. pbTemp += sizeof(DWORD);
  1219. dwTemp -= sizeof(DWORD);
  1220. dw = pCanonical->cbLicenseInfo;
  1221. if( dw>dwTemp )
  1222. {
  1223. INVALID_INPUT_RETURN;
  1224. }
  1225. if( pCanonical->cbLicenseInfo>0 )
  1226. {
  1227. if( NULL == (pCanonical->pbLicenseInfo = (BYTE FAR *)malloc(pCanonical->cbLicenseInfo)) )
  1228. {
  1229. pCanonical->cbLicenseInfo = 0;
  1230. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  1231. LS_LOG_RESULT(lsReturn);
  1232. goto ErrorReturn;
  1233. }
  1234. memset(pCanonical->pbLicenseInfo, 0x00, pCanonical->cbLicenseInfo);
  1235. memcpy(pCanonical->pbLicenseInfo, pbTemp, pCanonical->cbLicenseInfo);
  1236. pbTemp += pCanonical->cbLicenseInfo;
  1237. dwTemp -= pCanonical->cbLicenseInfo;
  1238. }
  1239. LS_LOG_RESULT(lsReturn);
  1240. LS_RETURN(lsReturn);
  1241. ErrorReturn:
  1242. if (pCanonical)
  1243. {
  1244. if(pCanonical->pbScope)
  1245. {
  1246. free(pCanonical->pbScope);
  1247. pCanonical->pbScope = NULL;
  1248. }
  1249. if(pCanonical->pbCompanyName)
  1250. {
  1251. free(pCanonical->pbCompanyName);
  1252. pCanonical->pbCompanyName = NULL;
  1253. }
  1254. if(pCanonical->pbProductID)
  1255. {
  1256. free(pCanonical->pbProductID);
  1257. pCanonical->pbProductID = NULL;
  1258. }
  1259. if(pCanonical->pbLicenseInfo)
  1260. {
  1261. free(pCanonical->pbLicenseInfo);
  1262. pCanonical->pbLicenseInfo = NULL;
  1263. }
  1264. }
  1265. LS_RETURN(lsReturn);
  1266. }
  1267. ///////////////////////////////////////////////////////////////////////////////
  1268. LICENSE_STATUS
  1269. UnPackExtendedErrorInfo(
  1270. UINT32 *puiExtendedErrorInfo,
  1271. Binary_Blob *pbbErrorInfo)
  1272. {
  1273. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  1274. BYTE FAR * pbTemp = NULL;
  1275. DWORD dwTemp = 0;
  1276. WORD wVersion;
  1277. LS_BEGIN(TEXT("UnpackExtendedErrorInfo\n"));
  1278. //Check for the validity of the inputs
  1279. if( (puiExtendedErrorInfo == NULL) || (pbbErrorInfo == NULL) )
  1280. {
  1281. INVALID_INPUT_RETURN;
  1282. }
  1283. dwTemp = sizeof(WORD) + sizeof(WORD) + sizeof(UINT32);
  1284. if(dwTemp > pbbErrorInfo->wBlobLen)
  1285. {
  1286. INVALID_INPUT_RETURN;
  1287. }
  1288. pbTemp = pbbErrorInfo->pBlob;
  1289. wVersion = *(UNALIGNED WORD*)pbTemp;
  1290. pbTemp += sizeof(WORD);
  1291. if (wVersion < BB_ERROR_BLOB_VERSION)
  1292. {
  1293. //
  1294. // Old version
  1295. //
  1296. INVALID_INPUT_RETURN;
  1297. }
  1298. //
  1299. // skip reserved field
  1300. //
  1301. pbTemp += sizeof(WORD);
  1302. *puiExtendedErrorInfo = *(UNALIGNED UINT32*)pbTemp;
  1303. LS_LOG_RESULT(lsReturn);
  1304. LS_RETURN(lsReturn);
  1305. ErrorReturn:
  1306. LS_RETURN(lsReturn);
  1307. }