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