Coverage Report

Created: 2026-08-14 07:34

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata7/src/app-layer-ssl.c
Line
Count
Source
1
/* Copyright (C) 2007-2022 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
#include "threads.h"
30
31
#include "stream-tcp-private.h"
32
#include "stream-tcp-reassemble.h"
33
#include "stream-tcp.h"
34
#include "stream.h"
35
36
#include "app-layer.h"
37
#include "app-layer-detect-proto.h"
38
#include "app-layer-protos.h"
39
#include "app-layer-parser.h"
40
#include "app-layer-frames.h"
41
#include "app-layer-ssl.h"
42
43
#include "decode-events.h"
44
#include "conf.h"
45
46
#include "feature.h"
47
48
#include "util-spm.h"
49
#include "util-unittest.h"
50
#include "util-debug.h"
51
#include "util-print.h"
52
#include "util-pool.h"
53
#include "util-byte.h"
54
#include "util-ja3.h"
55
#include "util-enum.h"
56
#include "flow-util.h"
57
#include "flow-private.h"
58
#include "util-validate.h"
59
60
SCEnumCharMap tls_frame_table[] = {
61
    {
62
            "pdu",
63
            TLS_FRAME_PDU,
64
    },
65
    {
66
            "hdr",
67
            TLS_FRAME_HDR,
68
    },
69
    {
70
            "data",
71
            TLS_FRAME_DATA,
72
    },
73
    {
74
            "alert",
75
            TLS_FRAME_ALERT_DATA,
76
    },
77
    {
78
            "heartbeat",
79
            TLS_FRAME_HB_DATA,
80
    },
81
    {
82
            "ssl2.hdr",
83
            TLS_FRAME_SSLV2_HDR,
84
    },
85
    {
86
            "ssl2.pdu",
87
            TLS_FRAME_SSLV2_PDU,
88
    },
89
    { NULL, -1 },
90
};
91
92
SCEnumCharMap tls_decoder_event_table[] = {
93
    /* TLS protocol messages */
94
    { "INVALID_SSLV2_HEADER", TLS_DECODER_EVENT_INVALID_SSLV2_HEADER },
95
    { "INVALID_TLS_HEADER", TLS_DECODER_EVENT_INVALID_TLS_HEADER },
96
    { "INVALID_RECORD_VERSION", TLS_DECODER_EVENT_INVALID_RECORD_VERSION },
97
    { "INVALID_RECORD_TYPE", TLS_DECODER_EVENT_INVALID_RECORD_TYPE },
98
    { "INVALID_RECORD_LENGTH", TLS_DECODER_EVENT_INVALID_RECORD_LENGTH },
99
    { "INVALID_HANDSHAKE_MESSAGE", TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE },
100
    { "HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_HEARTBEAT },
101
    { "INVALID_HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_INVALID_HEARTBEAT },
102
    { "OVERFLOW_HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_OVERFLOW_HEARTBEAT },
103
    { "DATALEAK_HEARTBEAT_MISMATCH", TLS_DECODER_EVENT_DATALEAK_HEARTBEAT_MISMATCH },
104
    { "HANDSHAKE_INVALID_LENGTH", TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH },
105
    { "MULTIPLE_SNI_EXTENSIONS", TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS },
106
    { "INVALID_SNI_TYPE", TLS_DECODER_EVENT_INVALID_SNI_TYPE },
107
    { "INVALID_SNI_LENGTH", TLS_DECODER_EVENT_INVALID_SNI_LENGTH },
108
    { "TOO_MANY_RECORDS_IN_PACKET", TLS_DECODER_EVENT_TOO_MANY_RECORDS_IN_PACKET },
109
    /* certificate decoding messages */
110
    { "INVALID_CERTIFICATE", TLS_DECODER_EVENT_INVALID_CERTIFICATE },
111
    { "CERTIFICATE_INVALID_LENGTH", TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH },
112
    { "CERTIFICATE_INVALID_VERSION", TLS_DECODER_EVENT_CERTIFICATE_INVALID_VERSION },
113
    { "CERTIFICATE_INVALID_SERIAL", TLS_DECODER_EVENT_CERTIFICATE_INVALID_SERIAL },
114
    { "CERTIFICATE_INVALID_ALGORITHMIDENTIFIER",
115
            TLS_DECODER_EVENT_CERTIFICATE_INVALID_ALGORITHMIDENTIFIER },
116
    { "CERTIFICATE_INVALID_X509NAME", TLS_DECODER_EVENT_CERTIFICATE_INVALID_X509NAME },
117
    { "CERTIFICATE_INVALID_DATE", TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE },
118
    { "CERTIFICATE_INVALID_EXTENSIONS", TLS_DECODER_EVENT_CERTIFICATE_INVALID_EXTENSIONS },
119
    { "CERTIFICATE_INVALID_DER", TLS_DECODER_EVENT_CERTIFICATE_INVALID_DER },
120
    { "CERTIFICATE_INVALID_SUBJECT", TLS_DECODER_EVENT_CERTIFICATE_INVALID_SUBJECT },
121
    { "CERTIFICATE_INVALID_ISSUER", TLS_DECODER_EVENT_CERTIFICATE_INVALID_ISSUER },
122
    { "CERTIFICATE_INVALID_VALIDITY", TLS_DECODER_EVENT_CERTIFICATE_INVALID_VALIDITY },
123
    { "ERROR_MESSAGE_ENCOUNTERED", TLS_DECODER_EVENT_ERROR_MSG_ENCOUNTERED },
124
    /* used as a generic error event */
125
    { "INVALID_SSL_RECORD", TLS_DECODER_EVENT_INVALID_SSL_RECORD },
126
    { NULL, -1 },
127
};
128
129
enum {
130
    /* X.509 error codes, returned by decoder
131
     * THESE CONSTANTS MUST MATCH rust/src/x509/mod.rs ! */
132
    ERR_INVALID_CERTIFICATE=1,
133
    ERR_INVALID_LENGTH,
134
    ERR_INVALID_VERSION,
135
    ERR_INVALID_SERIAL,
136
    ERR_INVALID_ALGORITHMIDENTIFIER,
137
    ERR_INVALID_X509NAME,
138
    ERR_INVALID_DATE,
139
    ERR_INVALID_EXTENSIONS,
140
    ERR_INVALID_DER,
141
142
    /* error getting data */
143
    ERR_EXTRACT_SUBJECT,
144
    ERR_EXTRACT_ISSUER,
145
    ERR_EXTRACT_VALIDITY,
146
};
147
148
/* JA3 and JA4 fingerprints are disabled by default */
149
150
#define SSL_CONFIG_DEFAULT_JA3 0
150
150
#define SSL_CONFIG_DEFAULT_JA4 0
151
152
enum SslConfigEncryptHandling {
153
    SSL_CNF_ENC_HANDLE_DEFAULT = 0, /**< disable raw content, continue tracking */
154
    SSL_CNF_ENC_HANDLE_BYPASS = 1,  /**< skip processing of flow, bypass if possible */
155
    SSL_CNF_ENC_HANDLE_FULL = 2,    /**< handle fully like any other proto */
156
};
157
158
typedef struct SslConfig_ {
159
    enum SslConfigEncryptHandling encrypt_mode;
160
    /** dynamic setting for ja3 and ja4: can be enabled on demand if not
161
     *  explicitly disabled. */
162
    SC_ATOMIC_DECLARE(int, enable_ja3);
163
    bool disable_ja3; /**< ja3 explicitly disabled. Don't enable on demand. */
164
    SC_ATOMIC_DECLARE(int, enable_ja4);
165
    bool disable_ja4; /**< ja4 explicitly disabled. Don't enable on demand. */
166
} SslConfig;
167
168
SslConfig ssl_config;
169
170
/* SSLv3 record types */
171
83.7k
#define SSLV3_CHANGE_CIPHER_SPEC       20
172
52.2k
#define SSLV3_ALERT_PROTOCOL           21
173
250k
#define SSLV3_HANDSHAKE_PROTOCOL       22
174
256k
#define SSLV3_APPLICATION_PROTOCOL     23
175
181k
#define SSLV3_HEARTBEAT_PROTOCOL       24
176
177
/* SSLv3 handshake protocol types */
178
181k
#define SSLV3_HS_HELLO_REQUEST          0
179
76.8k
#define SSLV3_HS_CLIENT_HELLO           1
180
133k
#define SSLV3_HS_SERVER_HELLO           2
181
177k
#define SSLV3_HS_NEW_SESSION_TICKET     4
182
139k
#define SSLV3_HS_CERTIFICATE           11
183
114k
#define SSLV3_HS_SERVER_KEY_EXCHANGE   12
184
171k
#define SSLV3_HS_CERTIFICATE_REQUEST   13
185
183k
#define SSLV3_HS_SERVER_HELLO_DONE     14
186
171k
#define SSLV3_HS_CERTIFICATE_VERIFY    15
187
119k
#define SSLV3_HS_CLIENT_KEY_EXCHANGE   16
188
172k
#define SSLV3_HS_FINISHED              20
189
173k
#define SSLV3_HS_CERTIFICATE_URL       21
190
177k
#define SSLV3_HS_CERTIFICATE_STATUS    22
191
192
/* SSLv2 protocol message types */
193
7.32k
#define SSLV2_MT_ERROR                  0
194
147k
#define SSLV2_MT_CLIENT_HELLO           1
195
2.33k
#define SSLV2_MT_CLIENT_MASTER_KEY      2
196
18.8k
#define SSLV2_MT_CLIENT_FINISHED        3
197
2.66k
#define SSLV2_MT_SERVER_HELLO           4
198
8.69k
#define SSLV2_MT_SERVER_VERIFY          5
199
15.2k
#define SSLV2_MT_SERVER_FINISHED        6
200
20.4k
#define SSLV2_MT_REQUEST_CERTIFICATE    7
201
15.0k
#define SSLV2_MT_CLIENT_CERTIFICATE     8
202
203
4.57M
#define SSLV3_RECORD_HDR_LEN            5
204
#define SSLV3_MESSAGE_HDR_LEN           4
205
/** max length according to RFC 5246 6.2.2 is 2^14 + 1024 */
206
174k
#define SSLV3_RECORD_MAX_LEN ((1 << 14) + 1024)
207
208
51.3k
#define SSLV3_CLIENT_HELLO_VERSION_LEN  2
209
98.9k
#define SSLV3_CLIENT_HELLO_RANDOM_LEN  32
210
211
/* TLS heartbeat protocol types */
212
7.31k
#define TLS_HB_REQUEST                  1
213
1.17k
#define TLS_HB_RESPONSE                 2
214
215
#define SSL_RECORD_MINIMUM_LENGTH       6
216
217
4.87k
#define SHA1_STRING_LENGTH             60
218
219
1.76M
#define HAS_SPACE(n) ((uint64_t)(input - initial_input) + (uint64_t)(n) <= (uint64_t)(input_len))
220
221
struct SSLDecoderResult {
222
    int retval;      // nr bytes consumed from input, or < 0 on error
223
    uint32_t needed; // more bytes needed
224
};
225
#define SSL_DECODER_ERROR(e)                                                                       \
226
2.27k
    (struct SSLDecoderResult)                                                                      \
227
2.27k
    {                                                                                              \
228
2.27k
        (e), 0                                                                                     \
229
2.27k
    }
230
#define SSL_DECODER_OK(c)                                                                          \
231
825k
    (struct SSLDecoderResult)                                                                      \
232
825k
    {                                                                                              \
233
825k
        (c), 0                                                                                     \
234
825k
    }
235
#define SSL_DECODER_INCOMPLETE(c, n)                                                               \
236
94.5k
    (struct SSLDecoderResult)                                                                      \
237
94.5k
    {                                                                                              \
238
94.5k
        (c), (n)                                                                                   \
239
94.5k
    }
240
241
static inline int SafeMemcpy(void *dst, size_t dst_offset, size_t dst_size,
242
        const void *src, size_t src_offset, size_t src_size, size_t src_tocopy) WARN_UNUSED;
243
244
static inline int SafeMemcpy(void *dst, size_t dst_offset, size_t dst_size,
245
        const void *src, size_t src_offset, size_t src_size, size_t src_tocopy)
246
112k
{
247
112k
    DEBUG_VALIDATE_BUG_ON(dst_offset >= dst_size);
248
112k
    DEBUG_VALIDATE_BUG_ON(src_offset >= src_size);
249
112k
    DEBUG_VALIDATE_BUG_ON(src_tocopy > (src_size - src_offset));
250
112k
    DEBUG_VALIDATE_BUG_ON(src_tocopy > (dst_size - dst_offset));
251
252
112k
    if (dst_offset < dst_size && src_offset < src_size &&
253
112k
        src_tocopy <= (src_size - src_offset) &&
254
112k
        src_tocopy <= (dst_size - dst_offset)) {
255
112k
        memcpy(dst + dst_offset, src + src_offset, src_tocopy);
256
112k
        return 0;
257
112k
    }
258
0
    return -1;
259
112k
}
260
261
#ifdef DEBUG_VALIDATION
262
#define ValidateRecordState(connp)                                              \
263
331k
    do {                                                                        \
264
331k
        DEBUG_VALIDATE_BUG_ON(((connp)->record_length + SSLV3_RECORD_HDR_LEN) < \
265
331k
                (connp)->bytes_processed);                                      \
266
331k
    } while(0);
267
#else
268
#define ValidateRecordState(...)
269
#endif
270
271
#define SSLParserHSReset(connp)                                                                    \
272
704k
    do {                                                                                           \
273
704k
        (connp)->handshake_type = 0;                                                               \
274
704k
        (connp)->message_length = 0;                                                               \
275
704k
    } while (0)
276
277
#define SSLParserReset(state)                       \
278
381k
    do {                                            \
279
381k
        SCLogDebug("resetting state");              \
280
381k
        (state)->curr_connp->bytes_processed = 0;   \
281
381k
        SSLParserHSReset((state)->curr_connp);      \
282
381k
    } while(0)
283
284
#define SSLSetEvent(ssl_state, event)                                                              \
285
341k
    do {                                                                                           \
286
341k
        SCLogDebug("setting event %u", (event));                                                   \
287
341k
        if ((ssl_state) == NULL) {                                                                 \
288
0
            SCLogDebug("could not set decoder event %u", event);                                   \
289
341k
        } else {                                                                                   \
290
341k
            AppLayerDecoderEventsSetEventRaw(&(ssl_state)->tx_data.events, (event));               \
291
341k
            (ssl_state)->events++;                                                                 \
292
341k
        }                                                                                          \
293
341k
    } while (0)
294
295
static void *SSLGetTx(void *state, uint64_t tx_id)
296
2.70M
{
297
2.70M
    SSLState *ssl_state = (SSLState *)state;
298
2.70M
    return ssl_state;
299
2.70M
}
300
301
static uint64_t SSLGetTxCnt(void *state)
302
4.56M
{
303
    /* single tx */
304
4.56M
    return 1;
305
4.56M
}
306
307
static int SSLGetAlstateProgress(void *tx, uint8_t direction)
308
2.33M
{
309
2.33M
    SSLState *ssl_state = (SSLState *)tx;
310
311
    /* we don't care about direction, only that app-layer parser is done
312
       and have sent an EOF */
313
2.33M
    if (ssl_state->flags & SSL_AL_FLAG_STATE_FINISHED) {
314
3.94k
        return TLS_STATE_FINISHED;
315
3.94k
    }
316
317
    /* we want the logger to log when the handshake is done, even if the
318
       state is not finished */
319
2.32M
    if (ssl_state->flags & SSL_AL_FLAG_HANDSHAKE_DONE) {
320
203k
        return TLS_HANDSHAKE_DONE;
321
203k
    }
322
323
2.12M
    if (direction == STREAM_TOSERVER &&
324
828k
        (ssl_state->server_connp.cert0_subject != NULL ||
325
824k
         ssl_state->server_connp.cert0_issuerdn != NULL))
326
3.23k
    {
327
3.23k
        return TLS_STATE_CERT_READY;
328
3.23k
    }
329
330
2.12M
    return TLS_STATE_IN_PROGRESS;
331
2.12M
}
332
333
static AppLayerTxData *SSLGetTxData(void *vtx)
334
3.97M
{
335
3.97M
    SSLState *ssl_state = (SSLState *)vtx;
336
3.97M
    return &ssl_state->tx_data;
337
3.97M
}
338
339
static AppLayerStateData *SSLGetStateData(void *vstate)
340
21.7k
{
341
21.7k
    SSLState *ssl_state = (SSLState *)vstate;
342
21.7k
    return &ssl_state->state_data;
343
21.7k
}
344
345
void SSLVersionToString(uint16_t version, char *buffer)
346
13.2k
{
347
13.2k
    buffer[0] = '\0';
348
349
13.2k
    switch (version) {
350
3.15k
        case TLS_VERSION_UNKNOWN:
351
3.15k
            strlcat(buffer, "UNDETERMINED", 13);
352
3.15k
            break;
353
671
        case SSL_VERSION_2:
354
671
            strlcat(buffer, "SSLv2", 6);
355
671
            break;
356
25
        case SSL_VERSION_3:
357
25
            strlcat(buffer, "SSLv3", 6);
358
25
            break;
359
2.08k
        case TLS_VERSION_10:
360
2.08k
            strlcat(buffer, "TLSv1", 6);
361
2.08k
            break;
362
25
        case TLS_VERSION_11:
363
25
            strlcat(buffer, "TLS 1.1", 8);
364
25
            break;
365
5.79k
        case TLS_VERSION_12:
366
5.79k
            strlcat(buffer, "TLS 1.2", 8);
367
5.79k
            break;
368
69
        case TLS_VERSION_13:
369
69
            strlcat(buffer, "TLS 1.3", 8);
370
69
            break;
371
0
        case TLS_VERSION_13_DRAFT28:
372
0
            strlcat(buffer, "TLS 1.3 draft-28", 17);
373
0
            break;
374
2
        case TLS_VERSION_13_DRAFT27:
375
2
            strlcat(buffer, "TLS 1.3 draft-27", 17);
376
2
            break;
377
0
        case TLS_VERSION_13_DRAFT26:
378
0
            strlcat(buffer, "TLS 1.3 draft-26", 17);
379
0
            break;
380
0
        case TLS_VERSION_13_DRAFT25:
381
0
            strlcat(buffer, "TLS 1.3 draft-25", 17);
382
0
            break;
383
0
        case TLS_VERSION_13_DRAFT24:
384
0
            strlcat(buffer, "TLS 1.3 draft-24", 17);
385
0
            break;
386
86
        case TLS_VERSION_13_DRAFT23:
387
86
            strlcat(buffer, "TLS 1.3 draft-23", 17);
388
86
            break;
389
0
        case TLS_VERSION_13_DRAFT22:
390
0
            strlcat(buffer, "TLS 1.3 draft-22", 17);
391
0
            break;
392
0
        case TLS_VERSION_13_DRAFT21:
393
0
            strlcat(buffer, "TLS 1.3 draft-21", 17);
394
0
            break;
395
0
        case TLS_VERSION_13_DRAFT20:
396
0
            strlcat(buffer, "TLS 1.3 draft-20", 17);
397
0
            break;
398
0
        case TLS_VERSION_13_DRAFT19:
399
0
            strlcat(buffer, "TLS 1.3 draft-19", 17);
400
0
            break;
401
0
        case TLS_VERSION_13_DRAFT18:
402
0
            strlcat(buffer, "TLS 1.3 draft-18", 17);
403
0
            break;
404
0
        case TLS_VERSION_13_DRAFT17:
405
0
            strlcat(buffer, "TLS 1.3 draft-17", 17);
406
0
            break;
407
0
        case TLS_VERSION_13_DRAFT16:
408
0
            strlcat(buffer, "TLS 1.3 draft-16", 17);
409
0
            break;
410
0
        case TLS_VERSION_13_PRE_DRAFT16:
411
0
            strlcat(buffer, "TLS 1.3 draft-<16", 18);
412
0
            break;
413
0
        case TLS_VERSION_13_DRAFT20_FB:
414
0
            strlcat(buffer, "TLS 1.3 draft-20-fb", 20);
415
0
            break;
416
0
        case TLS_VERSION_13_DRAFT21_FB:
417
0
            strlcat(buffer, "TLS 1.3 draft-21-fb", 20);
418
0
            break;
419
0
        case TLS_VERSION_13_DRAFT22_FB:
420
0
            strlcat(buffer, "TLS 1.3 draft-22-fb", 20);
421
0
            break;
422
0
        case TLS_VERSION_13_DRAFT23_FB:
423
0
            strlcat(buffer, "TLS 1.3 draft-23-fb", 20);
424
0
            break;
425
0
        case TLS_VERSION_13_DRAFT26_FB:
426
0
            strlcat(buffer, "TLS 1.3 draft-26-fb", 20);
427
0
            break;
428
1.31k
        default:
429
1.31k
            snprintf(buffer, 7, "0x%04x", version);
430
1.31k
            break;
431
13.2k
    }
432
13.2k
}
433
434
static void TlsDecodeHSCertificateErrSetEvent(SSLState *ssl_state, uint32_t err)
435
116k
{
436
116k
    switch(err) {
437
0
        case ERR_EXTRACT_VALIDITY:
438
0
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_VALIDITY);
439
0
            break;
440
23
        case ERR_EXTRACT_ISSUER:
441
23
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_ISSUER);
442
23
            break;
443
6
        case ERR_EXTRACT_SUBJECT:
444
6
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_SUBJECT);
445
6
            break;
446
16.7k
        case ERR_INVALID_DER:
447
16.7k
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_DER);
448
16.7k
            break;
449
67
        case ERR_INVALID_EXTENSIONS:
450
67
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_EXTENSIONS);
451
67
            break;
452
7.20k
        case ERR_INVALID_DATE:
453
7.20k
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE);
454
7.20k
            break;
455
0
        case ERR_INVALID_X509NAME:
456
0
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_X509NAME);
457
0
            break;
458
287
        case ERR_INVALID_ALGORITHMIDENTIFIER:
459
287
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_ALGORITHMIDENTIFIER);
460
287
            break;
461
4.24k
        case ERR_INVALID_SERIAL:
462
4.24k
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_SERIAL);
463
4.24k
            break;
464
3.47k
        case ERR_INVALID_VERSION:
465
3.47k
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_VERSION);
466
3.47k
            break;
467
77.3k
        case ERR_INVALID_LENGTH:
468
77.3k
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH);
469
77.3k
            break;
470
6.89k
        case ERR_INVALID_CERTIFICATE:
471
6.89k
        default:
472
6.89k
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_CERTIFICATE);
473
6.89k
            break;
474
116k
    }
475
116k
}
476
477
static inline int TlsDecodeHSCertificateFingerprint(
478
        SSLStateConnp *connp, const uint8_t *input, uint32_t cert_len)
479
4.87k
{
480
4.87k
    if (unlikely(connp->cert0_fingerprint != NULL))
481
0
        return 0;
482
483
4.87k
    connp->cert0_fingerprint = SCCalloc(1, SHA1_STRING_LENGTH);
484
4.87k
    if (connp->cert0_fingerprint == NULL)
485
0
        return -1;
486
487
4.87k
    uint8_t hash[SC_SHA1_LEN];
488
4.87k
    if (SCSha1HashBuffer(input, cert_len, hash, sizeof(hash)) == 1) {
489
4.87k
        rs_to_hex_sep(
490
4.87k
                (uint8_t *)connp->cert0_fingerprint, SHA1_STRING_LENGTH, ':', hash, SC_SHA1_LEN);
491
4.87k
    }
492
4.87k
    return 0;
493
4.87k
}
494
495
static inline int TlsDecodeHSCertificateAddCertToChain(
496
        SSLStateConnp *connp, const uint8_t *input, uint32_t cert_len)
497
11.0k
{
498
11.0k
    SSLCertsChain *cert = SCCalloc(1, sizeof(SSLCertsChain));
499
11.0k
    if (cert == NULL)
500
0
        return -1;
501
502
11.0k
    cert->cert_data = (uint8_t *)input;
503
11.0k
    cert->cert_len = cert_len;
504
11.0k
    TAILQ_INSERT_TAIL(&connp->certs, cert, next);
505
506
11.0k
    return 0;
507
11.0k
}
508
509
static int TlsDecodeHSCertificate(SSLState *ssl_state, SSLStateConnp *connp,
510
        const uint8_t *const initial_input, const uint32_t input_len, const int certn)
511
58.7k
{
512
58.7k
    const uint8_t *input = (uint8_t *)initial_input;
513
58.7k
    uint32_t err_code = 0;
514
58.7k
    X509 *x509 = NULL;
515
58.7k
    int rc = 0;
516
517
58.7k
    if (!(HAS_SPACE(3)))
518
526
        goto invalid_cert;
519
520
58.2k
    uint32_t cert_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
521
58.2k
    input += 3;
522
523
58.2k
    if (!(HAS_SPACE(cert_len)))
524
1.05k
        goto invalid_cert;
525
526
    /* only store fields from the first certificate in the chain */
527
57.1k
    if (certn == 0 && connp->cert0_subject == NULL && connp->cert0_issuerdn == NULL &&
528
53.6k
            connp->cert0_serial == NULL) {
529
53.6k
        x509 = rs_x509_decode(input, cert_len, &err_code);
530
53.6k
        if (x509 == NULL) {
531
51.3k
            TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
532
51.3k
            goto next;
533
51.3k
        }
534
535
2.31k
        char *str = rs_x509_get_subject(x509);
536
2.31k
        if (str == NULL) {
537
6
            err_code = ERR_EXTRACT_SUBJECT;
538
6
            goto error;
539
6
        }
540
2.31k
        connp->cert0_subject = str;
541
542
2.31k
        str = rs_x509_get_issuer(x509);
543
2.31k
        if (str == NULL) {
544
23
            err_code = ERR_EXTRACT_ISSUER;
545
23
            goto error;
546
23
        }
547
2.28k
        connp->cert0_issuerdn = str;
548
549
2.28k
        str = rs_x509_get_serial(x509);
550
2.28k
        if (str == NULL) {
551
0
            err_code = ERR_INVALID_SERIAL;
552
0
            goto error;
553
0
        }
554
2.28k
        connp->cert0_serial = str;
555
556
2.28k
        rc = rs_x509_get_validity(x509, &connp->cert0_not_before, &connp->cert0_not_after);
557
2.28k
        if (rc != 0) {
558
0
            err_code = ERR_EXTRACT_VALIDITY;
559
0
            goto error;
560
0
        }
561
562
2.28k
        rs_x509_free(x509);
563
2.28k
        x509 = NULL;
564
565
2.28k
        rc = TlsDecodeHSCertificateFingerprint(connp, input, cert_len);
566
2.28k
        if (rc != 0) {
567
0
            SCLogDebug("TlsDecodeHSCertificateFingerprint failed with %d", rc);
568
0
            goto error;
569
0
        }
570
2.28k
    }
571
572
5.82k
    rc = TlsDecodeHSCertificateAddCertToChain(connp, input, cert_len);
573
5.82k
    if (rc != 0) {
574
0
        SCLogDebug("TlsDecodeHSCertificateAddCertToChain failed with %d", rc);
575
0
        goto error;
576
0
    }
577
578
57.1k
next:
579
57.1k
    input += cert_len;
580
57.1k
    return (input - initial_input);
581
582
29
error:
583
29
    if (err_code != 0)
584
29
        TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
585
29
    if (x509 != NULL)
586
29
        rs_x509_free(x509);
587
29
    return -1;
588
589
1.57k
invalid_cert:
590
1.57k
    SCLogDebug("TLS invalid certificate");
591
1.57k
    SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_CERTIFICATE);
592
1.57k
    return -1;
593
5.82k
}
594
595
/** \internal
596
 * \brief parse cert data in a certificate handshake message
597
 *        will be called with all data.
598
 * \retval consumed bytes consumed or -1 on error
599
 */
600
static int TlsDecodeHSCertificates(SSLState *ssl_state, SSLStateConnp *connp,
601
        const uint8_t *const initial_input, const uint32_t input_len)
602
15.7k
{
603
15.7k
    const uint8_t *input = (uint8_t *)initial_input;
604
605
15.7k
    if (!(HAS_SPACE(3)))
606
2.65k
        return -1;
607
608
13.1k
    const uint32_t cert_chain_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
609
13.1k
    input += 3;
610
611
13.1k
    if (!(HAS_SPACE(cert_chain_len)))
612
939
        return -1;
613
614
12.1k
    if (connp->certs_buffer != NULL) {
615
        // TODO should we set an event here?
616
751
        return -1;
617
751
    }
618
619
11.4k
    connp->certs_buffer = SCCalloc(1, cert_chain_len);
620
11.4k
    if (connp->certs_buffer == NULL) {
621
0
        return -1;
622
0
    }
623
11.4k
    connp->certs_buffer_size = cert_chain_len;
624
11.4k
    memcpy(connp->certs_buffer, input, cert_chain_len);
625
626
11.4k
    int cert_cnt = 0;
627
11.4k
    uint32_t processed_len = 0;
628
    /* coverity[tainted_data] */
629
138k
    while (processed_len < cert_chain_len) {
630
130k
        int rc = TlsDecodeHSCertificate(ssl_state, connp, connp->certs_buffer + processed_len,
631
130k
                connp->certs_buffer_size - processed_len, cert_cnt);
632
130k
        if (rc <= 0) { // 0 should be impossible, but lets be defensive
633
3.37k
            return -1;
634
3.37k
        }
635
127k
        DEBUG_VALIDATE_BUG_ON(processed_len + (uint32_t)rc > cert_chain_len);
636
127k
        if (processed_len + (uint32_t)rc > cert_chain_len) {
637
0
            return -1;
638
0
        }
639
640
127k
        processed_len += (uint32_t)rc;
641
127k
    }
642
643
8.04k
    return processed_len + 3;
644
11.4k
}
645
646
/**
647
 * \inline
648
 * \brief Check if value is GREASE.
649
 *
650
 * http://tools.ietf.org/html/draft-davidben-tls-grease-00
651
 *
652
 * \param value Value to check.
653
 *
654
 * \retval 1 if is GREASE.
655
 * \retval 0 if not is GREASE.
656
 */
657
static inline int TLSDecodeValueIsGREASE(const uint16_t value)
658
2.01M
{
659
2.01M
    switch (value)
660
2.01M
    {
661
1.40k
        case 0x0a0a:
662
2.64k
        case 0x1a1a:
663
3.46k
        case 0x2a2a:
664
6.34k
        case 0x3a3a:
665
7.22k
        case 0x4a4a:
666
8.16k
        case 0x5a5a:
667
9.14k
        case 0x6a6a:
668
10.1k
        case 0x7a7a:
669
11.2k
        case 0x8a8a:
670
11.8k
        case 0x9a9a:
671
12.8k
        case 0xaaaa:
672
14.1k
        case 0xbaba:
673
15.0k
        case 0xcaca:
674
16.0k
        case 0xdada:
675
17.2k
        case 0xeaea:
676
18.5k
        case 0xfafa:
677
18.5k
            return 1;
678
2.00M
        default:
679
2.00M
            return 0;
680
2.01M
    }
681
2.01M
}
682
683
static inline int TLSDecodeHSHelloVersion(SSLState *ssl_state,
684
                                          const uint8_t * const initial_input,
685
                                          const uint32_t input_len)
686
52.2k
{
687
52.2k
    uint8_t *input = (uint8_t *)initial_input;
688
689
52.2k
    if (!(HAS_SPACE(SSLV3_CLIENT_HELLO_VERSION_LEN))) {
690
862
        SCLogDebug("TLS handshake invalid length");
691
862
        SSLSetEvent(ssl_state,
692
862
                    TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
693
862
        return -1;
694
862
    }
695
696
51.3k
    uint16_t version = (uint16_t)(*input << 8) | *(input + 1);
697
51.3k
    ssl_state->curr_connp->version = version;
698
699
51.3k
    if (ssl_state->curr_connp->ja4 != NULL &&
700
8.46k
            ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
701
7.95k
        SCJA4SetTLSVersion(ssl_state->curr_connp->ja4, version);
702
7.95k
    }
703
704
    /* TLSv1.3 draft1 to draft21 use the version field as earlier TLS
705
       versions, instead of using the supported versions extension. */
706
51.3k
    if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
707
26.0k
            ((ssl_state->curr_connp->version == TLS_VERSION_13) ||
708
25.6k
            (((ssl_state->curr_connp->version >> 8) & 0xff) == 0x7f))) {
709
1.89k
        ssl_state->flags |= SSL_AL_FLAG_LOG_WITHOUT_CERT;
710
1.89k
    }
711
712
    /* Catch some early TLSv1.3 draft implementations that does not conform
713
       to the draft version. */
714
51.3k
    if ((ssl_state->curr_connp->version >= 0x7f01) &&
715
4.13k
            (ssl_state->curr_connp->version < 0x7f10)) {
716
1.21k
        ssl_state->curr_connp->version = TLS_VERSION_13_PRE_DRAFT16;
717
1.21k
    }
718
719
    /* TLSv1.3 drafts from draft1 to draft15 use 0x0304 (TLSv1.3) as the
720
       version number, which makes it hard to accurately pinpoint the
721
       exact draft version. */
722
50.1k
    else if (ssl_state->curr_connp->version == TLS_VERSION_13) {
723
344
        ssl_state->curr_connp->version = TLS_VERSION_13_PRE_DRAFT16;
724
344
    }
725
726
51.3k
    if (SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_str == NULL) {
727
13.2k
        ssl_state->curr_connp->ja3_str = Ja3BufferInit();
728
13.2k
        if (ssl_state->curr_connp->ja3_str == NULL)
729
0
            return -1;
730
731
13.2k
        int rc = Ja3BufferAddValue(&ssl_state->curr_connp->ja3_str, version);
732
13.2k
        if (rc != 0)
733
0
            return -1;
734
13.2k
    }
735
736
51.3k
    input += SSLV3_CLIENT_HELLO_VERSION_LEN;
737
738
51.3k
    return (input - initial_input);
739
51.3k
}
740
741
static inline int TLSDecodeHSHelloRandom(SSLState *ssl_state,
742
                                         const uint8_t * const initial_input,
743
                                         const uint32_t input_len)
744
104k
{
745
104k
    uint8_t *input = (uint8_t *)initial_input;
746
747
104k
    if (!(HAS_SPACE(SSLV3_CLIENT_HELLO_RANDOM_LEN))) {
748
5.21k
        SCLogDebug("TLS handshake invalid length");
749
5.21k
        SSLSetEvent(ssl_state,
750
5.21k
                    TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
751
5.21k
        return -1;
752
5.21k
    }
753
754
98.9k
    if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
755
51.5k
        memcpy(ssl_state->server_connp.random, input, TLS_RANDOM_LEN);
756
51.5k
        ssl_state->flags |= TLS_TS_RANDOM_SET;
757
51.5k
    } else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
758
47.4k
        memcpy(ssl_state->client_connp.random, input, TLS_RANDOM_LEN);
759
47.4k
        ssl_state->flags |= TLS_TC_RANDOM_SET;
760
47.4k
    }
761
762
    /* Skip random */
763
98.9k
    input += SSLV3_CLIENT_HELLO_RANDOM_LEN;
764
765
98.9k
    return (input - initial_input);
766
104k
}
767
768
static inline int TLSDecodeHSHelloSessionID(SSLState *ssl_state,
769
                                            const uint8_t * const initial_input,
770
                                            const uint32_t input_len)
771
94.0k
{
772
94.0k
    uint8_t *input = (uint8_t *)initial_input;
773
774
94.0k
    if (!(HAS_SPACE(1)))
775
725
        goto invalid_length;
776
777
93.3k
    uint8_t session_id_length = *input;
778
93.3k
    input += 1;
779
780
93.3k
    if (!(HAS_SPACE(session_id_length)))
781
5.92k
        goto invalid_length;
782
783
87.4k
    if (session_id_length != 0 && ssl_state->curr_connp->session_id == NULL) {
784
18.9k
        ssl_state->curr_connp->session_id = SCMalloc(session_id_length);
785
786
18.9k
        if (unlikely(ssl_state->curr_connp->session_id == NULL)) {
787
0
            return -1;
788
0
        }
789
790
18.9k
        if (SafeMemcpy(ssl_state->curr_connp->session_id, 0, session_id_length,
791
18.9k
                    input, 0, input_len, session_id_length) != 0) {
792
0
            return -1;
793
0
        }
794
18.9k
        ssl_state->curr_connp->session_id_length = session_id_length;
795
796
18.9k
        if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
797
8.68k
                ssl_state->client_connp.session_id != NULL &&
798
7.27k
                ssl_state->server_connp.session_id != NULL) {
799
5.42k
            if ((ssl_state->client_connp.session_id_length ==
800
5.42k
                    ssl_state->server_connp.session_id_length) &&
801
5.30k
                    (memcmp(ssl_state->server_connp.session_id,
802
5.30k
                    ssl_state->client_connp.session_id, session_id_length) == 0)) {
803
3.27k
                ssl_state->flags |= SSL_AL_FLAG_SESSION_RESUMED;
804
3.27k
            }
805
5.42k
        }
806
18.9k
    }
807
808
87.4k
    input += session_id_length;
809
810
87.4k
    return (input - initial_input);
811
812
6.65k
invalid_length:
813
6.65k
    SCLogDebug("TLS handshake invalid length");
814
6.65k
    SSLSetEvent(ssl_state,
815
6.65k
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
816
6.65k
    return -1;
817
87.4k
}
818
819
static inline int TLSDecodeHSHelloCipherSuites(SSLState *ssl_state,
820
                                           const uint8_t * const initial_input,
821
                                           const uint32_t input_len)
822
46.3k
{
823
46.3k
    const uint8_t *input = initial_input;
824
825
46.3k
    if (!(HAS_SPACE(2)))
826
856
        goto invalid_length;
827
828
45.4k
    uint16_t cipher_suites_length;
829
830
45.4k
    if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
831
21.8k
        cipher_suites_length = 2;
832
23.5k
    } else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
833
23.5k
        cipher_suites_length = (uint16_t)(*input << 8) | *(input + 1);
834
23.5k
        input += 2;
835
23.5k
    } else {
836
0
        return -1;
837
0
    }
838
839
45.4k
    if (!(HAS_SPACE(cipher_suites_length)))
840
1.81k
        goto invalid_length;
841
842
    /* Cipher suites length should always be divisible by 2 */
843
43.6k
    if ((cipher_suites_length % 2) != 0) {
844
866
        goto invalid_length;
845
866
    }
846
847
42.7k
    const bool enable_ja3 =
848
42.7k
            SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_hash == NULL;
849
850
42.7k
    if (enable_ja3 || SC_ATOMIC_GET(ssl_config.enable_ja4)) {
851
13.3k
        JA3Buffer *ja3_cipher_suites = NULL;
852
853
13.3k
        if (enable_ja3) {
854
13.0k
            ja3_cipher_suites = Ja3BufferInit();
855
13.0k
            if (ja3_cipher_suites == NULL)
856
0
                return -1;
857
13.0k
        }
858
859
13.3k
        uint16_t processed_len = 0;
860
        /* coverity[tainted_data] */
861
161k
        while (processed_len < cipher_suites_length)
862
148k
        {
863
148k
            if (!(HAS_SPACE(2))) {
864
0
                if (enable_ja3) {
865
0
                    Ja3BufferFree(&ja3_cipher_suites);
866
0
                }
867
0
                goto invalid_length;
868
0
            }
869
870
148k
            uint16_t cipher_suite = (uint16_t)(*input << 8) | *(input + 1);
871
148k
            input += 2;
872
873
148k
            if (TLSDecodeValueIsGREASE(cipher_suite) != 1) {
874
147k
                if (ssl_state->curr_connp->ja4 != NULL &&
875
141k
                        ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
876
141k
                    SCJA4AddCipher(ssl_state->curr_connp->ja4, cipher_suite);
877
141k
                }
878
147k
                if (enable_ja3) {
879
146k
                    int rc = Ja3BufferAddValue(&ja3_cipher_suites, cipher_suite);
880
146k
                    if (rc != 0) {
881
0
                        return -1;
882
0
                    }
883
146k
                }
884
147k
            }
885
148k
            processed_len += 2;
886
148k
        }
887
888
13.3k
        if (enable_ja3) {
889
13.0k
            int rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str, &ja3_cipher_suites);
890
13.0k
            if (rc == -1) {
891
0
                return -1;
892
0
            }
893
13.0k
        }
894
895
29.4k
    } else {
896
        /* Skip cipher suites */
897
29.4k
        input += cipher_suites_length;
898
29.4k
    }
899
900
42.7k
    return (input - initial_input);
901
902
3.53k
invalid_length:
903
3.53k
    SCLogDebug("TLS handshake invalid length");
904
3.53k
    SSLSetEvent(ssl_state,
905
3.53k
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
906
3.53k
    return -1;
907
42.7k
}
908
909
static inline int TLSDecodeHSHelloCompressionMethods(SSLState *ssl_state,
910
                                           const uint8_t * const initial_input,
911
                                           const uint32_t input_len)
912
80.8k
{
913
80.8k
    const uint8_t *input = initial_input;
914
915
80.8k
    if (!(HAS_SPACE(1)))
916
1.77k
        goto invalid_length;
917
918
    /* Skip compression methods */
919
79.0k
    if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
920
40.3k
        input += 1;
921
40.3k
    } else {
922
38.7k
        uint8_t compression_methods_length = *input;
923
38.7k
        input += 1;
924
925
38.7k
        if (!(HAS_SPACE(compression_methods_length)))
926
1.23k
            goto invalid_length;
927
928
37.5k
        input += compression_methods_length;
929
37.5k
    }
930
931
77.8k
    return (input - initial_input);
932
933
3.01k
invalid_length:
934
3.01k
    SCLogDebug("TLS handshake invalid_length");
935
3.01k
    SSLSetEvent(ssl_state,
936
3.01k
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
937
3.01k
    return -1;
938
79.0k
}
939
940
static inline int TLSDecodeHSHelloExtensionSni(SSLState *ssl_state,
941
                                           const uint8_t * const initial_input,
942
                                           const uint32_t input_len)
943
119k
{
944
119k
    uint8_t *input = (uint8_t *)initial_input;
945
946
    /* Empty extension */
947
119k
    if (input_len == 0)
948
99.2k
        return 0;
949
950
20.6k
    if (!(HAS_SPACE(2)))
951
1.13k
        goto invalid_length;
952
953
    /* Skip sni_list_length */
954
19.5k
    input += 2;
955
956
19.5k
    if (!(HAS_SPACE(1)))
957
1.21k
        goto invalid_length;
958
959
18.3k
    uint8_t sni_type = *input;
960
18.3k
    input += 1;
961
962
    /* Currently the only type allowed is host_name
963
       (RFC6066 section 3). */
964
18.3k
    if (sni_type != SSL_SNI_TYPE_HOST_NAME) {
965
999
        SCLogDebug("Unknown SNI type");
966
999
        SSLSetEvent(ssl_state,
967
999
                TLS_DECODER_EVENT_INVALID_SNI_TYPE);
968
999
        return -1;
969
999
    }
970
971
17.3k
    if (!(HAS_SPACE(2)))
972
1.65k
        goto invalid_length;
973
974
15.6k
    uint16_t sni_len = (uint16_t)(*input << 8) | *(input + 1);
975
15.6k
    input += 2;
976
977
    /* host_name contains the fully qualified domain name,
978
       and should therefore be limited by the maximum domain
979
       name length. */
980
15.6k
    if (!(HAS_SPACE(sni_len)) || sni_len > 255 || sni_len == 0) {
981
2.02k
        SSLSetEvent(ssl_state,
982
2.02k
                TLS_DECODER_EVENT_INVALID_SNI_LENGTH);
983
2.02k
        return -1;
984
2.02k
    }
985
986
    /* There must not be more than one extension of the same
987
       type (RFC5246 section 7.4.1.4). */
988
13.6k
    if (ssl_state->curr_connp->sni) {
989
1.54k
        SCLogDebug("Multiple SNI extensions");
990
1.54k
        SSLSetEvent(ssl_state,
991
1.54k
                TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS);
992
1.54k
        input += sni_len;
993
1.54k
        return (input - initial_input);
994
1.54k
    }
995
996
12.1k
    const size_t sni_strlen = sni_len + 1;
997
12.1k
    ssl_state->curr_connp->sni = SCMalloc(sni_strlen);
998
12.1k
    if (unlikely(ssl_state->curr_connp->sni == NULL))
999
0
        return -1;
1000
1001
12.1k
    const size_t consumed = input - initial_input;
1002
12.1k
    if (SafeMemcpy(ssl_state->curr_connp->sni, 0, sni_strlen,
1003
12.1k
                initial_input, consumed, input_len, sni_len) != 0) {
1004
0
        SCFree(ssl_state->curr_connp->sni);
1005
0
        ssl_state->curr_connp->sni = NULL;
1006
0
        return -1;
1007
0
    }
1008
12.1k
    ssl_state->curr_connp->sni[sni_strlen-1] = 0;
1009
1010
12.1k
    input += sni_len;
1011
1012
12.1k
    return (input - initial_input);
1013
1014
4.00k
invalid_length:
1015
4.00k
    SCLogDebug("TLS handshake invalid length");
1016
4.00k
    SSLSetEvent(ssl_state,
1017
4.00k
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
1018
1019
1020
4.00k
    return -1;
1021
12.1k
}
1022
1023
static inline int TLSDecodeHSHelloExtensionSupportedVersions(SSLState *ssl_state,
1024
                                             const uint8_t * const initial_input,
1025
                                             const uint32_t input_len)
1026
15.8k
{
1027
15.8k
    const uint8_t *input = initial_input;
1028
1029
    /* Empty extension */
1030
15.8k
    if (input_len == 0)
1031
439
        return 0;
1032
1033
15.4k
    if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1034
13.9k
        if (!(HAS_SPACE(1)))
1035
0
            goto invalid_length;
1036
1037
13.9k
        uint8_t supported_ver_len = *input;
1038
13.9k
        input += 1;
1039
1040
13.9k
        if (supported_ver_len < 2)
1041
206
            goto invalid_length;
1042
1043
13.7k
        if (!(HAS_SPACE(supported_ver_len)))
1044
571
            goto invalid_length;
1045
1046
        /* Use the first (and prefered) valid version as client version,
1047
         * skip over GREASE and other possible noise. */
1048
13.2k
        uint16_t i = 0;
1049
46.1k
        while (i + 1 < (uint16_t)supported_ver_len) {
1050
45.6k
            uint16_t ver = (uint16_t)(input[i] << 8) | input[i + 1];
1051
45.6k
            if (TLSVersionValid(ver)) {
1052
12.6k
                ssl_state->curr_connp->version = ver;
1053
12.6k
                if (ssl_state->curr_connp->ja4 != NULL &&
1054
3.66k
                        ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1055
3.66k
                    SCJA4SetTLSVersion(ssl_state->curr_connp->ja4, ver);
1056
3.66k
                }
1057
12.6k
                break;
1058
12.6k
            }
1059
32.9k
            i += 2;
1060
32.9k
        }
1061
1062
        /* Set a flag to indicate that we have seen this extension */
1063
13.2k
        ssl_state->flags |= SSL_AL_FLAG_CH_VERSION_EXTENSION;
1064
1065
13.2k
        input += supported_ver_len;
1066
13.2k
    }
1067
1.42k
    else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
1068
1.42k
        if (!(HAS_SPACE(2)))
1069
399
            goto invalid_length;
1070
1071
1.03k
        uint16_t ver = (uint16_t)(*input << 8) | *(input + 1);
1072
1073
1.03k
        if ((ssl_state->flags & SSL_AL_FLAG_CH_VERSION_EXTENSION) &&
1074
769
                (ver > TLS_VERSION_12)) {
1075
270
            ssl_state->flags |= SSL_AL_FLAG_LOG_WITHOUT_CERT;
1076
270
        }
1077
1078
1.03k
        ssl_state->curr_connp->version = ver;
1079
1.03k
        input += 2;
1080
1.03k
    }
1081
1082
14.2k
    return (input - initial_input);
1083
1084
1.17k
invalid_length:
1085
1.17k
    SCLogDebug("TLS handshake invalid length");
1086
1.17k
    SSLSetEvent(ssl_state,
1087
1.17k
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
1088
1089
1.17k
    return -1;
1090
15.4k
}
1091
1092
static inline int TLSDecodeHSHelloExtensionEllipticCurves(SSLState *ssl_state,
1093
                                          const uint8_t * const initial_input,
1094
                                          const uint32_t input_len,
1095
                                          JA3Buffer *ja3_elliptic_curves)
1096
16.0k
{
1097
16.0k
    const uint8_t *input = initial_input;
1098
1099
    /* Empty extension */
1100
16.0k
    if (input_len == 0)
1101
1.01k
        return 0;
1102
1103
15.0k
    if (!(HAS_SPACE(2)))
1104
518
        goto invalid_length;
1105
1106
14.5k
    uint16_t elliptic_curves_len = (uint16_t)(*input << 8) | *(input + 1);
1107
14.5k
    input += 2;
1108
1109
14.5k
    if (!(HAS_SPACE(elliptic_curves_len)))
1110
639
        goto invalid_length;
1111
1112
13.9k
    if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) && ja3_elliptic_curves) {
1113
12.5k
        uint16_t ec_processed_len = 0;
1114
        /* coverity[tainted_data] */
1115
75.4k
        while (ec_processed_len < elliptic_curves_len)
1116
62.8k
        {
1117
62.8k
            if (!(HAS_SPACE(2)))
1118
2
                goto invalid_length;
1119
1120
62.8k
            uint16_t elliptic_curve = (uint16_t)(*input << 8) | *(input + 1);
1121
62.8k
            input += 2;
1122
1123
62.8k
            if (TLSDecodeValueIsGREASE(elliptic_curve) != 1) {
1124
62.3k
                int rc = Ja3BufferAddValue(&ja3_elliptic_curves,
1125
62.3k
                                           elliptic_curve);
1126
62.3k
                if (rc != 0)
1127
0
                    return -1;
1128
62.3k
            }
1129
1130
62.8k
            ec_processed_len += 2;
1131
62.8k
        }
1132
1133
12.5k
    } else {
1134
        /* Skip elliptic curves */
1135
1.39k
        input += elliptic_curves_len;
1136
1.39k
    }
1137
1138
13.9k
    return (input - initial_input);
1139
1140
1.15k
invalid_length:
1141
1.15k
    SCLogDebug("TLS handshake invalid length");
1142
1.15k
    SSLSetEvent(ssl_state,
1143
1.15k
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
1144
1145
1.15k
    return -1;
1146
13.9k
}
1147
1148
static inline int TLSDecodeHSHelloExtensionEllipticCurvePF(SSLState *ssl_state,
1149
                                            const uint8_t * const initial_input,
1150
                                            const uint32_t input_len,
1151
                                            JA3Buffer *ja3_elliptic_curves_pf)
1152
22.7k
{
1153
22.7k
    const uint8_t *input = initial_input;
1154
1155
    /* Empty extension */
1156
22.7k
    if (input_len == 0)
1157
1.70k
        return 0;
1158
1159
21.0k
    if (!(HAS_SPACE(1)))
1160
0
        goto invalid_length;
1161
1162
21.0k
    uint8_t ec_pf_len = *input;
1163
21.0k
    input += 1;
1164
1165
21.0k
    if (!(HAS_SPACE(ec_pf_len)))
1166
592
        goto invalid_length;
1167
1168
20.4k
    if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) && ja3_elliptic_curves_pf) {
1169
12.6k
        uint8_t ec_pf_processed_len = 0;
1170
        /* coverity[tainted_data] */
1171
27.6k
        while (ec_pf_processed_len < ec_pf_len)
1172
15.0k
        {
1173
15.0k
            uint8_t elliptic_curve_pf = *input;
1174
15.0k
            input += 1;
1175
1176
15.0k
            if (TLSDecodeValueIsGREASE(elliptic_curve_pf) != 1) {
1177
15.0k
                int rc = Ja3BufferAddValue(&ja3_elliptic_curves_pf,
1178
15.0k
                                           elliptic_curve_pf);
1179
15.0k
                if (rc != 0)
1180
0
                    return -1;
1181
15.0k
            }
1182
1183
15.0k
            ec_pf_processed_len += 1;
1184
15.0k
        }
1185
1186
12.6k
    } else {
1187
        /* Skip elliptic curve point formats */
1188
7.82k
        input += ec_pf_len;
1189
7.82k
    }
1190
1191
20.4k
    return (input - initial_input);
1192
1193
592
invalid_length:
1194
592
    SCLogDebug("TLS handshake invalid length");
1195
592
    SSLSetEvent(ssl_state,
1196
592
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
1197
1198
592
    return -1;
1199
20.4k
}
1200
1201
static inline int TLSDecodeHSHelloExtensionSigAlgorithms(
1202
        SSLState *ssl_state, const uint8_t *const initial_input, const uint32_t input_len)
1203
6.66k
{
1204
6.66k
    const uint8_t *input = initial_input;
1205
1206
    /* Empty extension */
1207
6.66k
    if (input_len == 0)
1208
683
        return 0;
1209
1210
5.98k
    if (!(HAS_SPACE(2)))
1211
447
        goto invalid_length;
1212
1213
5.53k
    uint16_t sigalgo_len = (uint16_t)(*input << 8) | *(input + 1);
1214
5.53k
    input += 2;
1215
1216
    /* Signature algorithms length should always be divisible by 2 */
1217
5.53k
    if ((sigalgo_len % 2) != 0) {
1218
392
        goto invalid_length;
1219
392
    }
1220
1221
5.14k
    if (!(HAS_SPACE(sigalgo_len)))
1222
362
        goto invalid_length;
1223
1224
4.78k
    if (ssl_state->curr_connp->ja4 != NULL &&
1225
4.14k
            ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1226
4.14k
        uint16_t sigalgo_processed_len = 0;
1227
51.0k
        while (sigalgo_processed_len < sigalgo_len) {
1228
46.9k
            uint16_t sigalgo = (uint16_t)(*input << 8) | *(input + 1);
1229
46.9k
            input += 2;
1230
46.9k
            sigalgo_processed_len += 2;
1231
1232
46.9k
            SCJA4AddSigAlgo(ssl_state->curr_connp->ja4, sigalgo);
1233
46.9k
        }
1234
4.14k
    } else {
1235
        /* Skip signature algorithms */
1236
643
        input += sigalgo_len;
1237
643
    }
1238
1239
4.78k
    return (input - initial_input);
1240
1241
1.20k
invalid_length:
1242
1.20k
    SCLogDebug("Signature algorithm list invalid length");
1243
1.20k
    SSLSetEvent(ssl_state, TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
1244
1245
1.20k
    return -1;
1246
5.14k
}
1247
1248
static inline int TLSDecodeHSHelloExtensionALPN(
1249
        SSLState *ssl_state, const uint8_t *const initial_input, const uint32_t input_len)
1250
9.70k
{
1251
9.70k
    const uint8_t *input = initial_input;
1252
1253
    /* Empty extension */
1254
9.70k
    if (input_len == 0)
1255
485
        return 0;
1256
1257
9.22k
    if (!(HAS_SPACE(2)))
1258
559
        goto invalid_length;
1259
1260
8.66k
    uint16_t alpn_len = (uint16_t)(*input << 8) | *(input + 1);
1261
8.66k
    input += 2;
1262
1263
8.66k
    if (!(HAS_SPACE(alpn_len)))
1264
697
        goto invalid_length;
1265
1266
7.96k
    if (ssl_state->curr_connp->ja4 != NULL &&
1267
4.62k
            ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1268
        /* We use 32 bits here to avoid potentially overflowing a value that
1269
           needs to be compared to an unsigned 16-bit value. */
1270
4.62k
        uint32_t alpn_processed_len = 0;
1271
13.8k
        while (alpn_processed_len < alpn_len) {
1272
9.24k
            uint8_t protolen = *input;
1273
9.24k
            input += 1;
1274
9.24k
            alpn_processed_len += 1;
1275
1276
9.24k
            if (!(HAS_SPACE(protolen)))
1277
6
                goto invalid_length;
1278
1279
            /* Check if reading another protolen bytes would exceed the
1280
               overall ALPN length; if so, skip and continue */
1281
9.24k
            if (alpn_processed_len + protolen > ((uint32_t)alpn_len)) {
1282
1
                input += alpn_len - alpn_processed_len;
1283
1
                break;
1284
1
            }
1285
1286
            /* Only record the first value for JA4 */
1287
9.24k
            if (alpn_processed_len == 1) {
1288
4.62k
                SCJA4SetALPN(ssl_state->curr_connp->ja4, (const char *)input, protolen);
1289
4.62k
            }
1290
1291
9.24k
            alpn_processed_len += protolen;
1292
9.24k
            input += protolen;
1293
9.24k
        }
1294
4.62k
    } else {
1295
        /* Skip ALPN protocols */
1296
3.33k
        input += alpn_len;
1297
3.33k
    }
1298
1299
7.95k
    return (input - initial_input);
1300
1301
1.26k
invalid_length:
1302
1.26k
    SCLogDebug("ALPN list invalid length");
1303
1.26k
    SSLSetEvent(ssl_state, TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
1304
1305
1.26k
    return -1;
1306
7.96k
}
1307
1308
static inline int TLSDecodeHSHelloExtensions(SSLState *ssl_state,
1309
                                         const uint8_t * const initial_input,
1310
                                         const uint32_t input_len)
1311
41.3k
{
1312
41.3k
    const uint8_t *input = initial_input;
1313
1314
41.3k
    int ret;
1315
41.3k
    int rc;
1316
    // if ja3_hash is already computed, do not use new hello to augment ja3_str
1317
41.3k
    const bool ja3 =
1318
41.3k
            (SC_ATOMIC_GET(ssl_config.enable_ja3) == 1) && ssl_state->curr_connp->ja3_hash == NULL;
1319
1320
41.3k
    JA3Buffer *ja3_extensions = NULL;
1321
41.3k
    JA3Buffer *ja3_elliptic_curves = NULL;
1322
41.3k
    JA3Buffer *ja3_elliptic_curves_pf = NULL;
1323
1324
41.3k
    if (ja3) {
1325
12.9k
        ja3_extensions = Ja3BufferInit();
1326
12.9k
        if (ja3_extensions == NULL)
1327
0
            goto error;
1328
1329
12.9k
        if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1330
7.21k
            ja3_elliptic_curves = Ja3BufferInit();
1331
7.21k
            if (ja3_elliptic_curves == NULL)
1332
0
                goto error;
1333
1334
7.21k
            ja3_elliptic_curves_pf = Ja3BufferInit();
1335
7.21k
            if (ja3_elliptic_curves_pf == NULL)
1336
0
                goto error;
1337
7.21k
        }
1338
12.9k
    }
1339
1340
    /* Extensions are optional (RFC5246 section 7.4.1.2) */
1341
41.3k
    if (!(HAS_SPACE(2)))
1342
854
        goto end;
1343
1344
40.4k
    uint16_t extensions_len = (uint16_t)(*input << 8) | *(input + 1);
1345
40.4k
    input += 2;
1346
1347
40.4k
    if (!(HAS_SPACE(extensions_len)))
1348
2.22k
        goto invalid_length;
1349
1350
38.2k
    uint16_t processed_len = 0;
1351
    /* coverity[tainted_data] */
1352
208k
    while (processed_len < extensions_len)
1353
187k
    {
1354
187k
        if (!(HAS_SPACE(2)))
1355
1.88k
            goto invalid_length;
1356
1357
186k
        uint16_t ext_type = (uint16_t)(*input << 8) | *(input + 1);
1358
186k
        input += 2;
1359
1360
186k
        if (!(HAS_SPACE(2)))
1361
2.42k
            goto invalid_length;
1362
1363
183k
        uint16_t ext_len = (uint16_t)(*input << 8) | *(input + 1);
1364
183k
        input += 2;
1365
1366
183k
        if (!(HAS_SPACE(ext_len)))
1367
5.63k
            goto invalid_length;
1368
1369
178k
        switch (ext_type) {
1370
59.2k
            case SSL_EXTENSION_SNI:
1371
59.2k
            {
1372
                /* coverity[tainted_data] */
1373
59.2k
                ret = TLSDecodeHSHelloExtensionSni(ssl_state, input,
1374
59.2k
                                                   ext_len);
1375
59.2k
                if (ret < 0)
1376
3.48k
                    goto end;
1377
1378
55.7k
                input += ret;
1379
1380
55.7k
                break;
1381
59.2k
            }
1382
1383
8.77k
            case SSL_EXTENSION_ELLIPTIC_CURVES:
1384
8.77k
            {
1385
                /* coverity[tainted_data] */
1386
8.77k
                ret = TLSDecodeHSHelloExtensionEllipticCurves(ssl_state, input,
1387
8.77k
                                                              ext_len,
1388
8.77k
                                                              ja3_elliptic_curves);
1389
8.77k
                if (ret < 0)
1390
587
                    goto end;
1391
1392
8.18k
                input += ret;
1393
1394
8.18k
                break;
1395
8.77k
            }
1396
1397
11.6k
            case SSL_EXTENSION_EC_POINT_FORMATS:
1398
11.6k
            {
1399
                /* coverity[tainted_data] */
1400
11.6k
                ret = TLSDecodeHSHelloExtensionEllipticCurvePF(ssl_state, input,
1401
11.6k
                                                               ext_len,
1402
11.6k
                                                               ja3_elliptic_curves_pf);
1403
11.6k
                if (ret < 0)
1404
291
                    goto end;
1405
1406
11.3k
                input += ret;
1407
1408
11.3k
                break;
1409
11.6k
            }
1410
1411
6.66k
            case SSL_EXTENSION_SIGNATURE_ALGORITHMS: {
1412
                /* coverity[tainted_data] */
1413
6.66k
                ret = TLSDecodeHSHelloExtensionSigAlgorithms(ssl_state, input, ext_len);
1414
6.66k
                if (ret < 0)
1415
1.20k
                    goto end;
1416
1417
5.46k
                input += ret;
1418
1419
5.46k
                break;
1420
6.66k
            }
1421
1422
9.70k
            case SSL_EXTENSION_ALPN: {
1423
                /* coverity[tainted_data] */
1424
9.70k
                ret = TLSDecodeHSHelloExtensionALPN(ssl_state, input, ext_len);
1425
9.70k
                if (ret < 0)
1426
1.26k
                    goto end;
1427
1428
8.44k
                input += ext_len;
1429
1430
8.44k
                break;
1431
9.70k
            }
1432
1433
1.93k
            case SSL_EXTENSION_EARLY_DATA:
1434
1.93k
            {
1435
1.93k
                if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1436
                    /* Used by 0-RTT to indicate that encrypted data will
1437
                       be sent right after the ClientHello record. */
1438
1.66k
                    ssl_state->flags |= SSL_AL_FLAG_EARLY_DATA;
1439
1.66k
                }
1440
1441
1.93k
                input += ext_len;
1442
1443
1.93k
                break;
1444
9.70k
            }
1445
1446
15.8k
            case SSL_EXTENSION_SUPPORTED_VERSIONS:
1447
15.8k
            {
1448
15.8k
                ret = TLSDecodeHSHelloExtensionSupportedVersions(ssl_state, input,
1449
15.8k
                                                                 ext_len);
1450
15.8k
                if (ret < 0)
1451
1.17k
                    goto end;
1452
1453
14.6k
                input += ret;
1454
1455
14.6k
                break;
1456
15.8k
            }
1457
1458
9.24k
            case SSL_EXTENSION_SESSION_TICKET:
1459
9.24k
            {
1460
9.24k
                if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1461
                    /* This has to be verified later on by checking if a
1462
                       certificate record has been sent by the server. */
1463
6.92k
                    ssl_state->flags |= SSL_AL_FLAG_SESSION_RESUMED;
1464
6.92k
                }
1465
1466
9.24k
                input += ext_len;
1467
1468
9.24k
                break;
1469
15.8k
            }
1470
1471
55.0k
            default:
1472
55.0k
            {
1473
55.0k
                input += ext_len;
1474
55.0k
                break;
1475
15.8k
            }
1476
178k
        }
1477
1478
170k
        if (ja3) {
1479
105k
            if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1480
105k
                rc = Ja3BufferAddValue(&ja3_extensions, ext_type);
1481
105k
                if (rc != 0)
1482
0
                    goto error;
1483
105k
            }
1484
105k
        }
1485
1486
170k
        if (ssl_state->curr_connp->ja4 != NULL &&
1487
85.6k
                ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1488
85.6k
            if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1489
85.4k
                SCJA4AddExtension(ssl_state->curr_connp->ja4, ext_type);
1490
85.4k
            }
1491
85.6k
        }
1492
1493
170k
        processed_len += ext_len + 4;
1494
170k
    }
1495
1496
29.1k
end:
1497
29.1k
    if (ja3) {
1498
10.8k
        rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
1499
10.8k
                                   &ja3_extensions);
1500
10.8k
        if (rc == -1)
1501
0
            goto error;
1502
1503
10.8k
        if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1504
5.36k
            rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
1505
5.36k
                                       &ja3_elliptic_curves);
1506
5.36k
            if (rc == -1)
1507
0
                goto error;
1508
1509
5.36k
            rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
1510
5.36k
                                       &ja3_elliptic_curves_pf);
1511
5.36k
            if (rc == -1)
1512
0
                goto error;
1513
5.36k
        }
1514
10.8k
    }
1515
1516
29.1k
    return (input - initial_input);
1517
1518
12.1k
invalid_length:
1519
12.1k
    SCLogDebug("TLS handshake invalid length");
1520
12.1k
    SSLSetEvent(ssl_state,
1521
12.1k
                TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH);
1522
1523
12.1k
error:
1524
12.1k
    if (ja3_extensions != NULL)
1525
2.13k
        Ja3BufferFree(&ja3_extensions);
1526
12.1k
    if (ja3_elliptic_curves != NULL)
1527
1.85k
        Ja3BufferFree(&ja3_elliptic_curves);
1528
12.1k
    if (ja3_elliptic_curves_pf != NULL)
1529
1.85k
        Ja3BufferFree(&ja3_elliptic_curves_pf);
1530
1531
12.1k
    return -1;
1532
12.1k
}
1533
1534
static int TLSDecodeHandshakeHello(SSLState *ssl_state,
1535
                                   const uint8_t * const input,
1536
                                   const uint32_t input_len)
1537
52.2k
{
1538
52.2k
    int ret;
1539
52.2k
    uint32_t parsed = 0;
1540
1541
    /* Ensure that we have a JA4 state defined by now if we have JA4 enabled,
1542
       we are in a client hello and we don't have such a state yet (to avoid
1543
       leaking memory in case this function is entered more than once). */
1544
52.2k
    if (SC_ATOMIC_GET(ssl_config.enable_ja4) &&
1545
52.2k
            ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO &&
1546
7.95k
            ssl_state->curr_connp->ja4 == NULL) {
1547
7.42k
        ssl_state->curr_connp->ja4 = SCJA4New();
1548
7.42k
    }
1549
1550
52.2k
    ret = TLSDecodeHSHelloVersion(ssl_state, input, input_len);
1551
52.2k
    if (ret < 0)
1552
862
        goto end;
1553
1554
51.3k
    parsed += ret;
1555
1556
51.3k
    ret = TLSDecodeHSHelloRandom(ssl_state, input + parsed, input_len - parsed);
1557
51.3k
    if (ret < 0)
1558
1.51k
        goto end;
1559
1560
49.8k
    parsed += ret;
1561
1562
    /* The session id field in the server hello record was removed in
1563
       TLSv1.3 draft1, but was readded in draft22. */
1564
49.8k
    if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) ||
1565
24.8k
            ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
1566
47.8k
            ((ssl_state->flags & SSL_AL_FLAG_LOG_WITHOUT_CERT) == 0))) {
1567
47.8k
        ret = TLSDecodeHSHelloSessionID(ssl_state, input + parsed,
1568
47.8k
                                        input_len - parsed);
1569
47.8k
        if (ret < 0)
1570
3.53k
            goto end;
1571
1572
44.3k
        parsed += ret;
1573
44.3k
    }
1574
1575
46.3k
    ret = TLSDecodeHSHelloCipherSuites(ssl_state, input + parsed,
1576
46.3k
                                       input_len - parsed);
1577
46.3k
    if (ret < 0)
1578
3.53k
        goto end;
1579
1580
42.7k
    parsed += ret;
1581
1582
   /* The compression methods field in the server hello record was
1583
      removed in TLSv1.3 draft1, but was readded in draft22. */
1584
42.7k
   if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) ||
1585
21.8k
              ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
1586
40.8k
              ((ssl_state->flags & SSL_AL_FLAG_LOG_WITHOUT_CERT) == 0))) {
1587
40.8k
        ret = TLSDecodeHSHelloCompressionMethods(ssl_state, input + parsed,
1588
40.8k
                                                 input_len - parsed);
1589
40.8k
        if (ret < 0)
1590
1.46k
            goto end;
1591
1592
39.3k
        parsed += ret;
1593
39.3k
    }
1594
1595
41.3k
    ret = TLSDecodeHSHelloExtensions(ssl_state, input + parsed,
1596
41.3k
                                     input_len - parsed);
1597
41.3k
    if (ret < 0)
1598
12.1k
        goto end;
1599
1600
29.1k
    if (SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_hash == NULL) {
1601
10.8k
        ssl_state->curr_connp->ja3_hash = Ja3GenerateHash(ssl_state->curr_connp->ja3_str);
1602
10.8k
    }
1603
1604
52.2k
end:
1605
52.2k
    return 0;
1606
29.1k
}
1607
1608
#ifdef DEBUG_VALIDATION
1609
static inline bool
1610
RecordAlreadyProcessed(const SSLStateConnp *curr_connp)
1611
168k
{
1612
168k
    return ((curr_connp->record_length + SSLV3_RECORD_HDR_LEN) <
1613
168k
            curr_connp->bytes_processed);
1614
168k
}
1615
#endif
1616
1617
static inline int SSLv3ParseHandshakeTypeCertificate(SSLState *ssl_state, SSLStateConnp *connp,
1618
        const uint8_t *const initial_input, const uint32_t input_len)
1619
7.02k
{
1620
7.02k
    int rc = TlsDecodeHSCertificates(ssl_state, connp, initial_input, input_len);
1621
7.02k
    SCLogDebug("rc %d", rc);
1622
7.02k
    if (rc > 0) {
1623
4.11k
        DEBUG_VALIDATE_BUG_ON(rc > (int)input_len);
1624
4.11k
        SSLParserHSReset(connp);
1625
4.11k
    } else if (rc < 0) {
1626
2.90k
        SCLogDebug("error parsing cert, reset state");
1627
2.90k
        SSLParserHSReset(connp);
1628
        /* fall through to still consume the cert bytes */
1629
2.90k
    }
1630
7.02k
    return input_len;
1631
7.02k
}
1632
1633
static int SupportedHandshakeType(const uint8_t type)
1634
241k
{
1635
241k
    switch (type) {
1636
50.7k
        case SSLV3_HS_CLIENT_HELLO:
1637
107k
        case SSLV3_HS_SERVER_HELLO:
1638
112k
        case SSLV3_HS_SERVER_KEY_EXCHANGE:
1639
117k
        case SSLV3_HS_CLIENT_KEY_EXCHANGE:
1640
132k
        case SSLV3_HS_CERTIFICATE:
1641
167k
        case SSLV3_HS_HELLO_REQUEST:
1642
170k
        case SSLV3_HS_CERTIFICATE_REQUEST:
1643
171k
        case SSLV3_HS_CERTIFICATE_VERIFY:
1644
172k
        case SSLV3_HS_FINISHED:
1645
173k
        case SSLV3_HS_CERTIFICATE_URL:
1646
175k
        case SSLV3_HS_CERTIFICATE_STATUS:
1647
176k
        case SSLV3_HS_NEW_SESSION_TICKET:
1648
183k
        case SSLV3_HS_SERVER_HELLO_DONE:
1649
183k
            return true;
1650
0
            break;
1651
1652
58.3k
        default:
1653
58.3k
            return false;
1654
0
            break;
1655
241k
    }
1656
241k
}
1657
1658
/**
1659
 *  \retval parsed number of consumed bytes
1660
 *  \retval < 0 error
1661
 */
1662
static int SSLv3ParseHandshakeType(SSLState *ssl_state, const uint8_t *input,
1663
                                   uint32_t input_len, uint8_t direction)
1664
93.6k
{
1665
93.6k
    const uint8_t *initial_input = input;
1666
93.6k
    int rc;
1667
1668
93.6k
    if (input_len == 0) {
1669
12.5k
        return 0;
1670
12.5k
    }
1671
81.1k
    DEBUG_VALIDATE_BUG_ON(RecordAlreadyProcessed(ssl_state->curr_connp));
1672
1673
81.1k
    switch (ssl_state->curr_connp->handshake_type) {
1674
26.0k
        case SSLV3_HS_CLIENT_HELLO:
1675
26.0k
            ssl_state->current_flags = SSL_AL_FLAG_STATE_CLIENT_HELLO;
1676
1677
26.0k
            rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1678
26.0k
            if (rc < 0)
1679
0
                return rc;
1680
26.0k
            break;
1681
1682
26.1k
        case SSLV3_HS_SERVER_HELLO:
1683
26.1k
            ssl_state->current_flags = SSL_AL_FLAG_STATE_SERVER_HELLO;
1684
1685
26.1k
            DEBUG_VALIDATE_BUG_ON(ssl_state->curr_connp->message_length != input_len);
1686
26.1k
            rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1687
26.1k
            if (rc < 0)
1688
0
                return rc;
1689
26.1k
            break;
1690
1691
26.1k
        case SSLV3_HS_SERVER_KEY_EXCHANGE:
1692
2.33k
            ssl_state->current_flags = SSL_AL_FLAG_STATE_SERVER_KEYX;
1693
2.33k
            break;
1694
1695
2.08k
        case SSLV3_HS_CLIENT_KEY_EXCHANGE:
1696
2.08k
            ssl_state->current_flags = SSL_AL_FLAG_STATE_CLIENT_KEYX;
1697
2.08k
            break;
1698
1699
7.02k
        case SSLV3_HS_CERTIFICATE:
1700
1701
7.02k
            rc = SSLv3ParseHandshakeTypeCertificate(ssl_state,
1702
7.02k
                    direction ? &ssl_state->server_connp : &ssl_state->client_connp, initial_input,
1703
7.02k
                    input_len);
1704
7.02k
            if (rc < 0)
1705
0
                return rc;
1706
7.02k
            break;
1707
1708
13.3k
        case SSLV3_HS_HELLO_REQUEST:
1709
13.3k
            break;
1710
1.38k
        case SSLV3_HS_CERTIFICATE_REQUEST:
1711
1.38k
            if (direction) {
1712
860
                ssl_state->current_flags = SSL_AL_FLAG_NEED_CLIENT_CERT;
1713
860
            }
1714
1.38k
            break;
1715
294
        case SSLV3_HS_CERTIFICATE_VERIFY:
1716
562
        case SSLV3_HS_FINISHED:
1717
857
        case SSLV3_HS_CERTIFICATE_URL:
1718
1.77k
        case SSLV3_HS_CERTIFICATE_STATUS:
1719
1.77k
            break;
1720
613
        case SSLV3_HS_NEW_SESSION_TICKET:
1721
613
            SCLogDebug("new session ticket");
1722
613
            break;
1723
285
        case SSLV3_HS_SERVER_HELLO_DONE:
1724
285
            break;
1725
0
        default:
1726
0
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSL_RECORD);
1727
0
            return -1;
1728
81.1k
    }
1729
1730
81.1k
    ssl_state->flags |= ssl_state->current_flags;
1731
1732
81.1k
    SCLogDebug("message: length %u", ssl_state->curr_connp->message_length);
1733
81.1k
    SCLogDebug("input_len %u ssl_state->curr_connp->bytes_processed %u", input_len, ssl_state->curr_connp->bytes_processed);
1734
1735
81.1k
    return input_len;
1736
81.1k
}
1737
1738
static int SSLv3ParseHandshakeProtocol(SSLState *ssl_state, const uint8_t *input,
1739
                                       uint32_t input_len, uint8_t direction)
1740
224k
{
1741
224k
    const uint8_t *initial_input = input;
1742
1743
224k
    if (input_len == 0 || ssl_state->curr_connp->bytes_processed ==
1744
224k
            (ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN)) {
1745
0
        SCReturnInt(0);
1746
0
    }
1747
1748
532k
    while (input_len) {
1749
317k
        SCLogDebug("input_len %u", input_len);
1750
1751
317k
        if (ssl_state->curr_connp->hs_buffer != NULL) {
1752
74.6k
            SCLogDebug("partial handshake record in place");
1753
74.6k
            const uint32_t need = ssl_state->curr_connp->hs_buffer_message_size -
1754
74.6k
                                  ssl_state->curr_connp->hs_buffer_offset;
1755
74.6k
            const uint32_t add = MIN(need, input_len);
1756
1757
            /* grow buffer to next multiple of 4k that fits all data we have */
1758
74.6k
            if (ssl_state->curr_connp->hs_buffer_offset + add >
1759
74.6k
                    ssl_state->curr_connp->hs_buffer_size) {
1760
6.24k
                const uint32_t avail = ssl_state->curr_connp->hs_buffer_offset + add;
1761
6.24k
                const uint32_t new_size = avail + (4096 - (avail % 4096));
1762
6.24k
                SCLogDebug("new_size %u, avail %u", new_size, avail);
1763
6.24k
                void *ptr = SCRealloc(ssl_state->curr_connp->hs_buffer, new_size);
1764
6.24k
                if (ptr == NULL)
1765
0
                    return -1;
1766
6.24k
                ssl_state->curr_connp->hs_buffer = ptr;
1767
6.24k
                ssl_state->curr_connp->hs_buffer_size = new_size;
1768
6.24k
            }
1769
1770
74.6k
            SCLogDebug("ssl_state->curr_connp->hs_buffer_offset %u "
1771
74.6k
                       "ssl_state->curr_connp->hs_buffer_size %u",
1772
74.6k
                    ssl_state->curr_connp->hs_buffer_offset, ssl_state->curr_connp->hs_buffer_size);
1773
74.6k
            SCLogDebug("to add %u total %u", add, ssl_state->curr_connp->hs_buffer_offset + add);
1774
1775
74.6k
            if (SafeMemcpy(ssl_state->curr_connp->hs_buffer,
1776
74.6k
                        ssl_state->curr_connp->hs_buffer_offset,
1777
74.6k
                        ssl_state->curr_connp->hs_buffer_size, input, 0, add, add) != 0) {
1778
0
                SCLogDebug("copy failed");
1779
0
                return -1;
1780
0
            }
1781
74.6k
            ssl_state->curr_connp->hs_buffer_offset += add;
1782
1783
74.6k
            if (ssl_state->curr_connp->hs_buffer_message_size <=
1784
74.6k
                    ssl_state->curr_connp->hs_buffer_offset) {
1785
6.33k
                DEBUG_VALIDATE_BUG_ON(ssl_state->curr_connp->hs_buffer_message_size !=
1786
6.33k
                                      ssl_state->curr_connp->hs_buffer_offset);
1787
1788
6.33k
                ssl_state->curr_connp->handshake_type =
1789
6.33k
                        ssl_state->curr_connp->hs_buffer_message_type;
1790
6.33k
                ssl_state->curr_connp->message_length =
1791
6.33k
                        ssl_state->curr_connp->hs_buffer_message_size;
1792
1793
6.33k
                SCLogDebug("got all data now: handshake_type %u message_length %u",
1794
6.33k
                        ssl_state->curr_connp->handshake_type,
1795
6.33k
                        ssl_state->curr_connp->message_length);
1796
1797
6.33k
                int retval = SSLv3ParseHandshakeType(ssl_state, ssl_state->curr_connp->hs_buffer,
1798
6.33k
                        ssl_state->curr_connp->hs_buffer_offset, direction);
1799
6.33k
                if (retval < 0) {
1800
0
                    SSLParserHSReset(ssl_state->curr_connp);
1801
0
                    return (retval);
1802
0
                }
1803
6.33k
                SCLogDebug("retval %d", retval);
1804
1805
                /* data processed, reset buffer */
1806
6.33k
                SCFree(ssl_state->curr_connp->hs_buffer);
1807
6.33k
                ssl_state->curr_connp->hs_buffer = NULL;
1808
6.33k
                ssl_state->curr_connp->hs_buffer_size = 0;
1809
6.33k
                ssl_state->curr_connp->hs_buffer_message_size = 0;
1810
6.33k
                ssl_state->curr_connp->hs_buffer_message_type = 0;
1811
6.33k
                ssl_state->curr_connp->hs_buffer_offset = 0;
1812
68.3k
            } else {
1813
68.3k
                SCLogDebug("partial data");
1814
68.3k
            }
1815
1816
74.6k
            input += add;
1817
74.6k
            input_len -= add;
1818
74.6k
            SCLogDebug("input_len %u", input_len);
1819
74.6k
            SSLParserHSReset(ssl_state->curr_connp);
1820
74.6k
            continue;
1821
74.6k
        }
1822
1823
242k
        SCLogDebug("bytes_processed %u", ssl_state->curr_connp->bytes_processed);
1824
242k
        SCLogDebug("input %p input_len %u", input, input_len);
1825
1826
242k
        if (input_len < 4) {
1827
1.08k
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSL_RECORD);
1828
1.08k
            SCReturnInt(-1);
1829
1.08k
        }
1830
1831
241k
        ssl_state->curr_connp->handshake_type = input[0];
1832
241k
        ssl_state->curr_connp->message_length = input[1] << 16 | input[2] << 8 | input[3];
1833
241k
        SCLogDebug("handshake_type %u message len %u input %p input_len %u",
1834
241k
                ssl_state->curr_connp->handshake_type, ssl_state->curr_connp->message_length, input,
1835
241k
                input_len);
1836
241k
        input += 4;
1837
241k
        input_len -= 4;
1838
1839
241k
        const uint32_t record_len = ssl_state->curr_connp->message_length;
1840
        /* see if we support this type. We check here to not use the fragment
1841
         * handling on things we don't support. */
1842
241k
        const bool supported_type = SupportedHandshakeType(ssl_state->curr_connp->handshake_type);
1843
241k
        SCLogDebug("supported_type %s handshake_type %u/%02x", supported_type ? "true" : "false",
1844
241k
                ssl_state->curr_connp->handshake_type, ssl_state->curr_connp->handshake_type);
1845
241k
        if (!supported_type) {
1846
58.3k
            uint32_t avail_record_len = MIN(input_len, record_len);
1847
58.3k
            input += avail_record_len;
1848
58.3k
            input_len -= avail_record_len;
1849
1850
58.3k
            SSLParserHSReset(ssl_state->curr_connp);
1851
1852
58.3k
            if ((direction && (ssl_state->flags & SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC)) ||
1853
57.4k
                    (!direction && (ssl_state->flags & SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC))) {
1854
                // after Change Cipher Spec we get Encrypted Handshake Messages
1855
56.2k
            } else {
1856
56.2k
                SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE);
1857
56.2k
            }
1858
58.3k
            continue;
1859
58.3k
        }
1860
1861
        /* if the message length exceeds our input_len, we have a tls fragment. */
1862
183k
        if (record_len > input_len) {
1863
8.33k
            const uint32_t avail = input_len;
1864
8.33k
            const uint32_t size = avail + (4096 - (avail % 4096));
1865
8.33k
            SCLogDebug("initial buffer size %u, based on input %u", size, avail);
1866
8.33k
            ssl_state->curr_connp->hs_buffer = SCCalloc(1, size);
1867
8.33k
            if (ssl_state->curr_connp->hs_buffer == NULL) {
1868
0
                return -1;
1869
0
            }
1870
8.33k
            ssl_state->curr_connp->hs_buffer_size = size;
1871
8.33k
            ssl_state->curr_connp->hs_buffer_message_size = record_len;
1872
8.33k
            ssl_state->curr_connp->hs_buffer_message_type = ssl_state->curr_connp->handshake_type;
1873
1874
8.33k
            if (input_len > 0) {
1875
6.48k
                if (SafeMemcpy(ssl_state->curr_connp->hs_buffer, 0,
1876
6.48k
                            ssl_state->curr_connp->hs_buffer_size, input, 0, input_len,
1877
6.48k
                            input_len) != 0) {
1878
0
                    return -1;
1879
0
                }
1880
6.48k
                ssl_state->curr_connp->hs_buffer_offset = input_len;
1881
6.48k
            }
1882
8.33k
            SCLogDebug("opened record buffer %p size %u offset %u type %u msg_size %u",
1883
8.33k
                    ssl_state->curr_connp->hs_buffer, ssl_state->curr_connp->hs_buffer_size,
1884
8.33k
                    ssl_state->curr_connp->hs_buffer_offset,
1885
8.33k
                    ssl_state->curr_connp->hs_buffer_message_type,
1886
8.33k
                    ssl_state->curr_connp->hs_buffer_message_size);
1887
8.33k
            input += input_len;
1888
8.33k
            SSLParserHSReset(ssl_state->curr_connp);
1889
8.33k
            return (input - initial_input);
1890
1891
174k
        } else {
1892
            /* full record, parse it now */
1893
174k
            int retval = SSLv3ParseHandshakeType(
1894
174k
                    ssl_state, input, ssl_state->curr_connp->message_length, direction);
1895
174k
            if (retval < 0 || retval > (int)input_len) {
1896
0
                DEBUG_VALIDATE_BUG_ON(retval > (int)input_len);
1897
0
                return (retval);
1898
0
            }
1899
174k
            SCLogDebug("retval %d input_len %u", retval, input_len);
1900
174k
            input += retval;
1901
174k
            input_len -= retval;
1902
1903
174k
            SSLParserHSReset(ssl_state->curr_connp);
1904
174k
        }
1905
174k
        SCLogDebug("input_len left %u", input_len);
1906
174k
    }
1907
215k
    return (input - initial_input);
1908
224k
}
1909
1910
/**
1911
 * \internal
1912
 * \brief TLS Heartbeat parser (see RFC 6520)
1913
 *
1914
 * \param sslstate  Pointer to the SSL state.
1915
 * \param input     Pointer to the received input data.
1916
 * \param input_len Length in bytes of the received data.
1917
 * \param direction 1 toclient, 0 toserver
1918
 *
1919
 * \retval The number of bytes parsed on success, 0 if nothing parsed, -1 on failure.
1920
 */
1921
static int SSLv3ParseHeartbeatProtocol(SSLState *ssl_state, const uint8_t *input,
1922
                                       uint32_t input_len, uint8_t direction)
1923
15.0k
{
1924
15.0k
    uint8_t hb_type;
1925
15.0k
    uint16_t payload_len;
1926
15.0k
    uint32_t padding_len;
1927
1928
    /* expect at least 3 bytes: heartbeat type (1) + length (2) */
1929
15.0k
    if (input_len < 3) {
1930
5.62k
        return 0;
1931
5.62k
    }
1932
1933
9.41k
    hb_type = *input++;
1934
1935
9.41k
    if (!(ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC)) {
1936
3.65k
        if (!(hb_type == TLS_HB_REQUEST || hb_type == TLS_HB_RESPONSE)) {
1937
21
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_HEARTBEAT);
1938
21
            return -1;
1939
21
        }
1940
3.65k
    }
1941
1942
9.38k
    if ((ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) == 0) {
1943
4.80k
        ssl_state->flags |= SSL_AL_FLAG_HB_INFLIGHT;
1944
1945
4.80k
        if (direction) {
1946
2.42k
            SCLogDebug("HeartBeat Record type sent in the toclient direction!");
1947
2.42k
            ssl_state->flags |= SSL_AL_FLAG_HB_SERVER_INIT;
1948
2.42k
        } else {
1949
2.38k
            SCLogDebug("HeartBeat Record type sent in the toserver direction!");
1950
2.38k
            ssl_state->flags |= SSL_AL_FLAG_HB_CLIENT_INIT;
1951
2.38k
        }
1952
1953
        /* if we reach this point, then we can assume that the HB request
1954
           is encrypted. If so, let's set the HB record length */
1955
4.80k
        if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
1956
2.91k
            ssl_state->hb_record_len = ssl_state->curr_connp->record_length;
1957
2.91k
            SCLogDebug("Encrypted HeartBeat Request In-flight. Storing len %u",
1958
2.91k
                       ssl_state->hb_record_len);
1959
2.91k
            return (ssl_state->curr_connp->record_length - 3);
1960
2.91k
        }
1961
1962
1.89k
        payload_len = (uint16_t)(*input << 8) | *(input + 1);
1963
1964
        /* check that the requested payload length is really present in
1965
           the record (CVE-2014-0160) */
1966
1.89k
        if ((uint32_t)(payload_len+3) > ssl_state->curr_connp->record_length) {
1967
61
            SCLogDebug("We have a short record in HeartBeat Request");
1968
61
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_OVERFLOW_HEARTBEAT);
1969
61
            return -1;
1970
61
        }
1971
1972
        /* check the padding length. It must be at least 16 bytes
1973
           (RFC 6520, section 4) */
1974
1.83k
        padding_len = ssl_state->curr_connp->record_length - payload_len - 3;
1975
1.83k
        if (padding_len < 16) {
1976
16
            SCLogDebug("We have a short record in HeartBeat Request");
1977
16
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_HEARTBEAT);
1978
16
            return -1;
1979
16
        }
1980
1981
        /* we don't have the payload */
1982
1.81k
        if (input_len < payload_len + padding_len) {
1983
0
            return 0;
1984
0
        }
1985
1986
    /* OpenSSL still seems to discard multiple in-flight
1987
       heartbeats although some tools send multiple at once */
1988
4.58k
    } else if (direction == 1 && (ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) &&
1989
2.24k
            (ssl_state->flags & SSL_AL_FLAG_HB_SERVER_INIT)) {
1990
32
        SCLogDebug("Multiple in-flight server initiated HeartBeats");
1991
32
        SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_HEARTBEAT);
1992
32
        return -1;
1993
1994
4.54k
    } else if (direction == 0 && (ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) &&
1995
2.33k
            (ssl_state->flags & SSL_AL_FLAG_HB_CLIENT_INIT)) {
1996
18
        SCLogDebug("Multiple in-flight client initiated HeartBeats");
1997
18
        SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_HEARTBEAT);
1998
18
        return -1;
1999
2000
4.53k
    } else {
2001
        /* we have a HB record in the opposite direction of the request,
2002
           let's reset our flags */
2003
4.53k
        ssl_state->flags &= ~SSL_AL_FLAG_HB_INFLIGHT;
2004
4.53k
        ssl_state->flags &= ~SSL_AL_FLAG_HB_SERVER_INIT;
2005
4.53k
        ssl_state->flags &= ~SSL_AL_FLAG_HB_CLIENT_INIT;
2006
2007
        /* if we reach this point, then we can assume that the HB request
2008
           is encrypted. If so, let's set the HB record length */
2009
4.53k
        if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
2010
            /* check to see if the encrypted response is longer than the
2011
               encrypted request */
2012
2.81k
            if (ssl_state->hb_record_len > 0 && ssl_state->hb_record_len <
2013
2.80k
                    ssl_state->curr_connp->record_length) {
2014
11
                SCLogDebug("My heart is bleeding.. OpenSSL HeartBleed response (%u)",
2015
11
                        ssl_state->hb_record_len);
2016
11
                SSLSetEvent(ssl_state,
2017
11
                        TLS_DECODER_EVENT_DATALEAK_HEARTBEAT_MISMATCH);
2018
11
                ssl_state->hb_record_len = 0;
2019
11
                return -1;
2020
11
            }
2021
2.81k
        }
2022
2023
        /* reset the HB record length in case we have a legit HB followed
2024
           by a bad one */
2025
4.51k
        ssl_state->hb_record_len = 0;
2026
4.51k
    }
2027
2028
    /* skip the HeartBeat, 3 bytes were already parsed,
2029
       e.g |18 03 02| for TLS 1.2 */
2030
6.33k
    return (ssl_state->curr_connp->record_length - 3);
2031
9.38k
}
2032
2033
static int SSLv3ParseRecord(uint8_t direction, SSLState *ssl_state,
2034
                            const uint8_t *input, uint32_t input_len)
2035
1.03M
{
2036
1.03M
    const uint8_t *initial_input = input;
2037
2038
1.03M
    if (input_len == 0) {
2039
0
        return 0;
2040
0
    }
2041
2042
1.03M
    uint8_t skip_version = 0;
2043
2044
    /* Only set SSL/TLS version here if it has not already been set in
2045
       client/server hello. */
2046
1.03M
    if (direction == 0) {
2047
283k
        if ((ssl_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
2048
153k
                (ssl_state->client_connp.version != TLS_VERSION_UNKNOWN)) {
2049
147k
            skip_version = 1;
2050
147k
        }
2051
752k
    } else {
2052
752k
        if ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
2053
689k
                (ssl_state->server_connp.version != TLS_VERSION_UNKNOWN)) {
2054
683k
            skip_version = 1;
2055
683k
        }
2056
752k
    }
2057
2058
1.03M
    switch (ssl_state->curr_connp->bytes_processed) {
2059
557k
        case 0:
2060
557k
            if (input_len >= 5) {
2061
427k
                ssl_state->curr_connp->content_type = input[0];
2062
427k
                if (!skip_version) {
2063
196k
                    ssl_state->curr_connp->version = (uint16_t)(input[1] << 8) | input[2];
2064
196k
                }
2065
427k
                ssl_state->curr_connp->record_length = input[3] << 8;
2066
427k
                ssl_state->curr_connp->record_length |= input[4];
2067
427k
                ssl_state->curr_connp->bytes_processed += SSLV3_RECORD_HDR_LEN;
2068
427k
                return SSLV3_RECORD_HDR_LEN;
2069
427k
            } else {
2070
130k
                ssl_state->curr_connp->content_type = *(input++);
2071
130k
                if (--input_len == 0)
2072
121k
                    break;
2073
130k
            }
2074
2075
            /* fall through */
2076
129k
        case 1:
2077
129k
            if (!skip_version) {
2078
4.84k
                ssl_state->curr_connp->version = (uint16_t)(*(input++) << 8);
2079
124k
            } else {
2080
124k
                input++;
2081
124k
            }
2082
129k
            if (--input_len == 0)
2083
120k
                break;
2084
2085
            /* fall through */
2086
127k
        case 2:
2087
127k
            if (!skip_version) {
2088
3.05k
                ssl_state->curr_connp->version |= *(input++);
2089
124k
            } else {
2090
124k
                input++;
2091
124k
            }
2092
127k
            if (--input_len == 0)
2093
119k
                break;
2094
2095
            /* fall through */
2096
127k
        case 3:
2097
127k
            ssl_state->curr_connp->record_length = *(input++) << 8;
2098
127k
            if (--input_len == 0)
2099
120k
                break;
2100
2101
            /* fall through */
2102
126k
        case 4:
2103
126k
            ssl_state->curr_connp->record_length |= *(input++);
2104
126k
            if (--input_len == 0)
2105
118k
                break;
2106
2107
            /* fall through */
2108
1.03M
    }
2109
2110
608k
    ssl_state->curr_connp->bytes_processed += (input - initial_input);
2111
2112
608k
    return (input - initial_input);
2113
1.03M
}
2114
2115
static int SSLv2ParseRecord(uint8_t direction, SSLState *ssl_state,
2116
                            const uint8_t *input, uint32_t input_len)
2117
219k
{
2118
219k
    const uint8_t *initial_input = input;
2119
2120
219k
    if (input_len == 0) {
2121
0
        return 0;
2122
0
    }
2123
2124
219k
    if (ssl_state->curr_connp->record_lengths_length == 2) {
2125
185k
        switch (ssl_state->curr_connp->bytes_processed) {
2126
180k
            case 0:
2127
180k
                if (input_len >= ssl_state->curr_connp->record_lengths_length + 1) {
2128
177k
                    ssl_state->curr_connp->record_length = (0x7f & input[0]) << 8 | input[1];
2129
177k
                    ssl_state->curr_connp->content_type = input[2];
2130
177k
                    ssl_state->curr_connp->version = SSL_VERSION_2;
2131
177k
                    ssl_state->curr_connp->bytes_processed += 3;
2132
177k
                    return 3;
2133
177k
                } else {
2134
3.46k
                    ssl_state->curr_connp->record_length = (0x7f & *(input++)) << 8;
2135
3.46k
                    if (--input_len == 0)
2136
1.84k
                        break;
2137
3.46k
                }
2138
2139
                /* fall through */
2140
3.18k
            case 1:
2141
3.18k
                ssl_state->curr_connp->record_length |= *(input++);
2142
3.18k
                if (--input_len == 0)
2143
1.76k
                    break;
2144
2145
                /* fall through */
2146
4.05k
            case 2:
2147
4.05k
                ssl_state->curr_connp->content_type = *(input++);
2148
4.05k
                ssl_state->curr_connp->version = SSL_VERSION_2;
2149
4.05k
                if (--input_len == 0)
2150
1.04k
                    break;
2151
2152
                /* fall through */
2153
185k
        }
2154
2155
185k
    } else {
2156
34.4k
        switch (ssl_state->curr_connp->bytes_processed) {
2157
29.2k
            case 0:
2158
29.2k
                if (input_len >= ssl_state->curr_connp->record_lengths_length + 1) {
2159
24.4k
                    ssl_state->curr_connp->record_length = (0x3f & input[0]) << 8 | input[1];
2160
24.4k
                    ssl_state->curr_connp->content_type = input[3];
2161
24.4k
                    ssl_state->curr_connp->version = SSL_VERSION_2;
2162
24.4k
                    ssl_state->curr_connp->bytes_processed += 4;
2163
24.4k
                    return 4;
2164
24.4k
                } else {
2165
4.74k
                    ssl_state->curr_connp->record_length = (0x3f & *(input++)) << 8;
2166
4.74k
                    if (--input_len == 0)
2167
2.01k
                        break;
2168
4.74k
                }
2169
2170
                /* fall through */
2171
4.49k
            case 1:
2172
4.49k
                ssl_state->curr_connp->record_length |= *(input++);
2173
4.49k
                if (--input_len == 0)
2174
1.70k
                    break;
2175
2176
                /* fall through */
2177
4.66k
            case 2:
2178
                /* padding */
2179
4.66k
                input++;
2180
4.66k
                if (--input_len == 0)
2181
1.52k
                    break;
2182
2183
                /* fall through */
2184
4.67k
            case 3:
2185
4.67k
                ssl_state->curr_connp->content_type = *(input++);
2186
4.67k
                ssl_state->curr_connp->version = SSL_VERSION_2;
2187
4.67k
                if (--input_len == 0)
2188
449
                    break;
2189
2190
                /* fall through */
2191
34.4k
        }
2192
34.4k
    }
2193
2194
17.5k
    ssl_state->curr_connp->bytes_processed += (input - initial_input);
2195
2196
17.5k
    return (input - initial_input);
2197
219k
}
2198
2199
static struct SSLDecoderResult SSLv2Decode(uint8_t direction, SSLState *ssl_state,
2200
        AppLayerParserState *pstate, const uint8_t *input, uint32_t input_len,
2201
        const StreamSlice stream_slice)
2202
292k
{
2203
292k
    const uint8_t *initial_input = input;
2204
2205
292k
    if (ssl_state->curr_connp->bytes_processed == 0) {
2206
210k
        if (input[0] & 0x80) {
2207
180k
            ssl_state->curr_connp->record_lengths_length = 2;
2208
180k
        } else {
2209
29.2k
            ssl_state->curr_connp->record_lengths_length = 3;
2210
29.2k
        }
2211
2212
210k
        SCLogDebug("record start: ssl2.hdr frame");
2213
210k
        AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input,
2214
210k
                ssl_state->curr_connp->record_lengths_length + 1, direction, TLS_FRAME_SSLV2_HDR);
2215
210k
    }
2216
2217
292k
    SCLogDebug("direction %u ssl_state->curr_connp->record_lengths_length + 1 %u, "
2218
292k
               "ssl_state->curr_connp->bytes_processed %u",
2219
292k
            direction, ssl_state->curr_connp->record_lengths_length + 1,
2220
292k
            ssl_state->curr_connp->bytes_processed);
2221
    /* the +1 is because we read one extra byte inside SSLv2ParseRecord
2222
       to read the msg_type */
2223
292k
    if (ssl_state->curr_connp->bytes_processed <
2224
292k
            (ssl_state->curr_connp->record_lengths_length + 1)) {
2225
219k
        const int retval = SSLv2ParseRecord(direction, ssl_state, input, input_len);
2226
219k
        SCLogDebug("retval %d ssl_state->curr_connp->record_length %u", retval,
2227
219k
                ssl_state->curr_connp->record_length);
2228
219k
        if (retval < 0 || retval > (int)input_len) {
2229
0
            DEBUG_VALIDATE_BUG_ON(retval > (int)input_len);
2230
0
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSLV2_HEADER);
2231
0
            return SSL_DECODER_ERROR(-1);
2232
0
        }
2233
2234
219k
        AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input,
2235
219k
                ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length,
2236
219k
                direction, TLS_FRAME_SSLV2_PDU);
2237
219k
        SCLogDebug("record start: ssl2.pdu frame");
2238
2239
219k
        input += retval;
2240
219k
        input_len -= retval;
2241
219k
    }
2242
2243
    /* if we don't have the full record, we return incomplete */
2244
292k
    if (ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length >
2245
292k
            input_len + ssl_state->curr_connp->bytes_processed) {
2246
82.9k
        uint32_t needed = ssl_state->curr_connp->record_length;
2247
82.9k
        SCLogDebug("record len %u input_len %u parsed %u: need %u bytes more data",
2248
82.9k
                ssl_state->curr_connp->record_length, input_len, (uint32_t)(input - initial_input),
2249
82.9k
                needed);
2250
82.9k
        return SSL_DECODER_INCOMPLETE((input - initial_input), needed);
2251
82.9k
    }
2252
2253
209k
    if (input_len == 0) {
2254
2.45k
        return SSL_DECODER_OK((input - initial_input));
2255
2.45k
    }
2256
2257
    /* record_length should never be zero */
2258
206k
    if (ssl_state->curr_connp->record_length == 0) {
2259
113
        SCLogDebug("SSLv2 record length is zero");
2260
113
        SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSLV2_HEADER);
2261
113
        return SSL_DECODER_ERROR(-1);
2262
113
    }
2263
2264
    /* record_lengths_length should never be zero */
2265
206k
    if (ssl_state->curr_connp->record_lengths_length == 0) {
2266
6
        SCLogDebug("SSLv2 record lengths length is zero");
2267
6
        SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSLV2_HEADER);
2268
6
        return SSL_DECODER_ERROR(-1);
2269
6
    }
2270
2271
206k
    switch (ssl_state->curr_connp->content_type) {
2272
7.32k
        case SSLV2_MT_ERROR:
2273
7.32k
            SCLogDebug("SSLV2_MT_ERROR msg_type received. Error encountered "
2274
7.32k
                       "in establishing the sslv2 session, may be version");
2275
7.32k
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_ERROR_MSG_ENCOUNTERED);
2276
2277
7.32k
            break;
2278
2279
147k
        case SSLV2_MT_CLIENT_HELLO:
2280
147k
            if (input_len < 6) {
2281
15
                SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSL_RECORD);
2282
15
                return SSL_DECODER_ERROR(-1);
2283
15
            }
2284
2285
147k
            ssl_state->current_flags = SSL_AL_FLAG_STATE_CLIENT_HELLO;
2286
147k
            ssl_state->current_flags |= SSL_AL_FLAG_SSL_CLIENT_HS;
2287
2288
147k
            const uint16_t version = (uint16_t)(input[0] << 8) | input[1];
2289
147k
            SCLogDebug("SSLv2: version %04x", version);
2290
147k
            ssl_state->curr_connp->version = version;
2291
147k
            uint16_t session_id_length = (input[5]) | (uint16_t)(input[4] << 8);
2292
147k
            input += 6;
2293
147k
            input_len -= 6;
2294
147k
            ssl_state->curr_connp->bytes_processed += 6;
2295
147k
            if (session_id_length == 0) {
2296
132k
                ssl_state->current_flags |= SSL_AL_FLAG_SSL_NO_SESSION_ID;
2297
132k
            }
2298
147k
            break;
2299
2300
2.33k
        case SSLV2_MT_CLIENT_MASTER_KEY:
2301
2.33k
            if (!(ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS)) {
2302
1.11k
                SCLogDebug("Client hello is not seen before master key "
2303
1.11k
                           "message!");
2304
1.11k
            }
2305
2.33k
            ssl_state->current_flags = SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY;
2306
2307
2.33k
            break;
2308
2309
6.82k
        case SSLV2_MT_CLIENT_CERTIFICATE:
2310
6.82k
            if (direction == 1) {
2311
4.39k
                SCLogDebug("Incorrect SSL Record type sent in the toclient "
2312
4.39k
                           "direction!");
2313
4.39k
            } else {
2314
2.43k
                ssl_state->current_flags = SSL_AL_FLAG_STATE_CLIENT_KEYX;
2315
2.43k
            }
2316
2317
            /* fall through */
2318
8.69k
        case SSLV2_MT_SERVER_VERIFY:
2319
15.2k
        case SSLV2_MT_SERVER_FINISHED:
2320
15.2k
            if (direction == 0 &&
2321
8.20k
                    !(ssl_state->curr_connp->content_type &
2322
8.20k
                    SSLV2_MT_CLIENT_CERTIFICATE)) {
2323
5.76k
                SCLogDebug("Incorrect SSL Record type sent in the toserver "
2324
5.76k
                           "direction!");
2325
5.76k
            }
2326
2327
            /* fall through */
2328
18.8k
        case SSLV2_MT_CLIENT_FINISHED:
2329
20.4k
        case SSLV2_MT_REQUEST_CERTIFICATE:
2330
            /* both client hello and server hello must be seen */
2331
20.4k
            if ((ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) &&
2332
12.4k
                    (ssl_state->flags & SSL_AL_FLAG_SSL_SERVER_HS)) {
2333
2334
8.94k
                if (direction == 0) {
2335
4.17k
                    if (ssl_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) {
2336
1.67k
                        ssl_state->current_flags |= SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED;
2337
1.67k
                        SCLogDebug("SSLv2 client side has started the encryption");
2338
2.49k
                    } else if (ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY) {
2339
927
                        ssl_state->current_flags = SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED;
2340
927
                        SCLogDebug("SSLv2 client side has started the encryption");
2341
927
                    }
2342
4.77k
                } else {
2343
4.77k
                    ssl_state->current_flags = SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED;
2344
4.77k
                    SCLogDebug("SSLv2 Server side has started the encryption");
2345
4.77k
                }
2346
2347
8.94k
                if ((ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED) &&
2348
5.74k
                    (ssl_state->flags & SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED))
2349
3.63k
                {
2350
3.63k
                    if (ssl_config.encrypt_mode != SSL_CNF_ENC_HANDLE_FULL) {
2351
3.63k
                        AppLayerParserStateSetFlag(pstate,
2352
3.63k
                                APP_LAYER_PARSER_NO_INSPECTION);
2353
3.63k
                    }
2354
2355
3.63k
                    if (ssl_config.encrypt_mode == SSL_CNF_ENC_HANDLE_BYPASS) {
2356
0
                        AppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_REASSEMBLY);
2357
0
                        AppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_BYPASS_READY);
2358
0
                    }
2359
3.63k
                    SCLogDebug("SSLv2 No reassembly & inspection has been set");
2360
3.63k
                }
2361
8.94k
            }
2362
2363
20.4k
            break;
2364
2365
2.66k
        case SSLV2_MT_SERVER_HELLO:
2366
2.66k
            ssl_state->current_flags = SSL_AL_FLAG_STATE_SERVER_HELLO;
2367
2.66k
            ssl_state->current_flags |= SSL_AL_FLAG_SSL_SERVER_HS;
2368
2369
2.66k
            break;
2370
206k
    }
2371
2372
206k
    ssl_state->flags |= ssl_state->current_flags;
2373
2374
206k
    if (input_len + ssl_state->curr_connp->bytes_processed >=
2375
206k
            (ssl_state->curr_connp->record_length +
2376
206k
            ssl_state->curr_connp->record_lengths_length)) {
2377
2378
        /* looks like we have another record after this */
2379
206k
        uint32_t diff = ssl_state->curr_connp->record_length +
2380
206k
                ssl_state->curr_connp->record_lengths_length + -
2381
206k
                ssl_state->curr_connp->bytes_processed;
2382
206k
        input += diff;
2383
206k
        SSLParserReset(ssl_state);
2384
2385
        /* we still don't have the entire record for the one we are
2386
           currently parsing */
2387
206k
    } else {
2388
0
        input += input_len;
2389
0
        ssl_state->curr_connp->bytes_processed += input_len;
2390
0
    }
2391
206k
    return SSL_DECODER_OK((input - initial_input));
2392
206k
}
2393
2394
static struct SSLDecoderResult SSLv3Decode(uint8_t direction, SSLState *ssl_state,
2395
        AppLayerParserState *pstate, const uint8_t *input, const uint32_t input_len,
2396
        const StreamSlice stream_slice)
2397
630k
{
2398
630k
    uint32_t parsed = 0;
2399
630k
    uint32_t record_len; /* slice of input_len for the current record */
2400
630k
    const bool first_call = (ssl_state->curr_connp->bytes_processed == 0);
2401
2402
630k
    if (ssl_state->curr_connp->bytes_processed < SSLV3_RECORD_HDR_LEN) {
2403
590k
        const uint16_t prev_version = ssl_state->curr_connp->version;
2404
2405
590k
        int retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
2406
590k
        if (retval < 0 || retval > (int)input_len) {
2407
0
            DEBUG_VALIDATE_BUG_ON(retval > (int)input_len);
2408
0
            SCLogDebug("SSLv3ParseRecord returned %d", retval);
2409
0
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_TLS_HEADER);
2410
0
            return SSL_DECODER_ERROR(-1);
2411
0
        }
2412
590k
        parsed = retval;
2413
2414
590k
        SCLogDebug("%s input %p record_length %u", (direction == 0) ? "toserver" : "toclient",
2415
590k
                input, ssl_state->curr_connp->record_length);
2416
2417
        /* first the hdr frame at our first chance */
2418
590k
        if (first_call) {
2419
288k
            AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input, SSLV3_RECORD_HDR_LEN,
2420
288k
                    direction, TLS_FRAME_HDR);
2421
288k
        }
2422
2423
        /* parser is streaming for the initial header, then switches to incomplete
2424
         * API: so if we don't have the hdr yet, return consumed bytes and wait
2425
         * until we are called again with new data. */
2426
590k
        if (ssl_state->curr_connp->bytes_processed < SSLV3_RECORD_HDR_LEN) {
2427
304k
            SCLogDebug(
2428
304k
                    "incomplete header, return %u bytes consumed and wait for more data", parsed);
2429
304k
            return SSL_DECODER_OK(parsed);
2430
304k
        }
2431
2432
        /* pdu frame needs record length, so only create it when hdr fully parsed. */
2433
286k
        AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input,
2434
286k
                ssl_state->curr_connp->record_length + retval, direction, TLS_FRAME_PDU);
2435
286k
        record_len = MIN(input_len - parsed, ssl_state->curr_connp->record_length);
2436
286k
        SCLogDebug(
2437
286k
                "record_len %u (input_len %u, parsed %u, ssl_state->curr_connp->record_length %u)",
2438
286k
                record_len, input_len, parsed, ssl_state->curr_connp->record_length);
2439
2440
286k
        bool unknown_record = false;
2441
286k
        switch (ssl_state->curr_connp->content_type) {
2442
41.9k
            case SSLV3_CHANGE_CIPHER_SPEC:
2443
47.1k
            case SSLV3_ALERT_PROTOCOL:
2444
149k
            case SSLV3_HANDSHAKE_PROTOCOL:
2445
167k
            case SSLV3_APPLICATION_PROTOCOL:
2446
174k
            case SSLV3_HEARTBEAT_PROTOCOL:
2447
174k
                break;
2448
111k
            default:
2449
111k
                unknown_record = true;
2450
111k
                break;
2451
286k
        }
2452
2453
        /* unknown record type. For TLS 1.0, 1.1 and 1.2 this is ok. For the rest it is fatal. Based
2454
         * on Wireshark logic. */
2455
286k
        if (prev_version == TLS_VERSION_10 || prev_version == TLS_VERSION_11) {
2456
166k
            if (unknown_record) {
2457
110k
                SCLogDebug("unknown record, ignore it");
2458
110k
                SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_RECORD_TYPE);
2459
2460
110k
                ssl_state->curr_connp->bytes_processed = 0; // TODO review this reset logic
2461
110k
                ssl_state->curr_connp->content_type = 0;
2462
110k
                ssl_state->curr_connp->record_length = 0;
2463
                // restore last good version
2464
110k
                ssl_state->curr_connp->version = prev_version;
2465
110k
                return SSL_DECODER_OK(input_len); // consume everything
2466
110k
            }
2467
166k
        } else {
2468
120k
            if (unknown_record) {
2469
1.13k
                SCLogDebug("unknown record, fatal");
2470
1.13k
                SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_RECORD_TYPE);
2471
1.13k
                return SSL_DECODER_ERROR(-1);
2472
1.13k
            }
2473
120k
        }
2474
2475
        /* record_length should never be zero */
2476
174k
        if (ssl_state->curr_connp->record_length == 0) {
2477
24
            SCLogDebug("SSLv3 Record length is 0");
2478
24
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_RECORD_LENGTH);
2479
24
            return SSL_DECODER_ERROR(-1);
2480
24
        }
2481
2482
174k
        if (!TLSVersionValid(ssl_state->curr_connp->version)) {
2483
318
            SCLogDebug("ssl_state->curr_connp->version %04x", ssl_state->curr_connp->version);
2484
318
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_RECORD_VERSION);
2485
318
            return SSL_DECODER_ERROR(-1);
2486
318
        }
2487
2488
174k
        if (ssl_state->curr_connp->bytes_processed == SSLV3_RECORD_HDR_LEN &&
2489
174k
                ssl_state->curr_connp->record_length > SSLV3_RECORD_MAX_LEN) {
2490
124
            SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_RECORD_LENGTH);
2491
124
            return SSL_DECODER_ERROR(-1);
2492
124
        }
2493
174k
        DEBUG_VALIDATE_BUG_ON(ssl_state->curr_connp->bytes_processed > SSLV3_RECORD_HDR_LEN);
2494
174k
    } else {
2495
39.1k
        ValidateRecordState(ssl_state->curr_connp);
2496
2497
39.1k
        record_len = (ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN)- ssl_state->curr_connp->bytes_processed;
2498
39.1k
        record_len = MIN(input_len, record_len);
2499
39.1k
    }
2500
213k
    SCLogDebug("record length %u processed %u got %u",
2501
213k
            ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed, record_len);
2502
2503
    /* if we don't have the full record, we return incomplete */
2504
213k
    if (ssl_state->curr_connp->record_length > input_len - parsed) {
2505
        /* no need to use incomplete api buffering for application
2506
         * records that we'll not use anyway. */
2507
41.9k
        if (ssl_state->curr_connp->content_type == SSLV3_APPLICATION_PROTOCOL) {
2508
30.4k
            SCLogDebug("application record");
2509
30.4k
        } else {
2510
11.5k
            uint32_t needed = ssl_state->curr_connp->record_length;
2511
11.5k
            SCLogDebug("record len %u input_len %u parsed %u: need %u bytes more data",
2512
11.5k
                    ssl_state->curr_connp->record_length, input_len, parsed, needed);
2513
11.5k
            DEBUG_VALIDATE_BUG_ON(needed > SSLV3_RECORD_MAX_LEN);
2514
11.5k
            return SSL_DECODER_INCOMPLETE(parsed, needed);
2515
11.5k
        }
2516
41.9k
    }
2517
2518
201k
    if (record_len == 0) {
2519
608
        return SSL_DECODER_OK(parsed);
2520
608
    }
2521
2522
201k
    AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input + parsed,
2523
201k
            ssl_state->curr_connp->record_length, direction, TLS_FRAME_DATA);
2524
2525
201k
    switch (ssl_state->curr_connp->content_type) {
2526
        /* we don't need any data from these types */
2527
41.7k
        case SSLV3_CHANGE_CIPHER_SPEC:
2528
41.7k
            ssl_state->flags |= SSL_AL_FLAG_CHANGE_CIPHER_SPEC;
2529
2530
41.7k
            if (direction) {
2531
4.23k
                ssl_state->flags |= SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC;
2532
37.5k
            } else {
2533
37.5k
                ssl_state->flags |= SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC;
2534
37.5k
            }
2535
41.7k
            break;
2536
2537
5.05k
        case SSLV3_ALERT_PROTOCOL:
2538
5.05k
            AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input + parsed,
2539
5.05k
                    ssl_state->curr_connp->record_length, direction, TLS_FRAME_ALERT_DATA);
2540
5.05k
            break;
2541
2542
46.7k
        case SSLV3_APPLICATION_PROTOCOL:
2543
            /* In TLSv1.3 early data (0-RTT) could be sent before the
2544
               handshake is complete (rfc8446, section 2.3). We should
2545
               therefore not mark the handshake as done before we have
2546
               seen the ServerHello record. */
2547
46.7k
            if ((ssl_state->flags & SSL_AL_FLAG_EARLY_DATA) &&
2548
826
                    ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0))
2549
390
                break;
2550
2551
            /* if we see (encrypted) application data, then this means the
2552
               handshake must be done */
2553
46.3k
            ssl_state->flags |= SSL_AL_FLAG_HANDSHAKE_DONE;
2554
2555
46.3k
            if (ssl_config.encrypt_mode != SSL_CNF_ENC_HANDLE_FULL) {
2556
46.3k
                SCLogDebug("setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
2557
46.3k
                AppLayerParserStateSetFlag(pstate,
2558
46.3k
                        APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD);
2559
46.3k
            }
2560
2561
            /* Encrypted data, reassembly not asked, bypass asked, let's sacrifice
2562
             * heartbeat lke inspection to be able to be able to bypass the flow */
2563
46.3k
            if (ssl_config.encrypt_mode == SSL_CNF_ENC_HANDLE_BYPASS) {
2564
0
                SCLogDebug("setting APP_LAYER_PARSER_NO_REASSEMBLY");
2565
0
                AppLayerParserStateSetFlag(pstate,
2566
0
                        APP_LAYER_PARSER_NO_REASSEMBLY);
2567
0
                AppLayerParserStateSetFlag(pstate,
2568
0
                        APP_LAYER_PARSER_NO_INSPECTION);
2569
0
                AppLayerParserStateSetFlag(pstate,
2570
0
                        APP_LAYER_PARSER_BYPASS_READY);
2571
0
            }
2572
46.3k
            break;
2573
2574
100k
        case SSLV3_HANDSHAKE_PROTOCOL: {
2575
100k
            if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
2576
                /* In TLSv1.3, ChangeCipherSpec is only used for middlebox
2577
                   compatibility (rfc8446, appendix D.4). */
2578
                // Client hello flags is needed to have a valid version
2579
10.1k
                if ((ssl_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
2580
8.39k
                        (ssl_state->client_connp.version > TLS_VERSION_12) &&
2581
6.07k
                        ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0)) {
2582
                    /* do nothing */
2583
8.58k
                } else {
2584
                    // if we started parsing this, we must stop
2585
8.58k
                    break;
2586
8.58k
                }
2587
10.1k
            }
2588
2589
92.1k
            if (ssl_state->curr_connp->record_length < 4) {
2590
24
                SSLParserReset(ssl_state);
2591
24
                SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSL_RECORD);
2592
24
                SCLogDebug("record len < 4 => %u", ssl_state->curr_connp->record_length);
2593
24
                return SSL_DECODER_ERROR(-1);
2594
24
            }
2595
2596
92.0k
            int retval = SSLv3ParseHandshakeProtocol(ssl_state, input + parsed,
2597
92.0k
                                                     record_len, direction);
2598
92.0k
            SCLogDebug("retval %d", retval);
2599
92.0k
            if (retval < 0 || retval > (int)record_len) {
2600
441
                DEBUG_VALIDATE_BUG_ON(retval > (int)record_len);
2601
441
                SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE);
2602
441
                SCLogDebug("SSLv3ParseHandshakeProtocol returned %d", retval);
2603
441
                return SSL_DECODER_ERROR(-1);
2604
441
            }
2605
91.6k
            ValidateRecordState(ssl_state->curr_connp);
2606
91.6k
            break;
2607
91.6k
        }
2608
6.91k
        case SSLV3_HEARTBEAT_PROTOCOL: {
2609
6.91k
            AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input + parsed,
2610
6.91k
                    ssl_state->curr_connp->record_length, direction, TLS_FRAME_HB_DATA);
2611
6.91k
            int retval = SSLv3ParseHeartbeatProtocol(ssl_state, input + parsed,
2612
6.91k
                                                 record_len, direction);
2613
6.91k
            if (retval < 0) {
2614
82
                SCLogDebug("SSLv3ParseHeartbeatProtocol returned %d", retval);
2615
82
                return SSL_DECODER_ERROR(-1);
2616
82
            }
2617
6.83k
            break;
2618
6.91k
        }
2619
6.83k
        default:
2620
            // should be unreachable now that we check after header parsing
2621
0
            DEBUG_VALIDATE_BUG_ON(1);
2622
0
            SCLogDebug("unsupported record type");
2623
0
            return SSL_DECODER_ERROR(-1);
2624
201k
    }
2625
2626
200k
    parsed += record_len;
2627
200k
    ssl_state->curr_connp->bytes_processed += record_len;
2628
2629
200k
    if (ssl_state->curr_connp->bytes_processed >=
2630
200k
            ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN) {
2631
171k
        SCLogDebug("record complete, trigger RAW");
2632
171k
        AppLayerParserTriggerRawStreamReassembly(
2633
171k
                ssl_state->f, direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
2634
171k
        SSLParserReset(ssl_state);
2635
171k
        ValidateRecordState(ssl_state->curr_connp);
2636
171k
        return SSL_DECODER_OK(parsed);
2637
2638
171k
    } else {
2639
        /* we still don't have the entire record for the one we are
2640
           currently parsing */
2641
28.7k
        ValidateRecordState(ssl_state->curr_connp);
2642
28.7k
        return SSL_DECODER_OK(parsed);
2643
28.7k
    }
2644
200k
}
2645
2646
/**
2647
 * \internal
2648
 * \brief SSLv2, SSLv23, SSLv3, TLSv1.1, TLSv1.2, TLSv1.3 parser.
2649
 *
2650
 *        On parsing error, this should be the only function that should reset
2651
 *        the parser state, to avoid multiple functions in the chain resetting
2652
 *        the parser state.
2653
 *
2654
 * \param direction 0 for toserver, 1 for toclient.
2655
 * \param alstate   Pointer to the state.
2656
 * \param pstate    Application layer parser state for this session.
2657
 * \param output    Pointer to the list of parsed output elements.
2658
 *
2659
 * \todo On reaching an inconsistent state, check if the input has
2660
 *  another new record, instead of just returning after the reset
2661
 *
2662
 * \retval >=0 On success.
2663
 */
2664
static AppLayerResult SSLDecode(Flow *f, uint8_t direction, void *alstate,
2665
        AppLayerParserState *pstate, StreamSlice stream_slice)
2666
546k
{
2667
546k
    SSLState *ssl_state = (SSLState *)alstate;
2668
546k
    ssl_state->tx_data.updated_tc = true;
2669
546k
    ssl_state->tx_data.updated_ts = true;
2670
546k
    uint32_t counter = 0;
2671
546k
    ssl_state->f = f;
2672
546k
    const uint8_t *input = StreamSliceGetData(&stream_slice);
2673
546k
    const uint8_t *init_input = input;
2674
546k
    int32_t input_len = (int32_t)StreamSliceGetDataLen(&stream_slice);
2675
2676
546k
    if ((input == NULL || input_len == 0) &&
2677
1.00k
            ((direction == 0 && AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS)) ||
2678
424
                    (direction == 1 &&
2679
1.00k
                            AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC)))) {
2680
        /* flag session as finished if APP_LAYER_PARSER_EOF is set */
2681
1.00k
        ssl_state->flags |= SSL_AL_FLAG_STATE_FINISHED;
2682
1.00k
        SCReturnStruct(APP_LAYER_OK);
2683
545k
    } else if (input == NULL || input_len == 0) {
2684
0
        SCReturnStruct(APP_LAYER_ERROR);
2685
0
    }
2686
2687
545k
    if (direction == 0)
2688
102k
        ssl_state->curr_connp = &ssl_state->client_connp;
2689
443k
    else
2690
443k
        ssl_state->curr_connp = &ssl_state->server_connp;
2691
2692
    /* If entering on a new record, reset the current flags. */
2693
545k
    if (ssl_state->curr_connp->bytes_processed == 0) {
2694
160k
        ssl_state->current_flags = 0;
2695
160k
    }
2696
2697
    /* if we have more than one record */
2698
545k
    uint32_t max_records = MAX((input_len / SSL_RECORD_MINIMUM_LENGTH),1);
2699
1.20M
    while (input_len > 0) {
2700
711k
        if (counter > max_records) {
2701
173
            SCLogDebug("Looks like we have looped quite a bit. Reset state "
2702
173
                       "and get out of here");
2703
173
            SSLParserReset(ssl_state);
2704
173
            SSLSetEvent(ssl_state,
2705
173
                        TLS_DECODER_EVENT_TOO_MANY_RECORDS_IN_PACKET);
2706
173
            return APP_LAYER_ERROR;
2707
173
        }
2708
2709
        /* ssl_state->bytes_processed is zero for a fresh record or
2710
           positive to indicate a record currently being parsed */
2711
2712
711k
        if (ssl_state->curr_connp->bytes_processed == 0) {
2713
327k
            if ((input[0] & 0x80) || (input[0] & 0x40)) {
2714
                /* only SSLv2, has one of the top 2 bits set */
2715
38.2k
                ssl_state->curr_connp->version = SSL_VERSION_2;
2716
38.2k
                SCLogDebug("SSLv2 detected");
2717
288k
            } else if (ssl_state->curr_connp->version == SSL_VERSION_2) {
2718
3.71k
                ssl_state->curr_connp->version = TLS_VERSION_UNKNOWN;
2719
3.71k
                SCLogDebug("SSL/TLS version reset");
2720
3.71k
            }
2721
327k
        }
2722
711k
        SCLogDebug("record %u: bytes_processed %u, version %02X, input_len %u", counter,
2723
711k
                ssl_state->curr_connp->bytes_processed, ssl_state->curr_connp->version, input_len);
2724
2725
711k
        if (ssl_state->curr_connp->version == SSL_VERSION_2) {
2726
81.6k
            if (ssl_state->curr_connp->bytes_processed == 0) {
2727
38.2k
                SCLogDebug("New SSLv2 record parsing");
2728
43.3k
            } else {
2729
43.3k
                SCLogDebug("Continuing parsing SSLv2 record");
2730
43.3k
            }
2731
81.6k
            struct SSLDecoderResult r =
2732
81.6k
                    SSLv2Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2733
81.6k
            if (r.retval < 0 || r.retval > input_len) {
2734
65
                DEBUG_VALIDATE_BUG_ON(r.retval > input_len);
2735
65
                SCLogDebug("Error parsing SSLv2. Resetting parser "
2736
65
                           "state. Let's get outta here");
2737
65
                SSLParserReset(ssl_state);
2738
65
                SSLSetEvent(ssl_state,
2739
65
                        TLS_DECODER_EVENT_INVALID_SSL_RECORD);
2740
65
                return APP_LAYER_ERROR;
2741
81.5k
            } else if (r.needed) {
2742
41.1k
                input += r.retval;
2743
41.1k
                SCLogDebug("returning consumed %" PRIuMAX " needed %u",
2744
41.1k
                        (uintmax_t)(input - init_input), r.needed);
2745
41.1k
                SCReturnStruct(APP_LAYER_INCOMPLETE(input - init_input, r.needed));
2746
41.1k
            }
2747
40.3k
            input_len -= r.retval;
2748
40.3k
            input += r.retval;
2749
40.3k
            SCLogDebug("SSLv2 decoder consumed %d bytes: %u left", r.retval, input_len);
2750
630k
        } else {
2751
630k
            if (ssl_state->curr_connp->bytes_processed == 0) {
2752
288k
                SCLogDebug("New TLS record: record_length %u",
2753
288k
                        ssl_state->curr_connp->record_length);
2754
341k
            } else {
2755
341k
                SCLogDebug("Continuing parsing TLS record: record_length %u, bytes_processed %u",
2756
341k
                        ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed);
2757
341k
            }
2758
630k
            struct SSLDecoderResult r =
2759
630k
                    SSLv3Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2760
630k
            if (r.retval < 0 || r.retval > input_len) {
2761
2.14k
                DEBUG_VALIDATE_BUG_ON(r.retval > input_len);
2762
2.14k
                SCLogDebug("Error parsing TLS. Resetting parser "
2763
2.14k
                           "state.  Let's get outta here");
2764
2.14k
                SSLParserReset(ssl_state);
2765
2.14k
                return APP_LAYER_ERROR;
2766
627k
            } else if (r.needed) {
2767
11.5k
                input += r.retval;
2768
11.5k
                SCLogDebug("returning consumed %" PRIuMAX " needed %u",
2769
11.5k
                        (uintmax_t)(input - init_input), r.needed);
2770
11.5k
                SCReturnStruct(APP_LAYER_INCOMPLETE(input - init_input, r.needed));
2771
11.5k
            }
2772
616k
            input_len -= r.retval;
2773
616k
            input += r.retval;
2774
616k
            SCLogDebug("TLS decoder consumed %d bytes: %u left", r.retval, input_len);
2775
2776
616k
            if (ssl_state->curr_connp->bytes_processed == SSLV3_RECORD_HDR_LEN
2777
608
                    && ssl_state->curr_connp->record_length == 0) {
2778
0
                SCLogDebug("TLS empty record");
2779
                /* empty record */
2780
0
                SSLParserReset(ssl_state);
2781
0
            }
2782
616k
        }
2783
656k
        counter++;
2784
656k
    } /* while (input_len) */
2785
2786
    /* mark handshake as done if we have subject and issuer */
2787
490k
    if ((ssl_state->flags & SSL_AL_FLAG_NEED_CLIENT_CERT) &&
2788
453
            ssl_state->client_connp.cert0_subject && ssl_state->client_connp.cert0_issuerdn) {
2789
0
        SCLogDebug("SSL_AL_FLAG_HANDSHAKE_DONE");
2790
0
        ssl_state->flags |= SSL_AL_FLAG_HANDSHAKE_DONE;
2791
490k
    } else if ((ssl_state->flags & SSL_AL_FLAG_NEED_CLIENT_CERT) == 0 &&
2792
489k
               ssl_state->server_connp.cert0_subject && ssl_state->server_connp.cert0_issuerdn) {
2793
8.47k
        SCLogDebug("SSL_AL_FLAG_HANDSHAKE_DONE");
2794
8.47k
        ssl_state->flags |= SSL_AL_FLAG_HANDSHAKE_DONE;
2795
8.47k
    }
2796
2797
    /* flag session as finished if APP_LAYER_PARSER_EOF is set */
2798
490k
    if (AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS) &&
2799
4.96k
        AppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC)) {
2800
30
        SCLogDebug("SSL_AL_FLAG_STATE_FINISHED");
2801
30
        ssl_state->flags |= SSL_AL_FLAG_STATE_FINISHED;
2802
30
    }
2803
2804
490k
    return APP_LAYER_OK;
2805
545k
}
2806
2807
static AppLayerResult SSLParseClientRecord(Flow *f, void *alstate, AppLayerParserState *pstate,
2808
        StreamSlice stream_slice, void *local_data)
2809
186k
{
2810
186k
    return SSLDecode(f, 0 /* toserver */, alstate, pstate, stream_slice);
2811
186k
}
2812
2813
static AppLayerResult SSLParseServerRecord(Flow *f, void *alstate, AppLayerParserState *pstate,
2814
        StreamSlice stream_slice, void *local_data)
2815
742k
{
2816
742k
    return SSLDecode(f, 1 /* toclient */, alstate, pstate, stream_slice);
2817
742k
}
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
33.1k
{
2825
33.1k
    SSLState *ssl_state = SCMalloc(sizeof(SSLState));
2826
33.1k
    if (unlikely(ssl_state == NULL))
2827
0
        return NULL;
2828
33.1k
    memset(ssl_state, 0, sizeof(SSLState));
2829
33.1k
    ssl_state->client_connp.cert_log_flag = 0;
2830
33.1k
    ssl_state->server_connp.cert_log_flag = 0;
2831
33.1k
    memset(ssl_state->client_connp.random, 0, TLS_RANDOM_LEN);
2832
33.1k
    memset(ssl_state->server_connp.random, 0, TLS_RANDOM_LEN);
2833
33.1k
    TAILQ_INIT(&ssl_state->server_connp.certs);
2834
33.1k
    TAILQ_INIT(&ssl_state->client_connp.certs);
2835
2836
33.1k
    return (void *)ssl_state;
2837
33.1k
}
2838
2839
/**
2840
 * \internal
2841
 * \brief Function to free the SSL state memory.
2842
 */
2843
static void SSLStateFree(void *p)
2844
16.5k
{
2845
16.5k
    SSLState *ssl_state = (SSLState *)p;
2846
16.5k
    SSLCertsChain *item;
2847
2848
16.5k
    if (ssl_state->client_connp.cert0_subject)
2849
264
        rs_cstring_free(ssl_state->client_connp.cert0_subject);
2850
16.5k
    if (ssl_state->client_connp.cert0_issuerdn)
2851
254
        rs_cstring_free(ssl_state->client_connp.cert0_issuerdn);
2852
16.5k
    if (ssl_state->client_connp.cert0_serial)
2853
254
        rs_cstring_free(ssl_state->client_connp.cert0_serial);
2854
16.5k
    if (ssl_state->client_connp.cert0_fingerprint)
2855
254
        SCFree(ssl_state->client_connp.cert0_fingerprint);
2856
16.5k
    if (ssl_state->client_connp.sni)
2857
6.84k
        SCFree(ssl_state->client_connp.sni);
2858
16.5k
    if (ssl_state->client_connp.session_id)
2859
6.99k
        SCFree(ssl_state->client_connp.session_id);
2860
16.5k
    if (ssl_state->client_connp.hs_buffer)
2861
725
        SCFree(ssl_state->client_connp.hs_buffer);
2862
2863
16.5k
    if (ssl_state->server_connp.cert0_subject)
2864
2.04k
        rs_cstring_free(ssl_state->server_connp.cert0_subject);
2865
16.5k
    if (ssl_state->server_connp.cert0_issuerdn)
2866
2.03k
        rs_cstring_free(ssl_state->server_connp.cert0_issuerdn);
2867
16.5k
    if (ssl_state->server_connp.cert0_serial)
2868
2.03k
        rs_cstring_free(ssl_state->server_connp.cert0_serial);
2869
16.5k
    if (ssl_state->server_connp.cert0_fingerprint)
2870
2.03k
        SCFree(ssl_state->server_connp.cert0_fingerprint);
2871
16.5k
    if (ssl_state->server_connp.sni)
2872
52
        SCFree(ssl_state->server_connp.sni);
2873
16.5k
    if (ssl_state->server_connp.session_id)
2874
3.15k
        SCFree(ssl_state->server_connp.session_id);
2875
2876
16.5k
    if (ssl_state->client_connp.ja4)
2877
7.38k
        SCJA4Free(ssl_state->client_connp.ja4);
2878
16.5k
    if (ssl_state->client_connp.ja3_str)
2879
8.58k
        Ja3BufferFree(&ssl_state->client_connp.ja3_str);
2880
16.5k
    if (ssl_state->client_connp.ja3_hash)
2881
6.38k
        SCFree(ssl_state->client_connp.ja3_hash);
2882
16.5k
    if (ssl_state->server_connp.ja3_str)
2883
4.63k
        Ja3BufferFree(&ssl_state->server_connp.ja3_str);
2884
16.5k
    if (ssl_state->server_connp.ja3_hash)
2885
4.46k
        SCFree(ssl_state->server_connp.ja3_hash);
2886
16.5k
    if (ssl_state->server_connp.hs_buffer)
2887
259
        SCFree(ssl_state->server_connp.hs_buffer);
2888
2889
16.5k
    AppLayerDecoderEventsFreeEvents(&ssl_state->tx_data.events);
2890
2891
16.5k
    if (ssl_state->tx_data.de_state != NULL) {
2892
1.21k
        DetectEngineStateFree(ssl_state->tx_data.de_state);
2893
1.21k
    }
2894
2895
    /* Free certificate chain */
2896
16.5k
    if (ssl_state->server_connp.certs_buffer)
2897
2.59k
        SCFree(ssl_state->server_connp.certs_buffer);
2898
21.7k
    while ((item = TAILQ_FIRST(&ssl_state->server_connp.certs))) {
2899
5.19k
        TAILQ_REMOVE(&ssl_state->server_connp.certs, item, next);
2900
5.19k
        SCFree(item);
2901
5.19k
    }
2902
16.5k
    TAILQ_INIT(&ssl_state->server_connp.certs);
2903
    /* Free certificate chain */
2904
16.5k
    if (ssl_state->client_connp.certs_buffer)
2905
3.13k
        SCFree(ssl_state->client_connp.certs_buffer);
2906
17.2k
    while ((item = TAILQ_FIRST(&ssl_state->client_connp.certs))) {
2907
633
        TAILQ_REMOVE(&ssl_state->client_connp.certs, item, next);
2908
633
        SCFree(item);
2909
633
    }
2910
16.5k
    TAILQ_INIT(&ssl_state->client_connp.certs);
2911
2912
16.5k
    SCFree(ssl_state);
2913
2914
16.5k
    return;
2915
16.5k
}
2916
2917
static void SSLStateTransactionFree(void *state, uint64_t tx_id)
2918
655
{
2919
    /* do nothing */
2920
655
}
2921
2922
static AppProto SSLProbingParser(Flow *f, uint8_t direction,
2923
        const uint8_t *input, uint32_t ilen, uint8_t *rdir)
2924
23.7k
{
2925
    /* probably a rst/fin sending an eof */
2926
23.7k
    if (ilen < 3)
2927
8.66k
        return ALPROTO_UNKNOWN;
2928
2929
    /* for now just the 3 byte header ones */
2930
    /* \todo Detect the 2 byte ones */
2931
15.0k
    if ((input[0] & 0x80) && (input[2] == 0x01)) {
2932
1.75k
        return ALPROTO_TLS;
2933
1.75k
    }
2934
2935
13.2k
    return ALPROTO_FAILED;
2936
15.0k
}
2937
2938
static int SSLStateGetFrameIdByName(const char *frame_name)
2939
825
{
2940
825
    int id = SCMapEnumNameToValue(frame_name, tls_frame_table);
2941
825
    if (id < 0) {
2942
228
        return -1;
2943
228
    }
2944
597
    return id;
2945
825
}
2946
2947
static const char *SSLStateGetFrameNameById(const uint8_t frame_id)
2948
193
{
2949
193
    const char *name = SCMapEnumValueToName(frame_id, tls_frame_table);
2950
193
    return name;
2951
193
}
2952
2953
static int SSLStateGetEventInfo(const char *event_name,
2954
                         int *event_id, AppLayerEventType *event_type)
2955
22.4k
{
2956
22.4k
    *event_id = SCMapEnumNameToValue(event_name, tls_decoder_event_table);
2957
22.4k
    if (*event_id == -1) {
2958
10.4k
        SCLogError("event \"%s\" not present in "
2959
10.4k
                   "ssl's enum map table.",
2960
10.4k
                event_name);
2961
        /* yes this is fatal */
2962
10.4k
        return -1;
2963
10.4k
    }
2964
2965
12.0k
    *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
2966
2967
12.0k
    return 0;
2968
22.4k
}
2969
2970
static int SSLStateGetEventInfoById(int event_id, const char **event_name,
2971
                                    AppLayerEventType *event_type)
2972
31.1k
{
2973
31.1k
    *event_name = SCMapEnumValueToName(event_id, tls_decoder_event_table);
2974
31.1k
    if (*event_name == NULL) {
2975
0
        SCLogError("event \"%d\" not present in "
2976
0
                   "ssl's enum map table.",
2977
0
                event_id);
2978
        /* yes this is fatal */
2979
0
        return -1;
2980
0
    }
2981
2982
31.1k
    *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
2983
2984
31.1k
    return 0;
2985
31.1k
}
2986
2987
static int SSLRegisterPatternsForProtocolDetection(void)
2988
75
{
2989
75
    if (AppLayerProtoDetectPMRegisterPatternCSwPP(IPPROTO_TCP, ALPROTO_TLS, "|01 00 02|", 5, 2,
2990
75
                STREAM_TOSERVER, SSLProbingParser, 0, 3) < 0) {
2991
0
        return -1;
2992
0
    }
2993
2994
    /** SSLv3 */
2995
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
2996
75
                                               "|01 03 00|", 3, 0, STREAM_TOSERVER) < 0)
2997
0
    {
2998
0
        return -1;
2999
0
    }
3000
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3001
75
                                               "|16 03 00|", 3, 0, STREAM_TOSERVER) < 0)
3002
0
    {
3003
0
        return -1;
3004
0
    }
3005
3006
    /** TLSv1 */
3007
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3008
75
                                               "|01 03 01|", 3, 0, STREAM_TOSERVER) < 0)
3009
0
    {
3010
0
        return -1;
3011
0
    }
3012
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3013
75
                                               "|16 03 01|", 3, 0, STREAM_TOSERVER) < 0)
3014
0
    {
3015
0
        return -1;
3016
0
    }
3017
3018
    /** TLSv1.1 */
3019
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3020
75
                                               "|01 03 02|", 3, 0, STREAM_TOSERVER) < 0)
3021
0
    {
3022
0
        return -1;
3023
0
    }
3024
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3025
75
                                               "|16 03 02|", 3, 0, STREAM_TOSERVER) < 0)
3026
0
    {
3027
0
        return -1;
3028
0
    }
3029
3030
    /** TLSv1.2 */
3031
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3032
75
                                               "|01 03 03|", 3, 0, STREAM_TOSERVER) < 0)
3033
0
    {
3034
0
        return -1;
3035
0
    }
3036
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3037
75
                                               "|16 03 03|", 3, 0, STREAM_TOSERVER) < 0)
3038
0
    {
3039
0
        return -1;
3040
0
    }
3041
3042
    /***** toclient direction *****/
3043
3044
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3045
75
                                               "|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
3046
0
    {
3047
0
        return -1;
3048
0
    }
3049
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3050
75
                                               "|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
3051
0
    {
3052
0
        return -1;
3053
0
    }
3054
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3055
75
                                               "|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
3056
0
    {
3057
0
        return -1;
3058
0
    }
3059
3060
    /** TLSv1 */
3061
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3062
75
                                               "|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
3063
0
    {
3064
0
        return -1;
3065
0
    }
3066
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3067
75
                                               "|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
3068
0
    {
3069
0
        return -1;
3070
0
    }
3071
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3072
75
                                               "|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
3073
0
    {
3074
0
        return -1;
3075
0
    }
3076
3077
    /** TLSv1.1 */
3078
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3079
75
                                               "|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
3080
0
    {
3081
0
        return -1;
3082
0
    }
3083
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3084
75
                                               "|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
3085
0
    {
3086
0
        return -1;
3087
0
    }
3088
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3089
75
                                               "|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
3090
0
    {
3091
0
        return -1;
3092
0
    }
3093
3094
    /** TLSv1.2 */
3095
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3096
75
                                               "|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
3097
0
    {
3098
0
        return -1;
3099
0
    }
3100
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3101
75
                                               "|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
3102
0
    {
3103
0
        return -1;
3104
0
    }
3105
75
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3106
75
                                               "|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
3107
0
    {
3108
0
        return -1;
3109
0
    }
3110
3111
    /* Subsection - SSLv2 style record by client, but informing the server
3112
     * the max version it supports.
3113
     * Updated by Anoop Saldanha.  Disabled it for now.  We'll get back to
3114
     * it after some tests */
3115
#if 0
3116
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3117
                                               "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
3118
    {
3119
        return -1;
3120
    }
3121
    if (AppLayerProtoDetectPMRegisterPatternCS(IPPROTO_TCP, ALPROTO_TLS,
3122
                                               "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
3123
    {
3124
        return -1;
3125
    }
3126
#endif
3127
3128
75
    return 0;
3129
75
}
3130
3131
#ifdef HAVE_JA3
3132
static void CheckJA3Enabled(void)
3133
75
{
3134
75
    const char *strval = NULL;
3135
    /* Check if we should generate JA3 fingerprints */
3136
75
    int enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
3137
75
    if (ConfGet("app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
3138
75
        enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
3139
75
    } else if (strcmp(strval, "auto") == 0) {
3140
0
        enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
3141
0
    } else if (ConfValIsFalse(strval)) {
3142
0
        enable_ja3 = 0;
3143
0
        ssl_config.disable_ja3 = true;
3144
0
    } else if (ConfValIsTrue(strval)) {
3145
0
        enable_ja3 = true;
3146
0
    }
3147
75
    SC_ATOMIC_SET(ssl_config.enable_ja3, enable_ja3);
3148
75
    if (!ssl_config.disable_ja3 && !g_disable_hashing) {
3149
        /* The feature is available, i.e. _could_ be activated by a rule or
3150
            even is enabled in the configuration. */
3151
75
        ProvidesFeature(FEATURE_JA3);
3152
75
    }
3153
75
}
3154
#endif /* HAVE_JA3 */
3155
3156
#ifdef HAVE_JA4
3157
static void CheckJA4Enabled(void)
3158
75
{
3159
75
    const char *strval = NULL;
3160
    /* Check if we should generate JA4 fingerprints */
3161
75
    int enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3162
75
    if (ConfGet("app-layer.protocols.tls.ja4-fingerprints", &strval) != 1) {
3163
75
        enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3164
75
    } else if (strcmp(strval, "auto") == 0) {
3165
0
        enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3166
0
    } else if (ConfValIsFalse(strval)) {
3167
0
        enable_ja4 = 0;
3168
0
        ssl_config.disable_ja4 = true;
3169
0
    } else if (ConfValIsTrue(strval)) {
3170
0
        enable_ja4 = true;
3171
0
    }
3172
75
    SC_ATOMIC_SET(ssl_config.enable_ja4, enable_ja4);
3173
75
    if (!ssl_config.disable_ja4 && !g_disable_hashing) {
3174
        /* The feature is available, i.e. _could_ be activated by a rule or
3175
            even is enabled in the configuration. */
3176
75
        ProvidesFeature(FEATURE_JA4);
3177
75
    }
3178
75
}
3179
#endif /* HAVE_JA4 */
3180
3181
/**
3182
 * \brief Function to register the SSL protocol parser and other functions
3183
 */
3184
void RegisterSSLParsers(void)
3185
34
{
3186
34
    const char *proto_name = "tls";
3187
3188
34
    SC_ATOMIC_INIT(ssl_config.enable_ja3);
3189
3190
    /** SSLv2  and SSLv23*/
3191
34
    if (AppLayerProtoDetectConfProtoDetectionEnabled("tcp", proto_name)) {
3192
34
        AppLayerProtoDetectRegisterProtocol(ALPROTO_TLS, proto_name);
3193
3194
34
        if (SSLRegisterPatternsForProtocolDetection() < 0)
3195
0
            return;
3196
3197
34
        if (RunmodeIsUnittests()) {
3198
0
            AppLayerProtoDetectPPRegister(IPPROTO_TCP,
3199
0
                                          "443",
3200
0
                                          ALPROTO_TLS,
3201
0
                                          0, 3,
3202
0
                                          STREAM_TOSERVER,
3203
0
                                          SSLProbingParser, NULL);
3204
34
        } else {
3205
34
            if (AppLayerProtoDetectPPParseConfPorts("tcp", IPPROTO_TCP,
3206
34
                                                    proto_name, ALPROTO_TLS,
3207
34
                                                    0, 3,
3208
34
                                                    SSLProbingParser, NULL) == 0) {
3209
34
                SCLogConfig("no TLS config found, "
3210
34
                            "enabling TLS detection on port 443.");
3211
34
                AppLayerProtoDetectPPRegister(IPPROTO_TCP,
3212
34
                                              "443",
3213
34
                                              ALPROTO_TLS,
3214
34
                                              0, 3,
3215
34
                                              STREAM_TOSERVER,
3216
34
                                              SSLProbingParser, NULL);
3217
34
            }
3218
34
        }
3219
34
    } else {
3220
0
        SCLogConfig("Protocol detection and parser disabled for %s protocol",
3221
0
                  proto_name);
3222
0
        return;
3223
0
    }
3224
3225
34
    if (AppLayerParserConfParserEnabled("tcp", proto_name)) {
3226
34
        AppLayerParserRegisterParser(IPPROTO_TCP, ALPROTO_TLS, STREAM_TOSERVER,
3227
34
                                     SSLParseClientRecord);
3228
3229
34
        AppLayerParserRegisterParser(IPPROTO_TCP, ALPROTO_TLS, STREAM_TOCLIENT,
3230
34
                                     SSLParseServerRecord);
3231
3232
34
        AppLayerParserRegisterGetFrameFuncs(
3233
34
                IPPROTO_TCP, ALPROTO_TLS, SSLStateGetFrameIdByName, SSLStateGetFrameNameById);
3234
34
        AppLayerParserRegisterGetEventInfo(IPPROTO_TCP, ALPROTO_TLS, SSLStateGetEventInfo);
3235
34
        AppLayerParserRegisterGetEventInfoById(IPPROTO_TCP, ALPROTO_TLS, SSLStateGetEventInfoById);
3236
3237
34
        AppLayerParserRegisterStateFuncs(IPPROTO_TCP, ALPROTO_TLS, SSLStateAlloc, SSLStateFree);
3238
3239
34
        AppLayerParserRegisterParserAcceptableDataDirection(IPPROTO_TCP, ALPROTO_TLS, STREAM_TOSERVER);
3240
3241
34
        AppLayerParserRegisterTxFreeFunc(IPPROTO_TCP, ALPROTO_TLS, SSLStateTransactionFree);
3242
3243
34
        AppLayerParserRegisterGetTx(IPPROTO_TCP, ALPROTO_TLS, SSLGetTx);
3244
34
        AppLayerParserRegisterTxDataFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetTxData);
3245
34
        AppLayerParserRegisterStateDataFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetStateData);
3246
3247
34
        AppLayerParserRegisterGetTxCnt(IPPROTO_TCP, ALPROTO_TLS, SSLGetTxCnt);
3248
3249
34
        AppLayerParserRegisterGetStateProgressFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetAlstateProgress);
3250
3251
34
        AppLayerParserRegisterStateProgressCompletionStatus(
3252
34
                ALPROTO_TLS, TLS_STATE_FINISHED, TLS_STATE_FINISHED);
3253
3254
34
        ConfNode *enc_handle = ConfGetNode("app-layer.protocols.tls.encryption-handling");
3255
34
        if (enc_handle != NULL && enc_handle->val != NULL) {
3256
0
            SCLogDebug("have app-layer.protocols.tls.encryption-handling = %s", enc_handle->val);
3257
0
            if (strcmp(enc_handle->val, "full") == 0) {
3258
0
                ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_FULL;
3259
0
            } else if (strcmp(enc_handle->val, "bypass") == 0) {
3260
0
                ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_BYPASS;
3261
0
            } else if (strcmp(enc_handle->val, "default") == 0) {
3262
0
                ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_DEFAULT;
3263
0
            } else {
3264
0
                ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_DEFAULT;
3265
0
            }
3266
34
        } else {
3267
            /* Get the value of no reassembly option from the config file */
3268
34
            if (ConfGetNode("app-layer.protocols.tls.no-reassemble") == NULL) {
3269
34
                int value = 0;
3270
34
                if (ConfGetBool("tls.no-reassemble", &value) == 1 && value == 1)
3271
0
                    ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_BYPASS;
3272
34
            } else {
3273
0
                int value = 0;
3274
0
                if (ConfGetBool("app-layer.protocols.tls.no-reassemble", &value) == 1 && value == 1)
3275
0
                    ssl_config.encrypt_mode = SSL_CNF_ENC_HANDLE_BYPASS;
3276
0
            }
3277
34
        }
3278
34
        SCLogDebug("ssl_config.encrypt_mode %u", ssl_config.encrypt_mode);
3279
3280
34
#ifdef HAVE_JA3
3281
34
        CheckJA3Enabled();
3282
34
#endif /* HAVE_JA3 */
3283
34
#ifdef HAVE_JA4
3284
34
        CheckJA4Enabled();
3285
34
#endif /* HAVE_JA4 */
3286
3287
34
        if (g_disable_hashing) {
3288
0
            if (SC_ATOMIC_GET(ssl_config.enable_ja3)) {
3289
0
                SCLogWarning("MD5 calculation has been disabled, disabling JA3");
3290
0
                SC_ATOMIC_SET(ssl_config.enable_ja3, 0);
3291
0
            }
3292
0
            if (SC_ATOMIC_GET(ssl_config.enable_ja4)) {
3293
0
                SCLogWarning("Hashing has been disabled, disabling JA4");
3294
0
                SC_ATOMIC_SET(ssl_config.enable_ja4, 0);
3295
0
            }
3296
34
        } else {
3297
34
            if (RunmodeIsUnittests()) {
3298
0
#ifdef HAVE_JA3
3299
0
                SC_ATOMIC_SET(ssl_config.enable_ja3, 1);
3300
0
#endif /* HAVE_JA3 */
3301
0
#ifdef HAVE_JA4
3302
0
                SC_ATOMIC_SET(ssl_config.enable_ja4, 1);
3303
0
#endif /* HAVE_JA4 */
3304
0
            }
3305
34
        }
3306
34
    } else {
3307
0
        SCLogConfig("Parsed disabled for %s protocol. Protocol detection"
3308
0
                  "still on.", proto_name);
3309
0
    }
3310
3311
34
    return;
3312
34
}
3313
3314
/**
3315
 * \brief if not explicitly disabled in config, enable ja3 support
3316
 *
3317
 * Implemented using atomic to allow rule reloads to do this at
3318
 * runtime.
3319
 */
3320
void SSLEnableJA3(void)
3321
13.9k
{
3322
13.9k
    if (g_disable_hashing || ssl_config.disable_ja3) {
3323
0
        return;
3324
0
    }
3325
13.9k
    if (SC_ATOMIC_GET(ssl_config.enable_ja3)) {
3326
13.9k
        return;
3327
13.9k
    }
3328
6
    SC_ATOMIC_SET(ssl_config.enable_ja3, 1);
3329
6
}
3330
3331
/**
3332
 * \brief if not explicitly disabled in config, enable ja4 support
3333
 *
3334
 * Implemented using atomic to allow rule reloads to do this at
3335
 * runtime.
3336
 */
3337
void SSLEnableJA4(void)
3338
3.89k
{
3339
3.89k
    if (g_disable_hashing || ssl_config.disable_ja4) {
3340
0
        return;
3341
0
    }
3342
3.89k
    if (SC_ATOMIC_GET(ssl_config.enable_ja4)) {
3343
3.88k
        return;
3344
3.88k
    }
3345
6
    SC_ATOMIC_SET(ssl_config.enable_ja4, 1);
3346
6
}
3347
3348
/**
3349
 * \brief return whether ja3 is effectively enabled
3350
 *
3351
 * This means that it either has been enabled explicitly or has been
3352
 * enabled by having loaded a rule while not being explicitly disabled.
3353
 *
3354
 * \retval true if enabled, false otherwise
3355
 */
3356
bool SSLJA3IsEnabled(void)
3357
13.9k
{
3358
13.9k
    return SC_ATOMIC_GET(ssl_config.enable_ja3);
3359
13.9k
}
3360
3361
/**
3362
 * \brief return whether ja4 is effectively enabled
3363
 *
3364
 * This means that it either has been enabled explicitly or has been
3365
 * enabled by having loaded a rule while not being explicitly disabled.
3366
 *
3367
 * \retval true if enabled, false otherwise
3368
 */
3369
bool SSLJA4IsEnabled(void)
3370
3.89k
{
3371
    return SC_ATOMIC_GET(ssl_config.enable_ja4);
3372
3.89k
}