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