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  const uint8_t *hs_hdr = input;
1925  if (input_len < 4 || ssl_state->curr_connp->hs_hdr_len > 0) {
1926  DEBUG_VALIDATE_BUG_ON(ssl_state->curr_connp->hs_hdr_len >= 4);
1927  uint32_t add = MIN(input_len, (uint32_t)(4 - ssl_state->curr_connp->hs_hdr_len));
1928  if (SafeMemcpy(ssl_state->curr_connp->hs_hdr, ssl_state->curr_connp->hs_hdr_len, 4,
1929  input, 0, input_len, add) != 0) {
1930  return -1;
1931  }
1932  ssl_state->curr_connp->hs_hdr_len += add;
1933  input += add;
1934  input_len -= add;
1935  if (ssl_state->curr_connp->hs_hdr_len < 4) {
1936  /* partial header received, not enough input len, return */
1937  SSLParserHSReset(ssl_state->curr_connp);
1938  return (int)(input - initial_input);
1939  }
1940  hs_hdr = ssl_state->curr_connp->hs_hdr;
1941  ssl_state->curr_connp->hs_hdr_len = 0;
1942  } else {
1943  input += 4;
1944  input_len -= 4;
1945  }
1946 
1947  ssl_state->curr_connp->handshake_type = hs_hdr[0];
1948  ssl_state->curr_connp->message_length = hs_hdr[1] << 16 | hs_hdr[2] << 8 | hs_hdr[3];
1949  /* the fragments of this message all belong to its phase */
1950  SSLv3HandshakePhaseEnter(ssl_state, ssl_state->curr_connp->handshake_type);
1951  SCLogDebug("handshake_type %u message len %u input %p input_len %u",
1952  ssl_state->curr_connp->handshake_type, ssl_state->curr_connp->message_length, input,
1953  input_len);
1954 
1955  const uint32_t record_len = ssl_state->curr_connp->message_length;
1956  /* see if we support this type. We check here to not use the fragment
1957  * handling on things we don't support. */
1958  const bool supported_type = SupportedHandshakeType(ssl_state->curr_connp->handshake_type);
1959  SCLogDebug("supported_type %s handshake_type %u/%02x", supported_type ? "true" : "false",
1960  ssl_state->curr_connp->handshake_type, ssl_state->curr_connp->handshake_type);
1961  if (!supported_type) {
1962  uint32_t avail_record_len = MIN(input_len, record_len);
1963  input += avail_record_len;
1964  input_len -= avail_record_len;
1965 
1966  SSLParserHSReset(ssl_state->curr_connp);
1967 
1968  if ((direction && (ssl_state->flags & SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC)) ||
1969  (!direction && (ssl_state->flags & SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC))) {
1970  // after Change Cipher Spec we get Encrypted Handshake Messages
1971  } else {
1973  }
1974  continue;
1975  }
1976 
1977  /* if the message length exceeds our input_len, we have a tls fragment. */
1978  if (record_len > input_len) {
1979  const uint32_t avail = input_len;
1980  const uint32_t size = avail + (4096 - (avail % 4096));
1981  SCLogDebug("initial buffer size %u, based on input %u", size, avail);
1982  ssl_state->curr_connp->hs_buffer = SCCalloc(1, size);
1983  if (ssl_state->curr_connp->hs_buffer == NULL) {
1984  return -1;
1985  }
1986  ssl_state->curr_connp->hs_buffer_size = size;
1987  ssl_state->curr_connp->hs_buffer_message_size = record_len;
1988  ssl_state->curr_connp->hs_buffer_message_type = ssl_state->curr_connp->handshake_type;
1989 
1990  if (input_len > 0) {
1991  if (SafeMemcpy(ssl_state->curr_connp->hs_buffer, 0,
1992  ssl_state->curr_connp->hs_buffer_size, input, 0, input_len,
1993  input_len) != 0) {
1994  return -1;
1995  }
1996  ssl_state->curr_connp->hs_buffer_offset = input_len;
1997  }
1998  SCLogDebug("opened record buffer %p size %u offset %u type %u msg_size %u",
1999  ssl_state->curr_connp->hs_buffer, ssl_state->curr_connp->hs_buffer_size,
2000  ssl_state->curr_connp->hs_buffer_offset,
2001  ssl_state->curr_connp->hs_buffer_message_type,
2002  ssl_state->curr_connp->hs_buffer_message_size);
2003  input += input_len;
2004  SSLParserHSReset(ssl_state->curr_connp);
2005  return (int)(input - initial_input);
2006 
2007  } else {
2008  /* full record, parse it now */
2009  const uint8_t full_record_hs_type = ssl_state->curr_connp->handshake_type;
2010  int retval = SSLv3ParseHandshakeType(
2011  ssl_state, input, ssl_state->curr_connp->message_length, direction);
2012  if (retval < 0 || retval > (int)input_len) {
2013  DEBUG_VALIDATE_BUG_ON(retval > (int)input_len);
2014  if (retval < 0)
2015  SSLParserHSReset(ssl_state->curr_connp);
2016  return (retval);
2017  }
2018  SCLogDebug("retval %d input_len %u", retval, input_len);
2019  SSLv3HandshakePhaseLeave(ssl_state, full_record_hs_type);
2020  input += retval;
2021  input_len -= retval;
2022 
2023  SSLParserHSReset(ssl_state->curr_connp);
2024  }
2025  SCLogDebug("input_len left %u", input_len);
2026  }
2027  return (int)(input - initial_input);
2028 }
2029 
2030 /**
2031  * \internal
2032  * \brief TLS Alert parser
2033  *
2034  * \param sslstate Pointer to the SSL state.
2035  * \param input Pointer to the received input data.
2036  * \param input_len Length in bytes of the received data.
2037  * \param direction 1 toclient, 0 toserver
2038  *
2039  * \retval The number of bytes parsed on success, 0 if nothing parsed, -1 on failure.
2040  */
2041 static int SSLv3ParseAlertProtocol(
2042  SSLState *ssl_state, const uint8_t *input, uint32_t input_len, uint8_t direction)
2043 {
2044  if (input_len < 2) {
2046  return -1;
2047  }
2048 
2049  /* assume a record > 2 to be an encrypted alert record */
2050  if (input_len == 2) {
2051  uint8_t level = input[0];
2052  // uint8_t desc = input[1];
2053 
2054  /* if level Fatal, we consider the tx finished */
2055  if (level == 2) {
2056  UpdateClientState(ssl_state, TLS_STATE_CLIENT_FINISHED);
2057  UpdateServerState(ssl_state, TLS_STATE_SERVER_FINISHED);
2058  }
2059  }
2060  return 0;
2061 }
2062 
2063 /**
2064  * \internal
2065  * \brief TLS Heartbeat parser (see RFC 6520)
2066  *
2067  * \param sslstate Pointer to the SSL state.
2068  * \param input Pointer to the received input data.
2069  * \param input_len Length in bytes of the received data.
2070  * \param direction 1 toclient, 0 toserver
2071  *
2072  * \retval The number of bytes parsed on success, 0 if nothing parsed, -1 on failure.
2073  */
2074 static int SSLv3ParseHeartbeatProtocol(SSLState *ssl_state, const uint8_t *input,
2075  uint32_t input_len, uint8_t direction)
2076 {
2077  uint8_t hb_type;
2078  uint16_t payload_len;
2079  uint32_t padding_len;
2080 
2081  /* expect at least 3 bytes: heartbeat type (1) + length (2) */
2082  if (input_len < 3) {
2083  return 0;
2084  }
2085 
2086  hb_type = *input++;
2087 
2088  if (!(ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC)) {
2089  if (!(hb_type == TLS_HB_REQUEST || hb_type == TLS_HB_RESPONSE)) {
2091  return -1;
2092  }
2093  }
2094 
2095  if ((ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) == 0) {
2096  ssl_state->flags |= SSL_AL_FLAG_HB_INFLIGHT;
2097 
2098  if (direction) {
2099  SCLogDebug("HeartBeat Record type sent in the toclient direction!");
2100  ssl_state->flags |= SSL_AL_FLAG_HB_SERVER_INIT;
2101  } else {
2102  SCLogDebug("HeartBeat Record type sent in the toserver direction!");
2103  ssl_state->flags |= SSL_AL_FLAG_HB_CLIENT_INIT;
2104  }
2105 
2106  /* if we reach this point, then we can assume that the HB request
2107  is encrypted. If so, let's set the HB record length */
2108  if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
2109  ssl_state->hb_record_len = ssl_state->curr_connp->record_length;
2110  SCLogDebug("Encrypted HeartBeat Request In-flight. Storing len %u",
2111  ssl_state->hb_record_len);
2112  return (ssl_state->curr_connp->record_length - 3);
2113  }
2114 
2115  payload_len = (uint16_t)(*input << 8) | *(input + 1);
2116 
2117  /* check that the requested payload length is really present in
2118  the record (CVE-2014-0160) */
2119  if ((uint32_t)(payload_len+3) > ssl_state->curr_connp->record_length) {
2120  SCLogDebug("We have a short record in HeartBeat Request");
2122  return -1;
2123  }
2124 
2125  /* check the padding length. It must be at least 16 bytes
2126  (RFC 6520, section 4) */
2127  padding_len = ssl_state->curr_connp->record_length - payload_len - 3;
2128  if (padding_len < 16) {
2129  SCLogDebug("We have a short record in HeartBeat Request");
2131  return -1;
2132  }
2133 
2134  /* we don't have the payload */
2135  if (input_len < payload_len + padding_len) {
2136  return 0;
2137  }
2138 
2139  /* OpenSSL still seems to discard multiple in-flight
2140  heartbeats although some tools send multiple at once */
2141  } else if (direction == 1 && (ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) &&
2142  (ssl_state->flags & SSL_AL_FLAG_HB_SERVER_INIT)) {
2143  SCLogDebug("Multiple in-flight server initiated HeartBeats");
2145  return -1;
2146 
2147  } else if (direction == 0 && (ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) &&
2148  (ssl_state->flags & SSL_AL_FLAG_HB_CLIENT_INIT)) {
2149  SCLogDebug("Multiple in-flight client initiated HeartBeats");
2151  return -1;
2152 
2153  } else {
2154  /* we have a HB record in the opposite direction of the request,
2155  let's reset our flags */
2156  ssl_state->flags &= ~SSL_AL_FLAG_HB_INFLIGHT;
2157  ssl_state->flags &= ~SSL_AL_FLAG_HB_SERVER_INIT;
2158  ssl_state->flags &= ~SSL_AL_FLAG_HB_CLIENT_INIT;
2159 
2160  /* if we reach this point, then we can assume that the HB request
2161  is encrypted. If so, let's set the HB record length */
2162  if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
2163  /* check to see if the encrypted response is longer than the
2164  encrypted request */
2165  if (ssl_state->hb_record_len > 0 && ssl_state->hb_record_len <
2166  ssl_state->curr_connp->record_length) {
2167  SCLogDebug("My heart is bleeding.. OpenSSL HeartBleed response (%u)",
2168  ssl_state->hb_record_len);
2169  SSLSetEvent(ssl_state,
2171  ssl_state->hb_record_len = 0;
2172  return -1;
2173  }
2174  }
2175 
2176  /* reset the HB record length in case we have a legit HB followed
2177  by a bad one */
2178  ssl_state->hb_record_len = 0;
2179  }
2180 
2181  /* skip the HeartBeat, 3 bytes were already parsed,
2182  e.g |18 03 02| for TLS 1.2 */
2183  return (ssl_state->curr_connp->record_length - 3);
2184 }
2185 
2186 static int SSLv3ParseRecord(uint8_t direction, SSLState *ssl_state,
2187  const uint8_t *input, uint32_t input_len)
2188 {
2189  const uint8_t *initial_input = input;
2190 
2191  if (input_len == 0) {
2192  return 0;
2193  }
2194 
2195  uint8_t skip_version = 0;
2196 
2197  /* Only set SSL/TLS version here if it has not already been set in
2198  client/server hello. */
2199  if (direction == 0) {
2200  if ((ssl_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
2201  (ssl_state->client_connp.version != TLS_VERSION_UNKNOWN)) {
2202  skip_version = 1;
2203  }
2204  } else {
2205  if ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
2206  (ssl_state->server_connp.version != TLS_VERSION_UNKNOWN)) {
2207  skip_version = 1;
2208  }
2209  }
2210 
2211  switch (ssl_state->curr_connp->bytes_processed) {
2212  case 0:
2213  if (input_len >= 5) {
2214  ssl_state->curr_connp->content_type = input[0];
2215  if (!skip_version) {
2216  ssl_state->curr_connp->version = (uint16_t)(input[1] << 8) | input[2];
2217  }
2218  ssl_state->curr_connp->record_length = input[3] << 8;
2219  ssl_state->curr_connp->record_length |= input[4];
2221  return SSLV3_RECORD_HDR_LEN;
2222  } else {
2223  ssl_state->curr_connp->content_type = *(input++);
2224  if (--input_len == 0)
2225  break;
2226  }
2227 
2228  /* fall through */
2229  case 1:
2230  if (!skip_version) {
2231  ssl_state->curr_connp->version = (uint16_t)(*(input++) << 8);
2232  } else {
2233  input++;
2234  }
2235  if (--input_len == 0)
2236  break;
2237 
2238  /* fall through */
2239  case 2:
2240  if (!skip_version) {
2241  ssl_state->curr_connp->version |= *(input++);
2242  } else {
2243  input++;
2244  }
2245  if (--input_len == 0)
2246  break;
2247 
2248  /* fall through */
2249  case 3:
2250  ssl_state->curr_connp->record_length = *(input++) << 8;
2251  if (--input_len == 0)
2252  break;
2253 
2254  /* fall through */
2255  case 4:
2256  ssl_state->curr_connp->record_length |= *(input++);
2257  if (--input_len == 0)
2258  break;
2259 
2260  /* fall through */
2261  }
2262 
2263  ssl_state->curr_connp->bytes_processed += (input - initial_input);
2264 
2265  return (int)(input - initial_input);
2266 }
2267 
2268 static int SSLv2ParseRecord(uint8_t direction, SSLState *ssl_state,
2269  const uint8_t *input, uint32_t input_len)
2270 {
2271  const uint8_t *initial_input = input;
2272 
2273  if (input_len == 0) {
2274  return 0;
2275  }
2276 
2277  if (ssl_state->curr_connp->record_lengths_length == 2) {
2278  switch (ssl_state->curr_connp->bytes_processed) {
2279  case 0:
2280  if (input_len >= ssl_state->curr_connp->record_lengths_length + 1) {
2281  ssl_state->curr_connp->record_length = (0x7f & input[0]) << 8 | input[1];
2282  ssl_state->curr_connp->content_type = input[2];
2283  ssl_state->curr_connp->version = SSL_VERSION_2;
2284  ssl_state->curr_connp->bytes_processed += 3;
2285  return 3;
2286  } else {
2287  ssl_state->curr_connp->record_length = (0x7f & *(input++)) << 8;
2288  if (--input_len == 0)
2289  break;
2290  }
2291 
2292  /* fall through */
2293  case 1:
2294  ssl_state->curr_connp->record_length |= *(input++);
2295  if (--input_len == 0)
2296  break;
2297 
2298  /* fall through */
2299  case 2:
2300  ssl_state->curr_connp->content_type = *(input++);
2301  ssl_state->curr_connp->version = SSL_VERSION_2;
2302  if (--input_len == 0)
2303  break;
2304 
2305  /* fall through */
2306  }
2307 
2308  } else {
2309  switch (ssl_state->curr_connp->bytes_processed) {
2310  case 0:
2311  if (input_len >= ssl_state->curr_connp->record_lengths_length + 1) {
2312  ssl_state->curr_connp->record_length = (0x3f & input[0]) << 8 | input[1];
2313  ssl_state->curr_connp->content_type = input[3];
2314  ssl_state->curr_connp->version = SSL_VERSION_2;
2315  ssl_state->curr_connp->bytes_processed += 4;
2316  return 4;
2317  } else {
2318  ssl_state->curr_connp->record_length = (0x3f & *(input++)) << 8;
2319  if (--input_len == 0)
2320  break;
2321  }
2322 
2323  /* fall through */
2324  case 1:
2325  ssl_state->curr_connp->record_length |= *(input++);
2326  if (--input_len == 0)
2327  break;
2328 
2329  /* fall through */
2330  case 2:
2331  /* padding */
2332  input++;
2333  if (--input_len == 0)
2334  break;
2335 
2336  /* fall through */
2337  case 3:
2338  ssl_state->curr_connp->content_type = *(input++);
2339  ssl_state->curr_connp->version = SSL_VERSION_2;
2340  if (--input_len == 0)
2341  break;
2342 
2343  /* fall through */
2344  }
2345  }
2346 
2347  ssl_state->curr_connp->bytes_processed += (input - initial_input);
2348 
2349  return (int)(input - initial_input);
2350 }
2351 
2352 static struct SSLDecoderResult SSLv2Decode(uint8_t direction, SSLState *ssl_state,
2353  AppLayerParserState *pstate, const uint8_t *input, uint32_t input_len,
2354  const StreamSlice stream_slice)
2355 {
2356  const uint8_t *initial_input = input;
2357 
2358  if (ssl_state->curr_connp->bytes_processed == 0) {
2359  if (input[0] & 0x80) {
2360  ssl_state->curr_connp->record_lengths_length = 2;
2361  } else {
2362  ssl_state->curr_connp->record_lengths_length = 3;
2363  }
2364 
2365  SCLogDebug("record start: ssl2.hdr frame");
2366  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input,
2367  ssl_state->curr_connp->record_lengths_length + 1, direction, TLS_FRAME_SSLV2_HDR);
2368  }
2369 
2370  SCLogDebug("direction %u ssl_state->curr_connp->record_lengths_length + 1 %u, "
2371  "ssl_state->curr_connp->bytes_processed %u",
2372  direction, ssl_state->curr_connp->record_lengths_length + 1,
2373  ssl_state->curr_connp->bytes_processed);
2374  /* the +1 is because we read one extra byte inside SSLv2ParseRecord
2375  to read the msg_type */
2376  if (ssl_state->curr_connp->bytes_processed <
2377  (ssl_state->curr_connp->record_lengths_length + 1)) {
2378  const int retval = SSLv2ParseRecord(direction, ssl_state, input, input_len);
2379  SCLogDebug("retval %d ssl_state->curr_connp->record_length %u", retval,
2380  ssl_state->curr_connp->record_length);
2381  if (retval < 0 || retval > (int)input_len) {
2382  DEBUG_VALIDATE_BUG_ON(retval > (int)input_len);
2384  return SSL_DECODER_ERROR(-1);
2385  }
2386 
2387  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input,
2388  ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length,
2389  direction, TLS_FRAME_SSLV2_PDU);
2390  SCLogDebug("record start: ssl2.pdu frame");
2391 
2392  input += retval;
2393  input_len -= retval;
2394  }
2395 
2396  /* if we don't have the full record, we return incomplete */
2397  if (ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length >
2398  input_len + ssl_state->curr_connp->bytes_processed) {
2399  uint32_t needed = ssl_state->curr_connp->record_length;
2400  SCLogDebug("record len %u input_len %u parsed %u: need %u bytes more data",
2401  ssl_state->curr_connp->record_length, input_len, (uint32_t)(input - initial_input),
2402  needed);
2403  return SSL_DECODER_INCOMPLETE((input - initial_input), needed);
2404  }
2405 
2406  if (input_len == 0) {
2407  return SSL_DECODER_OK((input - initial_input));
2408  }
2409 
2410  /* record_length should never be zero */
2411  if (ssl_state->curr_connp->record_length == 0) {
2412  SCLogDebug("SSLv2 record length is zero");
2414  return SSL_DECODER_ERROR(-1);
2415  }
2416 
2417  /* record_lengths_length should never be zero */
2418  if (ssl_state->curr_connp->record_lengths_length == 0) {
2419  SCLogDebug("SSLv2 record lengths length is zero");
2421  return SSL_DECODER_ERROR(-1);
2422  }
2423 
2424  switch (ssl_state->curr_connp->content_type) {
2425  case SSLV2_MT_ERROR:
2426  SCLogDebug("SSLV2_MT_ERROR msg_type received. Error encountered "
2427  "in establishing the sslv2 session, may be version");
2429 
2430  break;
2431 
2432  case SSLV2_MT_CLIENT_HELLO:
2433  /* record_length does not count the msg_type byte. CLIENT_HELLO
2434  * body starts with 3 fixed fields: client_version (2) +
2435  * cipher_spec_length (2) + session_id_length (2). We need at
2436  * least those 6 bytes after the msg_type, so record_length
2437  * must be >= 7. */
2438  if (input_len < 6 || ssl_state->curr_connp->record_length < 7) {
2440  return SSL_DECODER_ERROR(-1);
2441  }
2442 
2443  ssl_state->current_flags = SSL_AL_FLAG_STATE_CLIENT_HELLO;
2444  ssl_state->current_flags |= SSL_AL_FLAG_SSL_CLIENT_HS;
2445  UpdateClientState(ssl_state, TLS_STATE_CLIENT_HELLO);
2446 
2447  const uint16_t version = (uint16_t)(input[0] << 8) | input[1];
2448  SCLogDebug("SSLv2: version %04x", version);
2449  ssl_state->curr_connp->version = version;
2450  ssl_state->curr_connp->hello_decoded = true;
2451  uint16_t session_id_length = (input[5]) | (uint16_t)(input[4] << 8);
2452  input += 6;
2453  input_len -= 6;
2454  ssl_state->curr_connp->bytes_processed += 6;
2455  if (session_id_length == 0) {
2456  ssl_state->current_flags |= SSL_AL_FLAG_SSL_NO_SESSION_ID;
2457  }
2458  break;
2459 
2461  if (!(ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS)) {
2462  SCLogDebug("Client hello is not seen before master key "
2463  "message!");
2464  }
2465  ssl_state->current_flags = SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY;
2466 
2467  break;
2468 
2470  if (direction == 1) {
2471  SCLogDebug("Incorrect SSL Record type sent in the toclient "
2472  "direction!");
2473  } else {
2474  ssl_state->current_flags = SSL_AL_FLAG_STATE_CLIENT_KEYX;
2475  /* a v2 client certificate message names the client's
2476  * certificate phase, even if the v2 certificate payload is
2477  * not extracted */
2478  UpdateClientState(ssl_state, TLS_STATE_CLIENT_CERT);
2479  }
2480 
2481  /* fall through */
2484  if (direction == 0 &&
2485  !(ssl_state->curr_connp->content_type &
2487  SCLogDebug("Incorrect SSL Record type sent in the toserver "
2488  "direction!");
2489  }
2490 
2491  /* fall through */
2494  /* both client hello and server hello must be seen */
2495  if ((ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) &&
2496  (ssl_state->flags & SSL_AL_FLAG_SSL_SERVER_HS)) {
2497 
2498  if (direction == 0) {
2499  if (ssl_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) {
2500  ssl_state->current_flags |= SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED;
2501  SCLogDebug("SSLv2 client side has started the encryption");
2502  } else if (ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY) {
2503  ssl_state->current_flags = SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED;
2504  SCLogDebug("SSLv2 client side has started the encryption");
2505  }
2506  } else {
2507  ssl_state->current_flags = SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED;
2508  SCLogDebug("SSLv2 Server side has started the encryption");
2509  }
2510 
2511  if ((ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED) &&
2512  (ssl_state->flags & SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED))
2513  {
2515  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_INSPECTION);
2516  }
2517 
2519  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_REASSEMBLY);
2520  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_BYPASS_READY);
2521  }
2522  SCLogDebug("SSLv2 No reassembly & inspection has been set");
2523  }
2524  }
2525 
2526  break;
2527 
2528  case SSLV2_MT_SERVER_HELLO:
2529  ssl_state->current_flags = SSL_AL_FLAG_STATE_SERVER_HELLO;
2530  ssl_state->current_flags |= SSL_AL_FLAG_SSL_SERVER_HS;
2531  ssl_state->curr_connp->hello_decoded = true;
2532  UpdateServerState(ssl_state, TLS_STATE_SERVER_HELLO);
2533 
2534  break;
2535  }
2536 
2537  ssl_state->flags |= ssl_state->current_flags;
2538 
2539  if (ssl_state->curr_connp->bytes_processed >
2540  ssl_state->curr_connp->record_length + ssl_state->curr_connp->record_lengths_length) {
2541  SCLogDebug("SSLv2 bytes_processed (%u) exceeds record+hdr "
2542  "len (record_length=%u, lengths_length=%u)",
2543  ssl_state->curr_connp->bytes_processed, ssl_state->curr_connp->record_length,
2544  ssl_state->curr_connp->record_lengths_length);
2546  return SSL_DECODER_ERROR(-1);
2547  }
2548 
2549  if (input_len + ssl_state->curr_connp->bytes_processed >=
2550  (ssl_state->curr_connp->record_length +
2551  ssl_state->curr_connp->record_lengths_length)) {
2552 
2553  /* looks like we have another record after this */
2554  uint32_t diff = ssl_state->curr_connp->record_length +
2555  ssl_state->curr_connp->record_lengths_length + -
2556  ssl_state->curr_connp->bytes_processed;
2557  input += diff;
2558  SSLParserReset(ssl_state);
2559 
2560  /* we still don't have the entire record for the one we are
2561  currently parsing */
2562  } else {
2563  input += input_len;
2564  ssl_state->curr_connp->bytes_processed += input_len;
2565  }
2566  return SSL_DECODER_OK((input - initial_input));
2567 }
2568 
2569 static struct SSLDecoderResult SSLv3Decode(uint8_t direction, SSLState *ssl_state,
2570  AppLayerParserState *pstate, const uint8_t *input, const uint32_t input_len,
2571  const StreamSlice stream_slice)
2572 {
2573  uint32_t parsed = 0;
2574  uint32_t record_len; /* slice of input_len for the current record */
2575  const bool first_call = (ssl_state->curr_connp->bytes_processed == 0);
2576 
2577  if (ssl_state->curr_connp->bytes_processed < SSLV3_RECORD_HDR_LEN) {
2578  const uint16_t prev_version = ssl_state->curr_connp->version;
2579 
2580  int retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
2581  if (retval < 0 || retval > (int)input_len) {
2582  DEBUG_VALIDATE_BUG_ON(retval > (int)input_len);
2583  SCLogDebug("SSLv3ParseRecord returned %d", retval);
2585  return SSL_DECODER_ERROR(-1);
2586  }
2587  parsed = retval;
2588 
2589  SCLogDebug("%s input %p record_length %u", (direction == 0) ? "toserver" : "toclient",
2590  input, ssl_state->curr_connp->record_length);
2591 
2592  /* first the hdr frame at our first chance */
2593  if (first_call) {
2594  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input, SSLV3_RECORD_HDR_LEN,
2595  direction, TLS_FRAME_HDR);
2596  }
2597 
2598  /* parser is streaming for the initial header, then switches to incomplete
2599  * API: so if we don't have the hdr yet, return consumed bytes and wait
2600  * until we are called again with new data. */
2601  if (ssl_state->curr_connp->bytes_processed < SSLV3_RECORD_HDR_LEN) {
2602  SCLogDebug(
2603  "incomplete header, return %u bytes consumed and wait for more data", parsed);
2604  return SSL_DECODER_OK(parsed);
2605  }
2606 
2607  /* pdu frame needs record length, so only create it when hdr fully parsed. */
2608  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input,
2609  ssl_state->curr_connp->record_length + retval, direction, TLS_FRAME_PDU);
2610  record_len = MIN(input_len - parsed, ssl_state->curr_connp->record_length);
2611  SCLogDebug(
2612  "record_len %u (input_len %u, parsed %u, ssl_state->curr_connp->record_length %u)",
2613  record_len, input_len, parsed, ssl_state->curr_connp->record_length);
2614 
2615  bool unknown_record = false;
2616  switch (ssl_state->curr_connp->content_type) {
2618  case SSLV3_ALERT_PROTOCOL:
2622  break;
2623  default:
2624  unknown_record = true;
2625  break;
2626  }
2627 
2628  /* unknown record type. For TLS 1.0, 1.1 and 1.2 this is ok. For the rest it is fatal. Based
2629  * on Wireshark logic. */
2630  if (prev_version == TLS_VERSION_10 || prev_version == TLS_VERSION_11) {
2631  if (unknown_record) {
2632  SCLogDebug("unknown record, ignore it");
2634 
2635  ssl_state->curr_connp->bytes_processed = 0; // TODO review this reset logic
2636  ssl_state->curr_connp->content_type = 0;
2637  ssl_state->curr_connp->record_length = 0;
2638  // restore last good version
2639  ssl_state->curr_connp->version = prev_version;
2640  return SSL_DECODER_OK(input_len); // consume everything
2641  }
2642  } else {
2643  if (unknown_record) {
2644  SCLogDebug("unknown record, fatal");
2646  return SSL_DECODER_ERROR(-1);
2647  }
2648  }
2649 
2650  /* record_length should never be zero */
2651  if (ssl_state->curr_connp->record_length == 0) {
2652  SCLogDebug("SSLv3 Record length is 0");
2654  return SSL_DECODER_ERROR(-1);
2655  }
2656 
2657  if (!TLSVersionValid(ssl_state->curr_connp->version)) {
2658  SCLogDebug("ssl_state->curr_connp->version %04x", ssl_state->curr_connp->version);
2660  return SSL_DECODER_ERROR(-1);
2661  }
2662 
2663  if (ssl_state->curr_connp->bytes_processed == SSLV3_RECORD_HDR_LEN &&
2664  ssl_state->curr_connp->record_length > SSLV3_RECORD_MAX_LEN) {
2666  return SSL_DECODER_ERROR(-1);
2667  }
2668  DEBUG_VALIDATE_BUG_ON(ssl_state->curr_connp->bytes_processed > SSLV3_RECORD_HDR_LEN);
2669  } else {
2670  ValidateRecordState(ssl_state->curr_connp);
2671 
2672  record_len = (ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN)- ssl_state->curr_connp->bytes_processed;
2673  record_len = MIN(input_len, record_len);
2674  }
2675  SCLogDebug("record length %u processed %u got %u",
2676  ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed, record_len);
2677 
2678  /* if we don't have the full record, we return incomplete */
2679  if (ssl_state->curr_connp->record_length > input_len - parsed) {
2680  /* no need to use incomplete api buffering for application
2681  * records that we'll not use anyway. */
2682  if (ssl_state->curr_connp->content_type == SSLV3_APPLICATION_PROTOCOL) {
2683  SCLogDebug("application record");
2684  } else {
2685  uint32_t needed = ssl_state->curr_connp->record_length;
2686  SCLogDebug("record len %u input_len %u parsed %u: need %u bytes more data",
2687  ssl_state->curr_connp->record_length, input_len, parsed, needed);
2689  return SSL_DECODER_INCOMPLETE(parsed, needed);
2690  }
2691  }
2692 
2693  if (record_len == 0) {
2694  return SSL_DECODER_OK(parsed);
2695  }
2696 
2697  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input + parsed,
2698  ssl_state->curr_connp->record_length, direction, TLS_FRAME_DATA);
2699 
2700  switch (ssl_state->curr_connp->content_type) {
2701  /* we don't need any data from these types */
2703  ssl_state->flags |= SSL_AL_FLAG_CHANGE_CIPHER_SPEC;
2704 
2705  if (direction) {
2706  ssl_state->flags |= SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC;
2707  } else {
2708  ssl_state->flags |= SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC;
2709 
2710  // TODO TLS 1.3
2711  UpdateClientState(ssl_state, TLS_STATE_CLIENT_DATA);
2712  }
2713  break;
2714 
2715  case SSLV3_ALERT_PROTOCOL: {
2716  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input + parsed,
2717  ssl_state->curr_connp->record_length, direction, TLS_FRAME_ALERT_DATA);
2718 
2719  int retval = SSLv3ParseAlertProtocol(ssl_state, input + parsed, record_len, direction);
2720  if (retval < 0) {
2721  SCLogDebug("SSLv3ParseAlertProtocol returned %d", retval);
2722  return SSL_DECODER_ERROR(-1);
2723  }
2724  break;
2725  }
2727  /* In TLSv1.3 early data (0-RTT) could be sent before the
2728  handshake is complete (rfc8446, section 2.3). We should
2729  therefore not mark the handshake as done before we have
2730  seen the ServerHello record. */
2731  if ((ssl_state->flags & SSL_AL_FLAG_EARLY_DATA) &&
2732  ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0))
2733  break;
2734 
2735  /* if we see (encrypted) application data, then this means the
2736  handshake must be done */
2737  if (ssl_state->curr_connp == &ssl_state->client_connp) {
2738  UpdateClientState(ssl_state, TLS_STATE_CLIENT_DATA);
2739  } else {
2740  UpdateServerState(ssl_state, TLS_STATE_SERVER_DATA);
2741  }
2742 
2744  SCLogDebug("setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
2745  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD);
2746  }
2747 
2748  /* Encrypted data, reassembly not asked, bypass asked, let's sacrifice
2749  * heartbeat lke inspection to be able to be able to bypass the flow */
2751  SCLogDebug("setting APP_LAYER_PARSER_NO_REASSEMBLY");
2752  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_REASSEMBLY);
2753  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_NO_INSPECTION);
2754  SCAppLayerParserStateSetFlag(pstate, APP_LAYER_PARSER_BYPASS_READY);
2755  }
2756  break;
2757 
2758  case SSLV3_HANDSHAKE_PROTOCOL: {
2759  if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
2760  /* In TLSv1.3, ChangeCipherSpec is only used for middlebox
2761  compatibility (rfc8446, appendix D.4). */
2762  // Client hello flags is needed to have a valid version
2763  if ((ssl_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
2764  (ssl_state->client_connp.version > TLS_VERSION_12) &&
2765  ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0)) {
2766  /* do nothing */
2767  } else {
2768  // if we started parsing this, we must stop
2769  break;
2770  }
2771  }
2772 
2773  int retval = SSLv3ParseHandshakeProtocol(ssl_state, input + parsed,
2774  record_len, direction);
2775  SCLogDebug("retval %d", retval);
2776  if (retval < 0 || retval > (int)record_len) {
2777  DEBUG_VALIDATE_BUG_ON(retval > (int)record_len);
2779  SCLogDebug("SSLv3ParseHandshakeProtocol returned %d", retval);
2780  return SSL_DECODER_ERROR(-1);
2781  }
2782  ValidateRecordState(ssl_state->curr_connp);
2783  break;
2784  }
2785  case SSLV3_HEARTBEAT_PROTOCOL: {
2786  AppLayerFrameNewByPointer(ssl_state->f, &stream_slice, input + parsed,
2787  ssl_state->curr_connp->record_length, direction, TLS_FRAME_HB_DATA);
2788  int retval = SSLv3ParseHeartbeatProtocol(ssl_state, input + parsed,
2789  record_len, direction);
2790  if (retval < 0) {
2791  SCLogDebug("SSLv3ParseHeartbeatProtocol returned %d", retval);
2792  return SSL_DECODER_ERROR(-1);
2793  }
2794  break;
2795  }
2796  default:
2797  // should be unreachable now that we check after header parsing
2799  SCLogDebug("unsupported record type");
2800  return SSL_DECODER_ERROR(-1);
2801  }
2802 
2803  parsed += record_len;
2804  ssl_state->curr_connp->bytes_processed += record_len;
2805 
2806  if (ssl_state->curr_connp->bytes_processed >=
2807  ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN) {
2808  SCLogDebug("record complete, trigger RAW");
2810  ssl_state->f, direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
2811  SSLParserReset(ssl_state);
2812  ValidateRecordState(ssl_state->curr_connp);
2813  return SSL_DECODER_OK(parsed);
2814 
2815  } else {
2816  /* we still don't have the entire record for the one we are
2817  currently parsing */
2818  ValidateRecordState(ssl_state->curr_connp);
2819  return SSL_DECODER_OK(parsed);
2820  }
2821 }
2822 
2823 /**
2824  * \internal
2825  * \brief SSLv2, SSLv23, SSLv3, TLSv1.1, TLSv1.2, TLSv1.3 parser.
2826  *
2827  * On parsing error, this should be the only function that should reset
2828  * the parser state, to avoid multiple functions in the chain resetting
2829  * the parser state.
2830  *
2831  * \param direction 0 for toserver, 1 for toclient.
2832  * \param alstate Pointer to the state.
2833  * \param pstate Application layer parser state for this session.
2834  * \param output Pointer to the list of parsed output elements.
2835  *
2836  * \todo On reaching an inconsistent state, check if the input has
2837  * another new record, instead of just returning after the reset
2838  *
2839  * \retval >=0 On success.
2840  */
2841 static AppLayerResult SSLDecode(Flow *f, uint8_t direction, void *alstate,
2842  AppLayerParserState *pstate, StreamSlice stream_slice)
2843 {
2844  SSLState *ssl_state = (SSLState *)alstate;
2845  ssl_state->tx_data.updated_tc = true;
2846  ssl_state->tx_data.updated_ts = true;
2847  uint32_t counter = 0;
2848  ssl_state->f = f;
2849  const uint8_t *input = StreamSliceGetData(&stream_slice);
2850  const uint8_t *init_input = input;
2851  int32_t input_len = (int32_t)StreamSliceGetDataLen(&stream_slice);
2852 
2853  if ((input == NULL || input_len == 0) &&
2854  ((direction == 0 && SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS)) ||
2855  (direction == 1 &&
2856  SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC)))) {
2857  /* flag session as finished if APP_LAYER_PARSER_EOF is set */
2858  if (direction == 0)
2859  UpdateClientState(ssl_state, TLS_STATE_CLIENT_FINISHED);
2860  else
2861  UpdateServerState(ssl_state, TLS_STATE_SERVER_FINISHED);
2863  } else if (input == NULL || input_len == 0) {
2865  }
2866 
2867  if (direction == 0)
2868  ssl_state->curr_connp = &ssl_state->client_connp;
2869  else
2870  ssl_state->curr_connp = &ssl_state->server_connp;
2871 
2872  /* If entering on a new record, reset the current flags. */
2873  if (ssl_state->curr_connp->bytes_processed == 0) {
2874  ssl_state->current_flags = 0;
2875  }
2876 
2877  /* if we have more than one record */
2878  uint32_t max_records = MAX((input_len / SSL_RECORD_MINIMUM_LENGTH),1);
2879  while (input_len > 0) {
2880  if (counter > max_records) {
2881  SCLogDebug("Looks like we have looped quite a bit. Reset state "
2882  "and get out of here");
2883  SSLParserReset(ssl_state);
2884  SSLSetEvent(ssl_state,
2886  return APP_LAYER_ERROR;
2887  }
2888 
2889  /* ssl_state->bytes_processed is zero for a fresh record or
2890  positive to indicate a record currently being parsed */
2891 
2892  if (ssl_state->curr_connp->bytes_processed == 0) {
2893  if ((input[0] & 0x80) || (input[0] & 0x40)) {
2894  /* only SSLv2, has one of the top 2 bits set */
2895  ssl_state->curr_connp->version = SSL_VERSION_2;
2896  SCLogDebug("SSLv2 detected");
2897  } else if (ssl_state->curr_connp->version == SSL_VERSION_2) {
2898  ssl_state->curr_connp->version = TLS_VERSION_UNKNOWN;
2899  SCLogDebug("SSL/TLS version reset");
2900  }
2901  }
2902  SCLogDebug("record %u: bytes_processed %u, version %02X, input_len %u", counter,
2903  ssl_state->curr_connp->bytes_processed, ssl_state->curr_connp->version, input_len);
2904 
2905  if (ssl_state->curr_connp->version == SSL_VERSION_2) {
2906  if (ssl_state->curr_connp->bytes_processed == 0) {
2907  SCLogDebug("New SSLv2 record parsing");
2908  } else {
2909  SCLogDebug("Continuing parsing SSLv2 record");
2910  }
2911  struct SSLDecoderResult r =
2912  SSLv2Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2913  if (r.retval < 0 || r.retval > input_len) {
2914  DEBUG_VALIDATE_BUG_ON(r.retval > input_len);
2915  SCLogDebug("Error parsing SSLv2. Resetting parser "
2916  "state. Let's get outta here");
2917  SSLParserReset(ssl_state);
2918  SSLSetEvent(ssl_state,
2920  return APP_LAYER_ERROR;
2921  } else if (r.needed) {
2922  input += r.retval;
2923  SCLogDebug("returning consumed %" PRIuMAX " needed %u",
2924  (uintmax_t)(input - init_input), r.needed);
2925  SCReturnStruct(APP_LAYER_INCOMPLETE((uint32_t)(input - init_input), r.needed));
2926  }
2927  input_len -= r.retval;
2928  input += r.retval;
2929  SCLogDebug("SSLv2 decoder consumed %d bytes: %u left", r.retval, input_len);
2930  } else {
2931  if (ssl_state->curr_connp->bytes_processed == 0) {
2932  SCLogDebug("New TLS record: record_length %u",
2933  ssl_state->curr_connp->record_length);
2934  } else {
2935  SCLogDebug("Continuing parsing TLS record: record_length %u, bytes_processed %u",
2936  ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed);
2937  }
2938  struct SSLDecoderResult r =
2939  SSLv3Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2940  if (r.retval < 0 || r.retval > input_len) {
2941  DEBUG_VALIDATE_BUG_ON(r.retval > input_len);
2942  SCLogDebug("Error parsing TLS. Resetting parser "
2943  "state. Let's get outta here");
2944  SSLParserReset(ssl_state);
2945  return APP_LAYER_ERROR;
2946  } else if (r.needed) {
2947  input += r.retval;
2948  SCLogDebug("returning consumed %" PRIuMAX " needed %u",
2949  (uintmax_t)(input - init_input), r.needed);
2950  SCReturnStruct(APP_LAYER_INCOMPLETE((uint32_t)(input - init_input), r.needed));
2951  }
2952  input_len -= r.retval;
2953  input += r.retval;
2954  SCLogDebug("TLS decoder consumed %d bytes: %u left", r.retval, input_len);
2955 
2957  && ssl_state->curr_connp->record_length == 0) {
2958  SCLogDebug("TLS empty record");
2959  /* empty record */
2960  SSLParserReset(ssl_state);
2961  }
2962  }
2963  counter++;
2964  } /* while (input_len) */
2965 
2966  /* mark handshake as done if we have subject and issuer; the
2967  * certificate data must be complete - a certificate message whose
2968  * decode failed (even with the rest of the chain decoded) leaves the
2969  * data incomplete until a certificate message decodes fully, so the
2970  * tracks must not cross the cert phase on partial data */
2971  if ((ssl_state->flags & SSL_AL_FLAG_NEED_CLIENT_CERT) &&
2972  !ssl_state->client_connp.cert_data_incomplete &&
2973  ssl_state->client_connp.cert0_subject && ssl_state->client_connp.cert0_issuerdn) {
2974  /* per track: a track's own certificate data decides its own track;
2975  * pushing the peer track unconditionally would jump it over the
2976  * certificate phase before its own certificate is parsed (and
2977  * progression is monotonic, so the phase could never be visited
2978  * afterwards) */
2979  UpdateClientState(ssl_state, TLS_STATE_CLIENT_DATA);
2980  /* an mTLS handshake is complete with the client certificate: by
2981  * the flight order the server certificate phase was entered
2982  * already, so advancing the server track skips nothing (matters
2983  * when its own chain decode failed and no server app data
2984  * follows) */
2985  if (ssl_state->server_state >= TLS_STATE_SERVER_CERT) {
2986  UpdateServerState(ssl_state, TLS_STATE_SERVER_DATA);
2987  }
2988  }
2989  if (!ssl_state->server_connp.cert_data_incomplete && ssl_state->server_connp.cert0_subject &&
2990  ssl_state->server_connp.cert0_issuerdn) {
2991  UpdateServerState(ssl_state, TLS_STATE_SERVER_DATA);
2992  }
2993 
2994  /* flag session as finished if APP_LAYER_PARSER_EOF is set */
2995  if (SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TS) &&
2996  SCAppLayerParserStateIssetFlag(pstate, APP_LAYER_PARSER_EOF_TC)) {
2997  /* update both sides to keep existing behavior */
2998  UpdateClientState(ssl_state, TLS_STATE_CLIENT_FINISHED);
2999  UpdateServerState(ssl_state, TLS_STATE_SERVER_FINISHED);
3000  }
3001 
3002  return APP_LAYER_OK;
3003 }
3004 
3005 /* tls event content readiness: everything the logger may emit must be
3006  * final. cert0 fields are final as soon as any certificate decoded
3007  * into the slot - a later undecodable one in the same message does not
3008  * invalidate them (base parity: the old completion teleport logged on
3009  * fields alone). Where fields stay empty (resumption, a fully-garbage
3010  * chain), the track advance beyond the certificate phase makes the
3011  * data final; a track stuck below it can only end at close, where the
3012  * end-of-file pass logs. The cert_data_incomplete flag is not needed
3013  * here: it only gates phase advancement, and loggers run between
3014  * parses, never during one. The client certificate decision must also
3015  * be settled: the standard flight order puts the CertificateRequest
3016  * after the server Certificate, so server content readiness alone can
3017  * precede it - logging there would miss the client certificate
3018  * content and the decoder events a client flight may still raise (the
3019  * TLS tx logs once; base logged at server flight end, where both were
3020  * included). The decision is settled by the NEED_CLIENT_CERT flag, by
3021  * any message at or past the CertificateRequest position (server
3022  * flight: CertificateRequest, ServerHelloDone, Finished; client
3023  * flight: its Certificate or ClientKeyExchange - flag
3024  * SERVER_FLIGHT_DECIDED), by a change cipher spec record, or by the
3025  * client's first application data - a TLS 1.3 flight is encrypted
3026  * past the ServerHello, so its encrypted records are the mid-flow
3027  * settlement signal there (anything else leaves the log to close,
3028  * the generic release through SERVER_FINISHED). The mTLS branch is
3029  * separate and unaffected. */
3030 static bool TlsConnpLogReady(const SSLStateConnp *connp, const uint8_t track_done)
3031 {
3032  if (connp->cert0_subject && connp->cert0_issuerdn) {
3033  return true;
3034  }
3035  return track_done;
3036 }
3037 
3038 bool SSLV3TxLogReady(void *tx)
3039 {
3040  SSLState *ssl_state = (SSLState *)tx;
3041 
3042  if (!TlsConnpLogReady(
3043  &ssl_state->server_connp, ssl_state->server_state >= TLS_STATE_SERVER_DATA)) {
3044  return false;
3045  }
3046  if (ssl_state->flags & SSL_AL_FLAG_NEED_CLIENT_CERT) {
3047  if (!TlsConnpLogReady(
3048  &ssl_state->client_connp, ssl_state->client_state >= TLS_STATE_CLIENT_DATA)) {
3049  return false;
3050  }
3051  } else if ((ssl_state->flags &
3053  ssl_state->server_state < TLS_STATE_SERVER_FINISHED &&
3054  ssl_state->client_state < TLS_STATE_CLIENT_DATA) {
3055  /* a CertificateRequest may still arrive in this flight */
3056  return false;
3057  }
3058  return true;
3059 }
3060 
3061 static AppLayerResult SSLParseClientRecord(Flow *f, void *alstate, AppLayerParserState *pstate,
3062  StreamSlice stream_slice, void *local_data)
3063 {
3064  return SSLDecode(f, 0 /* toserver */, alstate, pstate, stream_slice);
3065 }
3066 
3067 static AppLayerResult SSLParseServerRecord(Flow *f, void *alstate, AppLayerParserState *pstate,
3068  StreamSlice stream_slice, void *local_data)
3069 {
3070  return SSLDecode(f, 1 /* toclient */, alstate, pstate, stream_slice);
3071 }
3072 
3073 /**
3074  * \internal
3075  * \brief Function to allocate the SSL state memory.
3076  */
3077 static void *SSLStateAlloc(void *orig_state, AppProto proto_orig)
3078 {
3079  SSLState *ssl_state = SCCalloc(1, sizeof(SSLState));
3080  if (unlikely(ssl_state == NULL))
3081  return NULL;
3082  TAILQ_INIT(&ssl_state->server_connp.certs);
3083  TAILQ_INIT(&ssl_state->client_connp.certs);
3084 
3085  return (void *)ssl_state;
3086 }
3087 
3088 static void SSLStateCertSANFree(SSLStateConnp *connp)
3089 {
3090  if (connp->cert0_sans) {
3091  for (uint16_t i = 0; i < connp->cert0_sans_num; i++) {
3092  SCX509ArrayFree(connp->cert0_sans[i].san, connp->cert0_sans[i].san_len);
3093  }
3094  SCFree(connp->cert0_sans);
3095  }
3096 }
3097 
3098 /**
3099  * \internal
3100  * \brief Function to free the SSL state memory.
3101  */
3102 static void SSLStateFree(void *p)
3103 {
3104  SSLState *ssl_state = (SSLState *)p;
3105  SSLCertsChain *item;
3106 
3107  if (ssl_state->client_connp.cert0_subject)
3108  SCX509ArrayFree(
3109  ssl_state->client_connp.cert0_subject, ssl_state->client_connp.cert0_subject_len);
3110  if (ssl_state->client_connp.cert0_issuerdn)
3111  SCX509ArrayFree(
3113  if (ssl_state->client_connp.cert0_serial)
3114  SCX509ArrayFree(
3115  ssl_state->client_connp.cert0_serial, ssl_state->client_connp.cert0_serial_len);
3116  if (ssl_state->client_connp.cert0_fingerprint)
3118  if (ssl_state->client_connp.sni)
3119  SCFree(ssl_state->client_connp.sni);
3120  if (ssl_state->client_connp.session_id)
3121  SCFree(ssl_state->client_connp.session_id);
3122  if (ssl_state->client_connp.hs_buffer)
3123  SCFree(ssl_state->client_connp.hs_buffer);
3124 
3125  if (ssl_state->server_connp.cert0_subject)
3126  SCX509ArrayFree(
3127  ssl_state->server_connp.cert0_subject, ssl_state->server_connp.cert0_subject_len);
3128  if (ssl_state->server_connp.cert0_issuerdn)
3129  SCX509ArrayFree(
3131  if (ssl_state->server_connp.cert0_serial)
3132  SCX509ArrayFree(
3133  ssl_state->server_connp.cert0_serial, ssl_state->server_connp.cert0_serial_len);
3134  if (ssl_state->server_connp.cert0_fingerprint)
3136  if (ssl_state->server_connp.sni)
3137  SCFree(ssl_state->server_connp.sni);
3138  if (ssl_state->server_connp.session_id)
3139  SCFree(ssl_state->server_connp.session_id);
3140 
3141  if (ssl_state->client_connp.hs)
3142  SCTLSHandshakeFree(ssl_state->client_connp.hs);
3143  if (ssl_state->client_connp.ja3_str)
3144  Ja3BufferFree(&ssl_state->client_connp.ja3_str);
3145  if (ssl_state->client_connp.ja3_hash)
3146  SCFree(ssl_state->client_connp.ja3_hash);
3147  if (ssl_state->server_connp.hs)
3148  SCTLSHandshakeFree(ssl_state->server_connp.hs);
3149  if (ssl_state->server_connp.ja3_str)
3150  Ja3BufferFree(&ssl_state->server_connp.ja3_str);
3151  if (ssl_state->server_connp.ja3_hash)
3152  SCFree(ssl_state->server_connp.ja3_hash);
3153  if (ssl_state->server_connp.hs_buffer)
3154  SCFree(ssl_state->server_connp.hs_buffer);
3155 
3156  SSLStateCertSANFree(&ssl_state->server_connp);
3157  SSLStateCertSANFree(&ssl_state->client_connp);
3158 
3159  SCAppLayerTxDataCleanup(&ssl_state->tx_data);
3160 
3161  /* Free certificate chain */
3162  if (ssl_state->server_connp.certs_buffer)
3163  SCFree(ssl_state->server_connp.certs_buffer);
3164  while ((item = TAILQ_FIRST(&ssl_state->server_connp.certs))) {
3165  TAILQ_REMOVE(&ssl_state->server_connp.certs, item, next);
3166  SCFree(item);
3167  }
3168  TAILQ_INIT(&ssl_state->server_connp.certs);
3169  /* Free certificate chain */
3170  if (ssl_state->client_connp.certs_buffer)
3171  SCFree(ssl_state->client_connp.certs_buffer);
3172  while ((item = TAILQ_FIRST(&ssl_state->client_connp.certs))) {
3173  TAILQ_REMOVE(&ssl_state->client_connp.certs, item, next);
3174  SCFree(item);
3175  }
3176  TAILQ_INIT(&ssl_state->client_connp.certs);
3177 
3178  SCFree(ssl_state);
3179 }
3180 
3181 static void SSLStateTransactionFree(void *state, uint64_t tx_id)
3182 {
3183  /* do nothing */
3184 }
3185 
3186 static AppProto SSLProbingParser(
3187  const Flow *f, uint8_t direction, const uint8_t *input, uint32_t ilen, uint8_t *rdir)
3188 {
3189  /* probably a rst/fin sending an eof */
3190  if (ilen < 3)
3191  return ALPROTO_UNKNOWN;
3192 
3193  /* for now just the 3 byte header ones */
3194  /* \todo Detect the 2 byte ones */
3195  if ((input[0] & 0x80) && (input[2] == 0x01)) {
3196  return ALPROTO_TLS;
3197  }
3198 
3199  return ALPROTO_FAILED;
3200 }
3201 
3202 static int SSLStateGetStateIdByName(const char *name, const uint8_t direction)
3203 {
3204  SCEnumCharMap *map =
3205  direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
3206 
3207  int id = SCMapEnumNameToValue(name, map);
3208  if (id < 0) {
3209  return -1;
3210  }
3211  return id;
3212 }
3213 
3214 static const char *SSLStateGetStateNameById(const int id, const uint8_t direction)
3215 {
3216  SCEnumCharMap *map =
3217  direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
3218  const char *name = SCMapEnumValueToName(id, map);
3219  return name;
3220 }
3221 
3222 static int SSLStateGetFrameIdByName(const char *frame_name)
3223 {
3224  int id = SCMapEnumNameToValue(frame_name, tls_frame_table);
3225  if (id < 0) {
3226  return -1;
3227  }
3228  return id;
3229 }
3230 
3231 static const char *SSLStateGetFrameNameById(const uint8_t frame_id)
3232 {
3233  const char *name = SCMapEnumValueToName(frame_id, tls_frame_table);
3234  return name;
3235 }
3236 
3237 static int SSLStateGetEventInfo(
3238  const char *event_name, uint8_t *event_id, AppLayerEventType *event_type)
3239 {
3240  if (SCAppLayerGetEventIdByName(event_name, tls_decoder_event_table, event_id) == 0) {
3241  *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
3242  return 0;
3243  }
3244  return -1;
3245 }
3246 
3247 static int SSLStateGetEventInfoById(
3248  uint8_t event_id, const char **event_name, AppLayerEventType *event_type)
3249 {
3250  *event_name = SCMapEnumValueToName(event_id, tls_decoder_event_table);
3251  if (*event_name == NULL) {
3252  SCLogError("event \"%d\" not present in "
3253  "ssl's enum map table.",
3254  event_id);
3255  /* yes this is fatal */
3256  return -1;
3257  }
3258 
3259  *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
3260 
3261  return 0;
3262 }
3263 
3264 static int SSLRegisterPatternsForProtocolDetection(void)
3265 {
3266  if (SCAppLayerProtoDetectPMRegisterPatternCSwPP(IPPROTO_TCP, ALPROTO_TLS, "|01 00 02|", 5, 2,
3267  STREAM_TOSERVER, SSLProbingParser, 0, 3) < 0) {
3268  return -1;
3269  }
3270 
3271  /** SSLv3 */
3273  IPPROTO_TCP, ALPROTO_TLS, "|01 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
3274  return -1;
3275  }
3277  IPPROTO_TCP, ALPROTO_TLS, "|16 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
3278  return -1;
3279  }
3280 
3281  /** TLSv1 */
3283  IPPROTO_TCP, ALPROTO_TLS, "|01 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
3284  return -1;
3285  }
3287  IPPROTO_TCP, ALPROTO_TLS, "|16 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
3288  return -1;
3289  }
3290 
3291  /** TLSv1.1 */
3293  IPPROTO_TCP, ALPROTO_TLS, "|01 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
3294  return -1;
3295  }
3297  IPPROTO_TCP, ALPROTO_TLS, "|16 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
3298  return -1;
3299  }
3300 
3301  /** TLSv1.2 */
3303  IPPROTO_TCP, ALPROTO_TLS, "|01 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
3304  return -1;
3305  }
3307  IPPROTO_TCP, ALPROTO_TLS, "|16 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
3308  return -1;
3309  }
3310 
3311  /***** toclient direction *****/
3312 
3314  IPPROTO_TCP, ALPROTO_TLS, "|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3315  return -1;
3316  }
3318  IPPROTO_TCP, ALPROTO_TLS, "|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3319  return -1;
3320  }
3322  IPPROTO_TCP, ALPROTO_TLS, "|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3323  return -1;
3324  }
3325 
3326  /** TLSv1 */
3328  IPPROTO_TCP, ALPROTO_TLS, "|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3329  return -1;
3330  }
3332  IPPROTO_TCP, ALPROTO_TLS, "|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3333  return -1;
3334  }
3336  IPPROTO_TCP, ALPROTO_TLS, "|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3337  return -1;
3338  }
3339 
3340  /** TLSv1.1 */
3342  IPPROTO_TCP, ALPROTO_TLS, "|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3343  return -1;
3344  }
3346  IPPROTO_TCP, ALPROTO_TLS, "|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3347  return -1;
3348  }
3350  IPPROTO_TCP, ALPROTO_TLS, "|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3351  return -1;
3352  }
3353 
3354  /** TLSv1.2 */
3356  IPPROTO_TCP, ALPROTO_TLS, "|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3357  return -1;
3358  }
3360  IPPROTO_TCP, ALPROTO_TLS, "|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3361  return -1;
3362  }
3364  IPPROTO_TCP, ALPROTO_TLS, "|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3365  return -1;
3366  }
3367 
3368  /* Subsection - SSLv2 style record by client, but informing the server
3369  * the max version it supports.
3370  * Updated by Anoop Saldanha. Disabled it for now. We'll get back to
3371  * it after some tests */
3372 #if 0
3374  "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
3375  {
3376  return -1;
3377  }
3379  "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
3380  {
3381  return -1;
3382  }
3383 #endif
3384 
3385  return 0;
3386 }
3387 
3388 #ifdef HAVE_JA3
3389 static void CheckJA3Enabled(void)
3390 {
3391  const char *strval = NULL;
3392  /* Check if we should generate JA3 fingerprints */
3393  int enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
3394  if (SCConfGetNonNull("app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
3395  enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
3396  } else if (strcmp(strval, "auto") == 0) {
3397  enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
3398  } else if (SCConfValIsFalse(strval)) {
3399  enable_ja3 = 0;
3400  ssl_config.disable_ja3 = true;
3401  } else if (SCConfValIsTrue(strval)) {
3402  enable_ja3 = true;
3403  }
3404  SC_ATOMIC_SET(ssl_config.enable_ja3, enable_ja3);
3406  /* The feature is available, i.e. _could_ be activated by a rule or
3407  even is enabled in the configuration. */
3409  }
3410 }
3411 #endif /* HAVE_JA3 */
3412 
3413 #ifdef HAVE_JA4
3414 static void CheckJA4Enabled(void)
3415 {
3416  const char *strval = NULL;
3417  /* Check if we should generate JA4 fingerprints */
3418  int enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3419  if (SCConfGetNonNull("app-layer.protocols.tls.ja4-fingerprints", &strval) != 1) {
3420  enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3421  } else if (strcmp(strval, "auto") == 0) {
3422  enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3423  } else if (SCConfValIsFalse(strval)) {
3424  enable_ja4 = 0;
3425  ssl_config.disable_ja4 = true;
3426  } else if (SCConfValIsTrue(strval)) {
3427  enable_ja4 = true;
3428  }
3429  SC_ATOMIC_SET(ssl_config.enable_ja4, enable_ja4);
3431  /* The feature is available, i.e. _could_ be activated by a rule or
3432  even is enabled in the configuration. */
3434  }
3435 }
3436 #endif /* HAVE_JA4 */
3437 
3438 /**
3439  * \brief Function to register the SSL protocol parser and other functions
3440  */
3441 #ifdef UNITTESTS
3442 void SSLStateParserRegisterTests(void);
3443 #endif /* UNITTESTS */
3444 
3446 {
3447  const char *proto_name = "tls";
3448 
3449  SC_ATOMIC_INIT(ssl_config.enable_ja3);
3450 
3451  /** SSLv2 and SSLv23*/
3452  if (SCAppLayerProtoDetectConfProtoDetectionEnabled("tcp", proto_name)) {
3454 
3455  if (SSLRegisterPatternsForProtocolDetection() < 0)
3456  return;
3457 
3458  if (RunmodeIsUnittests()) {
3460  IPPROTO_TCP, "443", ALPROTO_TLS, 0, 3, STREAM_TOSERVER, SSLProbingParser, NULL);
3461  } else {
3462  if (SCAppLayerProtoDetectPPParseConfPorts("tcp", IPPROTO_TCP, proto_name, ALPROTO_TLS,
3463  0, 3, SSLProbingParser, NULL) == 0) {
3464  SCLogConfig("no TLS config found, "
3465  "enabling TLS detection on port 443.");
3466  SCAppLayerProtoDetectPPRegister(IPPROTO_TCP, "443", ALPROTO_TLS, 0, 3,
3467  STREAM_TOSERVER, SSLProbingParser, NULL);
3468  }
3469  }
3470  } else {
3471  SCLogConfig("Protocol detection and parser disabled for %s protocol",
3472  proto_name);
3473  return;
3474  }
3475 
3476  if (SCAppLayerParserConfParserEnabled("tcp", proto_name)) {
3477  AppLayerParserRegisterParser(IPPROTO_TCP, ALPROTO_TLS, STREAM_TOSERVER,
3478  SSLParseClientRecord);
3479 
3480  AppLayerParserRegisterParser(IPPROTO_TCP, ALPROTO_TLS, STREAM_TOCLIENT,
3481  SSLParseServerRecord);
3483  IPPROTO_TCP, ALPROTO_TLS, SSLStateGetStateIdByName, SSLStateGetStateNameById);
3485  IPPROTO_TCP, ALPROTO_TLS, SSLStateGetFrameIdByName, SSLStateGetFrameNameById);
3486  AppLayerParserRegisterGetEventInfo(IPPROTO_TCP, ALPROTO_TLS, SSLStateGetEventInfo);
3487  AppLayerParserRegisterGetEventInfoById(IPPROTO_TCP, ALPROTO_TLS, SSLStateGetEventInfoById);
3488 
3489  AppLayerParserRegisterStateFuncs(IPPROTO_TCP, ALPROTO_TLS, SSLStateAlloc, SSLStateFree);
3490 
3492  IPPROTO_TCP, ALPROTO_TLS, STREAM_TOSERVER);
3493 
3494  AppLayerParserRegisterTxFreeFunc(IPPROTO_TCP, ALPROTO_TLS, SSLStateTransactionFree);
3495 
3496  AppLayerParserRegisterGetTx(IPPROTO_TCP, ALPROTO_TLS, SSLGetTx);
3497  AppLayerParserRegisterTxDataFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetTxData);
3498  AppLayerParserRegisterStateDataFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetStateData);
3499 
3500  AppLayerParserRegisterGetTxCnt(IPPROTO_TCP, ALPROTO_TLS, SSLGetTxCnt);
3501 
3502  AppLayerParserRegisterGetStateProgressFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetAlstateProgress);
3503 
3506 
3507 #ifdef UNITTESTS
3510 #endif /* UNITTESTS */
3511 
3512  SCConfNode *enc_handle = SCConfGetNode("app-layer.protocols.tls.encryption-handling");
3513  if (enc_handle != NULL && enc_handle->val != NULL) {
3514  SCLogDebug("have app-layer.protocols.tls.encryption-handling = %s", enc_handle->val);
3515  if (strcmp(enc_handle->val, "full") == 0) {
3517  } else if (strcmp(enc_handle->val, "bypass") == 0) {
3519  } else if (strcmp(enc_handle->val, "track-only") == 0) {
3521  } else if (strcmp(enc_handle->val, "default") == 0) {
3522  SCLogWarning("app-layer.protocols.tls.encryption-handling = default is deprecated "
3523  "and will be removed in Suricata 9, use \"track-only\" instead, "
3524  "(see ticket #7642)");
3526  } else {
3528  }
3529  } else {
3530  /* Get the value of no reassembly option from the config file */
3531  if (SCConfGetNode("app-layer.protocols.tls.no-reassemble") == NULL) {
3532  int value = 0;
3533  if (SCConfGetBool("tls.no-reassemble", &value) == 1 && value == 1)
3535  } else {
3536  int value = 0;
3537  if (SCConfGetBool("app-layer.protocols.tls.no-reassemble", &value) == 1 &&
3538  value == 1)
3540  }
3541  }
3542  SCLogDebug("ssl_config.encrypt_mode %u", ssl_config.encrypt_mode);
3543 
3544 #ifdef HAVE_JA3
3545  CheckJA3Enabled();
3546 #endif /* HAVE_JA3 */
3547 #ifdef HAVE_JA4
3548  CheckJA4Enabled();
3549 #endif /* HAVE_JA4 */
3550 
3551  if (g_disable_hashing) {
3552  if (SC_ATOMIC_GET(ssl_config.enable_ja3)) {
3553  SCLogWarning("MD5 calculation has been disabled, disabling JA3");
3554  SC_ATOMIC_SET(ssl_config.enable_ja3, 0);
3555  }
3556  if (SC_ATOMIC_GET(ssl_config.enable_ja4)) {
3557  SCLogWarning("Hashing has been disabled, disabling JA4");
3558  SC_ATOMIC_SET(ssl_config.enable_ja4, 0);
3559  }
3560  } else {
3561  if (RunmodeIsUnittests()) {
3562 #ifdef HAVE_JA3
3563  SC_ATOMIC_SET(ssl_config.enable_ja3, 1);
3564 #endif /* HAVE_JA3 */
3565 #ifdef HAVE_JA4
3566  SC_ATOMIC_SET(ssl_config.enable_ja4, 1);
3567 #endif /* HAVE_JA4 */
3568  }
3569  }
3570  } else {
3571  SCLogConfig("Parser disabled for %s protocol. Protocol detection still on.", proto_name);
3572  }
3573 }
3574 
3575 /**
3576  * \brief if not explicitly disabled in config, enable ja3 support
3577  *
3578  * Implemented using atomic to allow rule reloads to do this at
3579  * runtime.
3580  */
3581 void SSLEnableJA3(void)
3582 {
3584  return;
3585  }
3586  if (SC_ATOMIC_GET(ssl_config.enable_ja3)) {
3587  return;
3588  }
3589  SC_ATOMIC_SET(ssl_config.enable_ja3, 1);
3590 }
3591 
3592 /**
3593  * \brief if not explicitly disabled in config, enable ja4 support
3594  *
3595  * Implemented using atomic to allow rule reloads to do this at
3596  * runtime.
3597  */
3598 void SSLEnableJA4(void)
3599 {
3600  // only caller has #ifdef HAVE_JA4
3602  return;
3603  }
3604  if (SC_ATOMIC_GET(ssl_config.enable_ja4)) {
3605  return;
3606  }
3607  SC_ATOMIC_SET(ssl_config.enable_ja4, 1);
3608 }
3609 
3610 /**
3611  * \brief return whether ja3 is effectively enabled
3612  *
3613  * This means that it either has been enabled explicitly or has been
3614  * enabled by having loaded a rule while not being explicitly disabled.
3615  *
3616  * \retval true if enabled, false otherwise
3617  */
3619 {
3620  return SC_ATOMIC_GET(ssl_config.enable_ja3);
3621 }
3622 
3623 /**
3624  * \brief return whether ja4 is effectively enabled
3625  *
3626  * This means that it either has been enabled explicitly or has been
3627  * enabled by having loaded a rule while not being explicitly disabled.
3628  *
3629  * \retval true if enabled, false otherwise
3630  */
3632 {
3633  return SC_ATOMIC_GET(ssl_config.enable_ja4);
3634 }
3635 
3636 #ifdef UNITTESTS
3637 static int TLSPhaseStateTestClient(void)
3638 {
3639  const char *names[] = { "client_started", "client_hello", "client_cert", "client_data",
3640  "client_finished" };
3641  for (int i = 0; i < 5; i++) {
3643  IPPROTO_TCP, ALPROTO_TLS, names[i], STREAM_TOSERVER) != i);
3644  FAIL_IF(strcmp(AppLayerParserGetStateNameById(IPPROTO_TCP, ALPROTO_TLS, i, STREAM_TOSERVER),
3645  names[i]) != 0);
3646  }
3647  PASS;
3648 }
3649 
3650 static int TLSPhaseStateTestServer(void)
3651 {
3652  const char *names[] = { "server_started", "server_hello", "server_cert", "server_data",
3653  "server_finished" };
3654  for (int i = 0; i < 5; i++) {
3656  IPPROTO_TCP, ALPROTO_TLS, names[i], STREAM_TOCLIENT) != i);
3657  FAIL_IF(strcmp(AppLayerParserGetStateNameById(IPPROTO_TCP, ALPROTO_TLS, i, STREAM_TOCLIENT),
3658  names[i]) != 0);
3659  }
3660  PASS;
3661 }
3662 
3663 static int TLSPhaseStateTestOldNamesGone(void)
3664 {
3665  const char *old_client[] = { "client_in_progress", "client_hello_done", "client_cert_done",
3666  "client_handshake_done" };
3667  const char *old_server[] = { "server_in_progress", "server_hello_done", "server_cert_done",
3668  "server_handshake_done" };
3669  for (int i = 0; i < 4; i++) {
3671  IPPROTO_TCP, ALPROTO_TLS, old_client[i], STREAM_TOSERVER) >= 0);
3673  IPPROTO_TCP, ALPROTO_TLS, old_server[i], STREAM_TOCLIENT) >= 0);
3674  }
3675  PASS;
3676 }
3677 
3678 static int TLSPhaseStateTestCompletion(void)
3679 {
3682  /* progression is monotonic: a backwards write is a no-op (a peer track
3683  * shortcut must never undo a phase), an equal write passes */
3684  SSLState *ssl_state = SSLStateAlloc(NULL, ALPROTO_TLS);
3685  FAIL_IF(ssl_state == NULL);
3686  UpdateServerState(ssl_state, TLS_STATE_SERVER_DATA);
3687  UpdateServerState(ssl_state, TLS_STATE_SERVER_CERT);
3689  UpdateServerState(ssl_state, TLS_STATE_SERVER_DATA);
3691  SSLStateFree(ssl_state);
3692  PASS;
3693 }
3694 
3695 /* Drive a truncated ClientHello, a valid ServerHello and a garbage
3696  * Certificate through the handshake parser: a message enters its phase
3697  * at its start, a failed decode blocks only the hand-over to the next
3698  * phase - the bytes are consumed, the next message parses normally, and
3699  * a hello failure keeps the track in the hello phase. */
3700 static int TLSPhaseSuppressionTest(void)
3701 {
3702  SSLState *ssl_state = SSLStateAlloc(NULL, ALPROTO_TLS);
3703  if (ssl_state == NULL)
3704  return 0;
3705 
3706  /* P4: ClientHello, message length 6 - version parses, random
3707  * truncated */
3708  const uint8_t p4[] = { 0x01, 0x00, 0x00, 0x06, 0x03, 0x03, 0x01, 0x02, 0x03, 0x04 };
3709  /* P5: valid ServerHello (version, random, session id, selected
3710  * cipher, compression; no extensions - optional per RFC5246) */
3711  uint8_t p5[4 + 2 + 32 + 1 + 1 + 2 + 1];
3712  int i;
3713  int rc;
3714 
3715  p5[0] = 0x02;
3716  p5[1] = 0;
3717  p5[2] = 0;
3718  p5[3] = 39;
3719  p5[4] = 0x03;
3720  p5[5] = 0x03;
3721  for (i = 0; i < 32; i++)
3722  p5[6 + i] = (uint8_t)i;
3723  p5[38] = 1; /* session id length */
3724  p5[39] = 0xaa; /* session id */
3725  p5[40] = 0x13;
3726  p5[41] = 0x01; /* selected cipher suite */
3727  p5[42] = 0x00; /* compression */
3728  /* P6: Certificate with a 10-byte garbage body (not ASN.1) */
3729  const uint8_t p6[] = { 0x0b, 0x00, 0x00, 0x10, 0x00, 0x00, 0x0d, 0x00, 0x00, 0x0a, 0x41, 0x41,
3730  0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41 };
3731 
3732  /* truncated hello: entered at its start, the failed decode blocks
3733  * the hand-over, so the client stays in client_hello */
3734  ssl_state->curr_connp = &ssl_state->client_connp;
3735  ssl_state->curr_connp->record_length = sizeof(p4);
3736  ssl_state->curr_connp->bytes_processed = 0;
3737  ssl_state->curr_connp->version = 0x0303;
3738  rc = SSLv3ParseHandshakeProtocol(ssl_state, p4, sizeof(p4), 0);
3739  FAIL_IF(rc < 0);
3740  FAIL_IF(!ssl_state->client_connp.phase_suppressed);
3742 
3743  /* valid hello: the flag is reset per message, completion hands the
3744  * track to the cert phase */
3745  ssl_state->curr_connp = &ssl_state->server_connp;
3746  ssl_state->curr_connp->record_length = sizeof(p5);
3747  ssl_state->curr_connp->bytes_processed = 0;
3748  ssl_state->curr_connp->version = 0x0303;
3749  rc = SSLv3ParseHandshakeProtocol(ssl_state, p5, sizeof(p5), 1);
3750  FAIL_IF(rc < 0);
3753  /* the cert phase is entered with the hello, the chain is not
3754  * parsed yet: chain content is not final */
3756 
3757  /* garbage certificate: the cert phase was entered at the message
3758  * header, the failed decode keeps the track in it; the suppression
3759  * flag is hello-specific */
3760  ssl_state->curr_connp = &ssl_state->server_connp;
3761  ssl_state->curr_connp->record_length = sizeof(p6);
3762  ssl_state->curr_connp->bytes_processed = 0;
3763  rc = SSLv3ParseHandshakeProtocol(ssl_state, p6, sizeof(p6), 1);
3764  FAIL_IF(rc < 0);
3769 
3770  SSLStateFree(ssl_state);
3771  PASS;
3772 }
3773 
3774 /* A Certificate message whose first certificate is undecodable but whose
3775  * remaining certificates are valid: the bad leaf is skipped (one decoder
3776  * event), the rest of the chain is kept and the cert0 fields are filled
3777  * from the first decodable certificate (compared against a direct
3778  * extraction from the fixture). The message enters the cert phase at its
3779  * start; the incomplete-data condition is sticky - a following
3780  * ServerHelloDone resets the per-message suppression but the track stays
3781  * in the cert phase, out of the data phase. The valid certificate body
3782  * is the same fixture as the sv test ruletype-firewall-153 client
3783  * certificate. */
3784 static int TLSPhaseCertChainSkipTest(void)
3785 {
3786  static const uint8_t valid_cert[] = { 0x30, 0x82, 0x03, 0x0b, 0x30, 0x82, 0x01, 0xf3, 0xa0,
3787  0x03, 0x02, 0x01, 0x02, 0x02, 0x14, 0x16, 0x1b, 0x77, 0xa9, 0xae, 0xe9, 0xea, 0xaa, 0x2d,
3788  0xf9, 0xbb, 0xd6, 0xda, 0x13, 0x92, 0xf8, 0x73, 0xf8, 0x93, 0xa0, 0x30, 0x0d, 0x06, 0x09,
3789  0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x15, 0x31, 0x13,
3790  0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0a, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74,
3791  0x74, 0x65, 0x73, 0x74, 0x30, 0x1e, 0x17, 0x0d, 0x32, 0x36, 0x30, 0x39, 0x31, 0x38, 0x30,
3792  0x33, 0x34, 0x38, 0x35, 0x36, 0x5a, 0x17, 0x0d, 0x32, 0x36, 0x30, 0x39, 0x32, 0x30, 0x30,
3793  0x33, 0x34, 0x38, 0x35, 0x36, 0x5a, 0x30, 0x15, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55,
3794  0x04, 0x03, 0x0c, 0x0a, 0x63, 0x6c, 0x69, 0x65, 0x6e, 0x74, 0x74, 0x65, 0x73, 0x74, 0x30,
3795  0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
3796  0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01,
3797  0x01, 0x00, 0xc7, 0xdc, 0x32, 0x3c, 0x76, 0x5d, 0x86, 0x02, 0xbd, 0x3e, 0xc0, 0x8e, 0x6f,
3798  0x34, 0x7e, 0xa2, 0x2c, 0x66, 0xe4, 0x20, 0x2f, 0x1c, 0xf4, 0x6c, 0x4d, 0xb6, 0x8e, 0xed,
3799  0x5c, 0xdf, 0x36, 0xdb, 0xa8, 0xea, 0xf9, 0x01, 0xbe, 0x20, 0x92, 0x85, 0x72, 0x9c, 0x5d,
3800  0x19, 0xf5, 0xa1, 0xa6, 0xdf, 0x60, 0x17, 0xc3, 0xba, 0xb7, 0xed, 0x57, 0x99, 0x19, 0x46,
3801  0xee, 0xb2, 0x04, 0xfe, 0x18, 0x17, 0x65, 0x62, 0xe2, 0xd7, 0x45, 0xb1, 0x0f, 0x0c, 0xc8,
3802  0x6b, 0xe4, 0x63, 0x4a, 0x6b, 0xd9, 0x30, 0xae, 0xf5, 0x81, 0xc5, 0xac, 0x50, 0x51, 0xc6,
3803  0x91, 0x5c, 0x78, 0xc0, 0x74, 0x2a, 0x47, 0x6b, 0xae, 0xc3, 0x4b, 0x7d, 0x54, 0x66, 0x0d,
3804  0xd7, 0xa1, 0x81, 0x07, 0x80, 0x44, 0xba, 0x30, 0x02, 0xeb, 0x82, 0x3c, 0x78, 0xbb, 0x9f,
3805  0x2b, 0xeb, 0x1b, 0x1c, 0x30, 0x41, 0x02, 0xeb, 0xf5, 0x9c, 0xb3, 0xef, 0x43, 0x52, 0x4b,
3806  0x54, 0x1f, 0xf3, 0x0d, 0xeb, 0xcc, 0x25, 0x97, 0xda, 0x52, 0x07, 0xdf, 0x54, 0x35, 0x4b,
3807  0x85, 0x63, 0x67, 0xd6, 0x60, 0x9f, 0x0f, 0x88, 0xbf, 0x7e, 0xc9, 0x44, 0x2f, 0x34, 0xd6,
3808  0x28, 0xad, 0x2d, 0x59, 0x7e, 0x56, 0xb6, 0x73, 0x5a, 0xe0, 0xb0, 0x1c, 0xc9, 0x23, 0x5f,
3809  0x2a, 0x3c, 0x46, 0x1c, 0x27, 0x7f, 0x0e, 0x25, 0x16, 0x19, 0xb0, 0x03, 0x71, 0x05, 0xd5,
3810  0xc6, 0xed, 0x49, 0x7c, 0x90, 0xca, 0x94, 0xfb, 0xe4, 0x75, 0x23, 0x67, 0x17, 0x37, 0x08,
3811  0x05, 0xb8, 0x58, 0x7f, 0x61, 0x16, 0x24, 0x91, 0xeb, 0x0c, 0x1c, 0x05, 0x0d, 0xa5, 0x45,
3812  0x55, 0x03, 0x2e, 0x60, 0x15, 0xc4, 0x6b, 0xcb, 0x68, 0x1f, 0xec, 0xbe, 0x59, 0xbf, 0x4a,
3813  0x93, 0x0b, 0xa3, 0x08, 0x86, 0xe0, 0xe9, 0x5c, 0x08, 0x23, 0x3b, 0x0f, 0xbd, 0xc9, 0x91,
3814  0xa1, 0x48, 0xf9, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x53, 0x30, 0x51, 0x30, 0x1d, 0x06,
3815  0x03, 0x55, 0x1d, 0x0e, 0x04, 0x16, 0x04, 0x14, 0xae, 0xed, 0xef, 0x6c, 0xe0, 0xf6, 0xb7,
3816  0xe3, 0x8c, 0xd9, 0x37, 0x7f, 0x0b, 0x53, 0x36, 0x96, 0x06, 0x5d, 0x0f, 0x09, 0x30, 0x1f,
3817  0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80, 0x14, 0xae, 0xed, 0xef, 0x6c,
3818  0xe0, 0xf6, 0xb7, 0xe3, 0x8c, 0xd9, 0x37, 0x7f, 0x0b, 0x53, 0x36, 0x96, 0x06, 0x5d, 0x0f,
3819  0x09, 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x05, 0x30, 0x03,
3820  0x01, 0x01, 0xff, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
3821  0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x08, 0x2e, 0x15, 0x02, 0x17, 0xcb, 0xf8,
3822  0x5b, 0x4b, 0x31, 0x47, 0xf4, 0x8d, 0x55, 0x53, 0xa4, 0x05, 0x46, 0x73, 0xb7, 0x6a, 0x79,
3823  0xfc, 0x02, 0xe4, 0x89, 0x09, 0xb9, 0x3d, 0x8c, 0xda, 0x98, 0x0d, 0x67, 0x83, 0x30, 0x00,
3824  0xe5, 0x3b, 0xc4, 0x7c, 0xc4, 0x63, 0xa7, 0x20, 0x97, 0x74, 0x8d, 0x18, 0xfa, 0xb1, 0xe7,
3825  0x67, 0x13, 0x48, 0x55, 0x99, 0xde, 0xca, 0x70, 0xe4, 0x7a, 0x3a, 0x65, 0x28, 0x08, 0xce,
3826  0xc3, 0x93, 0xbb, 0x95, 0x15, 0x10, 0x25, 0x67, 0x7a, 0xad, 0xe8, 0xbc, 0xfb, 0xee, 0x94,
3827  0xfa, 0x09, 0x6a, 0x3e, 0xa6, 0xb9, 0x9b, 0xd7, 0x01, 0xe4, 0x4f, 0x53, 0x69, 0x92, 0x8a,
3828  0xf4, 0x52, 0xbf, 0x44, 0xfd, 0x26, 0x80, 0xb3, 0xed, 0x9d, 0x25, 0xa8, 0x99, 0x24, 0x1e,
3829  0xd7, 0x29, 0xde, 0xa3, 0x08, 0x63, 0x44, 0x2a, 0xb9, 0x94, 0x19, 0x73, 0xb9, 0xf3, 0xee,
3830  0x67, 0xc1, 0x81, 0xdd, 0x78, 0x2f, 0x8c, 0x1d, 0x7e, 0xb5, 0x06, 0x97, 0x24, 0x60, 0xfb,
3831  0x27, 0xf1, 0x4d, 0x85, 0x2d, 0x1e, 0x8c, 0x13, 0x95, 0x2b, 0x5c, 0x12, 0x3a, 0xe3, 0x31,
3832  0x55, 0x07, 0xdd, 0xf2, 0x85, 0xc5, 0x2e, 0x28, 0x2a, 0x9b, 0x3a, 0x89, 0xef, 0xd6, 0xba,
3833  0x00, 0x55, 0xc2, 0x61, 0x74, 0xaf, 0xcc, 0xfd, 0x44, 0x7c, 0xae, 0xd8, 0xbb, 0xa7, 0xb7,
3834  0x93, 0xc1, 0x3a, 0x0d, 0xef, 0x7e, 0x14, 0x67, 0x3a, 0x0b, 0x62, 0x57, 0x5e, 0x78, 0x58,
3835  0x5c, 0x63, 0x2b, 0x79, 0x2d, 0x81, 0x61, 0x4d, 0xd2, 0xdb, 0x41, 0xe9, 0xbd, 0x00, 0xc8,
3836  0x26, 0xd3, 0xea, 0x6b, 0x10, 0xa9, 0x8e, 0xab, 0x64, 0x85, 0x6a, 0x55, 0xc1, 0x78, 0x65,
3837  0x31, 0x9b, 0x4d, 0x44, 0x84, 0xea, 0x60, 0x15, 0x52, 0x6b, 0x9d, 0x4b, 0x14, 0x5c, 0xba,
3838  0x4f, 0xa2, 0x53, 0x8f, 0x2a, 0x2c, 0xda, 0x36, 0x96
3839 
3840  };
3841 
3842  /* a garbage first certificate (10 bytes, not ASN.1) followed by the
3843  * valid one */
3844  const uint32_t bad_len = 10;
3845  const uint32_t good_len = sizeof(valid_cert);
3846  const uint32_t chain_len = 3 + bad_len + 3 + good_len;
3847  const uint32_t hs_len = 3 + chain_len;
3848  const uint32_t msg_len = 4 + 3 + 3 + bad_len + 3 + good_len;
3849  uint8_t msg[msg_len];
3850 
3851  msg[0] = 0x0b;
3852  msg[1] = 0;
3853  msg[2] = (hs_len >> 8) & 0xff;
3854  msg[3] = hs_len & 0xff;
3855  msg[4] = 0;
3856  msg[5] = (chain_len >> 8) & 0xff;
3857  msg[6] = chain_len & 0xff;
3858  msg[7] = 0;
3859  msg[8] = 0;
3860  msg[9] = bad_len;
3861  for (uint32_t k = 0; k < bad_len; k++)
3862  msg[10 + k] = 0x41;
3863  const uint32_t off = 10 + bad_len;
3864  msg[off] = 0;
3865  msg[off + 1] = (good_len >> 8) & 0xff;
3866  msg[off + 2] = good_len & 0xff;
3867  memcpy(msg + off + 3, valid_cert, good_len);
3868 
3869  SSLState *ssl_state = SSLStateAlloc(NULL, ALPROTO_TLS);
3870  if (ssl_state == NULL)
3871  return 0;
3872 
3873  ssl_state->curr_connp = &ssl_state->server_connp;
3874  ssl_state->curr_connp->record_length = msg_len;
3875  ssl_state->curr_connp->bytes_processed = 0;
3876  ssl_state->curr_connp->version = 0x0303;
3877 
3878  /* the bytes are consumed (rc > 0) and the cert phase is entered at
3879  * the header; the incomplete data keeps the track out of the
3880  * handshake-done shortcut at the end of SSLDecode (the cert-fields
3881  * based tail requires complete certificate data), not out of the
3882  * record-level data phase (app data and CCS advance the track
3883  * regardless); the suppression flag is hello-specific */
3884  int rc = SSLv3ParseHandshakeProtocol(ssl_state, msg, msg_len, 1);
3885  FAIL_IF(rc < 0);
3889  /* the rest of the chain survived: the valid certificate is in the
3890  * chain and the cert0 fields come from the first decodable cert */
3891  SSLCertsChain *item = TAILQ_FIRST(&ssl_state->server_connp.certs);
3892  FAIL_IF(item == NULL);
3893  FAIL_IF(item->cert_len != good_len);
3894  FAIL_IF(ssl_state->server_connp.cert0_subject == NULL);
3895  FAIL_IF(ssl_state->server_connp.cert0_issuerdn == NULL);
3896 
3897  /* the certificate data comes from the valid (second) certificate:
3898  * compare the fields against a direct extraction from the fixture */
3899  uint32_t errc = 0;
3900  X509 *ref = SCX509Decode(valid_cert, sizeof(valid_cert), &errc);
3901  FAIL_IF(ref == NULL);
3902  uint8_t *subj = NULL;
3903  uint32_t subj_len = 0;
3904  uint8_t *iss = NULL;
3905  uint32_t iss_len = 0;
3906  SCX509GetSubject(ref, &subj, &subj_len);
3907  SCX509GetIssuer(ref, &iss, &iss_len);
3908  FAIL_IF(subj == NULL || iss == NULL);
3909  FAIL_IF(ssl_state->server_connp.cert0_subject_len != subj_len);
3910  FAIL_IF(memcmp(ssl_state->server_connp.cert0_subject, subj, subj_len) != 0);
3911  FAIL_IF(ssl_state->server_connp.cert0_issuerdn_len != iss_len);
3912  FAIL_IF(memcmp(ssl_state->server_connp.cert0_issuerdn, iss, iss_len) != 0);
3913  if (subj != NULL)
3914  SCX509ArrayFree(subj, subj_len);
3915  if (iss != NULL)
3916  SCX509ArrayFree(iss, iss_len);
3917  SCX509Free(ref);
3918 
3919  /* the incomplete-certificate-data condition is sticky: a later
3920  * handshake message resets the per-message suppression but does not
3921  * complete the certificate data (only a fully decoded certificate
3922  * message does), so the SSLDecode tail shortcut stays off and the
3923  * track stays in the cert phase, out of the data phase */
3924  const uint8_t shd[] = { 0x0e, 0x00, 0x00, 0x00 };
3925  ssl_state->curr_connp = &ssl_state->server_connp;
3926  ssl_state->curr_connp->record_length = sizeof(shd);
3927  ssl_state->curr_connp->bytes_processed = 0;
3928  rc = SSLv3ParseHandshakeProtocol(ssl_state, shd, sizeof(shd), 1);
3929  FAIL_IF(rc < 0);
3933 
3934  /* a fully decodable first certificate message clears the incomplete
3935  * flag and enters the cert phase at its start: the preconditions for
3936  * the cert-fields based data shortcut (the shortcut itself is pinned
3937  * by sv ruletype-firewall-51). On the client connp: its one-shot
3938  * buffer is untouched here */
3939  const uint32_t mlen = 4 + 3 + 3 + sizeof(valid_cert);
3940  uint8_t full[mlen];
3941  memset(full, 0, sizeof(full));
3942  full[0] = 0x0b;
3943  full[1] = ((3 + 3 + good_len) >> 16) & 0xff;
3944  full[2] = ((3 + 3 + good_len) >> 8) & 0xff;
3945  full[3] = (3 + 3 + good_len) & 0xff;
3946  full[4] = ((3 + good_len) >> 16) & 0xff;
3947  full[5] = ((3 + good_len) >> 8) & 0xff;
3948  full[6] = (3 + good_len) & 0xff;
3949  full[7] = (good_len >> 16) & 0xff;
3950  full[8] = (good_len >> 8) & 0xff;
3951  full[9] = good_len & 0xff;
3952  memcpy(full + 10, valid_cert, good_len);
3953  ssl_state->curr_connp = &ssl_state->client_connp;
3954  ssl_state->curr_connp->record_length = mlen;
3955  ssl_state->curr_connp->bytes_processed = 0;
3956  rc = SSLv3ParseHandshakeProtocol(ssl_state, full, mlen, 0);
3957  FAIL_IF(rc < 0);
3959  FAIL_IF(ssl_state->client_connp.cert0_subject == NULL);
3961  FAIL_IF(!ssl_state->client_connp.cert_chain_final);
3962 
3963  /* the one-shot buffer latches: a second server certificate message
3964  * is not decoded and cannot clear the incomplete flag (base
3965  * semantics, renegotiation re-sends the same certificate) */
3966  ssl_state->curr_connp = &ssl_state->server_connp;
3967  ssl_state->curr_connp->record_length = mlen;
3968  ssl_state->curr_connp->bytes_processed = 0;
3969  rc = SSLv3ParseHandshakeProtocol(ssl_state, full, mlen, 1);
3970  FAIL_IF(rc < 0);
3972 
3973  SSLStateFree(ssl_state);
3974  PASS;
3975 }
3976 
3977 /* The parser accepts the flight-settling handshake types from either
3978  * direction. A wrong-direction CLIENT_KEY_EXCHANGE (server -> client) must
3979  * not settle the server flight decision: doing so would release the one-shot
3980  * TLS logger before a later CertificateRequest, whose client certificate
3981  * would then be omitted. Only the client's own message settles it; a server
3982  * CertificateRequest still settles the decision afterwards. */
3983 static int TLSPhaseWrongDirectionKeyxTest(void)
3984 {
3985  SSLState *ssl_state = SSLStateAlloc(NULL, ALPROTO_TLS);
3986  if (ssl_state == NULL)
3987  return 0;
3988 
3989  /* wrong direction: server -> client CLIENT_KEY_EXCHANGE */
3990  const uint8_t ckx[] = { 0x10, 0x00, 0x00, 0x00 };
3991  ssl_state->curr_connp = &ssl_state->server_connp;
3992  ssl_state->curr_connp->record_length = sizeof(ckx);
3993  ssl_state->curr_connp->bytes_processed = 0;
3994  int rc = SSLv3ParseHandshakeProtocol(ssl_state, ckx, sizeof(ckx), 1);
3995  FAIL_IF(rc < 0);
3997 
3998  /* the later CertificateRequest still arrives and settles the decision */
3999  const uint8_t certreq[] = { 0x0d, 0x00, 0x00, 0x00 };
4000  ssl_state->curr_connp = &ssl_state->server_connp;
4001  ssl_state->curr_connp->record_length = sizeof(certreq);
4002  ssl_state->curr_connp->bytes_processed = 0;
4003  rc = SSLv3ParseHandshakeProtocol(ssl_state, certreq, sizeof(certreq), 1);
4004  FAIL_IF(rc < 0);
4005  FAIL_IF((ssl_state->current_flags & SSL_AL_FLAG_NEED_CLIENT_CERT) == 0);
4007 
4008  /* the client's own CLIENT_KEY_EXCHANGE settles it */
4009  SSLState *client_ckx = SSLStateAlloc(NULL, ALPROTO_TLS);
4010  FAIL_IF(client_ckx == NULL);
4011  client_ckx->curr_connp = &client_ckx->client_connp;
4012  client_ckx->curr_connp->record_length = sizeof(ckx);
4013  client_ckx->curr_connp->bytes_processed = 0;
4014  rc = SSLv3ParseHandshakeProtocol(client_ckx, ckx, sizeof(ckx), 0);
4015  FAIL_IF(rc < 0);
4017 
4018  SSLStateFree(ssl_state);
4019  SSLStateFree(client_ckx);
4020  PASS;
4021 }
4022 
4024 {
4025  UtRegisterTest("TLSPhaseStateTestClient", TLSPhaseStateTestClient);
4026  UtRegisterTest("TLSPhaseStateTestServer", TLSPhaseStateTestServer);
4027  UtRegisterTest("TLSPhaseStateTestOldNamesGone", TLSPhaseStateTestOldNamesGone);
4028  UtRegisterTest("TLSPhaseStateTestCompletion", TLSPhaseStateTestCompletion);
4029  UtRegisterTest("TLSPhaseSuppressionTest", TLSPhaseSuppressionTest);
4030  UtRegisterTest("TLSPhaseCertChainSkipTest", TLSPhaseCertChainSkipTest);
4031  UtRegisterTest("TLSPhaseWrongDirectionKeyxTest", TLSPhaseWrongDirectionKeyxTest);
4032 }
4033 #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:265
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
SSLStateConnp_::hs_hdr
uint8_t hs_hdr[4]
Definition: app-layer-ssl.h:256
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:275
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:286
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:3598
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:287
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:360
SSL_EXTENSION_SUPPORTED_VERSIONS
#define SSL_EXTENSION_SUPPORTED_VERSIONS
Definition: app-layer-ssl.h:158
SSLState_::f
Flow * f
Definition: app-layer-ssl.h:266
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:269
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:281
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:268
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:279
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
SSLStateConnp_::hs_hdr_len
uint8_t hs_hdr_len
Definition: app-layer-ssl.h:257
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:3618
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:283
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:284
SSLEnableJA3
void SSLEnableJA3(void)
if not explicitly disabled in config, enable ja3 support
Definition: app-layer-ssl.c:3581
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:3445
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:4023
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:3038
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:3631
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:272
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