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.

353 lines
14 KiB

  1. /**MOD+**********************************************************************/
  2. /* Header: CLicense.cpp */
  3. /* */
  4. /* Purpose: Client License Manager implementation */
  5. /* */
  6. /* Copyright(C) Microsoft Corporation 1997-1999 */
  7. /* */
  8. /****************************************************************************/
  9. #include <adcg.h>
  10. extern "C" {
  11. #include <clicense.h>
  12. #define TRC_GROUP TRC_GROUP_SECURITY
  13. #define TRC_FILE "clicense"
  14. #include <atrcapi.h>
  15. #ifdef ENFORCE_LICENSE
  16. #include "license.h"
  17. #include "cryptkey.h"
  18. #include "hccontxt.h"
  19. #endif //ENFORCE_LICENSE
  20. }
  21. #include "clicense.h"
  22. #include "autil.h"
  23. #include "wui.h"
  24. #include "sl.h"
  25. /****************************************************************************/
  26. /* License Handle Data */
  27. /****************************************************************************/
  28. typedef struct tagCLICENSE_DATA
  29. {
  30. int ANumber;
  31. } CLICENSE_DATA, * PCLICENSE_DATA;
  32. /****************************************************************************/
  33. /* Define our memory alloc function */
  34. /****************************************************************************/
  35. #define MemoryAlloc(x) LocalAlloc(LMEM_FIXED, x)
  36. #define MemoryFree(x) LocalFree(x)
  37. CLic::CLic(CObjs* objs)
  38. {
  39. _pClientObjects = objs;
  40. }
  41. CLic::~CLic()
  42. {
  43. }
  44. /**PROC+*********************************************************************/
  45. /* Name: CLicenseInit */
  46. /* */
  47. /* Purpose: Initialize ClientLicense Manager */
  48. /* */
  49. /* Returns: Handle to be passed to subsequent License Manager functions */
  50. /* */
  51. /* Params: None */
  52. /* */
  53. /* Operation: LicenseInit is called during Client initialization. Its */
  54. /* purpose is to allow one-time initialization. It returns a */
  55. /* handle which is subsequently passed to all License Manager */
  56. /* functions. A typical use for this handle is as a pointer to */
  57. /* memory containing per-instance data. */
  58. /* */
  59. /**PROC-*********************************************************************/
  60. int CALL_TYPE CLic::CLicenseInit(
  61. HANDLE FAR * phContext
  62. )
  63. {
  64. int
  65. nResult = LICENSE_OK;
  66. LICENSE_STATUS
  67. Status;
  68. DC_BEGIN_FN( "CLicenseInit" );
  69. _pUt = _pClientObjects->_pUtObject;
  70. _pSl = _pClientObjects->_pSlObject;
  71. _pMcs = _pClientObjects->_pMCSObject;
  72. _pUi = _pClientObjects->_pUiObject;
  73. //Set only if server capability specifies it
  74. _fEncryptLicensePackets = FALSE;
  75. TRC_NRM( ( TB, _T("ClicenseInit Called\n") ) );
  76. if( _pSl->_SL.encrypting )
  77. {
  78. //
  79. // Security exchange has already taken place, so we do not
  80. // have to do the server authentication again.
  81. //
  82. Status = LicenseInitializeContext(
  83. phContext,
  84. LICENSE_CONTEXT_NO_SERVER_AUTHENTICATION );
  85. if( LICENSE_STATUS_OK != Status )
  86. {
  87. TRC_ERR( ( TB, _T("Error Initializing License Context: %d\n"), Status ) );
  88. nResult = LICENSE_ERROR;
  89. }
  90. //
  91. // Keep track of the proprietory certificate or the public key that the
  92. // server has sent to us.
  93. //
  94. if( _pSl->_SL.pServerCert )
  95. {
  96. Status = LicenseSetCertificate( *phContext, _pSl->_SL.pServerCert );
  97. if( LICENSE_STATUS_OK != Status )
  98. {
  99. TRC_ERR( ( TB, _T("Error setting server certificate: %d\n"), Status ) );
  100. nResult = LICENSE_ERROR;
  101. }
  102. }
  103. else if( _pSl->_SL.pbServerPubKey )
  104. {
  105. Status = LicenseSetPublicKey( *phContext, _pSl->_SL.cbServerPubKey, _pSl->_SL.pbServerPubKey );
  106. if( LICENSE_STATUS_OK != Status )
  107. {
  108. TRC_ERR( ( TB, _T("Error setting server public key: %d\n"), Status ) );
  109. nResult = LICENSE_ERROR;
  110. }
  111. }
  112. else
  113. {
  114. TRC_ERR( ( TB, _T("Error: no server certificate or public key after security exchange\n") ) );
  115. nResult = LICENSE_ERROR;
  116. }
  117. }
  118. else
  119. {
  120. Status = LicenseInitializeContext( phContext, 0 );
  121. if( LICENSE_STATUS_OK != Status )
  122. {
  123. TRC_ERR( ( TB, _T("Error Initializing License Context: %d\n"), Status ) );
  124. nResult = LICENSE_ERROR;
  125. }
  126. }
  127. DC_END_FN();
  128. return( nResult );
  129. }
  130. /**PROC+*********************************************************************/
  131. /* Name: CLicenseData */
  132. /* */
  133. /* Purpose: Handle license data received from the Server */
  134. /* */
  135. /* Returns: LICENSE_OK - License negotiation is complete */
  136. /* LICENSE_CONTINUE - License negotiation will continue */
  137. /* */
  138. /* Params: pHandle - handle returned by LicenseInit */
  139. /* pData - data received from Server */
  140. /* dataLen - length of data received */
  141. /* */
  142. /* Operation: This function is passed all license packets received from the */
  143. /* Server. It should parse the packet and respond (by calling */
  144. /* suitable SL functions - see aslapi.h) as required. */
  145. /* */
  146. /* If license negotiation is complete, this function must return */
  147. /* LICENSE_OK */
  148. /* If license negotiation is not yet complete, return */
  149. /* LICENSE_CONTINUE */
  150. /* */
  151. /* Incoming packets from the Client will continue to be */
  152. /* interpreted as license packets until this function returns */
  153. /* LICENSE_OK. */
  154. /* */
  155. /**PROC-*********************************************************************/
  156. int CALL_TYPE CLic::CLicenseData(
  157. HANDLE hContext,
  158. LPVOID pData,
  159. DWORD dwDataLen,
  160. UINT32 *puiExtendedErrorInfo)
  161. {
  162. SL_BUFHND bufHandle;
  163. DWORD dwBufLen;
  164. DWORD dwHeaderLen, dwTotalLen, newDataLen;
  165. BYTE FAR * pbBuffer;
  166. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  167. PRNS_SECURITY_HEADER2 pSecHeader2;
  168. DC_BEGIN_FN("CLicenseData");
  169. TRC_NRM((TB, _T("CLicenseData Called\n")));
  170. TRC_NRM((TB, _T("CLicenseData called, length = %ld"), dwDataLen));
  171. lsReturn = LicenseAcceptContext( hContext,
  172. puiExtendedErrorInfo,
  173. (BYTE FAR *)pData,
  174. dwDataLen,
  175. NULL,
  176. &dwBufLen);
  177. if( lsReturn == LICENSE_STATUS_OK)
  178. {
  179. TRC_NRM((TB, _T("License verification succeeded\n")));
  180. DC_END_FN();
  181. return LICENSE_OK;
  182. }
  183. if(lsReturn != LICENSE_STATUS_CONTINUE)
  184. {
  185. TRC_ERR((TB, _T("Error %d during license verification.\n"), lsReturn));
  186. DC_END_FN();
  187. return LICENSE_ERROR;
  188. }
  189. /************************************************************************/
  190. /* Adjust requested length to account for SL header and */
  191. /* get the buffer from NL */
  192. /************************************************************************/
  193. if (_pSl->_SL.encrypting)
  194. {
  195. if (_pSl->_SL.encryptionMethodSelected == SM_FIPS_ENCRYPTION_FLAG) {
  196. // If FIPS is used,
  197. // it must have room for an extra block
  198. dwHeaderLen = sizeof(RNS_SECURITY_HEADER2);
  199. newDataLen = TSCAPI_AdjustDataLen(dwBufLen);
  200. dwTotalLen = newDataLen + dwHeaderLen;
  201. }
  202. else {
  203. dwHeaderLen = sizeof(RNS_SECURITY_HEADER1);
  204. dwTotalLen = dwBufLen + dwHeaderLen;
  205. }
  206. TRC_DBG((TB, _T("Ask NL for %d (was %d) bytes"), dwTotalLen, dwBufLen));
  207. }
  208. else
  209. {
  210. dwHeaderLen = sizeof(RNS_SECURITY_HEADER);
  211. dwTotalLen = dwBufLen + dwHeaderLen;
  212. TRC_DBG((TB, _T("Not encrypting, ask NL for %d bytes"), dwTotalLen));
  213. }
  214. if( !_pMcs->NL_GetBuffer((DCUINT)(dwTotalLen),
  215. (PPDCUINT8)&pbBuffer,
  216. &bufHandle) )
  217. {
  218. /********************************************************************/
  219. /* Buffer not available so can't send, try later. */
  220. /********************************************************************/
  221. TRC_ALT((TB, _T("Failed to get buffer for licensing data\n")));
  222. DC_END_FN();
  223. return LICENSE_ERROR;
  224. }
  225. // Since FIPS need extra block, fill in the padding size
  226. if (_pSl->_SL.encryptionMethodSelected == SM_FIPS_ENCRYPTION_FLAG) {
  227. pSecHeader2 = (PRNS_SECURITY_HEADER2)pbBuffer;
  228. pSecHeader2->padlen = (TSUINT8)(newDataLen - dwBufLen);
  229. }
  230. /********************************************************************/
  231. /* Adjust buffer pointer to account for SL header */
  232. /********************************************************************/
  233. pbBuffer += dwHeaderLen;
  234. lsReturn = LicenseAcceptContext(hContext,
  235. 0,
  236. (BYTE FAR *)pData,
  237. dwDataLen,
  238. pbBuffer,
  239. &dwBufLen);
  240. if( lsReturn != LICENSE_STATUS_CONTINUE )
  241. {
  242. TRC_ERR((TB, _T("Error %d during license verification.\n"), lsReturn));
  243. DC_END_FN();
  244. return LICENSE_ERROR;
  245. }
  246. if(dwBufLen >0)
  247. {
  248. //
  249. // Now send the data
  250. //
  251. _pSl->SL_SendPacket( pbBuffer,
  252. (DCUINT)(dwBufLen),
  253. RNS_SEC_LICENSE_PKT |
  254. (_fEncryptLicensePackets ? RNS_SEC_ENCRYPT : 0 ),
  255. bufHandle,
  256. _pUi->UI_GetClientMCSID(),
  257. _pUi->UI_GetChannelID(),
  258. TS_LOWPRIORITY );
  259. TRC_NRM((TB, _T("Sending license verification data, length = %ld"),
  260. dwBufLen));
  261. TRC_NRM((TB, _T("Send License Verification data.\n")));
  262. DC_END_FN();
  263. DC_END_FN();
  264. return LICENSE_CONTINUE;
  265. }
  266. DC_END_FN();
  267. return(LICENSE_OK);
  268. }
  269. /**PROC+*********************************************************************/
  270. /* Name: CLicenseTerm */
  271. /* */
  272. /* Purpose: Terminate Client License Manager */
  273. /* */
  274. /* Returns: None */
  275. /* */
  276. /* Params: pHandle - handle returned from LicenseInit */
  277. /* */
  278. /* Operation: This function is provided to do one-time termination of the */
  279. /* License Manager. For example, if pHandle points to per- */
  280. /* instance memory, this would be a good place to free it. */
  281. /* */
  282. /* Note that CLicenseTerm is called if CLicenseInit fails, hence */
  283. /* it can be called with a NULL pHandle. */
  284. /* */
  285. /**PROC-*********************************************************************/
  286. int CALL_TYPE CLic::CLicenseTerm(
  287. HANDLE hContext )
  288. {
  289. LICENSE_STATUS lsReturn = LICENSE_STATUS_OK;
  290. DC_BEGIN_FN("CLicenseTerm");
  291. TRC_NRM((TB, _T("CLicenseTerm called.\n")));
  292. if( LICENSE_STATUS_OK != ( lsReturn = LicenseDeleteContext(hContext) ) )
  293. {
  294. TRC_ERR((TB, _T("Error %d while deleting license context.\n"), lsReturn));
  295. DC_END_FN();
  296. return LICENSE_ERROR;
  297. }
  298. DC_END_FN();
  299. return LICENSE_OK;
  300. }