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.

1626 lines
41 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. DWORD dwTotalV3AuthSize=0;
  825. dwTotalSize += sizeof(GUID);
  826. dwTotalSize += sizeof(DWORD);
  827. dwTotalSize += sizeof(DWORD);
  828. dwNumAuthMethods = pIpsecNFAData->dwAuthMethodCount;
  829. if (!dwNumAuthMethods) {
  830. dwError = ERROR_INVALID_PARAMETER;
  831. BAIL_ON_WIN32_ERROR(dwError);
  832. }
  833. pSpecBuffer = AllocPolMem(
  834. sizeof(SPEC_BUFFER)*dwNumAuthMethods
  835. );
  836. if (!pSpecBuffer) {
  837. dwError = ERROR_OUTOFMEMORY;
  838. BAIL_ON_WIN32_ERROR(dwError);
  839. }
  840. pSpecBufferV2 = AllocPolMem(
  841. sizeof(SPEC_BUFFER)*dwNumAuthMethods
  842. );
  843. if (!pSpecBufferV2) {
  844. dwError = ERROR_OUTOFMEMORY;
  845. BAIL_ON_WIN32_ERROR(dwError);
  846. }
  847. for (i = 0; i < dwNumAuthMethods;i++){
  848. pIpsecAuthMethod = *(pIpsecNFAData->ppAuthMethods + i);
  849. dwError = MarshallAuthMethods(
  850. pIpsecAuthMethod,
  851. &pAuthMem,
  852. &dwAuthSize,
  853. AUTH_VERSION_ONE
  854. );
  855. BAIL_ON_WIN32_ERROR(dwError);
  856. dwTotalSize += dwAuthSize;
  857. (pSpecBuffer + i)->dwSize = dwAuthSize;
  858. (pSpecBuffer + i)->pMem = pAuthMem;
  859. }
  860. dwInterfaceType = pIpsecNFAData->dwInterfaceType;
  861. dwTotalSize += sizeof(DWORD);
  862. pszInterfaceName = pIpsecNFAData->pszInterfaceName;
  863. if (pszInterfaceName) {
  864. dwInterfaceNameLen = (wcslen(pszInterfaceName) + 1)*sizeof(WCHAR);
  865. }
  866. else {
  867. dwInterfaceNameLen = sizeof(WCHAR);
  868. }
  869. dwTotalSize += sizeof(DWORD);
  870. dwTotalSize += dwInterfaceNameLen;
  871. dwTunnelIpAddr = pIpsecNFAData->dwTunnelIpAddr;
  872. dwTotalSize += sizeof(DWORD);
  873. dwTunnelFlags = pIpsecNFAData->dwTunnelFlags;
  874. dwTotalSize += sizeof(DWORD);
  875. dwActiveFlag = pIpsecNFAData->dwActiveFlag;
  876. dwTotalSize += sizeof(DWORD);
  877. pszEndPointName = pIpsecNFAData->pszEndPointName;
  878. if (pszEndPointName) {
  879. dwEndPointNameLen = (wcslen(pszEndPointName) + 1)*sizeof(WCHAR);
  880. }
  881. else {
  882. dwEndPointNameLen = sizeof(WCHAR);
  883. }
  884. dwTotalSize += sizeof(DWORD);
  885. dwTotalSize += dwEndPointNameLen;
  886. //
  887. // Marshall version 2 auth data.
  888. //
  889. dwTotalSize += sizeof(GUID);
  890. dwTotalV2AuthSize += sizeof(GUID);
  891. dwTotalSize += sizeof(DWORD);
  892. dwTotalV2AuthSize += sizeof(DWORD);
  893. for (i = 0; i < dwNumAuthMethods; i++) {
  894. pIpsecAuthMethod = *(pIpsecNFAData->ppAuthMethods + i);
  895. dwError = MarshallAuthMethods(
  896. pIpsecAuthMethod,
  897. &pAuthMem,
  898. &dwAuthSize,
  899. AUTH_VERSION_TWO
  900. );
  901. BAIL_ON_WIN32_ERROR(dwError);
  902. dwTotalSize += dwAuthSize;
  903. dwTotalV2AuthSize += dwAuthSize;
  904. (pSpecBufferV2 + i)->dwSize = dwAuthSize;
  905. (pSpecBufferV2 + i)->pMem = pAuthMem;
  906. }
  907. //
  908. // Marshall version 3 auth flags
  909. //
  910. dwTotalSize += sizeof(GUID);
  911. dwTotalV3AuthSize += sizeof(GUID);
  912. dwTotalSize += sizeof(DWORD);
  913. dwTotalV3AuthSize += sizeof(DWORD);
  914. dwTotalSize += dwNumAuthMethods * sizeof(DWORD);
  915. dwTotalV3AuthSize += dwNumAuthMethods * sizeof(DWORD);
  916. dwTotalSize++;
  917. pBuffer = AllocPolMem(dwTotalSize);
  918. if (!pBuffer) {
  919. dwError = ERROR_OUTOFMEMORY;
  920. BAIL_ON_WIN32_ERROR(dwError);
  921. }
  922. pCurrentPos = pBuffer;
  923. memcpy(pCurrentPos, &GUID_IPSEC_NFA_BLOB, sizeof(GUID));
  924. pCurrentPos += sizeof(GUID);
  925. dwEffectiveSize = dwTotalSize - sizeof(GUID) - sizeof(DWORD) - 1 -
  926. dwTotalV2AuthSize - dwTotalV3AuthSize;
  927. memcpy(pCurrentPos, (LPBYTE)&dwEffectiveSize, sizeof(DWORD));
  928. pCurrentPos += sizeof(DWORD);
  929. memcpy(pCurrentPos, (LPBYTE)&dwNumAuthMethods, sizeof(DWORD));
  930. pCurrentPos += sizeof(DWORD);
  931. for (i = 0; i < dwNumAuthMethods; i++) {
  932. pAuthMem = (pSpecBuffer + i)->pMem;
  933. dwAuthSize = (pSpecBuffer + i)->dwSize;
  934. memcpy(pCurrentPos, pAuthMem, dwAuthSize);
  935. pCurrentPos += dwAuthSize;
  936. }
  937. memcpy(pCurrentPos, (LPBYTE)&dwInterfaceType, sizeof(DWORD));
  938. pCurrentPos += sizeof(DWORD);
  939. memcpy(pCurrentPos, (LPBYTE)&dwInterfaceNameLen, sizeof(DWORD));
  940. pCurrentPos += sizeof(DWORD);
  941. if (pszInterfaceName) {
  942. memcpy(pCurrentPos, pszInterfaceName, dwInterfaceNameLen);
  943. }
  944. pCurrentPos += dwInterfaceNameLen;
  945. memcpy(pCurrentPos, (LPBYTE)&dwTunnelIpAddr, sizeof(DWORD));
  946. pCurrentPos += sizeof(DWORD);
  947. memcpy(pCurrentPos, (LPBYTE)&dwTunnelFlags, sizeof(DWORD));
  948. pCurrentPos += sizeof(DWORD);
  949. memcpy(pCurrentPos, (LPBYTE)&dwActiveFlag, sizeof(DWORD));
  950. pCurrentPos += sizeof(DWORD);
  951. memcpy(pCurrentPos, (LPBYTE)&dwEndPointNameLen, sizeof(DWORD));
  952. pCurrentPos += sizeof(DWORD);
  953. if (pszEndPointName) {
  954. memcpy(pCurrentPos, pszEndPointName, dwEndPointNameLen);
  955. }
  956. pCurrentPos += dwEndPointNameLen;
  957. //
  958. // Copy version 2 auth data.
  959. //
  960. memset(pCurrentPos, 1, sizeof(GUID));
  961. pCurrentPos += sizeof(GUID);
  962. memcpy(pCurrentPos, (LPBYTE)&dwNumAuthMethods, sizeof(DWORD));
  963. pCurrentPos += sizeof(DWORD);
  964. for (i = 0; i < dwNumAuthMethods; i++) {
  965. pAuthMem = (pSpecBufferV2 + i)->pMem;
  966. dwAuthSize = (pSpecBufferV2 + i)->dwSize;
  967. memcpy(pCurrentPos, pAuthMem, dwAuthSize);
  968. pCurrentPos += dwAuthSize;
  969. }
  970. //
  971. // Create version 3 auth data
  972. //
  973. memset(pCurrentPos, 1, sizeof(GUID));
  974. (pCurrentPos[sizeof(GUID)-1])++;
  975. pCurrentPos += sizeof(GUID);
  976. memcpy(pCurrentPos, (LPBYTE)&dwNumAuthMethods, sizeof(DWORD));
  977. pCurrentPos += sizeof(DWORD);
  978. for (i = 0; i < dwNumAuthMethods; i++) {
  979. pIpsecAuthMethod = *(pIpsecNFAData->ppAuthMethods + i);
  980. memcpy(pCurrentPos,&pIpsecAuthMethod->dwAuthFlags,sizeof(DWORD));
  981. pCurrentPos += sizeof(DWORD);
  982. }
  983. *ppBuffer = pBuffer;
  984. *pdwBufferLen = dwTotalSize;
  985. cleanup:
  986. if (pSpecBuffer) {
  987. FreeSpecBuffer(
  988. pSpecBuffer,
  989. dwNumAuthMethods
  990. );
  991. }
  992. if (pSpecBufferV2) {
  993. FreeSpecBuffer(
  994. pSpecBufferV2,
  995. dwNumAuthMethods
  996. );
  997. }
  998. return (dwError);
  999. error:
  1000. *ppBuffer = NULL;
  1001. *pdwBufferLen = 0;
  1002. goto cleanup;
  1003. }
  1004. DWORD
  1005. MarshallAuthMethods(
  1006. PIPSEC_AUTH_METHOD pIpsecAuthMethod,
  1007. LPBYTE * ppMem,
  1008. DWORD * pdwSize,
  1009. DWORD dwVersion
  1010. )
  1011. {
  1012. DWORD dwSize = 0;
  1013. LPBYTE pMem = NULL;
  1014. LPBYTE pCurrentPos = NULL;
  1015. DWORD dwError = 0;
  1016. LPWSTR pszAuthMethod = NULL;
  1017. DWORD dwAuthType = 0;
  1018. DWORD dwAuthLen = 0;
  1019. PBYTE pAltAuthMethod = NULL;
  1020. dwAuthType = pIpsecAuthMethod->dwAuthType;
  1021. //
  1022. // Length in number of characters excluding the null character for
  1023. // auth version 1.
  1024. //
  1025. if (dwVersion == AUTH_VERSION_ONE) {
  1026. dwAuthLen = pIpsecAuthMethod->dwAuthLen;
  1027. dwAuthLen = (dwAuthLen + 1)*2;
  1028. pszAuthMethod = pIpsecAuthMethod->pszAuthMethod;
  1029. }
  1030. else {
  1031. dwAuthLen = pIpsecAuthMethod->dwAltAuthLen;
  1032. pAltAuthMethod = pIpsecAuthMethod->pAltAuthMethod;
  1033. }
  1034. dwSize += sizeof(DWORD);
  1035. dwSize += sizeof(DWORD);
  1036. dwSize += dwAuthLen;
  1037. pMem = AllocPolMem(dwSize);
  1038. if (!pMem) {
  1039. dwError = ERROR_OUTOFMEMORY;
  1040. BAIL_ON_WIN32_ERROR(dwError);
  1041. }
  1042. pCurrentPos = pMem;
  1043. memcpy(pCurrentPos, &dwAuthType, sizeof(DWORD));
  1044. pCurrentPos += sizeof(DWORD);
  1045. memcpy(pCurrentPos, &dwAuthLen, sizeof(DWORD));
  1046. pCurrentPos += sizeof(DWORD);
  1047. if (dwVersion == AUTH_VERSION_ONE) {
  1048. if (pszAuthMethod) {
  1049. memcpy(pCurrentPos, pszAuthMethod, dwAuthLen);
  1050. }
  1051. }
  1052. else {
  1053. if (pAltAuthMethod) {
  1054. memcpy(pCurrentPos, pAltAuthMethod, dwAuthLen);
  1055. }
  1056. }
  1057. *ppMem = pMem;
  1058. *pdwSize = dwSize;
  1059. return(dwError);
  1060. error:
  1061. *ppMem = NULL;
  1062. *pdwSize = 0;
  1063. return(dwError);
  1064. }
  1065. DWORD
  1066. ConvertGuidToNegPolString(
  1067. GUID NegPolIdentifier,
  1068. LPWSTR pszIpsecRootContainer,
  1069. LPWSTR * ppszIpsecNegPolReference
  1070. )
  1071. {
  1072. DWORD dwError = 0;
  1073. WCHAR szNegPolReference[MAX_PATH];
  1074. LPWSTR pszIpsecNegPolReference = NULL;
  1075. WCHAR szGuidString[MAX_PATH];
  1076. LPWSTR pszStringUuid = NULL;
  1077. dwError = UuidToString(
  1078. &NegPolIdentifier,
  1079. &pszStringUuid
  1080. );
  1081. BAIL_ON_WIN32_ERROR(dwError);
  1082. szGuidString[0] = L'\0';
  1083. wcscpy(szGuidString, L"{");
  1084. wcscat(szGuidString, pszStringUuid);
  1085. wcscat(szGuidString, L"}");
  1086. szNegPolReference[0] = L'\0';
  1087. SecStrCpyW(szNegPolReference, pszIpsecRootContainer, MAX_PATH);
  1088. wcscat(szNegPolReference, L"\\");
  1089. wcscat(szNegPolReference, L"ipsecNegotiationPolicy");
  1090. wcscat(szNegPolReference, szGuidString);
  1091. pszIpsecNegPolReference = AllocPolStr(
  1092. szNegPolReference
  1093. );
  1094. if (!pszIpsecNegPolReference) {
  1095. dwError = ERROR_OUTOFMEMORY;
  1096. BAIL_ON_WIN32_ERROR(dwError);
  1097. }
  1098. *ppszIpsecNegPolReference = pszIpsecNegPolReference;
  1099. cleanup:
  1100. if (pszStringUuid) {
  1101. RpcStringFree(&pszStringUuid);
  1102. }
  1103. return(dwError);
  1104. error:
  1105. *ppszIpsecNegPolReference = NULL;
  1106. goto cleanup;
  1107. }
  1108. DWORD
  1109. ConvertGuidToFilterString(
  1110. GUID FilterIdentifier,
  1111. LPWSTR pszIpsecRootContainer,
  1112. LPWSTR * ppszIpsecFilterReference
  1113. )
  1114. {
  1115. DWORD dwError = 0;
  1116. WCHAR szFilterReference[MAX_PATH];
  1117. LPWSTR pszIpsecFilterReference = NULL;
  1118. WCHAR szGuidString[MAX_PATH];
  1119. LPWSTR pszStringUuid = NULL;
  1120. dwError = UuidToString(
  1121. &FilterIdentifier,
  1122. &pszStringUuid
  1123. );
  1124. BAIL_ON_WIN32_ERROR(dwError);
  1125. szGuidString[0] = L'\0';
  1126. wcscpy(szGuidString, L"{");
  1127. wcscat(szGuidString, pszStringUuid);
  1128. wcscat(szGuidString, L"}");
  1129. szFilterReference[0] = L'\0';
  1130. SecStrCpyW(szFilterReference, pszIpsecRootContainer, MAX_PATH);
  1131. wcscat(szFilterReference, L"\\");
  1132. wcscat(szFilterReference, L"ipsecFilter");
  1133. wcscat(szFilterReference, szGuidString);
  1134. pszIpsecFilterReference = AllocPolStr(
  1135. szFilterReference
  1136. );
  1137. if (!pszIpsecFilterReference) {
  1138. dwError = ERROR_OUTOFMEMORY;
  1139. BAIL_ON_WIN32_ERROR(dwError);
  1140. }
  1141. *ppszIpsecFilterReference = pszIpsecFilterReference;
  1142. cleanup:
  1143. if (pszStringUuid) {
  1144. RpcStringFree(&pszStringUuid);
  1145. }
  1146. return(dwError);
  1147. error:
  1148. *ppszIpsecFilterReference = NULL;
  1149. goto cleanup;
  1150. }
  1151. DWORD
  1152. RegGetNFAExistingFilterRef(
  1153. HKEY hRegistryKey,
  1154. PIPSEC_NFA_DATA pIpsecNFAData,
  1155. LPWSTR * ppszFilterName
  1156. )
  1157. {
  1158. DWORD dwError = 0;
  1159. LPWSTR pszStringUuid = NULL;
  1160. WCHAR szRelativeName[MAX_PATH];
  1161. HKEY hRegKey = NULL;
  1162. DWORD dwSize = 0;
  1163. szRelativeName[0] = L'\0';
  1164. dwError = UuidToString(
  1165. &pIpsecNFAData->NFAIdentifier,
  1166. &pszStringUuid
  1167. );
  1168. BAIL_ON_WIN32_ERROR(dwError);
  1169. wcscpy(szRelativeName, L"ipsecNFA");
  1170. wcscat(szRelativeName, L"{");
  1171. wcscat(szRelativeName, pszStringUuid);
  1172. wcscat(szRelativeName, L"}");
  1173. dwError = RegOpenKeyExW(
  1174. hRegistryKey,
  1175. szRelativeName,
  1176. 0,
  1177. KEY_ALL_ACCESS,
  1178. &hRegKey
  1179. );
  1180. BAIL_ON_WIN32_ERROR(dwError);
  1181. dwError = RegstoreQueryValue(
  1182. hRegKey,
  1183. L"ipsecFilterReference",
  1184. REG_SZ,
  1185. (LPBYTE *)ppszFilterName,
  1186. &dwSize
  1187. );
  1188. // BAIL_ON_WIN32_ERROR(dwError);
  1189. dwError = 0;
  1190. error:
  1191. if (pszStringUuid) {
  1192. RpcStringFree(&pszStringUuid);
  1193. }
  1194. if (hRegKey) {
  1195. RegCloseKey(hRegKey);
  1196. }
  1197. return (dwError);
  1198. }
  1199. DWORD
  1200. RegGetNFAExistingNegPolRef(
  1201. HKEY hRegistryKey,
  1202. PIPSEC_NFA_DATA pIpsecNFAData,
  1203. LPWSTR * ppszNegPolName
  1204. )
  1205. {
  1206. DWORD dwError = 0;
  1207. LPWSTR pszStringUuid = NULL;
  1208. WCHAR szRelativeName[MAX_PATH];
  1209. HKEY hRegKey = NULL;
  1210. DWORD dwSize = 0;
  1211. szRelativeName[0] = L'\0';
  1212. dwError = UuidToString(
  1213. &pIpsecNFAData->NFAIdentifier,
  1214. &pszStringUuid
  1215. );
  1216. BAIL_ON_WIN32_ERROR(dwError);
  1217. wcscpy(szRelativeName, L"ipsecNFA");
  1218. wcscat(szRelativeName, L"{");
  1219. wcscat(szRelativeName, pszStringUuid);
  1220. wcscat(szRelativeName, L"}");
  1221. dwError = RegOpenKeyExW(
  1222. hRegistryKey,
  1223. szRelativeName,
  1224. 0,
  1225. KEY_ALL_ACCESS,
  1226. &hRegKey
  1227. );
  1228. BAIL_ON_WIN32_ERROR(dwError);
  1229. dwError = RegstoreQueryValue(
  1230. hRegKey,
  1231. L"ipsecNegotiationPolicyReference",
  1232. REG_SZ,
  1233. (LPBYTE *)ppszNegPolName,
  1234. &dwSize
  1235. );
  1236. BAIL_ON_WIN32_ERROR(dwError);
  1237. error:
  1238. if (pszStringUuid) {
  1239. RpcStringFree(&pszStringUuid);
  1240. }
  1241. if (hRegKey) {
  1242. RegCloseKey(hRegKey);
  1243. }
  1244. return (dwError);
  1245. }
  1246. DWORD
  1247. ConvertGuidToPolicyString(
  1248. GUID PolicyIdentifier,
  1249. LPWSTR pszIpsecRootContainer,
  1250. LPWSTR * ppszIpsecPolicyReference
  1251. )
  1252. {
  1253. DWORD dwError = 0;
  1254. WCHAR szPolicyReference[MAX_PATH];
  1255. LPWSTR pszIpsecPolicyReference = NULL;
  1256. WCHAR szGuidString[MAX_PATH];
  1257. LPWSTR pszStringUuid = NULL;
  1258. dwError = UuidToString(
  1259. &PolicyIdentifier,
  1260. &pszStringUuid
  1261. );
  1262. BAIL_ON_WIN32_ERROR(dwError);
  1263. szGuidString[0] = L'\0';
  1264. wcscpy(szGuidString, L"{");
  1265. wcscat(szGuidString, pszStringUuid);
  1266. wcscat(szGuidString, L"}");
  1267. szPolicyReference[0] = L'\0';
  1268. SecStrCpyW(szPolicyReference, pszIpsecRootContainer, MAX_PATH);
  1269. wcscat(szPolicyReference, L"\\");
  1270. wcscat(szPolicyReference, L"ipsecPolicy");
  1271. wcscat(szPolicyReference, szGuidString);
  1272. pszIpsecPolicyReference = AllocPolStr(
  1273. szPolicyReference
  1274. );
  1275. if (!pszIpsecPolicyReference) {
  1276. dwError = ERROR_OUTOFMEMORY;
  1277. BAIL_ON_WIN32_ERROR(dwError);
  1278. }
  1279. *ppszIpsecPolicyReference = pszIpsecPolicyReference;
  1280. cleanup:
  1281. if (pszStringUuid) {
  1282. RpcStringFree(&pszStringUuid);
  1283. }
  1284. return(dwError);
  1285. error:
  1286. *ppszIpsecPolicyReference = NULL;
  1287. goto cleanup;
  1288. }