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.

419 lines
12 KiB

  1. //
  2. // Microsoft Windows
  3. // Copyright (C) Microsoft Corporation, 1992 - 1998.
  4. //
  5. // File: HCContxt.c
  6. //
  7. // Contents: Functions that are used to pack and unpack different messages
  8. //
  9. // Classes:
  10. //
  11. // Functions:
  12. //
  13. // History: 12-23-97 v-sbhatt Created
  14. // 07-22-98 fredch Added LicenseSetPublicKey() function
  15. //----------------------------------------------------------------------------
  16. #include "windows.h"
  17. #include "stdlib.h"
  18. #include <tchar.h>
  19. #ifdef OS_WINCE
  20. #include <wincelic.h>
  21. #endif //OS_WINCE
  22. #include "license.h"
  23. #include "cryptkey.h"
  24. #include "hccontxt.h"
  25. #include "cliprot.h"
  26. #ifndef OS_WINCE
  27. #include "assert.h"
  28. #endif // OS_WINCE
  29. VOID
  30. FreeProprietaryCertificate(
  31. PHydra_Server_Cert * ppCertificate );
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. /******************************************************************
  36. * Funtion : PLicense_Client_Context LicenseCreateContext(VOID)
  37. * Purpose : Creates a New License_Client_Context and initializes
  38. * it with 0
  39. * Returns : Returns a pointer to created License_Client_Context
  40. *******************************************************************/
  41. PLicense_Client_Context
  42. LicenseCreateContext( VOID)
  43. {
  44. PLicense_Client_Context pContext;
  45. //Allocate approptiate memory!
  46. pContext = (PLicense_Client_Context)malloc(sizeof(License_Client_Context));
  47. if(pContext == NULL)
  48. return NULL;
  49. memset(pContext, 0, sizeof(License_Client_Context));
  50. //
  51. // allocate memory for the crypto context
  52. //
  53. pContext->pCryptParam = ( PCryptSystem )malloc( sizeof( CryptSystem ) );
  54. if( NULL == pContext->pCryptParam )
  55. {
  56. free( pContext );
  57. pContext = NULL;
  58. return( NULL );
  59. }
  60. return pContext;
  61. }
  62. /**********************************************************************************
  63. * Funtion : LICENSE_STATUS LicenseDeleteContext(PLicense_Client_Context pContext)
  64. * Purpose : Deletes an existing context and overwrites the memory with 0
  65. * Returns : Returns LICENSE_STATUS
  66. *******************************************************************/
  67. LICENSE_STATUS CALL_TYPE
  68. LicenseDeleteContext(
  69. HANDLE hContext
  70. )//PLicense_Client_Context pContext)
  71. {
  72. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  73. PLicense_Client_Context pContext = (PLicense_Client_Context)hContext;
  74. if(pContext == NULL)
  75. {
  76. lsReturn = LICENSE_STATUS_INVALID_CLIENT_CONTEXT;
  77. #if DBG
  78. OutputDebugString(_T("The context handle passed is invalid"));
  79. #endif
  80. return lsReturn;
  81. }
  82. //free pServerCert
  83. if(pContext->pServerCert)
  84. {
  85. FreeProprietaryCertificate( &pContext->pServerCert );
  86. }
  87. //
  88. // Free the public key
  89. //
  90. if( pContext->pbServerPubKey )
  91. {
  92. memset( pContext->pbServerPubKey, 0x00, pContext->cbServerPubKey );
  93. free( pContext->pbServerPubKey );
  94. pContext->pbServerPubKey = NULL;
  95. }
  96. //Free pCryptSystem
  97. if(pContext->pCryptParam)
  98. {
  99. memset(pContext->pCryptParam, 0x00, sizeof(CryptSystem));
  100. free(pContext->pCryptParam);
  101. pContext->pCryptParam = NULL;
  102. }
  103. //Free the last message
  104. if(pContext->pbLastMessage)
  105. {
  106. memset(pContext->pbLastMessage, 0x00, pContext->cbLastMessage);
  107. free(pContext->pbLastMessage);
  108. pContext->pbLastMessage = NULL;
  109. }
  110. if(pContext)
  111. {
  112. //Zeroise the memory
  113. memset(pContext, 0, sizeof(License_Client_Context));
  114. //Now free the context;
  115. free(pContext);
  116. pContext = NULL;
  117. }
  118. hContext = NULL;
  119. return lsReturn;
  120. }
  121. /**********************************************************************************
  122. * Funtion : LICENSE_STATUS
  123. * LicenseInitializeContext(
  124. * PLicense_Client_Context pContext,
  125. * DWORD dwFlags
  126. * );
  127. * Purpose : Initializes an existing context
  128. * Returns : Returns LICENSE_STATUS
  129. *******************************************************************/
  130. LICENSE_STATUS CALL_TYPE
  131. LicenseInitializeContext(
  132. HANDLE *phContext,
  133. DWORD dwFlags
  134. )
  135. {
  136. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  137. PLicense_Client_Context pContext;
  138. assert(phContext);
  139. pContext = LicenseCreateContext();
  140. if(pContext == NULL)
  141. {
  142. #if DBG
  143. OutputDebugString(_T("\nUnable to allocate memory for the context.\n"));
  144. #endif
  145. *phContext = NULL;
  146. lsReturn = LICENSE_STATUS_OUT_OF_MEMORY;
  147. return lsReturn;
  148. }
  149. //Now initialize different members of the context structure
  150. pContext->dwProtocolVersion = LICENSE_HIGHEST_PROTOCOL_VERSION;
  151. pContext->dwState = LICENSE_CLIENT_STATE_WAIT_SERVER_HELLO;
  152. pContext->pCryptParam->dwCryptState = CRYPT_SYSTEM_STATE_INITIALIZED;
  153. pContext->pCryptParam->dwSignatureAlg = SIGNATURE_ALG_RSA;
  154. pContext->pCryptParam->dwKeyExchAlg = KEY_EXCHANGE_ALG_RSA;
  155. pContext->pCryptParam->dwSessKeyAlg = BASIC_RC4_128;
  156. pContext->pCryptParam->dwMACAlg = MAC_MD5_SHA;
  157. memset(pContext->pCryptParam->rgbClientRandom, 0x00, LICENSE_RANDOM);
  158. memset(pContext->pCryptParam->rgbServerRandom, 0x00, LICENSE_RANDOM);
  159. memset(pContext->pCryptParam->rgbPreMasterSecret, 0x00, LICENSE_PRE_MASTER_SECRET);
  160. memset(pContext->pCryptParam->rgbMACSaltKey, 0x00, LICENSE_MAC_WRITE_KEY);
  161. memset(pContext->pCryptParam->rgbSessionKey, 0x00, LICENSE_SESSION_KEY);
  162. memset(pContext->rgbMACData, 0x00, LICENSE_MAC_DATA);
  163. pContext->cbLastMessage = 0;
  164. pContext->pbLastMessage = NULL;
  165. pContext->pServerCert = NULL;
  166. pContext->dwContextFlags = dwFlags;
  167. *phContext = (HANDLE)pContext;
  168. return lsReturn;
  169. }
  170. /******************************************************************
  171. * Funtion : LICENSE_STATUS
  172. * LicenseAcceptContext(
  173. * HANDLE hContext,
  174. * UINT32 puiExtendedErrorInfo,
  175. * BYTE FAR * pbInput,
  176. * DWORD cbInput,
  177. * BYTE FAR * pbOutput,
  178. * DWORD FAR * pcbOutput )
  179. *
  180. * Purpose : Process and construct licensing protocol data.
  181. *
  182. * Returns : Returns a LICENSE_STATUS return code.
  183. *******************************************************************/
  184. LICENSE_STATUS CALL_TYPE
  185. LicenseAcceptContext(
  186. HANDLE hContext,
  187. UINT32 *puiExtendedErrorInfo,
  188. BYTE FAR *pbInput,
  189. DWORD cbInput,
  190. BYTE FAR *pbOutput,
  191. DWORD FAR*pcbOutput
  192. )
  193. {
  194. PLicense_Client_Context pContext = (PLicense_Client_Context)hContext;
  195. return LicenseClientHandleServerMessage(pContext,
  196. puiExtendedErrorInfo,
  197. pbInput,
  198. cbInput,
  199. pbOutput,
  200. pcbOutput);
  201. }
  202. /******************************************************************
  203. * Funtion : LICENSE_STATUS
  204. * LicenseSetPublicKey(
  205. * HANDLE hContext,
  206. * DWORD cbPubKey,
  207. * BYTE FAR * pbPubKey )
  208. *
  209. * Purpose : Sets the public key to use.
  210. *
  211. * Returns : Returns a LICENSE_STATUS return code.
  212. *******************************************************************/
  213. LICENSE_STATUS CALL_TYPE
  214. LicenseSetPublicKey(
  215. HANDLE hContext,
  216. DWORD cbPubKey,
  217. BYTE FAR * pbPubKey )
  218. {
  219. PLicense_Client_Context
  220. pContext = ( PLicense_Client_Context )hContext;
  221. PBYTE
  222. pbOldPubKey = NULL;
  223. if( ( NULL == pbPubKey ) || ( 0 >= cbPubKey ) || ( NULL == pContext ) )
  224. {
  225. return( LICENSE_STATUS_INVALID_INPUT );
  226. }
  227. //
  228. // remember the old public key so that we can restore it if this
  229. // function call doesn't complete successfully.
  230. //
  231. pbOldPubKey = pContext->pbServerPubKey;
  232. //
  233. // allocate memory for the new public key
  234. //
  235. pContext->pbServerPubKey = malloc( cbPubKey );
  236. if( NULL == pContext->pbServerPubKey )
  237. {
  238. //
  239. // no memory, restore the old public key and return an error
  240. //
  241. pContext->pbServerPubKey = pbOldPubKey;
  242. return( LICENSE_STATUS_OUT_OF_MEMORY );
  243. }
  244. //
  245. // copy the new public key
  246. //
  247. memcpy( pContext->pbServerPubKey, pbPubKey, cbPubKey );
  248. pContext->cbServerPubKey = cbPubKey;
  249. if( pbOldPubKey )
  250. {
  251. free( pbOldPubKey );
  252. }
  253. return( LICENSE_STATUS_OK );
  254. }
  255. /******************************************************************
  256. * Funtion : LICENSE_STATUS
  257. * LicenseSetCertificate(
  258. * HANDLE hContext,
  259. * PHydra_Server_Cert pCertificate )
  260. *
  261. * Purpose : Sets the certificate to use.
  262. *
  263. * Returns : Returns a LICENSE_STATUS return code.
  264. *******************************************************************/
  265. LICENSE_STATUS CALL_TYPE
  266. LicenseSetCertificate(
  267. HANDLE hContext,
  268. PHydra_Server_Cert pCertificate )
  269. {
  270. PLicense_Client_Context
  271. pContext = ( PLicense_Client_Context )hContext;
  272. PHydra_Server_Cert
  273. pNewCert = NULL;
  274. LICENSE_STATUS
  275. Status = LICENSE_STATUS_OK;
  276. if( ( NULL == pCertificate ) || ( NULL == pContext ) ||
  277. ( NULL == pCertificate->PublicKeyData.pBlob) ||
  278. ( NULL == pCertificate->SignatureBlob.pBlob ) )
  279. {
  280. return( LICENSE_STATUS_INVALID_INPUT );
  281. }
  282. //
  283. // allocate memory for the new certificate
  284. //
  285. pNewCert = ( PHydra_Server_Cert )malloc( sizeof( Hydra_Server_Cert ) );
  286. if( NULL == pNewCert )
  287. {
  288. return( LICENSE_STATUS_OUT_OF_MEMORY );
  289. }
  290. memset( ( char * )pNewCert, 0, sizeof( Hydra_Server_Cert ) );
  291. pNewCert->PublicKeyData.pBlob = ( LPBYTE )malloc( pCertificate->PublicKeyData.wBlobLen );
  292. if( NULL == pNewCert->PublicKeyData.pBlob )
  293. {
  294. Status = LICENSE_STATUS_OUT_OF_MEMORY;
  295. goto ErrorExit;
  296. }
  297. pNewCert->SignatureBlob.pBlob = ( LPBYTE )malloc( pCertificate->SignatureBlob.wBlobLen );
  298. if( NULL == pNewCert->SignatureBlob.pBlob )
  299. {
  300. Status = LICENSE_STATUS_OUT_OF_MEMORY;
  301. goto ErrorExit;
  302. }
  303. //
  304. // copy the certificate info
  305. //
  306. pNewCert->dwVersion = pCertificate->dwVersion;
  307. pNewCert->dwSigAlgID = pCertificate->dwSigAlgID;
  308. pNewCert->dwKeyAlgID = pCertificate->dwKeyAlgID;
  309. pNewCert->PublicKeyData.wBlobType = pCertificate->PublicKeyData.wBlobType;
  310. pNewCert->PublicKeyData.wBlobLen = pCertificate->PublicKeyData.wBlobLen;
  311. memcpy( pNewCert->PublicKeyData.pBlob,
  312. pCertificate->PublicKeyData.pBlob,
  313. pNewCert->PublicKeyData.wBlobLen );
  314. pNewCert->SignatureBlob.wBlobType = pCertificate->SignatureBlob.wBlobType;
  315. pNewCert->SignatureBlob.wBlobLen = pCertificate->SignatureBlob.wBlobLen;
  316. memcpy( pNewCert->SignatureBlob.pBlob,
  317. pCertificate->SignatureBlob.pBlob,
  318. pNewCert->SignatureBlob.wBlobLen );
  319. //
  320. // free the old certificate and reset the pointer.
  321. //
  322. if( pContext->pServerCert )
  323. {
  324. FreeProprietaryCertificate( &pContext->pServerCert );
  325. }
  326. pContext->pServerCert = pNewCert;
  327. return( Status );
  328. ErrorExit:
  329. if( pNewCert->PublicKeyData.pBlob )
  330. {
  331. free( pNewCert->PublicKeyData.pBlob );
  332. }
  333. if( pNewCert->SignatureBlob.pBlob )
  334. {
  335. free( pNewCert->SignatureBlob.pBlob );
  336. }
  337. free( pNewCert );
  338. return( Status );
  339. }
  340. #ifdef __cpluscplus
  341. }
  342. #endif