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.

392 lines
11 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. exchsrv.cxx
  5. Abstract:
  6. IIS Crypto server-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(IisKeySrvGuid,
  72. 0x784d8929, 0xaa8c, 0x11d2, 0x92, 0x5e, 0x00, 0xc0, 0x4f, 0x72, 0xd9, 0x0e);
  73. CHAR ServerPlainText[] = "Server Server Server Server Server Server";
  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_SERVER * pserver;
  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 != 1 ) {
  106. printf(
  107. "use: exchsrv\n"
  108. );
  109. return 1;
  110. }
  111. //
  112. // Initialize debug stuff.
  113. //
  114. #ifndef _NO_TRACING_
  115. CREATE_DEBUG_PRINT_OBJECT( "iiscrypt", IisKeySrvGuid );
  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. pserver = 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( "exchsrv: Initializing...\n" );
  140. result = IISCryptoInitialize();
  141. TEST_HRESULT( "IISCryptoInitialize()" );
  142. //
  143. // Create & initialize the server-side key exchange object.
  144. //
  145. pserver = new IIS_CRYPTO_EXCHANGE_SERVER;
  146. if( pserver == NULL ) {
  147. printf( "out of memory\n" );
  148. goto cleanup;
  149. }
  150. result = pserver->Initialize(
  151. CRYPT_NULL,
  152. CRYPT_NULL,
  153. CRYPT_NULL,
  154. TRUE
  155. );
  156. TEST_HRESULT( "pserver->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->InitializeServer( SERVER_PORT );
  166. TEST_HRESULT( "psocket->Initialize()" );
  167. //
  168. // 2. SERVER(1)
  169. //
  170. printf( "exchsrv: Phase 1...\n" );
  171. sockerr = psocket->RecvBlob( &clientKeyExchangeKeyBlob );
  172. TEST_SOCKERR( "psocket->RecvBlob()" );
  173. sockerr = psocket->RecvBlob( &clientSignatureKeyBlob );
  174. TEST_SOCKERR( "psocket->RecvBlob()" );
  175. result = pserver->ServerPhase1(
  176. clientKeyExchangeKeyBlob,
  177. clientSignatureKeyBlob,
  178. &serverKeyExchangeKeyBlob,
  179. &serverSignatureKeyBlob,
  180. &serverSessionKeyBlob
  181. );
  182. TEST_HRESULT( "pserver->ServerPhase1()" );
  183. sockerr = psocket->SendBlob( serverKeyExchangeKeyBlob );
  184. TEST_SOCKERR( "psocket->SendBlob()" );
  185. sockerr = psocket->SendBlob( serverSignatureKeyBlob );
  186. TEST_SOCKERR( "psocket->SendBlob()" );
  187. sockerr = psocket->SendBlob( serverSessionKeyBlob );
  188. TEST_SOCKERR( "psocket->SendBlob()" );
  189. //
  190. // 4. SERVER(2)
  191. //
  192. printf( "exchsrv: Phase 2...\n" );
  193. sockerr = psocket->RecvBlob( &clientSessionKeyBlob );
  194. TEST_SOCKERR( "psocket->RecvBlob()" );
  195. sockerr = psocket->RecvBlob( &clientHashBlob );
  196. TEST_SOCKERR( "psocket->RecvBlob()" );
  197. result = pserver->ServerPhase2(
  198. clientSessionKeyBlob,
  199. clientHashBlob,
  200. &serverHashBlob
  201. );
  202. TEST_HRESULT( "pserver->ServerPhase2()" );
  203. sockerr = psocket->SendBlob( serverHashBlob );
  204. TEST_SOCKERR( "psocket->SendBlob()" );
  205. //
  206. // Create the storage objects.
  207. //
  208. printf( "exchsrv: Creating storage objects...\n" );
  209. clientStorage = new IIS_CRYPTO_STORAGE;
  210. if( clientStorage == NULL ) {
  211. printf( "out of memory\n" );
  212. goto cleanup;
  213. }
  214. result = clientStorage->Initialize(
  215. pserver->QueryProviderHandle(),
  216. pserver->AssumeClientSessionKey(),
  217. CRYPT_NULL,
  218. pserver->AssumeClientSignatureKey(),
  219. TRUE
  220. );
  221. TEST_HRESULT( "clientStorage->Initialize()" );
  222. serverStorage = new IIS_CRYPTO_STORAGE;
  223. if( serverStorage == NULL ) {
  224. printf( "out of memory\n" );
  225. goto cleanup;
  226. }
  227. result = serverStorage->Initialize(
  228. pserver->QueryProviderHandle(),
  229. pserver->AssumeServerSessionKey(),
  230. CRYPT_NULL,
  231. CRYPT_NULL,
  232. TRUE
  233. );
  234. TEST_HRESULT( "serverStorage->Initialize()" );
  235. //
  236. // Receive some encrypted data.
  237. //
  238. printf( "exchsrv: Receiving encrypted data...\n" );
  239. sockerr = psocket->RecvBlob( &dataBlob );
  240. TEST_SOCKERR( "psocket->RecvBlob()" );
  241. result = clientStorage->DecryptData(
  242. &buffer,
  243. &bufferLength,
  244. &bufferType,
  245. dataBlob
  246. );
  247. TEST_HRESULT( "clientStorage->DecryptData()" );
  248. printf( "exchsrv: Received data[%lu] = '%s'\n", bufferLength, buffer );
  249. FREE_BLOB( dataBlob );
  250. //
  251. // Send some encrypted data.
  252. //
  253. printf( "exchsrv: Encrypting '%s'...\n", ServerPlainText );
  254. result = serverStorage->EncryptData(
  255. &dataBlob,
  256. ServerPlainText,
  257. sizeof(ServerPlainText),
  258. REG_SZ
  259. );
  260. TEST_HRESULT( "serverStorage->EncryptData()" );
  261. printf( "exchsrv: Sending encrypted data...\n" );
  262. sockerr = psocket->SendBlob( dataBlob );
  263. TEST_SOCKERR( "psocket->SendBlob()" );
  264. //
  265. // Tests complete.
  266. //
  267. printf( "exchsrv: Done!\n" );
  268. cleanup:
  269. FREE_BLOB( dataBlob );
  270. FREE_BLOB( serverHashBlob );
  271. FREE_BLOB( clientHashBlob );
  272. FREE_BLOB( clientSessionKeyBlob );
  273. FREE_BLOB( serverSessionKeyBlob );
  274. FREE_BLOB( serverSignatureKeyBlob );
  275. FREE_BLOB( serverKeyExchangeKeyBlob );
  276. FREE_BLOB( clientSignatureKeyBlob );
  277. FREE_BLOB( clientKeyExchangeKeyBlob );
  278. delete psocket;
  279. delete clientStorage;
  280. delete serverStorage;
  281. delete pserver;
  282. (VOID)IISCryptoTerminate();
  283. DELETE_DEBUG_PRINT_OBJECT();
  284. return 0;
  285. } // main
  286. //
  287. // Private functions.
  288. //