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.

1912 lines
47 KiB

  1. #include "precomp.h"
  2. LPWSTR gpszIpsecRegContainer = L"SOFTWARE\\Policies\\Microsoft\\Windows\\IPSec\\Policy\\Local";
  3. DWORD
  4. OpenRegistryIPSECRootKey(
  5. LPWSTR pszServerName,
  6. LPWSTR pszIpsecRegRootContainer,
  7. HKEY * phRegistryKey
  8. )
  9. {
  10. DWORD dwError = 0;
  11. dwError = RegOpenKeyExW(
  12. HKEY_LOCAL_MACHINE,
  13. (LPCWSTR) pszIpsecRegRootContainer,
  14. 0,
  15. KEY_ALL_ACCESS,
  16. phRegistryKey
  17. );
  18. BAIL_ON_WIN32_ERROR(dwError);
  19. error:
  20. return(dwError);
  21. }
  22. DWORD
  23. ReadPolicyObjectFromRegistry(
  24. HKEY hRegistryKey,
  25. LPWSTR pszPolicyDN,
  26. LPWSTR pszIpsecRegRootContainer,
  27. PIPSEC_POLICY_OBJECT * ppIpsecPolicyObject
  28. )
  29. {
  30. DWORD dwError = 0;
  31. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  32. DWORD dwNumNFAObjectsReturned = 0;
  33. PIPSEC_NFA_OBJECT * ppIpsecNFAObjects = NULL;
  34. LPWSTR * ppszFilterReferences = NULL;
  35. DWORD dwNumFilterReferences = 0;
  36. LPWSTR * ppszNegPolReferences = NULL;
  37. DWORD dwNumNegPolReferences = 0;
  38. PIPSEC_FILTER_OBJECT * ppIpsecFilterObjects = NULL;
  39. DWORD dwNumFilterObjects = 0;
  40. PIPSEC_NEGPOL_OBJECT * ppIpsecNegPolObjects = NULL;
  41. DWORD dwNumNegPolObjects = 0;
  42. PIPSEC_ISAKMP_OBJECT * ppIpsecISAKMPObjects = NULL;
  43. DWORD dwNumISAKMPObjects = 0;
  44. dwError = UnMarshallRegistryPolicyObject(
  45. hRegistryKey,
  46. pszIpsecRegRootContainer,
  47. pszPolicyDN,
  48. REG_FULLY_QUALIFIED_NAME,
  49. &pIpsecPolicyObject
  50. );
  51. BAIL_ON_WIN32_ERROR(dwError);
  52. dwError = ReadNFAObjectsFromRegistry(
  53. hRegistryKey,
  54. pszIpsecRegRootContainer,
  55. pIpsecPolicyObject->pszIpsecOwnersReference,
  56. pIpsecPolicyObject->ppszIpsecNFAReferences,
  57. pIpsecPolicyObject->NumberofRules,
  58. &ppIpsecNFAObjects,
  59. &dwNumNFAObjectsReturned,
  60. &ppszFilterReferences,
  61. &dwNumFilterReferences,
  62. &ppszNegPolReferences,
  63. &dwNumNegPolReferences
  64. );
  65. BAIL_ON_WIN32_ERROR(dwError);
  66. dwError = ReadFilterObjectsFromRegistry(
  67. hRegistryKey,
  68. pszIpsecRegRootContainer,
  69. ppszFilterReferences,
  70. dwNumFilterReferences,
  71. &ppIpsecFilterObjects,
  72. &dwNumFilterObjects
  73. );
  74. BAIL_ON_WIN32_ERROR(dwError);
  75. dwError = ReadNegPolObjectsFromRegistry(
  76. hRegistryKey,
  77. pszIpsecRegRootContainer,
  78. ppszNegPolReferences,
  79. dwNumNegPolReferences,
  80. &ppIpsecNegPolObjects,
  81. &dwNumNegPolObjects
  82. );
  83. BAIL_ON_WIN32_ERROR(dwError);
  84. dwError = ReadISAKMPObjectsFromRegistry(
  85. hRegistryKey,
  86. pszIpsecRegRootContainer,
  87. &pIpsecPolicyObject->pszIpsecISAKMPReference,
  88. 1,
  89. &ppIpsecISAKMPObjects,
  90. &dwNumISAKMPObjects
  91. );
  92. BAIL_ON_WIN32_ERROR(dwError);
  93. pIpsecPolicyObject->ppIpsecNFAObjects = ppIpsecNFAObjects;
  94. pIpsecPolicyObject->NumberofRulesReturned = dwNumNFAObjectsReturned;
  95. pIpsecPolicyObject->NumberofFilters = dwNumFilterObjects;
  96. pIpsecPolicyObject->ppIpsecFilterObjects = ppIpsecFilterObjects;
  97. pIpsecPolicyObject->ppIpsecNegPolObjects = ppIpsecNegPolObjects;
  98. pIpsecPolicyObject->NumberofNegPols = dwNumNegPolObjects;
  99. pIpsecPolicyObject->NumberofISAKMPs = dwNumISAKMPObjects;
  100. pIpsecPolicyObject->ppIpsecISAKMPObjects = ppIpsecISAKMPObjects;
  101. *ppIpsecPolicyObject = pIpsecPolicyObject;
  102. cleanup:
  103. if (ppszFilterReferences) {
  104. FreeFilterReferences(
  105. ppszFilterReferences,
  106. dwNumFilterReferences
  107. );
  108. }
  109. if (ppszNegPolReferences) {
  110. FreeNegPolReferences(
  111. ppszNegPolReferences,
  112. dwNumNegPolReferences
  113. );
  114. }
  115. return(dwError);
  116. error:
  117. if (pIpsecPolicyObject) {
  118. FreeIpsecPolicyObject(
  119. pIpsecPolicyObject
  120. );
  121. }
  122. *ppIpsecPolicyObject = NULL;
  123. goto cleanup;
  124. }
  125. DWORD
  126. ReadNFAObjectsFromRegistry(
  127. HKEY hRegistryKey,
  128. LPWSTR pszIpsecRootContainer,
  129. LPWSTR pszIpsecOwnerReference,
  130. LPWSTR * ppszNFADNs,
  131. DWORD dwNumNfaObjects,
  132. PIPSEC_NFA_OBJECT ** pppIpsecNFAObjects,
  133. PDWORD pdwNumNfaObjects,
  134. LPWSTR ** pppszFilterReferences,
  135. PDWORD pdwNumFilterReferences,
  136. LPWSTR ** pppszNegPolReferences,
  137. PDWORD pdwNumNegPolReferences
  138. )
  139. {
  140. DWORD dwError = 0;
  141. DWORD i = 0;
  142. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  143. PIPSEC_NFA_OBJECT * ppIpsecNFAObjects = NULL;
  144. LPWSTR * ppszFilterReferences = NULL;
  145. LPWSTR * ppszNegPolReferences = NULL;
  146. LPWSTR pszFilterReference = NULL;
  147. LPWSTR pszNegPolReference = NULL;
  148. DWORD dwNumFilterReferences = 0;
  149. DWORD dwNumNegPolReferences = 0;
  150. DWORD dwNumNFAObjectsReturned = 0;
  151. *pppszNegPolReferences = NULL;
  152. *pdwNumFilterReferences = 0;
  153. *pppszFilterReferences = NULL;
  154. *pdwNumNegPolReferences = 0;
  155. *pppIpsecNFAObjects = NULL;
  156. *pdwNumNfaObjects = 0;
  157. ppIpsecNFAObjects = (PIPSEC_NFA_OBJECT *)AllocPolMem(
  158. sizeof(PIPSEC_NFA_OBJECT)*dwNumNfaObjects
  159. );
  160. if (!ppIpsecNFAObjects) {
  161. dwError = ERROR_OUTOFMEMORY;
  162. BAIL_ON_WIN32_ERROR(dwError);
  163. }
  164. ppszFilterReferences = (LPWSTR *)AllocPolMem(
  165. sizeof(LPWSTR)*dwNumNfaObjects
  166. );
  167. if (!ppszFilterReferences) {
  168. dwError = ERROR_OUTOFMEMORY;
  169. BAIL_ON_WIN32_ERROR(dwError);
  170. }
  171. ppszNegPolReferences = (LPWSTR *)AllocPolMem(
  172. sizeof(LPWSTR)*dwNumNfaObjects
  173. );
  174. if (!ppszNegPolReferences) {
  175. dwError = ERROR_OUTOFMEMORY;
  176. BAIL_ON_WIN32_ERROR(dwError);
  177. }
  178. for (i = 0; i < dwNumNfaObjects; i++) {
  179. dwError =UnMarshallRegistryNFAObject(
  180. hRegistryKey,
  181. pszIpsecRootContainer,
  182. *(ppszNFADNs + i),
  183. &pIpsecNFAObject,
  184. &pszFilterReference,
  185. &pszNegPolReference
  186. );
  187. if (dwError == ERROR_SUCCESS) {
  188. *(ppIpsecNFAObjects + dwNumNFAObjectsReturned) = pIpsecNFAObject;
  189. if (pszFilterReference) {
  190. *(ppszFilterReferences + dwNumFilterReferences) = pszFilterReference;
  191. dwNumFilterReferences++;
  192. }
  193. if (pszNegPolReference) {
  194. *(ppszNegPolReferences + dwNumNegPolReferences) = pszNegPolReference;
  195. dwNumNegPolReferences++;
  196. }
  197. dwNumNFAObjectsReturned++;
  198. }
  199. }
  200. if (dwNumNFAObjectsReturned == 0) {
  201. dwError = ERROR_INVALID_DATA;
  202. BAIL_ON_WIN32_ERROR(dwError);
  203. }
  204. *pppszFilterReferences = ppszFilterReferences;
  205. *pppszNegPolReferences = ppszNegPolReferences;
  206. *pppIpsecNFAObjects = ppIpsecNFAObjects;
  207. *pdwNumNfaObjects = dwNumNFAObjectsReturned;
  208. *pdwNumNegPolReferences = dwNumNegPolReferences;
  209. *pdwNumFilterReferences = dwNumFilterReferences;
  210. dwError = ERROR_SUCCESS;
  211. cleanup:
  212. return(dwError);
  213. error:
  214. if (ppszNegPolReferences) {
  215. FreeNegPolReferences(
  216. ppszNegPolReferences,
  217. dwNumNFAObjectsReturned
  218. );
  219. }
  220. if (ppszFilterReferences) {
  221. FreeFilterReferences(
  222. ppszFilterReferences,
  223. dwNumNFAObjectsReturned
  224. );
  225. }
  226. if (ppIpsecNFAObjects) {
  227. FreeIpsecNFAObjects(
  228. ppIpsecNFAObjects,
  229. dwNumNFAObjectsReturned
  230. );
  231. }
  232. *pppszNegPolReferences = NULL;
  233. *pppszFilterReferences = NULL;
  234. *pppIpsecNFAObjects = NULL;
  235. *pdwNumNfaObjects = 0;
  236. *pdwNumNegPolReferences = 0;
  237. *pdwNumFilterReferences = 0;
  238. goto cleanup;
  239. }
  240. DWORD
  241. ReadFilterObjectsFromRegistry(
  242. HKEY hRegistryKey,
  243. LPWSTR pszIpsecRootContainer,
  244. LPWSTR * ppszFilterDNs,
  245. DWORD dwNumFilterObjects,
  246. PIPSEC_FILTER_OBJECT ** pppIpsecFilterObjects,
  247. PDWORD pdwNumFilterObjects
  248. )
  249. {
  250. DWORD dwError = 0;
  251. DWORD i = 0;
  252. DWORD dwCount = 0;
  253. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  254. PIPSEC_FILTER_OBJECT * ppIpsecFilterObjects = NULL;
  255. DWORD dwNumFilterObjectsReturned = 0;
  256. *pppIpsecFilterObjects = NULL;
  257. *pdwNumFilterObjects = 0;
  258. ppIpsecFilterObjects = (PIPSEC_FILTER_OBJECT *)AllocPolMem(
  259. sizeof(PIPSEC_FILTER_OBJECT)*dwNumFilterObjects
  260. );
  261. if (!ppIpsecFilterObjects) {
  262. dwError = ERROR_OUTOFMEMORY;
  263. BAIL_ON_WIN32_ERROR(dwError);
  264. }
  265. for (i = 0; i < dwNumFilterObjects; i++) {
  266. dwError =UnMarshallRegistryFilterObject(
  267. hRegistryKey,
  268. pszIpsecRootContainer,
  269. *(ppszFilterDNs + i),
  270. REG_FULLY_QUALIFIED_NAME,
  271. &pIpsecFilterObject
  272. );
  273. if (dwError == ERROR_SUCCESS) {
  274. *(ppIpsecFilterObjects + dwNumFilterObjectsReturned) = pIpsecFilterObject;
  275. dwNumFilterObjectsReturned++;
  276. }
  277. }
  278. *pppIpsecFilterObjects = ppIpsecFilterObjects;
  279. *pdwNumFilterObjects = dwNumFilterObjectsReturned;
  280. dwError = ERROR_SUCCESS;
  281. return(dwError);
  282. error:
  283. if (ppIpsecFilterObjects) {
  284. FreeIpsecFilterObjects(
  285. ppIpsecFilterObjects,
  286. dwNumFilterObjectsReturned
  287. );
  288. }
  289. *pppIpsecFilterObjects = NULL;
  290. *pdwNumFilterObjects = 0;
  291. return(dwError);
  292. }
  293. DWORD
  294. ReadNegPolObjectsFromRegistry(
  295. HKEY hRegistryKey,
  296. LPWSTR pszIpsecRootContainer,
  297. LPWSTR * ppszNegPolDNs,
  298. DWORD dwNumNegPolObjects,
  299. PIPSEC_NEGPOL_OBJECT ** pppIpsecNegPolObjects,
  300. PDWORD pdwNumNegPolObjects
  301. )
  302. {
  303. DWORD dwError = 0;
  304. DWORD i = 0;
  305. DWORD dwCount = 0;
  306. PIPSEC_NEGPOL_OBJECT pIpsecNegPolObject = NULL;
  307. PIPSEC_NEGPOL_OBJECT * ppIpsecNegPolObjects = NULL;
  308. DWORD dwNumNegPolObjectsReturned = 0;
  309. *pppIpsecNegPolObjects = NULL;
  310. *pdwNumNegPolObjects = 0;
  311. ppIpsecNegPolObjects = (PIPSEC_NEGPOL_OBJECT *)AllocPolMem(
  312. sizeof(PIPSEC_NEGPOL_OBJECT)*dwNumNegPolObjects
  313. );
  314. if (!ppIpsecNegPolObjects) {
  315. dwError = ERROR_OUTOFMEMORY;
  316. BAIL_ON_WIN32_ERROR(dwError);
  317. }
  318. for (i = 0; i < dwNumNegPolObjects; i++) {
  319. dwError =UnMarshallRegistryNegPolObject(
  320. hRegistryKey,
  321. pszIpsecRootContainer,
  322. *(ppszNegPolDNs + i),
  323. REG_FULLY_QUALIFIED_NAME,
  324. &pIpsecNegPolObject
  325. );
  326. if (dwError == ERROR_SUCCESS) {
  327. *(ppIpsecNegPolObjects + dwNumNegPolObjectsReturned) = pIpsecNegPolObject;
  328. dwNumNegPolObjectsReturned++;
  329. }
  330. }
  331. if (dwNumNegPolObjectsReturned == 0) {
  332. dwError = ERROR_INVALID_DATA;
  333. BAIL_ON_WIN32_ERROR(dwError);
  334. }
  335. *pppIpsecNegPolObjects = ppIpsecNegPolObjects;
  336. *pdwNumNegPolObjects = dwNumNegPolObjectsReturned;
  337. dwError = ERROR_SUCCESS;
  338. return(dwError);
  339. error:
  340. if (ppIpsecNegPolObjects) {
  341. FreeIpsecNegPolObjects(
  342. ppIpsecNegPolObjects,
  343. dwNumNegPolObjectsReturned
  344. );
  345. }
  346. *pppIpsecNegPolObjects = NULL;
  347. *pdwNumNegPolObjects = 0;
  348. return(dwError);
  349. }
  350. DWORD
  351. ReadISAKMPObjectsFromRegistry(
  352. HKEY hRegistryKey,
  353. LPWSTR pszIpsecRootContainer,
  354. LPWSTR * ppszISAKMPDNs,
  355. DWORD dwNumISAKMPObjects,
  356. PIPSEC_ISAKMP_OBJECT ** pppIpsecISAKMPObjects,
  357. PDWORD pdwNumISAKMPObjects
  358. )
  359. {
  360. DWORD dwError = 0;
  361. DWORD i = 0;
  362. DWORD dwCount = 0;
  363. PIPSEC_ISAKMP_OBJECT pIpsecISAKMPObject = NULL;
  364. PIPSEC_ISAKMP_OBJECT * ppIpsecISAKMPObjects = NULL;
  365. DWORD dwNumISAKMPObjectsReturned = 0;
  366. *pppIpsecISAKMPObjects = NULL;
  367. *pdwNumISAKMPObjects = 0;
  368. ppIpsecISAKMPObjects = (PIPSEC_ISAKMP_OBJECT *)AllocPolMem(
  369. sizeof(PIPSEC_ISAKMP_OBJECT)*dwNumISAKMPObjects
  370. );
  371. if (!ppIpsecISAKMPObjects) {
  372. dwError = ERROR_OUTOFMEMORY;
  373. BAIL_ON_WIN32_ERROR(dwError);
  374. }
  375. for (i = 0; i < dwNumISAKMPObjects; i++) {
  376. dwError =UnMarshallRegistryISAKMPObject(
  377. hRegistryKey,
  378. pszIpsecRootContainer,
  379. *(ppszISAKMPDNs + i),
  380. REG_FULLY_QUALIFIED_NAME,
  381. &pIpsecISAKMPObject
  382. );
  383. if (dwError == ERROR_SUCCESS) {
  384. *(ppIpsecISAKMPObjects + dwNumISAKMPObjectsReturned) = pIpsecISAKMPObject;
  385. dwNumISAKMPObjectsReturned++;
  386. }
  387. }
  388. if (dwNumISAKMPObjectsReturned == 0) {
  389. dwError = ERROR_INVALID_DATA;
  390. BAIL_ON_WIN32_ERROR(dwError);
  391. }
  392. *pppIpsecISAKMPObjects = ppIpsecISAKMPObjects;
  393. *pdwNumISAKMPObjects = dwNumISAKMPObjectsReturned;
  394. dwError = ERROR_SUCCESS;
  395. return(dwError);
  396. error:
  397. if (ppIpsecISAKMPObjects) {
  398. FreeIpsecISAKMPObjects(
  399. ppIpsecISAKMPObjects,
  400. dwNumISAKMPObjectsReturned
  401. );
  402. }
  403. *pppIpsecISAKMPObjects = NULL;
  404. *pdwNumISAKMPObjects = 0;
  405. return(dwError);
  406. }
  407. DWORD
  408. UnMarshallRegistryPolicyObject(
  409. HKEY hRegistryKey,
  410. LPWSTR pszIpsecRegRootContainer,
  411. LPWSTR pszIpsecPolicyDN,
  412. DWORD dwNameType,
  413. PIPSEC_POLICY_OBJECT * ppIpsecPolicyObject
  414. )
  415. {
  416. PIPSEC_POLICY_OBJECT pIpsecPolicyObject = NULL;
  417. HKEY hRegKey = NULL;
  418. DWORD dwType = 0;
  419. DWORD dwSize = 0;
  420. DWORD dwIpsecDataType = 0;
  421. DWORD dwWhenChanged = 0;
  422. LPBYTE pBuffer = NULL;
  423. DWORD i = 0;
  424. DWORD dwCount = 0;
  425. DWORD dwError = 0;
  426. LPWSTR * ppszIpsecNFANames = NULL;
  427. LPWSTR pszIpsecNFAName = NULL;
  428. LPWSTR pszTemp = NULL;
  429. LPWSTR pszString = NULL;
  430. LPWSTR pszIpsecNFAReference = NULL;
  431. LPWSTR pszRelativeName = NULL;
  432. DWORD dwRootPathLen = 0;
  433. if (!pszIpsecPolicyDN || !*pszIpsecPolicyDN) {
  434. dwError = ERROR_INVALID_DATA;
  435. BAIL_ON_WIN32_ERROR(dwError);
  436. }
  437. if (dwNameType == REG_FULLY_QUALIFIED_NAME) {
  438. dwRootPathLen = wcslen(pszIpsecRegRootContainer);
  439. if (wcslen(pszIpsecPolicyDN) <= (dwRootPathLen+1)) {
  440. dwError = ERROR_INVALID_DATA;
  441. BAIL_ON_WIN32_ERROR(dwError);
  442. }
  443. pszRelativeName = pszIpsecPolicyDN + dwRootPathLen + 1;
  444. }else {
  445. pszRelativeName = pszIpsecPolicyDN;
  446. }
  447. dwError = RegOpenKeyExW(
  448. hRegistryKey,
  449. pszRelativeName,
  450. 0,
  451. KEY_ALL_ACCESS,
  452. &hRegKey
  453. );
  454. BAIL_ON_WIN32_ERROR(dwError);
  455. pIpsecPolicyObject = (PIPSEC_POLICY_OBJECT)AllocPolMem(
  456. sizeof(IPSEC_POLICY_OBJECT)
  457. );
  458. if (!pIpsecPolicyObject) {
  459. dwError = ERROR_OUTOFMEMORY;
  460. BAIL_ON_WIN32_ERROR(dwError);
  461. }
  462. /*
  463. dwError = RegstoreQueryValue(
  464. hRegKey,
  465. L"distinguishedName",
  466. REG_SZ,
  467. (LPBYTE *)&pIpsecPolicyObject->pszIpsecOwnersReference,
  468. &dwSize
  469. );
  470. BAIL_ON_WIN32_ERROR(dwError);
  471. */
  472. pIpsecPolicyObject->pszIpsecOwnersReference = AllocPolStr(
  473. pszIpsecPolicyDN
  474. );
  475. if (!pIpsecPolicyObject->pszIpsecOwnersReference) {
  476. dwError = ERROR_OUTOFMEMORY;
  477. BAIL_ON_WIN32_ERROR(dwError);
  478. }
  479. dwError = RegstoreQueryValue(
  480. hRegKey,
  481. L"ipsecName",
  482. REG_SZ,
  483. (LPBYTE *)&pIpsecPolicyObject->pszIpsecName,
  484. &dwSize
  485. );
  486. BAIL_ON_WIN32_ERROR(dwError);
  487. dwError = RegstoreQueryValue(
  488. hRegKey,
  489. L"description",
  490. REG_SZ,
  491. (LPBYTE *)&pIpsecPolicyObject->pszDescription,
  492. &dwSize
  493. );
  494. // BAIL_ON_WIN32_ERROR(dwError);
  495. dwError = RegstoreQueryValue(
  496. hRegKey,
  497. L"ipsecID",
  498. REG_SZ,
  499. (LPBYTE *)&pIpsecPolicyObject->pszIpsecID,
  500. &dwSize
  501. );
  502. BAIL_ON_WIN32_ERROR(dwError);
  503. dwType = REG_DWORD;
  504. dwSize = sizeof(DWORD);
  505. dwError = RegQueryValueExW(
  506. hRegKey,
  507. L"ipsecDataType",
  508. NULL,
  509. &dwType,
  510. (LPBYTE)&dwIpsecDataType,
  511. &dwSize
  512. );
  513. BAIL_ON_WIN32_ERROR(dwError);
  514. pIpsecPolicyObject->dwIpsecDataType = dwIpsecDataType;
  515. dwError = RegstoreQueryValue(
  516. hRegKey,
  517. L"ipsecData",
  518. REG_BINARY,
  519. &pIpsecPolicyObject->pIpsecData,
  520. &pIpsecPolicyObject->dwIpsecDataLen
  521. );
  522. BAIL_ON_WIN32_ERROR(dwError);
  523. dwError = RegstoreQueryValue(
  524. hRegKey,
  525. L"ipsecISAKMPReference",
  526. REG_SZ,
  527. (LPBYTE *)&pIpsecPolicyObject->pszIpsecISAKMPReference,
  528. &dwSize
  529. );
  530. BAIL_ON_WIN32_ERROR(dwError);
  531. dwError = RegstoreQueryValue(
  532. hRegKey,
  533. L"ipsecNFAReference",
  534. REG_MULTI_SZ,
  535. (LPBYTE *)&pszIpsecNFAReference,
  536. &dwSize
  537. );
  538. BAIL_ON_WIN32_ERROR(dwError);
  539. pszTemp = pszIpsecNFAReference;
  540. while (*pszTemp != L'\0') {
  541. pszTemp += wcslen(pszTemp) + 1;
  542. dwCount++;
  543. }
  544. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  545. sizeof(LPWSTR)*dwCount
  546. );
  547. if (!ppszIpsecNFANames) {
  548. dwError = ERROR_OUTOFMEMORY;
  549. BAIL_ON_WIN32_ERROR(dwError);
  550. }
  551. pszTemp = pszIpsecNFAReference;
  552. for (i = 0; i < dwCount; i++) {
  553. pszString = AllocPolStr(pszTemp);
  554. if (!pszString) {
  555. dwError = ERROR_OUTOFMEMORY;
  556. pIpsecPolicyObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  557. pIpsecPolicyObject->NumberofRules = i;
  558. BAIL_ON_WIN32_ERROR(dwError);
  559. }
  560. *(ppszIpsecNFANames + i) = pszString;
  561. pszTemp += wcslen(pszTemp) + 1; //for the null terminator;
  562. }
  563. if (pszIpsecNFAReference) {
  564. FreePolStr(pszIpsecNFAReference);
  565. }
  566. pIpsecPolicyObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  567. pIpsecPolicyObject->NumberofRules = dwCount;
  568. dwType = REG_DWORD;
  569. dwSize = sizeof(DWORD);
  570. dwError = RegQueryValueExW(
  571. hRegKey,
  572. L"whenChanged",
  573. NULL,
  574. &dwType,
  575. (LPBYTE)&dwWhenChanged,
  576. &dwSize
  577. );
  578. BAIL_ON_WIN32_ERROR(dwError);
  579. pIpsecPolicyObject->dwWhenChanged = dwWhenChanged;
  580. *ppIpsecPolicyObject = pIpsecPolicyObject;
  581. if (hRegKey) {
  582. RegCloseKey(hRegKey);
  583. }
  584. return(dwError);
  585. error:
  586. *ppIpsecPolicyObject = NULL;
  587. if (pszIpsecNFAReference) {
  588. FreePolStr(pszIpsecNFAReference);
  589. }
  590. if (pIpsecPolicyObject) {
  591. FreeIpsecPolicyObject(pIpsecPolicyObject);
  592. }
  593. if (hRegKey) {
  594. RegCloseKey(hRegKey);
  595. }
  596. return(dwError);
  597. }
  598. DWORD
  599. UnMarshallRegistryNFAObject(
  600. HKEY hRegistryKey,
  601. LPWSTR pszIpsecRegRootContainer,
  602. LPWSTR pszIpsecNFAReference,
  603. PIPSEC_NFA_OBJECT * ppIpsecNFAObject,
  604. LPWSTR * ppszFilterReference,
  605. LPWSTR * ppszNegPolReference
  606. )
  607. {
  608. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  609. HKEY hRegKey = NULL;
  610. DWORD dwType = 0;
  611. DWORD dwSize = 0;
  612. DWORD dwIpsecDataType = 0;
  613. DWORD dwWhenChanged = 0;
  614. LPBYTE pBuffer = NULL;
  615. DWORD i = 0;
  616. DWORD dwCount = 0;
  617. DWORD dwError = 0;
  618. LPWSTR pszTempFilterReference = NULL;
  619. LPWSTR pszTempNegPolReference = NULL;
  620. LPWSTR pszRelativeName = NULL;
  621. DWORD dwRootPathLen = 0;
  622. dwRootPathLen = wcslen(pszIpsecRegRootContainer);
  623. if (!pszIpsecNFAReference || !*pszIpsecNFAReference) {
  624. dwError = ERROR_INVALID_DATA;
  625. BAIL_ON_WIN32_ERROR(dwError);
  626. }
  627. if (wcslen(pszIpsecNFAReference) <= (dwRootPathLen+1)) {
  628. dwError = ERROR_INVALID_DATA;
  629. BAIL_ON_WIN32_ERROR(dwError);
  630. }
  631. pszRelativeName = pszIpsecNFAReference + dwRootPathLen + 1;
  632. dwError = RegOpenKeyExW(
  633. hRegistryKey,
  634. pszRelativeName,
  635. 0,
  636. KEY_ALL_ACCESS,
  637. &hRegKey
  638. );
  639. BAIL_ON_WIN32_ERROR(dwError);
  640. pIpsecNFAObject = (PIPSEC_NFA_OBJECT)AllocPolMem(
  641. sizeof(IPSEC_NFA_OBJECT)
  642. );
  643. if (!pIpsecNFAObject) {
  644. dwError = ERROR_OUTOFMEMORY;
  645. BAIL_ON_WIN32_ERROR(dwError);
  646. }
  647. /*
  648. dwError = RegstoreQueryValue(
  649. hRegKey,
  650. L"distinguishedName",
  651. REG_SZ,
  652. (LPBYTE *)&pIpsecNFAObject->pszDistinguishedName,
  653. &dwSize
  654. );
  655. BAIL_ON_WIN32_ERROR(dwError);
  656. */
  657. pIpsecNFAObject->pszDistinguishedName = AllocPolStr(
  658. pszIpsecNFAReference
  659. );
  660. if (!pIpsecNFAObject->pszDistinguishedName) {
  661. dwError = ERROR_OUTOFMEMORY;
  662. BAIL_ON_WIN32_ERROR(dwError);
  663. }
  664. //
  665. // Client does not always write the Name for an NFA
  666. //
  667. dwError = RegstoreQueryValue(
  668. hRegKey,
  669. L"ipsecName",
  670. REG_SZ,
  671. (LPBYTE *)&pIpsecNFAObject->pszIpsecName,
  672. &dwSize
  673. );
  674. // BAIL_ON_WIN32_ERROR(dwError);
  675. dwError = RegstoreQueryValue(
  676. hRegKey,
  677. L"description",
  678. REG_SZ,
  679. (LPBYTE *)&pIpsecNFAObject->pszDescription,
  680. &dwSize
  681. );
  682. // BAIL_ON_WIN32_ERROR(dwError);
  683. dwError = RegstoreQueryValue(
  684. hRegKey,
  685. L"ipsecID",
  686. REG_SZ,
  687. (LPBYTE *)&pIpsecNFAObject->pszIpsecID,
  688. &dwSize
  689. );
  690. BAIL_ON_WIN32_ERROR(dwError);
  691. dwType = REG_DWORD;
  692. dwSize = sizeof(DWORD);
  693. dwError = RegQueryValueExW(
  694. hRegKey,
  695. L"ipsecDataType",
  696. NULL,
  697. &dwType,
  698. (LPBYTE)&dwIpsecDataType,
  699. &dwSize
  700. );
  701. BAIL_ON_WIN32_ERROR(dwError);
  702. pIpsecNFAObject->dwIpsecDataType = dwIpsecDataType;
  703. //
  704. // unmarshall the ipsecData blob
  705. //
  706. dwError = RegstoreQueryValue(
  707. hRegKey,
  708. L"ipsecData",
  709. REG_BINARY,
  710. &pIpsecNFAObject->pIpsecData,
  711. &pIpsecNFAObject->dwIpsecDataLen
  712. );
  713. BAIL_ON_WIN32_ERROR(dwError);
  714. dwError = RegstoreQueryValue(
  715. hRegKey,
  716. L"ipsecOwnersReference",
  717. REG_SZ,
  718. (LPBYTE *)&pIpsecNFAObject->pszIpsecOwnersReference,
  719. &dwSize
  720. );
  721. // BAIL_ON_WIN32_ERROR(dwError);
  722. dwError = RegstoreQueryValue(
  723. hRegKey,
  724. L"ipsecNegotiationPolicyReference",
  725. REG_SZ,
  726. (LPBYTE *)&pIpsecNFAObject->pszIpsecNegPolReference,
  727. &dwSize
  728. );
  729. BAIL_ON_WIN32_ERROR(dwError);
  730. dwError = RegstoreQueryValue(
  731. hRegKey,
  732. L"ipsecFilterReference",
  733. REG_SZ,
  734. (LPBYTE *)&pIpsecNFAObject->pszIpsecFilterReference,
  735. &dwSize
  736. );
  737. // BAIL_ON_WIN32_ERROR(dwError);
  738. dwType = REG_DWORD;
  739. dwSize = sizeof(DWORD);
  740. dwError = RegQueryValueExW(
  741. hRegKey,
  742. L"whenChanged",
  743. NULL,
  744. &dwType,
  745. (LPBYTE)&dwWhenChanged,
  746. &dwSize
  747. );
  748. BAIL_ON_WIN32_ERROR(dwError);
  749. pIpsecNFAObject->dwWhenChanged = dwWhenChanged;
  750. if (pIpsecNFAObject->pszIpsecFilterReference && *(pIpsecNFAObject->pszIpsecFilterReference)) {
  751. pszTempFilterReference = AllocPolStr(
  752. pIpsecNFAObject->pszIpsecFilterReference
  753. );
  754. if (!pszTempFilterReference) {
  755. dwError = ERROR_OUTOFMEMORY;
  756. BAIL_ON_WIN32_ERROR(dwError);
  757. }
  758. }
  759. pszTempNegPolReference = AllocPolStr(
  760. pIpsecNFAObject->pszIpsecNegPolReference
  761. );
  762. if (!pszTempNegPolReference) {
  763. dwError = ERROR_OUTOFMEMORY;
  764. BAIL_ON_WIN32_ERROR(dwError);
  765. }
  766. *ppszFilterReference = pszTempFilterReference;
  767. *ppszNegPolReference = pszTempNegPolReference;
  768. *ppIpsecNFAObject = pIpsecNFAObject;
  769. cleanup:
  770. if (hRegKey) {
  771. RegCloseKey(hRegKey);
  772. }
  773. return(dwError);
  774. error:
  775. if (pIpsecNFAObject) {
  776. FreeIpsecNFAObject(pIpsecNFAObject);
  777. }
  778. if (pszTempFilterReference) {
  779. FreePolStr(pszTempFilterReference);
  780. }
  781. if (pszTempNegPolReference) {
  782. FreePolStr(pszTempNegPolReference);
  783. }
  784. *ppIpsecNFAObject = NULL;
  785. *ppszFilterReference = NULL;
  786. *ppszNegPolReference = NULL;
  787. goto cleanup;
  788. }
  789. DWORD
  790. UnMarshallRegistryFilterObject(
  791. HKEY hRegistryKey,
  792. LPWSTR pszIpsecRegRootContainer,
  793. LPWSTR pszIpsecFilterReference,
  794. DWORD dwNameType,
  795. PIPSEC_FILTER_OBJECT * ppIpsecFilterObject
  796. )
  797. {
  798. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  799. HKEY hRegKey = NULL;
  800. DWORD dwType = 0;
  801. DWORD dwSize = 0;
  802. DWORD dwIpsecDataType = 0;
  803. DWORD dwWhenChanged = 0;
  804. LPBYTE pBuffer = NULL;
  805. DWORD dwCount = 0;
  806. DWORD i = 0;
  807. LPWSTR * ppszIpsecNFANames = NULL;
  808. LPWSTR pszString = NULL;
  809. LPWSTR pszIpsecNFAReference = NULL;
  810. LPWSTR pszTemp = NULL;
  811. DWORD dwError = 0;
  812. LPWSTR pszRelativeName = NULL;
  813. DWORD dwRootPathLen = 0;
  814. if (!pszIpsecFilterReference || !*pszIpsecFilterReference) {
  815. dwError = ERROR_INVALID_DATA;
  816. BAIL_ON_WIN32_ERROR(dwError);
  817. }
  818. if (dwNameType == REG_FULLY_QUALIFIED_NAME) {
  819. dwRootPathLen = wcslen(pszIpsecRegRootContainer);
  820. if (wcslen(pszIpsecFilterReference) <= (dwRootPathLen+1)) {
  821. dwError = ERROR_INVALID_DATA;
  822. BAIL_ON_WIN32_ERROR(dwError);
  823. }
  824. pszRelativeName = pszIpsecFilterReference + dwRootPathLen + 1;
  825. }else {
  826. pszRelativeName = pszIpsecFilterReference;
  827. }
  828. dwError = RegOpenKeyExW(
  829. hRegistryKey,
  830. pszRelativeName,
  831. 0,
  832. KEY_ALL_ACCESS,
  833. &hRegKey
  834. );
  835. BAIL_ON_WIN32_ERROR(dwError);
  836. pIpsecFilterObject = (PIPSEC_FILTER_OBJECT)AllocPolMem(
  837. sizeof(IPSEC_FILTER_OBJECT)
  838. );
  839. if (!pIpsecFilterObject) {
  840. dwError = ERROR_OUTOFMEMORY;
  841. BAIL_ON_WIN32_ERROR(dwError);
  842. }
  843. /*
  844. dwError = RegstoreQueryValue(
  845. hRegKey,
  846. L"distinguishedName",
  847. REG_SZ,
  848. (LPBYTE *)&pIpsecFilterObject->pszDistinguishedName,
  849. &dwSize
  850. );
  851. BAIL_ON_WIN32_ERROR(dwError);
  852. */
  853. pIpsecFilterObject->pszDistinguishedName = AllocPolStr(
  854. pszIpsecFilterReference
  855. );
  856. if (!pIpsecFilterObject->pszDistinguishedName) {
  857. dwError = ERROR_OUTOFMEMORY;
  858. BAIL_ON_WIN32_ERROR(dwError);
  859. }
  860. dwError = RegstoreQueryValue(
  861. hRegKey,
  862. L"description",
  863. REG_SZ,
  864. (LPBYTE *)&pIpsecFilterObject->pszDescription,
  865. &dwSize
  866. );
  867. //BAIL_ON_WIN32_ERROR(dwError);
  868. dwError = RegstoreQueryValue(
  869. hRegKey,
  870. L"ipsecName",
  871. REG_SZ,
  872. (LPBYTE *)&pIpsecFilterObject->pszIpsecName,
  873. &dwSize
  874. );
  875. //BAIL_ON_WIN32_ERROR(dwError);
  876. dwError = RegstoreQueryValue(
  877. hRegKey,
  878. L"ipsecID",
  879. REG_SZ,
  880. (LPBYTE *)&pIpsecFilterObject->pszIpsecID,
  881. &dwSize
  882. );
  883. BAIL_ON_WIN32_ERROR(dwError);
  884. dwType = REG_DWORD,
  885. dwSize = sizeof(DWORD);
  886. dwError = RegQueryValueExW(
  887. hRegKey,
  888. L"ipsecDataType",
  889. NULL,
  890. &dwType,
  891. (LPBYTE)&dwIpsecDataType,
  892. &dwSize
  893. );
  894. BAIL_ON_WIN32_ERROR(dwError);
  895. pIpsecFilterObject->dwIpsecDataType = dwIpsecDataType;
  896. //
  897. // unmarshall the ipsecData blob
  898. //
  899. dwError = RegstoreQueryValue(
  900. hRegKey,
  901. L"ipsecData",
  902. dwType,
  903. &pIpsecFilterObject->pIpsecData,
  904. &pIpsecFilterObject->dwIpsecDataLen
  905. );
  906. BAIL_ON_WIN32_ERROR(dwError);
  907. //
  908. // Owner's reference
  909. //
  910. dwError = RegstoreQueryValue(
  911. hRegKey,
  912. L"ipsecOwnersReference",
  913. REG_MULTI_SZ,
  914. (LPBYTE *)&pszIpsecNFAReference,
  915. &dwSize
  916. );
  917. //BAIL_ON_WIN32_ERROR(dwError);
  918. if (!dwError) {
  919. pszTemp = pszIpsecNFAReference;
  920. while (*pszTemp != L'\0') {
  921. pszTemp += wcslen(pszTemp) + 1;
  922. dwCount++;
  923. }
  924. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  925. sizeof(LPWSTR)*dwCount
  926. );
  927. if (!ppszIpsecNFANames) {
  928. dwError = ERROR_OUTOFMEMORY;
  929. BAIL_ON_WIN32_ERROR(dwError);
  930. }
  931. pszTemp = pszIpsecNFAReference;
  932. for (i = 0; i < dwCount; i++) {
  933. pszString = AllocPolStr(pszTemp);
  934. if (!pszString) {
  935. dwError = ERROR_OUTOFMEMORY;
  936. pIpsecFilterObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  937. pIpsecFilterObject->dwNFACount = i;
  938. if (pszIpsecNFAReference) {
  939. FreePolStr(pszIpsecNFAReference);
  940. }
  941. BAIL_ON_WIN32_ERROR(dwError);
  942. }
  943. *(ppszIpsecNFANames + i) = pszString;
  944. pszTemp += wcslen(pszTemp) + 1; //for the null terminator;
  945. }
  946. if (pszIpsecNFAReference) {
  947. FreePolStr(pszIpsecNFAReference);
  948. }
  949. pIpsecFilterObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  950. pIpsecFilterObject->dwNFACount = dwCount;
  951. }
  952. dwType = REG_DWORD;
  953. dwSize = sizeof(DWORD);
  954. dwError = RegQueryValueExW(
  955. hRegKey,
  956. L"whenChanged",
  957. NULL,
  958. &dwType,
  959. (LPBYTE)&dwWhenChanged,
  960. &dwSize
  961. );
  962. BAIL_ON_WIN32_ERROR(dwError);
  963. pIpsecFilterObject->dwWhenChanged = dwWhenChanged;
  964. *ppIpsecFilterObject = pIpsecFilterObject;
  965. cleanup:
  966. if (hRegKey) {
  967. RegCloseKey(hRegKey);
  968. }
  969. return(dwError);
  970. error:
  971. if (pIpsecFilterObject) {
  972. FreeIpsecFilterObject(pIpsecFilterObject);
  973. }
  974. *ppIpsecFilterObject = NULL;
  975. goto cleanup;
  976. }
  977. DWORD
  978. UnMarshallRegistryNegPolObject(
  979. HKEY hRegistryKey,
  980. LPWSTR pszIpsecRegRootContainer,
  981. LPWSTR pszIpsecNegPolReference,
  982. DWORD dwNameType,
  983. PIPSEC_NEGPOL_OBJECT * ppIpsecNegPolObject
  984. )
  985. {
  986. PIPSEC_NEGPOL_OBJECT pIpsecNegPolObject = NULL;
  987. HKEY hRegKey = NULL;
  988. DWORD dwType = 0;
  989. DWORD dwSize = 0;
  990. DWORD dwIpsecDataType = 0;
  991. DWORD dwWhenChanged = 0;
  992. LPBYTE pBuffer = NULL;
  993. DWORD dwCount = 0;
  994. DWORD i = 0;
  995. LPWSTR * ppszIpsecNFANames = NULL;
  996. LPWSTR pszString = NULL;
  997. LPWSTR pszIpsecNFAReference = NULL;
  998. LPWSTR pszTemp = NULL;
  999. DWORD dwError = 0;
  1000. LPWSTR pszRelativeName = NULL;
  1001. DWORD dwRootPathLen = 0;
  1002. if (!pszIpsecNegPolReference || !*pszIpsecNegPolReference) {
  1003. dwError = ERROR_INVALID_DATA;
  1004. BAIL_ON_WIN32_ERROR(dwError);
  1005. }
  1006. if (dwNameType == REG_FULLY_QUALIFIED_NAME) {
  1007. dwRootPathLen = wcslen(pszIpsecRegRootContainer);
  1008. if (wcslen(pszIpsecNegPolReference) <= (dwRootPathLen+1)) {
  1009. dwError = ERROR_INVALID_DATA;
  1010. BAIL_ON_WIN32_ERROR(dwError);
  1011. }
  1012. pszRelativeName = pszIpsecNegPolReference + dwRootPathLen + 1;
  1013. }else {
  1014. pszRelativeName = pszIpsecNegPolReference;
  1015. }
  1016. dwError = RegOpenKeyExW(
  1017. hRegistryKey,
  1018. pszRelativeName,
  1019. 0,
  1020. KEY_ALL_ACCESS,
  1021. &hRegKey
  1022. );
  1023. BAIL_ON_WIN32_ERROR(dwError);
  1024. pIpsecNegPolObject = (PIPSEC_NEGPOL_OBJECT)AllocPolMem(
  1025. sizeof(IPSEC_NEGPOL_OBJECT)
  1026. );
  1027. if (!pIpsecNegPolObject) {
  1028. dwError = ERROR_OUTOFMEMORY;
  1029. BAIL_ON_WIN32_ERROR(dwError);
  1030. }
  1031. /*
  1032. dwError = RegstoreQueryValue(
  1033. hRegKey,
  1034. L"distinguishedName",
  1035. REG_SZ,
  1036. (LPBYTE *)&pIpsecNegPolObject->pszDistinguishedName,
  1037. &dwSize
  1038. );
  1039. BAIL_ON_WIN32_ERROR(dwError);
  1040. */
  1041. pIpsecNegPolObject->pszDistinguishedName = AllocPolStr(
  1042. pszIpsecNegPolReference
  1043. );
  1044. if (!pIpsecNegPolObject->pszDistinguishedName) {
  1045. dwError = ERROR_OUTOFMEMORY;
  1046. BAIL_ON_WIN32_ERROR(dwError);
  1047. }
  1048. //
  1049. // Names do not get written on an NegPol Object
  1050. //
  1051. dwError = RegstoreQueryValue(
  1052. hRegKey,
  1053. L"ipsecName",
  1054. REG_SZ,
  1055. (LPBYTE *)&pIpsecNegPolObject->pszIpsecName,
  1056. &dwSize
  1057. );
  1058. // BAIL_ON_WIN32_ERROR(dwError);
  1059. dwError = RegstoreQueryValue(
  1060. hRegKey,
  1061. L"description",
  1062. REG_SZ,
  1063. (LPBYTE *)&pIpsecNegPolObject->pszDescription,
  1064. &dwSize
  1065. );
  1066. // BAIL_ON_WIN32_ERROR(dwError);
  1067. dwError = RegstoreQueryValue(
  1068. hRegKey,
  1069. L"ipsecID",
  1070. REG_SZ,
  1071. (LPBYTE *)&pIpsecNegPolObject->pszIpsecID,
  1072. &dwSize
  1073. );
  1074. BAIL_ON_WIN32_ERROR(dwError);
  1075. dwError = RegstoreQueryValue(
  1076. hRegKey,
  1077. L"ipsecNegotiationPolicyAction",
  1078. REG_SZ,
  1079. (LPBYTE *)&pIpsecNegPolObject->pszIpsecNegPolAction,
  1080. &dwSize
  1081. );
  1082. BAIL_ON_WIN32_ERROR(dwError);
  1083. dwError = RegstoreQueryValue(
  1084. hRegKey,
  1085. L"ipsecNegotiationPolicyType",
  1086. REG_SZ,
  1087. (LPBYTE *)&pIpsecNegPolObject->pszIpsecNegPolType,
  1088. &dwSize
  1089. );
  1090. BAIL_ON_WIN32_ERROR(dwError);
  1091. dwType = REG_DWORD;
  1092. dwSize = sizeof(DWORD);
  1093. dwError = RegQueryValueExW(
  1094. hRegKey,
  1095. L"ipsecDataType",
  1096. NULL,
  1097. &dwType,
  1098. (LPBYTE)&dwIpsecDataType,
  1099. &dwSize
  1100. );
  1101. BAIL_ON_WIN32_ERROR(dwError);
  1102. pIpsecNegPolObject->dwIpsecDataType = dwIpsecDataType;
  1103. dwError = RegstoreQueryValue(
  1104. hRegKey,
  1105. L"ipsecData",
  1106. REG_BINARY,
  1107. &pIpsecNegPolObject->pIpsecData,
  1108. &pIpsecNegPolObject->dwIpsecDataLen
  1109. );
  1110. BAIL_ON_WIN32_ERROR(dwError);
  1111. dwError = RegstoreQueryValue(
  1112. hRegKey,
  1113. L"ipsecOwnersReference",
  1114. REG_MULTI_SZ,
  1115. (LPBYTE *)&pszIpsecNFAReference,
  1116. &dwSize
  1117. );
  1118. // BAIL_ON_WIN32_ERROR(dwError);
  1119. if (!dwError) {
  1120. pszTemp = pszIpsecNFAReference;
  1121. while (*pszTemp != L'\0') {
  1122. pszTemp += wcslen(pszTemp) + 1;
  1123. dwCount++;
  1124. }
  1125. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  1126. sizeof(LPWSTR)*dwCount
  1127. );
  1128. if (!ppszIpsecNFANames) {
  1129. dwError = ERROR_OUTOFMEMORY;
  1130. BAIL_ON_WIN32_ERROR(dwError);
  1131. }
  1132. pszTemp = pszIpsecNFAReference;
  1133. for (i = 0; i < dwCount; i++) {
  1134. pszString = AllocPolStr(pszTemp);
  1135. if (!pszString) {
  1136. dwError = ERROR_OUTOFMEMORY;
  1137. pIpsecNegPolObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  1138. pIpsecNegPolObject->dwNFACount = i;
  1139. if (pszIpsecNFAReference) {
  1140. FreePolStr(pszIpsecNFAReference);
  1141. }
  1142. BAIL_ON_WIN32_ERROR(dwError);
  1143. }
  1144. *(ppszIpsecNFANames + i) = pszString;
  1145. pszTemp += wcslen(pszTemp) + 1; //for the null terminator;
  1146. }
  1147. if (pszIpsecNFAReference) {
  1148. FreePolStr(pszIpsecNFAReference);
  1149. }
  1150. pIpsecNegPolObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  1151. pIpsecNegPolObject->dwNFACount = dwCount;
  1152. }
  1153. dwType = REG_DWORD;
  1154. dwSize = sizeof(DWORD);
  1155. dwError = RegQueryValueExW(
  1156. hRegKey,
  1157. L"whenChanged",
  1158. NULL,
  1159. &dwType,
  1160. (LPBYTE)&dwWhenChanged,
  1161. &dwSize
  1162. );
  1163. BAIL_ON_WIN32_ERROR(dwError);
  1164. pIpsecNegPolObject->dwWhenChanged = dwWhenChanged;
  1165. *ppIpsecNegPolObject = pIpsecNegPolObject;
  1166. cleanup:
  1167. if (hRegKey) {
  1168. RegCloseKey(hRegKey);
  1169. }
  1170. return(dwError);
  1171. error:
  1172. if (pIpsecNegPolObject) {
  1173. FreeIpsecNegPolObject(pIpsecNegPolObject);
  1174. }
  1175. *ppIpsecNegPolObject = NULL;
  1176. goto cleanup;
  1177. }
  1178. DWORD
  1179. UnMarshallRegistryISAKMPObject(
  1180. HKEY hRegistryKey,
  1181. LPWSTR pszIpsecRegRootContainer,
  1182. LPWSTR pszIpsecISAKMPReference,
  1183. DWORD dwNameType,
  1184. PIPSEC_ISAKMP_OBJECT * ppIpsecISAKMPObject
  1185. )
  1186. {
  1187. PIPSEC_ISAKMP_OBJECT pIpsecISAKMPObject = NULL;
  1188. HKEY hRegKey = NULL;
  1189. DWORD dwType = 0;
  1190. DWORD dwSize = 0;
  1191. DWORD dwIpsecDataType = 0;
  1192. DWORD dwWhenChanged = 0;
  1193. LPBYTE pBuffer = NULL;
  1194. DWORD dwCount = 0;
  1195. DWORD i = 0;
  1196. LPWSTR * ppszIpsecNFANames = NULL;
  1197. LPWSTR pszString = NULL;
  1198. LPWSTR pszIpsecNFAReference = NULL;
  1199. LPWSTR pszTemp = NULL;
  1200. DWORD dwError = 0;
  1201. LPWSTR pszRelativeName = NULL;
  1202. DWORD dwRootPathLen = 0;
  1203. if (!pszIpsecISAKMPReference || !*pszIpsecISAKMPReference) {
  1204. dwError = ERROR_INVALID_DATA;
  1205. BAIL_ON_WIN32_ERROR(dwError);
  1206. }
  1207. if (dwNameType == REG_FULLY_QUALIFIED_NAME) {
  1208. dwRootPathLen = wcslen(pszIpsecRegRootContainer);
  1209. if (wcslen(pszIpsecISAKMPReference) <= (dwRootPathLen+1)) {
  1210. dwError = ERROR_INVALID_DATA;
  1211. BAIL_ON_WIN32_ERROR(dwError);
  1212. }
  1213. pszRelativeName = pszIpsecISAKMPReference + dwRootPathLen + 1;
  1214. }else {
  1215. pszRelativeName = pszIpsecISAKMPReference;
  1216. }
  1217. dwError = RegOpenKeyExW(
  1218. hRegistryKey,
  1219. pszRelativeName,
  1220. 0,
  1221. KEY_ALL_ACCESS,
  1222. &hRegKey
  1223. );
  1224. BAIL_ON_WIN32_ERROR(dwError);
  1225. pIpsecISAKMPObject = (PIPSEC_ISAKMP_OBJECT)AllocPolMem(
  1226. sizeof(IPSEC_ISAKMP_OBJECT)
  1227. );
  1228. if (!pIpsecISAKMPObject) {
  1229. dwError = ERROR_OUTOFMEMORY;
  1230. BAIL_ON_WIN32_ERROR(dwError);
  1231. }
  1232. /*
  1233. dwError = RegstoreQueryValue(
  1234. hRegKey,
  1235. L"distinguishedName",
  1236. REG_SZ,
  1237. (LPBYTE *)&pIpsecISAKMPObject->pszDistinguishedName,
  1238. &dwSize
  1239. );
  1240. BAIL_ON_WIN32_ERROR(dwError);
  1241. */
  1242. pIpsecISAKMPObject->pszDistinguishedName = AllocPolStr(
  1243. pszIpsecISAKMPReference
  1244. );
  1245. if (!pIpsecISAKMPObject->pszDistinguishedName) {
  1246. dwError = ERROR_OUTOFMEMORY;
  1247. BAIL_ON_WIN32_ERROR(dwError);
  1248. }
  1249. //
  1250. // Names are not set for ISAKMP objects
  1251. //
  1252. dwError = RegstoreQueryValue(
  1253. hRegKey,
  1254. L"ipsecName",
  1255. REG_SZ,
  1256. (LPBYTE *)&pIpsecISAKMPObject->pszIpsecName,
  1257. &dwSize
  1258. );
  1259. // BAIL_ON_WIN32_ERROR(dwError);
  1260. dwError = RegstoreQueryValue(
  1261. hRegKey,
  1262. L"ipsecID",
  1263. REG_SZ,
  1264. (LPBYTE *)&pIpsecISAKMPObject->pszIpsecID,
  1265. &dwSize
  1266. );
  1267. BAIL_ON_WIN32_ERROR(dwError);
  1268. dwType = REG_DWORD,
  1269. dwSize = sizeof(DWORD);
  1270. dwError = RegQueryValueExW(
  1271. hRegKey,
  1272. L"ipsecDataType",
  1273. NULL,
  1274. &dwType,
  1275. (LPBYTE)&dwIpsecDataType,
  1276. &dwSize
  1277. );
  1278. BAIL_ON_WIN32_ERROR(dwError);
  1279. pIpsecISAKMPObject->dwIpsecDataType = dwIpsecDataType;
  1280. //
  1281. // unmarshall the ipsecData blob
  1282. //
  1283. dwError = RegstoreQueryValue(
  1284. hRegKey,
  1285. L"ipsecData",
  1286. REG_BINARY,
  1287. &pIpsecISAKMPObject->pIpsecData,
  1288. &pIpsecISAKMPObject->dwIpsecDataLen
  1289. );
  1290. BAIL_ON_WIN32_ERROR(dwError);
  1291. //
  1292. // ipsecOwnersReference not written
  1293. //
  1294. dwError = RegstoreQueryValue(
  1295. hRegKey,
  1296. L"ipsecOwnersReference",
  1297. REG_MULTI_SZ,
  1298. (LPBYTE *)&pszIpsecNFAReference,
  1299. &dwSize
  1300. );
  1301. // BAIL_ON_WIN32_ERROR(dwError);
  1302. if (!dwError) {
  1303. pszTemp = pszIpsecNFAReference;
  1304. while (*pszTemp != L'\0') {
  1305. pszTemp += wcslen(pszTemp) + 1;
  1306. dwCount++;
  1307. }
  1308. ppszIpsecNFANames = (LPWSTR *)AllocPolMem(
  1309. sizeof(LPWSTR)*dwCount
  1310. );
  1311. if (!ppszIpsecNFANames) {
  1312. dwError = ERROR_OUTOFMEMORY;
  1313. BAIL_ON_WIN32_ERROR(dwError);
  1314. }
  1315. pszTemp = pszIpsecNFAReference;
  1316. for (i = 0; i < dwCount; i++) {
  1317. pszString = AllocPolStr(pszTemp);
  1318. if (!pszString) {
  1319. dwError = ERROR_OUTOFMEMORY;
  1320. pIpsecISAKMPObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  1321. pIpsecISAKMPObject->dwNFACount = i;
  1322. if (pszIpsecNFAReference) {
  1323. FreePolStr(pszIpsecNFAReference);
  1324. }
  1325. BAIL_ON_WIN32_ERROR(dwError);
  1326. }
  1327. *(ppszIpsecNFANames + i) = pszString;
  1328. pszTemp += wcslen(pszTemp) + 1; //for the null terminator;
  1329. }
  1330. if (pszIpsecNFAReference) {
  1331. FreePolStr(pszIpsecNFAReference);
  1332. }
  1333. pIpsecISAKMPObject->ppszIpsecNFAReferences = ppszIpsecNFANames;
  1334. pIpsecISAKMPObject->dwNFACount = dwCount;
  1335. }
  1336. dwType = REG_DWORD;
  1337. dwSize = sizeof(DWORD);
  1338. dwError = RegQueryValueExW(
  1339. hRegKey,
  1340. L"whenChanged",
  1341. NULL,
  1342. &dwType,
  1343. (LPBYTE)&dwWhenChanged,
  1344. &dwSize
  1345. );
  1346. BAIL_ON_WIN32_ERROR(dwError);
  1347. pIpsecISAKMPObject->dwWhenChanged = dwWhenChanged;
  1348. *ppIpsecISAKMPObject = pIpsecISAKMPObject;
  1349. cleanup:
  1350. if (hRegKey) {
  1351. RegCloseKey(hRegKey);
  1352. }
  1353. return(dwError);
  1354. error:
  1355. if (pIpsecISAKMPObject) {
  1356. FreeIpsecISAKMPObject(pIpsecISAKMPObject);
  1357. }
  1358. *ppIpsecISAKMPObject = NULL;
  1359. goto cleanup;
  1360. }
  1361. DWORD
  1362. RegstoreQueryValue(
  1363. HKEY hRegKey,
  1364. LPWSTR pszValueName,
  1365. DWORD dwType,
  1366. LPBYTE * ppValueData,
  1367. LPDWORD pdwSize
  1368. )
  1369. {
  1370. DWORD dwSize = 0;
  1371. LPWSTR pszValueData = NULL;
  1372. DWORD dwError = 0;
  1373. LPBYTE pBuffer = NULL;
  1374. LPWSTR pszBuf = NULL;
  1375. dwError = RegQueryValueExW(
  1376. hRegKey,
  1377. pszValueName,
  1378. NULL,
  1379. &dwType,
  1380. NULL,
  1381. &dwSize
  1382. );
  1383. BAIL_ON_WIN32_ERROR(dwError);
  1384. if (dwSize == 0) {
  1385. dwError = ERROR_INVALID_DATA;
  1386. BAIL_ON_WIN32_ERROR(dwError);
  1387. }
  1388. pBuffer = (LPBYTE)AllocPolMem(dwSize);
  1389. if (!pBuffer) {
  1390. dwError = ERROR_OUTOFMEMORY;
  1391. BAIL_ON_WIN32_ERROR(dwError);
  1392. }
  1393. dwError = RegQueryValueExW(
  1394. hRegKey,
  1395. pszValueName,
  1396. NULL,
  1397. &dwType,
  1398. pBuffer,
  1399. &dwSize
  1400. );
  1401. BAIL_ON_WIN32_ERROR(dwError);
  1402. switch (dwType) {
  1403. case REG_SZ:
  1404. pszBuf = (LPWSTR) pBuffer;
  1405. if (!pszBuf || !*pszBuf) {
  1406. dwError = ERROR_INVALID_DATA;
  1407. BAIL_ON_WIN32_ERROR(dwError);
  1408. }
  1409. break;
  1410. default:
  1411. break;
  1412. }
  1413. *ppValueData = pBuffer;
  1414. *pdwSize = dwSize;
  1415. return(dwError);
  1416. error:
  1417. if (pBuffer) {
  1418. FreePolMem(pBuffer);
  1419. }
  1420. *ppValueData = NULL;
  1421. *pdwSize = 0;
  1422. return(dwError);
  1423. }
  1424. VOID
  1425. FlushRegSaveKey(
  1426. HKEY hRegistryKey
  1427. )
  1428. {
  1429. DWORD dwError = 0;
  1430. WCHAR lpszName[MAX_PATH];
  1431. DWORD dwSize = 0;
  1432. memset(lpszName, 0, sizeof(WCHAR)*MAX_PATH);
  1433. dwSize = MAX_PATH;
  1434. while((RegEnumKeyExW(
  1435. hRegistryKey,
  1436. 0,
  1437. lpszName,
  1438. &dwSize,
  1439. NULL,
  1440. NULL,
  1441. NULL,
  1442. NULL)) == ERROR_SUCCESS) {
  1443. dwError = RegDeleteKeyW(
  1444. hRegistryKey,
  1445. lpszName
  1446. );
  1447. if (dwError != ERROR_SUCCESS) {
  1448. break;
  1449. }
  1450. memset(lpszName, 0, sizeof(WCHAR)*MAX_PATH);
  1451. dwSize = MAX_PATH;
  1452. }
  1453. return;
  1454. }