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 
29 #include "app-layer-detect-proto.h"
30 #include "app-layer-protos.h"
31 #include "app-layer-parser.h"
32 #include "app-layer-frames.h"
33 #include "app-layer-events.h"
34 #include "app-layer-ssl.h"
35 
36 #include "conf.h"
37 
38 #include "feature.h"
39 
40 #include "util-debug.h"
41 #include "util-ja3.h"
42 #include "util-enum.h"
43 #include "util-validate.h"
44 #include "rust.h"
45 #include "suricata.h"
46 
47 static SCEnumCharMap tls_state_client_table[] = {
48  {
49  "client_started",
51  },
52  {
53  "client_hello",
55  },
56  {
57  "client_cert",
59  },
60  {
61  "client_data",
63  },
64  {
65  "client_finished",
67  },
68  { NULL, -1 },
69 };
70 
71 static SCEnumCharMap tls_state_server_table[] = {
72  {
73  "server_started",
75  },
76  {
77  "server_hello",
79  },
80  {
81  "server_cert",
83  },
84  {
85  "server_data",
87  },
88  {
89  "server_finished",
91  },
92  { NULL, -1 },
93 };
94 
96  {
97  "pdu",
99  },
100  {
101  "hdr",
103  },
104  {
105  "data",
107  },
108  {
109  "alert",
111  },
112  {
113  "heartbeat",
115  },
116  {
117  "ssl2.hdr",
119  },
120  {
121  "ssl2.pdu",
123  },
124  { NULL, -1 },
125 };
126 
128  /* TLS protocol messages */
129  { "INVALID_SSLV2_HEADER", TLS_DECODER_EVENT_INVALID_SSLV2_HEADER },
130  { "INVALID_TLS_HEADER", TLS_DECODER_EVENT_INVALID_TLS_HEADER },
131  { "INVALID_RECORD_VERSION", TLS_DECODER_EVENT_INVALID_RECORD_VERSION },
132  { "INVALID_RECORD_TYPE", TLS_DECODER_EVENT_INVALID_RECORD_TYPE },
133  { "INVALID_RECORD_LENGTH", TLS_DECODER_EVENT_INVALID_RECORD_LENGTH },
134  { "INVALID_HANDSHAKE_MESSAGE", TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE },
135  { "HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_HEARTBEAT },
136  { "INVALID_HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_INVALID_HEARTBEAT },
137  { "OVERFLOW_HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_OVERFLOW_HEARTBEAT },
138  { "DATALEAK_HEARTBEAT_MISMATCH", TLS_DECODER_EVENT_DATALEAK_HEARTBEAT_MISMATCH },
139  { "HANDSHAKE_INVALID_LENGTH", TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH },
140  { "MULTIPLE_SNI_EXTENSIONS", TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS },
141  { "INVALID_SNI_TYPE", TLS_DECODER_EVENT_INVALID_SNI_TYPE },
142  { "INVALID_SNI_LENGTH", TLS_DECODER_EVENT_INVALID_SNI_LENGTH },
143  { "TOO_MANY_RECORDS_IN_PACKET", TLS_DECODER_EVENT_TOO_MANY_RECORDS_IN_PACKET },
144  { "INVALID_ALERT_MESSAGE", TLS_DECODER_EVENT_INVALID_ALERT },
145  /* certificate decoding messages */
146  { "INVALID_CERTIFICATE", TLS_DECODER_EVENT_INVALID_CERTIFICATE },
147  { "CERTIFICATE_INVALID_LENGTH", TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH },
148  { "CERTIFICATE_INVALID_VERSION", TLS_DECODER_EVENT_CERTIFICATE_INVALID_VERSION },
149  { "CERTIFICATE_INVALID_SERIAL", TLS_DECODER_EVENT_CERTIFICATE_INVALID_SERIAL },
150  { "CERTIFICATE_INVALID_ALGORITHMIDENTIFIER",
152  { "CERTIFICATE_INVALID_X509NAME", TLS_DECODER_EVENT_CERTIFICATE_INVALID_X509NAME },
153  { "CERTIFICATE_INVALID_DATE", TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE },
154  { "CERTIFICATE_INVALID_EXTENSIONS", TLS_DECODER_EVENT_CERTIFICATE_INVALID_EXTENSIONS },
155  { "CERTIFICATE_INVALID_DER", TLS_DECODER_EVENT_CERTIFICATE_INVALID_DER },
156  { "CERTIFICATE_INVALID_SUBJECT", TLS_DECODER_EVENT_CERTIFICATE_INVALID_SUBJECT },
157  { "CERTIFICATE_INVALID_ISSUER", TLS_DECODER_EVENT_CERTIFICATE_INVALID_ISSUER },
158  { "CERTIFICATE_INVALID_VALIDITY", TLS_DECODER_EVENT_CERTIFICATE_INVALID_VALIDITY },
159  { "ERROR_MESSAGE_ENCOUNTERED", TLS_DECODER_EVENT_ERROR_MSG_ENCOUNTERED },
160  { "TOO_MANY_SUBJECT_ALTERNATIVE_NAMES", TLS_DECODER_EVENT_TOO_MANY_SUBJECT_ALTERNATIVE_NAMES },
161  /* used as a generic error event */
162  { "INVALID_SSL_RECORD", TLS_DECODER_EVENT_INVALID_SSL_RECORD },
163  { NULL, -1 },
164 };
165 
166 enum {
167  /* X.509 error codes, returned by decoder
168  * THESE CONSTANTS MUST MATCH rust/src/x509/mod.rs ! */
178 
179  /* error getting data */
183 };
184 
185 /* JA3 and JA4 fingerprints are disabled by default */
186 #define SSL_CONFIG_DEFAULT_JA3 0
187 #ifdef HAVE_JA4
188 #define SSL_CONFIG_DEFAULT_JA4 0
189 #endif
190 
192  SSL_CNF_ENC_HANDLE_TRACK_ONLY = 0, /**< disable raw content, continue tracking */
193  SSL_CNF_ENC_HANDLE_BYPASS = 1, /**< skip processing of flow, bypass if possible */
194  SSL_CNF_ENC_HANDLE_FULL = 2, /**< handle fully like any other proto */
195 };
196 
197 typedef struct SslConfig_ {
199  /** dynamic setting for ja3 and ja4: can be enabled on demand if not
200  * explicitly disabled. */
201  SC_ATOMIC_DECLARE(int, enable_ja3);
202  bool disable_ja3; /**< ja3 explicitly disabled. Don't enable on demand. */
203  SC_ATOMIC_DECLARE(int, enable_ja4);
204  bool disable_ja4; /**< ja4 explicitly disabled. Don't enable on demand. */
206 
208 
209 /* SSLv3 record types */
210 #define SSLV3_CHANGE_CIPHER_SPEC 20
211 #define SSLV3_ALERT_PROTOCOL 21
212 #define SSLV3_HANDSHAKE_PROTOCOL 22
213 #define SSLV3_APPLICATION_PROTOCOL 23
214 #define SSLV3_HEARTBEAT_PROTOCOL 24
215 
216 /* SSLv3 handshake protocol types */
217 #define SSLV3_HS_HELLO_REQUEST 0
218 #define SSLV3_HS_CLIENT_HELLO 1
219 #define SSLV3_HS_SERVER_HELLO 2
220 #define SSLV3_HS_NEW_SESSION_TICKET 4
221 #define SSLV3_HS_CERTIFICATE 11
222 #define SSLV3_HS_SERVER_KEY_EXCHANGE 12
223 #define SSLV3_HS_CERTIFICATE_REQUEST 13
224 #define SSLV3_HS_SERVER_HELLO_DONE 14
225 #define SSLV3_HS_CERTIFICATE_VERIFY 15
226 #define SSLV3_HS_CLIENT_KEY_EXCHANGE 16
227 #define SSLV3_HS_FINISHED 20
228 #define SSLV3_HS_CERTIFICATE_URL 21
229 #define SSLV3_HS_CERTIFICATE_STATUS 22
230 
231 /* SSLv2 protocol message types */
232 #define SSLV2_MT_ERROR 0
233 #define SSLV2_MT_CLIENT_HELLO 1
234 #define SSLV2_MT_CLIENT_MASTER_KEY 2
235 #define SSLV2_MT_CLIENT_FINISHED 3
236 #define SSLV2_MT_SERVER_HELLO 4
237 #define SSLV2_MT_SERVER_VERIFY 5
238 #define SSLV2_MT_SERVER_FINISHED 6
239 #define SSLV2_MT_REQUEST_CERTIFICATE 7
240 #define SSLV2_MT_CLIENT_CERTIFICATE 8
241 
242 #define SSLV3_RECORD_HDR_LEN 5
243 /** max length according to RFC 5246 6.2.2 is 2^14 + 1024 */
244 #define SSLV3_RECORD_MAX_LEN ((1 << 14) + 1024)
245 
246 #define SSLV3_CLIENT_HELLO_VERSION_LEN 2
247 #define SSLV3_CLIENT_HELLO_RANDOM_LEN 32
248 
249 /* TLS heartbeat protocol types */
250 #define TLS_HB_REQUEST 1
251 #define TLS_HB_RESPONSE 2
252 
253 #define SSL_RECORD_MINIMUM_LENGTH 6
254 
255 #define SHA1_STRING_LENGTH 60
256 
257 #define HAS_SPACE(n) ((uint64_t)(input - initial_input) + (uint64_t)(n) <= (uint64_t)(input_len))
258 
260  int retval; // nr bytes consumed from input, or < 0 on error
261  uint32_t needed; // more bytes needed
262 };
263 #define SSL_DECODER_ERROR(e) \
264  (struct SSLDecoderResult) \
265  { \
266  (e), 0 \
267  }
268 #define SSL_DECODER_OK(c) \
269  (struct SSLDecoderResult) \
270  { \
271  (uint32_t)(c), 0 \
272  }
273 #define SSL_DECODER_INCOMPLETE(c, n) \
274  (struct SSLDecoderResult) \
275  { \
276  (uint32_t)(c), (n) \
277  }
278 
279 static inline int SafeMemcpy(void *dst, size_t dst_offset, size_t dst_size,
280  const void *src, size_t src_offset, size_t src_size, size_t src_tocopy) WARN_UNUSED;
281 
282 static inline int SafeMemcpy(void *dst, size_t dst_offset, size_t dst_size,
283  const void *src, size_t src_offset, size_t src_size, size_t src_tocopy)
284 {
285  DEBUG_VALIDATE_BUG_ON(dst_offset >= dst_size);
286  DEBUG_VALIDATE_BUG_ON(src_offset >= src_size);
287  DEBUG_VALIDATE_BUG_ON(src_tocopy > (src_size - src_offset));
288  DEBUG_VALIDATE_BUG_ON(src_tocopy > (dst_size - dst_offset));
289 
290  if (dst_offset < dst_size && src_offset < src_size &&
291  src_tocopy <= (src_size - src_offset) &&
292  src_tocopy <= (dst_size - dst_offset)) {
293  memcpy(dst + dst_offset, src + src_offset, src_tocopy);
294  return 0;
295  }
296  return -1;
297 }
298 
299 #ifdef DEBUG_VALIDATION
300 #define ValidateRecordState(connp) \
301  do { \
302  DEBUG_VALIDATE_BUG_ON(((connp)->record_length + SSLV3_RECORD_HDR_LEN) < \
303  (connp)->bytes_processed); \
304  } while(0);
305 #else
306 #define ValidateRecordState(...)
307 #endif
308 
309 #define SSLParserHSReset(connp) \
310  do { \
311  (connp)->handshake_type = 0; \
312  (connp)->message_length = 0; \
313  } while (0)
314 
315 #define SSLParserReset(state) \
316  do { \
317  SCLogDebug("resetting state"); \
318  (state)->curr_connp->bytes_processed = 0; \
319  SSLParserHSReset((state)->curr_connp); \
320  } while(0)
321 
322 #define SSLSetEvent(ssl_state, event) \
323  do { \
324  SCLogDebug("setting event %u", (event)); \
325  if ((ssl_state) == NULL) { \
326  SCLogDebug("could not set decoder event %u", event); \
327  } else { \
328  SCAppLayerDecoderEventsSetEventRaw(&(ssl_state)->tx_data.events, (event)); \
329  (ssl_state)->events++; \
330  } \
331  } while (0)
332 
333 static void *SSLGetTx(void *state, uint64_t tx_id)
334 {
335  SSLState *ssl_state = (SSLState *)state;
336  return ssl_state;
337 }
338 
339 static uint64_t SSLGetTxCnt(void *state)
340 {
341  /* single tx */
342  return 1;
343 }
344 
345 static void UpdateClientState(SSLState *ssl_state, enum TlsStateClient s)
346 {
347  /* monotonic: a late app-data write must not move the state back */
348  if (s > ssl_state->client_state) {
349 #ifdef DEBUG
350  enum TlsStateClient old = ssl_state->client_state;
351 #endif
352  ssl_state->client_state = s;
353 #ifdef DEBUG
354  SCLogDebug("toserver: state updated to %u from %u", s, old);
355 #endif
356  }
357 }
358 
359 static void UpdateServerState(SSLState *ssl_state, enum TlsStateServer s)
360 {
361  /* monotonic: a late app-data write must not move the state back */
362  if (s > ssl_state->server_state) {
363 #ifdef DEBUG
364  enum TlsStateServer old = ssl_state->server_state;
365 #endif
366  ssl_state->server_state = s;
367 #ifdef DEBUG
368  SCLogDebug("toclient: state updated to %u from %u", s, old);
369 #endif
370  }
371 }
372 
373 static int SSLGetAlstateProgress(void *tx, uint8_t direction)
374 {
375  SSLState *ssl_state = (SSLState *)tx;
376  if (direction & STREAM_TOCLIENT) {
377  return ssl_state->server_state;
378  } else {
379  return ssl_state->client_state;
380  }
381 }
382 
383 static AppLayerTxData *SSLGetTxData(void *vtx)
384 {
385  SSLState *ssl_state = (SSLState *)vtx;
386  return &ssl_state->tx_data;
387 }
388 
389 static AppLayerStateData *SSLGetStateData(void *vstate)
390 {
391  SSLState *ssl_state = (SSLState *)vstate;
392  return &ssl_state->state_data;
393 }
394 
395 static void TlsDecodeHSCertificateErrSetEvent(SSLState *ssl_state, uint32_t err)
396 {
397  switch(err) {
400  break;
401  case ERR_EXTRACT_ISSUER:
403  break;
404  case ERR_EXTRACT_SUBJECT:
406  break;
407  case ERR_INVALID_DER:
409  break;
412  break;
413  case ERR_INVALID_DATE:
415  break;
418  break;
421  break;
422  case ERR_INVALID_SERIAL:
424  break;
425  case ERR_INVALID_VERSION:
427  break;
428  case ERR_INVALID_LENGTH:
430  break;
432  default:
434  break;
435  }
436 }
437 
438 static inline int TlsDecodeHSCertificateFingerprint(
439  char **out, const uint8_t *input, uint32_t cert_len)
440 {
441  *out = SCCalloc(1, SHA1_STRING_LENGTH);
442  if (*out == NULL)
443  return -1;
444 
445  uint8_t hash[SC_SHA1_LEN];
446  if (SCSha1HashBuffer(input, cert_len, hash, sizeof(hash)) == 1) {
447  SCToHex_sep((uint8_t *)*out, SHA1_STRING_LENGTH, ':', hash, SC_SHA1_LEN);
448  }
449  return 0;
450 }
451 
452 static inline int TlsDecodeHSCertificateAddCertToChain(
453  SSLStateConnp *connp, const uint8_t *input, uint32_t cert_len)
454 {
455  SSLCertsChain *cert = SCCalloc(1, sizeof(SSLCertsChain));
456  if (cert == NULL)
457  return -1;
458 
459  cert->cert_data = (uint8_t *)input;
460  cert->cert_len = cert_len;
461  TAILQ_INSERT_TAIL(&connp->certs, cert, next);
462 
463  return 0;
464 }
465 
466 static int TlsDecodeHSCertificate(SSLState *ssl_state, SSLStateConnp *connp,
467  const uint8_t *const initial_input, const uint32_t input_len, const int certn,
468  bool *extract_event_sent)
469 {
470  const uint8_t *input = (uint8_t *)initial_input;
471  uint32_t err_code = 0;
472  X509 *x509 = NULL;
473  int rc = 0;
474 
475  /* stashed cert0 extraction: published to connp only when the whole
476  * certificate decodes (see the cert0 fill below) */
477  uint8_t *st_subject = NULL, *st_issuer = NULL, *st_serial = NULL;
478  SSLSubjectAltName *st_sans = NULL;
479  char *st_fingerprint = NULL;
480  uint32_t st_subject_len = 0, st_issuer_len = 0, st_serial_len = 0;
481  uint16_t st_sans_num = 0;
482  int64_t st_not_before = 0, st_not_after = 0;
483 
484  if (!(HAS_SPACE(3)))
485  goto invalid_cert;
486 
487  uint32_t cert_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
488  input += 3;
489 
490  if (!(HAS_SPACE(cert_len)))
491  goto invalid_cert;
492 
493  /* only store fields from the first certificate in the chain */
494  if (certn == 0 && connp->cert0_subject == NULL && connp->cert0_issuerdn == NULL &&
495  connp->cert0_serial == NULL) {
496  x509 = SCX509Decode(input, cert_len, &err_code);
497  if (x509 == NULL) {
498  /* an undecodable certificate: report it once and let the
499  * caller skip it - the rest of the chain must still be
500  * decoded */
501  TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
502  goto fail;
503  }
504 
505  /* extract into the stashes and publish to connp->cert0_* only
506  * when the whole certificate yields every field: a partial fill
507  * would otherwise block the next certificate from completing the
508  * fields */
509  SCX509GetSubject(x509, &st_subject, &st_subject_len);
510  if (st_subject == NULL) {
511  err_code = ERR_EXTRACT_SUBJECT;
512  goto error;
513  }
514 
515  SCX509GetIssuer(x509, &st_issuer, &st_issuer_len);
516  if (st_issuer == NULL) {
517  err_code = ERR_EXTRACT_ISSUER;
518  goto error;
519  }
520 
521  st_sans_num = SCX509GetSubjectAltNameLen(x509);
522  if (st_sans_num == TLS_MAX_SAN && !*extract_event_sent) {
524  *extract_event_sent = true;
525  }
526  if (st_sans_num > 0) {
527  st_sans = SCCalloc(st_sans_num, sizeof(SSLSubjectAltName));
528  if (st_sans == NULL) {
529  st_sans_num = 0;
530  goto error;
531  }
532  for (uint16_t i = 0; i < st_sans_num; i++) {
533  SCX509GetSubjectAltNameAt(x509, i, &st_sans[i].san, &st_sans[i].san_len);
534  }
535  }
536 
537  SCX509GetSerial(x509, &st_serial, &st_serial_len);
538  if (st_serial == NULL) {
539  err_code = ERR_INVALID_SERIAL;
540  goto error;
541  }
542 
543  rc = SCX509GetValidity(x509, &st_not_before, &st_not_after);
544  if (rc != 0) {
545  err_code = ERR_EXTRACT_VALIDITY;
546  goto error;
547  }
548 
549  rc = TlsDecodeHSCertificateFingerprint(&st_fingerprint, input, cert_len);
550  if (rc != 0) {
551  SCLogDebug("TlsDecodeHSCertificateFingerprint failed with %d", rc);
552  goto error;
553  }
554 
555  /* the certificate yielded every field: publish it */
556  connp->cert0_subject = st_subject;
557  connp->cert0_subject_len = st_subject_len;
558  connp->cert0_issuerdn = st_issuer;
559  connp->cert0_issuerdn_len = st_issuer_len;
560  connp->cert0_sans = st_sans;
561  connp->cert0_sans_num = st_sans_num;
562  connp->cert0_serial = st_serial;
563  connp->cert0_serial_len = st_serial_len;
564  connp->cert0_not_before = st_not_before;
565  connp->cert0_not_after = st_not_after;
566  connp->cert0_fingerprint = st_fingerprint;
567  st_subject = st_issuer = st_serial = NULL;
568  st_sans = NULL;
569  st_fingerprint = NULL;
570 
571  SCX509Free(x509);
572  x509 = NULL;
573  }
574 
575  /* every certificate of the chain is kept, also for certn > 0 */
576  rc = TlsDecodeHSCertificateAddCertToChain(connp, input, cert_len);
577  if (rc != 0) {
578  SCLogDebug("TlsDecodeHSCertificateAddCertToChain failed with %d", rc);
579  goto error;
580  }
581 
582  input += cert_len;
583  return (int)(input - initial_input);
584 
585 error:
586  if (err_code != 0 && !*extract_event_sent) {
587  /* the message no longer aborts at the first failing certificate:
588  * report the first extraction failure once */
589  TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
590  *extract_event_sent = true;
591  }
592  /* the certificate failed before the publish block: nothing was
593  * published, release the stashed extraction so the next certificate
594  * can fill cert0 */
595  if (st_subject != NULL)
596  SCX509ArrayFree(st_subject, st_subject_len);
597  if (st_issuer != NULL)
598  SCX509ArrayFree(st_issuer, st_issuer_len);
599  if (st_serial != NULL)
600  SCX509ArrayFree(st_serial, st_serial_len);
601  if (st_fingerprint != NULL)
602  SCFree(st_fingerprint);
603  if (st_sans != NULL) {
604  for (uint16_t i = 0; i < st_sans_num; i++)
605  SCX509ArrayFree(st_sans[i].san, st_sans[i].san_len);
606  SCFree(st_sans);
607  }
608  if (x509 != NULL)
609  SCX509Free(x509);
610  return -1;
611 
612 fail:
613  return -1;
614 
615 invalid_cert:
616  SCLogDebug("TLS invalid certificate");
618  return -1;
619 }
620 
621 /** \internal
622  * \brief parse cert data in a certificate handshake message
623  * will be called with all data.
624  * \retval consumed bytes consumed or -1 on error
625  */
626 static int TlsDecodeHSCertificates(SSLState *ssl_state, SSLStateConnp *connp,
627  const uint8_t *const initial_input, const uint32_t input_len)
628 {
629  const uint8_t *input = (uint8_t *)initial_input;
630 
631  if (!(HAS_SPACE(3)))
632  return -1;
633 
634  const uint32_t cert_chain_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
635  input += 3;
636 
637  if (!(HAS_SPACE(cert_chain_len)))
638  return -1;
639 
640  if (connp->certs_buffer != NULL) {
641  /* one-shot buffer: a second certificate message must not
642  * overwrite or invalidate the data of the first one (base
643  * semantics, renegotiation re-sends the same certificate) */
644  return -1;
645  }
646 
647  /* this connp's certificate message: until it decodes fully the
648  * certificate data is incomplete - only a fully decoded certificate
649  * message completes it, never a later handshake message */
650  connp->cert_data_incomplete = true;
651 
652  connp->certs_buffer = SCCalloc(1, cert_chain_len);
653  if (connp->certs_buffer == NULL) {
654  return -1;
655  }
656  connp->certs_buffer_size = cert_chain_len;
657  memcpy(connp->certs_buffer, input, cert_chain_len);
658 
659  int cert_cnt = 0;
660  uint32_t processed_len = 0;
661  bool cert_failed = false;
662  bool extract_event_sent = false;
663  /* coverity[tainted_data] */
664  while (processed_len < cert_chain_len) {
665  int rc = TlsDecodeHSCertificate(ssl_state, connp, connp->certs_buffer + processed_len,
666  connp->certs_buffer_size - processed_len, cert_cnt, &extract_event_sent);
667  if (rc > 0) {
668  DEBUG_VALIDATE_BUG_ON(processed_len + (uint32_t)rc > cert_chain_len);
669  if (processed_len + (uint32_t)rc > cert_chain_len) {
670  return -1;
671  }
672  processed_len += (uint32_t)rc;
673  continue;
674  }
675 
676  /* a certificate failed to decode: skip its bytes so the rest
677  * of the chain is still decoded; an invalid length aborts the
678  * message */
679  const uint8_t *cert = connp->certs_buffer + processed_len;
680  if (processed_len + 3 > cert_chain_len)
681  return -1;
682  const uint32_t skip = cert[0] << 16 | cert[1] << 8 | cert[2];
683  if (processed_len + 3 + skip > cert_chain_len)
684  return -1;
685  processed_len += 3 + skip;
686  cert_failed = true;
687  }
688 
689  if (cert_failed) {
690  /* the message contains an undecodable certificate: the phase
691  * data is incomplete, so the caller suppresses the cert phase;
692  * the decoded rest of the chain is kept */
693  return -1;
694  }
695  connp->cert_data_incomplete = false;
696  connp->cert_chain_final = true;
697  return processed_len + 3;
698 }
699 
700 /**
701  * \inline
702  * \brief Check if value is GREASE.
703  *
704  * http://tools.ietf.org/html/draft-davidben-tls-grease-00
705  *
706  * \param value Value to check.
707  *
708  * \retval 1 if is GREASE.
709  * \retval 0 if not is GREASE.
710  */
711 static inline int TLSDecodeValueIsGREASE(const uint16_t value)
712 {
713  switch (value)
714  {
715  case 0x0a0a:
716  case 0x1a1a:
717  case 0x2a2a:
718  case 0x3a3a:
719  case 0x4a4a:
720  case 0x5a5a:
721  case 0x6a6a:
722  case 0x7a7a:
723  case 0x8a8a:
724  case 0x9a9a:
725  case 0xaaaa:
726  case 0xbaba:
727  case 0xcaca:
728  case 0xdada:
729  case 0xeaea:
730  case 0xfafa:
731  return 1;
732  default:
733  return 0;
734  }
735 }
736 
737 static inline int TLSDecodeHSHelloVersion(SSLState *ssl_state,
738  const uint8_t * const initial_input,
739  const uint32_t input_len)
740 {
741  uint8_t *input = (uint8_t *)initial_input;
742 
744  SCLogDebug("TLS handshake invalid length");
745  SSLSetEvent(ssl_state,
747  return -1;
748  }
749 
750  uint16_t version = (uint16_t)(*input << 8) | *(input + 1);
751  ssl_state->curr_connp->version = version;
752 
753  if (ssl_state->current_flags &
755  SCTLSHandshakeSetTLSVersion(ssl_state->curr_connp->hs, version);
756  }
757 
758  /* TLSv1.3 draft1 to draft21 use the version field as earlier TLS
759  versions, instead of using the supported versions extension. */
760  if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
761  ((ssl_state->curr_connp->version == TLS_VERSION_13) ||
762  (((ssl_state->curr_connp->version >> 8) & 0xff) == 0x7f))) {
763  ssl_state->flags |= SSL_AL_FLAG_LOG_WITHOUT_CERT;
764  }
765 
766  /* Catch some early TLSv1.3 draft implementations that does not conform
767  to the draft version. */
768  if ((ssl_state->curr_connp->version >= 0x7f01) &&
769  (ssl_state->curr_connp->version < 0x7f10)) {
770  ssl_state->curr_connp->version = TLS_VERSION_13_PRE_DRAFT16;
771  }
772 
773  /* TLSv1.3 drafts from draft1 to draft15 use 0x0304 (TLSv1.3) as the
774  version number, which makes it hard to accurately pinpoint the
775  exact draft version. */
776  else if (ssl_state->curr_connp->version == TLS_VERSION_13) {
777  ssl_state->curr_connp->version = TLS_VERSION_13_PRE_DRAFT16;
778  }
779 
780  if (SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_str == NULL) {
781  ssl_state->curr_connp->ja3_str = Ja3BufferInit();
782  if (ssl_state->curr_connp->ja3_str == NULL)
783  return -1;
784 
785  int rc = Ja3BufferAddValue(&ssl_state->curr_connp->ja3_str, version);
786  if (rc != 0)
787  return -1;
788  }
789 
791 
792  return (int)(input - initial_input);
793 }
794 
795 static inline int TLSDecodeHSHelloRandom(SSLState *ssl_state,
796  const uint8_t * const initial_input,
797  const uint32_t input_len)
798 {
799  uint8_t *input = (uint8_t *)initial_input;
800 
802  SCLogDebug("TLS handshake invalid length");
803  SSLSetEvent(ssl_state,
805  return -1;
806  }
807 
809  memcpy(ssl_state->server_connp.random, input, TLS_RANDOM_LEN);
810  ssl_state->flags |= TLS_TS_RANDOM_SET;
811  } else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
812  memcpy(ssl_state->client_connp.random, input, TLS_RANDOM_LEN);
813  ssl_state->flags |= TLS_TC_RANDOM_SET;
814  }
815 
816  /* Skip random */
818 
819  return (int)(input - initial_input);
820 }
821 
822 static inline int TLSDecodeHSHelloSessionID(SSLState *ssl_state,
823  const uint8_t * const initial_input,
824  const uint32_t input_len)
825 {
826  uint8_t *input = (uint8_t *)initial_input;
827 
828  if (!(HAS_SPACE(1)))
829  goto invalid_length;
830 
831  uint8_t session_id_length = *input;
832  input += 1;
833 
834  if (!(HAS_SPACE(session_id_length)))
835  goto invalid_length;
836 
837  if (session_id_length != 0 && ssl_state->curr_connp->session_id == NULL) {
838  ssl_state->curr_connp->session_id = SCMalloc(session_id_length);
839 
840  if (unlikely(ssl_state->curr_connp->session_id == NULL)) {
841  return -1;
842  }
843 
844  if (SafeMemcpy(ssl_state->curr_connp->session_id, 0, session_id_length,
845  input, 0, input_len, session_id_length) != 0) {
846  return -1;
847  }
848  ssl_state->curr_connp->session_id_length = session_id_length;
849 
850  if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
851  ssl_state->client_connp.session_id != NULL &&
852  ssl_state->server_connp.session_id != NULL) {
853  if ((ssl_state->client_connp.session_id_length ==
854  ssl_state->server_connp.session_id_length) &&
855  (memcmp(ssl_state->server_connp.session_id,
856  ssl_state->client_connp.session_id, session_id_length) == 0)) {
857  ssl_state->flags |= SSL_AL_FLAG_SESSION_RESUMED;
858  }
859  }
860  }
861 
862  input += session_id_length;
863 
864  return (int)(input - initial_input);
865 
866 invalid_length:
867  SCLogDebug("TLS handshake invalid length");
868  SSLSetEvent(ssl_state,
870  return -1;
871 }
872 
873 static inline int TLSDecodeHSHelloCipherSuites(SSLState *ssl_state,
874  const uint8_t * const initial_input,
875  const uint32_t input_len)
876 {
877  const uint8_t *input = initial_input;
878 
879  if (!(HAS_SPACE(2)))
880  goto invalid_length;
881 
882  uint16_t cipher_suites_length;
883 
885  cipher_suites_length = 2;
886  } else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
887  cipher_suites_length = (uint16_t)(*input << 8) | *(input + 1);
888  input += 2;
889  } else {
890  return -1;
891  }
892 
893  if (!(HAS_SPACE(cipher_suites_length)))
894  goto invalid_length;
895 
896  /* Cipher suites length should always be divisible by 2 */
897  if ((cipher_suites_length % 2) != 0) {
898  goto invalid_length;
899  }
900 
901  const bool enable_ja3 =
902  SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_hash == NULL;
903 
904  JA3Buffer *ja3_cipher_suites = NULL;
905 
906  if (enable_ja3) {
907  ja3_cipher_suites = Ja3BufferInit();
908  if (ja3_cipher_suites == NULL)
909  return -1;
910  }
911 
912  uint16_t processed_len = 0;
913  /* coverity[tainted_data] */
914  while (processed_len < cipher_suites_length) {
915  if (!(HAS_SPACE(2))) {
916  if (enable_ja3) {
917  Ja3BufferFree(&ja3_cipher_suites);
918  }
919  goto invalid_length;
920  }
921 
922  uint16_t cipher_suite = (uint16_t)(*input << 8) | *(input + 1);
923  input += 2;
924 
925  if (TLSDecodeValueIsGREASE(cipher_suite) != 1) {
926  if (ssl_state->current_flags &
928  SCTLSHandshakeAddCipher(ssl_state->curr_connp->hs, cipher_suite);
929  }
930  if (enable_ja3) {
931  int rc = Ja3BufferAddValue(&ja3_cipher_suites, cipher_suite);
932  if (rc != 0) {
933  return -1;
934  }
935  }
936  }
937  processed_len += 2;
938  }
939 
940  if (enable_ja3) {
941  int rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str, &ja3_cipher_suites);
942  if (rc == -1) {
943  return -1;
944  }
945  }
946 
947  return (int)(input - initial_input);
948 
949 invalid_length:
950  SCLogDebug("TLS handshake invalid length");
951  SSLSetEvent(ssl_state,
953  return -1;
954 }
955 
956 static inline int TLSDecodeHSHelloCompressionMethods(SSLState *ssl_state,
957  const uint8_t * const initial_input,
958  const uint32_t input_len)
959 {
960  const uint8_t *input = initial_input;
961 
962  if (!(HAS_SPACE(1)))
963  goto invalid_length;
964 
965  /* Skip compression methods */
967  input += 1;
968  } else {
969  uint8_t compression_methods_length = *input;
970  input += 1;
971 
972  if (!(HAS_SPACE(compression_methods_length)))
973  goto invalid_length;
974 
975  input += compression_methods_length;
976  }
977 
978  return (int)(input - initial_input);
979 
980 invalid_length:
981  SCLogDebug("TLS handshake invalid_length");
982  SSLSetEvent(ssl_state,
984  return -1;
985 }
986 
987 static inline int TLSDecodeHSHelloExtensionSni(SSLState *ssl_state,
988  const uint8_t * const initial_input,
989  const uint32_t input_len)
990 {
991  uint8_t *input = (uint8_t *)initial_input;
992 
993  /* Empty extension */
994  if (input_len == 0)
995  return 0;
996 
997  if (!(HAS_SPACE(2)))
998  goto invalid_length;
999 
1000  /* Skip sni_list_length */
1001  input += 2;
1002 
1003  if (!(HAS_SPACE(1)))
1004  goto invalid_length;
1005 
1006  uint8_t sni_type = *input;
1007  input += 1;
1008 
1009  /* Currently the only type allowed is host_name
1010  (RFC6066 section 3). */
1011  if (sni_type != SSL_SNI_TYPE_HOST_NAME) {
1012  SCLogDebug("Unknown SNI type");
1013  SSLSetEvent(ssl_state,
1015  return -1;
1016  }
1017 
1018  if (!(HAS_SPACE(2)))
1019  goto invalid_length;
1020 
1021  uint16_t sni_len = (uint16_t)(*input << 8) | *(input + 1);
1022  input += 2;
1023 
1024  /* host_name contains the fully qualified domain name,
1025  and should therefore be limited by the maximum domain
1026  name length. */
1027  if (!(HAS_SPACE(sni_len)) || sni_len > 255 || sni_len == 0) {
1028  SSLSetEvent(ssl_state,
1030  return -1;
1031  }
1032 
1033  /* There must not be more than one extension of the same
1034  type (RFC5246 section 7.4.1.4). */
1035  if (ssl_state->curr_connp->sni) {
1036  SCLogDebug("Multiple SNI extensions");
1037  SSLSetEvent(ssl_state,
1039  input += sni_len;
1040  return (int)(input - initial_input);
1041  }
1042 
1043  ssl_state->curr_connp->sni_len = sni_len;
1044  ssl_state->curr_connp->sni = SCMalloc(sni_len);
1045  if (unlikely(ssl_state->curr_connp->sni == NULL))
1046  return -1;
1047 
1048  const size_t consumed = input - initial_input;
1049  if (SafeMemcpy(ssl_state->curr_connp->sni, 0, sni_len, initial_input, consumed, input_len,
1050  sni_len) != 0) {
1051  SCFree(ssl_state->curr_connp->sni);
1052  ssl_state->curr_connp->sni = NULL;
1053  return -1;
1054  }
1055  input += sni_len;
1056 
1057  return (int)(input - initial_input);
1058 
1059 invalid_length:
1060  SCLogDebug("TLS handshake invalid length");
1061  SSLSetEvent(ssl_state,
1063 
1064 
1065  return -1;
1066 }
1067 
1068 static inline int TLSDecodeHSHelloExtensionSupportedVersions(SSLState *ssl_state,
1069  const uint8_t * const initial_input,
1070  const uint32_t input_len)
1071 {
1072  const uint8_t *input = initial_input;
1073 
1074  /* Empty extension */
1075  if (input_len == 0)
1076  return 0;
1077 
1078  if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1079  if (!(HAS_SPACE(1)))
1080  goto invalid_length;
1081 
1082  uint8_t supported_ver_len = *input;
1083  input += 1;
1084 
1085  if (supported_ver_len < 2)
1086  goto invalid_length;
1087 
1088  if (!(HAS_SPACE(supported_ver_len)))
1089  goto invalid_length;
1090 
1091  /* Use the first (and preferred) valid version as client version,
1092  * skip over GREASE and other possible noise. */
1093  uint16_t i = 0;
1094  while (i + 1 < (uint16_t)supported_ver_len) {
1095  uint16_t ver = (uint16_t)(input[i] << 8) | input[i + 1];
1096  if (TLSVersionValid(ver)) {
1097  ssl_state->curr_connp->version = ver;
1098  SCTLSHandshakeSetTLSVersion(ssl_state->curr_connp->hs, ver);
1099  break;
1100  }
1101  i += 2;
1102  }
1103 
1104  /* Set a flag to indicate that we have seen this extension */
1106 
1107  input += supported_ver_len;
1108  }
1109  else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
1110  if (!(HAS_SPACE(2)))
1111  goto invalid_length;
1112 
1113  uint16_t ver = (uint16_t)(*input << 8) | *(input + 1);
1114 
1115  if ((ssl_state->flags & SSL_AL_FLAG_CH_VERSION_EXTENSION) &&
1116  (ver > TLS_VERSION_12)) {
1117  ssl_state->flags |= SSL_AL_FLAG_LOG_WITHOUT_CERT;
1118  }
1119 
1120  ssl_state->curr_connp->version = ver;
1121  input += 2;
1122  }
1123 
1124  return (int)(input - initial_input);
1125 
1126 invalid_length:
1127  SCLogDebug("TLS handshake invalid length");
1128  SSLSetEvent(ssl_state,
1130 
1131  return -1;
1132 }
1133 
1134 static inline int TLSDecodeHSHelloExtensionEllipticCurves(SSLState *ssl_state,
1135  const uint8_t *const initial_input, const uint32_t input_len,
1136  JA3Buffer **ja3_elliptic_curves)
1137 {
1138  const uint8_t *input = initial_input;
1139 
1140  /* Empty extension */
1141  if (input_len == 0)
1142  return 0;
1143 
1144  if (!(HAS_SPACE(2)))
1145  goto invalid_length;
1146 
1147  uint16_t elliptic_curves_len = (uint16_t)(*input << 8) | *(input + 1);
1148  input += 2;
1149 
1150  if (!(HAS_SPACE(elliptic_curves_len)))
1151  goto invalid_length;
1152 
1153  if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
1154  *ja3_elliptic_curves != NULL) {
1155  uint16_t ec_processed_len = 0;
1156  /* coverity[tainted_data] */
1157  while (ec_processed_len < elliptic_curves_len)
1158  {
1159  if (!(HAS_SPACE(2)))
1160  goto invalid_length;
1161 
1162  uint16_t elliptic_curve = (uint16_t)(*input << 8) | *(input + 1);
1163  input += 2;
1164 
1165  if (TLSDecodeValueIsGREASE(elliptic_curve) != 1) {
1166  int rc = Ja3BufferAddValue(ja3_elliptic_curves, elliptic_curve);
1167  if (rc != 0)
1168  return -1;
1169  }
1170 
1171  ec_processed_len += 2;
1172  }
1173 
1174  } else {
1175  /* Skip elliptic curves */
1176  input += elliptic_curves_len;
1177  }
1178 
1179  return (int)(input - initial_input);
1180 
1181 invalid_length:
1182  SCLogDebug("TLS handshake invalid length");
1183  SSLSetEvent(ssl_state,
1185 
1186  return -1;
1187 }
1188 
1189 static inline int TLSDecodeHSHelloExtensionEllipticCurvePF(SSLState *ssl_state,
1190  const uint8_t *const initial_input, const uint32_t input_len,
1191  JA3Buffer **ja3_elliptic_curves_pf)
1192 {
1193  const uint8_t *input = initial_input;
1194 
1195  /* Empty extension */
1196  if (input_len == 0)
1197  return 0;
1198 
1199  if (!(HAS_SPACE(1)))
1200  goto invalid_length;
1201 
1202  uint8_t ec_pf_len = *input;
1203  input += 1;
1204 
1205  if (!(HAS_SPACE(ec_pf_len)))
1206  goto invalid_length;
1207 
1208  if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
1209  *ja3_elliptic_curves_pf != NULL) {
1210  uint8_t ec_pf_processed_len = 0;
1211  /* coverity[tainted_data] */
1212  while (ec_pf_processed_len < ec_pf_len)
1213  {
1214  uint8_t elliptic_curve_pf = *input;
1215  input += 1;
1216 
1217  if (TLSDecodeValueIsGREASE(elliptic_curve_pf) != 1) {
1218  int rc = Ja3BufferAddValue(ja3_elliptic_curves_pf, elliptic_curve_pf);
1219  if (rc != 0)
1220  return -1;
1221  }
1222 
1223  ec_pf_processed_len += 1;
1224  }
1225 
1226  } else {
1227  /* Skip elliptic curve point formats */
1228  input += ec_pf_len;
1229  }
1230 
1231  return (int)(input - initial_input);
1232 
1233 invalid_length:
1234  SCLogDebug("TLS handshake invalid length");
1235  SSLSetEvent(ssl_state,
1237 
1238  return -1;
1239 }
1240 
1241 static inline int TLSDecodeHSHelloExtensionSigAlgorithms(
1242  SSLState *ssl_state, const uint8_t *const initial_input, const uint32_t input_len)
1243 {
1244  const uint8_t *input = initial_input;
1245 
1246  /* Empty extension */
1247  if (input_len == 0)
1248  return 0;
1249 
1250  if (!(HAS_SPACE(2)))
1251  goto invalid_length;
1252 
1253  uint16_t sigalgo_len = (uint16_t)(*input << 8) | *(input + 1);
1254  input += 2;
1255 
1256  /* Signature algorithms length should always be divisible by 2 */
1257  if ((sigalgo_len % 2) != 0) {
1258  goto invalid_length;
1259  }
1260 
1261  if (!(HAS_SPACE(sigalgo_len)))
1262  goto invalid_length;
1263 
1264  if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1265  uint16_t sigalgo_processed_len = 0;
1266  while (sigalgo_processed_len < sigalgo_len) {
1267  uint16_t sigalgo = (uint16_t)(*input << 8) | *(input + 1);
1268  input += 2;
1269  sigalgo_processed_len += 2;
1270 
1271  SCTLSHandshakeAddSigAlgo(ssl_state->curr_connp->hs, sigalgo);
1272  }
1273  } else {
1274  /* Skip signature algorithms */
1275  input += sigalgo_len;
1276  }
1277 
1278  return (int)(input - initial_input);
1279 
1280 invalid_length:
1281  SCLogDebug("Signature algorithm list invalid length");
1283 
1284  return -1;
1285 }
1286 
1287 static inline int TLSDecodeHSHelloExtensionALPN(
1288  SSLState *ssl_state, const uint8_t *const initial_input, const uint32_t input_len)
1289 {
1290  const uint8_t *input = initial_input;
1291 
1292  /* Empty extension */
1293  if (input_len == 0)
1294  return 0;
1295 
1296  if (!(HAS_SPACE(2)))
1297  goto invalid_length;
1298 
1299  uint16_t alpn_len = (uint16_t)(*input << 8) | *(input + 1);
1300  input += 2;
1301 
1302  if (!(HAS_SPACE(alpn_len)))
1303  goto invalid_length;
1304 
1305  /* We use 32 bits here to avoid potentially overflowing a value that
1306  needs to be compared to an unsigned 16-bit value. */
1307  uint32_t alpn_processed_len = 0;
1308  while (alpn_processed_len < alpn_len) {
1309  uint8_t protolen = *input;
1310  input += 1;
1311  alpn_processed_len += 1;
1312 
1313  if (!(HAS_SPACE(protolen)))
1314  goto invalid_length;
1315 
1316  /* Check if reading another protolen bytes would exceed the
1317  overall ALPN length; if so, skip and continue */
1318  if (alpn_processed_len + protolen > ((uint32_t)alpn_len)) {
1319  input += alpn_len - alpn_processed_len;
1320  break;
1321  }
1322  SCTLSHandshakeAddALPN(ssl_state->curr_connp->hs, (const char *)input, protolen);
1323 
1324  alpn_processed_len += protolen;
1325  input += protolen;
1326  }
1327 
1328  return (int)(input - initial_input);
1329 
1330 invalid_length:
1331  SCLogDebug("ALPN list invalid length");
1333 
1334  return -1;
1335 }
1336 
1337 static inline int TLSDecodeHSHelloExtensions(SSLState *ssl_state,
1338  const uint8_t * const initial_input,
1339  const uint32_t input_len)
1340 {
1341  const uint8_t *input = initial_input;
1342 
1343  int ret;
1344  int rc;
1345  // if ja3_hash is already computed, do not use new hello to augment ja3_str
1346  const bool ja3 =
1347  (SC_ATOMIC_GET(ssl_config.enable_ja3) == 1) && ssl_state->curr_connp->ja3_hash == NULL;
1348 
1349  JA3Buffer *ja3_extensions = NULL;
1350  JA3Buffer *ja3_elliptic_curves = NULL;
1351  JA3Buffer *ja3_elliptic_curves_pf = NULL;
1352 
1353  if (ja3) {
1354  ja3_extensions = Ja3BufferInit();
1355  if (ja3_extensions == NULL)
1356  goto error;
1357 
1358  if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1359  ja3_elliptic_curves = Ja3BufferInit();
1360  if (ja3_elliptic_curves == NULL)
1361  goto error;
1362 
1363  ja3_elliptic_curves_pf = Ja3BufferInit();
1364  if (ja3_elliptic_curves_pf == NULL)
1365  goto error;
1366  }
1367  }
1368 
1369  /* Extensions are optional (RFC5246 section 7.4.1.2) */
1370  if (!(HAS_SPACE(2)))
1371  goto end;
1372 
1373  uint16_t extensions_len = (uint16_t)(*input << 8) | *(input + 1);
1374  input += 2;
1375 
1376  if (!(HAS_SPACE(extensions_len)))
1377  goto invalid_length;
1378 
1379  uint32_t processed_len = 0;
1380  /* coverity[tainted_data] */
1381  while (processed_len < (uint32_t)extensions_len) {
1382  if (!(HAS_SPACE(2)))
1383  goto invalid_length;
1384 
1385  uint16_t ext_type = (uint16_t)(*input << 8) | *(input + 1);
1386  input += 2;
1387 
1388  if (!(HAS_SPACE(2)))
1389  goto invalid_length;
1390 
1391  uint16_t ext_len = (uint16_t)(*input << 8) | *(input + 1);
1392  input += 2;
1393 
1394  if (!(HAS_SPACE(ext_len)))
1395  goto invalid_length;
1396 
1397  if (processed_len + 4UL + (uint32_t)ext_len > (uint32_t)extensions_len)
1398  goto invalid_length;
1399 
1400  switch (ext_type) {
1401  case SSL_EXTENSION_SNI:
1402  {
1403  /* coverity[tainted_data] */
1404  ret = TLSDecodeHSHelloExtensionSni(ssl_state, input,
1405  ext_len);
1406  if (ret < 0)
1407  goto end;
1408 
1409  input += ext_len;
1410 
1411  break;
1412  }
1413 
1415  {
1416  /* coverity[tainted_data] */
1417  ret = TLSDecodeHSHelloExtensionEllipticCurves(
1418  ssl_state, input, ext_len, &ja3_elliptic_curves);
1419  if (ret < 0)
1420  goto error;
1421 
1422  input += ext_len;
1423 
1424  break;
1425  }
1426 
1428  {
1429  /* coverity[tainted_data] */
1430  ret = TLSDecodeHSHelloExtensionEllipticCurvePF(
1431  ssl_state, input, ext_len, &ja3_elliptic_curves_pf);
1432  if (ret < 0)
1433  goto error;
1434 
1435  input += ext_len;
1436 
1437  break;
1438  }
1439 
1441  /* coverity[tainted_data] */
1442  ret = TLSDecodeHSHelloExtensionSigAlgorithms(ssl_state, input, ext_len);
1443  if (ret < 0)
1444  goto end;
1445 
1446  input += ext_len;
1447 
1448  break;
1449  }
1450 
1451  case SSL_EXTENSION_ALPN: {
1452  /* coverity[tainted_data] */
1453  ret = TLSDecodeHSHelloExtensionALPN(ssl_state, input, ext_len);
1454  if (ret < 0)
1455  goto end;
1456 
1457  input += ext_len;
1458 
1459  break;
1460  }
1461 
1463  {
1464  if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1465  /* Used by 0-RTT to indicate that encrypted data will
1466  be sent right after the ClientHello record. */
1467  ssl_state->flags |= SSL_AL_FLAG_EARLY_DATA;
1468  }
1469 
1470  input += ext_len;
1471 
1472  break;
1473  }
1474 
1476  {
1477  ret = TLSDecodeHSHelloExtensionSupportedVersions(ssl_state, input,
1478  ext_len);
1479  if (ret < 0)
1480  goto end;
1481 
1482  input += ext_len;
1483 
1484  break;
1485  }
1486 
1488  {
1489  if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1490  /* This has to be verified later on by checking if a
1491  certificate record has been sent by the server. */
1492  ssl_state->flags |= SSL_AL_FLAG_SESSION_RESUMED;
1493  }
1494 
1495  input += ext_len;
1496 
1497  break;
1498  }
1499 
1500  default:
1501  {
1502  input += ext_len;
1503  break;
1504  }
1505  }
1506 
1507  if (ja3) {
1508  if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1509  rc = Ja3BufferAddValue(&ja3_extensions, ext_type);
1510  if (rc != 0)
1511  goto error;
1512  }
1513  }
1514 
1515  if (ssl_state->current_flags &
1517  if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1518  SCTLSHandshakeAddExtension(ssl_state->curr_connp->hs, ext_type);
1519  }
1520  }
1521 
1522  processed_len += (uint32_t)ext_len + 4UL;
1523  }
1524 
1525 end:
1526  if (ja3) {
1527  rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
1528  &ja3_extensions);
1529  if (rc == -1)
1530  goto error;
1531 
1532  if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1533  rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
1534  &ja3_elliptic_curves);
1535  if (rc == -1)
1536  goto error;
1537 
1538  rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
1539  &ja3_elliptic_curves_pf);
1540  if (rc == -1)
1541  goto error;
1542  }
1543  }
1544 
1545  return (int)(input - initial_input);
1546 
1547 invalid_length:
1548  SCLogDebug("TLS handshake invalid length");
1549  SSLSetEvent(ssl_state,
1551 
1552 error:
1553  if (ja3_extensions != NULL)
1554  Ja3BufferFree(&ja3_extensions);
1555  if (ja3_elliptic_curves != NULL)
1556  Ja3BufferFree(&ja3_elliptic_curves);
1557  if (ja3_elliptic_curves_pf != NULL)
1558  Ja3BufferFree(&ja3_elliptic_curves_pf);
1559 
1560  return -1;
1561 }
1562 
1563 static int TLSDecodeHandshakeHello(SSLState *ssl_state,
1564  const uint8_t * const input,
1565  const uint32_t input_len)
1566 {
1567  int ret;
1568  uint32_t parsed = 0;
1569 
1570  /* a failed sub-decode must not fail the parse: the message bytes are
1571  * consumed either way. The phase advance that follows relies on a
1572  * fully parsed message, so suppress it via the connp flag instead. */
1573 
1574  ret = TLSDecodeHSHelloVersion(ssl_state, input, input_len);
1575  if (ret < 0)
1576  goto fail;
1577 
1578  parsed += ret;
1579 
1580  ret = TLSDecodeHSHelloRandom(ssl_state, input + parsed, input_len - parsed);
1581  if (ret < 0)
1582  goto fail;
1583 
1584  parsed += ret;
1585 
1586  /* The session id field in the server hello record was removed in
1587  TLSv1.3 draft1, but was readded in draft22. */
1588  if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) ||
1590  ((ssl_state->flags & SSL_AL_FLAG_LOG_WITHOUT_CERT) == 0))) {
1591  ret = TLSDecodeHSHelloSessionID(ssl_state, input + parsed,
1592  input_len - parsed);
1593  if (ret < 0)
1594  goto fail;
1595 
1596  parsed += ret;
1597  }
1598 
1599  ret = TLSDecodeHSHelloCipherSuites(ssl_state, input + parsed,
1600  input_len - parsed);
1601  if (ret < 0)
1602  goto fail;
1603 
1604  parsed += ret;
1605 
1606  /* The compression methods field in the server hello record was
1607  removed in TLSv1.3 draft1, but was readded in draft22. */
1608  if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) ||
1610  ((ssl_state->flags & SSL_AL_FLAG_LOG_WITHOUT_CERT) == 0))) {
1611  ret = TLSDecodeHSHelloCompressionMethods(ssl_state, input + parsed,
1612  input_len - parsed);
1613  if (ret < 0)
1614  goto fail;
1615 
1616  parsed += ret;
1617  }
1618 
1619  ret = TLSDecodeHSHelloExtensions(ssl_state, input + parsed,
1620  input_len - parsed);
1621  if (ret < 0)
1622  goto fail;
1623 
1624  if (SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_hash == NULL) {
1625  ssl_state->curr_connp->ja3_hash = Ja3GenerateHash(ssl_state->curr_connp->ja3_str);
1626  }
1627  /* the hello, supported_versions included, decoded cleanly: its version
1628  * is final for this connp */
1629  ssl_state->curr_connp->hello_decoded = true;
1630  return 0;
1631 
1632 fail:
1633  ssl_state->curr_connp->phase_suppressed = true;
1634  return 0;
1635 }
1636 
1637 #ifdef DEBUG_VALIDATION
1638 static inline bool
1639 RecordAlreadyProcessed(const SSLStateConnp *curr_connp)
1640 {
1641  return ((curr_connp->record_length + SSLV3_RECORD_HDR_LEN) <
1642  curr_connp->bytes_processed);
1643 }
1644 #endif
1645 
1646 static inline int SSLv3ParseHandshakeTypeCertificate(SSLState *ssl_state, SSLStateConnp *connp,
1647  const uint8_t *const initial_input, const uint32_t input_len)
1648 {
1649  int rc = TlsDecodeHSCertificates(ssl_state, connp, initial_input, input_len);
1650  SCLogDebug("rc %d", rc);
1651  if (rc > 0) {
1652  DEBUG_VALIDATE_BUG_ON(rc > (int)input_len);
1653  SSLParserHSReset(connp);
1654  } else if (rc < 0) {
1655  SCLogDebug("error parsing cert, reset state");
1656  SSLParserHSReset(connp);
1657  /* the certificate data is incomplete (an undecodable certificate);
1658  * the track is already in the certificate phase (entered at the
1659  * message header), so nothing to suppress here. cert_data_incomplete
1660  * only gates the handshake-done shortcut at the end of SSLDecode
1661  * (the cert-fields based tail), not the record-level data-phase
1662  * advance (app data and CCS records advance the track regardless) */
1663  }
1664  return input_len;
1665 }
1666 
1667 static int SupportedHandshakeType(const uint8_t type)
1668 {
1669  switch (type) {
1670  case SSLV3_HS_CLIENT_HELLO:
1671  case SSLV3_HS_SERVER_HELLO:
1674  case SSLV3_HS_CERTIFICATE:
1678  case SSLV3_HS_FINISHED:
1683  return true;
1684  break;
1685 
1686  default:
1687  return false;
1688  break;
1689  }
1690 }
1691 
1692 /**
1693  * \param input_len length of bytes after record header. Can be 0 (e.g. for server hello done).
1694  * \retval parsed number of consumed bytes
1695  * \retval < 0 error
1696  */
1697 static int SSLv3ParseHandshakeType(SSLState *ssl_state, const uint8_t *input,
1698  uint32_t input_len, uint8_t direction)
1699 {
1700  const uint8_t *initial_input = input;
1701  int rc;
1702 
1703  DEBUG_VALIDATE_BUG_ON(RecordAlreadyProcessed(ssl_state->curr_connp));
1704 
1705  switch (ssl_state->curr_connp->handshake_type) {
1706  case SSLV3_HS_CLIENT_HELLO:
1708 
1709  if (ssl_state->curr_connp->hs == NULL)
1710  ssl_state->curr_connp->hs = SCTLSHandshakeNew();
1711 
1712  rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1713  if (rc < 0)
1714  return rc;
1715  break;
1716 
1717  case SSLV3_HS_SERVER_HELLO:
1719 
1720  DEBUG_VALIDATE_BUG_ON(ssl_state->curr_connp->message_length != input_len);
1721  if (ssl_state->curr_connp->hs == NULL)
1722  ssl_state->curr_connp->hs = SCTLSHandshakeNew();
1723 
1724  rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1725  if (rc < 0)
1726  return rc;
1727  break;
1728 
1731  break;
1732 
1735  /* a client flight message: the server flight is over, no
1736  * CertificateRequest can still arrive. Only the client's own
1737  * message settles it - the parser accepts the type from either
1738  * direction. */
1739  if (!direction)
1741  break;
1742 
1743  case SSLV3_HS_CERTIFICATE:
1744  if (!direction) {
1745  /* the client certificate: its own flight (and so the
1746  * server's CertificateRequest decision) is past */
1748  }
1749  rc = SSLv3ParseHandshakeTypeCertificate(ssl_state,
1750  direction ? &ssl_state->server_connp : &ssl_state->client_connp, initial_input,
1751  input_len);
1752  if (rc < 0)
1753  return rc;
1754  break;
1755 
1757  break;
1759  if (direction) {
1760  ssl_state->current_flags =
1762  }
1763  break;
1767  break;
1768  case SSLV3_HS_FINISHED:
1769  /* a finished ends its flight in either direction: the server
1770  * flight (and any CertificateRequest) is past */
1772  break;
1774  /* the server flight's last message: only the server direction
1775  * settles it - the parser accepts the type from either
1776  * direction */
1777  if (direction)
1779  break;
1781  SCLogDebug("new session ticket");
1782  break;
1783  default:
1785  return -1;
1786  }
1787 
1788  ssl_state->flags |= ssl_state->current_flags;
1789 
1790  SCLogDebug("message: length %u", ssl_state->curr_connp->message_length);
1791  SCLogDebug("input_len %u ssl_state->curr_connp->bytes_processed %u", input_len, ssl_state->curr_connp->bytes_processed);
1792 
1793  return input_len;
1794 }
1795 
1796 /* the phase of a message is entered when the message starts, so all of its
1797  * fragments belong to it (HTTP1: request_line starts at the first byte of the
1798  * line, not when the line is complete) */
1799 static void SSLv3HandshakePhaseEnter(SSLState *ssl_state, const uint8_t hs_type)
1800 {
1801  /* one message is one phase: the flag is per message, reset at its
1802  * start (the header is read once per message) */
1803  ssl_state->curr_connp->phase_suppressed = false;
1804  if (ssl_state->curr_connp == &ssl_state->client_connp) {
1805  if (hs_type == SSLV3_HS_CLIENT_HELLO)
1806  UpdateClientState(ssl_state, TLS_STATE_CLIENT_HELLO);
1807  else if (hs_type == SSLV3_HS_CERTIFICATE)
1808  UpdateClientState(ssl_state, TLS_STATE_CLIENT_CERT);
1809  } else if (hs_type == SSLV3_HS_SERVER_HELLO) {
1810  UpdateServerState(ssl_state, TLS_STATE_SERVER_HELLO);
1811  } else if (hs_type == SSLV3_HS_CERTIFICATE) {
1812  UpdateServerState(ssl_state, TLS_STATE_SERVER_CERT);
1813  }
1814 }
1815 
1816 /* a complete hello hands the track over to the certificate phase, before any
1817  * certificate byte arrives (HTTP1: request_line -> request_headers at line
1818  * completion). A hello that failed to decode keeps the track in the hello
1819  * phase: its data never existed */
1820 static void SSLv3HandshakePhaseLeave(SSLState *ssl_state, const uint8_t hs_type)
1821 {
1822  /* some handshake messages (certificates) clear the connp handshake
1823  * type before returning, so the message type is passed in */
1824  if (ssl_state->curr_connp->phase_suppressed)
1825  return;
1826  if (ssl_state->curr_connp == &ssl_state->client_connp) {
1827  if (hs_type == SSLV3_HS_CLIENT_HELLO)
1828  UpdateClientState(ssl_state, TLS_STATE_CLIENT_CERT);
1829  } else if (hs_type == SSLV3_HS_SERVER_HELLO) {
1830  UpdateServerState(ssl_state, TLS_STATE_SERVER_CERT);
1831  }
1832 }
1833 
1834 static int SSLv3ParseHandshakeProtocol(SSLState *ssl_state, const uint8_t *input,
1835  uint32_t input_len, uint8_t direction)
1836 {
1837  const uint8_t *initial_input = input;
1838 
1839  if (input_len == 0 || ssl_state->curr_connp->bytes_processed ==
1840  (ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN)) {
1841  SCReturnInt(0);
1842  }
1843 
1844  while (input_len) {
1845  SCLogDebug("input_len %u", input_len);
1846 
1847  if (ssl_state->curr_connp->hs_buffer != NULL) {
1848  SCLogDebug("partial handshake record in place");
1849  const uint32_t need = ssl_state->curr_connp->hs_buffer_message_size -
1850  ssl_state->curr_connp->hs_buffer_offset;
1851  const uint32_t add = MIN(need, input_len);
1852 
1853  /* grow buffer to next multiple of 4k that fits all data we have */
1854  if (ssl_state->curr_connp->hs_buffer_offset + add >
1855  ssl_state->curr_connp->hs_buffer_size) {
1856  const uint32_t avail = ssl_state->curr_connp->hs_buffer_offset + add;
1857  const uint32_t new_size = avail + (4096 - (avail % 4096));
1858  SCLogDebug("new_size %u, avail %u", new_size, avail);
1859  void *ptr = SCRealloc(ssl_state->curr_connp->hs_buffer, new_size);
1860  if (ptr == NULL)
1861  return -1;
1862  ssl_state->curr_connp->hs_buffer = ptr;
1863  ssl_state->curr_connp->hs_buffer_size = new_size;
1864  }
1865 
1866  SCLogDebug("ssl_state->curr_connp->hs_buffer_offset %u "
1867  "ssl_state->curr_connp->hs_buffer_size %u",
1868  ssl_state->curr_connp->hs_buffer_offset, ssl_state->curr_connp->hs_buffer_size);
1869  SCLogDebug("to add %u total %u", add, ssl_state->curr_connp->hs_buffer_offset + add);
1870 
1871  if (SafeMemcpy(ssl_state->curr_connp->hs_buffer,
1872  ssl_state->curr_connp->hs_buffer_offset,
1873  ssl_state->curr_connp->hs_buffer_size, input, 0, add, add) != 0) {
1874  SCLogDebug("copy failed");
1875  return -1;
1876  }
1877  ssl_state->curr_connp->hs_buffer_offset += add;
1878 
1879  if (ssl_state->curr_connp->hs_buffer_message_size <=
1880  ssl_state->curr_connp->hs_buffer_offset) {
1882  ssl_state->curr_connp->hs_buffer_offset);
1883 
1884  ssl_state->curr_connp->handshake_type =
1885  ssl_state->curr_connp->hs_buffer_message_type;
1886  ssl_state->curr_connp->message_length =
1887  ssl_state->curr_connp->hs_buffer_message_size;
1888 
1889  SCLogDebug("got all data now: handshake_type %u message_length %u",
1890  ssl_state->curr_connp->handshake_type,
1891  ssl_state->curr_connp->message_length);
1892 
1893  int retval = SSLv3ParseHandshakeType(ssl_state, ssl_state->curr_connp->hs_buffer,
1894  ssl_state->curr_connp->hs_buffer_offset, direction);
1895  if (retval < 0) {
1896  SSLParserHSReset(ssl_state->curr_connp);
1897  return (retval);
1898  }
1899  SCLogDebug("retval %d", retval);
1900 
1901  SSLv3HandshakePhaseLeave(ssl_state, ssl_state->curr_connp->hs_buffer_message_type);
1902 
1903  /* data processed, reset buffer */
1904  SCFree(ssl_state->curr_connp->hs_buffer);
1905  ssl_state->curr_connp->hs_buffer = NULL;
1906  ssl_state->curr_connp->hs_buffer_size = 0;
1907  ssl_state->curr_connp->hs_buffer_message_size = 0;
1908  ssl_state->curr_connp->hs_buffer_message_type = 0;
1909  ssl_state->curr_connp->hs_buffer_offset = 0;
1910  } else {
1911  SCLogDebug("partial data");
1912  }
1913 
1914  input += add;
1915  input_len -= add;
1916  SCLogDebug("input_len %u", input_len);
1917  SSLParserHSReset(ssl_state->curr_connp);
1918  continue;
1919  }
1920 
1921  SCLogDebug("bytes_processed %u", ssl_state->curr_connp->bytes_processed);
1922  SCLogDebug("input %p input_len %u", input, input_len);
1923 
1924  if (input_len < 4) {
1926  SCReturnInt(-1);
1927  }
1928 
1929  ssl_state->curr_connp->handshake_type = input[0];
1930  ssl_state->curr_connp->message_length = input[1] << 16 | input[2] << 8 | input[3];
1931  /* the fragments of this message all belong to its phase */
1932  SSLv3HandshakePhaseEnter(ssl_state, ssl_state->curr_connp->handshake_type);
1933  SCLogDebug("handshake_type %u message len %u input %p input_len %u",
1934  ssl_state->curr_connp->handshake_type, ssl_state->curr_connp->message_length, input,
1935  input_len);
1936  input += 4;
1937  input_len -= 4;
1938 
1939  const uint32_t record_len = ssl_state->curr_connp->message_length;
1940  /* see if we support this type. We check here to not use the fragment
1941  * handling on things we don't support. */
1942  const bool supported_type = SupportedHandshakeType(ssl_state->curr_connp->handshake_type);
1943  SCLogDebug("supported_type %s handshake_type %u/%02x", supported_type ? "true" : "false",
1944  ssl_state->curr_connp->handshake_type, ssl_state->curr_connp->handshake_type);
1945  if (!supported_type) {
1946  uint32_t avail_record_len = MIN(input_len, record_len);
1947  input += avail_record_len;
1948  input_len -= avail_record_len;
1949 
1950  SSLParserHSReset(ssl_state->curr_connp);
1951 
1952  if ((direction && (ssl_state->flags & SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC)) ||
1953  (!direction && (ssl_state->flags & SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC))) {
1954  // after Change Cipher Spec we get Encrypted Handshake Messages
1955  } else {
1957  }
1958  continue;
1959  }
1960 
1961  /* if the message length exceeds our input_len, we have a tls fragment. */
1962  if (record_len > input_len) {
1963  const uint32_t avail = input_len;
1964  const uint32_t size = avail + (4096 - (avail % 4096));
1965  SCLogDebug("initial buffer size %u, based on input %u", size, avail);
1966  ssl_state->curr_connp->hs_buffer = SCCalloc(1, size);
1967  if (ssl_state->curr_connp->hs_buffer == NULL) {
1968  return -1;
1969  }
1970  ssl_state->curr_connp->hs_buffer_size = size;
1971  ssl_state->curr_connp->hs_buffer_message_size = record_len;
1972  ssl_state->curr_connp->hs_buffer_message_type = ssl_state->curr_connp->handshake_type;
1973 
1974  if (input_len > 0) {
1975  if (SafeMemcpy(ssl_state->curr_connp->hs_buffer, 0,
1976  ssl_state->curr_connp->hs_buffer_size, input, 0, input_len,
1977  input_len) != 0) {
1978  return -1;
1979  }
1980  ssl_state->curr_connp->hs_buffer_offset = input_len;
1981  }
1982  SCLogDebug("opened record buffer %p size %u offset %u type %u msg_size %u",
1983  ssl_state->curr_connp->hs_buffer, ssl_state->curr_connp->hs_buffer_size,
1984  ssl_state->curr_connp->hs_buffer_offset,
1985  ssl_state->curr_connp->hs_buffer_message_type,
1986  ssl_state->curr_connp->hs_buffer_message_size);
1987  input += input_len;
1988  SSLParserHSReset(ssl_state->curr_connp);
1989  return (int)(input - initial_input);
1990 
1991  } else {
1992  /* full record, parse it now */
1993  const uint8_t full_record_hs_type = ssl_state->curr_connp->handshake_type;
1994  int retval = SSLv3ParseHandshakeType(
1995  ssl_state, input, ssl_state->curr_connp->message_length, direction);
1996  if (retval < 0 || retval > (int)input_len) {
1997  DEBUG_VALIDATE_BUG_ON(retval > (int)input_len);
1998  if (retval < 0)
1999  SSLParserHSReset(ssl_state->curr_connp);
2000  return (retval);
2001  }
2002  SCLogDebug("retval %d input_len %u", retval, input_len);
2003  SSLv3HandshakePhaseLeave(ssl_state, full_record_hs_type);
2004  input += retval;
2005  input_len -= retval;
2006 
2007  SSLParserHSReset(ssl_state->curr_connp);
2008  }
2009  SCLogDebug("input_len left %u", input_len);
2010  }
2011  return (int)(input - initial_input);
2012 }
2013 
2014 /**
2015  * \internal
2016  * \brief TLS Alert parser
2017  *
2018  * \param sslstate Pointer to the SSL state.
2019  * \param input Pointer to the received input data.
2020  * \param input_len Length in bytes of the received data.
2021  * \param direction 1 toclient, 0 toserver
2022  *
2023  * \retval The number of bytes parsed on success, 0 if nothing parsed, -1 on failure.
2024  */
2025 static int SSLv3ParseAlertProtocol(
2026  SSLState *ssl_state, const uint8_t *input, uint32_t input_len, uint8_t direction)
2027 {
2028  if (input_len < 2) {
2030  return -1;
2031  }
2032 
2033  /* assume a record > 2 to be an encrypted alert record */
2034  if (input_len == 2) {
2035  uint8_t level = input[0];
2036  // uint8_t desc = input[1];
2037 
2038  /* if level Fatal, we consider the tx finished */
2039  if (level == 2) {
2040  UpdateClientState(ssl_state, TLS_STATE_CLIENT_FINISHED);
2041  UpdateServerState(ssl_state, TLS_STATE_SERVER_FINISHED);
2042  }
2043  }
2044  return 0;
2045 }
2046 
2047 /**
2048  * \internal
2049  * \brief TLS Heartbeat parser (see RFC 6520)
2050  *
2051  * \param sslstate Pointer to the SSL state.
2052  * \param input Pointer to the received input data.
2053  * \param input_len Length in bytes of the received data.
2054  * \param direction 1 toclient, 0 toserver
2055  *
2056  * \retval The number of bytes parsed on success, 0 if nothing parsed, -1 on failure.
2057  */
2058 static int SSLv3ParseHeartbeatProtocol(SSLState *ssl_state, const uint8_t *input,
2059  uint32_t input_len, uint8_t direction)
2060 {
2061  uint8_t hb_type;
2062  uint16_t payload_len;
2063  uint32_t padding_len;
2064 
2065  /* expect at least 3 bytes: heartbeat type (1) + length (2) */
2066  if (input_len < 3) {
2067  return 0;
2068  }
2069 
2070  hb_type = *input++;
2071 
2072  if (!(ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC)) {
2073  if (!(hb_type == TLS_HB_REQUEST || hb_type == TLS_HB_RESPONSE)) {
2075  return -1;
2076  }
2077  }
2078 
2079  if ((ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) == 0) {
2080  ssl_state->flags |= SSL_AL_FLAG_HB_INFLIGHT;
2081 
2082  if (direction) {
2083  SCLogDebug("HeartBeat Record type sent in the toclient direction!");
2084  ssl_state->flags |= SSL_AL_FLAG_HB_SERVER_INIT;
2085  } else {
2086  SCLogDebug("HeartBeat Record type sent in the toserver direction!");
2087  ssl_state->flags |= SSL_AL_FLAG_HB_CLIENT_INIT;
2088  }
2089 
2090  /* if we reach this point, then we can assume that the HB request
2091  is encrypted. If so, let's set the HB record length */
2092  if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
2093  ssl_state->hb_record_len = ssl_state->curr_connp->record_length;
2094  SCLogDebug("Encrypted HeartBeat Request In-flight. Storing len %u",
2095  ssl_state->hb_record_len);
2096  return (ssl_state->curr_connp->record_length - 3);
2097  }
2098 
2099  payload_len = (uint16_t)(*input << 8) | *(input + 1);
2100 
2101  /* check that the requested payload length is really present in
2102  the record (CVE-2014-0160) */
2103  if ((uint32_t)(payload_len+3) > ssl_state->curr_connp->record_length) {
2104  SCLogDebug("We have a short record in HeartBeat Request");
2106  return -1;
2107  }
2108 
2109  /* check the padding length. It must be at least 16 bytes
2110  (RFC 6520, section 4) */
2111  padding_len = ssl_state->curr_connp->record_length - payload_len - 3;
2112  if (padding_len < 16) {
2113  SCLogDebug("We have a short record in HeartBeat Request");
2115  return -1;
2116  }
2117 
2118  /* we don't have the payload */
2119  if (input_len < payload_len + padding_len) {
2120  return 0;
2121  }
2122 
2123  /* OpenSSL still seems to discard multiple in-flight
2124  heartbeats although some tools send multiple at once */
2125  } else if (direction == 1 && (ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) &&
2126  (ssl_state->flags & SSL_AL_FLAG_HB_SERVER_INIT)) {
2127  SCLogDebug("Multiple in-flight server initiated HeartBeats");
2129  return -1;
2130 
2131  } else if (direction == 0 && (ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) &&
2132  (ssl_state->flags & SSL_AL_FLAG_HB_CLIENT_INIT)) {
2133  SCLogDebug("Multiple in-flight client initiated HeartBeats");
2135  return -1;
2136 
2137  } else {
2138  /* we have a HB record in the opposite direction of the request,
2139  let's reset our flags */
2140  ssl_state->flags &= ~SSL_AL_FLAG_HB_INFLIGHT;
2141  ssl_state->flags &= ~SSL_AL_FLAG_HB_SERVER_INIT;
2142  ssl_state->flags &= ~SSL_AL_FLAG_HB_CLIENT_INIT;
2143 
2144  /* if we reach this point, then we can assume that the HB request
2145  is encrypted. If so, let's set the HB record length */
2146  if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
2147  /* check to see if the encrypted response is longer than the
2148  encrypted request */
2149  if (ssl_state->hb_record_len > 0 && ssl_state->hb_record_len <
2150  ssl_state->curr_connp->record_length) {
2151  SCLogDebug("My heart is bleeding.. OpenSSL HeartBleed response (%u)",
2152  ssl_state->hb_record_len);
2153  SSLSetEvent(ssl_state,
2155  ssl_state->hb_record_len = 0;
2156  return -1;
2157  }
2158  }
2159 
2160  /* reset the HB record length in case we have a legit HB followed
2161  by a bad one */
2162  ssl_state->hb_record_len = 0;
2163  }
2164 
2165  /* skip the HeartBeat, 3 bytes were already parsed,
2166  e.g |18 03 02| for TLS 1.2 */
2167  return (ssl_state->curr_connp->record_length - 3);
2168 }
2169 
2170 static int SSLv3ParseRecord(uint8_t direction, SSLState *ssl_state,
2171  const uint8_t *input, uint32_t input_len)
2172 {
2173  const uint8_t *initial_input = input;
2174 
2175  if (input_len == 0) {
2176  return 0;
2177  }
2178 
2179  uint8_t skip_version = 0;
2180 
2181  /* Only set SSL/TLS version here if it has not already been set in
2182  client/server hello. */
2183  if (direction == 0) {
2184  if ((ssl_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
2185  (ssl_state->client_connp.version != TLS_VERSION_UNKNOWN)) {
2186  skip_version = 1;
2187  }
2188  } else {
2189  if ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
2190  (ssl_state->server_connp.version != TLS_VERSION_UNKNOWN)) {
2191  skip_version = 1;
2192  }
2193  }
2194 
2195  switch (ssl_state->curr_connp->bytes_processed) {
2196  case 0:
2197  if (input_len >= 5) {
2198  ssl_state->curr_connp->content_type = input[0];
2199  if (!skip_version) {
2200  ssl_state->curr_connp->version = (uint16_t)(input[1] << 8) | input[2];
2201  }
2202  ssl_state->curr_connp->record_length = input[3] << 8;
2203  ssl_state->curr_connp->record_length |= input[4];
2205  return SSLV3_RECORD_HDR_LEN;
2206  } else {
2207  ssl_state->curr_connp->content_type = *(input++);
2208  if (--input_len == 0)
2209  break;
2210  }
2211 
2212  /* fall through */
2213  case 1:
2214  if (!skip_version) {
2215  ssl_state->curr_connp->version = (uint16_t)(*(input++) << 8);
2216  } else {
2217  input++;
2218  }
2219  if (--input_len == 0)
2220  break;
2221 
2222  /* fall through */
2223  case 2:
2224  if (!skip_version) {
2225  ssl_state->curr_connp->version |= *(input++);
2226  } else {
2227  input++;
2228  }
2229  if (--input_len == 0)
2230  break;
2231 
2232  /* fall through */
2233  case 3:
2234  ssl_state->curr_connp->record_length = *(input++) << 8;
2235  if (--input_len == 0)
2236  break;
2237 
2238  /* fall through */
2239  case 4:
2240  ssl_state->curr_connp->record_length |= *(input++);
2241  if (--input_len == 0)
2242  break;
2243 
2244  /* fall through */
2245  }
2246 
2247  ssl_state->curr_connp->bytes_processed += (input - initial_input);
2248 
2249  return (int)(input - initial_input);
2250 }
2251 
2252 static int SSLv2ParseRecord(uint8_t direction, SSLState *ssl_state,
2253  const uint8_t *input, uint32_t input_len)
2254 {
2255  const uint8_t *initial_input = input;
2256 
2257  if (input_len == 0) {
2258  return 0;
2259  }
2260 
2261  if (ssl_state->curr_connp->record_lengths_length == 2) {
2262  switch (ssl_state->curr_connp->bytes_processed) {
2263  case 0:
2264  if (input_len >= ssl_state->curr_connp->record_lengths_length + 1) {
2265  ssl_state->curr_connp->record_length = (0x7f & input[0]) << 8 | input[1];
2266  ssl_state->curr_connp->content_type = input[2];
2267  ssl_state->curr_connp->version = SSL_VERSION_2;
2268  ssl_state->curr_connp->bytes_processed += 3;
2269  return 3;
2270  } else {
2271  ssl_state->curr_connp->record_length = (0x7f & *(input++)) << 8;
2272  if (--input_len == 0)
2273  break;
2274  }
2275 
2276  /* fall through */
2277  case 1:
2278  ssl_state->curr_connp->record_length |= *(input++);
2279  if (--input_len == 0)
2280  break;
2281 
2282  /* fall through */
2283  case 2:
2284  ssl_state->curr_connp->content_type = *(input++);
2285  ssl_state->curr_connp->version = SSL_VERSION_2;
2286  if (--input_len == 0)
2287  break;
2288 
2289  /* fall through */
2290  }
2291 
2292  } else {
2293  switch (ssl_state->curr_connp->bytes_processed) {
2294  case 0:
2295  if (input_len >= ssl_state->curr_connp->record_lengths_length + 1) {
2296  ssl_state->curr_connp->record_length = (0x3f & input[0]) << 8 | input[1];
2297  ssl_state->curr_connp->content_type = input[3];
2298  ssl_state->curr_connp->version = SSL_VERSION_2;
2299  ssl_state->curr_connp->bytes_processed += 4;
2300  return 4;
2301  } else {
2302  ssl_state->curr_connp->record_length = (0x3f & *(input++)) << 8;
2303  if (--input_len == 0)
2304  break;
2305  }
2306 
2307  /* fall through */
2308  case 1:
2309  ssl_state->curr_connp->record_length |= *(input++);
2310  if (--input_len == 0)
2311  break;
2312 
2313  /* fall through */
2314  case 2:
2315  /* padding */
2316  input++;
2317  if (--input_len == 0)
2318  break;
2319 
2320  /* fall through */
2321  case 3:
2322  ssl_state->curr_connp->content_type = *(input++);
2323  ssl_state->curr_connp->version = SSL_VERSION_2;
2324  if (--input_len == 0)
2325  break;
2326 
2327  /* fall through */
2328  }
2329  }
2330 
2331  ssl_state->curr_connp->bytes_processed += (input - initial_input);
2332 
2333  return (int)(input - initial_input);
2334 }
2335 
2336 static struct SSLDecoderResult SSLv2Decode(uint8_t direction, SSLState *ssl_state,
2337  AppLayerParserState *pstate, const uint8_t *input, uint32_t input_len,
2338  const StreamSlice stream_slice)
2339 {
2340  const uint8_t *initial_input = input;
2341 
2342  if (ssl_state->curr_connp->bytes_processed == 0) {
2343  if (input[0] & 0x80) {
2344  ssl_state->curr_connp->record_lengths_length = 2;
2345  } else {
2346  ssl_state->curr_connp->record_lengths_length = 3;
2347  }
2348 
2349  SCLogDebug("record start: ssl2.hdr frame");
2350  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input,
2351  ssl_state->curr_connp->record_lengths_length + 1, direction, TLS_FRAME_SSLV2_HDR);
2352  }
2353 
2354  SCLogDebug("direction %u ssl_state->curr_connp->record_lengths_length + 1 %u, "
2355  "ssl_state->curr_connp->bytes_processed %u",
2356  direction, ssl_state->curr_connp->record_lengths_length + 1,
2357  ssl_state->curr_connp->bytes_processed);
2358  /* the +1 is because we read one extra byte inside SSLv2ParseRecord
2359  to read the msg_type */
2360  if (ssl_state->curr_connp->bytes_processed <
2361  (ssl_state->curr_connp->record_lengths_length + 1)) {
2362  const int retval = SSLv2ParseRecord(direction, ssl_state, input, input_len);
2363  SCLogDebug("retval %d ssl_state->curr_connp->record_length %u", retval,
2364  ssl_state->curr_connp->record_length);
2365  if (retval < 0 || retval > (int)input_len) {
2366  DEBUG_VALIDATE_BUG_ON(retval > (int)input_len);
2368  return SSL_DECODER_ERROR(-1);
2369  }
2370 
2371  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input,
2372  ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length,
2373  direction, TLS_FRAME_SSLV2_PDU);
2374  SCLogDebug("record start: ssl2.pdu frame");
2375 
2376  input += retval;
2377  input_len -= retval;
2378  }
2379 
2380  /* if we don't have the full record, we return incomplete */
2381  if (ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length >
2382  input_len + ssl_state->curr_connp->bytes_processed) {
2383  uint32_t needed = ssl_state->curr_connp->record_length;
2384  SCLogDebug("record len %u input_len %u parsed %u: need %u bytes more data",
2385  ssl_state->curr_connp->record_length, input_len, (uint32_t)(input - initial_input),
2386  needed);
2387  return SSL_DECODER_INCOMPLETE((input - initial_input), needed);
2388  }
2389 
2390  if (input_len == 0) {
2391  return SSL_DECODER_OK((input - initial_input));
2392  }
2393 
2394  /* record_length should never be zero */
2395  if (ssl_state->curr_connp->record_length == 0) {
2396  SCLogDebug("SSLv2 record length is zero");
2398  return SSL_DECODER_ERROR(-1);
2399  }
2400 
2401  /* record_lengths_length should never be zero */
2402  if (ssl_state->curr_connp->record_lengths_length == 0) {
2403  SCLogDebug("SSLv2 record lengths length is zero");
2405  return SSL_DECODER_ERROR(-1);
2406  }
2407 
2408  switch (ssl_state->curr_connp->content_type) {
2409  case SSLV2_MT_ERROR:
2410  SCLogDebug("SSLV2_MT_ERROR msg_type received. Error encountered "
2411  "in establishing the sslv2 session, may be version");
2413 
2414  break;
2415 
2416  case SSLV2_MT_CLIENT_HELLO:
2417  /* record_length does not count the msg_type byte. CLIENT_HELLO
2418  * body starts with 3 fixed fields: client_version (2) +
2419  * cipher_spec_length (2) + session_id_length (2). We need at
2420  * least those 6 bytes after the msg_type, so record_length
2421  * must be >= 7. */
2422  if (input_len < 6 || ssl_state->curr_connp->record_length < 7) {
2424  return SSL_DECODER_ERROR(-1);
2425  }
2426 
2427  ssl_state->current_flags = SSL_AL_FLAG_STATE_CLIENT_HELLO;
2428  ssl_state->current_flags |= SSL_AL_FLAG_SSL_CLIENT_HS;
2429  UpdateClientState(ssl_state, TLS_STATE_CLIENT_HELLO);
2430 
2431  const uint16_t version = (uint16_t)(input[0] << 8) | input[1];
2432  SCLogDebug("SSLv2: version %04x", version);
2433  ssl_state->curr_connp->version = version;
2434  ssl_state->curr_connp->hello_decoded = true;
2435  uint16_t session_id_length = (input[5]) | (uint16_t)(input[4] << 8);
2436  input += 6;
2437  input_len -= 6;
2438  ssl_state->curr_connp->bytes_processed += 6;
2439  if (session_id_length == 0) {
2440  ssl_state->current_flags |= SSL_AL_FLAG_SSL_NO_SESSION_ID;
2441  }
2442  break;
2443 
2445  if (!(ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS)) {
2446  SCLogDebug("Client hello is not seen before master key "
2447  "message!");
2448  }
2449  ssl_state->current_flags = SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY;
2450 
2451  break;
2452 
2454  if (direction == 1) {
2455  SCLogDebug("Incorrect SSL Record type sent in the toclient "
2456  "direction!");
2457  } else {
2458  ssl_state->current_flags = SSL_AL_FLAG_STATE_CLIENT_KEYX;
2459  /* a v2 client certificate message names the client's
2460  * certificate phase, even if the v2 certificate payload is
2461  * not extracted */
2462  UpdateClientState(ssl_state, TLS_STATE_CLIENT_CERT);
2463  }
2464 
2465  /* fall through */
2468  if (direction == 0 &&
2469  !(ssl_state->curr_connp->content_type &
2471  SCLogDebug("Incorrect SSL Record type sent in the toserver "
2472  "direction!");
2473  }
2474 
2475  /* fall through */
2478  /* both client hello and server hello must be seen */
2479  if ((ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) &&
2480  (ssl_state->flags & SSL_AL_FLAG_SSL_SERVER_HS)) {
2481 
2482  if (direction == 0) {
2483  if (ssl_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) {
2484  ssl_state->current_flags |= SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED;
2485  SCLogDebug("SSLv2 client side has started the encryption");
2486  } else if (ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY) {
2487  ssl_state->current_flags = SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED;
2488  SCLogDebug("SSLv2 client side has started the encryption");
2489  }
2490  } else {
2491  ssl_state->current_flags = SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED;
2492  SCLogDebug("SSLv2 Server side has started the encryption");
2493  }
2494 
2495  if ((ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED) &&
2496  (ssl_state->flags & SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED))
2497  {
2499  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_INSPECTION);
2500  }
2501 
2503  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_REASSEMBLY);
2504  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_BYPASS_READY);
2505  }
2506  SCLogDebug("SSLv2 No reassembly & inspection has been set");
2507  }
2508  }
2509 
2510  break;
2511 
2512  case SSLV2_MT_SERVER_HELLO:
2513  ssl_state->current_flags = SSL_AL_FLAG_STATE_SERVER_HELLO;
2514  ssl_state->current_flags |= SSL_AL_FLAG_SSL_SERVER_HS;
2515  ssl_state->curr_connp->hello_decoded = true;
2516  UpdateServerState(ssl_state, TLS_STATE_SERVER_HELLO);
2517 
2518  break;
2519  }
2520 
2521  ssl_state->flags |= ssl_state->current_flags;
2522 
2523  if (ssl_state->curr_connp->bytes_processed >
2524  ssl_state->curr_connp->record_length + ssl_state->curr_connp->record_lengths_length) {
2525  SCLogDebug("SSLv2 bytes_processed (%u) exceeds record+hdr "
2526  "len (record_length=%u, lengths_length=%u)",
2527  ssl_state->curr_connp->bytes_processed, ssl_state->curr_connp->record_length,
2528  ssl_state->curr_connp->record_lengths_length);
2530  return SSL_DECODER_ERROR(-1);
2531  }
2532 
2533  if (input_len + ssl_state->curr_connp->bytes_processed >=
2534  (ssl_state->curr_connp->record_length +
2535  ssl_state->curr_connp->record_lengths_length)) {
2536 
2537  /* looks like we have another record after this */
2538  uint32_t diff = ssl_state->curr_connp->record_length +
2539  ssl_state->curr_connp->record_lengths_length + -
2540  ssl_state->curr_connp->bytes_processed;
2541  input += diff;
2542  SSLParserReset(ssl_state);
2543 
2544  /* we still don't have the entire record for the one we are
2545  currently parsing */
2546  } else {
2547  input += input_len;
2548  ssl_state->curr_connp->bytes_processed += input_len;
2549  }
2550  return SSL_DECODER_OK((input - initial_input));
2551 }
2552 
2553 static struct SSLDecoderResult SSLv3Decode(uint8_t direction, SSLState *ssl_state,
2554  AppLayerParserState *pstate, const uint8_t *input, const uint32_t input_len,
2555  const StreamSlice stream_slice)
2556 {
2557  uint32_t parsed = 0;
2558  uint32_t record_len; /* slice of input_len for the current record */
2559  const bool first_call = (ssl_state->curr_connp->bytes_processed == 0);
2560 
2561  if (ssl_state->curr_connp->bytes_processed < SSLV3_RECORD_HDR_LEN) {
2562  const uint16_t prev_version = ssl_state->curr_connp->version;
2563 
2564  int retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
2565  if (retval < 0 || retval > (int)input_len) {
2566  DEBUG_VALIDATE_BUG_ON(retval > (int)input_len);
2567  SCLogDebug("SSLv3ParseRecord returned %d", retval);
2569  return SSL_DECODER_ERROR(-1);
2570  }
2571  parsed = retval;
2572 
2573  SCLogDebug("%s input %p record_length %u", (direction == 0) ? "toserver" : "toclient",
2574  input, ssl_state->curr_connp->record_length);
2575 
2576  /* first the hdr frame at our first chance */
2577  if (first_call) {
2578  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input, SSLV3_RECORD_HDR_LEN,
2579  direction, TLS_FRAME_HDR);
2580  }
2581 
2582  /* parser is streaming for the initial header, then switches to incomplete
2583  * API: so if we don't have the hdr yet, return consumed bytes and wait
2584  * until we are called again with new data. */
2585  if (ssl_state->curr_connp->bytes_processed < SSLV3_RECORD_HDR_LEN) {
2586  SCLogDebug(
2587  "incomplete header, return %u bytes consumed and wait for more data", parsed);
2588  return SSL_DECODER_OK(parsed);
2589  }
2590 
2591  /* pdu frame needs record length, so only create it when hdr fully parsed. */
2592  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input,
2593  ssl_state->curr_connp->record_length + retval, direction, TLS_FRAME_PDU);
2594  record_len = MIN(input_len - parsed, ssl_state->curr_connp->record_length);
2595  SCLogDebug(
2596  "record_len %u (input_len %u, parsed %u, ssl_state->curr_connp->record_length %u)",
2597  record_len, input_len, parsed, ssl_state->curr_connp->record_length);
2598 
2599  bool unknown_record = false;
2600  switch (ssl_state->curr_connp->content_type) {
2602  case SSLV3_ALERT_PROTOCOL:
2606  break;
2607  default:
2608  unknown_record = true;
2609  break;
2610  }
2611 
2612  /* unknown record type. For TLS 1.0, 1.1 and 1.2 this is ok. For the rest it is fatal. Based
2613  * on Wireshark logic. */
2614  if (prev_version == TLS_VERSION_10 || prev_version == TLS_VERSION_11) {
2615  if (unknown_record) {
2616  SCLogDebug("unknown record, ignore it");
2618 
2619  ssl_state->curr_connp->bytes_processed = 0; // TODO review this reset logic
2620  ssl_state->curr_connp->content_type = 0;
2621  ssl_state->curr_connp->record_length = 0;
2622  // restore last good version
2623  ssl_state->curr_connp->version = prev_version;
2624  return SSL_DECODER_OK(input_len); // consume everything
2625  }
2626  } else {
2627  if (unknown_record) {
2628  SCLogDebug("unknown record, fatal");
2630  return SSL_DECODER_ERROR(-1);
2631  }
2632  }
2633 
2634  /* record_length should never be zero */
2635  if (ssl_state->curr_connp->record_length == 0) {
2636  SCLogDebug("SSLv3 Record length is 0");
2638  return SSL_DECODER_ERROR(-1);
2639  }
2640 
2641  if (!TLSVersionValid(ssl_state->curr_connp->version)) {
2642  SCLogDebug("ssl_state->curr_connp->version %04x", ssl_state->curr_connp->version);
2644  return SSL_DECODER_ERROR(-1);
2645  }
2646 
2647  if (ssl_state->curr_connp->bytes_processed == SSLV3_RECORD_HDR_LEN &&
2648  ssl_state->curr_connp->record_length > SSLV3_RECORD_MAX_LEN) {
2650  return SSL_DECODER_ERROR(-1);
2651  }
2652  DEBUG_VALIDATE_BUG_ON(ssl_state->curr_connp->bytes_processed > SSLV3_RECORD_HDR_LEN);
2653  } else {
2654  ValidateRecordState(ssl_state->curr_connp);
2655 
2656  record_len = (ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN)- ssl_state->curr_connp->bytes_processed;
2657  record_len = MIN(input_len, record_len);
2658  }
2659  SCLogDebug("record length %u processed %u got %u",
2660  ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed, record_len);
2661 
2662  /* if we don't have the full record, we return incomplete */
2663  if (ssl_state->curr_connp->record_length > input_len - parsed) {
2664  /* no need to use incomplete api buffering for application
2665  * records that we'll not use anyway. */
2666  if (ssl_state->curr_connp->content_type == SSLV3_APPLICATION_PROTOCOL) {
2667  SCLogDebug("application record");
2668  } else {
2669  uint32_t needed = ssl_state->curr_connp->record_length;
2670  SCLogDebug("record len %u input_len %u parsed %u: need %u bytes more data",
2671  ssl_state->curr_connp->record_length, input_len, parsed, needed);
2673  return SSL_DECODER_INCOMPLETE(parsed, needed);
2674  }
2675  }
2676 
2677  if (record_len == 0) {
2678  return SSL_DECODER_OK(parsed);
2679  }
2680 
2681  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input + parsed,
2682  ssl_state->curr_connp->record_length, direction, TLS_FRAME_DATA);
2683 
2684  switch (ssl_state->curr_connp->content_type) {
2685  /* we don't need any data from these types */
2687  ssl_state->flags |= SSL_AL_FLAG_CHANGE_CIPHER_SPEC;
2688 
2689  if (direction) {
2690  ssl_state->flags |= SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC;
2691  } else {
2692  ssl_state->flags |= SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC;
2693 
2694  // TODO TLS 1.3
2695  UpdateClientState(ssl_state, TLS_STATE_CLIENT_DATA);
2696  }
2697  break;
2698 
2699  case SSLV3_ALERT_PROTOCOL: {
2700  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input + parsed,
2701  ssl_state->curr_connp->record_length, direction, TLS_FRAME_ALERT_DATA);
2702 
2703  int retval = SSLv3ParseAlertProtocol(ssl_state, input + parsed, record_len, direction);
2704  if (retval < 0) {
2705  SCLogDebug("SSLv3ParseAlertProtocol returned %d", retval);
2706  return SSL_DECODER_ERROR(-1);
2707  }
2708  break;
2709  }
2711  /* In TLSv1.3 early data (0-RTT) could be sent before the
2712  handshake is complete (rfc8446, section 2.3). We should
2713  therefore not mark the handshake as done before we have
2714  seen the ServerHello record. */
2715  if ((ssl_state->flags & SSL_AL_FLAG_EARLY_DATA) &&
2716  ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0))
2717  break;
2718 
2719  /* if we see (encrypted) application data, then this means the
2720  handshake must be done */
2721  if (ssl_state->curr_connp == &ssl_state->client_connp) {
2722  UpdateClientState(ssl_state, TLS_STATE_CLIENT_DATA);
2723  } else {
2724  UpdateServerState(ssl_state, TLS_STATE_SERVER_DATA);
2725  }
2726 
2728  SCLogDebug("setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
2729  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD);
2730  }
2731 
2732  /* Encrypted data, reassembly not asked, bypass asked, let's sacrifice
2733  * heartbeat lke inspection to be able to be able to bypass the flow */
2735  SCLogDebug("setting APP_LAYER_PARSER_NO_REASSEMBLY");
2736  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_REASSEMBLY);
2737  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_INSPECTION);
2738  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_BYPASS_READY);
2739  }
2740  break;
2741 
2742  case SSLV3_HANDSHAKE_PROTOCOL: {
2743  if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
2744  /* In TLSv1.3, ChangeCipherSpec is only used for middlebox
2745  compatibility (rfc8446, appendix D.4). */
2746  // Client hello flags is needed to have a valid version
2747  if ((ssl_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
2748  (ssl_state->client_connp.version > TLS_VERSION_12) &&
2749  ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0)) {
2750  /* do nothing */
2751  } else {
2752  // if we started parsing this, we must stop
2753  break;
2754  }
2755  }
2756 
2757  if (ssl_state->curr_connp->record_length < 4) {
2758  SSLParserReset(ssl_state);
2760  SCLogDebug("record len < 4 => %u", ssl_state->curr_connp->record_length);
2761  return SSL_DECODER_ERROR(-1);
2762  }
2763 
2764  int retval = SSLv3ParseHandshakeProtocol(ssl_state, input + parsed,
2765  record_len, direction);
2766  SCLogDebug("retval %d", retval);
2767  if (retval < 0 || retval > (int)record_len) {
2768  DEBUG_VALIDATE_BUG_ON(retval > (int)record_len);
2770  SCLogDebug("SSLv3ParseHandshakeProtocol returned %d", retval);
2771  return SSL_DECODER_ERROR(-1);
2772  }
2773  ValidateRecordState(ssl_state->curr_connp);
2774  break;
2775  }
2776  case SSLV3_HEARTBEAT_PROTOCOL: {
2777  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input + parsed,
2778  ssl_state->curr_connp->record_length, direction, TLS_FRAME_HB_DATA);
2779  int retval = SSLv3ParseHeartbeatProtocol(ssl_state, input + parsed,
2780  record_len, direction);
2781  if (retval < 0) {
2782  SCLogDebug("SSLv3ParseHeartbeatProtocol returned %d", retval);
2783  return SSL_DECODER_ERROR(-1);
2784  }
2785  break;
2786  }
2787  default:
2788  // should be unreachable now that we check after header parsing
2790  SCLogDebug("unsupported record type");
2791  return SSL_DECODER_ERROR(-1);
2792  }
2793 
2794  parsed += record_len;
2795  ssl_state->curr_connp->bytes_processed += record_len;
2796 
2797  if (ssl_state->curr_connp->bytes_processed >=
2798  ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN) {
2799  SCLogDebug("record complete, trigger RAW");
2801  ssl_state->f, direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
2802  SSLParserReset(ssl_state);
2803  ValidateRecordState(ssl_state->curr_connp);
2804  return SSL_DECODER_OK(parsed);
2805 
2806  } else {
2807  /* we still don't have the entire record for the one we are
2808  currently parsing */
2809  ValidateRecordState(ssl_state->curr_connp);
2810  return SSL_DECODER_OK(parsed);
2811  }
2812 }
2813 
2814 /**
2815  * \internal
2816  * \brief SSLv2, SSLv23, SSLv3, TLSv1.1, TLSv1.2, TLSv1.3 parser.
2817  *
2818  * On parsing error, this should be the only function that should reset
2819  * the parser state, to avoid multiple functions in the chain resetting
2820  * the parser state.
2821  *
2822  * \param direction 0 for toserver, 1 for toclient.
2823  * \param alstate Pointer to the state.
2824  * \param pstate Application layer parser state for this session.
2825  * \param output Pointer to the list of parsed output elements.
2826  *
2827  * \todo On reaching an inconsistent state, check if the input has
2828  * another new record, instead of just returning after the reset
2829  *
2830  * \retval >=0 On success.
2831  */
2832 static AppLayerResult SSLDecode(Flow *f, uint8_t direction, void *alstate,
2833  AppLayerParserState *pstate, StreamSlice stream_slice)
2834 {
2835  SSLState *ssl_state = (SSLState *)alstate;
2836  ssl_state->tx_data.updated_tc = true;
2837  ssl_state->tx_data.updated_ts = true;
2838  uint32_t counter = 0;
2839  ssl_state->f = f;
2840  const uint8_t *input = StreamSliceGetData(&stream_slice);
2841  const uint8_t *init_input = input;
2842  int32_t input_len = (int32_t)StreamSliceGetDataLen(&stream_slice);
2843 
2844  if ((input == NULL || input_len == 0) &&
2845  ((direction == 0 && SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS)) ||
2846  (direction == 1 &&
2847  SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC)))) {
2848  /* flag session as finished if APP_LAYER_PARSER_EOF is set */
2849  if (direction == 0)
2850  UpdateClientState(ssl_state, TLS_STATE_CLIENT_FINISHED);
2851  else
2852  UpdateServerState(ssl_state, TLS_STATE_SERVER_FINISHED);
2854  } else if (input == NULL || input_len == 0) {
2856  }
2857 
2858  if (direction == 0)
2859  ssl_state->curr_connp = &ssl_state->client_connp;
2860  else
2861  ssl_state->curr_connp = &ssl_state->server_connp;
2862 
2863  /* If entering on a new record, reset the current flags. */
2864  if (ssl_state->curr_connp->bytes_processed == 0) {
2865  ssl_state->current_flags = 0;
2866  }
2867 
2868  /* if we have more than one record */
2869  uint32_t max_records = MAX((input_len / SSL_RECORD_MINIMUM_LENGTH),1);
2870  while (input_len > 0) {
2871  if (counter > max_records) {
2872  SCLogDebug("Looks like we have looped quite a bit. Reset state "
2873  "and get out of here");
2874  SSLParserReset(ssl_state);
2875  SSLSetEvent(ssl_state,
2877  return APP_LAYER_ERROR;
2878  }
2879 
2880  /* ssl_state->bytes_processed is zero for a fresh record or
2881  positive to indicate a record currently being parsed */
2882 
2883  if (ssl_state->curr_connp->bytes_processed == 0) {
2884  if ((input[0] & 0x80) || (input[0] & 0x40)) {
2885  /* only SSLv2, has one of the top 2 bits set */
2886  ssl_state->curr_connp->version = SSL_VERSION_2;
2887  SCLogDebug("SSLv2 detected");
2888  } else if (ssl_state->curr_connp->version == SSL_VERSION_2) {
2889  ssl_state->curr_connp->version = TLS_VERSION_UNKNOWN;
2890  SCLogDebug("SSL/TLS version reset");
2891  }
2892  }
2893  SCLogDebug("record %u: bytes_processed %u, version %02X, input_len %u", counter,
2894  ssl_state->curr_connp->bytes_processed, ssl_state->curr_connp->version, input_len);
2895 
2896  if (ssl_state->curr_connp->version == SSL_VERSION_2) {
2897  if (ssl_state->curr_connp->bytes_processed == 0) {
2898  SCLogDebug("New SSLv2 record parsing");
2899  } else {
2900  SCLogDebug("Continuing parsing SSLv2 record");
2901  }
2902  struct SSLDecoderResult r =
2903  SSLv2Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2904  if (r.retval < 0 || r.retval > input_len) {
2905  DEBUG_VALIDATE_BUG_ON(r.retval > input_len);
2906  SCLogDebug("Error parsing SSLv2. Resetting parser "
2907  "state. Let's get outta here");
2908  SSLParserReset(ssl_state);
2909  SSLSetEvent(ssl_state,
2911  return APP_LAYER_ERROR;
2912  } else if (r.needed) {
2913  input += r.retval;
2914  SCLogDebug("returning consumed %" PRIuMAX " needed %u",
2915  (uintmax_t)(input - init_input), r.needed);
2916  SCReturnStruct(APP_LAYER_INCOMPLETE((uint32_t)(input - init_input), r.needed));
2917  }
2918  input_len -= r.retval;
2919  input += r.retval;
2920  SCLogDebug("SSLv2 decoder consumed %d bytes: %u left", r.retval, input_len);
2921  } else {
2922  if (ssl_state->curr_connp->bytes_processed == 0) {
2923  SCLogDebug("New TLS record: record_length %u",
2924  ssl_state->curr_connp->record_length);
2925  } else {
2926  SCLogDebug("Continuing parsing TLS record: record_length %u, bytes_processed %u",
2927  ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed);
2928  }
2929  struct SSLDecoderResult r =
2930  SSLv3Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2931  if (r.retval < 0 || r.retval > input_len) {
2932  DEBUG_VALIDATE_BUG_ON(r.retval > input_len);
2933  SCLogDebug("Error parsing TLS. Resetting parser "
2934  "state. Let's get outta here");
2935  SSLParserReset(ssl_state);
2936  return APP_LAYER_ERROR;
2937  } else if (r.needed) {
2938  input += r.retval;
2939  SCLogDebug("returning consumed %" PRIuMAX " needed %u",
2940  (uintmax_t)(input - init_input), r.needed);
2941  SCReturnStruct(APP_LAYER_INCOMPLETE((uint32_t)(input - init_input), r.needed));
2942  }
2943  input_len -= r.retval;
2944  input += r.retval;
2945  SCLogDebug("TLS decoder consumed %d bytes: %u left", r.retval, input_len);
2946 
2948  && ssl_state->curr_connp->record_length == 0) {
2949  SCLogDebug("TLS empty record");
2950  /* empty record */
2951  SSLParserReset(ssl_state);
2952  }
2953  }
2954  counter++;
2955  } /* while (input_len) */
2956 
2957  /* mark handshake as done if we have subject and issuer; the
2958  * certificate data must be complete - a certificate message whose
2959  * decode failed (even with the rest of the chain decoded) leaves the
2960  * data incomplete until a certificate message decodes fully, so the
2961  * tracks must not cross the cert phase on partial data */
2962  if ((ssl_state->flags & SSL_AL_FLAG_NEED_CLIENT_CERT) &&
2963  !ssl_state->client_connp.cert_data_incomplete &&
2964  ssl_state->client_connp.cert0_subject && ssl_state->client_connp.cert0_issuerdn) {
2965  /* per track: a track's own certificate data decides its own track;
2966  * pushing the peer track unconditionally would jump it over the
2967  * certificate phase before its own certificate is parsed (and
2968  * progression is monotonic, so the phase could never be visited
2969  * afterwards) */
2970  UpdateClientState(ssl_state, TLS_STATE_CLIENT_DATA);
2971  /* an mTLS handshake is complete with the client certificate: by
2972  * the flight order the server certificate phase was entered
2973  * already, so advancing the server track skips nothing (matters
2974  * when its own chain decode failed and no server app data
2975  * follows) */
2976  if (ssl_state->server_state >= TLS_STATE_SERVER_CERT) {
2977  UpdateServerState(ssl_state, TLS_STATE_SERVER_DATA);
2978  }
2979  }
2980  if (!ssl_state->server_connp.cert_data_incomplete && ssl_state->server_connp.cert0_subject &&
2981  ssl_state->server_connp.cert0_issuerdn) {
2982  UpdateServerState(ssl_state, TLS_STATE_SERVER_DATA);
2983  }
2984 
2985  /* flag session as finished if APP_LAYER_PARSER_EOF is set */
2986  if (SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS) &&
2987  SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC)) {
2988  /* update both sides to keep existing behavior */
2989  UpdateClientState(ssl_state, TLS_STATE_CLIENT_FINISHED);
2990  UpdateServerState(ssl_state, TLS_STATE_SERVER_FINISHED);
2991  }
2992 
2993  return APP_LAYER_OK;
2994 }
2995 
2996 /* tls event content readiness: everything the logger may emit must be
2997  * final. cert0 fields are final as soon as any certificate decoded
2998  * into the slot - a later undecodable one in the same message does not
2999  * invalidate them (base parity: the old completion teleport logged on
3000  * fields alone). Where fields stay empty (resumption, a fully-garbage
3001  * chain), the track advance beyond the certificate phase makes the
3002  * data final; a track stuck below it can only end at close, where the
3003  * end-of-file pass logs. The cert_data_incomplete flag is not needed
3004  * here: it only gates phase advancement, and loggers run between
3005  * parses, never during one. The client certificate decision must also
3006  * be settled: the standard flight order puts the CertificateRequest
3007  * after the server Certificate, so server content readiness alone can
3008  * precede it - logging there would miss the client certificate
3009  * content and the decoder events a client flight may still raise (the
3010  * TLS tx logs once; base logged at server flight end, where both were
3011  * included). The decision is settled by the NEED_CLIENT_CERT flag, by
3012  * any message at or past the CertificateRequest position (server
3013  * flight: CertificateRequest, ServerHelloDone, Finished; client
3014  * flight: its Certificate or ClientKeyExchange - flag
3015  * SERVER_FLIGHT_DECIDED), by a change cipher spec record, or by the
3016  * client's first application data - a TLS 1.3 flight is encrypted
3017  * past the ServerHello, so its encrypted records are the mid-flow
3018  * settlement signal there (anything else leaves the log to close,
3019  * the generic release through SERVER_FINISHED). The mTLS branch is
3020  * separate and unaffected. */
3021 static bool TlsConnpLogReady(const SSLStateConnp *connp, const uint8_t track_done)
3022 {
3023  if (connp->cert0_subject && connp->cert0_issuerdn) {
3024  return true;
3025  }
3026  return track_done;
3027 }
3028 
3029 bool SSLV3TxLogReady(void *tx)
3030 {
3031  SSLState *ssl_state = (SSLState *)tx;
3032 
3033  if (!TlsConnpLogReady(
3034  &ssl_state->server_connp, ssl_state->server_state >= TLS_STATE_SERVER_DATA)) {
3035  return false;
3036  }
3037  if (ssl_state->flags & SSL_AL_FLAG_NEED_CLIENT_CERT) {
3038  if (!TlsConnpLogReady(
3039  &ssl_state->client_connp, ssl_state->client_state >= TLS_STATE_CLIENT_DATA)) {
3040  return false;
3041  }
3042  } else if ((ssl_state->flags &
3044  ssl_state->server_state < TLS_STATE_SERVER_FINISHED &&
3045  ssl_state->client_state < TLS_STATE_CLIENT_DATA) {
3046  /* a CertificateRequest may still arrive in this flight */
3047  return false;
3048  }
3049  return true;
3050 }
3051 
3052 static AppLayerResult SSLParseClientRecord(Flow *f, void *alstate, AppLayerParserState *pstate,
3053  StreamSlice stream_slice, void *local_data)
3054 {
3055  return SSLDecode(f, 0 /* toserver */, alstate, pstate, stream_slice);
3056 }
3057 
3058 static AppLayerResult SSLParseServerRecord(Flow *f, void *alstate, AppLayerParserState *pstate,
3059  StreamSlice stream_slice, void *local_data)
3060 {
3061  return SSLDecode(f, 1 /* toclient */, alstate, pstate, stream_slice);
3062 }
3063 
3064 /**
3065  * \internal
3066  * \brief Function to allocate the SSL state memory.
3067  */
3068 static void *SSLStateAlloc(void *orig_state, AppProto proto_orig)
3069 {
3070  SSLState *ssl_state = SCCalloc(1, sizeof(SSLState));
3071  if (unlikely(ssl_state == NULL))
3072  return NULL;
3073  TAILQ_INIT(&ssl_state->server_connp.certs);
3074  TAILQ_INIT(&ssl_state->client_connp.certs);
3075 
3076  return (void *)ssl_state;
3077 }
3078 
3079 static void SSLStateCertSANFree(SSLStateConnp *connp)
3080 {
3081  if (connp->cert0_sans) {
3082  for (uint16_t i = 0; i < connp->cert0_sans_num; i++) {
3083  SCX509ArrayFree(connp->cert0_sans[i].san, connp->cert0_sans[i].san_len);
3084  }
3085  SCFree(connp->cert0_sans);
3086  }
3087 }
3088 
3089 /**
3090  * \internal
3091  * \brief Function to free the SSL state memory.
3092  */
3093 static void SSLStateFree(void *p)
3094 {
3095  SSLState *ssl_state = (SSLState *)p;
3096  SSLCertsChain *item;
3097 
3098  if (ssl_state->client_connp.cert0_subject)
3099  SCX509ArrayFree(
3100  ssl_state->client_connp.cert0_subject, ssl_state->client_connp.cert0_subject_len);
3101  if (ssl_state->client_connp.cert0_issuerdn)
3102  SCX509ArrayFree(
3104  if (ssl_state->client_connp.cert0_serial)
3105  SCX509ArrayFree(
3106  ssl_state->client_connp.cert0_serial, ssl_state->client_connp.cert0_serial_len);
3107  if (ssl_state->client_connp.cert0_fingerprint)
3109  if (ssl_state->client_connp.sni)
3110  SCFree(ssl_state->client_connp.sni);
3111  if (ssl_state->client_connp.session_id)
3112  SCFree(ssl_state->client_connp.session_id);
3113  if (ssl_state->client_connp.hs_buffer)
3114  SCFree(ssl_state->client_connp.hs_buffer);
3115 
3116  if (ssl_state->server_connp.cert0_subject)
3117  SCX509ArrayFree(
3118  ssl_state->server_connp.cert0_subject, ssl_state->server_connp.cert0_subject_len);
3119  if (ssl_state->server_connp.cert0_issuerdn)
3120  SCX509ArrayFree(
3122  if (ssl_state->server_connp.cert0_serial)
3123  SCX509ArrayFree(
3124  ssl_state->server_connp.cert0_serial, ssl_state->server_connp.cert0_serial_len);
3125  if (ssl_state->server_connp.cert0_fingerprint)
3127  if (ssl_state->server_connp.sni)
3128  SCFree(ssl_state->server_connp.sni);
3129  if (ssl_state->server_connp.session_id)
3130  SCFree(ssl_state->server_connp.session_id);
3131 
3132  if (ssl_state->client_connp.hs)
3133  SCTLSHandshakeFree(ssl_state->client_connp.hs);
3134  if (ssl_state->client_connp.ja3_str)
3135  Ja3BufferFree(&ssl_state->client_connp.ja3_str);
3136  if (ssl_state->client_connp.ja3_hash)
3137  SCFree(ssl_state->client_connp.ja3_hash);
3138  if (ssl_state->server_connp.hs)
3139  SCTLSHandshakeFree(ssl_state->server_connp.hs);
3140  if (ssl_state->server_connp.ja3_str)
3141  Ja3BufferFree(&ssl_state->server_connp.ja3_str);
3142  if (ssl_state->server_connp.ja3_hash)
3143  SCFree(ssl_state->server_connp.ja3_hash);
3144  if (ssl_state->server_connp.hs_buffer)
3145  SCFree(ssl_state->server_connp.hs_buffer);
3146 
3147  SSLStateCertSANFree(&ssl_state->server_connp);
3148  SSLStateCertSANFree(&ssl_state->client_connp);
3149 
3150  SCAppLayerTxDataCleanup(&ssl_state->tx_data);
3151 
3152  /* Free certificate chain */
3153  if (ssl_state->server_connp.certs_buffer)
3154  SCFree(ssl_state->server_connp.certs_buffer);
3155  while ((item = TAILQ_FIRST(&ssl_state->server_connp.certs))) {
3156  TAILQ_REMOVE(&ssl_state->server_connp.certs, item, next);
3157  SCFree(item);
3158  }
3159  TAILQ_INIT(&ssl_state->server_connp.certs);
3160  /* Free certificate chain */
3161  if (ssl_state->client_connp.certs_buffer)
3162  SCFree(ssl_state->client_connp.certs_buffer);
3163  while ((item = TAILQ_FIRST(&ssl_state->client_connp.certs))) {
3164  TAILQ_REMOVE(&ssl_state->client_connp.certs, item, next);
3165  SCFree(item);
3166  }
3167  TAILQ_INIT(&ssl_state->client_connp.certs);
3168 
3169  SCFree(ssl_state);
3170 }
3171 
3172 static void SSLStateTransactionFree(void *state, uint64_t tx_id)
3173 {
3174  /* do nothing */
3175 }
3176 
3177 static AppProto SSLProbingParser(
3178  const Flow *f, uint8_t direction, const uint8_t *input, uint32_t ilen, uint8_t *rdir)
3179 {
3180  /* probably a rst/fin sending an eof */
3181  if (ilen < 3)
3182  return ALPROTO_UNKNOWN;
3183 
3184  /* for now just the 3 byte header ones */
3185  /* \todo Detect the 2 byte ones */
3186  if ((input[0] & 0x80) && (input[2] == 0x01)) {
3187  return ALPROTO_TLS;
3188  }
3189 
3190  return ALPROTO_FAILED;
3191 }
3192 
3193 static int SSLStateGetStateIdByName(const char *name, const uint8_t direction)
3194 {
3195  SCEnumCharMap *map =
3196  direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
3197 
3198  int id = SCMapEnumNameToValue(name, map);
3199  if (id < 0) {
3200  return -1;
3201  }
3202  return id;
3203 }
3204 
3205 static const char *SSLStateGetStateNameById(const int id, const uint8_t direction)
3206 {
3207  SCEnumCharMap *map =
3208  direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
3209  const char *name = SCMapEnumValueToName(id, map);
3210  return name;
3211 }
3212 
3213 static int SSLStateGetFrameIdByName(const char *frame_name)
3214 {
3215  int id = SCMapEnumNameToValue(frame_name, tls_frame_table);
3216  if (id < 0) {
3217  return -1;
3218  }
3219  return id;
3220 }
3221 
3222 static const char *SSLStateGetFrameNameById(const uint8_t frame_id)
3223 {
3224  const char *name = SCMapEnumValueToName(frame_id, tls_frame_table);
3225  return name;
3226 }
3227 
3228 static int SSLStateGetEventInfo(
3229  const char *event_name, uint8_t *event_id, AppLayerEventType *event_type)
3230 {
3231  if (SCAppLayerGetEventIdByName(event_name, tls_decoder_event_table, event_id) == 0) {
3232  *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
3233  return 0;
3234  }
3235  return -1;
3236 }
3237 
3238 static int SSLStateGetEventInfoById(
3239  uint8_t event_id, const char **event_name, AppLayerEventType *event_type)
3240 {
3241  *event_name = SCMapEnumValueToName(event_id, tls_decoder_event_table);
3242  if (*event_name == NULL) {
3243  SCLogError("event \"%d\" not present in "
3244  "ssl's enum map table.",
3245  event_id);
3246  /* yes this is fatal */
3247  return -1;
3248  }
3249 
3250  *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
3251 
3252  return 0;
3253 }
3254 
3255 static int SSLRegisterPatternsForProtocolDetection(void)
3256 {
3257  if (SCAppLayerProtoDetectPMRegisterPatternCSwPP(IPPROTO_TCP, ALPROTO_TLS, "|01 00 02|", 5, 2,
3258  STREAM_TOSERVER, SSLProbingParser, 0, 3) < 0) {
3259  return -1;
3260  }
3261 
3262  /** SSLv3 */
3264  IPPROTO_TCP, ALPROTO_TLS, "|01 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
3265  return -1;
3266  }
3268  IPPROTO_TCP, ALPROTO_TLS, "|16 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
3269  return -1;
3270  }
3271 
3272  /** TLSv1 */
3274  IPPROTO_TCP, ALPROTO_TLS, "|01 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
3275  return -1;
3276  }
3278  IPPROTO_TCP, ALPROTO_TLS, "|16 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
3279  return -1;
3280  }
3281 
3282  /** TLSv1.1 */
3284  IPPROTO_TCP, ALPROTO_TLS, "|01 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
3285  return -1;
3286  }
3288  IPPROTO_TCP, ALPROTO_TLS, "|16 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
3289  return -1;
3290  }
3291 
3292  /** TLSv1.2 */
3294  IPPROTO_TCP, ALPROTO_TLS, "|01 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
3295  return -1;
3296  }
3298  IPPROTO_TCP, ALPROTO_TLS, "|16 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
3299  return -1;
3300  }
3301 
3302  /***** toclient direction *****/
3303 
3305  IPPROTO_TCP, ALPROTO_TLS, "|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3306  return -1;
3307  }
3309  IPPROTO_TCP, ALPROTO_TLS, "|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3310  return -1;
3311  }
3313  IPPROTO_TCP, ALPROTO_TLS, "|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3314  return -1;
3315  }
3316 
3317  /** TLSv1 */
3319  IPPROTO_TCP, ALPROTO_TLS, "|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3320  return -1;
3321  }
3323  IPPROTO_TCP, ALPROTO_TLS, "|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3324  return -1;
3325  }
3327  IPPROTO_TCP, ALPROTO_TLS, "|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3328  return -1;
3329  }
3330 
3331  /** TLSv1.1 */
3333  IPPROTO_TCP, ALPROTO_TLS, "|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3334  return -1;
3335  }
3337  IPPROTO_TCP, ALPROTO_TLS, "|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3338  return -1;
3339  }
3341  IPPROTO_TCP, ALPROTO_TLS, "|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3342  return -1;
3343  }
3344 
3345  /** TLSv1.2 */
3347  IPPROTO_TCP, ALPROTO_TLS, "|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3348  return -1;
3349  }
3351  IPPROTO_TCP, ALPROTO_TLS, "|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3352  return -1;
3353  }
3355  IPPROTO_TCP, ALPROTO_TLS, "|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3356  return -1;
3357  }
3358 
3359  /* Subsection - SSLv2 style record by client, but informing the server
3360  * the max version it supports.
3361  * Updated by Anoop Saldanha. Disabled it for now. We'll get back to
3362  * it after some tests */
3363 #if 0
3365  "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
3366  {
3367  return -1;
3368  }
3370  "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
3371  {
3372  return -1;
3373  }
3374 #endif
3375 
3376  return 0;
3377 }
3378 
3379 #ifdef HAVE_JA3
3380 static void CheckJA3Enabled(void)
3381 {
3382  const char *strval = NULL;
3383  /* Check if we should generate JA3 fingerprints */
3384  int enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
3385  if (SCConfGetNonNull("app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
3386  enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
3387  } else if (strcmp(strval, "auto") == 0) {
3388  enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
3389  } else if (SCConfValIsFalse(strval)) {
3390  enable_ja3 = 0;
3391  ssl_config.disable_ja3 = true;
3392  } else if (SCConfValIsTrue(strval)) {
3393  enable_ja3 = true;
3394  }
3395  SC_ATOMIC_SET(ssl_config.enable_ja3, enable_ja3);
3397  /* The feature is available, i.e. _could_ be activated by a rule or
3398  even is enabled in the configuration. */
3400  }
3401 }
3402 #endif /* HAVE_JA3 */
3403 
3404 #ifdef HAVE_JA4
3405 static void CheckJA4Enabled(void)
3406 {
3407  const char *strval = NULL;
3408  /* Check if we should generate JA4 fingerprints */
3409  int enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3410  if (SCConfGetNonNull("app-layer.protocols.tls.ja4-fingerprints", &strval) != 1) {
3411  enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3412  } else if (strcmp(strval, "auto") == 0) {
3413  enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3414  } else if (SCConfValIsFalse(strval)) {
3415  enable_ja4 = 0;
3416  ssl_config.disable_ja4 = true;
3417  } else if (SCConfValIsTrue(strval)) {
3418  enable_ja4 = true;
3419  }
3420  SC_ATOMIC_SET(ssl_config.enable_ja4, enable_ja4);
3422  /* The feature is available, i.e. _could_ be activated by a rule or
3423  even is enabled in the configuration. */
3425  }
3426 }
3427 #endif /* HAVE_JA4 */
3428 
3429 /**
3430  * \brief Function to register the SSL protocol parser and other functions
3431  */
3432 #ifdef UNITTESTS
3433 void SSLStateParserRegisterTests(void);
3434 #endif /* UNITTESTS */
3435 
3437 {
3438  const char *proto_name = "tls";
3439 
3440  SC_ATOMIC_INIT(ssl_config.enable_ja3);
3441 
3442  /** SSLv2 and SSLv23*/
3443  if (SCAppLayerProtoDetectConfProtoDetectionEnabled("tcp", proto_name)) {
3445 
3446  if (SSLRegisterPatternsForProtocolDetection() < 0)
3447  return;
3448 
3449  if (RunmodeIsUnittests()) {
3451  IPPROTO_TCP, "443", ALPROTO_TLS, 0, 3, STREAM_TOSERVER, SSLProbingParser, NULL);
3452  } else {
3453  if (SCAppLayerProtoDetectPPParseConfPorts("tcp", IPPROTO_TCP, proto_name, ALPROTO_TLS,
3454  0, 3, SSLProbingParser, NULL) == 0) {
3455  SCLogConfig("no TLS config found, "
3456  "enabling TLS detection on port 443.");
3457  SCAppLayerProtoDetectPPRegister(IPPROTO_TCP, "443", ALPROTO_TLS, 0, 3,
3458  STREAM_TOSERVER, SSLProbingParser, NULL);
3459  }
3460  }
3461  } else {
3462  SCLogConfig("Protocol detection and parser disabled for %s protocol",
3463  proto_name);
3464  return;
3465  }
3466 
3467  if (SCAppLayerParserConfParserEnabled("tcp", proto_name)) {
3468  AppLayerParserRegisterParser(IPPROTO_TCP, ALPROTO_TLS, STREAM_TOSERVER,
3469  SSLParseClientRecord);
3470 
3471  AppLayerParserRegisterParser(IPPROTO_TCP, ALPROTO_TLS, STREAM_TOCLIENT,
3472  SSLParseServerRecord);
3474  IPPROTO_TCP, ALPROTO_TLS, SSLStateGetStateIdByName, SSLStateGetStateNameById);
3476  IPPROTO_TCP, ALPROTO_TLS, SSLStateGetFrameIdByName, SSLStateGetFrameNameById);
3477  AppLayerParserRegisterGetEventInfo(IPPROTO_TCP, ALPROTO_TLS, SSLStateGetEventInfo);
3478  AppLayerParserRegisterGetEventInfoById(IPPROTO_TCP, ALPROTO_TLS, SSLStateGetEventInfoById);
3479 
3480  AppLayerParserRegisterStateFuncs(IPPROTO_TCP, ALPROTO_TLS, SSLStateAlloc, SSLStateFree);
3481 
3483  IPPROTO_TCP, ALPROTO_TLS, STREAM_TOSERVER);
3484 
3485  AppLayerParserRegisterTxFreeFunc(IPPROTO_TCP, ALPROTO_TLS, SSLStateTransactionFree);
3486 
3487  AppLayerParserRegisterGetTx(IPPROTO_TCP, ALPROTO_TLS, SSLGetTx);
3488  AppLayerParserRegisterTxDataFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetTxData);
3489  AppLayerParserRegisterStateDataFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetStateData);
3490 
3491  AppLayerParserRegisterGetTxCnt(IPPROTO_TCP, ALPROTO_TLS, SSLGetTxCnt);
3492 
3493  AppLayerParserRegisterGetStateProgressFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetAlstateProgress);
3494 
3497 
3498 #ifdef UNITTESTS
3501 #endif /* UNITTESTS */
3502 
3503  SCConfNode *enc_handle = SCConfGetNode("app-layer.protocols.tls.encryption-handling");
3504  if (enc_handle != NULL && enc_handle->val != NULL) {
3505  SCLogDebug("have app-layer.protocols.tls.encryption-handling = %s", enc_handle->val);
3506  if (strcmp(enc_handle->val, "full") == 0) {
3508  } else if (strcmp(enc_handle->val, "bypass") == 0) {
3510  } else if (strcmp(enc_handle->val, "track-only") == 0) {
3512  } else if (strcmp(enc_handle->val, "default") == 0) {
3513  SCLogWarning("app-layer.protocols.tls.encryption-handling = default is deprecated "
3514  "and will be removed in Suricata 9, use \"track-only\" instead, "
3515  "(see ticket #7642)");
3517  } else {
3519  }
3520  } else {
3521  /* Get the value of no reassembly option from the config file */
3522  if (SCConfGetNode("app-layer.protocols.tls.no-reassemble") == NULL) {
3523  int value = 0;
3524  if (SCConfGetBool("tls.no-reassemble", &value) == 1 && value == 1)
3526  } else {
3527  int value = 0;
3528  if (SCConfGetBool("app-layer.protocols.tls.no-reassemble", &value) == 1 &&
3529  value == 1)
3531  }
3532  }
3533  SCLogDebug("ssl_config.encrypt_mode %u", ssl_config.encrypt_mode);
3534 
3535 #ifdef HAVE_JA3
3536  CheckJA3Enabled();
3537 #endif /* HAVE_JA3 */
3538 #ifdef HAVE_JA4
3539  CheckJA4Enabled();
3540 #endif /* HAVE_JA4 */
3541 
3542  if (g_disable_hashing) {
3543  if (SC_ATOMIC_GET(ssl_config.enable_ja3)) {
3544  SCLogWarning("MD5 calculation has been disabled, disabling JA3");
3545  SC_ATOMIC_SET(ssl_config.enable_ja3, 0);
3546  }
3547  if (SC_ATOMIC_GET(ssl_config.enable_ja4)) {
3548  SCLogWarning("Hashing has been disabled, disabling JA4");
3549  SC_ATOMIC_SET(ssl_config.enable_ja4, 0);
3550  }
3551  } else {
3552  if (RunmodeIsUnittests()) {
3553 #ifdef HAVE_JA3
3554  SC_ATOMIC_SET(ssl_config.enable_ja3, 1);
3555 #endif /* HAVE_JA3 */
3556 #ifdef HAVE_JA4
3557  SC_ATOMIC_SET(ssl_config.enable_ja4, 1);
3558 #endif /* HAVE_JA4 */
3559  }
3560  }
3561  } else {
3562  SCLogConfig("Parser disabled for %s protocol. Protocol detection still on.", proto_name);
3563  }
3564 }
3565 
3566 /**
3567  * \brief if not explicitly disabled in config, enable ja3 support
3568  *
3569  * Implemented using atomic to allow rule reloads to do this at
3570  * runtime.
3571  */
3572 void SSLEnableJA3(void)
3573 {
3575  return;
3576  }
3577  if (SC_ATOMIC_GET(ssl_config.enable_ja3)) {
3578  return;
3579  }
3580  SC_ATOMIC_SET(ssl_config.enable_ja3, 1);
3581 }
3582 
3583 /**
3584  * \brief if not explicitly disabled in config, enable ja4 support
3585  *
3586  * Implemented using atomic to allow rule reloads to do this at
3587  * runtime.
3588  */
3589 void SSLEnableJA4(void)
3590 {
3591  // only caller has #ifdef HAVE_JA4
3593  return;
3594  }
3595  if (SC_ATOMIC_GET(ssl_config.enable_ja4)) {
3596  return;
3597  }
3598  SC_ATOMIC_SET(ssl_config.enable_ja4, 1);
3599 }
3600 
3601 /**
3602  * \brief return whether ja3 is effectively enabled
3603  *
3604  * This means that it either has been enabled explicitly or has been
3605  * enabled by having loaded a rule while not being explicitly disabled.
3606  *
3607  * \retval true if enabled, false otherwise
3608  */
3610 {
3611  return SC_ATOMIC_GET(ssl_config.enable_ja3);
3612 }
3613 
3614 /**
3615  * \brief return whether ja4 is effectively enabled
3616  *
3617  * This means that it either has been enabled explicitly or has been
3618  * enabled by having loaded a rule while not being explicitly disabled.
3619  *
3620  * \retval true if enabled, false otherwise
3621  */
3623 {
3624  return SC_ATOMIC_GET(ssl_config.enable_ja4);
3625 }
3626 
3627 #ifdef UNITTESTS
3628 static int TLSPhaseStateTestClient(void)
3629 {
3630  const char *names[] = { "client_started", "client_hello", "client_cert", "client_data",
3631  "client_finished" };
3632  for (int i = 0; i < 5; i++) {
3634  IPPROTO_TCP, ALPROTO_TLS, names[i], STREAM_TOSERVER) != i);
3635  FAIL_IF(strcmp(AppLayerParserGetStateNameById(IPPROTO_TCP, ALPROTO_TLS, i, STREAM_TOSERVER),
3636  names[i]) != 0);
3637  }
3638  PASS;
3639 }
3640 
3641 static int TLSPhaseStateTestServer(void)
3642 {
3643  const char *names[] = { "server_started", "server_hello", "server_cert", "server_data",
3644  "server_finished" };
3645  for (int i = 0; i < 5; i++) {
3647  IPPROTO_TCP, ALPROTO_TLS, names[i], STREAM_TOCLIENT) != i);
3648  FAIL_IF(strcmp(AppLayerParserGetStateNameById(IPPROTO_TCP, ALPROTO_TLS, i, STREAM_TOCLIENT),
3649  names[i]) != 0);
3650  }
3651  PASS;
3652 }
3653 
3654 static int TLSPhaseStateTestOldNamesGone(void)
3655 {
3656  const char *old_client[] = { "client_in_progress", "client_hello_done", "client_cert_done",
3657  "client_handshake_done" };
3658  const char *old_server[] = { "server_in_progress", "server_hello_done", "server_cert_done",
3659  "server_handshake_done" };
3660  for (int i = 0; i < 4; i++) {
3662  IPPROTO_TCP, ALPROTO_TLS, old_client[i], STREAM_TOSERVER) >= 0);
3664  IPPROTO_TCP, ALPROTO_TLS, old_server[i], STREAM_TOCLIENT) >= 0);
3665  }
3666  PASS;
3667 }
3668 
3669 static int TLSPhaseStateTestCompletion(void)
3670 {
3673  /* progression is monotonic: a backwards write is a no-op (a peer track
3674  * shortcut must never undo a phase), an equal write passes */
3675  SSLState *ssl_state = SSLStateAlloc(NULL, ALPROTO_TLS);
3676  FAIL_IF(ssl_state == NULL);
3677  UpdateServerState(ssl_state, TLS_STATE_SERVER_DATA);
3678  UpdateServerState(ssl_state, TLS_STATE_SERVER_CERT);
3680  UpdateServerState(ssl_state, TLS_STATE_SERVER_DATA);
3682  SSLStateFree(ssl_state);
3683  PASS;
3684 }
3685 
3686 /* Drive a truncated ClientHello, a valid ServerHello and a garbage
3687  * Certificate through the handshake parser: a message enters its phase
3688  * at its start, a failed decode blocks only the hand-over to the next
3689  * phase - the bytes are consumed, the next message parses normally, and
3690  * a hello failure keeps the track in the hello phase. */
3691 static int TLSPhaseSuppressionTest(void)
3692 {
3693  SSLState *ssl_state = SSLStateAlloc(NULL, ALPROTO_TLS);
3694  if (ssl_state == NULL)
3695  return 0;
3696 
3697  /* P4: ClientHello, message length 6 - version parses, random
3698  * truncated */
3699  const uint8_t p4[] = { 0x01, 0x00, 0x00, 0x06, 0x03, 0x03, 0x01, 0x02, 0x03, 0x04 };
3700  /* P5: valid ServerHello (version, random, session id, selected
3701  * cipher, compression; no extensions - optional per RFC5246) */
3702  uint8_t p5[4 + 2 + 32 + 1 + 1 + 2 + 1];
3703  int i;
3704  int rc;
3705 
3706  p5[0] = 0x02;
3707  p5[1] = 0;
3708  p5[2] = 0;
3709  p5[3] = 39;
3710  p5[4] = 0x03;
3711  p5[5] = 0x03;
3712  for (i = 0; i < 32; i++)
3713  p5[6 + i] = (uint8_t)i;
3714  p5[38] = 1; /* session id length */
3715  p5[39] = 0xaa; /* session id */
3716  p5[40] = 0x13;
3717  p5[41] = 0x01; /* selected cipher suite */
3718  p5[42] = 0x00; /* compression */
3719  /* P6: Certificate with a 10-byte garbage body (not ASN.1) */
3720  const uint8_t p6[] = { 0x0b, 0x00, 0x00, 0x10, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x0a, 0x41, 0x41,
3721  0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 };
3722 
3723  /* truncated hello: entered at its start, the failed decode blocks
3724  * the hand-over, so the client stays in client_hello */
3725  ssl_state->curr_connp = &ssl_state->client_connp;
3726  ssl_state->curr_connp->record_length = sizeof(p4);
3727  ssl_state->curr_connp->bytes_processed = 0;
3728  ssl_state->curr_connp->version = 0x0303;
3729  rc = SSLv3ParseHandshakeProtocol(ssl_state, p4, sizeof(p4), 0);
3730  FAIL_IF(rc < 0);
3731  FAIL_IF(!ssl_state->client_connp.phase_suppressed);
3733 
3734  /* valid hello: the flag is reset per message, completion hands the
3735  * track to the cert phase */
3736  ssl_state->curr_connp = &ssl_state->server_connp;
3737  ssl_state->curr_connp->record_length = sizeof(p5);
3738  ssl_state->curr_connp->bytes_processed = 0;
3739  ssl_state->curr_connp->version = 0x0303;
3740  rc = SSLv3ParseHandshakeProtocol(ssl_state, p5, sizeof(p5), 1);
3741  FAIL_IF(rc < 0);
3744  /* the cert phase is entered with the hello, the chain is not
3745  * parsed yet: chain content is not final */
3747 
3748  /* garbage certificate: the cert phase was entered at the message
3749  * header, the failed decode keeps the track in it; the suppression
3750  * flag is hello-specific */
3751  ssl_state->curr_connp = &ssl_state->server_connp;
3752  ssl_state->curr_connp->record_length = sizeof(p6);
3753  ssl_state->curr_connp->bytes_processed = 0;
3754  rc = SSLv3ParseHandshakeProtocol(ssl_state, p6, sizeof(p6), 1);
3755  FAIL_IF(rc < 0);
3760 
3761  SSLStateFree(ssl_state);
3762  PASS;
3763 }
3764 
3765 /* A Certificate message whose first certificate is undecodable but whose
3766  * remaining certificates are valid: the bad leaf is skipped (one decoder
3767  * event), the rest of the chain is kept and the cert0 fields are filled
3768  * from the first decodable certificate (compared against a direct
3769  * extraction from the fixture). The message enters the cert phase at its
3770  * start; the incomplete-data condition is sticky - a following
3771  * ServerHelloDone resets the per-message suppression but the track stays
3772  * in the cert phase, out of the data phase. The valid certificate body
3773  * is the same fixture as the sv test ruletype-firewall-153 client
3774  * certificate. */
3775 static int TLSPhaseCertChainSkipTest(void)
3776 {
3777  static const uint8_t valid_cert[] = { 0x30, 0x82, 0x03, 0x0b, 0x30, 0x82, 0x01, 0xf3, 0xa0,
3778  0x03, 0x02, 0x01, 0x02, 0x02, 0x14, 0x16, 0x1b, 0x77, 0xa9, 0xae, 0xe9, 0xea, 0xaa, 0x2d,
3779  0xf9, 0xbb, 0xd6, 0xda, 0x13, 0x92, 0xf8, 0x73, 0xf8, 0x93, 0xa0, 0x30, 0x0d, 0x06, 0x09,
3780  0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x15, 0x31, 0x13,
3781  0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0a, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74,
3782  0x74, 0x65, 0x73, 0x74, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x36, 0x30, 0x39, 0x31, 0x38, 0x30,
3783  0x33, 0x34, 0x38, 0x35, 0x36, 0x5a, 0x17, 0x0d, 0x32, 0x36, 0x30, 0x39, 0x32, 0x30, 0x30,
3784  0x33, 0x34, 0x38, 0x35, 0x36, 0x5a, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
3785  0x04, 0x03, 0x0c, 0x0a, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x74, 0x65, 0x73, 0x74, 0x30,
3786  0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
3787  0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01,
3788  0x01, 0x00, 0xc7, 0xdc, 0x32, 0x3c, 0x76, 0x5d, 0x86, 0x02, 0xbd, 0x3e, 0xc0, 0x8e, 0x6f,
3789  0x34, 0x7e, 0xa2, 0x2c, 0x66, 0xe4, 0x20, 0x2f, 0x1c, 0xf4, 0x6c, 0x4d, 0xb6, 0x8e, 0xed,
3790  0x5c, 0xdf, 0x36, 0xdb, 0xa8, 0xea, 0xf9, 0x01, 0xbe, 0x20, 0x92, 0x85, 0x72, 0x9c, 0x5d,
3791  0x19, 0xf5, 0xa1, 0xa6, 0xdf, 0x60, 0x17, 0xc3, 0xba, 0xb7, 0xed, 0x57, 0x99, 0x19, 0x46,
3792  0xee, 0xb2, 0x04, 0xfe, 0x18, 0x17, 0x65, 0x62, 0xe2, 0xd7, 0x45, 0xb1, 0x0f, 0x0c, 0xc8,
3793  0x6b, 0xe4, 0x63, 0x4a, 0x6b, 0xd9, 0x30, 0xae, 0xf5, 0x81, 0xc5, 0xac, 0x50, 0x51, 0xc6,
3794  0x91, 0x5c, 0x78, 0xc0, 0x74, 0x2a, 0x47, 0x6b, 0xae, 0xc3, 0x4b, 0x7d, 0x54, 0x66, 0x0d,
3795  0xd7, 0xa1, 0x81, 0x07, 0x80, 0x44, 0xba, 0x30, 0x02, 0xeb, 0x82, 0x3c, 0x78, 0xbb, 0x9f,
3796  0x2b, 0xeb, 0x1b, 0x1c, 0x30, 0x41, 0x02, 0xeb, 0xf5, 0x9c, 0xb3, 0xef, 0x43, 0x52, 0x4b,
3797  0x54, 0x1f, 0xf3, 0x0d, 0xeb, 0xcc, 0x25, 0x97, 0xda, 0x52, 0x07, 0xdf, 0x54, 0x35, 0x4b,
3798  0x85, 0x63, 0x67, 0xd6, 0x60, 0x9f, 0x0f, 0x88, 0xbf, 0x7e, 0xc9, 0x44, 0x2f, 0x34, 0xd6,
3799  0x28, 0xad, 0x2d, 0x59, 0x7e, 0x56, 0xb6, 0x73, 0x5a, 0xe0, 0xb0, 0x1c, 0xc9, 0x23, 0x5f,
3800  0x2a, 0x3c, 0x46, 0x1c, 0x27, 0x7f, 0x0e, 0x25, 0x16, 0x19, 0xb0, 0x03, 0x71, 0x05, 0xd5,
3801  0xc6, 0xed, 0x49, 0x7c, 0x90, 0xca, 0x94, 0xfb, 0xe4, 0x75, 0x23, 0x67, 0x17, 0x37, 0x08,
3802  0x05, 0xb8, 0x58, 0x7f, 0x61, 0x16, 0x24, 0x91, 0xeb, 0x0c, 0x1c, 0x05, 0x0d, 0xa5, 0x45,
3803  0x55, 0x03, 0x2e, 0x60, 0x15, 0xc4, 0x6b, 0xcb, 0x68, 0x1f, 0xec, 0xbe, 0x59, 0xbf, 0x4a,
3804  0x93, 0x0b, 0xa3, 0x08, 0x86, 0xe0, 0xe9, 0x5c, 0x08, 0x23, 0x3b, 0x0f, 0xbd, 0xc9, 0x91,
3805  0xa1, 0x48, 0xf9, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x53, 0x30, 0x51, 0x30, 0x1d, 0x06,
3806  0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xae, 0xed, 0xef, 0x6c, 0xe0, 0xf6, 0xb7,
3807  0xe3, 0x8c, 0xd9, 0x37, 0x7f, 0x0b, 0x53, 0x36, 0x96, 0x06, 0x5d, 0x0f, 0x09, 0x30, 0x1f,
3808  0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xae, 0xed, 0xef, 0x6c,
3809  0xe0, 0xf6, 0xb7, 0xe3, 0x8c, 0xd9, 0x37, 0x7f, 0x0b, 0x53, 0x36, 0x96, 0x06, 0x5d, 0x0f,
3810  0x09, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03,
3811  0x01, 0x01, 0xff, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
3812  0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x08, 0x2e, 0x15, 0x02, 0x17, 0xcb, 0xf8,
3813  0x5b, 0x4b, 0x31, 0x47, 0xf4, 0x8d, 0x55, 0x53, 0xa4, 0x05, 0x46, 0x73, 0xb7, 0x6a, 0x79,
3814  0xfc, 0x02, 0xe4, 0x89, 0x09, 0xb9, 0x3d, 0x8c, 0xda, 0x98, 0x0d, 0x67, 0x83, 0x30, 0x00,
3815  0xe5, 0x3b, 0xc4, 0x7c, 0xc4, 0x63, 0xa7, 0x20, 0x97, 0x74, 0x8d, 0x18, 0xfa, 0xb1, 0xe7,
3816  0x67, 0x13, 0x48, 0x55, 0x99, 0xde, 0xca, 0x70, 0xe4, 0x7a, 0x3a, 0x65, 0x28, 0x08, 0xce,
3817  0xc3, 0x93, 0xbb, 0x95, 0x15, 0x10, 0x25, 0x67, 0x7a, 0xad, 0xe8, 0xbc, 0xfb, 0xee, 0x94,
3818  0xfa, 0x09, 0x6a, 0x3e, 0xa6, 0xb9, 0x9b, 0xd7, 0x01, 0xe4, 0x4f, 0x53, 0x69, 0x92, 0x8a,
3819  0xf4, 0x52, 0xbf, 0x44, 0xfd, 0x26, 0x80, 0xb3, 0xed, 0x9d, 0x25, 0xa8, 0x99, 0x24, 0x1e,
3820  0xd7, 0x29, 0xde, 0xa3, 0x08, 0x63, 0x44, 0x2a, 0xb9, 0x94, 0x19, 0x73, 0xb9, 0xf3, 0xee,
3821  0x67, 0xc1, 0x81, 0xdd, 0x78, 0x2f, 0x8c, 0x1d, 0x7e, 0xb5, 0x06, 0x97, 0x24, 0x60, 0xfb,
3822  0x27, 0xf1, 0x4d, 0x85, 0x2d, 0x1e, 0x8c, 0x13, 0x95, 0x2b, 0x5c, 0x12, 0x3a, 0xe3, 0x31,
3823  0x55, 0x07, 0xdd, 0xf2, 0x85, 0xc5, 0x2e, 0x28, 0x2a, 0x9b, 0x3a, 0x89, 0xef, 0xd6, 0xba,
3824  0x00, 0x55, 0xc2, 0x61, 0x74, 0xaf, 0xcc, 0xfd, 0x44, 0x7c, 0xae, 0xd8, 0xbb, 0xa7, 0xb7,
3825  0x93, 0xc1, 0x3a, 0x0d, 0xef, 0x7e, 0x14, 0x67, 0x3a, 0x0b, 0x62, 0x57, 0x5e, 0x78, 0x58,
3826  0x5c, 0x63, 0x2b, 0x79, 0x2d, 0x81, 0x61, 0x4d, 0xd2, 0xdb, 0x41, 0xe9, 0xbd, 0x00, 0xc8,
3827  0x26, 0xd3, 0xea, 0x6b, 0x10, 0xa9, 0x8e, 0xab, 0x64, 0x85, 0x6a, 0x55, 0xc1, 0x78, 0x65,
3828  0x31, 0x9b, 0x4d, 0x44, 0x84, 0xea, 0x60, 0x15, 0x52, 0x6b, 0x9d, 0x4b, 0x14, 0x5c, 0xba,
3829  0x4f, 0xa2, 0x53, 0x8f, 0x2a, 0x2c, 0xda, 0x36, 0x96
3830 
3831  };
3832 
3833  /* a garbage first certificate (10 bytes, not ASN.1) followed by the
3834  * valid one */
3835  const uint32_t bad_len = 10;
3836  const uint32_t good_len = sizeof(valid_cert);
3837  const uint32_t chain_len = 3 + bad_len + 3 + good_len;
3838  const uint32_t hs_len = 3 + chain_len;
3839  const uint32_t msg_len = 4 + 3 + 3 + bad_len + 3 + good_len;
3840  uint8_t msg[msg_len];
3841 
3842  msg[0] = 0x0b;
3843  msg[1] = 0;
3844  msg[2] = (hs_len >> 8) & 0xff;
3845  msg[3] = hs_len & 0xff;
3846  msg[4] = 0;
3847  msg[5] = (chain_len >> 8) & 0xff;
3848  msg[6] = chain_len & 0xff;
3849  msg[7] = 0;
3850  msg[8] = 0;
3851  msg[9] = bad_len;
3852  for (uint32_t k = 0; k < bad_len; k++)
3853  msg[10 + k] = 0x41;
3854  const uint32_t off = 10 + bad_len;
3855  msg[off] = 0;
3856  msg[off + 1] = (good_len >> 8) & 0xff;
3857  msg[off + 2] = good_len & 0xff;
3858  memcpy(msg + off + 3, valid_cert, good_len);
3859 
3860  SSLState *ssl_state = SSLStateAlloc(NULL, ALPROTO_TLS);
3861  if (ssl_state == NULL)
3862  return 0;
3863 
3864  ssl_state->curr_connp = &ssl_state->server_connp;
3865  ssl_state->curr_connp->record_length = msg_len;
3866  ssl_state->curr_connp->bytes_processed = 0;
3867  ssl_state->curr_connp->version = 0x0303;
3868 
3869  /* the bytes are consumed (rc > 0) and the cert phase is entered at
3870  * the header; the incomplete data keeps the track out of the
3871  * handshake-done shortcut at the end of SSLDecode (the cert-fields
3872  * based tail requires complete certificate data), not out of the
3873  * record-level data phase (app data and CCS advance the track
3874  * regardless); the suppression flag is hello-specific */
3875  int rc = SSLv3ParseHandshakeProtocol(ssl_state, msg, msg_len, 1);
3876  FAIL_IF(rc < 0);
3880  /* the rest of the chain survived: the valid certificate is in the
3881  * chain and the cert0 fields come from the first decodable cert */
3882  SSLCertsChain *item = TAILQ_FIRST(&ssl_state->server_connp.certs);
3883  FAIL_IF(item == NULL);
3884  FAIL_IF(item->cert_len != good_len);
3885  FAIL_IF(ssl_state->server_connp.cert0_subject == NULL);
3886  FAIL_IF(ssl_state->server_connp.cert0_issuerdn == NULL);
3887 
3888  /* the certificate data comes from the valid (second) certificate:
3889  * compare the fields against a direct extraction from the fixture */
3890  uint32_t errc = 0;
3891  X509 *ref = SCX509Decode(valid_cert, sizeof(valid_cert), &errc);
3892  FAIL_IF(ref == NULL);
3893  uint8_t *subj = NULL;
3894  uint32_t subj_len = 0;
3895  uint8_t *iss = NULL;
3896  uint32_t iss_len = 0;
3897  SCX509GetSubject(ref, &subj, &subj_len);
3898  SCX509GetIssuer(ref, &iss, &iss_len);
3899  FAIL_IF(subj == NULL || iss == NULL);
3900  FAIL_IF(ssl_state->server_connp.cert0_subject_len != subj_len);
3901  FAIL_IF(memcmp(ssl_state->server_connp.cert0_subject, subj, subj_len) != 0);
3902  FAIL_IF(ssl_state->server_connp.cert0_issuerdn_len != iss_len);
3903  FAIL_IF(memcmp(ssl_state->server_connp.cert0_issuerdn, iss, iss_len) != 0);
3904  if (subj != NULL)
3905  SCX509ArrayFree(subj, subj_len);
3906  if (iss != NULL)
3907  SCX509ArrayFree(iss, iss_len);
3908  SCX509Free(ref);
3909 
3910  /* the incomplete-certificate-data condition is sticky: a later
3911  * handshake message resets the per-message suppression but does not
3912  * complete the certificate data (only a fully decoded certificate
3913  * message does), so the SSLDecode tail shortcut stays off and the
3914  * track stays in the cert phase, out of the data phase */
3915  const uint8_t shd[] = { 0x0e, 0x00, 0x00, 0x00 };
3916  ssl_state->curr_connp = &ssl_state->server_connp;
3917  ssl_state->curr_connp->record_length = sizeof(shd);
3918  ssl_state->curr_connp->bytes_processed = 0;
3919  rc = SSLv3ParseHandshakeProtocol(ssl_state, shd, sizeof(shd), 1);
3920  FAIL_IF(rc < 0);
3924 
3925  /* a fully decodable first certificate message clears the incomplete
3926  * flag and enters the cert phase at its start: the preconditions for
3927  * the cert-fields based data shortcut (the shortcut itself is pinned
3928  * by sv ruletype-firewall-51). On the client connp: its one-shot
3929  * buffer is untouched here */
3930  const uint32_t mlen = 4 + 3 + 3 + sizeof(valid_cert);
3931  uint8_t full[mlen];
3932  memset(full, 0, sizeof(full));
3933  full[0] = 0x0b;
3934  full[1] = ((3 + 3 + good_len) >> 16) & 0xff;
3935  full[2] = ((3 + 3 + good_len) >> 8) & 0xff;
3936  full[3] = (3 + 3 + good_len) & 0xff;
3937  full[4] = ((3 + good_len) >> 16) & 0xff;
3938  full[5] = ((3 + good_len) >> 8) & 0xff;
3939  full[6] = (3 + good_len) & 0xff;
3940  full[7] = (good_len >> 16) & 0xff;
3941  full[8] = (good_len >> 8) & 0xff;
3942  full[9] = good_len & 0xff;
3943  memcpy(full + 10, valid_cert, good_len);
3944  ssl_state->curr_connp = &ssl_state->client_connp;
3945  ssl_state->curr_connp->record_length = mlen;
3946  ssl_state->curr_connp->bytes_processed = 0;
3947  rc = SSLv3ParseHandshakeProtocol(ssl_state, full, mlen, 0);
3948  FAIL_IF(rc < 0);
3950  FAIL_IF(ssl_state->client_connp.cert0_subject == NULL);
3952  FAIL_IF(!ssl_state->client_connp.cert_chain_final);
3953 
3954  /* the one-shot buffer latches: a second server certificate message
3955  * is not decoded and cannot clear the incomplete flag (base
3956  * semantics, renegotiation re-sends the same certificate) */
3957  ssl_state->curr_connp = &ssl_state->server_connp;
3958  ssl_state->curr_connp->record_length = mlen;
3959  ssl_state->curr_connp->bytes_processed = 0;
3960  rc = SSLv3ParseHandshakeProtocol(ssl_state, full, mlen, 1);
3961  FAIL_IF(rc < 0);
3963 
3964  SSLStateFree(ssl_state);
3965  PASS;
3966 }
3967 
3968 /* The parser accepts the flight-settling handshake types from either
3969  * direction. A wrong-direction CLIENT_KEY_EXCHANGE (server -> client) must
3970  * not settle the server flight decision: doing so would release the one-shot
3971  * TLS logger before a later CertificateRequest, whose client certificate
3972  * would then be omitted. Only the client's own message settles it; a server
3973  * CertificateRequest still settles the decision afterwards. */
3974 static int TLSPhaseWrongDirectionKeyxTest(void)
3975 {
3976  SSLState *ssl_state = SSLStateAlloc(NULL, ALPROTO_TLS);
3977  if (ssl_state == NULL)
3978  return 0;
3979 
3980  /* wrong direction: server -> client CLIENT_KEY_EXCHANGE */
3981  const uint8_t ckx[] = { 0x10, 0x00, 0x00, 0x00 };
3982  ssl_state->curr_connp = &ssl_state->server_connp;
3983  ssl_state->curr_connp->record_length = sizeof(ckx);
3984  ssl_state->curr_connp->bytes_processed = 0;
3985  int rc = SSLv3ParseHandshakeProtocol(ssl_state, ckx, sizeof(ckx), 1);
3986  FAIL_IF(rc < 0);
3988 
3989  /* the later CertificateRequest still arrives and settles the decision */
3990  const uint8_t certreq[] = { 0x0d, 0x00, 0x00, 0x00 };
3991  ssl_state->curr_connp = &ssl_state->server_connp;
3992  ssl_state->curr_connp->record_length = sizeof(certreq);
3993  ssl_state->curr_connp->bytes_processed = 0;
3994  rc = SSLv3ParseHandshakeProtocol(ssl_state, certreq, sizeof(certreq), 1);
3995  FAIL_IF(rc < 0);
3996  FAIL_IF((ssl_state->current_flags & SSL_AL_FLAG_NEED_CLIENT_CERT) == 0);
3998 
3999  /* the client's own CLIENT_KEY_EXCHANGE settles it */
4000  SSLState *client_ckx = SSLStateAlloc(NULL, ALPROTO_TLS);
4001  FAIL_IF(client_ckx == NULL);
4002  client_ckx->curr_connp = &client_ckx->client_connp;
4003  client_ckx->curr_connp->record_length = sizeof(ckx);
4004  client_ckx->curr_connp->bytes_processed = 0;
4005  rc = SSLv3ParseHandshakeProtocol(client_ckx, ckx, sizeof(ckx), 0);
4006  FAIL_IF(rc < 0);
4008 
4009  SSLStateFree(ssl_state);
4010  SSLStateFree(client_ckx);
4011  PASS;
4012 }
4013 
4015 {
4016  UtRegisterTest("TLSPhaseStateTestClient", TLSPhaseStateTestClient);
4017  UtRegisterTest("TLSPhaseStateTestServer", TLSPhaseStateTestServer);
4018  UtRegisterTest("TLSPhaseStateTestOldNamesGone", TLSPhaseStateTestOldNamesGone);
4019  UtRegisterTest("TLSPhaseStateTestCompletion", TLSPhaseStateTestCompletion);
4020  UtRegisterTest("TLSPhaseSuppressionTest", TLSPhaseSuppressionTest);
4021  UtRegisterTest("TLSPhaseCertChainSkipTest", TLSPhaseCertChainSkipTest);
4022  UtRegisterTest("TLSPhaseWrongDirectionKeyxTest", TLSPhaseWrongDirectionKeyxTest);
4023 }
4024 #endif /* UNITTESTS */
SSLV3_CHANGE_CIPHER_SPEC
#define SSLV3_CHANGE_CIPHER_SPEC
Definition: app-layer-ssl.c:210
ERR_INVALID_SERIAL
@ ERR_INVALID_SERIAL
Definition: app-layer-ssl.c:172
tls_frame_table
SCEnumCharMap tls_frame_table[]
Definition: app-layer-ssl.c:95
StreamSlice
Definition: app-layer-parser.h:126
TLS_DECODER_EVENT_CERTIFICATE_INVALID_ISSUER
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_ISSUER
Definition: app-layer-ssl.h:72
AppLayerParserRegisterGetStateProgressFunc
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
Definition: app-layer-parser.c:544
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:263
SslConfig_::disable_ja3
bool disable_ja3
Definition: app-layer-ssl.c:202
SSL_EXTENSION_EC_POINT_FORMATS
#define SSL_EXTENSION_EC_POINT_FORMATS
Definition: app-layer-ssl.h:153
SSLSubjectAltName_::san_len
uint32_t san_len
Definition: app-layer-ssl.h:174
SCConfValIsTrue
int SCConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:577
SCAppLayerParserStateIssetFlag
uint16_t SCAppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint16_t flag)
Definition: app-layer-parser.c:2079
JA3Buffer_
Definition: util-ja3.h:31
TLS_DECODER_EVENT_DATALEAK_HEARTBEAT_MISMATCH
@ TLS_DECODER_EVENT_DATALEAK_HEARTBEAT_MISMATCH
Definition: app-layer-ssl.h:54
SSLCertsChain_::cert_len
uint32_t cert_len
Definition: app-layer-ssl.h:168
TLS_DECODER_EVENT_HEARTBEAT
@ TLS_DECODER_EVENT_HEARTBEAT
Definition: app-layer-ssl.h:51
TLS_HB_RESPONSE
#define TLS_HB_RESPONSE
Definition: app-layer-ssl.c:251
AppLayerParserGetStateNameById
const char * AppLayerParserGetStateNameById(uint8_t ipproto, AppProto alproto, const int id, const uint8_t direction)
Definition: app-layer-parser.c:1847
SHA1_STRING_LENGTH
#define SHA1_STRING_LENGTH
Definition: app-layer-ssl.c:255
TAILQ_INIT
#define TAILQ_INIT(head)
Definition: queue.h:262
TLS_DECODER_EVENT_OVERFLOW_HEARTBEAT
@ TLS_DECODER_EVENT_OVERFLOW_HEARTBEAT
Definition: app-layer-ssl.h:53
SC_ATOMIC_INIT
#define SC_ATOMIC_INIT(name)
wrapper for initializing an atomic variable.
Definition: util-atomic.h:314
TLS_DECODER_EVENT_TOO_MANY_SUBJECT_ALTERNATIVE_NAMES
@ TLS_DECODER_EVENT_TOO_MANY_SUBJECT_ALTERNATIVE_NAMES
Definition: app-layer-ssl.h:75
TLS_STATE_SERVER_CERT
@ TLS_STATE_SERVER_CERT
Definition: app-layer-ssl.h:90
TLS_DECODER_EVENT_ERROR_MSG_ENCOUNTERED
@ TLS_DECODER_EVENT_ERROR_MSG_ENCOUNTERED
Definition: app-layer-ssl.h:74
ERR_EXTRACT_SUBJECT
@ ERR_EXTRACT_SUBJECT
Definition: app-layer-ssl.c:180
SSLV3_HS_FINISHED
#define SSLV3_HS_FINISHED
Definition: app-layer-ssl.c:227
SSLState_::hb_record_len
uint32_t hb_record_len
Definition: app-layer-ssl.h:273
unlikely
#define unlikely(expr)
Definition: util-optimize.h:35
SSL_EXTENSION_ELLIPTIC_CURVES
#define SSL_EXTENSION_ELLIPTIC_CURVES
Definition: app-layer-ssl.h:152
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:184
UtRegisterTest
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
Definition: util-unittest.c:101
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:284
TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE
@ TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE
Definition: app-layer-ssl.h:50
TLS_FRAME_DATA
@ TLS_FRAME_DATA
Definition: app-layer-ssl.h:36
SSLStateConnp_::hello_decoded
bool hello_decoded
Definition: app-layer-ssl.h:224
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:39
SSLEnableJA4
void SSLEnableJA4(void)
if not explicitly disabled in config, enable ja4 support
Definition: app-layer-ssl.c:3589
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
SSL_AL_FLAG_EARLY_DATA
#define SSL_AL_FLAG_EARLY_DATA
Definition: app-layer-ssl.h:133
SSLStateConnp_::bytes_processed
uint32_t bytes_processed
Definition: app-layer-ssl.h:192
SCAppLayerTxDataCleanup
void SCAppLayerTxDataCleanup(AppLayerTxData *txd)
Definition: app-layer-parser.c:831
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
SSLV2_MT_SERVER_FINISHED
#define SSLV2_MT_SERVER_FINISHED
Definition: app-layer-ssl.c:238
SSLState_::server_connp
SSLStateConnp server_connp
Definition: app-layer-ssl.h:285
SSLStateConnp_::cert0_not_before
int64_t cert0_not_before
Definition: app-layer-ssl.h:225
name
const char * name
Definition: detect-engine-proto.c:47
SSL_AL_FLAG_SESSION_RESUMED
#define SSL_AL_FLAG_SESSION_RESUMED
Definition: app-layer-ssl.h:122
AppProto
uint16_t AppProto
Definition: app-layer-protos.h:87
ValidateRecordState
#define ValidateRecordState(...)
Definition: app-layer-ssl.c:306
SSLStateConnp_
Definition: app-layer-ssl.h:177
SSLStateConnp_::ja3_hash
char * ja3_hash
Definition: app-layer-ssl.h:245
AppLayerParserGetStateProgressCompletionStatus
uint8_t AppLayerParserGetStateProgressCompletionStatus(AppProto alproto, uint8_t direction)
Definition: app-layer-parser.c:1226
SSL_CONFIG_DEFAULT_JA3
#define SSL_CONFIG_DEFAULT_JA3
Definition: app-layer-ssl.c:186
type
uint8_t type
Definition: decode-sctp.h:0
SCAppLayerParserStateSetFlag
void SCAppLayerParserStateSetFlag(AppLayerParserState *pstate, uint16_t flag)
Definition: app-layer-parser.c:2071
SSLStateConnp_::session_id_length
uint16_t session_id_length
Definition: app-layer-ssl.h:194
AppLayerStateData
Definition: app-layer-parser.h:155
tls_decoder_event_table
SCEnumCharMap tls_decoder_event_table[]
Definition: app-layer-ssl.c:127
Flow_
Flow data structure.
Definition: flow.h:359
SSL_EXTENSION_SUPPORTED_VERSIONS
#define SSL_EXTENSION_SUPPORTED_VERSIONS
Definition: app-layer-ssl.h:158
SSLState_::f
Flow * f
Definition: app-layer-ssl.h:264
SSL_AL_FLAG_STATE_SERVER_HELLO
#define SSL_AL_FLAG_STATE_SERVER_HELLO
Definition: app-layer-ssl.h:111
SC_SHA1_LEN
#define SC_SHA1_LEN
Definition: util-file.h:107
SSLV3_HS_SERVER_HELLO
#define SSLV3_HS_SERVER_HELLO
Definition: app-layer-ssl.c:219
AppLayerParserRegisterStateProgressCompletionStatus
void AppLayerParserRegisterStateProgressCompletionStatus(AppProto alproto, const int ts, const int tc)
Definition: app-layer-parser.c:592
SSLState_::tx_data
AppLayerTxData tx_data
Definition: app-layer-ssl.h:267
SSLParserHSReset
#define SSLParserHSReset(connp)
Definition: app-layer-ssl.c:309
SSLV3_HS_CERTIFICATE
#define SSLV3_HS_CERTIFICATE
Definition: app-layer-ssl.c:221
ERR_INVALID_X509NAME
@ ERR_INVALID_X509NAME
Definition: app-layer-ssl.c:174
AppLayerParserRegisterTxFreeFunc
void AppLayerParserRegisterTxFreeFunc(uint8_t ipproto, AppProto alproto, void(*StateTransactionFree)(void *, uint64_t))
Definition: app-layer-parser.c:554
SSLV3_HEARTBEAT_PROTOCOL
#define SSLV3_HEARTBEAT_PROTOCOL
Definition: app-layer-ssl.c:214
SSLV2_MT_SERVER_HELLO
#define SSLV2_MT_SERVER_HELLO
Definition: app-layer-ssl.c:236
AppLayerParserGetStateIdByName
int AppLayerParserGetStateIdByName(uint8_t ipproto, AppProto alproto, const char *name, const uint8_t direction)
Definition: app-layer-parser.c:1832
SSLState_::curr_connp
SSLStateConnp * curr_connp
Definition: app-layer-ssl.h:279
Ja3BufferAddValue
int Ja3BufferAddValue(JA3Buffer **buffer, uint32_t value)
Definition: util-ja3.c:314
SSLV3_HS_CERTIFICATE_URL
#define SSLV3_HS_CERTIFICATE_URL
Definition: app-layer-ssl.c:228
rust.h
MIN
#define MIN(x, y)
Definition: suricata-common.h:413
SCConfGetBool
int SCConfGetBool(const char *name, int *val)
Retrieve a configuration value as a boolean.
Definition: conf.c:523
AppLayerParserRegisterGetStateFuncs
void AppLayerParserRegisterGetStateFuncs(uint8_t ipproto, AppProto alproto, AppLayerParserGetStateIdByNameFn GetIdByNameFunc, AppLayerParserGetStateNameByIdFn GetNameByIdFunc)
Definition: app-layer-parser.c:651
util-ja3.h
SCConfValIsFalse
int SCConfValIsFalse(const char *val)
Check if a value is false.
Definition: conf.c:602
ERR_INVALID_ALGORITHMIDENTIFIER
@ ERR_INVALID_ALGORITHMIDENTIFIER
Definition: app-layer-ssl.c:173
SSLStateConnp_::phase_suppressed
bool phase_suppressed
Definition: app-layer-ssl.h:207
SSL_AL_FLAG_STATE_SERVER_KEYX
#define SSL_AL_FLAG_STATE_SERVER_KEYX
Definition: app-layer-ssl.h:113
SSLState_::state_data
AppLayerStateData state_data
Definition: app-layer-ssl.h:266
p
Packet * p
Definition: fuzz_dataset.c:30
TAILQ_INSERT_TAIL
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:294
SSLV3_HS_CERTIFICATE_VERIFY
#define SSLV3_HS_CERTIFICATE_VERIFY
Definition: app-layer-ssl.c:225
TLS_DECODER_EVENT_INVALID_TLS_HEADER
@ TLS_DECODER_EVENT_INVALID_TLS_HEADER
Definition: app-layer-ssl.h:46
SSL_CNF_ENC_HANDLE_TRACK_ONLY
@ SSL_CNF_ENC_HANDLE_TRACK_ONLY
Definition: app-layer-ssl.c:192
MAX
#define MAX(x, y)
Definition: suricata-common.h:417
SSLStateConnp_::hs_buffer_message_type
uint8_t hs_buffer_message_type
Definition: app-layer-ssl.h:252
TLS_DECODER_EVENT_INVALID_SSL_RECORD
@ TLS_DECODER_EVENT_INVALID_SSL_RECORD
Definition: app-layer-ssl.h:76
TLS_DECODER_EVENT_TOO_MANY_RECORDS_IN_PACKET
@ TLS_DECODER_EVENT_TOO_MANY_RECORDS_IN_PACKET
Definition: app-layer-ssl.h:59
TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS
@ TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS
Definition: app-layer-ssl.h:56
SSLStateConnp_::record_lengths_length
uint32_t record_lengths_length
Definition: app-layer-ssl.h:181
SSLSubjectAltName_::san
uint8_t * san
Definition: app-layer-ssl.h:173
TLS_FRAME_SSLV2_HDR
@ TLS_FRAME_SSLV2_HDR
Definition: app-layer-ssl.h:39
SSL_AL_FLAG_CHANGE_CIPHER_SPEC
#define SSL_AL_FLAG_CHANGE_CIPHER_SPEC
Definition: app-layer-ssl.h:99
SCAppLayerProtoDetectPMRegisterPatternCSwPP
int SCAppLayerProtoDetectPMRegisterPatternCSwPP(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:1638
ERR_INVALID_DATE
@ ERR_INVALID_DATE
Definition: app-layer-ssl.c:175
SSLStateConnp_::hs_buffer_size
uint32_t hs_buffer_size
Definition: app-layer-ssl.h:254
TLS_DECODER_EVENT_CERTIFICATE_INVALID_DER
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_DER
Definition: app-layer-ssl.h:70
TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH
Definition: app-layer-ssl.h:63
TLS_FRAME_HB_DATA
@ TLS_FRAME_HB_DATA
Definition: app-layer-ssl.h:38
SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY
#define SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY
Definition: app-layer-ssl.h:104
SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED
#define SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED
Definition: app-layer-ssl.h:105
SSLStateConnp_::cert0_not_after
int64_t cert0_not_after
Definition: app-layer-ssl.h:226
SCAppLayerProtoDetectConfProtoDetectionEnabled
int SCAppLayerProtoDetectConfProtoDetectionEnabled(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:1989
ERR_INVALID_LENGTH
@ ERR_INVALID_LENGTH
Definition: app-layer-ssl.c:170
SSLState_::current_flags
uint32_t current_flags
Definition: app-layer-ssl.h:277
SSLStateConnp_::cert0_sans
SSLSubjectAltName * cert0_sans
Definition: app-layer-ssl.h:229
AppLayerResult
Definition: app-layer-parser.h:120
SSL_AL_FLAG_HB_SERVER_INIT
#define SSL_AL_FLAG_HB_SERVER_INIT
Definition: app-layer-ssl.h:119
SCAppLayerParserTriggerRawStreamInspection
void SCAppLayerParserTriggerRawStreamInspection(Flow *f, int direction)
Definition: app-layer-parser.c:1787
TLS_DECODER_EVENT_CERTIFICATE_INVALID_VALIDITY
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_VALIDITY
Definition: app-layer-ssl.h:73
SSLV2_MT_SERVER_VERIFY
#define SSLV2_MT_SERVER_VERIFY
Definition: app-layer-ssl.c:237
app-layer-detect-proto.h
SSLDecoderResult::needed
uint32_t needed
Definition: app-layer-ssl.c:261
Ja3BufferInit
JA3Buffer * Ja3BufferInit(void)
Allocate new buffer.
Definition: util-ja3.c:39
APP_LAYER_INCOMPLETE
#define APP_LAYER_INCOMPLETE(c, n)
Definition: app-layer-parser.h:70
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_STATE_CLIENT_DATA
@ TLS_STATE_CLIENT_DATA
Definition: app-layer-ssl.h:83
feature.h
util-debug.h
TAILQ_FIRST
#define TAILQ_FIRST(head)
Definition: queue.h:250
AppLayerParserState_
Definition: app-layer-parser.c:160
PASS
#define PASS
Pass the test.
Definition: util-unittest.h:105
TLS_HB_REQUEST
#define TLS_HB_REQUEST
Definition: app-layer-ssl.c:250
SSLJA3IsEnabled
bool SSLJA3IsEnabled(void)
return whether ja3 is effectively enabled
Definition: app-layer-ssl.c:3609
TLS_TC_RANDOM_SET
#define TLS_TC_RANDOM_SET
Definition: app-layer-ssl.h:139
AppLayerTxData
Definition: app-layer-parser.h:172
SCAppLayerParserConfParserEnabled
int SCAppLayerParserConfParserEnabled(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:385
TLS_DECODER_EVENT_INVALID_RECORD_TYPE
@ TLS_DECODER_EVENT_INVALID_RECORD_TYPE
Definition: app-layer-ssl.h:48
ERR_INVALID_CERTIFICATE
@ ERR_INVALID_CERTIFICATE
Definition: app-layer-ssl.c:169
SSLState_::client_state
enum TlsStateClient client_state
Definition: app-layer-ssl.h:281
APP_LAYER_EVENT_TYPE_TRANSACTION
@ APP_LAYER_EVENT_TYPE_TRANSACTION
Definition: app-layer-events.h:55
AppLayerEventType
AppLayerEventType
Definition: app-layer-events.h:54
Ja3BufferAppendBuffer
int Ja3BufferAppendBuffer(JA3Buffer **buffer1, JA3Buffer **buffer2)
Definition: util-ja3.c:309
SSLStateConnp_::handshake_type
uint8_t handshake_type
Definition: app-layer-ssl.h:189
SSLV2_MT_REQUEST_CERTIFICATE
#define SSLV2_MT_REQUEST_CERTIFICATE
Definition: app-layer-ssl.c:239
SSLDecoderResult::retval
int retval
Definition: app-layer-ssl.c:260
SSLStateConnp_::hs_buffer_offset
uint32_t hs_buffer_offset
Definition: app-layer-ssl.h:255
SSLStateConnp_::certs_buffer
uint8_t * certs_buffer
Definition: app-layer-ssl.h:239
TLS_DECODER_EVENT_INVALID_RECORD_VERSION
@ TLS_DECODER_EVENT_INVALID_RECORD_VERSION
Definition: app-layer-ssl.h:47
TlsStateServer
TlsStateServer
Definition: app-layer-ssl.h:87
TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE
Definition: app-layer-ssl.h:68
SSLCertsChain_
Definition: app-layer-ssl.h:166
SSLStateConnp_::cert0_serial_len
uint32_t cert0_serial_len
Definition: app-layer-ssl.h:202
AppLayerParserRegisterGetFrameFuncs
void AppLayerParserRegisterGetFrameFuncs(uint8_t ipproto, AppProto alproto, AppLayerParserGetFrameIdByNameFn GetIdByNameFunc, AppLayerParserGetFrameNameByIdFn GetNameByIdFunc)
Definition: app-layer-parser.c:661
TLS_DECODER_EVENT_CERTIFICATE_INVALID_SERIAL
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_SERIAL
Definition: app-layer-ssl.h:65
SSLParserReset
#define SSLParserReset(state)
Definition: app-layer-ssl.c:315
SCConfGetNonNull
int SCConfGetNonNull(const char *name, const char **vptr)
Retrieve the non-null value of a configuration node.
Definition: conf.c:380
TlsStateClient
TlsStateClient
Definition: app-layer-ssl.h:79
TLS_DECODER_EVENT_CERTIFICATE_INVALID_SUBJECT
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_SUBJECT
Definition: app-layer-ssl.h:71
TLS_TS_RANDOM_SET
#define TLS_TS_RANDOM_SET
Definition: app-layer-ssl.h:136
AppLayerParserRegisterStateFuncs
void AppLayerParserRegisterStateFuncs(uint8_t ipproto, AppProto alproto, void *(*StateAlloc)(void *, AppProto), void(*StateFree)(void *))
Definition: app-layer-parser.c:493
SSL_AL_FLAG_STATE_CLIENT_HELLO
#define SSL_AL_FLAG_STATE_CLIENT_HELLO
Definition: app-layer-ssl.h:110
TLS_STATE_SERVER_FINISHED
@ TLS_STATE_SERVER_FINISHED
Definition: app-layer-ssl.h:92
SCLogWarning
#define SCLogWarning(...)
Macro used to log WARNING messages.
Definition: util-debug.h:262
TLS_RANDOM_LEN
#define TLS_RANDOM_LEN
Definition: app-layer-ssl.h:164
SSLStateConnp_::cert0_serial
uint8_t * cert0_serial
Definition: app-layer-ssl.h:201
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:40
AppLayerParserRegisterGetEventInfo
void AppLayerParserRegisterGetEventInfo(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfo)(const char *event_name, uint8_t *event_id, AppLayerEventType *event_type))
Definition: app-layer-parser.c:671
SslConfigEncryptHandling
SslConfigEncryptHandling
Definition: app-layer-ssl.c:191
SSLStateConnp_::cert_data_incomplete
bool cert_data_incomplete
Definition: app-layer-ssl.h:213
SSL_AL_FLAG_HB_CLIENT_INIT
#define SSL_AL_FLAG_HB_CLIENT_INIT
Definition: app-layer-ssl.h:118
SSL_AL_FLAG_STATE_CLIENT_KEYX
#define SSL_AL_FLAG_STATE_CLIENT_KEYX
Definition: app-layer-ssl.h:112
SSLV3_RECORD_HDR_LEN
#define SSLV3_RECORD_HDR_LEN
Definition: app-layer-ssl.c:242
SSLV2_MT_CLIENT_MASTER_KEY
#define SSLV2_MT_CLIENT_MASTER_KEY
Definition: app-layer-ssl.c:234
AppLayerParserRegisterProtocolUnittests
void AppLayerParserRegisterProtocolUnittests(uint8_t ipproto, AppProto alproto, void(*RegisterUnittests)(void))
Definition: app-layer-parser.c:2090
SSL_EXTENSION_ALPN
#define SSL_EXTENSION_ALPN
Definition: app-layer-ssl.h:155
TLS_STATE_CLIENT_CERT
@ TLS_STATE_CLIENT_CERT
Definition: app-layer-ssl.h:82
TLS_DECODER_EVENT_INVALID_SSLV2_HEADER
@ TLS_DECODER_EVENT_INVALID_SSLV2_HEADER
Definition: app-layer-ssl.h:45
TLS_DECODER_EVENT_INVALID_RECORD_LENGTH
@ TLS_DECODER_EVENT_INVALID_RECORD_LENGTH
Definition: app-layer-ssl.h:49
conf.h
ERR_EXTRACT_ISSUER
@ ERR_EXTRACT_ISSUER
Definition: app-layer-ssl.c:181
SSL_AL_FLAG_CH_VERSION_EXTENSION
#define SSL_AL_FLAG_CH_VERSION_EXTENSION
Definition: app-layer-ssl.h:125
SSLStateConnp_::cert0_issuerdn
uint8_t * cert0_issuerdn
Definition: app-layer-ssl.h:199
SSLState_::server_state
enum TlsStateServer server_state
Definition: app-layer-ssl.h:282
SSLEnableJA3
void SSLEnableJA3(void)
if not explicitly disabled in config, enable ja3 support
Definition: app-layer-ssl.c:3572
SSLStateConnp_::record_length
uint32_t record_length
Definition: app-layer-ssl.h:179
SslConfig_
Definition: app-layer-ssl.c:197
SSLV2_MT_CLIENT_HELLO
#define SSLV2_MT_CLIENT_HELLO
Definition: app-layer-ssl.c:233
AppLayerProtoDetectRegisterProtocol
void AppLayerProtoDetectRegisterProtocol(AppProto alproto, const char *alproto_name)
Registers a protocol for protocol detection phase.
Definition: app-layer-detect-proto.c:1769
SSLV3_APPLICATION_PROTOCOL
#define SSLV3_APPLICATION_PROTOCOL
Definition: app-layer-ssl.c:213
TLS_STATE_CLIENT_HELLO
@ TLS_STATE_CLIENT_HELLO
Definition: app-layer-ssl.h:81
SSLV3_ALERT_PROTOCOL
#define SSLV3_ALERT_PROTOCOL
Definition: app-layer-ssl.c:211
SSLV2_MT_CLIENT_CERTIFICATE
#define SSLV2_MT_CLIENT_CERTIFICATE
Definition: app-layer-ssl.c:240
SSLStateConnp_::sni
uint8_t * sni
Definition: app-layer-ssl.h:232
SSLCertsChain_::cert_data
uint8_t * cert_data
Definition: app-layer-ssl.h:167
SSLStateConnp_::cert0_sans_num
uint16_t cert0_sans_num
Definition: app-layer-ssl.h:230
RunmodeIsUnittests
int RunmodeIsUnittests(void)
Definition: suricata.c:293
SSLStateConnp_::certs_buffer_size
uint32_t certs_buffer_size
Definition: app-layer-ssl.h:240
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:460
TLS_DECODER_EVENT_INVALID_HEARTBEAT
@ TLS_DECODER_EVENT_INVALID_HEARTBEAT
Definition: app-layer-ssl.h:52
SSL_AL_FLAG_NEED_CLIENT_CERT
#define SSL_AL_FLAG_NEED_CLIENT_CERT
Definition: app-layer-ssl.h:141
SSLV3_HS_CLIENT_KEY_EXCHANGE
#define SSLV3_HS_CLIENT_KEY_EXCHANGE
Definition: app-layer-ssl.c:226
SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC
#define SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC
Definition: app-layer-ssl.h:96
TLS_STATE_CLIENT_STARTED
@ TLS_STATE_CLIENT_STARTED
Definition: app-layer-ssl.h:80
SCRealloc
#define SCRealloc(ptr, sz)
Definition: util-mem.h:50
HAS_SPACE
#define HAS_SPACE(n)
Definition: app-layer-ssl.c:257
SCAppLayerProtoDetectPPRegister
void SCAppLayerProtoDetectPPRegister(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:1528
AppLayerParserRegisterGetTx
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
Definition: app-layer-parser.c:574
TLS_STATE_SERVER_DATA
@ TLS_STATE_SERVER_DATA
Definition: app-layer-ssl.h:91
TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH
@ TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH
Definition: app-layer-ssl.h:55
SSLDecoderResult
Definition: app-layer-ssl.c:259
APP_LAYER_OK
#define APP_LAYER_OK
Definition: app-layer-parser.h:58
TLS_DECODER_EVENT_CERTIFICATE_INVALID_VERSION
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_VERSION
Definition: app-layer-ssl.h:64
SSLStateConnp_::random
uint8_t random[TLS_RANDOM_LEN]
Definition: app-layer-ssl.h:196
SSLV3_RECORD_MAX_LEN
#define SSLV3_RECORD_MAX_LEN
Definition: app-layer-ssl.c:244
app-layer-frames.h
SSL_DECODER_ERROR
#define SSL_DECODER_ERROR(e)
Definition: app-layer-ssl.c:263
SSL_AL_FLAG_SSL_CLIENT_HS
#define SSL_AL_FLAG_SSL_CLIENT_HS
Definition: app-layer-ssl.h:102
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:304
ERR_EXTRACT_VALIDITY
@ ERR_EXTRACT_VALIDITY
Definition: app-layer-ssl.c:182
TLS_DECODER_EVENT_INVALID_CERTIFICATE
@ TLS_DECODER_EVENT_INVALID_CERTIFICATE
Definition: app-layer-ssl.h:62
RegisterSSLParsers
void RegisterSSLParsers(void)
Definition: app-layer-ssl.c:3436
FAIL_IF
#define FAIL_IF(expr)
Fail a test if expression evaluates to true.
Definition: util-unittest.h:71
SSLStateConnp_::cert0_subject_len
uint32_t cert0_subject_len
Definition: app-layer-ssl.h:198
SSLV3_CLIENT_HELLO_VERSION_LEN
#define SSLV3_CLIENT_HELLO_VERSION_LEN
Definition: app-layer-ssl.c:246
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:268
suricata-common.h
TLS_FRAME_PDU
@ TLS_FRAME_PDU
Definition: app-layer-ssl.h:34
SSL_EXTENSION_SESSION_TICKET
#define SSL_EXTENSION_SESSION_TICKET
Definition: app-layer-ssl.h:156
SSL_EXTENSION_SIGNATURE_ALGORITHMS
#define SSL_EXTENSION_SIGNATURE_ALGORITHMS
Definition: app-layer-ssl.h:154
AppLayerTxData::updated_tc
bool updated_tc
Definition: app-layer-parser.h:179
Ja3GenerateHash
char * Ja3GenerateHash(JA3Buffer *buffer)
Definition: util-ja3.c:319
SSL_EXTENSION_EARLY_DATA
#define SSL_EXTENSION_EARLY_DATA
Definition: app-layer-ssl.h:157
SCEnumCharMap_
Definition: util-enum.h:27
SCAppLayerParserRegisterParserAcceptableDataDirection
void SCAppLayerParserRegisterParserAcceptableDataDirection(uint8_t ipproto, AppProto alproto, uint8_t direction)
Definition: app-layer-parser.c:472
version
uint8_t version
Definition: decode-gre.h:1
SSLStateConnp_::content_type
uint8_t content_type
Definition: app-layer-ssl.h:187
SSLSetEvent
#define SSLSetEvent(ssl_state, event)
Definition: app-layer-ssl.c:322
AppLayerParserRegisterStateDataFunc
void AppLayerParserRegisterStateDataFunc(uint8_t ipproto, AppProto alproto, AppLayerStateData *(*GetStateData)(void *state))
Definition: app-layer-parser.c:692
SSLStateParserRegisterTests
void SSLStateParserRegisterTests(void)
Function to register the SSL protocol parser and other functions.
Definition: app-layer-ssl.c:4014
SSLStateConnp_::hs_buffer_message_size
uint32_t hs_buffer_message_size
Definition: app-layer-ssl.h:253
TLS_DECODER_EVENT_CERTIFICATE_INVALID_ALGORITHMIDENTIFIER
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_ALGORITHMIDENTIFIER
Definition: app-layer-ssl.h:66
SSL_DECODER_INCOMPLETE
#define SSL_DECODER_INCOMPLETE(c, n)
Definition: app-layer-ssl.c:273
SSLV3TxLogReady
bool SSLV3TxLogReady(void *tx)
Definition: app-layer-ssl.c:3029
AppLayerParserRegisterTxDataFunc
void AppLayerParserRegisterTxDataFunc(uint8_t ipproto, AppProto alproto, AppLayerTxData *(*GetTxData)(void *tx))
Definition: app-layer-parser.c:682
TLS_DECODER_EVENT_INVALID_SNI_TYPE
@ TLS_DECODER_EVENT_INVALID_SNI_TYPE
Definition: app-layer-ssl.h:57
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:466
FEATURE_JA4
#define FEATURE_JA4
Definition: feature.h:30
SslConfig_::encrypt_mode
enum SslConfigEncryptHandling encrypt_mode
Definition: app-layer-ssl.c:198
SSLV3_HS_HELLO_REQUEST
#define SSLV3_HS_HELLO_REQUEST
Definition: app-layer-ssl.c:217
app-layer-events.h
SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC
#define SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC
Definition: app-layer-ssl.h:98
SSLV3_HS_CERTIFICATE_REQUEST
#define SSLV3_HS_CERTIFICATE_REQUEST
Definition: app-layer-ssl.c:223
util-validate.h
SSL_SNI_TYPE_HOST_NAME
#define SSL_SNI_TYPE_HOST_NAME
Definition: app-layer-ssl.h:161
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:235
ssl_config
SslConfig ssl_config
Definition: app-layer-ssl.c:207
SCAppLayerProtoDetectPMRegisterPatternCS
int SCAppLayerProtoDetectPMRegisterPatternCS(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:1627
SCConfGetNode
SCConfNode * SCConfGetNode(const char *name)
Get a SCConfNode by name.
Definition: conf.c:183
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SCFree
#define SCFree(p)
Definition: util-mem.h:61
SCAppLayerProtoDetectPPParseConfPorts
int SCAppLayerProtoDetectPPParseConfPorts(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:1564
SSLSubjectAltName_
Definition: app-layer-ssl.h:172
TLS_DECODER_EVENT_CERTIFICATE_INVALID_X509NAME
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_X509NAME
Definition: app-layer-ssl.h:67
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:232
SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED
#define SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED
Definition: app-layer-ssl.h:106
SSLV3_HANDSHAKE_PROTOCOL
#define SSLV3_HANDSHAKE_PROTOCOL
Definition: app-layer-ssl.c:212
SSLJA4IsEnabled
bool SSLJA4IsEnabled(void)
return whether ja4 is effectively enabled
Definition: app-layer-ssl.c:3622
WARN_UNUSED
#define WARN_UNUSED
Definition: bindgen.h:33
SslConfig_::disable_ja4
bool disable_ja4
Definition: app-layer-ssl.c:204
ALPROTO_UNKNOWN
@ ALPROTO_UNKNOWN
Definition: app-layer-protos.h:29
ALPROTO_FAILED
@ ALPROTO_FAILED
Definition: app-layer-protos.h:33
SSLV3_HS_CLIENT_HELLO
#define SSLV3_HS_CLIENT_HELLO
Definition: app-layer-ssl.c:218
TLS_DECODER_EVENT_CERTIFICATE_INVALID_EXTENSIONS
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_EXTENSIONS
Definition: app-layer-ssl.h:69
SSLV3_CLIENT_HELLO_RANDOM_LEN
#define SSLV3_CLIENT_HELLO_RANDOM_LEN
Definition: app-layer-ssl.c:247
ProvidesFeature
void ProvidesFeature(const char *feature_name)
Definition: feature.c:106
SslConfig_::SC_ATOMIC_DECLARE
SC_ATOMIC_DECLARE(int, enable_ja4)
app-layer-protos.h
TLS_DECODER_EVENT_INVALID_SNI_LENGTH
@ TLS_DECODER_EVENT_INVALID_SNI_LENGTH
Definition: app-layer-ssl.h:58
suricata.h
ERR_INVALID_DER
@ ERR_INVALID_DER
Definition: app-layer-ssl.c:177
AppLayerParserRegisterGetTxCnt
void AppLayerParserRegisterGetTxCnt(uint8_t ipproto, AppProto alproto, uint64_t(*StateGetTxCnt)(void *alstate))
Definition: app-layer-parser.c:564
SSL_CNF_ENC_HANDLE_BYPASS
@ SSL_CNF_ENC_HANDLE_BYPASS
Definition: app-layer-ssl.c:193
APP_LAYER_ERROR
#define APP_LAYER_ERROR
Definition: app-layer-parser.h:62
SslConfig
struct SslConfig_ SslConfig
SSLStateConnp_::hs
HandshakeParams * hs
Definition: app-layer-ssl.h:247
FEATURE_JA3
#define FEATURE_JA3
Definition: feature.h:29
SSLStateConnp_::cert0_issuerdn_len
uint32_t cert0_issuerdn_len
Definition: app-layer-ssl.h:200
AppLayerParserRegisterGetEventInfoById
void AppLayerParserRegisterGetEventInfoById(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfoById)(uint8_t event_id, const char **event_name, AppLayerEventType *event_type))
Definition: app-layer-parser.c:607
TLS_FRAME_ALERT_DATA
@ TLS_FRAME_ALERT_DATA
Definition: app-layer-ssl.h:37
SSL_AL_FLAG_SSL_NO_SESSION_ID
#define SSL_AL_FLAG_SSL_NO_SESSION_ID
Definition: app-layer-ssl.h:107
TLS_STATE_SERVER_HELLO
@ TLS_STATE_SERVER_HELLO
Definition: app-layer-ssl.h:89
SSLStateConnp_::session_id
char * session_id
Definition: app-layer-ssl.h:235
TLS_FRAME_HDR
@ TLS_FRAME_HDR
Definition: app-layer-ssl.h:35
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
TLS_DECODER_EVENT_INVALID_ALERT
@ TLS_DECODER_EVENT_INVALID_ALERT
Definition: app-layer-ssl.h:60
SSL_EXTENSION_SNI
#define SSL_EXTENSION_SNI
Definition: app-layer-ssl.h:151
SSLStateConnp_::cert0_fingerprint
char * cert0_fingerprint
Definition: app-layer-ssl.h:227
SSL_RECORD_MINIMUM_LENGTH
#define SSL_RECORD_MINIMUM_LENGTH
Definition: app-layer-ssl.c:253
SSL_AL_FLAG_SERVER_FLIGHT_DECIDED
#define SSL_AL_FLAG_SERVER_FLIGHT_DECIDED
Definition: app-layer-ssl.h:145
SSLStateConnp_::sni_len
uint16_t sni_len
Definition: app-layer-ssl.h:233
SSLStateConnp_::ja3_str
JA3Buffer * ja3_str
Definition: app-layer-ssl.h:244
SCCalloc
#define SCCalloc(nm, sz)
Definition: util-mem.h:53
ERR_INVALID_EXTENSIONS
@ ERR_INVALID_EXTENSIONS
Definition: app-layer-ssl.c:176
SSLV3_HS_SERVER_HELLO_DONE
#define SSLV3_HS_SERVER_HELLO_DONE
Definition: app-layer-ssl.c:224
util-enum.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
SCConfNode_
Definition: conf.h:37
SSLStateConnp_::hs_buffer
uint8_t * hs_buffer
Definition: app-layer-ssl.h:251
SCConfNode_::val
char * val
Definition: conf.h:39
ERR_INVALID_VERSION
@ ERR_INVALID_VERSION
Definition: app-layer-ssl.c:171
TLS_STATE_CLIENT_FINISHED
@ TLS_STATE_CLIENT_FINISHED
Definition: app-layer-ssl.h:84
SSLStateConnp_::cert_chain_final
bool cert_chain_final
Definition: app-layer-ssl.h:219
DEBUG_VALIDATE_BUG_ON
#define DEBUG_VALIDATE_BUG_ON(exp)
Definition: util-validate.h:109
app-layer-ssl.h
SSLV3_HS_NEW_SESSION_TICKET
#define SSLV3_HS_NEW_SESSION_TICKET
Definition: app-layer-ssl.c:220
SCAppLayerGetEventIdByName
int SCAppLayerGetEventIdByName(const char *event_name, SCEnumCharMap *table, uint8_t *event_id)
Definition: app-layer-events.c:31
SSL_AL_FLAG_LOG_WITHOUT_CERT
#define SSL_AL_FLAG_LOG_WITHOUT_CERT
Definition: app-layer-ssl.h:129
SSLV3_HS_CERTIFICATE_STATUS
#define SSLV3_HS_CERTIFICATE_STATUS
Definition: app-layer-ssl.c:229
SSL_AL_FLAG_HB_INFLIGHT
#define SSL_AL_FLAG_HB_INFLIGHT
Definition: app-layer-ssl.h:117
SSL_AL_FLAG_SSL_SERVER_HS
#define SSL_AL_FLAG_SSL_SERVER_HS
Definition: app-layer-ssl.h:103
AppLayerTxData::updated_ts
bool updated_ts
Definition: app-layer-parser.h:180
TLS_STATE_SERVER_STARTED
@ TLS_STATE_SERVER_STARTED
Definition: app-layer-ssl.h:88
SSL_CNF_ENC_HANDLE_FULL
@ SSL_CNF_ENC_HANDLE_FULL
Definition: app-layer-ssl.c:194
f
Flow f
Definition: fuzz_dataset.c:32
SSLStateConnp_::cert0_subject
uint8_t * cert0_subject
Definition: app-layer-ssl.h:197
SSLState_::flags
uint32_t flags
Definition: app-layer-ssl.h:270
SSLStateConnp_::version
uint16_t version
Definition: app-layer-ssl.h:186
SSLV3_HS_SERVER_KEY_EXCHANGE
#define SSLV3_HS_SERVER_KEY_EXCHANGE
Definition: app-layer-ssl.c:222
g_disable_hashing
bool g_disable_hashing
Definition: suricata.c:219