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 AppProto SSLProbingParser(Flow *f, uint8_t direction,
2628  uint8_t *input, uint32_t ilen, uint8_t *rdir)
2629 {
2630  /* probably a rst/fin sending an eof */
2631  if (ilen == 0)
2632  return ALPROTO_UNKNOWN;
2633 
2634  /* for now just the 3 byte header ones */
2635  /* \todo Detect the 2 byte ones */
2636  if ((input[0] & 0x80) && (input[2] == 0x01)) {
2637  return ALPROTO_TLS;
2638  }
2639 
2640  return ALPROTO_FAILED;
2641 }
2642 
2643 static int SSLStateGetEventInfo(const char *event_name,
2644  int *event_id, AppLayerEventType *event_type)
2645 {
2646  *event_id = SCMapEnumNameToValue(event_name, tls_decoder_event_table);
2647  if (*event_id == -1) {
2648  SCLogError(SC_ERR_INVALID_ENUM_MAP, "event \"%s\" not present in "
2649  "ssl's enum map table.", event_name);
2650  /* yes this is fatal */
2651  return -1;
2652  }
2653 
2654  *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
2655 
2656  return 0;
2657 }
2658 
2659 static int SSLRegisterPatternsForProtocolDetection(void)
2660 {
2662  "|01 00 02|", 5, 2, STREAM_TOSERVER) < 0)
2663  {
2664  return -1;
2665  }
2666 
2667  /** SSLv3 */
2669  "|01 03 00|", 3, 0, STREAM_TOSERVER) < 0)
2670  {
2671  return -1;
2672  }
2674  "|16 03 00|", 3, 0, STREAM_TOSERVER) < 0)
2675  {
2676  return -1;
2677  }
2678 
2679  /** TLSv1 */
2681  "|01 03 01|", 3, 0, STREAM_TOSERVER) < 0)
2682  {
2683  return -1;
2684  }
2686  "|16 03 01|", 3, 0, STREAM_TOSERVER) < 0)
2687  {
2688  return -1;
2689  }
2690 
2691  /** TLSv1.1 */
2693  "|01 03 02|", 3, 0, STREAM_TOSERVER) < 0)
2694  {
2695  return -1;
2696  }
2698  "|16 03 02|", 3, 0, STREAM_TOSERVER) < 0)
2699  {
2700  return -1;
2701  }
2702 
2703  /** TLSv1.2 */
2705  "|01 03 03|", 3, 0, STREAM_TOSERVER) < 0)
2706  {
2707  return -1;
2708  }
2710  "|16 03 03|", 3, 0, STREAM_TOSERVER) < 0)
2711  {
2712  return -1;
2713  }
2714 
2715  /***** toclient direction *****/
2716 
2718  "|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
2719  {
2720  return -1;
2721  }
2723  "|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
2724  {
2725  return -1;
2726  }
2728  "|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
2729  {
2730  return -1;
2731  }
2732 
2733  /** TLSv1 */
2735  "|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
2736  {
2737  return -1;
2738  }
2740  "|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
2741  {
2742  return -1;
2743  }
2745  "|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
2746  {
2747  return -1;
2748  }
2749 
2750  /** TLSv1.1 */
2752  "|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
2753  {
2754  return -1;
2755  }
2757  "|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
2758  {
2759  return -1;
2760  }
2762  "|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
2763  {
2764  return -1;
2765  }
2766 
2767  /** TLSv1.2 */
2769  "|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
2770  {
2771  return -1;
2772  }
2774  "|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
2775  {
2776  return -1;
2777  }
2779  "|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
2780  {
2781  return -1;
2782  }
2783 
2784  /* Subsection - SSLv2 style record by client, but informing the server
2785  * the max version it supports.
2786  * Updated by Anoop Saldanha. Disabled it for now. We'll get back to
2787  * it after some tests */
2788 #if 0
2790  "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
2791  {
2792  return -1;
2793  }
2795  "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
2796  {
2797  return -1;
2798  }
2799 #endif
2800 
2801  return 0;
2802 }
2803 
2804 /**
2805  * \brief Function to register the SSL protocol parser and other functions
2806  */
2808 {
2809  const char *proto_name = "tls";
2810 
2811  /** SSLv2 and SSLv23*/
2812  if (AppLayerProtoDetectConfProtoDetectionEnabled("tcp", proto_name)) {
2814 
2815  if (SSLRegisterPatternsForProtocolDetection() < 0)
2816  return;
2817 
2818  if (RunmodeIsUnittests()) {
2819  AppLayerProtoDetectPPRegister(IPPROTO_TCP,
2820  "443",
2821  ALPROTO_TLS,
2822  0, 3,
2824  SSLProbingParser, NULL);
2825  } else {
2826  AppLayerProtoDetectPPParseConfPorts("tcp", IPPROTO_TCP,
2827  proto_name, ALPROTO_TLS,
2828  0, 3,
2829  SSLProbingParser, NULL);
2830  }
2831  } else {
2832  SCLogInfo("Protocol detection and parser disabled for %s protocol",
2833  proto_name);
2834  return;
2835  }
2836 
2837  if (AppLayerParserConfParserEnabled("tcp", proto_name)) {
2839  SSLParseClientRecord);
2840 
2842  SSLParseServerRecord);
2843 
2844  AppLayerParserRegisterGetEventInfo(IPPROTO_TCP, ALPROTO_TLS, SSLStateGetEventInfo);
2845 
2846  AppLayerParserRegisterStateFuncs(IPPROTO_TCP, ALPROTO_TLS, SSLStateAlloc, SSLStateFree);
2847 
2849 
2850  AppLayerParserRegisterTxFreeFunc(IPPROTO_TCP, ALPROTO_TLS, SSLStateTransactionFree);
2851 
2852  AppLayerParserRegisterGetEventsFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetEvents);
2853 
2855  SSLGetTxDetectState, SSLSetTxDetectState);
2856 
2857  AppLayerParserRegisterGetTx(IPPROTO_TCP, ALPROTO_TLS, SSLGetTx);
2858 
2859  AppLayerParserRegisterGetTxCnt(IPPROTO_TCP, ALPROTO_TLS, SSLGetTxCnt);
2860 
2861  AppLayerParserRegisterGetStateProgressFunc(IPPROTO_TCP, ALPROTO_TLS, SSLGetAlstateProgress);
2862 
2863  AppLayerParserRegisterLoggerFuncs(IPPROTO_TCP, ALPROTO_TLS, SSLGetTxLogged, SSLSetTxLogged);
2865  SSLGetTxDetectFlags, SSLSetTxDetectFlags);
2866 
2868  SSLGetAlstateProgressCompletionStatus);
2869 
2870  ConfNode *enc_handle = ConfGetNode("app-layer.protocols.tls.encryption-handling");
2871  if (enc_handle != NULL && enc_handle->val != NULL) {
2872  SCLogDebug("have app-layer.protocols.tls.encryption-handling = %s", enc_handle->val);
2873  if (strcmp(enc_handle->val, "full") == 0) {
2875  } else if (strcmp(enc_handle->val, "bypass") == 0) {
2877  } else if (strcmp(enc_handle->val, "default") == 0) {
2879  } else {
2881  }
2882  } else {
2883  /* Get the value of no reassembly option from the config file */
2884  if (ConfGetNode("app-layer.protocols.tls.no-reassemble") == NULL) {
2885  int value = 0;
2886  if (ConfGetBool("tls.no-reassemble", &value) == 1 && value == 1)
2888  } else {
2889  int value = 0;
2890  if (ConfGetBool("app-layer.protocols.tls.no-reassemble", &value) == 1 && value == 1)
2892  }
2893  }
2894  SCLogDebug("ssl_config.encrypt_mode %u", ssl_config.encrypt_mode);
2895 
2896  /* Check if we should generate JA3 fingerprints */
2897  if (ConfGetBool("app-layer.protocols.tls.ja3-fingerprints",
2898  &ssl_config.enable_ja3) != 1) {
2899  ssl_config.enable_ja3 = SSL_CONFIG_DEFAULT_JA3;
2900  }
2901 
2902 #ifndef HAVE_NSS
2903  if (ssl_config.enable_ja3) {
2905  "no MD5 calculation support built in, disabling JA3");
2906  ssl_config.enable_ja3 = 0;
2907  }
2908 #else
2909  if (RunmodeIsUnittests()) {
2910  ssl_config.enable_ja3 = 1;
2911  }
2912 #endif
2913 
2914  } else {
2915  SCLogInfo("Parsed disabled for %s protocol. Protocol detection"
2916  "still on.", proto_name);
2917  }
2918 
2919 #ifdef UNITTESTS
2921 #endif
2922  return;
2923 }
2924 
2925 /***************************************Unittests******************************/
2926 
2927 #ifdef UNITTESTS
2928 
2929 /**
2930  *\test Send a get request in one chunk.
2931  */
2932 static int SSLParserTest01(void)
2933 {
2934  Flow f;
2935  uint8_t tlsbuf[] = { 0x16, 0x03, 0x01 };
2936  uint32_t tlslen = sizeof(tlsbuf);
2937  TcpSession ssn;
2939 
2940  memset(&f, 0, sizeof(f));
2941  memset(&ssn, 0, sizeof(ssn));
2942 
2943  FLOW_INITIALIZE(&f);
2944  f.protoctx = (void *)&ssn;
2945  f.proto = IPPROTO_TCP;
2946  f.alproto = ALPROTO_TLS;
2947 
2949 
2950  FLOWLOCK_WRLOCK(&f);
2951  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
2952  STREAM_TOSERVER | STREAM_EOF, tlsbuf, tlslen);
2953  FLOWLOCK_UNLOCK(&f);
2954  FAIL_IF(r != 0);
2955 
2956  SSLState *ssl_state = f.alstate;
2957  FAIL_IF_NULL(ssl_state);
2958 
2959  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
2960 
2961  FAIL_IF(ssl_state->client_connp.version != TLS_VERSION_10);
2962 
2963  AppLayerParserThreadCtxFree(alp_tctx);
2965  FLOW_DESTROY(&f);
2966 
2967  PASS;
2968 }
2969 
2970 /** \test Send a get request in two chunks. */
2971 static int SSLParserTest02(void)
2972 {
2973  Flow f;
2974  uint8_t tlsbuf1[] = { 0x16 };
2975  uint32_t tlslen1 = sizeof(tlsbuf1);
2976  uint8_t tlsbuf2[] = { 0x03, 0x01 };
2977  uint32_t tlslen2 = sizeof(tlsbuf2);
2978  TcpSession ssn;
2980 
2981  memset(&f, 0, sizeof(f));
2982  memset(&ssn, 0, sizeof(ssn));
2983  FLOW_INITIALIZE(&f);
2984  f.protoctx = (void *)&ssn;
2985  f.proto = IPPROTO_TCP;
2986  f.alproto = ALPROTO_TLS;
2987 
2989 
2990  FLOWLOCK_WRLOCK(&f);
2991  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
2992  STREAM_TOSERVER, tlsbuf1, tlslen1);
2993  FLOWLOCK_UNLOCK(&f);
2994  FAIL_IF(r != 0);
2995 
2996  FLOWLOCK_WRLOCK(&f);
2997  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
2998  tlsbuf2, tlslen2);
2999  FLOWLOCK_UNLOCK(&f);
3000  FAIL_IF(r != 0);
3001 
3002  SSLState *ssl_state = f.alstate;
3003  FAIL_IF_NULL(ssl_state);
3004 
3005  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3006 
3007  FAIL_IF(ssl_state->client_connp.version != TLS_VERSION_10);
3008 
3009  AppLayerParserThreadCtxFree(alp_tctx);
3011  FLOW_DESTROY(&f);
3012 
3013  PASS;
3014 }
3015 
3016 /** \test Send a get request in three chunks. */
3017 static int SSLParserTest03(void)
3018 {
3019  Flow f;
3020  uint8_t tlsbuf1[] = { 0x16 };
3021  uint32_t tlslen1 = sizeof(tlsbuf1);
3022  uint8_t tlsbuf2[] = { 0x03 };
3023  uint32_t tlslen2 = sizeof(tlsbuf2);
3024  uint8_t tlsbuf3[] = { 0x01 };
3025  uint32_t tlslen3 = sizeof(tlsbuf3);
3026  TcpSession ssn;
3028 
3029  memset(&f, 0, sizeof(f));
3030  memset(&ssn, 0, sizeof(ssn));
3031  FLOW_INITIALIZE(&f);
3032  f.protoctx = (void *)&ssn;
3033  f.proto = IPPROTO_TCP;
3034  f.alproto = ALPROTO_TLS;
3035 
3037 
3038  FLOWLOCK_WRLOCK(&f);
3039  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3040  STREAM_TOSERVER, tlsbuf1, tlslen1);
3041  FLOWLOCK_UNLOCK(&f);
3042  FAIL_IF(r != 0);
3043 
3044  FLOWLOCK_WRLOCK(&f);
3045  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3046  tlsbuf2, tlslen2);
3047  FLOWLOCK_UNLOCK(&f);
3048  FAIL_IF(r != 0);
3049 
3050  FLOWLOCK_WRLOCK(&f);
3051  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3052  tlsbuf3, tlslen3);
3053  FLOWLOCK_UNLOCK(&f);
3054  FAIL_IF(r != 0);
3055 
3056  SSLState *ssl_state = f.alstate;
3057  FAIL_IF_NULL(ssl_state);
3058 
3059  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3060 
3061  FAIL_IF(ssl_state->client_connp.version != TLS_VERSION_10);
3062 
3063  AppLayerParserThreadCtxFree(alp_tctx);
3065  FLOW_DESTROY(&f);
3066 
3067  PASS;
3068 }
3069 
3070 /** \test Send a get request in three chunks + more data. */
3071 static int SSLParserTest04(void)
3072 {
3073  Flow f;
3074  uint8_t tlsbuf1[] = { 0x16 };
3075  uint32_t tlslen1 = sizeof(tlsbuf1);
3076  uint8_t tlsbuf2[] = { 0x03 };
3077  uint32_t tlslen2 = sizeof(tlsbuf2);
3078  uint8_t tlsbuf3[] = { 0x01 };
3079  uint32_t tlslen3 = sizeof(tlsbuf3);
3080  uint8_t tlsbuf4[] = { 0x01, 0x00, 0x00, 0xad, 0x03, 0x01 };
3081  uint32_t tlslen4 = sizeof(tlsbuf4);
3082  TcpSession ssn;
3084 
3085  memset(&f, 0, sizeof(f));
3086  memset(&ssn, 0, sizeof(ssn));
3087  FLOW_INITIALIZE(&f);
3088  f.protoctx = (void *)&ssn;
3089  f.proto = IPPROTO_TCP;
3090  f.alproto = ALPROTO_TLS;
3091 
3093 
3094  FLOWLOCK_WRLOCK(&f);
3095  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3096  STREAM_TOSERVER, tlsbuf1, tlslen1);
3097  FLOWLOCK_UNLOCK(&f);
3098  FAIL_IF(r != 0);
3099 
3100  FLOWLOCK_WRLOCK(&f);
3101  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3102  tlsbuf2, tlslen2);
3103  FLOWLOCK_UNLOCK(&f);
3104  FAIL_IF(r != 0);
3105 
3106  FLOWLOCK_WRLOCK(&f);
3107  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3108  tlsbuf3, tlslen3);
3109  FLOWLOCK_UNLOCK(&f);
3110  FAIL_IF(r != 0);
3111 
3112  FLOWLOCK_WRLOCK(&f);
3113  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3114  tlsbuf4, tlslen4);
3115  FLOWLOCK_UNLOCK(&f);
3116  FAIL_IF(r != 0);
3117 
3118  SSLState *ssl_state = f.alstate;
3119  FAIL_IF_NULL(ssl_state);
3120 
3121  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3122 
3123  FAIL_IF(ssl_state->client_connp.version != TLS_VERSION_10);
3124 
3125  AppLayerParserThreadCtxFree(alp_tctx);
3127  FLOW_DESTROY(&f);
3128 
3129  PASS;
3130 }
3131 
3132 #if 0
3133 /** \test Test the setting up of no reassembly and no payload inspection flag
3134  * after detection of the TLS handshake completion */
3135 static int SSLParserTest05(void)
3136 {
3137  int result = 1;
3138  Flow f;
3139  uint8_t tlsbuf[] = { 0x16, 0x03, 0x01, 0x00, 0x01 };
3140  uint32_t tlslen = sizeof(tlsbuf);
3141  TcpSession ssn;
3143 
3144  memset(&f, 0, sizeof(f));
3145  memset(&ssn, 0, sizeof(ssn));
3146  FLOW_INITIALIZE(&f);
3147  f.protoctx = (void *)&ssn;
3148  f.proto = IPPROTO_TCP;
3149  f.alproto = ALPROTO_TLS;
3150 
3152 
3153  int r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3154  if (r != 0) {
3155  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3156  result = 0;
3157  goto end;
3158  }
3159 
3160  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT, tlsbuf, tlslen);
3161  if (r != 0) {
3162  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3163  result = 0;
3164  goto end;
3165  }
3166 
3167  tlsbuf[0] = 0x14;
3168 
3169  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3170  if (r != 0) {
3171  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3172  result = 0;
3173  goto end;
3174  }
3175 
3176  tlsbuf[0] = 0x14;
3177 
3178  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT, tlsbuf, tlslen);
3179  if (r != 0) {
3180  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3181  result = 0;
3182  goto end;
3183  }
3184 
3185  tlsbuf[0] = 0x17;
3186 
3187  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3188  if (r != 0) {
3189  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3190  result = 0;
3191  goto end;
3192  }
3193 
3194  SSLState *ssl_state = f.alstate;
3195  if (ssl_state == NULL) {
3196  printf("no tls state: ");
3197  result = 0;
3198  goto end;
3199  }
3200 
3201  if (ssl_state->client_connp.content_type != 0x17) {
3202  printf("expected content_type %" PRIu8 ", got %" PRIu8 ": ", 0x17,
3203  ssl_state->client_connp.content_type);
3204  result = 0;
3205  goto end;
3206  }
3207 
3208  if (ssl_state->client_connp.version != TLS_VERSION_10) {
3209  printf("expected version %04" PRIu16 ", got %04" PRIu16 ": ",
3210  TLS_VERSION_10, ssl_state->client_connp.client_version);
3211  result = 0;
3212  goto end;
3213  }
3214 
3215  AppLayerParserStateStore *parser_state_store = (AppLayerParserStateStore *)
3216  ssn.alparser;
3217  AppLayerParserState *parser_state = &parser_state_store->to_server;
3218 
3219  if (!(parser_state->flags & APP_LAYER_PARSER_NO_INSPECTION) &&
3222  {
3223  printf("The flags should be set\n");
3224  result = 0;
3225  goto end;
3226  }
3227 
3228  if (!(f.flags & FLOW_NOPAYLOAD_INSPECTION)) {
3229  printf("The flags should be set\n");
3230  result = 0;
3231  goto end;
3232  }
3233 
3234 end:
3235  if (alp_tctx != NULL)
3236  AppLayerParserThreadCtxFree(alp_tctx);
3238  FLOW_DESTROY(&f);
3239  return result;
3240 }
3241 #endif
3242 
3243 #if 0
3244 /** \test Test the setting up of no reassembly and no payload inspection flag
3245  * after detection of the valid TLS handshake completion, the rouge
3246  * 0x17 packet will not be considered in the detection process */
3247 static int SSLParserTest06(void)
3248 {
3249  int result = 1;
3250  Flow f;
3251  uint8_t tlsbuf[] = { 0x16, 0x03, 0x01, 0x00, 0x01 };
3252  uint32_t tlslen = sizeof(tlsbuf);
3253  TcpSession ssn;
3255 
3256  memset(&f, 0, sizeof(f));
3257  memset(&ssn, 0, sizeof(ssn));
3258  FLOW_INITIALIZE(&f);
3259  f.protoctx = (void *)&ssn;
3260  f.proto = IPPROTO_TCP;
3261  f.alproto = ALPROTO_TLS;
3262 
3264 
3265  int r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3266  if (r != 0) {
3267  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3268  result = 0;
3269  goto end;
3270  }
3271 
3272  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT, tlsbuf, tlslen);
3273  if (r != 0) {
3274  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3275  result = 0;
3276  goto end;
3277  }
3278 
3279  tlsbuf[0] = 0x14;
3280 
3281  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3282  if (r != 0) {
3283  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3284  result = 0;
3285  goto end;
3286  }
3287 
3288  tlsbuf[0] = 0x17;
3289 
3290  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3291  if (r != 0) {
3292  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3293  result = 0;
3294  goto end;
3295  }
3296 
3297  SSLState *ssl_state = f.alstate;
3298  if (ssl_state == NULL) {
3299  printf("no tls state: ");
3300  result = 0;
3301  goto end;
3302  }
3303 
3304  if (ssl_state->client_connp.content_type != 0x17) {
3305  printf("expected content_type %" PRIu8 ", got %" PRIu8 ": ", 0x17,
3306  ssl_state->client_connp._content_type);
3307  result = 0;
3308  goto end;
3309  }
3310 
3311  if (ssl_state->client_connp.version != TLS_VERSION_10) {
3312  printf("expected version %04" PRIu16 ", got %04" PRIu16 ": ",
3313  TLS_VERSION_10, ssl_state->client_connp.version);
3314  result = 0;
3315  goto end;
3316  }
3317 
3318  AppLayerParserStateStore *parser_state_store = (AppLayerParserStateStore *)
3319  ssn.alparser;
3320  AppLayerParserState *parser_state = &parser_state_store->to_server;
3321 
3322  if ((parser_state->flags & APP_LAYER_PARSER_NO_INSPECTION) ||
3325  printf("The flags should not be set\n");
3326  result = 0;
3327  goto end;
3328  }
3329 
3330  tlsbuf[0] = 0x14;
3331 
3332  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT, tlsbuf, tlslen);
3333  if (r != 0) {
3334  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3335  result = 0;
3336  goto end;
3337  }
3338 
3339  tlsbuf[0] = 0x17;
3340 
3341  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3342  if (r != 0) {
3343  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3344  result = 0;
3345  goto end;
3346  }
3347 
3348  if (!(parser_state->flags & APP_LAYER_PARSER_NO_INSPECTION) &&
3351  printf("The flags should be set\n");
3352  result = 0;
3353  goto end;
3354  }
3355 
3356  if (!(f.flags & FLOW_NOPAYLOAD_INSPECTION)) {
3357  printf("The flags should be set\n");
3358  result = 0;
3359  goto end;
3360  }
3361 
3362 end:
3363  if (alp_tctx != NULL)
3364  AppLayerParserThreadCtxFree(alp_tctx);
3366  FLOW_DESTROY(&f);
3367  return result;
3368 }
3369 #endif
3370 
3371 /** \test multimsg test */
3372 static int SSLParserMultimsgTest01(void)
3373 {
3374  Flow f;
3375  /* 3 msgs */
3376  uint8_t tlsbuf1[] = {
3377  0x16, 0x03, 0x01, 0x00, 0x86, 0x10, 0x00, 0x00,
3378  0x82, 0x00, 0x80, 0xd3, 0x6f, 0x1f, 0x63, 0x82,
3379  0x8d, 0x75, 0x77, 0x8c, 0x91, 0xbc, 0xa1, 0x3d,
3380  0xbb, 0xe1, 0xb5, 0xd3, 0x31, 0x92, 0x59, 0x2b,
3381  0x2c, 0x43, 0x96, 0xa3, 0xaa, 0x23, 0x92, 0xd0,
3382  0x91, 0x2a, 0x5e, 0x10, 0x5b, 0xc8, 0xc1, 0xe2,
3383  0xd3, 0x5c, 0x8b, 0x8c, 0x91, 0x9e, 0xc2, 0xf2,
3384  0x9c, 0x3c, 0x4f, 0x37, 0x1e, 0x20, 0x5e, 0x33,
3385  0xd5, 0xf0, 0xd6, 0xaf, 0x89, 0xf5, 0xcc, 0xb2,
3386  0xcf, 0xc1, 0x60, 0x3a, 0x46, 0xd5, 0x4e, 0x2a,
3387  0xb6, 0x6a, 0xb9, 0xfc, 0x32, 0x8b, 0xe0, 0x6e,
3388  0xa0, 0xed, 0x25, 0xa0, 0xa4, 0x82, 0x81, 0x73,
3389  0x90, 0xbf, 0xb5, 0xde, 0xeb, 0x51, 0x8d, 0xde,
3390  0x5b, 0x6f, 0x94, 0xee, 0xba, 0xe5, 0x69, 0xfa,
3391  0x1a, 0x80, 0x30, 0x54, 0xeb, 0x12, 0x01, 0xb9,
3392  0xfe, 0xbf, 0x82, 0x95, 0x01, 0x7b, 0xb0, 0x97,
3393  0x14, 0xc2, 0x06, 0x3c, 0x69, 0xfb, 0x1c, 0x66,
3394  0x47, 0x17, 0xd9, 0x14, 0x03, 0x01, 0x00, 0x01,
3395  0x01, 0x16, 0x03, 0x01, 0x00, 0x30, 0xf6, 0xbc,
3396  0x0d, 0x6f, 0xe8, 0xbb, 0xaa, 0xbf, 0x14, 0xeb,
3397  0x7b, 0xcc, 0x6c, 0x28, 0xb0, 0xfc, 0xa6, 0x01,
3398  0x2a, 0x97, 0x96, 0x17, 0x5e, 0xe8, 0xb4, 0x4e,
3399  0x78, 0xc9, 0x04, 0x65, 0x53, 0xb6, 0x93, 0x3d,
3400  0xeb, 0x44, 0xee, 0x86, 0xf9, 0x80, 0x49, 0x45,
3401  0x21, 0x34, 0xd1, 0xee, 0xc8, 0x9c
3402  };
3403  uint32_t tlslen1 = sizeof(tlsbuf1);
3404  TcpSession ssn;
3406 
3407  memset(&f, 0, sizeof(f));
3408  memset(&ssn, 0, sizeof(ssn));
3409  FLOW_INITIALIZE(&f);
3410  f.protoctx = (void *)&ssn;
3411  f.proto = IPPROTO_TCP;
3412  f.alproto = ALPROTO_TLS;
3413 
3415 
3416  FLOWLOCK_WRLOCK(&f);
3417  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3418  STREAM_TOSERVER, tlsbuf1, tlslen1);
3419  FLOWLOCK_UNLOCK(&f);
3420  FAIL_IF(r != 0);
3421 
3422  SSLState *ssl_state = f.alstate;
3423  FAIL_IF_NULL(ssl_state);
3424 
3425  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3426 
3427  FAIL_IF(ssl_state->client_connp.version != TLS_VERSION_10);
3428 
3429  AppLayerParserThreadCtxFree(alp_tctx);
3431  FLOW_DESTROY(&f);
3432 
3433  PASS;
3434 }
3435 
3436 /** \test multimsg test server */
3437 static int SSLParserMultimsgTest02(void)
3438 {
3439  Flow f;
3440  /* 3 msgs */
3441  uint8_t tlsbuf1[] = {
3442  0x16, 0x03, 0x01, 0x00, 0x86, 0x10, 0x00, 0x00,
3443  0x82, 0x00, 0x80, 0xd3, 0x6f, 0x1f, 0x63, 0x82,
3444  0x8d, 0x75, 0x77, 0x8c, 0x91, 0xbc, 0xa1, 0x3d,
3445  0xbb, 0xe1, 0xb5, 0xd3, 0x31, 0x92, 0x59, 0x2b,
3446  0x2c, 0x43, 0x96, 0xa3, 0xaa, 0x23, 0x92, 0xd0,
3447  0x91, 0x2a, 0x5e, 0x10, 0x5b, 0xc8, 0xc1, 0xe2,
3448  0xd3, 0x5c, 0x8b, 0x8c, 0x91, 0x9e, 0xc2, 0xf2,
3449  0x9c, 0x3c, 0x4f, 0x37, 0x1e, 0x20, 0x5e, 0x33,
3450  0xd5, 0xf0, 0xd6, 0xaf, 0x89, 0xf5, 0xcc, 0xb2,
3451  0xcf, 0xc1, 0x60, 0x3a, 0x46, 0xd5, 0x4e, 0x2a,
3452  0xb6, 0x6a, 0xb9, 0xfc, 0x32, 0x8b, 0xe0, 0x6e,
3453  0xa0, 0xed, 0x25, 0xa0, 0xa4, 0x82, 0x81, 0x73,
3454  0x90, 0xbf, 0xb5, 0xde, 0xeb, 0x51, 0x8d, 0xde,
3455  0x5b, 0x6f, 0x94, 0xee, 0xba, 0xe5, 0x69, 0xfa,
3456  0x1a, 0x80, 0x30, 0x54, 0xeb, 0x12, 0x01, 0xb9,
3457  0xfe, 0xbf, 0x82, 0x95, 0x01, 0x7b, 0xb0, 0x97,
3458  0x14, 0xc2, 0x06, 0x3c, 0x69, 0xfb, 0x1c, 0x66,
3459  0x47, 0x17, 0xd9, 0x14, 0x03, 0x01, 0x00, 0x01,
3460  0x01, 0x16, 0x03, 0x01, 0x00, 0x30, 0xf6, 0xbc,
3461  0x0d, 0x6f, 0xe8, 0xbb, 0xaa, 0xbf, 0x14, 0xeb,
3462  0x7b, 0xcc, 0x6c, 0x28, 0xb0, 0xfc, 0xa6, 0x01,
3463  0x2a, 0x97, 0x96, 0x17, 0x5e, 0xe8, 0xb4, 0x4e,
3464  0x78, 0xc9, 0x04, 0x65, 0x53, 0xb6, 0x93, 0x3d,
3465  0xeb, 0x44, 0xee, 0x86, 0xf9, 0x80, 0x49, 0x45,
3466  0x21, 0x34, 0xd1, 0xee, 0xc8, 0x9c
3467  };
3468  uint32_t tlslen1 = sizeof(tlsbuf1);
3469  TcpSession ssn;
3471 
3472  memset(&f, 0, sizeof(f));
3473  memset(&ssn, 0, sizeof(ssn));
3474  FLOW_INITIALIZE(&f);
3475  f.protoctx = (void *)&ssn;
3476  f.proto = IPPROTO_TCP;
3477  f.alproto = ALPROTO_TLS;
3478 
3480 
3481  FLOWLOCK_WRLOCK(&f);
3482  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3483  STREAM_TOCLIENT, tlsbuf1, tlslen1);
3484  FLOWLOCK_UNLOCK(&f);
3485  FAIL_IF(r != 0);
3486 
3487  SSLState *ssl_state = f.alstate;
3488  FAIL_IF_NULL(ssl_state);
3489 
3490  FAIL_IF(ssl_state->server_connp.content_type != 0x16);
3491 
3492  FAIL_IF(ssl_state->server_connp.version != 0x0301);
3493 
3494  AppLayerParserThreadCtxFree(alp_tctx);
3496  FLOW_DESTROY(&f);
3497 
3498  PASS;
3499 }
3500 
3501 /**
3502  * \test Test the detection of SSLv3 protocol from the given packet
3503  */
3504 static int SSLParserTest07(void)
3505 {
3506  Flow f;
3507  uint8_t tlsbuf[] = { 0x16, 0x03, 0x00, 0x00, 0x4c, 0x01,
3508  0x00, 0x00, 0x48, 0x03, 0x00, 0x57, 0x04, 0x9f,
3509  0x8c, 0x66, 0x61, 0xf6, 0x3d, 0x4f, 0xbf, 0xbb,
3510  0xa7, 0x47, 0x21, 0x76, 0x6c, 0x21, 0x08, 0x9f,
3511  0xef, 0x3d, 0x0e, 0x5f, 0x65, 0x1a, 0xe1, 0x93,
3512  0xb8, 0xaf, 0xd2, 0x82, 0xbd, 0x00, 0x00, 0x06,
3513  0x00, 0x0a, 0x00, 0x16, 0x00, 0xff, 0x01, 0x00,
3514  0x00, 0x19, 0x00, 0x00, 0x00, 0x15, 0x00, 0x13,
3515  0x00, 0x00, 0x10, 0x61, 0x62, 0x63, 0x64, 0x65,
3516  0x66, 0x67, 0x68, 0x2e, 0x65, 0x66, 0x67, 0x68,
3517  0x2e, 0x6e, 0x6f };
3518  uint32_t tlslen = sizeof(tlsbuf);
3519  TcpSession ssn;
3521 
3522  memset(&f, 0, sizeof(f));
3523  memset(&ssn, 0, sizeof(ssn));
3524  FLOW_INITIALIZE(&f);
3525  f.protoctx = (void *)&ssn;
3526  f.proto = IPPROTO_TCP;
3527  f.alproto = ALPROTO_TLS;
3528 
3530 
3531  FLOWLOCK_WRLOCK(&f);
3532  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3533  STREAM_TOSERVER, tlsbuf, tlslen);
3534  FLOWLOCK_UNLOCK(&f);
3535  FAIL_IF(r != 0);
3536 
3537  SSLState *ssl_state = f.alstate;
3538  FAIL_IF_NULL(ssl_state);
3539 
3540  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3541 
3542  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
3543 
3544  AppLayerParserThreadCtxFree(alp_tctx);
3546  FLOW_DESTROY(&f);
3547 
3548  PASS;
3549 }
3550 
3551 #if 0
3552 /** \test Test the setting up of no reassembly and no payload inspection flag
3553  * after detection of the SSLv3 handshake completion */
3554 static int SSLParserTest08(void)
3555 {
3556  int result = 1;
3557  Flow f;
3558  uint8_t tlsbuf[] = { 0x16, 0x03, 0x00, 0x00, 0x01 };
3559  uint32_t tlslen = sizeof(tlsbuf);
3560  TcpSession ssn;
3562 
3563  memset(&f, 0, sizeof(f));
3564  memset(&ssn, 0, sizeof(ssn));
3565  FLOW_INITIALIZE(&f);
3566  f.protoctx = (void *)&ssn;
3567  f.proto = IPPROTO_TCP;
3568  f.alproto = ALPROTO_TLS;
3569 
3571 
3572  int r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3573  if (r != 0) {
3574  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3575  result = 0;
3576  goto end;
3577  }
3578 
3579  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT, tlsbuf, tlslen);
3580  if (r != 0) {
3581  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3582  result = 0;
3583  goto end;
3584  }
3585 
3586  tlsbuf[0] = 0x14;
3587 
3588  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3589  if (r != 0) {
3590  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3591  result = 0;
3592  goto end;
3593  }
3594 
3595  tlsbuf[0] = 0x14;
3596 
3597  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT, tlsbuf, tlslen);
3598  if (r != 0) {
3599  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3600  result = 0;
3601  goto end;
3602  }
3603 
3604  tlsbuf[0] = 0x17;
3605 
3606  r = AppLayerParserParse(alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER, tlsbuf, tlslen);
3607  if (r != 0) {
3608  printf("toserver chunk 1 returned %" PRId32 ", expected 0: ", r);
3609  result = 0;
3610  goto end;
3611  }
3612 
3613  SSLState *ssl_state = f.alstate;
3614  if (ssl_state == NULL) {
3615  printf("no tls state: ");
3616  result = 0;
3617  goto end;
3618  }
3619 
3620  if (ssl_state->client_connp.content_type != 0x17) {
3621  printf("expected content_type %" PRIu8 ", got %" PRIu8 ": ", 0x17,
3622  ssl_state->client_connp.content_type);
3623  result = 0;
3624  goto end;
3625  }
3626 
3627  if (ssl_state->client_connp.version != SSL_VERSION_3) {
3628  printf("expected version %04" PRIu16 ", got %04" PRIu16 ": ",
3629  SSL_VERSION_3, ssl_state->client_connp.version);
3630  result = 0;
3631  goto end;
3632  }
3633 
3634  AppLayerParserStateStore *parser_state_store = (AppLayerParserStateStore *)
3635  ssn.alparser;
3636  AppLayerParserState *parser_state = &parser_state_store->to_server;
3637 
3638  if (!(parser_state->flags & APP_LAYER_PARSER_NO_INSPECTION) &&
3641  printf("The flags should be set\n");
3642  result = 0;
3643  goto end;
3644  }
3645 
3646  if (!(f.flags & FLOW_NOPAYLOAD_INSPECTION)) {
3647  printf("The flags should be set\n");
3648  result = 0;
3649  goto end;
3650  }
3651 
3652 end:
3653  if (alp_tctx != NULL)
3654  AppLayerParserThreadCtxFree(alp_tctx);
3656  FLOW_DESTROY(&f);
3657  return result;
3658 }
3659 
3660 #endif
3661 
3662 /**
3663  * \test Tests the parser for handling fragmented records.
3664  */
3665 static int SSLParserTest09(void)
3666 {
3667  Flow f;
3668  uint8_t buf1[] = {
3669  0x16,
3670  };
3671  uint32_t buf1_len = sizeof(buf1);
3672 
3673  uint8_t buf2[] = {
3674  0x03, 0x00, 0x00, 0x4c, 0x01,
3675  0x00, 0x00, 0x48, 0x03, 0x00, 0x57, 0x04, 0x9f,
3676  0x8c, 0x66, 0x61, 0xf6, 0x3d, 0x4f, 0xbf, 0xbb,
3677  0xa7, 0x47, 0x21, 0x76, 0x6c, 0x21, 0x08, 0x9f,
3678  0xef, 0x3d, 0x0e, 0x5f, 0x65, 0x1a, 0xe1, 0x93,
3679  0xb8, 0xaf, 0xd2, 0x82, 0xbd, 0x00, 0x00, 0x06,
3680  0x00, 0x0a, 0x00, 0x16, 0x00, 0xff, 0x01, 0x00,
3681  0x00, 0x19, 0x00, 0x00, 0x00, 0x15, 0x00, 0x13,
3682  0x00, 0x00, 0x10, 0x61, 0x62, 0x63, 0x64, 0x65,
3683  0x66, 0x67, 0x68, 0x2e, 0x65, 0x66, 0x67, 0x68,
3684  0x2e, 0x6e, 0x6f
3685  };
3686  uint32_t buf2_len = sizeof(buf2);
3687  TcpSession ssn;
3689 
3690  memset(&f, 0, sizeof(f));
3691  memset(&ssn, 0, sizeof(ssn));
3692  FLOW_INITIALIZE(&f);
3693  f.protoctx = (void *)&ssn;
3694  f.proto = IPPROTO_TCP;
3695  f.alproto = ALPROTO_TLS;
3696 
3698 
3699  FLOWLOCK_WRLOCK(&f);
3700  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3701  STREAM_TOSERVER, buf1, buf1_len);
3702  FLOWLOCK_UNLOCK(&f);
3703  FAIL_IF(r != 0);
3704 
3705  FLOWLOCK_WRLOCK(&f);
3706  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3707  buf2, buf2_len);
3708  FLOWLOCK_UNLOCK(&f);
3709  FAIL_IF(r != 0);
3710 
3711  SSLState *ssl_state = f.alstate;
3712  FAIL_IF_NULL(ssl_state);
3713 
3714  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3715 
3716  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
3717 
3718  AppLayerParserThreadCtxFree(alp_tctx);
3720  FLOW_DESTROY(&f);
3721 
3722  PASS;
3723 }
3724 
3725 /**
3726  * \test Tests the parser for handling fragmented records.
3727  */
3728 static int SSLParserTest10(void)
3729 {
3730  Flow f;
3731  uint8_t buf1[] = {
3732  0x16, 0x03,
3733  };
3734  uint32_t buf1_len = sizeof(buf1);
3735 
3736  uint8_t buf2[] = {
3737  0x00, 0x00, 0x4c, 0x01,
3738  0x00, 0x00, 0x48, 0x03, 0x00, 0x57, 0x04, 0x9f,
3739  0x8c, 0x66, 0x61, 0xf6, 0x3d, 0x4f, 0xbf, 0xbb,
3740  0xa7, 0x47, 0x21, 0x76, 0x6c, 0x21, 0x08, 0x9f,
3741  0xef, 0x3d, 0x0e, 0x5f, 0x65, 0x1a, 0xe1, 0x93,
3742  0xb8, 0xaf, 0xd2, 0x82, 0xbd, 0x00, 0x00, 0x06,
3743  0x00, 0x0a, 0x00, 0x16, 0x00, 0xff, 0x01, 0x00,
3744  0x00, 0x19, 0x00, 0x00, 0x00, 0x15, 0x00, 0x13,
3745  0x00, 0x00, 0x10, 0x61, 0x62, 0x63, 0x64, 0x65,
3746  0x66, 0x67, 0x68, 0x2e, 0x65, 0x66, 0x67, 0x68,
3747  0x2e, 0x6e, 0x6f
3748  };
3749  uint32_t buf2_len = sizeof(buf2);
3750  TcpSession ssn;
3752 
3753  memset(&f, 0, sizeof(f));
3754  memset(&ssn, 0, sizeof(ssn));
3755  FLOW_INITIALIZE(&f);
3756  f.protoctx = (void *)&ssn;
3757  f.proto = IPPROTO_TCP;
3758  f.alproto = ALPROTO_TLS;
3759 
3761 
3762  FLOWLOCK_WRLOCK(&f);
3763  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3764  STREAM_TOSERVER, buf1, buf1_len);
3765  FLOWLOCK_UNLOCK(&f);
3766  FAIL_IF(r != 0);
3767 
3768  FLOWLOCK_WRLOCK(&f);
3769  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3770  buf2, buf2_len);
3771  FLOWLOCK_UNLOCK(&f);
3772  FAIL_IF(r != 0);
3773 
3774  SSLState *ssl_state = f.alstate;
3775  FAIL_IF_NULL(ssl_state);
3776 
3777  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3778 
3779  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
3780 
3781  AppLayerParserThreadCtxFree(alp_tctx);
3783  FLOW_DESTROY(&f);
3784 
3785  PASS;
3786 }
3787 
3788 /**
3789  * \test Tests the parser for handling fragmented records.
3790  */
3791 static int SSLParserTest11(void)
3792 {
3793  Flow f;
3794  uint8_t buf1[] = {
3795  0x16, 0x03, 0x00, 0x00, 0x4c, 0x01,
3796  };
3797  uint32_t buf1_len = sizeof(buf1);
3798 
3799  uint8_t buf2[] = {
3800  0x00, 0x00, 0x48, 0x03, 0x00, 0x57, 0x04, 0x9f,
3801  0x8c, 0x66, 0x61, 0xf6, 0x3d, 0x4f, 0xbf, 0xbb,
3802  0xa7, 0x47, 0x21, 0x76, 0x6c, 0x21, 0x08, 0x9f,
3803  0xef, 0x3d, 0x0e, 0x5f, 0x65, 0x1a, 0xe1, 0x93,
3804  0xb8, 0xaf, 0xd2, 0x82, 0xbd, 0x00, 0x00, 0x06,
3805  0x00, 0x0a, 0x00, 0x16, 0x00, 0xff, 0x01, 0x00,
3806  0x00, 0x19, 0x00, 0x00, 0x00, 0x15, 0x00, 0x13,
3807  0x00, 0x00, 0x10, 0x61, 0x62, 0x63, 0x64, 0x65,
3808  0x66, 0x67, 0x68, 0x2e, 0x65, 0x66, 0x67, 0x68,
3809  0x2e, 0x6e, 0x6f
3810  };
3811  uint32_t buf2_len = sizeof(buf2);
3812  TcpSession ssn;
3814 
3815  memset(&f, 0, sizeof(f));
3816  memset(&ssn, 0, sizeof(ssn));
3817  FLOW_INITIALIZE(&f);
3818  f.protoctx = (void *)&ssn;
3819  f.proto = IPPROTO_TCP;
3820  f.alproto = ALPROTO_TLS;
3821 
3823 
3824  FLOWLOCK_WRLOCK(&f);
3825  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3826  STREAM_TOSERVER, buf1, buf1_len);
3827  FLOWLOCK_UNLOCK(&f);
3828  FAIL_IF(r != 0);
3829 
3830  FLOWLOCK_WRLOCK(&f);
3831  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3832  buf2, buf2_len);
3833  FLOWLOCK_UNLOCK(&f);
3834  FAIL_IF(r != 0);
3835 
3836  SSLState *ssl_state = f.alstate;
3837  FAIL_IF_NULL(ssl_state);
3838 
3839  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3840 
3841  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
3842 
3843  AppLayerParserThreadCtxFree(alp_tctx);
3845  FLOW_DESTROY(&f);
3846 
3847  PASS;
3848 }
3849 
3850 /**
3851  * \test Tests the parser for handling fragmented records.
3852  */
3853 static int SSLParserTest12(void)
3854 {
3855  Flow f;
3856  uint8_t buf1[] = {
3857  0x16, 0x03, 0x00, 0x00, 0x4c, 0x01,
3858  };
3859  uint32_t buf1_len = sizeof(buf1);
3860 
3861  uint8_t buf2[] = {
3862  0x00, 0x00, 0x48,
3863  };
3864  uint32_t buf2_len = sizeof(buf2);
3865 
3866  uint8_t buf3[] = {
3867  0x03, 0x00, 0x57, 0x04, 0x9f,
3868  0x8c, 0x66, 0x61, 0xf6, 0x3d, 0x4f, 0xbf, 0xbb,
3869  0xa7, 0x47, 0x21, 0x76, 0x6c, 0x21, 0x08, 0x9f,
3870  0xef, 0x3d, 0x0e, 0x5f, 0x65, 0x1a, 0xe1, 0x93,
3871  0xb8, 0xaf, 0xd2, 0x82, 0xbd, 0x00, 0x00, 0x06,
3872  0x00, 0x0a, 0x00, 0x16, 0x00, 0xff, 0x01, 0x00,
3873  0x00, 0x19, 0x00, 0x00, 0x00, 0x15, 0x00, 0x13,
3874  0x00, 0x00, 0x10, 0x61, 0x62, 0x63, 0x64, 0x65,
3875  0x66, 0x67, 0x68, 0x2e, 0x65, 0x66, 0x67, 0x68,
3876  0x2e, 0x6e, 0x6f
3877  };
3878  uint32_t buf3_len = sizeof(buf2);
3879  TcpSession ssn;
3881 
3882  memset(&f, 0, sizeof(f));
3883  memset(&ssn, 0, sizeof(ssn));
3884  FLOW_INITIALIZE(&f);
3885  f.protoctx = (void *)&ssn;
3886  f.proto = IPPROTO_TCP;
3887  f.alproto = ALPROTO_TLS;
3888 
3890 
3891  FLOWLOCK_WRLOCK(&f);
3892  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3893  STREAM_TOSERVER, buf1, buf1_len);
3894  FLOWLOCK_UNLOCK(&f);
3895  FAIL_IF(r != 0);
3896 
3897  FLOWLOCK_WRLOCK(&f);
3898  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3899  buf2, buf2_len);
3900  FLOWLOCK_UNLOCK(&f);
3901  FAIL_IF(r != 0);
3902 
3903  FLOWLOCK_WRLOCK(&f);
3904  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3905  buf3, buf3_len);
3906  FLOWLOCK_UNLOCK(&f);
3907  FAIL_IF(r != 0);
3908 
3909  SSLState *ssl_state = f.alstate;
3910  FAIL_IF_NULL(ssl_state);
3911 
3912  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3913 
3914  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
3915 
3916  AppLayerParserThreadCtxFree(alp_tctx);
3918  FLOW_DESTROY(&f);
3919 
3920  PASS;
3921 }
3922 
3923 /**
3924  * \test Tests the parser for handling fragmented records.
3925  */
3926 static int SSLParserTest13(void)
3927 {
3928  Flow f;
3929  uint8_t buf1[] = {
3930  0x16, 0x03, 0x00, 0x00, 0x4c, 0x01,
3931  };
3932  uint32_t buf1_len = sizeof(buf1);
3933 
3934  uint8_t buf2[] = {
3935  0x00, 0x00, 0x48,
3936  };
3937  uint32_t buf2_len = sizeof(buf2);
3938 
3939  uint8_t buf3[] = {
3940  0x03, 0x00, 0x57, 0x04, 0x9f,
3941  0x8c, 0x66, 0x61, 0xf6, 0x3d, 0x4f,
3942  };
3943  uint32_t buf3_len = sizeof(buf3);
3944 
3945  uint8_t buf4[] = {
3946  0xbf, 0xbb,
3947  0xa7, 0x47, 0x21, 0x76, 0x6c, 0x21, 0x08, 0x9f,
3948  0xef, 0x3d, 0x0e, 0x5f, 0x65, 0x1a, 0xe1, 0x93,
3949  0xb8, 0xaf, 0xd2, 0x82, 0xbd, 0x00, 0x00, 0x06,
3950  0x00, 0x0a, 0x00, 0x16, 0x00, 0xff, 0x01, 0x00,
3951  0x00, 0x19, 0x00, 0x00, 0x00, 0x15, 0x00, 0x13,
3952  0x00, 0x00, 0x10, 0x61, 0x62, 0x63, 0x64, 0x65,
3953  0x66, 0x67, 0x68, 0x2e, 0x65, 0x66, 0x67, 0x68,
3954  0x2e, 0x6e, 0x6f
3955  };
3956  uint32_t buf4_len = sizeof(buf4);
3957  TcpSession ssn;
3959 
3960  memset(&f, 0, sizeof(f));
3961  memset(&ssn, 0, sizeof(ssn));
3962  FLOW_INITIALIZE(&f);
3963  f.protoctx = (void *)&ssn;
3964  f.proto = IPPROTO_TCP;
3965  f.alproto = ALPROTO_TLS;
3966 
3968 
3969  FLOWLOCK_WRLOCK(&f);
3970  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
3971  STREAM_TOSERVER, buf1, buf1_len);
3972  FLOWLOCK_UNLOCK(&f);
3973  FAIL_IF(r != 0);
3974 
3975  FLOWLOCK_WRLOCK(&f);
3976  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3977  buf2, buf2_len);
3978  FLOWLOCK_UNLOCK(&f);
3979  FAIL_IF(r != 0);
3980 
3981  FLOWLOCK_WRLOCK(&f);
3982  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3983  buf3, buf3_len);
3984  FLOWLOCK_UNLOCK(&f);
3985  FAIL_IF(r != 0);
3986 
3987  FLOWLOCK_WRLOCK(&f);
3988  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
3989  buf4, buf4_len);
3990  FLOWLOCK_UNLOCK(&f);
3991  FAIL_IF(r != 0);
3992 
3993  SSLState *ssl_state = f.alstate;
3994  FAIL_IF_NULL(ssl_state);
3995 
3996  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
3997 
3998  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
3999 
4000  AppLayerParserThreadCtxFree(alp_tctx);
4002  FLOW_DESTROY(&f);
4003 
4004  PASS;
4005 }
4006 
4007 /**
4008  * \test Tests the parser for handling fragmented records.
4009  */
4010 static int SSLParserTest14(void)
4011 {
4012  Flow f;
4013 
4014  uint8_t buf1[] = {
4015  0x16, 0x03, 0x00, 0x00, 0x00,
4016  };
4017  uint32_t buf1_len = sizeof(buf1);
4018 
4019  uint8_t buf2[] = {
4020  0x16, 0x03, 0x00, 0x00, 0x00,
4021  };
4022  uint32_t buf2_len = sizeof(buf2);
4023 
4024  TcpSession ssn;
4026 
4027  memset(&f, 0, sizeof(f));
4028  memset(&ssn, 0, sizeof(ssn));
4029  FLOW_INITIALIZE(&f);
4030  f.protoctx = (void *)&ssn;
4031  f.proto = IPPROTO_TCP;
4032  f.alproto = ALPROTO_TLS;
4033 
4035 
4036  FLOWLOCK_WRLOCK(&f);
4037  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4038  STREAM_TOSERVER, buf1, buf1_len);
4039  FLOWLOCK_UNLOCK(&f);
4040  FAIL_IF(r != 0);
4041 
4042  FLOWLOCK_WRLOCK(&f);
4043  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4044  buf2, buf2_len);
4045  FLOWLOCK_UNLOCK(&f);
4046  FAIL_IF(r != 0);
4047 
4048  SSLState *ssl_state = f.alstate;
4049  FAIL_IF_NULL(ssl_state);
4050 
4051  AppLayerParserThreadCtxFree(alp_tctx);
4053  FLOW_DESTROY(&f);
4054 
4055  PASS;
4056 }
4057 
4058 /**
4059  * \test Tests the parser for handling fragmented records.
4060  */
4061 static int SSLParserTest15(void)
4062 {
4063  Flow f;
4064 
4065  uint8_t buf1[] = {
4066  0x16, 0x03, 0x00, 0x00, 0x01, 0x01,
4067  };
4068  uint32_t buf1_len = sizeof(buf1);
4069 
4070  TcpSession ssn;
4072 
4073  memset(&f, 0, sizeof(f));
4074  memset(&ssn, 0, sizeof(ssn));
4075  FLOW_INITIALIZE(&f);
4076  f.protoctx = (void *)&ssn;
4077  f.proto = IPPROTO_TCP;
4078  f.alproto = ALPROTO_TLS;
4079 
4081 
4082  FLOWLOCK_WRLOCK(&f);
4083  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4084  STREAM_TOSERVER, buf1, buf1_len);
4085  FLOWLOCK_UNLOCK(&f);
4086  FAIL_IF(r == 0);
4087 
4088  AppLayerParserThreadCtxFree(alp_tctx);
4090  FLOW_DESTROY(&f);
4091 
4092  PASS;
4093 }
4094 
4095 /**
4096  * \test Tests the parser for handling fragmented records.
4097  */
4098 static int SSLParserTest16(void)
4099 {
4100  Flow f;
4101 
4102  uint8_t buf1[] = {
4103  0x16, 0x03, 0x00, 0x00, 0x02, 0x01, 0x00
4104  };
4105  uint32_t buf1_len = sizeof(buf1);
4106 
4107  TcpSession ssn;
4109 
4110  memset(&f, 0, sizeof(f));
4111  memset(&ssn, 0, sizeof(ssn));
4112  FLOW_INITIALIZE(&f);
4113  f.protoctx = (void *)&ssn;
4114  f.proto = IPPROTO_TCP;
4115  f.alproto = ALPROTO_TLS;
4116 
4118 
4119  FLOWLOCK_WRLOCK(&f);
4120  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4121  STREAM_TOSERVER, buf1, buf1_len);
4122  FLOWLOCK_UNLOCK(&f);
4123  FAIL_IF(r == 0);
4124 
4125  AppLayerParserThreadCtxFree(alp_tctx);
4127  FLOW_DESTROY(&f);
4128 
4129  PASS;
4130 }
4131 
4132 /**
4133  * \test Tests the parser for handling fragmented records.
4134  */
4135 static int SSLParserTest17(void)
4136 {
4137  Flow f;
4138 
4139  uint8_t buf1[] = {
4140  0x16, 0x03, 0x00, 0x00, 0x03, 0x01, 0x00, 0x00
4141  };
4142  uint32_t buf1_len = sizeof(buf1);
4143 
4144  TcpSession ssn;
4146 
4147  memset(&f, 0, sizeof(f));
4148  memset(&ssn, 0, sizeof(ssn));
4149  FLOW_INITIALIZE(&f);
4150  f.protoctx = (void *)&ssn;
4151  f.proto = IPPROTO_TCP;
4152  f.alproto = ALPROTO_TLS;
4153 
4155 
4156  FLOWLOCK_WRLOCK(&f);
4157  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4158  STREAM_TOSERVER, buf1, buf1_len);
4159  FLOWLOCK_UNLOCK(&f);
4160  FAIL_IF(r == 0);
4161 
4162  AppLayerParserThreadCtxFree(alp_tctx);
4164  FLOW_DESTROY(&f);
4165 
4166  PASS;
4167 }
4168 
4169 /**
4170  * \test Tests the parser for handling fragmented records.
4171  */
4172 static int SSLParserTest18(void)
4173 {
4174  Flow f;
4175 
4176  uint8_t buf1[] = {
4177  0x16, 0x03, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00,
4178  0x6b,
4179  };
4180  uint32_t buf1_len = sizeof(buf1);
4181 
4182  uint8_t buf2[] = {
4183  0x16, 0x03, 0x00, 0x00, 0x00,
4184  };
4185  uint32_t buf2_len = sizeof(buf2);
4186 
4187  TcpSession ssn;
4189 
4190  memset(&f, 0, sizeof(f));
4191  memset(&ssn, 0, sizeof(ssn));
4192  FLOW_INITIALIZE(&f);
4193  f.protoctx = (void *)&ssn;
4194  f.proto = IPPROTO_TCP;
4195  f.alproto = ALPROTO_TLS;
4196 
4198 
4199  FLOWLOCK_WRLOCK(&f);
4200  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4201  STREAM_TOSERVER, buf1, buf1_len);
4202  FLOWLOCK_UNLOCK(&f);
4203  FAIL_IF(r != 0);
4204 
4205  FLOWLOCK_WRLOCK(&f);
4206  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4207  buf2, buf2_len);
4208  FLOWLOCK_UNLOCK(&f);
4209  FAIL_IF(r != 0);
4210 
4211  SSLState *ssl_state = f.alstate;
4212  FAIL_IF_NULL(ssl_state);
4213 
4214  AppLayerParserThreadCtxFree(alp_tctx);
4216  FLOW_DESTROY(&f);
4217 
4218  PASS;
4219 }
4220 
4221 /**
4222  * \test Tests the parser for handling fragmented records.
4223  */
4224 static int SSLParserTest19(void)
4225 {
4226  Flow f;
4227 
4228  uint8_t buf1[] = {
4229  0x16, 0x03, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00,
4230  0x6b, 0x16, 0x03, 0x00, 0x00, 0x00,
4231  };
4232  uint32_t buf1_len = sizeof(buf1);
4233 
4234  TcpSession ssn;
4236 
4237  memset(&f, 0, sizeof(f));
4238  memset(&ssn, 0, sizeof(ssn));
4239  FLOW_INITIALIZE(&f);
4240  f.protoctx = (void *)&ssn;
4241  f.proto = IPPROTO_TCP;
4242  f.alproto = ALPROTO_TLS;
4243 
4245 
4246  FLOWLOCK_WRLOCK(&f);
4247  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4248  STREAM_TOSERVER, buf1, buf1_len);
4249  FLOWLOCK_UNLOCK(&f);
4250  FAIL_IF(r != 0);
4251 
4252  SSLState *ssl_state = f.alstate;
4253  FAIL_IF_NULL(ssl_state);
4254 
4255  AppLayerParserThreadCtxFree(alp_tctx);
4257  FLOW_DESTROY(&f);
4258 
4259  PASS;
4260 }
4261 
4262 /**
4263  * \test Tests the parser for handling fragmented records.
4264  */
4265 static int SSLParserTest20(void)
4266 {
4267  Flow f;
4268 
4269  uint8_t buf1[] = {
4270  0x16, 0x03, 0x00, 0x00, 0x03, 0x01, 0x00, 0x00,
4271  0x16, 0x03, 0x00, 0x00, 0x00,
4272  };
4273  uint32_t buf1_len = sizeof(buf1);
4274 
4275  TcpSession ssn;
4277 
4278  memset(&f, 0, sizeof(f));
4279  memset(&ssn, 0, sizeof(ssn));
4280  FLOW_INITIALIZE(&f);
4281  f.protoctx = (void *)&ssn;
4282  f.proto = IPPROTO_TCP;
4283  f.alproto = ALPROTO_TLS;
4284 
4286 
4287  FLOWLOCK_WRLOCK(&f);
4288  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4289  STREAM_TOSERVER, buf1, buf1_len);
4290  FLOWLOCK_UNLOCK(&f);
4291  FAIL_IF(r == 0);
4292 
4293  AppLayerParserThreadCtxFree(alp_tctx);
4295  FLOW_DESTROY(&f);
4296 
4297  PASS;
4298 }
4299 
4300 /**
4301  * \test SSLv2 Record parsing.
4302  */
4303 static int SSLParserTest21(void)
4304 {
4305  Flow f;
4306  uint8_t buf[] = {
4307  0x80, 0x31, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00,
4308  0x01,
4309  };
4310  uint32_t buf_len = sizeof(buf);
4311 
4312  TcpSession ssn;
4314 
4315  memset(&f, 0, sizeof(f));
4316  memset(&ssn, 0, sizeof(ssn));
4317 
4318  FLOW_INITIALIZE(&f);
4319  f.protoctx = (void *)&ssn;
4320  f.proto = IPPROTO_TCP;
4321  f.alproto = ALPROTO_TLS;
4322 
4324 
4325  FLOWLOCK_WRLOCK(&f);
4326  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4327  STREAM_TOSERVER | STREAM_EOF, buf, buf_len);
4328  FLOWLOCK_UNLOCK(&f);
4329  FAIL_IF(r != 0);
4330 
4331  SSLState *app_state = f.alstate;
4332  FAIL_IF_NULL(app_state);
4333 
4335 
4336  FAIL_IF(app_state->client_connp.version != SSL_VERSION_2);
4337 
4338  AppLayerParserThreadCtxFree(alp_tctx);
4340  FLOW_DESTROY(&f);
4341 
4342  PASS;
4343 }
4344 
4345 /**
4346  * \test SSLv2 Record parsing.
4347  */
4348 static int SSLParserTest22(void)
4349 {
4350  Flow f;
4351  uint8_t buf[] = {
4352  0x80, 0x31, 0x04, 0x00, 0x01, 0x00,
4353  0x02, 0x00, 0x00, 0x00, 0x10, 0x07, 0x00, 0xc0,
4354  0x05, 0x00, 0x80, 0x03, 0x00, 0x80, 0x01, 0x00,
4355  0x80, 0x08, 0x00, 0x80, 0x06, 0x00, 0x40, 0x04,
4356  0x00, 0x80, 0x02, 0x00, 0x80, 0x76, 0x64, 0x75,
4357  0x2d, 0xa7, 0x98, 0xfe, 0xc9, 0x12, 0x92, 0xc1,
4358  0x2f, 0x34, 0x84, 0x20, 0xc5};
4359  uint32_t buf_len = sizeof(buf);
4360  TcpSession ssn;
4362 
4363  //AppLayerDetectProtoThreadInit();
4364 
4365  memset(&f, 0, sizeof(f));
4366  memset(&ssn, 0, sizeof(ssn));
4367 
4368  FLOW_INITIALIZE(&f);
4369  f.protoctx = (void *)&ssn;
4370  f.proto = IPPROTO_TCP;
4371  f.alproto = ALPROTO_TLS;
4372 
4374 
4375  FLOWLOCK_WRLOCK(&f);
4376  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4377  STREAM_TOCLIENT | STREAM_EOF, buf, buf_len);
4378  FLOWLOCK_UNLOCK(&f);
4379  FAIL_IF(r != 0);
4380 
4381  SSLState *app_state = f.alstate;
4382  FAIL_IF_NULL(app_state);
4383 
4385 
4386  FAIL_IF(app_state->server_connp.version != SSL_VERSION_2);
4387 
4388  AppLayerParserThreadCtxFree(alp_tctx);
4390  FLOW_DESTROY(&f);
4391 
4392  PASS;
4393 }
4394 
4395 /**
4396  * \test SSLv2 Record parsing.
4397  */
4398 static int SSLParserTest23(void)
4399 {
4400  Flow f;
4401  uint8_t chello_buf[] = {
4402  0x80, 0x67, 0x01, 0x03, 0x00, 0x00, 0x4e, 0x00,
4403  0x00, 0x00, 0x10, 0x01, 0x00, 0x80, 0x03, 0x00,
4404  0x80, 0x07, 0x00, 0xc0, 0x06, 0x00, 0x40, 0x02,
4405  0x00, 0x80, 0x04, 0x00, 0x80, 0x00, 0x00, 0x39,
4406  0x00, 0x00, 0x38, 0x00, 0x00, 0x35, 0x00, 0x00,
4407  0x33, 0x00, 0x00, 0x32, 0x00, 0x00, 0x04, 0x00,
4408  0x00, 0x05, 0x00, 0x00, 0x2f, 0x00, 0x00, 0x16,
4409  0x00, 0x00, 0x13, 0x00, 0xfe, 0xff, 0x00, 0x00,
4410  0x0a, 0x00, 0x00, 0x15, 0x00, 0x00, 0x12, 0x00,
4411  0xfe, 0xfe, 0x00, 0x00, 0x09, 0x00, 0x00, 0x64,
4412  0x00, 0x00, 0x62, 0x00, 0x00, 0x03, 0x00, 0x00,
4413  0x06, 0xa8, 0xb8, 0x93, 0xbb, 0x90, 0xe9, 0x2a,
4414  0xa2, 0x4d, 0x6d, 0xcc, 0x1c, 0xe7, 0x2a, 0x80,
4415  0x21
4416  };
4417  uint32_t chello_buf_len = sizeof(chello_buf);
4418 
4419  uint8_t shello_buf[] = {
4420  0x16, 0x03, 0x00, 0x00, 0x4a, 0x02,
4421  0x00, 0x00, 0x46, 0x03, 0x00, 0x44, 0x4c, 0x94,
4422  0x8f, 0xfe, 0x81, 0xed, 0x93, 0x65, 0x02, 0x88,
4423  0xa3, 0xf8, 0xeb, 0x63, 0x86, 0x0e, 0x2c, 0xf6,
4424  0x8d, 0xd0, 0x0f, 0x2c, 0x2a, 0xd6, 0x4f, 0xcd,
4425  0x2d, 0x3c, 0x16, 0xd7, 0xd6, 0x20, 0xa0, 0xfb,
4426  0x60, 0x86, 0x3d, 0x1e, 0x76, 0xf3, 0x30, 0xfe,
4427  0x0b, 0x01, 0xfd, 0x1a, 0x01, 0xed, 0x95, 0xf6,
4428  0x7b, 0x8e, 0xc0, 0xd4, 0x27, 0xbf, 0xf0, 0x6e,
4429  0xc7, 0x56, 0xb1, 0x47, 0xce, 0x98, 0x00, 0x35,
4430  0x00, 0x16, 0x03, 0x00, 0x03, 0x44, 0x0b, 0x00,
4431  0x03, 0x40, 0x00, 0x03, 0x3d, 0x00, 0x03, 0x3a,
4432  0x30, 0x82, 0x03, 0x36, 0x30, 0x82, 0x02, 0x9f,
4433  0xa0, 0x03, 0x02, 0x01, 0x02, 0x02, 0x01, 0x01,
4434  0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
4435  0xf7, 0x0d, 0x01, 0x01, 0x04, 0x05, 0x00, 0x30,
4436  0x81, 0xa9, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03,
4437  0x55, 0x04, 0x06, 0x13, 0x02, 0x58, 0x59, 0x31,
4438  0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x08,
4439  0x13, 0x0c, 0x53, 0x6e, 0x61, 0x6b, 0x65, 0x20,
4440  0x44, 0x65, 0x73, 0x65, 0x72, 0x74, 0x31, 0x13,
4441  0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x07, 0x13,
4442  0x0a, 0x53, 0x6e, 0x61, 0x6b, 0x65, 0x20, 0x54,
4443  0x6f, 0x77, 0x6e, 0x31, 0x17, 0x30, 0x15, 0x06,
4444  0x03, 0x55, 0x04, 0x0a, 0x13, 0x0e, 0x53, 0x6e,
4445  0x61, 0x6b, 0x65, 0x20, 0x4f, 0x69, 0x6c, 0x2c,
4446  0x20, 0x4c, 0x74, 0x64, 0x31, 0x1e, 0x30, 0x1c,
4447  0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x15, 0x43,
4448  0x65, 0x72, 0x74, 0x69, 0x66, 0x69, 0x63, 0x61,
4449  0x74, 0x65, 0x20, 0x41, 0x75, 0x74, 0x68, 0x6f,
4450  0x72, 0x69, 0x74, 0x79, 0x31, 0x15, 0x30, 0x13,
4451  0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x0c, 0x53,
4452  0x6e, 0x61, 0x6b, 0x65, 0x20, 0x4f, 0x69, 0x6c,
4453  0x20, 0x43, 0x41, 0x31, 0x1e, 0x30, 0x1c, 0x06,
4454  0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
4455  0x09, 0x01, 0x16, 0x0f, 0x63, 0x61, 0x40, 0x73,
4456  0x6e, 0x61, 0x6b, 0x65, 0x6f, 0x69, 0x6c, 0x2e,
4457  0x64, 0x6f, 0x6d, 0x30, 0x1e, 0x17, 0x0d, 0x30,
4458  0x33, 0x30, 0x33, 0x30, 0x35, 0x31, 0x36, 0x34,
4459  0x37, 0x34, 0x35, 0x5a, 0x17, 0x0d, 0x30, 0x38,
4460  0x30, 0x33, 0x30, 0x33, 0x31, 0x36, 0x34, 0x37,
4461  0x34, 0x35, 0x5a, 0x30, 0x81, 0xa7, 0x31, 0x0b,
4462  0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
4463  0x02, 0x58, 0x59, 0x31, 0x15, 0x30, 0x13, 0x06,
4464  0x03, 0x55, 0x04, 0x08, 0x13, 0x0c, 0x53, 0x6e,
4465  0x61, 0x6b, 0x65, 0x20, 0x44, 0x65, 0x73, 0x65,
4466  0x72, 0x74, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03,
4467  0x55, 0x04, 0x07, 0x13, 0x0a, 0x53, 0x6e, 0x61,
4468  0x6b, 0x65, 0x20, 0x54, 0x6f, 0x77, 0x6e, 0x31,
4469  0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x0a,
4470  0x13, 0x0e, 0x53, 0x6e, 0x61, 0x6b, 0x65, 0x20,
4471  0x4f, 0x69, 0x6c, 0x2c, 0x20, 0x4c, 0x74, 0x64,
4472  0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04,
4473  0x0b, 0x13, 0x0e, 0x57, 0x65, 0x62, 0x73, 0x65,
4474  0x72, 0x76, 0x65, 0x72, 0x20, 0x54, 0x65, 0x61,
4475  0x6d, 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55,
4476  0x04, 0x03, 0x13, 0x10, 0x77, 0x77, 0x77, 0x2e,
4477  0x73, 0x6e, 0x61, 0x6b, 0x65, 0x6f, 0x69, 0x6c,
4478  0x2e, 0x64, 0x6f, 0x6d, 0x31, 0x1f, 0x30, 0x1d,
4479  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
4480  0x01, 0x09, 0x01, 0x16, 0x10, 0x77, 0x77, 0x77,
4481  0x40, 0x73, 0x6e, 0x61, 0x6b, 0x65, 0x6f, 0x69,
4482  0x6c, 0x2e, 0x64, 0x6f, 0x6d, 0x30, 0x81, 0x9f,
4483  0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
4484  0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03,
4485  0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02, 0x81,
4486  0x81, 0x00, 0xa4, 0x6e, 0x53, 0x14, 0x0a, 0xde,
4487  0x2c, 0xe3, 0x60, 0x55, 0x9a, 0xf2, 0x42, 0xa6,
4488  0xaf, 0x47, 0x12, 0x2f, 0x17, 0xce, 0xfa, 0xba,
4489  0xdc, 0x4e, 0x63, 0x56, 0x34, 0xb9, 0xba, 0x73,
4490  0x4b, 0x78, 0x44, 0x3d, 0xc6, 0x6c, 0x69, 0xa4,
4491  0x25, 0xb3, 0x61, 0x02, 0x9d, 0x09, 0x04, 0x3f,
4492  0x72, 0x3d, 0xd8, 0x27, 0xd3, 0xb0, 0x5a, 0x45,
4493  0x77, 0xb7, 0x36, 0xe4, 0x26, 0x23, 0xcc, 0x12,
4494  0xb8, 0xae, 0xde, 0xa7, 0xb6, 0x3a, 0x82, 0x3c,
4495  0x7c, 0x24, 0x59, 0x0a, 0xf8, 0x96, 0x43, 0x8b,
4496  0xa3, 0x29, 0x36, 0x3f, 0x91, 0x7f, 0x5d, 0xc7,
4497  0x23, 0x94, 0x29, 0x7f, 0x0a, 0xce, 0x0a, 0xbd,
4498  0x8d, 0x9b, 0x2f, 0x19, 0x17, 0xaa, 0xd5, 0x8e,
4499  0xec, 0x66, 0xa2, 0x37, 0xeb, 0x3f, 0x57, 0x53,
4500  0x3c, 0xf2, 0xaa, 0xbb, 0x79, 0x19, 0x4b, 0x90,
4501  0x7e, 0xa7, 0xa3, 0x99, 0xfe, 0x84, 0x4c, 0x89,
4502  0xf0, 0x3d, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3,
4503  0x6e, 0x30, 0x6c, 0x30, 0x1b, 0x06, 0x03, 0x55,
4504  0x1d, 0x11, 0x04, 0x14, 0x30, 0x12, 0x81, 0x10,
4505  0x77, 0x77, 0x77, 0x40, 0x73, 0x6e, 0x61, 0x6b,
4506  0x65, 0x6f, 0x69, 0x6c, 0x2e, 0x64, 0x6f, 0x6d,
4507  0x30, 0x3a, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
4508  0x86, 0xf8, 0x42, 0x01, 0x0d, 0x04, 0x2d, 0x16,
4509  0x2b, 0x6d, 0x6f, 0x64, 0x5f, 0x73, 0x73, 0x6c,
4510  0x20, 0x67, 0x65, 0x6e, 0x65, 0x72, 0x61, 0x74,
4511  0x65, 0x64, 0x20, 0x63, 0x75, 0x73, 0x74, 0x6f,
4512  0x6d, 0x20, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72,
4513  0x20, 0x63, 0x65, 0x72, 0x74, 0x69, 0x66, 0x69,
4514  0x63, 0x61, 0x74, 0x65, 0x30, 0x11, 0x06, 0x09,
4515  0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x42, 0x01,
4516  0x01, 0x04, 0x04, 0x03, 0x02, 0x06, 0x40, 0x30,
4517  0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7,
4518  0x0d, 0x01, 0x01, 0x04, 0x05, 0x00, 0x03, 0x81,
4519  0x81, 0x00, 0xae, 0x79, 0x79, 0x22, 0x90, 0x75,
4520  0xfd, 0xa6, 0xd5, 0xc4, 0xb8, 0xc4, 0x99, 0x4e,
4521  0x1c, 0x05, 0x7c, 0x91, 0x59, 0xbe, 0x89, 0x0d,
4522  0x3d, 0xc6, 0x8c, 0xa3, 0xcf, 0xf6, 0xba, 0x23,
4523  0xdf, 0xb8, 0xae, 0x44, 0x68, 0x8a, 0x8f, 0xb9,
4524  0x8b, 0xcb, 0x12, 0xda, 0xe6, 0xa2, 0xca, 0xa5,
4525  0xa6, 0x55, 0xd9, 0xd2, 0xa1, 0xad, 0xba, 0x9b,
4526  0x2c, 0x44, 0x95, 0x1d, 0x4a, 0x90, 0x59, 0x7f,
4527  0x83, 0xae, 0x81, 0x5e, 0x3f, 0x92, 0xe0, 0x14,
4528  0x41, 0x82, 0x4e, 0x7f, 0x53, 0xfd, 0x10, 0x23,
4529  0xeb, 0x8a, 0xeb, 0xe9, 0x92, 0xea, 0x61, 0xf2,
4530  0x8e, 0x19, 0xa1, 0xd3, 0x49, 0xc0, 0x84, 0x34,
4531  0x1e, 0x2e, 0x6e, 0xf6, 0x98, 0xe2, 0x87, 0x53,
4532  0xd6, 0x55, 0xd9, 0x1a, 0x8a, 0x92, 0x5c, 0xad,
4533  0xdc, 0x1e, 0x1c, 0x30, 0xa7, 0x65, 0x9d, 0xc2,
4534  0x4f, 0x60, 0xd2, 0x6f, 0xdb, 0xe0, 0x9f, 0x9e,
4535  0xbc, 0x41, 0x16, 0x03, 0x00, 0x00, 0x04, 0x0e,
4536  0x00, 0x00, 0x00
4537  };
4538  uint32_t shello_buf_len = sizeof(shello_buf);
4539 
4540  uint8_t client_change_cipher_spec_buf[] = {
4541  0x16, 0x03, 0x00, 0x00, 0x84, 0x10, 0x00, 0x00,
4542  0x80, 0x65, 0x51, 0x2d, 0xa6, 0xd4, 0xa7, 0x38,
4543  0xdf, 0xac, 0x79, 0x1f, 0x0b, 0xd9, 0xb2, 0x61,
4544  0x7d, 0x73, 0x88, 0x32, 0xd9, 0xf2, 0x62, 0x3a,
4545  0x8b, 0x11, 0x04, 0x75, 0xca, 0x42, 0xff, 0x4e,
4546  0xd9, 0xcc, 0xb9, 0xfa, 0x86, 0xf3, 0x16, 0x2f,
4547  0x09, 0x73, 0x51, 0x66, 0xaa, 0x29, 0xcd, 0x80,
4548  0x61, 0x0f, 0xe8, 0x13, 0xce, 0x5b, 0x8e, 0x0a,
4549  0x23, 0xf8, 0x91, 0x5e, 0x5f, 0x54, 0x70, 0x80,
4550  0x8e, 0x7b, 0x28, 0xef, 0xb6, 0x69, 0xb2, 0x59,
4551  0x85, 0x74, 0x98, 0xe2, 0x7e, 0xd8, 0xcc, 0x76,
4552  0x80, 0xe1, 0xb6, 0x45, 0x4d, 0xc7, 0xcd, 0x84,
4553  0xce, 0xb4, 0x52, 0x79, 0x74, 0xcd, 0xe6, 0xd7,
4554  0xd1, 0x9c, 0xad, 0xef, 0x63, 0x6c, 0x0f, 0xf7,
4555  0x05, 0xe4, 0x4d, 0x1a, 0xd3, 0xcb, 0x9c, 0xd2,
4556  0x51, 0xb5, 0x61, 0xcb, 0xff, 0x7c, 0xee, 0xc7,
4557  0xbc, 0x5e, 0x15, 0xa3, 0xf2, 0x52, 0x0f, 0xbb,
4558  0x32, 0x14, 0x03, 0x00, 0x00, 0x01, 0x01, 0x16,
4559  0x03, 0x00, 0x00, 0x40, 0xa9, 0xd8, 0xd7, 0x35,
4560  0xbc, 0x39, 0x56, 0x98, 0xad, 0x87, 0x61, 0x2a,
4561  0xc4, 0x8f, 0xcc, 0x03, 0xcb, 0x93, 0x80, 0x81,
4562  0xb0, 0x4a, 0xc4, 0xd2, 0x09, 0x71, 0x3e, 0x90,
4563  0x3c, 0x8d, 0xe0, 0x95, 0x44, 0xfe, 0x56, 0xd1,
4564  0x7e, 0x88, 0xe2, 0x48, 0xfd, 0x76, 0x70, 0x76,
4565  0xe2, 0xcd, 0x06, 0xd0, 0xf3, 0x9d, 0x13, 0x79,
4566  0x67, 0x1e, 0x37, 0xf6, 0x98, 0xbe, 0x59, 0x18,
4567  0x4c, 0xfc, 0x75, 0x56
4568  };
4569  uint32_t client_change_cipher_spec_buf_len =
4570  sizeof(client_change_cipher_spec_buf);
4571 
4572  uint8_t server_change_cipher_spec_buf[] = {
4573  0x14, 0x03, 0x00, 0x00, 0x01, 0x01, 0x16, 0x03,
4574  0x00, 0x00, 0x40, 0xce, 0x7c, 0x92, 0x43, 0x59,
4575  0xcc, 0x3d, 0x90, 0x91, 0x9c, 0x58, 0xf0, 0x7a,
4576  0xce, 0xae, 0x0d, 0x08, 0xe0, 0x76, 0xb4, 0x86,
4577  0xb1, 0x15, 0x5b, 0x32, 0xb8, 0x77, 0x53, 0xe7,
4578  0xa6, 0xf9, 0xd0, 0x95, 0x5f, 0xaa, 0x07, 0xc3,
4579  0x96, 0x7c, 0xc9, 0x88, 0xc2, 0x7a, 0x20, 0x89,
4580  0x4f, 0xeb, 0xeb, 0xb6, 0x19, 0xef, 0xaa, 0x27,
4581  0x73, 0x9d, 0xa6, 0xb4, 0x9f, 0xeb, 0x34, 0xe2,
4582  0x4d, 0x9f, 0x6b
4583  };
4584  uint32_t server_change_cipher_spec_buf_len =
4585  sizeof(server_change_cipher_spec_buf);
4586 
4587  uint8_t toserver_app_data_buf[] = {
4588  0x17, 0x03, 0x00, 0x01, 0xb0, 0x4a, 0xc3, 0x3e,
4589  0x9d, 0x77, 0x78, 0x01, 0x2c, 0xb4, 0xbc, 0x4c,
4590  0x9a, 0x84, 0xd7, 0xb9, 0x90, 0x0c, 0x21, 0x10,
4591  0xf0, 0xfa, 0x00, 0x7c, 0x16, 0xbb, 0x77, 0xfb,
4592  0x72, 0x42, 0x4f, 0xad, 0x50, 0x4a, 0xd0, 0xaa,
4593  0x6f, 0xaa, 0x44, 0x6c, 0x62, 0x94, 0x1b, 0xc5,
4594  0xfe, 0xe9, 0x1c, 0x5e, 0xde, 0x85, 0x0b, 0x0e,
4595  0x05, 0xe4, 0x18, 0x6e, 0xd2, 0xd3, 0xb5, 0x20,
4596  0xab, 0x81, 0xfd, 0x18, 0x9a, 0x73, 0xb8, 0xd7,
4597  0xef, 0xc3, 0xdd, 0x74, 0xd7, 0x9c, 0x1e, 0x6f,
4598  0x21, 0x6d, 0xf8, 0x24, 0xca, 0x3c, 0x70, 0x78,
4599  0x36, 0x12, 0x7a, 0x8a, 0x9c, 0xac, 0x4e, 0x1c,
4600  0xa8, 0xfb, 0x27, 0x30, 0xba, 0x9a, 0xf4, 0x2f,
4601  0x0a, 0xab, 0x80, 0x6a, 0xa1, 0x60, 0x74, 0xf0,
4602  0xe3, 0x91, 0x84, 0xe7, 0x90, 0x88, 0xcc, 0xf0,
4603  0x95, 0x7b, 0x0a, 0x22, 0xf2, 0xf9, 0x27, 0xe0,
4604  0xdd, 0x38, 0x0c, 0xfd, 0xe9, 0x03, 0x71, 0xdc,
4605  0x70, 0xa4, 0x6e, 0xdf, 0xe3, 0x72, 0x9e, 0xa1,
4606  0xf0, 0xc9, 0x00, 0xd6, 0x03, 0x55, 0x6a, 0x67,
4607  0x5d, 0x9c, 0xb8, 0x75, 0x01, 0xb0, 0x01, 0x9f,
4608  0xe6, 0xd2, 0x44, 0x18, 0xbc, 0xca, 0x7a, 0x10,
4609  0x39, 0xa6, 0xcf, 0x15, 0xc7, 0xf5, 0x35, 0xd4,
4610  0xb3, 0x6d, 0x91, 0x23, 0x84, 0x99, 0xba, 0xb0,
4611  0x7e, 0xd0, 0xc9, 0x4c, 0xbf, 0x3f, 0x33, 0x68,
4612  0x37, 0xb7, 0x7d, 0x44, 0xb0, 0x0b, 0x2c, 0x0f,
4613  0xd0, 0x75, 0xa2, 0x6b, 0x5b, 0xe1, 0x9f, 0xd4,
4614  0x69, 0x9a, 0x14, 0xc8, 0x29, 0xb7, 0xd9, 0x10,
4615  0xbb, 0x99, 0x30, 0x9a, 0xfb, 0xcc, 0x13, 0x1f,
4616  0x76, 0x4e, 0xe6, 0xdf, 0x14, 0xaa, 0xd5, 0x60,
4617  0xbf, 0x91, 0x49, 0x0d, 0x64, 0x42, 0x29, 0xa8,
4618  0x64, 0x27, 0xd4, 0x5e, 0x1b, 0x18, 0x03, 0xa8,
4619  0x73, 0xd6, 0x05, 0x6e, 0xf7, 0x50, 0xb0, 0x09,
4620  0x6b, 0x69, 0x7a, 0x12, 0x28, 0x58, 0xef, 0x5a,
4621  0x86, 0x11, 0xde, 0x71, 0x71, 0x9f, 0xca, 0xbd,
4622  0x79, 0x2a, 0xc2, 0xe5, 0x9b, 0x5e, 0x32, 0xe7,
4623  0xcb, 0x97, 0x6e, 0xa0, 0xea, 0xa4, 0xa4, 0x6a,
4624  0x32, 0xf9, 0x37, 0x39, 0xd8, 0x37, 0x6d, 0x63,
4625  0xf3, 0x08, 0x1c, 0xdd, 0x06, 0xdd, 0x2c, 0x2b,
4626  0x9f, 0x04, 0x88, 0x5f, 0x36, 0x42, 0xc1, 0xb1,
4627  0xc7, 0xe8, 0x2d, 0x5d, 0xa4, 0x6c, 0xe5, 0x60,
4628  0x94, 0xae, 0xd0, 0x90, 0x1e, 0x88, 0xa0, 0x87,
4629  0x52, 0xfb, 0xed, 0x97, 0xa5, 0x25, 0x5a, 0xb7,
4630  0x55, 0xc5, 0x13, 0x07, 0x85, 0x27, 0x40, 0xed,
4631  0xb8, 0xa0, 0x26, 0x13, 0x44, 0x0c, 0xfc, 0xcc,
4632  0x5a, 0x09, 0xe5, 0x44, 0xb5, 0x63, 0xa1, 0x43,
4633  0x51, 0x23, 0x4f, 0x17, 0x21, 0x89, 0x2e, 0x58,
4634  0xfd, 0xf9, 0x63, 0x74, 0x04, 0x70, 0x1e, 0x7d,
4635  0xd0, 0x66, 0xba, 0x40, 0x5e, 0x45, 0xdc, 0x39,
4636  0x7c, 0x53, 0x0f, 0xa8, 0x38, 0xb2, 0x13, 0x99,
4637  0x27, 0xd9, 0x4a, 0x51, 0xe9, 0x9f, 0x2a, 0x92,
4638  0xbb, 0x9c, 0x90, 0xab, 0xfd, 0xf1, 0xb7, 0x40,
4639  0x05, 0xa9, 0x7a, 0x20, 0x63, 0x36, 0xc1, 0xef,
4640  0xb9, 0xad, 0xa2, 0xe0, 0x1d, 0x20, 0x4f, 0xb2,
4641  0x34, 0xbd, 0xea, 0x07, 0xac, 0x21, 0xce, 0xf6,
4642  0x8a, 0xa2, 0x9e, 0xcd, 0xfa
4643  };
4644  uint32_t toserver_app_data_buf_len = sizeof(toserver_app_data_buf);
4645 
4646  TcpSession ssn;
4648 
4649  //AppLayerDetectProtoThreadInit();
4650 
4651  memset(&f, 0, sizeof(f));
4652  memset(&ssn, 0, sizeof(ssn));
4653 
4654  FLOW_INITIALIZE(&f);
4655  f.protoctx = (void *)&ssn;
4656  f.proto = IPPROTO_TCP;
4657  f.alproto = ALPROTO_TLS;
4658 
4660 
4661  FLOWLOCK_WRLOCK(&f);
4662  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4663  STREAM_TOSERVER | STREAM_START, chello_buf,
4664  chello_buf_len);
4665  FLOWLOCK_UNLOCK(&f);
4666  FAIL_IF(r != 0);
4667 
4668  SSLState *app_state = f.alstate;
4669  FAIL_IF_NULL(app_state);
4670 
4672 
4673  FAIL_IF(app_state->client_connp.version != SSL_VERSION_2);
4674 
4675  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) == 0);
4676  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) == 0);
4677  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) == 0);
4678 
4679  FLOWLOCK_WRLOCK(&f);
4680  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT,
4681  shello_buf, shello_buf_len);
4682  FLOWLOCK_UNLOCK(&f);
4683  FAIL_IF(r != 0);
4684 
4686 
4687  FAIL_IF(app_state->server_connp.version != SSL_VERSION_3);
4688 
4689  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) == 0);
4690  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) == 0);
4691  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) == 0);
4692  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0);
4693 
4694  FLOWLOCK_WRLOCK(&f);
4695  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4696  client_change_cipher_spec_buf,
4697  client_change_cipher_spec_buf_len);
4698  FLOWLOCK_UNLOCK(&f);
4699  FAIL_IF(r != 0);
4700 
4701  /* with multiple records the client content type hold the type from the last
4702  * record */
4704 
4705  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) == 0);
4706  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) == 0);
4707  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) == 0);
4708  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0);
4709  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_KEYX) == 0);
4711  FAIL_IF((app_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) == 0);
4712 
4713  FLOWLOCK_WRLOCK(&f);
4714  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT,
4715  server_change_cipher_spec_buf,
4716  server_change_cipher_spec_buf_len);
4717  FLOWLOCK_UNLOCK(&f);
4718  FAIL_IF(r != 0);
4719 
4720  /* with multiple records the serve content type hold the type from the last
4721  * record */
4723 
4724  FAIL_IF(app_state->server_connp.version != SSL_VERSION_3);
4725 
4726  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) == 0);
4727  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) == 0);
4728  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) == 0);
4729  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0);
4730  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_KEYX) == 0);
4733  FAIL_IF((app_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) == 0);
4734 
4735  FLOWLOCK_WRLOCK(&f);
4736  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4737  toserver_app_data_buf, toserver_app_data_buf_len);
4738  FLOWLOCK_UNLOCK(&f);
4739  FAIL_IF(r != 0);
4740 
4742 
4743  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) == 0);
4744  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_CLIENT_HS) == 0);
4745  FAIL_IF((app_state->flags & SSL_AL_FLAG_SSL_NO_SESSION_ID) == 0);
4746  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_SERVER_HELLO) == 0);
4747  FAIL_IF((app_state->flags & SSL_AL_FLAG_STATE_CLIENT_KEYX) == 0);
4750  FAIL_IF((app_state->flags & SSL_AL_FLAG_CHANGE_CIPHER_SPEC) == 0);
4751 
4753 
4754  if (alp_tctx != NULL)
4755  AppLayerParserThreadCtxFree(alp_tctx);
4757  FLOW_DESTROY(&f);
4758 
4759  PASS;
4760 }
4761 
4762 /**
4763  * \test Tests the parser for handling fragmented records.
4764  */
4765 static int SSLParserTest24(void)
4766 {
4767  Flow f;
4768  uint8_t buf1[] = {
4769  0x16, 0x03, 0x00, 0x00, 0x6f, 0x01, 0x00, 0x00,
4770  0x6b, 0x03,
4771  };
4772  uint32_t buf1_len = sizeof(buf1);
4773 
4774  uint8_t buf2[] = {
4775  0x00, 0x4b, 0x2f, 0xdc,
4776  0x4e, 0xe6, 0x95, 0xf1, 0xa0, 0xc7, 0xcf, 0x8e,
4777  0xf6, 0xeb, 0x22, 0x6d, 0xce, 0x9c, 0x44, 0xfb,
4778  0xc8, 0xa0, 0x44, 0x31, 0x15, 0x4c, 0xe9, 0x97,
4779  0xa7, 0xa1, 0xfe, 0xea, 0xcc, 0x20, 0x4b, 0x5d,
4780  0xfb, 0xa5, 0x63, 0x7a, 0x73, 0x95, 0xf7, 0xff,
4781  0x42, 0xac, 0x8f, 0x46, 0xed, 0xe4, 0xb1, 0x35,
4782  0x35, 0x78, 0x1a, 0x9d, 0xaf, 0x10, 0xc5, 0x52,
4783  0xf3, 0x7b, 0xfb, 0xb5, 0xe9, 0xa8, 0x00, 0x24,
4784  0x00, 0x88, 0x00, 0x87, 0x00, 0x39, 0x00, 0x38,
4785  0x00, 0x84, 0x00, 0x35, 0x00, 0x45, 0x00, 0x44,
4786  0x00, 0x33, 0x00, 0x32, 0x00, 0x96, 0x00, 0x41,
4787  0x00, 0x2f, 0x00, 0x16, 0x00, 0x13, 0xfe, 0xff,
4788  0x00, 0x0a, 0x00, 0x02, 0x01, 0x00
4789  };
4790  uint32_t buf2_len = sizeof(buf2);
4791  TcpSession ssn;
4793 
4794  memset(&f, 0, sizeof(f));
4795  memset(&ssn, 0, sizeof(ssn));
4796  FLOW_INITIALIZE(&f);
4797  f.protoctx = (void *)&ssn;
4798  f.proto = IPPROTO_TCP;
4799  f.alproto = ALPROTO_TLS;
4800 
4802 
4803  FLOWLOCK_WRLOCK(&f);
4804  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
4805  STREAM_TOSERVER, buf1, buf1_len);
4806  FLOWLOCK_UNLOCK(&f);
4807  FAIL_IF(r != 0);
4808 
4809  FLOWLOCK_WRLOCK(&f);
4810  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
4811  buf2, buf2_len);
4812  FLOWLOCK_UNLOCK(&f);
4813  FAIL_IF(r != 0);
4814 
4815  SSLState *ssl_state = f.alstate;
4816  FAIL_IF_NULL(ssl_state);
4817 
4818  FAIL_IF(ssl_state->client_connp.content_type != 0x16);
4819 
4820  FAIL_IF(ssl_state->client_connp.version != SSL_VERSION_3);
4821 
4822  AppLayerParserThreadCtxFree(alp_tctx);
4824  FLOW_DESTROY(&f);
4825 
4826  PASS;
4827 }
4828 
4829 /**
4830  * \test Test for bug #955 and CVE-2013-5919. The data is from the
4831  * pcap that was used to report this issue.
4832  */
4833 static int SSLParserTest25(void)
4834 {
4835  Flow f;
4836  uint8_t client_hello[] = {
4837  0x16, 0x03, 0x01, 0x00, 0xd3, 0x01, 0x00, 0x00,
4838  0xcf, 0x03, 0x01, 0x51, 0x60, 0xc2, 0x15, 0x36,
4839  0x73, 0xf5, 0xb8, 0x58, 0x55, 0x3b, 0x68, 0x12,
4840  0x7d, 0xe3, 0x28, 0xa3, 0xe1, 0x02, 0x79, 0x2d,
4841  0x12, 0xe1, 0xf4, 0x24, 0x12, 0xa2, 0x9e, 0xf1,
4842  0x08, 0x49, 0x68, 0x20, 0x0e, 0x96, 0x46, 0x3d,
4843  0x84, 0x5a, 0xc6, 0x55, 0xeb, 0x3b, 0x53, 0x77,
4844  0xf4, 0x8e, 0xf4, 0xd2, 0x8b, 0xec, 0xd6, 0x99,
4845  0x63, 0x64, 0x62, 0xf8, 0x3f, 0x3b, 0xd5, 0x35,
4846  0x45, 0x1b, 0x16, 0xac, 0x00, 0x46, 0x00, 0x04,
4847  0x00, 0x05, 0x00, 0x2f, 0x00, 0x35, 0xc0, 0x02,
4848  0xc0, 0x04, 0xc0, 0x05, 0xc0, 0x0c, 0xc0, 0x0e,
4849  0xc0, 0x0f, 0xc0, 0x07, 0xc0, 0x09, 0xc0, 0x0a,
4850  0xc0, 0x11, 0xc0, 0x13, 0xc0, 0x14, 0x00, 0x33,
4851  0x00, 0x39, 0x00, 0x32, 0x00, 0x38, 0x00, 0x0a,
4852  0xc0, 0x03, 0xc0, 0x0d, 0xc0, 0x08, 0xc0, 0x12,
4853  0x00, 0x16, 0x00, 0x13, 0x00, 0x09, 0x00, 0x15,
4854  0x00, 0x12, 0x00, 0x03, 0x00, 0x08, 0x00, 0x14,
4855  0x00, 0x11, 0x00, 0xff, 0x01, 0x00, 0x00, 0x40,
4856  0x00, 0x0b, 0x00, 0x04, 0x03, 0x00, 0x01, 0x02,
4857  0x00, 0x0a, 0x00, 0x34, 0x00, 0x32, 0x00, 0x0e,
4858  0x00, 0x0d, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x0c,
4859  0x00, 0x18, 0x00, 0x09, 0x00, 0x0a, 0x00, 0x16,
4860  0x00, 0x17, 0x00, 0x08, 0x00, 0x06, 0x00, 0x07,
4861  0x00, 0x14, 0x00, 0x15, 0x00, 0x04, 0x00, 0x05,
4862  0x00, 0x12, 0x00, 0x13, 0x00, 0x01, 0x00, 0x02,
4863  0x00, 0x03, 0x00, 0x0f, 0x00, 0x10, 0x00, 0x11
4864  };
4865  uint32_t client_hello_len = sizeof(client_hello);
4866 
4867  uint8_t server_hello_certificate_done[] = {
4868  0x16, 0x03, 0x01, 0x00, 0x51, 0x02, 0x00, 0x00,
4869  0x4d, 0x03, 0x01, 0x51, 0x60, 0xc2, 0x17, 0xb7,
4870  0x81, 0xaa, 0x27, 0xa1, 0xd5, 0xfa, 0x14, 0xc1,
4871  0xe0, 0x05, 0xab, 0x75, 0xf2, 0x51, 0xe7, 0x6e,
4872  0xe6, 0xf9, 0xc4, 0x8f, 0x16, 0x08, 0x26, 0x6c,
4873  0x1b, 0x86, 0x90, 0x20, 0x0a, 0x38, 0x90, 0x2d,
4874  0x17, 0x7d, 0xb7, 0x6b, 0x6b, 0xe5, 0xeb, 0x61,
4875  0x90, 0x35, 0xf8, 0xcd, 0xb1, 0x2a, 0x69, 0x6e,
4876  0x0e, 0x3e, 0x5f, 0x90, 0xdc, 0x2f, 0x51, 0x45,
4877  0x68, 0x63, 0xe3, 0xb3, 0x00, 0x05, 0x00, 0x00,
4878  0x05, 0xff, 0x01, 0x00, 0x01, 0x00, 0x16, 0x03,
4879  0x01, 0x07, 0x60, 0x0b, 0x00, 0x07, 0x5c, 0x00,
4880  0x07, 0x59, 0x00, 0x03, 0xcc, 0x30, 0x82, 0x03,
4881  0xc8, 0x30, 0x82, 0x03, 0x31, 0xa0, 0x03, 0x02,
4882  0x01, 0x02, 0x02, 0x10, 0x01, 0x7f, 0x77, 0xde,
4883  0xb3, 0xbc, 0xbb, 0x23, 0x5d, 0x44, 0xcc, 0xc7,
4884  0xdb, 0xa6, 0x2e, 0x72, 0x30, 0x0d, 0x06, 0x09,
4885  0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
4886  0x05, 0x05, 0x00, 0x30, 0x81, 0xba, 0x31, 0x1f,
4887  0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13,
4888  0x16, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67,
4889  0x6e, 0x20, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20,
4890  0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x31,
4891  0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x0b,
4892  0x13, 0x0e, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69,
4893  0x67, 0x6e, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e,
4894  0x31, 0x33, 0x30, 0x31, 0x06, 0x03, 0x55, 0x04,
4895  0x0b, 0x13, 0x2a, 0x56, 0x65, 0x72, 0x69, 0x53,
4896  0x69, 0x67, 0x6e, 0x20, 0x49, 0x6e, 0x74, 0x65,
4897  0x72, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x61,
4898  0x6c, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
4899  0x20, 0x43, 0x41, 0x20, 0x2d, 0x20, 0x43, 0x6c,
4900  0x61, 0x73, 0x73, 0x20, 0x33, 0x31, 0x49, 0x30,
4901  0x47, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x40,
4902  0x77, 0x77, 0x77, 0x2e, 0x76, 0x65, 0x72, 0x69,
4903  0x73, 0x69, 0x67, 0x6e, 0x2e, 0x63, 0x6f, 0x6d,
4904  0x2f, 0x43, 0x50, 0x53, 0x20, 0x49, 0x6e, 0x63,
4905  0x6f, 0x72, 0x70, 0x2e, 0x62, 0x79, 0x20, 0x52,
4906  0x65, 0x66, 0x2e, 0x20, 0x4c, 0x49, 0x41, 0x42,
4907  0x49, 0x4c, 0x49, 0x54, 0x59, 0x20, 0x4c, 0x54,
4908  0x44, 0x2e, 0x28, 0x63, 0x29, 0x39, 0x37, 0x20,
4909  0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67, 0x6e,
4910  0x30, 0x1e, 0x17, 0x0d, 0x31, 0x32, 0x30, 0x36,
4911  0x32, 0x31, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30,
4912  0x5a, 0x17, 0x0d, 0x31, 0x33, 0x31, 0x32, 0x33,
4913  0x31, 0x32, 0x33, 0x35, 0x39, 0x35, 0x39, 0x5a,
4914  0x30, 0x68, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03,
4915  0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
4916  0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08,
4917  0x13, 0x0a, 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f,
4918  0x72, 0x6e, 0x69, 0x61, 0x31, 0x12, 0x30, 0x10,
4919  0x06, 0x03, 0x55, 0x04, 0x07, 0x13, 0x09, 0x50,
4920  0x61, 0x6c, 0x6f, 0x20, 0x41, 0x6c, 0x74, 0x6f,
4921  0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04,
4922  0x0a, 0x13, 0x0e, 0x46, 0x61, 0x63, 0x65, 0x62,
4923  0x6f, 0x6f, 0x6b, 0x2c, 0x20, 0x49, 0x6e, 0x63,
4924  0x2e, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55,
4925  0x04, 0x02, 0x14, 0x0e, 0x2a, 0x2e, 0x66, 0x61,
4926  0x63, 0x65, 0x62, 0x6f, 0x6f, 0x6b, 0x2e, 0x63,
4927  0x6f, 0x6d, 0x30, 0x81, 0x9f, 0x30, 0x0d, 0x06,
4928  0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01,
4929  0x01, 0x01, 0x05, 0x00, 0x03, 0x81, 0x8d, 0x00,
4930  0x30, 0x81, 0x89, 0x02, 0x81, 0x81, 0x00, 0xae,
4931  0x94, 0xb1, 0x71, 0xe2, 0xde, 0xcc, 0xc1, 0x69,
4932  0x3e, 0x05, 0x10, 0x63, 0x24, 0x01, 0x02, 0xe0,
4933  0x68, 0x9a, 0xe8, 0x3c, 0x39, 0xb6, 0xb3, 0xe7,
4934  0x4b, 0x97, 0xd4, 0x8d, 0x7b, 0x23, 0x68, 0x91,
4935  0x00, 0xb0, 0xb4, 0x96, 0xee, 0x62, 0xf0, 0xe6,
4936  0xd3, 0x56, 0xbc, 0xf4, 0xaa, 0x0f, 0x50, 0x64,
4937  0x34, 0x02, 0xf5, 0xd1, 0x76, 0x6a, 0xa9, 0x72,
4938  0x83, 0x5a, 0x75, 0x64, 0x72, 0x3f, 0x39, 0xbb,
4939  0xef, 0x52, 0x90, 0xde, 0xd9, 0xbc, 0xdb, 0xf9,
4940  0xd3, 0xd5, 0x5d, 0xfa, 0xd2, 0x3a, 0xa0, 0x3d,
4941  0xc6, 0x04, 0xc5, 0x4d, 0x29, 0xcf, 0x1d, 0x4b,
4942  0x3b, 0xdb, 0xd1, 0xa8, 0x09, 0xcf, 0xae, 0x47,
4943  0xb4, 0x4c, 0x7e, 0xae, 0x17, 0xc5, 0x10, 0x9b,
4944  0xee, 0x24, 0xa9, 0xcf, 0x4a, 0x8d, 0x91, 0x1b,
4945  0xb0, 0xfd, 0x04, 0x15, 0xae, 0x4c, 0x3f, 0x43,
4946  0x0a, 0xa1, 0x2a, 0x55, 0x7e, 0x2a, 0xe1, 0x02,
4947  0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, 0x01, 0x1e,
4948  0x30, 0x82, 0x01, 0x1a, 0x30, 0x09, 0x06, 0x03,
4949  0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, 0x00, 0x30,
4950  0x44, 0x06, 0x03, 0x55, 0x1d, 0x20, 0x04, 0x3d,
4951  0x30, 0x3b, 0x30, 0x39, 0x06, 0x0b, 0x60, 0x86,
4952  0x48, 0x01, 0x86, 0xf8, 0x45, 0x01, 0x07, 0x17,
4953  0x03, 0x30, 0x2a, 0x30, 0x28, 0x06, 0x08, 0x2b,
4954  0x06, 0x01, 0x05, 0x05, 0x07, 0x00, 0x01, 0x16,
4955  0x1c, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
4956  0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x65, 0x72,
4957  0x69, 0x73, 0x69, 0x67, 0x6e, 0x2e, 0x63, 0x6f,
4958  0x6d, 0x2f, 0x72, 0x70, 0x61, 0x30, 0x3c, 0x06,
4959  0x03, 0x55, 0x1d, 0x1f, 0x04, 0x35, 0x30, 0x33,
4960  0x30, 0x31, 0xa0, 0x2f, 0xa0, 0x2d, 0x86, 0x2b,
4961  0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x53,
4962  0x56, 0x52, 0x49, 0x6e, 0x74, 0x6c, 0x2d, 0x63,
4963  0x72, 0x6c, 0x2e, 0x76, 0x65, 0x72, 0x69, 0x73,
4964  0x69, 0x67, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f,
4965  0x53, 0x56, 0x52, 0x49, 0x6e, 0x74, 0x6c, 0x2e,
4966  0x63, 0x72, 0x6c, 0x30, 0x1d, 0x06, 0x03, 0x55,
4967  0x1d, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06, 0x08,
4968  0x2b, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01,
4969  0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07,
4970  0x03, 0x02, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x1d,
4971  0x0f, 0x04, 0x04, 0x03, 0x02, 0x05, 0xa0, 0x30,
4972  0x34, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05,
4973  0x07, 0x01, 0x01, 0x04, 0x28, 0x30, 0x26, 0x30,
4974  0x24, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05,
4975  0x07, 0x30, 0x01, 0x86, 0x18, 0x68, 0x74, 0x74,
4976  0x70, 0x3a, 0x2f, 0x2f, 0x6f, 0x63, 0x73, 0x70,
4977  0x2e, 0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67,
4978  0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x27, 0x06,
4979  0x03, 0x55, 0x1d, 0x11, 0x04, 0x20, 0x30, 0x1e,
4980  0x82, 0x0e, 0x2a, 0x2e, 0x66, 0x61, 0x63, 0x65,
4981  0x62, 0x6f, 0x6f, 0x6b, 0x2e, 0x63, 0x6f, 0x6d,
4982  0x82, 0x0c, 0x66, 0x61, 0x63, 0x65, 0x62, 0x6f,
4983  0x6f, 0x6b, 0x2e, 0x63, 0x6f, 0x6d, 0x30, 0x0d,
4984  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
4985  0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x81, 0x81,
4986  0x00, 0x5b, 0x6c, 0x2b, 0x75, 0xf8, 0xed, 0x30,
4987  0xaa, 0x51, 0xaa, 0xd3, 0x6a, 0xba, 0x59, 0x5e,
4988  0x55, 0x51, 0x41, 0x95, 0x1f, 0x81, 0xa5, 0x3b,
4989  0x44, 0x79, 0x10, 0xac, 0x1f, 0x76, 0xff, 0x78,
4990  0xfc, 0x27, 0x81, 0x61, 0x6b, 0x58, 0xf3, 0x12,
4991  0x2a, 0xfc, 0x1c, 0x87, 0x01, 0x04, 0x25, 0xe9,
4992  0xed, 0x43, 0xdf, 0x1a, 0x7b, 0xa6, 0x49, 0x80,
4993  0x60, 0x67, 0xe2, 0x68, 0x8a, 0xf0, 0x3d, 0xb5,
4994  0x8c, 0x7d, 0xf4, 0xee, 0x03, 0x30, 0x9a, 0x6a,
4995  0xfc, 0x24, 0x7c, 0xcb, 0x13, 0x4d, 0xc3, 0x3e,
4996  0x54, 0xc6, 0xbc, 0x1d, 0x51, 0x33, 0xa5, 0x32,
4997  0xa7, 0x32, 0x73, 0xb1, 0xd7, 0x9c, 0xad, 0xc0,
4998  0x8e, 0x7e, 0x1a, 0x83, 0x11, 0x6d, 0x34, 0x52,
4999  0x33, 0x40, 0xb0, 0x30, 0x54, 0x27, 0xa2, 0x17,
5000  0x42, 0x82, 0x7c, 0x98, 0x91, 0x66, 0x98, 0xee,
5001  0x7e, 0xaf, 0x8c, 0x3b, 0xdd, 0x71, 0x70, 0x08,
5002  0x17, 0x00, 0x03, 0x87, 0x30, 0x82, 0x03, 0x83,
5003  0x30, 0x82, 0x02, 0xec, 0xa0, 0x03, 0x02, 0x01,
5004  0x02, 0x02, 0x10, 0x46, 0xfc, 0xeb, 0xba, 0xb4,
5005  0xd0, 0x2f, 0x0f, 0x92, 0x60, 0x98, 0x23, 0x3f,
5006  0x93, 0x07, 0x8f, 0x30, 0x0d, 0x06, 0x09, 0x2a,
5007  0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x05,
5008  0x05, 0x00, 0x30, 0x5f, 0x31, 0x0b, 0x30, 0x09,
5009  0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
5010  0x53, 0x31, 0x17, 0x30, 0x15, 0x06, 0x03, 0x55,
5011  0x04, 0x0a, 0x13, 0x0e, 0x56, 0x65, 0x72, 0x69,
5012  0x53, 0x69, 0x67, 0x6e, 0x2c, 0x20, 0x49, 0x6e,
5013  0x63, 0x2e, 0x31, 0x37, 0x30, 0x35, 0x06, 0x03,
5014  0x55, 0x04, 0x0b, 0x13, 0x2e, 0x43, 0x6c, 0x61,
5015  0x73, 0x73, 0x20, 0x33, 0x20, 0x50, 0x75, 0x62,
5016  0x6c, 0x69, 0x63, 0x20, 0x50, 0x72, 0x69, 0x6d,
5017  0x61, 0x72, 0x79, 0x20, 0x43, 0x65, 0x72, 0x74,
5018  0x69, 0x66, 0x69, 0x63, 0x61, 0x74, 0x69, 0x6f,
5019  0x6e, 0x20, 0x41, 0x75, 0x64, 0x68, 0x6f, 0x72,
5020  0x69, 0x74, 0x79, 0x30, 0x1e, 0x17, 0x0d, 0x39,
5021  0x37, 0x30, 0x34, 0x31, 0x37, 0x30, 0x30, 0x30,
5022  0x30, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x31, 0x36,
5023  0x31, 0x30, 0x32, 0x34, 0x32, 0x33, 0x35, 0x39,
5024  0x35, 0x39, 0x5a, 0x30, 0x81, 0xba, 0x31, 0x1f,
5025  0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x13,
5026  0x16, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69, 0x67,
5027  0x6e, 0x20, 0x54, 0x72, 0x75, 0x73, 0x74, 0x20,
5028  0x4e, 0x65, 0x74, 0x77, 0x6f, 0x72, 0x6b, 0x31,
5029  0x17, 0x30, 0x15, 0x06, 0x03, 0x55, 0x04, 0x0b,
5030  0x13, 0x0e, 0x56, 0x65, 0x72, 0x69, 0x53, 0x69,
5031  0x67, 0x6e, 0x2c, 0x20, 0x49, 0x6e, 0x63, 0x2e,
5032  0x31, 0x33, 0x30, 0x31, 0x06, 0x03, 0x55, 0x04,
5033  0x0b, 0x13, 0x2a, 0x56, 0x65, 0x72, 0x69, 0x53,
5034  0x69, 0x67, 0x6e, 0x20, 0x49, 0x6e, 0x74, 0x65,
5035  0x72, 0x6e, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x61,
5036  0x6c, 0x20, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72,
5037  0x20, 0x43, 0x41, 0x20, 0x2d, 0x20, 0x43, 0x6c,
5038  0x61, 0x73, 0x73, 0x20, 0x33, 0x31, 0x49, 0x30,
5039  0x47, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x13, 0x40,
5040  0x77, 0x77, 0x77, 0x2e, 0x76, 0x65, 0x72, 0x69,
5041  0x73, 0x69,
5042  0x67, 0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x43,
5043  0x50, 0x53, 0x20, 0x49, 0x6e, 0x63, 0x6f, 0x72,
5044  0x70, 0x2e, 0x62, 0x79, 0x20, 0x52, 0x65, 0x66,
5045  0x2e, 0x20, 0x4c, 0x49, 0x41, 0x42, 0x49, 0x4c,
5046  0x49, 0x54, 0x59, 0x20, 0x4c, 0x54, 0x44, 0x2e,
5047  0x28, 0x63, 0x29, 0x39, 0x37, 0x20, 0x56, 0x65,
5048  0x72, 0x69, 0x53, 0x69, 0x67, 0x6e, 0x30, 0x81,
5049  0x9f, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
5050  0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00,
5051  0x03, 0x81, 0x8d, 0x00, 0x30, 0x81, 0x89, 0x02,
5052  0x81, 0x81, 0x00, 0xd8, 0x82, 0x80, 0xe8, 0xd6,
5053  0x19, 0x02, 0x7d, 0x1f, 0x85, 0x18, 0x39, 0x25,
5054  0xa2, 0x65, 0x2b, 0xe1, 0xbf, 0xd4, 0x05, 0xd3,
5055  0xbc, 0xe6, 0x36, 0x3b, 0xaa, 0xf0, 0x4c, 0x6c,
5056  0x5b, 0xb6, 0xe7, 0xaa, 0x3c, 0x73, 0x45, 0x55,
5057  0xb2, 0xf1, 0xbd, 0xea, 0x97, 0x42, 0xed, 0x9a,
5058  0x34, 0x0a, 0x15, 0xd4, 0xa9, 0x5c, 0xf5, 0x40,
5059  0x25, 0xdd, 0xd9, 0x07, 0xc1, 0x32, 0xb2, 0x75,
5060  0x6c, 0xc4, 0xca, 0xbb, 0xa3, 0xfe, 0x56, 0x27,
5061  0x71, 0x43, 0xaa, 0x63, 0xf5, 0x30, 0x3e, 0x93,
5062  0x28, 0xe5, 0xfa, 0xf1, 0x09, 0x3b, 0xf3, 0xb7,
5063  0x4d, 0x4e, 0x39, 0xf7, 0x5c, 0x49, 0x5a, 0xb8,
5064  0xc1, 0x1d, 0xd3, 0xb2, 0x8a, 0xfe, 0x70, 0x30,
5065  0x95, 0x42, 0xcb, 0xfe, 0x2b, 0x51, 0x8b, 0x5a,
5066  0x3c, 0x3a, 0xf9, 0x22, 0x4f, 0x90, 0xb2, 0x02,
5067  0xa7, 0x53, 0x9c, 0x4f, 0x34, 0xe7, 0xab, 0x04,
5068  0xb2, 0x7b, 0x6f, 0x02, 0x03, 0x01, 0x00, 0x01,
5069  0xa3, 0x81, 0xe3, 0x30, 0x81, 0xe0, 0x30, 0x0f,
5070  0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x08, 0x30,
5071  0x06, 0x01, 0x01, 0xff, 0x02, 0x01, 0x00, 0x30,
5072  0x44, 0x06, 0x03, 0x55, 0x1d, 0x20, 0x04, 0x3d,
5073  0x30, 0x3b, 0x30, 0x39, 0x06, 0x0b, 0x60, 0x86,
5074  0x48, 0x01, 0x86, 0xf8, 0x45, 0x01, 0x07, 0x01,
5075  0x01, 0x30, 0x2a, 0x30, 0x28, 0x06, 0x08, 0x2b,
5076  0x06, 0x01, 0x05, 0x05, 0x07, 0x02, 0x01, 0x16,
5077  0x1c, 0x68, 0x74, 0x74, 0x70, 0x73, 0x3a, 0x2f,
5078  0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x65, 0x72,
5079  0x69, 0x73, 0x69, 0x67, 0x6e, 0x2e, 0x63, 0x6f,
5080  0x6d, 0x2f, 0x43, 0x50, 0x53, 0x30, 0x34, 0x06,
5081  0x03, 0x55, 0x1d, 0x25, 0x04, 0x2d, 0x30, 0x2b,
5082  0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, 0x05, 0x07,
5083  0x03, 0x01, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05,
5084  0x05, 0x07, 0x03, 0x02, 0x06, 0x09, 0x60, 0x86,
5085  0x48, 0x01, 0x86, 0xf8, 0x42, 0x04, 0x01, 0x06,
5086  0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xf8, 0x45,
5087  0x01, 0x08, 0x01, 0x30, 0x0b, 0x06, 0x03, 0x55,
5088  0x1d, 0x0f, 0x04, 0x04, 0x03, 0x02, 0x01, 0x06,
5089  0x30, 0x11, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01,
5090  0x86, 0xf8, 0x42, 0x01, 0x01, 0x04, 0x04, 0x03,
5091  0x02, 0x01, 0x06, 0x30, 0x31, 0x06, 0x03, 0x55,
5092  0x1d, 0x1f, 0x04, 0x2a, 0x30, 0x28, 0x30, 0x26,
5093  0xa0, 0x24, 0xa0, 0x22, 0x86, 0x20, 0x68, 0x74,
5094  0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x63, 0x72, 0x6c,
5095  0x2e, 0x76, 0x65, 0x72, 0x69, 0x73, 0x69, 0x67,
5096  0x6e, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x70, 0x63,
5097  0x61, 0x33, 0x2e, 0x63, 0x72, 0x6c, 0x30, 0x0d,
5098  0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
5099  0x01, 0x01, 0x05, 0x05, 0x00, 0x03, 0x81, 0x81,
5100  0x00, 0x40, 0x8e, 0x49, 0x97, 0x96, 0x8a, 0x73,
5101  0xdd, 0x8e, 0x4d, 0xef, 0x3e, 0x61, 0xb7, 0xca,
5102  0xa0, 0x62, 0xad, 0xf4, 0x0e, 0x0a, 0xbb, 0x75,
5103  0x3d, 0xe2, 0x6e, 0xd8, 0x2c, 0xc7, 0xbf, 0xf4,
5104  0xb9, 0x8c, 0x36, 0x9b, 0xca, 0xa2, 0xd0, 0x9c,
5105  0x72, 0x46, 0x39, 0xf6, 0xa6, 0x82, 0x03, 0x65,
5106  0x11, 0xc4, 0xbc, 0xbf, 0x2d, 0xa6, 0xf5, 0xd9,
5107  0x3b, 0x0a, 0xb5, 0x98, 0xfa, 0xb3, 0x78, 0xb9,
5108  0x1e, 0xf2, 0x2b, 0x4c, 0x62, 0xd5, 0xfd, 0xb2,
5109  0x7a, 0x1d, 0xdf, 0x33, 0xfd, 0x73, 0xf9, 0xa5,
5110  0xd8, 0x2d, 0x8c, 0x2a, 0xea, 0xd1, 0xfc, 0xb0,
5111  0x28, 0xb6, 0xe9, 0x49, 0x48, 0x13, 0x4b, 0x83,
5112  0x8a, 0x1b, 0x48, 0x7b, 0x24, 0xf7, 0x38, 0xde,
5113  0x6f, 0x41, 0x54, 0xb8, 0xab, 0x57, 0x6b, 0x06,
5114  0xdf, 0xc7, 0xa2, 0xd4, 0xa9, 0xf6, 0xf1, 0x36,
5115  0x62, 0x80, 0x88, 0xf2, 0x8b, 0x75, 0xd6, 0x80,
5116  0x75, 0x16, 0x03, 0x01, 0x00, 0x04, 0x0e, 0x00,
5117  0x00, 0x00
5118  };
5119  uint32_t server_hello_certificate_done_len = sizeof(server_hello_certificate_done);
5120 
5121  uint8_t client_key_exchange_cipher_enc_hs[] = {
5122  0x16, 0x03, 0x01, 0x00, 0x86, 0x10, 0x00, 0x00,
5123  0x80, 0x00, 0x80, 0x14, 0x2b, 0x2f, 0x9f, 0x02,
5124  0x1d, 0x4e, 0x0d, 0xa7, 0x41, 0x0f, 0x99, 0xc5,
5125  0xe9, 0x49, 0x22, 0x14, 0xa0, 0x42, 0x7b, 0xb4,
5126  0x6d, 0x4f, 0x82, 0x3c, 0x3a, 0x6e, 0xed, 0xd5,
5127  0x6e, 0x72, 0x71, 0xae, 0x00, 0x4a, 0x9a, 0xc9,
5128  0x0e, 0x2d, 0x08, 0xa2, 0xd3, 0x3a, 0xb0, 0xb2,
5129  0x1a, 0x56, 0x01, 0x7c, 0x9a, 0xfa, 0xfb, 0x1a,
5130  0xd7, 0x7e, 0x20, 0x68, 0x51, 0xd0, 0xfe, 0xd9,
5131  0xdc, 0xa7, 0x0b, 0xeb, 0x1a, 0xb6, 0xd3, 0xc7,
5132  0x17, 0x1f, 0xf3, 0x6e, 0x91, 0xdd, 0x06, 0x0d,
5133  0x48, 0xde, 0xcd, 0x0c, 0x36, 0x8c, 0x83, 0x29,
5134  0x9a, 0x40, 0x03, 0xcd, 0xf3, 0x1b, 0xdb, 0xd8,
5135  0x44, 0x6b, 0x75, 0xf3, 0x5a, 0x9f, 0x26, 0x1a,
5136  0xc4, 0x16, 0x35, 0x8f, 0xc1, 0x15, 0x19, 0xa9,
5137  0xdf, 0x07, 0xa9, 0xe5, 0x56, 0x45, 0x6d, 0xca,
5138  0x20, 0x3c, 0xcf, 0x8e, 0xbe, 0x44, 0x68, 0x73,
5139  0xc8, 0x0b, 0xc7, 0x14, 0x03, 0x01, 0x00, 0x01,
5140  0x01, 0x16, 0x03, 0x01, 0x00, 0x24, 0xf9, 0x7e,
5141  0x28, 0x77, 0xa9, 0x9a, 0x08, 0x0c, 0x2e, 0xa9,
5142  0x09, 0x15, 0x27, 0xcd, 0x93, 0x5f, 0xc0, 0x32,
5143  0x0a, 0x8d, 0x62, 0xd3, 0x54, 0x79, 0x6b, 0x51,
5144  0xd7, 0xba, 0x02, 0xd6, 0xdb, 0x66, 0xe8, 0x97,
5145  0x5d, 0x7a
5146  };
5147  uint32_t client_key_exchange_cipher_enc_hs_len = sizeof(client_key_exchange_cipher_enc_hs);
5148 
5149  TcpSession ssn;
5151 
5152  memset(&f, 0, sizeof(f));
5153  memset(&ssn, 0, sizeof(ssn));
5154  FLOW_INITIALIZE(&f);
5155  f.protoctx = (void *)&ssn;
5156  f.proto = IPPROTO_TCP;
5157  f.alproto = ALPROTO_TLS;
5158 
5160 
5161  FLOWLOCK_WRLOCK(&f);
5162  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
5163  STREAM_TOSERVER, client_hello,
5164  client_hello_len);
5165  FLOWLOCK_UNLOCK(&f);
5166  FAIL_IF(r != 0);
5167 
5168  SSLState *ssl_state = f.alstate;
5169  FAIL_IF_NULL(ssl_state);
5170 
5171  FAIL_IF(ssl_state->client_connp.bytes_processed != 0);
5172  FAIL_IF(ssl_state->client_connp.hs_bytes_processed != 0);
5173 
5174  FLOWLOCK_WRLOCK(&f);
5175  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT,
5176  server_hello_certificate_done,
5177  server_hello_certificate_done_len);
5178  FLOWLOCK_UNLOCK(&f);
5179  FAIL_IF(r != 0);
5180 
5181  FAIL_IF(ssl_state->client_connp.bytes_processed != 0);
5182  FAIL_IF(ssl_state->client_connp.hs_bytes_processed != 0);
5183 
5184  FLOWLOCK_WRLOCK(&f);
5185  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOSERVER,
5186  client_key_exchange_cipher_enc_hs,
5187  client_key_exchange_cipher_enc_hs_len);
5188  FLOWLOCK_UNLOCK(&f);
5189  FAIL_IF(r != 0);
5190 
5191  /* The reason hs_bytes_processed is 2 is because, the record
5192  * immediately after the client key exchange is 2 bytes long,
5193  * and next time we see a new handshake, it is after we have
5194  * seen a change cipher spec. Hence when we process the
5195  * handshake, we immediately break and don't parse the pdu from
5196  * where we left off, and leave the hs_bytes_processed var
5197  * isn't reset. */
5198  FAIL_IF(ssl_state->client_connp.bytes_processed != 0);
5199  FAIL_IF(ssl_state->client_connp.hs_bytes_processed != 2);
5200 
5201  AppLayerParserThreadCtxFree(alp_tctx);
5203  FLOW_DESTROY(&f);
5204 
5205  PASS;
5206 }
5207 
5208 static int SSLParserTest26(void)
5209 {
5210  Flow f;
5211  uint8_t client_hello[] = {
5212  0x16, 0x03, 0x01, 0x02, 0x0e, 0x01, 0x00, 0x02,
5213  0x0a, 0x03, 0x03, 0x58, 0x36, 0x15, 0x03, 0x8e,
5214  0x07, 0xf9, 0xad, 0x2a, 0xb7, 0x56, 0xbf, 0xe2,
5215  0xa2, 0xf8, 0x21, 0xe0, 0xbb, 0x69, 0xc2, 0xd6,
5216  0x76, 0xe6, 0x77, 0xfe, 0x09, 0xff, 0x8e, 0xac,
5217  0x80, 0xb5, 0x27, 0x20, 0xb7, 0xbb, 0x90, 0x35,
5218  0x7a, 0xdd, 0xd9, 0x67, 0xdf, 0x79, 0xd6, 0x16,
5219  0x90, 0xf6, 0xd7, 0x5c, 0xd3, 0x07, 0x19, 0x20,
5220  0x01, 0x39, 0x76, 0x25, 0x12, 0x32, 0x71, 0xa1,
5221  0x84, 0x8d, 0x2d, 0xea, 0x00, 0x88, 0xc0, 0x30,
5222  0xc0, 0x2c, 0xc0, 0x28, 0xc0, 0x24, 0xc0, 0x14,
5223  0xc0, 0x0a, 0x00, 0xa3, 0x00, 0x9f, 0x00, 0x6b,
5224  0x00, 0x6a, 0x00, 0x39, 0x00, 0x38, 0x00, 0x88,
5225  0x00, 0x87, 0xc0, 0x32, 0xc0, 0x2e, 0xc0, 0x2a,
5226  0xc0, 0x26, 0xc0, 0x0f, 0xc0, 0x05, 0x00, 0x9d,
5227  0x00, 0x3d, 0x00, 0x35, 0x00, 0x84, 0xc0, 0x12,
5228  0xc0, 0x08, 0x00, 0x16, 0x00, 0x13, 0xc0, 0x0d,
5229  0xc0, 0x03, 0x00, 0x0a, 0xc0, 0x2f, 0xc0, 0x2b,
5230  0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x13, 0xc0, 0x09,
5231  0x00, 0xa2, 0x00, 0x9e, 0x00, 0x67, 0x00, 0x40,
5232  0x00, 0x33, 0x00, 0x32, 0x00, 0x9a, 0x00, 0x99,
5233  0x00, 0x45, 0x00, 0x44, 0xc0, 0x31, 0xc0, 0x2d,
5234  0xc0, 0x29, 0xc0, 0x25, 0xc0, 0x0e, 0xc0, 0x04,
5235  0x00, 0x9c, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x96,
5236  0x00, 0x41, 0xc0, 0x11, 0xc0, 0x07, 0xc0, 0x0c,
5237  0xc0, 0x02, 0x00, 0x05, 0x00, 0x04, 0x00, 0x15,
5238  0x00, 0x12, 0x00, 0x09, 0x00, 0xff, 0x01, 0x00,
5239  0x01, 0x39, 0x00, 0x00, 0x00, 0x14, 0x00, 0x12,
5240  0x00, 0x00, 0x0f, 0x77, 0x77, 0x77, 0x2e, 0x79,
5241  0x6f, 0x75, 0x74, 0x75, 0x62, 0x65, 0x2e, 0x63,
5242  0x6f, 0x6d, 0x00, 0x0b, 0x00, 0x04, 0x03, 0x00,
5243  0x01, 0x02, 0x00, 0x0a, 0x00, 0x34, 0x00, 0x32,
5244  0x00, 0x0e, 0x00, 0x0d, 0x00, 0x19, 0x00, 0x0b,
5245  0x00, 0x0c, 0x00, 0x18, 0x00, 0x09, 0x00, 0x0a,
5246  0x00, 0x16, 0x00, 0x17, 0x00, 0x08, 0x00, 0x06,
5247  0x00, 0x07, 0x00, 0x14, 0x00, 0x15, 0x00, 0x04,
5248  0x00, 0x05, 0x00, 0x12, 0x00, 0x13, 0x00, 0x01,
5249  0x00, 0x02, 0x00, 0x03, 0x00, 0x0f, 0x00, 0x10,
5250  0x00, 0x11, 0x00, 0x23, 0x00, 0xb4, 0x05, 0x6c,
5251  0xfa, 0x27, 0x6f, 0x12, 0x2f, 0x2a, 0xe5, 0x56,
5252  0xcb, 0x42, 0x62, 0x44, 0xf2, 0xd7, 0xd1, 0x05,
5253  0x87, 0xd4, 0x52, 0x02, 0x10, 0x85, 0xa4, 0xa6,
5254  0x82, 0x6f, 0x6d, 0x7b, 0xaf, 0x11, 0xbe, 0x21,
5255  0x7e, 0x7c, 0x36, 0x03, 0x20, 0x29, 0xd8, 0xf9,
5256  0xe5, 0x2b, 0xe2, 0x26, 0xb2, 0x27, 0xc7, 0xb9,
5257  0xda, 0x59, 0xd7, 0xdc, 0xfd, 0x74, 0x74, 0x76,
5258  0xd0, 0x5e, 0xe4, 0xfe, 0x9d, 0xb7, 0x1b, 0x13,
5259  0x81, 0xce, 0x63, 0x75, 0x2b, 0x2f, 0x98, 0x3a,
5260  0x84, 0x46, 0xd3, 0x0c, 0xb3, 0x01, 0xdb, 0x62,
5261  0x51, 0x97, 0x92, 0x1c, 0xa5, 0x94, 0x60, 0xef,
5262  0xa6, 0xd8, 0xb2, 0x2f, 0x02, 0x42, 0x5c, 0xac,
5263  0xb4, 0xd9, 0x10, 0x2f, 0x7e, 0x89, 0xab, 0xa5,
5264  0xd7, 0x56, 0x6d, 0x03, 0xd2, 0x5f, 0x20, 0x2c,
5265  0xb6, 0x99, 0x2b, 0x66, 0xbd, 0xd4, 0xde, 0x53,
5266  0x76, 0x5c, 0x78, 0xf0, 0xe9, 0x6d, 0xa5, 0xc3,
5267  0x1a, 0x9e, 0x61, 0xb2, 0x45, 0xb0, 0xb3, 0x61,
5268  0xee, 0xa1, 0x07, 0xab, 0x2f, 0x84, 0xea, 0x43,
5269  0x76, 0x4b, 0x3d, 0xb0, 0xbe, 0xa4, 0xb4, 0x21,
5270  0xe1, 0xd3, 0xfd, 0x91, 0xe2, 0xe7, 0xf3, 0x38,
5271  0x9c, 0x56, 0x5f, 0xa1, 0xde, 0xa8, 0x2f, 0x0a,
5272  0x49, 0x6d, 0x44, 0x8e, 0xb7, 0xef, 0x4a, 0x6f,
5273  0x79, 0xb2, 0x00, 0x0d, 0x00, 0x20, 0x00, 0x1e,
5274  0x06, 0x01, 0x06, 0x02, 0x06, 0x03, 0x05, 0x01,
5275  0x05, 0x02, 0x05, 0x03, 0x04, 0x01, 0x04, 0x02,
5276  0x04, 0x03, 0x03, 0x01, 0x03, 0x02, 0x03, 0x03,
5277  0x02, 0x01, 0x02, 0x02, 0x02, 0x03, 0x00, 0x0f,
5278  0x00, 0x01, 0x01
5279  };
5280  uint32_t client_hello_len = sizeof(client_hello);
5281 
5282  uint8_t server_hello_change_cipher_spec[] = {
5283  0x16, 0x03, 0x03, 0x00, 0x57, 0x02, 0x00, 0x00,
5284  0x53, 0x03, 0x03, 0x58, 0x36, 0x15, 0x03, 0x9f,
5285  0x3b, 0xf3, 0x11, 0x96, 0x2b, 0xc3, 0xae, 0x91,
5286  0x8c, 0x5f, 0x8b, 0x3f, 0x90, 0xbd, 0xa9, 0x26,
5287  0x26, 0xb2, 0xfd, 0x12, 0xc5, 0xc5, 0x7b, 0xe4,
5288  0xd1, 0x3e, 0x81, 0x20, 0xb7, 0xbb, 0x90, 0x35,
5289  0x7a, 0xdd, 0xd9, 0x67, 0xdf, 0x79, 0xd6, 0x16,
5290  0x90, 0xf6, 0xd7, 0x5c, 0xd3, 0x07, 0x19, 0x20,
5291  0x01, 0x39, 0x76, 0x25, 0x12, 0x32, 0x71, 0xa1,
5292  0x84, 0x8d, 0x2d, 0xea, 0xc0, 0x2b, 0x00, 0x00,
5293  0x0b, 0xff, 0x01, 0x00, 0x01, 0x00, 0x00, 0x0b,
5294  0x00, 0x02, 0x01, 0x00, 0x14, 0x03, 0x03, 0x00,
5295  0x01, 0x01, 0x16, 0x03, 0x03, 0x00, 0x28, 0x00,
5296  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06,
5297  0x66, 0xfe, 0x07, 0x08, 0x33, 0x4d, 0xc2, 0x83,
5298  0x8e, 0x05, 0x8b, 0xf8, 0xd1, 0xb1, 0xa7, 0x16,
5299  0x4b, 0x42, 0x5c, 0x3a, 0xa4, 0x31, 0x0f, 0xba,
5300  0x84, 0x06, 0xcb, 0x9d, 0xc6, 0xc4, 0x66
5301  };
5302  uint32_t server_hello_change_cipher_spec_len = sizeof(server_hello_change_cipher_spec);
5303 
5304  TcpSession ssn;
5306 
5307  memset(&f, 0, sizeof(f));
5308  memset(&ssn, 0, sizeof(ssn));
5309  FLOW_INITIALIZE(&f);
5310  f.protoctx = (void *)&ssn;
5311  f.proto = IPPROTO_TCP;
5312  f.alproto = ALPROTO_TLS;
5313 
5315 
5316  FLOWLOCK_WRLOCK(&f);
5317  int r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS,
5318  STREAM_TOSERVER, client_hello,
5319  client_hello_len);
5320  FLOWLOCK_UNLOCK(&f);
5321  FAIL_IF(r != 0);
5322 
5323  SSLState *ssl_state = f.alstate;
5324  FAIL_IF_NULL(ssl_state);
5325 
5326  FAIL_IF((ssl_state->flags & SSL_AL_FLAG_STATE_CLIENT_HELLO) == 0);
5327  FAIL_IF_NULL(ssl_state->client_connp.session_id);
5328 
5329  FLOWLOCK_WRLOCK(&f);
5330  r = AppLayerParserParse(NULL, alp_tctx, &f, ALPROTO_TLS, STREAM_TOCLIENT,
5331  server_hello_change_cipher_spec,
5332  server_hello_change_cipher_spec_len);
5333  FLOWLOCK_UNLOCK(&f);
5334  FAIL_IF(r != 0);
5335 
5337  FAIL_IF((ssl_state->flags & SSL_AL_FLAG_SESSION_RESUMED) == 0);
5338 
5339  AppLayerParserThreadCtxFree(alp_tctx);
5341  FLOW_DESTROY(&f);
5342 
5343  PASS;
5344 }
5345 
5346 #endif /* UNITTESTS */
5347 
5349 {
5350 #ifdef UNITTESTS
5351  UtRegisterTest("SSLParserTest01", SSLParserTest01);
5352  UtRegisterTest("SSLParserTest02", SSLParserTest02);
5353  UtRegisterTest("SSLParserTest03", SSLParserTest03);
5354  UtRegisterTest("SSLParserTest04", SSLParserTest04);
5355  /* Updated by Anoop Saldanha. Faulty tests. Disable it for now */
5356  //UtRegisterTest("SSLParserTest05", SSLParserTest05, 1);
5357  //UtRegisterTest("SSLParserTest06", SSLParserTest06, 1);
5358  UtRegisterTest("SSLParserTest07", SSLParserTest07);
5359  //UtRegisterTest("SSLParserTest08", SSLParserTest08, 1);
5360  UtRegisterTest("SSLParserTest09", SSLParserTest09);
5361  UtRegisterTest("SSLParserTest10", SSLParserTest10);
5362  UtRegisterTest("SSLParserTest11", SSLParserTest11);
5363  UtRegisterTest("SSLParserTest12", SSLParserTest12);
5364  UtRegisterTest("SSLParserTest13", SSLParserTest13);
5365 
5366  UtRegisterTest("SSLParserTest14", SSLParserTest14);
5367  UtRegisterTest("SSLParserTest15", SSLParserTest15);
5368  UtRegisterTest("SSLParserTest16", SSLParserTest16);
5369  UtRegisterTest("SSLParserTest17", SSLParserTest17);
5370  UtRegisterTest("SSLParserTest18", SSLParserTest18);
5371  UtRegisterTest("SSLParserTest19", SSLParserTest19);
5372  UtRegisterTest("SSLParserTest20", SSLParserTest20);
5373  UtRegisterTest("SSLParserTest21", SSLParserTest21);
5374  UtRegisterTest("SSLParserTest22", SSLParserTest22);
5375  UtRegisterTest("SSLParserTest23", SSLParserTest23);
5376  UtRegisterTest("SSLParserTest24", SSLParserTest24);
5377  UtRegisterTest("SSLParserTest25", SSLParserTest25);
5378  UtRegisterTest("SSLParserTest26", SSLParserTest26);
5379 
5380  UtRegisterTest("SSLParserMultimsgTest01", SSLParserMultimsgTest01);
5381  UtRegisterTest("SSLParserMultimsgTest02", SSLParserMultimsgTest02);
5382 #endif /* UNITTESTS */
5383 
5384  return;
5385 }
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:343
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:242
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:239
#define SSLV3_HS_FINISHED
#define SSLV2_MT_REQUEST_CERTIFICATE
time_t cert0_not_after
int SCMapEnumNameToValue(const char *enum_name, SCEnumCharMap *table)
Maps a string name to an enum value from the supplied table. Please specify the last element of any m...
Definition: util-enum.c:41
void AppLayerParserRegisterLoggerFuncs(uint8_t ipproto, AppProto alproto, LoggerId(*StateGetTxLogged)(void *, void *), void(*StateSetTxLogged)(void *, void *, LoggerId))
uint16_t AppProto
char * val
Definition: conf.h:34
int Ja3BufferAppendBuffer(JA3Buffer **buffer1, JA3Buffer **buffer2)
Append buffer to buffer.
Definition: util-ja3.c:105
uint16_t bytes_processed
#define TRUE
#define ERR_DER_INVALID_SIZE
uint8_t * trec
#define SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY
Definition: app-layer-ssl.h:77
void * protoctx
Definition: flow.h:395
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:433
#define SSL_AL_FLAG_HB_CLIENT_INIT
Definition: app-layer-ssl.h:94
#define APP_LAYER_PARSER_NO_REASSEMBLY
int AppLayerParserConfParserEnabled(const char *ipproto, const char *alproto_name)
check if a parser is enabled in the config Returns enabled always if: were running unittests and when...
void AppLayerParserTriggerRawStreamReassembly(Flow *f, int direction)
#define SCCalloc(nm, a)
Definition: util-mem.h:197
uint64_t detect_flags_tc
#define SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED
Definition: app-layer-ssl.h:78
Data structure to store app layer decoder events.
int AppLayerParserRegisterParser(uint8_t ipproto, AppProto alproto, uint8_t direction, AppLayerParserFPtr Parser)
Register app layer parser for the protocol.
#define SSL_AL_FLAG_STATE_FINISHED
Definition: app-layer-ssl.h:90
#define SSLV3_HS_CERTIFICATE_VERIFY
#define SSL_AL_FLAG_STATE_SERVER_KEYX
Definition: app-layer-ssl.h:86
void AppLayerParserRegisterGetTx(uint8_t ipproto, AppProto alproto, void *(StateGetTx)(void *alstate, uint64_t tx_id))
enum SslConfigEncryptHandling encrypt_mode
Definition: app-layer-ssl.c:96
uint16_t session_id_length
#define SSLV2_MT_CLIENT_HELLO
#define SSL_AL_FLAG_CH_VERSION_EXTENSION
uint16_t events
#define TAILQ_INIT(head)
Definition: queue.h:370
int Asn1DerGetIssuerDN(const Asn1Generic *cert, char *buffer, uint32_t length, uint32_t *errcode)
int AppLayerProtoDetectPPParseConfPorts(const char *ipproto_name, uint8_t ipproto, const char *alproto_name, AppProto alproto, uint16_t min_depth, uint16_t max_depth, ProbingParserFPtr ProbingParserTs, ProbingParserFPtr ProbingParserTc)
void AppLayerParserRegisterGetEventInfo(uint8_t ipproto, AppProto alproto, int(*StateGetEventInfo)(const char *event_name, int *event_id, AppLayerEventType *event_type))
#define SSLV3_ALERT_PROTOCOL
#define SCLogError(err_code,...)
Macro used to log ERROR messages.
Definition: util-debug.h:294
#define FLOW_DESTROY(f)
Definition: flow-util.h: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:259
uint32_t trec_pos
#define SCReturnInt(x)
Definition: util-debug.h:341
#define SSLV2_MT_CLIENT_MASTER_KEY
#define SCLogWarning(err_code,...)
Macro used to log WARNING messages.
Definition: util-debug.h:281
#define SCRealloc(x, a)
Definition: util-mem.h:182
#define SSLV3_HS_CERTIFICATE_REQUEST
#define SSL_EXTENSION_SESSION_TICKET
#define TAILQ_INSERT_TAIL(head, elm, field)
Definition: queue.h:385
#define SSL_EXTENSION_EARLY_DATA
void AppLayerDecoderEventsFreeEvents(AppLayerDecoderEvents **events)
#define SSLV2_MT_ERROR
Definition: conf.h:32
#define ERR_DER_GENERIC
#define SCMalloc(a)
Definition: util-mem.h:166
#define ERR_DER_ELEMENT_SIZE_TOO_BIG
int Asn1DerGetSubjectDN(const Asn1Generic *cert, char *buffer, uint32_t length, uint32_t *errcode)
void SSLParserRegisterTests(void)
uint16_t payload_len
#define SSL_SNI_TYPE_HOST_NAME
#define SSL_AL_FLAG_STATE_CLIENT_KEYX
Definition: app-layer-ssl.h:85
#define SSLV3_RECORD_HDR_LEN
#define SCLogInfo(...)
Macro used to log INFORMATIONAL messages.
Definition: util-debug.h:254
uint32_t record_length
#define SSL_AL_FLAG_SSL_CLIENT_HS
Definition: app-layer-ssl.h:75
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:228
uint32_t cert_log_flag
#define ERR_DER_MISSING_ELEMENT
SslConfig ssl_config
#define SSLV2_MT_CLIENT_FINISHED
uint32_t cert_len
int AppLayerProtoDetectPMRegisterPatternCS(uint8_t ipproto, AppProto alproto, const char *pattern, uint16_t depth, uint16_t offset, uint8_t direction)
Registers a case-sensitive pattern for protocol detection.
uint16_t tx_id
#define SSL_RECORD_MINIMUM_LENGTH
void AppLayerParserRegisterGetEventsFunc(uint8_t ipproto, AppProto alproto, AppLayerDecoderEvents *(*StateGetEvents)(void *, uint64_t))
void RegisterSSLParsers(void)
Function to register the SSL protocol parser and other functions.
void AppLayerParserRegisterProtocolUnittests(uint8_t ipproto, AppProto alproto, void(*RegisterUnittests)(void))
#define SSLV2_MT_CLIENT_CERTIFICATE
uint8_t * cert_data
DetectEngineState * de_state
#define SSLV3_CLIENT_HELLO_RANDOM_LEN
#define STREAM_START
Definition: stream.h:29
#define SSLV3_HANDSHAKE_PROTOCOL
#define SSLV3_HS_CLIENT_KEY_EXCHANGE
#define ERR_DER_UNSUPPORTED_STRING
#define SSLV3_HS_CLIENT_HELLO
#define SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC
Definition: app-layer-ssl.h:69
#define FLOW_INITIALIZE(f)
Definition: flow-util.h:39
#define STREAM_TOSERVER
Definition: stream.h:31
#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
uint8_t content_type
Definition: