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.

902 lines
21 KiB

  1. ////////////////////////////////////////////////////////////////////////
  2. //
  3. // Module : Dynamic/Nshcertmgmt.cpp
  4. //
  5. // Purpose : Smartdefaults implementation.
  6. //
  7. // Developers Name : Bharat/Radhika
  8. //
  9. // History :
  10. //
  11. // Date Author Comments
  12. // 10-13-2001 Bharat Initial Version. V1.0
  13. //
  14. ////////////////////////////////////////////////////////////////////////
  15. #ifndef UNICODE
  16. #define UNICODE
  17. #endif
  18. #include <nt.h>
  19. #include <ntrtl.h>
  20. #include <nturtl.h>
  21. #include <ntlsa.h>
  22. #include <ntmsv1_0.h>
  23. #include <tchar.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <rpc.h>
  27. #include <windows.h>
  28. #include <wincrypt.h>
  29. #include <stdio.h>
  30. #include <winipsec.h>
  31. #include <ipsecshr.h>
  32. #include "memory.h"
  33. #include "nshcertmgmt.h"
  34. ///////////////////////////////////////////////////////////////////////////////////////////
  35. //
  36. // Function : fIsCertStoreEmpty
  37. //
  38. // Date of Creation : 10-10-2001
  39. //
  40. // Parameters : IN HCERTSTORE hCertStore
  41. // Return : BOOL
  42. //
  43. // Description : Check if certificate store is empty.
  44. //
  45. // Revision History :
  46. //
  47. // Date Author Comments
  48. //
  49. ////////////////////////////////////////////////////////////////////////////////////////////
  50. BOOL
  51. fIsCertStoreEmpty(
  52. IN HCERTSTORE hCertStore
  53. )
  54. {
  55. PCCERT_CONTEXT pCertContext = NULL;
  56. BOOL fResult = FALSE;
  57. pCertContext = CertEnumCertificatesInStore(hCertStore, NULL);
  58. if(NULL == pCertContext)
  59. {
  60. fResult = TRUE;
  61. }
  62. else
  63. {
  64. CertFreeCertificateContext(pCertContext);
  65. }
  66. return fResult;
  67. }
  68. ///////////////////////////////////////////////////////////////////////////////////////////
  69. //
  70. // Function : ListCertsInStore
  71. //
  72. // Date of Creation: 10-10-2001
  73. //
  74. // Parameters : IN HCERTSTORE hCertStore,
  75. // OUT INT_IPSEC_MM_AUTH_INFO ** ppAuthInfo,
  76. // OUT PDWORD pdwNumCertificates
  77. // Return : DWORD
  78. //
  79. // Description : Lists Certificates InStore hCertStore and fills in ppAuth.
  80. //
  81. // Revision History:
  82. //
  83. // Date Author Comments
  84. //
  85. //////////////////////////////////////////////////////////////////////////////////////////
  86. DWORD
  87. ListCertsInStore(
  88. IN HCERTSTORE hCertStore,
  89. OUT INT_IPSEC_MM_AUTH_INFO ** ppAuthInfo,
  90. OUT PDWORD pdwNumCertificates
  91. )
  92. {
  93. PCCERT_CONTEXT pPrevCertContext = NULL;
  94. PCCERT_CONTEXT pCertContext = NULL;
  95. CERT_NAME_BLOB NameBlob;
  96. PCERT_ENHKEY_USAGE pUsage = NULL;
  97. PCERT_NODE pCertificateList = NULL;
  98. PCERT_NODE pTemp = NULL;
  99. INT_IPSEC_MM_AUTH_INFO * pAuthInfo = NULL;
  100. INT_IPSEC_MM_AUTH_INFO * pCurrentAuth = NULL;
  101. LPWSTR pszSubjectName = NULL;
  102. DWORD i = 0;
  103. DWORD dwNumCertificates = 0;
  104. DWORD dwError = 0;
  105. DWORD Usage = 0;
  106. BOOL bValid = FALSE;
  107. while(TRUE)
  108. {
  109. pCertContext = CertEnumCertificatesInStore(hCertStore, pPrevCertContext);
  110. if (!pCertContext)
  111. {
  112. break;
  113. }
  114. pUsage = NULL;
  115. Usage = 0;
  116. bValid = FALSE;
  117. dwError = CertGetEnhancedKeyUsage(pCertContext, 0, NULL, &Usage);
  118. if(Usage)
  119. {
  120. pUsage = (PCERT_ENHKEY_USAGE)LocalAlloc(LPTR, Usage);
  121. if(!pUsage)
  122. {
  123. dwError = ERROR_OUTOFMEMORY;
  124. BAIL_ON_WIN32ERROR(dwError)
  125. }
  126. CertGetEnhancedKeyUsage(pCertContext, 0, pUsage, &Usage);
  127. for (Usage = 0; Usage < pUsage->cUsageIdentifier; Usage++)
  128. {
  129. if(!strcmp(pUsage->rgpszUsageIdentifier[Usage],szOID_PKIX_KP_SERVER_AUTH))
  130. {
  131. bValid = TRUE;
  132. }
  133. else if(!strcmp(pUsage->rgpszUsageIdentifier[Usage],szOID_PKIX_KP_CLIENT_AUTH))
  134. {
  135. bValid = TRUE;
  136. }
  137. else if(!strcmp(pUsage->rgpszUsageIdentifier[Usage],szOID_ANY_CERT_POLICY))
  138. {
  139. bValid = TRUE;
  140. }
  141. }
  142. if(pUsage)
  143. {
  144. LocalFree(pUsage);
  145. pUsage = NULL;
  146. }
  147. }
  148. else
  149. {
  150. dwError = ERROR_SUCCESS;
  151. }
  152. if(bValid)
  153. {
  154. NameBlob = pCertContext->pCertInfo->Issuer;
  155. dwError = GetCertificateName(&NameBlob, &pszSubjectName);
  156. if (dwError)
  157. {
  158. if(NULL != pPrevCertContext)
  159. {
  160. CertFreeCertificateContext(pCertContext);
  161. }
  162. break;
  163. }
  164. if (!FindCertificateInList(pCertificateList, pszSubjectName))
  165. {
  166. //
  167. // Append this CA to the list of CAs
  168. //
  169. pCertificateList = AppendCertificateNode(pCertificateList, pszSubjectName);
  170. dwNumCertificates++;
  171. }
  172. else
  173. {
  174. FreeADsStr(pszSubjectName);
  175. }
  176. }
  177. pPrevCertContext = pCertContext;
  178. }
  179. pAuthInfo = (INT_IPSEC_MM_AUTH_INFO *)AllocADsMem( sizeof(INT_IPSEC_MM_AUTH_INFO)*dwNumCertificates );
  180. if (!pAuthInfo)
  181. {
  182. dwError = ERROR_OUTOFMEMORY;
  183. BAIL_ON_WIN32ERROR(dwError)
  184. }
  185. pTemp = pCertificateList;
  186. for (i = 0; i < dwNumCertificates; i++)
  187. {
  188. pCurrentAuth = pAuthInfo + i;
  189. dwError = CopyCertificateNode(pCurrentAuth, pTemp);
  190. BAIL_ON_WIN32ERROR(dwError)
  191. pTemp = pTemp->pNext;
  192. }
  193. if (pCertificateList)
  194. {
  195. FreeCertificateList(pCertificateList);
  196. }
  197. *ppAuthInfo = pAuthInfo;
  198. *pdwNumCertificates = dwNumCertificates;
  199. return(dwError);
  200. error:
  201. if (pCertificateList)
  202. {
  203. FreeCertificateList(pCertificateList);
  204. }
  205. if(NULL != pAuthInfo)
  206. {
  207. FreeADsMem(pAuthInfo);
  208. }
  209. *ppAuthInfo = NULL;
  210. *pdwNumCertificates = 0;
  211. return(dwError);
  212. }
  213. ///////////////////////////////////////////////////////////////////////////////////////////
  214. //
  215. // Function : CopyCertificateNode
  216. //
  217. // Date of Creation : 10-10-2001
  218. //
  219. // Parameters : OUT PINT_IPSEC_MM_AUTH_INFO pCurrentAuth,
  220. // IN PCERT_NODE pTemp
  221. // Return : DWORD
  222. //
  223. // Description : This function copies certificate from node to authentication.
  224. //
  225. // Revision History :
  226. //
  227. // Date Author Comments
  228. //
  229. //////////////////////////////////////////////////////////////////////////////////////////
  230. DWORD
  231. CopyCertificateNode(
  232. OUT PINT_IPSEC_MM_AUTH_INFO pCurrentAuth,
  233. IN PCERT_NODE pTemp
  234. )
  235. {
  236. DWORD dwError = ERROR_SUCCESS;
  237. LPWSTR pszSubjectName = NULL;
  238. pCurrentAuth->AuthMethod = IKE_RSA_SIGNATURE; //value is 3
  239. pszSubjectName = AllocADsStr(pTemp->pszSubjectName);
  240. if (!pszSubjectName)
  241. {
  242. dwError = ERROR_OUTOFMEMORY;
  243. BAIL_ON_WIN32ERROR(dwError)
  244. }
  245. pCurrentAuth->pAuthInfo = (LPBYTE) pszSubjectName;
  246. //
  247. // The AuthInfoSize is in number of characters -1 the leading character
  248. // see oakley\isadb.c
  249. //
  250. pCurrentAuth->dwAuthInfoSize = wcslen(pTemp->pszSubjectName)*sizeof(WCHAR);
  251. error:
  252. return dwError;
  253. }
  254. ///////////////////////////////////////////////////////////////////////////////////////////
  255. //
  256. // Function : AppendCertificateNode
  257. //
  258. // Date of Creation: 10-10-2001
  259. //
  260. // Parameters : OUT PCERT_NODE pCertificateList,
  261. // IN LPWSTR pszSubjectName
  262. // Return : PCERT_NODE
  263. //
  264. // Description : Add node and allocates memory.
  265. //
  266. // Revision History:
  267. //
  268. // Date Author Comments
  269. //
  270. //////////////////////////////////////////////////////////////////////////////////////////
  271. PCERT_NODE
  272. AppendCertificateNode(
  273. OUT PCERT_NODE pCertificateList,
  274. IN LPWSTR pszSubjectName
  275. )
  276. {
  277. PCERT_NODE pCertificateNode = NULL;
  278. pCertificateNode = (PCERT_NODE)AllocADsMem(sizeof(CERT_NODE));
  279. if (!pCertificateNode)
  280. {
  281. pCertificateNode = pCertificateList;
  282. BAILOUT;
  283. }
  284. pCertificateNode->pszSubjectName = pszSubjectName;
  285. pCertificateNode->pNext = pCertificateList;
  286. error:
  287. return(pCertificateNode);
  288. }
  289. ///////////////////////////////////////////////////////////////////////////////////////////
  290. //
  291. // Function : FreeCertificateList
  292. //
  293. // Date of Creation: 10-10-2001
  294. //
  295. // Parameters : IN PCERT_NODE pCertificateList
  296. // Return : VOID
  297. //
  298. // Description : Frees node
  299. //
  300. // Revision History:
  301. //
  302. // Date Author Comments
  303. //
  304. //////////////////////////////////////////////////////////////////////////////////////////
  305. VOID
  306. FreeCertificateList(
  307. IN PCERT_NODE pCertificateList
  308. )
  309. {
  310. PCERT_NODE pTemp = NULL;
  311. pTemp = pCertificateList;
  312. while (pCertificateList)
  313. {
  314. pTemp = pCertificateList;
  315. pCertificateList = pCertificateList->pNext;
  316. if (pTemp)
  317. {
  318. FreeADsStr(pTemp->pszSubjectName);
  319. FreeADsMem(pTemp);
  320. }
  321. }
  322. return;
  323. }
  324. ///////////////////////////////////////////////////////////////////////////////////////////
  325. //
  326. // Function : GetCertificateName
  327. //
  328. // Date of Creation : 10-10-2001
  329. //
  330. // Parameters : IN CERT_NAME_BLOB * pCertNameBlob,
  331. // IN LPWSTR * ppszSubjectName
  332. // Return : DWORD
  333. //
  334. // Description : Gets certificate
  335. //
  336. // Revision History :
  337. //
  338. // Date Author Comments
  339. //
  340. //////////////////////////////////////////////////////////////////////////////////////////
  341. DWORD
  342. GetCertificateName(
  343. IN CERT_NAME_BLOB * pCertNameBlob,
  344. IN LPWSTR * ppszSubjectName
  345. )
  346. {
  347. DWORD dwSize = 0;
  348. DWORD dwError = 0;
  349. LPWSTR pszSubjectName = NULL;
  350. *ppszSubjectName = NULL;
  351. dwSize = CertNameToStrW(MY_ENCODING_TYPE_CERT, pCertNameBlob, CERT_X500_NAME_STR, NULL, dwSize );
  352. if (dwSize <= 1)
  353. {
  354. dwError = ERROR_OUTOFMEMORY;
  355. BAIL_ON_WIN32ERROR(dwError)
  356. }
  357. pszSubjectName = (LPWSTR)AllocADsMem((dwSize + 1)*sizeof(WCHAR));
  358. if (!pszSubjectName)
  359. {
  360. dwError = ERROR_OUTOFMEMORY;
  361. BAIL_ON_WIN32ERROR(dwError)
  362. }
  363. dwSize = CertNameToStrW(MY_ENCODING_TYPE_CERT, pCertNameBlob, CERT_X500_NAME_STR, pszSubjectName, dwSize );
  364. if(dwSize <= 1)
  365. {
  366. FreeADsMem(pszSubjectName);
  367. dwError = ERROR_OUTOFMEMORY;
  368. BAIL_ON_WIN32ERROR(dwError)
  369. }
  370. *ppszSubjectName = pszSubjectName;
  371. error:
  372. return(dwError);
  373. }
  374. ///////////////////////////////////////////////////////////////////////////////////////////
  375. //
  376. // Function : FreeCertificatesList
  377. //
  378. // Date of Creation : 10-10-2001
  379. //
  380. // Parameters : IN INT_IPSEC_MM_AUTH_INFO * pAuthInfo,
  381. // IN DWORD dwNumCertificates
  382. // Return : VOID
  383. //
  384. // Description : Frees list of certificates from pAuth
  385. //
  386. // Revision History :
  387. //
  388. // Date Author Comments
  389. //
  390. //////////////////////////////////////////////////////////////////////////////////////////
  391. VOID
  392. FreeCertificatesList(
  393. IN INT_IPSEC_MM_AUTH_INFO * pAuthInfo,
  394. IN DWORD dwNumCertificates
  395. )
  396. {
  397. DWORD i = 0;
  398. INT_IPSEC_MM_AUTH_INFO * pThisAuthInfo = NULL;
  399. if (!pAuthInfo)
  400. {
  401. BAILOUT;
  402. }
  403. for (i = 0; i < dwNumCertificates; i++)
  404. {
  405. pThisAuthInfo = pAuthInfo + i;
  406. if (pThisAuthInfo->pAuthInfo)
  407. {
  408. FreeADsMem(pThisAuthInfo->pAuthInfo);
  409. pThisAuthInfo->pAuthInfo = NULL;
  410. }
  411. }
  412. FreeADsMem(pAuthInfo);
  413. error:
  414. return;
  415. }
  416. ///////////////////////////////////////////////////////////////////////////////////////////
  417. //
  418. // Function : FindCertificateInList
  419. //
  420. // Date of Creation : 10-10-2001
  421. //
  422. // Parameters : IN PCERT_NODE pCertificateList,
  423. // IN LPWSTR pszSubjectName
  424. // Return : BOOL
  425. //
  426. // Description : Finds certificate in List
  427. //
  428. // Revision History :
  429. //
  430. // Date Author Comments
  431. //
  432. //////////////////////////////////////////////////////////////////////////////////////////
  433. BOOL
  434. FindCertificateInList(
  435. IN PCERT_NODE pCertificateList,
  436. IN LPWSTR pszSubjectName
  437. )
  438. {
  439. BOOL bReturn = FALSE;
  440. while (pCertificateList) {
  441. if (!_wcsicmp(pCertificateList->pszSubjectName, pszSubjectName)) {
  442. bReturn = TRUE;
  443. BAILOUT;
  444. }
  445. pCertificateList = pCertificateList->pNext;
  446. }
  447. error:
  448. return bReturn;
  449. }
  450. ///////////////////////////////////////////////////////////////////////////////////////////
  451. //
  452. // Function : ListCertChainsInStore
  453. //
  454. // Date of Creation: 10-10-2001
  455. //
  456. // Parameters : IN HCERTSTORE hCertStore,
  457. // OUT INT_IPSEC_MM_AUTH_INFO ** ppAuthInfo,
  458. // IN PDWORD pdwNumCertificates,
  459. // IN LPCSTR pszUsageIdentifier
  460. //
  461. // Return : DWORD
  462. //
  463. // Description : Lists certificate in store
  464. //
  465. // Revision History:
  466. //
  467. // Date Author Comments
  468. //
  469. //////////////////////////////////////////////////////////////////////////////////////////
  470. DWORD
  471. ListCertChainsInStore(
  472. IN HCERTSTORE hCertStore,
  473. OUT INT_IPSEC_MM_AUTH_INFO ** ppAuthInfo,
  474. IN PDWORD pdwNumCertificates,
  475. IN LPCSTR pszUsageIdentifier
  476. )
  477. {
  478. PCCERT_CHAIN_CONTEXT pPrevChain=NULL;
  479. PCCERT_CHAIN_CONTEXT pCertChain = NULL;
  480. DWORD dwError = 0;
  481. CERT_CHAIN_FIND_BY_ISSUER_PARA FindPara;
  482. CERT_NAME_BLOB *NameArray=NULL;
  483. DWORD ArraySize=0;
  484. PCERT_SIMPLE_CHAIN pSimpChain = NULL;
  485. PCERT_CHAIN_ELEMENT pRoot = NULL;
  486. DWORD dwRootIndex = 0;
  487. LPWSTR pszSubjectName = NULL;
  488. PCERT_NODE pCertificateList = NULL;
  489. PCERT_NODE pTemp = NULL;
  490. INT_IPSEC_MM_AUTH_INFO * pAuthInfo = NULL;
  491. INT_IPSEC_MM_AUTH_INFO * pCurrentAuth = NULL;
  492. DWORD i = 0;
  493. DWORD dwNumCertificates = 0;
  494. FindPara.pszUsageIdentifier = pszUsageIdentifier;
  495. FindPara.cbSize = sizeof(FindPara);
  496. FindPara.dwKeySpec = 0;
  497. FindPara.cIssuer = ArraySize;
  498. FindPara.rgIssuer = NameArray;
  499. FindPara.pfnFindCallback = NULL;
  500. FindPara.pvFindArg = NULL;
  501. while (TRUE)
  502. {
  503. pCertChain=CertFindChainInStore(hCertStore, X509_ASN_ENCODING,
  504. CERT_CHAIN_FIND_BY_ISSUER_COMPARE_KEY_FLAG |
  505. CERT_CHAIN_FIND_BY_ISSUER_LOCAL_MACHINE_FLAG |
  506. CERT_CHAIN_FIND_BY_ISSUER_CACHE_ONLY_URL_FLAG |
  507. CERT_CHAIN_FIND_BY_ISSUER_NO_KEY_FLAG,
  508. CERT_CHAIN_FIND_BY_ISSUER,
  509. &FindPara,
  510. pPrevChain
  511. );
  512. if (!pCertChain)
  513. {
  514. break;
  515. }
  516. pSimpChain=*(pCertChain->rgpChain);
  517. dwRootIndex=pSimpChain->cElement-1;
  518. pRoot=pSimpChain->rgpElement[dwRootIndex];
  519. dwError = GetCertificateName(
  520. &(pRoot->pCertContext->pCertInfo->Issuer),
  521. &pszSubjectName
  522. );
  523. BAIL_ON_WIN32ERROR(dwError)
  524. if (!FindCertificateInList(pCertificateList, pszSubjectName))
  525. {
  526. //
  527. // Append this CA to the list of CAs
  528. //
  529. pCertificateList = AppendCertificateNode( pCertificateList, pszSubjectName );
  530. dwNumCertificates++;
  531. }
  532. else
  533. {
  534. //
  535. // This is a duplicate - certificate payloads will not accept dupes.
  536. //
  537. FreeADsStr(pszSubjectName);
  538. }
  539. pPrevChain = pCertChain;
  540. }
  541. if (!dwNumCertificates)
  542. {
  543. dwError = ERROR_NO_MORE_ITEMS;
  544. BAIL_ON_WIN32ERROR(dwError);
  545. }
  546. //
  547. // Allocate one more authinfo for pre-sharedkey.
  548. //
  549. pAuthInfo = (INT_IPSEC_MM_AUTH_INFO *)AllocADsMem(sizeof(INT_IPSEC_MM_AUTH_INFO) * (1 + dwNumCertificates));
  550. if (!pAuthInfo)
  551. {
  552. dwError = ERROR_OUTOFMEMORY;
  553. BAIL_ON_WIN32ERROR(dwError);
  554. }
  555. pTemp = pCertificateList;
  556. for (i = 0; i < dwNumCertificates; i++)
  557. {
  558. pCurrentAuth = pAuthInfo + i;
  559. dwError = CopyCertificateNode( pCurrentAuth, pTemp);
  560. BAIL_ON_WIN32ERROR(dwError);
  561. pTemp = pTemp->pNext;
  562. }
  563. *ppAuthInfo = pAuthInfo;
  564. *pdwNumCertificates = dwNumCertificates;
  565. error:
  566. if (pCertificateList)
  567. {
  568. FreeCertificateList(pCertificateList);
  569. }
  570. if(dwError != 0)
  571. {
  572. *ppAuthInfo = NULL;
  573. *pdwNumCertificates = 0;
  574. }
  575. return(dwError);
  576. }
  577. ///////////////////////////////////////////////////////////////////////////////////////////
  578. //
  579. // Function : CopyCertificate
  580. //
  581. // Date of Creation: 10-10-2001
  582. //
  583. // Parameters : IN PINT_IPSEC_MM_AUTH_INFO pCurrentAuth,
  584. // IN PINT_IPSEC_MM_AUTH_INFO pCurrentAuthFrom
  585. // Return : DWORD
  586. //
  587. // Description : Copies certificate
  588. //
  589. // Revision History:
  590. //
  591. // Date Author Comments
  592. //
  593. //////////////////////////////////////////////////////////////////////////////////////////
  594. DWORD
  595. CopyCertificate(
  596. IN PINT_IPSEC_MM_AUTH_INFO pCurrentAuth,
  597. IN PINT_IPSEC_MM_AUTH_INFO pCurrentAuthFrom
  598. )
  599. {
  600. DWORD dwReturn = ERROR_SUCCESS;
  601. pCurrentAuth->AuthMethod = pCurrentAuthFrom->AuthMethod;
  602. dwReturn = EncodeCertificateName ((LPTSTR)pCurrentAuthFrom->pAuthInfo,
  603. &pCurrentAuth->pAuthInfo,
  604. &pCurrentAuth->dwAuthInfoSize);
  605. return dwReturn;
  606. }
  607. ///////////////////////////////////////////////////////////////////////////////////////////
  608. //
  609. // Function : IsDomainMember
  610. //
  611. // Date of Creation: 10-10-2001
  612. //
  613. // Parameters : IN LPTSTR pszMachine
  614. //
  615. // Return : BOOL
  616. //
  617. // Description : Checks for member of a domain(2k) or not of pszMachine.
  618. //
  619. // Revision History:
  620. //
  621. // Date Author Comments
  622. //
  623. //////////////////////////////////////////////////////////////////////////////////////////
  624. BOOL
  625. IsDomainMember(
  626. IN LPTSTR pszMachine
  627. )
  628. {
  629. BOOL bIsDomainMember = FALSE;
  630. do
  631. {
  632. NTSTATUS lStatus;
  633. LSA_HANDLE hLsa;
  634. LSA_OBJECT_ATTRIBUTES ObjectAttributes;
  635. LSA_UNICODE_STRING LsaString;
  636. // Initialize the object attributes to all zeroes.
  637. ZeroMemory( &ObjectAttributes, sizeof( ObjectAttributes ) );
  638. if ((pszMachine == NULL) || !wcscmp(pszMachine, L"\0"))
  639. {
  640. LsaString.Buffer = NULL;
  641. LsaString.Length = 0;
  642. LsaString.MaximumLength = 0;
  643. }
  644. else
  645. {
  646. LsaString.Buffer = pszMachine;
  647. LsaString.Length = (USHORT) (wcslen(pszMachine)* sizeof(WCHAR));
  648. LsaString.MaximumLength = (USHORT) (wcslen(pszMachine) + 1) * sizeof(WCHAR);
  649. }
  650. // Attempt to open the policy.
  651. lStatus = LsaOpenPolicy( &LsaString, &ObjectAttributes,
  652. GENERIC_READ | POLICY_VIEW_LOCAL_INFORMATION, &hLsa );
  653. // Exit on error.
  654. if (lStatus)
  655. {
  656. break;
  657. }
  658. // Query domain information
  659. PPOLICY_PRIMARY_DOMAIN_INFO ppdiDomainInfo;
  660. lStatus = LsaQueryInformationPolicy( hLsa, PolicyPrimaryDomainInformation,
  661. (PVOID*)&ppdiDomainInfo );
  662. // Exit on error.
  663. if (lStatus)
  664. {
  665. break;
  666. }
  667. //
  668. // Check the Sid pointer, if it is null, the workstation is either a
  669. // stand-alone computer or a member of a workgroup.
  670. //
  671. if( ppdiDomainInfo->Sid )
  672. {
  673. bIsDomainMember = TRUE;
  674. }
  675. //
  676. // Clean up
  677. //
  678. if (NULL != ppdiDomainInfo)
  679. LsaFreeMemory( ppdiDomainInfo );
  680. } while (0);
  681. return bIsDomainMember;
  682. }
  683. ///////////////////////////////////////////////////////////////////////////////////////////
  684. //
  685. // Function : SmartDefaults
  686. //
  687. // Date of Creation: 10-10-2001
  688. //
  689. // Parameters : IN PINT_IPSEC_MM_AUTH_INFO pAuthInfo,
  690. // IN LPTSTRT pszMachine,
  691. // IN DWORD * pdwNumberOfAuth,
  692. // IN BOOL bIsDomainPolicy
  693. //
  694. // Return : DWORD
  695. //
  696. // Description : Loads smart defaults of pszMachine in pAuthinfo.
  697. //
  698. // Revision History:
  699. //
  700. // Date Author Comments
  701. //
  702. //////////////////////////////////////////////////////////////////////////////////////////
  703. DWORD
  704. SmartDefaults(
  705. IN PINT_IPSEC_MM_AUTH_INFO *ppAuthInfo,
  706. IN LPTSTR pszMachine,
  707. IN DWORD * pdwNumberOfAuth,
  708. IN BOOL bIsDomainPolicy
  709. )
  710. {
  711. HCERTSTORE hCertStore = NULL;
  712. INT_IPSEC_MM_AUTH_INFO * pAuthInfoIKE = NULL, * pAuthInfoOthers = NULL;
  713. PINT_IPSEC_MM_AUTH_INFO pAuthInfo = NULL;
  714. DWORD dwReturn = ERROR_SUCCESS, i=0;
  715. DWORD dwNumCertificatesIKE = 0, dwNumCertificatesOthers = 0;
  716. _TCHAR szMachine[MACHINE_NAME] = {0};
  717. if((pszMachine == NULL) || (!wcscmp(pszMachine, L"\0")))
  718. {
  719. wcscpy(szMachine, L"MY\0");
  720. }
  721. else
  722. {
  723. wcscpy(szMachine, pszMachine);
  724. wcscat(szMachine, L"\\MY\0");
  725. }
  726. if (bIsDomainPolicy || IsDomainMember(pszMachine))
  727. {
  728. hCertStore = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0,
  729. CERT_STORE_OPEN_EXISTING_FLAG | CERT_STORE_READONLY_FLAG | CERT_SYSTEM_STORE_LOCAL_MACHINE,
  730. szMachine);
  731. if(hCertStore)
  732. {
  733. //
  734. // First fill IKE intermediate
  735. //
  736. dwReturn = ListCertChainsInStore( hCertStore, &pAuthInfoIKE,
  737. &dwNumCertificatesIKE, szOID_IPSEC_KP_IKE_INTERMEDIATE );
  738. if(dwNumCertificatesIKE == 0)
  739. {
  740. dwReturn = ERROR_SUCCESS;
  741. }
  742. if(dwReturn == ERROR_SUCCESS)
  743. {
  744. dwReturn = ListCertsInStore( hCertStore, &pAuthInfoOthers, &dwNumCertificatesOthers);
  745. }
  746. if(dwNumCertificatesOthers == 0)
  747. {
  748. dwReturn = ERROR_SUCCESS;
  749. }
  750. }
  751. else
  752. {
  753. dwReturn = GetLastError();
  754. BAIL_ON_WIN32ERROR(dwReturn)
  755. }
  756. }
  757. *pdwNumberOfAuth = dwNumCertificatesIKE + dwNumCertificatesOthers + 1;
  758. pAuthInfo = new INT_IPSEC_MM_AUTH_INFO[(*pdwNumberOfAuth)];
  759. if(pAuthInfo == NULL)
  760. {
  761. dwReturn = ERROR_OUTOFMEMORY;
  762. BAIL_ON_WIN32ERROR(dwReturn)
  763. }
  764. ZeroMemory(pAuthInfo, sizeof(INT_IPSEC_MM_AUTH_INFO) * (*pdwNumberOfAuth));
  765. if(dwReturn == ERROR_SUCCESS)
  766. {
  767. for( i=0; i< dwNumCertificatesIKE; i++)
  768. {
  769. CopyCertificate( &pAuthInfo[i], &pAuthInfoIKE[i]);
  770. }
  771. for( i=0; i< dwNumCertificatesOthers; i++)
  772. {
  773. CopyCertificate( &pAuthInfo[i+dwNumCertificatesIKE], &pAuthInfoOthers[i]);
  774. }
  775. pAuthInfo[(*pdwNumberOfAuth)-1].AuthMethod = IKE_SSPI;
  776. pAuthInfo[(*pdwNumberOfAuth)-1].dwAuthInfoSize = 0;
  777. pAuthInfo[(*pdwNumberOfAuth)-1].pAuthInfo = (LPBYTE)new _TCHAR[1];
  778. if(pAuthInfo[(*pdwNumberOfAuth)-1].pAuthInfo != NULL)
  779. {
  780. pAuthInfo[(*pdwNumberOfAuth)-1].pAuthInfo[0] = UNICODE_NULL;
  781. }
  782. else
  783. {
  784. dwReturn = ERROR_OUTOFMEMORY;
  785. BAIL_ON_WIN32ERROR(dwReturn)
  786. }
  787. }
  788. error:
  789. *ppAuthInfo = pAuthInfo;
  790. if(pAuthInfoIKE)
  791. FreeADsMem(pAuthInfoIKE);
  792. if(pAuthInfoOthers)
  793. FreeADsMem(pAuthInfoOthers);
  794. return dwReturn;
  795. }