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.

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