Source code of Windows XP (NT5)
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.

863 lines
20 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996-1998
  5. //
  6. // File: tlsapip.cpp
  7. //
  8. // Contents: Private API
  9. //
  10. // History: 09-09-97 HueiWang Created
  11. //
  12. //---------------------------------------------------------------------------
  13. #include <nt.h>
  14. #include <ntrtl.h>
  15. #include <nturtl.h>
  16. #include <windows.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <tchar.h>
  20. #include <rpc.h>
  21. #include "lscommon.h"
  22. #include <wincrypt.h>
  23. #include "tlsrpc.h"
  24. #include "tlsapi.h"
  25. #include "tlsapip.h"
  26. //----------------------------------------------------------------------------
  27. BOOL
  28. TLSIsLicenseEnforceEnable()
  29. /*++
  30. --*/
  31. {
  32. #if ENFORCE_LICENSING
  33. return TRUE;
  34. #else
  35. return FALSE;
  36. #endif
  37. }
  38. //----------------------------------------------------------------------------
  39. BOOL
  40. TLSIsBetaNTServer()
  41. /*++
  42. Abstract:
  43. Detemine if base NT is a beta or RTM version.
  44. Parameter:
  45. None.
  46. Return:
  47. TRUE/FALSE
  48. --*/
  49. {
  50. BOOL bBetaNt = FALSE;
  51. DWORD dwStatus;
  52. DWORD cbData;
  53. DWORD cbType;
  54. HKEY hKey = NULL;
  55. __try {
  56. LARGE_INTEGER Time = USER_SHARED_DATA->SystemExpirationDate;
  57. if(Time.QuadPart)
  58. {
  59. bBetaNt = TRUE;
  60. // check our special registry key - force
  61. // issuing a RTM license.
  62. dwStatus = RegOpenKeyEx(
  63. HKEY_LOCAL_MACHINE,
  64. L"SOFTWARE\\Microsoft\\TermServLicensing",
  65. 0,
  66. KEY_ALL_ACCESS,
  67. &hKey
  68. );
  69. if(dwStatus == ERROR_SUCCESS)
  70. {
  71. dwStatus = RegQueryValueEx(
  72. hKey,
  73. _TEXT("RunAsRTM"),
  74. NULL,
  75. &cbType,
  76. NULL,
  77. &cbData
  78. );
  79. // for testing, force it to run as RTM version.
  80. // key must exist and must be DWORD type
  81. if(dwStatus == ERROR_SUCCESS && cbType == REG_DWORD)
  82. {
  83. bBetaNt = FALSE;
  84. }
  85. }
  86. }
  87. }
  88. __except(EXCEPTION_EXECUTE_HANDLER) {
  89. ASSERT(FALSE);
  90. }
  91. if(hKey != NULL)
  92. {
  93. RegCloseKey(hKey);
  94. }
  95. return bBetaNt;
  96. }
  97. //----------------------------------------------------------------------------
  98. DWORD WINAPI
  99. TLSAllocateInternetLicenseEx(
  100. IN TLS_HANDLE hHandle,
  101. IN const CHALLENGE_CONTEXT ChallengeContext,
  102. IN const LICENSEREQUEST* pRequest,
  103. IN LPTSTR pszMachineName,
  104. IN LPTSTR pszUserName,
  105. IN const DWORD cbChallengeResponse,
  106. IN const PBYTE pbChallengeResponse,
  107. OUT PTLSInternetLicense pInternetLicense,
  108. OUT PDWORD pdwErrCode
  109. )
  110. /*++
  111. --*/
  112. {
  113. TLSLICENSEREQUEST rpcRequest;
  114. RequestToTlsRequest( pRequest, &rpcRequest );
  115. return TLSRpcAllocateInternetLicenseEx(
  116. hHandle,
  117. ChallengeContext,
  118. &rpcRequest,
  119. pszMachineName,
  120. pszUserName,
  121. cbChallengeResponse,
  122. pbChallengeResponse,
  123. pInternetLicense,
  124. pdwErrCode
  125. );
  126. }
  127. //----------------------------------------------------------------------------
  128. DWORD WINAPI
  129. TLSReturnInternetLicenseEx(
  130. IN TLS_HANDLE hHandle,
  131. IN const LICENSEREQUEST* pRequest,
  132. IN const ULARGE_INTEGER* pulSerialNumber,
  133. IN const DWORD dwQuantity,
  134. OUT PDWORD pdwErrCode
  135. )
  136. /*++
  137. --*/
  138. {
  139. TLSLICENSEREQUEST rpcRequest;
  140. RequestToTlsRequest( pRequest, &rpcRequest );
  141. return TLSRpcReturnInternetLicenseEx(
  142. hHandle,
  143. &rpcRequest,
  144. pulSerialNumber,
  145. dwQuantity,
  146. pdwErrCode
  147. );
  148. }
  149. //----------------------------------------------------------------------------
  150. DWORD WINAPI
  151. TLSRegisterLicenseKeyPack(
  152. TLS_HANDLE hHandle,
  153. LPBYTE pbCHCertBlob,
  154. DWORD cbCHCertBlobSize,
  155. LPBYTE pbRootCertBlob,
  156. DWORD cbRootCertBlob,
  157. LPBYTE lpKeyPackBlob,
  158. DWORD dwKeyPackBlobLen,
  159. PDWORD pdwErrCode
  160. )
  161. /*++
  162. ++*/
  163. {
  164. return TLSRpcRegisterLicenseKeyPack(
  165. hHandle,
  166. pbCHCertBlob,
  167. cbCHCertBlobSize,
  168. pbRootCertBlob,
  169. cbRootCertBlob,
  170. lpKeyPackBlob,
  171. dwKeyPackBlobLen,
  172. pdwErrCode
  173. );
  174. }
  175. //----------------------------------------------------------------------------
  176. DWORD WINAPI
  177. TLSTelephoneRegisterLKP(
  178. IN TLS_HANDLE hHandle,
  179. IN DWORD cbData,
  180. IN PBYTE pbData,
  181. OUT PDWORD pdwErrCode
  182. )
  183. /*++
  184. --*/
  185. {
  186. return TLSRpcTelephoneRegisterLKP(
  187. hHandle,
  188. cbData,
  189. pbData,
  190. pdwErrCode
  191. );
  192. }
  193. //----------------------------------------------------------------------------
  194. DWORD WINAPI
  195. RequestToTlsRequest(
  196. const LICENSEREQUEST* lpRequest,
  197. TLSLICENSEREQUEST* lpRpcRequest
  198. )
  199. /*++
  200. ++*/
  201. {
  202. if(lpRequest == NULL || lpRpcRequest == NULL || lpRequest->pProductInfo == NULL)
  203. return ERROR_INVALID_PARAMETER;
  204. //
  205. // NOTE : No memory allocation, DO NOT FREE ...
  206. //
  207. lpRpcRequest->cbEncryptedHwid = lpRequest->cbEncryptedHwid;
  208. lpRpcRequest->pbEncryptedHwid = lpRequest->pbEncryptedHwid;
  209. lpRpcRequest->dwLanguageID = lpRequest->dwLanguageID;
  210. lpRpcRequest->dwPlatformID = lpRequest->dwPlatformID;
  211. lpRpcRequest->ProductInfo.dwVersion = lpRequest->pProductInfo->dwVersion;
  212. lpRpcRequest->ProductInfo.cbCompanyName = lpRequest->pProductInfo->cbCompanyName;
  213. lpRpcRequest->ProductInfo.pbCompanyName = lpRequest->pProductInfo->pbCompanyName;
  214. lpRpcRequest->ProductInfo.cbProductID = lpRequest->pProductInfo->cbProductID;
  215. lpRpcRequest->ProductInfo.pbProductID = lpRequest->pProductInfo->pbProductID;
  216. return ERROR_SUCCESS;
  217. }
  218. //----------------------------------------------------------------------------
  219. DWORD WINAPI
  220. TLSReturnLicense(
  221. TLS_HANDLE hHandle,
  222. DWORD dwKeyPackId,
  223. DWORD dwLicenseId,
  224. DWORD dwRetrunReason,
  225. PDWORD pdwErrCode
  226. )
  227. /*++
  228. ++*/
  229. {
  230. return TLSRpcReturnLicense(
  231. hHandle,
  232. dwKeyPackId,
  233. dwLicenseId,
  234. dwRetrunReason,
  235. pdwErrCode
  236. );
  237. }
  238. //----------------------------------------------------------------------------
  239. DWORD WINAPI
  240. TLSGetLSPKCS10CertRequest(
  241. TLS_HANDLE hHandle,
  242. DWORD dwCertType,
  243. PDWORD pcbData,
  244. PBYTE* ppbData,
  245. PDWORD pdwErrCode
  246. )
  247. /*++
  248. ++*/
  249. {
  250. return TLSRpcGetLSPKCS10CertRequest(
  251. hHandle,
  252. dwCertType,
  253. pcbData,
  254. ppbData,
  255. pdwErrCode
  256. );
  257. }
  258. //----------------------------------------------------------------------------
  259. DWORD WINAPI
  260. TLSRequestTermServCert(
  261. TLS_HANDLE hHandle,
  262. LPLSHydraCertRequest pRequest,
  263. PDWORD cbChallengeData,
  264. PBYTE* pbChallengeData,
  265. PDWORD pdwErrCode
  266. )
  267. /*++
  268. ++*/
  269. {
  270. TLSHYDRACERTREQUEST CertRequest;
  271. CertRequest.dwHydraVersion = pRequest->dwHydraVersion;
  272. CertRequest.pbEncryptedHwid = pRequest->pbEncryptedHwid;
  273. CertRequest.cbEncryptedHwid = pRequest->cbEncryptedHwid;
  274. CertRequest.szSubjectRdn = pRequest->szSubjectRdn;
  275. CertRequest.pSubjectPublicKeyInfo = (TLSCERT_PUBLIC_KEY_INFO *)pRequest->SubjectPublicKeyInfo;
  276. CertRequest.dwNumCertExtension = pRequest->dwNumCertExtension;
  277. CertRequest.pCertExtensions = (TLSCERT_EXTENSION *)pRequest->pCertExtensions;
  278. return TLSRpcRequestTermServCert(
  279. hHandle,
  280. &CertRequest,
  281. cbChallengeData,
  282. pbChallengeData,
  283. pdwErrCode
  284. );
  285. }
  286. //----------------------------------------------------------------------------
  287. DWORD WINAPI
  288. TLSRetrieveTermServCert(
  289. TLS_HANDLE hHandle,
  290. DWORD cbResponseData,
  291. PBYTE pbResponseData,
  292. PDWORD pcbCert,
  293. PBYTE* ppbCert,
  294. PDWORD pdwErrCode
  295. )
  296. /*++
  297. ++*/
  298. {
  299. return TLSRpcRetrieveTermServCert(
  300. hHandle,
  301. cbResponseData,
  302. pbResponseData,
  303. pcbCert,
  304. ppbCert,
  305. pdwErrCode
  306. );
  307. }
  308. //----------------------------------------------------------------------------
  309. DWORD WINAPI
  310. TLSInstallCertificate(
  311. TLS_HANDLE hHandle,
  312. DWORD dwCertType,
  313. DWORD dwCertLevel,
  314. DWORD cbSingnatureCert,
  315. PBYTE pbSingnatureCert,
  316. DWORD cbExchangeCert,
  317. PBYTE pbExchangeCert,
  318. PDWORD pdwErrCode
  319. )
  320. /*++
  321. ++*/
  322. {
  323. return TLSRpcInstallCertificate(
  324. hHandle,
  325. dwCertType,
  326. dwCertLevel,
  327. cbSingnatureCert,
  328. pbSingnatureCert,
  329. cbExchangeCert,
  330. pbExchangeCert,
  331. pdwErrCode
  332. );
  333. }
  334. //----------------------------------------------------------------------------
  335. DWORD WINAPI
  336. TLSGetServerCertificate(
  337. TLS_HANDLE hHandle,
  338. BOOL bSignCert,
  339. LPBYTE *ppbCertBlob,
  340. LPDWORD lpdwCertBlobLen,
  341. PDWORD pdwErrCode
  342. )
  343. /*++
  344. ++*/
  345. {
  346. return TLSRpcGetServerCertificate(
  347. hHandle,
  348. bSignCert,
  349. ppbCertBlob,
  350. lpdwCertBlobLen,
  351. pdwErrCode
  352. );
  353. }
  354. //----------------------------------------------------------------------------
  355. DWORD WINAPI
  356. TLSKeyPackAdd(
  357. TLS_HANDLE hHandle,
  358. LPLSKeyPack lpKeypack,
  359. PDWORD pdwErrCode
  360. )
  361. /*++
  362. ++*/
  363. {
  364. return TLSRpcKeyPackAdd(
  365. hHandle,
  366. lpKeypack,
  367. pdwErrCode
  368. );
  369. }
  370. //----------------------------------------------------------------------------
  371. DWORD WINAPI
  372. TLSKeyPackSetStatus(
  373. TLS_HANDLE hHandle,
  374. DWORD dwSetParm,
  375. LPLSKeyPack lpKeyPack,
  376. PDWORD pdwErrCode
  377. )
  378. /*++
  379. ++*/
  380. {
  381. return TLSRpcKeyPackSetStatus(
  382. hHandle,
  383. dwSetParm,
  384. lpKeyPack,
  385. pdwErrCode
  386. );
  387. }
  388. //-----------------------------------------------------------------
  389. DWORD WINAPI
  390. TLSAnnounceServer(
  391. IN TLS_HANDLE hHandle,
  392. IN DWORD dwType,
  393. IN FILETIME* pftTime,
  394. IN LPTSTR pszSetupId,
  395. IN LPTSTR pszDomainName,
  396. IN LPTSTR pszMachineName,
  397. OUT PDWORD pdwErrCode
  398. )
  399. /*++
  400. ++*/
  401. {
  402. return TLSRpcAnnounceServer(
  403. hHandle,
  404. dwType,
  405. pftTime,
  406. pszSetupId,
  407. pszDomainName,
  408. pszMachineName,
  409. pdwErrCode
  410. );
  411. }
  412. //-----------------------------------------------------------------
  413. DWORD WINAPI
  414. TLSLookupServer(
  415. IN TLS_HANDLE hHandle,
  416. IN LPTSTR pszLookupSetupId,
  417. OUT LPTSTR pszLsSetupId,
  418. IN OUT PDWORD pcbSetupId,
  419. OUT LPTSTR pszDomainName,
  420. IN OUT PDWORD pcbDomainName,
  421. IN LPTSTR pszLsName,
  422. IN OUT PDWORD pcbMachineName,
  423. OUT PDWORD pdwErrCode
  424. )
  425. /*++
  426. ++*/
  427. {
  428. return TLSRpcLookupServer(
  429. hHandle,
  430. pszLookupSetupId,
  431. pszLsSetupId,
  432. pcbSetupId,
  433. pszDomainName,
  434. pcbDomainName,
  435. pszLsName,
  436. pcbMachineName,
  437. pdwErrCode
  438. );
  439. }
  440. //-------------------------------------------------------
  441. DWORD WINAPI
  442. TLSAnnounceLicensePack(
  443. IN TLS_HANDLE hHandle,
  444. IN PTLSReplRecord pReplRecord,
  445. OUT PDWORD pdwErrCode
  446. )
  447. /*++
  448. ++*/
  449. {
  450. return TLSRpcAnnounceLicensePack(
  451. hHandle,
  452. pReplRecord,
  453. pdwErrCode
  454. );
  455. }
  456. //-------------------------------------------------------
  457. DWORD WINAPI
  458. TLSReturnLicensedProduct(
  459. IN TLS_HANDLE hHandle,
  460. IN PTLSLicenseToBeReturn pClientLicense,
  461. OUT PDWORD pdwErrCode
  462. )
  463. /*++
  464. ++*/
  465. {
  466. return TLSRpcReturnLicensedProduct(
  467. hHandle,
  468. pClientLicense,
  469. pdwErrCode
  470. );
  471. }
  472. //-------------------------------------------------------
  473. DWORD WINAPI
  474. TLSChallengeServer(
  475. IN TLS_HANDLE hHandle,
  476. IN DWORD dwClientType,
  477. IN PTLSCHALLENGEDATA pClientChallenge,
  478. OUT PTLSCHALLENGERESPONSEDATA* ppServerResponse,
  479. OUT PTLSCHALLENGEDATA* ppServerChallenge,
  480. OUT PDWORD pdwErrCode
  481. )
  482. /*++
  483. --*/
  484. {
  485. return TLSRpcChallengeServer(
  486. hHandle,
  487. dwClientType,
  488. pClientChallenge,
  489. ppServerResponse,
  490. ppServerChallenge,
  491. pdwErrCode
  492. );
  493. }
  494. //-------------------------------------------------------
  495. DWORD WINAPI
  496. TLSResponseServerChallenge(
  497. IN TLS_HANDLE hHandle,
  498. IN PTLSCHALLENGERESPONSEDATA pClientResponse,
  499. OUT PDWORD pdwErrCode
  500. )
  501. /*++
  502. --*/
  503. {
  504. return TLSRpcResponseServerChallenge(
  505. hHandle,
  506. pClientResponse,
  507. pdwErrCode
  508. );
  509. }
  510. //------------------------------------------------------
  511. DWORD WINAPI
  512. TLSGetTlsPrivateData(
  513. IN TLS_HANDLE hHandle,
  514. IN DWORD dwGetDataType,
  515. IN PTLSPrivateDataUnion pGetParm,
  516. OUT PDWORD pdwRetDataType,
  517. OUT PTLSPrivateDataUnion* ppRetData,
  518. OUT PDWORD pdwErrCode
  519. )
  520. /*++
  521. --*/
  522. {
  523. return TLSRpcGetTlsPrivateData(
  524. hHandle,
  525. dwGetDataType,
  526. pGetParm,
  527. pdwRetDataType,
  528. ppRetData,
  529. pdwErrCode
  530. );
  531. }
  532. //------------------------------------------------------
  533. DWORD WINAPI
  534. TLSTriggerReGenKey(
  535. IN TLS_HANDLE hHandle,
  536. IN BOOL bKeepSPK,
  537. OUT PDWORD pdwErrCode
  538. )
  539. /*++
  540. --*/
  541. {
  542. return TLSRpcTriggerReGenKey(
  543. hHandle,
  544. bKeepSPK,
  545. pdwErrCode
  546. );
  547. }
  548. //------------------------------------------------------
  549. DWORD
  550. GetPrivateBinaryDataFromServer(
  551. TLS_HANDLE hHandle,
  552. DWORD dwType,
  553. PDWORD pcbData,
  554. PBYTE* ppbData,
  555. PDWORD pdwErrCode
  556. )
  557. /*++
  558. --*/
  559. {
  560. TLSPrivateDataUnion SearchParm;
  561. PTLSPrivateDataUnion pPrivateData = NULL;
  562. DWORD dwRetType;
  563. DWORD dwStatus;
  564. memset(
  565. &SearchParm,
  566. 0,
  567. sizeof(TLSPrivateDataUnion)
  568. );
  569. dwStatus = TLSRpcGetTlsPrivateData(
  570. hHandle,
  571. dwType,
  572. &SearchParm,
  573. &dwRetType,
  574. &pPrivateData,
  575. pdwErrCode
  576. );
  577. if(dwStatus != RPC_S_OK || *pdwErrCode != ERROR_SUCCESS)
  578. {
  579. goto cleanup;
  580. }
  581. if(dwRetType != dwType)
  582. {
  583. //
  584. // License Server error
  585. //
  586. *pdwErrCode = LSERVER_E_INVALID_RETURN;
  587. goto cleanup;
  588. }
  589. //
  590. // Copy over unique ID
  591. //
  592. *ppbData = (PBYTE)MIDL_user_allocate(pPrivateData->BinaryData.cbData);
  593. if(*ppbData != NULL)
  594. {
  595. memset(
  596. *ppbData,
  597. 0,
  598. pPrivateData->BinaryData.cbData
  599. );
  600. *pcbData = pPrivateData->BinaryData.cbData;
  601. memcpy(
  602. *ppbData,
  603. pPrivateData->BinaryData.pbData,
  604. pPrivateData->BinaryData.cbData
  605. );
  606. }
  607. else
  608. {
  609. *pdwErrCode = LSERVER_E_OUTOFMEMORY;
  610. }
  611. cleanup:
  612. if(pPrivateData != NULL)
  613. {
  614. midl_user_free(pPrivateData);
  615. }
  616. return dwStatus;
  617. }
  618. //------------------------------------------------------
  619. DWORD WINAPI
  620. TLSGetServerPID(
  621. TLS_HANDLE hHandle,
  622. PDWORD pcbData,
  623. PBYTE* ppbData,
  624. PDWORD pdwErrCode
  625. )
  626. /*++
  627. --*/
  628. {
  629. return GetPrivateBinaryDataFromServer(
  630. hHandle,
  631. TLS_PRIVATEDATA_PID,
  632. pcbData,
  633. ppbData,
  634. pdwErrCode
  635. );
  636. }
  637. //------------------------------------------------------
  638. DWORD WINAPI
  639. TLSGetServerSPK(
  640. TLS_HANDLE hHandle,
  641. PDWORD pcbData,
  642. PBYTE* ppbData,
  643. PDWORD pdwErrCode
  644. )
  645. /*++
  646. --*/
  647. {
  648. TLSPrivateDataUnion SearchParm;
  649. PTLSPrivateDataUnion pPrivateData = NULL;
  650. DWORD dwRetType;
  651. DWORD dwStatus;
  652. memset(
  653. &SearchParm,
  654. 0,
  655. sizeof(TLSPrivateDataUnion)
  656. );
  657. dwStatus = TLSRpcGetTlsPrivateData(
  658. hHandle,
  659. TLS_PRIVATEDATA_SPK,
  660. &SearchParm,
  661. &dwRetType,
  662. &pPrivateData,
  663. pdwErrCode
  664. );
  665. if(dwStatus != RPC_S_OK || *pdwErrCode != ERROR_SUCCESS)
  666. {
  667. goto cleanup;
  668. }
  669. if(dwRetType != TLS_PRIVATEDATA_SPK)
  670. {
  671. //
  672. // License Server error
  673. //
  674. *pdwErrCode = LSERVER_E_INVALID_RETURN;
  675. goto cleanup;
  676. }
  677. //
  678. // Copy over Server's SPK.
  679. // Server never return CH's cert extension.
  680. //
  681. *ppbData = (PBYTE)MIDL_user_allocate(pPrivateData->SPK.cbSPK);
  682. if(*ppbData != NULL)
  683. {
  684. memset(
  685. *ppbData,
  686. 0,
  687. pPrivateData->SPK.cbSPK
  688. );
  689. *pcbData = pPrivateData->SPK.cbSPK;
  690. memcpy(
  691. *ppbData,
  692. pPrivateData->SPK.pbSPK,
  693. pPrivateData->SPK.cbSPK
  694. );
  695. }
  696. else
  697. {
  698. *pdwErrCode = LSERVER_E_OUTOFMEMORY;
  699. }
  700. cleanup:
  701. if(pPrivateData != NULL)
  702. {
  703. midl_user_free(pPrivateData);
  704. }
  705. return dwStatus;
  706. }
  707. //-----------------------------------------------------------
  708. DWORD WINAPI
  709. TLSDepositeServerSPK(
  710. IN TLS_HANDLE hHandle,
  711. IN DWORD cbSPK,
  712. IN PBYTE pbSPK,
  713. IN PCERT_EXTENSIONS pCertExtensions,
  714. OUT PDWORD pdwErrCode
  715. )
  716. /*++
  717. --*/
  718. {
  719. TLSPrivateDataUnion SetData;
  720. DWORD dwStatus;
  721. SetData.SPK.cbSPK = cbSPK;
  722. SetData.SPK.pbSPK = pbSPK;
  723. SetData.SPK.pCertExtensions = pCertExtensions;
  724. return TLSRpcSetTlsPrivateData(
  725. hHandle,
  726. TLS_PRIVATEDATA_SPK,
  727. &SetData,
  728. pdwErrCode
  729. );
  730. }