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.

402 lines
11 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. exchcli.cxx
  5. Abstract:
  6. IIS Crypto client-side key exchange test.
  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 TEST_SOCKERR(api) \
  30. if( sockerr != NO_ERROR ) { \
  31. \
  32. printf( \
  33. "%s:%lu failed, error %d\n", \
  34. api, \
  35. __LINE__, \
  36. sockerr \
  37. ); \
  38. \
  39. goto cleanup; \
  40. \
  41. } else
  42. #define FREE_BLOB(b) \
  43. if( b != NULL ) { \
  44. \
  45. HRESULT _result; \
  46. \
  47. _result = IISCryptoFreeBlob( b ); \
  48. \
  49. if( FAILED(_result) ) { \
  50. \
  51. printf( \
  52. "IISCryptoFreeBlob( %08lx ):%lu failed, error %08lx\n", \
  53. b, \
  54. __LINE__, \
  55. _result \
  56. ); \
  57. \
  58. } \
  59. \
  60. (b) = NULL; \
  61. \
  62. }
  63. //
  64. // Private types.
  65. //
  66. //
  67. // Private globals.
  68. //
  69. DECLARE_DEBUG_PRINTS_OBJECT()
  70. #include <initguid.h>
  71. DEFINE_GUID(IisCryptGuid,
  72. 0x784d8927, 0xaa8c, 0x11d2, 0x92, 0x5e, 0x00, 0xc0, 0x4f, 0x72, 0xd9, 0x0e);
  73. CHAR ClientPlainText[] = "Client Client Client Client Client Client";
  74. //
  75. // Private prototypes.
  76. //
  77. //
  78. // Public functions.
  79. //
  80. INT
  81. __cdecl
  82. main(
  83. INT argc,
  84. CHAR * argv[]
  85. )
  86. {
  87. INT sockerr;
  88. HRESULT result;
  89. IIS_CRYPTO_EXCHANGE_CLIENT * pclient;
  90. BUFFERED_SOCKET * psocket;
  91. PIIS_CRYPTO_BLOB clientKeyExchangeKeyBlob;
  92. PIIS_CRYPTO_BLOB clientSignatureKeyBlob;
  93. PIIS_CRYPTO_BLOB serverKeyExchangeKeyBlob;
  94. PIIS_CRYPTO_BLOB serverSignatureKeyBlob;
  95. PIIS_CRYPTO_BLOB serverSessionKeyBlob;
  96. PIIS_CRYPTO_BLOB clientSessionKeyBlob;
  97. PIIS_CRYPTO_BLOB clientHashBlob;
  98. PIIS_CRYPTO_BLOB serverHashBlob;
  99. PIIS_CRYPTO_BLOB dataBlob;
  100. IIS_CRYPTO_STORAGE * clientStorage;
  101. IIS_CRYPTO_STORAGE * serverStorage;
  102. PVOID buffer;
  103. DWORD bufferLength;
  104. DWORD bufferType;
  105. if( argc != 2 ) {
  106. printf(
  107. "use: exchcli target_server\n"
  108. );
  109. return 1;
  110. }
  111. //
  112. // Initialize debug stuff.
  113. //
  114. #ifndef _NO_TRACING_
  115. CREATE_DEBUG_PRINT_OBJECT( "iiscrypt", IisCryptGuid );
  116. CREATE_INITIALIZE_DEBUG();
  117. #else
  118. CREATE_DEBUG_PRINT_OBJECT( "iiscrypt" );
  119. #endif
  120. //
  121. // Setup our locals so we know how to cleanup on exit.
  122. //
  123. pclient = NULL;
  124. psocket = NULL;
  125. clientKeyExchangeKeyBlob = NULL;
  126. clientSignatureKeyBlob = NULL;
  127. serverKeyExchangeKeyBlob = NULL;
  128. serverSignatureKeyBlob = NULL;
  129. serverSessionKeyBlob = NULL;
  130. clientSessionKeyBlob = NULL;
  131. clientHashBlob = NULL;
  132. serverHashBlob = NULL;
  133. dataBlob = NULL;
  134. clientStorage = NULL;
  135. serverStorage = NULL;
  136. //
  137. // Initialize the crypto package.
  138. //
  139. printf( "exchcli: Initializing...\n" );
  140. result = IISCryptoInitialize();
  141. TEST_HRESULT( "IISCryptoInitialize()" );
  142. //
  143. // Create & initialize the client-side key exchange object.
  144. //
  145. pclient = new IIS_CRYPTO_EXCHANGE_CLIENT;
  146. if( pclient == NULL ) {
  147. printf( "out of memory\n" );
  148. goto cleanup;
  149. }
  150. result = pclient->Initialize(
  151. CRYPT_NULL,
  152. CRYPT_NULL,
  153. CRYPT_NULL,
  154. TRUE
  155. );
  156. TEST_HRESULT( "pclient->Initialize()" );
  157. //
  158. // Create & initialize the buffered socket object.
  159. //
  160. psocket = new BUFFERED_SOCKET;
  161. if( psocket == NULL ) {
  162. printf( "out of memory\n" );
  163. goto cleanup;
  164. }
  165. result = psocket->InitializeClient( argv[1], SERVER_PORT );
  166. TEST_HRESULT( "psocket->Initialize()" );
  167. //
  168. // 1. CLIENT(1)
  169. //
  170. printf( "exchcli: Phase 1...\n" );
  171. result = pclient->ClientPhase1(
  172. &clientKeyExchangeKeyBlob,
  173. &clientSignatureKeyBlob
  174. );
  175. TEST_HRESULT( "pclient->ClientPhase1()" );
  176. sockerr = psocket->SendBlob( clientKeyExchangeKeyBlob );
  177. TEST_SOCKERR( "psocket->SendBlob()" );
  178. sockerr = psocket->SendBlob( clientSignatureKeyBlob );
  179. TEST_SOCKERR( "psocket->SendBlob()" );
  180. //
  181. // 3. CLIENT(2)
  182. //
  183. printf( "exchcli: Phase 2...\n" );
  184. sockerr = psocket->RecvBlob( &serverKeyExchangeKeyBlob );
  185. TEST_SOCKERR( "psocket->RecvBlob()" );
  186. sockerr = psocket->RecvBlob( &serverSignatureKeyBlob );
  187. TEST_SOCKERR( "psocket->RecvBlob()" );
  188. sockerr = psocket->RecvBlob( &serverSessionKeyBlob );
  189. TEST_SOCKERR( "psocket->RecvBlob()" );
  190. result = pclient->ClientPhase2(
  191. serverKeyExchangeKeyBlob,
  192. serverSignatureKeyBlob,
  193. serverSessionKeyBlob,
  194. &clientSessionKeyBlob,
  195. &clientHashBlob
  196. );
  197. TEST_HRESULT( "pclient->ClientPhase2()" );
  198. sockerr = psocket->SendBlob( clientSessionKeyBlob );
  199. TEST_SOCKERR( "psocket->SendBlob()" );
  200. sockerr = psocket->SendBlob( clientHashBlob );
  201. TEST_SOCKERR( "psocket->SendBlob()" );
  202. //
  203. // 5. CLIENT(3)
  204. //
  205. printf( "exchcli: Phase 3...\n" );
  206. sockerr = psocket->RecvBlob( &serverHashBlob );
  207. TEST_SOCKERR( "psocket->RecvBlob()" );
  208. result = pclient->ClientPhase3(
  209. serverHashBlob
  210. );
  211. TEST_HRESULT( "pclient->ClientPhase3()" );
  212. //
  213. // Create the storage objects.
  214. //
  215. printf( "exchcli: Creating storage objects...\n" );
  216. clientStorage = new IIS_CRYPTO_STORAGE;
  217. if( clientStorage == NULL ) {
  218. printf( "out of memory\n" );
  219. goto cleanup;
  220. }
  221. result = clientStorage->Initialize(
  222. pclient->QueryProviderHandle(),
  223. pclient->AssumeClientSessionKey(),
  224. CRYPT_NULL,
  225. CRYPT_NULL,
  226. TRUE
  227. );
  228. TEST_HRESULT( "clientStorage->Initialize()" );
  229. serverStorage = new IIS_CRYPTO_STORAGE;
  230. if( serverStorage == NULL ) {
  231. printf( "out of memory\n" );
  232. goto cleanup;
  233. }
  234. result = serverStorage->Initialize(
  235. pclient->QueryProviderHandle(),
  236. pclient->AssumeServerSessionKey(),
  237. CRYPT_NULL,
  238. pclient->AssumeServerSignatureKey(),
  239. TRUE
  240. );
  241. TEST_HRESULT( "serverStorage->Initialize()" );
  242. //
  243. // Send some encrypted data.
  244. //
  245. printf( "exchcli: Encrypting '%s'...\n", ClientPlainText );
  246. result = clientStorage->EncryptData(
  247. &dataBlob,
  248. ClientPlainText,
  249. sizeof(ClientPlainText),
  250. REG_SZ
  251. );
  252. TEST_HRESULT( "clientStorage->EncryptData()" );
  253. printf( "exchcli: Sending encrypted data...\n" );
  254. sockerr = psocket->SendBlob( dataBlob );
  255. TEST_SOCKERR( "psocket->SendBlob()" );
  256. FREE_BLOB( dataBlob );
  257. //
  258. // Receive some encrypted data.
  259. //
  260. printf( "exchcli: Receiving encrypted data...\n" );
  261. sockerr = psocket->RecvBlob( &dataBlob );
  262. TEST_SOCKERR( "psocket->RecvBlob()" );
  263. result = serverStorage->DecryptData(
  264. &buffer,
  265. &bufferLength,
  266. &bufferType,
  267. dataBlob
  268. );
  269. TEST_HRESULT( "serverStorage->DecryptData()" );
  270. printf( "exchcli: Received data[%lu] = '%s'\n", bufferLength, buffer );
  271. //
  272. // Tests complete.
  273. //
  274. printf( "exchcli: Done!\n" );
  275. cleanup:
  276. FREE_BLOB( dataBlob );
  277. FREE_BLOB( serverHashBlob );
  278. FREE_BLOB( clientHashBlob );
  279. FREE_BLOB( clientSessionKeyBlob );
  280. FREE_BLOB( serverSessionKeyBlob );
  281. FREE_BLOB( serverSignatureKeyBlob );
  282. FREE_BLOB( serverKeyExchangeKeyBlob );
  283. FREE_BLOB( clientSignatureKeyBlob );
  284. FREE_BLOB( clientKeyExchangeKeyBlob );
  285. delete psocket;
  286. delete clientStorage;
  287. delete serverStorage;
  288. delete pclient;
  289. (VOID)IISCryptoTerminate();
  290. DELETE_DEBUG_PRINT_OBJECT();
  291. return 0;
  292. } // main
  293. //
  294. // Private functions.
  295. //