summaryrefslogtreecommitdiffstats
path: root/plugins/pluginWinIPSecVista/plugin_win_ipsec_vista.c
blob: 63b119488b8276b86252d452b7e5a87f827fcc5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
/* Copyright (C) 2013-2014 Mamadou DIOP
* Copyright (C) 2013-2014 Doubango Telecom <http://www.doubango.org>
*
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*/
#include "plugin_win_ipsec_vista_config.h"

#include "tipsec.h" /* From tinyIPSec project. Requires linking against "tinyIPSec.lib" */

#include "tsk_memory.h"
#include "tsk_object.h"
#include "tsk_debug.h"

#include <ws2tcpip.h>
#include <Fwpmu.h>
#include <Rpc.h>

#if defined(_MSC_VER)
#	pragma comment(lib, "Fwpuclnt.lib")
#	pragma comment(lib, "Rpcrt4.lib")
#endif

typedef FWP_BYTE_BLOB* PFWP_BYTE_BLOB;

/* as WFP do not provide null encryption I define my own*/
static const IPSEC_CIPHER_TRANSFORM_ID0 IPSEC_CIPHER_TRANSFORM_ID_NULL_NULL= {
    (IPSEC_CIPHER_TYPE)NULL,
    (IPSEC_CIPHER_TYPE)NULL
};

#define TINYIPSEC_FILTER_NAME					TEXT("Doubango Telecom tinyIPSec (Windows Vista)")
#define TINYIPSEC_PROVIDER_KEY					NULL

#define TINYIPSEC_SA_NUM_ENTRIES_TO_REQUEST		INT_MAX
#define TINYIPSEC_SA_MAX_LIFETIME				172799

#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
#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 )
#define TINYIPSEC_VISTA_GET_MODE(mode)	 (mode == tipsec_mode_tun) ? IPSEC_TRAFFIC_TYPE_TUNNEL : IPSEC_TRAFFIC_TYPE_TRANSPORT
#define TINYIPSEC_VISTA_GET_IPPROTO(ipproto)	 (ipproto == tipsec_ipproto_tcp) ? IPPROTO_TCP : ((ipproto == tipsec_ipproto_icmp) ? IPPROTO_ICMP : IPPROTO_UDP)
#define TINYIPSEC_VISTA_GET_IPVER(ipv6)	 (ipv6) ? FWP_IP_VERSION_V6 : FWP_IP_VERSION_V4
#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 );

typedef struct plugin_win_ipsec_vista_ctx_s {
    TIPSEC_DECLARE_CTX;

    tipsec_ctx_t* pc_base;
    UINT64 saId_us;
    UINT64 saId_uc;
    UINT64 filterId_in_us;
    UINT64 filterId_out_us;
    UINT64 filterId_in_uc;
    UINT64 filterId_out_uc;
    WCHAR filter_name[256];

    HANDLE engine;
}
plugin_win_ipsec_vista_ctx_t;

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);
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);

static int _vista_flushAll(const plugin_win_ipsec_vista_ctx_t* p_ctx);
static void _vista_deleteSaContextAndFilters(__in HANDLE engine, __in UINT64 inFilterId, __in UINT64 outFilterId, __in UINT64 saId);

//
//	Plugin implementation
//

static tipsec_error_t _plugin_win_ipsec_vista_ctx_init(tipsec_ctx_t* _p_ctx)
{
    plugin_win_ipsec_vista_ctx_t* p_ctx = (plugin_win_ipsec_vista_ctx_t*)_p_ctx;
    DWORD code;
    UUID uuid;
    RPC_STATUS status;
    static uint64_t __guard = 0;

    if (p_ctx->pc_base->initialized) {
        TSK_DEBUG_ERROR("Already initialized");
        return tipsec_error_invalid_state;
    }

    /* Create filter name */
    status = UuidCreate(&uuid);
    if (status == RPC_S_OK) {
        WCHAR* wszUuid = NULL;
        UuidToStringW(&uuid, (RPC_WSTR*)&wszUuid);
        if (!wszUuid) {
            TSK_DEBUG_ERROR("Failed to convert the UUID");
            return tipsec_error_sys;
        }
        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++);
        RpcStringFree((RPC_WSTR*)&wszUuid);
    }
    else {
        TSK_DEBUG_ERROR("Failed to create new UUID");
        return tipsec_error_sys;
    }



    /* Open engine */
    if ((code = FwpmEngineOpen0(NULL, RPC_C_AUTHN_WINNT, NULL, NULL, &p_ctx->engine))) {
        p_ctx->pc_base->initialized = tsk_false;
        TSK_DEBUG_ERROR("FwpmEngineOpen0 failed with error code [%x].", code);
        return tipsec_error_sys;
    }
    else {
        p_ctx->pc_base->initialized = tsk_true;
        p_ctx->pc_base->state = tipsec_state_initial;
        return tipsec_error_success;
    }
}

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)
{
    plugin_win_ipsec_vista_ctx_t* p_ctx = (plugin_win_ipsec_vista_ctx_t*)_p_ctx;
    int ret;

    _p_ctx->addr_local = tsk_realloc(_p_ctx->addr_local, _p_ctx->use_ipv6 ? 16 : 4);
    if (!_p_ctx->addr_local) {
        return tipsec_error_outofmemory;
    }
    _p_ctx->addr_remote = tsk_realloc(_p_ctx->addr_remote, _p_ctx->use_ipv6 ? 16 : 4);
    if (!_p_ctx->addr_remote) {
        return tipsec_error_outofmemory;
    }

    /* Set local IP */
    if (_p_ctx->use_ipv6) {
        if ((ret = inet_pton(AF_INET6, addr_local, _p_ctx->addr_local)) != 1 ) {
            TSK_DEBUG_ERROR("inet_pton(%s) have failed with error code [%x].", addr_local, ret);
            return tipsec_error_sys;
        }
        if ((ret = inet_pton(AF_INET6, addr_remote, _p_ctx->addr_remote)) != 1 ) {
            TSK_DEBUG_ERROR("inet_pton(%s) have failed with error code [%x].", addr_remote, ret);
            return tipsec_error_sys;
        }
    }
    else {
        if ((ret = inet_pton(AF_INET, addr_local, _p_ctx->addr_local)) != 1 ) {
            TSK_DEBUG_ERROR("inet_pton(%s) have failed with error code [%x].", addr_local, ret);
            return tipsec_error_sys;
        }
        else {
            *((UINT32*)_p_ctx->addr_local) = ntohl(*((UINT32*)_p_ctx->addr_local));
        }
        if ((ret = inet_pton(AF_INET, addr_remote, _p_ctx->addr_remote)) != 1 ) {
            TSK_DEBUG_ERROR("inet_pton(%s) have failed with error code [%x].", addr_remote, ret);
            return tipsec_error_sys;
        }
        else {
            *((UINT32*)_p_ctx->addr_remote) = ntohl(*((UINT32*)_p_ctx->addr_remote));
        }
    }

    /* Set ports */
    _p_ctx->port_uc = port_uc;
    _p_ctx->port_us = port_us;

    // Create SA1: (UC -> PS)
    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))) {
        return tipsec_error_sys;
    }

    // Create SA2: (US <-PC)
    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))) {
        return tipsec_error_sys;
    }

    _p_ctx->state = tipsec_state_inbound;

    return tipsec_error_success;
}

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)
{
    plugin_win_ipsec_vista_ctx_t* p_ctx = (plugin_win_ipsec_vista_ctx_t*)_p_ctx;

    /* Set Lifetime */
    _p_ctx->lifetime = lifetime;

    /* Set ports */
    _p_ctx->port_ps = port_ps;
    _p_ctx->port_pc = port_pc;

    /* Set SPIs */
    _p_ctx->spi_ps = spi_ps;
    _p_ctx->spi_pc = spi_pc;

    _p_ctx->state = tipsec_state_full;

    return tipsec_error_success;
}

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)
{
    plugin_win_ipsec_vista_ctx_t* p_ctx = (plugin_win_ipsec_vista_ctx_t*)_p_ctx;
    PFWP_BYTE_BLOB _ik, _ck;

    /* Compute ik and ck */
    _p_ctx->ik = tsk_realloc(_p_ctx->ik, sizeof(FWP_BYTE_BLOB));
    if (!_p_ctx->ik) {
        return tipsec_error_outofmemory;
    }
    _ik = (PFWP_BYTE_BLOB)_p_ctx->ik;
    _p_ctx->ck = tsk_realloc(_p_ctx->ck, sizeof(FWP_BYTE_BLOB));
    if (!_p_ctx->ck) {
        return tipsec_error_outofmemory;
    }
    _ck = (PFWP_BYTE_BLOB)_p_ctx->ck;

    _ik->data = tsk_calloc(TIPSEC_IK_LEN, 1);
    if (!_ik->data) {
        return tipsec_error_outofmemory;
    }
    memcpy(_ik->data, ik, TIPSEC_KEY_LEN);
    _ik->size = TIPSEC_KEY_LEN;

    _ck->data = tsk_calloc(TIPSEC_CK_LEN, 1);
    if (!_ck->data) {
        return tipsec_error_outofmemory;
    }
    memcpy(_ck->data, ck, TIPSEC_KEY_LEN);
    _ck->size = TIPSEC_KEY_LEN;

    return tipsec_error_success;
}

static tipsec_error_t _plugin_win_ipsec_vista_ctx_start(tipsec_ctx_t* _p_ctx)
{
    plugin_win_ipsec_vista_ctx_t* p_ctx = (plugin_win_ipsec_vista_ctx_t*)_p_ctx;
    int ret;

    /* VERY IMPORTANT: The SA context functions must be called in a specific order:
    		(http://msdn.microsoft.com/en-us/library/bb540652(VS.85).aspx).

    	IPsecSaContextCreate0
    	IPsecSaContextGetSpi0
    	IPsecSaContextAddInbound0
    	IPsecSaContextAddOutbound0
    */

    /* US <- PC */
    if ((ret = _vista_boundSA(p_ctx, p_ctx->saId_us, _p_ctx->spi_us, TRUE))) {
        TSK_DEBUG_ERROR("Failed to setup [US <- PC] SA. Error code = %d", ret);
        return tipsec_error_sys;
    }
    /* UC <- PS */
    if ((ret = _vista_boundSA(p_ctx, p_ctx->saId_uc, _p_ctx->spi_uc, TRUE))) {
        TSK_DEBUG_ERROR("Failed to setup [UC <- PS] SA. Error code = %d", ret);
        return tipsec_error_sys;
    }

    /* UC -> PS */
    if ((ret = _vista_boundSA(p_ctx, p_ctx->saId_uc, _p_ctx->spi_ps, FALSE))) {
        TSK_DEBUG_ERROR("Failed to setup [UC -> PS] SA. Error code = %d", ret);
        return tipsec_error_sys;
    }
    /* US -> PC */
    if ((ret = _vista_boundSA(p_ctx, p_ctx->saId_us, _p_ctx->spi_pc, FALSE))) {
        TSK_DEBUG_ERROR("Failed to setup [US -> PC] SA. Error code = %d", ret);
        return tipsec_error_sys;
    }

    _p_ctx->state = tipsec_state_active;
    _p_ctx->started = 1;

    return tipsec_error_success;
}

static tipsec_error_t _plugin_win_ipsec_vista_ctx_stop(tipsec_ctx_t* _p_ctx)
{
    plugin_win_ipsec_vista_ctx_t* p_ctx = (plugin_win_ipsec_vista_ctx_t*)_p_ctx;
    tipsec_error_t err = tipsec_error_success;

    //if (!_p_ctx->started) {
    //    return tipsec_error_success;
    //}

    /* Flush (delete) all SAs associated to tinyIPSEC */
    _vista_flushAll(p_ctx);

    _p_ctx->started = 0;
    _p_ctx->state = tipsec_state_initial;

    return tipsec_error_success;
}

//
//	Private functions
//
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)
{
    DWORD result = NO_ERROR;
    UINT64 tmpInFilterId = 0, tmpOutFilterId = 0, tmpSaId = 0;
    FWPM_FILTER0 filter;
    IPSEC_TRAFFIC0 outTraffic;
    IPSEC_GETSPI0 getSpi;
    int ret = -1;
    FWPM_FILTER_CONDITION0 conds[6];
    UINT32 numFilterConditions = 3;

    *spi = 0;
    *saId = 0;
    *filterId_in = 0;
    *filterId_out = 0;

    conds[0].fieldKey = FWPM_CONDITION_IP_LOCAL_ADDRESS;
    conds[0].matchType = FWP_MATCH_EQUAL;
    conds[1].fieldKey = FWPM_CONDITION_IP_REMOTE_ADDRESS;
    conds[1].matchType = FWP_MATCH_EQUAL;
    if (p_ctx->pc_base->use_ipv6) {
        conds[0].conditionValue.type = FWP_BYTE_ARRAY16_TYPE;
        conds[0].conditionValue.byteArray16 = (FWP_BYTE_ARRAY16*)p_ctx->pc_base->addr_local;
        conds[1].conditionValue.type = FWP_BYTE_ARRAY16_TYPE;
        conds[1].conditionValue.byteArray16 = (FWP_BYTE_ARRAY16*)p_ctx->pc_base->addr_remote;
    }
    else {
        conds[0].conditionValue.type = FWP_UINT32;
        conds[0].conditionValue.uint32 = *((UINT32*)p_ctx->pc_base->addr_local);
        conds[1].conditionValue.type = FWP_UINT32;
        conds[1].conditionValue.uint32 = *((UINT32*)p_ctx->pc_base->addr_remote);
    }

    conds[2].fieldKey = FWPM_CONDITION_IP_LOCAL_PORT;
    conds[2].matchType = FWP_MATCH_EQUAL;
    conds[2].conditionValue.type = FWP_UINT16;
    conds[2].conditionValue.uint16 = local_port;

    if (p_ctx->pc_base->ipproto != tipsec_ipproto_all) {
        conds[numFilterConditions].fieldKey = FWPM_CONDITION_IP_PROTOCOL;
        conds[numFilterConditions].matchType = FWP_MATCH_EQUAL;
        conds[numFilterConditions].conditionValue.type = FWP_UINT8;
        conds[numFilterConditions].conditionValue.uint8 = TINYIPSEC_VISTA_GET_IPPROTO(p_ctx->pc_base->ipproto);
        ++numFilterConditions;
    }

    // Fill in the common fields shared by both filters.
    memset(&filter, 0, sizeof(filter));
    // For MUI compatibility, object names should be indirect strings. See
    // SHLoadIndirectString for details.
    filter.displayData.name = (PWCHAR)p_ctx->filter_name;
    // Link all objects to our provider. When multiple providers are installed
    // on a computer, this makes it easy to determine who added what.
    filter.providerKey = (GUID*)TINYIPSEC_PROVIDER_KEY;
    filter.numFilterConditions = numFilterConditions;
    filter.filterCondition = conds;
    filter.action.type = FWP_ACTION_CALLOUT_TERMINATING;
    filter.flags = FWPM_FILTER_FLAG_NONE;
    filter.weight.type = FWP_EMPTY;

    // Add the inbound filter.
    filter.layerKey = (p_ctx->pc_base->use_ipv6) ? FWPM_LAYER_INBOUND_TRANSPORT_V6 : FWPM_LAYER_INBOUND_TRANSPORT_V4;
    if (p_ctx->pc_base->mode == tipsec_mode_tun) {
        filter.action.calloutKey = (p_ctx->pc_base->use_ipv6) ? FWPM_CALLOUT_IPSEC_INBOUND_TUNNEL_V6 : FWPM_CALLOUT_IPSEC_INBOUND_TUNNEL_V4;
    }
    else {
        filter.action.calloutKey = (p_ctx->pc_base->use_ipv6) ? FWPM_CALLOUT_IPSEC_INBOUND_TRANSPORT_V6 : FWPM_CALLOUT_IPSEC_INBOUND_TRANSPORT_V4;
    }
    if ((result = FwpmFilterAdd0(p_ctx->engine, &filter, NULL, &tmpInFilterId)) != ERROR_SUCCESS) {
        TSK_DEBUG_ERROR("FwpmFilterAdd0 (inbound) failed with error code [%x]", result);
        goto CLEANUP;
    }

    // Add the outbound filter.
    filter.layerKey = (p_ctx->pc_base->use_ipv6) ? FWPM_LAYER_OUTBOUND_TRANSPORT_V6 : FWPM_LAYER_OUTBOUND_TRANSPORT_V4;
    if (p_ctx->pc_base->mode == tipsec_mode_tun) {
        filter.action.calloutKey = (p_ctx->pc_base->use_ipv6) ? FWPM_CALLOUT_IPSEC_OUTBOUND_TUNNEL_V6 : FWPM_CALLOUT_IPSEC_OUTBOUND_TUNNEL_V4;
    }
    else {
        filter.action.calloutKey = (p_ctx->pc_base->use_ipv6) ? FWPM_CALLOUT_IPSEC_OUTBOUND_TRANSPORT_V6 : FWPM_CALLOUT_IPSEC_OUTBOUND_TRANSPORT_V4;
    }
    if ((result = FwpmFilterAdd0(p_ctx->engine, &filter, NULL, &tmpOutFilterId)) != ERROR_SUCCESS) {
        TSK_DEBUG_ERROR("FwpmFilterAdd0(outbound) failed with error code [%x]", result);
        goto CLEANUP;
    }

    // Create the SA context using the outbound traffic descriptor.
    memset(&outTraffic, 0, sizeof(outTraffic));
    outTraffic.ipVersion = TINYIPSEC_VISTA_GET_IPVER(p_ctx->pc_base->use_ipv6);
    if (p_ctx->pc_base->use_ipv6) {
        memcpy(outTraffic.localV6Address, p_ctx->pc_base->addr_local, 16);
        memcpy(outTraffic.remoteV6Address, p_ctx->pc_base->addr_remote, 16);
    }
    else {
        outTraffic.localV4Address = *((UINT32*)p_ctx->pc_base->addr_local);
        outTraffic.remoteV4Address = *((UINT32*)p_ctx->pc_base->addr_remote);
    }
    outTraffic.trafficType = TINYIPSEC_VISTA_GET_MODE(p_ctx->pc_base->mode);
    outTraffic.ipsecFilterId = tmpOutFilterId;
    if ((result = IPsecSaContextCreate0(p_ctx->engine, &outTraffic, NULL, &tmpSaId)) != ERROR_SUCCESS) {
        TSK_DEBUG_ERROR("IPsecSaContextCreate0 failed with error code [%x]", result);
        goto CLEANUP;
    }

    // Get the inbound SPI using the inbound traffic descriptor.
    memset(&getSpi, 0, sizeof(getSpi));
    getSpi.inboundIpsecTraffic.ipVersion = TINYIPSEC_VISTA_GET_IPVER(p_ctx->pc_base->use_ipv6);
    if (p_ctx->pc_base->use_ipv6) {
        memcpy(getSpi.inboundIpsecTraffic.localV6Address, p_ctx->pc_base->addr_local, 16);
        memcpy(getSpi.inboundIpsecTraffic.remoteV6Address, p_ctx->pc_base->addr_remote, 16);
    }
    else {
        getSpi.inboundIpsecTraffic.localV4Address = *((UINT32*)p_ctx->pc_base->addr_local);
        getSpi.inboundIpsecTraffic.remoteV4Address = *((UINT32*)p_ctx->pc_base->addr_remote);
    }
    getSpi.inboundIpsecTraffic.trafficType = TINYIPSEC_VISTA_GET_MODE(p_ctx->pc_base->mode);
    getSpi.inboundIpsecTraffic.ipsecFilterId = tmpInFilterId;
    getSpi.ipVersion = TINYIPSEC_VISTA_GET_IPVER(p_ctx->pc_base->use_ipv6);
    if ((result = IPsecSaContextGetSpi0(p_ctx->engine, tmpSaId, &getSpi, spi))) {
        TSK_DEBUG_ERROR("IPsecSaContextGetSpi0 failed with error code [%x]", result);
        goto CLEANUP;
    }

    //// Return the various LUIDs to the caller, so he can clean up.
    *filterId_in = tmpInFilterId;
    *filterId_out = tmpOutFilterId;
    *saId = tmpSaId;

CLEANUP:
    if (result != NO_ERROR) {
        _vista_deleteSaContextAndFilters(p_ctx->engine, tmpInFilterId, tmpOutFilterId, tmpSaId);
    }
    else {
        ret = 0;
    }

    return ret;
}

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)
{
    UINT32 i=0, j=0;
    DWORD result = NO_ERROR;
    IPSEC_SA0 sa;
    IPSEC_SA_BUNDLE0 bundle;
    IPSEC_SA_AUTH_INFORMATION0 authInfo; // must be global because use as reference (X = &authInfo)
    IPSEC_SA_AUTH_AND_CIPHER_INFORMATION0 cipherAuthInfo; // must be global because use as reference (X = &cipherAuthInfo)
    PFWP_BYTE_BLOB ik = (PFWP_BYTE_BLOB)p_ctx->pc_base->ik;
    PFWP_BYTE_BLOB ck = (PFWP_BYTE_BLOB)p_ctx->pc_base->ck;

    memset(&sa, 0, sizeof(sa));
    sa.spi = remote_spi;
    sa.saTransformType = TINYIPSEC_VISTA_GET_PROTO(p_ctx->pc_base->protocol, p_ctx->pc_base->ealg);

    //
    //	Keys padding
    //
    if (p_ctx->pc_base->alg == tipsec_alg_hmac_sha_1_96) {
        if (ik->size < TIPSEC_IK_LEN) {
            for(i = ik->size; i < TIPSEC_KEY_LEN; i++) {
                ik->data[i] = 0x00; /* Already done by "tsk_calloc" but ... */
            }
            ik->size = TIPSEC_IK_LEN;
        }
    }
    if (p_ctx->pc_base->ealg == tipsec_ealg_des_ede3_cbc) {
        if (ck->size < TIPSEC_CK_LEN) {
            for (i = ck->size; i<TIPSEC_CK_LEN; i++) {
                ck->data[i] = ck->data[j++];
            }
            ck->size = TIPSEC_CK_LEN;
        }
    }

    //
    //	In all case create Authentication info
    //
    memset(&authInfo, 0, sizeof(authInfo));
    authInfo.authTransform.authTransformId = TINYIPSEC_VISTA_GET_ALGO(p_ctx->pc_base->alg);
    authInfo.authKey = *ik;

    if ( sa.saTransformType == IPSEC_TRANSFORM_AH ) {
        sa.ahInformation = &authInfo;
    }
    else if ( sa.saTransformType == IPSEC_TRANSFORM_ESP_AUTH ) {
        sa.espAuthInformation = &authInfo;
    }
    else if ( sa.saTransformType == IPSEC_TRANSFORM_ESP_CIPHER ) {
        IPSEC_SA_CIPHER_INFORMATION0 cipherInfo;

        memset(&cipherInfo, 0, sizeof(cipherInfo));
        cipherInfo.cipherTransform.cipherTransformId = TINYIPSEC_VISTA_GET_EALGO(p_ctx->pc_base->ealg);
        cipherInfo.cipherKey = *ck;

        sa.espCipherInformation = &cipherInfo;
    }
    else if ( sa.saTransformType == IPSEC_TRANSFORM_ESP_AUTH_AND_CIPHER ) {
        IPSEC_SA_CIPHER_INFORMATION0 cipherInfo;

        memset(&cipherInfo, 0, sizeof(cipherInfo));
        cipherInfo.cipherTransform.cipherTransformId = TINYIPSEC_VISTA_GET_EALGO(p_ctx->pc_base->ealg);
        cipherInfo.cipherKey = *ck;

        memset(&cipherAuthInfo, 0, sizeof(cipherAuthInfo));
        cipherAuthInfo.saAuthInformation = authInfo;
        cipherAuthInfo.saCipherInformation = cipherInfo;

        sa.espAuthAndCipherInformation = &cipherAuthInfo;
    }

    memset(&bundle, 0, sizeof(bundle));
    bundle.numSAs = 1;
    bundle.saList = &sa;
    bundle.ipVersion = TINYIPSEC_VISTA_GET_IPVER(p_ctx->pc_base->use_ipv6);
    bundle.lifetime.lifetimeSeconds = (UINT32)((p_ctx->pc_base->lifetime > TINYIPSEC_SA_MAX_LIFETIME) ? TINYIPSEC_SA_MAX_LIFETIME : p_ctx->pc_base->lifetime);

    /* From remote to local (inbound) ? */
    if (toInbound) {
        if((result = IPsecSaContextAddInbound0(p_ctx->engine, local_saId, &bundle)) != ERROR_SUCCESS) {
            TSK_DEBUG_ERROR("IPsecSaContextAddInbound0 failed with error code [%x]", result);
            goto CLEANUP;
        }
    }
    else {
        if ((result = IPsecSaContextAddOutbound0(p_ctx->engine, local_saId, &bundle)) != ERROR_SUCCESS) {
            TSK_DEBUG_ERROR("IPsecSaContextAddOutbound0 failed with error code [%x]", result);
            goto CLEANUP;
        }
    }

CLEANUP:
    return (result == ERROR_SUCCESS) ? 0 : -1;
}

static int _vista_flushAll(const plugin_win_ipsec_vista_ctx_t* p_ctx)
{
#if 1
    int ret = -1;
    if (p_ctx && p_ctx->engine) {
        DWORD result;
        result = FwpmFilterDeleteById0(p_ctx->engine, p_ctx->filterId_in_uc);
        if (result != ERROR_SUCCESS && result != FWP_E_FILTER_NOT_FOUND) {
            TSK_DEBUG_ERROR("FwpmFilterDeleteById0 failed with error code [%x]", result);
        }
        result = FwpmFilterDeleteById0(p_ctx->engine, p_ctx->filterId_in_us);
        if (result != ERROR_SUCCESS && result != FWP_E_FILTER_NOT_FOUND) {
            TSK_DEBUG_ERROR("FwpmFilterDeleteById0 failed with error code [%x]", result);
        }
        result = FwpmFilterDeleteById0(p_ctx->engine, p_ctx->filterId_out_uc);
        if (result != ERROR_SUCCESS && result != FWP_E_FILTER_NOT_FOUND) {
            TSK_DEBUG_ERROR("FwpmFilterDeleteById0 failed with error code [%x]", result);
        }
        result = FwpmFilterDeleteById0(p_ctx->engine, p_ctx->filterId_out_us);
        if (result != ERROR_SUCCESS && result != FWP_E_FILTER_NOT_FOUND) {
            TSK_DEBUG_ERROR("FwpmFilterDeleteById0 failed with error code [%x]", result);
        }
        return 0;
    }
    //
    return ret;
#else
    UINT32 i;
    int ret = -1;

    if (p_ctx && p_ctx->engine) {
        HANDLE enumHandle = NULL;
        IPSEC_SA_DETAILS0** entries = NULL;
        UINT32 numEntriesReturned = 0;
        DWORD result;

        if ((result = IPsecSaCreateEnumHandle0(p_ctx->engine, NULL, &enumHandle)) != ERROR_SUCCESS) {
            TSK_DEBUG_ERROR("IPsecSaCreateEnumHandle0 failed with error code [%x].", result);
            goto CLEANUP;
        }

        if ((result = IPsecSaEnum0(p_ctx->engine, enumHandle, TINYIPSEC_SA_NUM_ENTRIES_TO_REQUEST, &entries, &numEntriesReturned)) != ERROR_SUCCESS) {
            TSK_DEBUG_ERROR("IPsecSaEnum0 failed with error code [%x].", result);
            goto CLEANUP;
        }

        for (i = 0; i<numEntriesReturned; i++) {
            IPSEC_SA_DETAILS0* entry = (entries)[i];
            if ( !wcscmp(entry->transportFilter->displayData.name, p_ctx->filter_name)) {
                if ((result = FwpmFilterDeleteById0(p_ctx->engine, entry->transportFilter->filterId)) != ERROR_SUCCESS) {
                    TSK_DEBUG_ERROR("FwpmFilterDeleteById0 failed with error code [%x].", result);
                    goto CLEANUP;
                }
            }
        }

        TSK_DEBUG_INFO("All SAs have been flushed.");
        ret = 0;

CLEANUP:
        if (entries) {
            FwpmFreeMemory0((void**)entries);
        }
        if (enumHandle) {
            if ((result = IPsecSaDestroyEnumHandle0(p_ctx->engine, enumHandle)) != ERROR_SUCCESS) {
                TSK_DEBUG_ERROR("IPsecSaDestroyEnumHandle0 failed with error code [%x].", result);
            }
        }
    }

    return ret;
#endif
}

static void _vista_deleteSaContextAndFilters(__in HANDLE engine, __in UINT64 inFilterId, __in UINT64 outFilterId, __in UINT64 saId)
{
    DWORD result;

    // Allow the LUIDs to be zero, so we can use this function to cleanup
    // partial results.
    if (saId != 0) {
        result = IPsecSaContextDeleteById0(engine, saId);
        if (result != ERROR_SUCCESS) {
            // There's not much we can do if delete fails, so continue trying to
            // clean up the remaining objects.
            TSK_DEBUG_ERROR("IPsecSaContextDeleteById0 = 0x%08X\n", result);
        }
    }
    if (outFilterId != 0) {
        result = FwpmFilterDeleteById0(engine, outFilterId);
        if (result != ERROR_SUCCESS) {
            TSK_DEBUG_ERROR("FwpmFilterDeleteById0 = 0x%08X\n", result);
        }
    }
    if (inFilterId != 0) {
        result = FwpmFilterDeleteById0(engine, inFilterId);
        if (result != ERROR_SUCCESS) {
            TSK_DEBUG_ERROR("FwpmFilterDeleteById0 = 0x%08X\n", result);
        }
    }
}

//
//	Windows Vista IPSec Plugin definition
//

/* constructor */
static tsk_object_t* _plugin_win_ipsec_vista_ctx_ctor(tsk_object_t * self, va_list * app)
{
    plugin_win_ipsec_vista_ctx_t *p_ctx = (plugin_win_ipsec_vista_ctx_t *)self;
    if (p_ctx) {
        p_ctx->pc_base = TIPSEC_CTX(p_ctx);
    }
    return self;
}
/* destructor */
static tsk_object_t* _plugin_win_ipsec_vista_ctx_dtor(tsk_object_t * self)
{
    plugin_win_ipsec_vista_ctx_t *p_ctx = (plugin_win_ipsec_vista_ctx_t *)self;
    if (p_ctx) {
        DWORD code;

        if (p_ctx->pc_base->started) {
            tipsec_ctx_stop(p_ctx->pc_base);
        }

        /* Close engine */
        if (p_ctx->engine) {
            if ((code = FwpmEngineClose0(p_ctx->engine))) {
                TSK_DEBUG_ERROR("FwpmEngineClose0 failed with error code [%x].", code);
            }
        }

        TSK_FREE(p_ctx->pc_base->addr_local);
        TSK_FREE(p_ctx->pc_base->addr_remote);

        if (p_ctx->pc_base->ik) {
            TSK_FREE(((PFWP_BYTE_BLOB)p_ctx->pc_base->ik)->data);
            TSK_FREE(p_ctx->pc_base->ik);
        }
        if (p_ctx->pc_base->ck) {
            TSK_FREE(((PFWP_BYTE_BLOB)p_ctx->pc_base->ck)->data);
            TSK_FREE(p_ctx->pc_base->ck);
        }

        TSK_DEBUG_INFO("*** Windows Vista IPSec plugin (Windows Filtering Platform) context destroyed ***");
    }

    return self;
}
/* object definition */
static const tsk_object_def_t plugin_win_ipsec_vista_ctx_def_s = {
    sizeof(plugin_win_ipsec_vista_ctx_t),
    _plugin_win_ipsec_vista_ctx_ctor,
    _plugin_win_ipsec_vista_ctx_dtor,
    tsk_null,
};
/* plugin definition*/
static const tipsec_plugin_def_t plugin_win_ipsec_vista_plugin_def_s = {
    &plugin_win_ipsec_vista_ctx_def_s,

    tipsec_impl_type_vista,
    "Windows Vista IPSec (Windows Filtering Platform)",

    _plugin_win_ipsec_vista_ctx_init,
    _plugin_win_ipsec_vista_ctx_set_local,
    _plugin_win_ipsec_vista_ctx_set_remote,
    _plugin_win_ipsec_vista_ctx_set_keys,
    _plugin_win_ipsec_vista_ctx_start,
    _plugin_win_ipsec_vista_ctx_stop,
};
const tipsec_plugin_def_t *plugin_win_ipsec_vista_plugin_def_t = &plugin_win_ipsec_vista_plugin_def_s;
OpenPOWER on IntegriCloud