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.

471 lines
14 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. iiscrypt.c
  5. Abstract:
  6. IIS Crypto test app.
  7. Author:
  8. Keith Moore (keithmo) 02-Dec-1996
  9. Revision History:
  10. --*/
  11. #include "precomp.h"
  12. #pragma hdrstop
  13. //
  14. // Private constants.
  15. //
  16. #define TEST_HRESULT(api) \
  17. if( FAILED(result) ) { \
  18. \
  19. printf( \
  20. "%s:%lu failed, error %08lx\n", \
  21. api, \
  22. __LINE__, \
  23. result \
  24. ); \
  25. \
  26. goto cleanup; \
  27. \
  28. } else
  29. #define CLOSE_KEY(h) \
  30. if( h != CRYPT_NULL ) { \
  31. \
  32. HRESULT _result; \
  33. \
  34. _result = IISCryptoCloseKey( h ); \
  35. \
  36. if( FAILED(_result) ) { \
  37. \
  38. printf( \
  39. "IISCryptoCloseKey( %08lx ):%lu failed, error %08lx\n", \
  40. h, \
  41. __LINE__, \
  42. _result \
  43. ); \
  44. \
  45. } \
  46. \
  47. }
  48. #define DESTROY_HASH(h) \
  49. if( h != CRYPT_NULL ) { \
  50. \
  51. HRESULT _result; \
  52. \
  53. _result = IISCryptoDestroyHash( h ); \
  54. \
  55. if( FAILED(_result) ) { \
  56. \
  57. printf( \
  58. "IISCryptoDestroyHash( %08lx ):%lu failed, error %08lx\n", \
  59. h, \
  60. __LINE__, \
  61. _result \
  62. ); \
  63. \
  64. } \
  65. \
  66. }
  67. #define FREE_BLOB(b) \
  68. if( b != NULL ) { \
  69. \
  70. HRESULT _result; \
  71. \
  72. _result = IISCryptoFreeBlob( b ); \
  73. \
  74. if( FAILED(_result) ) { \
  75. \
  76. printf( \
  77. "IISCryptoFreeBlob( %08lx ):%lu failed, error %08lx\n", \
  78. b, \
  79. __LINE__, \
  80. _result \
  81. ); \
  82. \
  83. } \
  84. \
  85. }
  86. //
  87. // Private types.
  88. //
  89. //
  90. // Private globals.
  91. //
  92. DECLARE_DEBUG_PRINTS_OBJECT()
  93. #include <initguid.h>
  94. DEFINE_GUID(IisCryptSimpleGuid,
  95. 0x784d892C, 0xaa8c, 0x11d2, 0x92, 0x5e, 0x00, 0xc0, 0x4f, 0x72, 0xd9, 0x0e);
  96. CHAR PlainText[] = "This is our sample plaintext that we'll encrypt.";
  97. //
  98. // Private prototypes.
  99. //
  100. //
  101. // Public functions.
  102. //
  103. INT
  104. __cdecl
  105. main(
  106. INT argc,
  107. CHAR * argv[]
  108. )
  109. {
  110. HRESULT result;
  111. PVOID buffer;
  112. DWORD bufferLength;
  113. DWORD type;
  114. HCRYPTPROV hProv;
  115. HCRYPTKEY hKeyExchangeKey;
  116. HCRYPTKEY hKeyExchangeKey2;
  117. HCRYPTKEY hSignatureKey;
  118. HCRYPTKEY hSignatureKey2;
  119. HCRYPTKEY hSessionKey;
  120. HCRYPTKEY hSessionKey2;
  121. HCRYPTHASH hHash1;
  122. HCRYPTHASH hHash2;
  123. PIIS_CRYPTO_BLOB keyExchangePublicKeyBlob;
  124. PIIS_CRYPTO_BLOB signaturePublicKeyBlob;
  125. PIIS_CRYPTO_BLOB sessionKeyBlob;
  126. PIIS_CRYPTO_BLOB dataBlob;
  127. PIIS_CRYPTO_BLOB hashBlob1;
  128. PIIS_CRYPTO_BLOB hashBlob2;
  129. //
  130. // Initialize debug stuff.
  131. //
  132. #ifndef _NO_TRACING_
  133. CREATE_DEBUG_PRINT_OBJECT( "iiscrypt", IisCryptSimpleGuid );
  134. CREATE_INITIALIZE_DEBUG();
  135. #else
  136. CREATE_DEBUG_PRINT_OBJECT( "iiscrypt" );
  137. #endif
  138. //
  139. // Setup our locals so we know how to cleanup on exit.
  140. //
  141. hProv = CRYPT_NULL;
  142. hKeyExchangeKey = CRYPT_NULL;
  143. hKeyExchangeKey2 = CRYPT_NULL;
  144. hSignatureKey = CRYPT_NULL;
  145. hSignatureKey2 = CRYPT_NULL;
  146. hSessionKey = CRYPT_NULL;
  147. hSessionKey2 = CRYPT_NULL;
  148. hHash1 = CRYPT_NULL;
  149. hHash2 = CRYPT_NULL;
  150. keyExchangePublicKeyBlob = NULL;
  151. signaturePublicKeyBlob = NULL;
  152. sessionKeyBlob = NULL;
  153. dataBlob = NULL;
  154. hashBlob1 = NULL;
  155. hashBlob2 = NULL;
  156. //
  157. // Initialize the crypto package.
  158. //
  159. result = IISCryptoInitialize();
  160. TEST_HRESULT( "IISCryptoInitialize()" );
  161. //
  162. // Open the container.
  163. //
  164. result = IISCryptoGetStandardContainer(
  165. &hProv,
  166. 0
  167. );
  168. TEST_HRESULT( "IISCryptoGetStandardContainer()" );
  169. printf( "hProv = %08lx\n", hProv );
  170. //
  171. // Get some keys.
  172. //
  173. result = IISCryptoGetKeyExchangeKey(
  174. &hKeyExchangeKey,
  175. hProv
  176. );
  177. TEST_HRESULT( "IISCryptoGetKeyExchangeKey()" );
  178. printf( "hKeyExchangeKey = %08lx\n", hKeyExchangeKey );
  179. result = IISCryptoGetSignatureKey(
  180. &hSignatureKey,
  181. hProv
  182. );
  183. TEST_HRESULT( "IISCryptoGetSignatureKey()" );
  184. printf( "hSignatureKey = %08lx\n", hSignatureKey );
  185. result = IISCryptoGenerateSessionKey(
  186. &hSessionKey,
  187. hProv
  188. );
  189. TEST_HRESULT( "IISCryptoGenerateSessionKey()" );
  190. printf( "hSessionKey = %08lx\n", hSessionKey );
  191. //
  192. // Create a couple of public key blobs.
  193. //
  194. result = IISCryptoExportPublicKeyBlob(
  195. &keyExchangePublicKeyBlob,
  196. hProv,
  197. hKeyExchangeKey
  198. );
  199. TEST_HRESULT( "IISCryptoExportPublicKeyBlob()" );
  200. printf( "keyExchangePublicKeyBlob = %p\n", keyExchangePublicKeyBlob );
  201. result = IISCryptoExportPublicKeyBlob(
  202. &signaturePublicKeyBlob,
  203. hProv,
  204. hSignatureKey
  205. );
  206. TEST_HRESULT( "IISCryptoExportPublicKeyBlob()" );
  207. printf( "signaturePublicKeyBlob = %p\n", signaturePublicKeyBlob );
  208. //
  209. // Now try to import them.
  210. //
  211. result = IISCryptoImportPublicKeyBlob(
  212. &hKeyExchangeKey2,
  213. keyExchangePublicKeyBlob,
  214. hProv
  215. );
  216. TEST_HRESULT( "IISCryptoImportPublicKeyBlob()" );
  217. printf( "hKeyExchangeKey2 = %08lx\n", hKeyExchangeKey2 );
  218. result = IISCryptoImportPublicKeyBlob(
  219. &hSignatureKey2,
  220. signaturePublicKeyBlob,
  221. hProv
  222. );
  223. TEST_HRESULT( "IISCryptoImportPublicKeyBlob()" );
  224. printf( "hSignatureKey2 = %08lx\n", hSignatureKey2 );
  225. //
  226. // Create a session key blob.
  227. //
  228. result = IISCryptoExportSessionKeyBlob(
  229. &sessionKeyBlob,
  230. hProv,
  231. hSessionKey,
  232. hKeyExchangeKey
  233. );
  234. TEST_HRESULT( "IISCryptoExportSessionKeyBlob()" );
  235. printf( "sessionKeyBlob = %p\n", sessionKeyBlob );
  236. //
  237. // Now try to import it back, using the imported signature key.
  238. //
  239. result = IISCryptoImportSessionKeyBlob(
  240. &hSessionKey2,
  241. sessionKeyBlob,
  242. hProv,
  243. hSignatureKey2
  244. );
  245. TEST_HRESULT( "IISCryptoImportKeyBlob()" );
  246. printf( "hSessionKey2 = %08lx\n", hSessionKey2 );
  247. //
  248. // Create an encrypted data blob using the original session key.
  249. //
  250. printf( "PlainText[%lu:%lu] = %s\n", sizeof(PlainText), REG_SZ, PlainText );
  251. result = IISCryptoEncryptDataBlob(
  252. &dataBlob,
  253. PlainText,
  254. sizeof(PlainText),
  255. REG_SZ,
  256. hProv,
  257. hSessionKey
  258. );
  259. TEST_HRESULT( "IISCryptoEncryptDataBlob()" );
  260. printf( "dataBlob = %p\n", dataBlob );
  261. //
  262. // Decrypt the data blob using the imported session and signature keys.
  263. //
  264. result = IISCryptoDecryptDataBlob(
  265. &buffer,
  266. &bufferLength,
  267. &type,
  268. dataBlob,
  269. hProv,
  270. hSessionKey2,
  271. hSignatureKey2
  272. );
  273. TEST_HRESULT( "IISCryptoDecryptDataBlob()" );
  274. printf( "decrypted data[%lu:%lu] = %s\n", bufferLength, type, buffer );
  275. //
  276. // Create a hash object number 1 containing the plaintext data
  277. // and the original session key, then export it as a hash blob.
  278. //
  279. result = IISCryptoCreateHash(
  280. &hHash1,
  281. hProv
  282. );
  283. TEST_HRESULT( "IISCryptoCreateHash()" );
  284. printf( "hHash1 = %08lx\n", hHash1 );
  285. result = IISCryptoHashData(
  286. hHash1,
  287. PlainText,
  288. sizeof(PlainText)
  289. );
  290. TEST_HRESULT( "IISCryptoHashData()" );
  291. result = IISCryptoHashSessionKey(
  292. hHash1,
  293. hSessionKey
  294. );
  295. TEST_HRESULT( "IISCryptoHashSessionKey()" );
  296. result = IISCryptoExportHashBlob(
  297. &hashBlob1,
  298. hHash1
  299. );
  300. TEST_HRESULT( "IISCryptoExportHashBlob()" );
  301. printf( "hashBlob1 = %p\n", hashBlob1 );
  302. //
  303. // Now do the same with the decrypted data and the imported key.
  304. //
  305. result = IISCryptoCreateHash(
  306. &hHash2,
  307. hProv
  308. );
  309. TEST_HRESULT( "IISCryptoCreateHash()" );
  310. printf( "hHash2 = %08lx\n", hHash2 );
  311. result = IISCryptoHashData(
  312. hHash2,
  313. buffer,
  314. bufferLength
  315. );
  316. TEST_HRESULT( "IISCryptoHashData()" );
  317. result = IISCryptoHashSessionKey(
  318. hHash2,
  319. hSessionKey2
  320. );
  321. TEST_HRESULT( "IISCryptoHashSessionKey()" );
  322. result = IISCryptoExportHashBlob(
  323. &hashBlob2,
  324. hHash2
  325. );
  326. TEST_HRESULT( "IISCryptoExportHashBlob()" );
  327. printf( "hashBlob2 = %p\n", hashBlob2 );
  328. //
  329. // Now compare the hash blobs. They had better match.
  330. //
  331. if( IISCryptoCompareBlobs( hashBlob1, hashBlob2 ) ) {
  332. printf( "hashBlob1 == hashBlob2\n" );
  333. } else {
  334. printf( "HASH BLOBS DON'T MATCH!!\n" );
  335. }
  336. cleanup:
  337. FREE_BLOB( hashBlob2);
  338. FREE_BLOB( hashBlob1);
  339. FREE_BLOB( dataBlob );
  340. FREE_BLOB( sessionKeyBlob );
  341. FREE_BLOB( signaturePublicKeyBlob );
  342. FREE_BLOB( keyExchangePublicKeyBlob );
  343. DESTROY_HASH( hHash2 );
  344. DESTROY_HASH( hHash1 );
  345. CLOSE_KEY( hSessionKey2 );
  346. CLOSE_KEY( hSessionKey );
  347. CLOSE_KEY( hSignatureKey2 );
  348. CLOSE_KEY( hSignatureKey );
  349. CLOSE_KEY( hKeyExchangeKey2 );
  350. CLOSE_KEY( hKeyExchangeKey );
  351. if( hProv != CRYPT_NULL ) {
  352. (VOID)IISCryptoCloseContainer( hProv );
  353. }
  354. (VOID)IISCryptoTerminate();
  355. DELETE_DEBUG_PRINT_OBJECT();
  356. return 0;
  357. } // main
  358. //
  359. // Private functions.
  360. //