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.

1051 lines
37 KiB

  1. #include "ipseccmd.h"
  2. #ifdef __cplusplus
  3. extern "C" {
  4. #endif
  5. IPSECPolicyToStorage::IPSECPolicyToStorage():
  6. myPolicyStorage(NULL),
  7. myIPSECPolicy(NULL),
  8. mybIsOpen(false),
  9. mybPolicyExists(false)
  10. {
  11. }
  12. IPSECPolicyToStorage::~IPSECPolicyToStorage()
  13. {
  14. // if (myIPSECPolicy) IPSecFreePolicyData(myIPSECPolicy);
  15. // polstore AVs if something inside the policy is missing
  16. if (myPolicyStorage)
  17. {
  18. IPSecClosePolicyStore(myPolicyStorage);
  19. }
  20. }
  21. // this small function will attempt to CreateIpsecPolicyData, and if it succeeds, it'll mark that object is created in the storage
  22. // so that next time we'll use Set routine, not Create
  23. void
  24. IPSECPolicyToStorage::TryToCreatePolicy()
  25. {
  26. if (IPSecCreatePolicyData(myPolicyStorage, myIPSECPolicy) == ERROR_SUCCESS)
  27. {
  28. mybPolicyExists = true;
  29. }
  30. }
  31. // opens storage an initializes policy
  32. // pass name as NULL for local storage or if you want
  33. // the DS to be located
  34. // szPolicyName is required
  35. // szDescription is optional
  36. HRESULT
  37. IPSECPolicyToStorage::Open(
  38. IN DWORD location,
  39. IN LPTSTR name,
  40. IN LPTSTR szPolicyName,
  41. IN LPTSTR szDescription,
  42. IN time_t tPollingInterval,
  43. IN bool bUseExisting)
  44. {
  45. HRESULT hrReturnCode = S_OK;
  46. RPC_STATUS RpcStat;
  47. // need to do error checking
  48. if (!szPolicyName || (szPolicyName[0] == TEXT('\0')) )
  49. {
  50. hrReturnCode = P2STORE_MISSING_NAME;
  51. }
  52. else
  53. {
  54. if (myPolicyStorage)
  55. {
  56. IPSecClosePolicyStore(myPolicyStorage);
  57. myPolicyStorage = NULL;
  58. }
  59. hrReturnCode = IPSecOpenPolicyStore(name, location, NULL, &myPolicyStorage);
  60. if (hrReturnCode == ERROR_SUCCESS)
  61. {
  62. mybIsOpen = true;
  63. if (myIPSECPolicy)
  64. {
  65. IPSecFreePolicyData(myIPSECPolicy);
  66. myIPSECPolicy = NULL;
  67. }
  68. if (bUseExisting)
  69. {
  70. // try to find existing policy cause user requested us to
  71. // myIPSECPolicy will be NULL if we haven't found it
  72. PIPSEC_POLICY_DATA *ppPolicyEnum = NULL;
  73. DWORD dwNumPolicies = 0;
  74. hrReturnCode = IPSecEnumPolicyData(myPolicyStorage, &ppPolicyEnum, &dwNumPolicies);
  75. if (hrReturnCode == ERROR_SUCCESS && dwNumPolicies > 0 && ppPolicyEnum != NULL)
  76. {
  77. // we have something in the storage, let's go through it
  78. int i;
  79. for (i = 0; i < (int) dwNumPolicies; i++)
  80. {
  81. if (wcscmp(ppPolicyEnum[i]->pszIpsecName, szPolicyName) == 0)
  82. {
  83. // we found it
  84. // make a copy in myIPSECPolicy and update description and polling interval
  85. hrReturnCode = IPSecCopyPolicyData(ppPolicyEnum[i], &myIPSECPolicy);
  86. mybPolicyExists = true;
  87. if (hrReturnCode == ERROR_SUCCESS)
  88. {
  89. hrReturnCode = IPSecEnumNFAData(myPolicyStorage, myIPSECPolicy->PolicyIdentifier
  90. , &(myIPSECPolicy->ppIpsecNFAData), &(myIPSECPolicy->dwNumNFACount));
  91. }
  92. if (hrReturnCode == ERROR_SUCCESS)
  93. {
  94. int i;
  95. // also get other parts of the policy, no error checks here
  96. IPSecGetISAKMPData(myPolicyStorage, myIPSECPolicy->ISAKMPIdentifier, &(myIPSECPolicy->pIpsecISAKMPData));
  97. for (i = 0; i < (int) myIPSECPolicy->dwNumNFACount; i++)
  98. {
  99. if (!UuidIsNil(&(myIPSECPolicy->ppIpsecNFAData[i]->NegPolIdentifier), &RpcStat))
  100. {
  101. IPSecGetNegPolData(myPolicyStorage,
  102. myIPSECPolicy->ppIpsecNFAData[i]->NegPolIdentifier,
  103. &(myIPSECPolicy->ppIpsecNFAData[i]->pIpsecNegPolData));
  104. }
  105. if (!UuidIsNil(&(myIPSECPolicy->ppIpsecNFAData[i]->FilterIdentifier), &RpcStat))
  106. {
  107. IPSecGetFilterData(myPolicyStorage,
  108. myIPSECPolicy->ppIpsecNFAData[i]->FilterIdentifier,
  109. &(myIPSECPolicy->ppIpsecNFAData[i]->pIpsecFilterData));
  110. }
  111. }
  112. if (myIPSECPolicy->pszDescription == NULL && szDescription != NULL)
  113. {
  114. // we've got description to set
  115. myIPSECPolicy->pszDescription = IPSecAllocPolStr(szDescription);
  116. }
  117. // set Polling Interval
  118. if (tPollingInterval >= 0)
  119. {
  120. myIPSECPolicy->dwPollingInterval = (DWORD) tPollingInterval;
  121. }
  122. // commit
  123. hrReturnCode = IPSecSetPolicyData(myPolicyStorage, myIPSECPolicy);
  124. }
  125. }
  126. }
  127. }
  128. else
  129. {
  130. hrReturnCode = ERROR_SUCCESS;
  131. }
  132. // clean it up
  133. if (dwNumPolicies > 0 && ppPolicyEnum != NULL)
  134. {
  135. IPSecFreeMulPolicyData(ppPolicyEnum, dwNumPolicies);
  136. }
  137. }
  138. // this is executed only when no bUseExisting was specified or existing policy wasn't found
  139. if (!bUseExisting || myIPSECPolicy == NULL)
  140. {
  141. // create
  142. mybPolicyExists = false;
  143. myIPSECPolicy = (PIPSEC_POLICY_DATA) IPSecAllocPolMem(sizeof(IPSEC_POLICY_DATA));
  144. if (myIPSECPolicy == NULL)
  145. {
  146. hrReturnCode = GetLastError();
  147. }
  148. else
  149. {
  150. myIPSECPolicy->pszIpsecName = IPSecAllocPolStr(szPolicyName);
  151. if (szDescription)
  152. {
  153. myIPSECPolicy->pszDescription = IPSecAllocPolStr(szDescription);
  154. }
  155. else
  156. {
  157. myIPSECPolicy->pszDescription = NULL;
  158. }
  159. myIPSECPolicy->dwPollingInterval = (DWORD) tPollingInterval;
  160. RpcStat = UuidCreate(&(myIPSECPolicy->PolicyIdentifier));
  161. assert(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY);
  162. // now init other stuff somehow
  163. myIPSECPolicy->pIpsecISAKMPData = NULL;
  164. myIPSECPolicy->ppIpsecNFAData = NULL;
  165. myIPSECPolicy->dwNumNFACount = 0;
  166. myIPSECPolicy->dwWhenChanged = 0;
  167. UuidCreateNil(&(myIPSECPolicy->ISAKMPIdentifier));
  168. // TryToCreatePolicy();
  169. }
  170. }
  171. }
  172. }
  173. return hrReturnCode;
  174. }
  175. HRESULT
  176. IPSECPolicyToStorage::AddRule(
  177. IN IPSEC_IKE_POLICY IpsecIkePol,
  178. PSTORAGE_INFO pStorageInfo)
  179. {
  180. HRESULT hrReturn = S_OK;
  181. PIPSEC_NFA_DATA pRule = MakeRule(IpsecIkePol, pStorageInfo);
  182. int i;
  183. if (!IsOpen())
  184. {
  185. return ERROR_ACCESS_DENIED;
  186. }
  187. // form policy data structures
  188. myIPSECPolicy->dwNumNFACount++;
  189. myIPSECPolicy->ppIpsecNFAData = (PIPSEC_NFA_DATA *) ReallocPolMem(myIPSECPolicy->ppIpsecNFAData
  190. , (myIPSECPolicy->dwNumNFACount-1)*sizeof(PIPSEC_NFA_DATA)
  191. , (myIPSECPolicy->dwNumNFACount)*sizeof(PIPSEC_NFA_DATA));
  192. myIPSECPolicy->ppIpsecNFAData[myIPSECPolicy->dwNumNFACount-1] = pRule;
  193. // commit everything
  194. // start from qm policy
  195. if (!pRule->pIpsecFilterData)
  196. {
  197. // fix up neg pol
  198. pRule->pIpsecNegPolData->NegPolType = GUID_NEGOTIATION_TYPE_DEFAULT;
  199. }
  200. hrReturn = IPSecCreateNegPolData(myPolicyStorage, pRule->pIpsecNegPolData);
  201. if (hrReturn != ERROR_SUCCESS)
  202. {
  203. return hrReturn;
  204. }
  205. // filter
  206. if (pRule->pIpsecFilterData)
  207. {
  208. hrReturn = IPSecCreateFilterData(myPolicyStorage, pRule->pIpsecFilterData);
  209. if (hrReturn != ERROR_SUCCESS)
  210. {
  211. return hrReturn;
  212. }
  213. }
  214. // NFA
  215. IPSecCreateNFAData(myPolicyStorage, myIPSECPolicy->PolicyIdentifier, pRule);
  216. // policy
  217. if (IsPolicyInStorage())
  218. {
  219. IPSecSetPolicyData(myPolicyStorage, myIPSECPolicy);
  220. }
  221. else
  222. {
  223. TryToCreatePolicy();
  224. }
  225. return hrReturn;
  226. }
  227. // Add default response rule to the policy
  228. HRESULT IPSECPolicyToStorage::AddDefaultResponseRule ( )
  229. {
  230. HRESULT hrReturn = S_OK;
  231. PIPSEC_NFA_DATA pRule = MakeDefaultResponseRule();
  232. int i;
  233. if (!IsOpen())
  234. {
  235. return ERROR_ACCESS_DENIED;
  236. }
  237. // form policy data structures
  238. myIPSECPolicy->dwNumNFACount++;
  239. myIPSECPolicy->ppIpsecNFAData = (PIPSEC_NFA_DATA *) ReallocPolMem(myIPSECPolicy->ppIpsecNFAData
  240. , (myIPSECPolicy->dwNumNFACount-1)*sizeof(PIPSEC_NFA_DATA)
  241. , (myIPSECPolicy->dwNumNFACount)*sizeof(PIPSEC_NFA_DATA));
  242. myIPSECPolicy->ppIpsecNFAData[myIPSECPolicy->dwNumNFACount-1] = pRule;
  243. // commit everything
  244. // start from qm policy
  245. // fix up neg pol
  246. pRule->pIpsecNegPolData->NegPolType = GUID_NEGOTIATION_TYPE_DEFAULT;
  247. hrReturn = IPSecCreateNegPolData(myPolicyStorage, pRule->pIpsecNegPolData);
  248. if (hrReturn != ERROR_SUCCESS)
  249. {
  250. return hrReturn;
  251. }
  252. // NFA
  253. IPSecCreateNFAData(myPolicyStorage, myIPSECPolicy->PolicyIdentifier, pRule);
  254. // policy
  255. if (IsPolicyInStorage())
  256. {
  257. IPSecSetPolicyData(myPolicyStorage, myIPSECPolicy);
  258. }
  259. else
  260. {
  261. TryToCreatePolicy();
  262. }
  263. return hrReturn;
  264. }
  265. // this will update a rule:
  266. // if there are filters, all the filters from the current NFA
  267. // are removed and the ones passed in are added
  268. // same thing goes for negotiation policies and authinfos
  269. HRESULT
  270. IPSECPolicyToStorage::UpdateRule(
  271. IN PIPSEC_NFA_DATA pRule,
  272. IN IPSEC_IKE_POLICY IpsecIkePol,
  273. IN PSTORAGE_INFO pStorageInfo)
  274. {
  275. RPC_STATUS RpcStat;
  276. T2P_FILTER *pFilterList;
  277. IF_TYPE Interface;
  278. WCHAR wszRuleName[POTF_MAX_STRLEN];
  279. HRESULT hrReturn = S_OK;
  280. int i;
  281. GUID oldFilter, oldNegPol;
  282. if (pRule == NULL || !IsOpen())
  283. {
  284. return ERROR_ACCESS_DENIED;
  285. }
  286. // first, remove filter and policy data
  287. oldFilter = pRule->FilterIdentifier;
  288. if (pRule->pIpsecFilterData)
  289. {
  290. IPSecFreeFilterData(pRule->pIpsecFilterData);
  291. }
  292. oldNegPol = pRule->NegPolIdentifier;
  293. if (pRule->pIpsecNegPolData)
  294. {
  295. IPSecFreeNegPolData(pRule->pIpsecNegPolData);
  296. }
  297. // free auth methods
  298. if (pRule->ppAuthMethods)
  299. {
  300. for (i = 0; i < (int) pRule->dwAuthMethodCount; i++)
  301. {
  302. if (pRule->ppAuthMethods[i]->pszAuthMethod)
  303. {
  304. IPSecFreePolMem(pRule->ppAuthMethods[i]->pszAuthMethod);
  305. }
  306. if (pRule->ppAuthMethods[i]->dwAltAuthLen && pRule->ppAuthMethods[i]->pAltAuthMethod)
  307. {
  308. IPSecFreePolMem(pRule->ppAuthMethods[i]->pAltAuthMethod);
  309. }
  310. IPSecFreePolMem(pRule->ppAuthMethods[i]);
  311. }
  312. }
  313. IPSecFreePolMem(pRule->ppAuthMethods);
  314. // free strings
  315. if (pRule->pszIpsecName)
  316. {
  317. IPSecFreePolStr(pRule->pszIpsecName);
  318. }
  319. if (pRule->pszInterfaceName)
  320. {
  321. IPSecFreePolStr(pRule->pszInterfaceName);
  322. }
  323. if (pRule->pszEndPointName)
  324. {
  325. IPSecFreePolStr(pRule->pszEndPointName);
  326. }
  327. if (pRule->pszDescription)
  328. {
  329. IPSecFreePolStr(pRule->pszDescription);
  330. }
  331. // now make a rule
  332. pRule->pszIpsecName = pRule->pszDescription = pRule->pszInterfaceName = pRule->pszEndPointName = NULL;
  333. if (pStorageInfo)
  334. {
  335. pRule->pszIpsecName = IPSecAllocPolStr(pStorageInfo->szRuleName);
  336. }
  337. pRule->dwWhenChanged = 0;
  338. // filters
  339. if (IpsecIkePol.QMFilterType == QM_TRANSPORT_FILTER
  340. && IpsecIkePol.pTransportFilters[0].SrcAddr.AddrType == IP_ADDR_UNIQUE
  341. && IpsecIkePol.pTransportFilters[0].SrcAddr.uIpAddr == IP_ADDRESS_ME
  342. && IpsecIkePol.pTransportFilters[0].DesAddr.AddrType == IP_ADDR_UNIQUE
  343. && IpsecIkePol.pTransportFilters[0].DesAddr.uIpAddr == IP_ADDRESS_ME
  344. && IpsecIkePol.pTransportFilters[0].InboundFilterFlag == (FILTER_FLAG) POTF_DEFAULT_RESPONSE_FLAG
  345. && IpsecIkePol.pTransportFilters[0].OutboundFilterFlag == (FILTER_FLAG) POTF_DEFAULT_RESPONSE_FLAG)
  346. {
  347. pRule->pIpsecFilterData = NULL;
  348. UuidCreateNil(&(pRule->FilterIdentifier));
  349. }
  350. else
  351. {
  352. pFilterList = new T2P_FILTER[IpsecIkePol.dwNumFilters];
  353. for (i = 0; i < (int) IpsecIkePol.dwNumFilters; i++)
  354. {
  355. if (IpsecIkePol.QMFilterType == QM_TRANSPORT_FILTER)
  356. {
  357. pFilterList[i].QMFilterType = QM_TRANSPORT_FILTER;
  358. pFilterList[i].TransportFilter = IpsecIkePol.pTransportFilters[i];
  359. }
  360. else
  361. {
  362. // tunnel
  363. pFilterList[i].QMFilterType = QM_TUNNEL_FILTER;
  364. pFilterList[i].TunnelFilter = IpsecIkePol.pTunnelFilters[i];
  365. }
  366. }
  367. if (pStorageInfo)
  368. {
  369. pRule->pIpsecFilterData = MakeFilters(pFilterList, IpsecIkePol.dwNumFilters, pStorageInfo->szRuleName);
  370. }
  371. else
  372. {
  373. pRule->pIpsecFilterData = MakeFilters(pFilterList, IpsecIkePol.dwNumFilters, L"");
  374. }
  375. pRule->FilterIdentifier = pRule->pIpsecFilterData->FilterIdentifier;
  376. delete[] pFilterList;
  377. }
  378. // filter action
  379. if (pStorageInfo)
  380. {
  381. pRule->pIpsecNegPolData = MakeNegotiationPolicy(IpsecIkePol.IpsPol, pStorageInfo->szRuleName);
  382. }
  383. else
  384. {
  385. pRule->pIpsecNegPolData = MakeNegotiationPolicy(IpsecIkePol.IpsPol, L"");
  386. }
  387. pRule->NegPolIdentifier = pRule->pIpsecNegPolData->NegPolIdentifier;
  388. if (pStorageInfo && pStorageInfo->guidNegPolAction != GUID_NEGOTIATION_ACTION_NORMAL_IPSEC)
  389. {
  390. pRule->pIpsecNegPolData->NegPolAction = pStorageInfo->guidNegPolAction;
  391. }
  392. // tunnel address
  393. pRule->dwTunnelFlags = 0;
  394. if (IpsecIkePol.QMFilterType == QM_TUNNEL_FILTER)
  395. {
  396. pRule->dwTunnelFlags = 1;
  397. pRule->dwTunnelIpAddr = IpsecIkePol.pTunnelFilters[0].DesTunnelAddr.uIpAddr;
  398. }
  399. // interface type
  400. Interface = (IpsecIkePol.QMFilterType == QM_TRANSPORT_FILTER) ? IpsecIkePol.pTransportFilters[0].InterfaceType : IpsecIkePol.pTunnelFilters[0].InterfaceType;
  401. if (Interface == INTERFACE_TYPE_ALL)
  402. {
  403. pRule->dwInterfaceType = PAS_INTERFACE_TYPE_ALL;
  404. }
  405. else if (Interface == INTERFACE_TYPE_LAN)
  406. {
  407. pRule->dwInterfaceType = PAS_INTERFACE_TYPE_LAN;
  408. }
  409. else if (Interface == INTERFACE_TYPE_DIALUP)
  410. {
  411. pRule->dwInterfaceType = PAS_INTERFACE_TYPE_DIALUP;
  412. }
  413. else
  414. {
  415. pRule->dwInterfaceType = PAS_INTERFACE_TYPE_NONE;
  416. }
  417. // active flag
  418. pRule->dwActiveFlag = TRUE;
  419. // auth methods
  420. pRule->dwAuthMethodCount = IpsecIkePol.AuthInfos.dwNumAuthInfos;
  421. pRule->ppAuthMethods = (PIPSEC_AUTH_METHOD *) IPSecAllocPolMem(pRule->dwAuthMethodCount * sizeof(PIPSEC_AUTH_METHOD));
  422. for (i = 0; i < (int) pRule->dwAuthMethodCount; i++)
  423. {
  424. pRule->ppAuthMethods[i] = (PIPSEC_AUTH_METHOD) IPSecAllocPolMem(sizeof(IPSEC_AUTH_METHOD));
  425. pRule->ppAuthMethods[i]->dwAuthType = IpsecIkePol.AuthInfos.pAuthenticationInfo[i].AuthMethod;
  426. if (pRule->ppAuthMethods[i]->dwAuthType == IKE_SSPI)
  427. {
  428. pRule->ppAuthMethods[i]->dwAuthLen = 0;
  429. pRule->ppAuthMethods[i]->pszAuthMethod = NULL;
  430. pRule->ppAuthMethods[i]->dwAltAuthLen = 0;
  431. pRule->ppAuthMethods[i]->pAltAuthMethod = 0;
  432. }
  433. else if (pRule->ppAuthMethods[i]->dwAuthType == IKE_RSA_SIGNATURE)
  434. {
  435. LPTSTR pTemp = NULL;
  436. pRule->ppAuthMethods[i]->dwAltAuthLen = IpsecIkePol.AuthInfos.pAuthenticationInfo[i].dwAuthInfoSize;
  437. pRule->ppAuthMethods[i]->pAltAuthMethod = (PBYTE) IPSecAllocPolMem(IpsecIkePol.AuthInfos.pAuthenticationInfo[i].dwAuthInfoSize);
  438. memcpy(pRule->ppAuthMethods[i]->pAltAuthMethod, IpsecIkePol.AuthInfos.pAuthenticationInfo[i].pAuthInfo, IpsecIkePol.AuthInfos.pAuthenticationInfo[i].dwAuthInfoSize);
  439. hrReturn = CM_DecodeName(pRule->ppAuthMethods[i]->pAltAuthMethod, pRule->ppAuthMethods[i]->dwAltAuthLen, &pTemp);
  440. assert(hrReturn == ERROR_SUCCESS);
  441. pRule->ppAuthMethods[i]->pszAuthMethod = IPSecAllocPolStr(pTemp);
  442. pRule->ppAuthMethods[i]->dwAuthLen = wcslen(pRule->ppAuthMethods[i]->pszAuthMethod);
  443. delete[] pTemp;
  444. }
  445. else
  446. {
  447. pRule->ppAuthMethods[i]->dwAuthLen = IpsecIkePol.AuthInfos.pAuthenticationInfo[i].dwAuthInfoSize / sizeof(WCHAR);
  448. pRule->ppAuthMethods[i]->pszAuthMethod = (LPWSTR) IPSecAllocPolMem(IpsecIkePol.AuthInfos.pAuthenticationInfo[i].dwAuthInfoSize + sizeof(WCHAR));
  449. memcpy(pRule->ppAuthMethods[i]->pszAuthMethod, IpsecIkePol.AuthInfos.pAuthenticationInfo[i].pAuthInfo, IpsecIkePol.AuthInfos.pAuthenticationInfo[i].dwAuthInfoSize);
  450. // add trailing '\0' - this is requirement for the polstore
  451. pRule->ppAuthMethods[i]->pszAuthMethod[pRule->ppAuthMethods[i]->dwAuthLen] = 0;
  452. pRule->ppAuthMethods[i]->dwAltAuthLen = 0;
  453. pRule->ppAuthMethods[i]->pAltAuthMethod = 0;
  454. }
  455. }
  456. // plumb it
  457. if (!pRule->pIpsecFilterData)
  458. {
  459. // fix up neg pol
  460. pRule->pIpsecNegPolData->NegPolType = GUID_NEGOTIATION_TYPE_DEFAULT;
  461. }
  462. hrReturn = IPSecCreateNegPolData(myPolicyStorage, pRule->pIpsecNegPolData);
  463. if (hrReturn != ERROR_SUCCESS)
  464. {
  465. return hrReturn;
  466. }
  467. // filter
  468. if (pRule->pIpsecFilterData)
  469. {
  470. hrReturn = IPSecCreateFilterData(myPolicyStorage, pRule->pIpsecFilterData);
  471. if (hrReturn != ERROR_SUCCESS)
  472. {
  473. return hrReturn;
  474. }
  475. }
  476. // NFA
  477. IPSecSetNFAData(myPolicyStorage, myIPSECPolicy->PolicyIdentifier, pRule);
  478. // policy
  479. if (IsPolicyInStorage())
  480. {
  481. IPSecSetPolicyData(myPolicyStorage, myIPSECPolicy);
  482. }
  483. else
  484. {
  485. TryToCreatePolicy();
  486. }
  487. if (!UuidIsNil(&oldFilter, &RpcStat))
  488. {
  489. IPSecDeleteFilterData(myPolicyStorage, oldFilter);
  490. }
  491. IPSecDeleteNegPolData(myPolicyStorage, oldNegPol);
  492. return hrReturn;
  493. }
  494. // forms a rule but doesn't commit it
  495. PIPSEC_NFA_DATA
  496. IPSECPolicyToStorage::MakeRule(IN IPSEC_IKE_POLICY IpsecIkePol, IN PSTORAGE_INFO pStorageInfo)
  497. {
  498. RPC_STATUS RpcStat;
  499. T2P_FILTER *pFilterList;
  500. IF_TYPE Interface;
  501. int i;
  502. WCHAR wszRuleName[POTF_MAX_STRLEN];
  503. PIPSEC_NFA_DATA pRule = (PIPSEC_NFA_DATA) IPSecAllocPolMem(sizeof(IPSEC_NFA_DATA));
  504. HRESULT hrReturn = S_OK;
  505. assert(pRule);
  506. pRule->pszIpsecName = pRule->pszDescription = pRule->pszInterfaceName = pRule->pszEndPointName = NULL;
  507. if (pStorageInfo)
  508. {
  509. pRule->pszIpsecName = IPSecAllocPolStr(pStorageInfo->szRuleName);
  510. }
  511. RpcStat = UuidCreate(&(pRule->NFAIdentifier));
  512. assert(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY);
  513. pRule->dwWhenChanged = 0;
  514. // filters
  515. if (IpsecIkePol.QMFilterType == QM_TRANSPORT_FILTER
  516. && IpsecIkePol.pTransportFilters[0].SrcAddr.AddrType == IP_ADDR_UNIQUE
  517. && IpsecIkePol.pTransportFilters[0].SrcAddr.uIpAddr == IP_ADDRESS_ME
  518. && IpsecIkePol.pTransportFilters[0].DesAddr.AddrType == IP_ADDR_UNIQUE
  519. && IpsecIkePol.pTransportFilters[0].DesAddr.uIpAddr == IP_ADDRESS_ME
  520. && IpsecIkePol.pTransportFilters[0].InboundFilterFlag == (FILTER_FLAG) POTF_DEFAULT_RESPONSE_FLAG
  521. && IpsecIkePol.pTransportFilters[0].OutboundFilterFlag == (FILTER_FLAG) POTF_DEFAULT_RESPONSE_FLAG)
  522. {
  523. pRule->pIpsecFilterData = NULL;
  524. UuidCreateNil(&(pRule->FilterIdentifier));
  525. }
  526. else
  527. {
  528. pFilterList = new T2P_FILTER[IpsecIkePol.dwNumFilters];
  529. for (i = 0; i < (int) IpsecIkePol.dwNumFilters; i++)
  530. {
  531. if (IpsecIkePol.QMFilterType == QM_TRANSPORT_FILTER)
  532. {
  533. pFilterList[i].QMFilterType = QM_TRANSPORT_FILTER;
  534. pFilterList[i].TransportFilter = IpsecIkePol.pTransportFilters[i];
  535. }
  536. else
  537. {
  538. // tunnel
  539. pFilterList[i].QMFilterType = QM_TUNNEL_FILTER;
  540. pFilterList[i].TunnelFilter = IpsecIkePol.pTunnelFilters[i];
  541. }
  542. }
  543. if (pStorageInfo)
  544. {
  545. pRule->pIpsecFilterData = MakeFilters(pFilterList, IpsecIkePol.dwNumFilters, pStorageInfo->szRuleName);
  546. }
  547. else
  548. {
  549. pRule->pIpsecFilterData = MakeFilters(pFilterList, IpsecIkePol.dwNumFilters, L"");
  550. }
  551. pRule->FilterIdentifier = pRule->pIpsecFilterData->FilterIdentifier;
  552. delete[] pFilterList;
  553. }
  554. // filter action
  555. if (pStorageInfo)
  556. {
  557. pRule->pIpsecNegPolData = MakeNegotiationPolicy(IpsecIkePol.IpsPol, pStorageInfo->szRuleName);
  558. }
  559. else
  560. {
  561. pRule->pIpsecNegPolData = MakeNegotiationPolicy(IpsecIkePol.IpsPol, L"");
  562. }
  563. pRule->NegPolIdentifier = pRule->pIpsecNegPolData->NegPolIdentifier;
  564. if (pStorageInfo && pStorageInfo->guidNegPolAction != GUID_NEGOTIATION_ACTION_NORMAL_IPSEC)
  565. {
  566. pRule->pIpsecNegPolData->NegPolAction = pStorageInfo->guidNegPolAction;
  567. }
  568. // tunnel address
  569. pRule->dwTunnelFlags = 0;
  570. if (IpsecIkePol.QMFilterType == QM_TUNNEL_FILTER)
  571. {
  572. pRule->dwTunnelFlags = 1;
  573. pRule->dwTunnelIpAddr = IpsecIkePol.pTunnelFilters[0].DesTunnelAddr.uIpAddr;
  574. }
  575. // interface type
  576. Interface = (IpsecIkePol.QMFilterType == QM_TRANSPORT_FILTER) ? IpsecIkePol.pTransportFilters[0].InterfaceType : IpsecIkePol.pTunnelFilters[0].InterfaceType;
  577. if (Interface == INTERFACE_TYPE_ALL)
  578. {
  579. pRule->dwInterfaceType = PAS_INTERFACE_TYPE_ALL;
  580. }
  581. else if (Interface == INTERFACE_TYPE_LAN)
  582. {
  583. pRule->dwInterfaceType = PAS_INTERFACE_TYPE_LAN;
  584. }
  585. else if (Interface == INTERFACE_TYPE_DIALUP)
  586. {
  587. pRule->dwInterfaceType = PAS_INTERFACE_TYPE_DIALUP;
  588. }
  589. else
  590. {
  591. pRule->dwInterfaceType = PAS_INTERFACE_TYPE_NONE;
  592. }
  593. // active flag
  594. pRule->dwActiveFlag = TRUE;
  595. // auth methods
  596. pRule->dwAuthMethodCount = IpsecIkePol.AuthInfos.dwNumAuthInfos;
  597. pRule->ppAuthMethods = (PIPSEC_AUTH_METHOD *) IPSecAllocPolMem(pRule->dwAuthMethodCount * sizeof(PIPSEC_AUTH_METHOD));
  598. for (i = 0; i < (int) pRule->dwAuthMethodCount; i++)
  599. {
  600. pRule->ppAuthMethods[i] = (PIPSEC_AUTH_METHOD) IPSecAllocPolMem(sizeof(IPSEC_AUTH_METHOD));
  601. pRule->ppAuthMethods[i]->dwAuthType = IpsecIkePol.AuthInfos.pAuthenticationInfo[i].AuthMethod;
  602. if (pRule->ppAuthMethods[i]->dwAuthType == IKE_SSPI)
  603. {
  604. pRule->ppAuthMethods[i]->dwAuthLen = 0;
  605. pRule->ppAuthMethods[i]->pszAuthMethod = NULL;
  606. pRule->ppAuthMethods[i]->dwAltAuthLen = 0;
  607. pRule->ppAuthMethods[i]->pAltAuthMethod = 0;
  608. }
  609. else if (pRule->ppAuthMethods[i]->dwAuthType == IKE_RSA_SIGNATURE)
  610. {
  611. LPTSTR pTemp = NULL;
  612. pRule->ppAuthMethods[i]->dwAltAuthLen = IpsecIkePol.AuthInfos.pAuthenticationInfo[i].dwAuthInfoSize;
  613. pRule->ppAuthMethods[i]->pAltAuthMethod = (PBYTE) IPSecAllocPolMem(IpsecIkePol.AuthInfos.pAuthenticationInfo[i].dwAuthInfoSize);
  614. memcpy(pRule->ppAuthMethods[i]->pAltAuthMethod, IpsecIkePol.AuthInfos.pAuthenticationInfo[i].pAuthInfo, IpsecIkePol.AuthInfos.pAuthenticationInfo[i].dwAuthInfoSize);
  615. hrReturn = CM_DecodeName(pRule->ppAuthMethods[i]->pAltAuthMethod, pRule->ppAuthMethods[i]->dwAltAuthLen, &pTemp);
  616. assert(hrReturn == ERROR_SUCCESS);
  617. pRule->ppAuthMethods[i]->pszAuthMethod = IPSecAllocPolStr(pTemp);
  618. pRule->ppAuthMethods[i]->dwAuthLen = wcslen(pRule->ppAuthMethods[i]->pszAuthMethod);
  619. delete[] pTemp;
  620. }
  621. else
  622. {
  623. pRule->ppAuthMethods[i]->dwAuthLen = IpsecIkePol.AuthInfos.pAuthenticationInfo[i].dwAuthInfoSize / sizeof(WCHAR);
  624. pRule->ppAuthMethods[i]->pszAuthMethod = (LPWSTR) IPSecAllocPolMem(IpsecIkePol.AuthInfos.pAuthenticationInfo[i].dwAuthInfoSize + sizeof(WCHAR));
  625. memcpy(pRule->ppAuthMethods[i]->pszAuthMethod, IpsecIkePol.AuthInfos.pAuthenticationInfo[i].pAuthInfo, IpsecIkePol.AuthInfos.pAuthenticationInfo[i].dwAuthInfoSize);
  626. // add trailing '\0' - this is requirement for the polstore
  627. pRule->ppAuthMethods[i]->pszAuthMethod[pRule->ppAuthMethods[i]->dwAuthLen] = 0;
  628. pRule->ppAuthMethods[i]->dwAltAuthLen = 0;
  629. pRule->ppAuthMethods[i]->pAltAuthMethod = 0;
  630. }
  631. }
  632. return pRule;
  633. }
  634. // does not commit it to the storage
  635. // it is assumed that action is NEGOTIATE_SECURITY. If that's not true, it'll be corrected in MakeRule call
  636. // Soft SA flag is handled here
  637. PIPSEC_NEGPOL_DATA
  638. IPSECPolicyToStorage::MakeNegotiationPolicy(IPSEC_QM_POLICY IpsPol,
  639. LPWSTR Name)
  640. {
  641. RPC_STATUS RpcStat;
  642. int i;
  643. PIPSEC_NEGPOL_DATA pNegPol = (PIPSEC_NEGPOL_DATA) IPSecAllocPolMem(sizeof(IPSEC_NEGPOL_DATA));
  644. BOOL bPFS = FALSE;
  645. WCHAR pFAName[POTF_MAX_STRLEN];
  646. RpcStat = UuidCreate(&(pNegPol->NegPolIdentifier));
  647. assert(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY);
  648. pNegPol->NegPolAction = GUID_NEGOTIATION_ACTION_NORMAL_IPSEC;
  649. pNegPol->NegPolType = GUID_NEGOTIATION_TYPE_STANDARD;
  650. pNegPol->dwSecurityMethodCount = IpsPol.dwOfferCount;
  651. if (IpsPol.dwFlags & IPSEC_QM_POLICY_ALLOW_SOFT)
  652. {
  653. // we need to add one more offer with no security algorithms
  654. pNegPol->dwSecurityMethodCount++;
  655. }
  656. // allocate sec.methods
  657. pNegPol->pIpsecSecurityMethods = (IPSEC_SECURITY_METHOD *) IPSecAllocPolMem(pNegPol->dwSecurityMethodCount * sizeof(IPSEC_SECURITY_METHOD));
  658. // fix up PFS, in storage it is the property of the filter action, not individual offer
  659. for (i = 0; i < (int) IpsPol.dwOfferCount; i++)
  660. {
  661. if (IpsPol.pOffers[i].bPFSRequired)
  662. {
  663. bPFS = TRUE;
  664. }
  665. }
  666. // handle sec.methods
  667. for (i = 0; i < (int) IpsPol.dwOfferCount; i++)
  668. {
  669. int j;
  670. pNegPol->pIpsecSecurityMethods[i].Lifetime.KeyExpirationBytes = IpsPol.pOffers[i].Lifetime.uKeyExpirationKBytes;
  671. pNegPol->pIpsecSecurityMethods[i].Lifetime.KeyExpirationTime = IpsPol.pOffers[i].Lifetime.uKeyExpirationTime;
  672. pNegPol->pIpsecSecurityMethods[i].Flags = 0;
  673. pNegPol->pIpsecSecurityMethods[i].PfsQMRequired = bPFS;
  674. pNegPol->pIpsecSecurityMethods[i].Count = IpsPol.pOffers[i].dwNumAlgos;
  675. for (j = 0; j < (int) pNegPol->pIpsecSecurityMethods[i].Count && j < QM_MAX_ALGOS; j++)
  676. {
  677. pNegPol->pIpsecSecurityMethods[i].Algos[j].algoIdentifier = IpsPol.pOffers[i].Algos[j].uAlgoIdentifier;
  678. pNegPol->pIpsecSecurityMethods[i].Algos[j].secondaryAlgoIdentifier = IpsPol.pOffers[i].Algos[j].uSecAlgoIdentifier;
  679. pNegPol->pIpsecSecurityMethods[i].Algos[j].algoKeylen = IpsPol.pOffers[i].Algos[j].uAlgoKeyLen;
  680. pNegPol->pIpsecSecurityMethods[i].Algos[j].algoRounds = IpsPol.pOffers[i].Algos[j].uAlgoRounds;
  681. switch (IpsPol.pOffers[i].Algos[j].Operation)
  682. {
  683. case AUTHENTICATION:
  684. pNegPol->pIpsecSecurityMethods[i].Algos[j].operation = Auth;
  685. break;
  686. case ENCRYPTION:
  687. pNegPol->pIpsecSecurityMethods[i].Algos[j].operation = Encrypt;
  688. break;
  689. default:
  690. pNegPol->pIpsecSecurityMethods[i].Algos[j].operation = None;
  691. }
  692. }
  693. }
  694. // add soft
  695. if (IpsPol.dwFlags & IPSEC_QM_POLICY_ALLOW_SOFT)
  696. {
  697. // set Count (and everything) to 0
  698. memset(&(pNegPol->pIpsecSecurityMethods[pNegPol->dwSecurityMethodCount - 1]), 0, sizeof(IPSEC_SECURITY_METHOD));
  699. }
  700. // name
  701. swprintf(pFAName, TEXT("%s filter action"), Name);
  702. pNegPol->pszIpsecName = IPSecAllocPolStr(pFAName);
  703. pNegPol->pszDescription = NULL;
  704. return pNegPol;
  705. }
  706. PIPSEC_FILTER_DATA
  707. IPSECPolicyToStorage::MakeFilters(
  708. T2P_FILTER *Filters,
  709. UINT NumFilters,
  710. LPWSTR Name)
  711. {
  712. RPC_STATUS RpcStat;
  713. PIPSEC_FILTER_DATA pFilter = (PIPSEC_FILTER_DATA) IPSecAllocPolMem(sizeof(IPSEC_FILTER_DATA));
  714. int i;
  715. WCHAR pFLName[POTF_MAX_STRLEN];
  716. RpcStat = UuidCreate(&(pFilter->FilterIdentifier));
  717. assert(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY);
  718. pFilter->dwNumFilterSpecs = NumFilters;
  719. pFilter->ppFilterSpecs = (PIPSEC_FILTER_SPEC *) IPSecAllocPolMem(NumFilters*sizeof(PIPSEC_FILTER_SPEC));
  720. assert(pFilter->ppFilterSpecs);
  721. for (i = 0; i < (int) NumFilters; i++)
  722. {
  723. pFilter->ppFilterSpecs[i] = (PIPSEC_FILTER_SPEC) IPSecAllocPolMem(sizeof(IPSEC_FILTER_SPEC));
  724. assert(pFilter->ppFilterSpecs[i]);
  725. ConvertFilter(Filters[i], *(pFilter->ppFilterSpecs[i]));
  726. }
  727. pFilter->dwWhenChanged = 0;
  728. swprintf(pFLName, TEXT("%s filter list"), Name);
  729. pFilter->pszIpsecName = IPSecAllocPolStr(pFLName);
  730. pFilter->pszDescription = NULL;
  731. return pFilter;
  732. }
  733. HRESULT
  734. IPSECPolicyToStorage::SetISAKMPPolicy(IPSEC_MM_POLICY IkePol)
  735. {
  736. HRESULT hrReturn = S_OK;
  737. RPC_STATUS RpcStat;
  738. GUID oldISAKMP;
  739. int i;
  740. if (!IsOpen())
  741. {
  742. return ERROR_ACCESS_DENIED;
  743. }
  744. oldISAKMP = myIPSECPolicy->ISAKMPIdentifier;
  745. if (myIPSECPolicy->pIpsecISAKMPData)
  746. {
  747. IPSecFreeISAKMPData(myIPSECPolicy->pIpsecISAKMPData);
  748. }
  749. RpcStat = UuidCreate(&(myIPSECPolicy->ISAKMPIdentifier));
  750. assert(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY);
  751. myIPSECPolicy->pIpsecISAKMPData = (PIPSEC_ISAKMP_DATA) IPSecAllocPolMem(sizeof(IPSEC_ISAKMP_DATA));
  752. myIPSECPolicy->pIpsecISAKMPData->ISAKMPIdentifier = myIPSECPolicy->ISAKMPIdentifier;
  753. myIPSECPolicy->pIpsecISAKMPData->dwWhenChanged = 0;
  754. // sec methods stuff
  755. myIPSECPolicy->pIpsecISAKMPData->dwNumISAKMPSecurityMethods = IkePol.dwOfferCount;
  756. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods = (PCRYPTO_BUNDLE) IPSecAllocPolMem(sizeof(CRYPTO_BUNDLE)*IkePol.dwOfferCount);
  757. for (i = 0; i < (int) IkePol.dwOfferCount; i++)
  758. {
  759. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].MajorVersion = 0;
  760. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].MinorVersion = 0;
  761. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].AuthenticationMethod = 0;
  762. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].PseudoRandomFunction.AlgorithmIdentifier = 0;
  763. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].PseudoRandomFunction.KeySize = 0;
  764. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].PseudoRandomFunction.Rounds = 0;
  765. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].PfsIdentityRequired = (IkePol.pOffers[i].dwQuickModeLimit == 1);
  766. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].EncryptionAlgorithm.AlgorithmIdentifier = IkePol.pOffers[i].EncryptionAlgorithm.uAlgoIdentifier;
  767. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].EncryptionAlgorithm.KeySize = IkePol.pOffers[i].EncryptionAlgorithm.uAlgoKeyLen;
  768. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].EncryptionAlgorithm.Rounds = IkePol.pOffers[i].EncryptionAlgorithm.uAlgoRounds;
  769. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].HashAlgorithm.AlgorithmIdentifier = IkePol.pOffers[i].HashingAlgorithm.uAlgoIdentifier;
  770. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].HashAlgorithm.KeySize = IkePol.pOffers[i].HashingAlgorithm.uAlgoKeyLen;
  771. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].HashAlgorithm.Rounds = IkePol.pOffers[i].HashingAlgorithm.uAlgoRounds;
  772. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].OakleyGroup = IkePol.pOffers[i].dwDHGroup;
  773. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].QuickModeLimit = IkePol.pOffers[i].dwQuickModeLimit;
  774. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].Lifetime.KBytes = IkePol.pOffers[i].Lifetime.uKeyExpirationKBytes;
  775. myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[i].Lifetime.Seconds = IkePol.pOffers[i].Lifetime.uKeyExpirationTime;
  776. }
  777. // now for other stuff - ISAKMPPolicy
  778. myIPSECPolicy->pIpsecISAKMPData->ISAKMPPolicy.PolicyId = myIPSECPolicy->ISAKMPIdentifier;
  779. myIPSECPolicy->pIpsecISAKMPData->ISAKMPPolicy.IdentityProtectionRequired = 0;
  780. myIPSECPolicy->pIpsecISAKMPData->ISAKMPPolicy.PfsIdentityRequired = myIPSECPolicy->pIpsecISAKMPData->pSecurityMethods[0].PfsIdentityRequired;
  781. // save it
  782. hrReturn = IPSecCreateISAKMPData(myPolicyStorage, myIPSECPolicy->pIpsecISAKMPData);
  783. if (hrReturn != ERROR_SUCCESS)
  784. {
  785. return hrReturn;
  786. }
  787. if (IsPolicyInStorage())
  788. {
  789. IPSecSetPolicyData(myPolicyStorage, myIPSECPolicy);
  790. }
  791. else
  792. {
  793. TryToCreatePolicy();
  794. }
  795. IPSecDeleteISAKMPData(myPolicyStorage, oldISAKMP);
  796. return ERROR_SUCCESS;
  797. }
  798. LPVOID
  799. IPSECPolicyToStorage::ReallocPolMem(
  800. LPVOID pOldMem,
  801. DWORD cbOld,
  802. DWORD cbNew
  803. )
  804. {
  805. LPVOID pNewMem;
  806. pNewMem=IPSecAllocPolMem(cbNew);
  807. if (pOldMem && pNewMem) {
  808. memcpy(pNewMem, pOldMem, min(cbNew, cbOld));
  809. IPSecFreePolMem(pOldMem);
  810. }
  811. return pNewMem;
  812. }
  813. // forms a rule but doesn't commit it
  814. // forms default response rule
  815. PIPSEC_NFA_DATA
  816. IPSECPolicyToStorage::MakeDefaultResponseRule ()
  817. {
  818. RPC_STATUS RpcStat;
  819. PIPSEC_NFA_DATA pRule = (PIPSEC_NFA_DATA) IPSecAllocPolMem(sizeof(IPSEC_NFA_DATA));
  820. assert(pRule);
  821. pRule->pszIpsecName = pRule->pszDescription = pRule->pszInterfaceName = pRule->pszEndPointName = NULL;
  822. RpcStat = UuidCreate(&(pRule->NFAIdentifier));
  823. assert(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY);
  824. pRule->dwWhenChanged = 0;
  825. // filter list
  826. pRule->pIpsecFilterData = NULL;
  827. UuidCreateNil(&(pRule->FilterIdentifier));
  828. // filter action
  829. pRule->pIpsecNegPolData = MakeDefaultResponseNegotiationPolicy ();
  830. pRule->NegPolIdentifier = pRule->pIpsecNegPolData->NegPolIdentifier;
  831. // tunnel address
  832. pRule->dwTunnelFlags = 0;
  833. // interface type
  834. pRule->dwInterfaceType = PAS_INTERFACE_TYPE_ALL;
  835. // active flag
  836. pRule->dwActiveFlag = FALSE;
  837. // auth methods
  838. pRule->dwAuthMethodCount = 1;
  839. pRule->ppAuthMethods = (PIPSEC_AUTH_METHOD *) IPSecAllocPolMem(pRule->dwAuthMethodCount * sizeof(PIPSEC_AUTH_METHOD));
  840. assert(pRule->ppAuthMethods);
  841. pRule->ppAuthMethods[0] = (PIPSEC_AUTH_METHOD) IPSecAllocPolMem(sizeof(IPSEC_AUTH_METHOD));
  842. pRule->ppAuthMethods[0]->dwAuthType = IKE_SSPI;
  843. pRule->ppAuthMethods[0]->dwAuthLen = 0;
  844. pRule->ppAuthMethods[0]->pszAuthMethod = NULL;
  845. return pRule;
  846. }
  847. // does not commit it to the storage
  848. PIPSEC_NEGPOL_DATA
  849. IPSECPolicyToStorage::MakeDefaultResponseNegotiationPolicy ( )
  850. {
  851. RPC_STATUS RpcStat;
  852. int i;
  853. PIPSEC_NEGPOL_DATA pNegPol = (PIPSEC_NEGPOL_DATA) IPSecAllocPolMem(sizeof(IPSEC_NEGPOL_DATA));
  854. WCHAR pFAName[POTF_MAX_STRLEN];
  855. RpcStat = UuidCreate(&(pNegPol->NegPolIdentifier));
  856. assert(RpcStat == RPC_S_OK || RpcStat == RPC_S_UUID_LOCAL_ONLY);
  857. pNegPol->NegPolAction = GUID_NEGOTIATION_ACTION_NORMAL_IPSEC;
  858. pNegPol->NegPolType = GUID_NEGOTIATION_TYPE_DEFAULT;
  859. pNegPol->dwSecurityMethodCount = 6;
  860. // allocate sec.methods
  861. pNegPol->pIpsecSecurityMethods = (IPSEC_SECURITY_METHOD *) IPSecAllocPolMem(pNegPol->dwSecurityMethodCount * sizeof(IPSEC_SECURITY_METHOD));
  862. // method 0 - ESP[3DES, SHA1]
  863. pNegPol->pIpsecSecurityMethods[0].Lifetime.KeyExpirationBytes = 0;
  864. pNegPol->pIpsecSecurityMethods[0].Lifetime.KeyExpirationTime = 0;
  865. pNegPol->pIpsecSecurityMethods[0].Flags = 0;
  866. pNegPol->pIpsecSecurityMethods[0].PfsQMRequired = FALSE;
  867. pNegPol->pIpsecSecurityMethods[0].Count = 1;
  868. pNegPol->pIpsecSecurityMethods[0].Algos[0].algoIdentifier = IPSEC_DOI_ESP_3_DES;
  869. pNegPol->pIpsecSecurityMethods[0].Algos[0].secondaryAlgoIdentifier = IPSEC_DOI_AH_SHA1;
  870. pNegPol->pIpsecSecurityMethods[0].Algos[0].algoKeylen = 0;
  871. pNegPol->pIpsecSecurityMethods[0].Algos[0].algoRounds = 0;
  872. pNegPol->pIpsecSecurityMethods[0].Algos[0].operation = Encrypt;
  873. // method 1 - ESP[3DES, MD5]
  874. pNegPol->pIpsecSecurityMethods[1].Lifetime.KeyExpirationBytes = 0;
  875. pNegPol->pIpsecSecurityMethods[1].Lifetime.KeyExpirationTime = 0;
  876. pNegPol->pIpsecSecurityMethods[1].Flags = 0;
  877. pNegPol->pIpsecSecurityMethods[1].PfsQMRequired = FALSE;
  878. pNegPol->pIpsecSecurityMethods[1].Count = 1;
  879. pNegPol->pIpsecSecurityMethods[1].Algos[0].algoIdentifier = IPSEC_DOI_ESP_3_DES;
  880. pNegPol->pIpsecSecurityMethods[1].Algos[0].secondaryAlgoIdentifier = IPSEC_DOI_AH_MD5;
  881. pNegPol->pIpsecSecurityMethods[1].Algos[0].algoKeylen = 0;
  882. pNegPol->pIpsecSecurityMethods[1].Algos[0].algoRounds = 0;
  883. pNegPol->pIpsecSecurityMethods[1].Algos[0].operation = Encrypt;
  884. // method 2 - ESP[DES, SHA1]
  885. pNegPol->pIpsecSecurityMethods[2].Lifetime.KeyExpirationBytes = 0;
  886. pNegPol->pIpsecSecurityMethods[2].Lifetime.KeyExpirationTime = 0;
  887. pNegPol->pIpsecSecurityMethods[2].Flags = 0;
  888. pNegPol->pIpsecSecurityMethods[2].PfsQMRequired = FALSE;
  889. pNegPol->pIpsecSecurityMethods[2].Count = 1;
  890. pNegPol->pIpsecSecurityMethods[2].Algos[0].algoIdentifier = IPSEC_DOI_ESP_DES;
  891. pNegPol->pIpsecSecurityMethods[2].Algos[0].secondaryAlgoIdentifier = IPSEC_DOI_AH_SHA1;
  892. pNegPol->pIpsecSecurityMethods[2].Algos[0].algoKeylen = 0;
  893. pNegPol->pIpsecSecurityMethods[2].Algos[0].algoRounds = 0;
  894. pNegPol->pIpsecSecurityMethods[2].Algos[0].operation = Encrypt;
  895. // method 3 - ESP[DES, MD5]
  896. pNegPol->pIpsecSecurityMethods[3].Lifetime.KeyExpirationBytes = 0;
  897. pNegPol->pIpsecSecurityMethods[3].Lifetime.KeyExpirationTime = 0;
  898. pNegPol->pIpsecSecurityMethods[3].Flags = 0;
  899. pNegPol->pIpsecSecurityMethods[3].PfsQMRequired = FALSE;
  900. pNegPol->pIpsecSecurityMethods[3].Count = 1;
  901. pNegPol->pIpsecSecurityMethods[3].Algos[0].algoIdentifier = IPSEC_DOI_ESP_DES;
  902. pNegPol->pIpsecSecurityMethods[3].Algos[0].secondaryAlgoIdentifier = IPSEC_DOI_AH_MD5;
  903. pNegPol->pIpsecSecurityMethods[3].Algos[0].algoKeylen = 0;
  904. pNegPol->pIpsecSecurityMethods[3].Algos[0].algoRounds = 0;
  905. pNegPol->pIpsecSecurityMethods[3].Algos[0].operation = Encrypt;
  906. // method 4 - AH[SHA1]
  907. pNegPol->pIpsecSecurityMethods[4].Lifetime.KeyExpirationBytes = 0;
  908. pNegPol->pIpsecSecurityMethods[4].Lifetime.KeyExpirationTime = 0;
  909. pNegPol->pIpsecSecurityMethods[4].Flags = 0;
  910. pNegPol->pIpsecSecurityMethods[4].PfsQMRequired = FALSE;
  911. pNegPol->pIpsecSecurityMethods[4].Count = 1;
  912. pNegPol->pIpsecSecurityMethods[4].Algos[0].algoIdentifier = IPSEC_DOI_AH_SHA1;
  913. pNegPol->pIpsecSecurityMethods[4].Algos[0].secondaryAlgoIdentifier = IPSEC_DOI_AH_NONE;
  914. pNegPol->pIpsecSecurityMethods[4].Algos[0].algoKeylen = 0;
  915. pNegPol->pIpsecSecurityMethods[4].Algos[0].algoRounds = 0;
  916. pNegPol->pIpsecSecurityMethods[4].Algos[0].operation = Auth;
  917. // method 5 - AH[MD5]
  918. pNegPol->pIpsecSecurityMethods[5].Lifetime.KeyExpirationBytes = 0;
  919. pNegPol->pIpsecSecurityMethods[5].Lifetime.KeyExpirationTime = 0;
  920. pNegPol->pIpsecSecurityMethods[5].Flags = 0;
  921. pNegPol->pIpsecSecurityMethods[5].PfsQMRequired = FALSE;
  922. pNegPol->pIpsecSecurityMethods[5].Count = 1;
  923. pNegPol->pIpsecSecurityMethods[5].Algos[0].algoIdentifier = IPSEC_DOI_AH_MD5;
  924. pNegPol->pIpsecSecurityMethods[5].Algos[0].secondaryAlgoIdentifier = IPSEC_DOI_AH_NONE;
  925. pNegPol->pIpsecSecurityMethods[5].Algos[0].algoKeylen = 0;
  926. pNegPol->pIpsecSecurityMethods[5].Algos[0].algoRounds = 0;
  927. pNegPol->pIpsecSecurityMethods[5].Algos[0].operation = Auth;
  928. // name
  929. swprintf(pFAName, TEXT(""));
  930. pNegPol->pszIpsecName = IPSecAllocPolStr(pFAName);
  931. pNegPol->pszDescription = NULL;
  932. return pNegPol;
  933. }
  934. #ifdef __cplusplus
  935. }
  936. #endif
  937.