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
8.0 KiB

  1. /*++
  2. Copyright (c) 1997 Microsoft Corporation
  3. Module Name:
  4. exchange.cxx
  5. Abstract:
  6. This module implements the IIS_CRYPTO_EXCHANGE_SERVER class.
  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. //
  17. // Private types.
  18. //
  19. //
  20. // Private globals.
  21. //
  22. //
  23. // Private prototypes.
  24. //
  25. //
  26. // Public functions.
  27. //
  28. IIS_CRYPTO_EXCHANGE_SERVER::IIS_CRYPTO_EXCHANGE_SERVER()
  29. /*++
  30. Routine Description:
  31. IIS_CRYPTO_EXCHANGE_SERVER class constructor.
  32. Arguments:
  33. None.
  34. Return Value:
  35. None.
  36. --*/
  37. {
  38. //
  39. // Just put the member variables into known states.
  40. //
  41. m_hClientKeyExchangeKey = CRYPT_NULL;
  42. m_hClientSignatureKey = CRYPT_NULL;
  43. } // IIS_CRYPTO_EXCHANGE_SERVER::IIS_CRYPTO_EXCHANGE_SERVER
  44. IIS_CRYPTO_EXCHANGE_SERVER::~IIS_CRYPTO_EXCHANGE_SERVER()
  45. /*++
  46. Routine Description:
  47. IIS_CRYPTO_EXCHANGE_SERVER class destructor.
  48. Arguments:
  49. None.
  50. Return Value:
  51. None.
  52. --*/
  53. {
  54. //
  55. // Close any open keys.
  56. //
  57. CLOSE_KEY( m_hClientKeyExchangeKey );
  58. CLOSE_KEY( m_hClientSignatureKey );
  59. } // IIS_CRYPTO_EXCHANGE_SERVER::~IIS_CRYPTO_EXCHANGE_SERVER
  60. HRESULT
  61. IIS_CRYPTO_EXCHANGE_SERVER::ServerPhase1(
  62. IN PIIS_CRYPTO_BLOB pClientKeyExchangeKeyBlob,
  63. IN PIIS_CRYPTO_BLOB pClientSignatureKeyBlob,
  64. OUT PIIS_CRYPTO_BLOB * ppServerKeyExchangeKeyBlob,
  65. OUT PIIS_CRYPTO_BLOB * ppServerSignatureKeyBlob,
  66. OUT PIIS_CRYPTO_BLOB * ppServerSessionKeyBlob
  67. )
  68. /*++
  69. Routine Description:
  70. Performs server-side phase 1 of the multi-phase key exchange protocol.
  71. Arguments:
  72. pClientKeyExchangeKeyBlob - Pointer to the client's key exchange key
  73. public blob.
  74. pClientSignatureKeyBlob - Pointer to the client's signature public
  75. blob.
  76. ppServerKeyExchangeKeyBlob - Receives a pointer to the server's key
  77. exchange key public blob if successful. It is the server's
  78. responsibility to (somehow) transmit this to the client.
  79. ppServerSignatureKeyBlob - Receives a pointer to the server's signature
  80. public blob if successful. It is the server's responsibility to
  81. (somehow) transmit this to the client.
  82. ppServerSessionKeyBlob - Receives a pointer to the server's
  83. session key blob if successful. It is the server's responsibility
  84. to (somehow) transmit this to the client.
  85. Return Value:
  86. HRESULT - Completion status, 0 if successful, !0 otherwise.
  87. --*/
  88. {
  89. HRESULT result;
  90. PIIS_CRYPTO_BLOB keyExchangeKeyBlob = NULL;
  91. PIIS_CRYPTO_BLOB signatureKeyBlob = NULL;
  92. PIIS_CRYPTO_BLOB sessionKeyBlob = NULL;
  93. //
  94. // Sanity check.
  95. //
  96. DBG_ASSERT( ValidateState() );
  97. DBG_ASSERT( pClientKeyExchangeKeyBlob != NULL );
  98. DBG_ASSERT( ::IISCryptoIsValidBlob( pClientKeyExchangeKeyBlob ) );
  99. DBG_ASSERT( pClientSignatureKeyBlob != NULL );
  100. DBG_ASSERT( ::IISCryptoIsValidBlob( pClientSignatureKeyBlob ) );
  101. DBG_ASSERT( ppServerKeyExchangeKeyBlob != NULL );
  102. DBG_ASSERT( ppServerSessionKeyBlob != NULL );
  103. //
  104. // Import the client's keys.
  105. //
  106. DBG_ASSERT( m_hClientKeyExchangeKey == CRYPT_NULL );
  107. result = ::IISCryptoImportPublicKeyBlob(
  108. &m_hClientKeyExchangeKey,
  109. pClientKeyExchangeKeyBlob,
  110. m_hProv
  111. );
  112. if( FAILED(result) ) {
  113. goto fatal;
  114. }
  115. DBG_ASSERT( m_hClientSignatureKey == CRYPT_NULL );
  116. result = ::IISCryptoImportPublicKeyBlob(
  117. &m_hClientSignatureKey,
  118. pClientSignatureKeyBlob,
  119. m_hProv
  120. );
  121. if( FAILED(result) ) {
  122. goto fatal;
  123. }
  124. //
  125. // Export the key exchange key blob.
  126. //
  127. result = ::IISCryptoExportPublicKeyBlob(
  128. &keyExchangeKeyBlob,
  129. m_hProv,
  130. m_hKeyExchangeKey
  131. );
  132. if( FAILED(result) ) {
  133. goto fatal;
  134. }
  135. //
  136. // Export the signature key blob.
  137. //
  138. result = ::IISCryptoExportPublicKeyBlob(
  139. &signatureKeyBlob,
  140. m_hProv,
  141. m_hSignatureKey
  142. );
  143. if( FAILED(result) ) {
  144. goto fatal;
  145. }
  146. //
  147. // Generate our local session key.
  148. //
  149. DBG_ASSERT( m_hServerSessionKey == CRYPT_NULL );
  150. result = ::IISCryptoGenerateSessionKey(
  151. &m_hServerSessionKey,
  152. m_hProv
  153. );
  154. if( FAILED(result) ) {
  155. goto fatal;
  156. }
  157. //
  158. // Export it.
  159. //
  160. result = SafeExportSessionKeyBlob(
  161. &sessionKeyBlob,
  162. m_hProv,
  163. m_hServerSessionKey,
  164. m_hClientKeyExchangeKey
  165. );
  166. if( FAILED(result) ) {
  167. goto fatal;
  168. }
  169. //
  170. // Success!
  171. //
  172. *ppServerKeyExchangeKeyBlob = keyExchangeKeyBlob;
  173. *ppServerSignatureKeyBlob = signatureKeyBlob;
  174. *ppServerSessionKeyBlob = sessionKeyBlob;
  175. return NO_ERROR;
  176. fatal:
  177. FREE_BLOB(sessionKeyBlob);
  178. FREE_BLOB(signatureKeyBlob);
  179. FREE_BLOB(keyExchangeKeyBlob);
  180. DBG_ASSERT( FAILED(result) );
  181. return result;
  182. } // IIS_CRYPTO_EXCHANGE_SERVER::ServerPhase1
  183. HRESULT
  184. IIS_CRYPTO_EXCHANGE_SERVER::ServerPhase2(
  185. IN PIIS_CRYPTO_BLOB pClientSessionKeyBlob,
  186. IN PIIS_CRYPTO_BLOB pClientHashBlob,
  187. OUT PIIS_CRYPTO_BLOB * ppServerHashBlob
  188. )
  189. /*++
  190. Routine Description:
  191. Performs server-side phase 2 of the multi-phase key exchange protocol.
  192. Arguments:
  193. pClientSessionKeyBlob - Pointer to the client's session key blob.
  194. pClientHashBlob - Pointer to the client's hash blob.
  195. ppServerHashBlob - Receives a pointer to the server's hash blob
  196. if successful.
  197. Return Value:
  198. HRESULT - Completion status, 0 if successful, !0 otherwise.
  199. --*/
  200. {
  201. HRESULT result;
  202. PIIS_CRYPTO_BLOB phase3HashBlob;
  203. PIIS_CRYPTO_BLOB phase4HashBlob;
  204. //
  205. // Sanity check.
  206. //
  207. DBG_ASSERT( ValidateState() );
  208. DBG_ASSERT( pClientSessionKeyBlob != NULL );
  209. DBG_ASSERT( IISCryptoIsValidBlob( pClientSessionKeyBlob ) );
  210. DBG_ASSERT( pClientHashBlob != NULL );
  211. DBG_ASSERT( IISCryptoIsValidBlob( pClientHashBlob ) );
  212. DBG_ASSERT( ppServerHashBlob != NULL );
  213. //
  214. // Setup our locals so we know how to cleanup on exit.
  215. //
  216. phase3HashBlob = NULL;
  217. phase4HashBlob = NULL;
  218. //
  219. // Import the client's session key.
  220. //
  221. DBG_ASSERT( m_hClientSessionKey == CRYPT_NULL );
  222. result = SafeImportSessionKeyBlob(
  223. &m_hClientSessionKey,
  224. pClientSessionKeyBlob,
  225. m_hProv,
  226. m_hClientSignatureKey
  227. );
  228. if( FAILED(result) ) {
  229. goto fatal;
  230. }
  231. //
  232. // Create the phase 3 hash and compare it with the incoming hash.
  233. //
  234. result = CreatePhase3Hash(
  235. &phase3HashBlob
  236. );
  237. if( FAILED(result) ) {
  238. goto fatal;
  239. }
  240. if( !::IISCryptoCompareBlobs(
  241. pClientHashBlob,
  242. phase3HashBlob
  243. ) ) {
  244. result = ERROR_INVALID_DATA;
  245. goto fatal;
  246. }
  247. //
  248. // Create the phase 4 hash to return to the client.
  249. //
  250. result = CreatePhase4Hash(
  251. &phase4HashBlob
  252. );
  253. if( FAILED(result) ) {
  254. goto fatal;
  255. }
  256. //
  257. // Success!
  258. //
  259. *ppServerHashBlob = phase4HashBlob;
  260. FREE_BLOB( phase3HashBlob );
  261. return NO_ERROR;
  262. fatal:
  263. FREE_BLOB( phase3HashBlob );
  264. FREE_BLOB( phase4HashBlob );
  265. DBG_ASSERT( FAILED(result) );
  266. return result;
  267. } // IIS_CRYPTO_EXCHANGE_SERVER::ServerPhase2
  268. //
  269. // Private functions.
  270. //