plugin_win_ipsec_vista.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. /* Copyright (C) 2013-2014 Mamadou DIOP
  2. * Copyright (C) 2013-2014 Doubango Telecom <http://www.doubango.org>
  3. *
  4. * This file is part of Open Source Doubango Framework.
  5. *
  6. * DOUBANGO is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * DOUBANGO is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with DOUBANGO.
  18. */
  19. #include "plugin_win_ipsec_vista_config.h"
  20. #include "tipsec.h" /* From tinyIPSec project. Requires linking against "tinyIPSec.lib" */
  21. #include "tsk_memory.h"
  22. #include "tsk_object.h"
  23. #include "tsk_debug.h"
  24. #include <ws2tcpip.h>
  25. #include <Fwpmu.h>
  26. #include <Rpc.h>
  27. #if defined(_MSC_VER)
  28. # pragma comment(lib, "Fwpuclnt.lib")
  29. # pragma comment(lib, "Rpcrt4.lib")
  30. #endif
  31. typedef FWP_BYTE_BLOB* PFWP_BYTE_BLOB;
  32. /* as WFP do not provide null encryption I define my own*/
  33. static const IPSEC_CIPHER_TRANSFORM_ID0 IPSEC_CIPHER_TRANSFORM_ID_NULL_NULL= {
  34. (IPSEC_CIPHER_TYPE)NULL,
  35. (IPSEC_CIPHER_TYPE)NULL
  36. };
  37. #define TINYIPSEC_FILTER_NAME TEXT("Doubango Telecom tinyIPSec (Windows Vista)")
  38. #define TINYIPSEC_PROVIDER_KEY NULL
  39. #define TINYIPSEC_SA_NUM_ENTRIES_TO_REQUEST INT_MAX
  40. #define TINYIPSEC_SA_MAX_LIFETIME 172799
  41. #define TINYIPSEC_VISTA_GET_ALGO(algo) (algo == tipsec_alg_hmac_md5_96) ? IPSEC_AUTH_TRANSFORM_ID_HMAC_MD5_96 : IPSEC_AUTH_TRANSFORM_ID_HMAC_SHA_1_96
  42. #define TINYIPSEC_VISTA_GET_EALGO(ealg) (ealg == tipsec_ealg_des_ede3_cbc) ? IPSEC_CIPHER_TRANSFORM_ID_CBC_3DES : ( (ealg == tipsec_ealg_aes) ? IPSEC_CIPHER_TRANSFORM_ID_AES_128 : IPSEC_CIPHER_TRANSFORM_ID_NULL_NULL )
  43. #define TINYIPSEC_VISTA_GET_MODE(mode) (mode == tipsec_mode_tun) ? IPSEC_TRAFFIC_TYPE_TUNNEL : IPSEC_TRAFFIC_TYPE_TRANSPORT
  44. #define TINYIPSEC_VISTA_GET_IPPROTO(ipproto) (ipproto == tipsec_ipproto_tcp) ? IPPROTO_TCP : ((ipproto == tipsec_ipproto_icmp) ? IPPROTO_ICMP : IPPROTO_UDP)
  45. #define TINYIPSEC_VISTA_GET_IPVER(ipv6) (ipv6) ? FWP_IP_VERSION_V6 : FWP_IP_VERSION_V4
  46. #define TINYIPSEC_VISTA_GET_PROTO(proto, ealg) (proto == tipsec_proto_ah) ? IPSEC_TRANSFORM_AH : ( (proto == tipsec_proto_esp) ? (ealg == tipsec_ealg_null ? IPSEC_TRANSFORM_ESP_AUTH : IPSEC_TRANSFORM_ESP_AUTH_AND_CIPHER) : IPSEC_TRANSFORM_ESP_AUTH_AND_CIPHER );
  47. typedef struct plugin_win_ipsec_vista_ctx_s {
  48. TIPSEC_DECLARE_CTX;
  49. tipsec_ctx_t* pc_base;
  50. UINT64 saId_us;
  51. UINT64 saId_uc;
  52. UINT64 filterId_in_us;
  53. UINT64 filterId_out_us;
  54. UINT64 filterId_in_uc;
  55. UINT64 filterId_out_uc;
  56. WCHAR filter_name[256];
  57. HANDLE engine;
  58. }
  59. plugin_win_ipsec_vista_ctx_t;
  60. static int _vista_createLocalSA(__in const plugin_win_ipsec_vista_ctx_t* p_ctx, __in tipsec_port_t local_port, __out tipsec_spi_t *spi, __out UINT64 *saId, __out UINT64 *filterId_in, __out UINT64 *filterId_out);
  61. static int _vista_boundSA(__in const plugin_win_ipsec_vista_ctx_t* p_ctx, __in UINT64 local_saId, __in tipsec_spi_t remote_spi, __in BOOLEAN toInbound);
  62. static int _vista_flushAll(const plugin_win_ipsec_vista_ctx_t* p_ctx);
  63. static void _vista_deleteSaContextAndFilters(__in HANDLE engine, __in UINT64 inFilterId, __in UINT64 outFilterId, __in UINT64 saId);
  64. //
  65. // Plugin implementation
  66. //
  67. static tipsec_error_t _plugin_win_ipsec_vista_ctx_init(tipsec_ctx_t* _p_ctx)
  68. {
  69. plugin_win_ipsec_vista_ctx_t* p_ctx = (plugin_win_ipsec_vista_ctx_t*)_p_ctx;
  70. DWORD code;
  71. UUID uuid;
  72. RPC_STATUS status;
  73. static uint64_t __guard = 0;
  74. if (p_ctx->pc_base->initialized) {
  75. TSK_DEBUG_ERROR("Already initialized");
  76. return tipsec_error_invalid_state;
  77. }
  78. /* Create filter name */
  79. status = UuidCreate(&uuid);
  80. if (status == RPC_S_OK) {
  81. WCHAR* wszUuid = NULL;
  82. UuidToStringW(&uuid, (RPC_WSTR*)&wszUuid);
  83. if (!wszUuid) {
  84. TSK_DEBUG_ERROR("Failed to convert the UUID");
  85. return tipsec_error_sys;
  86. }
  87. swprintf(p_ctx->filter_name, sizeof(p_ctx->filter_name)/sizeof(p_ctx->filter_name[0]), L"%s//%s//%llu", TINYIPSEC_FILTER_NAME, wszUuid, __guard++);
  88. RpcStringFree((RPC_WSTR*)&wszUuid);
  89. }
  90. else {
  91. TSK_DEBUG_ERROR("Failed to create new UUID");
  92. return tipsec_error_sys;
  93. }
  94. /* Open engine */
  95. if ((code = FwpmEngineOpen0(NULL, RPC_C_AUTHN_WINNT, NULL, NULL, &p_ctx->engine))) {
  96. p_ctx->pc_base->initialized = tsk_false;
  97. TSK_DEBUG_ERROR("FwpmEngineOpen0 failed with error code [%x].", code);
  98. return tipsec_error_sys;
  99. }
  100. else {
  101. p_ctx->pc_base->initialized = tsk_true;
  102. p_ctx->pc_base->state = tipsec_state_initial;
  103. return tipsec_error_success;
  104. }
  105. }
  106. static tipsec_error_t _plugin_win_ipsec_vista_ctx_set_local(tipsec_ctx_t* _p_ctx, const char* addr_local, const char* addr_remote, tipsec_port_t port_uc, tipsec_port_t port_us)
  107. {
  108. plugin_win_ipsec_vista_ctx_t* p_ctx = (plugin_win_ipsec_vista_ctx_t*)_p_ctx;
  109. int ret;
  110. _p_ctx->addr_local = tsk_realloc(_p_ctx->addr_local, _p_ctx->use_ipv6 ? 16 : 4);
  111. if (!_p_ctx->addr_local) {
  112. return tipsec_error_outofmemory;
  113. }
  114. _p_ctx->addr_remote = tsk_realloc(_p_ctx->addr_remote, _p_ctx->use_ipv6 ? 16 : 4);
  115. if (!_p_ctx->addr_remote) {
  116. return tipsec_error_outofmemory;
  117. }
  118. /* Set local IP */
  119. if (_p_ctx->use_ipv6) {
  120. if ((ret = inet_pton(AF_INET6, addr_local, _p_ctx->addr_local)) != 1 ) {
  121. TSK_DEBUG_ERROR("inet_pton(%s) have failed with error code [%x].", addr_local, ret);
  122. return tipsec_error_sys;
  123. }
  124. if ((ret = inet_pton(AF_INET6, addr_remote, _p_ctx->addr_remote)) != 1 ) {
  125. TSK_DEBUG_ERROR("inet_pton(%s) have failed with error code [%x].", addr_remote, ret);
  126. return tipsec_error_sys;
  127. }
  128. }
  129. else {
  130. if ((ret = inet_pton(AF_INET, addr_local, _p_ctx->addr_local)) != 1 ) {
  131. TSK_DEBUG_ERROR("inet_pton(%s) have failed with error code [%x].", addr_local, ret);
  132. return tipsec_error_sys;
  133. }
  134. else {
  135. *((UINT32*)_p_ctx->addr_local) = ntohl(*((UINT32*)_p_ctx->addr_local));
  136. }
  137. if ((ret = inet_pton(AF_INET, addr_remote, _p_ctx->addr_remote)) != 1 ) {
  138. TSK_DEBUG_ERROR("inet_pton(%s) have failed with error code [%x].", addr_remote, ret);
  139. return tipsec_error_sys;
  140. }
  141. else {
  142. *((UINT32*)_p_ctx->addr_remote) = ntohl(*((UINT32*)_p_ctx->addr_remote));
  143. }
  144. }
  145. /* Set ports */
  146. _p_ctx->port_uc = port_uc;
  147. _p_ctx->port_us = port_us;
  148. // Create SA1: (UC -> PS)
  149. if ((ret = _vista_createLocalSA(p_ctx, _p_ctx->port_uc, &_p_ctx->spi_uc, &p_ctx->saId_uc, &p_ctx->filterId_in_uc, &p_ctx->filterId_out_uc))) {
  150. return tipsec_error_sys;
  151. }
  152. // Create SA2: (US <-PC)
  153. if ((ret = _vista_createLocalSA(p_ctx, _p_ctx->port_us, &_p_ctx->spi_us, &p_ctx->saId_us, &p_ctx->filterId_in_us, &p_ctx->filterId_out_uc))) {
  154. return tipsec_error_sys;
  155. }
  156. _p_ctx->state = tipsec_state_inbound;
  157. return tipsec_error_success;
  158. }
  159. static tipsec_error_t _plugin_win_ipsec_vista_ctx_set_remote(tipsec_ctx_t* _p_ctx, tipsec_spi_t spi_pc, tipsec_spi_t spi_ps, tipsec_port_t port_pc, tipsec_port_t port_ps, tipsec_lifetime_t lifetime)
  160. {
  161. plugin_win_ipsec_vista_ctx_t* p_ctx = (plugin_win_ipsec_vista_ctx_t*)_p_ctx;
  162. /* Set Lifetime */
  163. _p_ctx->lifetime = lifetime;
  164. /* Set ports */
  165. _p_ctx->port_ps = port_ps;
  166. _p_ctx->port_pc = port_pc;
  167. /* Set SPIs */
  168. _p_ctx->spi_ps = spi_ps;
  169. _p_ctx->spi_pc = spi_pc;
  170. _p_ctx->state = tipsec_state_full;
  171. return tipsec_error_success;
  172. }
  173. static tipsec_error_t _plugin_win_ipsec_vista_ctx_set_keys(tipsec_ctx_t* _p_ctx, const tipsec_key_t* ik, const tipsec_key_t* ck)
  174. {
  175. plugin_win_ipsec_vista_ctx_t* p_ctx = (plugin_win_ipsec_vista_ctx_t*)_p_ctx;
  176. PFWP_BYTE_BLOB _ik, _ck;
  177. /* Compute ik and ck */
  178. _p_ctx->ik = tsk_realloc(_p_ctx->ik, sizeof(FWP_BYTE_BLOB));
  179. if (!_p_ctx->ik) {
  180. return tipsec_error_outofmemory;
  181. }
  182. _ik = (PFWP_BYTE_BLOB)_p_ctx->ik;
  183. _p_ctx->ck = tsk_realloc(_p_ctx->ck, sizeof(FWP_BYTE_BLOB));
  184. if (!_p_ctx->ck) {
  185. return tipsec_error_outofmemory;
  186. }
  187. _ck = (PFWP_BYTE_BLOB)_p_ctx->ck;
  188. _ik->data = tsk_calloc(TIPSEC_IK_LEN, 1);
  189. if (!_ik->data) {
  190. return tipsec_error_outofmemory;
  191. }
  192. memcpy(_ik->data, ik, TIPSEC_KEY_LEN);
  193. _ik->size = TIPSEC_KEY_LEN;
  194. _ck->data = tsk_calloc(TIPSEC_CK_LEN, 1);
  195. if (!_ck->data) {
  196. return tipsec_error_outofmemory;
  197. }
  198. memcpy(_ck->data, ck, TIPSEC_KEY_LEN);
  199. _ck->size = TIPSEC_KEY_LEN;
  200. return tipsec_error_success;
  201. }
  202. static tipsec_error_t _plugin_win_ipsec_vista_ctx_start(tipsec_ctx_t* _p_ctx)
  203. {
  204. plugin_win_ipsec_vista_ctx_t* p_ctx = (plugin_win_ipsec_vista_ctx_t*)_p_ctx;
  205. int ret;
  206. /* VERY IMPORTANT: The SA context functions must be called in a specific order:
  207. (http://msdn.microsoft.com/en-us/library/bb540652(VS.85).aspx).
  208. IPsecSaContextCreate0
  209. IPsecSaContextGetSpi0
  210. IPsecSaContextAddInbound0
  211. IPsecSaContextAddOutbound0
  212. */
  213. /* US <- PC */
  214. if ((ret = _vista_boundSA(p_ctx, p_ctx->saId_us, _p_ctx->spi_us, TRUE))) {
  215. TSK_DEBUG_ERROR("Failed to setup [US <- PC] SA. Error code = %d", ret);
  216. return tipsec_error_sys;
  217. }
  218. /* UC <- PS */
  219. if ((ret = _vista_boundSA(p_ctx, p_ctx->saId_uc, _p_ctx->spi_uc, TRUE))) {
  220. TSK_DEBUG_ERROR("Failed to setup [UC <- PS] SA. Error code = %d", ret);
  221. return tipsec_error_sys;
  222. }
  223. /* UC -> PS */
  224. if ((ret = _vista_boundSA(p_ctx, p_ctx->saId_uc, _p_ctx->spi_ps, FALSE))) {
  225. TSK_DEBUG_ERROR("Failed to setup [UC -> PS] SA. Error code = %d", ret);
  226. return tipsec_error_sys;
  227. }
  228. /* US -> PC */
  229. if ((ret = _vista_boundSA(p_ctx, p_ctx->saId_us, _p_ctx->spi_pc, FALSE))) {
  230. TSK_DEBUG_ERROR("Failed to setup [US -> PC] SA. Error code = %d", ret);
  231. return tipsec_error_sys;
  232. }
  233. _p_ctx->state = tipsec_state_active;
  234. _p_ctx->started = 1;
  235. return tipsec_error_success;
  236. }
  237. static tipsec_error_t _plugin_win_ipsec_vista_ctx_stop(tipsec_ctx_t* _p_ctx)
  238. {
  239. plugin_win_ipsec_vista_ctx_t* p_ctx = (plugin_win_ipsec_vista_ctx_t*)_p_ctx;
  240. tipsec_error_t err = tipsec_error_success;
  241. //if (!_p_ctx->started) {
  242. // return tipsec_error_success;
  243. //}
  244. /* Flush (delete) all SAs associated to tinyIPSEC */
  245. _vista_flushAll(p_ctx);
  246. _p_ctx->started = 0;
  247. _p_ctx->state = tipsec_state_initial;
  248. return tipsec_error_success;
  249. }
  250. //
  251. // Private functions
  252. //
  253. static int _vista_createLocalSA(__in const plugin_win_ipsec_vista_ctx_t* p_ctx, __in tipsec_port_t local_port, __out tipsec_spi_t *spi, __out UINT64 *saId, __out UINT64 *filterId_in, __out UINT64 *filterId_out)
  254. {
  255. DWORD result = NO_ERROR;
  256. UINT64 tmpInFilterId = 0, tmpOutFilterId = 0, tmpSaId = 0;
  257. FWPM_FILTER0 filter;
  258. IPSEC_TRAFFIC0 outTraffic;
  259. IPSEC_GETSPI0 getSpi;
  260. int ret = -1;
  261. FWPM_FILTER_CONDITION0 conds[6];
  262. UINT32 numFilterConditions = 3;
  263. *spi = 0;
  264. *saId = 0;
  265. *filterId_in = 0;
  266. *filterId_out = 0;
  267. conds[0].fieldKey = FWPM_CONDITION_IP_LOCAL_ADDRESS;
  268. conds[0].matchType = FWP_MATCH_EQUAL;
  269. conds[1].fieldKey = FWPM_CONDITION_IP_REMOTE_ADDRESS;
  270. conds[1].matchType = FWP_MATCH_EQUAL;
  271. if (p_ctx->pc_base->use_ipv6) {
  272. conds[0].conditionValue.type = FWP_BYTE_ARRAY16_TYPE;
  273. conds[0].conditionValue.byteArray16 = (FWP_BYTE_ARRAY16*)p_ctx->pc_base->addr_local;
  274. conds[1].conditionValue.type = FWP_BYTE_ARRAY16_TYPE;
  275. conds[1].conditionValue.byteArray16 = (FWP_BYTE_ARRAY16*)p_ctx->pc_base->addr_remote;
  276. }
  277. else {
  278. conds[0].conditionValue.type = FWP_UINT32;
  279. conds[0].conditionValue.uint32 = *((UINT32*)p_ctx->pc_base->addr_local);
  280. conds[1].conditionValue.type = FWP_UINT32;
  281. conds[1].conditionValue.uint32 = *((UINT32*)p_ctx->pc_base->addr_remote);
  282. }
  283. conds[2].fieldKey = FWPM_CONDITION_IP_LOCAL_PORT;
  284. conds[2].matchType = FWP_MATCH_EQUAL;
  285. conds[2].conditionValue.type = FWP_UINT16;
  286. conds[2].conditionValue.uint16 = local_port;
  287. if (p_ctx->pc_base->ipproto != tipsec_ipproto_all) {
  288. conds[numFilterConditions].fieldKey = FWPM_CONDITION_IP_PROTOCOL;
  289. conds[numFilterConditions].matchType = FWP_MATCH_EQUAL;
  290. conds[numFilterConditions].conditionValue.type = FWP_UINT8;
  291. conds[numFilterConditions].conditionValue.uint8 = TINYIPSEC_VISTA_GET_IPPROTO(p_ctx->pc_base->ipproto);
  292. ++numFilterConditions;
  293. }
  294. // Fill in the common fields shared by both filters.
  295. memset(&filter, 0, sizeof(filter));
  296. // For MUI compatibility, object names should be indirect strings. See
  297. // SHLoadIndirectString for details.
  298. filter.displayData.name = (PWCHAR)p_ctx->filter_name;
  299. // Link all objects to our provider. When multiple providers are installed
  300. // on a computer, this makes it easy to determine who added what.
  301. filter.providerKey = (GUID*)TINYIPSEC_PROVIDER_KEY;
  302. filter.numFilterConditions = numFilterConditions;
  303. filter.filterCondition = conds;
  304. filter.action.type = FWP_ACTION_CALLOUT_TERMINATING;
  305. filter.flags = FWPM_FILTER_FLAG_NONE;
  306. filter.weight.type = FWP_EMPTY;
  307. // Add the inbound filter.
  308. filter.layerKey = (p_ctx->pc_base->use_ipv6) ? FWPM_LAYER_INBOUND_TRANSPORT_V6 : FWPM_LAYER_INBOUND_TRANSPORT_V4;
  309. if (p_ctx->pc_base->mode == tipsec_mode_tun) {
  310. filter.action.calloutKey = (p_ctx->pc_base->use_ipv6) ? FWPM_CALLOUT_IPSEC_INBOUND_TUNNEL_V6 : FWPM_CALLOUT_IPSEC_INBOUND_TUNNEL_V4;
  311. }
  312. else {
  313. filter.action.calloutKey = (p_ctx->pc_base->use_ipv6) ? FWPM_CALLOUT_IPSEC_INBOUND_TRANSPORT_V6 : FWPM_CALLOUT_IPSEC_INBOUND_TRANSPORT_V4;
  314. }
  315. if ((result = FwpmFilterAdd0(p_ctx->engine, &filter, NULL, &tmpInFilterId)) != ERROR_SUCCESS) {
  316. TSK_DEBUG_ERROR("FwpmFilterAdd0 (inbound) failed with error code [%x]", result);
  317. goto CLEANUP;
  318. }
  319. // Add the outbound filter.
  320. filter.layerKey = (p_ctx->pc_base->use_ipv6) ? FWPM_LAYER_OUTBOUND_TRANSPORT_V6 : FWPM_LAYER_OUTBOUND_TRANSPORT_V4;
  321. if (p_ctx->pc_base->mode == tipsec_mode_tun) {
  322. filter.action.calloutKey = (p_ctx->pc_base->use_ipv6) ? FWPM_CALLOUT_IPSEC_OUTBOUND_TUNNEL_V6 : FWPM_CALLOUT_IPSEC_OUTBOUND_TUNNEL_V4;
  323. }
  324. else {
  325. filter.action.calloutKey = (p_ctx->pc_base->use_ipv6) ? FWPM_CALLOUT_IPSEC_OUTBOUND_TRANSPORT_V6 : FWPM_CALLOUT_IPSEC_OUTBOUND_TRANSPORT_V4;
  326. }
  327. if ((result = FwpmFilterAdd0(p_ctx->engine, &filter, NULL, &tmpOutFilterId)) != ERROR_SUCCESS) {
  328. TSK_DEBUG_ERROR("FwpmFilterAdd0(outbound) failed with error code [%x]", result);
  329. goto CLEANUP;
  330. }
  331. // Create the SA context using the outbound traffic descriptor.
  332. memset(&outTraffic, 0, sizeof(outTraffic));
  333. outTraffic.ipVersion = TINYIPSEC_VISTA_GET_IPVER(p_ctx->pc_base->use_ipv6);
  334. if (p_ctx->pc_base->use_ipv6) {
  335. memcpy(outTraffic.localV6Address, p_ctx->pc_base->addr_local, 16);
  336. memcpy(outTraffic.remoteV6Address, p_ctx->pc_base->addr_remote, 16);
  337. }
  338. else {
  339. outTraffic.localV4Address = *((UINT32*)p_ctx->pc_base->addr_local);
  340. outTraffic.remoteV4Address = *((UINT32*)p_ctx->pc_base->addr_remote);
  341. }
  342. outTraffic.trafficType = TINYIPSEC_VISTA_GET_MODE(p_ctx->pc_base->mode);
  343. outTraffic.ipsecFilterId = tmpOutFilterId;
  344. if ((result = IPsecSaContextCreate0(p_ctx->engine, &outTraffic, NULL, &tmpSaId)) != ERROR_SUCCESS) {
  345. TSK_DEBUG_ERROR("IPsecSaContextCreate0 failed with error code [%x]", result);
  346. goto CLEANUP;
  347. }
  348. // Get the inbound SPI using the inbound traffic descriptor.
  349. memset(&getSpi, 0, sizeof(getSpi));
  350. getSpi.inboundIpsecTraffic.ipVersion = TINYIPSEC_VISTA_GET_IPVER(p_ctx->pc_base->use_ipv6);
  351. if (p_ctx->pc_base->use_ipv6) {
  352. memcpy(getSpi.inboundIpsecTraffic.localV6Address, p_ctx->pc_base->addr_local, 16);
  353. memcpy(getSpi.inboundIpsecTraffic.remoteV6Address, p_ctx->pc_base->addr_remote, 16);
  354. }
  355. else {
  356. getSpi.inboundIpsecTraffic.localV4Address = *((UINT32*)p_ctx->pc_base->addr_local);
  357. getSpi.inboundIpsecTraffic.remoteV4Address = *((UINT32*)p_ctx->pc_base->addr_remote);
  358. }
  359. getSpi.inboundIpsecTraffic.trafficType = TINYIPSEC_VISTA_GET_MODE(p_ctx->pc_base->mode);
  360. getSpi.inboundIpsecTraffic.ipsecFilterId = tmpInFilterId;
  361. getSpi.ipVersion = TINYIPSEC_VISTA_GET_IPVER(p_ctx->pc_base->use_ipv6);
  362. if ((result = IPsecSaContextGetSpi0(p_ctx->engine, tmpSaId, &getSpi, spi))) {
  363. TSK_DEBUG_ERROR("IPsecSaContextGetSpi0 failed with error code [%x]", result);
  364. goto CLEANUP;
  365. }
  366. //// Return the various LUIDs to the caller, so he can clean up.
  367. *filterId_in = tmpInFilterId;
  368. *filterId_out = tmpOutFilterId;
  369. *saId = tmpSaId;
  370. CLEANUP:
  371. if (result != NO_ERROR) {
  372. _vista_deleteSaContextAndFilters(p_ctx->engine, tmpInFilterId, tmpOutFilterId, tmpSaId);
  373. }
  374. else {
  375. ret = 0;
  376. }
  377. return ret;
  378. }
  379. static int _vista_boundSA(__in const plugin_win_ipsec_vista_ctx_t* p_ctx, __in UINT64 local_saId, __in tipsec_spi_t remote_spi, __in BOOLEAN toInbound)
  380. {
  381. UINT32 i=0, j=0;
  382. DWORD result = NO_ERROR;
  383. IPSEC_SA0 sa;
  384. IPSEC_SA_BUNDLE0 bundle;
  385. IPSEC_SA_AUTH_INFORMATION0 authInfo; // must be global because use as reference (X = &authInfo)
  386. IPSEC_SA_AUTH_AND_CIPHER_INFORMATION0 cipherAuthInfo; // must be global because use as reference (X = &cipherAuthInfo)
  387. PFWP_BYTE_BLOB ik = (PFWP_BYTE_BLOB)p_ctx->pc_base->ik;
  388. PFWP_BYTE_BLOB ck = (PFWP_BYTE_BLOB)p_ctx->pc_base->ck;
  389. memset(&sa, 0, sizeof(sa));
  390. sa.spi = remote_spi;
  391. sa.saTransformType = TINYIPSEC_VISTA_GET_PROTO(p_ctx->pc_base->protocol, p_ctx->pc_base->ealg);
  392. //
  393. // Keys padding
  394. //
  395. if (p_ctx->pc_base->alg == tipsec_alg_hmac_sha_1_96) {
  396. if (ik->size < TIPSEC_IK_LEN) {
  397. for(i = ik->size; i < TIPSEC_KEY_LEN; i++) {
  398. ik->data[i] = 0x00; /* Already done by "tsk_calloc" but ... */
  399. }
  400. ik->size = TIPSEC_IK_LEN;
  401. }
  402. }
  403. if (p_ctx->pc_base->ealg == tipsec_ealg_des_ede3_cbc) {
  404. if (ck->size < TIPSEC_CK_LEN) {
  405. for (i = ck->size; i<TIPSEC_CK_LEN; i++) {
  406. ck->data[i] = ck->data[j++];
  407. }
  408. ck->size = TIPSEC_CK_LEN;
  409. }
  410. }
  411. //
  412. // In all case create Authentication info
  413. //
  414. memset(&authInfo, 0, sizeof(authInfo));
  415. authInfo.authTransform.authTransformId = TINYIPSEC_VISTA_GET_ALGO(p_ctx->pc_base->alg);
  416. authInfo.authKey = *ik;
  417. if ( sa.saTransformType == IPSEC_TRANSFORM_AH ) {
  418. sa.ahInformation = &authInfo;
  419. }
  420. else if ( sa.saTransformType == IPSEC_TRANSFORM_ESP_AUTH ) {
  421. sa.espAuthInformation = &authInfo;
  422. }
  423. else if ( sa.saTransformType == IPSEC_TRANSFORM_ESP_CIPHER ) {
  424. IPSEC_SA_CIPHER_INFORMATION0 cipherInfo;
  425. memset(&cipherInfo, 0, sizeof(cipherInfo));
  426. cipherInfo.cipherTransform.cipherTransformId = TINYIPSEC_VISTA_GET_EALGO(p_ctx->pc_base->ealg);
  427. cipherInfo.cipherKey = *ck;
  428. sa.espCipherInformation = &cipherInfo;
  429. }
  430. else if ( sa.saTransformType == IPSEC_TRANSFORM_ESP_AUTH_AND_CIPHER ) {
  431. IPSEC_SA_CIPHER_INFORMATION0 cipherInfo;
  432. memset(&cipherInfo, 0, sizeof(cipherInfo));
  433. cipherInfo.cipherTransform.cipherTransformId = TINYIPSEC_VISTA_GET_EALGO(p_ctx->pc_base->ealg);
  434. cipherInfo.cipherKey = *ck;
  435. memset(&cipherAuthInfo, 0, sizeof(cipherAuthInfo));
  436. cipherAuthInfo.saAuthInformation = authInfo;
  437. cipherAuthInfo.saCipherInformation = cipherInfo;
  438. sa.espAuthAndCipherInformation = &cipherAuthInfo;
  439. }
  440. memset(&bundle, 0, sizeof(bundle));
  441. bundle.numSAs = 1;
  442. bundle.saList = &sa;
  443. bundle.ipVersion = TINYIPSEC_VISTA_GET_IPVER(p_ctx->pc_base->use_ipv6);
  444. bundle.lifetime.lifetimeSeconds = (UINT32)((p_ctx->pc_base->lifetime > TINYIPSEC_SA_MAX_LIFETIME) ? TINYIPSEC_SA_MAX_LIFETIME : p_ctx->pc_base->lifetime);
  445. /* From remote to local (inbound) ? */
  446. if (toInbound) {
  447. if((result = IPsecSaContextAddInbound0(p_ctx->engine, local_saId, &bundle)) != ERROR_SUCCESS) {
  448. TSK_DEBUG_ERROR("IPsecSaContextAddInbound0 failed with error code [%x]", result);
  449. goto CLEANUP;
  450. }
  451. }
  452. else {
  453. if ((result = IPsecSaContextAddOutbound0(p_ctx->engine, local_saId, &bundle)) != ERROR_SUCCESS) {
  454. TSK_DEBUG_ERROR("IPsecSaContextAddOutbound0 failed with error code [%x]", result);
  455. goto CLEANUP;
  456. }
  457. }
  458. CLEANUP:
  459. return (result == ERROR_SUCCESS) ? 0 : -1;
  460. }
  461. static int _vista_flushAll(const plugin_win_ipsec_vista_ctx_t* p_ctx)
  462. {
  463. #if 1
  464. int ret = -1;
  465. if (p_ctx && p_ctx->engine) {
  466. DWORD result;
  467. result = FwpmFilterDeleteById0(p_ctx->engine, p_ctx->filterId_in_uc);
  468. if (result != ERROR_SUCCESS && result != FWP_E_FILTER_NOT_FOUND) {
  469. TSK_DEBUG_ERROR("FwpmFilterDeleteById0 failed with error code [%x]", result);
  470. }
  471. result = FwpmFilterDeleteById0(p_ctx->engine, p_ctx->filterId_in_us);
  472. if (result != ERROR_SUCCESS && result != FWP_E_FILTER_NOT_FOUND) {
  473. TSK_DEBUG_ERROR("FwpmFilterDeleteById0 failed with error code [%x]", result);
  474. }
  475. result = FwpmFilterDeleteById0(p_ctx->engine, p_ctx->filterId_out_uc);
  476. if (result != ERROR_SUCCESS && result != FWP_E_FILTER_NOT_FOUND) {
  477. TSK_DEBUG_ERROR("FwpmFilterDeleteById0 failed with error code [%x]", result);
  478. }
  479. result = FwpmFilterDeleteById0(p_ctx->engine, p_ctx->filterId_out_us);
  480. if (result != ERROR_SUCCESS && result != FWP_E_FILTER_NOT_FOUND) {
  481. TSK_DEBUG_ERROR("FwpmFilterDeleteById0 failed with error code [%x]", result);
  482. }
  483. return 0;
  484. }
  485. //
  486. return ret;
  487. #else
  488. UINT32 i;
  489. int ret = -1;
  490. if (p_ctx && p_ctx->engine) {
  491. HANDLE enumHandle = NULL;
  492. IPSEC_SA_DETAILS0** entries = NULL;
  493. UINT32 numEntriesReturned = 0;
  494. DWORD result;
  495. if ((result = IPsecSaCreateEnumHandle0(p_ctx->engine, NULL, &enumHandle)) != ERROR_SUCCESS) {
  496. TSK_DEBUG_ERROR("IPsecSaCreateEnumHandle0 failed with error code [%x].", result);
  497. goto CLEANUP;
  498. }
  499. if ((result = IPsecSaEnum0(p_ctx->engine, enumHandle, TINYIPSEC_SA_NUM_ENTRIES_TO_REQUEST, &entries, &numEntriesReturned)) != ERROR_SUCCESS) {
  500. TSK_DEBUG_ERROR("IPsecSaEnum0 failed with error code [%x].", result);
  501. goto CLEANUP;
  502. }
  503. for (i = 0; i<numEntriesReturned; i++) {
  504. IPSEC_SA_DETAILS0* entry = (entries)[i];
  505. if ( !wcscmp(entry->transportFilter->displayData.name, p_ctx->filter_name)) {
  506. if ((result = FwpmFilterDeleteById0(p_ctx->engine, entry->transportFilter->filterId)) != ERROR_SUCCESS) {
  507. TSK_DEBUG_ERROR("FwpmFilterDeleteById0 failed with error code [%x].", result);
  508. goto CLEANUP;
  509. }
  510. }
  511. }
  512. TSK_DEBUG_INFO("All SAs have been flushed.");
  513. ret = 0;
  514. CLEANUP:
  515. if (entries) {
  516. FwpmFreeMemory0((void**)entries);
  517. }
  518. if (enumHandle) {
  519. if ((result = IPsecSaDestroyEnumHandle0(p_ctx->engine, enumHandle)) != ERROR_SUCCESS) {
  520. TSK_DEBUG_ERROR("IPsecSaDestroyEnumHandle0 failed with error code [%x].", result);
  521. }
  522. }
  523. }
  524. return ret;
  525. #endif
  526. }
  527. static void _vista_deleteSaContextAndFilters(__in HANDLE engine, __in UINT64 inFilterId, __in UINT64 outFilterId, __in UINT64 saId)
  528. {
  529. DWORD result;
  530. // Allow the LUIDs to be zero, so we can use this function to cleanup
  531. // partial results.
  532. if (saId != 0) {
  533. result = IPsecSaContextDeleteById0(engine, saId);
  534. if (result != ERROR_SUCCESS) {
  535. // There's not much we can do if delete fails, so continue trying to
  536. // clean up the remaining objects.
  537. TSK_DEBUG_ERROR("IPsecSaContextDeleteById0 = 0x%08X\n", result);
  538. }
  539. }
  540. if (outFilterId != 0) {
  541. result = FwpmFilterDeleteById0(engine, outFilterId);
  542. if (result != ERROR_SUCCESS) {
  543. TSK_DEBUG_ERROR("FwpmFilterDeleteById0 = 0x%08X\n", result);
  544. }
  545. }
  546. if (inFilterId != 0) {
  547. result = FwpmFilterDeleteById0(engine, inFilterId);
  548. if (result != ERROR_SUCCESS) {
  549. TSK_DEBUG_ERROR("FwpmFilterDeleteById0 = 0x%08X\n", result);
  550. }
  551. }
  552. }
  553. //
  554. // Windows Vista IPSec Plugin definition
  555. //
  556. /* constructor */
  557. static tsk_object_t* _plugin_win_ipsec_vista_ctx_ctor(tsk_object_t * self, va_list * app)
  558. {
  559. plugin_win_ipsec_vista_ctx_t *p_ctx = (plugin_win_ipsec_vista_ctx_t *)self;
  560. if (p_ctx) {
  561. p_ctx->pc_base = TIPSEC_CTX(p_ctx);
  562. }
  563. return self;
  564. }
  565. /* destructor */
  566. static tsk_object_t* _plugin_win_ipsec_vista_ctx_dtor(tsk_object_t * self)
  567. {
  568. plugin_win_ipsec_vista_ctx_t *p_ctx = (plugin_win_ipsec_vista_ctx_t *)self;
  569. if (p_ctx) {
  570. DWORD code;
  571. if (p_ctx->pc_base->started) {
  572. tipsec_ctx_stop(p_ctx->pc_base);
  573. }
  574. /* Close engine */
  575. if (p_ctx->engine) {
  576. if ((code = FwpmEngineClose0(p_ctx->engine))) {
  577. TSK_DEBUG_ERROR("FwpmEngineClose0 failed with error code [%x].", code);
  578. }
  579. }
  580. TSK_FREE(p_ctx->pc_base->addr_local);
  581. TSK_FREE(p_ctx->pc_base->addr_remote);
  582. if (p_ctx->pc_base->ik) {
  583. TSK_FREE(((PFWP_BYTE_BLOB)p_ctx->pc_base->ik)->data);
  584. TSK_FREE(p_ctx->pc_base->ik);
  585. }
  586. if (p_ctx->pc_base->ck) {
  587. TSK_FREE(((PFWP_BYTE_BLOB)p_ctx->pc_base->ck)->data);
  588. TSK_FREE(p_ctx->pc_base->ck);
  589. }
  590. TSK_DEBUG_INFO("*** Windows Vista IPSec plugin (Windows Filtering Platform) context destroyed ***");
  591. }
  592. return self;
  593. }
  594. /* object definition */
  595. static const tsk_object_def_t plugin_win_ipsec_vista_ctx_def_s = {
  596. sizeof(plugin_win_ipsec_vista_ctx_t),
  597. _plugin_win_ipsec_vista_ctx_ctor,
  598. _plugin_win_ipsec_vista_ctx_dtor,
  599. tsk_null,
  600. };
  601. /* plugin definition*/
  602. static const tipsec_plugin_def_t plugin_win_ipsec_vista_plugin_def_s = {
  603. &plugin_win_ipsec_vista_ctx_def_s,
  604. tipsec_impl_type_vista,
  605. "Windows Vista IPSec (Windows Filtering Platform)",
  606. _plugin_win_ipsec_vista_ctx_init,
  607. _plugin_win_ipsec_vista_ctx_set_local,
  608. _plugin_win_ipsec_vista_ctx_set_remote,
  609. _plugin_win_ipsec_vista_ctx_set_keys,
  610. _plugin_win_ipsec_vista_ctx_start,
  611. _plugin_win_ipsec_vista_ctx_stop,
  612. };
  613. const tipsec_plugin_def_t *plugin_win_ipsec_vista_plugin_def_t = &plugin_win_ipsec_vista_plugin_def_s;