Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

647 lines
14 KiB

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