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.

2578 lines
64 KiB

  1. #include "precomp.h"
  2. DWORD
  3. ProcessNFAs(
  4. PIPSEC_POLICY_OBJECT pIpsecPolicyObject,
  5. DWORD dwStoreType,
  6. PDWORD pdwSlientErrorCode,
  7. PIPSEC_POLICY_DATA * ppIpsecPolicyData
  8. )
  9. {
  10. DWORD dwError = 0;
  11. DWORD i = 0;
  12. DWORD NumberofRules = 0;
  13. PIPSEC_NFA_OBJECT * ppIpsecNFAObjects = NULL;
  14. PIPSEC_FILTER_OBJECT * ppIpsecFilterObjects = NULL;
  15. PIPSEC_NEGPOL_OBJECT * ppIpsecNegPolObjects = NULL;
  16. DWORD dwNumFilterObjects = 0;
  17. DWORD dwNumNegPolObjects = 0;
  18. PIPSEC_NFA_OBJECT pIpsecNFAObject = NULL;
  19. PIPSEC_NFA_DATA * ppIpsecNFAData = NULL;
  20. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  21. PIPSEC_POLICY_DATA pIpsecPolicyData = NULL;
  22. DWORD dwNumNFACount = 0;
  23. NumberofRules = pIpsecPolicyObject->NumberofRulesReturned;
  24. ppIpsecNFAObjects = pIpsecPolicyObject->ppIpsecNFAObjects;
  25. ppIpsecFilterObjects = pIpsecPolicyObject->ppIpsecFilterObjects;
  26. ppIpsecNegPolObjects = pIpsecPolicyObject->ppIpsecNegPolObjects;
  27. dwNumFilterObjects = pIpsecPolicyObject->NumberofFilters;
  28. dwNumNegPolObjects = pIpsecPolicyObject->NumberofNegPols;
  29. __try {
  30. dwError = UnmarshallPolicyObject(
  31. pIpsecPolicyObject,
  32. dwStoreType,
  33. &pIpsecPolicyData
  34. );
  35. } __except(EXCEPTION_EXECUTE_HANDLER) {
  36. dwError = ERROR_INVALID_DATA;
  37. }
  38. BAIL_ON_WIN32_ERROR(dwError);
  39. __try {
  40. dwError = UnmarshallISAKMPObject(
  41. *(pIpsecPolicyObject->ppIpsecISAKMPObjects),
  42. &pIpsecPolicyData->pIpsecISAKMPData
  43. );
  44. } __except(EXCEPTION_EXECUTE_HANDLER) {
  45. dwError = ERROR_INVALID_DATA;
  46. }
  47. BAIL_ON_WIN32_ERROR(dwError);
  48. ppIpsecNFAData = (PIPSEC_NFA_DATA *)AllocPolMem(
  49. sizeof(PIPSEC_NFA_DATA)* NumberofRules
  50. );
  51. if (!ppIpsecNFAData) {
  52. dwError = ERROR_OUTOFMEMORY;
  53. BAIL_ON_WIN32_ERROR(dwError);
  54. }
  55. for (i = 0; i < NumberofRules; i++) {
  56. pIpsecNFAObject = *(ppIpsecNFAObjects + i);
  57. dwError = ProcessNFA(
  58. pIpsecNFAObject,
  59. dwStoreType,
  60. ppIpsecFilterObjects,
  61. dwNumFilterObjects,
  62. ppIpsecNegPolObjects,
  63. dwNumNegPolObjects,
  64. &pIpsecNFAData
  65. );
  66. if (dwError == ERROR_SUCCESS) {
  67. if (pIpsecNFAData->dwActiveFlag != 0) {
  68. *(ppIpsecNFAData + dwNumNFACount) = pIpsecNFAData;
  69. dwNumNFACount++;
  70. }
  71. else {
  72. FreeIpsecNFAData(pIpsecNFAData);
  73. }
  74. }
  75. else {
  76. *pdwSlientErrorCode = dwError;
  77. }
  78. }
  79. pIpsecPolicyData->ppIpsecNFAData = ppIpsecNFAData;
  80. pIpsecPolicyData->dwNumNFACount = dwNumNFACount;
  81. *ppIpsecPolicyData = pIpsecPolicyData;
  82. return(dwError);
  83. error:
  84. if (pIpsecPolicyData) {
  85. FreeIpsecPolicyData(
  86. pIpsecPolicyData
  87. );
  88. }
  89. *ppIpsecPolicyData = NULL;
  90. return(dwError);
  91. }
  92. DWORD
  93. ProcessNFA(
  94. PIPSEC_NFA_OBJECT pIpsecNFAObject,
  95. DWORD dwStoreType,
  96. PIPSEC_FILTER_OBJECT * ppIpsecFilterObjects,
  97. DWORD dwNumFilterObjects,
  98. PIPSEC_NEGPOL_OBJECT * ppIpsecNegPolObjects,
  99. DWORD dwNumNegPolObjects,
  100. PIPSEC_NFA_DATA * ppIpsecNFAData
  101. )
  102. {
  103. DWORD dwError = 0;
  104. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  105. PIPSEC_NEGPOL_OBJECT pIpsecNegPolObject = NULL;
  106. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  107. __try {
  108. dwError = UnmarshallNFAObject(
  109. pIpsecNFAObject,
  110. dwStoreType,
  111. &pIpsecNFAData
  112. );
  113. } __except(EXCEPTION_EXECUTE_HANDLER) {
  114. dwError = ERROR_INVALID_DATA;
  115. }
  116. BAIL_ON_WIN32_ERROR(dwError);
  117. if (pIpsecNFAObject->pszIpsecFilterReference &&
  118. *pIpsecNFAObject->pszIpsecFilterReference) {
  119. dwError = FindIpsecFilterObject(
  120. pIpsecNFAObject,
  121. ppIpsecFilterObjects,
  122. dwNumFilterObjects,
  123. &pIpsecFilterObject
  124. );
  125. BAIL_ON_WIN32_ERROR(dwError);
  126. __try {
  127. dwError = UnmarshallFilterObject(
  128. pIpsecFilterObject,
  129. &pIpsecNFAData->pIpsecFilterData
  130. );
  131. } __except(EXCEPTION_EXECUTE_HANDLER) {
  132. dwError = ERROR_INVALID_DATA;
  133. }
  134. BAIL_ON_WIN32_ERROR(dwError);
  135. } else {
  136. //
  137. // We've received a NULL FilterReference or a NULL string FilterReference
  138. //
  139. //
  140. // This is acceptable - implies there is no filter associated
  141. //
  142. pIpsecNFAData->pIpsecFilterData = NULL;
  143. }
  144. dwError = FindIpsecNegPolObject(
  145. pIpsecNFAObject,
  146. ppIpsecNegPolObjects,
  147. dwNumNegPolObjects,
  148. &pIpsecNegPolObject
  149. );
  150. BAIL_ON_WIN32_ERROR(dwError);
  151. __try {
  152. dwError = UnmarshallNegPolObject(
  153. pIpsecNegPolObject,
  154. &pIpsecNFAData->pIpsecNegPolData
  155. );
  156. } __except(EXCEPTION_EXECUTE_HANDLER) {
  157. dwError = ERROR_INVALID_DATA;
  158. }
  159. BAIL_ON_WIN32_ERROR(dwError);
  160. *ppIpsecNFAData = pIpsecNFAData;
  161. return(dwError);
  162. error:
  163. if (pIpsecNFAData) {
  164. FreeIpsecNFAData(
  165. pIpsecNFAData
  166. );
  167. }
  168. return(dwError);
  169. }
  170. DWORD
  171. UnmarshallPolicyObject(
  172. PIPSEC_POLICY_OBJECT pIpsecPolicyObject,
  173. DWORD dwStoreType,
  174. PIPSEC_POLICY_DATA * ppIpsecPolicyData
  175. )
  176. {
  177. LPBYTE pMem = NULL;
  178. LPWSTR pszIpsecISAKMPReference = NULL;
  179. PIPSEC_POLICY_DATA pIpsecPolicyData = NULL;
  180. DWORD dwPollingInterval = 0;
  181. DWORD dwError = 0;
  182. DWORD dwSkipSize = 0;
  183. // {6A1F5C6F-72B7-11d2-ACF0-0060B0ECCA17}
  184. GUID GUID_POLSTORE_VERSION_INFO =
  185. { 0x6a1f5c6f, 0x72b7, 0x11d2, { 0xac, 0xf0, 0x0, 0x60, 0xb0, 0xec, 0xca, 0x17 } };
  186. // {22202163-4F4C-11d1-863B-00A0248D3021}
  187. static const GUID GUID_IPSEC_POLICY_DATA_BLOB =
  188. { 0x22202163, 0x4f4c, 0x11d1, { 0x86, 0x3b, 0x0, 0xa0, 0x24, 0x8d, 0x30, 0x21 } };
  189. pMem = pIpsecPolicyObject->pIpsecData;
  190. //
  191. // Check if the first blob is the version.
  192. //
  193. if (!memcmp(pMem, &(GUID_POLSTORE_VERSION_INFO), sizeof(GUID))) {
  194. pMem += sizeof(GUID);
  195. memcpy(&dwSkipSize, pMem, sizeof(DWORD));
  196. pMem += sizeof(DWORD);
  197. pMem += dwSkipSize;
  198. }
  199. pMem += sizeof(GUID);
  200. pMem += sizeof(DWORD);
  201. memcpy((LPBYTE)&dwPollingInterval, pMem, sizeof(DWORD));
  202. pIpsecPolicyData = (PIPSEC_POLICY_DATA)AllocPolMem(
  203. sizeof(IPSEC_POLICY_DATA)
  204. );
  205. if (!pIpsecPolicyData) {
  206. dwError = ERROR_OUTOFMEMORY;
  207. BAIL_ON_WIN32_ERROR(dwError);
  208. }
  209. if (pIpsecPolicyObject->pszIpsecName && *(pIpsecPolicyObject->pszIpsecName)) {
  210. pIpsecPolicyData->pszIpsecName = AllocPolStr(
  211. pIpsecPolicyObject->pszIpsecName
  212. );
  213. if (!pIpsecPolicyData->pszIpsecName) {
  214. dwError = ERROR_OUTOFMEMORY;
  215. BAIL_ON_WIN32_ERROR(dwError);
  216. }
  217. }
  218. if (pIpsecPolicyObject->pszDescription && *(pIpsecPolicyObject->pszDescription)){
  219. pIpsecPolicyData->pszDescription = AllocPolStr(
  220. pIpsecPolicyObject->pszDescription
  221. );
  222. if (!pIpsecPolicyData->pszDescription) {
  223. dwError = ERROR_OUTOFMEMORY;
  224. BAIL_ON_WIN32_ERROR(dwError);
  225. }
  226. }
  227. wGUIDFromString(pIpsecPolicyObject->pszIpsecID,
  228. &pIpsecPolicyData->PolicyIdentifier
  229. );
  230. switch(dwStoreType) {
  231. case IPSEC_REGISTRY_PROVIDER:
  232. dwError = GenGUIDFromRegISAKMPReference(
  233. pIpsecPolicyObject->pszIpsecISAKMPReference,
  234. &pIpsecPolicyData->ISAKMPIdentifier
  235. );
  236. BAIL_ON_WIN32_ERROR(dwError);
  237. break;
  238. case IPSEC_DIRECTORY_PROVIDER:
  239. dwError = CopyISAKMPDSToFQRegString(
  240. pIpsecPolicyObject->pszIpsecISAKMPReference,
  241. &pszIpsecISAKMPReference
  242. );
  243. BAIL_ON_WIN32_ERROR(dwError);
  244. dwError = GenGUIDFromRegISAKMPReference(
  245. pszIpsecISAKMPReference,
  246. &pIpsecPolicyData->ISAKMPIdentifier
  247. );
  248. BAIL_ON_WIN32_ERROR(dwError);
  249. break;
  250. default:
  251. dwError = ERROR_INVALID_PARAMETER;
  252. BAIL_ON_WIN32_ERROR(dwError);
  253. }
  254. pIpsecPolicyData->dwWhenChanged = pIpsecPolicyObject->dwWhenChanged;
  255. pIpsecPolicyData->dwPollingInterval = dwPollingInterval;
  256. *ppIpsecPolicyData = pIpsecPolicyData;
  257. if (pszIpsecISAKMPReference) {
  258. FreePolStr(pszIpsecISAKMPReference);
  259. }
  260. return(0);
  261. error:
  262. if (pIpsecPolicyData) {
  263. FreeIpsecPolicyData(pIpsecPolicyData);
  264. }
  265. if (pszIpsecISAKMPReference) {
  266. FreePolStr(pszIpsecISAKMPReference);
  267. }
  268. *ppIpsecPolicyData = NULL;
  269. return(dwError);
  270. }
  271. DWORD
  272. UnmarshallNFAObject(
  273. PIPSEC_NFA_OBJECT pIpsecNFAObject,
  274. DWORD dwStoreType,
  275. PIPSEC_NFA_DATA * ppIpsecNFAData
  276. )
  277. {
  278. LPBYTE pMem = NULL;
  279. DWORD dwNumAuthMethods = 0;
  280. PIPSEC_AUTH_METHOD * ppIpsecAuthMethods = NULL;
  281. DWORD dwError = 0;
  282. DWORD i = 0;
  283. PIPSEC_AUTH_METHOD pIpsecAuthMethod = NULL;
  284. DWORD dwInterfaceType = 0;
  285. DWORD dwInterfaceNameLen = 0;
  286. LPWSTR pszInterfaceName = NULL;
  287. DWORD dwTunnelIpAddr = 0;
  288. DWORD dwTunnelFlags = 0;
  289. DWORD dwActiveFlag = 0;
  290. DWORD dwEndPointNameLen = 0;
  291. LPWSTR pszEndPointName = NULL;
  292. DWORD dwNumBytesAdvanced = 0;
  293. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  294. LPWSTR pszIpsecFilterReference = NULL;
  295. LPWSTR pszIpsecNegPolReference = NULL;
  296. // {11BBAC00-498D-11d1-8639-00A0248D3021}
  297. static const GUID GUID_IPSEC_NFA_BLOB =
  298. { 0x11bbac00, 0x498d, 0x11d1, { 0x86, 0x39, 0x0, 0xa0, 0x24, 0x8d, 0x30, 0x21 } };
  299. DWORD dwReadBytes = 0;
  300. GUID TmpGuid;
  301. DWORD dwNumAltAuthMethods = 0;
  302. pMem = pIpsecNFAObject->pIpsecData;
  303. pMem += sizeof(GUID); // for the GUID
  304. pMem += sizeof(DWORD); // for the size
  305. memcpy((LPBYTE)&dwNumAuthMethods, pMem, sizeof(DWORD));
  306. pIpsecNFAData = (PIPSEC_NFA_DATA)AllocPolMem(
  307. sizeof(IPSEC_NFA_DATA)
  308. );
  309. if (!pIpsecNFAData) {
  310. dwError = ERROR_OUTOFMEMORY;
  311. BAIL_ON_WIN32_ERROR(dwError);
  312. }
  313. if (dwNumAuthMethods) {
  314. ppIpsecAuthMethods = (PIPSEC_AUTH_METHOD *) AllocPolMem(
  315. sizeof(PIPSEC_AUTH_METHOD)*dwNumAuthMethods
  316. );
  317. if (!ppIpsecAuthMethods) {
  318. dwError = ERROR_OUTOFMEMORY;
  319. BAIL_ON_WIN32_ERROR(dwError);
  320. }
  321. }
  322. pMem += sizeof(DWORD);
  323. for (i = 0; i < dwNumAuthMethods;i++){
  324. __try {
  325. dwError = UnmarshallAuthMethods(
  326. pMem,
  327. &pIpsecAuthMethod,
  328. &dwNumBytesAdvanced
  329. );
  330. } __except(EXCEPTION_EXECUTE_HANDLER) {
  331. dwError = ERROR_INVALID_DATA;
  332. }
  333. if (dwError) {
  334. pIpsecNFAData->dwAuthMethodCount = i;
  335. pIpsecNFAData->ppAuthMethods = ppIpsecAuthMethods;
  336. BAIL_ON_WIN32_ERROR(dwError);
  337. }
  338. pMem += dwNumBytesAdvanced;
  339. *(ppIpsecAuthMethods + i) = pIpsecAuthMethod;
  340. }
  341. pIpsecNFAData->dwAuthMethodCount = dwNumAuthMethods;
  342. pIpsecNFAData->ppAuthMethods = ppIpsecAuthMethods;
  343. memcpy((LPBYTE)&dwInterfaceType, pMem, sizeof(DWORD));
  344. pIpsecNFAData->dwInterfaceType = dwInterfaceType;
  345. pMem += sizeof(DWORD);
  346. memcpy((LPBYTE)&dwInterfaceNameLen, pMem, sizeof(DWORD));
  347. pMem += sizeof(DWORD);
  348. if (dwInterfaceNameLen) {
  349. pszInterfaceName = AllocPolStr((LPWSTR)pMem);
  350. if (!pszInterfaceName) {
  351. dwError = ERROR_OUTOFMEMORY;
  352. BAIL_ON_WIN32_ERROR(dwError);
  353. }
  354. }
  355. pIpsecNFAData->pszInterfaceName = pszInterfaceName;
  356. pMem += dwInterfaceNameLen;
  357. memcpy((LPBYTE)&dwTunnelIpAddr, pMem, sizeof(DWORD));
  358. pIpsecNFAData->dwTunnelIpAddr = dwTunnelIpAddr;
  359. pMem += sizeof(DWORD);
  360. memcpy((LPBYTE)&dwTunnelFlags, pMem, sizeof(DWORD));
  361. pIpsecNFAData->dwTunnelFlags = dwTunnelFlags;
  362. pMem += sizeof(DWORD);
  363. memcpy((LPBYTE)&dwActiveFlag, pMem, sizeof(DWORD));
  364. pIpsecNFAData->dwActiveFlag = dwActiveFlag;
  365. pMem += sizeof(DWORD);
  366. memcpy((LPBYTE)&dwEndPointNameLen, pMem, sizeof(DWORD));
  367. pMem += sizeof(DWORD);
  368. if (dwEndPointNameLen) {
  369. pszEndPointName = AllocPolStr((LPWSTR)pMem);
  370. if (!pszEndPointName) {
  371. dwError = ERROR_OUTOFMEMORY;
  372. BAIL_ON_WIN32_ERROR(dwError);
  373. }
  374. }
  375. pIpsecNFAData->pszEndPointName = pszEndPointName;
  376. pMem += dwEndPointNameLen;
  377. __try {
  378. dwReadBytes = (DWORD) ((BYTE *) pMem - (BYTE *) pIpsecNFAObject->pIpsecData);
  379. if ((dwReadBytes < pIpsecNFAObject->dwIpsecDataLen) &&
  380. ((pIpsecNFAObject->dwIpsecDataLen - dwReadBytes) > sizeof(GUID))) {
  381. memset(&TmpGuid, 1, sizeof(GUID));
  382. if (memcmp(pMem, &TmpGuid, sizeof(GUID)) == 0) {
  383. pMem += sizeof(GUID);
  384. memcpy(&dwNumAltAuthMethods, pMem, sizeof(DWORD));
  385. pMem += sizeof(DWORD);
  386. if (dwNumAltAuthMethods == dwNumAuthMethods) {
  387. for (i = 0; i < dwNumAuthMethods; i++) {
  388. dwError = UnmarshallAltAuthMethods(
  389. pMem,
  390. ppIpsecAuthMethods[i],
  391. &dwNumBytesAdvanced
  392. );
  393. if (dwError) {
  394. break;
  395. }
  396. pMem += dwNumBytesAdvanced;
  397. }
  398. }
  399. }
  400. }
  401. } __except(EXCEPTION_EXECUTE_HANDLER) {
  402. }
  403. dwError = ERROR_SUCCESS;
  404. //
  405. // Convert the ipsecID to its GUID format
  406. //
  407. wGUIDFromString(pIpsecNFAObject->pszIpsecID,
  408. &pIpsecNFAData->NFAIdentifier
  409. );
  410. if (pIpsecNFAObject->pszIpsecName && *(pIpsecNFAObject->pszIpsecName)) {
  411. pIpsecNFAData->pszIpsecName = AllocPolStr(
  412. pIpsecNFAObject->pszIpsecName
  413. );
  414. if (!pIpsecNFAData->pszIpsecName) {
  415. dwError = ERROR_OUTOFMEMORY;
  416. BAIL_ON_WIN32_ERROR(dwError);
  417. }
  418. }
  419. if (pIpsecNFAObject->pszDescription && *(pIpsecNFAObject->pszDescription)) {
  420. pIpsecNFAData->pszDescription = AllocPolStr(
  421. pIpsecNFAObject->pszDescription
  422. );
  423. if (!pIpsecNFAData->pszDescription) {
  424. dwError = ERROR_OUTOFMEMORY;
  425. BAIL_ON_WIN32_ERROR(dwError);
  426. }
  427. }
  428. switch(dwStoreType) {
  429. case IPSEC_REGISTRY_PROVIDER:
  430. dwError = GenGUIDFromRegFilterReference(
  431. pIpsecNFAObject->pszIpsecFilterReference,
  432. &pIpsecNFAData->FilterIdentifier
  433. );
  434. BAIL_ON_WIN32_ERROR(dwError);
  435. dwError = GenGUIDFromRegNegPolReference(
  436. pIpsecNFAObject->pszIpsecNegPolReference,
  437. &pIpsecNFAData->NegPolIdentifier
  438. );
  439. BAIL_ON_WIN32_ERROR(dwError);
  440. break;
  441. case IPSEC_DIRECTORY_PROVIDER:
  442. if (pIpsecNFAObject->pszIpsecFilterReference &&
  443. *pIpsecNFAObject->pszIpsecFilterReference) {
  444. dwError = CopyFilterDSToFQRegString(
  445. pIpsecNFAObject->pszIpsecFilterReference,
  446. &pszIpsecFilterReference
  447. );
  448. BAIL_ON_WIN32_ERROR(dwError);
  449. dwError = GenGUIDFromRegFilterReference(
  450. pszIpsecFilterReference,
  451. &pIpsecNFAData->FilterIdentifier
  452. );
  453. BAIL_ON_WIN32_ERROR(dwError);
  454. }
  455. dwError = CopyNegPolDSToFQRegString(
  456. pIpsecNFAObject->pszIpsecNegPolReference,
  457. &pszIpsecNegPolReference
  458. );
  459. BAIL_ON_WIN32_ERROR(dwError);
  460. dwError = GenGUIDFromRegNegPolReference(
  461. pszIpsecNegPolReference,
  462. &pIpsecNFAData->NegPolIdentifier
  463. );
  464. BAIL_ON_WIN32_ERROR(dwError);
  465. break;
  466. default:
  467. dwError = ERROR_INVALID_PARAMETER;
  468. BAIL_ON_WIN32_ERROR(dwError);
  469. }
  470. pIpsecNFAData->dwWhenChanged = pIpsecNFAObject->dwWhenChanged;
  471. *ppIpsecNFAData = pIpsecNFAData;
  472. if (pszIpsecFilterReference) {
  473. FreePolStr(pszIpsecFilterReference);
  474. }
  475. if (pszIpsecNegPolReference) {
  476. FreePolStr(pszIpsecNegPolReference);
  477. }
  478. return(0);
  479. error:
  480. if (pIpsecNFAData) {
  481. FreeIpsecNFAData(pIpsecNFAData);
  482. }
  483. if (pszIpsecFilterReference) {
  484. FreePolStr(pszIpsecFilterReference);
  485. }
  486. if (pszIpsecNegPolReference) {
  487. FreePolStr(pszIpsecNegPolReference);
  488. }
  489. *ppIpsecNFAData = NULL;
  490. return(dwError);
  491. }
  492. DWORD
  493. UnmarshallFilterObject(
  494. PIPSEC_FILTER_OBJECT pIpsecFilterObject,
  495. PIPSEC_FILTER_DATA * ppIpsecFilterData
  496. )
  497. {
  498. LPBYTE pMem = NULL;
  499. DWORD dwNumFilterSpecs = 0;
  500. PIPSEC_FILTER_SPEC * ppIpsecFilterSpecs = NULL;
  501. PIPSEC_FILTER_SPEC pIpsecFilterSpec = NULL;
  502. DWORD i = 0;
  503. DWORD dwNumBytesAdvanced = 0;
  504. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  505. DWORD dwError = 0;
  506. // {80DC20B5-2EC8-11d1-A89E-00A0248D3021}
  507. static const GUID GUID_IPSEC_FILTER_BLOB =
  508. { 0x80dc20b5, 0x2ec8, 0x11d1, { 0xa8, 0x9e, 0x0, 0xa0, 0x24, 0x8d, 0x30, 0x21 } };
  509. pIpsecFilterData = (PIPSEC_FILTER_DATA)AllocPolMem(
  510. sizeof(IPSEC_FILTER_DATA)
  511. );
  512. if (!pIpsecFilterData) {
  513. dwError = ERROR_OUTOFMEMORY;
  514. BAIL_ON_WIN32_ERROR(dwError);
  515. }
  516. pMem = pIpsecFilterObject->pIpsecData;
  517. pMem += sizeof(GUID);
  518. pMem += sizeof(DWORD);
  519. memcpy((LPBYTE)&dwNumFilterSpecs, pMem, sizeof(DWORD));
  520. if (dwNumFilterSpecs) {
  521. ppIpsecFilterSpecs = (PIPSEC_FILTER_SPEC *)AllocPolMem(
  522. sizeof(PIPSEC_FILTER_SPEC)*dwNumFilterSpecs
  523. );
  524. if (!ppIpsecFilterSpecs) {
  525. dwError = ERROR_OUTOFMEMORY;
  526. BAIL_ON_WIN32_ERROR(dwError);
  527. }
  528. }
  529. pMem += sizeof(DWORD);
  530. for (i = 0; i < dwNumFilterSpecs;i++){
  531. __try {
  532. dwError = UnmarshallFilterSpec(
  533. pMem,
  534. &pIpsecFilterSpec,
  535. &dwNumBytesAdvanced
  536. );
  537. } __except(EXCEPTION_EXECUTE_HANDLER) {
  538. dwError = ERROR_INVALID_DATA;
  539. }
  540. if (dwError) {
  541. pIpsecFilterData->dwNumFilterSpecs = i;
  542. pIpsecFilterData->ppFilterSpecs = ppIpsecFilterSpecs;
  543. BAIL_ON_WIN32_ERROR(dwError);
  544. }
  545. pMem += dwNumBytesAdvanced;
  546. *(ppIpsecFilterSpecs + i) = pIpsecFilterSpec;
  547. }
  548. pIpsecFilterData->dwNumFilterSpecs = dwNumFilterSpecs;
  549. pIpsecFilterData->ppFilterSpecs = ppIpsecFilterSpecs;
  550. //
  551. // Convert the ipsecID to its GUID format
  552. //
  553. if (pIpsecFilterObject->pszIpsecName && *(pIpsecFilterObject->pszIpsecName)) {
  554. pIpsecFilterData->pszIpsecName = AllocPolStr(
  555. pIpsecFilterObject->pszIpsecName
  556. );
  557. if (!pIpsecFilterData->pszIpsecName) {
  558. dwError = ERROR_OUTOFMEMORY;
  559. BAIL_ON_WIN32_ERROR(dwError);
  560. }
  561. }
  562. if (pIpsecFilterObject->pszDescription && *(pIpsecFilterObject->pszDescription)) {
  563. pIpsecFilterData->pszDescription = AllocPolStr(
  564. pIpsecFilterObject->pszDescription
  565. );
  566. if (!pIpsecFilterData->pszDescription) {
  567. dwError = ERROR_OUTOFMEMORY;
  568. BAIL_ON_WIN32_ERROR(dwError);
  569. }
  570. }
  571. wGUIDFromString(pIpsecFilterObject->pszIpsecID,
  572. &pIpsecFilterData->FilterIdentifier
  573. );
  574. pIpsecFilterData->dwWhenChanged = pIpsecFilterObject->dwWhenChanged;
  575. *ppIpsecFilterData = pIpsecFilterData;
  576. return(dwError);
  577. error:
  578. if (pIpsecFilterData) {
  579. FreeIpsecFilterData(pIpsecFilterData);
  580. }
  581. *ppIpsecFilterData = NULL;
  582. return(dwError);
  583. }
  584. DWORD
  585. UnmarshallNegPolObject(
  586. PIPSEC_NEGPOL_OBJECT pIpsecNegPolObject,
  587. PIPSEC_NEGPOL_DATA * ppIpsecNegPolData
  588. )
  589. {
  590. LPBYTE pMem = NULL;
  591. DWORD dwNumSecurityOffers = 0;
  592. PIPSEC_SECURITY_METHOD pIpsecOffer = NULL;
  593. PIPSEC_NEGPOL_DATA pIpsecNegPolData = NULL;
  594. DWORD dwError = 0;
  595. // {80DC20B9-2EC8-11d1-A89E-00A0248D3021}
  596. static const GUID GUID_IPSEC_NEGPOLICY_BLOB =
  597. { 0x80dc20b9, 0x2ec8, 0x11d1, { 0xa8, 0x9e, 0x0, 0xa0, 0x24, 0x8d, 0x30, 0x21 } };
  598. pMem = pIpsecNegPolObject->pIpsecData;
  599. pIpsecNegPolData = (PIPSEC_NEGPOL_DATA)AllocPolMem(
  600. sizeof(IPSEC_NEGPOL_DATA)
  601. );
  602. if (!pIpsecNegPolData) {
  603. dwError = ERROR_OUTOFMEMORY;
  604. BAIL_ON_WIN32_ERROR(dwError);
  605. }
  606. pMem += sizeof(GUID);
  607. pMem += sizeof(DWORD);
  608. memcpy((LPBYTE)&dwNumSecurityOffers, pMem, sizeof(DWORD));
  609. pMem += sizeof(DWORD);
  610. if (dwNumSecurityOffers) {
  611. pIpsecOffer = (PIPSEC_SECURITY_METHOD)AllocPolMem(
  612. sizeof(IPSEC_SECURITY_METHOD)*dwNumSecurityOffers
  613. );
  614. if (!pIpsecOffer) {
  615. dwError = ERROR_OUTOFMEMORY;
  616. BAIL_ON_WIN32_ERROR(dwError);
  617. }
  618. memcpy((LPBYTE)pIpsecOffer, pMem,
  619. sizeof(IPSEC_SECURITY_METHOD)*dwNumSecurityOffers);
  620. }
  621. //
  622. // Convert the ipsecID to its GUID format
  623. //
  624. if (pIpsecNegPolObject->pszIpsecName && *(pIpsecNegPolObject->pszIpsecName)) {
  625. pIpsecNegPolData->pszIpsecName = AllocPolStr(
  626. pIpsecNegPolObject->pszIpsecName
  627. );
  628. if (!pIpsecNegPolData->pszIpsecName) {
  629. dwError = ERROR_OUTOFMEMORY;
  630. BAIL_ON_WIN32_ERROR(dwError);
  631. }
  632. }
  633. if (pIpsecNegPolObject->pszDescription && *(pIpsecNegPolObject->pszDescription)){
  634. pIpsecNegPolData->pszDescription = AllocPolStr(
  635. pIpsecNegPolObject->pszDescription
  636. );
  637. if (!pIpsecNegPolData->pszDescription) {
  638. dwError = ERROR_OUTOFMEMORY;
  639. BAIL_ON_WIN32_ERROR(dwError);
  640. }
  641. }
  642. wGUIDFromString(pIpsecNegPolObject->pszIpsecID,
  643. &pIpsecNegPolData->NegPolIdentifier
  644. );
  645. wGUIDFromString(pIpsecNegPolObject->pszIpsecNegPolAction,
  646. &pIpsecNegPolData->NegPolAction
  647. );
  648. wGUIDFromString(pIpsecNegPolObject->pszIpsecNegPolType,
  649. &pIpsecNegPolData->NegPolType
  650. );
  651. pIpsecNegPolData->dwSecurityMethodCount = dwNumSecurityOffers;
  652. pIpsecNegPolData->pIpsecSecurityMethods = pIpsecOffer;
  653. pIpsecNegPolData->dwWhenChanged = pIpsecNegPolObject->dwWhenChanged;
  654. *ppIpsecNegPolData = pIpsecNegPolData;
  655. return(0);
  656. error:
  657. if (pIpsecOffer) {
  658. FreePolMem(pIpsecOffer);
  659. }
  660. if (pIpsecNegPolData) {
  661. FreeIpsecNegPolData(pIpsecNegPolData);
  662. }
  663. *ppIpsecNegPolData = NULL;
  664. return(dwError);
  665. }
  666. DWORD
  667. UnmarshallISAKMPObject(
  668. PIPSEC_ISAKMP_OBJECT pIpsecISAKMPObject,
  669. PIPSEC_ISAKMP_DATA * ppIpsecISAKMPData
  670. )
  671. {
  672. LPBYTE pMem = NULL;
  673. PIPSEC_ISAKMP_DATA pIpsecISAKMPData = NULL;
  674. DWORD dwNumISAKMPSecurityMethods = 0;
  675. PCRYPTO_BUNDLE pSecurityMethods = NULL;
  676. PCRYPTO_BUNDLE pSecurityMethod = NULL;
  677. DWORD i = 0;
  678. DWORD dwError = 0;
  679. // {80DC20B8-2EC8-11d1-A89E-00A0248D3021}
  680. static const GUID GUID_IPSEC_ISAKMP_POLICY_BLOB =
  681. { 0x80dc20b8, 0x2ec8, 0x11d1, { 0xa8, 0x9e, 0x0, 0xa0, 0x24, 0x8d, 0x30, 0x21 } };
  682. // CHECK THIS PART
  683. pMem = pIpsecISAKMPObject->pIpsecData;
  684. pIpsecISAKMPData = (PIPSEC_ISAKMP_DATA) AllocPolMem(
  685. sizeof(IPSEC_ISAKMP_DATA)
  686. );
  687. if (!pIpsecISAKMPData) {
  688. dwError = ERROR_OUTOFMEMORY;
  689. BAIL_ON_WIN32_ERROR(dwError);
  690. }
  691. pMem += sizeof(GUID);
  692. pMem += sizeof(DWORD);
  693. memcpy((LPBYTE)&pIpsecISAKMPData->ISAKMPPolicy, pMem, sizeof(ISAKMP_POLICY));
  694. pMem += sizeof(ISAKMP_POLICY);
  695. memcpy((LPBYTE)&dwNumISAKMPSecurityMethods, pMem, sizeof(DWORD));
  696. pMem += sizeof(DWORD);
  697. if (!dwNumISAKMPSecurityMethods) {
  698. dwError = ERROR_INVALID_DATA;
  699. BAIL_ON_WIN32_ERROR(dwError);
  700. }
  701. if (dwNumISAKMPSecurityMethods) {
  702. pSecurityMethods = (PCRYPTO_BUNDLE) AllocPolMem(
  703. sizeof(CRYPTO_BUNDLE)*dwNumISAKMPSecurityMethods
  704. );
  705. if (!pSecurityMethods) {
  706. dwError = ERROR_OUTOFMEMORY;
  707. BAIL_ON_WIN32_ERROR(dwError);
  708. }
  709. }
  710. for (i = 0; i < dwNumISAKMPSecurityMethods; i++) {
  711. pSecurityMethod = pSecurityMethods + i;
  712. memcpy(pSecurityMethod, pMem, sizeof(CRYPTO_BUNDLE));
  713. pMem += sizeof(CRYPTO_BUNDLE);
  714. }
  715. pIpsecISAKMPData->dwNumISAKMPSecurityMethods = dwNumISAKMPSecurityMethods;
  716. pIpsecISAKMPData->pSecurityMethods = pSecurityMethods;
  717. //
  718. // Convert the ipsecID to its GUID format
  719. //
  720. wGUIDFromString(pIpsecISAKMPObject->pszIpsecID,
  721. &pIpsecISAKMPData->ISAKMPIdentifier
  722. );
  723. pIpsecISAKMPData->dwWhenChanged = pIpsecISAKMPObject->dwWhenChanged;
  724. *ppIpsecISAKMPData = pIpsecISAKMPData;
  725. return(0);
  726. error:
  727. if (pIpsecISAKMPData) {
  728. FreeIpsecISAKMPData(pIpsecISAKMPData);
  729. }
  730. *ppIpsecISAKMPData = NULL;
  731. return(dwError);
  732. }
  733. DWORD
  734. FindIpsecFilterObject(
  735. PIPSEC_NFA_OBJECT pIpsecNFAObject,
  736. PIPSEC_FILTER_OBJECT * ppIpsecFilterObjects,
  737. DWORD dwNumFilterObjects,
  738. PIPSEC_FILTER_OBJECT * ppIpsecFilterObject
  739. )
  740. {
  741. DWORD i = 0;
  742. PIPSEC_FILTER_OBJECT pIpsecFilterObject = NULL;
  743. for (i = 0; i < dwNumFilterObjects; i++) {
  744. pIpsecFilterObject = *(ppIpsecFilterObjects + i);
  745. if (!_wcsicmp(pIpsecFilterObject->pszDistinguishedName,
  746. pIpsecNFAObject->pszIpsecFilterReference)) {
  747. *ppIpsecFilterObject = pIpsecFilterObject;
  748. return(0);
  749. }
  750. }
  751. *ppIpsecFilterObject = NULL;
  752. return(ERROR_NOT_FOUND);
  753. }
  754. DWORD
  755. FindIpsecNegPolObject(
  756. PIPSEC_NFA_OBJECT pIpsecNFAObject,
  757. PIPSEC_NEGPOL_OBJECT * ppIpsecNegPolObjects,
  758. DWORD dwNumNegPolObjects,
  759. PIPSEC_NEGPOL_OBJECT * ppIpsecNegPolObject
  760. )
  761. {
  762. DWORD i = 0;
  763. PIPSEC_NEGPOL_OBJECT pIpsecNegPolObject = NULL;
  764. for (i = 0; i < dwNumNegPolObjects; i++) {
  765. pIpsecNegPolObject = *(ppIpsecNegPolObjects + i);
  766. if (!_wcsicmp(pIpsecNegPolObject->pszDistinguishedName,
  767. pIpsecNFAObject->pszIpsecNegPolReference)) {
  768. *ppIpsecNegPolObject = pIpsecNegPolObject;
  769. return(0);
  770. }
  771. }
  772. *ppIpsecNegPolObject = NULL;
  773. return(ERROR_NOT_FOUND);
  774. }
  775. void
  776. FreeIpsecFilterData(
  777. PIPSEC_FILTER_DATA pIpsecFilterData
  778. )
  779. {
  780. DWORD dwNumFilterSpecs = 0;
  781. PIPSEC_FILTER_SPEC * ppIpsecFilterSpecs = NULL;
  782. DWORD i = 0;
  783. PIPSEC_FILTER_SPEC pIpsecFilterSpec = NULL;
  784. dwNumFilterSpecs = pIpsecFilterData->dwNumFilterSpecs;
  785. ppIpsecFilterSpecs = pIpsecFilterData->ppFilterSpecs;
  786. for (i = 0; i < dwNumFilterSpecs; i++) {
  787. pIpsecFilterSpec = *(ppIpsecFilterSpecs + i);
  788. if (pIpsecFilterSpec) {
  789. if (pIpsecFilterSpec->pszSrcDNSName){
  790. FreePolStr(pIpsecFilterSpec->pszSrcDNSName);
  791. }
  792. if (pIpsecFilterSpec->pszDestDNSName){
  793. FreePolStr(pIpsecFilterSpec->pszDestDNSName);
  794. }
  795. if (pIpsecFilterSpec->pszDescription){
  796. FreePolStr(pIpsecFilterSpec->pszDescription);
  797. }
  798. FreePolMem(pIpsecFilterSpec);
  799. }
  800. }
  801. FreePolMem(ppIpsecFilterSpecs);
  802. if (pIpsecFilterData->pszIpsecName) {
  803. FreePolStr(pIpsecFilterData->pszIpsecName);
  804. }
  805. if (pIpsecFilterData->pszDescription) {
  806. FreePolStr(pIpsecFilterData->pszDescription);
  807. }
  808. FreePolMem(pIpsecFilterData);
  809. return;
  810. }
  811. void
  812. FreeIpsecISAKMPData(
  813. PIPSEC_ISAKMP_DATA pIpsecISAKMPData
  814. )
  815. {
  816. if (pIpsecISAKMPData->pSecurityMethods) {
  817. FreePolMem(pIpsecISAKMPData->pSecurityMethods);
  818. }
  819. FreePolMem(pIpsecISAKMPData);
  820. return;
  821. }
  822. void
  823. FreeIpsecNegPolData(
  824. PIPSEC_NEGPOL_DATA pIpsecNegPolData
  825. )
  826. {
  827. if (pIpsecNegPolData->pIpsecSecurityMethods){
  828. FreePolMem(pIpsecNegPolData->pIpsecSecurityMethods);
  829. }
  830. if (pIpsecNegPolData->pszIpsecName) {
  831. FreePolStr(pIpsecNegPolData->pszIpsecName);
  832. }
  833. if (pIpsecNegPolData->pszDescription) {
  834. FreePolStr(pIpsecNegPolData->pszDescription);
  835. }
  836. FreePolMem(pIpsecNegPolData);
  837. return;
  838. }
  839. void
  840. FreeIpsecNFAData(
  841. PIPSEC_NFA_DATA pIpsecNFAData
  842. )
  843. {
  844. DWORD dwNumAuthMethods = 0;
  845. PIPSEC_AUTH_METHOD * ppIpsecAuthMethods = NULL;
  846. DWORD i = 0;
  847. PIPSEC_AUTH_METHOD pIpsecAuthMethod = NULL;
  848. ppIpsecAuthMethods = pIpsecNFAData->ppAuthMethods;
  849. dwNumAuthMethods = pIpsecNFAData->dwAuthMethodCount;
  850. for (i = 0; i < dwNumAuthMethods; i++) {
  851. pIpsecAuthMethod = *(ppIpsecAuthMethods + i);
  852. if (pIpsecAuthMethod) {
  853. if (pIpsecAuthMethod->pszAuthMethod) {
  854. FreePolStr(pIpsecAuthMethod->pszAuthMethod);
  855. }
  856. if (pIpsecAuthMethod->pAltAuthMethod) {
  857. FreePolMem(pIpsecAuthMethod->pAltAuthMethod);
  858. }
  859. FreePolMem(pIpsecAuthMethod);
  860. }
  861. }
  862. if (pIpsecNFAData->ppAuthMethods) {
  863. FreePolMem(pIpsecNFAData->ppAuthMethods);
  864. }
  865. if (pIpsecNFAData->pszInterfaceName) {
  866. FreePolStr(pIpsecNFAData->pszInterfaceName);
  867. }
  868. if (pIpsecNFAData->pszEndPointName) {
  869. FreePolStr(pIpsecNFAData->pszEndPointName);
  870. }
  871. if (pIpsecNFAData->pIpsecFilterData) {
  872. FreeIpsecFilterData(pIpsecNFAData->pIpsecFilterData);
  873. }
  874. if (pIpsecNFAData->pIpsecNegPolData) {
  875. FreeIpsecNegPolData(pIpsecNFAData->pIpsecNegPolData);
  876. }
  877. if (pIpsecNFAData->pszIpsecName) {
  878. FreePolStr(pIpsecNFAData->pszIpsecName);
  879. }
  880. if (pIpsecNFAData->pszDescription) {
  881. FreePolStr(pIpsecNFAData->pszDescription);
  882. }
  883. FreePolMem(pIpsecNFAData);
  884. }
  885. void
  886. FreeIpsecPolicyData(
  887. PIPSEC_POLICY_DATA pIpsecPolicyData
  888. )
  889. {
  890. DWORD i = 0;
  891. DWORD dwNumNFACount = 0;
  892. PIPSEC_NFA_DATA * ppIpsecNFAData = NULL;
  893. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  894. PIPSEC_ISAKMP_DATA pIpsecISAKMPData = NULL;
  895. dwNumNFACount = pIpsecPolicyData->dwNumNFACount;
  896. ppIpsecNFAData = pIpsecPolicyData->ppIpsecNFAData;
  897. pIpsecISAKMPData = pIpsecPolicyData->pIpsecISAKMPData;
  898. if (pIpsecISAKMPData) {
  899. FreeIpsecISAKMPData(
  900. pIpsecISAKMPData
  901. );
  902. }
  903. for (i = 0; i < dwNumNFACount; i++) {
  904. pIpsecNFAData = *(ppIpsecNFAData + i);
  905. if (pIpsecNFAData) {
  906. FreeIpsecNFAData(
  907. pIpsecNFAData
  908. );
  909. }
  910. }
  911. if (pIpsecPolicyData->ppIpsecNFAData) {
  912. FreePolMem(pIpsecPolicyData->ppIpsecNFAData);
  913. }
  914. if (pIpsecPolicyData->pszIpsecName) {
  915. FreePolStr(pIpsecPolicyData->pszIpsecName);
  916. }
  917. if (pIpsecPolicyData->pszDescription) {
  918. FreePolStr(pIpsecPolicyData->pszDescription);
  919. }
  920. if (pIpsecPolicyData) {
  921. FreePolMem(pIpsecPolicyData);
  922. }
  923. }
  924. DWORD
  925. CopyIpsecPolicyData(
  926. PIPSEC_POLICY_DATA pIpsecPolicyData,
  927. PIPSEC_POLICY_DATA * ppIpsecPolicyData
  928. )
  929. {
  930. DWORD dwError = 0;
  931. PIPSEC_POLICY_DATA pNewIpsecPolicyData = NULL;
  932. DWORD dwNumberofRules = 0;
  933. PIPSEC_NFA_DATA * ppIpsecNFAData = NULL;
  934. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  935. PIPSEC_NFA_DATA pNewIpsecNFAData = NULL;
  936. PIPSEC_NFA_DATA * ppNewIpsecNFAData = NULL;
  937. DWORD i = 0;
  938. *ppIpsecPolicyData = NULL;
  939. pNewIpsecPolicyData = (PIPSEC_POLICY_DATA) AllocPolMem(
  940. sizeof(IPSEC_POLICY_DATA)
  941. );
  942. if (!pNewIpsecPolicyData) {
  943. dwError = ERROR_OUTOFMEMORY;
  944. BAIL_ON_WIN32_ERROR(dwError);
  945. }
  946. dwNumberofRules = pIpsecPolicyData->dwNumNFACount;
  947. ppIpsecNFAData = pIpsecPolicyData->ppIpsecNFAData;
  948. if (dwNumberofRules) {
  949. ppNewIpsecNFAData = (PIPSEC_NFA_DATA *) AllocPolMem(
  950. sizeof(PIPSEC_NFA_DATA)*
  951. dwNumberofRules
  952. );
  953. if (!ppNewIpsecNFAData) {
  954. dwError = ERROR_OUTOFMEMORY;
  955. BAIL_ON_WIN32_ERROR(dwError);
  956. }
  957. }
  958. for (i = 0; i < dwNumberofRules; i++) {
  959. pIpsecNFAData = *(ppIpsecNFAData + i);
  960. dwError = CopyIpsecNFAData(
  961. pIpsecNFAData,
  962. &pNewIpsecNFAData
  963. );
  964. if (dwError) {
  965. pNewIpsecPolicyData->ppIpsecNFAData = ppNewIpsecNFAData;
  966. pNewIpsecPolicyData->dwNumNFACount = i;
  967. BAIL_ON_WIN32_ERROR(dwError);
  968. }
  969. *(ppNewIpsecNFAData + i) = pNewIpsecNFAData;
  970. }
  971. pNewIpsecPolicyData->ppIpsecNFAData = ppNewIpsecNFAData;
  972. pNewIpsecPolicyData->dwNumNFACount = dwNumberofRules;
  973. if (pIpsecPolicyData->pIpsecISAKMPData) {
  974. dwError = CopyIpsecISAKMPData(
  975. pIpsecPolicyData->pIpsecISAKMPData,
  976. &pNewIpsecPolicyData->pIpsecISAKMPData
  977. );
  978. BAIL_ON_WIN32_ERROR(dwError);
  979. }
  980. pNewIpsecPolicyData->dwPollingInterval = pIpsecPolicyData->dwPollingInterval;
  981. pNewIpsecPolicyData->dwWhenChanged = pIpsecPolicyData->dwWhenChanged;
  982. memcpy(
  983. &(pNewIpsecPolicyData->PolicyIdentifier),
  984. &(pIpsecPolicyData->PolicyIdentifier),
  985. sizeof(GUID)
  986. );
  987. memcpy(
  988. &(pNewIpsecPolicyData->ISAKMPIdentifier),
  989. &(pIpsecPolicyData->ISAKMPIdentifier),
  990. sizeof(GUID)
  991. );
  992. if (pIpsecPolicyData->pszIpsecName &&
  993. *pIpsecPolicyData->pszIpsecName) {
  994. pNewIpsecPolicyData->pszIpsecName = AllocPolStr(
  995. pIpsecPolicyData->pszIpsecName
  996. );
  997. if (!pNewIpsecPolicyData->pszIpsecName) {
  998. dwError = ERROR_OUTOFMEMORY;
  999. BAIL_ON_WIN32_ERROR(dwError);
  1000. }
  1001. }
  1002. if (pIpsecPolicyData->pszDescription &&
  1003. *pIpsecPolicyData->pszDescription) {
  1004. pNewIpsecPolicyData->pszDescription = AllocPolStr(
  1005. pIpsecPolicyData->pszDescription
  1006. );
  1007. if (!pNewIpsecPolicyData->pszDescription) {
  1008. dwError = ERROR_OUTOFMEMORY;
  1009. BAIL_ON_WIN32_ERROR(dwError);
  1010. }
  1011. }
  1012. *ppIpsecPolicyData = pNewIpsecPolicyData;
  1013. return (dwError);
  1014. error:
  1015. if (pNewIpsecPolicyData) {
  1016. FreeIpsecPolicyData(pNewIpsecPolicyData);
  1017. }
  1018. *ppIpsecPolicyData = NULL;
  1019. return (dwError);
  1020. }
  1021. DWORD
  1022. CopyIpsecNFAData(
  1023. PIPSEC_NFA_DATA pIpsecNFAData,
  1024. PIPSEC_NFA_DATA * ppIpsecNFAData
  1025. )
  1026. {
  1027. PIPSEC_NFA_DATA pNewIpsecNFAData = NULL;
  1028. DWORD dwError = 0;
  1029. DWORD i = 0;
  1030. DWORD dwAuthMethodCount = 0;
  1031. PIPSEC_AUTH_METHOD * ppAuthMethods = NULL;
  1032. PIPSEC_AUTH_METHOD * ppNewAuthMethods = NULL;
  1033. PIPSEC_AUTH_METHOD pAuthMethod = NULL;
  1034. PIPSEC_AUTH_METHOD pNewAuthMethod = NULL;
  1035. pNewIpsecNFAData = (PIPSEC_NFA_DATA) AllocPolMem(
  1036. sizeof(IPSEC_NFA_DATA)
  1037. );
  1038. if (!pNewIpsecNFAData) {
  1039. dwError = ERROR_OUTOFMEMORY;
  1040. BAIL_ON_WIN32_ERROR(dwError);
  1041. }
  1042. dwAuthMethodCount = pIpsecNFAData->dwAuthMethodCount;
  1043. ppAuthMethods = pIpsecNFAData->ppAuthMethods;
  1044. if (dwAuthMethodCount) {
  1045. ppNewAuthMethods = (PIPSEC_AUTH_METHOD *) AllocPolMem(
  1046. sizeof(PIPSEC_AUTH_METHOD)*
  1047. dwAuthMethodCount
  1048. );
  1049. if (!ppNewAuthMethods) {
  1050. dwError = ERROR_OUTOFMEMORY;
  1051. BAIL_ON_WIN32_ERROR(dwError);
  1052. }
  1053. }
  1054. for (i = 0; i < dwAuthMethodCount; i++) {
  1055. pAuthMethod = *(ppAuthMethods + i);
  1056. dwError = CopyIpsecAuthMethod(
  1057. pAuthMethod,
  1058. &pNewAuthMethod
  1059. );
  1060. if (dwError) {
  1061. pNewIpsecNFAData->ppAuthMethods = ppNewAuthMethods;
  1062. pNewIpsecNFAData->dwAuthMethodCount = i;
  1063. BAIL_ON_WIN32_ERROR(dwError);
  1064. }
  1065. *(ppNewAuthMethods + i) = pNewAuthMethod;
  1066. }
  1067. pNewIpsecNFAData->ppAuthMethods = ppNewAuthMethods;
  1068. pNewIpsecNFAData->dwAuthMethodCount = dwAuthMethodCount;
  1069. if (pIpsecNFAData->pszIpsecName && *pIpsecNFAData->pszIpsecName) {
  1070. pNewIpsecNFAData->pszIpsecName = AllocPolStr(
  1071. pIpsecNFAData->pszIpsecName
  1072. );
  1073. if (!(pNewIpsecNFAData->pszIpsecName)) {
  1074. dwError = ERROR_OUTOFMEMORY;
  1075. BAIL_ON_WIN32_ERROR(dwError);
  1076. }
  1077. }
  1078. if (pIpsecNFAData->pszDescription && *pIpsecNFAData->pszDescription) {
  1079. pNewIpsecNFAData->pszDescription = AllocPolStr(
  1080. pIpsecNFAData->pszDescription
  1081. );
  1082. if (!(pNewIpsecNFAData->pszDescription)) {
  1083. dwError = ERROR_OUTOFMEMORY;
  1084. BAIL_ON_WIN32_ERROR(dwError);
  1085. }
  1086. }
  1087. memcpy(
  1088. &(pNewIpsecNFAData->NFAIdentifier),
  1089. &(pIpsecNFAData->NFAIdentifier),
  1090. sizeof(GUID)
  1091. );
  1092. memcpy(
  1093. &(pNewIpsecNFAData->FilterIdentifier),
  1094. &(pIpsecNFAData->FilterIdentifier),
  1095. sizeof(GUID)
  1096. );
  1097. memcpy(
  1098. &(pNewIpsecNFAData->NegPolIdentifier),
  1099. &(pIpsecNFAData->NegPolIdentifier),
  1100. sizeof(GUID)
  1101. );
  1102. pNewIpsecNFAData->dwInterfaceType = pIpsecNFAData->dwInterfaceType;
  1103. if (pIpsecNFAData->pszInterfaceName && *pIpsecNFAData->pszInterfaceName) {
  1104. pNewIpsecNFAData->pszInterfaceName = AllocPolStr(
  1105. pIpsecNFAData->pszInterfaceName
  1106. );
  1107. if (!(pNewIpsecNFAData->pszInterfaceName)) {
  1108. dwError = ERROR_OUTOFMEMORY;
  1109. BAIL_ON_WIN32_ERROR(dwError);
  1110. }
  1111. }
  1112. pNewIpsecNFAData->dwTunnelIpAddr = pIpsecNFAData->dwTunnelIpAddr;
  1113. pNewIpsecNFAData->dwTunnelFlags = pIpsecNFAData->dwTunnelFlags;
  1114. pNewIpsecNFAData->dwActiveFlag = pIpsecNFAData->dwActiveFlag;
  1115. if (pIpsecNFAData->pszEndPointName && *pIpsecNFAData->pszEndPointName) {
  1116. pNewIpsecNFAData->pszEndPointName = AllocPolStr(
  1117. pIpsecNFAData->pszEndPointName
  1118. );
  1119. if (!(pNewIpsecNFAData->pszEndPointName)) {
  1120. dwError = ERROR_OUTOFMEMORY;
  1121. BAIL_ON_WIN32_ERROR(dwError);
  1122. }
  1123. }
  1124. if (pIpsecNFAData->pIpsecFilterData) {
  1125. dwError = CopyIpsecFilterData(
  1126. pIpsecNFAData->pIpsecFilterData,
  1127. &pNewIpsecNFAData->pIpsecFilterData
  1128. );
  1129. BAIL_ON_WIN32_ERROR(dwError);
  1130. }
  1131. if (pIpsecNFAData->pIpsecNegPolData) {
  1132. dwError = CopyIpsecNegPolData(
  1133. pIpsecNFAData->pIpsecNegPolData,
  1134. &pNewIpsecNFAData->pIpsecNegPolData
  1135. );
  1136. BAIL_ON_WIN32_ERROR(dwError);
  1137. }
  1138. pNewIpsecNFAData->dwWhenChanged = pIpsecNFAData->dwWhenChanged;
  1139. *ppIpsecNFAData = pNewIpsecNFAData;
  1140. return(ERROR_SUCCESS);
  1141. error:
  1142. if (pNewIpsecNFAData) {
  1143. FreeIpsecNFAData(pNewIpsecNFAData);
  1144. }
  1145. *ppIpsecNFAData = NULL;
  1146. return (dwError);
  1147. }
  1148. DWORD
  1149. CopyIpsecAuthMethod(
  1150. PIPSEC_AUTH_METHOD pAuthMethod,
  1151. PIPSEC_AUTH_METHOD * ppAuthMethod
  1152. )
  1153. {
  1154. DWORD dwError = 0;
  1155. PIPSEC_AUTH_METHOD pNewAuthMethod = NULL;
  1156. pNewAuthMethod = (PIPSEC_AUTH_METHOD) AllocPolMem(
  1157. sizeof(IPSEC_AUTH_METHOD)
  1158. );
  1159. if (!pNewAuthMethod) {
  1160. dwError = ERROR_OUTOFMEMORY;
  1161. BAIL_ON_WIN32_ERROR(dwError);
  1162. }
  1163. pNewAuthMethod->dwAuthType = pAuthMethod->dwAuthType;
  1164. pNewAuthMethod->dwAuthLen = pAuthMethod->dwAuthLen;
  1165. if (pAuthMethod->pszAuthMethod) {
  1166. pNewAuthMethod->pszAuthMethod = AllocPolStr(
  1167. pAuthMethod->pszAuthMethod
  1168. );
  1169. if (!(pNewAuthMethod->pszAuthMethod)) {
  1170. dwError = ERROR_OUTOFMEMORY;
  1171. BAIL_ON_WIN32_ERROR(dwError);
  1172. }
  1173. }
  1174. pNewAuthMethod->dwAltAuthLen = 0;
  1175. pNewAuthMethod->pAltAuthMethod = NULL;
  1176. if (pAuthMethod->dwAltAuthLen && pAuthMethod->pAltAuthMethod) {
  1177. pNewAuthMethod->pAltAuthMethod = AllocPolMem(
  1178. pAuthMethod->dwAltAuthLen
  1179. );
  1180. if (!(pNewAuthMethod->pAltAuthMethod)) {
  1181. dwError = ERROR_OUTOFMEMORY;
  1182. BAIL_ON_WIN32_ERROR(dwError);
  1183. }
  1184. memcpy(
  1185. pNewAuthMethod->pAltAuthMethod,
  1186. pAuthMethod->pAltAuthMethod,
  1187. pAuthMethod->dwAltAuthLen
  1188. );
  1189. pNewAuthMethod->dwAltAuthLen = pAuthMethod->dwAltAuthLen;
  1190. }
  1191. *ppAuthMethod = pNewAuthMethod;
  1192. return (ERROR_SUCCESS);
  1193. error:
  1194. if (pNewAuthMethod) {
  1195. if (pNewAuthMethod->pszAuthMethod) {
  1196. FreePolStr(pNewAuthMethod->pszAuthMethod);
  1197. }
  1198. if (pNewAuthMethod->pAltAuthMethod) {
  1199. FreePolMem(pNewAuthMethod->pAltAuthMethod);
  1200. }
  1201. FreePolMem(pNewAuthMethod);
  1202. }
  1203. *ppAuthMethod = NULL;
  1204. return (dwError);
  1205. }
  1206. DWORD
  1207. CopyIpsecISAKMPData(
  1208. PIPSEC_ISAKMP_DATA pIpsecISAKMPData,
  1209. PIPSEC_ISAKMP_DATA * ppIpsecISAKMPData
  1210. )
  1211. {
  1212. DWORD dwError = 0;
  1213. PIPSEC_ISAKMP_DATA pNewIpsecISAKMPData = NULL;
  1214. DWORD dwNumISAKMPSecurityMethods = 0;
  1215. pNewIpsecISAKMPData = (PIPSEC_ISAKMP_DATA) AllocPolMem(
  1216. sizeof(IPSEC_ISAKMP_DATA)
  1217. );
  1218. if (!pNewIpsecISAKMPData) {
  1219. dwError = ERROR_OUTOFMEMORY;
  1220. BAIL_ON_WIN32_ERROR(dwError);
  1221. }
  1222. memcpy(
  1223. &(pNewIpsecISAKMPData->ISAKMPIdentifier),
  1224. &(pIpsecISAKMPData->ISAKMPIdentifier),
  1225. sizeof(GUID)
  1226. );
  1227. memcpy(
  1228. &(pNewIpsecISAKMPData->ISAKMPPolicy),
  1229. &(pIpsecISAKMPData->ISAKMPPolicy),
  1230. sizeof(ISAKMP_POLICY)
  1231. );
  1232. dwNumISAKMPSecurityMethods =
  1233. pIpsecISAKMPData->dwNumISAKMPSecurityMethods;
  1234. pNewIpsecISAKMPData->dwWhenChanged =
  1235. pIpsecISAKMPData->dwWhenChanged;
  1236. if (pIpsecISAKMPData->pSecurityMethods) {
  1237. pNewIpsecISAKMPData->pSecurityMethods = (PCRYPTO_BUNDLE) AllocPolMem(
  1238. sizeof(CRYPTO_BUNDLE) *
  1239. dwNumISAKMPSecurityMethods
  1240. );
  1241. if (!(pNewIpsecISAKMPData->pSecurityMethods)) {
  1242. dwError = ERROR_OUTOFMEMORY;
  1243. BAIL_ON_WIN32_ERROR(dwError);
  1244. }
  1245. memcpy(
  1246. pNewIpsecISAKMPData->pSecurityMethods,
  1247. pIpsecISAKMPData->pSecurityMethods,
  1248. sizeof(CRYPTO_BUNDLE)*dwNumISAKMPSecurityMethods
  1249. );
  1250. pNewIpsecISAKMPData->dwNumISAKMPSecurityMethods =
  1251. pIpsecISAKMPData->dwNumISAKMPSecurityMethods;
  1252. }
  1253. else {
  1254. pNewIpsecISAKMPData->pSecurityMethods = NULL;
  1255. pNewIpsecISAKMPData->dwNumISAKMPSecurityMethods = 0;
  1256. }
  1257. *ppIpsecISAKMPData = pNewIpsecISAKMPData;
  1258. return(ERROR_SUCCESS);
  1259. error:
  1260. *ppIpsecISAKMPData = NULL;
  1261. return (dwError);
  1262. }
  1263. DWORD
  1264. CopyIpsecFilterData(
  1265. PIPSEC_FILTER_DATA pIpsecFilterData,
  1266. PIPSEC_FILTER_DATA * ppIpsecFilterData
  1267. )
  1268. {
  1269. DWORD dwError = 0;
  1270. DWORD i = 0;
  1271. PIPSEC_FILTER_DATA pNewIpsecFilterData = NULL;
  1272. DWORD dwNumFilterSpecs = 0;
  1273. PIPSEC_FILTER_SPEC * ppFilterSpecs = NULL;
  1274. PIPSEC_FILTER_SPEC * ppNewFilterSpecs = NULL;
  1275. PIPSEC_FILTER_SPEC pFilterSpecs = NULL;
  1276. PIPSEC_FILTER_SPEC pNewFilterSpecs = NULL;
  1277. pNewIpsecFilterData = (PIPSEC_FILTER_DATA) AllocPolMem(
  1278. sizeof(IPSEC_FILTER_DATA)
  1279. );
  1280. if (!pNewIpsecFilterData) {
  1281. dwError = ERROR_OUTOFMEMORY;
  1282. BAIL_ON_WIN32_ERROR(dwError);
  1283. }
  1284. dwNumFilterSpecs = pIpsecFilterData->dwNumFilterSpecs;
  1285. ppFilterSpecs = pIpsecFilterData->ppFilterSpecs;
  1286. if (dwNumFilterSpecs) {
  1287. ppNewFilterSpecs = (PIPSEC_FILTER_SPEC *) AllocPolMem(
  1288. sizeof(PIPSEC_FILTER_SPEC)*
  1289. dwNumFilterSpecs
  1290. );
  1291. if (!ppNewFilterSpecs) {
  1292. dwError = ERROR_OUTOFMEMORY;
  1293. BAIL_ON_WIN32_ERROR(dwError);
  1294. }
  1295. }
  1296. for (i = 0; i < dwNumFilterSpecs; i++) {
  1297. pFilterSpecs = *(ppFilterSpecs + i);
  1298. dwError = CopyIpsecFilterSpec(
  1299. pFilterSpecs,
  1300. &pNewFilterSpecs
  1301. );
  1302. if (dwError) {
  1303. pNewIpsecFilterData->ppFilterSpecs = ppNewFilterSpecs;
  1304. pNewIpsecFilterData->dwNumFilterSpecs = i;
  1305. BAIL_ON_WIN32_ERROR(dwError);
  1306. }
  1307. *(ppNewFilterSpecs + i) = pNewFilterSpecs;
  1308. }
  1309. pNewIpsecFilterData->ppFilterSpecs = ppNewFilterSpecs;
  1310. pNewIpsecFilterData->dwNumFilterSpecs = dwNumFilterSpecs;
  1311. if (pIpsecFilterData->pszIpsecName && *pIpsecFilterData->pszIpsecName) {
  1312. pNewIpsecFilterData->pszIpsecName = AllocPolStr(
  1313. pIpsecFilterData->pszIpsecName
  1314. );
  1315. if (!(pNewIpsecFilterData->pszIpsecName)) {
  1316. dwError = ERROR_OUTOFMEMORY;
  1317. BAIL_ON_WIN32_ERROR(dwError);
  1318. }
  1319. }
  1320. if (pIpsecFilterData->pszDescription && *pIpsecFilterData->pszDescription) {
  1321. pNewIpsecFilterData->pszDescription = AllocPolStr(
  1322. pIpsecFilterData->pszDescription
  1323. );
  1324. if (!(pNewIpsecFilterData->pszDescription)) {
  1325. dwError = ERROR_OUTOFMEMORY;
  1326. BAIL_ON_WIN32_ERROR(dwError);
  1327. }
  1328. }
  1329. memcpy(
  1330. &(pNewIpsecFilterData->FilterIdentifier),
  1331. &(pIpsecFilterData->FilterIdentifier),
  1332. sizeof(GUID)
  1333. );
  1334. pNewIpsecFilterData->dwWhenChanged = pIpsecFilterData->dwWhenChanged;
  1335. *ppIpsecFilterData = pNewIpsecFilterData;
  1336. return(ERROR_SUCCESS);
  1337. error:
  1338. if (pNewIpsecFilterData) {
  1339. FreeIpsecFilterData(pNewIpsecFilterData);
  1340. }
  1341. *ppIpsecFilterData = NULL;
  1342. return (dwError);
  1343. }
  1344. DWORD
  1345. CopyIpsecFilterSpec(
  1346. PIPSEC_FILTER_SPEC pFilterSpecs,
  1347. PIPSEC_FILTER_SPEC * ppFilterSpecs
  1348. )
  1349. {
  1350. DWORD dwError = 0;
  1351. PIPSEC_FILTER_SPEC pNewFilterSpecs = NULL;
  1352. pNewFilterSpecs = (PIPSEC_FILTER_SPEC) AllocPolMem(
  1353. sizeof(IPSEC_FILTER_SPEC)
  1354. );
  1355. if (!pNewFilterSpecs) {
  1356. dwError = ERROR_OUTOFMEMORY;
  1357. BAIL_ON_WIN32_ERROR(dwError);
  1358. }
  1359. if (pFilterSpecs->pszSrcDNSName) {
  1360. pNewFilterSpecs->pszSrcDNSName = AllocPolStr(
  1361. pFilterSpecs->pszSrcDNSName
  1362. );
  1363. if (!(pNewFilterSpecs->pszSrcDNSName)) {
  1364. dwError = ERROR_OUTOFMEMORY;
  1365. BAIL_ON_WIN32_ERROR(dwError);
  1366. }
  1367. }
  1368. if (pFilterSpecs->pszDestDNSName) {
  1369. pNewFilterSpecs->pszDestDNSName = AllocPolStr(
  1370. pFilterSpecs->pszDestDNSName
  1371. );
  1372. if (!(pNewFilterSpecs->pszDestDNSName)) {
  1373. dwError = ERROR_OUTOFMEMORY;
  1374. BAIL_ON_WIN32_ERROR(dwError);
  1375. }
  1376. }
  1377. if (pFilterSpecs->pszDescription) {
  1378. pNewFilterSpecs->pszDescription = AllocPolStr(
  1379. pFilterSpecs->pszDescription
  1380. );
  1381. if (!(pNewFilterSpecs->pszDescription)) {
  1382. dwError = ERROR_OUTOFMEMORY;
  1383. BAIL_ON_WIN32_ERROR(dwError);
  1384. }
  1385. }
  1386. memcpy(
  1387. &(pNewFilterSpecs->FilterSpecGUID),
  1388. &(pFilterSpecs->FilterSpecGUID),
  1389. sizeof(GUID)
  1390. );
  1391. pNewFilterSpecs->dwMirrorFlag = pFilterSpecs->dwMirrorFlag;
  1392. memcpy(
  1393. &(pNewFilterSpecs->Filter),
  1394. &(pFilterSpecs->Filter),
  1395. sizeof(IPSEC_FILTER)
  1396. );
  1397. *ppFilterSpecs = pNewFilterSpecs;
  1398. return(ERROR_SUCCESS);
  1399. error:
  1400. if (pNewFilterSpecs) {
  1401. if (pNewFilterSpecs->pszSrcDNSName){
  1402. FreePolStr(pNewFilterSpecs->pszSrcDNSName);
  1403. }
  1404. if (pNewFilterSpecs->pszDestDNSName){
  1405. FreePolStr(pNewFilterSpecs->pszDestDNSName);
  1406. }
  1407. if (pNewFilterSpecs->pszDescription){
  1408. FreePolStr(pNewFilterSpecs->pszDescription);
  1409. }
  1410. FreePolMem(pNewFilterSpecs);
  1411. }
  1412. *ppFilterSpecs = NULL;
  1413. return (dwError);
  1414. }
  1415. DWORD
  1416. CopyIpsecNegPolData(
  1417. PIPSEC_NEGPOL_DATA pIpsecNegPolData,
  1418. PIPSEC_NEGPOL_DATA * ppIpsecNegPolData
  1419. )
  1420. {
  1421. DWORD dwNumSecurityOffers = 0;
  1422. PIPSEC_NEGPOL_DATA pNewIpsecNegPolData = NULL;
  1423. DWORD dwError = 0;
  1424. pNewIpsecNegPolData = (PIPSEC_NEGPOL_DATA) AllocPolMem(
  1425. sizeof(IPSEC_NEGPOL_DATA)
  1426. );
  1427. if (!pNewIpsecNegPolData) {
  1428. dwError = ERROR_OUTOFMEMORY;
  1429. BAIL_ON_WIN32_ERROR(dwError);
  1430. }
  1431. dwNumSecurityOffers = pIpsecNegPolData->dwSecurityMethodCount;
  1432. if (dwNumSecurityOffers) {
  1433. pNewIpsecNegPolData->pIpsecSecurityMethods = (PIPSEC_SECURITY_METHOD) AllocPolMem(
  1434. sizeof(IPSEC_SECURITY_METHOD)*dwNumSecurityOffers
  1435. );
  1436. if (!(pNewIpsecNegPolData->pIpsecSecurityMethods)) {
  1437. dwError = ERROR_OUTOFMEMORY;
  1438. BAIL_ON_WIN32_ERROR(dwError);
  1439. }
  1440. memcpy(
  1441. (pNewIpsecNegPolData->pIpsecSecurityMethods),
  1442. (pIpsecNegPolData->pIpsecSecurityMethods),
  1443. sizeof(IPSEC_SECURITY_METHOD)*dwNumSecurityOffers
  1444. );
  1445. pNewIpsecNegPolData->dwSecurityMethodCount = dwNumSecurityOffers;
  1446. }
  1447. else {
  1448. pNewIpsecNegPolData->dwSecurityMethodCount = 0;
  1449. pNewIpsecNegPolData->pIpsecSecurityMethods = NULL;
  1450. }
  1451. if (pIpsecNegPolData->pszIpsecName && *pIpsecNegPolData->pszIpsecName) {
  1452. pNewIpsecNegPolData->pszIpsecName = AllocPolStr(
  1453. pIpsecNegPolData->pszIpsecName
  1454. );
  1455. if (!(pNewIpsecNegPolData->pszIpsecName)) {
  1456. dwError = ERROR_OUTOFMEMORY;
  1457. BAIL_ON_WIN32_ERROR(dwError);
  1458. }
  1459. }
  1460. if (pIpsecNegPolData->pszDescription && *pIpsecNegPolData->pszDescription) {
  1461. pNewIpsecNegPolData->pszDescription = AllocPolStr(
  1462. pIpsecNegPolData->pszDescription
  1463. );
  1464. if (!(pNewIpsecNegPolData->pszDescription)) {
  1465. dwError = ERROR_OUTOFMEMORY;
  1466. BAIL_ON_WIN32_ERROR(dwError);
  1467. }
  1468. }
  1469. //
  1470. // Convert the ipsecID to its GUID format
  1471. //
  1472. memcpy(
  1473. &(pNewIpsecNegPolData->NegPolIdentifier),
  1474. &(pIpsecNegPolData->NegPolIdentifier),
  1475. sizeof(GUID)
  1476. );
  1477. memcpy(
  1478. &(pNewIpsecNegPolData->NegPolAction),
  1479. &(pIpsecNegPolData->NegPolAction),
  1480. sizeof(GUID)
  1481. );
  1482. memcpy(
  1483. &(pNewIpsecNegPolData->NegPolType),
  1484. &(pIpsecNegPolData->NegPolType),
  1485. sizeof(GUID)
  1486. );
  1487. pNewIpsecNegPolData->dwWhenChanged = pIpsecNegPolData->dwWhenChanged;
  1488. *ppIpsecNegPolData = pNewIpsecNegPolData;
  1489. return (0);
  1490. error:
  1491. if (pNewIpsecNegPolData) {
  1492. FreeIpsecNegPolData(pNewIpsecNegPolData);
  1493. }
  1494. *ppIpsecNegPolData = NULL;
  1495. return(dwError);
  1496. }
  1497. DWORD
  1498. UnmarshallFilterSpec(
  1499. LPBYTE pMem,
  1500. PIPSEC_FILTER_SPEC * ppIpsecFilterSpec,
  1501. PDWORD pdwNumBytesAdvanced
  1502. )
  1503. {
  1504. DWORD dwSrcDNSNameLen = 0;
  1505. LPWSTR pszSrcDNSName = NULL;
  1506. DWORD dwDestDNSNameLen = 0;
  1507. LPWSTR pszDestDNSName = NULL;
  1508. DWORD dwDescriptionLen = 0;
  1509. LPWSTR pszDescription = NULL;
  1510. GUID FilterSpecGUID;
  1511. DWORD dwMirrorFlag = 0;
  1512. IPSEC_FILTER ipsecFilter;
  1513. PIPSEC_FILTER_SPEC pIpsecFilterSpec = NULL;
  1514. DWORD dwNumBytesAdvanced = 0;
  1515. DWORD dwError = 0;
  1516. *ppIpsecFilterSpec = NULL;
  1517. *pdwNumBytesAdvanced = 0;
  1518. memcpy((LPBYTE)&dwSrcDNSNameLen, pMem, sizeof(DWORD));
  1519. pMem += sizeof(DWORD);
  1520. dwNumBytesAdvanced += sizeof(DWORD);
  1521. if (dwSrcDNSNameLen) {
  1522. pszSrcDNSName = AllocPolStr((LPWSTR)pMem);
  1523. if (!pszSrcDNSName) {
  1524. dwError = ERROR_OUTOFMEMORY;
  1525. BAIL_ON_WIN32_ERROR(dwError);
  1526. }
  1527. }
  1528. pMem += dwSrcDNSNameLen;
  1529. dwNumBytesAdvanced += dwSrcDNSNameLen;
  1530. memcpy((LPBYTE)&dwDestDNSNameLen, pMem, sizeof(DWORD));
  1531. pMem += sizeof(DWORD);
  1532. dwNumBytesAdvanced += sizeof(DWORD);
  1533. if (dwDestDNSNameLen) {
  1534. pszDestDNSName = AllocPolStr((LPWSTR)pMem);
  1535. if (!pszDestDNSName) {
  1536. dwError = ERROR_OUTOFMEMORY;
  1537. BAIL_ON_WIN32_ERROR(dwError);
  1538. }
  1539. }
  1540. pMem += dwDestDNSNameLen;
  1541. dwNumBytesAdvanced += dwDestDNSNameLen;
  1542. memcpy((LPBYTE)&dwDescriptionLen, pMem, sizeof(DWORD));
  1543. pMem += sizeof(DWORD);
  1544. dwNumBytesAdvanced += sizeof(DWORD);
  1545. if (dwDescriptionLen) {
  1546. pszDescription = AllocPolStr((LPWSTR)pMem);
  1547. if (!pszDescription) {
  1548. dwError = ERROR_OUTOFMEMORY;
  1549. BAIL_ON_WIN32_ERROR(dwError);
  1550. }
  1551. }
  1552. pMem += dwDescriptionLen;
  1553. dwNumBytesAdvanced += dwDescriptionLen;
  1554. memcpy((LPBYTE)&FilterSpecGUID, pMem, sizeof(GUID));
  1555. pMem += sizeof(GUID);
  1556. dwNumBytesAdvanced += sizeof(GUID);
  1557. memcpy((LPBYTE)&dwMirrorFlag, pMem, sizeof(DWORD));
  1558. pMem += sizeof(DWORD);
  1559. dwNumBytesAdvanced += sizeof(DWORD);
  1560. memcpy((LPBYTE)&ipsecFilter, pMem, sizeof(IPSEC_FILTER));
  1561. pIpsecFilterSpec = (PIPSEC_FILTER_SPEC)AllocPolMem(
  1562. sizeof(IPSEC_FILTER_SPEC)
  1563. );
  1564. if (!pIpsecFilterSpec) {
  1565. dwError = ERROR_OUTOFMEMORY;
  1566. BAIL_ON_WIN32_ERROR(dwError);
  1567. }
  1568. pMem += sizeof(IPSEC_FILTER);
  1569. dwNumBytesAdvanced += sizeof(IPSEC_FILTER);
  1570. pIpsecFilterSpec->pszSrcDNSName = pszSrcDNSName;
  1571. pIpsecFilterSpec->pszDestDNSName = pszDestDNSName;
  1572. pIpsecFilterSpec->pszDescription = pszDescription;
  1573. pIpsecFilterSpec->dwMirrorFlag = dwMirrorFlag;
  1574. memcpy((LPBYTE)&pIpsecFilterSpec->FilterSpecGUID, &FilterSpecGUID, sizeof(GUID));
  1575. memcpy((LPBYTE)&pIpsecFilterSpec->Filter, &ipsecFilter, sizeof(IPSEC_FILTER));
  1576. *ppIpsecFilterSpec = pIpsecFilterSpec;
  1577. *pdwNumBytesAdvanced = dwNumBytesAdvanced;
  1578. return(dwError);
  1579. error:
  1580. if (pszSrcDNSName) {
  1581. FreePolStr(pszSrcDNSName);
  1582. }
  1583. if (pszDestDNSName) {
  1584. FreePolStr(pszDestDNSName);
  1585. }
  1586. if (pszDescription) {
  1587. FreePolStr(pszDescription);
  1588. }
  1589. *ppIpsecFilterSpec = NULL;
  1590. *pdwNumBytesAdvanced = 0;
  1591. return(dwError);
  1592. }
  1593. DWORD
  1594. UnmarshallAuthMethods(
  1595. LPBYTE pMem,
  1596. PIPSEC_AUTH_METHOD * ppIpsecAuthMethod,
  1597. PDWORD pdwNumBytesAdvanced
  1598. )
  1599. {
  1600. DWORD dwError = 0;
  1601. DWORD dwAuthType = 0;
  1602. DWORD dwAuthLen = 0;
  1603. LPWSTR pszAuthMethod = NULL;
  1604. PIPSEC_AUTH_METHOD pIpsecAuthMethod = NULL;
  1605. DWORD dwNumBytesAdvanced = 0;
  1606. pIpsecAuthMethod = (PIPSEC_AUTH_METHOD)AllocPolMem(
  1607. sizeof(IPSEC_AUTH_METHOD)
  1608. );
  1609. if (!pIpsecAuthMethod) {
  1610. dwError = ERROR_OUTOFMEMORY;
  1611. BAIL_ON_WIN32_ERROR(dwError);
  1612. }
  1613. memcpy((LPBYTE)&dwAuthType, pMem, sizeof(DWORD));
  1614. pMem += sizeof(DWORD);
  1615. dwNumBytesAdvanced += sizeof(DWORD);
  1616. memcpy((LPBYTE)&dwAuthLen, pMem, sizeof(DWORD));
  1617. pMem += sizeof(DWORD);
  1618. dwNumBytesAdvanced += sizeof(DWORD);
  1619. if (dwAuthLen) {
  1620. pszAuthMethod = AllocPolStr((LPWSTR)pMem);
  1621. if (!pszAuthMethod) {
  1622. dwError = ERROR_OUTOFMEMORY;
  1623. BAIL_ON_WIN32_ERROR(dwError);
  1624. }
  1625. }
  1626. pMem += dwAuthLen;
  1627. dwNumBytesAdvanced += dwAuthLen;
  1628. //
  1629. // Unmarshall parameters.
  1630. //
  1631. pIpsecAuthMethod->dwAuthType = dwAuthType;
  1632. pIpsecAuthMethod->dwAuthLen = (dwAuthLen - 2)/2;
  1633. pIpsecAuthMethod->pszAuthMethod = pszAuthMethod;
  1634. pIpsecAuthMethod->dwAltAuthLen = 0;
  1635. pIpsecAuthMethod->pAltAuthMethod = NULL;
  1636. *ppIpsecAuthMethod = pIpsecAuthMethod;
  1637. *pdwNumBytesAdvanced = dwNumBytesAdvanced;
  1638. return (dwError);
  1639. error:
  1640. if (pszAuthMethod) {
  1641. FreePolStr(pszAuthMethod);
  1642. }
  1643. if (pIpsecAuthMethod) {
  1644. FreePolMem(pIpsecAuthMethod);
  1645. }
  1646. *ppIpsecAuthMethod = NULL;
  1647. *pdwNumBytesAdvanced = 0;
  1648. return (dwError);
  1649. }
  1650. DWORD
  1651. UnmarshallAltAuthMethods(
  1652. LPBYTE pMem,
  1653. PIPSEC_AUTH_METHOD pIpsecAuthMethod,
  1654. PDWORD pdwNumBytesAdvanced
  1655. )
  1656. {
  1657. DWORD dwError = 0;
  1658. DWORD dwAuthType = 0;
  1659. DWORD dwAuthLen = 0;
  1660. PBYTE pAltAuthMethod = NULL;
  1661. DWORD dwNumBytesAdvanced = 0;
  1662. memcpy((LPBYTE)&dwAuthType, pMem, sizeof(DWORD));
  1663. pMem += sizeof(DWORD);
  1664. dwNumBytesAdvanced += sizeof(DWORD);
  1665. memcpy((LPBYTE)&dwAuthLen, pMem, sizeof(DWORD));
  1666. pMem += sizeof(DWORD);
  1667. dwNumBytesAdvanced += sizeof(DWORD);
  1668. if (dwAuthLen) {
  1669. pAltAuthMethod = (PBYTE) AllocPolMem(dwAuthLen);
  1670. if (!pAltAuthMethod) {
  1671. dwError = ERROR_OUTOFMEMORY;
  1672. BAIL_ON_WIN32_ERROR(dwError);
  1673. }
  1674. memcpy(pAltAuthMethod, pMem, dwAuthLen);
  1675. }
  1676. pMem += dwAuthLen;
  1677. dwNumBytesAdvanced += dwAuthLen;
  1678. pIpsecAuthMethod->dwAltAuthLen = dwAuthLen;
  1679. pIpsecAuthMethod->pAltAuthMethod = pAltAuthMethod;
  1680. *pdwNumBytesAdvanced = dwNumBytesAdvanced;
  1681. return (dwError);
  1682. error:
  1683. if (pAltAuthMethod) {
  1684. FreePolMem(pAltAuthMethod);
  1685. }
  1686. pIpsecAuthMethod->dwAltAuthLen = 0;
  1687. pIpsecAuthMethod->pAltAuthMethod = NULL;
  1688. *pdwNumBytesAdvanced = 0;
  1689. return (dwError);
  1690. }
  1691. void
  1692. FreeMulIpsecFilterData(
  1693. PIPSEC_FILTER_DATA * ppIpsecFilterData,
  1694. DWORD dwNumFilterObjects
  1695. )
  1696. {
  1697. DWORD i = 0;
  1698. PIPSEC_FILTER_DATA pIpsecFilterData = NULL;
  1699. if (!ppIpsecFilterData) {
  1700. return;
  1701. }
  1702. for (i = 0; i < dwNumFilterObjects; i++) {
  1703. pIpsecFilterData = *(ppIpsecFilterData + i);
  1704. if (pIpsecFilterData) {
  1705. FreeIpsecFilterData(pIpsecFilterData);
  1706. }
  1707. }
  1708. FreePolMem(ppIpsecFilterData);
  1709. return;
  1710. }
  1711. void
  1712. FreeMulIpsecNegPolData(
  1713. PIPSEC_NEGPOL_DATA * ppIpsecNegPolData,
  1714. DWORD dwNumNegPolObjects
  1715. )
  1716. {
  1717. DWORD i = 0;
  1718. PIPSEC_NEGPOL_DATA pIpsecNegPolData = NULL;
  1719. if (!ppIpsecNegPolData) {
  1720. return;
  1721. }
  1722. for (i = 0; i < dwNumNegPolObjects; i++) {
  1723. pIpsecNegPolData = *(ppIpsecNegPolData + i);
  1724. if (pIpsecNegPolData) {
  1725. FreeIpsecNegPolData(pIpsecNegPolData);
  1726. }
  1727. }
  1728. FreePolMem(ppIpsecNegPolData);
  1729. return;
  1730. }
  1731. void
  1732. FreeMulIpsecPolicyData(
  1733. PIPSEC_POLICY_DATA * ppIpsecPolicyData,
  1734. DWORD dwNumPolicyObjects
  1735. )
  1736. {
  1737. DWORD i = 0;
  1738. PIPSEC_POLICY_DATA pIpsecPolicyData = NULL;
  1739. if (!ppIpsecPolicyData) {
  1740. return;
  1741. }
  1742. for (i = 0; i < dwNumPolicyObjects; i++) {
  1743. pIpsecPolicyData = *(ppIpsecPolicyData + i);
  1744. if (pIpsecPolicyData) {
  1745. FreeIpsecPolicyData(pIpsecPolicyData);
  1746. }
  1747. }
  1748. FreePolMem(ppIpsecPolicyData);
  1749. return;
  1750. }
  1751. void
  1752. FreeMulIpsecNFAData(
  1753. PIPSEC_NFA_DATA * ppIpsecNFAData,
  1754. DWORD dwNumNFAObjects
  1755. )
  1756. {
  1757. DWORD i = 0;
  1758. PIPSEC_NFA_DATA pIpsecNFAData = NULL;
  1759. if (!ppIpsecNFAData) {
  1760. return;
  1761. }
  1762. for (i = 0; i < dwNumNFAObjects; i++) {
  1763. pIpsecNFAData = *(ppIpsecNFAData + i);
  1764. if (pIpsecNFAData) {
  1765. FreeIpsecNFAData(pIpsecNFAData);
  1766. }
  1767. }
  1768. FreePolMem(ppIpsecNFAData);
  1769. return;
  1770. }
  1771. DWORD
  1772. GenGUIDFromRegFilterReference(
  1773. LPWSTR pszIpsecFilterReference,
  1774. GUID * FilterIdentifier
  1775. )
  1776. {
  1777. DWORD dwError = 0;
  1778. LPWSTR pszGuid = NULL;
  1779. if (pszIpsecFilterReference) {
  1780. pszGuid = wcschr(pszIpsecFilterReference, L'{');
  1781. if (!pszGuid) {
  1782. dwError = ERROR_INVALID_DATA;
  1783. return (dwError);
  1784. }
  1785. wGUIDFromString(pszGuid, FilterIdentifier);
  1786. }else {
  1787. memset(FilterIdentifier, 0, sizeof(GUID));
  1788. }
  1789. return(dwError);
  1790. }
  1791. DWORD
  1792. GenGUIDFromRegNegPolReference(
  1793. LPWSTR pszIpsecNegPolReference,
  1794. GUID * NegPolIdentifier
  1795. )
  1796. {
  1797. DWORD dwError = 0;
  1798. LPWSTR pszGuid = NULL;
  1799. if (pszIpsecNegPolReference) {
  1800. pszGuid = wcschr(pszIpsecNegPolReference, L'{');
  1801. if (!pszGuid) {
  1802. dwError = ERROR_INVALID_DATA;
  1803. return (dwError);
  1804. }
  1805. wGUIDFromString(pszGuid, NegPolIdentifier);
  1806. }else {
  1807. memset(NegPolIdentifier, 0, sizeof(GUID));
  1808. }
  1809. return(dwError);
  1810. }
  1811. DWORD
  1812. GenGUIDFromRegISAKMPReference(
  1813. LPWSTR pszIpsecISAKMPReference,
  1814. GUID * ISAKMPIdentifier
  1815. )
  1816. {
  1817. DWORD dwError = 0;
  1818. LPWSTR pszGuid = NULL;
  1819. if (pszIpsecISAKMPReference) {
  1820. pszGuid = wcschr(pszIpsecISAKMPReference, L'{');
  1821. if (!pszGuid) {
  1822. dwError = ERROR_INVALID_DATA;
  1823. return (dwError);
  1824. }
  1825. wGUIDFromString(pszGuid, ISAKMPIdentifier);
  1826. }else {
  1827. memset(ISAKMPIdentifier, 0, sizeof(GUID));
  1828. }
  1829. return(dwError);
  1830. }
  1831. void
  1832. FreeIpsecFilterSpecs(
  1833. PIPSEC_FILTER_SPEC * ppIpsecFilterSpecs,
  1834. DWORD dwNumFilterSpecs
  1835. )
  1836. {
  1837. DWORD i = 0;
  1838. PIPSEC_FILTER_SPEC pIpsecFilterSpec = NULL;
  1839. for (i = 0; i < dwNumFilterSpecs; i++) {
  1840. pIpsecFilterSpec = *(ppIpsecFilterSpecs + i);
  1841. if (pIpsecFilterSpec) {
  1842. FreeIpsecFilterSpec(pIpsecFilterSpec);
  1843. }
  1844. }
  1845. FreePolMem(ppIpsecFilterSpecs);
  1846. }
  1847. void
  1848. FreeIpsecFilterSpec(
  1849. PIPSEC_FILTER_SPEC pIpsecFilterSpec
  1850. )
  1851. {
  1852. if (pIpsecFilterSpec->pszSrcDNSName){
  1853. FreePolStr(pIpsecFilterSpec->pszSrcDNSName);
  1854. }
  1855. if (pIpsecFilterSpec->pszDestDNSName){
  1856. FreePolStr(pIpsecFilterSpec->pszDestDNSName);
  1857. }
  1858. if (pIpsecFilterSpec->pszDescription){
  1859. FreePolStr(pIpsecFilterSpec->pszDescription);
  1860. }
  1861. FreePolMem(pIpsecFilterSpec);
  1862. return;
  1863. }
  1864. void
  1865. FreeMulIpsecISAKMPData(
  1866. PIPSEC_ISAKMP_DATA * ppIpsecISAKMPData,
  1867. DWORD dwNumISAKMPObjects
  1868. )
  1869. {
  1870. DWORD i = 0;
  1871. PIPSEC_ISAKMP_DATA pIpsecISAKMPData = NULL;
  1872. if (!ppIpsecISAKMPData) {
  1873. return;
  1874. }
  1875. for (i = 0; i < dwNumISAKMPObjects; i++) {
  1876. pIpsecISAKMPData = *(ppIpsecISAKMPData + i);
  1877. if (pIpsecISAKMPData) {
  1878. FreeIpsecISAKMPData(pIpsecISAKMPData);
  1879. }
  1880. }
  1881. FreePolMem(ppIpsecISAKMPData);
  1882. return;
  1883. }