• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

OISF / suricata / 23374838686

21 Mar 2026 07:29AM UTC coverage: 59.341% (-20.0%) from 79.315%
23374838686

Pull #15075

github

web-flow
Merge 90b4e834f into 6587e363a
Pull Request #15075: Stack 8001 v16.4

38 of 70 new or added lines in 10 files covered. (54.29%)

34165 existing lines in 563 files now uncovered.

119621 of 201584 relevant lines covered (59.34%)

650666.92 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

79.52
/src/app-layer-ssl.c
1
/* Copyright (C) 2007-2024 Open Information Security Foundation
2
 *
3
 * You can copy, redistribute or modify this Program under the terms of
4
 * the GNU General Public License version 2 as published by the Free
5
 * Software Foundation.
6
 *
7
 * This program is distributed in the hope that it will be useful,
8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
 * GNU General Public License for more details.
11
 *
12
 * You should have received a copy of the GNU General Public License
13
 * version 2 along with this program; if not, write to the Free Software
14
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15
 * 02110-1301, USA.
16
 */
17

18
/**
19
 * \file
20
 *
21
 * \author Anoop Saldanha <anoopsaldanha@gmail.com>
22
 * \author Pierre Chifflier <pierre.chifflier@ssi.gouv.fr>
23
 * \author Mats Klepsland <mats.klepsland@gmail.com>
24
 *
25
 */
26

27
#include "suricata-common.h"
28
#include "decode.h"
29

30
#include "app-layer.h"
31
#include "app-layer-detect-proto.h"
32
#include "app-layer-protos.h"
33
#include "app-layer-parser.h"
34
#include "app-layer-frames.h"
35
#include "app-layer-events.h"
36
#include "app-layer-ssl.h"
37

38
#include "conf.h"
39

40
#include "feature.h"
41

42
#include "util-debug.h"
43
#include "util-ja3.h"
44
#include "util-enum.h"
45
#include "util-validate.h"
46

47
static SCEnumCharMap tls_state_client_table[] = {
48
    {
49
            "client_in_progress",
50
            TLS_STATE_CLIENT_IN_PROGRESS,
51
    },
52
    {
53
            "client_hello_done",
54
            TLS_STATE_CLIENT_HELLO_DONE,
55
    },
56
    {
57
            "client_cert_done",
58
            TLS_STATE_CLIENT_CERT_DONE,
59
    },
60
    {
61
            "client_handshake_done",
62
            TLS_STATE_CLIENT_HANDSHAKE_DONE,
63
    },
64
    {
65
            "client_finished",
66
            TLS_STATE_CLIENT_FINISHED,
67
    },
68
    { NULL, -1 },
69
};
70

71
static SCEnumCharMap tls_state_server_table[] = {
72
    {
73
            "server_in_progress",
74
            TLS_STATE_SERVER_IN_PROGRESS,
75
    },
76
    {
77
            "server_hello",
78
            TLS_STATE_SERVER_HELLO,
79
    },
80
    {
81
            "server_cert_done",
82
            TLS_STATE_SERVER_CERT_DONE,
83
    },
84
    {
85
            "server_hello_done",
86
            TLS_STATE_SERVER_HELLO_DONE,
87
    },
88
    {
89
            "server_handshake_done",
90
            TLS_STATE_SERVER_HANDSHAKE_DONE,
91
    },
92
    {
93
            "server_finished",
94
            TLS_STATE_SERVER_FINISHED,
95
    },
96
    { NULL, -1 },
97
};
98

99
SCEnumCharMap tls_frame_table[] = {
100
    {
101
            "pdu",
102
            TLS_FRAME_PDU,
103
    },
104
    {
105
            "hdr",
106
            TLS_FRAME_HDR,
107
    },
108
    {
109
            "data",
110
            TLS_FRAME_DATA,
111
    },
112
    {
113
            "alert",
114
            TLS_FRAME_ALERT_DATA,
115
    },
116
    {
117
            "heartbeat",
118
            TLS_FRAME_HB_DATA,
119
    },
120
    {
121
            "ssl2.hdr",
122
            TLS_FRAME_SSLV2_HDR,
123
    },
124
    {
125
            "ssl2.pdu",
126
            TLS_FRAME_SSLV2_PDU,
127
    },
128
    { NULL, -1 },
129
};
130

131
SCEnumCharMap tls_decoder_event_table[] = {
132
    /* TLS protocol messages */
133
    { "INVALID_SSLV2_HEADER", TLS_DECODER_EVENT_INVALID_SSLV2_HEADER },
134
    { "INVALID_TLS_HEADER", TLS_DECODER_EVENT_INVALID_TLS_HEADER },
135
    { "INVALID_RECORD_VERSION", TLS_DECODER_EVENT_INVALID_RECORD_VERSION },
136
    { "INVALID_RECORD_TYPE", TLS_DECODER_EVENT_INVALID_RECORD_TYPE },
137
    { "INVALID_RECORD_LENGTH", TLS_DECODER_EVENT_INVALID_RECORD_LENGTH },
138
    { "INVALID_HANDSHAKE_MESSAGE", TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE },
139
    { "HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_HEARTBEAT },
140
    { "INVALID_HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_INVALID_HEARTBEAT },
141
    { "OVERFLOW_HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_OVERFLOW_HEARTBEAT },
142
    { "DATALEAK_HEARTBEAT_MISMATCH", TLS_DECODER_EVENT_DATALEAK_HEARTBEAT_MISMATCH },
143
    { "HANDSHAKE_INVALID_LENGTH", TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH },
144
    { "MULTIPLE_SNI_EXTENSIONS", TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS },
145
    { "INVALID_SNI_TYPE", TLS_DECODER_EVENT_INVALID_SNI_TYPE },
146
    { "INVALID_SNI_LENGTH", TLS_DECODER_EVENT_INVALID_SNI_LENGTH },
147
    { "TOO_MANY_RECORDS_IN_PACKET", TLS_DECODER_EVENT_TOO_MANY_RECORDS_IN_PACKET },
148
    { "INVALID_ALERT_MESSAGE", TLS_DECODER_EVENT_INVALID_ALERT },
149
    /* certificate decoding messages */
150
    { "INVALID_CERTIFICATE", TLS_DECODER_EVENT_INVALID_CERTIFICATE },
151
    { "CERTIFICATE_INVALID_LENGTH", TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH },
152
    { "CERTIFICATE_INVALID_VERSION", TLS_DECODER_EVENT_CERTIFICATE_INVALID_VERSION },
153
    { "CERTIFICATE_INVALID_SERIAL", TLS_DECODER_EVENT_CERTIFICATE_INVALID_SERIAL },
154
    { "CERTIFICATE_INVALID_ALGORITHMIDENTIFIER",
155
            TLS_DECODER_EVENT_CERTIFICATE_INVALID_ALGORITHMIDENTIFIER },
156
    { "CERTIFICATE_INVALID_X509NAME", TLS_DECODER_EVENT_CERTIFICATE_INVALID_X509NAME },
157
    { "CERTIFICATE_INVALID_DATE", TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE },
158
    { "CERTIFICATE_INVALID_EXTENSIONS", TLS_DECODER_EVENT_CERTIFICATE_INVALID_EXTENSIONS },
159
    { "CERTIFICATE_INVALID_DER", TLS_DECODER_EVENT_CERTIFICATE_INVALID_DER },
160
    { "CERTIFICATE_INVALID_SUBJECT", TLS_DECODER_EVENT_CERTIFICATE_INVALID_SUBJECT },
161
    { "CERTIFICATE_INVALID_ISSUER", TLS_DECODER_EVENT_CERTIFICATE_INVALID_ISSUER },
162
    { "CERTIFICATE_INVALID_VALIDITY", TLS_DECODER_EVENT_CERTIFICATE_INVALID_VALIDITY },
163
    { "ERROR_MESSAGE_ENCOUNTERED", TLS_DECODER_EVENT_ERROR_MSG_ENCOUNTERED },
164
    /* used as a generic error event */
165
    { "INVALID_SSL_RECORD", TLS_DECODER_EVENT_INVALID_SSL_RECORD },
166
    { NULL, -1 },
167
};
168

169
enum {
170
    /* X.509 error codes, returned by decoder
171
     * THESE CONSTANTS MUST MATCH rust/src/x509/mod.rs ! */
172
    ERR_INVALID_CERTIFICATE=1,
173
    ERR_INVALID_LENGTH,
174
    ERR_INVALID_VERSION,
175
    ERR_INVALID_SERIAL,
176
    ERR_INVALID_ALGORITHMIDENTIFIER,
177
    ERR_INVALID_X509NAME,
178
    ERR_INVALID_DATE,
179
    ERR_INVALID_EXTENSIONS,
180
    ERR_INVALID_DER,
181

182
    /* error getting data */
183
    ERR_EXTRACT_SUBJECT,
184
    ERR_EXTRACT_ISSUER,
185
    ERR_EXTRACT_VALIDITY,
186
};
187

188
/* JA3 and JA4 fingerprints are disabled by default */
189
#define SSL_CONFIG_DEFAULT_JA3 0
8✔
190
#ifdef HAVE_JA4
191
#define SSL_CONFIG_DEFAULT_JA4 0
8✔
192
#endif
193

194
enum SslConfigEncryptHandling {
195
    SSL_CNF_ENC_HANDLE_TRACK_ONLY = 0, /**< disable raw content, continue tracking */
196
    SSL_CNF_ENC_HANDLE_BYPASS = 1,     /**< skip processing of flow, bypass if possible */
197
    SSL_CNF_ENC_HANDLE_FULL = 2,       /**< handle fully like any other proto */
198
};
199

200
typedef struct SslConfig_ {
201
    enum SslConfigEncryptHandling encrypt_mode;
202
    /** dynamic setting for ja3 and ja4: can be enabled on demand if not
203
     *  explicitly disabled. */
204
    SC_ATOMIC_DECLARE(int, enable_ja3);
205
    bool disable_ja3; /**< ja3 explicitly disabled. Don't enable on demand. */
206
    SC_ATOMIC_DECLARE(int, enable_ja4);
207
    bool disable_ja4; /**< ja4 explicitly disabled. Don't enable on demand. */
208
} SslConfig;
209

210
SslConfig ssl_config;
211

212
/* SSLv3 record types */
213
#define SSLV3_CHANGE_CIPHER_SPEC       20
7,489✔
214
#define SSLV3_ALERT_PROTOCOL           21
3,896✔
215
#define SSLV3_HANDSHAKE_PROTOCOL       22
32,246✔
216
#define SSLV3_APPLICATION_PROTOCOL     23
62,520✔
217
#define SSLV3_HEARTBEAT_PROTOCOL       24
33,814✔
218

219
/* SSLv3 handshake protocol types */
220
#define SSLV3_HS_HELLO_REQUEST          0
8,954✔
221
#define SSLV3_HS_CLIENT_HELLO           1
5,405✔
222
#define SSLV3_HS_SERVER_HELLO           2
7,744✔
223
#define SSLV3_HS_NEW_SESSION_TICKET     4
9,094✔
224
#define SSLV3_HS_CERTIFICATE           11
10,169✔
225
#define SSLV3_HS_SERVER_KEY_EXCHANGE   12
7,143✔
226
#define SSLV3_HS_CERTIFICATE_REQUEST   13
8,823✔
227
#define SSLV3_HS_SERVER_HELLO_DONE     14
11,875✔
228
#define SSLV3_HS_CERTIFICATE_VERIFY    15
8,824✔
229
#define SSLV3_HS_CLIENT_KEY_EXCHANGE   16
8,179✔
230
#define SSLV3_HS_FINISHED              20
8,828✔
231
#define SSLV3_HS_CERTIFICATE_URL       21
8,828✔
232
#define SSLV3_HS_CERTIFICATE_STATUS    22
9,292✔
233

234
/* SSLv2 protocol message types */
235
#define SSLV2_MT_ERROR                  0
1✔
236
#define SSLV2_MT_CLIENT_HELLO           1
64✔
237
#define SSLV2_MT_CLIENT_MASTER_KEY      2
×
238
#define SSLV2_MT_CLIENT_FINISHED        3
2✔
239
#define SSLV2_MT_SERVER_HELLO           4
1✔
240
#define SSLV2_MT_SERVER_VERIFY          5
1✔
241
#define SSLV2_MT_SERVER_FINISHED        6
1✔
242
#define SSLV2_MT_REQUEST_CERTIFICATE    7
2✔
243
#define SSLV2_MT_CLIENT_CERTIFICATE     8
1✔
244

245
#define SSLV3_RECORD_HDR_LEN 5
376,302✔
246
/** max length according to RFC 5246 6.2.2 is 2^14 + 1024 */
247
#define SSLV3_RECORD_MAX_LEN ((1 << 14) + 1024)
33,775✔
248

249
#define SSLV3_CLIENT_HELLO_VERSION_LEN  2
5,212✔
250
#define SSLV3_CLIENT_HELLO_RANDOM_LEN  32
5,211✔
251

252
/* TLS heartbeat protocol types */
253
#define TLS_HB_REQUEST                  1
×
254
#define TLS_HB_RESPONSE                 2
×
255

256
#define SSL_RECORD_MINIMUM_LENGTH       6
257

258
#define SHA1_STRING_LENGTH             60
1,379✔
259

260
#define HAS_SPACE(n) ((uint64_t)(input - initial_input) + (uint64_t)(n) <= (uint64_t)(input_len))
283,201✔
261

262
struct SSLDecoderResult {
263
    int retval;      // nr bytes consumed from input, or < 0 on error
264
    uint32_t needed; // more bytes needed
265
};
266
#define SSL_DECODER_ERROR(e)                                                                       \
267
    (struct SSLDecoderResult)                                                                      \
103✔
268
    {                                                                                              \
103✔
269
        (e), 0                                                                                     \
103✔
270
    }
103✔
271
#define SSL_DECODER_OK(c)                                                                          \
272
    (struct SSLDecoderResult)                                                                      \
38,863✔
273
    {                                                                                              \
38,863✔
274
        (uint32_t)(c), 0                                                                           \
38,863✔
275
    }
38,863✔
276
#define SSL_DECODER_INCOMPLETE(c, n)                                                               \
277
    (struct SSLDecoderResult)                                                                      \
1,428✔
278
    {                                                                                              \
1,428✔
279
        (uint32_t)(c), (n)                                                                         \
1,428✔
280
    }
1,428✔
281

282
static inline int SafeMemcpy(void *dst, size_t dst_offset, size_t dst_size,
283
        const void *src, size_t src_offset, size_t src_size, size_t src_tocopy) WARN_UNUSED;
284

285
static inline int SafeMemcpy(void *dst, size_t dst_offset, size_t dst_size,
286
        const void *src, size_t src_offset, size_t src_size, size_t src_tocopy)
287
{
6,057✔
288
    DEBUG_VALIDATE_BUG_ON(dst_offset >= dst_size);
6,057✔
289
    DEBUG_VALIDATE_BUG_ON(src_offset >= src_size);
6,057✔
290
    DEBUG_VALIDATE_BUG_ON(src_tocopy > (src_size - src_offset));
6,057✔
291
    DEBUG_VALIDATE_BUG_ON(src_tocopy > (dst_size - dst_offset));
6,057✔
292

293
    if (dst_offset < dst_size && src_offset < src_size &&
6,057✔
294
        src_tocopy <= (src_size - src_offset) &&
6,057✔
295
        src_tocopy <= (dst_size - dst_offset)) {
6,057✔
296
        memcpy(dst + dst_offset, src + src_offset, src_tocopy);
6,057✔
297
        return 0;
6,057✔
298
    }
6,057✔
UNCOV
299
    return -1;
×
300
}
6,057✔
301

302
#ifdef DEBUG_VALIDATION
303
#define ValidateRecordState(connp)                                              \
304
    do {                                                                        \
305
        DEBUG_VALIDATE_BUG_ON(((connp)->record_length + SSLV3_RECORD_HDR_LEN) < \
306
                (connp)->bytes_processed);                                      \
307
    } while(0);
308
#else
309
#define ValidateRecordState(...)
310
#endif
311

312
#define SSLParserHSReset(connp)                                                                    \
313
    do {                                                                                           \
45,843✔
314
        (connp)->handshake_type = 0;                                                               \
45,843✔
315
        (connp)->message_length = 0;                                                               \
45,843✔
316
    } while (0)
45,843✔
317

318
#define SSLParserReset(state)                       \
319
    do {                                            \
33,804✔
320
        SCLogDebug("resetting state");              \
33,804✔
321
        (state)->curr_connp->bytes_processed = 0;   \
33,804✔
322
        SSLParserHSReset((state)->curr_connp);      \
33,804✔
323
    } while(0)
33,804✔
324

325
#define SSLSetEvent(ssl_state, event)                                                              \
326
    do {                                                                                           \
3,002✔
327
        SCLogDebug("setting event %u", (event));                                                   \
3,002✔
328
        if ((ssl_state) == NULL) {                                                                 \
3,002✔
329
            SCLogDebug("could not set decoder event %u", event);                                   \
×
330
        } else {                                                                                   \
3,002✔
331
            SCAppLayerDecoderEventsSetEventRaw(&(ssl_state)->tx_data.events, (event));             \
3,002✔
332
            (ssl_state)->events++;                                                                 \
3,002✔
333
        }                                                                                          \
3,002✔
334
    } while (0)
3,002✔
335

336
static void *SSLGetTx(void *state, uint64_t tx_id)
337
{
110,094✔
338
    SSLState *ssl_state = (SSLState *)state;
110,094✔
339
    return ssl_state;
110,094✔
340
}
110,094✔
341

342
static uint64_t SSLGetTxCnt(void *state)
343
{
156,745✔
344
    /* single tx */
345
    return 1;
156,745✔
346
}
156,745✔
347

348
static void UpdateClientState(SSLState *ssl_state, enum TlsStateClient s)
349
{
19,741✔
350
#ifdef DEBUG
351
    enum TlsStateClient old = ssl_state->client_state;
352
#endif
353
    ssl_state->client_state = s;
19,741✔
354
#ifdef DEBUG
355
    SCLogDebug("toserver: state updated to %u from %u", s, old);
356
#endif
357
}
19,741✔
358

359
static void UpdateServerState(SSLState *ssl_state, enum TlsStateServer s)
360
{
29,958✔
361
#ifdef DEBUG
362
    enum TlsStateServer old = ssl_state->server_state;
363
#endif
364
    ssl_state->server_state = s;
29,958✔
365
#ifdef DEBUG
366
    SCLogDebug("toclient: state updated to %u from %u", s, old);
367
#endif
368
}
29,958✔
369

370
static int SSLGetAlstateProgress(void *tx, uint8_t direction)
371
{
151,650✔
372
    SSLState *ssl_state = (SSLState *)tx;
151,650✔
373
    if (direction & STREAM_TOCLIENT) {
151,650✔
374
        return ssl_state->server_state;
97,633✔
375
    } else {
97,633✔
376
        return ssl_state->client_state;
54,017✔
377
    }
54,017✔
378
}
151,650✔
379

380
static AppLayerTxData *SSLGetTxData(void *vtx)
381
{
135,241✔
382
    SSLState *ssl_state = (SSLState *)vtx;
135,241✔
383
    return &ssl_state->tx_data;
135,241✔
384
}
135,241✔
385

386
static AppLayerStateData *SSLGetStateData(void *vstate)
387
{
3,046✔
388
    SSLState *ssl_state = (SSLState *)vstate;
3,046✔
389
    return &ssl_state->state_data;
3,046✔
390
}
3,046✔
391

392
static void TlsDecodeHSCertificateErrSetEvent(SSLState *ssl_state, uint32_t err)
393
{
2,581✔
394
    switch(err) {
2,581✔
395
        case ERR_EXTRACT_VALIDITY:
×
396
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_VALIDITY);
×
397
            break;
×
398
        case ERR_EXTRACT_ISSUER:
×
399
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_ISSUER);
×
400
            break;
×
401
        case ERR_EXTRACT_SUBJECT:
×
402
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_SUBJECT);
×
403
            break;
×
404
        case ERR_INVALID_DER:
85✔
405
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_DER);
85✔
406
            break;
85✔
407
        case ERR_INVALID_EXTENSIONS:
7✔
408
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_EXTENSIONS);
7✔
409
            break;
7✔
410
        case ERR_INVALID_DATE:
27✔
411
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE);
27✔
412
            break;
27✔
413
        case ERR_INVALID_X509NAME:
×
414
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_X509NAME);
×
415
            break;
×
416
        case ERR_INVALID_ALGORITHMIDENTIFIER:
1✔
417
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_ALGORITHMIDENTIFIER);
1✔
418
            break;
1✔
419
        case ERR_INVALID_SERIAL:
4✔
420
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_SERIAL);
4✔
421
            break;
4✔
422
        case ERR_INVALID_VERSION:
5✔
423
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_VERSION);
5✔
424
            break;
5✔
425
        case ERR_INVALID_LENGTH:
2,347✔
426
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH);
2,347✔
427
            break;
2,347✔
428
        case ERR_INVALID_CERTIFICATE:
105✔
429
        default:
105✔
430
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_CERTIFICATE);
105✔
431
            break;
105✔
432
    }
2,581✔
433
}
2,581✔
434

435
static inline int TlsDecodeHSCertificateFingerprint(
436
        SSLStateConnp *connp, const uint8_t *input, uint32_t cert_len)
437
{
1,379✔
438
    if (unlikely(connp->cert0_fingerprint != NULL))
1,379✔
439
        return 0;
×
440

441
    connp->cert0_fingerprint = SCCalloc(1, SHA1_STRING_LENGTH);
1,379✔
442
    if (connp->cert0_fingerprint == NULL)
1,379✔
443
        return -1;
×
444

445
    uint8_t hash[SC_SHA1_LEN];
1,379✔
446
    if (SCSha1HashBuffer(input, cert_len, hash, sizeof(hash)) == 1) {
1,379✔
447
        SCToHex_sep(
1,379✔
448
                (uint8_t *)connp->cert0_fingerprint, SHA1_STRING_LENGTH, ':', hash, SC_SHA1_LEN);
1,379✔
449
    }
1,379✔
450
    return 0;
1,379✔
451
}
1,379✔
452

453
static inline int TlsDecodeHSCertificateAddCertToChain(
454
        SSLStateConnp *connp, const uint8_t *input, uint32_t cert_len)
455
{
2,796✔
456
    SSLCertsChain *cert = SCCalloc(1, sizeof(SSLCertsChain));
2,796✔
457
    if (cert == NULL)
2,796✔
458
        return -1;
×
459

460
    cert->cert_data = (uint8_t *)input;
2,796✔
461
    cert->cert_len = cert_len;
2,796✔
462
    TAILQ_INSERT_TAIL(&connp->certs, cert, next);
2,796✔
463

464
    return 0;
2,796✔
465
}
2,796✔
466

467
static int TlsDecodeHSCertificate(SSLState *ssl_state, SSLStateConnp *connp,
468
        const uint8_t *const initial_input, const uint32_t input_len, const int certn)
469
{
5,394✔
470
    const uint8_t *input = (uint8_t *)initial_input;
5,394✔
471
    uint32_t err_code = 0;
5,394✔
472
    X509 *x509 = NULL;
5,394✔
473
    int rc = 0;
5,394✔
474

475
    if (!(HAS_SPACE(3)))
5,394✔
476
        goto invalid_cert;
5✔
477

478
    uint32_t cert_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
5,389✔
479
    input += 3;
5,389✔
480

481
    if (!(HAS_SPACE(cert_len)))
5,389✔
482
        goto invalid_cert;
12✔
483

484
    /* only store fields from the first certificate in the chain */
485
    if (certn == 0 && connp->cert0_subject == NULL && connp->cert0_issuerdn == NULL &&
5,377✔
486
            connp->cert0_serial == NULL) {
5,377✔
487
        x509 = SCX509Decode(input, cert_len, &err_code);
3,960✔
488
        if (x509 == NULL) {
3,960✔
489
            TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
2,581✔
490
            goto next;
2,581✔
491
        }
2,581✔
492

493
        SCX509GetSubject(x509, &connp->cert0_subject, &connp->cert0_subject_len);
1,379✔
494
        if (connp->cert0_subject == NULL) {
1,379✔
495
            err_code = ERR_EXTRACT_SUBJECT;
×
496
            goto error;
×
497
        }
×
498

499
        SCX509GetIssuer(x509, &connp->cert0_issuerdn, &connp->cert0_issuerdn_len);
1,379✔
500
        if (connp->cert0_issuerdn == NULL) {
1,379✔
501
            err_code = ERR_EXTRACT_ISSUER;
×
502
            goto error;
×
503
        }
×
504

505
        connp->cert0_sans_num = SCX509GetSubjectAltNameLen(x509);
1,379✔
506
        connp->cert0_sans = SCCalloc(connp->cert0_sans_num, sizeof(SSLSubjectAltName));
1,379✔
507
        if (connp->cert0_sans == NULL) {
1,379✔
508
            goto error;
×
509
        }
×
510
        for (uint16_t i = 0; i < connp->cert0_sans_num; i++) {
11,661✔
511
            SCX509GetSubjectAltNameAt(
10,282✔
512
                    x509, i, &connp->cert0_sans[i].san, &connp->cert0_sans[i].san_len);
10,282✔
513
        }
10,282✔
514

515
        SCX509GetSerial(x509, &connp->cert0_serial, &connp->cert0_serial_len);
1,379✔
516
        if (connp->cert0_serial == NULL) {
1,379✔
517
            err_code = ERR_INVALID_SERIAL;
×
518
            goto error;
×
519
        }
×
520

521
        rc = SCX509GetValidity(x509, &connp->cert0_not_before, &connp->cert0_not_after);
1,379✔
522
        if (rc != 0) {
1,379✔
523
            err_code = ERR_EXTRACT_VALIDITY;
×
524
            goto error;
×
525
        }
×
526

527
        SCX509Free(x509);
1,379✔
528
        x509 = NULL;
1,379✔
529

530
        rc = TlsDecodeHSCertificateFingerprint(connp, input, cert_len);
1,379✔
531
        if (rc != 0) {
1,379✔
532
            SCLogDebug("TlsDecodeHSCertificateFingerprint failed with %d", rc);
×
533
            goto error;
×
534
        }
×
535
    }
1,379✔
536

537
    rc = TlsDecodeHSCertificateAddCertToChain(connp, input, cert_len);
2,796✔
538
    if (rc != 0) {
2,796✔
539
        SCLogDebug("TlsDecodeHSCertificateAddCertToChain failed with %d", rc);
×
540
        goto error;
×
541
    }
×
542

543
next:
5,377✔
544
    input += cert_len;
5,377✔
545
    return (int)(input - initial_input);
5,377✔
546

547
error:
×
548
    if (err_code != 0)
×
549
        TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
×
550
    if (x509 != NULL)
×
551
        SCX509Free(x509);
×
552

553
    return -1;
×
554

555
invalid_cert:
17✔
556
    SCLogDebug("TLS invalid certificate");
17✔
557
    SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_CERTIFICATE);
17✔
558
    return -1;
17✔
559
}
2,796✔
560

561
/** \internal
562
 * \brief parse cert data in a certificate handshake message
563
 *        will be called with all data.
564
 * \retval consumed bytes consumed or -1 on error
565
 */
566
static int TlsDecodeHSCertificates(SSLState *ssl_state, SSLStateConnp *connp,
567
        const uint8_t *const initial_input, const uint32_t input_len)
568
{
1,491✔
569
    const uint8_t *input = (uint8_t *)initial_input;
1,491✔
570

571
    if (!(HAS_SPACE(3)))
1,491✔
572
        return -1;
×
573

574
    const uint32_t cert_chain_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
1,491✔
575
    input += 3;
1,491✔
576

577
    if (!(HAS_SPACE(cert_chain_len)))
1,491✔
578
        return -1;
2✔
579

580
    if (connp->certs_buffer != NULL) {
1,489✔
581
        // TODO should we set an event here?
582
        return -1;
×
583
    }
×
584

585
    connp->certs_buffer = SCCalloc(1, cert_chain_len);
1,489✔
586
    if (connp->certs_buffer == NULL) {
1,489✔
587
        return -1;
×
588
    }
×
589
    connp->certs_buffer_size = cert_chain_len;
1,489✔
590
    memcpy(connp->certs_buffer, input, cert_chain_len);
1,489✔
591

592
    int cert_cnt = 0;
1,489✔
593
    uint32_t processed_len = 0;
1,489✔
594
    /* coverity[tainted_data] */
595
    while (processed_len < cert_chain_len) {
6,866✔
596
        int rc = TlsDecodeHSCertificate(ssl_state, connp, connp->certs_buffer + processed_len,
5,394✔
597
                connp->certs_buffer_size - processed_len, cert_cnt);
5,394✔
598
        if (rc <= 0) { // 0 should be impossible, but lets be defensive
5,394✔
599
            return -1;
17✔
600
        }
17✔
601
        DEBUG_VALIDATE_BUG_ON(processed_len + (uint32_t)rc > cert_chain_len);
5,377✔
602
        if (processed_len + (uint32_t)rc > cert_chain_len) {
5,377✔
603
            return -1;
×
604
        }
×
605

606
        processed_len += (uint32_t)rc;
5,377✔
607
    }
5,377✔
608

609
    return processed_len + 3;
1,472✔
610
}
1,489✔
611

612
/**
613
 * \inline
614
 * \brief Check if value is GREASE.
615
 *
616
 * http://tools.ietf.org/html/draft-davidben-tls-grease-00
617
 *
618
 * \param value Value to check.
619
 *
620
 * \retval 1 if is GREASE.
621
 * \retval 0 if not is GREASE.
622
 */
623
static inline int TLSDecodeValueIsGREASE(const uint16_t value)
624
{
148,529✔
625
    switch (value)
148,529✔
626
    {
148,529✔
627
        case 0x0a0a:
171✔
628
        case 0x1a1a:
238✔
629
        case 0x2a2a:
322✔
630
        case 0x3a3a:
473✔
631
        case 0x4a4a:
475✔
632
        case 0x5a5a:
508✔
633
        case 0x6a6a:
620✔
634
        case 0x7a7a:
673✔
635
        case 0x8a8a:
833✔
636
        case 0x9a9a:
834✔
637
        case 0xaaaa:
865✔
638
        case 0xbaba:
1,005✔
639
        case 0xcaca:
1,027✔
640
        case 0xdada:
1,071✔
641
        case 0xeaea:
1,160✔
642
        case 0xfafa:
1,217✔
643
            return 1;
1,217✔
644
        default:
147,312✔
645
            return 0;
147,312✔
646
    }
148,529✔
647
}
148,529✔
648

649
static inline int TLSDecodeHSHelloVersion(SSLState *ssl_state,
650
                                          const uint8_t * const initial_input,
651
                                          const uint32_t input_len)
652
{
5,215✔
653
    uint8_t *input = (uint8_t *)initial_input;
5,215✔
654

655
    if (!(HAS_SPACE(SSLV3_CLIENT_HELLO_VERSION_LEN))) {
5,215✔
656
        SCLogDebug("TLS handshake invalid length");
3✔
657
        SSLSetEvent(ssl_state,
3✔
658
                    TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
3✔
659
        return -1;
3✔
660
    }
3✔
661

662
    uint16_t version = (uint16_t)(*input << 8) | *(input + 1);
5,212✔
663
    ssl_state->curr_connp->version = version;
5,212✔
664

665
    if (ssl_state->current_flags &
5,212✔
666
            (SSL_AL_FLAG_STATE_CLIENT_HELLO | SSL_AL_FLAG_STATE_SERVER_HELLO)) {
5,212✔
667
        SCTLSHandshakeSetTLSVersion(ssl_state->curr_connp->hs, version);
5,212✔
668
    }
5,212✔
669

670
    /* TLSv1.3 draft1 to draft21 use the version field as earlier TLS
671
       versions, instead of using the supported versions extension. */
672
    if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
5,212✔
673
            ((ssl_state->curr_connp->version == TLS_VERSION_13) ||
5,212✔
674
            (((ssl_state->curr_connp->version >> 8) & 0xff) == 0x7f))) {
2,516✔
675
        ssl_state->flags |= SSL_AL_FLAG_LOG_WITHOUT_CERT;
5✔
676
    }
5✔
677

678
    /* Catch some early TLSv1.3 draft implementations that does not conform
679
       to the draft version. */
680
    if ((ssl_state->curr_connp->version >= 0x7f01) &&
5,212✔
681
            (ssl_state->curr_connp->version < 0x7f10)) {
5,212✔
682
        ssl_state->curr_connp->version = TLS_VERSION_13_PRE_DRAFT16;
×
683
    }
×
684

685
    /* TLSv1.3 drafts from draft1 to draft15 use 0x0304 (TLSv1.3) as the
686
       version number, which makes it hard to accurately pinpoint the
687
       exact draft version. */
688
    else if (ssl_state->curr_connp->version == TLS_VERSION_13) {
5,212✔
689
        ssl_state->curr_connp->version = TLS_VERSION_13_PRE_DRAFT16;
3✔
690
    }
3✔
691

692
    if (SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_str == NULL) {
5,212✔
693
        ssl_state->curr_connp->ja3_str = Ja3BufferInit();
5,193✔
694
        if (ssl_state->curr_connp->ja3_str == NULL)
5,193✔
695
            return -1;
×
696

697
        int rc = Ja3BufferAddValue(&ssl_state->curr_connp->ja3_str, version);
5,193✔
698
        if (rc != 0)
5,193✔
699
            return -1;
×
700
    }
5,193✔
701

702
    input += SSLV3_CLIENT_HELLO_VERSION_LEN;
5,212✔
703

704
    return (int)(input - initial_input);
5,212✔
705
}
5,212✔
706

707
static inline int TLSDecodeHSHelloRandom(SSLState *ssl_state,
708
                                         const uint8_t * const initial_input,
709
                                         const uint32_t input_len)
710
{
5,212✔
711
    uint8_t *input = (uint8_t *)initial_input;
5,212✔
712

713
    if (!(HAS_SPACE(SSLV3_CLIENT_HELLO_RANDOM_LEN))) {
5,212✔
714
        SCLogDebug("TLS handshake invalid length");
1✔
715
        SSLSetEvent(ssl_state,
1✔
716
                    TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
1✔
717
        return -1;
1✔
718
    }
1✔
719

720
    if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
5,211✔
721
        memcpy(ssl_state->server_connp.random, input, TLS_RANDOM_LEN);
2,516✔
722
        ssl_state->flags |= TLS_TS_RANDOM_SET;
2,516✔
723
    } else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
2,695✔
724
        memcpy(ssl_state->client_connp.random, input, TLS_RANDOM_LEN);
2,695✔
725
        ssl_state->flags |= TLS_TC_RANDOM_SET;
2,695✔
726
    }
2,695✔
727

728
    /* Skip random */
729
    input += SSLV3_CLIENT_HELLO_RANDOM_LEN;
5,211✔
730

731
    return (int)(input - initial_input);
5,211✔
732
}
5,212✔
733

734
static inline int TLSDecodeHSHelloSessionID(SSLState *ssl_state,
735
                                            const uint8_t * const initial_input,
736
                                            const uint32_t input_len)
737
{
5,206✔
738
    uint8_t *input = (uint8_t *)initial_input;
5,206✔
739

740
    if (!(HAS_SPACE(1)))
5,206✔
741
        goto invalid_length;
×
742

743
    uint8_t session_id_length = *input;
5,206✔
744
    input += 1;
5,206✔
745

746
    if (!(HAS_SPACE(session_id_length)))
5,206✔
747
        goto invalid_length;
12✔
748

749
    if (session_id_length != 0 && ssl_state->curr_connp->session_id == NULL) {
5,194✔
750
        ssl_state->curr_connp->session_id = SCMalloc(session_id_length);
3,577✔
751

752
        if (unlikely(ssl_state->curr_connp->session_id == NULL)) {
3,577✔
753
            return -1;
×
754
        }
×
755

756
        if (SafeMemcpy(ssl_state->curr_connp->session_id, 0, session_id_length,
3,577✔
757
                    input, 0, input_len, session_id_length) != 0) {
3,577✔
758
            return -1;
×
759
        }
×
760
        ssl_state->curr_connp->session_id_length = session_id_length;
3,577✔
761

762
        if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
3,577✔
763
                ssl_state->client_connp.session_id != NULL &&
3,577✔
764
                ssl_state->server_connp.session_id != NULL) {
3,577✔
765
            if ((ssl_state->client_connp.session_id_length ==
1,474✔
766
                    ssl_state->server_connp.session_id_length) &&
1,474✔
767
                    (memcmp(ssl_state->server_connp.session_id,
1,474✔
768
                    ssl_state->client_connp.session_id, session_id_length) == 0)) {
1,471✔
769
                ssl_state->flags |= SSL_AL_FLAG_SESSION_RESUMED;
897✔
770
            }
897✔
771
        }
1,474✔
772
    }
3,577✔
773

774
    input += session_id_length;
5,194✔
775

776
    return (int)(input - initial_input);
5,194✔
777

778
invalid_length:
12✔
779
    SCLogDebug("TLS handshake invalid length");
12✔
780
    SSLSetEvent(ssl_state,
12✔
781
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
12✔
782
    return -1;
12✔
783
}
5,194✔
784

785
static inline int TLSDecodeHSHelloCipherSuites(SSLState *ssl_state,
786
                                           const uint8_t * const initial_input,
787
                                           const uint32_t input_len)
788
{
5,199✔
789
    const uint8_t *input = initial_input;
5,199✔
790

791
    if (!(HAS_SPACE(2)))
5,199✔
792
        goto invalid_length;
1✔
793

794
    uint16_t cipher_suites_length;
5,198✔
795

796
    if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
5,198✔
797
        cipher_suites_length = 2;
2,505✔
798
    } else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
2,693✔
799
        cipher_suites_length = (uint16_t)(*input << 8) | *(input + 1);
2,693✔
800
        input += 2;
2,693✔
801
    } else {
2,693✔
UNCOV
802
        return -1;
×
UNCOV
803
    }
×
804

805
    if (!(HAS_SPACE(cipher_suites_length)))
5,198✔
806
        goto invalid_length;
18✔
807

808
    /* Cipher suites length should always be divisible by 2 */
809
    if ((cipher_suites_length % 2) != 0) {
5,180✔
810
        goto invalid_length;
3✔
811
    }
3✔
812

813
    const bool enable_ja3 =
5,177✔
814
            SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_hash == NULL;
5,177✔
815

816
    JA3Buffer *ja3_cipher_suites = NULL;
5,177✔
817

818
    if (enable_ja3) {
5,177✔
819
        ja3_cipher_suites = Ja3BufferInit();
5,165✔
820
        if (ja3_cipher_suites == NULL)
5,165✔
821
            return -1;
×
822
    }
5,165✔
823

824
    uint16_t processed_len = 0;
5,177✔
825
    /* coverity[tainted_data] */
826
    while (processed_len < cipher_suites_length) {
68,835✔
827
        if (!(HAS_SPACE(2))) {
63,658✔
828
            if (enable_ja3) {
×
829
                Ja3BufferFree(&ja3_cipher_suites);
×
830
            }
×
831
            goto invalid_length;
×
832
        }
×
833

834
        uint16_t cipher_suite = (uint16_t)(*input << 8) | *(input + 1);
63,658✔
835
        input += 2;
63,658✔
836

837
        if (TLSDecodeValueIsGREASE(cipher_suite) != 1) {
63,658✔
838
            if (ssl_state->current_flags &
63,392✔
839
                    (SSL_AL_FLAG_STATE_CLIENT_HELLO | SSL_AL_FLAG_STATE_SERVER_HELLO)) {
63,392✔
840
                SCTLSHandshakeAddCipher(ssl_state->curr_connp->hs, cipher_suite);
63,392✔
841
            }
63,392✔
842
            if (enable_ja3) {
63,392✔
843
                int rc = Ja3BufferAddValue(&ja3_cipher_suites, cipher_suite);
63,098✔
844
                if (rc != 0) {
63,098✔
845
                    return -1;
×
846
                }
×
847
            }
63,098✔
848
        }
63,392✔
849
        processed_len += 2;
63,658✔
850
    }
63,658✔
851

852
    if (enable_ja3) {
5,177✔
853
        int rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str, &ja3_cipher_suites);
5,165✔
854
        if (rc == -1) {
5,165✔
855
            return -1;
×
856
        }
×
857
    }
5,165✔
858

859
    return (int)(input - initial_input);
5,177✔
860

861
invalid_length:
22✔
862
    SCLogDebug("TLS handshake invalid length");
22✔
863
    SSLSetEvent(ssl_state,
22✔
864
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
22✔
865
    return -1;
22✔
866
}
5,177✔
867

868
static inline int TLSDecodeHSHelloCompressionMethods(SSLState *ssl_state,
869
                                           const uint8_t * const initial_input,
870
                                           const uint32_t input_len)
871
{
5,172✔
872
    const uint8_t *input = initial_input;
5,172✔
873

874
    if (!(HAS_SPACE(1)))
5,172✔
875
        goto invalid_length;
1✔
876

877
    /* Skip compression methods */
878
    if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
5,171✔
879
        input += 1;
2,500✔
880
    } else {
2,671✔
881
        uint8_t compression_methods_length = *input;
2,671✔
882
        input += 1;
2,671✔
883

884
        if (!(HAS_SPACE(compression_methods_length)))
2,671✔
885
            goto invalid_length;
5✔
886

887
        input += compression_methods_length;
2,666✔
888
    }
2,666✔
889

890
    return (int)(input - initial_input);
5,166✔
891

892
invalid_length:
6✔
893
    SCLogDebug("TLS handshake invalid_length");
6✔
894
    SSLSetEvent(ssl_state,
6✔
895
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
6✔
896
    return -1;
6✔
897
}
5,171✔
898

899
static inline int TLSDecodeHSHelloExtensionSni(SSLState *ssl_state,
900
                                           const uint8_t * const initial_input,
901
                                           const uint32_t input_len)
902
{
3,652✔
903
    uint8_t *input = (uint8_t *)initial_input;
3,652✔
904

905
    /* Empty extension */
906
    if (input_len == 0)
3,652✔
907
        return 0;
1,213✔
908

909
    if (!(HAS_SPACE(2)))
2,439✔
910
        goto invalid_length;
2✔
911

912
    /* Skip sni_list_length */
913
    input += 2;
2,437✔
914

915
    if (!(HAS_SPACE(1)))
2,437✔
916
        goto invalid_length;
×
917

918
    uint8_t sni_type = *input;
2,437✔
919
    input += 1;
2,437✔
920

921
    /* Currently the only type allowed is host_name
922
       (RFC6066 section 3). */
923
    if (sni_type != SSL_SNI_TYPE_HOST_NAME) {
2,437✔
924
        SCLogDebug("Unknown SNI type");
8✔
925
        SSLSetEvent(ssl_state,
8✔
926
                TLS_DECODER_EVENT_INVALID_SNI_TYPE);
8✔
927
        return -1;
8✔
928
    }
8✔
929

930
    if (!(HAS_SPACE(2)))
2,429✔
931
        goto invalid_length;
1✔
932

933
    uint16_t sni_len = (uint16_t)(*input << 8) | *(input + 1);
2,428✔
934
    input += 2;
2,428✔
935

936
    /* host_name contains the fully qualified domain name,
937
       and should therefore be limited by the maximum domain
938
       name length. */
939
    if (!(HAS_SPACE(sni_len)) || sni_len > 255 || sni_len == 0) {
2,428✔
940
        SSLSetEvent(ssl_state,
7✔
941
                TLS_DECODER_EVENT_INVALID_SNI_LENGTH);
7✔
942
        return -1;
7✔
943
    }
7✔
944

945
    /* There must not be more than one extension of the same
946
       type (RFC5246 section 7.4.1.4). */
947
    if (ssl_state->curr_connp->sni) {
2,421✔
948
        SCLogDebug("Multiple SNI extensions");
4✔
949
        SSLSetEvent(ssl_state,
4✔
950
                TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS);
4✔
951
        input += sni_len;
4✔
952
        return (int)(input - initial_input);
4✔
953
    }
4✔
954

955
    ssl_state->curr_connp->sni_len = sni_len;
2,417✔
956
    ssl_state->curr_connp->sni = SCMalloc(sni_len);
2,417✔
957
    if (unlikely(ssl_state->curr_connp->sni == NULL))
2,417✔
958
        return -1;
×
959

960
    const size_t consumed = input - initial_input;
2,417✔
961
    if (SafeMemcpy(ssl_state->curr_connp->sni, 0, sni_len, initial_input, consumed, input_len,
2,417✔
962
                sni_len) != 0) {
2,417✔
963
        SCFree(ssl_state->curr_connp->sni);
×
964
        ssl_state->curr_connp->sni = NULL;
×
965
        return -1;
×
966
    }
×
967
    input += sni_len;
2,417✔
968

969
    return (int)(input - initial_input);
2,417✔
970

971
invalid_length:
3✔
972
    SCLogDebug("TLS handshake invalid length");
3✔
973
    SSLSetEvent(ssl_state,
3✔
974
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
3✔
975

976

977
    return -1;
3✔
978
}
2,417✔
979

980
static inline int TLSDecodeHSHelloExtensionSupportedVersions(SSLState *ssl_state,
981
                                             const uint8_t * const initial_input,
982
                                             const uint32_t input_len)
983
{
1,698✔
984
    const uint8_t *input = initial_input;
1,698✔
985

986
    /* Empty extension */
987
    if (input_len == 0)
1,698✔
988
        return 0;
1✔
989

990
    if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1,697✔
991
        if (!(HAS_SPACE(1)))
1,559✔
992
            goto invalid_length;
×
993

994
        uint8_t supported_ver_len = *input;
1,559✔
995
        input += 1;
1,559✔
996

997
        if (supported_ver_len < 2)
1,559✔
998
            goto invalid_length;
1✔
999

1000
        if (!(HAS_SPACE(supported_ver_len)))
1,558✔
1001
            goto invalid_length;
3✔
1002

1003
        /* Use the first (and preferred) valid version as client version,
1004
         * skip over GREASE and other possible noise. */
1005
        uint16_t i = 0;
1,555✔
1006
        while (i + 1 < (uint16_t)supported_ver_len) {
1,742✔
1007
            uint16_t ver = (uint16_t)(input[i] << 8) | input[i + 1];
1,740✔
1008
            if (TLSVersionValid(ver)) {
1,740✔
1009
                ssl_state->curr_connp->version = ver;
1,553✔
1010
                SCTLSHandshakeSetTLSVersion(ssl_state->curr_connp->hs, ver);
1,553✔
1011
                break;
1,553✔
1012
            }
1,553✔
1013
            i += 2;
187✔
1014
        }
187✔
1015

1016
        /* Set a flag to indicate that we have seen this extension */
1017
        ssl_state->flags |= SSL_AL_FLAG_CH_VERSION_EXTENSION;
1,555✔
1018

1019
        input += supported_ver_len;
1,555✔
1020
    }
1,555✔
1021
    else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
138✔
1022
        if (!(HAS_SPACE(2)))
138✔
1023
            goto invalid_length;
×
1024

1025
        uint16_t ver = (uint16_t)(*input << 8) | *(input + 1);
138✔
1026

1027
        if ((ssl_state->flags & SSL_AL_FLAG_CH_VERSION_EXTENSION) &&
138✔
1028
                (ver > TLS_VERSION_12)) {
138✔
1029
            ssl_state->flags |= SSL_AL_FLAG_LOG_WITHOUT_CERT;
126✔
1030
        }
126✔
1031

1032
        ssl_state->curr_connp->version = ver;
138✔
1033
        input += 2;
138✔
1034
    }
138✔
1035

1036
    return (int)(input - initial_input);
1,693✔
1037

1038
invalid_length:
4✔
1039
    SCLogDebug("TLS handshake invalid length");
4✔
1040
    SSLSetEvent(ssl_state,
4✔
1041
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
4✔
1042

1043
    return -1;
4✔
1044
}
1,697✔
1045

1046
static inline int TLSDecodeHSHelloExtensionEllipticCurves(SSLState *ssl_state,
1047
                                          const uint8_t * const initial_input,
1048
                                          const uint32_t input_len,
1049
                                          JA3Buffer *ja3_elliptic_curves)
1050
{
2,567✔
1051
    const uint8_t *input = initial_input;
2,567✔
1052

1053
    /* Empty extension */
1054
    if (input_len == 0)
2,567✔
1055
        return 0;
8✔
1056

1057
    if (!(HAS_SPACE(2)))
2,559✔
1058
        goto invalid_length;
1✔
1059

1060
    uint16_t elliptic_curves_len = (uint16_t)(*input << 8) | *(input + 1);
2,558✔
1061
    input += 2;
2,558✔
1062

1063
    if (!(HAS_SPACE(elliptic_curves_len)))
2,558✔
1064
        goto invalid_length;
8✔
1065

1066
    if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) && ja3_elliptic_curves) {
2,550✔
1067
        uint16_t ec_processed_len = 0;
2,540✔
1068
        /* coverity[tainted_data] */
1069
        while (ec_processed_len < elliptic_curves_len)
14,549✔
1070
        {
12,009✔
1071
            if (!(HAS_SPACE(2)))
12,009✔
1072
                goto invalid_length;
×
1073

1074
            uint16_t elliptic_curve = (uint16_t)(*input << 8) | *(input + 1);
12,009✔
1075
            input += 2;
12,009✔
1076

1077
            if (TLSDecodeValueIsGREASE(elliptic_curve) != 1) {
12,009✔
1078
                int rc = Ja3BufferAddValue(&ja3_elliptic_curves,
11,814✔
1079
                                           elliptic_curve);
11,814✔
1080
                if (rc != 0)
11,814✔
1081
                    return -1;
×
1082
            }
11,814✔
1083

1084
            ec_processed_len += 2;
12,009✔
1085
        }
12,009✔
1086

1087
    } else {
2,540✔
1088
        /* Skip elliptic curves */
1089
        input += elliptic_curves_len;
10✔
1090
    }
10✔
1091

1092
    return (int)(input - initial_input);
2,550✔
1093

1094
invalid_length:
9✔
1095
    SCLogDebug("TLS handshake invalid length");
9✔
1096
    SSLSetEvent(ssl_state,
9✔
1097
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
9✔
1098

1099
    return -1;
9✔
1100
}
2,550✔
1101

1102
static inline int TLSDecodeHSHelloExtensionEllipticCurvePF(SSLState *ssl_state,
1103
                                            const uint8_t * const initial_input,
1104
                                            const uint32_t input_len,
1105
                                            JA3Buffer *ja3_elliptic_curves_pf)
1106
{
3,900✔
1107
    const uint8_t *input = initial_input;
3,900✔
1108

1109
    /* Empty extension */
1110
    if (input_len == 0)
3,900✔
1111
        return 0;
2✔
1112

1113
    if (!(HAS_SPACE(1)))
3,898✔
1114
        goto invalid_length;
×
1115

1116
    uint8_t ec_pf_len = *input;
3,898✔
1117
    input += 1;
3,898✔
1118

1119
    if (!(HAS_SPACE(ec_pf_len)))
3,898✔
1120
        goto invalid_length;
8✔
1121

1122
    if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) && ja3_elliptic_curves_pf) {
3,890✔
1123
        uint8_t ec_pf_processed_len = 0;
2,545✔
1124
        /* coverity[tainted_data] */
1125
        while (ec_pf_processed_len < ec_pf_len)
5,359✔
1126
        {
2,814✔
1127
            uint8_t elliptic_curve_pf = *input;
2,814✔
1128
            input += 1;
2,814✔
1129

1130
            if (TLSDecodeValueIsGREASE(elliptic_curve_pf) != 1) {
2,814✔
1131
                int rc = Ja3BufferAddValue(&ja3_elliptic_curves_pf,
2,814✔
1132
                                           elliptic_curve_pf);
2,814✔
1133
                if (rc != 0)
2,814✔
1134
                    return -1;
×
1135
            }
2,814✔
1136

1137
            ec_pf_processed_len += 1;
2,814✔
1138
        }
2,814✔
1139

1140
    } else {
2,545✔
1141
        /* Skip elliptic curve point formats */
1142
        input += ec_pf_len;
1,345✔
1143
    }
1,345✔
1144

1145
    return (int)(input - initial_input);
3,890✔
1146

1147
invalid_length:
8✔
1148
    SCLogDebug("TLS handshake invalid length");
8✔
1149
    SSLSetEvent(ssl_state,
8✔
1150
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
8✔
1151

1152
    return -1;
8✔
1153
}
3,890✔
1154

1155
static inline int TLSDecodeHSHelloExtensionSigAlgorithms(
1156
        SSLState *ssl_state, const uint8_t *const initial_input, const uint32_t input_len)
1157
{
1,833✔
1158
    const uint8_t *input = initial_input;
1,833✔
1159

1160
    /* Empty extension */
1161
    if (input_len == 0)
1,833✔
1162
        return 0;
1✔
1163

1164
    if (!(HAS_SPACE(2)))
1,832✔
1165
        goto invalid_length;
1✔
1166

1167
    uint16_t sigalgo_len = (uint16_t)(*input << 8) | *(input + 1);
1,831✔
1168
    input += 2;
1,831✔
1169

1170
    /* Signature algorithms length should always be divisible by 2 */
1171
    if ((sigalgo_len % 2) != 0) {
1,831✔
1172
        goto invalid_length;
1✔
1173
    }
1✔
1174

1175
    if (!(HAS_SPACE(sigalgo_len)))
1,830✔
1176
        goto invalid_length;
5✔
1177

1178
    if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1,825✔
1179
        uint16_t sigalgo_processed_len = 0;
1,825✔
1180
        while (sigalgo_processed_len < sigalgo_len) {
21,826✔
1181
            uint16_t sigalgo = (uint16_t)(*input << 8) | *(input + 1);
20,001✔
1182
            input += 2;
20,001✔
1183
            sigalgo_processed_len += 2;
20,001✔
1184

1185
            SCTLSHandshakeAddSigAlgo(ssl_state->curr_connp->hs, sigalgo);
20,001✔
1186
        }
20,001✔
1187
    } else {
1,825✔
1188
        /* Skip signature algorithms */
UNCOV
1189
        input += sigalgo_len;
×
UNCOV
1190
    }
×
1191

1192
    return (int)(input - initial_input);
1,825✔
1193

1194
invalid_length:
7✔
1195
    SCLogDebug("Signature algorithm list invalid length");
7✔
1196
    SSLSetEvent(ssl_state, TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
7✔
1197

1198
    return -1;
7✔
1199
}
1,830✔
1200

1201
static inline int TLSDecodeHSHelloExtensionALPN(
1202
        SSLState *ssl_state, const uint8_t *const initial_input, const uint32_t input_len)
1203
{
2,512✔
1204
    const uint8_t *input = initial_input;
2,512✔
1205

1206
    /* Empty extension */
1207
    if (input_len == 0)
2,512✔
1208
        return 0;
1✔
1209

1210
    if (!(HAS_SPACE(2)))
2,511✔
1211
        goto invalid_length;
1✔
1212

1213
    uint16_t alpn_len = (uint16_t)(*input << 8) | *(input + 1);
2,510✔
1214
    input += 2;
2,510✔
1215

1216
    if (!(HAS_SPACE(alpn_len)))
2,510✔
1217
        goto invalid_length;
6✔
1218

1219
    /* We use 32 bits here to avoid potentially overflowing a value that
1220
       needs to be compared to an unsigned 16-bit value. */
1221
    uint32_t alpn_processed_len = 0;
2,504✔
1222
    while (alpn_processed_len < alpn_len) {
6,701✔
1223
        uint8_t protolen = *input;
4,203✔
1224
        input += 1;
4,203✔
1225
        alpn_processed_len += 1;
4,203✔
1226

1227
        if (!(HAS_SPACE(protolen)))
4,203✔
1228
            goto invalid_length;
6✔
1229

1230
        /* Check if reading another protolen bytes would exceed the
1231
           overall ALPN length; if so, skip and continue */
1232
        if (alpn_processed_len + protolen > ((uint32_t)alpn_len)) {
4,197✔
1233
            input += alpn_len - alpn_processed_len;
×
1234
            break;
×
1235
        }
×
1236
        SCTLSHandshakeAddALPN(ssl_state->curr_connp->hs, (const char *)input, protolen);
4,197✔
1237

1238
        alpn_processed_len += protolen;
4,197✔
1239
        input += protolen;
4,197✔
1240
    }
4,197✔
1241

1242
    return (int)(input - initial_input);
2,498✔
1243

1244
invalid_length:
13✔
1245
    SCLogDebug("ALPN list invalid length");
13✔
1246
    SSLSetEvent(ssl_state, TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
13✔
1247

1248
    return -1;
13✔
1249
}
2,504✔
1250

1251
static inline int TLSDecodeHSHelloExtensions(SSLState *ssl_state,
1252
                                         const uint8_t * const initial_input,
1253
                                         const uint32_t input_len)
1254
{
5,171✔
1255
    const uint8_t *input = initial_input;
5,171✔
1256

1257
    int ret;
5,171✔
1258
    int rc;
5,171✔
1259
    // if ja3_hash is already computed, do not use new hello to augment ja3_str
1260
    const bool ja3 =
5,171✔
1261
            (SC_ATOMIC_GET(ssl_config.enable_ja3) == 1) && ssl_state->curr_connp->ja3_hash == NULL;
5,171✔
1262

1263
    JA3Buffer *ja3_extensions = NULL;
5,171✔
1264
    JA3Buffer *ja3_elliptic_curves = NULL;
5,171✔
1265
    JA3Buffer *ja3_elliptic_curves_pf = NULL;
5,171✔
1266

1267
    if (ja3) {
5,171✔
1268
        ja3_extensions = Ja3BufferInit();
5,159✔
1269
        if (ja3_extensions == NULL)
5,159✔
1270
            goto error;
×
1271

1272
        if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
5,159✔
1273
            ja3_elliptic_curves = Ja3BufferInit();
2,654✔
1274
            if (ja3_elliptic_curves == NULL)
2,654✔
1275
                goto error;
×
1276

1277
            ja3_elliptic_curves_pf = Ja3BufferInit();
2,654✔
1278
            if (ja3_elliptic_curves_pf == NULL)
2,654✔
1279
                goto error;
×
1280
        }
2,654✔
1281
    }
5,159✔
1282

1283
    /* Extensions are optional (RFC5246 section 7.4.1.2) */
1284
    if (!(HAS_SPACE(2)))
5,171✔
1285
        goto end;
76✔
1286

1287
    uint16_t extensions_len = (uint16_t)(*input << 8) | *(input + 1);
5,095✔
1288
    input += 2;
5,095✔
1289

1290
    if (!(HAS_SPACE(extensions_len)))
5,095✔
1291
        goto invalid_length;
29✔
1292

1293
    uint16_t processed_len = 0;
5,066✔
1294
    /* coverity[tainted_data] */
1295
    while (processed_len < extensions_len)
40,132✔
1296
    {
35,215✔
1297
        if (!(HAS_SPACE(2)))
35,215✔
1298
            goto invalid_length;
3✔
1299

1300
        uint16_t ext_type = (uint16_t)(*input << 8) | *(input + 1);
35,212✔
1301
        input += 2;
35,212✔
1302

1303
        if (!(HAS_SPACE(2)))
35,212✔
1304
            goto invalid_length;
2✔
1305

1306
        uint16_t ext_len = (uint16_t)(*input << 8) | *(input + 1);
35,210✔
1307
        input += 2;
35,210✔
1308

1309
        if (!(HAS_SPACE(ext_len)))
35,210✔
1310
            goto invalid_length;
85✔
1311

1312
        switch (ext_type) {
35,125✔
1313
            case SSL_EXTENSION_SNI:
3,652✔
1314
            {
3,652✔
1315
                /* coverity[tainted_data] */
1316
                ret = TLSDecodeHSHelloExtensionSni(ssl_state, input,
3,652✔
1317
                                                   ext_len);
3,652✔
1318
                if (ret < 0)
3,652✔
1319
                    goto end;
18✔
1320

1321
                input += ret;
3,634✔
1322

1323
                break;
3,634✔
1324
            }
3,652✔
1325

1326
            case SSL_EXTENSION_ELLIPTIC_CURVES:
2,567✔
1327
            {
2,567✔
1328
                /* coverity[tainted_data] */
1329
                ret = TLSDecodeHSHelloExtensionEllipticCurves(ssl_state, input,
2,567✔
1330
                                                              ext_len,
2,567✔
1331
                                                              ja3_elliptic_curves);
2,567✔
1332
                if (ret < 0)
2,567✔
1333
                    goto end;
9✔
1334

1335
                input += ret;
2,558✔
1336

1337
                break;
2,558✔
1338
            }
2,567✔
1339

1340
            case SSL_EXTENSION_EC_POINT_FORMATS:
3,900✔
1341
            {
3,900✔
1342
                /* coverity[tainted_data] */
1343
                ret = TLSDecodeHSHelloExtensionEllipticCurvePF(ssl_state, input,
3,900✔
1344
                                                               ext_len,
3,900✔
1345
                                                               ja3_elliptic_curves_pf);
3,900✔
1346
                if (ret < 0)
3,900✔
1347
                    goto end;
8✔
1348

1349
                input += ret;
3,892✔
1350

1351
                break;
3,892✔
1352
            }
3,900✔
1353

1354
            case SSL_EXTENSION_SIGNATURE_ALGORITHMS: {
1,833✔
1355
                /* coverity[tainted_data] */
1356
                ret = TLSDecodeHSHelloExtensionSigAlgorithms(ssl_state, input, ext_len);
1,833✔
1357
                if (ret < 0)
1,833✔
1358
                    goto end;
7✔
1359

1360
                input += ret;
1,826✔
1361

1362
                break;
1,826✔
1363
            }
1,833✔
1364

1365
            case SSL_EXTENSION_ALPN: {
2,512✔
1366
                /* coverity[tainted_data] */
1367
                ret = TLSDecodeHSHelloExtensionALPN(ssl_state, input, ext_len);
2,512✔
1368
                if (ret < 0)
2,512✔
1369
                    goto end;
13✔
1370

1371
                input += ext_len;
2,499✔
1372

1373
                break;
2,499✔
1374
            }
2,512✔
1375

1376
            case SSL_EXTENSION_EARLY_DATA:
2✔
1377
            {
2✔
1378
                if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
2✔
1379
                    /* Used by 0-RTT to indicate that encrypted data will
1380
                       be sent right after the ClientHello record. */
1381
                    ssl_state->flags |= SSL_AL_FLAG_EARLY_DATA;
2✔
1382
                }
2✔
1383

1384
                input += ext_len;
2✔
1385

1386
                break;
2✔
1387
            }
2,512✔
1388

1389
            case SSL_EXTENSION_SUPPORTED_VERSIONS:
1,698✔
1390
            {
1,698✔
1391
                ret = TLSDecodeHSHelloExtensionSupportedVersions(ssl_state, input,
1,698✔
1392
                                                                 ext_len);
1,698✔
1393
                if (ret < 0)
1,698✔
1394
                    goto end;
4✔
1395

1396
                input += ret;
1,694✔
1397

1398
                break;
1,694✔
1399
            }
1,698✔
1400

1401
            case SSL_EXTENSION_SESSION_TICKET:
3,127✔
1402
            {
3,127✔
1403
                if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
3,127✔
1404
                    /* This has to be verified later on by checking if a
1405
                       certificate record has been sent by the server. */
1406
                    ssl_state->flags |= SSL_AL_FLAG_SESSION_RESUMED;
2,354✔
1407
                }
2,354✔
1408

1409
                input += ext_len;
3,127✔
1410

1411
                break;
3,127✔
1412
            }
1,698✔
1413

1414
            default:
15,834✔
1415
            {
15,834✔
1416
                input += ext_len;
15,834✔
1417
                break;
15,834✔
1418
            }
1,698✔
1419
        }
35,125✔
1420

1421
        if (ja3) {
35,066✔
1422
            if (TLSDecodeValueIsGREASE(ext_type) != 1) {
34,982✔
1423
                rc = Ja3BufferAddValue(&ja3_extensions, ext_type);
34,604✔
1424
                if (rc != 0)
34,604✔
1425
                    goto error;
×
1426
            }
34,604✔
1427
        }
34,982✔
1428

1429
        if (ssl_state->current_flags &
35,066✔
1430
                (SSL_AL_FLAG_STATE_CLIENT_HELLO | SSL_AL_FLAG_STATE_SERVER_HELLO)) {
35,066✔
1431
            if (TLSDecodeValueIsGREASE(ext_type) != 1) {
35,066✔
1432
                SCTLSHandshakeAddExtension(ssl_state->curr_connp->hs, ext_type);
34,688✔
1433
            }
34,688✔
1434
        }
35,066✔
1435

1436
        processed_len += ext_len + 4;
35,066✔
1437
    }
35,066✔
1438

1439
end:
5,052✔
1440
    if (ja3) {
5,052✔
1441
        rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
5,047✔
1442
                                   &ja3_extensions);
5,047✔
1443
        if (rc == -1)
5,047✔
1444
            goto error;
×
1445

1446
        if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
5,047✔
1447
            rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
2,554✔
1448
                                       &ja3_elliptic_curves);
2,554✔
1449
            if (rc == -1)
2,554✔
1450
                goto error;
×
1451

1452
            rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
2,554✔
1453
                                       &ja3_elliptic_curves_pf);
2,554✔
1454
            if (rc == -1)
2,554✔
1455
                goto error;
×
1456
        }
2,554✔
1457
    }
5,047✔
1458

1459
    return (int)(input - initial_input);
5,052✔
1460

1461
invalid_length:
119✔
1462
    SCLogDebug("TLS handshake invalid length");
119✔
1463
    SSLSetEvent(ssl_state,
119✔
1464
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
119✔
1465

1466
error:
119✔
1467
    if (ja3_extensions != NULL)
119✔
1468
        Ja3BufferFree(&ja3_extensions);
112✔
1469
    if (ja3_elliptic_curves != NULL)
119✔
1470
        Ja3BufferFree(&ja3_elliptic_curves);
100✔
1471
    if (ja3_elliptic_curves_pf != NULL)
119✔
1472
        Ja3BufferFree(&ja3_elliptic_curves_pf);
100✔
1473

1474
    return -1;
119✔
1475
}
119✔
1476

1477
static int TLSDecodeHandshakeHello(SSLState *ssl_state,
1478
                                   const uint8_t * const input,
1479
                                   const uint32_t input_len)
1480
{
5,215✔
1481
    int ret;
5,215✔
1482
    uint32_t parsed = 0;
5,215✔
1483

1484
    ret = TLSDecodeHSHelloVersion(ssl_state, input, input_len);
5,215✔
1485
    if (ret < 0)
5,215✔
1486
        goto end;
3✔
1487

1488
    parsed += ret;
5,212✔
1489

1490
    ret = TLSDecodeHSHelloRandom(ssl_state, input + parsed, input_len - parsed);
5,212✔
1491
    if (ret < 0)
5,212✔
1492
        goto end;
1✔
1493

1494
    parsed += ret;
5,211✔
1495

1496
    /* The session id field in the server hello record was removed in
1497
       TLSv1.3 draft1, but was readded in draft22. */
1498
    if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) ||
5,211✔
1499
            ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
5,211✔
1500
            ((ssl_state->flags & SSL_AL_FLAG_LOG_WITHOUT_CERT) == 0))) {
5,206✔
1501
        ret = TLSDecodeHSHelloSessionID(ssl_state, input + parsed,
5,206✔
1502
                                        input_len - parsed);
5,206✔
1503
        if (ret < 0)
5,206✔
1504
            goto end;
12✔
1505

1506
        parsed += ret;
5,194✔
1507
    }
5,194✔
1508

1509
    ret = TLSDecodeHSHelloCipherSuites(ssl_state, input + parsed,
5,199✔
1510
                                       input_len - parsed);
5,199✔
1511
    if (ret < 0)
5,199✔
1512
        goto end;
22✔
1513

1514
    parsed += ret;
5,177✔
1515

1516
   /* The compression methods field in the server hello record was
1517
      removed in TLSv1.3 draft1, but was readded in draft22. */
1518
   if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) ||
5,177✔
1519
              ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
5,177✔
1520
              ((ssl_state->flags & SSL_AL_FLAG_LOG_WITHOUT_CERT) == 0))) {
5,172✔
1521
        ret = TLSDecodeHSHelloCompressionMethods(ssl_state, input + parsed,
5,172✔
1522
                                                 input_len - parsed);
5,172✔
1523
        if (ret < 0)
5,172✔
1524
            goto end;
6✔
1525

1526
        parsed += ret;
5,166✔
1527
    }
5,166✔
1528

1529
    ret = TLSDecodeHSHelloExtensions(ssl_state, input + parsed,
5,171✔
1530
                                     input_len - parsed);
5,171✔
1531
    if (ret < 0)
5,171✔
1532
        goto end;
119✔
1533

1534
    if (SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_hash == NULL) {
5,052✔
1535
        ssl_state->curr_connp->ja3_hash = Ja3GenerateHash(ssl_state->curr_connp->ja3_str);
5,047✔
1536
    }
5,047✔
1537

1538
    if (ssl_state->curr_connp == &ssl_state->client_connp) {
5,052✔
1539
        UpdateClientState(ssl_state, TLS_STATE_CLIENT_HELLO_DONE);
2,622✔
1540
    } else {
2,622✔
1541
        UpdateServerState(ssl_state, TLS_STATE_SERVER_HELLO);
2,430✔
1542
    }
2,430✔
1543
end:
5,215✔
1544
    return 0;
5,215✔
1545
}
5,052✔
1546

1547
#ifdef DEBUG_VALIDATION
1548
static inline bool
1549
RecordAlreadyProcessed(const SSLStateConnp *curr_connp)
1550
{
1551
    return ((curr_connp->record_length + SSLV3_RECORD_HDR_LEN) <
1552
            curr_connp->bytes_processed);
1553
}
1554
#endif
1555

1556
static inline int SSLv3ParseHandshakeTypeCertificate(SSLState *ssl_state, SSLStateConnp *connp,
1557
        const uint8_t *const initial_input, const uint32_t input_len)
1558
{
1,491✔
1559
    int rc = TlsDecodeHSCertificates(ssl_state, connp, initial_input, input_len);
1,491✔
1560
    SCLogDebug("rc %d", rc);
1,491✔
1561
    if (rc > 0) {
1,491✔
1562
        DEBUG_VALIDATE_BUG_ON(rc > (int)input_len);
1,472✔
1563
        SSLParserHSReset(connp);
1,472✔
1564
    } else if (rc < 0) {
1,472✔
1565
        SCLogDebug("error parsing cert, reset state");
19✔
1566
        SSLParserHSReset(connp);
19✔
1567
        /* fall through to still consume the cert bytes */
1568
    }
19✔
1569
    if (connp == &ssl_state->client_connp) {
1,491✔
1570
        UpdateClientState(ssl_state, TLS_STATE_CLIENT_CERT_DONE);
30✔
1571
    } else {
1,461✔
1572
        UpdateServerState(ssl_state, TLS_STATE_SERVER_CERT_DONE);
1,461✔
1573
    }
1,461✔
1574
    return input_len;
1,491✔
1575
}
1,491✔
1576

1577
static int SupportedHandshakeType(const uint8_t type)
1578
{
10,527✔
1579
    switch (type) {
10,527✔
1580
        case SSLV3_HS_CLIENT_HELLO:
2,706✔
1581
        case SSLV3_HS_SERVER_HELLO:
5,228✔
1582
        case SSLV3_HS_SERVER_KEY_EXCHANGE:
6,193✔
1583
        case SSLV3_HS_CLIENT_KEY_EXCHANGE:
7,186✔
1584
        case SSLV3_HS_CERTIFICATE:
8,678✔
1585
        case SSLV3_HS_HELLO_REQUEST:
8,821✔
1586
        case SSLV3_HS_CERTIFICATE_REQUEST:
8,822✔
1587
        case SSLV3_HS_CERTIFICATE_VERIFY:
8,823✔
1588
        case SSLV3_HS_FINISHED:
8,825✔
1589
        case SSLV3_HS_CERTIFICATE_URL:
8,825✔
1590
        case SSLV3_HS_CERTIFICATE_STATUS:
9,058✔
1591
        case SSLV3_HS_NEW_SESSION_TICKET:
9,076✔
1592
        case SSLV3_HS_SERVER_HELLO_DONE:
10,476✔
1593
            return true;
10,476✔
1594
            break;
×
1595

1596
        default:
51✔
1597
            return false;
51✔
1598
            break;
×
1599
    }
10,527✔
1600
}
10,527✔
1601

1602
/**
1603
 *  \param input_len length of bytes after record header. Can be 0 (e.g. for server hello done).
1604
 *  \retval parsed number of consumed bytes
1605
 *  \retval < 0 error
1606
 */
1607
static int SSLv3ParseHandshakeType(SSLState *ssl_state, const uint8_t *input,
1608
                                   uint32_t input_len, uint8_t direction)
1609
{
10,434✔
1610
    const uint8_t *initial_input = input;
10,434✔
1611
    int rc;
10,434✔
1612

1613
    DEBUG_VALIDATE_BUG_ON(RecordAlreadyProcessed(ssl_state->curr_connp));
10,434✔
1614

1615
    switch (ssl_state->curr_connp->handshake_type) {
10,434✔
1616
        case SSLV3_HS_CLIENT_HELLO:
2,699✔
1617
            ssl_state->current_flags = SSL_AL_FLAG_STATE_CLIENT_HELLO;
2,699✔
1618

1619
            if (ssl_state->curr_connp->hs == NULL)
2,699✔
1620
                ssl_state->curr_connp->hs = SCTLSHandshakeNew();
2,680✔
1621

1622
            rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
2,699✔
1623
            if (rc < 0)
2,699✔
1624
                return rc;
×
1625
            break;
2,699✔
1626

1627
        case SSLV3_HS_SERVER_HELLO:
2,699✔
1628
            ssl_state->current_flags = SSL_AL_FLAG_STATE_SERVER_HELLO;
2,516✔
1629

1630
            DEBUG_VALIDATE_BUG_ON(ssl_state->curr_connp->message_length != input_len);
2,516✔
1631
            if (ssl_state->curr_connp->hs == NULL)
2,516✔
1632
                ssl_state->curr_connp->hs = SCTLSHandshakeNew();
2,516✔
1633

1634
            rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
2,516✔
1635
            if (rc < 0)
2,516✔
1636
                return rc;
×
1637
            break;
2,516✔
1638

1639
        case SSLV3_HS_SERVER_KEY_EXCHANGE:
2,516✔
1640
            ssl_state->current_flags = SSL_AL_FLAG_STATE_SERVER_KEYX;
950✔
1641
            break;
950✔
1642

1643
        case SSLV3_HS_CLIENT_KEY_EXCHANGE:
993✔
1644
            ssl_state->current_flags = SSL_AL_FLAG_STATE_CLIENT_KEYX;
993✔
1645
            break;
993✔
1646

1647
        case SSLV3_HS_CERTIFICATE:
1,491✔
1648
            rc = SSLv3ParseHandshakeTypeCertificate(ssl_state,
1,491✔
1649
                    direction ? &ssl_state->server_connp : &ssl_state->client_connp, initial_input,
1,491✔
1650
                    input_len);
1,491✔
1651
            if (rc < 0)
1,491✔
1652
                return rc;
×
1653
            break;
1,491✔
1654

1655
        case SSLV3_HS_HELLO_REQUEST:
1,491✔
1656
            break;
133✔
1657
        case SSLV3_HS_CERTIFICATE_REQUEST:
1✔
1658
            if (direction) {
1✔
1659
                ssl_state->current_flags = SSL_AL_FLAG_NEED_CLIENT_CERT;
1✔
1660
            }
1✔
1661
            break;
1✔
1662
        case SSLV3_HS_CERTIFICATE_VERIFY:
1✔
1663
        case SSLV3_HS_FINISHED:
3✔
1664
        case SSLV3_HS_CERTIFICATE_URL:
3✔
1665
        case SSLV3_HS_CERTIFICATE_STATUS:
234✔
1666
            break;
234✔
1667
        case SSLV3_HS_NEW_SESSION_TICKET:
18✔
1668
            SCLogDebug("new session ticket");
18✔
1669
            break;
18✔
1670
        case SSLV3_HS_SERVER_HELLO_DONE:
1,399✔
1671
            if (direction) {
1,399✔
1672
                UpdateServerState(ssl_state, TLS_STATE_SERVER_HELLO_DONE);
1,377✔
1673
            }
1,377✔
1674
            break;
1,399✔
1675
        default:
×
1676
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSL_RECORD);
×
1677
            return -1;
×
1678
    }
10,434✔
1679

1680
    ssl_state->flags |= ssl_state->current_flags;
10,434✔
1681

1682
    SCLogDebug("message: length %u", ssl_state->curr_connp->message_length);
10,434✔
1683
    SCLogDebug("input_len %u ssl_state->curr_connp->bytes_processed %u", input_len, ssl_state->curr_connp->bytes_processed);
10,434✔
1684

1685
    return input_len;
10,434✔
1686
}
10,434✔
1687

1688
static int SSLv3ParseHandshakeProtocol(SSLState *ssl_state, const uint8_t *input,
1689
                                       uint32_t input_len, uint8_t direction)
1690
{
10,297✔
1691
    const uint8_t *initial_input = input;
10,297✔
1692

1693
    if (input_len == 0 || ssl_state->curr_connp->bytes_processed ==
10,297✔
1694
            (ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN)) {
10,297✔
1695
        SCReturnInt(0);
×
1696
    }
×
1697

1698
    while (input_len) {
20,801✔
1699
        SCLogDebug("input_len %u", input_len);
10,551✔
1700

1701
        if (ssl_state->curr_connp->hs_buffer != NULL) {
10,551✔
1702
            SCLogDebug("partial handshake record in place");
21✔
1703
            const uint32_t need = ssl_state->curr_connp->hs_buffer_message_size -
21✔
1704
                                  ssl_state->curr_connp->hs_buffer_offset;
21✔
1705
            const uint32_t add = MIN(need, input_len);
21✔
1706

1707
            /* grow buffer to next multiple of 4k that fits all data we have */
1708
            if (ssl_state->curr_connp->hs_buffer_offset + add >
21✔
1709
                    ssl_state->curr_connp->hs_buffer_size) {
21✔
1710
                const uint32_t avail = ssl_state->curr_connp->hs_buffer_offset + add;
×
1711
                const uint32_t new_size = avail + (4096 - (avail % 4096));
×
1712
                SCLogDebug("new_size %u, avail %u", new_size, avail);
×
1713
                void *ptr = SCRealloc(ssl_state->curr_connp->hs_buffer, new_size);
×
1714
                if (ptr == NULL)
×
1715
                    return -1;
×
1716
                ssl_state->curr_connp->hs_buffer = ptr;
×
1717
                ssl_state->curr_connp->hs_buffer_size = new_size;
×
1718
            }
×
1719

1720
            SCLogDebug("ssl_state->curr_connp->hs_buffer_offset %u "
21✔
1721
                       "ssl_state->curr_connp->hs_buffer_size %u",
21✔
1722
                    ssl_state->curr_connp->hs_buffer_offset, ssl_state->curr_connp->hs_buffer_size);
21✔
1723
            SCLogDebug("to add %u total %u", add, ssl_state->curr_connp->hs_buffer_offset + add);
21✔
1724

1725
            if (SafeMemcpy(ssl_state->curr_connp->hs_buffer,
21✔
1726
                        ssl_state->curr_connp->hs_buffer_offset,
21✔
1727
                        ssl_state->curr_connp->hs_buffer_size, input, 0, add, add) != 0) {
21✔
1728
                SCLogDebug("copy failed");
×
1729
                return -1;
×
1730
            }
×
1731
            ssl_state->curr_connp->hs_buffer_offset += add;
21✔
1732

1733
            if (ssl_state->curr_connp->hs_buffer_message_size <=
21✔
1734
                    ssl_state->curr_connp->hs_buffer_offset) {
21✔
1735
                DEBUG_VALIDATE_BUG_ON(ssl_state->curr_connp->hs_buffer_message_size !=
2✔
1736
                                      ssl_state->curr_connp->hs_buffer_offset);
2✔
1737

1738
                ssl_state->curr_connp->handshake_type =
2✔
1739
                        ssl_state->curr_connp->hs_buffer_message_type;
2✔
1740
                ssl_state->curr_connp->message_length =
2✔
1741
                        ssl_state->curr_connp->hs_buffer_message_size;
2✔
1742

1743
                SCLogDebug("got all data now: handshake_type %u message_length %u",
2✔
1744
                        ssl_state->curr_connp->handshake_type,
2✔
1745
                        ssl_state->curr_connp->message_length);
2✔
1746

1747
                int retval = SSLv3ParseHandshakeType(ssl_state, ssl_state->curr_connp->hs_buffer,
2✔
1748
                        ssl_state->curr_connp->hs_buffer_offset, direction);
2✔
1749
                if (retval < 0) {
2✔
1750
                    SSLParserHSReset(ssl_state->curr_connp);
×
1751
                    return (retval);
×
1752
                }
×
1753
                SCLogDebug("retval %d", retval);
2✔
1754

1755
                /* data processed, reset buffer */
1756
                SCFree(ssl_state->curr_connp->hs_buffer);
2✔
1757
                ssl_state->curr_connp->hs_buffer = NULL;
2✔
1758
                ssl_state->curr_connp->hs_buffer_size = 0;
2✔
1759
                ssl_state->curr_connp->hs_buffer_message_size = 0;
2✔
1760
                ssl_state->curr_connp->hs_buffer_message_type = 0;
2✔
1761
                ssl_state->curr_connp->hs_buffer_offset = 0;
2✔
1762
            } else {
19✔
1763
                SCLogDebug("partial data");
19✔
1764
            }
19✔
1765

1766
            input += add;
21✔
1767
            input_len -= add;
21✔
1768
            SCLogDebug("input_len %u", input_len);
21✔
1769
            SSLParserHSReset(ssl_state->curr_connp);
21✔
1770
            continue;
21✔
1771
        }
21✔
1772

1773
        SCLogDebug("bytes_processed %u", ssl_state->curr_connp->bytes_processed);
10,530✔
1774
        SCLogDebug("input %p input_len %u", input, input_len);
10,530✔
1775

1776
        if (input_len < 4) {
10,530✔
1777
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSL_RECORD);
3✔
1778
            SCReturnInt(-1);
3✔
1779
        }
3✔
1780

1781
        ssl_state->curr_connp->handshake_type = input[0];
10,527✔
1782
        ssl_state->curr_connp->message_length = input[1] << 16 | input[2] << 8 | input[3];
10,527✔
1783
        SCLogDebug("handshake_type %u message len %u input %p input_len %u",
10,527✔
1784
                ssl_state->curr_connp->handshake_type, ssl_state->curr_connp->message_length, input,
10,527✔
1785
                input_len);
10,527✔
1786
        input += 4;
10,527✔
1787
        input_len -= 4;
10,527✔
1788

1789
        const uint32_t record_len = ssl_state->curr_connp->message_length;
10,527✔
1790
        /* see if we support this type. We check here to not use the fragment
1791
         * handling on things we don't support. */
1792
        const bool supported_type = SupportedHandshakeType(ssl_state->curr_connp->handshake_type);
10,527✔
1793
        SCLogDebug("supported_type %s handshake_type %u/%02x", supported_type ? "true" : "false",
10,527✔
1794
                ssl_state->curr_connp->handshake_type, ssl_state->curr_connp->handshake_type);
10,527✔
1795
        if (!supported_type) {
10,527✔
1796
            uint32_t avail_record_len = MIN(input_len, record_len);
51✔
1797
            input += avail_record_len;
51✔
1798
            input_len -= avail_record_len;
51✔
1799

1800
            SSLParserHSReset(ssl_state->curr_connp);
51✔
1801

1802
            if ((direction && (ssl_state->flags & SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC)) ||
51✔
1803
                    (!direction && (ssl_state->flags & SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC))) {
51✔
1804
                // after Change Cipher Spec we get Encrypted Handshake Messages
1805
            } else {
46✔
1806
                SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE);
46✔
1807
            }
46✔
1808
            continue;
51✔
1809
        }
51✔
1810

1811
        /* if the message length exceeds our input_len, we have a tls fragment. */
1812
        if (record_len > input_len) {
10,476✔
1813
            const uint32_t avail = input_len;
44✔
1814
            const uint32_t size = avail + (4096 - (avail % 4096));
44✔
1815
            SCLogDebug("initial buffer size %u, based on input %u", size, avail);
44✔
1816
            ssl_state->curr_connp->hs_buffer = SCCalloc(1, size);
44✔
1817
            if (ssl_state->curr_connp->hs_buffer == NULL) {
44✔
1818
                return -1;
×
1819
            }
×
1820
            ssl_state->curr_connp->hs_buffer_size = size;
44✔
1821
            ssl_state->curr_connp->hs_buffer_message_size = record_len;
44✔
1822
            ssl_state->curr_connp->hs_buffer_message_type = ssl_state->curr_connp->handshake_type;
44✔
1823

1824
            if (input_len > 0) {
44✔
1825
                if (SafeMemcpy(ssl_state->curr_connp->hs_buffer, 0,
42✔
1826
                            ssl_state->curr_connp->hs_buffer_size, input, 0, input_len,
42✔
1827
                            input_len) != 0) {
42✔
1828
                    return -1;
×
1829
                }
×
1830
                ssl_state->curr_connp->hs_buffer_offset = input_len;
42✔
1831
            }
42✔
1832
            SCLogDebug("opened record buffer %p size %u offset %u type %u msg_size %u",
44✔
1833
                    ssl_state->curr_connp->hs_buffer, ssl_state->curr_connp->hs_buffer_size,
44✔
1834
                    ssl_state->curr_connp->hs_buffer_offset,
44✔
1835
                    ssl_state->curr_connp->hs_buffer_message_type,
44✔
1836
                    ssl_state->curr_connp->hs_buffer_message_size);
44✔
1837
            input += input_len;
44✔
1838
            SSLParserHSReset(ssl_state->curr_connp);
44✔
1839
            return (int)(input - initial_input);
44✔
1840

1841
        } else {
10,432✔
1842
            /* full record, parse it now */
1843
            int retval = SSLv3ParseHandshakeType(
10,432✔
1844
                    ssl_state, input, ssl_state->curr_connp->message_length, direction);
10,432✔
1845
            if (retval < 0 || retval > (int)input_len) {
10,432✔
1846
                DEBUG_VALIDATE_BUG_ON(retval > (int)input_len);
×
1847
                return (retval);
×
1848
            }
×
1849
            SCLogDebug("retval %d input_len %u", retval, input_len);
10,432✔
1850
            input += retval;
10,432✔
1851
            input_len -= retval;
10,432✔
1852

1853
            SSLParserHSReset(ssl_state->curr_connp);
10,432✔
1854
        }
10,432✔
1855
        SCLogDebug("input_len left %u", input_len);
10,432✔
1856
    }
10,432✔
1857
    return (int)(input - initial_input);
10,250✔
1858
}
10,297✔
1859

1860
/**
1861
 * \internal
1862
 * \brief TLS Alert parser
1863
 *
1864
 * \param sslstate  Pointer to the SSL state.
1865
 * \param input     Pointer to the received input data.
1866
 * \param input_len Length in bytes of the received data.
1867
 * \param direction 1 toclient, 0 toserver
1868
 *
1869
 * \retval The number of bytes parsed on success, 0 if nothing parsed, -1 on failure.
1870
 */
1871
static int SSLv3ParseAlertProtocol(
1872
        SSLState *ssl_state, const uint8_t *input, uint32_t input_len, uint8_t direction)
1873
{
73✔
1874
    if (input_len < 2) {
73✔
1875
        SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_ALERT);
×
1876
        return -1;
×
1877
    }
×
1878

1879
    /* assume a record > 2 to be an encrypted alert record */
1880
    if (input_len == 2) {
73✔
1881
        uint8_t level = input[0];
9✔
1882
        // uint8_t desc = input[1];
1883

1884
        /* if level Fatal, we consider the tx finished */
1885
        if (level == 2) {
9✔
1886
            UpdateClientState(ssl_state, TLS_STATE_CLIENT_FINISHED);
9✔
1887
            UpdateServerState(ssl_state, TLS_STATE_SERVER_FINISHED);
9✔
1888
        }
9✔
1889
    }
9✔
1890
    return 0;
73✔
1891
}
73✔
1892

1893
/**
1894
 * \internal
1895
 * \brief TLS Heartbeat parser (see RFC 6520)
1896
 *
1897
 * \param sslstate  Pointer to the SSL state.
1898
 * \param input     Pointer to the received input data.
1899
 * \param input_len Length in bytes of the received data.
1900
 * \param direction 1 toclient, 0 toserver
1901
 *
1902
 * \retval The number of bytes parsed on success, 0 if nothing parsed, -1 on failure.
1903
 */
1904
static int SSLv3ParseHeartbeatProtocol(SSLState *ssl_state, const uint8_t *input,
1905
                                       uint32_t input_len, uint8_t direction)
1906
{
×
1907
    uint8_t hb_type;
×
1908
    uint16_t payload_len;
×
1909
    uint32_t padding_len;
×
1910

1911
    /* expect at least 3 bytes: heartbeat type (1) + length (2) */
1912
    if (input_len < 3) {
×
1913
        return 0;
×
1914
    }
×
1915

1916
    hb_type = *input++;
×
1917

1918
    if (!(ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC)) {
×
1919
        if (!(hb_type == TLS_HB_REQUEST || hb_type == TLS_HB_RESPONSE)) {
×
1920
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_HEARTBEAT);
×
1921
            return -1;
×
1922
        }
×
1923
    }
×
1924

1925
    if ((ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) == 0) {
×
1926
        ssl_state->flags |= SSL_AL_FLAG_HB_INFLIGHT;
×
1927

1928
        if (direction) {
×
1929
            SCLogDebug("HeartBeat Record type sent in the toclient direction!");
×
1930
            ssl_state->flags |= SSL_AL_FLAG_HB_SERVER_INIT;
×
1931
        } else {
×
1932
            SCLogDebug("HeartBeat Record type sent in the toserver direction!");
×
1933
            ssl_state->flags |= SSL_AL_FLAG_HB_CLIENT_INIT;
×
1934
        }
×
1935

1936
        /* if we reach this point, then we can assume that the HB request
1937
           is encrypted. If so, let's set the HB record length */
1938
        if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
×
1939
            ssl_state->hb_record_len = ssl_state->curr_connp->record_length;
×
1940
            SCLogDebug("Encrypted HeartBeat Request In-flight. Storing len %u",
×
1941
                       ssl_state->hb_record_len);
×
1942
            return (ssl_state->curr_connp->record_length - 3);
×
1943
        }
×
1944

1945
        payload_len = (uint16_t)(*input << 8) | *(input + 1);
×
1946

1947
        /* check that the requested payload length is really present in
1948
           the record (CVE-2014-0160) */
1949
        if ((uint32_t)(payload_len+3) > ssl_state->curr_connp->record_length) {
×
1950
            SCLogDebug("We have a short record in HeartBeat Request");
×
1951
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_OVERFLOW_HEARTBEAT);
×
1952
            return -1;
×
1953
        }
×
1954

1955
        /* check the padding length. It must be at least 16 bytes
1956
           (RFC 6520, section 4) */
1957
        padding_len = ssl_state->curr_connp->record_length - payload_len - 3;
×
1958
        if (padding_len < 16) {
×
1959
            SCLogDebug("We have a short record in HeartBeat Request");
×
1960
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_HEARTBEAT);
×
1961
            return -1;
×
1962
        }
×
1963

1964
        /* we don't have the payload */
1965
        if (input_len < payload_len + padding_len) {
×
1966
            return 0;
×
1967
        }
×
1968

1969
    /* OpenSSL still seems to discard multiple in-flight
1970
       heartbeats although some tools send multiple at once */
1971
    } else if (direction == 1 && (ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) &&
×
1972
            (ssl_state->flags & SSL_AL_FLAG_HB_SERVER_INIT)) {
×
1973
        SCLogDebug("Multiple in-flight server initiated HeartBeats");
×
1974
        SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_HEARTBEAT);
×
1975
        return -1;
×
1976

1977
    } else if (direction == 0 && (ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) &&
×
1978
            (ssl_state->flags & SSL_AL_FLAG_HB_CLIENT_INIT)) {
×
1979
        SCLogDebug("Multiple in-flight client initiated HeartBeats");
×
1980
        SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_HEARTBEAT);
×
1981
        return -1;
×
1982

1983
    } else {
×
1984
        /* we have a HB record in the opposite direction of the request,
1985
           let's reset our flags */
1986
        ssl_state->flags &= ~SSL_AL_FLAG_HB_INFLIGHT;
×
1987
        ssl_state->flags &= ~SSL_AL_FLAG_HB_SERVER_INIT;
×
1988
        ssl_state->flags &= ~SSL_AL_FLAG_HB_CLIENT_INIT;
×
1989

1990
        /* if we reach this point, then we can assume that the HB request
1991
           is encrypted. If so, let's set the HB record length */
1992
        if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
×
1993
            /* check to see if the encrypted response is longer than the
1994
               encrypted request */
1995
            if (ssl_state->hb_record_len > 0 && ssl_state->hb_record_len <
×
1996
                    ssl_state->curr_connp->record_length) {
×
1997
                SCLogDebug("My heart is bleeding.. OpenSSL HeartBleed response (%u)",
×
1998
                        ssl_state->hb_record_len);
×
1999
                SSLSetEvent(ssl_state,
×
2000
                        TLS_DECODER_EVENT_DATALEAK_HEARTBEAT_MISMATCH);
×
2001
                ssl_state->hb_record_len = 0;
×
2002
                return -1;
×
2003
            }
×
2004
        }
×
2005

2006
        /* reset the HB record length in case we have a legit HB followed
2007
           by a bad one */
2008
        ssl_state->hb_record_len = 0;
×
2009
    }
×
2010

2011
    /* skip the HeartBeat, 3 bytes were already parsed,
2012
       e.g |18 03 02| for TLS 1.2 */
2013
    return (ssl_state->curr_connp->record_length - 3);
×
2014
}
×
2015

2016
static int SSLv3ParseRecord(uint8_t direction, SSLState *ssl_state,
2017
                            const uint8_t *input, uint32_t input_len)
2018
{
33,908✔
2019
    const uint8_t *initial_input = input;
33,908✔
2020

2021
    if (input_len == 0) {
33,908✔
2022
        return 0;
×
2023
    }
×
2024

2025
    uint8_t skip_version = 0;
33,908✔
2026

2027
    /* Only set SSL/TLS version here if it has not already been set in
2028
       client/server hello. */
2029
    if (direction == 0) {
33,908✔
2030
        if ((ssl_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
13,121✔
2031
                (ssl_state->client_connp.version != TLS_VERSION_UNKNOWN)) {
13,121✔
2032
            skip_version = 1;
10,038✔
2033
        }
10,038✔
2034
    } else {
20,787✔
2035
        if ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
20,787✔
2036
                (ssl_state->server_connp.version != TLS_VERSION_UNKNOWN)) {
20,787✔
2037
            skip_version = 1;
18,134✔
2038
        }
18,134✔
2039
    }
20,787✔
2040

2041
    switch (ssl_state->curr_connp->bytes_processed) {
33,908✔
2042
        case 0:
33,896✔
2043
            if (input_len >= 5) {
33,896✔
2044
                ssl_state->curr_connp->content_type = input[0];
33,882✔
2045
                if (!skip_version) {
33,882✔
2046
                    ssl_state->curr_connp->version = (uint16_t)(input[1] << 8) | input[2];
5,735✔
2047
                }
5,735✔
2048
                ssl_state->curr_connp->record_length = input[3] << 8;
33,882✔
2049
                ssl_state->curr_connp->record_length |= input[4];
33,882✔
2050
                ssl_state->curr_connp->bytes_processed += SSLV3_RECORD_HDR_LEN;
33,882✔
2051
                return SSLV3_RECORD_HDR_LEN;
33,882✔
2052
            } else {
33,882✔
2053
                ssl_state->curr_connp->content_type = *(input++);
14✔
2054
                if (--input_len == 0)
14✔
2055
                    break;
2✔
2056
            }
14✔
2057

2058
            /* fall through */
2059
        case 1:
13✔
2060
            if (!skip_version) {
13✔
2061
                ssl_state->curr_connp->version = (uint16_t)(*(input++) << 8);
1✔
2062
            } else {
12✔
2063
                input++;
12✔
2064
            }
12✔
2065
            if (--input_len == 0)
13✔
2066
                break;
1✔
2067

2068
            /* fall through */
2069
        case 2:
13✔
2070
            if (!skip_version) {
13✔
2071
                ssl_state->curr_connp->version |= *(input++);
1✔
2072
            } else {
12✔
2073
                input++;
12✔
2074
            }
12✔
2075
            if (--input_len == 0)
13✔
2076
                break;
5✔
2077

2078
            /* fall through */
2079
        case 3:
13✔
2080
            ssl_state->curr_connp->record_length = *(input++) << 8;
13✔
2081
            if (--input_len == 0)
13✔
2082
                break;
6✔
2083

2084
            /* fall through */
2085
        case 4:
12✔
2086
            ssl_state->curr_connp->record_length |= *(input++);
12✔
2087
            if (--input_len == 0)
12✔
2088
                break;
×
2089

2090
            /* fall through */
2091
    }
33,908✔
2092

2093
    ssl_state->curr_connp->bytes_processed += (input - initial_input);
26✔
2094

2095
    return (int)(input - initial_input);
26✔
2096
}
33,908✔
2097

2098
static int SSLv2ParseRecord(uint8_t direction, SSLState *ssl_state,
2099
                            const uint8_t *input, uint32_t input_len)
2100
{
161✔
2101
    const uint8_t *initial_input = input;
161✔
2102

2103
    if (input_len == 0) {
161✔
2104
        return 0;
×
2105
    }
×
2106

2107
    if (ssl_state->curr_connp->record_lengths_length == 2) {
161✔
2108
        switch (ssl_state->curr_connp->bytes_processed) {
113✔
2109
            case 0:
113✔
2110
                if (input_len >= ssl_state->curr_connp->record_lengths_length + 1) {
113✔
2111
                    ssl_state->curr_connp->record_length = (0x7f & input[0]) << 8 | input[1];
113✔
2112
                    ssl_state->curr_connp->content_type = input[2];
113✔
2113
                    ssl_state->curr_connp->version = SSL_VERSION_2;
113✔
2114
                    ssl_state->curr_connp->bytes_processed += 3;
113✔
2115
                    return 3;
113✔
2116
                } else {
113✔
2117
                    ssl_state->curr_connp->record_length = (0x7f & *(input++)) << 8;
×
2118
                    if (--input_len == 0)
×
2119
                        break;
×
2120
                }
×
2121

2122
                /* fall through */
2123
            case 1:
×
2124
                ssl_state->curr_connp->record_length |= *(input++);
×
2125
                if (--input_len == 0)
×
2126
                    break;
×
2127

2128
                /* fall through */
2129
            case 2:
×
2130
                ssl_state->curr_connp->content_type = *(input++);
×
2131
                ssl_state->curr_connp->version = SSL_VERSION_2;
×
2132
                if (--input_len == 0)
×
2133
                    break;
×
2134

2135
                /* fall through */
2136
        }
113✔
2137

2138
    } else {
113✔
2139
        switch (ssl_state->curr_connp->bytes_processed) {
48✔
2140
            case 0:
48✔
2141
                if (input_len >= ssl_state->curr_connp->record_lengths_length + 1) {
48✔
2142
                    ssl_state->curr_connp->record_length = (0x3f & input[0]) << 8 | input[1];
43✔
2143
                    ssl_state->curr_connp->content_type = input[3];
43✔
2144
                    ssl_state->curr_connp->version = SSL_VERSION_2;
43✔
2145
                    ssl_state->curr_connp->bytes_processed += 4;
43✔
2146
                    return 4;
43✔
2147
                } else {
43✔
2148
                    ssl_state->curr_connp->record_length = (0x3f & *(input++)) << 8;
5✔
2149
                    if (--input_len == 0)
5✔
2150
                        break;
×
2151
                }
5✔
2152

2153
                /* fall through */
2154
            case 1:
5✔
2155
                ssl_state->curr_connp->record_length |= *(input++);
5✔
2156
                if (--input_len == 0)
5✔
2157
                    break;
1✔
2158

2159
                /* fall through */
2160
            case 2:
4✔
2161
                /* padding */
2162
                input++;
4✔
2163
                if (--input_len == 0)
4✔
2164
                    break;
4✔
2165

2166
                /* fall through */
UNCOV
2167
            case 3:
×
UNCOV
2168
                ssl_state->curr_connp->content_type = *(input++);
×
UNCOV
2169
                ssl_state->curr_connp->version = SSL_VERSION_2;
×
UNCOV
2170
                if (--input_len == 0)
×
2171
                    break;
×
2172

2173
                /* fall through */
2174
        }
48✔
2175
    }
48✔
2176

2177
    ssl_state->curr_connp->bytes_processed += (input - initial_input);
5✔
2178

2179
    return (int)(input - initial_input);
5✔
2180
}
161✔
2181

2182
static struct SSLDecoderResult SSLv2Decode(uint8_t direction, SSLState *ssl_state,
2183
        AppLayerParserState *pstate, const uint8_t *input, uint32_t input_len,
2184
        const StreamSlice stream_slice)
2185
{
165✔
2186
    const uint8_t *initial_input = input;
165✔
2187

2188
    if (ssl_state->curr_connp->bytes_processed == 0) {
165✔
2189
        if (input[0] & 0x80) {
161✔
2190
            ssl_state->curr_connp->record_lengths_length = 2;
113✔
2191
        } else {
113✔
2192
            ssl_state->curr_connp->record_lengths_length = 3;
48✔
2193
        }
48✔
2194

2195
        SCLogDebug("record start: ssl2.hdr frame");
161✔
2196
        AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input,
161✔
2197
                ssl_state->curr_connp->record_lengths_length + 1, direction, TLS_FRAME_SSLV2_HDR);
161✔
2198
    }
161✔
2199

2200
    SCLogDebug("direction %u ssl_state->curr_connp->record_lengths_length + 1 %u, "
165✔
2201
               "ssl_state->curr_connp->bytes_processed %u",
165✔
2202
            direction, ssl_state->curr_connp->record_lengths_length + 1,
165✔
2203
            ssl_state->curr_connp->bytes_processed);
165✔
2204
    /* the +1 is because we read one extra byte inside SSLv2ParseRecord
2205
       to read the msg_type */
2206
    if (ssl_state->curr_connp->bytes_processed <
165✔
2207
            (ssl_state->curr_connp->record_lengths_length + 1)) {
165✔
2208
        const int retval = SSLv2ParseRecord(direction, ssl_state, input, input_len);
161✔
2209
        SCLogDebug("retval %d ssl_state->curr_connp->record_length %u", retval,
161✔
2210
                ssl_state->curr_connp->record_length);
161✔
2211
        if (retval < 0 || retval > (int)input_len) {
161✔
2212
            DEBUG_VALIDATE_BUG_ON(retval > (int)input_len);
×
2213
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSLV2_HEADER);
×
2214
            return SSL_DECODER_ERROR(-1);
×
2215
        }
×
2216

2217
        AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input,
161✔
2218
                ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length,
161✔
2219
                direction, TLS_FRAME_SSLV2_PDU);
161✔
2220
        SCLogDebug("record start: ssl2.pdu frame");
161✔
2221

2222
        input += retval;
161✔
2223
        input_len -= retval;
161✔
2224
    }
161✔
2225

2226
    /* if we don't have the full record, we return incomplete */
2227
    if (ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length >
165✔
2228
            input_len + ssl_state->curr_connp->bytes_processed) {
165✔
2229
        uint32_t needed = ssl_state->curr_connp->record_length;
94✔
2230
        SCLogDebug("record len %u input_len %u parsed %u: need %u bytes more data",
94✔
2231
                ssl_state->curr_connp->record_length, input_len, (uint32_t)(input - initial_input),
94✔
2232
                needed);
94✔
2233
        return SSL_DECODER_INCOMPLETE((input - initial_input), needed);
94✔
2234
    }
94✔
2235

2236
    if (input_len == 0) {
71✔
2237
        return SSL_DECODER_OK((input - initial_input));
×
2238
    }
×
2239

2240
    /* record_length should never be zero */
2241
    if (ssl_state->curr_connp->record_length == 0) {
71✔
2242
        SCLogDebug("SSLv2 record length is zero");
×
2243
        SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSLV2_HEADER);
×
2244
        return SSL_DECODER_ERROR(-1);
×
2245
    }
×
2246

2247
    /* record_lengths_length should never be zero */
2248
    if (ssl_state->curr_connp->record_lengths_length == 0) {
71✔
2249
        SCLogDebug("SSLv2 record lengths length is zero");
×
2250
        SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSLV2_HEADER);
×
2251
        return SSL_DECODER_ERROR(-1);
×
2252
    }
×
2253

2254
    switch (ssl_state->curr_connp->content_type) {
71✔
2255
        case SSLV2_MT_ERROR:
1✔
2256
            SCLogDebug("SSLV2_MT_ERROR msg_type received. Error encountered "
1✔
2257
                       "in establishing the sslv2 session, may be version");
1✔
2258
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_ERROR_MSG_ENCOUNTERED);
1✔
2259

2260
            break;
1✔
2261

2262
        case SSLV2_MT_CLIENT_HELLO:
64✔
2263
            if (input_len < 6) {
64✔
2264
                SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSL_RECORD);
×
2265
                return SSL_DECODER_ERROR(-1);
×
2266
            }
×
2267

2268
            ssl_state->current_flags = SSL_AL_FLAG_STATE_CLIENT_HELLO;
64✔
2269
            ssl_state->current_flags |= SSL_AL_FLAG_SSL_CLIENT_HS;
64✔
2270
            UpdateClientState(ssl_state, TLS_STATE_CLIENT_HELLO_DONE);
64✔
2271

2272
            const uint16_t version = (uint16_t)(input[0] << 8) | input[1];
64✔
2273
            SCLogDebug("SSLv2: version %04x", version);
64✔
2274
            ssl_state->curr_connp->version = version;
64✔
2275
            uint16_t session_id_length = (input[5]) | (uint16_t)(input[4] << 8);
64✔
2276
            input += 6;
64✔
2277
            input_len -= 6;
64✔
2278
            ssl_state->curr_connp->bytes_processed += 6;
64✔
2279
            if (session_id_length == 0) {
64✔
2280
                ssl_state->current_flags |= SSL_AL_FLAG_SSL_NO_SESSION_ID;
63✔
2281
            }
63✔
2282
            break;
64✔
2283

2284
        case SSLV2_MT_CLIENT_MASTER_KEY:
×
2285
            if (!(ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS)) {
×
2286
                SCLogDebug("Client hello is not seen before master key "
×
2287
                           "message!");
×
2288
            }
×
2289
            ssl_state->current_flags = SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY;
×
2290

2291
            break;
×
2292

2293
        case SSLV2_MT_CLIENT_CERTIFICATE:
1✔
2294
            if (direction == 1) {
1✔
2295
                SCLogDebug("Incorrect SSL Record type sent in the toclient "
1✔
2296
                           "direction!");
1✔
2297
            } else {
1✔
2298
                ssl_state->current_flags = SSL_AL_FLAG_STATE_CLIENT_KEYX;
×
2299
            }
×
2300
            UpdateServerState(ssl_state, TLS_STATE_SERVER_CERT_DONE);
1✔
2301

2302
            /* fall through */
2303
        case SSLV2_MT_SERVER_VERIFY:
1✔
2304
        case SSLV2_MT_SERVER_FINISHED:
1✔
2305
            if (direction == 0 &&
1✔
2306
                    !(ssl_state->curr_connp->content_type &
1✔
2307
                    SSLV2_MT_CLIENT_CERTIFICATE)) {
×
2308
                SCLogDebug("Incorrect SSL Record type sent in the toserver "
×
2309
                           "direction!");
×
2310
            }
×
2311

2312
            /* fall through */
2313
        case SSLV2_MT_CLIENT_FINISHED:
2✔
2314
        case SSLV2_MT_REQUEST_CERTIFICATE:
2✔
2315
            /* both client hello and server hello must be seen */
2316
            if ((ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) &&
2✔
2317
                    (ssl_state->flags & SSL_AL_FLAG_SSL_SERVER_HS)) {
2✔
2318

2319
                if (direction == 0) {
×
2320
                    if (ssl_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) {
×
2321
                        ssl_state->current_flags |= SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED;
×
2322
                        SCLogDebug("SSLv2 client side has started the encryption");
×
2323
                    } else if (ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY) {
×
2324
                        ssl_state->current_flags = SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED;
×
2325
                        SCLogDebug("SSLv2 client side has started the encryption");
×
2326
                    }
×
2327
                } else {
×
2328
                    ssl_state->current_flags = SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED;
×
2329
                    SCLogDebug("SSLv2 Server side has started the encryption");
×
2330
                }
×
2331

2332
                if ((ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED) &&
×
2333
                    (ssl_state->flags & SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED))
×
2334
                {
×
2335
                    if (ssl_config.encrypt_mode != SSL_CNF_ENC_HANDLE_FULL) {
×
2336
                        SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_INSPECTION);
×
2337
                    }
×
2338

2339
                    if (ssl_config.encrypt_mode == SSL_CNF_ENC_HANDLE_BYPASS) {
×
2340
                        SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_REASSEMBLY);
×
2341
                        SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_BYPASS_READY);
×
2342
                    }
×
2343
                    SCLogDebug("SSLv2 No reassembly & inspection has been set");
×
2344
                }
×
2345
            }
×
2346

2347
            break;
2✔
2348

2349
        case SSLV2_MT_SERVER_HELLO:
1✔
2350
            ssl_state->current_flags = SSL_AL_FLAG_STATE_SERVER_HELLO;
1✔
2351
            ssl_state->current_flags |= SSL_AL_FLAG_SSL_SERVER_HS;
1✔
2352
            UpdateServerState(ssl_state, TLS_STATE_SERVER_HELLO);
1✔
2353

2354
            break;
1✔
2355
    }
71✔
2356

2357
    ssl_state->flags |= ssl_state->current_flags;
71✔
2358

2359
    if (input_len + ssl_state->curr_connp->bytes_processed >=
71✔
2360
            (ssl_state->curr_connp->record_length +
71✔
2361
            ssl_state->curr_connp->record_lengths_length)) {
71✔
2362

2363
        /* looks like we have another record after this */
2364
        uint32_t diff = ssl_state->curr_connp->record_length +
71✔
2365
                ssl_state->curr_connp->record_lengths_length + -
71✔
2366
                ssl_state->curr_connp->bytes_processed;
71✔
2367
        input += diff;
71✔
2368
        SSLParserReset(ssl_state);
71✔
2369

2370
        /* we still don't have the entire record for the one we are
2371
           currently parsing */
2372
    } else {
71✔
2373
        input += input_len;
×
2374
        ssl_state->curr_connp->bytes_processed += input_len;
×
2375
    }
×
2376
    return SSL_DECODER_OK((input - initial_input));
71✔
2377
}
71✔
2378

2379
static struct SSLDecoderResult SSLv3Decode(uint8_t direction, SSLState *ssl_state,
2380
        AppLayerParserState *pstate, const uint8_t *input, const uint32_t input_len,
2381
        const StreamSlice stream_slice)
2382
{
40,229✔
2383
    uint32_t parsed = 0;
40,229✔
2384
    uint32_t record_len; /* slice of input_len for the current record */
40,229✔
2385
    const bool first_call = (ssl_state->curr_connp->bytes_processed == 0);
40,229✔
2386

2387
    if (ssl_state->curr_connp->bytes_processed < SSLV3_RECORD_HDR_LEN) {
40,229✔
2388
        const uint16_t prev_version = ssl_state->curr_connp->version;
33,908✔
2389

2390
        int retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
33,908✔
2391
        if (retval < 0 || retval > (int)input_len) {
33,908✔
2392
            DEBUG_VALIDATE_BUG_ON(retval > (int)input_len);
×
2393
            SCLogDebug("SSLv3ParseRecord returned %d", retval);
×
2394
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_TLS_HEADER);
×
2395
            return SSL_DECODER_ERROR(-1);
×
2396
        }
×
2397
        parsed = retval;
33,908✔
2398

2399
        SCLogDebug("%s input %p record_length %u", (direction == 0) ? "toserver" : "toclient",
33,908✔
2400
                input, ssl_state->curr_connp->record_length);
33,908✔
2401

2402
        /* first the hdr frame at our first chance */
2403
        if (first_call) {
33,908✔
2404
            AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input, SSLV3_RECORD_HDR_LEN,
33,896✔
2405
                    direction, TLS_FRAME_HDR);
33,896✔
2406
        }
33,896✔
2407

2408
        /* parser is streaming for the initial header, then switches to incomplete
2409
         * API: so if we don't have the hdr yet, return consumed bytes and wait
2410
         * until we are called again with new data. */
2411
        if (ssl_state->curr_connp->bytes_processed < SSLV3_RECORD_HDR_LEN) {
33,908✔
2412
            SCLogDebug(
14✔
2413
                    "incomplete header, return %u bytes consumed and wait for more data", parsed);
14✔
2414
            return SSL_DECODER_OK(parsed);
14✔
2415
        }
14✔
2416

2417
        /* pdu frame needs record length, so only create it when hdr fully parsed. */
2418
        AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input,
33,894✔
2419
                ssl_state->curr_connp->record_length + retval, direction, TLS_FRAME_PDU);
33,894✔
2420
        record_len = MIN(input_len - parsed, ssl_state->curr_connp->record_length);
33,894✔
2421
        SCLogDebug(
33,894✔
2422
                "record_len %u (input_len %u, parsed %u, ssl_state->curr_connp->record_length %u)",
33,894✔
2423
                record_len, input_len, parsed, ssl_state->curr_connp->record_length);
33,894✔
2424

2425
        bool unknown_record = false;
33,894✔
2426
        switch (ssl_state->curr_connp->content_type) {
33,894✔
2427
            case SSLV3_CHANGE_CIPHER_SPEC:
3,750✔
2428
            case SSLV3_ALERT_PROTOCOL:
3,823✔
2429
            case SSLV3_HANDSHAKE_PROTOCOL:
18,083✔
2430
            case SSLV3_APPLICATION_PROTOCOL:
33,814✔
2431
            case SSLV3_HEARTBEAT_PROTOCOL:
33,814✔
2432
                break;
33,814✔
2433
            default:
80✔
2434
                unknown_record = true;
80✔
2435
                break;
80✔
2436
        }
33,894✔
2437

2438
        /* unknown record type. For TLS 1.0, 1.1 and 1.2 this is ok. For the rest it is fatal. Based
2439
         * on Wireshark logic. */
2440
        if (prev_version == TLS_VERSION_10 || prev_version == TLS_VERSION_11) {
33,894✔
2441
            if (unknown_record) {
6,219✔
2442
                SCLogDebug("unknown record, ignore it");
25✔
2443
                SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_RECORD_TYPE);
25✔
2444

2445
                ssl_state->curr_connp->bytes_processed = 0; // TODO review this reset logic
25✔
2446
                ssl_state->curr_connp->content_type = 0;
25✔
2447
                ssl_state->curr_connp->record_length = 0;
25✔
2448
                // restore last good version
2449
                ssl_state->curr_connp->version = prev_version;
25✔
2450
                return SSL_DECODER_OK(input_len); // consume everything
25✔
2451
            }
25✔
2452
        } else {
27,675✔
2453
            if (unknown_record) {
27,675✔
2454
                SCLogDebug("unknown record, fatal");
55✔
2455
                SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_RECORD_TYPE);
55✔
2456
                return SSL_DECODER_ERROR(-1);
55✔
2457
            }
55✔
2458
        }
27,675✔
2459

2460
        /* record_length should never be zero */
2461
        if (ssl_state->curr_connp->record_length == 0) {
33,814✔
2462
            SCLogDebug("SSLv3 Record length is 0");
15✔
2463
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_RECORD_LENGTH);
15✔
2464
            return SSL_DECODER_ERROR(-1);
15✔
2465
        }
15✔
2466

2467
        if (!TLSVersionValid(ssl_state->curr_connp->version)) {
33,799✔
2468
            SCLogDebug("ssl_state->curr_connp->version %04x", ssl_state->curr_connp->version);
24✔
2469
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_RECORD_VERSION);
24✔
2470
            return SSL_DECODER_ERROR(-1);
24✔
2471
        }
24✔
2472

2473
        if (ssl_state->curr_connp->bytes_processed == SSLV3_RECORD_HDR_LEN &&
33,775✔
2474
                ssl_state->curr_connp->record_length > SSLV3_RECORD_MAX_LEN) {
33,775✔
2475
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_RECORD_LENGTH);
6✔
2476
            return SSL_DECODER_ERROR(-1);
6✔
2477
        }
6✔
2478
        DEBUG_VALIDATE_BUG_ON(ssl_state->curr_connp->bytes_processed > SSLV3_RECORD_HDR_LEN);
33,769✔
2479
    } else {
33,769✔
2480
        ValidateRecordState(ssl_state->curr_connp);
6,321✔
2481

2482
        record_len = (ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN)- ssl_state->curr_connp->bytes_processed;
6,321✔
2483
        record_len = MIN(input_len, record_len);
6,321✔
2484
    }
6,321✔
2485
    SCLogDebug("record length %u processed %u got %u",
40,090✔
2486
            ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed, record_len);
40,090✔
2487

2488
    /* if we don't have the full record, we return incomplete */
2489
    if (ssl_state->curr_connp->record_length > input_len - parsed) {
40,090✔
2490
        /* no need to use incomplete api buffering for application
2491
         * records that we'll not use anyway. */
2492
        if (ssl_state->curr_connp->content_type == SSLV3_APPLICATION_PROTOCOL) {
7,925✔
2493
            SCLogDebug("application record");
6,591✔
2494
        } else {
6,591✔
2495
            uint32_t needed = ssl_state->curr_connp->record_length;
1,334✔
2496
            SCLogDebug("record len %u input_len %u parsed %u: need %u bytes more data",
1,334✔
2497
                    ssl_state->curr_connp->record_length, input_len, parsed, needed);
1,334✔
2498
            DEBUG_VALIDATE_BUG_ON(needed > SSLV3_RECORD_MAX_LEN);
1,334✔
2499
            return SSL_DECODER_INCOMPLETE(parsed, needed);
1,334✔
2500
        }
1,334✔
2501
    }
7,925✔
2502

2503
    if (record_len == 0) {
38,756✔
2504
        return SSL_DECODER_OK(parsed);
×
2505
    }
×
2506

2507
    AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input + parsed,
38,756✔
2508
            ssl_state->curr_connp->record_length, direction, TLS_FRAME_DATA);
38,756✔
2509

2510
    switch (ssl_state->curr_connp->content_type) {
38,756✔
2511
        /* we don't need any data from these types */
2512
        case SSLV3_CHANGE_CIPHER_SPEC:
3,739✔
2513
            ssl_state->flags |= SSL_AL_FLAG_CHANGE_CIPHER_SPEC;
3,739✔
2514

2515
            if (direction) {
3,739✔
2516
                ssl_state->flags |= SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC;
1,884✔
2517
            } else {
1,884✔
2518
                ssl_state->flags |= SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC;
1,855✔
2519

2520
                // TODO TLS 1.3
2521
                UpdateClientState(ssl_state, TLS_STATE_CLIENT_HANDSHAKE_DONE);
1,855✔
2522
            }
1,855✔
2523
            break;
3,739✔
2524

2525
        case SSLV3_ALERT_PROTOCOL: {
73✔
2526
            AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input + parsed,
73✔
2527
                    ssl_state->curr_connp->record_length, direction, TLS_FRAME_ALERT_DATA);
73✔
2528

2529
            int retval = SSLv3ParseAlertProtocol(ssl_state, input + parsed, record_len, direction);
73✔
2530
            if (retval < 0) {
73✔
2531
                SCLogDebug("SSLv3ParseAlertProtocol returned %d", retval);
×
2532
                return SSL_DECODER_ERROR(-1);
×
2533
            }
×
2534
            break;
73✔
2535
        }
73✔
2536
        case SSLV3_APPLICATION_PROTOCOL:
20,781✔
2537
            /* In TLSv1.3 early data (0-RTT) could be sent before the
2538
               handshake is complete (rfc8446, section 2.3). We should
2539
               therefore not mark the handshake as done before we have
2540
               seen the ServerHello record. */
2541
            if ((ssl_state->flags & SSL_AL_FLAG_EARLY_DATA) &&
20,781✔
2542
                    ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0))
20,781✔
2543
                break;
2✔
2544

2545
            /* if we see (encrypted) application data, then this means the
2546
               handshake must be done */
2547
            if (ssl_state->curr_connp == &ssl_state->client_connp) {
20,779✔
2548
                UpdateClientState(ssl_state, TLS_STATE_CLIENT_HANDSHAKE_DONE);
5,602✔
2549
            } else {
15,177✔
2550
                UpdateServerState(ssl_state, TLS_STATE_SERVER_HANDSHAKE_DONE);
15,177✔
2551
            }
15,177✔
2552

2553
            if (ssl_config.encrypt_mode != SSL_CNF_ENC_HANDLE_FULL) {
20,779✔
2554
                SCLogDebug("setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
20,779✔
2555
                SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD);
20,779✔
2556
            }
20,779✔
2557

2558
            /* Encrypted data, reassembly not asked, bypass asked, let's sacrifice
2559
             * heartbeat lke inspection to be able to be able to bypass the flow */
2560
            if (ssl_config.encrypt_mode == SSL_CNF_ENC_HANDLE_BYPASS) {
20,779✔
UNCOV
2561
                SCLogDebug("setting APP_LAYER_PARSER_NO_REASSEMBLY");
×
UNCOV
2562
                SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_REASSEMBLY);
×
UNCOV
2563
                SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_INSPECTION);
×
UNCOV
2564
                SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_BYPASS_READY);
×
UNCOV
2565
            }
×
2566
            break;
20,779✔
2567

2568
        case SSLV3_HANDSHAKE_PROTOCOL: {
14,163✔
2569
            if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
14,163✔
2570
                /* In TLSv1.3, ChangeCipherSpec is only used for middlebox
2571
                   compatibility (rfc8446, appendix D.4). */
2572
                // Client hello flags is needed to have a valid version
2573
                if ((ssl_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
3,873✔
2574
                        (ssl_state->client_connp.version > TLS_VERSION_12) &&
3,873✔
2575
                        ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0)) {
3,873✔
2576
                    /* do nothing */
2577
                } else {
3,866✔
2578
                    // if we started parsing this, we must stop
2579
                    break;
3,866✔
2580
                }
3,866✔
2581
            }
3,873✔
2582

2583
            if (ssl_state->curr_connp->record_length < 4) {
10,297✔
2584
                SSLParserReset(ssl_state);
×
2585
                SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSL_RECORD);
×
2586
                SCLogDebug("record len < 4 => %u", ssl_state->curr_connp->record_length);
×
2587
                return SSL_DECODER_ERROR(-1);
×
2588
            }
×
2589

2590
            int retval = SSLv3ParseHandshakeProtocol(ssl_state, input + parsed,
10,297✔
2591
                                                     record_len, direction);
10,297✔
2592
            SCLogDebug("retval %d", retval);
10,297✔
2593
            if (retval < 0 || retval > (int)record_len) {
10,297✔
2594
                DEBUG_VALIDATE_BUG_ON(retval > (int)record_len);
3✔
2595
                SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE);
3✔
2596
                SCLogDebug("SSLv3ParseHandshakeProtocol returned %d", retval);
3✔
2597
                return SSL_DECODER_ERROR(-1);
3✔
2598
            }
3✔
2599
            ValidateRecordState(ssl_state->curr_connp);
10,294✔
2600
            break;
10,294✔
2601
        }
10,297✔
2602
        case SSLV3_HEARTBEAT_PROTOCOL: {
×
2603
            AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input + parsed,
×
2604
                    ssl_state->curr_connp->record_length, direction, TLS_FRAME_HB_DATA);
×
2605
            int retval = SSLv3ParseHeartbeatProtocol(ssl_state, input + parsed,
×
2606
                                                 record_len, direction);
×
2607
            if (retval < 0) {
×
2608
                SCLogDebug("SSLv3ParseHeartbeatProtocol returned %d", retval);
×
2609
                return SSL_DECODER_ERROR(-1);
×
2610
            }
×
2611
            break;
×
2612
        }
×
2613
        default:
×
2614
            // should be unreachable now that we check after header parsing
2615
            DEBUG_VALIDATE_BUG_ON(1);
×
2616
            SCLogDebug("unsupported record type");
×
2617
            return SSL_DECODER_ERROR(-1);
×
2618
    }
38,756✔
2619

2620
    parsed += record_len;
38,753✔
2621
    ssl_state->curr_connp->bytes_processed += record_len;
38,753✔
2622

2623
    if (ssl_state->curr_connp->bytes_processed >=
38,753✔
2624
            ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN) {
38,753✔
2625
        SCLogDebug("record complete, trigger RAW");
33,630✔
2626
        SCAppLayerParserTriggerRawStreamInspection(
33,630✔
2627
                ssl_state->f, direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
33,630✔
2628
        SSLParserReset(ssl_state);
33,630✔
2629
        ValidateRecordState(ssl_state->curr_connp);
33,630✔
2630
        return SSL_DECODER_OK(parsed);
33,630✔
2631

2632
    } else {
33,630✔
2633
        /* we still don't have the entire record for the one we are
2634
           currently parsing */
2635
        ValidateRecordState(ssl_state->curr_connp);
5,123✔
2636
        return SSL_DECODER_OK(parsed);
5,123✔
2637
    }
5,123✔
2638
}
38,753✔
2639

2640
/**
2641
 * \internal
2642
 * \brief SSLv2, SSLv23, SSLv3, TLSv1.1, TLSv1.2, TLSv1.3 parser.
2643
 *
2644
 *        On parsing error, this should be the only function that should reset
2645
 *        the parser state, to avoid multiple functions in the chain resetting
2646
 *        the parser state.
2647
 *
2648
 * \param direction 0 for toserver, 1 for toclient.
2649
 * \param alstate   Pointer to the state.
2650
 * \param pstate    Application layer parser state for this session.
2651
 * \param output    Pointer to the list of parsed output elements.
2652
 *
2653
 * \todo On reaching an inconsistent state, check if the input has
2654
 *  another new record, instead of just returning after the reset
2655
 *
2656
 * \retval >=0 On success.
2657
 */
2658
static AppLayerResult SSLDecode(Flow *f, uint8_t direction, void *alstate,
2659
        AppLayerParserState *pstate, StreamSlice stream_slice)
2660
{
23,036✔
2661
    SSLState *ssl_state = (SSLState *)alstate;
23,036✔
2662
    ssl_state->tx_data.updated_tc = true;
23,036✔
2663
    ssl_state->tx_data.updated_ts = true;
23,036✔
2664
    uint32_t counter = 0;
23,036✔
2665
    ssl_state->f = f;
23,036✔
2666
    const uint8_t *input = StreamSliceGetData(&stream_slice);
23,036✔
2667
    const uint8_t *init_input = input;
23,036✔
2668
    int32_t input_len = (int32_t)StreamSliceGetDataLen(&stream_slice);
23,036✔
2669

2670
    if ((input == NULL || input_len == 0) &&
23,036✔
2671
            ((direction == 0 && SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS)) ||
23,036✔
2672
                    (direction == 1 &&
681✔
2673
                            SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC)))) {
681✔
2674
        /* flag session as finished if APP_LAYER_PARSER_EOF is set */
2675
        if (direction == 0)
681✔
2676
            UpdateClientState(ssl_state, TLS_STATE_CLIENT_FINISHED);
369✔
2677
        else
312✔
2678
            UpdateServerState(ssl_state, TLS_STATE_SERVER_FINISHED);
312✔
2679
        SCReturnStruct(APP_LAYER_OK);
681✔
2680
    } else if (input == NULL || input_len == 0) {
22,355✔
2681
        SCReturnStruct(APP_LAYER_ERROR);
×
2682
    }
×
2683

2684
    if (direction == 0)
22,355✔
2685
        ssl_state->curr_connp = &ssl_state->client_connp;
9,117✔
2686
    else
13,238✔
2687
        ssl_state->curr_connp = &ssl_state->server_connp;
13,238✔
2688

2689
    /* If entering on a new record, reset the current flags. */
2690
    if (ssl_state->curr_connp->bytes_processed == 0) {
22,355✔
2691
        ssl_state->current_flags = 0;
16,018✔
2692
    }
16,018✔
2693

2694
    /* if we have more than one record */
2695
    uint32_t max_records = MAX((input_len / SSL_RECORD_MINIMUM_LENGTH),1);
22,355✔
2696
    while (input_len > 0) {
61,218✔
2697
        if (counter > max_records) {
40,394✔
2698
            SCLogDebug("Looks like we have looped quite a bit. Reset state "
×
2699
                       "and get out of here");
×
2700
            SSLParserReset(ssl_state);
×
2701
            SSLSetEvent(ssl_state,
×
2702
                        TLS_DECODER_EVENT_TOO_MANY_RECORDS_IN_PACKET);
×
2703
            return APP_LAYER_ERROR;
×
2704
        }
×
2705

2706
        /* ssl_state->bytes_processed is zero for a fresh record or
2707
           positive to indicate a record currently being parsed */
2708

2709
        if (ssl_state->curr_connp->bytes_processed == 0) {
40,394✔
2710
            if ((input[0] & 0x80) || (input[0] & 0x40)) {
34,057✔
2711
                /* only SSLv2, has one of the top 2 bits set */
2712
                ssl_state->curr_connp->version = SSL_VERSION_2;
161✔
2713
                SCLogDebug("SSLv2 detected");
161✔
2714
            } else if (ssl_state->curr_connp->version == SSL_VERSION_2) {
33,896✔
2715
                ssl_state->curr_connp->version = TLS_VERSION_UNKNOWN;
1✔
2716
                SCLogDebug("SSL/TLS version reset");
1✔
2717
            }
1✔
2718
        }
34,057✔
2719
        SCLogDebug("record %u: bytes_processed %u, version %02X, input_len %u", counter,
40,394✔
2720
                ssl_state->curr_connp->bytes_processed, ssl_state->curr_connp->version, input_len);
40,394✔
2721

2722
        if (ssl_state->curr_connp->version == SSL_VERSION_2) {
40,394✔
2723
            if (ssl_state->curr_connp->bytes_processed == 0) {
165✔
2724
                SCLogDebug("New SSLv2 record parsing");
161✔
2725
            } else {
161✔
2726
                SCLogDebug("Continuing parsing SSLv2 record");
4✔
2727
            }
4✔
2728
            struct SSLDecoderResult r =
165✔
2729
                    SSLv2Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
165✔
2730
            if (r.retval < 0 || r.retval > input_len) {
165✔
2731
                DEBUG_VALIDATE_BUG_ON(r.retval > input_len);
×
2732
                SCLogDebug("Error parsing SSLv2. Resetting parser "
×
2733
                           "state. Let's get outta here");
×
2734
                SSLParserReset(ssl_state);
×
2735
                SSLSetEvent(ssl_state,
×
2736
                        TLS_DECODER_EVENT_INVALID_SSL_RECORD);
×
2737
                return APP_LAYER_ERROR;
×
2738
            } else if (r.needed) {
165✔
2739
                input += r.retval;
94✔
2740
                SCLogDebug("returning consumed %" PRIuMAX " needed %u",
94✔
2741
                        (uintmax_t)(input - init_input), r.needed);
94✔
2742
                SCReturnStruct(APP_LAYER_INCOMPLETE((uint32_t)(input - init_input), r.needed));
94✔
2743
            }
94✔
2744
            input_len -= r.retval;
71✔
2745
            input += r.retval;
71✔
2746
            SCLogDebug("SSLv2 decoder consumed %d bytes: %u left", r.retval, input_len);
71✔
2747
        } else {
40,229✔
2748
            if (ssl_state->curr_connp->bytes_processed == 0) {
40,229✔
2749
                SCLogDebug("New TLS record: record_length %u",
33,896✔
2750
                        ssl_state->curr_connp->record_length);
33,896✔
2751
            } else {
33,896✔
2752
                SCLogDebug("Continuing parsing TLS record: record_length %u, bytes_processed %u",
6,333✔
2753
                        ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed);
6,333✔
2754
            }
6,333✔
2755
            struct SSLDecoderResult r =
40,229✔
2756
                    SSLv3Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
40,229✔
2757
            if (r.retval < 0 || r.retval > input_len) {
40,229✔
2758
                DEBUG_VALIDATE_BUG_ON(r.retval > input_len);
103✔
2759
                SCLogDebug("Error parsing TLS. Resetting parser "
103✔
2760
                           "state.  Let's get outta here");
103✔
2761
                SSLParserReset(ssl_state);
103✔
2762
                return APP_LAYER_ERROR;
103✔
2763
            } else if (r.needed) {
40,126✔
2764
                input += r.retval;
1,334✔
2765
                SCLogDebug("returning consumed %" PRIuMAX " needed %u",
1,334✔
2766
                        (uintmax_t)(input - init_input), r.needed);
1,334✔
2767
                SCReturnStruct(APP_LAYER_INCOMPLETE((uint32_t)(input - init_input), r.needed));
1,334✔
2768
            }
1,334✔
2769
            input_len -= r.retval;
38,792✔
2770
            input += r.retval;
38,792✔
2771
            SCLogDebug("TLS decoder consumed %d bytes: %u left", r.retval, input_len);
38,792✔
2772

2773
            if (ssl_state->curr_connp->bytes_processed == SSLV3_RECORD_HDR_LEN
38,792✔
2774
                    && ssl_state->curr_connp->record_length == 0) {
38,792✔
2775
                SCLogDebug("TLS empty record");
×
2776
                /* empty record */
2777
                SSLParserReset(ssl_state);
×
2778
            }
×
2779
        }
38,792✔
2780
        counter++;
38,863✔
2781
    } /* while (input_len) */
38,863✔
2782

2783
    /* mark handshake as done if we have subject and issuer */
2784
    if ((ssl_state->flags & SSL_AL_FLAG_NEED_CLIENT_CERT) &&
20,824✔
2785
            ssl_state->client_connp.cert0_subject && ssl_state->client_connp.cert0_issuerdn) {
20,824✔
2786
        /* update both sides to keep existing behavior */
2787
        UpdateClientState(ssl_state, TLS_STATE_CLIENT_HANDSHAKE_DONE);
5✔
2788
        UpdateServerState(ssl_state, TLS_STATE_SERVER_HANDSHAKE_DONE);
5✔
2789
    } else if ((ssl_state->flags & SSL_AL_FLAG_NEED_CLIENT_CERT) == 0 &&
20,819✔
2790
               ssl_state->server_connp.cert0_subject && ssl_state->server_connp.cert0_issuerdn) {
20,819✔
2791
        /* update both sides to keep existing behavior */
2792
        UpdateClientState(ssl_state, TLS_STATE_CLIENT_HANDSHAKE_DONE);
9,159✔
2793
        UpdateServerState(ssl_state, TLS_STATE_SERVER_HANDSHAKE_DONE);
9,159✔
2794
    }
9,159✔
2795

2796
    /* flag session as finished if APP_LAYER_PARSER_EOF is set */
2797
    if (SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS) &&
20,824✔
2798
            SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC)) {
20,824✔
2799
        /* update both sides to keep existing behavior */
2800
        UpdateClientState(ssl_state, TLS_STATE_CLIENT_FINISHED);
26✔
2801
        UpdateServerState(ssl_state, TLS_STATE_SERVER_FINISHED);
26✔
2802
    }
26✔
2803

2804
    return APP_LAYER_OK;
20,824✔
2805
}
22,355✔
2806

2807
static AppLayerResult SSLParseClientRecord(Flow *f, void *alstate, AppLayerParserState *pstate,
2808
        StreamSlice stream_slice, void *local_data)
2809
{
9,486✔
2810
    return SSLDecode(f, 0 /* toserver */, alstate, pstate, stream_slice);
9,486✔
2811
}
9,486✔
2812

2813
static AppLayerResult SSLParseServerRecord(Flow *f, void *alstate, AppLayerParserState *pstate,
2814
        StreamSlice stream_slice, void *local_data)
2815
{
13,550✔
2816
    return SSLDecode(f, 1 /* toclient */, alstate, pstate, stream_slice);
13,550✔
2817
}
13,550✔
2818

2819
/**
2820
 * \internal
2821
 * \brief Function to allocate the SSL state memory.
2822
 */
2823
static void *SSLStateAlloc(void *orig_state, AppProto proto_orig)
2824
{
2,905✔
2825
    SSLState *ssl_state = SCCalloc(1, sizeof(SSLState));
2,905✔
2826
    if (unlikely(ssl_state == NULL))
2,905✔
2827
        return NULL;
×
2828
    TAILQ_INIT(&ssl_state->server_connp.certs);
2,905✔
2829
    TAILQ_INIT(&ssl_state->client_connp.certs);
2,905✔
2830

2831
    return (void *)ssl_state;
2,905✔
2832
}
2,905✔
2833

2834
static void SSLStateCertSANFree(SSLStateConnp *connp)
2835
{
5,810✔
2836
    if (connp->cert0_sans) {
5,810✔
2837
        for (uint16_t i = 0; i < connp->cert0_sans_num; i++) {
11,661✔
2838
            SCX509ArrayFree(connp->cert0_sans[i].san, connp->cert0_sans[i].san_len);
10,282✔
2839
        }
10,282✔
2840
        SCFree(connp->cert0_sans);
1,379✔
2841
    }
1,379✔
2842
}
5,810✔
2843

2844
/**
2845
 * \internal
2846
 * \brief Function to free the SSL state memory.
2847
 */
2848
static void SSLStateFree(void *p)
2849
{
2,905✔
2850
    SSLState *ssl_state = (SSLState *)p;
2,905✔
2851
    SSLCertsChain *item;
2,905✔
2852

2853
    if (ssl_state->client_connp.cert0_subject)
2,905✔
2854
        SCX509ArrayFree(
19✔
2855
                ssl_state->client_connp.cert0_subject, ssl_state->client_connp.cert0_subject_len);
19✔
2856
    if (ssl_state->client_connp.cert0_issuerdn)
2,905✔
2857
        SCX509ArrayFree(
19✔
2858
                ssl_state->client_connp.cert0_issuerdn, ssl_state->client_connp.cert0_issuerdn_len);
19✔
2859
    if (ssl_state->client_connp.cert0_serial)
2,905✔
2860
        SCX509ArrayFree(
19✔
2861
                ssl_state->client_connp.cert0_serial, ssl_state->client_connp.cert0_serial_len);
19✔
2862
    if (ssl_state->client_connp.cert0_fingerprint)
2,905✔
2863
        SCFree(ssl_state->client_connp.cert0_fingerprint);
19✔
2864
    if (ssl_state->client_connp.sni)
2,905✔
2865
        SCFree(ssl_state->client_connp.sni);
2,417✔
2866
    if (ssl_state->client_connp.session_id)
2,905✔
2867
        SCFree(ssl_state->client_connp.session_id);
1,932✔
2868
    if (ssl_state->client_connp.hs_buffer)
2,905✔
2869
        SCFree(ssl_state->client_connp.hs_buffer);
14✔
2870

2871
    if (ssl_state->server_connp.cert0_subject)
2,905✔
2872
        SCX509ArrayFree(
1,360✔
2873
                ssl_state->server_connp.cert0_subject, ssl_state->server_connp.cert0_subject_len);
1,360✔
2874
    if (ssl_state->server_connp.cert0_issuerdn)
2,905✔
2875
        SCX509ArrayFree(
1,360✔
2876
                ssl_state->server_connp.cert0_issuerdn, ssl_state->server_connp.cert0_issuerdn_len);
1,360✔
2877
    if (ssl_state->server_connp.cert0_serial)
2,905✔
2878
        SCX509ArrayFree(
1,360✔
2879
                ssl_state->server_connp.cert0_serial, ssl_state->server_connp.cert0_serial_len);
1,360✔
2880
    if (ssl_state->server_connp.cert0_fingerprint)
2,905✔
2881
        SCFree(ssl_state->server_connp.cert0_fingerprint);
1,360✔
2882
    if (ssl_state->server_connp.sni)
2,905✔
2883
        SCFree(ssl_state->server_connp.sni);
×
2884
    if (ssl_state->server_connp.session_id)
2,905✔
2885
        SCFree(ssl_state->server_connp.session_id);
1,645✔
2886

2887
    if (ssl_state->client_connp.hs)
2,905✔
2888
        SCTLSHandshakeFree(ssl_state->client_connp.hs);
2,750✔
2889
    if (ssl_state->client_connp.ja3_str)
2,905✔
2890
        Ja3BufferFree(&ssl_state->client_connp.ja3_str);
2,747✔
2891
    if (ssl_state->client_connp.ja3_hash)
2,905✔
2892
        SCFree(ssl_state->client_connp.ja3_hash);
2,617✔
2893
    if (ssl_state->server_connp.hs)
2,905✔
2894
        SCTLSHandshakeFree(ssl_state->server_connp.hs);
2,446✔
2895
    if (ssl_state->server_connp.ja3_str)
2,905✔
2896
        Ja3BufferFree(&ssl_state->server_connp.ja3_str);
2,446✔
2897
    if (ssl_state->server_connp.ja3_hash)
2,905✔
2898
        SCFree(ssl_state->server_connp.ja3_hash);
2,430✔
2899
    if (ssl_state->server_connp.hs_buffer)
2,905✔
2900
        SCFree(ssl_state->server_connp.hs_buffer);
28✔
2901

2902
    SSLStateCertSANFree(&ssl_state->server_connp);
2,905✔
2903
    SSLStateCertSANFree(&ssl_state->client_connp);
2,905✔
2904

2905
    SCAppLayerTxDataCleanup(&ssl_state->tx_data);
2,905✔
2906

2907
    /* Free certificate chain */
2908
    if (ssl_state->server_connp.certs_buffer)
2,905✔
2909
        SCFree(ssl_state->server_connp.certs_buffer);
1,460✔
2910
    while ((item = TAILQ_FIRST(&ssl_state->server_connp.certs))) {
5,671✔
2911
        TAILQ_REMOVE(&ssl_state->server_connp.certs, item, next);
2,766✔
2912
        SCFree(item);
2,766✔
2913
    }
2,766✔
2914
    TAILQ_INIT(&ssl_state->server_connp.certs);
2,905✔
2915
    /* Free certificate chain */
2916
    if (ssl_state->client_connp.certs_buffer)
2,905✔
2917
        SCFree(ssl_state->client_connp.certs_buffer);
29✔
2918
    while ((item = TAILQ_FIRST(&ssl_state->client_connp.certs))) {
2,935✔
2919
        TAILQ_REMOVE(&ssl_state->client_connp.certs, item, next);
30✔
2920
        SCFree(item);
30✔
2921
    }
30✔
2922
    TAILQ_INIT(&ssl_state->client_connp.certs);
2,905✔
2923

2924
    SCFree(ssl_state);
2,905✔
2925
}
2,905✔
2926

2927
static void SSLStateTransactionFree(void *state, uint64_t tx_id)
2928
{
81✔
2929
    /* do nothing */
2930
}
81✔
2931

2932
static AppProto SSLProbingParser(
2933
        const Flow *f, uint8_t direction, const uint8_t *input, uint32_t ilen, uint8_t *rdir)
2934
{
367✔
2935
    /* probably a rst/fin sending an eof */
2936
    if (ilen < 3)
367✔
2937
        return ALPROTO_UNKNOWN;
114✔
2938

2939
    /* for now just the 3 byte header ones */
2940
    /* \todo Detect the 2 byte ones */
2941
    if ((input[0] & 0x80) && (input[2] == 0x01)) {
253✔
2942
        return ALPROTO_TLS;
67✔
2943
    }
67✔
2944

2945
    return ALPROTO_FAILED;
186✔
2946
}
253✔
2947

2948
static int SSLStateGetStateIdByName(const char *name, const uint8_t direction)
2949
{
19✔
2950
    SCEnumCharMap *map =
19✔
2951
            direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
19✔
2952

2953
    int id = SCMapEnumNameToValue(name, map);
19✔
2954
    if (id < 0) {
19✔
2955
        return -1;
12✔
2956
    }
12✔
2957
    return id;
7✔
2958
}
19✔
2959

2960
static const char *SSLStateGetStateNameById(const int id, const uint8_t direction)
2961
{
370✔
2962
    SCEnumCharMap *map =
370✔
2963
            direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
370✔
2964
    const char *name = SCMapEnumValueToName(id, map);
370✔
2965
    return name;
370✔
2966
}
370✔
2967

2968
static int SSLStateGetFrameIdByName(const char *frame_name)
2969
{
631✔
2970
    int id = SCMapEnumNameToValue(frame_name, tls_frame_table);
631✔
2971
    if (id < 0) {
631✔
2972
        return -1;
154✔
2973
    }
154✔
2974
    return id;
477✔
2975
}
631✔
2976

2977
static const char *SSLStateGetFrameNameById(const uint8_t frame_id)
UNCOV
2978
{
×
UNCOV
2979
    const char *name = SCMapEnumValueToName(frame_id, tls_frame_table);
×
UNCOV
2980
    return name;
×
UNCOV
2981
}
×
2982

2983
static int SSLStateGetEventInfo(
2984
        const char *event_name, uint8_t *event_id, AppLayerEventType *event_type)
2985
{
19,308✔
2986
    if (SCAppLayerGetEventIdByName(event_name, tls_decoder_event_table, event_id) == 0) {
19,308✔
2987
        *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
17,699✔
2988
        return 0;
17,699✔
2989
    }
17,699✔
2990
    return -1;
1,609✔
2991
}
19,308✔
2992

2993
static int SSLStateGetEventInfoById(
2994
        uint8_t event_id, const char **event_name, AppLayerEventType *event_type)
2995
{
2,946✔
2996
    *event_name = SCMapEnumValueToName(event_id, tls_decoder_event_table);
2,946✔
2997
    if (*event_name == NULL) {
2,946✔
2998
        SCLogError("event \"%d\" not present in "
×
2999
                   "ssl's enum map table.",
×
3000
                event_id);
×
3001
        /* yes this is fatal */
3002
        return -1;
×
3003
    }
×
3004

3005
    *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
2,946✔
3006

3007
    return 0;
2,946✔
3008
}
2,946✔
3009

3010
static int SSLRegisterPatternsForProtocolDetection(void)
3011
{
4✔
3012
    if (SCAppLayerProtoDetectPMRegisterPatternCSwPP(IPPROTO_TCP, ALPROTO_TLS, "|01 00 02|", 5, 2,
4✔
3013
                STREAM_TOSERVER, SSLProbingParser, 0, 3) < 0) {
4✔
3014
        return -1;
×
3015
    }
×
3016

3017
    /** SSLv3 */
3018
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3019
                IPPROTO_TCP, ALPROTO_TLS, "|01 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
4✔
3020
        return -1;
×
3021
    }
×
3022
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3023
                IPPROTO_TCP, ALPROTO_TLS, "|16 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
4✔
3024
        return -1;
×
3025
    }
×
3026

3027
    /** TLSv1 */
3028
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3029
                IPPROTO_TCP, ALPROTO_TLS, "|01 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
4✔
3030
        return -1;
×
3031
    }
×
3032
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3033
                IPPROTO_TCP, ALPROTO_TLS, "|16 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
4✔
3034
        return -1;
×
3035
    }
×
3036

3037
    /** TLSv1.1 */
3038
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3039
                IPPROTO_TCP, ALPROTO_TLS, "|01 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
4✔
3040
        return -1;
×
3041
    }
×
3042
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3043
                IPPROTO_TCP, ALPROTO_TLS, "|16 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
4✔
3044
        return -1;
×
3045
    }
×
3046

3047
    /** TLSv1.2 */
3048
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3049
                IPPROTO_TCP, ALPROTO_TLS, "|01 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
4✔
3050
        return -1;
×
3051
    }
×
3052
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3053
                IPPROTO_TCP, ALPROTO_TLS, "|16 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
4✔
3054
        return -1;
×
3055
    }
×
3056

3057
    /***** toclient direction *****/
3058

3059
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3060
                IPPROTO_TCP, ALPROTO_TLS, "|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
4✔
3061
        return -1;
×
3062
    }
×
3063
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3064
                IPPROTO_TCP, ALPROTO_TLS, "|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
4✔
3065
        return -1;
×
3066
    }
×
3067
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3068
                IPPROTO_TCP, ALPROTO_TLS, "|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
4✔
3069
        return -1;
×
3070
    }
×
3071

3072
    /** TLSv1 */
3073
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3074
                IPPROTO_TCP, ALPROTO_TLS, "|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
4✔
3075
        return -1;
×
3076
    }
×
3077
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3078
                IPPROTO_TCP, ALPROTO_TLS, "|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
4✔
3079
        return -1;
×
3080
    }
×
3081
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3082
                IPPROTO_TCP, ALPROTO_TLS, "|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
4✔
3083
        return -1;
×
3084
    }
×
3085

3086
    /** TLSv1.1 */
3087
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3088
                IPPROTO_TCP, ALPROTO_TLS, "|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
4✔
3089
        return -1;
×
3090
    }
×
3091
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3092
                IPPROTO_TCP, ALPROTO_TLS, "|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
4✔
3093
        return -1;
×
3094
    }
×
3095
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3096
                IPPROTO_TCP, ALPROTO_TLS, "|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
4✔
3097
        return -1;
×
3098
    }
×
3099

3100
    /** TLSv1.2 */
3101
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3102
                IPPROTO_TCP, ALPROTO_TLS, "|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
4✔
3103
        return -1;
×
3104
    }
×
3105
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3106
                IPPROTO_TCP, ALPROTO_TLS, "|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
4✔
3107
        return -1;
×
3108
    }
×
3109
    if (SCAppLayerProtoDetectPMRegisterPatternCS(
4✔
3110
                IPPROTO_TCP, ALPROTO_TLS, "|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
4✔
3111
        return -1;
×
3112
    }
×
3113

3114
    /* Subsection - SSLv2 style record by client, but informing the server
3115
     * the max version it supports.
3116
     * Updated by Anoop Saldanha.  Disabled it for now.  We'll get back to
3117
     * it after some tests */
3118
#if 0
3119
    if (SCAppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3120
                                               "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
3121
    {
3122
        return -1;
3123
    }
3124
    if (SCAppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3125
                                               "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
3126
    {
3127
        return -1;
3128
    }
3129
#endif
3130

3131
    return 0;
4✔
3132
}
4✔
3133

3134
#ifdef HAVE_JA3
3135
static void CheckJA3Enabled(void)
3136
{
4✔
3137
    const char *strval = NULL;
4✔
3138
    /* Check if we should generate JA3 fingerprints */
3139
    int enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
4✔
3140
    if (SCConfGet("app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
4✔
3141
        enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
4✔
3142
    } else if (strcmp(strval, "auto") == 0) {
4✔
3143
        enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
×
UNCOV
3144
    } else if (SCConfValIsFalse(strval)) {
×
UNCOV
3145
        enable_ja3 = 0;
×
UNCOV
3146
        ssl_config.disable_ja3 = true;
×
UNCOV
3147
    } else if (SCConfValIsTrue(strval)) {
×
UNCOV
3148
        enable_ja3 = true;
×
UNCOV
3149
    }
×
3150
    SC_ATOMIC_SET(ssl_config.enable_ja3, enable_ja3);
4✔
3151
    if (!ssl_config.disable_ja3 && !g_disable_hashing) {
4✔
3152
        /* The feature is available, i.e. _could_ be activated by a rule or
3153
            even is enabled in the configuration. */
3154
        ProvidesFeature(FEATURE_JA3);
4✔
3155
    }
4✔
3156
}
4✔
3157
#endif /* HAVE_JA3 */
3158

3159
#ifdef HAVE_JA4
3160
static void CheckJA4Enabled(void)
3161
{
4✔
3162
    const char *strval = NULL;
4✔
3163
    /* Check if we should generate JA4 fingerprints */
3164
    int enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
4✔
3165
    if (SCConfGet("app-layer.protocols.tls.ja4-fingerprints", &strval) != 1) {
4✔
3166
        enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
4✔
3167
    } else if (strcmp(strval, "auto") == 0) {
4✔
3168
        enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
×
UNCOV
3169
    } else if (SCConfValIsFalse(strval)) {
×
UNCOV
3170
        enable_ja4 = 0;
×
UNCOV
3171
        ssl_config.disable_ja4 = true;
×
UNCOV
3172
    } else if (SCConfValIsTrue(strval)) {
×
UNCOV
3173
        enable_ja4 = true;
×
UNCOV
3174
    }
×
3175
    SC_ATOMIC_SET(ssl_config.enable_ja4, enable_ja4);
4✔
3176
    if (!ssl_config.disable_ja4 && !g_disable_hashing) {
4✔
3177
        /* The feature is available, i.e. _could_ be activated by a rule or
3178
            even is enabled in the configuration. */
3179
        ProvidesFeature(FEATURE_JA4);
4✔
3180
    }
4✔
3181
}
4✔
3182
#endif /* HAVE_JA4 */
3183

3184
/**
3185
 * \brief Function to register the SSL protocol parser and other functions
3186
 */
3187
void RegisterSSLParsers(void)
3188
{
4✔
3189
    const char *proto_name = "tls";
4✔
3190

3191
    SC_ATOMIC_INIT(ssl_config.enable_ja3);
4✔
3192

3193
    /** SSLv2  and SSLv23*/
3194
    if (SCAppLayerProtoDetectConfProtoDetectionEnabled("tcp", proto_name)) {
4✔
3195
        AppLayerProtoDetectRegisterProtocol(ALPROTO_TLS, proto_name);
4✔
3196

3197
        if (SSLRegisterPatternsForProtocolDetection() < 0)
4✔
3198
            return;
×
3199

3200
        if (RunmodeIsUnittests()) {
4✔
UNCOV
3201
            SCAppLayerProtoDetectPPRegister(
×
UNCOV
3202
                    IPPROTO_TCP, "443", ALPROTO_TLS, 0, 3, STREAM_TOSERVER, SSLProbingParser, NULL);
×
3203
        } else {
4✔
3204
            if (SCAppLayerProtoDetectPPParseConfPorts("tcp", IPPROTO_TCP, proto_name, ALPROTO_TLS,
4✔
3205
                        0, 3, SSLProbingParser, NULL) == 0) {
4✔
3206
                SCLogConfig("no TLS config found, "
4✔
3207
                            "enabling TLS detection on port 443.");
4✔
3208
                SCAppLayerProtoDetectPPRegister(IPPROTO_TCP, "443", ALPROTO_TLS, 0, 3,
4✔
3209
                        STREAM_TOSERVER, SSLProbingParser, NULL);
4✔
3210
            }
4✔
3211
        }
4✔
3212
    } else {
4✔
3213
        SCLogConfig("Protocol detection and parser disabled for %s protocol",
×
3214
                  proto_name);
×
3215
        return;
×
3216
    }
×
3217

3218
    if (SCAppLayerParserConfParserEnabled("tcp", proto_name)) {
4✔
3219
        AppLayerParserRegisterParser(IPPROTO_TCP, ALPROTO_TLS, STREAM_TOSERVER,
4✔
3220
                                     SSLParseClientRecord);
4✔
3221

3222
        AppLayerParserRegisterParser(IPPROTO_TCP, ALPROTO_TLS, STREAM_TOCLIENT,
4✔
3223
                                     SSLParseServerRecord);
4✔
3224
        AppLayerParserRegisterGetStateFuncs(
4✔
3225
                IPPROTO_TCP, ALPROTO_TLS, SSLStateGetStateIdByName, SSLStateGetStateNameById);
4✔
3226
        AppLayerParserRegisterGetFrameFuncs(
4✔
3227
                IPPROTO_TCP, ALPROTO_TLS, SSLStateGetFrameIdByName, SSLStateGetFrameNameById);
4✔
3228
        AppLayerParserRegisterGetEventInfo(IPPROTO_TCP, ALPROTO_TLS, SSLStateGetEventInfo);
4✔
3229
        AppLayerParserRegisterGetEventInfoById(IPPROTO_TCP, ALPROTO_TLS, SSLStateGetEventInfoById);
4✔
3230

3231
        AppLayerParserRegisterStateFuncs(IPPROTO_TCP, ALPROTO_TLS, SSLStateAlloc, SSLStateFree);
4✔
3232

3233
        SCAppLayerParserRegisterParserAcceptableDataDirection(
4✔
3234
                IPPROTO_TCP, ALPROTO_TLS, STREAM_TOSERVER);
4✔
3235

3236
        AppLayerParserRegisterTxFreeFunc(IPPROTO_TCP, ALPROTO_TLS, SSLStateTransactionFree);
4✔
3237

3238
        AppLayerParserRegisterGetTx(IPPROTO_TCP, ALPROTO_TLS, SSLGetTx);
4✔
3239
        AppLayerParserRegisterTxDataFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetTxData);
4✔
3240
        AppLayerParserRegisterStateDataFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetStateData);
4✔
3241

3242
        AppLayerParserRegisterGetTxCnt(IPPROTO_TCP, ALPROTO_TLS, SSLGetTxCnt);
4✔
3243

3244
        AppLayerParserRegisterGetStateProgressFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetAlstateProgress);
4✔
3245

3246
        AppLayerParserRegisterStateProgressCompletionStatus(
4✔
3247
                ALPROTO_TLS, TLS_STATE_CLIENT_FINISHED, TLS_STATE_SERVER_FINISHED);
4✔
3248

3249
        SCConfNode *enc_handle = SCConfGetNode("app-layer.protocols.tls.encryption-handling");
4✔
3250
        if (enc_handle != NULL && enc_handle->val != NULL) {
4✔
UNCOV
3251
            SCLogDebug("have app-layer.protocols.tls.encryption-handling = %s", enc_handle->val);
×
UNCOV
3252
            if (strcmp(enc_handle->val, "full") == 0) {
×
UNCOV
3253
                ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_FULL;
×
UNCOV
3254
            } else if (strcmp(enc_handle->val, "bypass") == 0) {
×
UNCOV
3255
                ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_BYPASS;
×
UNCOV
3256
            } else if (strcmp(enc_handle->val, "track-only") == 0) {
×
3257
                ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_TRACK_ONLY;
×
3258
            } else if (strcmp(enc_handle->val, "default") == 0) {
×
3259
                SCLogWarning("app-layer.protocols.tls.encryption-handling = default is deprecated "
×
3260
                             "and will be removed in Suricata 9, use \"track-only\" instead, "
×
3261
                             "(see ticket #7642)");
×
3262
                ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_TRACK_ONLY;
×
3263
            } else {
×
3264
                ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_TRACK_ONLY;
×
3265
            }
×
3266
        } else {
4✔
3267
            /* Get the value of no reassembly option from the config file */
3268
            if (SCConfGetNode("app-layer.protocols.tls.no-reassemble") == NULL) {
4✔
3269
                int value = 0;
4✔
3270
                if (SCConfGetBool("tls.no-reassemble", &value) == 1 && value == 1)
4✔
3271
                    ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_BYPASS;
×
3272
            } else {
4✔
3273
                int value = 0;
×
3274
                if (SCConfGetBool("app-layer.protocols.tls.no-reassemble", &value) == 1 &&
×
3275
                        value == 1)
×
3276
                    ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_BYPASS;
×
3277
            }
×
3278
        }
4✔
3279
        SCLogDebug("ssl_config.encrypt_mode %u", ssl_config.encrypt_mode);
4✔
3280

3281
#ifdef HAVE_JA3
4✔
3282
        CheckJA3Enabled();
4✔
3283
#endif /* HAVE_JA3 */
4✔
3284
#ifdef HAVE_JA4
4✔
3285
        CheckJA4Enabled();
4✔
3286
#endif /* HAVE_JA4 */
4✔
3287

3288
        if (g_disable_hashing) {
4✔
3289
            if (SC_ATOMIC_GET(ssl_config.enable_ja3)) {
×
3290
                SCLogWarning("MD5 calculation has been disabled, disabling JA3");
×
3291
                SC_ATOMIC_SET(ssl_config.enable_ja3, 0);
×
3292
            }
×
3293
            if (SC_ATOMIC_GET(ssl_config.enable_ja4)) {
×
3294
                SCLogWarning("Hashing has been disabled, disabling JA4");
×
3295
                SC_ATOMIC_SET(ssl_config.enable_ja4, 0);
×
3296
            }
×
3297
        } else {
4✔
3298
            if (RunmodeIsUnittests()) {
4✔
UNCOV
3299
#ifdef HAVE_JA3
×
UNCOV
3300
                SC_ATOMIC_SET(ssl_config.enable_ja3, 1);
×
UNCOV
3301
#endif /* HAVE_JA3 */
×
UNCOV
3302
#ifdef HAVE_JA4
×
UNCOV
3303
                SC_ATOMIC_SET(ssl_config.enable_ja4, 1);
×
UNCOV
3304
#endif /* HAVE_JA4 */
×
UNCOV
3305
            }
×
3306
        }
4✔
3307
    } else {
4✔
3308
        SCLogConfig("Parser disabled for %s protocol. Protocol detection still on.", proto_name);
×
3309
    }
×
3310
}
4✔
3311

3312
/**
3313
 * \brief if not explicitly disabled in config, enable ja3 support
3314
 *
3315
 * Implemented using atomic to allow rule reloads to do this at
3316
 * runtime.
3317
 */
3318
void SSLEnableJA3(void)
3319
{
7,369✔
3320
    if (g_disable_hashing || ssl_config.disable_ja3) {
7,369✔
3321
        return;
×
3322
    }
×
3323
    if (SC_ATOMIC_GET(ssl_config.enable_ja3)) {
7,369✔
3324
        return;
7,367✔
3325
    }
7,367✔
3326
    SC_ATOMIC_SET(ssl_config.enable_ja3, 1);
2✔
3327
}
2✔
3328

3329
/**
3330
 * \brief if not explicitly disabled in config, enable ja4 support
3331
 *
3332
 * Implemented using atomic to allow rule reloads to do this at
3333
 * runtime.
3334
 */
3335
void SSLEnableJA4(void)
3336
{
1,614✔
3337
    // only caller has #ifdef HAVE_JA4
3338
    if (g_disable_hashing || ssl_config.disable_ja4) {
1,614✔
UNCOV
3339
        return;
×
UNCOV
3340
    }
×
3341
    if (SC_ATOMIC_GET(ssl_config.enable_ja4)) {
1,614✔
3342
        return;
1,612✔
3343
    }
1,612✔
3344
    SC_ATOMIC_SET(ssl_config.enable_ja4, 1);
2✔
3345
}
2✔
3346

3347
/**
3348
 * \brief return whether ja3 is effectively enabled
3349
 *
3350
 * This means that it either has been enabled explicitly or has been
3351
 * enabled by having loaded a rule while not being explicitly disabled.
3352
 *
3353
 * \retval true if enabled, false otherwise
3354
 */
3355
bool SSLJA3IsEnabled(void)
3356
{
7,369✔
3357
    return SC_ATOMIC_GET(ssl_config.enable_ja3);
7,369✔
3358
}
7,369✔
3359

3360
/**
3361
 * \brief return whether ja4 is effectively enabled
3362
 *
3363
 * This means that it either has been enabled explicitly or has been
3364
 * enabled by having loaded a rule while not being explicitly disabled.
3365
 *
3366
 * \retval true if enabled, false otherwise
3367
 */
3368
bool SSLJA4IsEnabled(void)
3369
{
1,614✔
3370
    return SC_ATOMIC_GET(ssl_config.enable_ja4);
3371
}
1,614✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc