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