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.

1003 lines
30 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. IKE utils
  5. Abstract:
  6. Contains parameter validation
  7. Author:
  8. BrianSw 10-19-200
  9. Environment:
  10. User Level: Win32
  11. Revision History:
  12. --*/
  13. #include "precomp.h"
  14. DWORD
  15. ValidateInitiateIKENegotiation(
  16. STRING_HANDLE pServerName,
  17. PQM_FILTER_CONTAINER pQMFilterContainer,
  18. DWORD dwClientProcessId,
  19. ULONG uhClientEvent,
  20. DWORD dwFlags,
  21. IPSEC_UDP_ENCAP_CONTEXT UdpEncapContext,
  22. IKENEGOTIATION_HANDLE * phIKENegotiation
  23. )
  24. {
  25. DWORD dwError=ERROR_SUCCESS;
  26. if (pQMFilterContainer == NULL ||
  27. pQMFilterContainer->pQMFilters == NULL) {
  28. dwError=ERROR_INVALID_PARAMETER;
  29. }
  30. if (pServerName && (wcscmp(pServerName,L"") != 0)) {
  31. if (uhClientEvent || phIKENegotiation || dwClientProcessId) {
  32. dwError=ERROR_NOT_SUPPORTED;
  33. BAIL_ON_WIN32_ERROR(dwError);
  34. }
  35. } else {
  36. if (phIKENegotiation || dwClientProcessId || uhClientEvent) {
  37. if (!phIKENegotiation || !dwClientProcessId || !uhClientEvent) {
  38. dwError=ERROR_INVALID_PARAMETER;
  39. BAIL_ON_WIN32_ERROR(dwError);
  40. }
  41. }
  42. }
  43. if (!(pQMFilterContainer->pQMFilters) ||
  44. !(pQMFilterContainer->dwNumFilters)) {
  45. dwError = ERROR_INVALID_PARAMETER;
  46. BAIL_ON_WIN32_ERROR(dwError);
  47. }
  48. dwError = ValidateIPSecQMFilter(
  49. pQMFilterContainer->pQMFilters
  50. );
  51. BAIL_ON_WIN32_ERROR(dwError);
  52. error:
  53. return dwError;
  54. }
  55. DWORD
  56. ValidateQueryIKENegotiationStatus(
  57. IKENEGOTIATION_HANDLE hIKENegotiation,
  58. SA_NEGOTIATION_STATUS_INFO *NegotiationStatus
  59. )
  60. {
  61. DWORD dwError=ERROR_SUCCESS;
  62. if (!hIKENegotiation || !NegotiationStatus) {
  63. dwError = ERROR_INVALID_PARAMETER;
  64. BAIL_ON_WIN32_ERROR(dwError);
  65. }
  66. error:
  67. return dwError;
  68. }
  69. DWORD
  70. ValidateCloseIKENegotiationHandle(
  71. IKENEGOTIATION_HANDLE * phIKENegotiation
  72. )
  73. {
  74. DWORD dwError=ERROR_SUCCESS;
  75. if (!phIKENegotiation) {
  76. dwError=ERROR_INVALID_PARAMETER;
  77. }
  78. BAIL_ON_WIN32_ERROR(dwError);
  79. error:
  80. return dwError;
  81. }
  82. DWORD
  83. ValidateEnumMMSAs(
  84. STRING_HANDLE pServerName,
  85. PMM_SA_CONTAINER pMMTemplate,
  86. PMM_SA_CONTAINER *ppMMSAContainer,
  87. LPDWORD pdwNumEntries,
  88. LPDWORD pdwTotalMMsAvailable,
  89. LPDWORD pdwEnumHandle,
  90. DWORD dwFlags
  91. )
  92. {
  93. DWORD dwError=ERROR_SUCCESS;
  94. if (pMMTemplate == NULL ||
  95. pMMTemplate->pMMSAs == NULL ||
  96. pMMTemplate->dwNumMMSAs == 0 ||
  97. ppMMSAContainer == NULL ||
  98. *ppMMSAContainer == NULL ||
  99. pdwNumEntries == NULL ||
  100. pdwTotalMMsAvailable == NULL ||
  101. pdwEnumHandle == NULL ) {
  102. dwError=ERROR_INVALID_PARAMETER;
  103. }
  104. BAIL_ON_WIN32_ERROR(dwError);
  105. dwError = ValidateAddr(&(pMMTemplate->pMMSAs->Me));
  106. BAIL_ON_WIN32_ERROR(dwError);
  107. dwError = ValidateAddr(&(pMMTemplate->pMMSAs->Peer));
  108. BAIL_ON_WIN32_ERROR(dwError);
  109. error:
  110. return dwError;
  111. }
  112. DWORD
  113. ValidateDeleteMMSAs(
  114. STRING_HANDLE pServerName,
  115. PMM_SA_CONTAINER pMMTemplate,
  116. DWORD dwFlags
  117. )
  118. {
  119. DWORD dwError=ERROR_SUCCESS;
  120. if (pMMTemplate == NULL ||
  121. pMMTemplate->dwNumMMSAs == 0 ||
  122. pMMTemplate->pMMSAs == NULL) {
  123. dwError=ERROR_INVALID_PARAMETER;
  124. }
  125. BAIL_ON_WIN32_ERROR(dwError);
  126. dwError = ValidateAddr(&(pMMTemplate->pMMSAs->Me));
  127. BAIL_ON_WIN32_ERROR(dwError);
  128. dwError = ValidateAddr(&(pMMTemplate->pMMSAs->Peer));
  129. BAIL_ON_WIN32_ERROR(dwError);
  130. error:
  131. return dwError;
  132. }
  133. DWORD
  134. ValidateQueryIKEStatistics(
  135. STRING_HANDLE pServerName,
  136. IKE_STATISTICS *pIKEStatistics
  137. )
  138. {
  139. DWORD dwError=ERROR_SUCCESS;
  140. if (pIKEStatistics == NULL) {
  141. dwError=ERROR_INVALID_PARAMETER;
  142. }
  143. BAIL_ON_WIN32_ERROR(dwError);
  144. error:
  145. return dwError;
  146. }
  147. DWORD
  148. ValidateRegisterIKENotifyClient(
  149. STRING_HANDLE pServerName,
  150. DWORD dwClientProcessId,
  151. ULONG uhClientEvent,
  152. PQM_SA_CONTAINER pQMSATemplateContainer,
  153. IKENOTIFY_HANDLE *phNotifyHandle,
  154. DWORD dwFlags
  155. )
  156. {
  157. DWORD dwError=ERROR_SUCCESS;
  158. if (pServerName && (wcscmp(pServerName,L"") != 0)) {
  159. return ERROR_NOT_SUPPORTED;
  160. }
  161. if (pQMSATemplateContainer == NULL ||
  162. pQMSATemplateContainer->pQMSAs == NULL ||
  163. pQMSATemplateContainer->dwNumQMSAs == 0 ||
  164. phNotifyHandle == NULL) {
  165. dwError=ERROR_INVALID_PARAMETER;
  166. }
  167. BAIL_ON_WIN32_ERROR(dwError);
  168. dwError = ValidateQMFilterAddresses(
  169. &pQMSATemplateContainer->pQMSAs->IpsecQMFilter
  170. );
  171. BAIL_ON_WIN32_ERROR(dwError);
  172. error:
  173. return dwError;
  174. }
  175. DWORD ValidateQueryNotifyData(
  176. IKENOTIFY_HANDLE uhNotifyHandle,
  177. PDWORD pdwNumEntries,
  178. PQM_SA_CONTAINER *ppQMSAContainer,
  179. DWORD dwFlags
  180. )
  181. {
  182. DWORD dwError=ERROR_SUCCESS;
  183. if (ppQMSAContainer == NULL ||
  184. *ppQMSAContainer == NULL ||
  185. pdwNumEntries == NULL ||
  186. *pdwNumEntries == 0) {
  187. dwError=ERROR_INVALID_PARAMETER;
  188. }
  189. BAIL_ON_WIN32_ERROR(dwError);
  190. error:
  191. return dwError;
  192. }
  193. DWORD ValidateCloseNotifyHandle(
  194. IKENOTIFY_HANDLE *phHandle
  195. )
  196. {
  197. DWORD dwError=ERROR_SUCCESS;
  198. if (phHandle == NULL) {
  199. dwError=ERROR_INVALID_PARAMETER;
  200. }
  201. BAIL_ON_WIN32_ERROR(dwError);
  202. error:
  203. return dwError;
  204. }
  205. DWORD ValidateIPSecAddSA(
  206. STRING_HANDLE pServerName,
  207. IPSEC_SA_DIRECTION SADirection,
  208. PIPSEC_QM_POLICY_CONTAINER pQMPolicyContainer,
  209. PQM_FILTER_CONTAINER pQMFilterContainer,
  210. DWORD *uhLarvalContext,
  211. DWORD dwInboundKeyMatLen,
  212. BYTE *pInboundKeyMat,
  213. DWORD dwOutboundKeyMatLen,
  214. BYTE *pOutboundKeyMat,
  215. BYTE *pContextInfo,
  216. UDP_ENCAP_INFO EncapInfo,
  217. DWORD dwFlags)
  218. {
  219. DWORD dwError=ERROR_SUCCESS;
  220. DWORD dwNumOffers=1;
  221. if (!pQMFilterContainer ||
  222. !pQMPolicyContainer ||
  223. pContextInfo == NULL) {
  224. dwError= ERROR_INVALID_PARAMETER;
  225. }
  226. BAIL_ON_WIN32_ERROR(dwError);
  227. if (uhLarvalContext == NULL){
  228. dwError= ERROR_INVALID_PARAMETER;
  229. BAIL_ON_WIN32_ERROR(dwError);
  230. }
  231. if (SADirection >= SA_DIRECTION_MAX ||
  232. SADirection < SA_DIRECTION_BOTH) {
  233. dwError= ERROR_INVALID_PARAMETER;
  234. BAIL_ON_WIN32_ERROR(dwError);
  235. }
  236. if (!(pQMFilterContainer->pQMFilters) ||
  237. !(pQMFilterContainer->dwNumFilters)) {
  238. dwError = ERROR_INVALID_PARAMETER;
  239. BAIL_ON_WIN32_ERROR(dwError);
  240. }
  241. if (!(pQMPolicyContainer->pPolicies) ||
  242. !(pQMPolicyContainer->dwNumPolicies)) {
  243. dwError = ERROR_INVALID_PARAMETER;
  244. BAIL_ON_WIN32_ERROR(dwError);
  245. }
  246. dwError = ValidateIPSecQMFilter(
  247. pQMFilterContainer->pQMFilters
  248. );
  249. BAIL_ON_WIN32_ERROR(dwError);
  250. dwError = ValidateQMOffers(
  251. dwNumOffers,
  252. pQMPolicyContainer->pPolicies->pOffers);
  253. BAIL_ON_WIN32_ERROR(dwError);
  254. if (EncapInfo.SAEncapType < SA_UDP_ENCAP_TYPE_NONE ||
  255. EncapInfo.SAEncapType >= SA_UDP_ENCAP_TYPE_MAX) {
  256. dwError=ERROR_INVALID_PARAMETER;
  257. }
  258. BAIL_ON_WIN32_ERROR(dwError);
  259. if (EncapInfo.SAEncapType == SA_UDP_ENCAP_TYPE_OTHER) {
  260. dwError=ERROR_NOT_SUPPORTED;
  261. }
  262. BAIL_ON_WIN32_ERROR(dwError);
  263. if (EncapInfo.SAEncapType != SA_UDP_ENCAP_TYPE_NONE) {
  264. if (EncapInfo.PeerAddrVersion != IPSEC_PROTOCOL_V4) {
  265. dwError=ERROR_INVALID_PARAMETER;
  266. }
  267. BAIL_ON_WIN32_ERROR(dwError);
  268. if (EncapInfo.PeerPrivateAddr.AddrType != IP_ADDR_UNIQUE) {
  269. dwError=ERROR_INVALID_PARAMETER;
  270. }
  271. BAIL_ON_WIN32_ERROR(dwError);
  272. dwError=VerifyAddresses(&EncapInfo.PeerPrivateAddr,
  273. FALSE,
  274. TRUE);
  275. }
  276. BAIL_ON_WIN32_ERROR(dwError);
  277. error:
  278. return dwError;
  279. }
  280. DWORD ValidateSetConfigurationVariables(
  281. LPWSTR pServerName,
  282. IKE_CONFIG IKEConfig
  283. )
  284. {
  285. DWORD dwError = ERROR_SUCCESS;
  286. if (IKEConfig.dwStrongCRLCheck > 2) {
  287. dwError= ERROR_INVALID_PARAMETER;
  288. }
  289. BAIL_ON_WIN32_ERROR(dwError);
  290. if (IKEConfig.dwNLBSFlags >= FLAGS_NLBS_MAX) {
  291. dwError= ERROR_INVALID_PARAMETER;
  292. }
  293. BAIL_ON_WIN32_ERROR(dwError);
  294. if (IKEConfig.dwFlags != 0) {
  295. dwError= ERROR_INVALID_PARAMETER;
  296. }
  297. BAIL_ON_WIN32_ERROR(dwError);
  298. error:
  299. return dwError;
  300. }
  301. DWORD ValidateGetConfigurationVariables(
  302. LPWSTR pServerName,
  303. PIKE_CONFIG pIKEConfig
  304. )
  305. {
  306. DWORD dwError = ERROR_SUCCESS;
  307. if (pIKEConfig == NULL) {
  308. dwError=ERROR_INVALID_PARAMETER;
  309. }
  310. BAIL_ON_WIN32_ERROR(dwError);
  311. error:
  312. return dwError;
  313. }
  314. DWORD WINAPI ConvertExtMMAuthToInt(PMM_AUTH_METHODS pMMAuthMethods,
  315. PINT_MM_AUTH_METHODS *pIntMMAuthMethods)
  316. {
  317. PINT_MM_AUTH_METHODS pIntAuth=NULL;
  318. DWORD dwError;
  319. DWORD dwNumIntAuths=0;
  320. DWORD i,j;
  321. PIPSEC_MM_AUTH_INFO pAuthInfo;
  322. PINT_IPSEC_MM_AUTH_INFO pIntAuthInfo;
  323. DWORD dwCurIndex=0;
  324. PCERT_ROOT_CONFIG pInboundCertArray;
  325. dwError=AllocateSPDMemory(sizeof(INT_MM_AUTH_METHODS),&pIntAuth);
  326. BAIL_ON_WIN32_ERROR(dwError);
  327. memcpy(&pIntAuth->gMMAuthID,&pMMAuthMethods->gMMAuthID,sizeof(GUID));
  328. pIntAuth->dwFlags=pMMAuthMethods->dwFlags;
  329. for (i=0; i < pMMAuthMethods->dwNumAuthInfos; i++) {
  330. pAuthInfo=&pMMAuthMethods->pAuthenticationInfo[i];
  331. switch(pAuthInfo->AuthMethod) {
  332. case IKE_PRESHARED_KEY:
  333. case IKE_SSPI:
  334. dwNumIntAuths++;
  335. break;
  336. case IKE_RSA_SIGNATURE:
  337. dwNumIntAuths += pAuthInfo->CertAuthInfo.dwInboundRootArraySize;
  338. break;
  339. }
  340. }
  341. pIntAuth->dwNumAuthInfos=dwNumIntAuths;
  342. dwError=AllocateSPDMemory(sizeof(INT_IPSEC_MM_AUTH_INFO) * dwNumIntAuths,
  343. &pIntAuth->pAuthenticationInfo);
  344. BAIL_ON_WIN32_ERROR(dwError);
  345. for (i=0; i < pMMAuthMethods->dwNumAuthInfos; i++) {
  346. pAuthInfo=&pMMAuthMethods->pAuthenticationInfo[i];
  347. pIntAuthInfo=&pIntAuth->pAuthenticationInfo[dwCurIndex];
  348. pIntAuthInfo->AuthMethod=pAuthInfo->AuthMethod;
  349. switch(pAuthInfo->AuthMethod) {
  350. case IKE_PRESHARED_KEY:
  351. dwError=AllocateSPDMemory(pAuthInfo->GeneralAuthInfo.dwAuthInfoSize,
  352. &pIntAuthInfo->pAuthInfo);
  353. BAIL_ON_WIN32_ERROR(dwError);
  354. memcpy(pIntAuthInfo->pAuthInfo,
  355. pAuthInfo->GeneralAuthInfo.pAuthInfo,
  356. pAuthInfo->GeneralAuthInfo.dwAuthInfoSize);
  357. pIntAuthInfo->dwAuthInfoSize=pAuthInfo->GeneralAuthInfo.dwAuthInfoSize;
  358. dwCurIndex++;
  359. break;
  360. case IKE_SSPI:
  361. dwCurIndex++;
  362. break;
  363. case IKE_RSA_SIGNATURE:
  364. pInboundCertArray=pAuthInfo->CertAuthInfo.pInboundRootArray;
  365. for (j=0;j< pAuthInfo->CertAuthInfo.dwInboundRootArraySize;j++) {
  366. pIntAuthInfo=&pIntAuth->pAuthenticationInfo[dwCurIndex];
  367. dwError=AllocateSPDMemory(pInboundCertArray[j].dwCertDataSize,
  368. &pIntAuthInfo->pAuthInfo);
  369. BAIL_ON_WIN32_ERROR(dwError);
  370. memcpy(pIntAuthInfo->pAuthInfo,pInboundCertArray[j].pCertData,
  371. pInboundCertArray[j].dwCertDataSize);
  372. pIntAuthInfo->dwAuthInfoSize=pInboundCertArray[j].dwCertDataSize;
  373. pIntAuthInfo->AuthMethod=IKE_RSA_SIGNATURE;
  374. pIntAuthInfo->dwAuthFlags=pInboundCertArray[j].dwFlags;
  375. dwCurIndex++;
  376. }
  377. break;
  378. }
  379. }
  380. *pIntMMAuthMethods=pIntAuth;
  381. return ERROR_SUCCESS;
  382. error:
  383. FreeIntMMAuthMethods(pIntAuth);
  384. return (dwError);
  385. }
  386. #define INVALID_INDEX 0xffff
  387. DWORD WINAPI ConvertIntMMAuthToExt(PINT_MM_AUTH_METHODS pIntMMAuthMethods,
  388. PMM_AUTH_METHODS *pMMAuthMethods)
  389. {
  390. PMM_AUTH_METHODS pExtAuth=NULL;
  391. DWORD dwError;
  392. DWORD dwNumExtAuths=0;
  393. DWORD i;
  394. PINT_IPSEC_MM_AUTH_INFO pAuthInfo;
  395. PIPSEC_MM_AUTH_INFO pExtAuthInfo;
  396. PMM_CERT_INFO pCertInfo;
  397. DWORD dwCurCertIndex=0;
  398. PCERT_ROOT_CONFIG pInboundCertArray;
  399. DWORD dwNumCerts=0;
  400. DWORD dwCertIndex=INVALID_INDEX;
  401. DWORD dwPSKeyIndex=INVALID_INDEX;
  402. DWORD dwKerbIndex=INVALID_INDEX;
  403. dwError=AllocateSPDMemory(sizeof(MM_AUTH_METHODS),&pExtAuth);
  404. BAIL_ON_WIN32_ERROR(dwError);
  405. memcpy(&pExtAuth->gMMAuthID,&pIntMMAuthMethods->gMMAuthID,sizeof(GUID));
  406. pExtAuth->dwFlags=pIntMMAuthMethods->dwFlags;
  407. for (i=0; i < pIntMMAuthMethods->dwNumAuthInfos; i++) {
  408. pAuthInfo=&pIntMMAuthMethods->pAuthenticationInfo[i];
  409. switch(pAuthInfo->AuthMethod) {
  410. case IKE_PRESHARED_KEY:
  411. dwPSKeyIndex=dwNumExtAuths;
  412. dwNumExtAuths++;
  413. break;
  414. case IKE_SSPI:
  415. dwKerbIndex=dwNumExtAuths;
  416. dwNumExtAuths++;
  417. break;
  418. case IKE_RSA_SIGNATURE:
  419. if (dwCertIndex == INVALID_INDEX) {
  420. dwCertIndex=dwNumExtAuths;
  421. dwNumExtAuths++;
  422. }
  423. dwNumCerts++;
  424. break;
  425. }
  426. }
  427. pExtAuth->dwNumAuthInfos=dwNumExtAuths;
  428. dwError=AllocateSPDMemory(sizeof(IPSEC_MM_AUTH_INFO) * dwNumExtAuths,
  429. &pExtAuth->pAuthenticationInfo);
  430. BAIL_ON_WIN32_ERROR(dwError);
  431. if (dwCertIndex != INVALID_INDEX) {
  432. pCertInfo=&pExtAuth->pAuthenticationInfo[dwCertIndex].CertAuthInfo;
  433. dwError=AllocateSPDMemory(sizeof(CERT_ROOT_CONFIG) * dwNumCerts,
  434. &pCertInfo->pInboundRootArray);
  435. BAIL_ON_WIN32_ERROR(dwError);
  436. dwError=AllocateSPDMemory(sizeof(CERT_ROOT_CONFIG) * dwNumCerts,
  437. &pCertInfo->pOutboundRootArray);
  438. BAIL_ON_WIN32_ERROR(dwError);
  439. }
  440. for (i=0; i < pIntMMAuthMethods->dwNumAuthInfos; i++) {
  441. pAuthInfo=&pIntMMAuthMethods->pAuthenticationInfo[i];
  442. switch(pAuthInfo->AuthMethod) {
  443. case IKE_PRESHARED_KEY:
  444. pExtAuthInfo=&pExtAuth->pAuthenticationInfo[dwPSKeyIndex];
  445. dwError=AllocateSPDMemory(pAuthInfo->dwAuthInfoSize,
  446. &pExtAuthInfo->GeneralAuthInfo.pAuthInfo);
  447. BAIL_ON_WIN32_ERROR(dwError);
  448. memcpy(pExtAuthInfo->GeneralAuthInfo.pAuthInfo,
  449. pAuthInfo->pAuthInfo,
  450. pAuthInfo->dwAuthInfoSize);
  451. pExtAuthInfo->GeneralAuthInfo.dwAuthInfoSize=pAuthInfo->dwAuthInfoSize;
  452. pExtAuthInfo->AuthMethod=IKE_PRESHARED_KEY;
  453. break;
  454. case IKE_SSPI:
  455. pExtAuthInfo=&pExtAuth->pAuthenticationInfo[dwKerbIndex];
  456. pExtAuthInfo->AuthMethod=IKE_SSPI;
  457. break;
  458. case IKE_RSA_SIGNATURE:
  459. pExtAuthInfo=&pExtAuth->pAuthenticationInfo[dwCertIndex];
  460. pExtAuthInfo->AuthMethod=IKE_RSA_SIGNATURE;
  461. pCertInfo=&pExtAuthInfo->CertAuthInfo;
  462. dwError=AllocateSPDMemory(pAuthInfo->dwAuthInfoSize,
  463. &pCertInfo->pInboundRootArray[dwCurCertIndex].pCertData);
  464. BAIL_ON_WIN32_ERROR(dwError);
  465. memcpy(pCertInfo->pInboundRootArray[dwCurCertIndex].pCertData,
  466. pAuthInfo->pAuthInfo,
  467. pAuthInfo->dwAuthInfoSize);
  468. pCertInfo->pInboundRootArray[dwCurCertIndex].dwCertDataSize=pAuthInfo->dwAuthInfoSize;
  469. pCertInfo->pInboundRootArray[dwCurCertIndex].dwFlags = pAuthInfo->dwAuthFlags;
  470. // Copy same info into *pOutboundRootArray as well.
  471. dwError=AllocateSPDMemory(pAuthInfo->dwAuthInfoSize,
  472. &pCertInfo->pOutboundRootArray[dwCurCertIndex].pCertData);
  473. BAIL_ON_WIN32_ERROR(dwError);
  474. memcpy(pCertInfo->pOutboundRootArray[dwCurCertIndex].pCertData,
  475. pAuthInfo->pAuthInfo,
  476. pAuthInfo->dwAuthInfoSize);
  477. pCertInfo->pOutboundRootArray[dwCurCertIndex].dwCertDataSize=pAuthInfo->dwAuthInfoSize;
  478. pCertInfo->pOutboundRootArray[dwCurCertIndex].dwFlags = pAuthInfo->dwAuthFlags;
  479. dwCurCertIndex++;
  480. break;
  481. }
  482. }
  483. if (dwCertIndex != INVALID_INDEX) {
  484. pExtAuth->pAuthenticationInfo[dwCertIndex].CertAuthInfo.dwInboundRootArraySize = dwCurCertIndex;
  485. pExtAuth->pAuthenticationInfo[dwCertIndex].CertAuthInfo.dwOutboundRootArraySize = dwCurCertIndex;
  486. }
  487. *pMMAuthMethods=pExtAuth;
  488. return ERROR_SUCCESS;
  489. error:
  490. FreeExtMMAuthMethods(pExtAuth);
  491. return (dwError);
  492. }
  493. DWORD WINAPI SPDConvertIntMMAuthToExt(PINT_MM_AUTH_METHODS pIntMMAuthMethods,
  494. PMM_AUTH_METHODS *pMMAuthMethods)
  495. {
  496. PMM_AUTH_METHODS pExtAuth=NULL;
  497. DWORD dwError;
  498. DWORD dwNumExtAuths=0;
  499. DWORD i;
  500. PINT_IPSEC_MM_AUTH_INFO pAuthInfo;
  501. PIPSEC_MM_AUTH_INFO pExtAuthInfo;
  502. PMM_CERT_INFO pCertInfo;
  503. DWORD dwCurCertIndex=0;
  504. PCERT_ROOT_CONFIG pInboundCertArray;
  505. DWORD dwNumCerts=0;
  506. DWORD dwCertIndex=INVALID_INDEX;
  507. DWORD dwPSKeyIndex=INVALID_INDEX;
  508. DWORD dwKerbIndex=INVALID_INDEX;
  509. dwError=SPDApiBufferAllocate(sizeof(MM_AUTH_METHODS),&pExtAuth);
  510. BAIL_ON_WIN32_ERROR(dwError);
  511. memcpy(&pExtAuth->gMMAuthID,&pIntMMAuthMethods->gMMAuthID,sizeof(GUID));
  512. pExtAuth->dwFlags=pIntMMAuthMethods->dwFlags;
  513. for (i=0; i < pIntMMAuthMethods->dwNumAuthInfos; i++) {
  514. pAuthInfo=&pIntMMAuthMethods->pAuthenticationInfo[i];
  515. switch(pAuthInfo->AuthMethod) {
  516. case IKE_PRESHARED_KEY:
  517. dwPSKeyIndex=dwNumExtAuths;
  518. dwNumExtAuths++;
  519. break;
  520. case IKE_SSPI:
  521. dwKerbIndex=dwNumExtAuths;
  522. dwNumExtAuths++;
  523. break;
  524. case IKE_RSA_SIGNATURE:
  525. if (dwCertIndex == INVALID_INDEX) {
  526. dwCertIndex=dwNumExtAuths;
  527. dwNumExtAuths++;
  528. }
  529. dwNumCerts++;
  530. break;
  531. }
  532. }
  533. pExtAuth->dwNumAuthInfos=dwNumExtAuths;
  534. dwError=SPDApiBufferAllocate(sizeof(IPSEC_MM_AUTH_INFO) * dwNumExtAuths,
  535. &pExtAuth->pAuthenticationInfo);
  536. BAIL_ON_WIN32_ERROR(dwError);
  537. if (dwCertIndex != INVALID_INDEX) {
  538. pCertInfo=&pExtAuth->pAuthenticationInfo[dwCertIndex].CertAuthInfo;
  539. dwError=SPDApiBufferAllocate(sizeof(CERT_ROOT_CONFIG) * dwNumCerts,
  540. &pCertInfo->pInboundRootArray);
  541. BAIL_ON_WIN32_ERROR(dwError);
  542. dwError=SPDApiBufferAllocate(sizeof(CERT_ROOT_CONFIG) * dwNumCerts,
  543. &pCertInfo->pOutboundRootArray);
  544. BAIL_ON_WIN32_ERROR(dwError);
  545. }
  546. for (i=0; i < pIntMMAuthMethods->dwNumAuthInfos; i++) {
  547. pAuthInfo=&pIntMMAuthMethods->pAuthenticationInfo[i];
  548. switch(pAuthInfo->AuthMethod) {
  549. case IKE_PRESHARED_KEY:
  550. pExtAuthInfo=&pExtAuth->pAuthenticationInfo[dwPSKeyIndex];
  551. dwError=SPDApiBufferAllocate(pAuthInfo->dwAuthInfoSize,
  552. &pExtAuthInfo->GeneralAuthInfo.pAuthInfo);
  553. BAIL_ON_WIN32_ERROR(dwError);
  554. memcpy(pExtAuthInfo->GeneralAuthInfo.pAuthInfo,
  555. pAuthInfo->pAuthInfo,
  556. pAuthInfo->dwAuthInfoSize);
  557. pExtAuthInfo->GeneralAuthInfo.dwAuthInfoSize=pAuthInfo->dwAuthInfoSize;
  558. pExtAuthInfo->AuthMethod=IKE_PRESHARED_KEY;
  559. break;
  560. case IKE_SSPI:
  561. pExtAuthInfo=&pExtAuth->pAuthenticationInfo[dwKerbIndex];
  562. pExtAuthInfo->AuthMethod=IKE_SSPI;
  563. break;
  564. case IKE_RSA_SIGNATURE:
  565. pExtAuthInfo=&pExtAuth->pAuthenticationInfo[dwCertIndex];
  566. pExtAuthInfo->AuthMethod=IKE_RSA_SIGNATURE;
  567. pCertInfo=&pExtAuthInfo->CertAuthInfo;
  568. dwError=SPDApiBufferAllocate(pAuthInfo->dwAuthInfoSize,
  569. &pCertInfo->pInboundRootArray[dwCurCertIndex].pCertData);
  570. BAIL_ON_WIN32_ERROR(dwError);
  571. memcpy(pCertInfo->pInboundRootArray[dwCurCertIndex].pCertData,
  572. pAuthInfo->pAuthInfo,
  573. pAuthInfo->dwAuthInfoSize);
  574. pCertInfo->pInboundRootArray[dwCurCertIndex].dwCertDataSize=pAuthInfo->dwAuthInfoSize;
  575. pCertInfo->pInboundRootArray[dwCurCertIndex].dwFlags = pAuthInfo->dwAuthFlags;
  576. // Copy same info into *pOutboundRootArray as well.
  577. dwError=SPDApiBufferAllocate(pAuthInfo->dwAuthInfoSize,
  578. &pCertInfo->pOutboundRootArray[dwCurCertIndex].pCertData);
  579. BAIL_ON_WIN32_ERROR(dwError);
  580. memcpy(pCertInfo->pOutboundRootArray[dwCurCertIndex].pCertData,
  581. pAuthInfo->pAuthInfo,
  582. pAuthInfo->dwAuthInfoSize);
  583. pCertInfo->pOutboundRootArray[dwCurCertIndex].dwCertDataSize=pAuthInfo->dwAuthInfoSize;
  584. pCertInfo->pOutboundRootArray[dwCurCertIndex].dwFlags = pAuthInfo->dwAuthFlags;
  585. dwCurCertIndex++;
  586. break;
  587. }
  588. }
  589. if (dwCertIndex != INVALID_INDEX) {
  590. pExtAuth->pAuthenticationInfo[dwCertIndex].CertAuthInfo.dwInboundRootArraySize = dwCurCertIndex;
  591. pExtAuth->pAuthenticationInfo[dwCertIndex].CertAuthInfo.dwOutboundRootArraySize = dwCurCertIndex;
  592. }
  593. *pMMAuthMethods=pExtAuth;
  594. return ERROR_SUCCESS;
  595. error:
  596. SPDFreeExtMMAuthMethods(pExtAuth);
  597. return (dwError);
  598. }
  599. DWORD
  600. WINAPI
  601. SPDConvertArrayIntMMAuthToExt(
  602. PINT_MM_AUTH_METHODS pIntMMAuthMethods,
  603. PMM_AUTH_METHODS *ppMMAuthMethods,
  604. DWORD dwNumAuthMeths)
  605. {
  606. DWORD dwError = ERROR_SUCCESS;
  607. DWORD dwConvertIdx = 0;
  608. PMM_AUTH_METHODS *ppTempAuthMethods = NULL;
  609. PMM_AUTH_METHODS pExtAuthMethods = NULL;
  610. dwError = SPDApiBufferAllocate(
  611. dwNumAuthMeths * sizeof(MM_AUTH_METHODS),
  612. &pExtAuthMethods
  613. );
  614. BAIL_ON_WIN32_ERROR(dwError);
  615. dwError = SPDApiBufferAllocate(
  616. dwNumAuthMeths * sizeof(PMM_AUTH_METHODS),
  617. (LPVOID *) &ppTempAuthMethods
  618. );
  619. BAIL_ON_WIN32_ERROR(dwError);
  620. for (dwConvertIdx = 0; dwConvertIdx < dwNumAuthMeths; dwConvertIdx++) {
  621. dwError = SPDConvertIntMMAuthToExt(&pIntMMAuthMethods[dwConvertIdx],
  622. &ppTempAuthMethods[dwConvertIdx]);
  623. BAIL_ON_WIN32_ERROR(dwError);
  624. memcpy(
  625. &pExtAuthMethods[dwConvertIdx],
  626. ppTempAuthMethods[dwConvertIdx],
  627. sizeof(MM_AUTH_METHODS)
  628. );
  629. }
  630. // Shallow free ppTempAuthMethods[i] (pExtAuthMethods has copies.)
  631. for (dwConvertIdx = 0; dwConvertIdx < dwNumAuthMeths; dwConvertIdx++) {
  632. SPDApiBufferFree(ppTempAuthMethods[dwConvertIdx]);
  633. }
  634. SPDApiBufferFree(ppTempAuthMethods);
  635. *ppMMAuthMethods = pExtAuthMethods;
  636. return dwError;
  637. error:
  638. // ASSERT: dwConvertIdx == number of internal auth methods successfully
  639. // converted.
  640. for (; dwConvertIdx ; dwConvertIdx--) {
  641. SPDFreeExtMMAuthMethods(ppTempAuthMethods[dwConvertIdx-1]);
  642. }
  643. SPDApiBufferFree(ppTempAuthMethods);
  644. SPDApiBufferFree(pExtAuthMethods);
  645. *ppMMAuthMethods = NULL;
  646. return dwError;
  647. }
  648. DWORD WINAPI FreeIntMMAuthMethods(PINT_MM_AUTH_METHODS pIntMMAuthMethods)
  649. {
  650. DWORD i;
  651. PINT_IPSEC_MM_AUTH_INFO pIntAuthInfo;
  652. if (pIntMMAuthMethods) {
  653. if (pIntMMAuthMethods->pAuthenticationInfo) {
  654. for (i=0; i < pIntMMAuthMethods->dwNumAuthInfos; i++) {
  655. pIntAuthInfo=&pIntMMAuthMethods->pAuthenticationInfo[i];
  656. FreeSPDMemory(pIntAuthInfo->pAuthInfo);
  657. }
  658. FreeSPDMemory(pIntMMAuthMethods->pAuthenticationInfo);
  659. }
  660. FreeSPDMemory(pIntMMAuthMethods);
  661. }
  662. return ERROR_SUCCESS;
  663. }
  664. DWORD
  665. WINAPI
  666. SPDFreeIntMMAuthMethods(
  667. PINT_MM_AUTH_METHODS pIntMMAuthMethods,
  668. BOOLEAN FreeTop)
  669. {
  670. DWORD i;
  671. PINT_IPSEC_MM_AUTH_INFO pIntAuthInfo;
  672. if (pIntMMAuthMethods) {
  673. if (pIntMMAuthMethods->pAuthenticationInfo) {
  674. for (i=0; i < pIntMMAuthMethods->dwNumAuthInfos; i++) {
  675. pIntAuthInfo=&pIntMMAuthMethods->pAuthenticationInfo[i];
  676. SPDApiBufferFree(pIntAuthInfo->pAuthInfo);
  677. }
  678. SPDApiBufferFree(pIntMMAuthMethods->pAuthenticationInfo);
  679. }
  680. if (FreeTop)
  681. SPDApiBufferFree(pIntMMAuthMethods);
  682. }
  683. return ERROR_SUCCESS;
  684. }
  685. DWORD
  686. WINAPI
  687. SPDFreeIntMMAuthMethodsArray(
  688. PINT_MM_AUTH_METHODS pIntMMAuthMethods,
  689. DWORD dwNumAuthMeths)
  690. {
  691. DWORD Idx;
  692. for (Idx = 0; Idx < dwNumAuthMeths; Idx++) {
  693. SPDFreeIntMMAuthMethods(&pIntMMAuthMethods[Idx], FALSE);
  694. }
  695. SPDApiBufferFree(pIntMMAuthMethods);
  696. return ERROR_SUCCESS;
  697. }
  698. VOID FreeMMAuthInfo(PIPSEC_MM_AUTH_INFO pAuthInfo)
  699. {
  700. PMM_CERT_INFO pCertInfo;
  701. DWORD i;
  702. if (pAuthInfo->AuthMethod == IKE_PRESHARED_KEY) {
  703. FreeSPDMemory(pAuthInfo->GeneralAuthInfo.pAuthInfo);
  704. }
  705. if (pAuthInfo->AuthMethod == IKE_RSA_SIGNATURE) {
  706. pCertInfo=&pAuthInfo->CertAuthInfo;
  707. if (pCertInfo->pInboundRootArray) {
  708. for (i=0; i < pCertInfo->dwInboundRootArraySize; i++) {
  709. FreeSPDMemory(pCertInfo->pInboundRootArray[i].pCertData);
  710. // The following is unused, but included for completeness
  711. FreeSPDMemory(pCertInfo->pInboundRootArray[i].pAuthorizationData);
  712. }
  713. FreeSPDMemory(pCertInfo->pInboundRootArray);
  714. }
  715. if (pCertInfo->pOutboundRootArray) {
  716. for (i=0; i < pCertInfo->dwOutboundRootArraySize; i++) {
  717. FreeSPDMemory(pCertInfo->pOutboundRootArray[i].pCertData);
  718. // The following is unused, but included for completeness
  719. FreeSPDMemory(pCertInfo->pOutboundRootArray[i].pAuthorizationData);
  720. }
  721. FreeSPDMemory(pCertInfo->pOutboundRootArray);
  722. }
  723. // The following is unused.
  724. FreeSPDMemory(pCertInfo->pMyCertHash);
  725. }
  726. }
  727. VOID SPDFreeMMAuthInfo(PIPSEC_MM_AUTH_INFO pAuthInfo)
  728. {
  729. PMM_CERT_INFO pCertInfo;
  730. DWORD i;
  731. if (pAuthInfo->AuthMethod == IKE_PRESHARED_KEY) {
  732. SPDApiBufferFree(pAuthInfo->GeneralAuthInfo.pAuthInfo);
  733. }
  734. if (pAuthInfo->AuthMethod == IKE_RSA_SIGNATURE) {
  735. pCertInfo=&pAuthInfo->CertAuthInfo;
  736. if (pCertInfo->pInboundRootArray) {
  737. for (i=0; i < pCertInfo->dwInboundRootArraySize; i++) {
  738. SPDApiBufferFree(pCertInfo->pInboundRootArray[i].pCertData);
  739. // The following is unused, but included for completeness
  740. SPDApiBufferFree(pCertInfo->pInboundRootArray[i].pAuthorizationData);
  741. }
  742. SPDApiBufferFree(pCertInfo->pInboundRootArray);
  743. }
  744. if (pCertInfo->pOutboundRootArray) {
  745. for (i=0; i < pCertInfo->dwOutboundRootArraySize; i++) {
  746. SPDApiBufferFree(pCertInfo->pOutboundRootArray[i].pCertData);
  747. // The following is unused, but included for completeness
  748. SPDApiBufferFree(pCertInfo->pOutboundRootArray[i].pAuthorizationData);
  749. }
  750. SPDApiBufferFree(pCertInfo->pOutboundRootArray);
  751. }
  752. // The following is unused.
  753. SPDApiBufferFree(pCertInfo->pMyCertHash);
  754. }
  755. }
  756. DWORD WINAPI FreeExtMMAuthMethods(PMM_AUTH_METHODS pMMAuthMethods)
  757. {
  758. DWORD i;
  759. PIPSEC_MM_AUTH_INFO pAuthInfo;
  760. if (pMMAuthMethods) {
  761. if (pMMAuthMethods->pAuthenticationInfo) {
  762. for (i=0; i < pMMAuthMethods->dwNumAuthInfos; i++) {
  763. pAuthInfo=&pMMAuthMethods->pAuthenticationInfo[i];
  764. FreeMMAuthInfo(pAuthInfo);
  765. }
  766. FreeSPDMemory(pMMAuthMethods->pAuthenticationInfo);
  767. }
  768. FreeSPDMemory(pMMAuthMethods);
  769. }
  770. return ERROR_SUCCESS;
  771. }
  772. DWORD WINAPI SPDFreeExtMMAuthMethods(PMM_AUTH_METHODS pMMAuthMethods)
  773. {
  774. DWORD i;
  775. PIPSEC_MM_AUTH_INFO pAuthInfo;
  776. if (pMMAuthMethods) {
  777. if (pMMAuthMethods->pAuthenticationInfo) {
  778. for (i=0; i < pMMAuthMethods->dwNumAuthInfos; i++) {
  779. pAuthInfo=&pMMAuthMethods->pAuthenticationInfo[i];
  780. SPDFreeMMAuthInfo(pAuthInfo);
  781. }
  782. SPDApiBufferFree(pMMAuthMethods->pAuthenticationInfo);
  783. }
  784. SPDApiBufferFree(pMMAuthMethods);
  785. }
  786. return ERROR_SUCCESS;
  787. }
  788. BOOLEAN
  789. IsSpecialServ(
  790. ADDR_TYPE AddrType
  791. )
  792. {
  793. switch (AddrType) {
  794. case IP_ADDR_DNS_SERVER:
  795. case IP_ADDR_WINS_SERVER:
  796. case IP_ADDR_DHCP_SERVER:
  797. case IP_ADDR_DEFAULT_GATEWAY:
  798. return TRUE;
  799. default:
  800. return FALSE;
  801. }
  802. }
  803. ADDR_TYPE
  804. ExTypeToAddrType(
  805. UCHAR ExType
  806. )
  807. {
  808. ExType &= ~EXT_DEST;
  809. switch (ExType) {
  810. case EXT_DNS_SERVER:
  811. return IP_ADDR_DNS_SERVER;
  812. case EXT_WINS_SERVER:
  813. return IP_ADDR_WINS_SERVER;
  814. case EXT_DHCP_SERVER:
  815. return IP_ADDR_DHCP_SERVER;
  816. case EXT_DEFAULT_GATEWAY:
  817. return IP_ADDR_DEFAULT_GATEWAY;
  818. }
  819. return IP_ADDR_UNIQUE;
  820. }