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.

610 lines
12 KiB

  1. #include "precomp.h"
  2. DWORD
  3. PAAddMMPolicies(
  4. PIPSEC_ISAKMP_DATA * ppIpsecISAKMPData,
  5. DWORD dwNumPolicies
  6. )
  7. {
  8. DWORD dwError = 0;
  9. DWORD i = 0;
  10. PMMPOLICYSTATE pMMPolicyState = NULL;
  11. PIPSEC_MM_POLICY pSPDMMPolicy = NULL;
  12. LPWSTR pServerName = NULL;
  13. DWORD dwPersist = 0;
  14. for (i = 0; i < dwNumPolicies; i++) {
  15. dwError = PACreateMMPolicyState(
  16. *(ppIpsecISAKMPData + i),
  17. &pMMPolicyState
  18. );
  19. if (dwError) {
  20. continue;
  21. }
  22. dwError = PACreateMMPolicy(
  23. *(ppIpsecISAKMPData + i),
  24. pMMPolicyState,
  25. &pSPDMMPolicy
  26. );
  27. if (dwError) {
  28. pMMPolicyState->bInSPD = FALSE;
  29. pMMPolicyState->dwErrorCode = dwError;
  30. pMMPolicyState->pNext = gpMMPolicyState;
  31. gpMMPolicyState = pMMPolicyState;
  32. continue;
  33. }
  34. dwError = AddMMPolicy(
  35. pServerName,
  36. dwPersist,
  37. pSPDMMPolicy
  38. );
  39. if (dwError) {
  40. pMMPolicyState->bInSPD = FALSE;
  41. pMMPolicyState->dwErrorCode = dwError;
  42. }
  43. else {
  44. pMMPolicyState->bInSPD = TRUE;
  45. pMMPolicyState->dwErrorCode = ERROR_SUCCESS;
  46. }
  47. pMMPolicyState->pNext = gpMMPolicyState;
  48. gpMMPolicyState = pMMPolicyState;
  49. PAFreeMMPolicy(pSPDMMPolicy);
  50. }
  51. return (dwError);
  52. }
  53. DWORD
  54. PACreateMMPolicyState(
  55. PIPSEC_ISAKMP_DATA pIpsecISAKMPData,
  56. PMMPOLICYSTATE * ppMMPolicyState
  57. )
  58. {
  59. DWORD dwError = 0;
  60. PMMPOLICYSTATE pMMPolicyState = NULL;
  61. WCHAR pszName[512];
  62. dwError = AllocateSPDMemory(
  63. sizeof(MMPOLICYSTATE),
  64. &pMMPolicyState
  65. );
  66. BAIL_ON_WIN32_ERROR(dwError);
  67. memcpy(
  68. &(pMMPolicyState->gPolicyID),
  69. &(pIpsecISAKMPData->ISAKMPIdentifier),
  70. sizeof(GUID)
  71. );
  72. wsprintf(pszName, L"%d", ++gdwMMPolicyCounter);
  73. dwError = AllocateSPDString(
  74. pszName,
  75. &(pMMPolicyState->pszPolicyName)
  76. );
  77. BAIL_ON_WIN32_ERROR(dwError);
  78. pMMPolicyState->bInSPD = FALSE;
  79. pMMPolicyState->dwErrorCode = 0;
  80. pMMPolicyState->pNext = NULL;
  81. *ppMMPolicyState = pMMPolicyState;
  82. return (dwError);
  83. error:
  84. if (pMMPolicyState) {
  85. PAFreeMMPolicyState(pMMPolicyState);
  86. }
  87. *ppMMPolicyState = NULL;
  88. return (dwError);
  89. }
  90. VOID
  91. PAFreeMMPolicyState(
  92. PMMPOLICYSTATE pMMPolicyState
  93. )
  94. {
  95. if (pMMPolicyState) {
  96. if (pMMPolicyState->pszPolicyName) {
  97. FreeSPDString(pMMPolicyState->pszPolicyName);
  98. }
  99. FreeSPDMemory(pMMPolicyState);
  100. }
  101. }
  102. DWORD
  103. PACreateMMPolicy(
  104. PIPSEC_ISAKMP_DATA pIpsecISAKMPData,
  105. PMMPOLICYSTATE pMMPolicyState,
  106. PIPSEC_MM_POLICY * ppSPDMMPolicy
  107. )
  108. {
  109. DWORD dwError = 0;
  110. PIPSEC_MM_POLICY pSPDMMPolicy = NULL;
  111. dwError = AllocateSPDMemory(
  112. sizeof(IPSEC_MM_POLICY),
  113. &pSPDMMPolicy
  114. );
  115. BAIL_ON_WIN32_ERROR(dwError);
  116. memcpy(
  117. &(pSPDMMPolicy->gPolicyID),
  118. &(pIpsecISAKMPData->ISAKMPIdentifier),
  119. sizeof(GUID)
  120. );
  121. dwError = AllocateSPDString(
  122. pMMPolicyState->pszPolicyName,
  123. &(pSPDMMPolicy->pszPolicyName)
  124. );
  125. BAIL_ON_WIN32_ERROR(dwError);
  126. pSPDMMPolicy->dwFlags = 0;
  127. pSPDMMPolicy->dwFlags |= IPSEC_MM_POLICY_DEFAULT_POLICY;
  128. dwError = PACreateMMOffers(
  129. pIpsecISAKMPData->dwNumISAKMPSecurityMethods,
  130. pIpsecISAKMPData->pSecurityMethods,
  131. &(pSPDMMPolicy->dwOfferCount),
  132. &(pSPDMMPolicy->pOffers)
  133. );
  134. BAIL_ON_WIN32_ERROR(dwError);
  135. pSPDMMPolicy->uSoftSAExpirationTime =
  136. (pSPDMMPolicy->pOffers)->Lifetime.uKeyExpirationTime;
  137. *ppSPDMMPolicy = pSPDMMPolicy;
  138. return (dwError);
  139. error:
  140. if (pSPDMMPolicy) {
  141. PAFreeMMPolicy(
  142. pSPDMMPolicy
  143. );
  144. }
  145. *ppSPDMMPolicy = NULL;
  146. return (dwError);
  147. }
  148. DWORD
  149. PACreateMMOffers(
  150. DWORD dwNumISAKMPSecurityMethods,
  151. PCRYPTO_BUNDLE pSecurityMethods,
  152. PDWORD pdwOfferCount,
  153. PIPSEC_MM_OFFER * ppOffers
  154. )
  155. {
  156. DWORD dwError = 0;
  157. DWORD dwOfferCount = 0;
  158. PIPSEC_MM_OFFER pOffers = NULL;
  159. PIPSEC_MM_OFFER pTempOffer = NULL;
  160. PCRYPTO_BUNDLE pTempBundle = NULL;
  161. DWORD i = 0;
  162. if (!dwNumISAKMPSecurityMethods || !pSecurityMethods) {
  163. dwError = ERROR_INVALID_PARAMETER;
  164. BAIL_ON_WIN32_ERROR(dwError);
  165. }
  166. if (dwNumISAKMPSecurityMethods > IPSEC_MAX_MM_OFFERS) {
  167. dwOfferCount = IPSEC_MAX_MM_OFFERS;
  168. }
  169. else {
  170. dwOfferCount = dwNumISAKMPSecurityMethods;
  171. }
  172. dwError = AllocateSPDMemory(
  173. sizeof(IPSEC_MM_OFFER)*dwOfferCount,
  174. &(pOffers)
  175. );
  176. BAIL_ON_WIN32_ERROR(dwError);
  177. pTempOffer = pOffers;
  178. pTempBundle = pSecurityMethods;
  179. for (i = 0; i < dwOfferCount; i++) {
  180. PACopyMMOffer(
  181. pTempBundle,
  182. pTempOffer
  183. );
  184. pTempOffer++;
  185. pTempBundle++;
  186. }
  187. *pdwOfferCount = dwOfferCount;
  188. *ppOffers = pOffers;
  189. return (dwError);
  190. error:
  191. if (pOffers) {
  192. PAFreeMMOffers(
  193. i,
  194. pOffers
  195. );
  196. }
  197. *pdwOfferCount = 0;
  198. *ppOffers = NULL;
  199. return (dwError);
  200. }
  201. VOID
  202. PACopyMMOffer(
  203. PCRYPTO_BUNDLE pBundle,
  204. PIPSEC_MM_OFFER pOffer
  205. )
  206. {
  207. pOffer->Lifetime.uKeyExpirationKBytes = pBundle->Lifetime.KBytes;
  208. pOffer->Lifetime.uKeyExpirationTime = pBundle->Lifetime.Seconds;
  209. pOffer->dwFlags = 0;
  210. pOffer->dwQuickModeLimit = pBundle->QuickModeLimit;
  211. pOffer->dwDHGroup = pBundle->OakleyGroup;
  212. if ((pOffer->dwDHGroup != DH_GROUP_1) &&
  213. (pOffer->dwDHGroup != DH_GROUP_2)) {
  214. pOffer->dwDHGroup = DH_GROUP_1;
  215. }
  216. switch (pBundle->EncryptionAlgorithm.AlgorithmIdentifier) {
  217. case IPSEC_ESP_DES:
  218. pOffer->EncryptionAlgorithm.uAlgoIdentifier = IPSEC_DOI_ESP_DES;
  219. break;
  220. case IPSEC_ESP_DES_40:
  221. pOffer->EncryptionAlgorithm.uAlgoIdentifier = IPSEC_DOI_ESP_DES;
  222. break;
  223. case IPSEC_ESP_3_DES:
  224. pOffer->EncryptionAlgorithm.uAlgoIdentifier = IPSEC_DOI_ESP_3_DES;
  225. break;
  226. default:
  227. pOffer->EncryptionAlgorithm.uAlgoIdentifier= IPSEC_DOI_ESP_NONE;
  228. break;
  229. }
  230. pOffer->EncryptionAlgorithm.uAlgoKeyLen =
  231. pBundle->EncryptionAlgorithm.KeySize;
  232. pOffer->EncryptionAlgorithm.uAlgoRounds =
  233. pBundle->EncryptionAlgorithm.Rounds;
  234. switch(pBundle->HashAlgorithm.AlgorithmIdentifier) {
  235. case IPSEC_AH_MD5:
  236. pOffer->HashingAlgorithm.uAlgoIdentifier = IPSEC_DOI_AH_MD5;
  237. break;
  238. case IPSEC_AH_SHA:
  239. pOffer->HashingAlgorithm.uAlgoIdentifier = IPSEC_DOI_AH_SHA1;
  240. break;
  241. default:
  242. pOffer->HashingAlgorithm.uAlgoIdentifier = IPSEC_DOI_AH_NONE;
  243. break;
  244. }
  245. pOffer->HashingAlgorithm.uAlgoKeyLen =
  246. pBundle->HashAlgorithm.KeySize;
  247. pOffer->HashingAlgorithm.uAlgoRounds =
  248. pBundle->HashAlgorithm.Rounds;
  249. }
  250. VOID
  251. PAFreeMMPolicy(
  252. PIPSEC_MM_POLICY pSPDMMPolicy
  253. )
  254. {
  255. if (pSPDMMPolicy) {
  256. if (pSPDMMPolicy->pszPolicyName) {
  257. FreeSPDString(pSPDMMPolicy->pszPolicyName);
  258. }
  259. PAFreeMMOffers(
  260. pSPDMMPolicy->dwOfferCount,
  261. pSPDMMPolicy->pOffers
  262. );
  263. FreeSPDMemory(pSPDMMPolicy);
  264. }
  265. }
  266. VOID
  267. PAFreeMMOffers(
  268. DWORD dwOfferCount,
  269. PIPSEC_MM_OFFER pOffers
  270. )
  271. {
  272. if (pOffers) {
  273. FreeSPDMemory(pOffers);
  274. }
  275. }
  276. DWORD
  277. PADeleteAllMMPolicies(
  278. )
  279. {
  280. DWORD dwError = 0;
  281. PMMPOLICYSTATE pMMPolicyState = NULL;
  282. LPWSTR pServerName = NULL;
  283. PMMPOLICYSTATE pTemp = NULL;
  284. PMMPOLICYSTATE pLeftMMPolicyState = NULL;
  285. pMMPolicyState = gpMMPolicyState;
  286. while (pMMPolicyState) {
  287. if (pMMPolicyState->bInSPD) {
  288. dwError = DeleteMMPolicy(
  289. pServerName,
  290. pMMPolicyState->pszPolicyName
  291. );
  292. if (!dwError) {
  293. pTemp = pMMPolicyState;
  294. pMMPolicyState = pMMPolicyState->pNext;
  295. PAFreeMMPolicyState(pTemp);
  296. }
  297. else {
  298. pMMPolicyState->dwErrorCode = dwError;
  299. pTemp = pMMPolicyState;
  300. pMMPolicyState = pMMPolicyState->pNext;
  301. pTemp->pNext = pLeftMMPolicyState;
  302. pLeftMMPolicyState = pTemp;
  303. }
  304. }
  305. else {
  306. pTemp = pMMPolicyState;
  307. pMMPolicyState = pMMPolicyState->pNext;
  308. PAFreeMMPolicyState(pTemp);
  309. }
  310. }
  311. gpMMPolicyState = pLeftMMPolicyState;
  312. return (dwError);
  313. }
  314. VOID
  315. PAFreeMMPolicyStateList(
  316. PMMPOLICYSTATE pMMPolicyState
  317. )
  318. {
  319. PMMPOLICYSTATE pTemp = NULL;
  320. while (pMMPolicyState) {
  321. pTemp = pMMPolicyState;
  322. pMMPolicyState = pMMPolicyState->pNext;
  323. PAFreeMMPolicyState(pTemp);
  324. }
  325. }
  326. PMMPOLICYSTATE
  327. FindMMPolicyState(
  328. GUID gPolicyID
  329. )
  330. {
  331. PMMPOLICYSTATE pMMPolicyState = NULL;
  332. pMMPolicyState = gpMMPolicyState;
  333. while (pMMPolicyState) {
  334. if (!memcmp(&(pMMPolicyState->gPolicyID), &gPolicyID, sizeof(GUID))) {
  335. return (pMMPolicyState);
  336. }
  337. pMMPolicyState = pMMPolicyState->pNext;
  338. }
  339. return (NULL);
  340. }
  341. DWORD
  342. PADeleteMMPolicies(
  343. PIPSEC_ISAKMP_DATA * ppIpsecISAKMPData,
  344. DWORD dwNumPolicies
  345. )
  346. {
  347. DWORD dwError = 0;
  348. DWORD i = 0;
  349. PIPSEC_ISAKMP_DATA pIpsecISAKMPData = NULL;
  350. for (i = 0; i < dwNumPolicies; i++) {
  351. pIpsecISAKMPData = *(ppIpsecISAKMPData + i);
  352. dwError = PADeleteMMPolicy(
  353. pIpsecISAKMPData->ISAKMPIdentifier
  354. );
  355. }
  356. return (dwError);
  357. }
  358. DWORD
  359. PADeleteMMPolicy(
  360. GUID gPolicyID
  361. )
  362. {
  363. DWORD dwError = 0;
  364. PMMPOLICYSTATE pMMPolicyState = NULL;
  365. LPWSTR pServerName = NULL;
  366. pMMPolicyState = FindMMPolicyState(
  367. gPolicyID
  368. );
  369. if (!pMMPolicyState) {
  370. dwError = ERROR_SUCCESS;
  371. return (dwError);
  372. }
  373. if (pMMPolicyState->bInSPD) {
  374. dwError = DeleteMMPolicy(
  375. pServerName,
  376. pMMPolicyState->pszPolicyName
  377. );
  378. if (dwError) {
  379. pMMPolicyState->dwErrorCode = dwError;
  380. }
  381. BAIL_ON_WIN32_ERROR(dwError);
  382. }
  383. PADeleteMMPolicyState(pMMPolicyState);
  384. error:
  385. return (dwError);
  386. }
  387. VOID
  388. PADeleteMMPolicyState(
  389. PMMPOLICYSTATE pMMPolicyState
  390. )
  391. {
  392. PMMPOLICYSTATE * ppTemp = NULL;
  393. ppTemp = &gpMMPolicyState;
  394. while (*ppTemp) {
  395. if (*ppTemp == pMMPolicyState) {
  396. break;
  397. }
  398. ppTemp = &((*ppTemp)->pNext);
  399. }
  400. if (*ppTemp) {
  401. *ppTemp = pMMPolicyState->pNext;
  402. }
  403. PAFreeMMPolicyState(pMMPolicyState);
  404. return;
  405. }
  406. DWORD
  407. PADeleteInUseMMPolicies(
  408. )
  409. {
  410. DWORD dwError = 0;
  411. PMMPOLICYSTATE pMMPolicyState = NULL;
  412. LPWSTR pServerName = NULL;
  413. PMMPOLICYSTATE pTemp = NULL;
  414. PMMPOLICYSTATE pLeftMMPolicyState = NULL;
  415. pMMPolicyState = gpMMPolicyState;
  416. while (pMMPolicyState) {
  417. if (pMMPolicyState->bInSPD &&
  418. (pMMPolicyState->dwErrorCode == ERROR_IPSEC_MM_POLICY_IN_USE)) {
  419. dwError = DeleteMMPolicy(
  420. pServerName,
  421. pMMPolicyState->pszPolicyName
  422. );
  423. if (!dwError) {
  424. pTemp = pMMPolicyState;
  425. pMMPolicyState = pMMPolicyState->pNext;
  426. PAFreeMMPolicyState(pTemp);
  427. }
  428. else {
  429. pTemp = pMMPolicyState;
  430. pMMPolicyState = pMMPolicyState->pNext;
  431. pTemp->pNext = pLeftMMPolicyState;
  432. pLeftMMPolicyState = pTemp;
  433. }
  434. }
  435. else {
  436. pTemp = pMMPolicyState;
  437. pMMPolicyState = pMMPolicyState->pNext;
  438. pTemp->pNext = pLeftMMPolicyState;
  439. pLeftMMPolicyState = pTemp;
  440. }
  441. }
  442. gpMMPolicyState = pLeftMMPolicyState;
  443. return (dwError);
  444. }