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.

272 lines
5.6 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1997-1999 Microsoft Corporation
  4. //
  5. // File:
  6. //
  7. // Contents:
  8. //
  9. // History:
  10. //
  11. //---------------------------------------------------------------------------
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include "license.h"
  15. #include "cryptkey.h"
  16. #include "lscsp.h"
  17. #include "licecert.h"
  18. #define SECRET_DATA "I love sushi"
  19. BOOL
  20. GetCspData(
  21. LSCSPINFO CspInfo,
  22. LPBYTE * ppbData,
  23. LPDWORD pcbData );
  24. //+----------------------------------------------------------------------------
  25. int _cdecl main( int argc, char *argv[] )
  26. {
  27. LICENSE_STATUS
  28. Status;
  29. LPBYTE
  30. pbProprietoryCert = NULL,
  31. pbX509Cert = NULL,
  32. pbPrivKey = NULL,
  33. pbX509PrivKey = NULL,
  34. pbX509PubKey = NULL,
  35. pbEnvelopedData = NULL,
  36. pbData = NULL;
  37. DWORD
  38. cbProprietoryCert = 0,
  39. cbX509Cert = 0,
  40. cbPrivKey = 0,
  41. cbX509PrivKey = 0,
  42. cbX509PubKey = 0,
  43. cbEnvelopedData = 0,
  44. cbData = 0;
  45. BYTE
  46. abData[512];
  47. //
  48. // Initialize the CSP library
  49. //
  50. Status = LsCsp_Initialize();
  51. if( LICENSE_STATUS_OK != Status )
  52. {
  53. printf( "Error initializing LSCSP: %x\n", Status );
  54. return 1;
  55. }
  56. //
  57. // Retrieve the proprietory certificate
  58. //
  59. if( !GetCspData( LsCspInfo_Certificate, &pbProprietoryCert, &cbProprietoryCert ) )
  60. {
  61. printf( "Cannot get proprietory certificate\n" );
  62. }
  63. else
  64. {
  65. printf( "Got proprietory certificate\n" );
  66. }
  67. //
  68. // Retrieve the X509 certificate
  69. //
  70. if( !GetCspData( LsCspInfo_X509Certificate, &pbX509Cert, &cbX509Cert ) )
  71. {
  72. printf( "Cannot get X509 certificate\n" );
  73. }
  74. else
  75. {
  76. printf( "Got X509 certificate\n" );
  77. }
  78. //
  79. // retrieve private key for the proprietory certificate
  80. //
  81. if( !GetCspData( LsCspInfo_PrivateKey, &pbPrivKey, &cbPrivKey ) )
  82. {
  83. printf( "Cannot get private key for the proprietory certificate\n");
  84. }
  85. else
  86. {
  87. printf( "Got the private key for the proprietory certificate\n" );
  88. }
  89. //
  90. // retrieve the private key for the X509 certificate
  91. //
  92. if( !GetCspData( LsCspInfo_X509CertPrivateKey, &pbX509PrivKey, &cbX509PrivKey ) )
  93. {
  94. printf( "Cannot get private key for the X509 certificate\n");
  95. }
  96. else
  97. {
  98. printf( "Got the private key for the X509 certificate\n" );
  99. }
  100. //
  101. // validate the X509 certificate and get the public key from the certificate
  102. //
  103. Status = VerifyCertChain( pbX509Cert, cbX509Cert, NULL, &cbX509PubKey );
  104. if( LICENSE_STATUS_INSUFFICIENT_BUFFER == Status )
  105. {
  106. pbX509PubKey = new BYTE[ cbX509PubKey ];
  107. if( NULL != pbX509PubKey )
  108. {
  109. Status = VerifyCertChain( pbX509Cert, cbX509Cert, pbX509PubKey, &cbX509PubKey );
  110. }
  111. }
  112. if( LICENSE_STATUS_OK != Status )
  113. {
  114. printf( "Cannot verify certificate chain\n" );
  115. goto done;
  116. }
  117. //
  118. // Use the public key to encrypt a blob of data
  119. //
  120. Status = LicenseEnvelopeData(
  121. pbX509PubKey,
  122. cbX509PubKey,
  123. ( LPBYTE )SECRET_DATA,
  124. strlen( SECRET_DATA ) + 1,
  125. NULL,
  126. &cbEnvelopedData );
  127. pbEnvelopedData = new BYTE[ cbEnvelopedData ];
  128. if( NULL == pbEnvelopedData )
  129. {
  130. goto done;
  131. }
  132. Status = LicenseEnvelopeData(
  133. pbX509PubKey,
  134. cbX509PubKey,
  135. ( LPBYTE )SECRET_DATA,
  136. strlen( SECRET_DATA ) + 1,
  137. pbEnvelopedData,
  138. &cbEnvelopedData );
  139. //
  140. // Decrypt the encrypted data
  141. //
  142. cbData = sizeof( abData );
  143. Status = LsCsp_DecryptEnvelopedData(
  144. CERT_TYPE_X509,
  145. pbEnvelopedData,
  146. cbEnvelopedData,
  147. abData,
  148. &cbData );
  149. if( LICENSE_STATUS_OK == Status )
  150. {
  151. printf( "Secret data is: %s", pbData );
  152. }
  153. done:
  154. if( pbProprietoryCert )
  155. {
  156. delete [] pbProprietoryCert;
  157. }
  158. if( pbX509Cert )
  159. {
  160. delete [] pbX509Cert;
  161. }
  162. if( pbPrivKey )
  163. {
  164. delete [] pbPrivKey;
  165. }
  166. if( pbX509PrivKey )
  167. {
  168. delete [] pbX509PrivKey;
  169. }
  170. if( pbX509PubKey )
  171. {
  172. delete [] pbX509PubKey;
  173. }
  174. if( pbEnvelopedData )
  175. {
  176. delete [] pbEnvelopedData;
  177. }
  178. LsCsp_Exit();
  179. return 1;
  180. }
  181. ///////////////////////////////////////////////////////////////////////////////
  182. BOOL
  183. GetCspData(
  184. LSCSPINFO CspInfo,
  185. LPBYTE * ppbData,
  186. LPDWORD pcbData )
  187. {
  188. LICENSE_STATUS
  189. Status;
  190. BOOL
  191. fResult = TRUE;
  192. *ppbData = NULL;
  193. *pcbData = 0;
  194. Status = LsCsp_GetServerData( CspInfo, NULL, pcbData );
  195. if( LICENSE_STATUS_OK == Status )
  196. {
  197. *ppbData = new BYTE[ *pcbData ];
  198. if( NULL == *ppbData )
  199. {
  200. printf( "Out of memory\n" );
  201. fResult = FALSE;
  202. goto done;
  203. }
  204. Status = LsCsp_GetServerData( CspInfo, *ppbData, pcbData );
  205. }
  206. if( LICENSE_STATUS_OK != Status )
  207. {
  208. printf( "cannot get LSCSP data: %x\n", Status );
  209. if( *ppbData )
  210. {
  211. delete [] *ppbData;
  212. *pcbData = 0;
  213. }
  214. fResult = FALSE;
  215. }
  216. done:
  217. return( fResult );
  218. }