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.

460 lines
9.2 KiB

  1. /**********************************************************************/
  2. /** Microsoft Windows/NT **/
  3. /** Copyright(c) Microsoft Corporation, 1997 - 1999 **/
  4. /**********************************************************************/
  5. /*
  6. spdutil.cpp
  7. FILE HISTORY:
  8. */
  9. #include "stdafx.h"
  10. #include "winipsec.h"
  11. #include "spdutil.h"
  12. #include "objplus.h"
  13. #include "ipaddres.h"
  14. #include "spddb.h"
  15. #include "server.h"
  16. extern CHashTable g_HashTable;
  17. const DWORD IPSM_PROTOCOL_TCP = 6;
  18. const DWORD IPSM_PROTOCOL_UDP = 17;
  19. const TCHAR c_szSingleAddressMask[] = _T("255.255.255.255");
  20. const ProtocolStringMap c_ProtocolStringMap[] =
  21. {
  22. {0, IDS_PROTOCOL_ANY},
  23. {1, IDS_PROTOCOL_ICMP},
  24. {3, IDS_PROTOCOL_GGP},
  25. {6, IDS_PROTOCOL_TCP},
  26. {8, IDS_PROTOCOL_EGP},
  27. {12, IDS_PROTOCOL_PUP},
  28. {17, IDS_PROTOCOL_UDP},
  29. {20, IDS_PROTOCOL_HMP},
  30. {22, IDS_PROTOCOL_XNS_IDP},
  31. {27, IDS_PROTOCOL_RDP},
  32. {66, IDS_PROTOCOL_RVD}
  33. };
  34. const int c_nProtocols = DimensionOf(c_ProtocolStringMap);
  35. ULONG RevertDwordBytes(DWORD dw)
  36. {
  37. ULONG ulRet;
  38. ulRet = dw >> 24;
  39. ulRet += (dw & 0xFF0000) >> 8;
  40. ulRet += (dw & 0x00FF00) << 8;
  41. ulRet += (dw & 0x0000FF) << 24;
  42. return ulRet;
  43. }
  44. void PortToString
  45. (
  46. PORT port,
  47. CString * pst
  48. )
  49. {
  50. if (0 == port.wPort)
  51. {
  52. pst->LoadString(IDS_PORT_ANY);
  53. }
  54. else
  55. {
  56. pst->Format(_T("%d"), port.wPort);
  57. }
  58. }
  59. void FilterFlagToString
  60. (
  61. FILTER_FLAG FltrFlag,
  62. CString * pst
  63. )
  64. {
  65. pst->Empty();
  66. switch(FltrFlag)
  67. {
  68. case PASS_THRU:
  69. pst->LoadString(IDS_PASS_THROUGH);
  70. break;
  71. case BLOCKING:
  72. pst->LoadString(IDS_BLOCKING);
  73. break;
  74. case NEGOTIATE_SECURITY:
  75. pst->LoadString(IDS_NEG_SEC);
  76. break;
  77. }
  78. }
  79. void ProtocolToString
  80. (
  81. PROTOCOL protocol,
  82. CString * pst
  83. )
  84. {
  85. BOOL fFound = FALSE;
  86. for (int i = 0; i < DimensionOf(c_ProtocolStringMap); i++)
  87. {
  88. if (c_ProtocolStringMap[i].dwProtocol == protocol.dwProtocol)
  89. {
  90. pst->LoadString(c_ProtocolStringMap[i].nStringID);
  91. fFound = TRUE;
  92. }
  93. }
  94. if (!fFound)
  95. {
  96. pst->Format(IDS_OTHER_PROTO, protocol.dwProtocol);
  97. }
  98. }
  99. void InterfaceTypeToString
  100. (
  101. IF_TYPE ifType,
  102. CString * pst
  103. )
  104. {
  105. switch (ifType)
  106. {
  107. case INTERFACE_TYPE_ALL:
  108. pst->LoadString (IDS_IF_TYPE_ALL);
  109. break;
  110. case INTERFACE_TYPE_LAN:
  111. pst->LoadString (IDS_IF_TYPE_LAN);
  112. break;
  113. case INTERFACE_TYPE_DIALUP:
  114. pst->LoadString (IDS_IF_TYPE_RAS);
  115. break;
  116. default:
  117. pst->LoadString (IDS_UNKNOWN);
  118. break;
  119. }
  120. }
  121. void BoolToString
  122. (
  123. BOOL bl,
  124. CString * pst
  125. )
  126. {
  127. if (bl)
  128. pst->LoadString (IDS_YES);
  129. else
  130. pst->LoadString (IDS_NO);
  131. }
  132. void DirectionToString
  133. (
  134. DWORD dwDir,
  135. CString * pst
  136. )
  137. {
  138. switch (dwDir)
  139. {
  140. case FILTER_DIRECTION_INBOUND:
  141. pst->LoadString(IDS_FLTR_DIR_IN);
  142. break;
  143. case FILTER_DIRECTION_OUTBOUND:
  144. pst->LoadString(IDS_FLTR_DIR_OUT);
  145. break;
  146. default:
  147. pst->Empty();
  148. break;
  149. }
  150. }
  151. void DoiEspAlgorithmToString
  152. (
  153. IPSEC_MM_ALGO algo,
  154. CString * pst
  155. )
  156. {
  157. switch (algo.uAlgoIdentifier)
  158. {
  159. case IPSEC_DOI_ESP_NONE:
  160. pst->LoadString(IDS_DOI_ESP_NONE);
  161. break;
  162. case IPSEC_DOI_ESP_DES:
  163. pst->LoadString(IDS_DOI_ESP_DES);
  164. break;
  165. case IPSEC_DOI_ESP_3_DES:
  166. pst->LoadString(IDS_DOI_ESP_3_DES);
  167. break;
  168. default:
  169. pst->Empty();
  170. break;
  171. }
  172. }
  173. void DoiAuthAlgorithmToString
  174. (
  175. IPSEC_MM_ALGO algo,
  176. CString * pst
  177. )
  178. {
  179. switch(algo.uAlgoIdentifier)
  180. {
  181. case IPSEC_DOI_AH_NONE:
  182. pst->LoadString(IDS_DOI_AH_NONE);
  183. break;
  184. case IPSEC_DOI_AH_MD5:
  185. pst->LoadString(IDS_DOI_AH_MD5);
  186. break;
  187. case IPSEC_DOI_AH_SHA1:
  188. pst->LoadString(IDS_DOI_AH_SHA);
  189. break;
  190. default:
  191. pst->Empty();
  192. break;
  193. }
  194. }
  195. void DhGroupToString(DWORD dwGp, CString * pst)
  196. {
  197. switch(dwGp)
  198. {
  199. case 1:
  200. pst->LoadString(IDS_DHGROUP_LOW);
  201. break;
  202. case 2:
  203. pst->LoadString(IDS_DHGROUP_MEDIUM);
  204. break;
  205. case 3:
  206. pst->LoadString(IDS_DHGROUP_HIGH);
  207. break;
  208. default:
  209. pst->Format(_T("%d"), dwGp);
  210. break;
  211. }
  212. }
  213. void MmAuthToString(MM_AUTH_ENUM auth, CString * pst)
  214. {
  215. switch(auth)
  216. {
  217. case IKE_PRESHARED_KEY:
  218. pst->LoadString(IDS_IKE_PRESHARED_KEY);
  219. break;
  220. case IKE_DSS_SIGNATURE:
  221. pst->LoadString(IDS_IKE_DSS_SIGNATURE);
  222. break;
  223. case IKE_RSA_SIGNATURE:
  224. pst->LoadString(IDS_IKE_RSA_SIGNATURE);
  225. break;
  226. case IKE_RSA_ENCRYPTION:
  227. pst->LoadString(IDS_IKE_RSA_ENCRYPTION);
  228. break;
  229. case IKE_SSPI:
  230. pst->LoadString(IDS_IKE_SSPI);
  231. break;
  232. default:
  233. pst->Empty();
  234. break;
  235. }
  236. }
  237. void KeyLifetimeToString(KEY_LIFETIME lifetime, CString * pst)
  238. {
  239. pst->Format(IDS_KEY_LIFE_TIME, lifetime.uKeyExpirationKBytes, lifetime.uKeyExpirationTime);
  240. }
  241. void IpToString(ULONG ulIp, CString *pst)
  242. {
  243. ULONG ul;
  244. CIpAddress ipAddr;
  245. ul = RevertDwordBytes(ulIp);
  246. ipAddr = ul;
  247. *pst = (CString) ipAddr;
  248. }
  249. void AddressToString(ADDR addr, CString * pst, BOOL * pfIsDnsName)
  250. {
  251. Assert(pst);
  252. if (NULL == pst)
  253. return;
  254. if (pfIsDnsName)
  255. {
  256. *pfIsDnsName = FALSE;
  257. }
  258. ULONG ul;
  259. CIpAddress ipAddr;
  260. pst->Empty();
  261. switch (addr.AddrType)
  262. {
  263. case IP_ADDR_UNIQUE:
  264. if (IP_ADDRESS_ME == addr.uIpAddr)
  265. {
  266. pst->LoadString(IDS_ADDR_ME);
  267. }
  268. else
  269. {
  270. HashEntry *pHashEntry=NULL;
  271. if (g_HashTable.GetObject(&pHashEntry,*(in_addr*)&addr.uIpAddr) != ERROR_SUCCESS) {
  272. ul = RevertDwordBytes(addr.uIpAddr);
  273. ipAddr = ul;
  274. *pst = (CString) ipAddr;
  275. }
  276. else
  277. {
  278. *pst=pHashEntry->HostName;
  279. if (pfIsDnsName)
  280. {
  281. *pfIsDnsName = TRUE;
  282. }
  283. }
  284. }
  285. break;
  286. case IP_ADDR_SUBNET:
  287. if (SUBNET_ADDRESS_ANY == addr.uSubNetMask)
  288. {
  289. pst->LoadString(IDS_ADDR_ANY);
  290. }
  291. else
  292. {
  293. ul = RevertDwordBytes(addr.uIpAddr);
  294. ipAddr = ul;
  295. *pst = (CString) ipAddr;
  296. *pst += _T("(");
  297. ul = RevertDwordBytes(addr.uSubNetMask);
  298. ipAddr = ul;
  299. *pst += (CString) ipAddr;
  300. *pst += _T(")");
  301. }
  302. break;
  303. }
  304. }
  305. void IpsecByteBlobToString(const IPSEC_BYTE_BLOB& blob, CString * pst)
  306. {
  307. Assert(pst);
  308. if (NULL == pst)
  309. return;
  310. pst->Empty();
  311. //TODO to translate the blob info to readable strings
  312. }
  313. void QmAlgorithmToString
  314. (
  315. QM_ALGO_TYPE type,
  316. CQmOffer * pOffer,
  317. CString * pst
  318. )
  319. {
  320. Assert(pst);
  321. Assert(pOffer);
  322. if (NULL == pst || NULL == pOffer)
  323. return;
  324. pst->LoadString(IDS_ALGO_NONE);
  325. for (DWORD i = 0; i < pOffer->m_dwNumAlgos; i++)
  326. {
  327. switch(type)
  328. {
  329. case QM_ALGO_AUTH:
  330. if (AUTHENTICATION == pOffer->m_arrAlgos[i].m_Operation)
  331. {
  332. switch(pOffer->m_arrAlgos[i].m_ulAlgo)
  333. {
  334. case IPSEC_DOI_AH_MD5:
  335. pst->LoadString(IDS_DOI_AH_MD5);
  336. break;
  337. case IPSEC_DOI_AH_SHA1:
  338. pst->LoadString(IDS_DOI_AH_SHA);
  339. break;
  340. }
  341. }
  342. break;
  343. case QM_ALGO_ESP_CONF:
  344. if (ENCRYPTION == pOffer->m_arrAlgos[i].m_Operation)
  345. {
  346. switch(pOffer->m_arrAlgos[i].m_ulAlgo)
  347. {
  348. case IPSEC_DOI_ESP_DES:
  349. pst->LoadString(IDS_DOI_ESP_DES);
  350. break;
  351. case IPSEC_DOI_ESP_3_DES:
  352. pst->LoadString(IDS_DOI_ESP_3_DES);
  353. break;
  354. }
  355. }
  356. break;
  357. case QM_ALGO_ESP_INTEG:
  358. if (ENCRYPTION == pOffer->m_arrAlgos[i].m_Operation)
  359. {
  360. switch(pOffer->m_arrAlgos[i].m_SecAlgo)
  361. {
  362. case HMAC_AH_MD5:
  363. pst->LoadString(IDS_HMAC_AH_MD5);
  364. break;
  365. case HMAC_AH_SHA1:
  366. pst->LoadString(IDS_HMAC_AH_SHA);
  367. break;
  368. }
  369. }
  370. break;
  371. }
  372. }
  373. }
  374. void TnlEpToString
  375. (
  376. QM_FILTER_TYPE FltrType,
  377. ADDR TnlEp,
  378. CString * pst
  379. )
  380. {
  381. Assert(pst);
  382. if (NULL == pst)
  383. return;
  384. if (QM_TUNNEL_FILTER == FltrType)
  385. {
  386. AddressToString(TnlEp, pst);
  387. }
  388. else
  389. {
  390. pst->LoadString(IDS_NOT_AVAILABLE);
  391. }
  392. }
  393. void TnlEpToString
  394. (
  395. FILTER_TYPE FltrType,
  396. ADDR TnlEp,
  397. CString * pst
  398. )
  399. {
  400. Assert(pst);
  401. if (NULL == pst)
  402. return;
  403. if (FILTER_TYPE_TUNNEL == FltrType)
  404. {
  405. AddressToString(TnlEp, pst);
  406. }
  407. else
  408. {
  409. pst->LoadString(IDS_NOT_AVAILABLE);
  410. }
  411. }