Source code of Windows XP (NT5)
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.

988 lines
25 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1999 Microsoft Corporation
  4. //
  5. // File:
  6. //
  7. // Contents:
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #include <windows.h>
  13. #include <wincrypt.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <tchar.h>
  17. #include "licekpak.h"
  18. #define AllocateMemory(size) LocalAlloc(LPTR, size)
  19. #define FreeMemory(ptr) LocalFree(ptr)
  20. #define ENCRYPTCONTAINER _TEXT("Encrypt")
  21. #define DECRYPTCONTAINER _TEXT("Decrypt")
  22. ///////////////////////////////////////////////////////////////////////////////
  23. // The keypack serial number
  24. //
  25. LPTSTR pszPID = _TEXT("This is a test");
  26. GUID g_KeypackSN = { 0xdedaa678, 0xb83c, 0x11d1, { 0x9c, 0xb3, 0x00, 0xc0, 0x4f, 0xb1, 0x6e, 0x75 } };
  27. DWORD
  28. InitializeKeyPackInfo(
  29. PLicense_KeyPack pLicenseKeyPack );
  30. void
  31. FreeLicenseKeyPack(
  32. PLicense_KeyPack pLicenseKeyPack );
  33. void
  34. PrintKeyPackInfo(
  35. PLicense_KeyPack pLicenseKeyPack );
  36. DWORD
  37. EncodeKeyPack(
  38. HCRYPTPROV hCrypt,
  39. PLicense_KeyPack pLicenseKeyPack,
  40. PBYTE pbLSCert,
  41. DWORD cbLSCert,
  42. PDWORD pcbEncodedBlob,
  43. PBYTE * ppbEncodedBlob );
  44. DWORD
  45. DecodeKeyPack(
  46. HCRYPTPROV hCryptProv,
  47. PLicense_KeyPack pLicenseKeyPack,
  48. DWORD cbEncodedBlob,
  49. PBYTE pbEncodedBlob,
  50. char * pszCHCertFile,
  51. char * pszLSKeyContainer );
  52. DWORD
  53. GetCertificate(
  54. char * szCertFile,
  55. PBYTE * ppCert,
  56. PDWORD pcbCert );
  57. ///////////////////////////////////////////////////////////////////////////////
  58. DWORD
  59. TLSSaveCertAsPKCS7(
  60. HCRYPTPROV hCryptProv,
  61. PBYTE pbCert,
  62. DWORD cbCert,
  63. PBYTE* ppbEncodedCert,
  64. PDWORD pcbEncodedCert
  65. )
  66. /*
  67. */
  68. {
  69. DWORD dwStatus=ERROR_SUCCESS;
  70. HCERTSTORE hStore=NULL;
  71. PCCERT_CONTEXT pCertContext=NULL;
  72. CRYPT_DATA_BLOB saveBlob;
  73. do {
  74. hStore=CertOpenStore(
  75. CERT_STORE_PROV_MEMORY,
  76. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  77. hCryptProv,
  78. CERT_STORE_NO_CRYPT_RELEASE_FLAG,
  79. NULL
  80. );
  81. if(!hStore)
  82. {
  83. dwStatus = GetLastError();
  84. break;
  85. }
  86. pCertContext = CertCreateCertificateContext(
  87. X509_ASN_ENCODING,
  88. pbCert,
  89. cbCert
  90. );
  91. if(!pCertContext)
  92. {
  93. dwStatus = GetLastError();
  94. break;
  95. }
  96. //
  97. // always start from empty so CERT_STORE_ADD_ALWAYS
  98. if(!CertAddCertificateContextToStore(
  99. hStore,
  100. pCertContext,
  101. CERT_STORE_ADD_ALWAYS,
  102. NULL
  103. ))
  104. {
  105. dwStatus = GetLastError();
  106. break;
  107. }
  108. memset(&saveBlob, 0, sizeof(saveBlob));
  109. // save certificate into memory
  110. if(!CertSaveStore(hStore,
  111. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  112. CERT_STORE_SAVE_AS_PKCS7,
  113. CERT_STORE_SAVE_TO_MEMORY,
  114. &saveBlob,
  115. 0) && (dwStatus=GetLastError()) != ERROR_MORE_DATA)
  116. {
  117. dwStatus = GetLastError();
  118. break;
  119. }
  120. if(!(saveBlob.pbData = (PBYTE)AllocateMemory(saveBlob.cbData)))
  121. {
  122. dwStatus=GetLastError();
  123. break;
  124. }
  125. // save certificate into memory
  126. if(!CertSaveStore(hStore,
  127. X509_ASN_ENCODING | PKCS_7_ASN_ENCODING,
  128. CERT_STORE_SAVE_AS_PKCS7,
  129. CERT_STORE_SAVE_TO_MEMORY,
  130. &saveBlob,
  131. 0))
  132. {
  133. dwStatus = GetLastError();
  134. break;
  135. }
  136. *ppbEncodedCert = saveBlob.pbData;
  137. *pcbEncodedCert = saveBlob.cbData;
  138. } while(FALSE);
  139. if(pCertContext)
  140. {
  141. CertFreeCertificateContext(pCertContext);
  142. }
  143. if(hStore)
  144. {
  145. CertCloseStore(hStore, CERT_CLOSE_STORE_FORCE_FLAG);
  146. }
  147. return dwStatus;
  148. }
  149. /////////////////////////////////////////////////////////////////////
  150. HCRYPTPROV
  151. CryptoInit(
  152. LPTSTR szContainer
  153. )
  154. /*++
  155. --*/
  156. {
  157. HCRYPTPROV hProv;
  158. HCRYPTKEY hKey;
  159. // Attempt to acquire a handle to the default key container.
  160. if(!CryptAcquireContext(&hProv, szContainer, NULL, PROV_RSA_FULL, CRYPT_MACHINE_KEYSET))
  161. {
  162. // Create default key container.
  163. if(!CryptAcquireContext(&hProv, szContainer, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET | CRYPT_MACHINE_KEYSET))
  164. {
  165. return NULL;
  166. }
  167. }
  168. // Attempt to get handle to signature key.
  169. if(!CryptGetUserKey(hProv, AT_SIGNATURE, &hKey))
  170. {
  171. if(GetLastError() == NTE_NO_KEY && !CryptGenKey(hProv,AT_SIGNATURE,0,&hKey))
  172. return NULL;
  173. }
  174. CryptDestroyKey(hKey);
  175. // Attempt to get handle to exchange key.
  176. if(!CryptGetUserKey(hProv, AT_KEYEXCHANGE, &hKey))
  177. {
  178. if(GetLastError() == NTE_NO_KEY && !CryptGenKey(hProv, AT_KEYEXCHANGE, 0, &hKey))
  179. return NULL;
  180. }
  181. CryptDestroyKey(hKey);
  182. return hProv;
  183. }
  184. ///////////////////////////////////////////////////////////////////////////////
  185. const DWORD dwCertRdnValueType = CERT_RDN_PRINTABLE_STRING;
  186. /////////////////////////////////////////////////////////////////////
  187. DWORD
  188. TLSExportPublicKey(
  189. IN HCRYPTPROV hCryptProv,
  190. IN DWORD dwKeyType,
  191. IN OUT PDWORD pcbByte,
  192. IN OUT PCERT_PUBLIC_KEY_INFO *ppbByte
  193. )
  194. /*
  195. */
  196. {
  197. BOOL bRetCode=TRUE;
  198. *pcbByte=0;
  199. *ppbByte=NULL;
  200. bRetCode = CryptExportPublicKeyInfo(
  201. hCryptProv,
  202. dwKeyType,
  203. X509_ASN_ENCODING,
  204. NULL,
  205. pcbByte);
  206. if(bRetCode == FALSE)
  207. goto cleanup;
  208. if((*ppbByte=(PCERT_PUBLIC_KEY_INFO)AllocateMemory(*pcbByte)) == NULL)
  209. goto cleanup;
  210. bRetCode = CryptExportPublicKeyInfo(
  211. hCryptProv,
  212. dwKeyType,
  213. X509_ASN_ENCODING,
  214. *ppbByte,
  215. pcbByte);
  216. if(bRetCode == FALSE)
  217. {
  218. FreeMemory(*ppbByte);
  219. *pcbByte = 0;
  220. }
  221. cleanup:
  222. return (bRetCode) ? ERROR_SUCCESS : GetLastError();
  223. }
  224. /////////////////////////////////////////////////////////////////////
  225. DWORD
  226. TLSCryptEncodeObject(
  227. IN DWORD dwEncodingType,
  228. IN LPCSTR lpszStructType,
  229. IN const void * pvStructInfo,
  230. OUT PBYTE* ppbEncoded,
  231. OUT DWORD* pcbEncoded
  232. )
  233. /*
  234. Description:
  235. Allocate memory and encode object, wrapper for CryptEncodeObject()
  236. */
  237. {
  238. DWORD dwStatus = ERROR_SUCCESS;
  239. if(!CryptEncodeObject(dwEncodingType, lpszStructType, pvStructInfo, NULL, pcbEncoded) ||
  240. (*ppbEncoded=(PBYTE)AllocateMemory(*pcbEncoded)) == NULL ||
  241. !CryptEncodeObject(dwEncodingType, lpszStructType, pvStructInfo, *ppbEncoded, pcbEncoded))
  242. {
  243. dwStatus=GetLastError();
  244. }
  245. return dwStatus;
  246. }
  247. /////////////////////////////////////////////////////////////////////
  248. DWORD
  249. TLSCryptSignAndEncodeCertificate(
  250. IN HCRYPTPROV hCryptProv,
  251. IN DWORD dwKeySpec,
  252. IN PCERT_INFO pCertInfo,
  253. IN PCRYPT_ALGORITHM_IDENTIFIER pSignatureAlgorithm,
  254. IN OUT PBYTE* ppbEncodedCert,
  255. IN OUT PDWORD pcbEncodedCert
  256. )
  257. /*
  258. */
  259. {
  260. BOOL bRetCode;
  261. bRetCode = CryptSignAndEncodeCertificate(
  262. hCryptProv,
  263. dwKeySpec,
  264. X509_ASN_ENCODING,
  265. X509_CERT_TO_BE_SIGNED,
  266. pCertInfo,
  267. pSignatureAlgorithm,
  268. NULL,
  269. NULL,
  270. pcbEncodedCert);
  271. if(bRetCode == FALSE && GetLastError() != ERROR_MORE_DATA)
  272. goto cleanup;
  273. *ppbEncodedCert=(PBYTE)AllocateMemory(*pcbEncodedCert);
  274. if(*ppbEncodedCert == FALSE)
  275. goto cleanup;
  276. bRetCode = CryptSignAndEncodeCertificate(
  277. hCryptProv,
  278. AT_SIGNATURE,
  279. X509_ASN_ENCODING,
  280. X509_CERT_TO_BE_SIGNED,
  281. pCertInfo,
  282. pSignatureAlgorithm,
  283. NULL,
  284. *ppbEncodedCert,
  285. pcbEncodedCert);
  286. if(bRetCode == FALSE)
  287. {
  288. FreeMemory(*ppbEncodedCert);
  289. *pcbEncodedCert = 0;
  290. }
  291. cleanup:
  292. return (bRetCode) ? ERROR_SUCCESS : GetLastError();
  293. }
  294. /////////////////////////////////////////////////////////////////////
  295. DWORD
  296. CreateSelfSignCertificate(
  297. HCRYPTPROV hCryptProv,
  298. LPTSTR pszIssuer,
  299. DWORD dwKeySpec,
  300. PBYTE* ppbCert,
  301. PDWORD pcbCert
  302. )
  303. /*++
  304. --*/
  305. {
  306. DWORD dwStatus=ERROR_SUCCESS;
  307. CRYPT_ALGORITHM_IDENTIFIER SignatureAlgorithm={ szOID_OIWSEC_sha1RSASign, 0, 0 };
  308. CERT_INFO CertInfo;
  309. PCERT_PUBLIC_KEY_INFO pbPublicKeyInfo=NULL;
  310. DWORD cbPublicKeyInfo=0;
  311. DWORD serialnumber;
  312. CERT_RDN_ATTR rgNameAttr[] = {
  313. {
  314. szOID_COMMON_NAME,
  315. dwCertRdnValueType,
  316. _tcslen(pszIssuer) * sizeof(TCHAR),
  317. (UCHAR *)pszIssuer
  318. }
  319. };
  320. CERT_RDN rgRDN[] = { sizeof(rgNameAttr)/sizeof(rgNameAttr[0]), &rgNameAttr[0] };
  321. CERT_NAME_INFO Name = {1, rgRDN};
  322. memset(&CertInfo, 0, sizeof(CERT_INFO));
  323. CertInfo.dwVersion = CERT_V3;
  324. CertInfo.SerialNumber.cbData = sizeof(DWORD);
  325. CertInfo.SerialNumber.pbData = (PBYTE)&serialnumber;
  326. CertInfo.SignatureAlgorithm = SignatureAlgorithm;
  327. TLSCryptEncodeObject(
  328. CRYPT_ASN_ENCODING,
  329. X509_NAME,
  330. &Name,
  331. &(CertInfo.Issuer.pbData),
  332. &(CertInfo.Issuer.cbData)
  333. );
  334. GetSystemTimeAsFileTime(&CertInfo.NotBefore);
  335. GetSystemTimeAsFileTime(&CertInfo.NotAfter);
  336. CertInfo.Subject.pbData = CertInfo.Issuer.pbData;
  337. CertInfo.Subject.cbData = CertInfo.Issuer.cbData;
  338. dwStatus = TLSExportPublicKey(
  339. hCryptProv,
  340. dwKeySpec,
  341. &cbPublicKeyInfo,
  342. &pbPublicKeyInfo
  343. );
  344. if(dwStatus != ERROR_SUCCESS)
  345. {
  346. goto cleanup;
  347. }
  348. CertInfo.SubjectPublicKeyInfo = *pbPublicKeyInfo;
  349. dwStatus = TLSCryptSignAndEncodeCertificate(
  350. hCryptProv,
  351. dwKeySpec,
  352. &CertInfo,
  353. &SignatureAlgorithm,
  354. ppbCert,
  355. pcbCert
  356. );
  357. cleanup:
  358. return dwStatus;
  359. }
  360. ///////////////////////////////////////////////////////////////////////////////
  361. void _cdecl main(int argc, char *argv[])
  362. {
  363. License_KeyPack LicenseKeyPack;
  364. License_KeyPack DecodedKeyPack;
  365. PBYTE pbEncodedBlob;
  366. DWORD cbEncodedBlob;
  367. DWORD dwRetCode;
  368. HCRYPTPROV hTest;
  369. PBYTE pbEncryptCert;
  370. DWORD cbEncryptCert;
  371. PBYTE pbDecryptCert;
  372. DWORD cbDecryptCert;
  373. PBYTE pbEncryptCertPKCS7;
  374. DWORD cbEncryptCertPKCS7;
  375. PBYTE pbDecryptCertPKCS7;
  376. DWORD cbDecryptCertPKCS7;
  377. HCRYPTPROV hEncrypt;
  378. HCRYPTPROV hDecrypt;
  379. memset(&LicenseKeyPack, 0, sizeof(LicenseKeyPack));
  380. memset(&DecodedKeyPack, 0, sizeof(DecodedKeyPack));
  381. hTest = CryptoInit(_TEXT("Test"));
  382. if(hTest == NULL)
  383. {
  384. exit(0);
  385. }
  386. //
  387. // Initialize two crypto provider
  388. //
  389. hEncrypt = CryptoInit(ENCRYPTCONTAINER);
  390. if(hEncrypt == NULL)
  391. {
  392. exit(0);
  393. }
  394. hDecrypt = CryptoInit(DECRYPTCONTAINER);
  395. if(hDecrypt == NULL)
  396. {
  397. exit(0);
  398. }
  399. //
  400. // Generate two test certificate
  401. //
  402. if(CreateSelfSignCertificate(
  403. hEncrypt,
  404. ENCRYPTCONTAINER,
  405. AT_KEYEXCHANGE,
  406. &pbEncryptCert,
  407. &cbEncryptCert
  408. ) != ERROR_SUCCESS)
  409. {
  410. exit(0);
  411. }
  412. //
  413. // Generate two test certificate
  414. //
  415. if(CreateSelfSignCertificate(
  416. hDecrypt,
  417. DECRYPTCONTAINER,
  418. AT_KEYEXCHANGE,
  419. &pbDecryptCert,
  420. &cbDecryptCert
  421. ) != ERROR_SUCCESS)
  422. {
  423. exit(0);
  424. }
  425. TLSSaveCertAsPKCS7(
  426. hEncrypt,
  427. pbEncryptCert,
  428. cbEncryptCert,
  429. &pbEncryptCertPKCS7,
  430. &cbEncryptCertPKCS7
  431. );
  432. TLSSaveCertAsPKCS7(
  433. hDecrypt,
  434. pbDecryptCert,
  435. cbDecryptCert,
  436. &pbDecryptCertPKCS7,
  437. &cbDecryptCertPKCS7
  438. );
  439. //
  440. // initialize the license key pack with the information we want to
  441. // encode
  442. //
  443. memset( &LicenseKeyPack , 0, sizeof( License_KeyPack ) );
  444. dwRetCode = InitializeKeyPackInfo( &LicenseKeyPack );
  445. if( ERROR_SUCCESS != dwRetCode )
  446. {
  447. goto done;
  448. }
  449. //
  450. // encode the license key pack.
  451. //
  452. dwRetCode = EncodeKeyPack(
  453. hTest,
  454. &LicenseKeyPack,
  455. pbDecryptCertPKCS7,
  456. cbDecryptCertPKCS7,
  457. &cbEncodedBlob,
  458. &pbEncodedBlob
  459. );
  460. if( ERROR_SUCCESS != dwRetCode )
  461. {
  462. printf( "cannot encode key pack: %d\n", dwRetCode );
  463. goto done;
  464. }
  465. memset( &DecodedKeyPack, 0, sizeof( License_KeyPack ) );
  466. if( ERROR_SUCCESS != dwRetCode )
  467. {
  468. goto done;
  469. }
  470. //
  471. // decode the license key pack
  472. //
  473. dwRetCode = DecodeKeyPack(
  474. hDecrypt,
  475. &DecodedKeyPack,
  476. cbEncodedBlob,
  477. pbEncodedBlob,
  478. pbDecryptCertPKCS7,
  479. cbDecryptCertPKCS7
  480. );
  481. if( ERROR_SUCCESS != dwRetCode )
  482. {
  483. printf( "cannot encode key pack: %d\n", dwRetCode );
  484. goto done;
  485. }
  486. //
  487. // print the information in the decoded license key pack
  488. //
  489. PrintKeyPackInfo( &DecodedKeyPack );
  490. done:
  491. FreeLicenseKeyPack( &LicenseKeyPack );
  492. FreeLicenseKeyPack( &DecodedKeyPack );
  493. if( pbEncodedBlob )
  494. {
  495. LocalFree( pbEncodedBlob );
  496. }
  497. return;
  498. Usage:
  499. printf( "Usage: \n" );
  500. printf( "LS_Certificate_File, LS_KeyContainer_Name, CH_Certificate_File, CH_KeyContainer_Name\n" );
  501. return;
  502. }
  503. #define KEYPACK_DESCRIPTION_1 L"Joe's BlockBuster Software"
  504. #define KEYPACK_DESCRIPTION_2 L"Claire's Garage Bestseller"
  505. #define KEYPACK_PRODUCTNAME_1 L"Joe and Claire's Most Excellent Compiler"
  506. #define KEYPACK_PRODUCTNAME_2 L"NetWise Surfer"
  507. #define MANUFACTURER L"Joe and Claire"
  508. #define MANUFACTURER_DATA L"2356 Elm Street, Seattle, WA"
  509. #define PRODUCT_ID L"1234"
  510. ///////////////////////////////////////////////////////////////////////////////
  511. DWORD
  512. InitializeKeyPackInfo(
  513. PLicense_KeyPack pLicenseKeyPack )
  514. {
  515. SYSTEMTIME SystemTime;
  516. PKeyPack_Description pKpDesc;
  517. pLicenseKeyPack->dwVersion = LICENSE_KEYPACK_VERSION_1_0;
  518. pLicenseKeyPack->dwKeypackType = LICENSE_KEYPACK_TYPE_MOLP;
  519. pLicenseKeyPack->dwDistChannel = LICENSE_DISTRIBUTION_CHANNEL_OEM;
  520. memcpy( &pLicenseKeyPack->KeypackSerialNum, &g_KeypackSN, sizeof( GUID ) );
  521. //
  522. // setting all the time to the current time
  523. //
  524. GetSystemTime( &SystemTime );
  525. SystemTimeToFileTime( &SystemTime, &pLicenseKeyPack->IssueDate );
  526. pLicenseKeyPack->ActiveDate = pLicenseKeyPack->IssueDate;
  527. pLicenseKeyPack->ExpireDate = pLicenseKeyPack->IssueDate;
  528. pLicenseKeyPack->dwBeginSerialNum = 9999;
  529. pLicenseKeyPack->dwQuantity = 799;
  530. pLicenseKeyPack->cbProductId = ( wcslen( PRODUCT_ID ) + 1 ) * sizeof( WCHAR );
  531. pLicenseKeyPack->pbProductId = LocalAlloc( GPTR, pLicenseKeyPack->cbProductId );
  532. memcpy( pLicenseKeyPack->pbProductId, PRODUCT_ID, pLicenseKeyPack->cbProductId );
  533. pLicenseKeyPack->dwProductVersion = 0x0000FFFF;
  534. pLicenseKeyPack->dwPlatformId = 0x0000FFFF;
  535. pLicenseKeyPack->dwLicenseType = 0xBEEFFACE;
  536. pLicenseKeyPack->dwDescriptionCount = 2;
  537. pLicenseKeyPack->pDescription = LocalAlloc( GPTR,
  538. 2 * sizeof( KeyPack_Description ) );
  539. if( NULL == pLicenseKeyPack->pDescription )
  540. {
  541. return( GetLastError() );
  542. }
  543. //
  544. // fill in the keypack descriptions
  545. //
  546. pKpDesc = pLicenseKeyPack->pDescription;
  547. pKpDesc->Locale = GetSystemDefaultLCID();
  548. pKpDesc->cbProductName = ( wcslen( KEYPACK_PRODUCTNAME_1 ) + 1 ) * sizeof( WCHAR );
  549. pKpDesc->pbProductName = LocalAlloc( GPTR, pKpDesc->cbProductName );
  550. if( NULL == pKpDesc->pbProductName )
  551. {
  552. return( GetLastError() );
  553. }
  554. memcpy( pKpDesc->pbProductName, KEYPACK_PRODUCTNAME_1, pKpDesc->cbProductName );
  555. pKpDesc->cbDescription = ( wcslen( KEYPACK_DESCRIPTION_1 ) + 1 ) * sizeof( WCHAR );
  556. pKpDesc->pDescription = LocalAlloc( GPTR, pKpDesc->cbDescription );
  557. if( NULL == pKpDesc->pDescription )
  558. {
  559. return( GetLastError() );
  560. }
  561. memcpy( pKpDesc->pDescription, KEYPACK_DESCRIPTION_1, pKpDesc->cbDescription );
  562. pKpDesc++;
  563. pKpDesc->Locale = GetSystemDefaultLCID();
  564. pKpDesc->cbProductName = ( wcslen( KEYPACK_PRODUCTNAME_2 ) + 1 ) * sizeof( WCHAR );
  565. pKpDesc->pbProductName = LocalAlloc( GPTR, pKpDesc->cbProductName );
  566. if( NULL == pKpDesc->pbProductName )
  567. {
  568. return( GetLastError() );
  569. }
  570. memcpy( pKpDesc->pbProductName, KEYPACK_PRODUCTNAME_2, pKpDesc->cbProductName );
  571. pKpDesc->cbDescription = ( wcslen( KEYPACK_DESCRIPTION_2 ) + 1 ) * sizeof( WCHAR );
  572. pKpDesc->pDescription = LocalAlloc( GPTR, pKpDesc->cbDescription );
  573. if( NULL == pKpDesc->pDescription )
  574. {
  575. return( GetLastError() );
  576. }
  577. memcpy( pKpDesc->pDescription, KEYPACK_DESCRIPTION_2, pKpDesc->cbDescription );
  578. pLicenseKeyPack->cbManufacturer = ( wcslen( MANUFACTURER ) + 1 ) * sizeof( WCHAR );
  579. pLicenseKeyPack->pbManufacturer = LocalAlloc( GPTR, pLicenseKeyPack->cbManufacturer );
  580. if( NULL == pLicenseKeyPack->pbManufacturer )
  581. {
  582. return( GetLastError() );
  583. }
  584. memcpy( pLicenseKeyPack->pbManufacturer, MANUFACTURER, pLicenseKeyPack->cbManufacturer );
  585. pLicenseKeyPack->cbManufacturerData = ( wcslen( MANUFACTURER_DATA ) + 1 ) * sizeof( WCHAR );
  586. pLicenseKeyPack->pbManufacturerData = LocalAlloc( GPTR, pLicenseKeyPack->cbManufacturerData );
  587. if( NULL == pLicenseKeyPack->pbManufacturerData )
  588. {
  589. return( GetLastError() );
  590. }
  591. memcpy( pLicenseKeyPack->pbManufacturerData, MANUFACTURER_DATA, pLicenseKeyPack->cbManufacturerData );
  592. return( ERROR_SUCCESS );
  593. }
  594. ///////////////////////////////////////////////////////////////////////////////
  595. void
  596. FreeLicenseKeyPack(
  597. PLicense_KeyPack pLicenseKeyPack )
  598. {
  599. DWORD i;
  600. PKeyPack_Description pKpDesc;
  601. if( pLicenseKeyPack->pDescription )
  602. {
  603. for( i = 0, pKpDesc = pLicenseKeyPack->pDescription;
  604. i < pLicenseKeyPack->dwDescriptionCount;
  605. i++, pKpDesc++ )
  606. {
  607. LocalFree( pKpDesc->pDescription );
  608. }
  609. }
  610. LocalFree( pLicenseKeyPack->pDescription );
  611. LocalFree( pLicenseKeyPack->pbManufacturer );
  612. LocalFree( pLicenseKeyPack->pbManufacturerData );
  613. return;
  614. }
  615. ///////////////////////////////////////////////////////////////////////////////
  616. void
  617. PrintKeyPackInfo(
  618. PLicense_KeyPack pLicenseKeyPack )
  619. {
  620. DWORD i;
  621. PKeyPack_Description pKpDesc;
  622. printf( "Keypack version: 0x%x\n", pLicenseKeyPack->dwVersion );
  623. printf( "Keypack type: 0x%x\n", pLicenseKeyPack->dwKeypackType );
  624. printf( "Keypack distribution channel: 0x%x\n", pLicenseKeyPack->dwDistChannel );
  625. printf( "Keypack serial number: 0x%x-0x%x-0x%x-0x%x%x%x%x%x%x%x%x\n",
  626. pLicenseKeyPack->KeypackSerialNum.Data1,
  627. pLicenseKeyPack->KeypackSerialNum.Data2,
  628. pLicenseKeyPack->KeypackSerialNum.Data3,
  629. ( DWORD )pLicenseKeyPack->KeypackSerialNum.Data4[0],
  630. ( DWORD )pLicenseKeyPack->KeypackSerialNum.Data4[1],
  631. ( DWORD )pLicenseKeyPack->KeypackSerialNum.Data4[2],
  632. ( DWORD )pLicenseKeyPack->KeypackSerialNum.Data4[3],
  633. ( DWORD )pLicenseKeyPack->KeypackSerialNum.Data4[4],
  634. ( DWORD )pLicenseKeyPack->KeypackSerialNum.Data4[5],
  635. ( DWORD )pLicenseKeyPack->KeypackSerialNum.Data4[6],
  636. ( DWORD )pLicenseKeyPack->KeypackSerialNum.Data4[7] );
  637. printf( "Keypack Quantity: %d\n", pLicenseKeyPack->dwQuantity );
  638. printf( "Keypack Product Id: %S\n", pLicenseKeyPack->pbProductId );
  639. printf( "Keypack product version: 0x%x\n", pLicenseKeyPack->dwProductVersion );
  640. printf( "Keypack platform Id: 0x%x\n", pLicenseKeyPack->dwPlatformId );
  641. printf( "Keypack license type: 0x%x\n", pLicenseKeyPack->dwLicenseType );
  642. if( pLicenseKeyPack->dwDescriptionCount )
  643. {
  644. for( i = 0, pKpDesc = pLicenseKeyPack->pDescription;
  645. i < pLicenseKeyPack->dwDescriptionCount;
  646. i++, pKpDesc++ )
  647. {
  648. printf( "Keypack Product Name: %S\n", pKpDesc->pbProductName );
  649. printf( "Keypack Description: %S\n", pKpDesc->pDescription );
  650. }
  651. }
  652. printf( "Keypack Manufacturer: %S\n", pLicenseKeyPack->pbManufacturer );
  653. printf( "Keypack Manufacturer data: %S\n", pLicenseKeyPack->pbManufacturerData );
  654. return;
  655. }
  656. ///////////////////////////////////////////////////////////////////////////////
  657. DWORD
  658. EncodeKeyPack(
  659. HCRYPTPROV hCrypt,
  660. PLicense_KeyPack pLicenseKeyPack,
  661. PBYTE pbLSCert,
  662. DWORD cbLSCert,
  663. PDWORD pcbEncodedBlob,
  664. PBYTE * ppbEncodedBlob )
  665. {
  666. DWORD dwRetCode = ERROR_SUCCESS;
  667. //
  668. // encode the license keypack info
  669. //
  670. LicensePackEncodeParm parm;
  671. memset(&parm, 0, sizeof(LicensePackEncodeParm));
  672. parm.dwEncodeType = LICENSE_KEYPACK_ENCRYPT_ALWAYSFRENCH;
  673. parm.hCryptProv = hCrypt;
  674. parm.pbEncryptParm = pbLSCert;
  675. parm.cbEncryptParm = cbLSCert;
  676. dwRetCode = EncodeLicenseKeyPackEx(
  677. pLicenseKeyPack,
  678. &parm,
  679. ppbEncodedBlob,
  680. pcbEncodedBlob
  681. );
  682. done:
  683. return( dwRetCode );
  684. }
  685. ///////////////////////////////////////////////////////////////////////////////
  686. DWORD
  687. DecodeKeyPack(
  688. HCRYPTPROV hCrypt,
  689. PLicense_KeyPack pLicenseKeyPack,
  690. DWORD cbEncodedBlob,
  691. PBYTE pbEncodedBlob,
  692. PBYTE pbCHCert,
  693. DWORD cbCHCert )
  694. {
  695. DWORD dwRetCode = ERROR_SUCCESS;
  696. LicensePackDecodeParm parm;
  697. memset(&parm, 0, sizeof(parm));
  698. parm.hCryptProv = hCrypt;
  699. parm.pbDecryptParm = (PBYTE)pszPID;
  700. parm.cbDecryptParm = (lstrlen(pszPID) + 1) * sizeof(TCHAR);
  701. parm.cbClearingHouseCert = cbCHCert;
  702. parm.pbClearingHouseCert = pbCHCert;
  703. parm.pbRootCertificate = pbCHCert;
  704. parm.cbRootCertificate = cbCHCert;
  705. dwRetCode = DecodeLicenseKeyPackEx(
  706. pLicenseKeyPack,
  707. &parm,
  708. cbEncodedBlob,
  709. pbEncodedBlob
  710. );
  711. done:
  712. return( dwRetCode );
  713. }
  714. ///////////////////////////////////////////////////////////////////////////////
  715. DWORD
  716. GetCertificate(
  717. char * szCertFile,
  718. PBYTE * ppCert,
  719. PDWORD pcbCert )
  720. {
  721. HANDLE hFile = INVALID_HANDLE_VALUE;
  722. DWORD dwRetCode = ERROR_SUCCESS, cbRead = 0;
  723. TCHAR tszCertFile[255];
  724. mbstowcs( tszCertFile, szCertFile, strlen( szCertFile ) + 1 );
  725. hFile = CreateFile( tszCertFile, GENERIC_READ, 0, NULL, OPEN_EXISTING,
  726. FILE_ATTRIBUTE_NORMAL, NULL );
  727. if( INVALID_HANDLE_VALUE == hFile )
  728. {
  729. dwRetCode = GetLastError();
  730. goto done;
  731. }
  732. //
  733. // find out the size of the file
  734. //
  735. *pcbCert = GetFileSize( hFile, NULL );
  736. if( 0xFFFFFFFF == ( *pcbCert ) )
  737. {
  738. dwRetCode = GetLastError();
  739. goto done;
  740. }
  741. //
  742. // allocate memory for reading the file content
  743. //
  744. *ppCert = LocalAlloc( GPTR, *pcbCert );
  745. if( NULL == ( *ppCert ) )
  746. {
  747. dwRetCode = GetLastError();
  748. goto done;
  749. }
  750. //
  751. // read the manufacturer info
  752. //
  753. if( !ReadFile( hFile, *ppCert, *pcbCert, &cbRead, NULL ) )
  754. {
  755. dwRetCode = GetLastError();
  756. }
  757. done:
  758. if( INVALID_HANDLE_VALUE != hFile )
  759. {
  760. CloseHandle( hFile );
  761. }
  762. return( dwRetCode );
  763. }