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