suricata
app-layer-ssl.c
Go to the documentation of this file.
1 /* Copyright (C) 2007-2019 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
22  * \author Pierre Chifflier <pierre.chifflier@ssi.gouv.fr>
23  * \author Mats Klepsland <mats.klepsland@gmail.com>
24  *
25  */
26 
27 #include "suricata-common.h"
28 #include "debug.h"
29 #include "decode.h"
30 #include "threads.h"
31 
32 #include "stream-tcp-private.h"
33 #include "stream-tcp-reassemble.h"
34 #include "stream-tcp.h"
35 #include "stream.h"
36 
37 #include "app-layer.h"
38 #include "app-layer-protos.h"
39 #include "app-layer-parser.h"
40 #include "app-layer-ssl.h"
41 
42 #include "decode-events.h"
43 #include "conf.h"
44 
45 #include "util-crypt.h"
46 #include "util-decode-der.h"
47 #include "util-decode-der-get.h"
48 #include "util-spm.h"
49 #include "util-unittest.h"
50 #include "util-debug.h"
51 #include "util-print.h"
52 #include "util-pool.h"
53 #include "util-byte.h"
54 #include "util-ja3.h"
55 #include "flow-util.h"
56 #include "flow-private.h"
57 
59  /* TLS protocol messages */
60  { "INVALID_SSLV2_HEADER", TLS_DECODER_EVENT_INVALID_SSLV2_HEADER },
61  { "INVALID_TLS_HEADER", TLS_DECODER_EVENT_INVALID_TLS_HEADER },
62  { "INVALID_RECORD_VERSION", TLS_DECODER_EVENT_INVALID_RECORD_VERSION },
63  { "INVALID_RECORD_TYPE", TLS_DECODER_EVENT_INVALID_RECORD_TYPE },
64  { "INVALID_HANDSHAKE_MESSAGE", TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE },
65  { "HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_HEARTBEAT },
66  { "INVALID_HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_INVALID_HEARTBEAT },
67  { "OVERFLOW_HEARTBEAT_MESSAGE", TLS_DECODER_EVENT_OVERFLOW_HEARTBEAT },
68  { "DATALEAK_HEARTBEAT_MISMATCH", TLS_DECODER_EVENT_DATALEAK_HEARTBEAT_MISMATCH },
69  { "HANDSHAKE_INVALID_LENGTH", TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH },
70  { "MULTIPLE_SNI_EXTENSIONS", TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS },
71  { "INVALID_SNI_TYPE", TLS_DECODER_EVENT_INVALID_SNI_TYPE },
72  { "INVALID_SNI_LENGTH", TLS_DECODER_EVENT_INVALID_SNI_LENGTH },
73  { "TOO_MANY_RECORDS_IN_PACKET", TLS_DECODER_EVENT_TOO_MANY_RECORDS_IN_PACKET },
74  /* certificate decoding messages */
75  { "INVALID_CERTIFICATE", TLS_DECODER_EVENT_INVALID_CERTIFICATE },
76  { "CERTIFICATE_MISSING_ELEMENT", TLS_DECODER_EVENT_CERTIFICATE_MISSING_ELEMENT },
77  { "CERTIFICATE_UNKNOWN_ELEMENT", TLS_DECODER_EVENT_CERTIFICATE_UNKNOWN_ELEMENT },
78  { "CERTIFICATE_INVALID_LENGTH", TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH },
79  { "CERTIFICATE_INVALID_STRING", TLS_DECODER_EVENT_CERTIFICATE_INVALID_STRING },
80  { "ERROR_MESSAGE_ENCOUNTERED", TLS_DECODER_EVENT_ERROR_MSG_ENCOUNTERED },
81  /* used as a generic error event */
82  { "INVALID_SSL_RECORD", TLS_DECODER_EVENT_INVALID_SSL_RECORD },
83  { NULL, -1 },
84 };
85 
86 /* JA3 fingerprints are disabled by default */
87 #define SSL_CONFIG_DEFAULT_JA3 0
88 
90  SSL_CNF_ENC_HANDLE_DEFAULT = 0, /**< disable raw content, continue tracking */
91  SSL_CNF_ENC_HANDLE_BYPASS = 1, /**< skip processing of flow, bypass if possible */
92  SSL_CNF_ENC_HANDLE_FULL = 2, /**< handle fully like any other proto */
93 };
94 
95 typedef struct SslConfig_ {
97  /** dynamic setting for ja3: can be enabled on demand if not explicitly
98  * disabled. */
99  SC_ATOMIC_DECLARE(int, enable_ja3);
100  bool disable_ja3; /**< ja3 explicitly disabled. Don't enable on demand. */
101 } SslConfig;
102 
104 
105 /* SSLv3 record types */
106 #define SSLV3_CHANGE_CIPHER_SPEC 20
107 #define SSLV3_ALERT_PROTOCOL 21
108 #define SSLV3_HANDSHAKE_PROTOCOL 22
109 #define SSLV3_APPLICATION_PROTOCOL 23
110 #define SSLV3_HEARTBEAT_PROTOCOL 24
111 
112 /* SSLv3 handshake protocol types */
113 #define SSLV3_HS_HELLO_REQUEST 0
114 #define SSLV3_HS_CLIENT_HELLO 1
115 #define SSLV3_HS_SERVER_HELLO 2
116 #define SSLV3_HS_NEW_SESSION_TICKET 4
117 #define SSLV3_HS_CERTIFICATE 11
118 #define SSLV3_HS_SERVER_KEY_EXCHANGE 12
119 #define SSLV3_HS_CERTIFICATE_REQUEST 13
120 #define SSLV3_HS_SERVER_HELLO_DONE 14
121 #define SSLV3_HS_CERTIFICATE_VERIFY 15
122 #define SSLV3_HS_CLIENT_KEY_EXCHANGE 16
123 #define SSLV3_HS_FINISHED 20
124 #define SSLV3_HS_CERTIFICATE_URL 21
125 #define SSLV3_HS_CERTIFICATE_STATUS 22
126 
127 /* SSLv2 protocol message types */
128 #define SSLV2_MT_ERROR 0
129 #define SSLV2_MT_CLIENT_HELLO 1
130 #define SSLV2_MT_CLIENT_MASTER_KEY 2
131 #define SSLV2_MT_CLIENT_FINISHED 3
132 #define SSLV2_MT_SERVER_HELLO 4
133 #define SSLV2_MT_SERVER_VERIFY 5
134 #define SSLV2_MT_SERVER_FINISHED 6
135 #define SSLV2_MT_REQUEST_CERTIFICATE 7
136 #define SSLV2_MT_CLIENT_CERTIFICATE 8
137 
138 #define SSLV3_RECORD_HDR_LEN 5
139 #define SSLV3_MESSAGE_HDR_LEN 4
140 
141 #define SSLV3_CLIENT_HELLO_VERSION_LEN 2
142 #define SSLV3_CLIENT_HELLO_RANDOM_LEN 32
143 
144 /* TLS heartbeat protocol types */
145 #define TLS_HB_REQUEST 1
146 #define TLS_HB_RESPONSE 2
147 
148 #define SSL_RECORD_MINIMUM_LENGTH 6
149 
150 #define SHA1_STRING_LENGTH 60
151 
152 #define HAS_SPACE(n) ((uint64_t)(input - initial_input) + (uint64_t)(n) <= (uint64_t)(input_len))
153 
154 static void SSLParserReset(SSLState *ssl_state)
155 {
156  ssl_state->curr_connp->bytes_processed = 0;
157 }
158 
159 void SSLSetEvent(SSLState *ssl_state, uint8_t event)
160 {
161  if (ssl_state == NULL) {
162  SCLogDebug("Could not set decoder event: %u", event);
163  return;
164  }
165 
167  ssl_state->events++;
168 }
169 
170 static AppLayerDecoderEvents *SSLGetEvents(void *tx)
171 {
172  /* for TLS, TX == state, see GetTx */
173  SSLState *ssl_state = (SSLState *)tx;
174  return ssl_state->decoder_events;
175 }
176 
177 static int SSLSetTxDetectState(void *vtx, DetectEngineState *de_state)
178 {
179  SSLState *ssl_state = (SSLState *)vtx;
180  ssl_state->de_state = de_state;
181  return 0;
182 }
183 
184 static DetectEngineState *SSLGetTxDetectState(void *vtx)
185 {
186  SSLState *ssl_state = (SSLState *)vtx;
187  return ssl_state->de_state;
188 }
189 
190 static void *SSLGetTx(void *state, uint64_t tx_id)
191 {
192  SSLState *ssl_state = (SSLState *)state;
193  return ssl_state;
194 }
195 
196 static uint64_t SSLGetTxCnt(void *state)
197 {
198  /* single tx */
199  return 1;
200 }
201 
202 static void SSLSetTxLogged(void *state, void *tx, LoggerId logged)
203 {
204  SSLState *ssl_state = (SSLState *)state;
205  if (ssl_state)
206  ssl_state->logged = logged;
207 }
208 
209 static LoggerId SSLGetTxLogged(void *state, void *tx)
210 {
211  SSLState *ssl_state = (SSLState *)state;
212  if (ssl_state)
213  return (ssl_state->logged);
214 
215  return 0;
216 }
217 
218 static int SSLGetAlstateProgressCompletionStatus(uint8_t direction)
219 {
220  return TLS_STATE_FINISHED;
221 }
222 
223 static int SSLGetAlstateProgress(void *tx, uint8_t direction)
224 {
225  SSLState *ssl_state = (SSLState *)tx;
226 
227  /* we don't care about direction, only that app-layer parser is done
228  and have sent an EOF */
229  if (ssl_state->flags & SSL_AL_FLAG_STATE_FINISHED) {
230  return TLS_STATE_FINISHED;
231  }
232 
233  /* we want the logger to log when the handshake is done, even if the
234  state is not finished */
235  if (ssl_state->flags & SSL_AL_FLAG_HANDSHAKE_DONE) {
236  return TLS_HANDSHAKE_DONE;
237  }
238 
239  if (direction == STREAM_TOSERVER &&
240  (ssl_state->server_connp.cert0_subject != NULL ||
241  ssl_state->server_connp.cert0_issuerdn != NULL))
242  {
243  return TLS_STATE_CERT_READY;
244  }
245 
246  return TLS_STATE_IN_PROGRESS;
247 }
248 
249 static uint64_t SSLGetTxDetectFlags(void *vtx, uint8_t dir)
250 {
251  SSLState *ssl_state = (SSLState *)vtx;
252  if (dir & STREAM_TOSERVER) {
253  return ssl_state->detect_flags_ts;
254  } else {
255  return ssl_state->detect_flags_tc;
256  }
257 }
258 
259 static void SSLSetTxDetectFlags(void *vtx, uint8_t dir, uint64_t flags)
260 {
261  SSLState *ssl_state = (SSLState *)vtx;
262  if (dir & STREAM_TOSERVER) {
263  ssl_state->detect_flags_ts = flags;
264  } else {
265  ssl_state->detect_flags_tc = flags;
266  }
267 }
268 
269 void SSLVersionToString(uint16_t version, char *buffer)
270 {
271  buffer[0] = '\0';
272 
273  switch (version) {
274  case TLS_VERSION_UNKNOWN:
275  strlcat(buffer, "UNDETERMINED", 13);
276  break;
277  case SSL_VERSION_2:
278  strlcat(buffer, "SSLv2", 6);
279  break;
280  case SSL_VERSION_3:
281  strlcat(buffer, "SSLv3", 6);
282  break;
283  case TLS_VERSION_10:
284  strlcat(buffer, "TLSv1", 6);
285  break;
286  case TLS_VERSION_11:
287  strlcat(buffer, "TLS 1.1", 8);
288  break;
289  case TLS_VERSION_12:
290  strlcat(buffer, "TLS 1.2", 8);
291  break;
292  case TLS_VERSION_13:
293  strlcat(buffer, "TLS 1.3", 8);
294  break;
296  strlcat(buffer, "TLS 1.3 draft-28", 17);
297  break;
299  strlcat(buffer, "TLS 1.3 draft-27", 17);
300  break;
302  strlcat(buffer, "TLS 1.3 draft-26", 17);
303  break;
305  strlcat(buffer, "TLS 1.3 draft-25", 17);
306  break;
308  strlcat(buffer, "TLS 1.3 draft-24", 17);
309  break;
311  strlcat(buffer, "TLS 1.3 draft-23", 17);
312  break;
314  strlcat(buffer, "TLS 1.3 draft-22", 17);
315  break;
317  strlcat(buffer, "TLS 1.3 draft-21", 17);
318  break;
320  strlcat(buffer, "TLS 1.3 draft-20", 17);
321  break;
323  strlcat(buffer, "TLS 1.3 draft-19", 17);
324  break;
326  strlcat(buffer, "TLS 1.3 draft-18", 17);
327  break;
329  strlcat(buffer, "TLS 1.3 draft-17", 17);
330  break;
332  strlcat(buffer, "TLS 1.3 draft-16", 17);
333  break;
335  strlcat(buffer, "TLS 1.3 draft-<16", 18);
336  break;
338  strlcat(buffer, "TLS 1.3 draft-20-fb", 20);
339  break;
341  strlcat(buffer, "TLS 1.3 draft-21-fb", 20);
342  break;
344  strlcat(buffer, "TLS 1.3 draft-22-fb", 20);
345  break;
347  strlcat(buffer, "TLS 1.3 draft-23-fb", 20);
348  break;
350  strlcat(buffer, "TLS 1.3 draft-26-fb", 20);
351  break;
352  default:
353  snprintf(buffer, 7, "0x%04x", version);
354  break;
355  }
356 }
357 
358 static void TlsDecodeHSCertificateErrSetEvent(SSLState *ssl_state, uint32_t err)
359 {
360  switch (err) {
362  SSLSetEvent(ssl_state,
364  break;
368  SSLSetEvent(ssl_state,
370  break;
372  SSLSetEvent(ssl_state,
374  break;
376  SSLSetEvent(ssl_state,
378  break;
379  case ERR_DER_INVALID_TAG:
381  case ERR_DER_GENERIC:
382  default:
384  break;
385  }
386 }
387 
388 static inline int TlsDecodeHSCertificateSubject(SSLState *ssl_state,
389  Asn1Generic *cert)
390 {
391  if (unlikely(ssl_state->server_connp.cert0_subject != NULL))
392  return 0;
393 
394  uint32_t err = 0;
395  char buffer[512];
396 
397  int rc = Asn1DerGetSubjectDN(cert, buffer, sizeof(buffer), &err);
398  if (rc != 0) {
399  TlsDecodeHSCertificateErrSetEvent(ssl_state, err);
400  return 0;
401  }
402 
403  ssl_state->server_connp.cert0_subject = SCStrdup(buffer);
404  if (ssl_state->server_connp.cert0_subject == NULL)
405  return -1;
406 
407  return 0;
408 }
409 
410 static inline int TlsDecodeHSCertificateIssuer(SSLState *ssl_state,
411  Asn1Generic *cert)
412 {
413  if (unlikely(ssl_state->server_connp.cert0_issuerdn != NULL))
414  return 0;
415 
416  uint32_t err = 0;
417  char buffer[512];
418 
419  int rc = Asn1DerGetIssuerDN(cert, buffer, sizeof(buffer), &err);
420  if (rc != 0) {
421  TlsDecodeHSCertificateErrSetEvent(ssl_state, err);
422  return 0;
423  }
424 
425  ssl_state->server_connp.cert0_issuerdn = SCStrdup(buffer);
426  if (ssl_state->server_connp.cert0_issuerdn == NULL)
427  return -1;
428 
429  return 0;
430 }
431 
432 static inline int TlsDecodeHSCertificateSerial(SSLState *ssl_state,
433  Asn1Generic *cert)
434 {
435  if (unlikely(ssl_state->server_connp.cert0_serial != NULL))
436  return 0;
437 
438  uint32_t err = 0;
439  char buffer[512];
440 
441  int rc = Asn1DerGetSerial(cert, buffer, sizeof(buffer), &err);
442  if (rc != 0) {
443  TlsDecodeHSCertificateErrSetEvent(ssl_state, err);
444  return 0;
445  }
446 
447  ssl_state->server_connp.cert0_serial = SCStrdup(buffer);
448  if (ssl_state->server_connp.cert0_serial == NULL)
449  return -1;
450 
451  return 0;
452 }
453 
454 static inline int TlsDecodeHSCertificateValidity(SSLState *ssl_state,
455  Asn1Generic *cert)
456 {
457  uint32_t err = 0;
458  time_t not_before;
459  time_t not_after;
460 
461  int rc = Asn1DerGetValidity(cert, &not_before, &not_after, &err);
462  if (rc != 0) {
463  TlsDecodeHSCertificateErrSetEvent(ssl_state, err);
464  return 0;
465  }
466 
467  ssl_state->server_connp.cert0_not_before = not_before;
468  ssl_state->server_connp.cert0_not_after = not_after;
469 
470  return 0;
471 }
472 
473 static inline int TlsDecodeHSCertificateFingerprint(SSLState *ssl_state,
474  const uint8_t *input,
475  uint32_t cert_len)
476 {
477  if (unlikely(ssl_state->server_connp.cert0_fingerprint != NULL))
478  return 0;
479 
481  sizeof(char));
482  if (ssl_state->server_connp.cert0_fingerprint == NULL)
483  return -1;
484 
485  uint8_t hash[SHA1_LENGTH];
486  if (ComputeSHA1(input, cert_len, hash, sizeof(hash)) == 1) {
487  for (int i = 0, x = 0; x < SHA1_LENGTH; x++)
488  {
489  i += snprintf(ssl_state->server_connp.cert0_fingerprint + i,
490  SHA1_STRING_LENGTH - i, i == 0 ? "%02x" : ":%02x",
491  hash[x]);
492  }
493  }
494  return 0;
495 }
496 
497 static inline int TlsDecodeHSCertificateAddCertToChain(SSLState *ssl_state,
498  const uint8_t *input,
499  uint32_t cert_len)
500 {
501  SSLCertsChain *cert = SCCalloc(1, sizeof(SSLCertsChain));
502  if (cert == NULL)
503  return -1;
504 
505  cert->cert_data = (uint8_t *)input;
506  cert->cert_len = cert_len;
507  TAILQ_INSERT_TAIL(&ssl_state->server_connp.certs, cert, next);
508 
509  return 0;
510 }
511 
512 static int TlsDecodeHSCertificate(SSLState *ssl_state,
513  const uint8_t * const initial_input,
514  const uint32_t input_len)
515 {
516  const uint8_t *input = (uint8_t *)initial_input;
517 
518  Asn1Generic *cert = NULL;
519 
520  if (!(HAS_SPACE(3)))
521  return 1;
522 
523  uint32_t cert_chain_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
524  input += 3;
525 
526  if (!(HAS_SPACE(cert_chain_len)))
527  return 0;
528 
529  uint32_t processed_len = 0;
530  /* coverity[tainted_data] */
531  while (processed_len < cert_chain_len)
532  {
533  if (!(HAS_SPACE(3)))
534  goto invalid_cert;
535 
536  uint32_t cert_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
537  input += 3;
538 
539  if (!(HAS_SPACE(cert_len)))
540  goto invalid_cert;
541 
542  uint32_t err = 0;
543  int rc = 0;
544 
545  /* only store fields from the first certificate in the chain */
546  if (processed_len == 0) {
547  /* coverity[tainted_data] */
548  cert = DecodeDer(input, cert_len, &err);
549  if (cert == NULL) {
550  TlsDecodeHSCertificateErrSetEvent(ssl_state, err);
551  goto next;
552  }
553 
554  rc = TlsDecodeHSCertificateSubject(ssl_state, cert);
555  if (rc != 0)
556  goto error;
557 
558  rc = TlsDecodeHSCertificateIssuer(ssl_state, cert);
559  if (rc != 0)
560  goto error;
561 
562  rc = TlsDecodeHSCertificateSerial(ssl_state, cert);
563  if (rc != 0)
564  goto error;
565 
566  rc = TlsDecodeHSCertificateValidity(ssl_state, cert);
567  if (rc != 0)
568  goto error;
569 
570  rc = TlsDecodeHSCertificateFingerprint(ssl_state, input, cert_len);
571  if (rc != 0)
572  goto error;
573 
574  DerFree(cert);
575  cert = NULL;
576  }
577 
578  rc = TlsDecodeHSCertificateAddCertToChain(ssl_state, input, cert_len);
579  if (rc != 0)
580  goto error;
581 
582 next:
583  input += cert_len;
584  processed_len += cert_len + 3;
585  }
586 
587  return (input - initial_input);
588 
589 error:
590  if (cert != NULL)
591  DerFree(cert);
592  return -1;
593 
594 invalid_cert:
595  SCLogDebug("TLS invalid certificate");
597  return -1;
598 }
599 
600 /**
601  * \inline
602  * \brief Check if value is GREASE.
603  *
604  * http://tools.ietf.org/html/draft-davidben-tls-grease-00
605  *
606  * \param value Value to check.
607  *
608  * \retval 1 if is GREASE.
609  * \retval 0 if not is GREASE.
610  */
611 static inline int TLSDecodeValueIsGREASE(const uint16_t value)
612 {
613  switch (value)
614  {
615  case 0x0a0a:
616  case 0x1a1a:
617  case 0x2a2a:
618  case 0x3a3a:
619  case 0x4a4a:
620  case 0x5a5a:
621  case 0x6a6a:
622  case 0x7a7a:
623  case 0x8a8a:
624  case 0x9a9a:
625  case 0xaaaa:
626  case 0xbaba:
627  case 0xcaca:
628  case 0xdada:
629  case 0xeaea:
630  case 0xfafa:
631  return 1;
632  default:
633  return 0;
634  }
635 }
636 
637 static inline int TLSDecodeHSHelloVersion(SSLState *ssl_state,
638  const uint8_t * const initial_input,
639  const uint32_t input_len)
640 {
641  uint8_t *input = (uint8_t *)initial_input;
642 
644  SCLogDebug("TLS handshake invalid length");
645  SSLSetEvent(ssl_state,
647  return -1;
648  }
649 
650  uint16_t version = *input << 8 | *(input + 1);
651  ssl_state->curr_connp->version = version;
652 
653  /* TLSv1.3 draft1 to draft21 use the version field as earlier TLS
654  versions, instead of using the supported versions extension. */
655  if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
656  ((ssl_state->curr_connp->version == TLS_VERSION_13) ||
657  (((ssl_state->curr_connp->version >> 8) & 0xff) == 0x7f))) {
658  ssl_state->flags |= SSL_AL_FLAG_LOG_WITHOUT_CERT;
659  }
660 
661  /* Catch some early TLSv1.3 draft implementations that does not conform
662  to the draft version. */
663  if ((ssl_state->curr_connp->version >= 0x7f01) &&
664  (ssl_state->curr_connp->version < 0x7f10)) {
666  }
667 
668  /* TLSv1.3 drafts from draft1 to draft15 use 0x0304 (TLSv1.3) as the
669  version number, which makes it hard to accurately pinpoint the
670  exact draft version. */
671  else if (ssl_state->curr_connp->version == TLS_VERSION_13) {
673  }
674 
675  if (SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_str == NULL) {
676  ssl_state->curr_connp->ja3_str = Ja3BufferInit();
677  if (ssl_state->curr_connp->ja3_str == NULL)
678  return -1;
679 
680  int rc = Ja3BufferAddValue(&ssl_state->curr_connp->ja3_str, version);
681  if (rc != 0)
682  return -1;
683  }
684 
686 
687  return (input - initial_input);
688 }
689 
690 static inline int TLSDecodeHSHelloRandom(SSLState *ssl_state,
691  const uint8_t * const initial_input,
692  const uint32_t input_len)
693 {
694  uint8_t *input = (uint8_t *)initial_input;
695 
697  SCLogDebug("TLS handshake invalid length");
698  SSLSetEvent(ssl_state,
700  return -1;
701  }
702 
703  /* Skip random */
705 
706  return (input - initial_input);
707 }
708 
709 static inline int TLSDecodeHSHelloSessionID(SSLState *ssl_state,
710  const uint8_t * const initial_input,
711  const uint32_t input_len)
712 {
713  uint8_t *input = (uint8_t *)initial_input;
714 
715  if (!(HAS_SPACE(1)))
716  goto invalid_length;
717 
718  uint8_t session_id_length = *input;
719  input += 1;
720 
721  if (!(HAS_SPACE(session_id_length)))
722  goto invalid_length;
723 
724  if (session_id_length != 0 && ssl_state->curr_connp->session_id == NULL) {
725  ssl_state->curr_connp->session_id = SCMalloc(session_id_length);
726 
727  if (unlikely(ssl_state->curr_connp->session_id == NULL)) {
728  return -1;
729  }
730 
731  memcpy(ssl_state->curr_connp->session_id, input, session_id_length);
732  ssl_state->curr_connp->session_id_length = session_id_length;
733 
734  if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
735  ssl_state->client_connp.session_id != NULL &&
736  ssl_state->server_connp.session_id != NULL) {
737  if ((ssl_state->client_connp.session_id_length ==
738  ssl_state->server_connp.session_id_length) &&
739  (memcmp(ssl_state->server_connp.session_id,
740  ssl_state->client_connp.session_id, session_id_length) == 0)) {
741  ssl_state->flags |= SSL_AL_FLAG_SESSION_RESUMED;
742  }
743  }
744  }
745 
746  input += session_id_length;
747 
748  return (input - initial_input);
749 
750 invalid_length:
751  SCLogDebug("TLS handshake invalid length");
752  SSLSetEvent(ssl_state,
754  return -1;
755 }
756 
757 static inline int TLSDecodeHSHelloCipherSuites(SSLState *ssl_state,
758  const uint8_t * const initial_input,
759  const uint32_t input_len)
760 {
761  const uint8_t *input = initial_input;
762 
763  if (!(HAS_SPACE(2)))
764  goto invalid_length;
765 
766  uint16_t cipher_suites_length;
767 
769  cipher_suites_length = 2;
770  } else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
771  cipher_suites_length = *input << 8 | *(input + 1);
772  input += 2;
773  } else {
774  return -1;
775  }
776 
777  if (!(HAS_SPACE(cipher_suites_length)))
778  goto invalid_length;
779 
780  /* Cipher suites length should always be divisible by 2 */
781  if ((cipher_suites_length % 2) != 0) {
782  goto invalid_length;
783  }
784 
785  if (SC_ATOMIC_GET(ssl_config.enable_ja3)) {
786  JA3Buffer *ja3_cipher_suites = Ja3BufferInit();
787  if (ja3_cipher_suites == NULL)
788  return -1;
789 
790  uint16_t processed_len = 0;
791  /* coverity[tainted_data] */
792  while (processed_len < cipher_suites_length)
793  {
794  if (!(HAS_SPACE(2))) {
795  Ja3BufferFree(&ja3_cipher_suites);
796  goto invalid_length;
797  }
798 
799  uint16_t cipher_suite = *input << 8 | *(input + 1);
800  input += 2;
801 
802  if (TLSDecodeValueIsGREASE(cipher_suite) != 1) {
803  int rc = Ja3BufferAddValue(&ja3_cipher_suites, cipher_suite);
804  if (rc != 0) {
805  return -1;
806  }
807  }
808 
809  processed_len += 2;
810  }
811 
812  int rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
813  &ja3_cipher_suites);
814  if (rc == -1) {
815  return -1;
816  }
817 
818  } else {
819  /* Skip cipher suites */
820  input += cipher_suites_length;
821  }
822 
823  return (input - initial_input);
824 
825 invalid_length:
826  SCLogDebug("TLS handshake invalid length");
827  SSLSetEvent(ssl_state,
829  return -1;
830 }
831 
832 static inline int TLSDecodeHSHelloCompressionMethods(SSLState *ssl_state,
833  const uint8_t * const initial_input,
834  const uint32_t input_len)
835 {
836  const uint8_t *input = initial_input;
837 
838  if (!(HAS_SPACE(1)))
839  goto invalid_length;
840 
841  /* Skip compression methods */
843  input += 1;
844  } else {
845  uint8_t compression_methods_length = *input;
846  input += 1;
847 
848  if (!(HAS_SPACE(compression_methods_length)))
849  goto invalid_length;
850 
851  input += compression_methods_length;
852  }
853 
854  return (input - initial_input);
855 
856 invalid_length:
857  SCLogDebug("TLS handshake invalid_length");
858  SSLSetEvent(ssl_state,
860  return -1;
861 }
862 
863 static inline int TLSDecodeHSHelloExtensionSni(SSLState *ssl_state,
864  const uint8_t * const initial_input,
865  const uint32_t input_len)
866 {
867  uint8_t *input = (uint8_t *)initial_input;
868 
869  /* Empty extension */
870  if (input_len == 0)
871  return 0;
872 
873  if (!(HAS_SPACE(2)))
874  goto invalid_length;
875 
876  /* Skip sni_list_length */
877  input += 2;
878 
879  if (!(HAS_SPACE(1)))
880  goto invalid_length;
881 
882  uint8_t sni_type = *input;
883  input += 1;
884 
885  /* Currently the only type allowed is host_name
886  (RFC6066 section 3). */
887  if (sni_type != SSL_SNI_TYPE_HOST_NAME) {
888  SCLogDebug("Unknown SNI type");
889  SSLSetEvent(ssl_state,
891  return -1;
892  }
893 
894  if (!(HAS_SPACE(2)))
895  goto invalid_length;
896 
897  uint16_t sni_len = *input << 8 | *(input + 1);
898  input += 2;
899 
900  if (!(HAS_SPACE(sni_len)))
901  goto invalid_length;
902 
903  /* There must not be more than one extension of the same
904  type (RFC5246 section 7.4.1.4). */
905  if (ssl_state->curr_connp->sni) {
906  SCLogDebug("Multiple SNI extensions");
907  SSLSetEvent(ssl_state,
909  input += sni_len;
910  return (input - initial_input);
911  }
912 
913  /* host_name contains the fully qualified domain name,
914  and should therefore be limited by the maximum domain
915  name length. */
916  if (sni_len > 255) {
917  SCLogDebug("SNI length >255");
918  SSLSetEvent(ssl_state,
920  return -1;
921  }
922 
923  size_t sni_strlen = sni_len + 1;
924  ssl_state->curr_connp->sni = SCMalloc(sni_strlen);
925  if (unlikely(ssl_state->curr_connp->sni == NULL))
926  return -1;
927 
928  memcpy(ssl_state->curr_connp->sni, input, sni_strlen - 1);
929  ssl_state->curr_connp->sni[sni_strlen-1] = 0;
930 
931  input += sni_len;
932 
933  return (input - initial_input);
934 
935 invalid_length:
936  SCLogDebug("TLS handshake invalid length");
937  SSLSetEvent(ssl_state,
939 
940 
941  return -1;
942 }
943 
944 static inline int TLSDecodeHSHelloExtensionSupportedVersions(SSLState *ssl_state,
945  const uint8_t * const initial_input,
946  const uint32_t input_len)
947 {
948  const uint8_t *input = initial_input;
949 
950  /* Empty extension */
951  if (input_len == 0)
952  return 0;
953 
955  if (!(HAS_SPACE(1)))
956  goto invalid_length;
957 
958  uint8_t supported_ver_len = *input;
959  input += 1;
960 
961  if (supported_ver_len < 2)
962  goto invalid_length;
963 
964  if (!(HAS_SPACE(supported_ver_len)))
965  goto invalid_length;
966 
967  /* Use the first (and prefered) version as client version */
968  ssl_state->curr_connp->version = *input << 8 | *(input + 1);
969 
970  /* Set a flag to indicate that we have seen this extension */
972 
973  input += supported_ver_len;
974  }
975  else if (ssl_state->current_flags & SSL_AL_FLAG_STATE_SERVER_HELLO) {
976  if (!(HAS_SPACE(2)))
977  goto invalid_length;
978 
979  uint16_t ver = *input << 8 | *(input + 1);
980 
981  if ((ssl_state->flags & SSL_AL_FLAG_CH_VERSION_EXTENSION) &&
982  (ver > TLS_VERSION_12)) {
983  ssl_state->flags |= SSL_AL_FLAG_LOG_WITHOUT_CERT;
984  }
985 
986  ssl_state->curr_connp->version = ver;
987  input += 2;
988  }
989 
990  return (input - initial_input);
991 
992 invalid_length:
993  SCLogDebug("TLS handshake invalid length");
994  SSLSetEvent(ssl_state,
996 
997  return -1;
998 }
999 
1000 static inline int TLSDecodeHSHelloExtensionEllipticCurves(SSLState *ssl_state,
1001  const uint8_t * const initial_input,
1002  const uint32_t input_len,
1003  JA3Buffer *ja3_elliptic_curves)
1004 {
1005  const uint8_t *input = initial_input;
1006 
1007  /* Empty extension */
1008  if (input_len == 0)
1009  return 0;
1010 
1011  if (!(HAS_SPACE(2)))
1012  goto invalid_length;
1013 
1014  uint16_t elliptic_curves_len = *input << 8 | *(input + 1);
1015  input += 2;
1016 
1017  if (!(HAS_SPACE(elliptic_curves_len)))
1018  goto invalid_length;
1019 
1020  if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
1021  SC_ATOMIC_GET(ssl_config.enable_ja3)) {
1022  uint16_t ec_processed_len = 0;
1023  /* coverity[tainted_data] */
1024  while (ec_processed_len < elliptic_curves_len)
1025  {
1026  if (!(HAS_SPACE(2)))
1027  goto invalid_length;
1028 
1029  uint16_t elliptic_curve = *input << 8 | *(input + 1);
1030  input += 2;
1031 
1032  if (TLSDecodeValueIsGREASE(elliptic_curve) != 1) {
1033  int rc = Ja3BufferAddValue(&ja3_elliptic_curves,
1034  elliptic_curve);
1035  if (rc != 0)
1036  return -1;
1037  }
1038 
1039  ec_processed_len += 2;
1040  }
1041 
1042  } else {
1043  /* Skip elliptic curves */
1044  input += elliptic_curves_len;
1045  }
1046 
1047  return (input - initial_input);
1048 
1049 invalid_length:
1050  SCLogDebug("TLS handshake invalid length");
1051  SSLSetEvent(ssl_state,
1053 
1054  return -1;
1055 }
1056 
1057 static inline int TLSDecodeHSHelloExtensionEllipticCurvePF(SSLState *ssl_state,
1058  const uint8_t * const initial_input,
1059  const uint32_t input_len,
1060  JA3Buffer *ja3_elliptic_curves_pf)
1061 {
1062  const uint8_t *input = initial_input;
1063 
1064  /* Empty extension */
1065  if (input_len == 0)
1066  return 0;
1067 
1068  if (!(HAS_SPACE(1)))
1069  goto invalid_length;
1070 
1071  uint8_t ec_pf_len = *input;
1072  input += 1;
1073 
1074  if (!(HAS_SPACE(ec_pf_len)))
1075  goto invalid_length;
1076 
1077  if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
1078  SC_ATOMIC_GET(ssl_config.enable_ja3)) {
1079  uint8_t ec_pf_processed_len = 0;
1080  /* coverity[tainted_data] */
1081  while (ec_pf_processed_len < ec_pf_len)
1082  {
1083  uint8_t elliptic_curve_pf = *input;
1084  input += 1;
1085 
1086  if (TLSDecodeValueIsGREASE(elliptic_curve_pf) != 1) {
1087  int rc = Ja3BufferAddValue(&ja3_elliptic_curves_pf,
1088  elliptic_curve_pf);
1089  if (rc != 0)
1090  return -1;
1091  }
1092 
1093  ec_pf_processed_len += 1;
1094  }
1095 
1096  } else {
1097  /* Skip elliptic curve point formats */
1098  input += ec_pf_len;
1099  }
1100 
1101  return (input - initial_input);
1102 
1103 invalid_length:
1104  SCLogDebug("TLS handshake invalid length");
1105  SSLSetEvent(ssl_state,
1107 
1108  return -1;
1109 }
1110 
1111 static inline int TLSDecodeHSHelloExtensions(SSLState *ssl_state,
1112  const uint8_t * const initial_input,
1113  const uint32_t input_len)
1114 {
1115  const uint8_t *input = initial_input;
1116 
1117  int ret;
1118  int rc;
1119  const bool ja3 = (SC_ATOMIC_GET(ssl_config.enable_ja3) == 1);
1120 
1121  JA3Buffer *ja3_extensions = NULL;
1122  JA3Buffer *ja3_elliptic_curves = NULL;
1123  JA3Buffer *ja3_elliptic_curves_pf = NULL;
1124 
1125  if (ja3) {
1126  ja3_extensions = Ja3BufferInit();
1127  if (ja3_extensions == NULL)
1128  goto error;
1129 
1130  if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1131  ja3_elliptic_curves = Ja3BufferInit();
1132  if (ja3_elliptic_curves == NULL)
1133  goto error;
1134 
1135  ja3_elliptic_curves_pf = Ja3BufferInit();
1136  if (ja3_elliptic_curves_pf == NULL)
1137  goto error;
1138  }
1139  }
1140 
1141  /* Extensions are optional (RFC5246 section 7.4.1.2) */
1142  if (!(HAS_SPACE(2)))
1143  goto end;
1144 
1145  uint16_t extensions_len = *input << 8 | *(input + 1);
1146  input += 2;
1147 
1148  if (!(HAS_SPACE(extensions_len)))
1149  goto invalid_length;
1150 
1151  uint16_t processed_len = 0;
1152  /* coverity[tainted_data] */
1153  while (processed_len < extensions_len)
1154  {
1155  if (!(HAS_SPACE(2)))
1156  goto invalid_length;
1157 
1158  uint16_t ext_type = *input << 8 | *(input + 1);
1159  input += 2;
1160 
1161  if (!(HAS_SPACE(2)))
1162  goto invalid_length;
1163 
1164  uint16_t ext_len = *input << 8 | *(input + 1);
1165  input += 2;
1166 
1167  if (!(HAS_SPACE(ext_len)))
1168  goto invalid_length;
1169 
1170  switch (ext_type) {
1171  case SSL_EXTENSION_SNI:
1172  {
1173  /* coverity[tainted_data] */
1174  ret = TLSDecodeHSHelloExtensionSni(ssl_state, input,
1175  ext_len);
1176  if (ret < 0)
1177  goto end;
1178 
1179  input += ret;
1180 
1181  break;
1182  }
1183 
1185  {
1186  /* coverity[tainted_data] */
1187  ret = TLSDecodeHSHelloExtensionEllipticCurves(ssl_state, input,
1188  ext_len,
1189  ja3_elliptic_curves);
1190  if (ret < 0)
1191  goto end;
1192 
1193  input += ret;
1194 
1195  break;
1196  }
1197 
1199  {
1200  /* coverity[tainted_data] */
1201  ret = TLSDecodeHSHelloExtensionEllipticCurvePF(ssl_state, input,
1202  ext_len,
1203  ja3_elliptic_curves_pf);
1204  if (ret < 0)
1205  goto end;
1206 
1207  input += ret;
1208 
1209  break;
1210  }
1211 
1213  {
1214  if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1215  /* Used by 0-RTT to indicate that encrypted data will
1216  be sent right after the ClientHello record. */
1217  ssl_state->flags |= SSL_AL_FLAG_EARLY_DATA;
1218  }
1219 
1220  input += ext_len;
1221 
1222  break;
1223  }
1224 
1226  {
1227  ret = TLSDecodeHSHelloExtensionSupportedVersions(ssl_state, input,
1228  ext_len);
1229  if (ret < 0)
1230  goto end;
1231 
1232  input += ret;
1233 
1234  break;
1235  }
1236 
1238  {
1239  if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1240  /* This has to be verified later on by checking if a
1241  certificate record has been sent by the server. */
1242  ssl_state->flags |= SSL_AL_FLAG_SESSION_RESUMED;
1243  }
1244 
1245  input += ext_len;
1246 
1247  break;
1248  }
1249 
1250  default:
1251  {
1252  input += ext_len;
1253  break;
1254  }
1255  }
1256 
1257  if (ja3) {
1258  if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1259  rc = Ja3BufferAddValue(&ja3_extensions, ext_type);
1260  if (rc != 0)
1261  goto error;
1262  }
1263  }
1264 
1265  processed_len += ext_len + 4;
1266  }
1267 
1268 end:
1269  if (ja3) {
1270  rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
1271  &ja3_extensions);
1272  if (rc == -1)
1273  goto error;
1274 
1275  if (ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) {
1276  rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
1277  &ja3_elliptic_curves);
1278  if (rc == -1)
1279  goto error;
1280 
1281  rc = Ja3BufferAppendBuffer(&ssl_state->curr_connp->ja3_str,
1282  &ja3_elliptic_curves_pf);
1283  if (rc == -1)
1284  goto error;
1285  }
1286  }
1287 
1288  return (input - initial_input);
1289 
1290 invalid_length:
1291  SCLogDebug("TLS handshake invalid length");
1292  SSLSetEvent(ssl_state,
1294 
1295 error:
1296  if (ja3_extensions != NULL)
1297  Ja3BufferFree(&ja3_extensions);
1298  if (ja3_elliptic_curves != NULL)
1299  Ja3BufferFree(&ja3_elliptic_curves);
1300  if (ja3_elliptic_curves_pf != NULL)
1301  Ja3BufferFree(&ja3_elliptic_curves_pf);
1302 
1303  return -1;
1304 }
1305 
1306 static int TLSDecodeHandshakeHello(SSLState *ssl_state,
1307  const uint8_t * const input,
1308  const uint32_t input_len)
1309 {
1310  int ret;
1311  uint32_t parsed = 0;
1312 
1313  ret = TLSDecodeHSHelloVersion(ssl_state, input, input_len);
1314  if (ret < 0)
1315  goto end;
1316 
1317  parsed += ret;
1318 
1319  ret = TLSDecodeHSHelloRandom(ssl_state, input + parsed, input_len - parsed);
1320  if (ret < 0)
1321  goto end;
1322 
1323  parsed += ret;
1324 
1325  /* The session id field in the server hello record was removed in
1326  TLSv1.3 draft1, but was readded in draft22. */
1327  if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) ||
1329  ((ssl_state->flags & SSL_AL_FLAG_LOG_WITHOUT_CERT) == 0))) {
1330  ret = TLSDecodeHSHelloSessionID(ssl_state, input + parsed,
1331  input_len - parsed);
1332  if (ret < 0)
1333  goto end;
1334 
1335  parsed += ret;
1336  }
1337 
1338  ret = TLSDecodeHSHelloCipherSuites(ssl_state, input + parsed,
1339  input_len - parsed);
1340  if (ret < 0)
1341  goto end;
1342 
1343  parsed += ret;
1344 
1345  /* The compression methods field in the server hello record was
1346  removed in TLSv1.3 draft1, but was readded in draft22. */
1347  if ((ssl_state->current_flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) ||
1349  ((ssl_state->flags & SSL_AL_FLAG_LOG_WITHOUT_CERT) == 0))) {
1350  ret = TLSDecodeHSHelloCompressionMethods(ssl_state, input + parsed,
1351  input_len - parsed);
1352  if (ret < 0)
1353  goto end;
1354 
1355  parsed += ret;
1356  }
1357 
1358  ret = TLSDecodeHSHelloExtensions(ssl_state, input + parsed,
1359  input_len - parsed);
1360  if (ret < 0)
1361  goto end;
1362 
1363  if (SC_ATOMIC_GET(ssl_config.enable_ja3) && ssl_state->curr_connp->ja3_hash == NULL) {
1364  ssl_state->curr_connp->ja3_hash = Ja3GenerateHash(ssl_state->curr_connp->ja3_str);
1365  }
1366 
1367 end:
1368  ssl_state->curr_connp->hs_bytes_processed = 0;
1369  return 0;
1370 }
1371 
1372 static int SSLv3ParseHandshakeType(SSLState *ssl_state, const uint8_t *input,
1373  uint32_t input_len, uint8_t direction)
1374 {
1375  void *ptmp;
1376  const uint8_t *initial_input = input;
1377  uint32_t parsed = 0;
1378  int rc;
1379 
1380  if (input_len == 0) {
1381  return 0;
1382  }
1383 
1384  switch (ssl_state->curr_connp->handshake_type) {
1385  case SSLV3_HS_CLIENT_HELLO:
1387 
1388  /* Only parse the message if it is complete */
1389  if (input_len >= ssl_state->curr_connp->message_length &&
1390  input_len >= 40) {
1391  rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1392 
1393  if (rc < 0)
1394  return rc;
1395  }
1396 
1397  break;
1398 
1399  case SSLV3_HS_SERVER_HELLO:
1401 
1402  /* Only parse the message if it is complete */
1403  if (input_len >= ssl_state->curr_connp->message_length &&
1404  input_len >= 40) {
1405  rc = TLSDecodeHandshakeHello(ssl_state, input,
1406  ssl_state->curr_connp->message_length);
1407 
1408  if (rc < 0)
1409  return rc;
1410  }
1411 
1412  break;
1413 
1416  break;
1417 
1420  break;
1421 
1422  case SSLV3_HS_CERTIFICATE:
1423  /* For now, only decode the server certificate */
1424  if (direction == 0) {
1425  SCLogDebug("Incorrect SSL Record type sent in the toserver "
1426  "direction!");
1427  break;
1428  }
1429  if (ssl_state->curr_connp->trec == NULL) {
1430  ssl_state->curr_connp->trec_len =
1431  2 * ssl_state->curr_connp->record_length +
1433  ssl_state->curr_connp->trec =
1434  SCMalloc(ssl_state->curr_connp->trec_len);
1435  }
1436  if (ssl_state->curr_connp->trec_pos + input_len >=
1437  ssl_state->curr_connp->trec_len) {
1438  ssl_state->curr_connp->trec_len =
1439  ssl_state->curr_connp->trec_len + 2 * input_len + 1;
1440  ptmp = SCRealloc(ssl_state->curr_connp->trec,
1441  ssl_state->curr_connp->trec_len);
1442 
1443  if (unlikely(ptmp == NULL)) {
1444  SCFree(ssl_state->curr_connp->trec);
1445  }
1446 
1447  ssl_state->curr_connp->trec = ptmp;
1448  }
1449  if (unlikely(ssl_state->curr_connp->trec == NULL)) {
1450  ssl_state->curr_connp->trec_len = 0;
1451  /* error, skip packet */
1452  parsed += input_len;
1453  (void)parsed; /* for scan-build */
1454  ssl_state->curr_connp->bytes_processed += input_len;
1455  return -1;
1456  }
1457 
1458  uint32_t write_len = 0;
1459  if ((ssl_state->curr_connp->bytes_processed + input_len) >
1460  ssl_state->curr_connp->record_length +
1462  if ((ssl_state->curr_connp->record_length +
1464  ssl_state->curr_connp->bytes_processed) {
1465  SSLSetEvent(ssl_state,
1467  return -1;
1468  }
1469  write_len = (ssl_state->curr_connp->record_length +
1471  ssl_state->curr_connp->bytes_processed;
1472  } else {
1473  write_len = input_len;
1474  }
1475 
1476  memcpy(ssl_state->curr_connp->trec +
1477  ssl_state->curr_connp->trec_pos, initial_input, write_len);
1478  ssl_state->curr_connp->trec_pos += write_len;
1479 
1480  rc = TlsDecodeHSCertificate(ssl_state, ssl_state->curr_connp->trec,
1481  ssl_state->curr_connp->trec_pos);
1482 
1483  if (rc > 0) {
1484  /* do not return normally if the packet was fragmented:
1485  we would return the size of the _entire_ message,
1486  while we expect only the number of bytes parsed bytes
1487  from the _current_ fragment */
1488  if (write_len < (ssl_state->curr_connp->trec_pos - rc)) {
1489  SSLSetEvent(ssl_state,
1491  return -1;
1492  }
1493 
1494  uint32_t diff = write_len -
1495  (ssl_state->curr_connp->trec_pos - rc);
1496  ssl_state->curr_connp->bytes_processed += diff;
1497 
1498  ssl_state->curr_connp->trec_pos = 0;
1499  ssl_state->curr_connp->handshake_type = 0;
1500  ssl_state->curr_connp->hs_bytes_processed = 0;
1501  ssl_state->curr_connp->message_length = 0;
1502 
1503  return diff;
1504  } else {
1505  ssl_state->curr_connp->bytes_processed += write_len;
1506  parsed += write_len;
1507  return parsed;
1508  }
1509 
1510  break;
1514  case SSLV3_HS_FINISHED:
1517  break;
1519  SCLogDebug("new session ticket");
1520  break;
1521  default:
1523  return -1;
1524  }
1525 
1526  ssl_state->flags |= ssl_state->current_flags;
1527 
1528  uint32_t write_len = 0;
1529  if ((ssl_state->curr_connp->bytes_processed + input_len) >=
1530  ssl_state->curr_connp->record_length + (SSLV3_RECORD_HDR_LEN)) {
1531  if ((ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN) <
1532  ssl_state->curr_connp->bytes_processed) {
1534  return -1;
1535  }
1536  write_len = (ssl_state->curr_connp->record_length +
1538  } else {
1539  write_len = input_len;
1540  }
1541 
1542  if ((ssl_state->curr_connp->trec_pos + write_len) >=
1543  ssl_state->curr_connp->message_length) {
1544  if (ssl_state->curr_connp->message_length <
1545  ssl_state->curr_connp->trec_pos) {
1547  return -1;
1548  }
1549  parsed += ssl_state->curr_connp->message_length -
1550  ssl_state->curr_connp->trec_pos;
1551 
1552  ssl_state->curr_connp->bytes_processed +=
1553  ssl_state->curr_connp->message_length -
1554  ssl_state->curr_connp->trec_pos;
1555 
1556  ssl_state->curr_connp->handshake_type = 0;
1557  ssl_state->curr_connp->hs_bytes_processed = 0;
1558  ssl_state->curr_connp->message_length = 0;
1559  ssl_state->curr_connp->trec_pos = 0;
1560 
1561  return parsed;
1562  } else {
1563  ssl_state->curr_connp->trec_pos += write_len;
1564  ssl_state->curr_connp->bytes_processed += write_len;
1565  parsed += write_len;
1566  return parsed;
1567  }
1568 }
1569 
1570 static int SSLv3ParseHandshakeProtocol(SSLState *ssl_state, const uint8_t *input,
1571  uint32_t input_len, uint8_t direction)
1572 {
1573  const uint8_t *initial_input = input;
1574  int retval;
1575 
1576  if (input_len == 0 || ssl_state->curr_connp->bytes_processed ==
1577  (ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN)) {
1578  return 0;
1579  }
1580 
1581  switch (ssl_state->curr_connp->hs_bytes_processed) {
1582  case 0:
1583  ssl_state->curr_connp->handshake_type = *(input++);
1584  ssl_state->curr_connp->bytes_processed++;
1585  ssl_state->curr_connp->hs_bytes_processed++;
1586  if (--input_len == 0 || ssl_state->curr_connp->bytes_processed ==
1587  (ssl_state->curr_connp->record_length +
1589  return (input - initial_input);
1590  }
1591 
1592  /* fall through */
1593  case 1:
1594  ssl_state->curr_connp->message_length = *(input++) << 16;
1595  ssl_state->curr_connp->bytes_processed++;
1596  ssl_state->curr_connp->hs_bytes_processed++;
1597  if (--input_len == 0 || ssl_state->curr_connp->bytes_processed ==
1598  (ssl_state->curr_connp->record_length +
1600  return (input - initial_input);
1601  }
1602 
1603  /* fall through */
1604  case 2:
1605  ssl_state->curr_connp->message_length |= *(input++) << 8;
1606  ssl_state->curr_connp->bytes_processed++;
1607  ssl_state->curr_connp->hs_bytes_processed++;
1608  if (--input_len == 0 || ssl_state->curr_connp->bytes_processed ==
1609  (ssl_state->curr_connp->record_length +
1611  return (input - initial_input);
1612  }
1613 
1614  /* fall through */
1615  case 3:
1616  ssl_state->curr_connp->message_length |= *(input++);
1617  ssl_state->curr_connp->bytes_processed++;
1618  ssl_state->curr_connp->hs_bytes_processed++;
1619  --input_len;
1620 
1621  /* fall through */
1622  }
1623 
1624  retval = SSLv3ParseHandshakeType(ssl_state, input, input_len, direction);
1625  if (retval < 0) {
1626  return retval;
1627  }
1628 
1629  input += retval;
1630 
1631  return (input - initial_input);
1632 }
1633 
1634 /**
1635  * \internal
1636  * \brief TLS Heartbeat parser (see RFC 6520)
1637  *
1638  * \param sslstate Pointer to the SSL state.
1639  * \param input Pointer to the received input data.
1640  * \param input_len Length in bytes of the received data.
1641  * \param direction 1 toclient, 0 toserver
1642  *
1643  * \retval The number of bytes parsed on success, 0 if nothing parsed, -1 on failure.
1644  */
1645 static int SSLv3ParseHeartbeatProtocol(SSLState *ssl_state, const uint8_t *input,
1646  uint32_t input_len, uint8_t direction)
1647 {
1648  uint8_t hb_type;
1649  uint16_t payload_len;
1650  uint16_t padding_len;
1651 
1652  /* expect at least 3 bytes: heartbeat type (1) + length (2) */
1653  if (input_len < 3) {
1654  return 0;
1655  }
1656 
1657  hb_type = *input++;
1658 
1659  if (!(ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC)) {
1660  if (!(hb_type == TLS_HB_REQUEST || hb_type == TLS_HB_RESPONSE)) {
1662  return -1;
1663  }
1664  }
1665 
1666  if ((ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) == 0) {
1667  ssl_state->flags |= SSL_AL_FLAG_HB_INFLIGHT;
1668 
1669  if (direction) {
1670  SCLogDebug("HeartBeat Record type sent in the toclient direction!");
1671  ssl_state->flags |= SSL_AL_FLAG_HB_SERVER_INIT;
1672  } else {
1673  SCLogDebug("HeartBeat Record type sent in the toserver direction!");
1674  ssl_state->flags |= SSL_AL_FLAG_HB_CLIENT_INIT;
1675  }
1676 
1677  /* if we reach this point, then we can assume that the HB request
1678  is encrypted. If so, let's set the HB record length */
1679  if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
1680  ssl_state->hb_record_len = ssl_state->curr_connp->record_length;
1681  SCLogDebug("Encrypted HeartBeat Request In-flight. Storing len %u",
1682  ssl_state->hb_record_len);
1683  return (ssl_state->curr_connp->record_length - 3);
1684  }
1685 
1686  payload_len = (*input++) << 8;
1687  payload_len |= (*input++);
1688 
1689  /* check that the requested payload length is really present in
1690  the record (CVE-2014-0160) */
1691  if ((uint32_t)(payload_len+3) > ssl_state->curr_connp->record_length) {
1692  SCLogDebug("We have a short record in HeartBeat Request");
1694  return -1;
1695  }
1696 
1697  /* check the padding length. It must be at least 16 bytes
1698  (RFC 6520, section 4) */
1699  padding_len = ssl_state->curr_connp->record_length - payload_len - 3;
1700  if (padding_len < 16) {
1701  SCLogDebug("We have a short record in HeartBeat Request");
1703  return -1;
1704  }
1705 
1706  /* we don't have the payload */
1707  if (input_len < payload_len + padding_len) {
1708  return 0;
1709  }
1710 
1711  /* OpenSSL still seems to discard multiple in-flight
1712  heartbeats although some tools send multiple at once */
1713  } else if (direction == 1 && (ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) &&
1714  (ssl_state->flags & SSL_AL_FLAG_HB_SERVER_INIT)) {
1715  SCLogDebug("Multiple in-flight server initiated HeartBeats");
1717  return -1;
1718 
1719  } else if (direction == 0 && (ssl_state->flags & SSL_AL_FLAG_HB_INFLIGHT) &&
1720  (ssl_state->flags & SSL_AL_FLAG_HB_CLIENT_INIT)) {
1721  SCLogDebug("Multiple in-flight client initiated HeartBeats");
1723  return -1;
1724 
1725  } else {
1726  /* we have a HB record in the opposite direction of the request,
1727  let's reset our flags */
1728  ssl_state->flags &= ~SSL_AL_FLAG_HB_INFLIGHT;
1729  ssl_state->flags &= ~SSL_AL_FLAG_HB_SERVER_INIT;
1730  ssl_state->flags &= ~SSL_AL_FLAG_HB_CLIENT_INIT;
1731 
1732  /* if we reach this point, then we can assume that the HB request
1733  is encrypted. If so, let's set the HB record length */
1734  if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
1735  /* check to see if the encrypted response is longer than the
1736  encrypted request */
1737  if (ssl_state->hb_record_len > 0 && ssl_state->hb_record_len <
1738  ssl_state->curr_connp->record_length) {
1739  SCLogDebug("My heart is bleeding.. OpenSSL HeartBleed response (%u)",
1740  ssl_state->hb_record_len);
1741  SSLSetEvent(ssl_state,
1743  ssl_state->hb_record_len = 0;
1744  return -1;
1745  }
1746  }
1747 
1748  /* reset the HB record length in case we have a legit HB followed
1749  by a bad one */
1750  ssl_state->hb_record_len = 0;
1751  }
1752 
1753  /* skip the HeartBeat, 3 bytes were already parsed,
1754  e.g |18 03 02| for TLS 1.2 */
1755  return (ssl_state->curr_connp->record_length - 3);
1756 }
1757 
1758 static int SSLv3ParseRecord(uint8_t direction, SSLState *ssl_state,
1759  const uint8_t *input, uint32_t input_len)
1760 {
1761  const uint8_t *initial_input = input;
1762 
1763  if (input_len == 0) {
1764  return 0;
1765  }
1766 
1767  uint8_t skip_version = 0;
1768 
1769  /* Only set SSL/TLS version here if it has not already been set in
1770  client/server hello. */
1771  if (direction == 0) {
1772  if ((ssl_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) &&
1773  (ssl_state->client_connp.version != TLS_VERSION_UNKNOWN)) {
1774  skip_version = 1;
1775  }
1776  } else {
1777  if ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) &&
1778  (ssl_state->server_connp.version != TLS_VERSION_UNKNOWN)) {
1779  skip_version = 1;
1780  }
1781  }
1782 
1783  switch (ssl_state->curr_connp->bytes_processed) {
1784  case 0:
1785  if (input_len >= 5) {
1786  ssl_state->curr_connp->content_type = input[0];
1787  if (!skip_version) {
1788  ssl_state->curr_connp->version = input[1] << 8;
1789  ssl_state->curr_connp->version |= input[2];
1790  }
1791  ssl_state->curr_connp->record_length = input[3] << 8;
1792  ssl_state->curr_connp->record_length |= input[4];
1794  return SSLV3_RECORD_HDR_LEN;
1795  } else {
1796  ssl_state->curr_connp->content_type = *(input++);
1797  if (--input_len == 0)
1798  break;
1799  }
1800 
1801  /* fall through */
1802  case 1:
1803  if (!skip_version) {
1804  ssl_state->curr_connp->version = *(input++) << 8;
1805  } else {
1806  input++;
1807  }
1808  if (--input_len == 0)
1809  break;
1810 
1811  /* fall through */
1812  case 2:
1813  if (!skip_version) {
1814  ssl_state->curr_connp->version |= *(input++);
1815  } else {
1816  input++;
1817  }
1818  if (--input_len == 0)
1819  break;
1820 
1821  /* fall through */
1822  case 3:
1823  ssl_state->curr_connp->record_length = *(input++) << 8;
1824  if (--input_len == 0)
1825  break;
1826 
1827  /* fall through */
1828  case 4:
1829  ssl_state->curr_connp->record_length |= *(input++);
1830  if (--input_len == 0)
1831  break;
1832 
1833  /* fall through */
1834  }
1835 
1836  ssl_state->curr_connp->bytes_processed += (input - initial_input);
1837 
1838  return (input - initial_input);
1839 }
1840 
1841 static int SSLv2ParseRecord(uint8_t direction, SSLState *ssl_state,
1842  const uint8_t *input, uint32_t input_len)
1843 {
1844  const uint8_t *initial_input = input;
1845 
1846  if (input_len == 0) {
1847  return 0;
1848  }
1849 
1850  if (ssl_state->curr_connp->record_lengths_length == 2) {
1851  switch (ssl_state->curr_connp->bytes_processed) {
1852  case 0:
1853  if (input_len >= ssl_state->curr_connp->record_lengths_length + 1) {
1854  ssl_state->curr_connp->record_length = (0x7f & input[0]) << 8 | input[1];
1855  ssl_state->curr_connp->content_type = input[2];
1856  ssl_state->curr_connp->version = SSL_VERSION_2;
1857  ssl_state->curr_connp->bytes_processed += 3;
1858  return 3;
1859  } else {
1860  ssl_state->curr_connp->record_length = (0x7f & *(input++)) << 8;
1861  if (--input_len == 0)
1862  break;
1863  }
1864 
1865  /* fall through */
1866  case 1:
1867  ssl_state->curr_connp->record_length |= *(input++);
1868  if (--input_len == 0)
1869  break;
1870 
1871  /* fall through */
1872  case 2:
1873  ssl_state->curr_connp->content_type = *(input++);
1874  ssl_state->curr_connp->version = SSL_VERSION_2;
1875  if (--input_len == 0)
1876  break;
1877 
1878  /* fall through */
1879  }
1880 
1881  } else {
1882  switch (ssl_state->curr_connp->bytes_processed) {
1883  case 0:
1884  if (input_len >= ssl_state->curr_connp->record_lengths_length + 1) {
1885  ssl_state->curr_connp->record_length = (0x3f & input[0]) << 8 | input[1];
1886  ssl_state->curr_connp->content_type = input[3];
1887  ssl_state->curr_connp->version = SSL_VERSION_2;
1888  ssl_state->curr_connp->bytes_processed += 4;
1889  return 4;
1890  } else {
1891  ssl_state->curr_connp->record_length = (0x3f & *(input++)) << 8;
1892  if (--input_len == 0)
1893  break;
1894  }
1895 
1896  /* fall through */
1897  case 1:
1898  ssl_state->curr_connp->record_length |= *(input++);
1899  if (--input_len == 0)
1900  break;
1901 
1902  /* fall through */
1903  case 2:
1904  /* padding */
1905  input++;
1906  if (--input_len == 0)
1907  break;
1908 
1909  /* fall through */
1910  case 3:
1911  ssl_state->curr_connp->content_type = *(input++);
1912  ssl_state->curr_connp->version = SSL_VERSION_2;
1913  if (--input_len == 0)
1914  break;
1915 
1916  /* fall through */
1917  }
1918  }
1919 
1920  ssl_state->curr_connp->bytes_processed += (input - initial_input);
1921 
1922  return (input - initial_input);
1923 }
1924 
1925 static int SSLv2Decode(uint8_t direction, SSLState *ssl_state,
1926  AppLayerParserState *pstate, const uint8_t *input,
1927  uint32_t input_len)
1928 {
1929  int retval = 0;
1930  const uint8_t *initial_input = input;
1931 
1932  if (ssl_state->curr_connp->bytes_processed == 0) {
1933  if (input[0] & 0x80) {
1934  ssl_state->curr_connp->record_lengths_length = 2;
1935  } else {
1936  ssl_state->curr_connp->record_lengths_length = 3;
1937  }
1938  }
1939 
1940  /* the +1 is because we read one extra byte inside SSLv2ParseRecord
1941  to read the msg_type */
1942  if (ssl_state->curr_connp->bytes_processed <
1943  (ssl_state->curr_connp->record_lengths_length + 1)) {
1944  retval = SSLv2ParseRecord(direction, ssl_state, input, input_len);
1945  if (retval == -1) {
1947  return -1;
1948  } else {
1949  input += retval;
1950  input_len -= retval;
1951  }
1952  }
1953 
1954  if (input_len == 0) {
1955  return (input - initial_input);
1956  }
1957 
1958  /* record_length should never be zero */
1959  if (ssl_state->curr_connp->record_length == 0) {
1960  SCLogDebug("SSLv2 record length is zero");
1962  return -1;
1963  }
1964 
1965  /* record_lenghts_length should never be zero */
1966  if (ssl_state->curr_connp->record_lengths_length == 0) {
1967  SCLogDebug("SSLv2 record lengths length is zero");
1969  return -1;
1970  }
1971 
1972  switch (ssl_state->curr_connp->content_type) {
1973  case SSLV2_MT_ERROR:
1974  SCLogDebug("SSLV2_MT_ERROR msg_type received. Error encountered "
1975  "in establishing the sslv2 session, may be version");
1977 
1978  break;
1979 
1980  case SSLV2_MT_CLIENT_HELLO:
1983 
1984  if (ssl_state->curr_connp->record_lengths_length == 3) {
1985  switch (ssl_state->curr_connp->bytes_processed) {
1986  case 4:
1987  if (input_len >= 6) {
1988  ssl_state->curr_connp->session_id_length = input[4] << 8;
1989  ssl_state->curr_connp->session_id_length |= input[5];
1990  input += 6;
1991  input_len -= 6;
1992  ssl_state->curr_connp->bytes_processed += 6;
1993  if (ssl_state->curr_connp->session_id_length == 0) {
1995  }
1996 
1997  break;
1998  } else {
1999  input++;
2000  ssl_state->curr_connp->bytes_processed++;
2001  if (--input_len == 0)
2002  break;
2003  }
2004 
2005  /* fall through */
2006  case 5:
2007  input++;
2008  ssl_state->curr_connp->bytes_processed++;
2009  if (--input_len == 0)
2010  break;
2011 
2012  /* fall through */
2013  case 6:
2014  input++;
2015  ssl_state->curr_connp->bytes_processed++;
2016  if (--input_len == 0)
2017  break;
2018 
2019  /* fall through */
2020  case 7:
2021  input++;
2022  ssl_state->curr_connp->bytes_processed++;
2023  if (--input_len == 0)
2024  break;
2025 
2026  /* fall through */
2027  case 8:
2028  ssl_state->curr_connp->session_id_length = *(input++) << 8;
2029  ssl_state->curr_connp->bytes_processed++;
2030  if (--input_len == 0)
2031  break;
2032 
2033  /* fall through */
2034  case 9:
2035  ssl_state->curr_connp->session_id_length |= *(input++);
2036  ssl_state->curr_connp->bytes_processed++;
2037  if (--input_len == 0)
2038  break;
2039 
2040  /* fall through */
2041  }
2042 
2043  } else {
2044  switch (ssl_state->curr_connp->bytes_processed) {
2045  case 3:
2046  if (input_len >= 6) {
2047  ssl_state->curr_connp->session_id_length = input[4] << 8;
2048  ssl_state->curr_connp->session_id_length |= input[5];
2049  input += 6;
2050  input_len -= 6;
2051  ssl_state->curr_connp->bytes_processed += 6;
2052  if (ssl_state->curr_connp->session_id_length == 0) {
2054  }
2055 
2056  break;
2057  } else {
2058  input++;
2059  ssl_state->curr_connp->bytes_processed++;
2060  if (--input_len == 0)
2061  break;
2062  }
2063 
2064  /* fall through */
2065  case 4:
2066  input++;
2067  ssl_state->curr_connp->bytes_processed++;
2068  if (--input_len == 0)
2069  break;
2070 
2071  /* fall through */
2072  case 5:
2073  input++;
2074  ssl_state->curr_connp->bytes_processed++;
2075  if (--input_len == 0)
2076  break;
2077 
2078  /* fall through */
2079  case 6:
2080  input++;
2081  ssl_state->curr_connp->bytes_processed++;
2082  if (--input_len == 0)
2083  break;
2084 
2085  /* fall through */
2086  case 7:
2087  ssl_state->curr_connp->session_id_length = *(input++) << 8;
2088  ssl_state->curr_connp->bytes_processed++;
2089  if (--input_len == 0)
2090  break;
2091 
2092  /* fall through */
2093  case 8:
2094  ssl_state->curr_connp->session_id_length |= *(input++);
2095  ssl_state->curr_connp->bytes_processed++;
2096  if (--input_len == 0)
2097  break;
2098 
2099  /* fall through */
2100  }
2101  }
2102 
2103  break;
2104 
2106  if (!(ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS)) {
2107  SCLogDebug("Client hello is not seen before master key "
2108  "message!");
2109  }
2111 
2112  break;
2113 
2115  if (direction == 1) {
2116  SCLogDebug("Incorrect SSL Record type sent in the toclient "
2117  "direction!");
2118  } else {
2120  }
2121 
2122  /* fall through */
2125  if (direction == 0 &&
2126  !(ssl_state->curr_connp->content_type &
2128  SCLogDebug("Incorrect SSL Record type sent in the toserver "
2129  "direction!");
2130  }
2131 
2132  /* fall through */
2135  /* both client hello and server hello must be seen */
2136  if ((ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) &&
2137  (ssl_state->flags & SSL_AL_FLAG_SSL_SERVER_HS)) {
2138 
2139  if (direction == 0) {
2140  if (ssl_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) {
2142  SCLogDebug("SSLv2 client side has started the encryption");
2143  } else if (ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY) {
2145  SCLogDebug("SSLv2 client side has started the encryption");
2146  }
2147  } else {
2149  SCLogDebug("SSLv2 Server side has started the encryption");
2150  }
2151 
2152  if ((ssl_state->flags & SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED) &&
2154  {
2155  if (ssl_config.encrypt_mode != SSL_CNF_ENC_HANDLE_FULL) {
2158  }
2159 
2160  if (ssl_config.encrypt_mode == SSL_CNF_ENC_HANDLE_BYPASS) {
2163  }
2164  SCLogDebug("SSLv2 No reassembly & inspection has been set");
2165  }
2166  }
2167 
2168  break;
2169 
2170  case SSLV2_MT_SERVER_HELLO:
2173 
2174  break;
2175  }
2176 
2177  ssl_state->flags |= ssl_state->current_flags;
2178 
2179  if (input_len + ssl_state->curr_connp->bytes_processed >=
2180  (ssl_state->curr_connp->record_length +
2181  ssl_state->curr_connp->record_lengths_length)) {
2182 
2183  /* looks like we have another record after this */
2184  uint32_t diff = ssl_state->curr_connp->record_length +
2185  ssl_state->curr_connp->record_lengths_length + -
2186  ssl_state->curr_connp->bytes_processed;
2187  input += diff;
2188  SSLParserReset(ssl_state);
2189  return (input - initial_input);
2190 
2191  /* we still don't have the entire record for the one we are
2192  currently parsing */
2193  } else {
2194  input += input_len;
2195  ssl_state->curr_connp->bytes_processed += input_len;
2196  return (input - initial_input);
2197  }
2198 }
2199 
2200 static int SSLv3Decode(uint8_t direction, SSLState *ssl_state,
2201  AppLayerParserState *pstate, const uint8_t *input,
2202  uint32_t input_len)
2203 {
2204  int retval = 0;
2205  uint32_t parsed = 0;
2206 
2207  if (ssl_state->curr_connp->bytes_processed < SSLV3_RECORD_HDR_LEN) {
2208  retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
2209  if (retval < 0) {
2211  return -1;
2212  } else {
2213  parsed += retval;
2214  input_len -= retval;
2215  }
2216  }
2217 
2218  if (input_len == 0) {
2219  return parsed;
2220  }
2221 
2222  /* record_length should never be zero */
2223  if (ssl_state->curr_connp->record_length == 0) {
2224  SCLogDebug("SSLv3 Record length is 0");
2226  return -1;
2227  }
2228 
2229  switch (ssl_state->curr_connp->content_type) {
2230 
2231  /* we don't need any data from these types */
2233  ssl_state->flags |= SSL_AL_FLAG_CHANGE_CIPHER_SPEC;
2234 
2235  if (direction) {
2237  } else {
2239  }
2240 
2241  break;
2242 
2243  case SSLV3_ALERT_PROTOCOL:
2244  break;
2245 
2247  /* In TLSv1.3 early data (0-RTT) could be sent before the
2248  handshake is complete (rfc8446, section 2.3). We should
2249  therefore not mark the handshake as done before we have
2250  seen the ServerHello record. */
2251  if ((ssl_state->flags & SSL_AL_FLAG_EARLY_DATA) &&
2252  ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0))
2253  break;
2254 
2255  /* if we see (encrypted) aplication data, then this means the
2256  handshake must be done */
2257  ssl_state->flags |= SSL_AL_FLAG_HANDSHAKE_DONE;
2258 
2259  if (ssl_config.encrypt_mode != SSL_CNF_ENC_HANDLE_FULL) {
2260  SCLogDebug("setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
2263  }
2264 
2265  /* Encrypted data, reassembly not asked, bypass asked, let's sacrifice
2266  * heartbeat lke inspection to be able to be able to bypass the flow */
2267  if (ssl_config.encrypt_mode == SSL_CNF_ENC_HANDLE_BYPASS) {
2268  SCLogDebug("setting APP_LAYER_PARSER_NO_REASSEMBLY");
2275  }
2276 
2277  break;
2278 
2280  if (ssl_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) {
2281  /* In TLSv1.3, ChangeCipherSpec is only used for middlebox
2282  compability (rfc8446, appendix D.4). */
2283  if ((ssl_state->client_connp.version > TLS_VERSION_12) &&
2284  ((ssl_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0)) {
2285  /* do nothing */
2286  } else {
2287  break;
2288  }
2289  }
2290 
2291  if (ssl_state->curr_connp->record_length < 4) {
2292  SSLParserReset(ssl_state);
2294  return -1;
2295  }
2296 
2297  retval = SSLv3ParseHandshakeProtocol(ssl_state, input + parsed,
2298  input_len, direction);
2299  if (retval < 0) {
2300  SSLSetEvent(ssl_state,
2302  SSLSetEvent(ssl_state,
2304  return -1;
2305  } else {
2306  if ((uint32_t)retval > input_len) {
2307  SCLogDebug("Error parsing SSLv3.x. Reseting parser "
2308  "state. Let's get outta here");
2309  SSLParserReset(ssl_state);
2310  SSLSetEvent(ssl_state,
2312  return -1;
2313  }
2314 
2315  parsed += retval;
2316  input_len -= retval;
2317  (void)input_len; /* for scan-build */
2318 
2319  if (ssl_state->curr_connp->bytes_processed ==
2320  ssl_state->curr_connp->record_length +
2322  SSLParserReset(ssl_state);
2323  }
2324 
2325  SCLogDebug("trigger RAW! (post HS)");
2327  direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
2328  return parsed;
2329  }
2330 
2331  break;
2332 
2334  retval = SSLv3ParseHeartbeatProtocol(ssl_state, input + parsed,
2335  input_len, direction);
2336  if (retval < 0)
2337  return -1;
2338 
2339  break;
2340 
2341  default:
2342  /* \todo fix the event from invalid rule to unknown rule */
2345  return -1;
2346  }
2347 
2348  if (input_len + ssl_state->curr_connp->bytes_processed >=
2350  if ((ssl_state->curr_connp->record_length + SSLV3_RECORD_HDR_LEN) <
2351  ssl_state->curr_connp->bytes_processed) {
2352  /* defensive checks. Something is wrong. */
2354  return -1;
2355  }
2356 
2357  SCLogDebug("record complete, trigger RAW");
2359  direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
2360 
2361  /* looks like we have another record */
2362  uint32_t diff = ssl_state->curr_connp->record_length +
2364  parsed += diff;
2365  SSLParserReset(ssl_state);
2366  return parsed;
2367 
2368  /* we still don't have the entire record for the one we are
2369  currently parsing */
2370  } else {
2371  parsed += input_len;
2372  ssl_state->curr_connp->bytes_processed += input_len;
2373  return parsed;
2374  }
2375 
2376 }
2377 
2378 /**
2379  * \internal
2380  * \brief SSLv2, SSLv23, SSLv3, TLSv1.1, TLSv1.2, TLSv1.3 parser.
2381  *
2382  * On parsing error, this should be the only function that should reset
2383  * the parser state, to avoid multiple functions in the chain reseting
2384  * the parser state.
2385  *
2386  * \param direction 0 for toserver, 1 for toclient.
2387  * \param alstate Pointer to the state.
2388  * \param pstate Application layer parser state for this session.
2389  * \param input Pointer the received input data.
2390  * \param input_len Length in bytes of the received data.
2391  * \param output Pointer to the list of parsed output elements.
2392  *
2393  * \todo On reaching an inconsistent state, check if the input has
2394  * another new record, instead of just returning after the reset
2395  *
2396  * \retval >=0 On success.
2397  */
2398 static int SSLDecode(Flow *f, uint8_t direction, void *alstate, AppLayerParserState *pstate,
2399  const uint8_t *input, uint32_t ilen)
2400 {
2401  SSLState *ssl_state = (SSLState *)alstate;
2402  int retval = 0;
2403  uint32_t counter = 0;
2404 
2405  int32_t input_len = (int32_t)ilen;
2406 
2407  ssl_state->f = f;
2408 
2409  if (input == NULL &&
2411  /* flag session as finished if APP_LAYER_PARSER_EOF is set */
2412  ssl_state->flags |= SSL_AL_FLAG_STATE_FINISHED;
2413  SCReturnInt(1);
2414  } else if (input == NULL || input_len == 0) {
2415  SCReturnInt(-1);
2416  }
2417 
2418  if (direction == 0)
2419  ssl_state->curr_connp = &ssl_state->client_connp;
2420  else
2421  ssl_state->curr_connp = &ssl_state->server_connp;
2422 
2423  /* If entering on a new record, reset the current flags. */
2424  if (ssl_state->curr_connp->bytes_processed == 0) {
2425  ssl_state->current_flags = 0;
2426  }
2427 
2428  /* if we have more than one record */
2429  uint32_t max_records = MAX((input_len / SSL_RECORD_MINIMUM_LENGTH),1);
2430  while (input_len > 0) {
2431  if (counter > max_records) {
2432  SCLogDebug("Looks like we have looped quite a bit. Reset state "
2433  "and get out of here");
2434  SSLParserReset(ssl_state);
2435  SSLSetEvent(ssl_state,
2437  return -1;
2438  }
2439 
2440  /* ssl_state->bytes_processed is zero for a fresh record or
2441  positive to indicate a record currently being parsed */
2442  switch (ssl_state->curr_connp->bytes_processed) {
2443  /* fresh record */
2444  case 0:
2445  /* only SSLv2, has one of the top 2 bits set */
2446  if ((input[0] & 0x80) || (input[0] & 0x40)) {
2447  SCLogDebug("SSLv2 detected");
2448  ssl_state->curr_connp->version = SSL_VERSION_2;
2449  retval = SSLv2Decode(direction, ssl_state, pstate, input,
2450  input_len);
2451  if (retval < 0) {
2452  SCLogDebug("Error parsing SSLv2.x. Reseting parser "
2453  "state. Let's get outta here");
2454  SSLParserReset(ssl_state);
2455  SSLSetEvent(ssl_state,
2457  return -1;
2458  } else {
2459  input_len -= retval;
2460  input += retval;
2461  }
2462  } else {
2463  SCLogDebug("SSLv3.x detected");
2464  retval = SSLv3Decode(direction, ssl_state, pstate, input,
2465  input_len);
2466  if (retval < 0) {
2467  SCLogDebug("Error parsing SSLv3.x. Reseting parser "
2468  "state. Let's get outta here");
2469  SSLParserReset(ssl_state);
2470  SSLSetEvent(ssl_state,
2472  return -1;
2473  } else {
2474  input_len -= retval;
2475  input += retval;
2477  && ssl_state->curr_connp->record_length == 0) {
2478  /* empty record */
2479  SSLParserReset(ssl_state);
2480  }
2481  }
2482  }
2483 
2484  break;
2485 
2486  default:
2487  /* we would have established by now if we are dealing with
2488  * SSLv2 or above */
2489  if (ssl_state->curr_connp->version == SSL_VERSION_2) {
2490  SCLogDebug("Continuing parsing SSLv2 record from where we "
2491  "previously left off");
2492  retval = SSLv2Decode(direction, ssl_state, pstate, input,
2493  input_len);
2494  if (retval < 0) {
2495  SCLogDebug("Error parsing SSLv2.x. Reseting parser "
2496  "state. Let's get outta here");
2497  SSLParserReset(ssl_state);
2498  return 0;
2499  } else {
2500  input_len -= retval;
2501  input += retval;
2502  }
2503  } else {
2504  SCLogDebug("Continuing parsing SSLv3.x record from where we "
2505  "previously left off");
2506  retval = SSLv3Decode(direction, ssl_state, pstate, input,
2507  input_len);
2508  if (retval < 0) {
2509  SCLogDebug("Error parsing SSLv3.x. Reseting parser "
2510  "state. Let's get outta here");
2511  SSLParserReset(ssl_state);
2512  return 0;
2513  } else {
2514  if (retval > input_len) {
2515  SCLogDebug("Error parsing SSLv3.x. Reseting parser "
2516  "state. Let's get outta here");
2517  SSLParserReset(ssl_state);
2518  }
2519  input_len -= retval;
2520  input += retval;
2522  && ssl_state->curr_connp->record_length == 0) {
2523  /* empty record */
2524  SSLParserReset(ssl_state);
2525  }
2526  }
2527  }
2528 
2529  break;
2530  } /* switch (ssl_state->curr_connp->bytes_processed) */
2531 
2532  counter++;
2533  } /* while (input_len) */
2534 
2535  /* mark handshake as done if we have subject and issuer */
2536  if (ssl_state->server_connp.cert0_subject &&
2537  ssl_state->server_connp.cert0_issuerdn)
2538  ssl_state->flags |= SSL_AL_FLAG_HANDSHAKE_DONE;
2539 
2540  /* flag session as finished if APP_LAYER_PARSER_EOF is set */
2542  ssl_state->flags |= SSL_AL_FLAG_STATE_FINISHED;
2543 
2544  return 1;
2545 }
2546 
2547 static int SSLParseClientRecord(Flow *f, void *alstate, AppLayerParserState *pstate,
2548  const uint8_t *input, uint32_t input_len,
2549  void *local_data, const uint8_t flags)
2550 {
2551  return SSLDecode(f, 0 /* toserver */, alstate, pstate, input, input_len);
2552 }
2553 
2554 static int SSLParseServerRecord(Flow *f, void *alstate, AppLayerParserState *pstate,
2555  const uint8_t *input, uint32_t input_len,
2556  void *local_data, const uint8_t flags)
2557 {
2558  return SSLDecode(f, 1 /* toclient */, alstate, pstate, input, input_len);
2559 }
2560 
2561 /**
2562  * \internal
2563  * \brief Function to allocate the SSL state memory.
2564  */
2565 static void *SSLStateAlloc(void)
2566 {
2567  SSLState *ssl_state = SCMalloc(sizeof(SSLState));
2568  if (unlikely(ssl_state == NULL))
2569  return NULL;
2570  memset(ssl_state, 0, sizeof(SSLState));
2571  ssl_state->client_connp.cert_log_flag = 0;
2572  ssl_state->server_connp.cert_log_flag = 0;
2573  TAILQ_INIT(&ssl_state->server_connp.certs);
2574 
2575  return (void *)ssl_state;
2576 }
2577 
2578 /**
2579  * \internal
2580  * \brief Function to free the SSL state memory.
2581  */
2582 static void SSLStateFree(void *p)
2583 {
2584  SSLState *ssl_state = (SSLState *)p;
2585  SSLCertsChain *item;
2586 
2587  if (ssl_state->client_connp.trec)
2588  SCFree(ssl_state->client_connp.trec);
2589  if (ssl_state->client_connp.cert0_subject)
2590  SCFree(ssl_state->client_connp.cert0_subject);
2591  if (ssl_state->client_connp.cert0_issuerdn)
2592  SCFree(ssl_state->client_connp.cert0_issuerdn);
2593  if (ssl_state->server_connp.cert0_serial)
2594  SCFree(ssl_state->server_connp.cert0_serial);
2595  if (ssl_state->client_connp.cert0_fingerprint)
2597  if (ssl_state->client_connp.sni)
2598  SCFree(ssl_state->client_connp.sni);
2599  if (ssl_state->client_connp.session_id)
2600  SCFree(ssl_state->client_connp.session_id);
2601 
2602  if (ssl_state->server_connp.trec)
2603  SCFree(ssl_state->server_connp.trec);
2604  if (ssl_state->server_connp.cert0_subject)
2605  SCFree(ssl_state->server_connp.cert0_subject);
2606  if (ssl_state->server_connp.cert0_issuerdn)
2607  SCFree(ssl_state->server_connp.cert0_issuerdn);
2608  if (ssl_state->server_connp.cert0_fingerprint)
2610  if (ssl_state->server_connp.sni)
2611  SCFree(ssl_state->server_connp.sni);
2612  if (ssl_state->server_connp.session_id)
2613  SCFree(ssl_state->server_connp.session_id);
2614 
2615  if (ssl_state->client_connp.ja3_str)
2616  Ja3BufferFree(&ssl_state->client_connp.ja3_str);
2617  if (ssl_state->client_connp.ja3_hash)
2618  SCFree(ssl_state->client_connp.ja3_hash);
2619  if (ssl_state->server_connp.ja3_str)
2620  Ja3BufferFree(&ssl_state->server_connp.ja3_str);
2621  if (ssl_state->server_connp.ja3_hash)
2622  SCFree(ssl_state->server_connp.ja3_hash);
2623 
2625 
2626  if (ssl_state->de_state != NULL) {
2627  DetectEngineStateFree(ssl_state->de_state);
2628  }
2629 
2630  /* Free certificate chain */
2631  while ((item = TAILQ_FIRST(&ssl_state->server_connp.certs))) {
2632  TAILQ_REMOVE(&ssl_state->server_connp.certs, item, next);
2633  SCFree(item);
2634  }
2635  TAILQ_INIT(&ssl_state->server_connp.certs);
2636 
2637  SCFree(ssl_state);
2638 
2639  return;
2640 }
2641 
2642 static void SSLStateTransactionFree(void *state, uint64_t tx_id)
2643 {
2644  /* do nothing */
2645 }
2646 
2647 static AppProto SSLProbingParser(Flow *f, uint8_t direction,
2648  const uint8_t *input, uint32_t ilen, uint8_t *rdir)
2649 {
2650  /* probably a rst/fin sending an eof */
2651  if (ilen < 3)
2652  return ALPROTO_UNKNOWN;
2653 
2654  /* for now just the 3 byte header ones */
2655  /* \todo Detect the 2 byte ones */
2656  if ((input[0] & 0x80) && (input[2] == 0x01)) {
2657  return ALPROTO_TLS;
2658  }
2659 
2660  return ALPROTO_FAILED;
2661 }
2662 
2663 static int SSLStateGetEventInfo(const char *event_name,
2664  int *event_id, AppLayerEventType *event_type)
2665 {
2666  *event_id = SCMapEnumNameToValue(event_name, tls_decoder_event_table);
2667  if (*event_id == -1) {
2668  SCLogError(SC_ERR_INVALID_ENUM_MAP, "event \"%s\" not present in "
2669  "ssl's enum map table.", event_name);
2670  /* yes this is fatal */
2671  return -1;
2672  }
2673 
2674  *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
2675 
2676  return 0;
2677 }
2678 
2679 static int SSLStateGetEventInfoById(int event_id, const char **event_name,
2681 {
2682  *event_name = SCMapEnumValueToName(event_id, tls_decoder_event_table);
2683  if (*event_name == NULL) {
2684  SCLogError(SC_ERR_INVALID_ENUM_MAP, "event \"%d\" not present in "
2685  "ssl's enum map table.", event_id);
2686  /* yes this is fatal */
2687  return -1;
2688  }
2689 
2690  *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
2691 
2692  return 0;
2693 }
2694 
2695 static int SSLRegisterPatternsForProtocolDetection(void)
2696 {
2698  "|01 00 02|", 5, 2, STREAM_TOSERVER) < 0)
2699  {
2700  return -1;
2701  }
2702 
2703  /** SSLv3 */
2705  "|01 03 00|", 3, 0, STREAM_TOSERVER) < 0)
2706  {
2707  return -1;
2708  }
2710  "|16 03 00|", 3, 0, STREAM_TOSERVER) < 0)
2711  {
2712  return -1;
2713  }
2714 
2715  /** TLSv1 */
2717  "|01 03 01|", 3, 0, STREAM_TOSERVER) < 0)
2718  {
2719  return -1;
2720  }
2722  "|16 03 01|", 3, 0, STREAM_TOSERVER) < 0)
2723  {
2724  return -1;
2725  }
2726 
2727  /** TLSv1.1 */
2729  "|01 03 02|", 3, 0, STREAM_TOSERVER) < 0)
2730  {
2731  return -1;
2732  }
2734  "|16 03 02|", 3, 0, STREAM_TOSERVER) < 0)
2735  {
2736  return -1;
2737  }
2738 
2739  /** TLSv1.2 */
2741  "|01 03 03|", 3, 0, STREAM_TOSERVER) < 0)
2742  {
2743  return -1;
2744  }
2746  "|16 03 03|", 3, 0, STREAM_TOSERVER) < 0)
2747  {
2748  return -1;
2749  }
2750 
2751  /***** toclient direction *****/
2752 
2754  "|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
2755  {
2756  return -1;
2757  }
2759  "|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
2760  {
2761  return -1;
2762  }
2764  "|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
2765  {
2766  return -1;
2767  }
2768 
2769  /** TLSv1 */
2771  "|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
2772  {
2773  return -1;
2774  }
2776  "|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
2777  {
2778  return -1;
2779  }
2781  "|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
2782  {
2783  return -1;
2784  }
2785 
2786  /** TLSv1.1 */
2788  "|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
2789  {
2790  return -1;
2791  }
2793  "|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
2794  {
2795  return -1;
2796  }
2798  "|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
2799  {
2800  return -1;
2801  }
2802 
2803  /** TLSv1.2 */
2805  "|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
2806  {
2807  return -1;
2808  }
2810  "|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
2811  {
2812  return -1;
2813  }
2815  "|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
2816  {
2817  return -1;
2818  }
2819 
2820  /* Subsection - SSLv2 style record by client, but informing the server
2821  * the max version it supports.
2822  * Updated by Anoop Saldanha. Disabled it for now. We'll get back to
2823  * it after some tests */
2824 #if 0
2826  "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
2827  {
2828  return -1;
2829  }
2831  "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
2832  {
2833  return -1;
2834  }
2835 #endif
2836 
2837  return 0;
2838 }
2839 
2840 /**
2841  * \brief Function to register the SSL protocol parser and other functions
2842  */
2844 {
2845  const char *proto_name = "tls";
2846 
2847  SC_ATOMIC_INIT(ssl_config.enable_ja3);
2848 
2849  /** SSLv2 and SSLv23*/
2850  if (AppLayerProtoDetectConfProtoDetectionEnabled("tcp", proto_name)) {
2852 
2853  if (SSLRegisterPatternsForProtocolDetection() < 0)
2854  return;
2855 
2856  if (RunmodeIsUnittests()) {
2857  AppLayerProtoDetectPPRegister(IPPROTO_TCP,
2858  "443",
2859  ALPROTO_TLS,
2860  0, 3,
2862  SSLProbingParser, NULL);
2863  } else {
2864  if (AppLayerProtoDetectPPParseConfPorts("tcp", IPPROTO_TCP,
2865  proto_name, ALPROTO_TLS,
2866  0, 3,
2867  SSLProbingParser, NULL) == 0) {
2868  SCLogConfig("no TLS config found, "
2869  "enabling TLS detection on port 443.");
2870  AppLayerProtoDetectPPRegister(IPPROTO_TCP,
2871  "443",
2872  ALPROTO_TLS,
2873  0, 3,
2875  SSLProbingParser, NULL);
2876  }
2877  }
2878  } else {
2879  SCLogConfig("Protocol detection and parser disabled for %s protocol",
2880  proto_name);
2881  return;
2882  }
2883 
2884  if (AppLayerParserConfParserEnabled("tcp", proto_name)) {
2886  SSLParseClientRecord);
2887 
2889  SSLParseServerRecord);
2890 
2891  AppLayerParserRegisterGetEventInfo(IPPROTO_TCP, ALPROTO_TLS, SSLStateGetEventInfo);
2892  AppLayerParserRegisterGetEventInfoById(IPPROTO_TCP, ALPROTO_TLS, SSLStateGetEventInfoById);
2893 
2894  AppLayerParserRegisterStateFuncs(IPPROTO_TCP, ALPROTO_TLS, SSLStateAlloc, SSLStateFree);
2895 
2897 
2898  AppLayerParserRegisterTxFreeFunc(IPPROTO_TCP, ALPROTO_TLS, SSLStateTransactionFree);
2899 
2900  AppLayerParserRegisterGetEventsFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetEvents);
2901 
2903  SSLGetTxDetectState, SSLSetTxDetectState);
2904 
2905  AppLayerParserRegisterGetTx(IPPROTO_TCP, ALPROTO_TLS, SSLGetTx);
2906 
2907  AppLayerParserRegisterGetTxCnt(IPPROTO_TCP, ALPROTO_TLS, SSLGetTxCnt);
2908 
2909  AppLayerParserRegisterGetStateProgressFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetAlstateProgress);
2910 
2911  AppLayerParserRegisterLoggerFuncs(IPPROTO_TCP, ALPROTO_TLS, SSLGetTxLogged, SSLSetTxLogged);
2913  SSLGetTxDetectFlags, SSLSetTxDetectFlags);
2914 
2916  SSLGetAlstateProgressCompletionStatus);
2917 
2918  ConfNode *enc_handle = ConfGetNode("app-layer.protocols.tls.encryption-handling");
2919  if (enc_handle != NULL && enc_handle->val != NULL) {
2920  SCLogDebug("have app-layer.protocols.tls.encryption-handling = %s", enc_handle->val);
2921  if (strcmp(enc_handle->val, "full") == 0) {
2923  } else if (strcmp(enc_handle->val, "bypass") == 0) {
2925  } else if (strcmp(enc_handle->val, "default") == 0) {
2927  } else {
2929  }
2930  } else {
2931  /* Get the value of no reassembly option from the config file */
2932  if (ConfGetNode("app-layer.protocols.tls.no-reassemble") == NULL) {
2933  int value = 0;
2934  if (ConfGetBool("tls.no-reassemble", &value) == 1 && value == 1)
2936  } else {
2937  int value = 0;
2938  if (ConfGetBool("app-layer.protocols.tls.no-reassemble", &value) == 1 && value == 1)
2940  }
2941  }
2942  SCLogDebug("ssl_config.encrypt_mode %u", ssl_config.encrypt_mode);
2943 
2944  /* Check if we should generate JA3 fingerprints */
2945  int enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
2946  const char *strval = NULL;
2947  if (ConfGetValue("app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
2948  enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
2949  } else if (strcmp(strval, "auto") == 0) {
2950  enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
2951  } else if (ConfValIsFalse(strval)) {
2952  enable_ja3 = 0;
2953  ssl_config.disable_ja3 = true;
2954  } else if (ConfValIsTrue(strval)) {
2955  enable_ja3 = true;
2956  }
2957  SC_ATOMIC_SET(ssl_config.enable_ja3, enable_ja3);
2958 
2959 #ifndef HAVE_NSS
2960  if (SC_ATOMIC_GET(ssl_config.enable_ja3)) {
2962  "no MD5 calculation support built in (LibNSS), disabling JA3");
2963  SC_ATOMIC_SET(ssl_config.enable_ja3, 0);
2964  }
2965 #else
2966  if (RunmodeIsUnittests()) {
2967  SC_ATOMIC_SET(ssl_config.enable_ja3, 1);
2968  }
2969 #endif
2970 
2971  } else {
2972  SCLogConfig("Parsed disabled for %s protocol. Protocol detection"
2973  "still on.", proto_name);
2974  }
2975 
2976 #ifdef UNITTESTS
2978 #endif
2979  return;
2980 }
2981 
2982 /**
2983  * \brief if not explicitly disabled in config, enable ja3 support
2984  *
2985  * Implemented using atomic to allow rule reloads to do this at
2986  * runtime.
2987  */
2988 void SSLEnableJA3(void)
2989 {
2990 #ifdef HAVE_NSS
2991  if (ssl_config.disable_ja3) {
2992  return;
2993  }
2994  if (SC_ATOMIC_GET(ssl_config.enable_ja3)) {
2995  return;
2996  }
2997  SC_ATOMIC_SET(ssl_config.enable_ja3, 1);
2998 #endif
2999 }
3000 
3002 {
3003 #ifdef HAVE_NSS
3004  if (SC_ATOMIC_GET(ssl_config.enable_ja3)) {
3005  return true;
3006  }
3007 #endif
3008  return false;
3009 }
3010 
3011 /***************************************Unittests******************************/
3012 
3013 #ifdef UNITTESTS
3014 
3015 /**
3016  *\test Send a get request in one chunk.
3017  */
3018 static int SSLParserTest01(void)
3019 {
3020  Flow f;
3021  uint8_t tlsbuf[] = { 0x16, 0x03, 0x01 };
3022  uint32_t tlslen = sizeof(tlsbuf);
3023  TcpSession ssn;
3025 
3026  memset(&f, 0, sizeof(f));
3027  memset(&ssn, 0, sizeof(ssn));
3028 
3029  FLOW_INITIALIZE(&f);
3030  f.protoctx = (void *)&ssn;
3031  f.proto = IPPROTO_TCP;
3032  f.alproto = ALPROTO_TLS;
3033 
3035 
3036  FLOWLOCK_WRLOCK(&f);
3037  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3038  STREAM_TOSERVER | STREAM_EOF, tlsbuf, tlslen);
3039  FLOWLOCK_UNLOCK(&f);
3040  FAIL_IF(r != 0);
3041 
3042  SSLState *ssl_state = f.alstate;
3043  FAIL_IF_NULL(ssl_state);
3044 
3045  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3046 
3047  FAIL_IF(ssl_state->client_connp.version != TLS_VERSION_10);
3048 
3049  AppLayerParserThreadCtxFree(alp_tctx);
3051  FLOW_DESTROY(&f);
3052 
3053  PASS;
3054 }
3055 
3056 /** \test Send a get request in two chunks. */
3057 static int SSLParserTest02(void)
3058 {
3059  Flow f;
3060  uint8_t tlsbuf1[] = { 0x16 };
3061  uint32_t tlslen1 = sizeof(tlsbuf1);
3062  uint8_t tlsbuf2[] = { 0x03, 0x01 };
3063  uint32_t tlslen2 = sizeof(tlsbuf2);
3064  TcpSession ssn;
3066 
3067  memset(&f, 0, sizeof(f));
3068  memset(&ssn, 0, sizeof(ssn));
3069  FLOW_INITIALIZE(&f);
3070  f.protoctx = (void *)&ssn;
3071  f.proto = IPPROTO_TCP;
3072  f.alproto = ALPROTO_TLS;
3073 
3075 
3076  FLOWLOCK_WRLOCK(&f);
3077  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3078  STREAM_TOSERVER, tlsbuf1, tlslen1);
3079  FLOWLOCK_UNLOCK(&f);
3080  FAIL_IF(r != 0);
3081 
3082  FLOWLOCK_WRLOCK(&f);
3083  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3084  tlsbuf2, tlslen2);
3085  FLOWLOCK_UNLOCK(&f);
3086  FAIL_IF(r != 0);
3087 
3088  SSLState *ssl_state = f.alstate;
3089  FAIL_IF_NULL(ssl_state);
3090 
3091  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3092 
3093  FAIL_IF(ssl_state->client_connp.version != TLS_VERSION_10);
3094 
3095  AppLayerParserThreadCtxFree(alp_tctx);
3097  FLOW_DESTROY(&f);
3098 
3099  PASS;
3100 }
3101 
3102 /** \test Send a get request in three chunks. */
3103 static int SSLParserTest03(void)
3104 {
3105  Flow f;
3106  uint8_t tlsbuf1[] = { 0x16 };
3107  uint32_t tlslen1 = sizeof(tlsbuf1);
3108  uint8_t tlsbuf2[] = { 0x03 };
3109  uint32_t tlslen2 = sizeof(tlsbuf2);
3110  uint8_t tlsbuf3[] = { 0x01 };
3111  uint32_t tlslen3 = sizeof(tlsbuf3);
3112  TcpSession ssn;
3114 
3115  memset(&f, 0, sizeof(f));
3116  memset(&ssn, 0, sizeof(ssn));
3117  FLOW_INITIALIZE(&f);
3118  f.protoctx = (void *)&ssn;
3119  f.proto = IPPROTO_TCP;
3120  f.alproto = ALPROTO_TLS;
3121 
3123 
3124  FLOWLOCK_WRLOCK(&f);
3125  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3126  STREAM_TOSERVER, tlsbuf1, tlslen1);
3127  FLOWLOCK_UNLOCK(&f);
3128  FAIL_IF(r != 0);
3129 
3130  FLOWLOCK_WRLOCK(&f);
3131  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3132  tlsbuf2, tlslen2);
3133  FLOWLOCK_UNLOCK(&f);
3134  FAIL_IF(r != 0);
3135 
3136  FLOWLOCK_WRLOCK(&f);
3137  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3138  tlsbuf3, tlslen3);
3139  FLOWLOCK_UNLOCK(&f);
3140  FAIL_IF(r != 0);
3141 
3142  SSLState *ssl_state = f.alstate;
3143  FAIL_IF_NULL(ssl_state);
3144 
3145  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3146 
3147  FAIL_IF(ssl_state->client_connp.version != TLS_VERSION_10);
3148 
3149  AppLayerParserThreadCtxFree(alp_tctx);
3151  FLOW_DESTROY(&f);
3152 
3153  PASS;
3154 }
3155 
3156 /** \test Send a get request in three chunks + more data. */
3157 static int SSLParserTest04(void)
3158 {
3159  Flow f;
3160  uint8_t tlsbuf1[] = { 0x16 };
3161  uint32_t tlslen1 = sizeof(tlsbuf1);
3162  uint8_t tlsbuf2[] = { 0x03 };
3163  uint32_t tlslen2 = sizeof(tlsbuf2);
3164  uint8_t tlsbuf3[] = { 0x01 };
3165  uint32_t tlslen3 = sizeof(tlsbuf3);
3166  uint8_t tlsbuf4[] = { 0x01, 0x00, 0x00, 0xad, 0x03, 0x01 };
3167  uint32_t tlslen4 = sizeof(tlsbuf4);
3168  TcpSession ssn;
3170 
3171  memset(&f, 0, sizeof(f));
3172  memset(&ssn, 0, sizeof(ssn));
3173  FLOW_INITIALIZE(&f);
3174  f.protoctx = (void *)&ssn;
3175  f.proto = IPPROTO_TCP;
3176  f.alproto = ALPROTO_TLS;
3177 
3179 
3180  FLOWLOCK_WRLOCK(&f);
3181  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3182  STREAM_TOSERVER, tlsbuf1, tlslen1);
3183  FLOWLOCK_UNLOCK(&f);
3184  FAIL_IF(r != 0);
3185 
3186  FLOWLOCK_WRLOCK(&f);
3187  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3188  tlsbuf2, tlslen2);
3189  FLOWLOCK_UNLOCK(&f);
3190  FAIL_IF(r != 0);
3191 
3192  FLOWLOCK_WRLOCK(&f);
3193  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3194  tlsbuf3, tlslen3);
3195  FLOWLOCK_UNLOCK(&f);
3196  FAIL_IF(r != 0);
3197 
3198  FLOWLOCK_WRLOCK(&f);
3199  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3200  tlsbuf4, tlslen4);
3201  FLOWLOCK_UNLOCK(&f);
3202  FAIL_IF(r != 0);
3203 
3204  SSLState *ssl_state = f.alstate;
3205  FAIL_IF_NULL(ssl_state);
3206 
3207  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3208 
3209  FAIL_IF(ssl_state->client_connp.version != TLS_VERSION_10);
3210 
3211  AppLayerParserThreadCtxFree(alp_tctx);
3213  FLOW_DESTROY(&f);
3214 
3215  PASS;
3216 }
3217 
3218 #if 0
3219 /** \test Test the setting up of no reassembly and no payload inspection flag
3220  * after detection of the TLS handshake completion */
3221 static int SSLParserTest05(void)
3222 {
3223  int result = 1;
3224  Flow f;
3225  uint8_t tlsbuf[] = { 0x16, 0x03, 0x01, 0x00, 0x01 };
3226  uint32_t tlslen = sizeof(tlsbuf);
3227  TcpSession ssn;
3229 
3230  memset(&f, 0, sizeof(f));
3231  memset(&ssn, 0, sizeof(ssn));
3232  FLOW_INITIALIZE(&f);
3233  f.protoctx = (void *)&ssn;
3234  f.proto = IPPROTO_TCP;
3235  f.alproto = ALPROTO_TLS;
3236 
3238 
3239  int r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3240  if (r != 0) {
3241  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3242  result = 0;
3243  goto end;
3244  }
3245 
3246  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT, tlsbuf, tlslen);
3247  if (r != 0) {
3248  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3249  result = 0;
3250  goto end;
3251  }
3252 
3253  tlsbuf[0] = 0x14;
3254 
3255  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3256  if (r != 0) {
3257  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3258  result = 0;
3259  goto end;
3260  }
3261 
3262  tlsbuf[0] = 0x14;
3263 
3264  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT, tlsbuf, tlslen);
3265  if (r != 0) {
3266  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3267  result = 0;
3268  goto end;
3269  }
3270 
3271  tlsbuf[0] = 0x17;
3272 
3273  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3274  if (r != 0) {
3275  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3276  result = 0;
3277  goto end;
3278  }
3279 
3280  SSLState *ssl_state = f.alstate;
3281  if (ssl_state == NULL) {
3282  printf("no tls state: ");
3283  result = 0;
3284  goto end;
3285  }
3286 
3287  if (ssl_state->client_connp.content_type != 0x17) {
3288  printf("expected content_type %" PRIu8 ", got %" PRIu8 ": ", 0x17,
3289  ssl_state->client_connp.content_type);
3290  result = 0;
3291  goto end;
3292  }
3293 
3294  if (ssl_state->client_connp.version != TLS_VERSION_10) {
3295  printf("expected version %04" PRIu16 ", got %04" PRIu16 ": ",
3296  TLS_VERSION_10, ssl_state->client_connp.client_version);
3297  result = 0;
3298  goto end;
3299  }
3300 
3301  AppLayerParserStateStore *parser_state_store = (AppLayerParserStateStore *)
3302  ssn.alparser;
3303  AppLayerParserState *parser_state = &parser_state_store->to_server;
3304 
3305  if (!(parser_state->flags & APP_LAYER_PARSER_NO_INSPECTION) &&
3308  {
3309  printf("The flags should be set\n");
3310  result = 0;
3311  goto end;
3312  }
3313 
3314  if (!(f.flags & FLOW_NOPAYLOAD_INSPECTION)) {
3315  printf("The flags should be set\n");
3316  result = 0;
3317  goto end;
3318  }
3319 
3320 end:
3321  if (alp_tctx != NULL)
3322  AppLayerParserThreadCtxFree(alp_tctx);
3324  FLOW_DESTROY(&f);
3325  return result;
3326 }
3327 #endif
3328 
3329 #if 0
3330 /** \test Test the setting up of no reassembly and no payload inspection flag
3331  * after detection of the valid TLS handshake completion, the rouge
3332  * 0x17 packet will not be considered in the detection process */
3333 static int SSLParserTest06(void)
3334 {
3335  int result = 1;
3336  Flow f;
3337  uint8_t tlsbuf[] = { 0x16, 0x03, 0x01, 0x00, 0x01 };
3338  uint32_t tlslen = sizeof(tlsbuf);
3339  TcpSession ssn;
3341 
3342  memset(&f, 0, sizeof(f));
3343  memset(&ssn, 0, sizeof(ssn));
3344  FLOW_INITIALIZE(&f);
3345  f.protoctx = (void *)&ssn;
3346  f.proto = IPPROTO_TCP;
3347  f.alproto = ALPROTO_TLS;
3348 
3350 
3351  int r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3352  if (r != 0) {
3353  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3354  result = 0;
3355  goto end;
3356  }
3357 
3358  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT, tlsbuf, tlslen);
3359  if (r != 0) {
3360  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3361  result = 0;
3362  goto end;
3363  }
3364 
3365  tlsbuf[0] = 0x14;
3366 
3367  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3368  if (r != 0) {
3369  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3370  result = 0;
3371  goto end;
3372  }
3373 
3374  tlsbuf[0] = 0x17;
3375 
3376  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3377  if (r != 0) {
3378  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3379  result = 0;
3380  goto end;
3381  }
3382 
3383  SSLState *ssl_state = f.alstate;
3384  if (ssl_state == NULL) {
3385  printf("no tls state: ");
3386  result = 0;
3387  goto end;
3388  }
3389 
3390  if (ssl_state->client_connp.content_type != 0x17) {
3391  printf("expected content_type %" PRIu8 ", got %" PRIu8 ": ", 0x17,
3392  ssl_state->client_connp._content_type);
3393  result = 0;
3394  goto end;
3395  }
3396 
3397  if (ssl_state->client_connp.version != TLS_VERSION_10) {
3398  printf("expected version %04" PRIu16 ", got %04" PRIu16 ": ",
3399  TLS_VERSION_10, ssl_state->client_connp.version);
3400  result = 0;
3401  goto end;
3402  }
3403 
3404  AppLayerParserStateStore *parser_state_store = (AppLayerParserStateStore *)
3405  ssn.alparser;
3406  AppLayerParserState *parser_state = &parser_state_store->to_server;
3407 
3408  if ((parser_state->flags & APP_LAYER_PARSER_NO_INSPECTION) ||
3411  printf("The flags should not be set\n");
3412  result = 0;
3413  goto end;
3414  }
3415 
3416  tlsbuf[0] = 0x14;
3417 
3418  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT, tlsbuf, tlslen);
3419  if (r != 0) {
3420  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3421  result = 0;
3422  goto end;
3423  }
3424 
3425  tlsbuf[0] = 0x17;
3426 
3427  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3428  if (r != 0) {
3429  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3430  result = 0;
3431  goto end;
3432  }
3433 
3434  if (!(parser_state->flags & APP_LAYER_PARSER_NO_INSPECTION) &&
3437  printf("The flags should be set\n");
3438  result = 0;
3439  goto end;
3440  }
3441 
3442  if (!(f.flags & FLOW_NOPAYLOAD_INSPECTION)) {
3443  printf("The flags should be set\n");
3444  result = 0;
3445  goto end;
3446  }
3447 
3448 end:
3449  if (alp_tctx != NULL)
3450  AppLayerParserThreadCtxFree(alp_tctx);
3452  FLOW_DESTROY(&f);
3453  return result;
3454 }
3455 #endif
3456 
3457 /** \test multimsg test */
3458 static int SSLParserMultimsgTest01(void)
3459 {
3460  Flow f;
3461  /* 3 msgs */
3462  uint8_t tlsbuf1[] = {
3463  0x16, 0x03, 0x01, 0x00, 0x86, 0x10, 0x00, 0x00,
3464  0x82, 0x00, 0x80, 0xd3, 0x6f, 0x1f, 0x63, 0x82,
3465  0x8d, 0x75, 0x77, 0x8c, 0x91, 0xbc, 0xa1, 0x3d,
3466  0xbb, 0xe1, 0xb5, 0xd3, 0x31, 0x92, 0x59, 0x2b,
3467  0x2c, 0x43, 0x96, 0xa3, 0xaa, 0x23, 0x92, 0xd0,
3468  0x91, 0x2a, 0x5e, 0x10, 0x5b, 0xc8, 0xc1, 0xe2,
3469  0xd3, 0x5c, 0x8b, 0x8c, 0x91, 0x9e, 0xc2, 0xf2,
3470  0x9c, 0x3c, 0x4f, 0x37, 0x1e, 0x20, 0x5e, 0x33,
3471  0xd5, 0xf0, 0xd6, 0xaf, 0x89, 0xf5, 0xcc, 0xb2,
3472  0xcf, 0xc1, 0x60, 0x3a, 0x46, 0xd5, 0x4e, 0x2a,
3473  0xb6, 0x6a, 0xb9, 0xfc, 0x32, 0x8b, 0xe0, 0x6e,
3474  0xa0, 0xed, 0x25, 0xa0, 0xa4, 0x82, 0x81, 0x73,
3475  0x90, 0xbf, 0xb5, 0xde, 0xeb, 0x51, 0x8d, 0xde,
3476  0x5b, 0x6f, 0x94, 0xee, 0xba, 0xe5, 0x69, 0xfa,
3477  0x1a, 0x80, 0x30, 0x54, 0xeb, 0x12, 0x01, 0xb9,
3478  0xfe, 0xbf, 0x82, 0x95, 0x01, 0x7b, 0xb0, 0x97,
3479  0x14, 0xc2, 0x06, 0x3c, 0x69, 0xfb, 0x1c, 0x66,
3480  0x47, 0x17, 0xd9, 0x14, 0x03, 0x01, 0x00, 0x01,
3481  0x01, 0x16, 0x03, 0x01, 0x00, 0x30, 0xf6, 0xbc,
3482  0x0d, 0x6f, 0xe8, 0xbb, 0xaa, 0xbf, 0x14, 0xeb,
3483  0x7b, 0xcc, 0x6c, 0x28, 0xb0, 0xfc, 0xa6, 0x01,
3484  0x2a, 0x97, 0x96, 0x17, 0x5e, 0xe8, 0xb4, 0x4e,
3485  0x78, 0xc9, 0x04, 0x65, 0x53, 0xb6, 0x93, 0x3d,
3486  0xeb, 0x44, 0xee, 0x86, 0xf9, 0x80, 0x49, 0x45,
3487  0x21, 0x34, 0xd1, 0xee, 0xc8, 0x9c
3488  };
3489  uint32_t tlslen1 = sizeof(tlsbuf1);
3490  TcpSession ssn;
3492 
3493  memset(&f, 0, sizeof(f));
3494  memset(&ssn, 0, sizeof(ssn));
3495  FLOW_INITIALIZE(&f);
3496  f.protoctx = (void *)&ssn;
3497  f.proto = IPPROTO_TCP;
3498  f.alproto = ALPROTO_TLS;
3499 
3501 
3502  FLOWLOCK_WRLOCK(&f);
3503  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3504  STREAM_TOSERVER, tlsbuf1, tlslen1);
3505  FLOWLOCK_UNLOCK(&f);
3506  FAIL_IF(r != 0);
3507 
3508  SSLState *ssl_state = f.alstate;
3509  FAIL_IF_NULL(ssl_state);
3510 
3511  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3512 
3513  FAIL_IF(ssl_state->client_connp.version != TLS_VERSION_10);
3514 
3515  AppLayerParserThreadCtxFree(alp_tctx);
3517  FLOW_DESTROY(&f);
3518 
3519  PASS;
3520 }
3521 
3522 /** \test multimsg test server */
3523 static int SSLParserMultimsgTest02(void)
3524 {
3525  Flow f;
3526  /* 3 msgs */
3527  uint8_t tlsbuf1[] = {
3528  0x16, 0x03, 0x01, 0x00, 0x86, 0x10, 0x00, 0x00,
3529  0x82, 0x00, 0x80, 0xd3, 0x6f, 0x1f, 0x63, 0x82,
3530  0x8d, 0x75, 0x77, 0x8c, 0x91, 0xbc, 0xa1, 0x3d,
3531  0xbb, 0xe1, 0xb5, 0xd3, 0x31, 0x92, 0x59, 0x2b,
3532  0x2c, 0x43, 0x96, 0xa3, 0xaa, 0x23, 0x92, 0xd0,
3533  0x91, 0x2a, 0x5e, 0x10, 0x5b, 0xc8, 0xc1, 0xe2,
3534  0xd3, 0x5c, 0x8b, 0x8c, 0x91, 0x9e, 0xc2, 0xf2,
3535  0x9c, 0x3c, 0x4f, 0x37, 0x1e, 0x20, 0x5e, 0x33,
3536  0xd5, 0xf0, 0xd6, 0xaf, 0x89, 0xf5, 0xcc, 0xb2,
3537  0xcf, 0xc1, 0x60, 0x3a, 0x46, 0xd5, 0x4e, 0x2a,
3538  0xb6, 0x6a, 0xb9, 0xfc, 0x32, 0x8b, 0xe0, 0x6e,
3539  0xa0, 0xed, 0x25, 0xa0, 0xa4, 0x82, 0x81, 0x73,
3540  0x90, 0xbf, 0xb5, 0xde, 0xeb, 0x51, 0x8d, 0xde,
3541  0x5b, 0x6f, 0x94, 0xee, 0xba, 0xe5, 0x69, 0xfa,
3542  0x1a, 0x80, 0x30, 0x54, 0xeb, 0x12, 0x01, 0xb9,
3543  0xfe, 0xbf, 0x82, 0x95, 0x01, 0x7b, 0xb0, 0x97,
3544  0x14, 0xc2, 0x06, 0x3c, 0x69, 0xfb, 0x1c, 0x66,
3545  0x47, 0x17, 0xd9, 0x14, 0x03, 0x01, 0x00, 0x01,
3546  0x01, 0x16, 0x03, 0x01, 0x00, 0x30, 0xf6, 0xbc,
3547  0x0d, 0x6f, 0xe8, 0xbb, 0xaa, 0xbf, 0x14, 0xeb,
3548  0x7b, 0xcc, 0x6c, 0x28, 0xb0, 0xfc, 0xa6, 0x01,
3549  0x2a, 0x97, 0x96, 0x17, 0x5e, 0xe8, 0xb4, 0x4e,
3550  0x78, 0xc9, 0x04, 0x65, 0x53, 0xb6, 0x93, 0x3d,
3551  0xeb, 0x44, 0xee, 0x86, 0xf9, 0x80, 0x49, 0x45,
3552  0x21, 0x34, 0xd1, 0xee, 0xc8, 0x9c
3553  };
3554  uint32_t tlslen1 = sizeof(tlsbuf1);
3555  TcpSession ssn;
3557 
3558  memset(&f, 0, sizeof(f));
3559  memset(&ssn, 0, sizeof(ssn));
3560  FLOW_INITIALIZE(&f);
3561  f.protoctx = (void *)&ssn;
3562  f.proto = IPPROTO_TCP;
3563  f.alproto = ALPROTO_TLS;
3564 
3566 
3567  FLOWLOCK_WRLOCK(&f);
3568  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3569  STREAM_TOCLIENT, tlsbuf1, tlslen1);
3570  FLOWLOCK_UNLOCK(&f);
3571  FAIL_IF(r != 0);
3572 
3573  SSLState *ssl_state = f.alstate;
3574  FAIL_IF_NULL(ssl_state);
3575 
3576  FAIL_IF(ssl_state->server_connp.content_type != 0x16);
3577 
3578  FAIL_IF(ssl_state->server_connp.version != 0x0301);
3579 
3580  AppLayerParserThreadCtxFree(alp_tctx);
3582  FLOW_DESTROY(&f);
3583 
3584  PASS;
3585 }
3586 
3587 /**
3588  * \test Test the detection of SSLv3 protocol from the given packet
3589  */
3590 static int SSLParserTest07(void)
3591 {
3592  Flow f;
3593  uint8_t tlsbuf[] = { 0x16, 0x03, 0x00, 0x00, 0x4c, 0x01,
3594  0x00, 0x00, 0x48, 0x03, 0x00, 0x57, 0x04, 0x9f,
3595  0x8c, 0x66, 0x61, 0xf6, 0x3d, 0x4f, 0xbf, 0xbb,
3596  0xa7, 0x47, 0x21, 0x76, 0x6c, 0x21, 0x08, 0x9f,
3597  0xef, 0x3d, 0x0e, 0x5f, 0x65, 0x1a, 0xe1, 0x93,
3598  0xb8, 0xaf, 0xd2, 0x82, 0xbd, 0x00, 0x00, 0x06,
3599  0x00, 0x0a, 0x00, 0x16, 0x00, 0xff, 0x01, 0x00,
3600  0x00, 0x19, 0x00, 0x00, 0x00, 0x15, 0x00, 0x13,
3601  0x00, 0x00, 0x10, 0x61, 0x62, 0x63, 0x64, 0x65,
3602  0x66, 0x67, 0x68, 0x2e, 0x65, 0x66, 0x67, 0x68,
3603  0x2e, 0x6e, 0x6f };
3604  uint32_t tlslen = sizeof(tlsbuf);
3605  TcpSession ssn;
3607 
3608  memset(&f, 0, sizeof(f));
3609  memset(&ssn, 0, sizeof(ssn));
3610  FLOW_INITIALIZE(&f);
3611  f.protoctx = (void *)&ssn;
3612  f.proto = IPPROTO_TCP;
3613  f.alproto = ALPROTO_TLS;
3614 
3616 
3617  FLOWLOCK_WRLOCK(&f);
3618  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3619  STREAM_TOSERVER, tlsbuf, tlslen);
3620  FLOWLOCK_UNLOCK(&f);
3621  FAIL_IF(r != 0);
3622 
3623  SSLState *ssl_state = f.alstate;
3624  FAIL_IF_NULL(ssl_state);
3625 
3626  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3627 
3628  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
3629 
3630  AppLayerParserThreadCtxFree(alp_tctx);
3632  FLOW_DESTROY(&f);
3633 
3634  PASS;
3635 }
3636 
3637 #if 0
3638 /** \test Test the setting up of no reassembly and no payload inspection flag
3639  * after detection of the SSLv3 handshake completion */
3640 static int SSLParserTest08(void)
3641 {
3642  int result = 1;
3643  Flow f;
3644  uint8_t tlsbuf[] = { 0x16, 0x03, 0x00, 0x00, 0x01 };
3645  uint32_t tlslen = sizeof(tlsbuf);
3646  TcpSession ssn;
3648 
3649  memset(&f, 0, sizeof(f));
3650  memset(&ssn, 0, sizeof(ssn));
3651  FLOW_INITIALIZE(&f);
3652  f.protoctx = (void *)&ssn;
3653  f.proto = IPPROTO_TCP;
3654  f.alproto = ALPROTO_TLS;
3655 
3657 
3658  int r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3659  if (r != 0) {
3660  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3661  result = 0;
3662  goto end;
3663  }
3664 
3665  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT, tlsbuf, tlslen);
3666  if (r != 0) {
3667  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3668  result = 0;
3669  goto end;
3670  }
3671 
3672  tlsbuf[0] = 0x14;
3673 
3674  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3675  if (r != 0) {
3676  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3677  result = 0;
3678  goto end;
3679  }
3680 
3681  tlsbuf[0] = 0x14;
3682 
3683  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT, tlsbuf, tlslen);
3684  if (r != 0) {
3685  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3686  result = 0;
3687  goto end;
3688  }
3689 
3690  tlsbuf[0] = 0x17;
3691 
3692  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3693  if (r != 0) {
3694  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3695  result = 0;
3696  goto end;
3697  }
3698 
3699  SSLState *ssl_state = f.alstate;
3700  if (ssl_state == NULL) {
3701  printf("no tls state: ");
3702  result = 0;
3703  goto end;
3704  }
3705 
3706  if (ssl_state->client_connp.content_type != 0x17) {
3707  printf("expected content_type %" PRIu8 ", got %" PRIu8 ": ", 0x17,
3708  ssl_state->client_connp.content_type);
3709  result = 0;
3710  goto end;
3711  }
3712 
3713  if (ssl_state->client_connp.version != SSL_VERSION_3) {
3714  printf("expected version %04" PRIu16 ", got %04" PRIu16 ": ",
3715  SSL_VERSION_3, ssl_state->client_connp.version);
3716  result = 0;
3717  goto end;
3718  }
3719 
3720  AppLayerParserStateStore *parser_state_store = (AppLayerParserStateStore *)
3721  ssn.alparser;
3722  AppLayerParserState *parser_state = &parser_state_store->to_server;
3723 
3724  if (!(parser_state->flags & APP_LAYER_PARSER_NO_INSPECTION) &&
3727  printf("The flags should be set\n");
3728  result = 0;
3729  goto end;
3730  }
3731 
3732  if (!(f.flags & FLOW_NOPAYLOAD_INSPECTION)) {
3733  printf("The flags should be set\n");
3734  result = 0;
3735  goto end;
3736  }
3737 
3738 end:
3739  if (alp_tctx != NULL)
3740  AppLayerParserThreadCtxFree(alp_tctx);
3742  FLOW_DESTROY(&f);
3743  return result;
3744 }
3745 
3746 #endif
3747 
3748 /**
3749  * \test Tests the parser for handling fragmented records.
3750  */
3751 static int SSLParserTest09(void)
3752 {
3753  Flow f;
3754  uint8_t buf1[] = {
3755  0x16,
3756  };
3757  uint32_t buf1_len = sizeof(buf1);
3758 
3759  uint8_t buf2[] = {
3760  0x03, 0x00, 0x00, 0x4c, 0x01,
3761  0x00, 0x00, 0x48, 0x03, 0x00, 0x57, 0x04, 0x9f,
3762  0x8c, 0x66, 0x61, 0xf6, 0x3d, 0x4f, 0xbf, 0xbb,
3763  0xa7, 0x47, 0x21, 0x76, 0x6c, 0x21, 0x08, 0x9f,
3764  0xef, 0x3d, 0x0e, 0x5f, 0x65, 0x1a, 0xe1, 0x93,
3765  0xb8, 0xaf, 0xd2, 0x82, 0xbd, 0x00, 0x00, 0x06,
3766  0x00, 0x0a, 0x00, 0x16, 0x00, 0xff, 0x01, 0x00,
3767  0x00, 0x19, 0x00, 0x00, 0x00, 0x15, 0x00, 0x13,
3768  0x00, 0x00, 0x10, 0x61, 0x62, 0x63, 0x64, 0x65,
3769  0x66, 0x67, 0x68, 0x2e, 0x65, 0x66, 0x67, 0x68,
3770  0x2e, 0x6e, 0x6f
3771  };
3772  uint32_t buf2_len = sizeof(buf2);
3773  TcpSession ssn;
3775 
3776  memset(&f, 0, sizeof(f));
3777  memset(&ssn, 0, sizeof(ssn));
3778  FLOW_INITIALIZE(&f);
3779  f.protoctx = (void *)&ssn;
3780  f.proto = IPPROTO_TCP;
3781  f.alproto = ALPROTO_TLS;
3782 
3784 
3785  FLOWLOCK_WRLOCK(&f);
3786  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3787  STREAM_TOSERVER, buf1, buf1_len);
3788  FLOWLOCK_UNLOCK(&f);
3789  FAIL_IF(r != 0);
3790 
3791  FLOWLOCK_WRLOCK(&f);
3792  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3793  buf2, buf2_len);
3794  FLOWLOCK_UNLOCK(&f);
3795  FAIL_IF(r != 0);
3796 
3797  SSLState *ssl_state = f.alstate;
3798  FAIL_IF_NULL(ssl_state);
3799 
3800  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3801 
3802  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
3803 
3804  AppLayerParserThreadCtxFree(alp_tctx);
3806  FLOW_DESTROY(&f);
3807 
3808  PASS;
3809 }
3810 
3811 /**
3812  * \test Tests the parser for handling fragmented records.
3813  */
3814 static int SSLParserTest10(void)
3815 {
3816  Flow f;
3817  uint8_t buf1[] = {
3818  0x16, 0x03,
3819  };
3820  uint32_t buf1_len = sizeof(buf1);
3821 
3822  uint8_t buf2[] = {
3823  0x00, 0x00, 0x4c, 0x01,
3824  0x00, 0x00, 0x48, 0x03, 0x00, 0x57, 0x04, 0x9f,
3825  0x8c, 0x66, 0x61, 0xf6, 0x3d, 0x4f, 0xbf, 0xbb,
3826  0xa7, 0x47, 0x21, 0x76, 0x6c, 0x21, 0x08, 0x9f,
3827  0xef, 0x3d, 0x0e, 0x5f, 0x65, 0x1a, 0xe1, 0x93,
3828  0xb8, 0xaf, 0xd2, 0x82, 0xbd, 0x00, 0x00, 0x06,
3829  0x00, 0x0a, 0x00, 0x16, 0x00, 0xff, 0x01, 0x00,
3830  0x00, 0x19, 0x00, 0x00, 0x00, 0x15, 0x00, 0x13,
3831  0x00, 0x00, 0x10, 0x61, 0x62, 0x63, 0x64, 0x65,
3832  0x66, 0x67, 0x68, 0x2e, 0x65, 0x66, 0x67, 0x68,
3833  0x2e, 0x6e, 0x6f
3834  };
3835  uint32_t buf2_len = sizeof(buf2);
3836  TcpSession ssn;
3838 
3839  memset(&f, 0, sizeof(f));
3840  memset(&ssn, 0, sizeof(ssn));
3841  FLOW_INITIALIZE(&f);
3842  f.protoctx = (void *)&ssn;
3843  f.proto = IPPROTO_TCP;
3844  f.alproto = ALPROTO_TLS;
3845 
3847 
3848  FLOWLOCK_WRLOCK(&f);
3849  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3850  STREAM_TOSERVER, buf1, buf1_len);
3851  FLOWLOCK_UNLOCK(&f);
3852  FAIL_IF(r != 0);
3853 
3854  FLOWLOCK_WRLOCK(&f);
3855  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3856  buf2, buf2_len);
3857  FLOWLOCK_UNLOCK(&f);
3858  FAIL_IF(r != 0);
3859 
3860  SSLState *ssl_state = f.alstate;
3861  FAIL_IF_NULL(ssl_state);
3862 
3863  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3864 
3865  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
3866 
3867  AppLayerParserThreadCtxFree(alp_tctx);
3869  FLOW_DESTROY(&f);
3870 
3871  PASS;
3872 }
3873 
3874 /**
3875  * \test Tests the parser for handling fragmented records.
3876  */
3877 static int SSLParserTest11(void)
3878 {
3879  Flow f;
3880  uint8_t buf1[] = {
3881  0x16, 0x03, 0x00, 0x00, 0x4c, 0x01,
3882  };
3883  uint32_t buf1_len = sizeof(buf1);
3884 
3885  uint8_t buf2[] = {
3886  0x00, 0x00, 0x48, 0x03, 0x00, 0x57, 0x04, 0x9f,
3887  0x8c, 0x66, 0x61, 0xf6, 0x3d, 0x4f, 0xbf, 0xbb,
3888  0xa7, 0x47, 0x21, 0x76, 0x6c, 0x21, 0x08, 0x9f,
3889  0xef, 0x3d, 0x0e, 0x5f, 0x65, 0x1a, 0xe1, 0x93,
3890  0xb8, 0xaf, 0xd2, 0x82, 0xbd, 0x00, 0x00, 0x06,
3891  0x00, 0x0a, 0x00, 0x16, 0x00, 0xff, 0x01, 0x00,
3892  0x00, 0x19, 0x00, 0x00, 0x00, 0x15, 0x00, 0x13,
3893  0x00, 0x00, 0x10, 0x61, 0x62, 0x63, 0x64, 0x65,
3894  0x66, 0x67, 0x68, 0x2e, 0x65, 0x66, 0x67, 0x68,
3895  0x2e, 0x6e, 0x6f
3896  };
3897  uint32_t buf2_len = sizeof(buf2);
3898  TcpSession ssn;
3900 
3901  memset(&f, 0, sizeof(f));
3902  memset(&ssn, 0, sizeof(ssn));
3903  FLOW_INITIALIZE(&f);
3904  f.protoctx = (void *)&ssn;
3905  f.proto = IPPROTO_TCP;
3906  f.alproto = ALPROTO_TLS;
3907 
3909 
3910  FLOWLOCK_WRLOCK(&f);
3911  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3912  STREAM_TOSERVER, buf1, buf1_len);
3913  FLOWLOCK_UNLOCK(&f);
3914  FAIL_IF(r != 0);
3915 
3916  FLOWLOCK_WRLOCK(&f);
3917  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3918  buf2, buf2_len);
3919  FLOWLOCK_UNLOCK(&f);
3920  FAIL_IF(r != 0);
3921 
3922  SSLState *ssl_state = f.alstate;
3923  FAIL_IF_NULL(ssl_state);
3924 
3925  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3926 
3927  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
3928 
3929  AppLayerParserThreadCtxFree(alp_tctx);
3931  FLOW_DESTROY(&f);
3932 
3933  PASS;
3934 }
3935 
3936 /**
3937  * \test Tests the parser for handling fragmented records.
3938  */
3939 static int SSLParserTest12(void)
3940 {
3941  Flow f;
3942  uint8_t buf1[] = {
3943  0x16, 0x03, 0x00, 0x00, 0x4c, 0x01,
3944  };
3945  uint32_t buf1_len = sizeof(buf1);
3946 
3947  uint8_t buf2[] = {
3948  0x00, 0x00, 0x48,
3949  };
3950  uint32_t buf2_len = sizeof(buf2);
3951 
3952  uint8_t buf3[] = {
3953  0x03, 0x00, 0x57, 0x04, 0x9f,
3954  0x8c, 0x66, 0x61, 0xf6, 0x3d, 0x4f, 0xbf, 0xbb,
3955  0xa7, 0x47, 0x21, 0x76, 0x6c, 0x21, 0x08, 0x9f,
3956  0xef, 0x3d, 0x0e, 0x5f, 0x65, 0x1a, 0xe1, 0x93,
3957  0xb8, 0xaf, 0xd2, 0x82, 0xbd, 0x00, 0x00, 0x06,
3958  0x00, 0x0a, 0x00, 0x16, 0x00, 0xff, 0x01, 0x00,
3959  0x00, 0x19, 0x00, 0x00, 0x00, 0x15, 0x00, 0x13,
3960  0x00, 0x00, 0x10, 0x61, 0x62, 0x63, 0x64, 0x65,
3961  0x66, 0x67, 0x68, 0x2e, 0x65, 0x66, 0x67, 0x68,
3962  0x2e, 0x6e, 0x6f
3963  };
3964  uint32_t buf3_len = sizeof(buf2);
3965  TcpSession ssn;
3967 
3968  memset(&f, 0, sizeof(f));
3969  memset(&ssn, 0, sizeof(ssn));
3970  FLOW_INITIALIZE(&f);
3971  f.protoctx = (void *)&ssn;
3972  f.proto = IPPROTO_TCP;
3973  f.alproto = ALPROTO_TLS;
3974 
3976 
3977  FLOWLOCK_WRLOCK(&f);
3978  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3979  STREAM_TOSERVER, buf1, buf1_len);
3980  FLOWLOCK_UNLOCK(&f);
3981  FAIL_IF(r != 0);
3982 
3983  FLOWLOCK_WRLOCK(&f);
3984  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3985  buf2, buf2_len);
3986  FLOWLOCK_UNLOCK(&f);
3987  FAIL_IF(r != 0);
3988 
3989  FLOWLOCK_WRLOCK(&f);
3990  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3991  buf3, buf3_len);
3992  FLOWLOCK_UNLOCK(&f);
3993  FAIL_IF(r != 0);
3994 
3995  SSLState *ssl_state = f.alstate;
3996  FAIL_IF_NULL(ssl_state);
3997 
3998  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3999 
4000  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
4001 
4002  AppLayerParserThreadCtxFree(alp_tctx);
4004  FLOW_DESTROY(&f);
4005 
4006  PASS;
4007 }
4008 
4009 /**
4010  * \test Tests the parser for handling fragmented records.
4011  */
4012 static int SSLParserTest13(void)
4013 {
4014  Flow f;
4015  uint8_t buf1[] = {
4016  0x16, 0x03, 0x00, 0x00, 0x4c, 0x01,
4017  };
4018  uint32_t buf1_len = sizeof(buf1);
4019 
4020  uint8_t buf2[] = {
4021  0x00, 0x00, 0x48,
4022  };
4023  uint32_t buf2_len = sizeof(buf2);
4024 
4025  uint8_t buf3[] = {
4026  0x03, 0x00, 0x57, 0x04, 0x9f,
4027  0x8c, 0x66, 0x61, 0xf6, 0x3d, 0x4f,
4028  };
4029  uint32_t buf3_len = sizeof(buf3);
4030 
4031  uint8_t buf4[] = {
4032  0xbf, 0xbb,
4033  0xa7, 0x47, 0x21, 0x76, 0x6c, 0x21, 0x08, 0x9f,
4034  0xef, 0x3d, 0x0e, 0x5f, 0x65, 0x1a, 0xe1, 0x93,
4035  0xb8, 0xaf, 0xd2, 0x82, 0xbd, 0x00, 0x00, 0x06,
4036  0x00, 0x0a, 0x00, 0x16, 0x00, 0xff, 0x01, 0x00,
4037  0x00, 0x19, 0x00, 0x00, 0x00, 0x15, 0x00, 0x13,
4038  0x00, 0x00, 0x10, 0x61, 0x62, 0x63, 0x64, 0x65,
4039  0x66, 0x67, 0x68, 0x2e, 0x65, 0x66, 0x67, 0x68,
4040  0x2e, 0x6e, 0x6f
4041  };
4042  uint32_t buf4_len = sizeof(buf4);
4043  TcpSession ssn;
4045 
4046  memset(&f, 0, sizeof(f));
4047  memset(&ssn, 0, sizeof(ssn));
4048  FLOW_INITIALIZE(&f);
4049  f.protoctx = (void *)&ssn;
4050  f.proto = IPPROTO_TCP;
4051  f.alproto = ALPROTO_TLS;
4052 
4054 
4055  FLOWLOCK_WRLOCK(&f);
4056  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4057  STREAM_TOSERVER, buf1, buf1_len);
4058  FLOWLOCK_UNLOCK(&f);
4059  FAIL_IF(r != 0);
4060 
4061  FLOWLOCK_WRLOCK(&f);
4062  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4063  buf2, buf2_len);
4064  FLOWLOCK_UNLOCK(&f);
4065  FAIL_IF(r != 0);
4066 
4067  FLOWLOCK_WRLOCK(&f);
4068  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4069  buf3, buf3_len);
4070  FLOWLOCK_UNLOCK(&f);
4071  FAIL_IF(r != 0);
4072 
4073  FLOWLOCK_WRLOCK(&f);
4074  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4075  buf4, buf4_len);
4076  FLOWLOCK_UNLOCK(&f);
4077  FAIL_IF(r != 0);
4078 
4079  SSLState *ssl_state = f.alstate;
4080  FAIL_IF_NULL(ssl_state);
4081 
4082  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
4083 
4084  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
4085 
4086  AppLayerParserThreadCtxFree(alp_tctx);
4088  FLOW_DESTROY(&f);
4089 
4090  PASS;
4091 }
4092 
4093 /**
4094  * \test Tests the parser for handling fragmented records.
4095  */
4096 static int SSLParserTest14(void)
4097 {
4098  Flow f;
4099 
4100  uint8_t buf1[] = {
4101  0x16, 0x03, 0x00, 0x00, 0x00,
4102  };
4103  uint32_t buf1_len = sizeof(buf1);
4104 
4105  uint8_t buf2[] = {
4106  0x16, 0x03, 0x00, 0x00, 0x00,
4107  };
4108  uint32_t buf2_len = sizeof(buf2);
4109 
4110  TcpSession ssn;
4112 
4113  memset(&f, 0, sizeof(f));
4114  memset(&ssn, 0, sizeof(ssn));
4115  FLOW_INITIALIZE(&f);
4116  f.protoctx = (void *)&ssn;
4117  f.proto = IPPROTO_TCP;
4118  f.alproto = ALPROTO_TLS;
4119 
4121 
4122  FLOWLOCK_WRLOCK(&f);
4123  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4124  STREAM_TOSERVER, buf1, buf1_len);
4125  FLOWLOCK_UNLOCK(&f);
4126  FAIL_IF(r != 0);
4127 
4128  FLOWLOCK_WRLOCK(&f);
4129  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4130  buf2, buf2_len);
4131  FLOWLOCK_UNLOCK(&f);
4132  FAIL_IF(r != 0);
4133 
4134  SSLState *ssl_state = f.alstate;
4135  FAIL_IF_NULL(ssl_state);
4136 
4137  AppLayerParserThreadCtxFree(alp_tctx);
4139  FLOW_DESTROY(&f);
4140 
4141  PASS;
4142 }
4143 
4144 /**
4145  * \test Tests the parser for handling fragmented records.
4146  */
4147 static int SSLParserTest15(void)
4148 {
4149  Flow f;
4150 
4151  uint8_t buf1[] = {
4152  0x16, 0x03, 0x00, 0x00, 0x01, 0x01,
4153  };
4154  uint32_t buf1_len = sizeof(buf1);
4155 
4156  TcpSession ssn;
4158 
4159  memset(&f, 0, sizeof(f));
4160  memset(&ssn, 0, sizeof(ssn));
4161  FLOW_INITIALIZE(&f);
4162  f.protoctx = (void *)&ssn;
4163  f.proto = IPPROTO_TCP;
4164  f.alproto = ALPROTO_TLS;
4165 
4167 
4168  FLOWLOCK_WRLOCK(&f);
4169  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4170  STREAM_TOSERVER, buf1, buf1_len);
4171  FLOWLOCK_UNLOCK(&f);
4172  FAIL_IF(r == 0);
4173 
4174  AppLayerParserThreadCtxFree(alp_tctx);
4176  FLOW_DESTROY(&f);
4177 
4178  PASS;
4179 }
4180 
4181 /**
4182  * \test Tests the parser for handling fragmented records.
4183  */
4184 static int SSLParserTest16(void)
4185 {
4186  Flow f;
4187 
4188  uint8_t buf1[] = {
4189  0x16, 0x03, 0x00, 0x00, 0x02, 0x01, 0x00
4190  };
4191  uint32_t buf1_len = sizeof(buf1);
4192 
4193  TcpSession ssn;
4195 
4196  memset(&f, 0, sizeof(f));
4197  memset(&ssn, 0, sizeof(ssn));
4198  FLOW_INITIALIZE(&f);
4199  f.protoctx = (void *)&ssn;
4200  f.proto = IPPROTO_TCP;
4201  f.alproto = ALPROTO_TLS;
4202 
4204 
4205  FLOWLOCK_WRLOCK(&f);
4206  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4207  STREAM_TOSERVER, buf1, buf1_len);
4208  FLOWLOCK_UNLOCK(&f);
4209  FAIL_IF(r == 0);
4210 
4211  AppLayerParserThreadCtxFree(alp_tctx);
4213  FLOW_DESTROY(&f);
4214 
4215  PASS;
4216 }
4217 
4218 /**
4219  * \test Tests the parser for handling fragmented records.
4220  */
4221 static int SSLParserTest17(void)
4222 {
4223  Flow f;
4224 
4225  uint8_t buf1[] = {
4226  0x16, 0x03, 0x00, 0x00, 0x03, 0x01, 0x00, 0x00
4227  };
4228  uint32_t buf1_len = sizeof(buf1);
4229 
4230  TcpSession ssn;
4232 
4233  memset(&f, 0, sizeof(f));
4234  memset(&ssn, 0, sizeof(ssn));
4235  FLOW_INITIALIZE(&f);
4236  f.protoctx = (void *)&ssn;
4237  f.proto = IPPROTO_TCP;
4238  f.alproto = ALPROTO_TLS;
4239 
4241 
4242  FLOWLOCK_WRLOCK(&f);
4243  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4244  STREAM_TOSERVER, buf1, buf1_len);
4245  FLOWLOCK_UNLOCK(&f);
4246  FAIL_IF(r == 0);
4247 
4248  AppLayerParserThreadCtxFree(alp_tctx);
4250  FLOW_DESTROY(&f);
4251 
4252  PASS;
4253 }
4254 
4255 /**
4256  * \test Tests the parser for handling fragmented records.
4257  */
4258 static int SSLParserTest18(void)
4259 {
4260  Flow f;
4261 
4262  uint8_t buf1[] = {
4263  0x16, 0x03, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00,
4264  0x6b,
4265  };
4266  uint32_t buf1_len = sizeof(buf1);
4267 
4268  uint8_t buf2[] = {
4269  0x16, 0x03, 0x00, 0x00, 0x00,
4270  };
4271  uint32_t buf2_len = sizeof(buf2);
4272 
4273  TcpSession ssn;
4275 
4276  memset(&f, 0, sizeof(f));
4277  memset(&ssn, 0, sizeof(ssn));
4278  FLOW_INITIALIZE(&f);
4279  f.protoctx = (void *)&ssn;
4280  f.proto = IPPROTO_TCP;
4281  f.alproto = ALPROTO_TLS;
4282 
4284 
4285  FLOWLOCK_WRLOCK(&f);
4286  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4287  STREAM_TOSERVER, buf1, buf1_len);
4288  FLOWLOCK_UNLOCK(&f);
4289  FAIL_IF(r != 0);
4290 
4291  FLOWLOCK_WRLOCK(&f);
4292  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4293  buf2, buf2_len);
4294  FLOWLOCK_UNLOCK(&f);
4295  FAIL_IF(r != 0);
4296 
4297  SSLState *ssl_state = f.alstate;
4298  FAIL_IF_NULL(ssl_state);
4299 
4300  AppLayerParserThreadCtxFree(alp_tctx);
4302  FLOW_DESTROY(&f);
4303 
4304  PASS;
4305 }
4306 
4307 /**
4308  * \test Tests the parser for handling fragmented records.
4309  */
4310 static int SSLParserTest19(void)
4311 {
4312  Flow f;
4313 
4314  uint8_t buf1[] = {
4315  0x16, 0x03, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00,
4316  0x6b, 0x16, 0x03, 0x00, 0x00, 0x00,
4317  };
4318  uint32_t buf1_len = sizeof(buf1);
4319 
4320  TcpSession ssn;
4322 
4323  memset(&f, 0, sizeof(f));
4324  memset(&ssn, 0, sizeof(ssn));
4325  FLOW_INITIALIZE(&f);
4326  f.protoctx = (void *)&ssn;
4327  f.proto = IPPROTO_TCP;
4328  f.alproto = ALPROTO_TLS;
4329 
4331 
4332  FLOWLOCK_WRLOCK(&f);
4333  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4334  STREAM_TOSERVER, buf1, buf1_len);
4335  FLOWLOCK_UNLOCK(&f);
4336  FAIL_IF(r != 0);
4337 
4338  SSLState *ssl_state = f.alstate;
4339  FAIL_IF_NULL(ssl_state);
4340 
4341  AppLayerParserThreadCtxFree(alp_tctx);
4343  FLOW_DESTROY(&f);
4344 
4345  PASS;
4346 }
4347 
4348 /**
4349  * \test Tests the parser for handling fragmented records.
4350  */
4351 static int SSLParserTest20(void)
4352 {
4353  Flow f;
4354 
4355  uint8_t buf1[] = {
4356  0x16, 0x03, 0x00, 0x00, 0x03, 0x01, 0x00, 0x00,
4357  0x16, 0x03, 0x00, 0x00, 0x00,
4358  };
4359  uint32_t buf1_len = sizeof(buf1);
4360 
4361  TcpSession ssn;
4363 
4364  memset(&f, 0, sizeof(f));
4365  memset(&ssn, 0, sizeof(ssn));
4366  FLOW_INITIALIZE(&f);
4367  f.protoctx = (void *)&ssn;
4368  f.proto = IPPROTO_TCP;
4369  f.alproto = ALPROTO_TLS;
4370 
4372 
4373  FLOWLOCK_WRLOCK(&f);
4374  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4375  STREAM_TOSERVER, buf1, buf1_len);
4376  FLOWLOCK_UNLOCK(&f);
4377  FAIL_IF(r == 0);
4378 
4379  AppLayerParserThreadCtxFree(alp_tctx);
4381  FLOW_DESTROY(&f);
4382 
4383  PASS;
4384 }
4385 
4386 /**
4387  * \test SSLv2 Record parsing.
4388  */
4389 static int SSLParserTest21(void)
4390 {
4391  Flow f;
4392  uint8_t buf[] = {
4393  0x80, 0x31, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
4394  0x01,
4395  };
4396  uint32_t buf_len = sizeof(buf);
4397 
4398  TcpSession ssn;
4400 
4401  memset(&f, 0, sizeof(f));
4402  memset(&ssn, 0, sizeof(ssn));
4403 
4404  FLOW_INITIALIZE(&f);
4405  f.protoctx = (void *)&ssn;
4406  f.proto = IPPROTO_TCP;
4407  f.alproto = ALPROTO_TLS;
4408 
4410 
4411  FLOWLOCK_WRLOCK(&f);
4412  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4413  STREAM_TOSERVER | STREAM_EOF, buf, buf_len);
4414  FLOWLOCK_UNLOCK(&f);
4415  FAIL_IF(r != 0);
4416 
4417  SSLState *app_state = f.alstate;
4418  FAIL_IF_NULL(app_state);
4419 
4421 
4422  FAIL_IF(app_state->client_connp.version != SSL_VERSION_2);
4423 
4424  AppLayerParserThreadCtxFree(alp_tctx);
4426  FLOW_DESTROY(&f);
4427 
4428  PASS;
4429 }
4430 
4431 /**
4432  * \test SSLv2 Record parsing.
4433  */
4434 static int SSLParserTest22(void)
4435 {
4436  Flow f;
4437  uint8_t buf[] = {
4438  0x80, 0x31, 0x04, 0x00, 0x01, 0x00,
4439  0x02, 0x00, 0x00, 0x00, 0x10, 0x07, 0x00, 0xc0,
4440  0x05, 0x00, 0x80, 0x03, 0x00, 0x80, 0x01, 0x00,
4441  0x80, 0x08, 0x00, 0x80, 0x06, 0x00, 0x40, 0x04,
4442  0x00, 0x80, 0x02, 0x00, 0x80, 0x76, 0x64, 0x75,
4443  0x2d, 0xa7, 0x98, 0xfe, 0xc9, 0x12, 0x92, 0xc1,
4444  0x2f, 0x34, 0x84, 0x20, 0xc5};
4445  uint32_t buf_len = sizeof(buf);
4446  TcpSession ssn;
4448 
4449  //AppLayerDetectProtoThreadInit();
4450 
4451  memset(&f, 0, sizeof(f));
4452  memset(&ssn, 0, sizeof(ssn));
4453 
4454  FLOW_INITIALIZE(&f);
4455  f.protoctx = (void *)&ssn;
4456  f.proto = IPPROTO_TCP;
4457  f.alproto = ALPROTO_TLS;
4458 
4460 
4461  FLOWLOCK_WRLOCK(&f);
4462  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4463  STREAM_TOCLIENT | STREAM_EOF, buf, buf_len);
4464  FLOWLOCK_UNLOCK(&f);
4465  FAIL_IF(r != 0);
4466 
4467  SSLState *app_state = f.alstate;
4468  FAIL_IF_NULL(app_state);
4469 
4471 
4472  FAIL_IF(app_state->server_connp.version != SSL_VERSION_2);
4473 
4474  AppLayerParserThreadCtxFree(alp_tctx);
4476  FLOW_DESTROY(&f);
4477 
4478  PASS;
4479 }
4480 
4481 /**
4482  * \test SSLv2 Record parsing.
4483  */
4484 static int SSLParserTest23(void)
4485 {
4486  Flow f;
4487  uint8_t chello_buf[] = {
4488  0x80, 0x67, 0x01, 0x03, 0x00, 0x00, 0x4e, 0x00,
4489  0x00, 0x00, 0x10, 0x01, 0x00, 0x80, 0x03, 0x00,
4490  0x80, 0x07, 0x00, 0xc0, 0x06, 0x00, 0x40, 0x02,
4491  0x00, 0x80, 0x04, 0x00, 0x80, 0x00, 0x00, 0x39,
4492  0x00, 0x00, 0x38, 0x00, 0x00, 0x35, 0x00, 0x00,
4493  0x33, 0x00, 0x00, 0x32, 0x00, 0x00, 0x04, 0x00,
4494  0x00, 0x05, 0x00, 0x00, 0x2f, 0x00, 0x00, 0x16,
4495  0x00, 0x00, 0x13, 0x00, 0xfe, 0xff, 0x00, 0x00,
4496  0x0a, 0x00, 0x00, 0x15, 0x00, 0x00, 0x12, 0x00,
4497  0xfe, 0xfe, 0x00, 0x00, 0x09, 0x00, 0x00, 0x64,
4498  0x00, 0x00, 0x62, 0x00, 0x00, 0x03, 0x00, 0x00,
4499  0x06, 0xa8, 0xb8, 0x93, 0xbb, 0x90, 0xe9, 0x2a,
4500  0xa2, 0x4d, 0x6d, 0xcc, 0x1c, 0xe7, 0x2a, 0x80,
4501  0x21
4502  };
4503  uint32_t chello_buf_len = sizeof(chello_buf);
4504 
4505  uint8_t shello_buf[] = {
4506  0x16, 0x03, 0x00, 0x00, 0x4a, 0x02,
4507  0x00, 0x00, 0x46, 0x03, 0x00, 0x44, 0x4c, 0x94,
4508  0x8f, 0xfe, 0x81, 0xed, 0x93, 0x65, 0x02, 0x88,
4509  0xa3, 0xf8, 0xeb, 0x63, 0x86, 0x0e, 0x2c, 0xf6,
4510  0x8d, 0xd0, 0x0f, 0x2c, 0x2a, 0xd6, 0x4f, 0xcd,
4511  0x2d, 0x3c, 0x16, 0xd7, 0xd6, 0x20, 0xa0, 0xfb,
4512  0x60, 0x86, 0x3d, 0x1e, 0x76, 0xf3, 0x30, 0xfe,
4513  0x0b, 0x01, 0xfd, 0x1a, 0x01, 0xed, 0x95, 0xf6,
4514  0x7b, 0x8e, 0xc0, 0xd4, 0x27, 0xbf, 0xf0, 0x6e,
4515  0xc7, 0x56, 0xb1, 0x47, 0xce, 0x98, 0x00, 0x35,
4516  0x00, 0x16, 0x03, 0x00, 0x03, 0x44, 0x0b, 0x00,
4517  0x03, 0x40, 0x00, 0x03, 0x3d, 0x00, 0x03, 0x3a,
4518  0x30, 0x82, 0x03, 0x36, 0x30, 0x82, 0x02, 0x9f,
4519  0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01, 0x01,
4520  0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
4521  0xf7, 0x0d, 0x01, 0x01, 0x04, 0x05, 0x00, 0x30,
4522  0x81, 0xa9, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03,
4523  0x55, 0x04, 0x06, 0x13, 0x02, 0x58, 0x59, 0x31,
4524  0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x08,
4525  0x13, 0x0c, 0x53, 0x6e, 0x61, 0x6b, 0x65, 0x20,
4526  0x44, 0x65, 0x73, 0x65, 0x72, 0x74, 0x31, 0x13,
4527  0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13,
4528  0x0a, 0x53, 0x6e, 0x61, 0x6b, 0x65, 0x20, 0x54,
4529  0x6f, 0x77, 0x6e, 0x31, 0x17, 0x30, 0x15, 0x06,
4530  0x03, 0x55, 0x04, 0x0a, 0x13, 0x0e, 0x53, 0x6e,
4531  0x61, 0x6b, 0x65, 0x20, 0x4f, 0x69, 0x6c, 0x2c,
4532  0x20, 0x4c, 0x74, 0x64, 0x31, 0x1e, 0x30, 0x1c,
4533  0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x15, 0x43,
4534  0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61,
4535  0x74, 0x65, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f,
4536  0x72, 0x69, 0x74, 0x79, 0x31, 0x15, 0x30, 0x13,
4537  0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0c, 0x53,
4538  0x6e, 0x61, 0x6b, 0x65, 0x20, 0x4f, 0x69, 0x6c,
4539  0x20, 0x43, 0x41, 0x31, 0x1e, 0x30, 0x1c, 0x06,
4540  0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
4541  0x09, 0x01, 0x16, 0x0f, 0x63, 0x61, 0x40, 0x73,
4542  0x6e, 0x61, 0x6b, 0x65, 0x6f, 0x69, 0x6c, 0x2e,
4543  0x64, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x30,
4544  0x33, 0x30, 0x33, 0x30, 0x35, 0x31, 0x36, 0x34,
4545  0x37, 0x34, 0x35, 0x5a, 0x17, 0x0d, 0x30, 0x38,
4546  0x30, 0x33, 0x30, 0x33, 0x31, 0x36, 0x34, 0x37,
4547  0x34, 0x35, 0x5a, 0x30, 0x81, 0xa7, 0x31, 0x0b,
4548  0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
4549  0x02, 0x58, 0x59, 0x31, 0x15, 0x30, 0x13, 0x06,
4550  0x03, 0x55, 0x04, 0x08, 0x13, 0x0c, 0x53, 0x6e,
4551  0x61, 0x6b, 0x65, 0x20, 0x44, 0x65, 0x73, 0x65,
4552  0x72, 0x74, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
4553  0x55, 0x04, 0x07, 0x13, 0x0a, 0x53, 0x6e, 0x61,
4554  0x6b, 0x65, 0x20, 0x54, 0x6f, 0x77, 0x6e, 0x31,
4555  0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x0a,
4556  0x13, 0x0e, 0x53, 0x6e, 0x61, 0x6b, 0x65, 0x20,
4557  0x4f, 0x69, 0x6c, 0x2c, 0x20, 0x4c, 0x74, 0x64,
4558  0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04,
4559  0x0b, 0x13, 0x0e, 0x57, 0x65, 0x62, 0x73, 0x65,
4560  0x72, 0x76, 0x65, 0x72, 0x20, 0x54, 0x65, 0x61,
4561  0x6d, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55,
4562  0x04, 0x03, 0x13, 0x10, 0x77, 0x77, 0x77, 0x2e,
4563  0x73, 0x6e, 0x61, 0x6b, 0x65, 0x6f, 0x69, 0x6c,
4564  0x2e, 0x64, 0x6f, 0x6d, 0x31, 0x1f, 0x30, 0x1d,
4565  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
4566  0x01, 0x09, 0x01, 0x16, 0x10, 0x77, 0x77, 0x77,
4567  0x40, 0x73, 0x6e, 0x61, 0x6b, 0x65, 0x6f, 0x69,
4568  0x6c, 0x2e, 0x64, 0x6f, 0x6d, 0x30, 0x81, 0x9f,
4569  0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
4570  0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
4571  0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81,
4572  0x81, 0x00, 0xa4, 0x6e, 0x53, 0x14, 0x0a, 0xde,
4573  0x2c, 0xe3, 0x60, 0x55, 0x9a, 0xf2, 0x42, 0xa6,
4574  0xaf, 0x47, 0x12, 0x2f, 0x17, 0xce, 0xfa, 0xba,
4575  0xdc, 0x4e, 0x63, 0x56, 0x34, 0xb9, 0xba, 0x73,
4576  0x4b, 0x78, 0x44, 0x3d, 0xc6, 0x6c, 0x69, 0xa4,
4577  0x25, 0xb3, 0x61, 0x02, 0x9d, 0x09, 0x04, 0x3f,
4578  0x72, 0x3d, 0xd8, 0x27, 0xd3, 0xb0, 0x5a, 0x45,
4579  0x77, 0xb7, 0x36, 0xe4, 0x26, 0x23, 0xcc, 0x12,
4580  0xb8, 0xae, 0xde, 0xa7, 0xb6, 0x3a, 0x82, 0x3c,
4581  0x7c, 0x24, 0x59, 0x0a, 0xf8, 0x96, 0x43, 0x8b,
4582  0xa3, 0x29, 0x36, 0x3f, 0x91, 0x7f, 0x5d, 0xc7,
4583  0x23, 0x94, 0x29, 0x7f, 0x0a, 0xce, 0x0a, 0xbd,
4584  0x8d, 0x9b, 0x2f, 0x19, 0x17, 0xaa, 0xd5, 0x8e,
4585  0xec, 0x66, 0xa2, 0x37, 0xeb, 0x3f, 0x57, 0x53,
4586  0x3c, 0xf2, 0xaa, 0xbb, 0x79, 0x19, 0x4b, 0x90,
4587  0x7e, 0xa7, 0xa3, 0x99, 0xfe, 0x84, 0x4c, 0x89,
4588  0xf0, 0x3d, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3,
4589  0x6e, 0x30, 0x6c, 0x30, 0x1b, 0x06, 0x03, 0x55,
4590  0x1d, 0x11, 0x04, 0x14, 0x30, 0x12, 0x81, 0x10,
4591  0x77, 0x77, 0x77, 0x40, 0x73, 0x6e, 0x61, 0x6b,
4592  0x65, 0x6f, 0x69, 0x6c, 0x2e, 0x64, 0x6f, 0x6d,
4593  0x30, 0x3a, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
4594  0x86, 0xf8, 0x42, 0x01, 0x0d, 0x04, 0x2d, 0x16,
4595  0x2b, 0x6d, 0x6f, 0x64, 0x5f, 0x73, 0x73, 0x6c,
4596  0x20, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74,
4597  0x65, 0x64, 0x20, 0x63, 0x75, 0x73, 0x74, 0x6f,
4598  0x6d, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72,
4599  0x20, 0x63, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69,
4600  0x63, 0x61, 0x74, 0x65, 0x30, 0x11, 0x06, 0x09,
4601  0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x01,
4602  0x01, 0x04, 0x04, 0x03, 0x02, 0x06, 0x40, 0x30,
4603  0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
4604  0x0d, 0x01, 0x01, 0x04, 0x05, 0x00, 0x03, 0x81,
4605  0x81, 0x00, 0xae, 0x79, 0x79, 0x22, 0x90, 0x75,
4606  0xfd, 0xa6, 0xd5, 0xc4, 0xb8, 0xc4, 0x99, 0x4e,
4607  0x1c, 0x05, 0x7c, 0x91, 0x59, 0xbe, 0x89, 0x0d,
4608  0x3d, 0xc6, 0x8c, 0xa3, 0xcf, 0xf6, 0xba, 0x23,
4609  0xdf, 0xb8, 0xae, 0x44, 0x68, 0x8a, 0x8f, 0xb9,
4610  0x8b, 0xcb, 0x12, 0xda, 0xe6, 0xa2, 0xca, 0xa5,
4611  0xa6, 0x55, 0xd9, 0xd2, 0xa1, 0xad, 0xba, 0x9b,
4612  0x2c, 0x44, 0x95, 0x1d, 0x4a, 0x90, 0x59, 0x7f,
4613  0x83, 0xae, 0x81, 0x5e, 0x3f, 0x92, 0xe0, 0x14,
4614  0x41, 0x82, 0x4e, 0x7f, 0x53, 0xfd, 0x10, 0x23,
4615  0xeb, 0x8a, 0xeb, 0xe9, 0x92, 0xea, 0x61, 0xf2,
4616  0x8e, 0x19, 0xa1, 0xd3, 0x49, 0xc0, 0x84, 0x34,
4617  0x1e, 0x2e, 0x6e, 0xf6, 0x98, 0xe2, 0x87, 0x53,
4618  0xd6, 0x55, 0xd9, 0x1a, 0x8a, 0x92, 0x5c, 0xad,
4619  0xdc, 0x1e, 0x1c, 0x30, 0xa7, 0x65, 0x9d, 0xc2,
4620  0x4f, 0x60, 0xd2, 0x6f, 0xdb, 0xe0, 0x9f, 0x9e,
4621  0xbc, 0x41, 0x16, 0x03, 0x00, 0x00, 0x04, 0x0e,
4622  0x00, 0x00, 0x00
4623  };
4624  uint32_t shello_buf_len = sizeof(shello_buf);
4625 
4626  uint8_t client_change_cipher_spec_buf[] = {
4627  0x16, 0x03, 0x00, 0x00, 0x84, 0x10, 0x00, 0x00,
4628  0x80, 0x65, 0x51, 0x2d, 0xa6, 0xd4, 0xa7, 0x38,
4629  0xdf, 0xac, 0x79, 0x1f, 0x0b, 0xd9, 0xb2, 0x61,
4630  0x7d, 0x73, 0x88, 0x32, 0xd9, 0xf2, 0x62, 0x3a,
4631  0x8b, 0x11, 0x04, 0x75, 0xca, 0x42, 0xff, 0x4e,
4632  0xd9, 0xcc, 0xb9, 0xfa, 0x86, 0xf3, 0x16, 0x2f,
4633  0x09, 0x73, 0x51, 0x66, 0xaa, 0x29, 0xcd, 0x80,
4634  0x61, 0x0f, 0xe8, 0x13, 0xce, 0x5b, 0x8e, 0x0a,
4635  0x23, 0xf8, 0x91, 0x5e, 0x5f, 0x54, 0x70, 0x80,
4636  0x8e, 0x7b, 0x28, 0xef, 0xb6, 0x69, 0xb2, 0x59,
4637  0x85, 0x74, 0x98, 0xe2, 0x7e, 0xd8, 0xcc, 0x76,
4638  0x80, 0xe1, 0xb6, 0x45, 0x4d, 0xc7, 0xcd, 0x84,
4639  0xce, 0xb4, 0x52, 0x79, 0x74, 0xcd, 0xe6, 0xd7,
4640  0xd1, 0x9c, 0xad, 0xef, 0x63, 0x6c, 0x0f, 0xf7,
4641  0x05, 0xe4, 0x4d, 0x1a, 0xd3, 0xcb, 0x9c, 0xd2,
4642  0x51, 0xb5, 0x61, 0xcb, 0xff, 0x7c, 0xee, 0xc7,
4643  0xbc, 0x5e, 0x15, 0xa3, 0xf2, 0x52, 0x0f, 0xbb,
4644  0x32, 0x14, 0x03, 0x00, 0x00, 0x01, 0x01, 0x16,
4645  0x03, 0x00, 0x00, 0x40, 0xa9, 0xd8, 0xd7, 0x35,
4646  0xbc, 0x39, 0x56, 0x98, 0xad, 0x87, 0x61, 0x2a,
4647  0xc4, 0x8f, 0xcc, 0x03, 0xcb, 0x93, 0x80, 0x81,
4648  0xb0, 0x4a, 0xc4, 0xd2, 0x09, 0x71, 0x3e, 0x90,
4649  0x3c, 0x8d, 0xe0, 0x95, 0x44, 0xfe, 0x56, 0xd1,
4650  0x7e, 0x88, 0xe2, 0x48, 0xfd, 0x76, 0x70, 0x76,
4651  0xe2, 0xcd, 0x06, 0xd0, 0xf3, 0x9d, 0x13, 0x79,
4652  0x67, 0x1e, 0x37, 0xf6, 0x98, 0xbe, 0x59, 0x18,
4653  0x4c, 0xfc, 0x75, 0x56
4654  };
4655  uint32_t client_change_cipher_spec_buf_len =
4656  sizeof(client_change_cipher_spec_buf);
4657 
4658  uint8_t server_change_cipher_spec_buf[] = {
4659  0x14, 0x03, 0x00, 0x00, 0x01, 0x01, 0x16, 0x03,
4660  0x00, 0x00, 0x40, 0xce, 0x7c, 0x92, 0x43, 0x59,
4661  0xcc, 0x3d, 0x90, 0x91, 0x9c, 0x58, 0xf0, 0x7a,
4662  0xce, 0xae, 0x0d, 0x08, 0xe0, 0x76, 0xb4, 0x86,
4663  0xb1, 0x15, 0x5b, 0x32, 0xb8, 0x77, 0x53, 0xe7,
4664  0xa6, 0xf9, 0xd0, 0x95, 0x5f, 0xaa, 0x07, 0xc3,
4665  0x96, 0x7c, 0xc9, 0x88, 0xc2, 0x7a, 0x20, 0x89,
4666  0x4f, 0xeb, 0xeb, 0xb6, 0x19, 0xef, 0xaa, 0x27,
4667  0x73, 0x9d, 0xa6, 0xb4, 0x9f, 0xeb, 0x34, 0xe2,
4668  0x4d, 0x9f, 0x6b
4669  };
4670  uint32_t server_change_cipher_spec_buf_len =
4671  sizeof(server_change_cipher_spec_buf);
4672 
4673  uint8_t toserver_app_data_buf[] = {
4674  0x17, 0x03, 0x00, 0x01, 0xb0, 0x4a, 0xc3, 0x3e,
4675  0x9d, 0x77, 0x78, 0x01, 0x2c, 0xb4, 0xbc, 0x4c,
4676  0x9a, 0x84, 0xd7, 0xb9, 0x90, 0x0c, 0x21, 0x10,
4677  0xf0, 0xfa, 0x00, 0x7c, 0x16, 0xbb, 0x77, 0xfb,
4678  0x72, 0x42, 0x4f, 0xad, 0x50, 0x4a, 0xd0, 0xaa,
4679  0x6f, 0xaa, 0x44, 0x6c, 0x62, 0x94, 0x1b, 0xc5,
4680  0xfe, 0xe9, 0x1c, 0x5e, 0xde, 0x85, 0x0b, 0x0e,
4681  0x05, 0xe4, 0x18, 0x6e, 0xd2, 0xd3, 0xb5, 0x20,
4682  0xab, 0x81, 0xfd, 0x18, 0x9a, 0x73, 0xb8, 0xd7,
4683  0xef, 0xc3, 0xdd, 0x74, 0xd7, 0x9c, 0x1e, 0x6f,
4684  0x21, 0x6d, 0xf8, 0x24, 0xca, 0x3c, 0x70, 0x78,
4685  0x36, 0x12, 0x7a, 0x8a, 0x9c, 0xac, 0x4e, 0x1c,
4686  0xa8, 0xfb, 0x27, 0x30, 0xba, 0x9a, 0xf4, 0x2f,
4687  0x0a, 0xab, 0x80, 0x6a, 0xa1, 0x60, 0x74, 0xf0,
4688  0xe3, 0x91, 0x84, 0xe7, 0x90, 0x88, 0xcc, 0xf0,
4689  0x95, 0x7b, 0x0a, 0x22, 0xf2, 0xf9, 0x27, 0xe0,
4690  0xdd, 0x38, 0x0c, 0xfd, 0xe9, 0x03, 0x71, 0xdc,
4691  0x70, 0xa4, 0x6e, 0xdf, 0xe3, 0x72, 0x9e, 0xa1,
4692  0xf0, 0xc9, 0x00, 0xd6, 0x03, 0x55, 0x6a, 0x67,
4693  0x5d, 0x9c, 0xb8, 0x75, 0x01, 0xb0, 0x01, 0x9f,
4694  0xe6, 0xd2, 0x44, 0x18, 0xbc, 0xca, 0x7a, 0x10,
4695  0x39, 0xa6, 0xcf, 0x15, 0xc7, 0xf5, 0x35, 0xd4,
4696  0xb3, 0x6d, 0x91, 0x23, 0x84, 0x99, 0xba, 0xb0,
4697  0x7e, 0xd0, 0xc9, 0x4c, 0xbf, 0x3f, 0x33, 0x68,
4698  0x37, 0xb7, 0x7d, 0x44, 0xb0, 0x0b, 0x2c, 0x0f,
4699  0xd0, 0x75, 0xa2, 0x6b, 0x5b, 0xe1, 0x9f, 0xd4,
4700  0x69, 0x9a, 0x14, 0xc8, 0x29, 0xb7, 0xd9, 0x10,
4701  0xbb, 0x99, 0x30, 0x9a, 0xfb, 0xcc, 0x13, 0x1f,
4702  0x76, 0x4e, 0xe6, 0xdf, 0x14, 0xaa, 0xd5, 0x60,
4703  0xbf, 0x91, 0x49, 0x0d, 0x64, 0x42, 0x29, 0xa8,
4704  0x64, 0x27, 0xd4, 0x5e, 0x1b, 0x18, 0x03, 0xa8,
4705  0x73, 0xd6, 0x05, 0x6e, 0xf7, 0x50, 0xb0, 0x09,
4706  0x6b, 0x69, 0x7a, 0x12, 0x28, 0x58, 0xef, 0x5a,
4707  0x86, 0x11, 0xde, 0x71, 0x71, 0x9f, 0xca, 0xbd,
4708  0x79, 0x2a, 0xc2, 0xe5, 0x9b, 0x5e, 0x32, 0xe7,
4709  0xcb, 0x97, 0x6e, 0xa0, 0xea, 0xa4, 0xa4, 0x6a,
4710  0x32, 0xf9, 0x37, 0x39, 0xd8, 0x37, 0x6d, 0x63,
4711  0xf3, 0x08, 0x1c, 0xdd, 0x06, 0xdd, 0x2c, 0x2b,
4712  0x9f, 0x04, 0x88, 0x5f, 0x36, 0x42, 0xc1, 0xb1,
4713  0xc7, 0xe8, 0x2d, 0x5d, 0xa4, 0x6c, 0xe5, 0x60,
4714  0x94, 0xae, 0xd0, 0x90, 0x1e, 0x88, 0xa0, 0x87,
4715  0x52, 0xfb, 0xed, 0x97, 0xa5, 0x25, 0x5a, 0xb7,
4716  0x55, 0xc5, 0x13, 0x07, 0x85, 0x27, 0x40, 0xed,
4717  0xb8, 0xa0, 0x26, 0x13, 0x44, 0x0c, 0xfc, 0xcc,
4718  0x5a, 0x09, 0xe5, 0x44, 0xb5, 0x63, 0xa1, 0x43,
4719  0x51, 0x23, 0x4f, 0x17, 0x21, 0x89, 0x2e, 0x58,
4720  0xfd, 0xf9, 0x63, 0x74, 0x04, 0x70, 0x1e, 0x7d,
4721  0xd0, 0x66, 0xba, 0x40, 0x5e, 0x45, 0xdc, 0x39,
4722  0x7c, 0x53, 0x0f, 0xa8, 0x38, 0xb2, 0x13, 0x99,
4723  0x27, 0xd9, 0x4a, 0x51, 0xe9, 0x9f, 0x2a, 0x92,
4724  0xbb, 0x9c, 0x90, 0xab, 0xfd, 0xf1, 0xb7, 0x40,
4725  0x05, 0xa9, 0x7a, 0x20, 0x63, 0x36, 0xc1, 0xef,
4726  0xb9, 0xad, 0xa2, 0xe0, 0x1d, 0x20, 0x4f, 0xb2,
4727  0x34, 0xbd, 0xea, 0x07, 0xac, 0x21, 0xce, 0xf6,
4728  0x8a, 0xa2, 0x9e, 0xcd, 0xfa
4729  };
4730  uint32_t toserver_app_data_buf_len = sizeof(toserver_app_data_buf);
4731 
4732  TcpSession ssn;
4734 
4735  //AppLayerDetectProtoThreadInit();
4736 
4737  memset(&f, 0, sizeof(f));
4738  memset(&ssn, 0, sizeof(ssn));
4739 
4740  FLOW_INITIALIZE(&f);
4741  f.protoctx = (void *)&ssn;
4742  f.proto = IPPROTO_TCP;
4743  f.alproto = ALPROTO_TLS;
4744 
4746 
4747  FLOWLOCK_WRLOCK(&f);
4748  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4749  STREAM_TOSERVER | STREAM_START, chello_buf,
4750  chello_buf_len);
4751  FLOWLOCK_UNLOCK(&f);
4752  FAIL_IF(r != 0);
4753 
4754  SSLState *app_state = f.alstate;
4755  FAIL_IF_NULL(app_state);
4756 
4758 
4759  FAIL_IF(app_state->client_connp.version != SSL_VERSION_2);
4760 
4761  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) == 0);
4762  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) == 0);
4763  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) == 0);
4764 
4765  FLOWLOCK_WRLOCK(&f);
4766  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT,
4767  shello_buf, shello_buf_len);
4768  FLOWLOCK_UNLOCK(&f);
4769  FAIL_IF(r != 0);
4770 
4772 
4773  FAIL_IF(app_state->server_connp.version != SSL_VERSION_3);
4774 
4775  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) == 0);
4776  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) == 0);
4777  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) == 0);
4778  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0);
4779 
4780  FLOWLOCK_WRLOCK(&f);
4781  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4782  client_change_cipher_spec_buf,
4783  client_change_cipher_spec_buf_len);
4784  FLOWLOCK_UNLOCK(&f);
4785  FAIL_IF(r != 0);
4786 
4787  /* with multiple records the client content type hold the type from the last
4788  * record */
4790 
4791  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) == 0);
4792  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) == 0);
4793  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) == 0);
4794  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0);
4795  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_KEYX) == 0);
4797  FAIL_IF((app_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) == 0);
4798 
4799  FLOWLOCK_WRLOCK(&f);
4800  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT,
4801  server_change_cipher_spec_buf,
4802  server_change_cipher_spec_buf_len);
4803  FLOWLOCK_UNLOCK(&f);
4804  FAIL_IF(r != 0);
4805 
4806  /* with multiple records the serve content type hold the type from the last
4807  * record */
4809 
4810  FAIL_IF(app_state->server_connp.version != SSL_VERSION_3);
4811 
4812  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) == 0);
4813  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) == 0);
4814  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) == 0);
4815  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0);
4816  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_KEYX) == 0);
4819  FAIL_IF((app_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) == 0);
4820 
4821  FLOWLOCK_WRLOCK(&f);
4822  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4823  toserver_app_data_buf, toserver_app_data_buf_len);
4824  FLOWLOCK_UNLOCK(&f);
4825  FAIL_IF(r != 0);
4826 
4828 
4829  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) == 0);
4830  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) == 0);
4831  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) == 0);
4832  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0);
4833  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_KEYX) == 0);
4836  FAIL_IF((app_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) == 0);
4837 
4839 
4840  if (alp_tctx != NULL)
4841  AppLayerParserThreadCtxFree(alp_tctx);
4843  FLOW_DESTROY(&f);
4844 
4845  PASS;
4846 }
4847 
4848 /**
4849  * \test Tests the parser for handling fragmented records.
4850  */
4851 static int SSLParserTest24(void)
4852 {
4853  Flow f;
4854  uint8_t buf1[] = {
4855  0x16, 0x03, 0x00, 0x00, 0x6f, 0x01, 0x00, 0x00,
4856  0x6b, 0x03,
4857  };
4858  uint32_t buf1_len = sizeof(buf1);
4859 
4860  uint8_t buf2[] = {
4861  0x00, 0x4b, 0x2f, 0xdc,
4862  0x4e, 0xe6, 0x95, 0xf1, 0xa0, 0xc7, 0xcf, 0x8e,
4863  0xf6, 0xeb, 0x22, 0x6d, 0xce, 0x9c, 0x44, 0xfb,
4864  0xc8, 0xa0, 0x44, 0x31, 0x15, 0x4c, 0xe9, 0x97,
4865  0xa7, 0xa1, 0xfe, 0xea, 0xcc, 0x20, 0x4b, 0x5d,
4866  0xfb, 0xa5, 0x63, 0x7a, 0x73, 0x95, 0xf7, 0xff,
4867  0x42, 0xac, 0x8f, 0x46, 0xed, 0xe4, 0xb1, 0x35,
4868  0x35, 0x78, 0x1a, 0x9d, 0xaf, 0x10, 0xc5, 0x52,
4869  0xf3, 0x7b, 0xfb, 0xb5, 0xe9, 0xa8, 0x00, 0x24,
4870  0x00, 0x88, 0x00, 0x87, 0x00, 0x39, 0x00, 0x38,
4871  0x00, 0x84, 0x00, 0x35, 0x00, 0x45, 0x00, 0x44,
4872  0x00, 0x33, 0x00, 0x32, 0x00, 0x96, 0x00, 0x41,
4873  0x00, 0x2f, 0x00, 0x16, 0x00, 0x13, 0xfe, 0xff,
4874  0x00, 0x0a, 0x00, 0x02, 0x01, 0x00
4875  };
4876  uint32_t buf2_len = sizeof(buf2);
4877  TcpSession ssn;
4879 
4880  memset(&f, 0, sizeof(f));
4881  memset(&ssn, 0, sizeof(ssn));
4882  FLOW_INITIALIZE(&f);
4883  f.protoctx = (void *)&ssn;
4884  f.proto = IPPROTO_TCP;
4885  f.alproto = ALPROTO_TLS;
4886 
4888 
4889  FLOWLOCK_WRLOCK(&f);
4890  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4891  STREAM_TOSERVER, buf1, buf1_len);
4892  FLOWLOCK_UNLOCK(&f);
4893  FAIL_IF(r != 0);
4894 
4895  FLOWLOCK_WRLOCK(&f);
4896  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4897  buf2, buf2_len);
4898  FLOWLOCK_UNLOCK(&f);
4899  FAIL_IF(r != 0);
4900 
4901  SSLState *ssl_state = f.alstate;
4902  FAIL_IF_NULL(ssl_state);
4903 
4904  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
4905 
4906  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
4907 
4908  AppLayerParserThreadCtxFree(alp_tctx);
4910  FLOW_DESTROY(&f);
4911 
4912  PASS;
4913 }
4914 
4915 /**
4916  * \test Test for bug #955 and CVE-2013-5919. The data is from the
4917  * pcap that was used to report this issue.
4918  */
4919 static int SSLParserTest25(void)
4920 {
4921  Flow f;
4922  uint8_t client_hello[] = {
4923  0x16, 0x03, 0x01, 0x00, 0xd3, 0x01, 0x00, 0x00,
4924  0xcf, 0x03, 0x01, 0x51, 0x60, 0xc2, 0x15, 0x36,
4925  0x73, 0xf5, 0xb8, 0x58, 0x55, 0x3b, 0x68, 0x12,
4926  0x7d, 0xe3, 0x28, 0xa3, 0xe1, 0x02, 0x79, 0x2d,
4927  0x12, 0xe1, 0xf4, 0x24, 0x12, 0xa2, 0x9e, 0xf1,
4928  0x08, 0x49, 0x68, 0x20, 0x0e, 0x96, 0x46, 0x3d,
4929  0x84, 0x5a, 0xc6, 0x55, 0xeb, 0x3b, 0x53, 0x77,
4930  0xf4, 0x8e, 0xf4, 0xd2, 0x8b, 0xec, 0xd6, 0x99,
4931  0x63, 0x64, 0x62, 0xf8, 0x3f, 0x3b, 0xd5, 0x35,
4932  0x45, 0x1b, 0x16, 0xac, 0x00, 0x46, 0x00, 0x04,
4933  0x00, 0x05, 0x00, 0x2f, 0x00, 0x35, 0xc0, 0x02,
4934  0xc0, 0x04, 0xc0, 0x05, 0xc0, 0x0c, 0xc0, 0x0e,
4935  0xc0, 0x0f, 0xc0, 0x07, 0xc0, 0x09, 0xc0, 0x0a,
4936  0xc0, 0x11, 0xc0, 0x13, 0xc0, 0x14, 0x00, 0x33,
4937  0x00, 0x39, 0x00, 0x32, 0x00, 0x38, 0x00, 0x0a,
4938  0xc0, 0x03, 0xc0, 0x0d, 0xc0, 0x08, 0xc0, 0x12,
4939  0x00, 0x16, 0x00, 0x13, 0x00, 0x09, 0x00, 0x15,
4940  0x00, 0x12, 0x00, 0x03, 0x00, 0x08, 0x00, 0x14,
4941  0x00, 0x11, 0x00, 0xff, 0x01, 0x00, 0x00, 0x40,
4942  0x00, 0x0b, 0x00, 0x04, 0x03, 0x00, 0x01, 0x02,
4943  0x00, 0x0a, 0x00, 0x34, 0x00, 0x32, 0x00, 0x0e,
4944  0x00, 0x0d, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x0c,
4945  0x00, 0x18, 0x00, 0x09, 0x00, 0x0a, 0x00, 0x16,
4946  0x00, 0x17, 0x00, 0x08, 0x00, 0x06, 0x00, 0x07,
4947  0x00, 0x14, 0x00, 0x15, 0x00, 0x04, 0x00, 0x05,
4948  0x00, 0x12, 0x00, 0x13, 0x00, 0x01, 0x00, 0x02,
4949  0x00, 0x03, 0x00, 0x0f, 0x00, 0x10, 0x00, 0x11
4950  };
4951  uint32_t client_hello_len = sizeof(client_hello);
4952 
4953  uint8_t server_hello_certificate_done[] = {
4954  0x16, 0x03, 0x01, 0x00, 0x51, 0x02, 0x00, 0x00,
4955  0x4d, 0x03, 0x01, 0x51, 0x60, 0xc2, 0x17, 0xb7,
4956  0x81, 0xaa, 0x27, 0xa1, 0xd5, 0xfa, 0x14, 0xc1,
4957  0xe0, 0x05, 0xab, 0x75, 0xf2, 0x51, 0xe7, 0x6e,
4958  0xe6, 0xf9, 0xc4, 0x8f, 0x16, 0x08, 0x26, 0x6c,
4959  0x1b, 0x86, 0x90, 0x20, 0x0a, 0x38, 0x90, 0x2d,
4960  0x17, 0x7d, 0xb7, 0x6b, 0x6b, 0xe5, 0xeb, 0x61,
4961  0x90, 0x35, 0xf8, 0xcd, 0xb1, 0x2a, 0x69, 0x6e,
4962  0x0e, 0x3e, 0x5f, 0x90, 0xdc, 0x2f, 0x51, 0x45,
4963  0x68, 0x63, 0xe3, 0xb3, 0x00, 0x05, 0x00, 0x00,
4964  0x05, 0xff, 0x01, 0x00, 0x01, 0x00, 0x16, 0x03,
4965  0x01, 0x07, 0x60, 0x0b, 0x00, 0x07, 0x5c, 0x00,
4966  0x07, 0x59, 0x00, 0x03, 0xcc, 0x30, 0x82, 0x03,
4967  0xc8, 0x30, 0x82, 0x03, 0x31, 0xa0, 0x03, 0x02,
4968  0x01, 0x02, 0x02, 0x10, 0x01, 0x7f, 0x77, 0xde,
4969  0xb3, 0xbc, 0xbb, 0x23, 0x5d, 0x44, 0xcc, 0xc7,
4970  0xdb, 0xa6, 0x2e, 0x72, 0x30, 0x0d, 0x06, 0x09,
4971  0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
4972  0x05, 0x05, 0x00, 0x30, 0x81, 0xba, 0x31, 0x1f,
4973  0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13,
4974  0x16, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67,
4975  0x6e, 0x20, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20,
4976  0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x31,
4977  0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x0b,
4978  0x13, 0x0e, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69,
4979  0x67, 0x6e, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e,
4980  0x31, 0x33, 0x30, 0x31, 0x06, 0x03, 0x55, 0x04,
4981  0x0b, 0x13, 0x2a, 0x56, 0x65, 0x72, 0x69, 0x53,
4982  0x69, 0x67, 0x6e, 0x20, 0x49, 0x6e, 0x74, 0x65,
4983  0x72, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x61,
4984  0x6c, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
4985  0x20, 0x43, 0x41, 0x20, 0x2d, 0x20, 0x43, 0x6c,
4986  0x61, 0x73, 0x73, 0x20, 0x33, 0x31, 0x49, 0x30,
4987  0x47, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x40,
4988  0x77, 0x77, 0x77, 0x2e, 0x76, 0x65, 0x72, 0x69,
4989  0x73, 0x69, 0x67, 0x6e, 0x2e, 0x63, 0x6f, 0x6d,
4990  0x2f, 0x43, 0x50, 0x53, 0x20, 0x49, 0x6e, 0x63,
4991  0x6f, 0x72, 0x70, 0x2e, 0x62, 0x79, 0x20, 0x52,
4992  0x65, 0x66, 0x2e, 0x20, 0x4c, 0x49, 0x41, 0x42,
4993  0x49, 0x4c, 0x49, 0x54, 0x59, 0x20, 0x4c, 0x54,
4994  0x44, 0x2e, 0x28, 0x63, 0x29, 0x39, 0x37, 0x20,
4995  0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e,
4996  0x30, 0x1e, 0x17, 0x0d, 0x31, 0x32, 0x30, 0x36,
4997  0x32, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
4998  0x5a, 0x17, 0x0d, 0x31, 0x33, 0x31, 0x32, 0x33,
4999  0x31, 0x32, 0x33, 0x35, 0x39, 0x35, 0x39, 0x5a,
5000  0x30, 0x68, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03,
5001  0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
5002  0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08,
5003  0x13, 0x0a, 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f,
5004  0x72, 0x6e, 0x69, 0x61, 0x31, 0x12, 0x30, 0x10,
5005  0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x09, 0x50,
5006  0x61, 0x6c, 0x6f, 0x20, 0x41, 0x6c, 0x74, 0x6f,
5007  0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04,
5008  0x0a, 0x13, 0x0e, 0x46, 0x61, 0x63, 0x65, 0x62,
5009  0x6f, 0x6f, 0x6b, 0x2c, 0x20, 0x49, 0x6e, 0x63,
5010  0x2e, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55,
5011  0x04, 0x02, 0x14, 0x0e, 0x2a, 0x2e, 0x66, 0x61,
5012  0x63, 0x65, 0x62, 0x6f, 0x6f, 0x6b, 0x2e, 0x63,
5013  0x6f, 0x6d, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06,
5014  0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
5015  0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00,
5016  0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xae,
5017  0x94, 0xb1, 0x71, 0xe2, 0xde, 0xcc, 0xc1, 0x69,
5018  0x3e, 0x05, 0x10, 0x63, 0x24, 0x01, 0x02, 0xe0,
5019  0x68, 0x9a, 0xe8, 0x3c, 0x39, 0xb6, 0xb3, 0xe7,
5020  0x4b, 0x97, 0xd4, 0x8d, 0x7b, 0x23, 0x68, 0x91,
5021  0x00, 0xb0, 0xb4, 0x96, 0xee, 0x62, 0xf0, 0xe6,
5022  0xd3, 0x56, 0xbc, 0xf4, 0xaa, 0x0f, 0x50, 0x64,
5023  0x34, 0x02, 0xf5, 0xd1, 0x76, 0x6a, 0xa9, 0x72,
5024  0x83, 0x5a, 0x75, 0x64, 0x72, 0x3f, 0x39, 0xbb,
5025  0xef, 0x52, 0x90, 0xde, 0xd9, 0xbc, 0xdb, 0xf9,
5026  0xd3, 0xd5, 0x5d, 0xfa, 0xd2, 0x3a, 0xa0, 0x3d,
5027  0xc6, 0x04, 0xc5, 0x4d, 0x29, 0xcf, 0x1d, 0x4b,
5028  0x3b, 0xdb, 0xd1, 0xa8, 0x09, 0xcf, 0xae, 0x47,
5029  0xb4, 0x4c, 0x7e, 0xae, 0x17, 0xc5, 0x10, 0x9b,
5030  0xee, 0x24, 0xa9, 0xcf, 0x4a, 0x8d, 0x91, 0x1b,
5031  0xb0, 0xfd, 0x04, 0x15, 0xae, 0x4c, 0x3f, 0x43,
5032  0x0a, 0xa1, 0x2a, 0x55, 0x7e, 0x2a, 0xe1, 0x02,
5033  0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, 0x01, 0x1e,
5034  0x30, 0x82, 0x01, 0x1a, 0x30, 0x09, 0x06, 0x03,
5035  0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30,
5036  0x44, 0x06, 0x03, 0x55, 0x1d, 0x20, 0x04, 0x3d,
5037  0x30, 0x3b, 0x30, 0x39, 0x06, 0x0b, 0x60, 0x86,
5038  0x48, 0x01, 0x86, 0xf8, 0x45, 0x01, 0x07, 0x17,
5039  0x03, 0x30, 0x2a, 0x30, 0x28, 0x06, 0x08, 0x2b,
5040  0x06, 0x01, 0x05, 0x05, 0x07, 0x00, 0x01, 0x16,
5041  0x1c, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
5042  0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x65, 0x72,
5043  0x69, 0x73, 0x69, 0x67, 0x6e, 0x2e, 0x63, 0x6f,
5044  0x6d, 0x2f, 0x72, 0x70, 0x61, 0x30, 0x3c, 0x06,
5045  0x03, 0x55, 0x1d, 0x1f, 0x04, 0x35, 0x30, 0x33,
5046  0x30, 0x31, 0xa0, 0x2f, 0xa0, 0x2d, 0x86, 0x2b,
5047  0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x53,
5048  0x56, 0x52, 0x49, 0x6e, 0x74, 0x6c, 0x2d, 0x63,
5049  0x72, 0x6c, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x73,
5050  0x69, 0x67, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
5051  0x53, 0x56, 0x52, 0x49, 0x6e, 0x74, 0x6c, 0x2e,
5052  0x63, 0x72, 0x6c, 0x30, 0x1d, 0x06, 0x03, 0x55,
5053  0x1d, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06, 0x08,
5054  0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01,
5055  0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07,
5056  0x03, 0x02, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d,
5057  0x0f, 0x04, 0x04, 0x03, 0x02, 0x05, 0xa0, 0x30,
5058  0x34, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05,
5059  0x07, 0x01, 0x01, 0x04, 0x28, 0x30, 0x26, 0x30,
5060  0x24, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05,
5061  0x07, 0x30, 0x01, 0x86, 0x18, 0x68, 0x74, 0x74,
5062  0x70, 0x3a, 0x2f, 0x2f, 0x6f, 0x63, 0x73, 0x70,
5063  0x2e, 0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67,
5064  0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x27, 0x06,
5065  0x03, 0x55, 0x1d, 0x11, 0x04, 0x20, 0x30, 0x1e,
5066  0x82, 0x0e, 0x2a, 0x2e, 0x66, 0x61, 0x63, 0x65,
5067  0x62, 0x6f, 0x6f, 0x6b, 0x2e, 0x63, 0x6f, 0x6d,
5068  0x82, 0x0c, 0x66, 0x61, 0x63, 0x65, 0x62, 0x6f,
5069  0x6f, 0x6b, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x0d,
5070  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
5071  0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x81, 0x81,
5072  0x00, 0x5b, 0x6c, 0x2b, 0x75, 0xf8, 0xed, 0x30,
5073  0xaa, 0x51, 0xaa, 0xd3, 0x6a, 0xba, 0x59, 0x5e,
5074  0x55, 0x51, 0x41, 0x95, 0x1f, 0x81, 0xa5, 0x3b,
5075  0x44, 0x79, 0x10, 0xac, 0x1f, 0x76, 0xff, 0x78,
5076  0xfc, 0x27, 0x81, 0x61, 0x6b, 0x58, 0xf3, 0x12,
5077  0x2a, 0xfc, 0x1c, 0x87, 0x01, 0x04, 0x25, 0xe9,
5078  0xed, 0x43, 0xdf, 0x1a, 0x7b, 0xa6, 0x49, 0x80,
5079  0x60, 0x67, 0xe2, 0x68, 0x8a, 0xf0, 0x3d, 0xb5,
5080  0x8c, 0x7d, 0xf4, 0xee, 0x03, 0x30, 0x9a, 0x6a,
5081  0xfc, 0x24, 0x7c, 0xcb, 0x13, 0x4d, 0xc3, 0x3e,
5082  0x54, 0xc6, 0xbc, 0x1d, 0x51, 0x33, 0xa5, 0x32,
5083  0xa7, 0x32, 0x73, 0xb1, 0xd7, 0x9c, 0xad, 0xc0,
5084  0x8e, 0x7e, 0x1a, 0x83, 0x11, 0x6d, 0x34, 0x52,
5085  0x33, 0x40, 0xb0, 0x30, 0x54, 0x27, 0xa2, 0x17,
5086  0x42, 0x82, 0x7c, 0x98, 0x91, 0x66, 0x98, 0xee,
5087  0x7e, 0xaf, 0x8c, 0x3b, 0xdd, 0x71, 0x70, 0x08,
5088  0x17, 0x00, 0x03, 0x87, 0x30, 0x82, 0x03, 0x83,
5089  0x30, 0x82, 0x02, 0xec, 0xa0, 0x03, 0x02, 0x01,
5090  0x02, 0x02, 0x10, 0x46, 0xfc, 0xeb, 0xba, 0xb4,
5091  0xd0, 0x2f, 0x0f, 0x92, 0x60, 0x98, 0x23, 0x3f,
5092  0x93, 0x07, 0x8f, 0x30, 0x0d, 0x06, 0x09, 0x2a,
5093  0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05,
5094  0x05, 0x00, 0x30, 0x5f, 0x31, 0x0b, 0x30, 0x09,
5095  0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
5096  0x53, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55,
5097  0x04, 0x0a, 0x13, 0x0e, 0x56, 0x65, 0x72, 0x69,
5098  0x53, 0x69, 0x67, 0x6e, 0x2c, 0x20, 0x49, 0x6e,
5099  0x63, 0x2e, 0x31, 0x37, 0x30, 0x35, 0x06, 0x03,
5100  0x55, 0x04, 0x0b, 0x13, 0x2e, 0x43, 0x6c, 0x61,
5101  0x73, 0x73, 0x20, 0x33, 0x20, 0x50, 0x75, 0x62,
5102  0x6c, 0x69, 0x63, 0x20, 0x50, 0x72, 0x69, 0x6d,
5103  0x61, 0x72, 0x79, 0x20, 0x43, 0x65, 0x72, 0x74,
5104  0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
5105  0x6e, 0x20, 0x41, 0x75, 0x64, 0x68, 0x6f, 0x72,
5106  0x69, 0x74, 0x79, 0x30, 0x1e, 0x17, 0x0d, 0x39,
5107  0x37, 0x30, 0x34, 0x31, 0x37, 0x30, 0x30, 0x30,
5108  0x30, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x31, 0x36,
5109  0x31, 0x30, 0x32, 0x34, 0x32, 0x33, 0x35, 0x39,
5110  0x35, 0x39, 0x5a, 0x30, 0x81, 0xba, 0x31, 0x1f,
5111  0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13,
5112  0x16, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67,
5113  0x6e, 0x20, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20,
5114  0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x31,
5115  0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x0b,
5116  0x13, 0x0e, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69,
5117  0x67, 0x6e, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e,
5118  0x31, 0x33, 0x30, 0x31, 0x06, 0x03, 0x55, 0x04,
5119  0x0b, 0x13, 0x2a, 0x56, 0x65, 0x72, 0x69, 0x53,
5120  0x69, 0x67, 0x6e, 0x20, 0x49, 0x6e, 0x74, 0x65,
5121  0x72, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x61,
5122  0x6c, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
5123  0x20, 0x43, 0x41, 0x20, 0x2d, 0x20, 0x43, 0x6c,
5124  0x61, 0x73, 0x73, 0x20, 0x33, 0x31, 0x49, 0x30,
5125  0x47, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x40,
5126  0x77, 0x77, 0x77, 0x2e, 0x76, 0x65, 0x72, 0x69,
5127  0x73, 0x69,
5128  0x67, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x43,
5129  0x50, 0x53, 0x20, 0x49, 0x6e, 0x63, 0x6f, 0x72,
5130  0x70, 0x2e, 0x62, 0x79, 0x20, 0x52, 0x65, 0x66,
5131  0x2e, 0x20, 0x4c, 0x49, 0x41, 0x42, 0x49, 0x4c,
5132  0x49, 0x54, 0x59, 0x20, 0x4c, 0x54, 0x44, 0x2e,
5133  0x28, 0x63, 0x29, 0x39, 0x37, 0x20, 0x56, 0x65,
5134  0x72, 0x69, 0x53, 0x69, 0x67, 0x6e, 0x30, 0x81,
5135  0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
5136  0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,
5137  0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02,
5138  0x81, 0x81, 0x00, 0xd8, 0x82, 0x80, 0xe8, 0xd6,
5139  0x19, 0x02, 0x7d, 0x1f, 0x85, 0x18, 0x39, 0x25,
5140  0xa2, 0x65, 0x2b, 0xe1, 0xbf, 0xd4, 0x05, 0xd3,
5141  0xbc, 0xe6, 0x36, 0x3b, 0xaa, 0xf0, 0x4c, 0x6c,
5142  0x5b, 0xb6, 0xe7, 0xaa, 0x3c, 0x73, 0x45, 0x55,
5143  0xb2, 0xf1, 0xbd, 0xea, 0x97, 0x42, 0xed, 0x9a,
5144  0x34, 0x0a, 0x15, 0xd4, 0xa9, 0x5c, 0xf5, 0x40,
5145  0x25, 0xdd, 0xd9, 0x07, 0xc1, 0x32, 0xb2, 0x75,
5146  0x6c, 0xc4, 0xca, 0xbb, 0xa3, 0xfe, 0x56, 0x27,
5147  0x71, 0x43, 0xaa, 0x63, 0xf5, 0x30, 0x3e, 0x93,
5148  0x28, 0xe5, 0xfa, 0xf1, 0x09, 0x3b, 0xf3, 0xb7,
5149  0x4d, 0x4e, 0x39, 0xf7, 0x5c, 0x49, 0x5a, 0xb8,
5150  0xc1, 0x1d, 0xd3, 0xb2, 0x8a, 0xfe, 0x70, 0x30,
5151  0x95, 0x42, 0xcb, 0xfe, 0x2b, 0x51, 0x8b, 0x5a,
5152  0x3c, 0x3a, 0xf9, 0x22, 0x4f, 0x90, 0xb2, 0x02,
5153  0xa7, 0x53, 0x9c, 0x4f, 0x34, 0xe7, 0xab, 0x04,
5154  0xb2, 0x7b, 0x6f, 0x02, 0x03, 0x01, 0x00, 0x01,
5155  0xa3, 0x81, 0xe3, 0x30, 0x81, 0xe0, 0x30, 0x0f,
5156  0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x08, 0x30,
5157  0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x00, 0x30,
5158  0x44, 0x06, 0x03, 0x55, 0x1d, 0x20, 0x04, 0x3d,
5159  0x30, 0x3b, 0x30, 0x39, 0x06, 0x0b, 0x60, 0x86,
5160  0x48, 0x01, 0x86, 0xf8, 0x45, 0x01, 0x07, 0x01,
5161  0x01, 0x30, 0x2a, 0x30, 0x28, 0x06, 0x08, 0x2b,
5162  0x06, 0x01, 0x05, 0x05, 0x07, 0x02, 0x01, 0x16,
5163  0x1c, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
5164  0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x65, 0x72,
5165  0x69, 0x73, 0x69, 0x67, 0x6e, 0x2e, 0x63, 0x6f,
5166  0x6d, 0x2f, 0x43, 0x50, 0x53, 0x30, 0x34, 0x06,
5167  0x03, 0x55, 0x1d, 0x25, 0x04, 0x2d, 0x30, 0x2b,
5168  0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07,
5169  0x03, 0x01, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05,
5170  0x05, 0x07, 0x03, 0x02, 0x06, 0x09, 0x60, 0x86,
5171  0x48, 0x01, 0x86, 0xf8, 0x42, 0x04, 0x01, 0x06,
5172  0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x45,
5173  0x01, 0x08, 0x01, 0x30, 0x0b, 0x06, 0x03, 0x55,
5174  0x1d, 0x0f, 0x04, 0x04, 0x03, 0x02, 0x01, 0x06,
5175  0x30, 0x11, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
5176  0x86, 0xf8, 0x42, 0x01, 0x01, 0x04, 0x04, 0x03,
5177  0x02, 0x01, 0x06, 0x30, 0x31, 0x06, 0x03, 0x55,
5178  0x1d, 0x1f, 0x04, 0x2a, 0x30, 0x28, 0x30, 0x26,
5179  0xa0, 0x24, 0xa0, 0x22, 0x86, 0x20, 0x68, 0x74,
5180  0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x63, 0x72, 0x6c,
5181  0x2e, 0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67,
5182  0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x63,
5183  0x61, 0x33, 0x2e, 0x63, 0x72, 0x6c, 0x30, 0x0d,
5184  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
5185  0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x81, 0x81,
5186  0x00, 0x40, 0x8e, 0x49, 0x97, 0x96, 0x8a, 0x73,
5187  0xdd, 0x8e, 0x4d, 0xef, 0x3e, 0x61, 0xb7, 0xca,
5188  0xa0, 0x62, 0xad, 0xf4, 0x0e, 0x0a, 0xbb, 0x75,
5189  0x3d, 0xe2, 0x6e, 0xd8, 0x2c, 0xc7, 0xbf, 0xf4,
5190  0xb9, 0x8c, 0x36, 0x9b, 0xca, 0xa2, 0xd0, 0x9c,
5191  0x72, 0x46, 0x39, 0xf6, 0xa6, 0x82, 0x03, 0x65,
5192  0x11, 0xc4, 0xbc, 0xbf, 0x2d, 0xa6, 0xf5, 0xd9,
5193  0x3b, 0x0a, 0xb5, 0x98, 0xfa, 0xb3, 0x78, 0xb9,
5194  0x1e, 0xf2, 0x2b, 0x4c, 0x62, 0xd5, 0xfd, 0xb2,
5195  0x7a, 0x1d, 0xdf, 0x33, 0xfd, 0x73, 0xf9, 0xa5,
5196  0xd8, 0x2d, 0x8c, 0x2a, 0xea, 0xd1, 0xfc, 0xb0,
5197  0x28, 0xb6, 0xe9, 0x49, 0x48, 0x13, 0x4b, 0x83,
5198  0x8a, 0x1b, 0x48, 0x7b, 0x24, 0xf7, 0x38, 0xde,
5199  0x6f, 0x41, 0x54, 0xb8, 0xab, 0x57, 0x6b, 0x06,
5200  0xdf, 0xc7, 0xa2, 0xd4, 0xa9, 0xf6, 0xf1, 0x36,
5201  0x62, 0x80, 0x88, 0xf2, 0x8b, 0x75, 0xd6, 0x80,
5202  0x75, 0x16, 0x03, 0x01, 0x00, 0x04, 0x0e, 0x00,
5203  0x00, 0x00
5204  };
5205  uint32_t server_hello_certificate_done_len = sizeof(server_hello_certificate_done);
5206 
5207  uint8_t client_key_exchange_cipher_enc_hs[] = {
5208  0x16, 0x03, 0x01, 0x00, 0x86, 0x10, 0x00, 0x00,
5209  0x80, 0x00, 0x80, 0x14, 0x2b, 0x2f, 0x9f, 0x02,
5210  0x1d, 0x4e, 0x0d, 0xa7, 0x41, 0x0f, 0x99, 0xc5,
5211  0xe9, 0x49, 0x22, 0x14, 0xa0, 0x42, 0x7b, 0xb4,
5212  0x6d, 0x4f, 0x82, 0x3c, 0x3a, 0x6e, 0xed, 0xd5,
5213  0x6e, 0x72, 0x71, 0xae, 0x00, 0x4a, 0x9a, 0xc9,
5214  0x0e, 0x2d, 0x08, 0xa2, 0xd3, 0x3a, 0xb0, 0xb2,
5215  0x1a, 0x56, 0x01, 0x7c, 0x9a, 0xfa, 0xfb, 0x1a,
5216  0xd7, 0x7e, 0x20, 0x68, 0x51, 0xd0, 0xfe, 0xd9,
5217  0xdc, 0xa7, 0x0b, 0xeb, 0x1a, 0xb6, 0xd3, 0xc7,
5218  0x17, 0x1f, 0xf3, 0x6e, 0x91, 0xdd, 0x06, 0x0d,
5219  0x48, 0xde, 0xcd, 0x0c, 0x36, 0x8c, 0x83, 0x29,
5220  0x9a, 0x40, 0x03, 0xcd, 0xf3, 0x1b, 0xdb, 0xd8,
5221  0x44, 0x6b, 0x75, 0xf3, 0x5a, 0x9f, 0x26, 0x1a,
5222  0xc4, 0x16, 0x35, 0x8f, 0xc1, 0x15, 0x19, 0xa9,
5223  0xdf, 0x07, 0xa9, 0xe5, 0x56, 0x45, 0x6d, 0xca,
5224  0x20, 0x3c, 0xcf, 0x8e, 0xbe, 0x44, 0x68, 0x73,
5225  0xc8, 0x0b, 0xc7, 0x14, 0x03, 0x01, 0x00, 0x01,
5226  0x01, 0x16, 0x03, 0x01, 0x00, 0x24, 0xf9, 0x7e,
5227  0x28, 0x77, 0xa9, 0x9a, 0x08, 0x0c, 0x2e, 0xa9,
5228  0x09, 0x15, 0x27, 0xcd, 0x93, 0x5f, 0xc0, 0x32,
5229  0x0a, 0x8d, 0x62, 0xd3, 0x54, 0x79, 0x6b, 0x51,
5230  0xd7, 0xba, 0x02, 0xd6, 0xdb, 0x66, 0xe8, 0x97,
5231  0x5d, 0x7a
5232  };
5233  uint32_t client_key_exchange_cipher_enc_hs_len = sizeof(client_key_exchange_cipher_enc_hs);
5234 
5235  TcpSession ssn;
5237 
5238  memset(&f, 0, sizeof(f));
5239  memset(&ssn, 0, sizeof(ssn));
5240  FLOW_INITIALIZE(&f);
5241  f.protoctx = (void *)&ssn;
5242  f.proto = IPPROTO_TCP;
5243  f.alproto = ALPROTO_TLS;
5244 
5246 
5247  FLOWLOCK_WRLOCK(&f);
5248  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
5249  STREAM_TOSERVER, client_hello,
5250  client_hello_len);
5251  FLOWLOCK_UNLOCK(&f);
5252  FAIL_IF(r != 0);
5253 
5254  SSLState *ssl_state = f.alstate;
5255  FAIL_IF_NULL(ssl_state);
5256 
5257  FAIL_IF(ssl_state->client_connp.bytes_processed != 0);
5258  FAIL_IF(ssl_state->client_connp.hs_bytes_processed != 0);
5259 
5260  FLOWLOCK_WRLOCK(&f);
5261  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT,
5262  server_hello_certificate_done,
5263  server_hello_certificate_done_len);
5264  FLOWLOCK_UNLOCK(&f);
5265  FAIL_IF(r != 0);
5266 
5267  FAIL_IF(ssl_state->client_connp.bytes_processed != 0);
5268  FAIL_IF(ssl_state->client_connp.hs_bytes_processed != 0);
5269 
5270  FLOWLOCK_WRLOCK(&f);
5271  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
5272  client_key_exchange_cipher_enc_hs,
5273  client_key_exchange_cipher_enc_hs_len);
5274  FLOWLOCK_UNLOCK(&f);
5275  FAIL_IF(r != 0);
5276 
5277  /* The reason hs_bytes_processed is 2 is because, the record
5278  * immediately after the client key exchange is 2 bytes long,
5279  * and next time we see a new handshake, it is after we have
5280  * seen a change cipher spec. Hence when we process the
5281  * handshake, we immediately break and don't parse the pdu from
5282  * where we left off, and leave the hs_bytes_processed var
5283  * isn't reset. */
5284  FAIL_IF(ssl_state->client_connp.bytes_processed != 0);
5285  FAIL_IF(ssl_state->client_connp.hs_bytes_processed != 2);
5286 
5287  AppLayerParserThreadCtxFree(alp_tctx);
5289  FLOW_DESTROY(&f);
5290 
5291  PASS;
5292 }
5293 
5294 static int SSLParserTest26(void)
5295 {
5296  Flow f;
5297  uint8_t client_hello[] = {
5298  0x16, 0x03, 0x01, 0x02, 0x0e, 0x01, 0x00, 0x02,
5299  0x0a, 0x03, 0x03, 0x58, 0x36, 0x15, 0x03, 0x8e,
5300  0x07, 0xf9, 0xad, 0x2a, 0xb7, 0x56, 0xbf, 0xe2,
5301  0xa2, 0xf8, 0x21, 0xe0, 0xbb, 0x69, 0xc2, 0xd6,
5302  0x76, 0xe6, 0x77, 0xfe, 0x09, 0xff, 0x8e, 0xac,
5303  0x80, 0xb5, 0x27, 0x20, 0xb7, 0xbb, 0x90, 0x35,
5304  0x7a, 0xdd, 0xd9, 0x67, 0xdf, 0x79, 0xd6, 0x16,
5305  0x90, 0xf6, 0xd7, 0x5c, 0xd3, 0x07, 0x19, 0x20,
5306  0x01, 0x39, 0x76, 0x25, 0x12, 0x32, 0x71, 0xa1,
5307  0x84, 0x8d, 0x2d, 0xea, 0x00, 0x88, 0xc0, 0x30,
5308  0xc0, 0x2c, 0xc0, 0x28, 0xc0, 0x24, 0xc0, 0x14,
5309  0xc0, 0x0a, 0x00, 0xa3, 0x00, 0x9f, 0x00, 0x6b,
5310  0x00, 0x6a, 0x00, 0x39, 0x00, 0x38, 0x00, 0x88,
5311  0x00, 0x87, 0xc0, 0x32, 0xc0, 0x2e, 0xc0, 0x2a,
5312  0xc0, 0x26, 0xc0, 0x0f, 0xc0, 0x05, 0x00, 0x9d,
5313  0x00, 0x3d, 0x00, 0x35, 0x00, 0x84, 0xc0, 0x12,
5314  0xc0, 0x08, 0x00, 0x16, 0x00, 0x13, 0xc0, 0x0d,
5315  0xc0, 0x03, 0x00, 0x0a, 0xc0, 0x2f, 0xc0, 0x2b,
5316  0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x13, 0xc0, 0x09,
5317  0x00, 0xa2, 0x00, 0x9e, 0x00, 0x67, 0x00, 0x40,
5318  0x00, 0x33, 0x00, 0x32, 0x00, 0x9a, 0x00, 0x99,
5319  0x00, 0x45, 0x00, 0x44, 0xc0, 0x31, 0xc0, 0x2d,
5320  0xc0, 0x29, 0xc0, 0x25, 0xc0, 0x0e, 0xc0, 0x04,
5321  0x00, 0x9c, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x96,
5322  0x00, 0x41, 0xc0, 0x11, 0xc0, 0x07, 0xc0, 0x0c,
5323  0xc0, 0x02, 0x00, 0x05, 0x00, 0x04, 0x00, 0x15,
5324  0x00, 0x12, 0x00, 0x09, 0x00, 0xff, 0x01, 0x00,
5325  0x01, 0x39, 0x00, 0x00, 0x00, 0x14, 0x00, 0x12,
5326  0x00, 0x00, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x79,
5327  0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x2e, 0x63,
5328  0x6f, 0x6d, 0x00, 0x0b, 0x00, 0x04, 0x03, 0x00,
5329  0x01, 0x02, 0x00, 0x0a, 0x00, 0x34, 0x00, 0x32,
5330  0x00, 0x0e, 0x00, 0x0d, 0x00, 0x19, 0x00, 0x0b,
5331  0x00, 0x0c, 0x00, 0x18, 0x00, 0x09, 0x00, 0x0a,
5332  0x00, 0x16, 0x00, 0x17, 0x00, 0x08, 0x00, 0x06,
5333  0x00, 0x07, 0x00, 0x14, 0x00, 0x15, 0x00, 0x04,
5334  0x00, 0x05, 0x00, 0x12, 0x00, 0x13, 0x00, 0x01,
5335  0x00, 0x02, 0x00, 0x03, 0x00, 0x0f, 0x00, 0x10,
5336  0x00, 0x11, 0x00, 0x23, 0x00, 0xb4, 0x05, 0x6c,
5337  0xfa, 0x27, 0x6f, 0x12, 0x2f, 0x2a, 0xe5, 0x56,
5338  0xcb, 0x42, 0x62, 0x44, 0xf2, 0xd7, 0xd1, 0x05,
5339  0x87, 0xd4, 0x52, 0x02, 0x10, 0x85, 0xa4, 0xa6,
5340  0x82, 0x6f, 0x6d, 0x7b, 0xaf, 0x11, 0xbe, 0x21,
5341  0x7e, 0x7c, 0x36, 0x03, 0x20, 0x29, 0xd8, 0xf9,
5342  0xe5, 0x2b, 0xe2, 0x26, 0xb2, 0x27, 0xc7, 0xb9,
5343  0xda, 0x59, 0xd7, 0xdc, 0xfd, 0x74, 0x74, 0x76,
5344  0xd0, 0x5e, 0xe4, 0xfe, 0x9d, 0xb7, 0x1b, 0x13,
5345  0x81, 0xce, 0x63, 0x75, 0x2b, 0x2f, 0x98, 0x3a,
5346  0x84, 0x46, 0xd3, 0x0c, 0xb3, 0x01, 0xdb, 0x62,
5347  0x51, 0x97, 0x92, 0x1c, 0xa5, 0x94, 0x60, 0xef,
5348  0xa6, 0xd8, 0xb2, 0x2f, 0x02, 0x42, 0x5c, 0xac,
5349  0xb4, 0xd9, 0x10, 0x2f, 0x7e, 0x89, 0xab, 0xa5,
5350  0xd7, 0x56, 0x6d, 0x03, 0xd2, 0x5f, 0x20, 0x2c,
5351  0xb6, 0x99, 0x2b, 0x66, 0xbd, 0xd4, 0xde, 0x53,
5352  0x76, 0x5c, 0x78, 0xf0, 0xe9, 0x6d, 0xa5, 0xc3,
5353  0x1a, 0x9e, 0x61, 0xb2, 0x45, 0xb0, 0xb3, 0x61,
5354  0xee, 0xa1, 0x07, 0xab, 0x2f, 0x84, 0xea, 0x43,
5355  0x76, 0x4b, 0x3d, 0xb0, 0xbe, 0xa4, 0xb4, 0x21,
5356  0xe1, 0xd3, 0xfd, 0x91, 0xe2, 0xe7, 0xf3, 0x38,
5357  0x9c, 0x56, 0x5f, 0xa1, 0xde, 0xa8, 0x2f, 0x0a,
5358  0x49, 0x6d, 0x44, 0x8e, 0xb7, 0xef, 0x4a, 0x6f,
5359  0x79, 0xb2, 0x00, 0x0d, 0x00, 0x20, 0x00, 0x1e,
5360  0x06, 0x01, 0x06, 0x02, 0x06, 0x03, 0x05, 0x01,
5361  0x05, 0x02, 0x05, 0x03, 0x04, 0x01, 0x04, 0x02,
5362  0x04, 0x03, 0x03, 0x01, 0x03, 0x02, 0x03, 0x03,
5363  0x02, 0x01, 0x02, 0x02, 0x02, 0x03, 0x00, 0x0f,
5364  0x00, 0x01, 0x01
5365  };
5366  uint32_t client_hello_len = sizeof(client_hello);
5367 
5368  uint8_t server_hello_change_cipher_spec[] = {
5369  0x16, 0x03, 0x03, 0x00, 0x57, 0x02, 0x00, 0x00,
5370  0x53, 0x03, 0x03, 0x58, 0x36, 0x15, 0x03, 0x9f,
5371  0x3b, 0xf3, 0x11, 0x96, 0x2b, 0xc3, 0xae, 0x91,
5372  0x8c, 0x5f, 0x8b, 0x3f, 0x90, 0xbd, 0xa9, 0x26,
5373  0x26, 0xb2, 0xfd, 0x12, 0xc5, 0xc5, 0x7b, 0xe4,
5374  0xd1, 0x3e, 0x81, 0x20, 0xb7, 0xbb, 0x90, 0x35,
5375  0x7a, 0xdd, 0xd9, 0x67, 0xdf, 0x79, 0xd6, 0x16,
5376  0x90, 0xf6, 0xd7, 0x5c, 0xd3, 0x07, 0x19, 0x20,
5377  0x01, 0x39, 0x76, 0x25, 0x12, 0x32, 0x71, 0xa1,
5378  0x84, 0x8d, 0x2d, 0xea, 0xc0, 0x2b, 0x00, 0x00,
5379  0x0b, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x0b,
5380  0x00, 0x02, 0x01, 0x00, 0x14, 0x03, 0x03, 0x00,
5381  0x01, 0x01, 0x16, 0x03, 0x03, 0x00, 0x28, 0x00,
5382  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
5383  0x66, 0xfe, 0x07, 0x08, 0x33, 0x4d, 0xc2, 0x83,
5384  0x8e, 0x05, 0x8b, 0xf8, 0xd1, 0xb1, 0xa7, 0x16,
5385  0x4b, 0x42, 0x5c, 0x3a, 0xa4, 0x31, 0x0f, 0xba,
5386  0x84, 0x06, 0xcb, 0x9d, 0xc6, 0xc4, 0x66
5387  };
5388  uint32_t server_hello_change_cipher_spec_len = sizeof(server_hello_change_cipher_spec);
5389 
5390  TcpSession ssn;
5392 
5393  memset(&f, 0, sizeof(f));
5394  memset(&ssn, 0, sizeof(ssn));
5395  FLOW_INITIALIZE(&f);
5396  f.protoctx = (void *)&ssn;
5397  f.proto = IPPROTO_TCP;
5398  f.alproto = ALPROTO_TLS;
5399 
5401 
5402  FLOWLOCK_WRLOCK(&f);
5403  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
5404  STREAM_TOSERVER, client_hello,
5405  client_hello_len);
5406  FLOWLOCK_UNLOCK(&f);
5407  FAIL_IF(r != 0);
5408 
5409  SSLState *ssl_state = f.alstate;
5410  FAIL_IF_NULL(ssl_state);
5411 
5412  FAIL_IF((ssl_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) == 0);
5413  FAIL_IF_NULL(ssl_state->client_connp.session_id);
5414 
5415  FLOWLOCK_WRLOCK(&f);
5416  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT,
5417  server_hello_change_cipher_spec,
5418  server_hello_change_cipher_spec_len);
5419  FLOWLOCK_UNLOCK(&f);
5420  FAIL_IF(r != 0);
5421 
5423  FAIL_IF((ssl_state->flags & SSL_AL_FLAG_SESSION_RESUMED) == 0);
5424 
5425  AppLayerParserThreadCtxFree(alp_tctx);
5427  FLOW_DESTROY(&f);
5428 
5429  PASS;
5430 }
5431 
5432 #endif /* UNITTESTS */
5433 
5435 {
5436 #ifdef UNITTESTS
5437  UtRegisterTest("SSLParserTest01", SSLParserTest01);
5438  UtRegisterTest("SSLParserTest02", SSLParserTest02);
5439  UtRegisterTest("SSLParserTest03", SSLParserTest03);
5440  UtRegisterTest("SSLParserTest04", SSLParserTest04);
5441  /* Updated by Anoop Saldanha. Faulty tests. Disable it for now */
5442  //UtRegisterTest("SSLParserTest05", SSLParserTest05, 1);
5443  //UtRegisterTest("SSLParserTest06", SSLParserTest06, 1);
5444  UtRegisterTest("SSLParserTest07", SSLParserTest07);
5445  //UtRegisterTest("SSLParserTest08", SSLParserTest08, 1);
5446  UtRegisterTest("SSLParserTest09", SSLParserTest09);
5447  UtRegisterTest("SSLParserTest10", SSLParserTest10);
5448  UtRegisterTest("SSLParserTest11", SSLParserTest11);
5449  UtRegisterTest("SSLParserTest12", SSLParserTest12);
5450  UtRegisterTest("SSLParserTest13", SSLParserTest13);
5451 
5452  UtRegisterTest("SSLParserTest14", SSLParserTest14);
5453  UtRegisterTest("SSLParserTest15", SSLParserTest15);
5454  UtRegisterTest("SSLParserTest16", SSLParserTest16);
5455  UtRegisterTest("SSLParserTest17", SSLParserTest17);
5456  UtRegisterTest("SSLParserTest18", SSLParserTest18);
5457  UtRegisterTest("SSLParserTest19", SSLParserTest19);
5458  UtRegisterTest("SSLParserTest20", SSLParserTest20);
5459  UtRegisterTest("SSLParserTest21", SSLParserTest21);
5460  UtRegisterTest("SSLParserTest22", SSLParserTest22);
5461  UtRegisterTest("SSLParserTest23", SSLParserTest23);
5462  UtRegisterTest("SSLParserTest24", SSLParserTest24);
5463  UtRegisterTest("SSLParserTest25", SSLParserTest25);
5464  UtRegisterTest("SSLParserTest26", SSLParserTest26);
5465 
5466  UtRegisterTest("SSLParserMultimsgTest01", SSLParserMultimsgTest01);
5467  UtRegisterTest("SSLParserMultimsgTest02", SSLParserMultimsgTest02);
5468 #endif /* UNITTESTS */
5469 
5470  return;
5471 }
uint16_t hb_record_len
#define SSLV3_HS_CERTIFICATE_STATUS
#define SSL_AL_FLAG_SESSION_RESUMED
#define SSL_AL_FLAG_STATE_SERVER_HELLO
Definition: app-layer-ssl.h:84
uint64_t detect_flags_ts
int Asn1DerGetValidity(const Asn1Generic *cert, time_t *not_before, time_t *not_after, uint32_t *errcode)
enum AppLayerEventType_ AppLayerEventType
#define SSL_AL_FLAG_HANDSHAKE_DONE
Definition: app-layer-ssl.h:98
int Ja3BufferAddValue(JA3Buffer **buffer, uint32_t value)
Add value to buffer.
Definition: util-ja3.c:170
uint16_t flags
#define SSLV2_MT_SERVER_HELLO
#define SSLV3_HEARTBEAT_PROTOCOL
#define SSL_AL_FLAG_HB_INFLIGHT
Definition: app-layer-ssl.h:93
char * cert0_subject
time_t cert0_not_before
#define SCLogDebug(...)
Definition: util-debug.h:335
uint32_t flags
#define MAX(x, y)
const char * SCMapEnumValueToName(int enum_value, SCEnumCharMap *table)
Maps an enum value to a string name, from the supplied table.
Definition: util-enum.c:69
#define SSL_AL_FLAG_SSL_NO_SESSION_ID
Definition: app-layer-ssl.h:80
#define SSLV3_HS_CERTIFICATE
#define TAILQ_FIRST(head)
Definition: queue.h:339
#define SSLV2_MT_SERVER_FINISHED
uint16_t hs_bytes_processed
void AppLayerProtoDetectPPRegister(uint8_t ipproto, const char *portstr, AppProto alproto, uint16_t min_depth, uint16_t max_depth, uint8_t direction, ProbingParserFPtr ProbingParser1, ProbingParserFPtr ProbingParser2)
register parser at a port
struct HtpBodyChunk_ * next
#define SSL_AL_FLAG_EARLY_DATA
void AppLayerParserRegisterGetStateProgressFunc(uint8_t ipproto, AppProto alproto, int(*StateGetProgress)(void *alstate, uint8_t direction))
#define SSLV3_HS_NEW_SESSION_TICKET
uint32_t event_type
uint8_t proto
Definition: flow.h:344
int ComputeSHA1(const uint8_t *inbuf, size_t inbuf_len, uint8_t *outbuf, size_t outbuf_size)
calculate SHA1 hash
Definition: util-crypt.c:230
SCEnumCharMap tls_decoder_event_table[]
Definition: app-layer-ssl.c:58
uint16_t version
LoggerId
#define FLOWLOCK_UNLOCK(fb)
Definition: flow.h:243
int logged
void DerFree(Asn1Generic *a)
uint32_t message_length
#define PASS
Pass the test.
#define unlikely(expr)
Definition: util-optimize.h:35
#define SHA1_STRING_LENGTH
#define SSL_AL_FLAG_SSL_SERVER_HS
Definition: app-layer-ssl.h:76
SSLStateConnp * curr_connp
#define SSLV3_HS_SERVER_HELLO
int ConfGetBool(const char *name, int *val)
Retrieve a configuration value as an boolen.
Definition: conf.c:517
int AppLayerProtoDetectConfProtoDetectionEnabled(const char *ipproto, const char *alproto)
Given a protocol name, checks if proto detection is enabled in the conf file.
void AppLayerParserRegisterDetectFlagsFuncs(uint8_t ipproto, AppProto alproto, uint64_t(*GetTxDetectFlags)(void *tx, uint8_t dir), void(*SetTxDetectFlags)(void *tx, uint8_t dir, uint64_t))
int AppLayerParserStateIssetFlag(AppLayerParserState *pstate, uint8_t flag)
char * cert0_fingerprint
void AppLayerParserRegisterParserAcceptableDataDirection(uint8_t ipproto, AppProto alproto, uint8_t direction)
void AppLayerParserRegisterDetectStateFuncs(uint8_t ipproto, AppProto alproto, DetectEngineState *(*GetTxDetectState)(void *tx), int(*SetTxDetectState)(void *tx, DetectEngineState *))
#define SSLV3_HS_CERTIFICATE_URL
#define SSL_EXTENSION_EC_POINT_FORMATS
SSLStateConnp server_connp
#define ERR_DER_RECURSION_LIMIT
size_t strlcat(char *, const char *src, size_t siz)
Definition: util-strlcatu.c:45
#define SSLV2_MT_SERVER_VERIFY
#define SSL_EXTENSION_SUPPORTED_VERSIONS
#define FAIL_IF(expr)
Fail a test if expression evaluates to false.
Definition: util-unittest.h:71
#define APP_LAYER_PARSER_BYPASS_READY
void AppLayerParserThreadCtxFree(AppLayerParserThreadCtx *tctx)
Destroys the app layer parser thread context obtained using AppLayerParserThreadCtxAlloc().
#define SSL_AL_FLAG_HB_SERVER_INIT
Definition: app-layer-ssl.h:95
void AppLayerDecoderEventsSetEventRaw(AppLayerDecoderEvents **sevents, uint8_t event)
Set an app layer decoder event.
void StreamTcpFreeConfig(char quiet)
Definition: stream-tcp.c:668
#define FLOWLOCK_WRLOCK(fb)
Definition: flow.h:240
#define SSLV3_HS_FINISHED
#define SSLV2_MT_REQUEST_CERTIFICATE
time_t cert0_not_after
void AppLayerParserRegisterGetEventsFunc(uint8_t ipproto, AppProto alproto, AppLayerDecoderEvents *(*StateGetEvents)(void *))
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:41
void AppLayerParserRegisterLoggerFuncs(uint8_t ipproto, AppProto alproto, LoggerId(*StateGetTxLogged)(void *, void *), void(*StateSetTxLogged)(void *, void *, LoggerId))
uint16_t AppProto
char * val
Definition: conf.h:34
int Ja3BufferAppendBuffer(JA3Buffer **buffer1, JA3Buffer **buffer2)
Append buffer to buffer.
Definition: util-ja3.c:106
uint16_t bytes_processed
#define TRUE
#define ERR_DER_INVALID_SIZE
uint8_t * trec
#define SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY
Definition: app-layer-ssl.h:77
void * protoctx
Definition: flow.h:400
void SSLVersionToString(uint16_t version, char *buffer)
uint32_t record_lengths_length
JA3Buffer * Ja3BufferInit(void)
Allocate new buffer.
Definition: util-ja3.c:39
SslConfigEncryptHandling
Definition: app-layer-ssl.c:89
#define SSL_AL_FLAG_CHANGE_CIPHER_SPEC
Definition: app-layer-ssl.h:72
#define TLS_HB_REQUEST
SSLv[2.0|3.[0|1|2|3]] state structure.
void * alstate
Definition: flow.h:438
#define SSL_AL_FLAG_HB_CLIENT_INIT
Definition: app-layer-ssl.h:94
#define APP_LAYER_PARSER_NO_REASSEMBLY
int AppLayerParserConfParserEnabled(const char *ipproto, const char *alproto_name)
check if a parser is enabled in the config Returns enabled always if: were running unittests and when...
void AppLayerParserTriggerRawStreamReassembly(Flow *f, int direction)
#define SC_ATOMIC_INIT(name)
Initialize the previously declared atomic variable and it&#39;s lock.
Definition: util-atomic.h:81
#define SCCalloc(nm, a)
Definition: util-mem.h:253
uint64_t detect_flags_tc
#define SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED
Definition: app-layer-ssl.h:78
Data structure to store app layer decoder events.
int AppLayerParserRegisterParser(uint8_t ipproto, AppProto alproto, uint8_t direction, AppLayerParserFPtr Parser)
Register app layer parser for the protocol.
#define SSL_AL_FLAG_STATE_FINISHED
Definition: app-layer-ssl.h:90
#define SSLV3_HS_CERTIFICATE_VERIFY
#define SSL_AL_FLAG_STATE_SERVER_KEYX
Definition: app-layer-ssl.h:86
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
enum SslConfigEncryptHandling encrypt_mode
Definition: app-layer-ssl.c:96
uint16_t session_id_length
#define SSLV2_MT_CLIENT_HELLO
#define SSL_AL_FLAG_CH_VERSION_EXTENSION
uint16_t events
#define TAILQ_INIT(head)
Definition: queue.h:370
int Asn1DerGetIssuerDN(const Asn1Generic *cert, char *buffer, uint32_t length, uint32_t *errcode)
int AppLayerProtoDetectPPParseConfPorts(const char *ipproto_name, uint8_t ipproto, const char *alproto_name, AppProto alproto, uint16_t min_depth, uint16_t max_depth, ProbingParserFPtr ProbingParserTs, ProbingParserFPtr ProbingParserTc)
void AppLayerParserRegisterGetEventInfo(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfo)(const char *event_name, int *event_id, AppLayerEventType *event_type))
#define SSLV3_ALERT_PROTOCOL
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
#define FLOW_DESTROY(f)
Definition: flow-util.h:121
uint32_t logged
#define FLOW_NOPAYLOAD_INSPECTION
Definition: flow.h:61
#define STREAM_EOF
Definition: stream.h:30
void UtRegisterTest(const char *name, int(*TestFn)(void))
Register unit test.
AppLayerDecoderEvents * decoder_events
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:412
#define APP_LAYER_PARSER_NO_INSPECTION
uint32_t current_flags
void StreamTcpInitConfig(char)
To initialize the stream global configuration data.
Definition: stream-tcp.c:364
void SSLEnableJA3(void)
if not explicitly disabled in config, enable ja3 support
void SSLSetEvent(SSLState *ssl_state, uint8_t event)
int ConfValIsFalse(const char *val)
Check if a value is false.
Definition: conf.c:591
uint32_t trec_len
#define STREAM_TOCLIENT
Definition: stream.h:32
#define SSLV3_APPLICATION_PROTOCOL
char * Ja3GenerateHash(JA3Buffer *buffer)
Generate Ja3 hash string.
Definition: util-ja3.c:217
void AppLayerParserRegisterGetStateProgressCompletionStatus(AppProto alproto, int(*StateGetProgressCompletionStatus)(uint8_t direction))
uint8_t handshake_type
AppLayerParserThreadCtx * AppLayerParserThreadCtxAlloc(void)
Gets a new app layer protocol&#39;s parser thread context.
#define SSL_AL_FLAG_STATE_CLIENT_HELLO
Definition: app-layer-ssl.h:83
#define HAS_SPACE(n)
void DetectEngineStateFree(DetectEngineState *state)
Frees a DetectEngineState object.
int ConfGetValue(const char *name, const char **vptr)
Retrieve the value of a configuration node.
Definition: conf.c:360
int RunmodeIsUnittests(void)
Definition: suricata.c:267
bool SSLJA3IsEnabled(void)
uint32_t trec_pos
#define SCReturnInt(x)
Definition: util-debug.h:341
#define SSLV2_MT_CLIENT_MASTER_KEY
void AppLayerParserRegisterGetEventInfoById(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfoById)(int event_id, const char **event_name, AppLayerEventType *event_type))
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
#define SCRealloc(x, a)
Definition: util-mem.h:238
#define SSLV3_HS_CERTIFICATE_REQUEST
int ConfValIsTrue(const char *val)
Check if a value is true.
Definition: conf.c:566
#define SSL_EXTENSION_SESSION_TICKET
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:385
#define SSL_EXTENSION_EARLY_DATA
void AppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events)
#define SSLV2_MT_ERROR
Definition: conf.h:32
#define ERR_DER_GENERIC
#define SCMalloc(a)
Definition: util-mem.h:222
#define SC_ATOMIC_SET(name, val)
Set the value for the atomic variable.
Definition: util-atomic.h:207
#define ERR_DER_ELEMENT_SIZE_TOO_BIG
int Asn1DerGetSubjectDN(const Asn1Generic *cert, char *buffer, uint32_t length, ui