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.

240 lines
7.8 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. icrypt.cxx
  5. Abstract:
  6. IIS Crypto test app.
  7. Author:
  8. Keith Moore (keithmo) 02-Dec-1996
  9. Revision History:
  10. --*/
  11. #include "precomp.hxx"
  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(IisICryptGuid,
  95. 0x784d892A, 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. IIS_CRYPTO_STORAGE * storage;
  112. PVOID buffer;
  113. DWORD bufferLength;
  114. DWORD type;
  115. PIIS_CRYPTO_BLOB dataBlob;
  116. //
  117. // Initialize debug stuff.
  118. //
  119. #ifndef _NO_TRACING_
  120. CREATE_DEBUG_PRINT_OBJECT( "iiscrypt", IisICryptGuid );
  121. CREATE_INITIALIZE_DEBUG();
  122. #else
  123. CREATE_DEBUG_PRINT_OBJECT( "iiscrypt" );
  124. #endif
  125. //
  126. // Setup our locals so we know how to cleanup on exit.
  127. //
  128. storage = NULL;
  129. dataBlob = NULL;
  130. //
  131. // Initialize the crypto package.
  132. //
  133. result = IISCryptoInitialize();
  134. TEST_HRESULT( "IISCryptoInitialize()" );
  135. //
  136. // Create the crypto storage object.
  137. //
  138. storage = new(IIS_CRYPTO_STORAGE);
  139. if( storage == NULL ) {
  140. printf( "out of memory\n" );
  141. goto cleanup;
  142. }
  143. //
  144. // Initialize with a new session key.
  145. //
  146. result = storage->Initialize();
  147. TEST_HRESULT( "storage->Initialize()" );
  148. //
  149. // Create an encrypted data blob.
  150. //
  151. printf( "PlainText[%lu] = %s\n", sizeof(PlainText), PlainText );
  152. result = storage->EncryptData(
  153. &dataBlob,
  154. PlainText,
  155. sizeof(PlainText),
  156. REG_SZ
  157. );
  158. TEST_HRESULT( "storage->EncryptData()" );
  159. printf( "dataBlob = %08lx\n", dataBlob );
  160. //
  161. // Decrypt the data blob.
  162. //
  163. result = storage->DecryptData(
  164. &buffer,
  165. &bufferLength,
  166. &type,
  167. dataBlob
  168. );
  169. TEST_HRESULT( "storage->DecryptData()" );
  170. printf( "decrypted data[%lu] = %s\n", bufferLength, buffer );
  171. cleanup:
  172. FREE_BLOB( dataBlob );
  173. delete storage;
  174. (VOID)IISCryptoTerminate();
  175. DELETE_DEBUG_PRINT_OBJECT();
  176. return 0;
  177. } // main
  178. //
  179. // Private functions.
  180. //