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.

1598 lines
39 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: rules-r.c
  7. //
  8. // Contents: Rule management for registry.
  9. //
  10. //
  11. // History: KrishnaG.
  12. // AbhisheV.
  13. //
  14. //----------------------------------------------------------------------------
  15. #include "precomp.h"
  16. extern LPWSTR NFADNAttributes[];
  17. #define AUTH_VERSION_ONE 1
  18. #define AUTH_VERSION_TWO 2
  19. DWORD
  20. RegCreateNFAData(
  21. HKEY hRegistryKey,
  22. LPWSTR pszIpsecRootContainer,
  23. GUID PolicyIdentifier,
  24. LPWSTR pszLocationName,
  25. PIPSEC_NFA_DATA pIpsecNFAData
  26. )
  27. {
  28. DWORD dwError = 0;
  29. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  30. WCHAR szAbsNFAReference[MAX_PATH];
  31. LPWSTR pszAbsPolicyReference = NULL;
  32. LPWSTR pszRelPolicyReference = NULL;
  33. LPWSTR pszRelFilterReference = NULL;
  34. LPWSTR pszRelNegPolReference = NULL;
  35. DWORD dwRootPathLen = 0;
  36. LPWSTR pszIpsecPolicyRef = NULL;
  37. BOOL bIsActive = FALSE;
  38. dwRootPathLen = wcslen(pszIpsecRootContainer);
  39. dwError = RegMarshallNFAObject(
  40. pIpsecNFAData,
  41. pszIpsecRootContainer,
  42. &pIpsecNFAObject
  43. );
  44. BAIL_ON_WIN32_ERROR(dwError);
  45. //
  46. // Create the NFA object in the store.
  47. //
  48. dwError = RegCreateNFAObject(
  49. hRegistryKey,
  50. pszIpsecRootContainer,
  51. pIpsecNFAObject
  52. );
  53. BAIL_ON_WIN32_ERROR(dwError);
  54. dwError = ConvertGuidToPolicyString(
  55. PolicyIdentifier,
  56. pszIpsecRootContainer,
  57. &pszAbsPolicyReference
  58. );
  59. BAIL_ON_WIN32_ERROR(dwError);
  60. pszRelPolicyReference = pszAbsPolicyReference
  61. + dwRootPathLen + 1;
  62. szAbsNFAReference[0] = L'\0';
  63. wcscpy(szAbsNFAReference, pszIpsecRootContainer);
  64. wcscat(szAbsNFAReference, L"\\");
  65. wcscat(szAbsNFAReference, pIpsecNFAObject->pszDistinguishedName);
  66. //
  67. // Write the policy object reference.
  68. //
  69. dwError = RegAddNFAReferenceToPolicyObject(
  70. hRegistryKey,
  71. pszRelPolicyReference,
  72. szAbsNFAReference
  73. );
  74. BAIL_ON_WIN32_ERROR(dwError);
  75. //
  76. // Write the NFA object reference.
  77. //
  78. dwError = RegAddPolicyReferenceToNFAObject(
  79. hRegistryKey,
  80. pIpsecNFAObject->pszDistinguishedName,
  81. pszAbsPolicyReference
  82. );
  83. BAIL_ON_WIN32_ERROR(dwError);
  84. //
  85. // Write the filter object reference for the NFA
  86. // only if the NFA is not a default rule.
  87. //
  88. if (pIpsecNFAObject->pszIpsecFilterReference) {
  89. pszRelFilterReference = pIpsecNFAObject->pszIpsecFilterReference
  90. + dwRootPathLen + 1;
  91. dwError = RegAddNFAReferenceToFilterObject(
  92. hRegistryKey,
  93. pszRelFilterReference,
  94. szAbsNFAReference
  95. );
  96. BAIL_ON_WIN32_ERROR(dwError);
  97. }
  98. //
  99. // Write the NFA object reference for the filter
  100. // only if the NFA is not a default rule.
  101. //
  102. if (pIpsecNFAObject->pszIpsecFilterReference) {
  103. dwError = RegAddFilterReferenceToNFAObject(
  104. hRegistryKey,
  105. pIpsecNFAObject->pszDistinguishedName,
  106. pIpsecNFAObject->pszIpsecFilterReference
  107. );
  108. BAIL_ON_WIN32_ERROR(dwError);
  109. }
  110. //
  111. // Write the negpol object reference for the NFA.
  112. //
  113. pszRelNegPolReference = pIpsecNFAObject->pszIpsecNegPolReference
  114. + dwRootPathLen + 1;
  115. dwError = RegAddNFAReferenceToNegPolObject(
  116. hRegistryKey,
  117. pszRelNegPolReference,
  118. szAbsNFAReference
  119. );
  120. BAIL_ON_WIN32_ERROR(dwError);
  121. //
  122. // Write the NFA object reference for the negpol.
  123. //
  124. dwError = RegAddNegPolReferenceToNFAObject(
  125. hRegistryKey,
  126. pIpsecNFAObject->pszDistinguishedName,
  127. pIpsecNFAObject->pszIpsecNegPolReference
  128. );
  129. BAIL_ON_WIN32_ERROR(dwError);
  130. dwError = IsRegPolicyCurrentlyActive(
  131. hRegistryKey,
  132. pszIpsecRootContainer,
  133. PolicyIdentifier,
  134. &bIsActive
  135. );
  136. BAIL_ON_WIN32_ERROR(dwError);
  137. if (bIsActive) {
  138. dwError = PingPolicyAgentSvc(pszLocationName);
  139. BAIL_ON_WIN32_ERROR(dwError);
  140. }
  141. error:
  142. if (pIpsecNFAObject) {
  143. FreeIpsecNFAObject(pIpsecNFAObject);
  144. }
  145. if (pszAbsPolicyReference) {
  146. FreePolStr(pszAbsPolicyReference);
  147. }
  148. return(dwError);
  149. }
  150. DWORD
  151. RegSetNFAData(
  152. HKEY hRegistryKey,
  153. LPWSTR pszIpsecRootContainer,
  154. GUID PolicyIdentifier,
  155. LPWSTR pszLocationName,
  156. PIPSEC_NFA_DATA pIpsecNFAData
  157. )
  158. {
  159. DWORD dwError = 0;
  160. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  161. WCHAR szAbsNFAReference[MAX_PATH];
  162. LPWSTR pszAbsOldFilterRef = NULL;
  163. LPWSTR pszAbsOldNegPolRef = NULL;
  164. LPWSTR pszRelOldFilterRef = NULL;
  165. LPWSTR pszRelOldNegPolRef = NULL;
  166. LPWSTR pszRelFilterReference = NULL;
  167. LPWSTR pszRelNegPolReference = NULL;
  168. DWORD dwRootPathLen = 0;
  169. dwRootPathLen = wcslen(pszIpsecRootContainer);
  170. dwError = RegGetNFAExistingFilterRef(
  171. hRegistryKey,
  172. pIpsecNFAData,
  173. &pszAbsOldFilterRef
  174. );
  175. //
  176. // Filter Reference can be null for a default rule.
  177. // BAIL_ON_WIN32_ERROR(dwError);
  178. //
  179. if (pszAbsOldFilterRef && *pszAbsOldFilterRef) {
  180. pszRelOldFilterRef = pszAbsOldFilterRef + dwRootPathLen + 1;
  181. }
  182. dwError = RegGetNFAExistingNegPolRef(
  183. hRegistryKey,
  184. pIpsecNFAData,
  185. &pszAbsOldNegPolRef
  186. );
  187. // BAIL_ON_WIN32_ERROR(dwError);
  188. if (pszAbsOldNegPolRef && *pszAbsOldNegPolRef) {
  189. pszRelOldNegPolRef = pszAbsOldNegPolRef + dwRootPathLen + 1;
  190. }
  191. //
  192. // Marshall to update the NFA object in the store
  193. //
  194. dwError = RegMarshallNFAObject(
  195. pIpsecNFAData,
  196. pszIpsecRootContainer,
  197. &pIpsecNFAObject
  198. );
  199. BAIL_ON_WIN32_ERROR(dwError);
  200. //
  201. // Update the NFA object
  202. //
  203. dwError = RegSetNFAObject(
  204. hRegistryKey,
  205. pszIpsecRootContainer,
  206. pIpsecNFAObject
  207. );
  208. BAIL_ON_WIN32_ERROR(dwError);
  209. szAbsNFAReference[0] = L'\0';
  210. wcscpy(szAbsNFAReference, pszIpsecRootContainer);
  211. wcscat(szAbsNFAReference, L"\\");
  212. wcscat(szAbsNFAReference, pIpsecNFAObject->pszDistinguishedName);
  213. if (pszRelOldFilterRef && *pszRelOldFilterRef) {
  214. dwError = RegDeleteNFAReferenceInFilterObject(
  215. hRegistryKey,
  216. pszRelOldFilterRef,
  217. szAbsNFAReference
  218. );
  219. // BAIL_ON_WIN32_ERROR(dwError);
  220. }
  221. //
  222. // Write the new filter object reference for the NFA.
  223. //
  224. if (pIpsecNFAObject->pszIpsecFilterReference) {
  225. pszRelFilterReference = pIpsecNFAObject->pszIpsecFilterReference
  226. + dwRootPathLen + 1;
  227. dwError = RegAddNFAReferenceToFilterObject(
  228. hRegistryKey,
  229. pszRelFilterReference,
  230. szAbsNFAReference
  231. );
  232. BAIL_ON_WIN32_ERROR(dwError);
  233. }
  234. //
  235. // Update the NFA object reference for the filter.
  236. //
  237. if (pIpsecNFAObject->pszIpsecFilterReference) {
  238. dwError = RegUpdateFilterReferenceInNFAObject(
  239. hRegistryKey,
  240. pIpsecNFAObject->pszDistinguishedName,
  241. pszAbsOldFilterRef,
  242. pIpsecNFAObject->pszIpsecFilterReference
  243. );
  244. BAIL_ON_WIN32_ERROR(dwError);
  245. }
  246. else {
  247. dwError = RegDelFilterRefValueOfNFAObject(
  248. hRegistryKey,
  249. pIpsecNFAObject->pszDistinguishedName
  250. );
  251. // BAIL_ON_WIN32_ERROR(dwError);
  252. }
  253. //
  254. // Write the new negpol object reference for the NFA.
  255. //
  256. pszRelNegPolReference = pIpsecNFAObject->pszIpsecNegPolReference
  257. + dwRootPathLen + 1;
  258. if (pszRelOldNegPolRef && *pszRelOldNegPolRef) {
  259. dwError = RegDeleteNFAReferenceInNegPolObject(
  260. hRegistryKey,
  261. pszRelOldNegPolRef,
  262. szAbsNFAReference
  263. );
  264. // BAIL_ON_WIN32_ERROR(dwError);
  265. }
  266. dwError = RegAddNFAReferenceToNegPolObject(
  267. hRegistryKey,
  268. pszRelNegPolReference,
  269. szAbsNFAReference
  270. );
  271. BAIL_ON_WIN32_ERROR(dwError);
  272. //
  273. // Update the NFA object reference for the negpol.
  274. //
  275. dwError = RegUpdateNegPolReferenceInNFAObject(
  276. hRegistryKey,
  277. pIpsecNFAObject->pszDistinguishedName,
  278. pszAbsOldNegPolRef,
  279. pIpsecNFAObject->pszIpsecNegPolReference
  280. );
  281. BAIL_ON_WIN32_ERROR(dwError);
  282. dwError = RegBackPropIncChangesForNFAToPolicy(
  283. hRegistryKey,
  284. pszIpsecRootContainer,
  285. pszLocationName,
  286. pIpsecNFAObject->pszDistinguishedName
  287. );
  288. BAIL_ON_WIN32_ERROR(dwError);
  289. error:
  290. if (pIpsecNFAObject) {
  291. FreeIpsecNFAObject(pIpsecNFAObject);
  292. }
  293. if (pszAbsOldFilterRef) {
  294. FreePolStr(pszAbsOldFilterRef);
  295. }
  296. if (pszAbsOldNegPolRef) {
  297. FreePolStr(pszAbsOldNegPolRef);
  298. }
  299. return(dwError);
  300. }
  301. DWORD
  302. RegDeleteNFAData(
  303. HKEY hRegistryKey,
  304. LPWSTR pszIpsecRootContainer,
  305. GUID PolicyIdentifier,
  306. LPWSTR pszLocationName,
  307. PIPSEC_NFA_DATA pIpsecNFAData
  308. )
  309. {
  310. DWORD dwError = 0;
  311. LPWSTR pszAbsPolicyReference = NULL;
  312. LPWSTR pszRelPolicyReference = NULL;
  313. WCHAR szAbsNFAReference[MAX_PATH];
  314. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  315. DWORD dwRootPathLen = 0;
  316. LPWSTR pszRelNegPolReference = NULL;
  317. LPWSTR pszRelFilterReference = NULL;
  318. BOOL bIsActive = FALSE;
  319. dwRootPathLen = wcslen(pszIpsecRootContainer);
  320. dwError = ConvertGuidToPolicyString(
  321. PolicyIdentifier,
  322. pszIpsecRootContainer,
  323. &pszAbsPolicyReference
  324. );
  325. BAIL_ON_WIN32_ERROR(dwError);
  326. pszRelPolicyReference = pszAbsPolicyReference
  327. + dwRootPathLen + 1;
  328. dwError = RegMarshallNFAObject(
  329. pIpsecNFAData,
  330. pszIpsecRootContainer,
  331. &pIpsecNFAObject
  332. );
  333. BAIL_ON_WIN32_ERROR(dwError);
  334. szAbsNFAReference[0] = L'\0';
  335. wcscpy(szAbsNFAReference, pszIpsecRootContainer);
  336. wcscat(szAbsNFAReference, L"\\");
  337. wcscat(szAbsNFAReference, pIpsecNFAObject->pszDistinguishedName);
  338. //
  339. // Remove the NFA reference from the policy object.
  340. //
  341. dwError = RegRemoveNFAReferenceFromPolicyObject(
  342. hRegistryKey,
  343. pszRelPolicyReference,
  344. szAbsNFAReference
  345. );
  346. BAIL_ON_WIN32_ERROR(dwError);
  347. pszRelNegPolReference = pIpsecNFAObject->pszIpsecNegPolReference
  348. + dwRootPathLen + 1;
  349. dwError = RegDeleteNFAReferenceInNegPolObject(
  350. hRegistryKey,
  351. pszRelNegPolReference,
  352. szAbsNFAReference
  353. );
  354. // BAIL_ON_WIN32_ERROR(dwError);
  355. if (pIpsecNFAObject->pszIpsecFilterReference) {
  356. pszRelFilterReference = pIpsecNFAObject->pszIpsecFilterReference
  357. + dwRootPathLen + 1;
  358. dwError = RegDeleteNFAReferenceInFilterObject(
  359. hRegistryKey,
  360. pszRelFilterReference,
  361. szAbsNFAReference
  362. );
  363. // BAIL_ON_WIN32_ERROR(dwError);
  364. }
  365. dwError = RegDeleteKeyW(
  366. hRegistryKey,
  367. pIpsecNFAObject->pszDistinguishedName
  368. );
  369. BAIL_ON_WIN32_ERROR(dwError);
  370. dwError = IsRegPolicyCurrentlyActive(
  371. hRegistryKey,
  372. pszIpsecRootContainer,
  373. PolicyIdentifier,
  374. &bIsActive
  375. );
  376. BAIL_ON_WIN32_ERROR(dwError);
  377. if (bIsActive) {
  378. dwError = PingPolicyAgentSvc(pszLocationName);
  379. BAIL_ON_WIN32_ERROR(dwError);
  380. }
  381. error:
  382. if (pIpsecNFAObject) {
  383. FreeIpsecNFAObject(pIpsecNFAObject);
  384. }
  385. if (pszAbsPolicyReference) {
  386. FreePolStr(pszAbsPolicyReference);
  387. }
  388. return(dwError);
  389. }
  390. DWORD
  391. RegEnumNFAData(
  392. HKEY hRegistryKey,
  393. LPWSTR pszIpsecRootContainer,
  394. GUID PolicyIdentifier,
  395. PIPSEC_NFA_DATA ** pppIpsecNFAData,
  396. PDWORD pdwNumNFAObjects
  397. )
  398. {
  399. DWORD dwError = 0;
  400. DWORD i = 0;
  401. PIPSEC_NFA_OBJECT * ppIpsecNFAObject = NULL;
  402. DWORD dwNumNFAObjects = 0;
  403. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  404. PIPSEC_NFA_DATA * ppIpsecNFAData = NULL;
  405. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  406. LPWSTR pszAbsPolicyReference = NULL;
  407. LPWSTR pszRelPolicyReference = NULL;
  408. DWORD dwRootPathLen = 0;
  409. DWORD j = 0;
  410. dwRootPathLen = wcslen(pszIpsecRootContainer);
  411. dwError = ConvertGuidToPolicyString(
  412. PolicyIdentifier,
  413. pszIpsecRootContainer,
  414. &pszAbsPolicyReference
  415. );
  416. BAIL_ON_WIN32_ERROR(dwError);
  417. pszRelPolicyReference = pszAbsPolicyReference
  418. + dwRootPathLen + 1;
  419. dwError = RegEnumNFAObjects(
  420. hRegistryKey,
  421. pszIpsecRootContainer,
  422. pszRelPolicyReference,
  423. &ppIpsecNFAObject,
  424. &dwNumNFAObjects
  425. );
  426. BAIL_ON_WIN32_ERROR(dwError);
  427. if (dwNumNFAObjects) {
  428. ppIpsecNFAData = (PIPSEC_NFA_DATA *)AllocPolMem(
  429. sizeof(PIPSEC_NFA_DATA)*dwNumNFAObjects
  430. );
  431. if (!ppIpsecNFAData) {
  432. dwError = ERROR_OUTOFMEMORY;
  433. BAIL_ON_WIN32_ERROR(dwError);
  434. }
  435. }
  436. for (i = 0; i < dwNumNFAObjects; i++) {
  437. pIpsecNFAObject = *(ppIpsecNFAObject + i);
  438. dwError = RegUnmarshallNFAData(
  439. pIpsecNFAObject,
  440. &pIpsecNFAData
  441. );
  442. if (!dwError) {
  443. *(ppIpsecNFAData + j) = pIpsecNFAData;
  444. j++;
  445. }
  446. }
  447. if (j == 0) {
  448. if (ppIpsecNFAData) {
  449. FreePolMem(ppIpsecNFAData);
  450. ppIpsecNFAData = NULL;
  451. }
  452. }
  453. *pppIpsecNFAData = ppIpsecNFAData;
  454. *pdwNumNFAObjects = j;
  455. dwError = ERROR_SUCCESS;
  456. cleanup:
  457. if (ppIpsecNFAObject) {
  458. FreeIpsecNFAObjects(
  459. ppIpsecNFAObject,
  460. dwNumNFAObjects
  461. );
  462. }
  463. if (pszAbsPolicyReference) {
  464. FreePolStr(pszAbsPolicyReference);
  465. }
  466. return(dwError);
  467. error:
  468. if (ppIpsecNFAData) {
  469. FreeMulIpsecNFAData(
  470. ppIpsecNFAData,
  471. i
  472. );
  473. }
  474. *pppIpsecNFAData = NULL;
  475. *pdwNumNFAObjects = 0;
  476. goto cleanup;
  477. }
  478. DWORD
  479. RegEnumNFAObjects(
  480. HKEY hRegistryKey,
  481. LPWSTR pszIpsecRootContainer,
  482. LPWSTR pszIpsecRelPolicyName,
  483. PIPSEC_NFA_OBJECT ** pppIpsecNFAObjects,
  484. PDWORD pdwNumNFAObjects
  485. )
  486. {
  487. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  488. PIPSEC_NFA_OBJECT * ppIpsecNFAObjects = NULL;
  489. DWORD dwNumNFAObjects = 0;
  490. DWORD dwError = 0;
  491. DWORD dwSize = 0;
  492. HKEY hRegKey = 0;
  493. DWORD i = 0;
  494. DWORD dwCount = 0;
  495. LPWSTR * ppszIpsecNFANames = NULL;
  496. LPWSTR pszIpsecNFAName = NULL;
  497. LPWSTR pszTemp = NULL;
  498. LPWSTR pszString = NULL;
  499. LPWSTR pszIpsecNFAReference = NULL;
  500. LPWSTR pszFilterReference = NULL;
  501. LPWSTR pszNegPolReference = NULL;
  502. *pppIpsecNFAObjects = NULL;
  503. *pdwNumNFAObjects = 0;
  504. dwError = RegOpenKeyExW(
  505. hRegistryKey,
  506. pszIpsecRelPolicyName,
  507. 0,
  508. KEY_ALL_ACCESS,
  509. &hRegKey
  510. );
  511. BAIL_ON_WIN32_ERROR(dwError);
  512. dwError = RegstoreQueryValue(
  513. hRegKey,
  514. L"ipsecNFAReference",
  515. REG_MULTI_SZ,
  516. (LPBYTE *)&pszIpsecNFAReference,
  517. &dwSize
  518. );
  519. BAIL_ON_WIN32_ERROR(dwError);
  520. pszTemp = pszIpsecNFAReference;
  521. while (*pszTemp != L'\0') {
  522. pszTemp += wcslen(pszTemp) + 1;
  523. dwCount++;
  524. }
  525. if (!dwCount) {
  526. dwError = ERROR_NO_DATA;
  527. BAIL_ON_WIN32_ERROR(dwError);
  528. }
  529. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  530. sizeof(LPWSTR)*dwCount
  531. );
  532. if (!ppszIpsecNFANames) {
  533. dwError = ERROR_OUTOFMEMORY;
  534. BAIL_ON_WIN32_ERROR(dwError);
  535. }
  536. pszTemp = pszIpsecNFAReference;
  537. for (i = 0; i < dwCount; i++) {
  538. pszString = AllocPolStr(pszTemp);
  539. if (!pszString) {
  540. dwError = ERROR_OUTOFMEMORY;
  541. BAIL_ON_WIN32_ERROR(dwError);
  542. }
  543. *(ppszIpsecNFANames + i) = pszString;
  544. pszTemp += wcslen(pszTemp) + 1; //for the null terminator;
  545. }
  546. ppIpsecNFAObjects = (PIPSEC_NFA_OBJECT *)AllocPolMem(
  547. sizeof(PIPSEC_NFA_OBJECT)*dwCount
  548. );
  549. if (!ppIpsecNFAObjects) {
  550. dwError = ERROR_OUTOFMEMORY;
  551. BAIL_ON_WIN32_ERROR(dwError);
  552. }
  553. for (i = 0; i < dwCount; i++) {
  554. dwError = UnMarshallRegistryNFAObject(
  555. hRegistryKey,
  556. pszIpsecRootContainer,
  557. *(ppszIpsecNFANames + i),
  558. &pIpsecNFAObject,
  559. &pszFilterReference,
  560. &pszNegPolReference
  561. );
  562. if (dwError == ERROR_SUCCESS) {
  563. *(ppIpsecNFAObjects + dwNumNFAObjects) = pIpsecNFAObject;
  564. dwNumNFAObjects++;
  565. if (pszFilterReference) {
  566. FreePolStr(pszFilterReference);
  567. }
  568. if (pszNegPolReference) {
  569. FreePolStr(pszNegPolReference);
  570. }
  571. }
  572. }
  573. *pppIpsecNFAObjects = ppIpsecNFAObjects;
  574. *pdwNumNFAObjects = dwNumNFAObjects;
  575. dwError = ERROR_SUCCESS;
  576. cleanup:
  577. if (hRegKey) {
  578. RegCloseKey(hRegKey);
  579. }
  580. if (pszIpsecNFAReference) {
  581. FreePolStr(pszIpsecNFAReference);
  582. }
  583. if (ppszIpsecNFANames) {
  584. FreeNFAReferences(
  585. ppszIpsecNFANames,
  586. dwCount
  587. );
  588. }
  589. return(dwError);
  590. error:
  591. if (ppIpsecNFAObjects) {
  592. FreeIpsecNFAObjects(
  593. ppIpsecNFAObjects,
  594. dwNumNFAObjects
  595. );
  596. }
  597. *pppIpsecNFAObjects = NULL;
  598. *pdwNumNFAObjects = 0;
  599. goto cleanup;
  600. }
  601. DWORD
  602. RegCreateNFAObject(
  603. HKEY hRegistryKey,
  604. LPWSTR pszIpsecRootContainer,
  605. PIPSEC_NFA_OBJECT pIpsecNFAObject
  606. )
  607. {
  608. DWORD dwError = 0;
  609. dwError = PersistNFAObject(
  610. hRegistryKey,
  611. pIpsecNFAObject
  612. );
  613. BAIL_ON_WIN32_ERROR(dwError);
  614. error:
  615. return(dwError);
  616. }
  617. DWORD
  618. RegSetNFAObject(
  619. HKEY hRegistryKey,
  620. LPWSTR pszIpsecRootContainer,
  621. PIPSEC_NFA_OBJECT pIpsecNFAObject
  622. )
  623. {
  624. DWORD dwError = 0;
  625. dwError = PersistNFAObject(
  626. hRegistryKey,
  627. pIpsecNFAObject
  628. );
  629. BAIL_ON_WIN32_ERROR(dwError);
  630. error:
  631. return(dwError);
  632. }
  633. DWORD
  634. RegUnmarshallNFAData(
  635. PIPSEC_NFA_OBJECT pIpsecNFAObject,
  636. PIPSEC_NFA_DATA * ppIpsecNFAData
  637. )
  638. {
  639. DWORD dwError = 0;
  640. dwError = UnmarshallNFAObject(
  641. pIpsecNFAObject,
  642. IPSEC_REGISTRY_PROVIDER,
  643. ppIpsecNFAData
  644. );
  645. BAIL_ON_WIN32_ERROR(dwError);
  646. error:
  647. return(dwError);
  648. }
  649. DWORD
  650. RegMarshallNFAObject(
  651. PIPSEC_NFA_DATA pIpsecNFAData,
  652. LPWSTR pszIpsecRootContainer,
  653. PIPSEC_NFA_OBJECT * ppIpsecNFAObject
  654. )
  655. {
  656. DWORD dwError = 0;
  657. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  658. WCHAR szGuid[MAX_PATH];
  659. WCHAR szDistinguishedName[MAX_PATH];
  660. LPBYTE pBuffer = NULL;
  661. DWORD dwBufferLen = 0;
  662. LPWSTR pszStringUuid = NULL;
  663. LPWSTR pszIpsecFilterReference = NULL;
  664. LPWSTR pszIpsecNegPolReference = NULL;
  665. GUID ZeroGuid;
  666. time_t PresentTime;
  667. memset(&ZeroGuid, 0, sizeof(GUID));
  668. szGuid[0] = L'\0';
  669. szDistinguishedName[0] = L'\0';
  670. pIpsecNFAObject = (PIPSEC_NFA_OBJECT)AllocPolMem(
  671. sizeof(IPSEC_NFA_OBJECT)
  672. );
  673. if (!pIpsecNFAObject) {
  674. dwError = ERROR_OUTOFMEMORY;
  675. BAIL_ON_WIN32_ERROR(dwError);
  676. }
  677. dwError = UuidToString(
  678. &pIpsecNFAData->NFAIdentifier,
  679. &pszStringUuid
  680. );
  681. BAIL_ON_WIN32_ERROR(dwError);
  682. wcscpy(szGuid, L"{");
  683. wcscat(szGuid, pszStringUuid);
  684. wcscat(szGuid, L"}");
  685. //
  686. // Fill in the distinguishedName
  687. //
  688. wcscpy(szDistinguishedName,L"ipsecNFA");
  689. wcscat(szDistinguishedName, szGuid);
  690. pIpsecNFAObject->pszDistinguishedName = AllocPolStr(
  691. szDistinguishedName
  692. );
  693. if (!pIpsecNFAObject->pszDistinguishedName) {
  694. dwError = ERROR_OUTOFMEMORY;
  695. BAIL_ON_WIN32_ERROR(dwError);
  696. }
  697. //
  698. // Fill in the ipsecName
  699. //
  700. if (pIpsecNFAData->pszIpsecName &&
  701. *pIpsecNFAData->pszIpsecName) {
  702. pIpsecNFAObject->pszIpsecName = AllocPolStr(
  703. pIpsecNFAData->pszIpsecName
  704. );
  705. if (!pIpsecNFAObject->pszIpsecName) {
  706. dwError = ERROR_OUTOFMEMORY;
  707. BAIL_ON_WIN32_ERROR(dwError);
  708. }
  709. }
  710. if (pIpsecNFAData->pszDescription &&
  711. *pIpsecNFAData->pszDescription) {
  712. pIpsecNFAObject->pszDescription = AllocPolStr(
  713. pIpsecNFAData->pszDescription
  714. );
  715. if (!pIpsecNFAObject->pszDescription) {
  716. dwError = ERROR_OUTOFMEMORY;
  717. BAIL_ON_WIN32_ERROR(dwError);
  718. }
  719. }
  720. //
  721. // Fill in the ipsecID
  722. //
  723. pIpsecNFAObject->pszIpsecID = AllocPolStr(
  724. szGuid
  725. );
  726. if (!pIpsecNFAObject->pszIpsecID) {
  727. dwError = ERROR_OUTOFMEMORY;
  728. BAIL_ON_WIN32_ERROR(dwError);
  729. }
  730. //
  731. // Fill in the ipsecDataType
  732. //
  733. pIpsecNFAObject->dwIpsecDataType = 0x100;
  734. //
  735. // Marshall the pIpsecDataBuffer and the Length
  736. //
  737. dwError = MarshallNFABuffer(
  738. pIpsecNFAData,
  739. &pBuffer,
  740. &dwBufferLen
  741. );
  742. BAIL_ON_WIN32_ERROR(dwError);
  743. pIpsecNFAObject->pIpsecData = pBuffer;
  744. pIpsecNFAObject->dwIpsecDataLen = dwBufferLen;
  745. //
  746. // Marshall the Filter Reference.
  747. // There's no filter reference for a default rule.
  748. //
  749. if (memcmp(
  750. &pIpsecNFAData->FilterIdentifier,
  751. &ZeroGuid,
  752. sizeof(GUID))) {
  753. dwError = ConvertGuidToFilterString(
  754. pIpsecNFAData->FilterIdentifier,
  755. pszIpsecRootContainer,
  756. &pszIpsecFilterReference
  757. );
  758. BAIL_ON_WIN32_ERROR(dwError);
  759. pIpsecNFAObject->pszIpsecFilterReference = pszIpsecFilterReference;
  760. }
  761. else {
  762. pIpsecNFAObject->pszIpsecFilterReference = NULL;
  763. }
  764. //
  765. // Marshall the NegPol Reference
  766. //
  767. dwError = ConvertGuidToNegPolString(
  768. pIpsecNFAData->NegPolIdentifier,
  769. pszIpsecRootContainer,
  770. &pszIpsecNegPolReference
  771. );
  772. BAIL_ON_WIN32_ERROR(dwError);
  773. pIpsecNFAObject->pszIpsecNegPolReference = pszIpsecNegPolReference;
  774. time(&PresentTime);
  775. pIpsecNFAObject->dwWhenChanged = (DWORD) PresentTime;
  776. *ppIpsecNFAObject = pIpsecNFAObject;
  777. cleanup:
  778. if (pszStringUuid) {
  779. RpcStringFree(
  780. &pszStringUuid
  781. );
  782. }
  783. return(dwError);
  784. error:
  785. if (pIpsecNFAObject) {
  786. FreeIpsecNFAObject(
  787. pIpsecNFAObject
  788. );
  789. }
  790. *ppIpsecNFAObject = NULL;
  791. goto cleanup;
  792. }
  793. DWORD
  794. MarshallNFABuffer(
  795. PIPSEC_NFA_DATA pIpsecNFAData,
  796. LPBYTE * ppBuffer,
  797. DWORD * pdwBufferLen
  798. )
  799. {
  800. LPBYTE pBuffer = NULL;
  801. LPBYTE pCurrentPos = NULL;
  802. DWORD dwTotalSize = 0;
  803. DWORD dwError = 0;
  804. LPBYTE pAuthMem = NULL;
  805. DWORD dwAuthSize = 0;
  806. DWORD dwInterfaceType = 0;
  807. DWORD dwInterfaceNameLen = 0;
  808. LPWSTR pszInterfaceName = NULL;
  809. DWORD dwTunnelIpAddr = 0;
  810. DWORD dwTunnelFlags = 0;
  811. DWORD dwActiveFlag = 0;
  812. LPWSTR pszEndPointName = NULL;
  813. DWORD dwEndPointNameLen = 0;
  814. PIPSEC_AUTH_METHOD pIpsecAuthMethod = NULL;
  815. DWORD dwNumAuthMethods = 0;
  816. DWORD i = 0;
  817. PSPEC_BUFFER pSpecBuffer = NULL;
  818. PSPEC_BUFFER pSpecBufferV2 = NULL;
  819. // {11BBAC00-498D-11d1-8639-00A0248D3021}
  820. static const GUID GUID_IPSEC_NFA_BLOB =
  821. { 0x11bbac00, 0x498d, 0x11d1, { 0x86, 0x39, 0x0, 0xa0, 0x24, 0x8d, 0x30, 0x21 } };
  822. DWORD dwEffectiveSize = 0;
  823. DWORD dwTotalV2AuthSize=0;
  824. dwTotalSize += sizeof(GUID);
  825. dwTotalSize += sizeof(DWORD);
  826. dwTotalSize += sizeof(DWORD);
  827. dwNumAuthMethods = pIpsecNFAData->dwAuthMethodCount;
  828. if (!dwNumAuthMethods) {
  829. dwError = ERROR_INVALID_PARAMETER;
  830. BAIL_ON_WIN32_ERROR(dwError);
  831. }
  832. pSpecBuffer = AllocPolMem(
  833. sizeof(SPEC_BUFFER)*dwNumAuthMethods
  834. );
  835. if (!pSpecBuffer) {
  836. dwError = ERROR_OUTOFMEMORY;
  837. BAIL_ON_WIN32_ERROR(dwError);
  838. }
  839. pSpecBufferV2 = AllocPolMem(
  840. sizeof(SPEC_BUFFER)*dwNumAuthMethods
  841. );
  842. if (!pSpecBufferV2) {
  843. dwError = ERROR_OUTOFMEMORY;
  844. BAIL_ON_WIN32_ERROR(dwError);
  845. }
  846. for (i = 0; i < dwNumAuthMethods;i++){
  847. pIpsecAuthMethod = *(pIpsecNFAData->ppAuthMethods + i);
  848. dwError = MarshallAuthMethods(
  849. pIpsecAuthMethod,
  850. &pAuthMem,
  851. &dwAuthSize,
  852. AUTH_VERSION_ONE
  853. );
  854. BAIL_ON_WIN32_ERROR(dwError);
  855. dwTotalSize += dwAuthSize;
  856. (pSpecBuffer + i)->dwSize = dwAuthSize;
  857. (pSpecBuffer + i)->pMem = pAuthMem;
  858. }
  859. dwInterfaceType = pIpsecNFAData->dwInterfaceType;
  860. dwTotalSize += sizeof(DWORD);
  861. pszInterfaceName = pIpsecNFAData->pszInterfaceName;
  862. if (pszInterfaceName) {
  863. dwInterfaceNameLen = (wcslen(pszInterfaceName) + 1)*sizeof(WCHAR);
  864. }
  865. else {
  866. dwInterfaceNameLen = sizeof(WCHAR);
  867. }
  868. dwTotalSize += sizeof(DWORD);
  869. dwTotalSize += dwInterfaceNameLen;
  870. dwTunnelIpAddr = pIpsecNFAData->dwTunnelIpAddr;
  871. dwTotalSize += sizeof(DWORD);
  872. dwTunnelFlags = pIpsecNFAData->dwTunnelFlags;
  873. dwTotalSize += sizeof(DWORD);
  874. dwActiveFlag = pIpsecNFAData->dwActiveFlag;
  875. dwTotalSize += sizeof(DWORD);
  876. pszEndPointName = pIpsecNFAData->pszEndPointName;
  877. if (pszEndPointName) {
  878. dwEndPointNameLen = (wcslen(pszEndPointName) + 1)*sizeof(WCHAR);
  879. }
  880. else {
  881. dwEndPointNameLen = sizeof(WCHAR);
  882. }
  883. dwTotalSize += sizeof(DWORD);
  884. dwTotalSize += dwEndPointNameLen;
  885. //
  886. // Marshall version 2 auth data.
  887. //
  888. dwTotalSize += sizeof(GUID);
  889. dwTotalV2AuthSize += sizeof(GUID);
  890. dwTotalSize += sizeof(DWORD);
  891. dwTotalV2AuthSize += sizeof(DWORD);
  892. for (i = 0; i < dwNumAuthMethods; i++) {
  893. pIpsecAuthMethod = *(pIpsecNFAData->ppAuthMethods + i);
  894. dwError = MarshallAuthMethods(
  895. pIpsecAuthMethod,
  896. &pAuthMem,
  897. &dwAuthSize,
  898. AUTH_VERSION_TWO
  899. );
  900. BAIL_ON_WIN32_ERROR(dwError);
  901. dwTotalSize += dwAuthSize;
  902. dwTotalV2AuthSize += dwAuthSize;
  903. (pSpecBufferV2 + i)->dwSize = dwAuthSize;
  904. (pSpecBufferV2 + i)->pMem = pAuthMem;
  905. }
  906. dwTotalSize++;
  907. pBuffer = AllocPolMem(dwTotalSize);
  908. if (!pBuffer) {
  909. dwError = ERROR_OUTOFMEMORY;
  910. BAIL_ON_WIN32_ERROR(dwError);
  911. }
  912. pCurrentPos = pBuffer;
  913. memcpy(pCurrentPos, &GUID_IPSEC_NFA_BLOB, sizeof(GUID));
  914. pCurrentPos += sizeof(GUID);
  915. dwEffectiveSize = dwTotalSize - sizeof(GUID) - sizeof(DWORD) - 1 -
  916. dwTotalV2AuthSize;
  917. memcpy(pCurrentPos, (LPBYTE)&dwEffectiveSize, sizeof(DWORD));
  918. pCurrentPos += sizeof(DWORD);
  919. memcpy(pCurrentPos, (LPBYTE)&dwNumAuthMethods, sizeof(DWORD));
  920. pCurrentPos += sizeof(DWORD);
  921. for (i = 0; i < dwNumAuthMethods; i++) {
  922. pAuthMem = (pSpecBuffer + i)->pMem;
  923. dwAuthSize = (pSpecBuffer + i)->dwSize;
  924. memcpy(pCurrentPos, pAuthMem, dwAuthSize);
  925. pCurrentPos += dwAuthSize;
  926. }
  927. memcpy(pCurrentPos, (LPBYTE)&dwInterfaceType, sizeof(DWORD));
  928. pCurrentPos += sizeof(DWORD);
  929. memcpy(pCurrentPos, (LPBYTE)&dwInterfaceNameLen, sizeof(DWORD));
  930. pCurrentPos += sizeof(DWORD);
  931. if (pszInterfaceName) {
  932. memcpy(pCurrentPos, pszInterfaceName, dwInterfaceNameLen);
  933. }
  934. pCurrentPos += dwInterfaceNameLen;
  935. memcpy(pCurrentPos, (LPBYTE)&dwTunnelIpAddr, sizeof(DWORD));
  936. pCurrentPos += sizeof(DWORD);
  937. memcpy(pCurrentPos, (LPBYTE)&dwTunnelFlags, sizeof(DWORD));
  938. pCurrentPos += sizeof(DWORD);
  939. memcpy(pCurrentPos, (LPBYTE)&dwActiveFlag, sizeof(DWORD));
  940. pCurrentPos += sizeof(DWORD);
  941. memcpy(pCurrentPos, (LPBYTE)&dwEndPointNameLen, sizeof(DWORD));
  942. pCurrentPos += sizeof(DWORD);
  943. if (pszEndPointName) {
  944. memcpy(pCurrentPos, pszEndPointName, dwEndPointNameLen);
  945. }
  946. pCurrentPos += dwEndPointNameLen;
  947. //
  948. // Copy version 2 auth data.
  949. //
  950. memset(pCurrentPos, 1, sizeof(GUID));
  951. pCurrentPos += sizeof(GUID);
  952. memcpy(pCurrentPos, (LPBYTE)&dwNumAuthMethods, sizeof(DWORD));
  953. pCurrentPos += sizeof(DWORD);
  954. for (i = 0; i < dwNumAuthMethods; i++) {
  955. pAuthMem = (pSpecBufferV2 + i)->pMem;
  956. dwAuthSize = (pSpecBufferV2 + i)->dwSize;
  957. memcpy(pCurrentPos, pAuthMem, dwAuthSize);
  958. pCurrentPos += dwAuthSize;
  959. }
  960. *ppBuffer = pBuffer;
  961. *pdwBufferLen = dwTotalSize;
  962. cleanup:
  963. if (pSpecBuffer) {
  964. FreeSpecBuffer(
  965. pSpecBuffer,
  966. dwNumAuthMethods
  967. );
  968. }
  969. if (pSpecBufferV2) {
  970. FreeSpecBuffer(
  971. pSpecBufferV2,
  972. dwNumAuthMethods
  973. );
  974. }
  975. return (dwError);
  976. error:
  977. *ppBuffer = NULL;
  978. *pdwBufferLen = 0;
  979. goto cleanup;
  980. }
  981. DWORD
  982. MarshallAuthMethods(
  983. PIPSEC_AUTH_METHOD pIpsecAuthMethod,
  984. LPBYTE * ppMem,
  985. DWORD * pdwSize,
  986. DWORD dwVersion
  987. )
  988. {
  989. DWORD dwSize = 0;
  990. LPBYTE pMem = NULL;
  991. LPBYTE pCurrentPos = NULL;
  992. DWORD dwError = 0;
  993. LPWSTR pszAuthMethod = NULL;
  994. DWORD dwAuthType = 0;
  995. DWORD dwAuthLen = 0;
  996. PBYTE pAltAuthMethod = NULL;
  997. dwAuthType = pIpsecAuthMethod->dwAuthType;
  998. //
  999. // Length in number of characters excluding the null character for
  1000. // auth version 1.
  1001. //
  1002. if (dwVersion == AUTH_VERSION_ONE) {
  1003. dwAuthLen = pIpsecAuthMethod->dwAuthLen;
  1004. dwAuthLen = (dwAuthLen + 1)*2;
  1005. pszAuthMethod = pIpsecAuthMethod->pszAuthMethod;
  1006. }
  1007. else {
  1008. dwAuthLen = pIpsecAuthMethod->dwAltAuthLen;
  1009. pAltAuthMethod = pIpsecAuthMethod->pAltAuthMethod;
  1010. }
  1011. dwSize += sizeof(DWORD);
  1012. dwSize += sizeof(DWORD);
  1013. dwSize += dwAuthLen;
  1014. pMem = AllocPolMem(dwSize);
  1015. if (!pMem) {
  1016. dwError = ERROR_OUTOFMEMORY;
  1017. BAIL_ON_WIN32_ERROR(dwError);
  1018. }
  1019. pCurrentPos = pMem;
  1020. memcpy(pCurrentPos, &dwAuthType, sizeof(DWORD));
  1021. pCurrentPos += sizeof(DWORD);
  1022. memcpy(pCurrentPos, &dwAuthLen, sizeof(DWORD));
  1023. pCurrentPos += sizeof(DWORD);
  1024. if (dwVersion == AUTH_VERSION_ONE) {
  1025. if (pszAuthMethod) {
  1026. memcpy(pCurrentPos, pszAuthMethod, dwAuthLen);
  1027. }
  1028. }
  1029. else {
  1030. if (pAltAuthMethod) {
  1031. memcpy(pCurrentPos, pAltAuthMethod, dwAuthLen);
  1032. }
  1033. }
  1034. *ppMem = pMem;
  1035. *pdwSize = dwSize;
  1036. return(dwError);
  1037. error:
  1038. *ppMem = NULL;
  1039. *pdwSize = 0;
  1040. return(dwError);
  1041. }
  1042. DWORD
  1043. ConvertGuidToNegPolString(
  1044. GUID NegPolIdentifier,
  1045. LPWSTR pszIpsecRootContainer,
  1046. LPWSTR * ppszIpsecNegPolReference
  1047. )
  1048. {
  1049. DWORD dwError = 0;
  1050. WCHAR szNegPolReference[MAX_PATH];
  1051. LPWSTR pszIpsecNegPolReference = NULL;
  1052. WCHAR szGuidString[MAX_PATH];
  1053. LPWSTR pszStringUuid = NULL;
  1054. dwError = UuidToString(
  1055. &NegPolIdentifier,
  1056. &pszStringUuid
  1057. );
  1058. BAIL_ON_WIN32_ERROR(dwError);
  1059. szGuidString[0] = L'\0';
  1060. wcscpy(szGuidString, L"{");
  1061. wcscat(szGuidString, pszStringUuid);
  1062. wcscat(szGuidString, L"}");
  1063. szNegPolReference[0] = L'\0';
  1064. wcscpy(szNegPolReference, pszIpsecRootContainer);
  1065. wcscat(szNegPolReference, L"\\");
  1066. wcscat(szNegPolReference, L"ipsecNegotiationPolicy");
  1067. wcscat(szNegPolReference, szGuidString);
  1068. pszIpsecNegPolReference = AllocPolStr(
  1069. szNegPolReference
  1070. );
  1071. if (!pszIpsecNegPolReference) {
  1072. dwError = ERROR_OUTOFMEMORY;
  1073. BAIL_ON_WIN32_ERROR(dwError);
  1074. }
  1075. *ppszIpsecNegPolReference = pszIpsecNegPolReference;
  1076. cleanup:
  1077. if (pszStringUuid) {
  1078. RpcStringFree(&pszStringUuid);
  1079. }
  1080. return(dwError);
  1081. error:
  1082. *ppszIpsecNegPolReference = NULL;
  1083. goto cleanup;
  1084. }
  1085. DWORD
  1086. ConvertGuidToFilterString(
  1087. GUID FilterIdentifier,
  1088. LPWSTR pszIpsecRootContainer,
  1089. LPWSTR * ppszIpsecFilterReference
  1090. )
  1091. {
  1092. DWORD dwError = 0;
  1093. WCHAR szFilterReference[MAX_PATH];
  1094. LPWSTR pszIpsecFilterReference = NULL;
  1095. WCHAR szGuidString[MAX_PATH];
  1096. LPWSTR pszStringUuid = NULL;
  1097. dwError = UuidToString(
  1098. &FilterIdentifier,
  1099. &pszStringUuid
  1100. );
  1101. BAIL_ON_WIN32_ERROR(dwError);
  1102. szGuidString[0] = L'\0';
  1103. wcscpy(szGuidString, L"{");
  1104. wcscat(szGuidString, pszStringUuid);
  1105. wcscat(szGuidString, L"}");
  1106. szFilterReference[0] = L'\0';
  1107. wcscpy(szFilterReference, pszIpsecRootContainer);
  1108. wcscat(szFilterReference, L"\\");
  1109. wcscat(szFilterReference, L"ipsecFilter");
  1110. wcscat(szFilterReference, szGuidString);
  1111. pszIpsecFilterReference = AllocPolStr(
  1112. szFilterReference
  1113. );
  1114. if (!pszIpsecFilterReference) {
  1115. dwError = ERROR_OUTOFMEMORY;
  1116. BAIL_ON_WIN32_ERROR(dwError);
  1117. }
  1118. *ppszIpsecFilterReference = pszIpsecFilterReference;
  1119. cleanup:
  1120. if (pszStringUuid) {
  1121. RpcStringFree(&pszStringUuid);
  1122. }
  1123. return(dwError);
  1124. error:
  1125. *ppszIpsecFilterReference = NULL;
  1126. goto cleanup;
  1127. }
  1128. DWORD
  1129. RegGetNFAExistingFilterRef(
  1130. HKEY hRegistryKey,
  1131. PIPSEC_NFA_DATA pIpsecNFAData,
  1132. LPWSTR * ppszFilterName
  1133. )
  1134. {
  1135. DWORD dwError = 0;
  1136. LPWSTR pszStringUuid = NULL;
  1137. WCHAR szRelativeName[MAX_PATH];
  1138. HKEY hRegKey = NULL;
  1139. DWORD dwSize = 0;
  1140. szRelativeName[0] = L'\0';
  1141. dwError = UuidToString(
  1142. &pIpsecNFAData->NFAIdentifier,
  1143. &pszStringUuid
  1144. );
  1145. BAIL_ON_WIN32_ERROR(dwError);
  1146. wcscpy(szRelativeName, L"ipsecNFA");
  1147. wcscat(szRelativeName, L"{");
  1148. wcscat(szRelativeName, pszStringUuid);
  1149. wcscat(szRelativeName, L"}");
  1150. dwError = RegOpenKeyExW(
  1151. hRegistryKey,
  1152. szRelativeName,
  1153. 0,
  1154. KEY_ALL_ACCESS,
  1155. &hRegKey
  1156. );
  1157. BAIL_ON_WIN32_ERROR(dwError);
  1158. dwError = RegstoreQueryValue(
  1159. hRegKey,
  1160. L"ipsecFilterReference",
  1161. REG_SZ,
  1162. (LPBYTE *)ppszFilterName,
  1163. &dwSize
  1164. );
  1165. // BAIL_ON_WIN32_ERROR(dwError);
  1166. dwError = 0;
  1167. error:
  1168. if (pszStringUuid) {
  1169. RpcStringFree(&pszStringUuid);
  1170. }
  1171. if (hRegKey) {
  1172. RegCloseKey(hRegKey);
  1173. }
  1174. return (dwError);
  1175. }
  1176. DWORD
  1177. RegGetNFAExistingNegPolRef(
  1178. HKEY hRegistryKey,
  1179. PIPSEC_NFA_DATA pIpsecNFAData,
  1180. LPWSTR * ppszNegPolName
  1181. )
  1182. {
  1183. DWORD dwError = 0;
  1184. LPWSTR pszStringUuid = NULL;
  1185. WCHAR szRelativeName[MAX_PATH];
  1186. HKEY hRegKey = NULL;
  1187. DWORD dwSize = 0;
  1188. szRelativeName[0] = L'\0';
  1189. dwError = UuidToString(
  1190. &pIpsecNFAData->NFAIdentifier,
  1191. &pszStringUuid
  1192. );
  1193. BAIL_ON_WIN32_ERROR(dwError);
  1194. wcscpy(szRelativeName, L"ipsecNFA");
  1195. wcscat(szRelativeName, L"{");
  1196. wcscat(szRelativeName, pszStringUuid);
  1197. wcscat(szRelativeName, L"}");
  1198. dwError = RegOpenKeyExW(
  1199. hRegistryKey,
  1200. szRelativeName,
  1201. 0,
  1202. KEY_ALL_ACCESS,
  1203. &hRegKey
  1204. );
  1205. BAIL_ON_WIN32_ERROR(dwError);
  1206. dwError = RegstoreQueryValue(
  1207. hRegKey,
  1208. L"ipsecNegotiationPolicyReference",
  1209. REG_SZ,
  1210. (LPBYTE *)ppszNegPolName,
  1211. &dwSize
  1212. );
  1213. BAIL_ON_WIN32_ERROR(dwError);
  1214. error:
  1215. if (pszStringUuid) {
  1216. RpcStringFree(&pszStringUuid);
  1217. }
  1218. if (hRegKey) {
  1219. RegCloseKey(hRegKey);
  1220. }
  1221. return (dwError);
  1222. }
  1223. DWORD
  1224. ConvertGuidToPolicyString(
  1225. GUID PolicyIdentifier,
  1226. LPWSTR pszIpsecRootContainer,
  1227. LPWSTR * ppszIpsecPolicyReference
  1228. )
  1229. {
  1230. DWORD dwError = 0;
  1231. WCHAR szPolicyReference[MAX_PATH];
  1232. LPWSTR pszIpsecPolicyReference = NULL;
  1233. WCHAR szGuidString[MAX_PATH];
  1234. LPWSTR pszStringUuid = NULL;
  1235. dwError = UuidToString(
  1236. &PolicyIdentifier,
  1237. &pszStringUuid
  1238. );
  1239. BAIL_ON_WIN32_ERROR(dwError);
  1240. szGuidString[0] = L'\0';
  1241. wcscpy(szGuidString, L"{");
  1242. wcscat(szGuidString, pszStringUuid);
  1243. wcscat(szGuidString, L"}");
  1244. szPolicyReference[0] = L'\0';
  1245. wcscpy(szPolicyReference, pszIpsecRootContainer);
  1246. wcscat(szPolicyReference, L"\\");
  1247. wcscat(szPolicyReference, L"ipsecPolicy");
  1248. wcscat(szPolicyReference, szGuidString);
  1249. pszIpsecPolicyReference = AllocPolStr(
  1250. szPolicyReference
  1251. );
  1252. if (!pszIpsecPolicyReference) {
  1253. dwError = ERROR_OUTOFMEMORY;
  1254. BAIL_ON_WIN32_ERROR(dwError);
  1255. }
  1256. *ppszIpsecPolicyReference = pszIpsecPolicyReference;
  1257. cleanup:
  1258. if (pszStringUuid) {
  1259. RpcStringFree(&pszStringUuid);
  1260. }
  1261. return(dwError);
  1262. error:
  1263. *ppszIpsecPolicyReference = NULL;
  1264. goto cleanup;
  1265. }