112 {
"CERTIFICATE_INVALID_ALGORITHMIDENTIFIER",
147 #define SSL_CONFIG_DEFAULT_JA3 0
166 #define SSLV3_CHANGE_CIPHER_SPEC 20
167 #define SSLV3_ALERT_PROTOCOL 21
168 #define SSLV3_HANDSHAKE_PROTOCOL 22
169 #define SSLV3_APPLICATION_PROTOCOL 23
170 #define SSLV3_HEARTBEAT_PROTOCOL 24
173 #define SSLV3_HS_HELLO_REQUEST 0
174 #define SSLV3_HS_CLIENT_HELLO 1
175 #define SSLV3_HS_SERVER_HELLO 2
176 #define SSLV3_HS_NEW_SESSION_TICKET 4
177 #define SSLV3_HS_CERTIFICATE 11
178 #define SSLV3_HS_SERVER_KEY_EXCHANGE 12
179 #define SSLV3_HS_CERTIFICATE_REQUEST 13
180 #define SSLV3_HS_SERVER_HELLO_DONE 14
181 #define SSLV3_HS_CERTIFICATE_VERIFY 15
182 #define SSLV3_HS_CLIENT_KEY_EXCHANGE 16
183 #define SSLV3_HS_FINISHED 20
184 #define SSLV3_HS_CERTIFICATE_URL 21
185 #define SSLV3_HS_CERTIFICATE_STATUS 22
188 #define SSLV2_MT_ERROR 0
189 #define SSLV2_MT_CLIENT_HELLO 1
190 #define SSLV2_MT_CLIENT_MASTER_KEY 2
191 #define SSLV2_MT_CLIENT_FINISHED 3
192 #define SSLV2_MT_SERVER_HELLO 4
193 #define SSLV2_MT_SERVER_VERIFY 5
194 #define SSLV2_MT_SERVER_FINISHED 6
195 #define SSLV2_MT_REQUEST_CERTIFICATE 7
196 #define SSLV2_MT_CLIENT_CERTIFICATE 8
198 #define SSLV3_RECORD_HDR_LEN 5
199 #define SSLV3_MESSAGE_HDR_LEN 4
201 #define SSLV3_RECORD_MAX_LEN ((1 << 14) + 1024)
203 #define SSLV3_CLIENT_HELLO_VERSION_LEN 2
204 #define SSLV3_CLIENT_HELLO_RANDOM_LEN 32
207 #define TLS_HB_REQUEST 1
208 #define TLS_HB_RESPONSE 2
210 #define SSL_RECORD_MINIMUM_LENGTH 6
212 #define SHA1_STRING_LENGTH 60
214 #define HAS_SPACE(n) ((uint64_t)(input - initial_input) + (uint64_t)(n) <= (uint64_t)(input_len))
220 #define SSL_DECODER_ERROR(e) \
221 (struct SSLDecoderResult) \
225 #define SSL_DECODER_OK(c) \
226 (struct SSLDecoderResult) \
230 #define SSL_DECODER_INCOMPLETE(c, n) \
231 (struct SSLDecoderResult) \
236 static inline int SafeMemcpy(
void *
dst,
size_t dst_offset,
size_t dst_size,
237 const void *
src,
size_t src_offset,
size_t src_size,
size_t src_tocopy)
WARN_UNUSED;
239 static inline int SafeMemcpy(
void *
dst,
size_t dst_offset,
size_t dst_size,
240 const void *
src,
size_t src_offset,
size_t src_size,
size_t src_tocopy)
247 if (dst_offset < dst_size && src_offset < src_size &&
248 src_tocopy <= (src_size - src_offset) &&
249 src_tocopy <= (dst_size - dst_offset)) {
250 memcpy(
dst + dst_offset,
src + src_offset, src_tocopy);
256 #ifdef DEBUG_VALIDATION
257 #define ValidateRecordState(connp) \
259 DEBUG_VALIDATE_BUG_ON(((connp)->record_length + SSLV3_RECORD_HDR_LEN) < \
260 (connp)->bytes_processed); \
263 #define ValidateRecordState(...)
266 #define SSLParserHSReset(connp) \
268 (connp)->handshake_type = 0; \
269 (connp)->message_length = 0; \
272 #define SSLParserReset(state) \
274 SCLogDebug("resetting state"); \
275 (state)->curr_connp->bytes_processed = 0; \
276 SSLParserHSReset((state)->curr_connp); \
279 #define SSLSetEvent(ssl_state, event) \
281 SCLogDebug("setting event %u", (event)); \
282 if ((ssl_state) == NULL) { \
283 SCLogDebug("could not set decoder event %u", event); \
285 AppLayerDecoderEventsSetEventRaw(&(ssl_state)->tx_data.events, (event)); \
286 (ssl_state)->events++; \
290 static void *SSLGetTx(
void *state, uint64_t tx_id)
296 static uint64_t SSLGetTxCnt(
void *state)
302 static int SSLGetAlstateProgress(
void *tx, uint8_t direction)
318 if (direction == STREAM_TOSERVER &&
334 static AppLayerStateData *SSLGetStateData(
void *vstate)
346 strlcat(buffer,
"UNDETERMINED", 13);
367 strlcat(buffer,
"TLS 1.3 draft-28", 17);
370 strlcat(buffer,
"TLS 1.3 draft-27", 17);
373 strlcat(buffer,
"TLS 1.3 draft-26", 17);
376 strlcat(buffer,
"TLS 1.3 draft-25", 17);
379 strlcat(buffer,
"TLS 1.3 draft-24", 17);
382 strlcat(buffer,
"TLS 1.3 draft-23", 17);
385 strlcat(buffer,
"TLS 1.3 draft-22", 17);
388 strlcat(buffer,
"TLS 1.3 draft-21", 17);
391 strlcat(buffer,
"TLS 1.3 draft-20", 17);
394 strlcat(buffer,
"TLS 1.3 draft-19", 17);
397 strlcat(buffer,
"TLS 1.3 draft-18", 17);
400 strlcat(buffer,
"TLS 1.3 draft-17", 17);
403 strlcat(buffer,
"TLS 1.3 draft-16", 17);
406 strlcat(buffer,
"TLS 1.3 draft-<16", 18);
409 strlcat(buffer,
"TLS 1.3 draft-20-fb", 20);
412 strlcat(buffer,
"TLS 1.3 draft-21-fb", 20);
415 strlcat(buffer,
"TLS 1.3 draft-22-fb", 20);
418 strlcat(buffer,
"TLS 1.3 draft-23-fb", 20);
421 strlcat(buffer,
"TLS 1.3 draft-26-fb", 20);
424 snprintf(buffer, 7,
"0x%04x",
version);
429 static void TlsDecodeHSCertificateErrSetEvent(
SSLState *ssl_state, uint32_t err)
472 static inline int TlsDecodeHSCertificateFingerprint(
473 SSLStateConnp *connp,
const uint8_t *input, uint32_t cert_len)
483 if (SCSha1HashBuffer(input, cert_len, hash,
sizeof(hash)) == 1) {
490 static inline int TlsDecodeHSCertificateAddCertToChain(
491 SSLStateConnp *connp,
const uint8_t *input, uint32_t cert_len)
505 const uint8_t *
const initial_input,
const uint32_t input_len,
const int certn)
507 const uint8_t *input = (uint8_t *)initial_input;
508 uint32_t err_code = 0;
515 uint32_t cert_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
524 x509 = rs_x509_decode(input, cert_len, &err_code);
526 TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
530 char *
str = rs_x509_get_subject(x509);
537 str = rs_x509_get_issuer(x509);
544 str = rs_x509_get_serial(x509);
560 rc = TlsDecodeHSCertificateFingerprint(connp, input, cert_len);
562 SCLogDebug(
"TlsDecodeHSCertificateFingerprint failed with %d", rc);
567 rc = TlsDecodeHSCertificateAddCertToChain(connp, input, cert_len);
569 SCLogDebug(
"TlsDecodeHSCertificateAddCertToChain failed with %d", rc);
575 return (input - initial_input);
579 TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
596 const uint8_t *
const initial_input,
const uint32_t input_len)
598 const uint8_t *input = (uint8_t *)initial_input;
603 const uint32_t cert_chain_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
622 uint32_t processed_len = 0;
624 while (processed_len < cert_chain_len) {
625 int rc = TlsDecodeHSCertificate(ssl_state, connp, connp->
certs_buffer + processed_len,
631 if (processed_len + (uint32_t)rc > cert_chain_len) {
635 processed_len += (uint32_t)rc;
638 return processed_len + 3;
652 static inline int TLSDecodeValueIsGREASE(
const uint16_t value)
678 static inline int TLSDecodeHSHelloVersion(
SSLState *ssl_state,
679 const uint8_t *
const initial_input,
680 const uint32_t input_len)
682 uint8_t *input = (uint8_t *)initial_input;
691 uint16_t
version = (uint16_t)(*input << 8) | *(input + 1);
728 return (input - initial_input);
731 static inline int TLSDecodeHSHelloRandom(
SSLState *ssl_state,
732 const uint8_t *
const initial_input,
733 const uint32_t input_len)
735 uint8_t *input = (uint8_t *)initial_input;
755 return (input - initial_input);
758 static inline int TLSDecodeHSHelloSessionID(
SSLState *ssl_state,
759 const uint8_t *
const initial_input,
760 const uint32_t input_len)
762 uint8_t *input = (uint8_t *)initial_input;
767 uint8_t session_id_length = *input;
781 input, 0, input_len, session_id_length) != 0) {
798 input += session_id_length;
800 return (input - initial_input);
809 static inline int TLSDecodeHSHelloCipherSuites(
SSLState *ssl_state,
810 const uint8_t *
const initial_input,
811 const uint32_t input_len)
813 const uint8_t *input = initial_input;
818 uint16_t cipher_suites_length;
821 cipher_suites_length = 2;
823 cipher_suites_length = (uint16_t)(*input << 8) | *(input + 1);
833 if ((cipher_suites_length % 2) != 0) {
839 if (ja3_cipher_suites == NULL)
842 uint16_t processed_len = 0;
844 while (processed_len < cipher_suites_length)
851 uint16_t cipher_suite = (uint16_t)(*input << 8) | *(input + 1);
854 if (TLSDecodeValueIsGREASE(cipher_suite) != 1) {
872 input += cipher_suites_length;
875 return (input - initial_input);
884 static inline int TLSDecodeHSHelloCompressionMethods(
SSLState *ssl_state,
885 const uint8_t *
const initial_input,
886 const uint32_t input_len)
888 const uint8_t *input = initial_input;
897 uint8_t compression_methods_length = *input;
900 if (!(
HAS_SPACE(compression_methods_length)))
903 input += compression_methods_length;
906 return (input - initial_input);
915 static inline int TLSDecodeHSHelloExtensionSni(
SSLState *ssl_state,
916 const uint8_t *
const initial_input,
917 const uint32_t input_len)
919 uint8_t *input = (uint8_t *)initial_input;
934 uint8_t sni_type = *input;
949 uint16_t sni_len = (uint16_t)(*input << 8) | *(input + 1);
955 if (!(
HAS_SPACE(sni_len)) || sni_len > 255 || sni_len == 0) {
968 return (input - initial_input);
971 const size_t sni_strlen = sni_len + 1;
976 const size_t consumed = input - initial_input;
978 initial_input, consumed, input_len, sni_len) != 0) {
987 return (input - initial_input);
998 static inline int TLSDecodeHSHelloExtensionSupportedVersions(
SSLState *ssl_state,
999 const uint8_t *
const initial_input,
1000 const uint32_t input_len)
1002 const uint8_t *input = initial_input;
1010 goto invalid_length;
1012 uint8_t supported_ver_len = *input;
1015 if (supported_ver_len < 2)
1016 goto invalid_length;
1019 goto invalid_length;
1024 while (i + 1 < (uint16_t)supported_ver_len) {
1025 uint16_t ver = (uint16_t)(input[i] << 8) | input[i + 1];
1026 if (TLSVersionValid(ver)) {
1036 input += supported_ver_len;
1040 goto invalid_length;
1042 uint16_t ver = (uint16_t)(*input << 8) | *(input + 1);
1053 return (input - initial_input);
1063 static inline int TLSDecodeHSHelloExtensionEllipticCurves(
SSLState *ssl_state,
1064 const uint8_t *
const initial_input,
1065 const uint32_t input_len,
1068 const uint8_t *input = initial_input;
1075 goto invalid_length;
1077 uint16_t elliptic_curves_len = (uint16_t)(*input << 8) | *(input + 1);
1081 goto invalid_length;
1085 uint16_t ec_processed_len = 0;
1087 while (ec_processed_len < elliptic_curves_len)
1090 goto invalid_length;
1092 uint16_t elliptic_curve = (uint16_t)(*input << 8) | *(input + 1);
1095 if (TLSDecodeValueIsGREASE(elliptic_curve) != 1) {
1102 ec_processed_len += 2;
1107 input += elliptic_curves_len;
1110 return (input - initial_input);
1120 static inline int TLSDecodeHSHelloExtensionEllipticCurvePF(
SSLState *ssl_state,
1121 const uint8_t *
const initial_input,
1122 const uint32_t input_len,
1125 const uint8_t *input = initial_input;
1132 goto invalid_length;
1134 uint8_t ec_pf_len = *input;
1138 goto invalid_length;
1142 uint8_t ec_pf_processed_len = 0;
1144 while (ec_pf_processed_len < ec_pf_len)
1146 uint8_t elliptic_curve_pf = *input;
1149 if (TLSDecodeValueIsGREASE(elliptic_curve_pf) != 1) {
1156 ec_pf_processed_len += 1;
1164 return (input - initial_input);
1174 static inline int TLSDecodeHSHelloExtensions(
SSLState *ssl_state,
1175 const uint8_t *
const initial_input,
1176 const uint32_t input_len)
1178 const uint8_t *input = initial_input;
1186 JA3Buffer *ja3_elliptic_curves_pf = NULL;
1190 if (ja3_extensions == NULL)
1195 if (ja3_elliptic_curves == NULL)
1199 if (ja3_elliptic_curves_pf == NULL)
1208 uint16_t extensions_len = (uint16_t)(*input << 8) | *(input + 1);
1212 goto invalid_length;
1214 uint16_t processed_len = 0;
1216 while (processed_len < extensions_len)
1219 goto invalid_length;
1221 uint16_t ext_type = (uint16_t)(*input << 8) | *(input + 1);
1225 goto invalid_length;
1227 uint16_t ext_len = (uint16_t)(*input << 8) | *(input + 1);
1231 goto invalid_length;
1237 ret = TLSDecodeHSHelloExtensionSni(ssl_state, input,
1250 ret = TLSDecodeHSHelloExtensionEllipticCurves(ssl_state, input,
1252 ja3_elliptic_curves);
1264 ret = TLSDecodeHSHelloExtensionEllipticCurvePF(ssl_state, input,
1266 ja3_elliptic_curves_pf);
1290 ret = TLSDecodeHSHelloExtensionSupportedVersions(ssl_state, input,
1321 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1328 processed_len += ext_len + 4;
1340 &ja3_elliptic_curves);
1345 &ja3_elliptic_curves_pf);
1351 return (input - initial_input);
1359 if (ja3_extensions != NULL)
1361 if (ja3_elliptic_curves != NULL)
1363 if (ja3_elliptic_curves_pf != NULL)
1369 static int TLSDecodeHandshakeHello(
SSLState *ssl_state,
1370 const uint8_t *
const input,
1371 const uint32_t input_len)
1374 uint32_t parsed = 0;
1376 ret = TLSDecodeHSHelloVersion(ssl_state, input, input_len);
1382 ret = TLSDecodeHSHelloRandom(ssl_state, input + parsed, input_len - parsed);
1393 ret = TLSDecodeHSHelloSessionID(ssl_state, input + parsed,
1394 input_len - parsed);
1401 ret = TLSDecodeHSHelloCipherSuites(ssl_state, input + parsed,
1402 input_len - parsed);
1413 ret = TLSDecodeHSHelloCompressionMethods(ssl_state, input + parsed,
1414 input_len - parsed);
1421 ret = TLSDecodeHSHelloExtensions(ssl_state, input + parsed,
1422 input_len - parsed);
1434 #ifdef DEBUG_VALIDATION
1444 const uint8_t *
const initial_input,
const uint32_t input_len)
1446 int rc = TlsDecodeHSCertificates(ssl_state, connp, initial_input, input_len);
1451 }
else if (rc < 0) {
1452 SCLogDebug(
"error parsing cert, reset state");
1459 static int SupportedHandshakeType(
const uint8_t
type)
1488 static int SSLv3ParseHandshakeType(
SSLState *ssl_state,
const uint8_t *input,
1489 uint32_t input_len, uint8_t direction)
1491 const uint8_t *initial_input = input;
1494 if (input_len == 0) {
1503 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1512 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1527 rc = SSLv3ParseHandshakeTypeCertificate(ssl_state,
1564 static int SSLv3ParseHandshakeProtocol(
SSLState *ssl_state,
const uint8_t *input,
1565 uint32_t input_len, uint8_t direction)
1567 const uint8_t *initial_input = input;
1578 SCLogDebug(
"partial handshake record in place");
1581 const uint32_t add =
MIN(need, input_len);
1587 const uint32_t new_size = avail + (4096 - (avail % 4096));
1588 SCLogDebug(
"new_size %u, avail %u", new_size, avail);
1596 SCLogDebug(
"ssl_state->curr_connp->hs_buffer_offset %u "
1597 "ssl_state->curr_connp->hs_buffer_size %u",
1619 SCLogDebug(
"got all data now: handshake_type %u message_length %u",
1650 SCLogDebug(
"input %p input_len %u", input, input_len);
1652 if (input_len < 4) {
1659 SCLogDebug(
"handshake_type %u message len %u input %p input_len %u",
1669 SCLogDebug(
"supported_type %s handshake_type %u/%02x", supported_type ?
"true" :
"false",
1671 if (!supported_type) {
1672 uint32_t avail_record_len =
MIN(input_len, record_len);
1673 input += avail_record_len;
1674 input_len -= avail_record_len;
1688 if (record_len > input_len) {
1689 const uint32_t avail = input_len;
1690 const uint32_t size = avail + (4096 - (avail % 4096));
1691 SCLogDebug(
"initial buffer size %u, based on input %u", size, avail);
1700 if (input_len > 0) {
1708 SCLogDebug(
"opened record buffer %p size %u offset %u type %u msg_size %u",
1715 return (input - initial_input);
1719 int retval = SSLv3ParseHandshakeType(
1721 if (retval < 0 || retval > (
int)input_len) {
1725 SCLogDebug(
"retval %d input_len %u", retval, input_len);
1727 input_len -= retval;
1733 return (input - initial_input);
1747 static int SSLv3ParseHeartbeatProtocol(
SSLState *ssl_state,
const uint8_t *input,
1748 uint32_t input_len, uint8_t direction)
1752 uint32_t padding_len;
1755 if (input_len < 3) {
1772 SCLogDebug(
"HeartBeat Record type sent in the toclient direction!");
1775 SCLogDebug(
"HeartBeat Record type sent in the toserver direction!");
1783 SCLogDebug(
"Encrypted HeartBeat Request In-flight. Storing len %u",
1788 payload_len = (uint16_t)(*input << 8) | *(input + 1);
1793 SCLogDebug(
"We have a short record in HeartBeat Request");
1801 if (padding_len < 16) {
1802 SCLogDebug(
"We have a short record in HeartBeat Request");
1816 SCLogDebug(
"Multiple in-flight server initiated HeartBeats");
1822 SCLogDebug(
"Multiple in-flight client initiated HeartBeats");
1840 SCLogDebug(
"My heart is bleeding.. OpenSSL HeartBleed response (%u)",
1859 static int SSLv3ParseRecord(uint8_t direction,
SSLState *ssl_state,
1860 const uint8_t *input, uint32_t input_len)
1862 const uint8_t *initial_input = input;
1864 if (input_len == 0) {
1868 uint8_t skip_version = 0;
1872 if (direction == 0) {
1886 if (input_len >= 5) {
1888 if (!skip_version) {
1897 if (--input_len == 0)
1903 if (!skip_version) {
1908 if (--input_len == 0)
1913 if (!skip_version) {
1918 if (--input_len == 0)
1924 if (--input_len == 0)
1930 if (--input_len == 0)
1938 return (input - initial_input);
1941 static int SSLv2ParseRecord(uint8_t direction,
SSLState *ssl_state,
1942 const uint8_t *input, uint32_t input_len)
1944 const uint8_t *initial_input = input;
1946 if (input_len == 0) {
1961 if (--input_len == 0)
1968 if (--input_len == 0)
1975 if (--input_len == 0)
1992 if (--input_len == 0)
1999 if (--input_len == 0)
2006 if (--input_len == 0)
2013 if (--input_len == 0)
2022 return (input - initial_input);
2027 const StreamSlice stream_slice)
2029 const uint8_t *initial_input = input;
2031 if (ssl_state->curr_connp->bytes_processed == 0) {
2032 if (input[0] & 0x80) {
2033 ssl_state->curr_connp->record_lengths_length = 2;
2035 ssl_state->curr_connp->record_lengths_length = 3;
2043 SCLogDebug(
"direction %u ssl_state->curr_connp->record_lengths_length + 1 %u, "
2044 "ssl_state->curr_connp->bytes_processed %u",
2045 direction, ssl_state->curr_connp->record_lengths_length + 1,
2046 ssl_state->curr_connp->bytes_processed);
2049 if (ssl_state->curr_connp->bytes_processed <
2050 (ssl_state->curr_connp->record_lengths_length + 1)) {
2051 const int retval = SSLv2ParseRecord(direction, ssl_state, input, input_len);
2053 ssl_state->curr_connp->record_length);
2054 if (retval < 0 || retval > (
int)input_len) {
2061 ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length,
2070 if (ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length >
2071 input_len + ssl_state->curr_connp->bytes_processed) {
2072 uint32_t
needed = ssl_state->curr_connp->record_length;
2073 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2074 ssl_state->curr_connp->record_length, input_len, (uint32_t)(input - initial_input),
2079 if (input_len == 0) {
2084 if (ssl_state->curr_connp->record_length == 0) {
2091 if (ssl_state->curr_connp->record_lengths_length == 0) {
2092 SCLogDebug(
"SSLv2 record lengths length is zero");
2097 switch (ssl_state->curr_connp->content_type) {
2099 SCLogDebug(
"SSLV2_MT_ERROR msg_type received. Error encountered "
2100 "in establishing the sslv2 session, may be version");
2106 if (input_len < 6) {
2114 const uint16_t
version = (uint16_t)(input[0] << 8) | input[1];
2116 ssl_state->curr_connp->version =
version;
2117 uint16_t session_id_length = (input[5]) | (uint16_t)(input[4] << 8);
2120 ssl_state->curr_connp->bytes_processed += 6;
2121 if (session_id_length == 0) {
2128 SCLogDebug(
"Client hello is not seen before master key "
2136 if (direction == 1) {
2137 SCLogDebug(
"Incorrect SSL Record type sent in the toclient "
2146 if (direction == 0 &&
2147 !(ssl_state->curr_connp->content_type &
2149 SCLogDebug(
"Incorrect SSL Record type sent in the toserver "
2160 if (direction == 0) {
2163 SCLogDebug(
"SSLv2 client side has started the encryption");
2166 SCLogDebug(
"SSLv2 client side has started the encryption");
2170 SCLogDebug(
"SSLv2 Server side has started the encryption");
2185 SCLogDebug(
"SSLv2 No reassembly & inspection has been set");
2198 ssl_state->flags |= ssl_state->current_flags;
2200 if (input_len + ssl_state->curr_connp->bytes_processed >=
2201 (ssl_state->curr_connp->record_length +
2202 ssl_state->curr_connp->record_lengths_length)) {
2205 uint32_t diff = ssl_state->curr_connp->record_length +
2206 ssl_state->curr_connp->record_lengths_length + -
2207 ssl_state->curr_connp->bytes_processed;
2215 ssl_state->curr_connp->bytes_processed += input_len;
2222 const StreamSlice stream_slice)
2224 uint32_t parsed = 0;
2225 uint32_t record_len;
2226 const bool first_call = (ssl_state->curr_connp->bytes_processed == 0);
2229 const uint16_t prev_version = ssl_state->curr_connp->version;
2231 int retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
2232 if (retval < 0 || retval > (
int)input_len) {
2240 SCLogDebug(
"%s input %p record_length %u", (direction == 0) ?
"toserver" :
"toclient",
2241 input, ssl_state->curr_connp->record_length);
2254 "incomplete header, return %u bytes consumed and wait for more data", parsed);
2261 record_len =
MIN(input_len - parsed, ssl_state->curr_connp->record_length);
2263 "record_len %u (input_len %u, parsed %u, ssl_state->curr_connp->record_length %u)",
2264 record_len, input_len, parsed, ssl_state->curr_connp->record_length);
2266 bool unknown_record =
false;
2267 switch (ssl_state->curr_connp->content_type) {
2275 unknown_record =
true;
2282 if (unknown_record) {
2286 ssl_state->curr_connp->bytes_processed = 0;
2287 ssl_state->curr_connp->content_type = 0;
2288 ssl_state->curr_connp->record_length = 0;
2290 ssl_state->curr_connp->version = prev_version;
2294 if (unknown_record) {
2302 if (ssl_state->curr_connp->record_length == 0) {
2308 if (!TLSVersionValid(ssl_state->curr_connp->version)) {
2309 SCLogDebug(
"ssl_state->curr_connp->version %04x", ssl_state->curr_connp->version);
2323 record_len = (ssl_state->curr_connp->record_length +
SSLV3_RECORD_HDR_LEN)- ssl_state->curr_connp->bytes_processed;
2324 record_len =
MIN(input_len, record_len);
2326 SCLogDebug(
"record length %u processed %u got %u",
2327 ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed, record_len);
2330 if (ssl_state->curr_connp->record_length > input_len - parsed) {
2336 uint32_t
needed = ssl_state->curr_connp->record_length;
2337 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2338 ssl_state->curr_connp->record_length, input_len, parsed,
needed);
2344 if (record_len == 0) {
2349 ssl_state->curr_connp->record_length, direction,
TLS_FRAME_DATA);
2351 switch (ssl_state->curr_connp->content_type) {
2382 SCLogDebug(
"setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
2390 SCLogDebug(
"setting APP_LAYER_PARSER_NO_REASSEMBLY");
2415 if (ssl_state->curr_connp->record_length < 4) {
2418 SCLogDebug(
"record len < 4 => %u", ssl_state->curr_connp->record_length);
2422 int retval = SSLv3ParseHandshakeProtocol(ssl_state, input + parsed,
2423 record_len, direction);
2425 if (retval < 0 || retval > (
int)record_len) {
2437 int retval = SSLv3ParseHeartbeatProtocol(ssl_state, input + parsed,
2438 record_len, direction);
2452 parsed += record_len;
2453 ssl_state->curr_connp->bytes_processed += record_len;
2455 if (ssl_state->curr_connp->bytes_processed >=
2459 ssl_state->f, direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
2490 static AppLayerResult SSLDecode(
Flow *f, uint8_t direction,
void *alstate,
2494 uint32_t counter = 0;
2496 const uint8_t *input = StreamSliceGetData(&stream_slice);
2497 const uint8_t *init_input = input;
2498 int32_t input_len = (int32_t)StreamSliceGetDataLen(&stream_slice);
2500 if (input == NULL &&
2507 }
else if (input == NULL || input_len == 0) {
2523 while (input_len > 0) {
2524 if (counter > max_records) {
2525 SCLogDebug(
"Looks like we have looped quite a bit. Reset state "
2526 "and get out of here");
2537 if ((input[0] & 0x80) || (input[0] & 0x40)) {
2546 SCLogDebug(
"record %u: bytes_processed %u, version %02X, input_len %u", counter,
2553 SCLogDebug(
"Continuing parsing SSLv2 record");
2556 SSLv2Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2559 SCLogDebug(
"Error parsing SSLv2. Resetting parser "
2560 "state. Let's get outta here");
2567 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2568 (uintmax_t)(input - init_input), r.
needed);
2573 SCLogDebug(
"SSLv2 decoder consumed %d bytes: %u left", r.
retval, input_len);
2576 SCLogDebug(
"New TLS record: record_length %u",
2579 SCLogDebug(
"Continuing parsing TLS record: record_length %u, bytes_processed %u",
2583 SSLv3Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2586 SCLogDebug(
"Error parsing TLS. Resetting parser "
2587 "state. Let's get outta here");
2592 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2593 (uintmax_t)(input - init_input), r.
needed);
2632 StreamSlice stream_slice,
void *local_data)
2634 return SSLDecode(f, 0 , alstate, pstate, stream_slice);
2638 StreamSlice stream_slice,
void *local_data)
2640 return SSLDecode(f, 1 , alstate, pstate, stream_slice);
2647 static void *SSLStateAlloc(
void *orig_state,
AppProto proto_orig)
2652 memset(ssl_state, 0,
sizeof(
SSLState));
2660 return (
void *)ssl_state;
2667 static void SSLStateFree(
void *p)
2713 if (ssl_state->
tx_data.de_state != NULL) {
2739 static void SSLStateTransactionFree(
void *state, uint64_t tx_id)
2744 static AppProto SSLProbingParser(
Flow *f, uint8_t direction,
2745 const uint8_t *input, uint32_t ilen, uint8_t *rdir)
2753 if ((input[0] & 0x80) && (input[2] == 0x01)) {
2760 static int SSLStateGetFrameIdByName(
const char *frame_name)
2769 static const char *SSLStateGetFrameNameById(
const uint8_t frame_id)
2775 static int SSLStateGetEventInfo(
const char *event_name,
2776 int *event_id, AppLayerEventType *event_type)
2779 if (*event_id == -1) {
2781 "ssl's enum map table.",
2787 *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
2792 static int SSLStateGetEventInfoById(
int event_id,
const char **event_name,
2793 AppLayerEventType *event_type)
2796 if (*event_name == NULL) {
2798 "ssl's enum map table.",
2804 *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
2809 static int SSLRegisterPatternsForProtocolDetection(
void)
2812 STREAM_TOSERVER, SSLProbingParser, 0, 3) < 0) {
2818 "|01 03 00|", 3, 0, STREAM_TOSERVER) < 0)
2823 "|16 03 00|", 3, 0, STREAM_TOSERVER) < 0)
2830 "|01 03 01|", 3, 0, STREAM_TOSERVER) < 0)
2835 "|16 03 01|", 3, 0, STREAM_TOSERVER) < 0)
2842 "|01 03 02|", 3, 0, STREAM_TOSERVER) < 0)
2847 "|16 03 02|", 3, 0, STREAM_TOSERVER) < 0)
2854 "|01 03 03|", 3, 0, STREAM_TOSERVER) < 0)
2859 "|16 03 03|", 3, 0, STREAM_TOSERVER) < 0)
2867 "|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
2872 "|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
2877 "|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
2884 "|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
2889 "|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
2894 "|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
2901 "|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
2906 "|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
2911 "|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
2918 "|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
2923 "|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
2928 "|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
2939 "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
2944 "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
2958 const char *proto_name =
"tls";
2966 if (SSLRegisterPatternsForProtocolDetection() < 0)
2975 SSLProbingParser, NULL);
2980 SSLProbingParser, NULL) == 0) {
2982 "enabling TLS detection on port 443.");
2988 SSLProbingParser, NULL);
2992 SCLogConfig(
"Protocol detection and parser disabled for %s protocol",
2999 SSLParseClientRecord);
3002 SSLParseServerRecord);
3005 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetFrameIdByName, SSLStateGetFrameNameById);
3027 if (enc_handle != NULL && enc_handle->
val != NULL) {
3028 SCLogDebug(
"have app-layer.protocols.tls.encryption-handling = %s", enc_handle->
val);
3029 if (strcmp(enc_handle->
val,
"full") == 0) {
3031 }
else if (strcmp(enc_handle->
val,
"bypass") == 0) {
3033 }
else if (strcmp(enc_handle->
val,
"default") == 0) {
3040 if (
ConfGetNode(
"app-layer.protocols.tls.no-reassemble") == NULL) {
3042 if (
ConfGetBool(
"tls.no-reassemble", &value) == 1 && value == 1)
3046 if (
ConfGetBool(
"app-layer.protocols.tls.no-reassemble", &value) == 1 && value == 1)
3054 const char *strval = NULL;
3055 if (
ConfGet(
"app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
3057 }
else if (strcmp(strval,
"auto") == 0) {
3069 SCLogWarning(
"MD5 calculation has been disabled, disabling JA3");
3078 SCLogConfig(
"Parsed disabled for %s protocol. Protocol detection"
3079 "still on.", proto_name);