summaryrefslogtreecommitdiffstats
path: root/src/decode-esp.c
blob: 1dd5b739b0d0833be1856f218965e853336514e3 (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
/* Copyright (C) 2020-2021 Open Information Security Foundation
 *
 * You can copy, redistribute or modify this Program under the terms of
 * the GNU General Public License version 2 as published by the Free
 * Software Foundation.
 *
 * This program 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
 * version 2 along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

/**
 * \ingroup decode
 *
 * @{
 */

/**
 * \file
 *
 * Decode Encapsulating Security Payload (ESP)
 */

#include "suricata-common.h"
#include "decode-esp.h"
#include "flow.h"

#include "util-validate.h"

static int DecodeESPPacket(ThreadVars *tv, Packet *p, const uint8_t *pkt, uint16_t len)
{
    DEBUG_VALIDATE_BUG_ON(pkt == NULL);

    if (unlikely(len < ESP_HEADER_LEN)) {
        ENGINE_SET_INVALID_EVENT(p, ESP_PKT_TOO_SMALL);
        return -1;
    }

    p->esph = (ESPHdr *)pkt;

    p->payload = (uint8_t *)pkt + sizeof(ESPHdr);
    p->payload_len = len - sizeof(ESPHdr);

    p->proto = IPPROTO_ESP;

    return 0;
}

/**
 * \brief Function to decode IPSEC-ESP packets
 * \param tv thread vars
 * \param dtv decoder thread vars
 * \param p packet
 * \param pkt raw packet data
 * \param len length in bytes of pkt array
 * \retval TM_ECODE_OK or TM_ECODE_FAILED on serious error
 */
int DecodeESP(ThreadVars *tv, DecodeThreadVars *dtv, Packet *p, const uint8_t *pkt, uint16_t len)
{
    DEBUG_VALIDATE_BUG_ON(pkt == NULL);

    StatsIncr(tv, dtv->counter_esp);

    if (!PacketIncreaseCheckLayers(p)) {
        return TM_ECODE_FAILED;
    }
    if (unlikely(DecodeESPPacket(tv, p, pkt, len) < 0)) {
        CLEAR_ESP_PACKET(p);
        return TM_ECODE_FAILED;
    }

    SCLogDebug("ESP spi: %" PRIu32 " sequence: %" PRIu32, ESP_GET_SPI(p), ESP_GET_SEQUENCE(p));

    FlowSetupPacket(p);

    return TM_ECODE_OK;
}

#ifdef UNITTESTS

#include "util-unittest.h"

/** \test Successful decoding */
static int DecodeESPTest01(void)
{
    uint8_t raw_esp[] = { 0x00, 0x00, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x08 };

    Packet *p = PacketGetFromAlloc();
    FAIL_IF_NULL(p);

    ThreadVars tv;
    DecodeThreadVars dtv;

    memset(&tv, 0, sizeof(ThreadVars));
    memset(&dtv, 0, sizeof(DecodeThreadVars));

    int ret = DecodeESP(&tv, &dtv, p, raw_esp, sizeof(raw_esp));
    FAIL_IF(ret != TM_ECODE_OK);

    FAIL_IF(p->proto != IPPROTO_ESP);
    FAIL_IF(p->payload_len != sizeof(raw_esp) - ESP_HEADER_LEN);
    FAIL_IF(ESP_GET_SPI(p) != 0x7b);
    FAIL_IF(ESP_GET_SEQUENCE(p) != 0x08);

    SCFree(p);

    PASS;
}

/** \test Successful decoding, with payload data */
static int DecodeESPTest02(void)
{
    uint8_t raw_esp[] = { 0x00, 0x00, 0x00, 0x7b, 0x00, 0x00, 0x00, 0x08, 0xFF, 0xFF };

    Packet *p = PacketGetFromAlloc();
    FAIL_IF_NULL(p);

    ThreadVars tv;
    DecodeThreadVars dtv;

    memset(&tv, 0, sizeof(ThreadVars));
    memset(&dtv, 0, sizeof(DecodeThreadVars));

    int ret = DecodeESP(&tv, &dtv, p, raw_esp, sizeof(raw_esp));
    FAIL_IF(ret != TM_ECODE_OK);

    FAIL_IF(p->proto != IPPROTO_ESP);
    FAIL_IF(p->payload_len != sizeof(raw_esp) - ESP_HEADER_LEN);
    FAIL_IF(memcmp(p->payload, raw_esp + ESP_HEADER_LEN, p->payload_len) != 0);
    FAIL_IF(ESP_GET_SPI(p) != 0x7b);
    FAIL_IF(ESP_GET_SEQUENCE(p) != 0x08);

    SCFree(p);

    PASS;
}

/** \test Failure decoding, not enough data */
static int DecodeESPTest03(void)
{
    uint8_t raw_esp[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

    Packet *p = PacketGetFromAlloc();
    FAIL_IF_NULL(p);

    ThreadVars tv;
    DecodeThreadVars dtv;

    memset(&tv, 0, sizeof(ThreadVars));
    memset(&dtv, 0, sizeof(DecodeThreadVars));

    int ret = DecodeESP(&tv, &dtv, p, raw_esp, sizeof(raw_esp));
    FAIL_IF(ret != TM_ECODE_FAILED);

    // expect ESP_PKT_TOO_SMALL
    FAIL_IF_NOT(ENGINE_ISSET_EVENT(p, ESP_PKT_TOO_SMALL));

    SCFree(p);

    PASS;
}

/** \test Failure decoding, no data */
static int DecodeESPTest04(void)
{
    uint8_t raw_esp[] = {};

    Packet *p = PacketGetFromAlloc();
    FAIL_IF_NULL(p);

    ThreadVars tv;
    DecodeThreadVars dtv;

    memset(&tv, 0, sizeof(ThreadVars));
    memset(&dtv, 0, sizeof(DecodeThreadVars));

    int ret = DecodeESP(&tv, &dtv, p, raw_esp, sizeof(raw_esp));
    FAIL_IF(ret != TM_ECODE_FAILED);

    // expect ESP_PKT_TOO_SMALL
    FAIL_IF_NOT(ENGINE_ISSET_EVENT(p, ESP_PKT_TOO_SMALL));

    SCFree(p);

    PASS;
}
#endif /* UNITTESTS */

void DecodeESPRegisterTests(void)
{
#ifdef UNITTESTS
    UtRegisterTest("DecodeESPTest01", DecodeESPTest01);
    UtRegisterTest("DecodeESPTest02", DecodeESPTest02);
    UtRegisterTest("DecodeESPTest03", DecodeESPTest03);
    UtRegisterTest("DecodeESPTest04", DecodeESPTest04);
#endif /* UNITTESTS */
}

/**
 * @}
 */