61 "client_handshake_done",
89 "server_handshake_done",
154 {
"CERTIFICATE_INVALID_ALGORITHMIDENTIFIER",
189 #define SSL_CONFIG_DEFAULT_JA3 0
191 #define SSL_CONFIG_DEFAULT_JA4 0
213 #define SSLV3_CHANGE_CIPHER_SPEC 20
214 #define SSLV3_ALERT_PROTOCOL 21
215 #define SSLV3_HANDSHAKE_PROTOCOL 22
216 #define SSLV3_APPLICATION_PROTOCOL 23
217 #define SSLV3_HEARTBEAT_PROTOCOL 24
220 #define SSLV3_HS_HELLO_REQUEST 0
221 #define SSLV3_HS_CLIENT_HELLO 1
222 #define SSLV3_HS_SERVER_HELLO 2
223 #define SSLV3_HS_NEW_SESSION_TICKET 4
224 #define SSLV3_HS_CERTIFICATE 11
225 #define SSLV3_HS_SERVER_KEY_EXCHANGE 12
226 #define SSLV3_HS_CERTIFICATE_REQUEST 13
227 #define SSLV3_HS_SERVER_HELLO_DONE 14
228 #define SSLV3_HS_CERTIFICATE_VERIFY 15
229 #define SSLV3_HS_CLIENT_KEY_EXCHANGE 16
230 #define SSLV3_HS_FINISHED 20
231 #define SSLV3_HS_CERTIFICATE_URL 21
232 #define SSLV3_HS_CERTIFICATE_STATUS 22
235 #define SSLV2_MT_ERROR 0
236 #define SSLV2_MT_CLIENT_HELLO 1
237 #define SSLV2_MT_CLIENT_MASTER_KEY 2
238 #define SSLV2_MT_CLIENT_FINISHED 3
239 #define SSLV2_MT_SERVER_HELLO 4
240 #define SSLV2_MT_SERVER_VERIFY 5
241 #define SSLV2_MT_SERVER_FINISHED 6
242 #define SSLV2_MT_REQUEST_CERTIFICATE 7
243 #define SSLV2_MT_CLIENT_CERTIFICATE 8
245 #define SSLV3_RECORD_HDR_LEN 5
247 #define SSLV3_RECORD_MAX_LEN ((1 << 14) + 1024)
249 #define SSLV3_CLIENT_HELLO_VERSION_LEN 2
250 #define SSLV3_CLIENT_HELLO_RANDOM_LEN 32
253 #define TLS_HB_REQUEST 1
254 #define TLS_HB_RESPONSE 2
256 #define SSL_RECORD_MINIMUM_LENGTH 6
258 #define SHA1_STRING_LENGTH 60
260 #define HAS_SPACE(n) ((uint64_t)(input - initial_input) + (uint64_t)(n) <= (uint64_t)(input_len))
266 #define SSL_DECODER_ERROR(e) \
267 (struct SSLDecoderResult) \
271 #define SSL_DECODER_OK(c) \
272 (struct SSLDecoderResult) \
276 #define SSL_DECODER_INCOMPLETE(c, n) \
277 (struct SSLDecoderResult) \
282 static inline int SafeMemcpy(
void *
dst,
size_t dst_offset,
size_t dst_size,
283 const void *
src,
size_t src_offset,
size_t src_size,
size_t src_tocopy)
WARN_UNUSED;
285 static inline int SafeMemcpy(
void *
dst,
size_t dst_offset,
size_t dst_size,
286 const void *
src,
size_t src_offset,
size_t src_size,
size_t src_tocopy)
293 if (dst_offset < dst_size && src_offset < src_size &&
294 src_tocopy <= (src_size - src_offset) &&
295 src_tocopy <= (dst_size - dst_offset)) {
296 memcpy(
dst + dst_offset,
src + src_offset, src_tocopy);
302 #ifdef DEBUG_VALIDATION
303 #define ValidateRecordState(connp) \
305 DEBUG_VALIDATE_BUG_ON(((connp)->record_length + SSLV3_RECORD_HDR_LEN) < \
306 (connp)->bytes_processed); \
309 #define ValidateRecordState(...)
312 #define SSLParserHSReset(connp) \
314 (connp)->handshake_type = 0; \
315 (connp)->message_length = 0; \
318 #define SSLParserReset(state) \
320 SCLogDebug("resetting state"); \
321 (state)->curr_connp->bytes_processed = 0; \
322 SSLParserHSReset((state)->curr_connp); \
325 #define SSLSetEvent(ssl_state, event) \
327 SCLogDebug("setting event %u", (event)); \
328 if ((ssl_state) == NULL) { \
329 SCLogDebug("could not set decoder event %u", event); \
331 SCAppLayerDecoderEventsSetEventRaw(&(ssl_state)->tx_data.events, (event)); \
332 (ssl_state)->events++; \
336 static void *SSLGetTx(
void *state, uint64_t tx_id)
342 static uint64_t SSLGetTxCnt(
void *state)
355 SCLogDebug(
"toserver: state updated to %u from %u", s, old);
366 SCLogDebug(
"toclient: state updated to %u from %u", s, old);
370 static int SSLGetAlstateProgress(
void *tx, uint8_t direction)
373 if (direction & STREAM_TOCLIENT) {
392 static void TlsDecodeHSCertificateErrSetEvent(
SSLState *ssl_state, uint32_t err)
435 static inline int TlsDecodeHSCertificateFingerprint(
436 SSLStateConnp *connp,
const uint8_t *input, uint32_t cert_len)
446 if (SCSha1HashBuffer(input, cert_len, hash,
sizeof(hash)) == 1) {
453 static inline int TlsDecodeHSCertificateAddCertToChain(
454 SSLStateConnp *connp,
const uint8_t *input, uint32_t cert_len)
468 const uint8_t *
const initial_input,
const uint32_t input_len,
const int certn)
470 const uint8_t *input = (uint8_t *)initial_input;
471 uint32_t err_code = 0;
478 uint32_t cert_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
487 x509 = SCX509Decode(input, cert_len, &err_code);
489 TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
511 SCX509GetSubjectAltNameAt(
530 rc = TlsDecodeHSCertificateFingerprint(connp, input, cert_len);
532 SCLogDebug(
"TlsDecodeHSCertificateFingerprint failed with %d", rc);
537 rc = TlsDecodeHSCertificateAddCertToChain(connp, input, cert_len);
539 SCLogDebug(
"TlsDecodeHSCertificateAddCertToChain failed with %d", rc);
545 return (
int)(input - initial_input);
549 TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
567 const uint8_t *
const initial_input,
const uint32_t input_len)
569 const uint8_t *input = (uint8_t *)initial_input;
574 const uint32_t cert_chain_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
593 uint32_t processed_len = 0;
595 while (processed_len < cert_chain_len) {
596 int rc = TlsDecodeHSCertificate(ssl_state, connp, connp->
certs_buffer + processed_len,
602 if (processed_len + (uint32_t)rc > cert_chain_len) {
606 processed_len += (uint32_t)rc;
609 return processed_len + 3;
623 static inline int TLSDecodeValueIsGREASE(
const uint16_t value)
649 static inline int TLSDecodeHSHelloVersion(
SSLState *ssl_state,
650 const uint8_t *
const initial_input,
651 const uint32_t input_len)
653 uint8_t *input = (uint8_t *)initial_input;
662 uint16_t
version = (uint16_t)(*input << 8) | *(input + 1);
704 return (
int)(input - initial_input);
707 static inline int TLSDecodeHSHelloRandom(
SSLState *ssl_state,
708 const uint8_t *
const initial_input,
709 const uint32_t input_len)
711 uint8_t *input = (uint8_t *)initial_input;
731 return (
int)(input - initial_input);
734 static inline int TLSDecodeHSHelloSessionID(
SSLState *ssl_state,
735 const uint8_t *
const initial_input,
736 const uint32_t input_len)
738 uint8_t *input = (uint8_t *)initial_input;
743 uint8_t session_id_length = *input;
757 input, 0, input_len, session_id_length) != 0) {
774 input += session_id_length;
776 return (
int)(input - initial_input);
785 static inline int TLSDecodeHSHelloCipherSuites(
SSLState *ssl_state,
786 const uint8_t *
const initial_input,
787 const uint32_t input_len)
789 const uint8_t *input = initial_input;
794 uint16_t cipher_suites_length;
797 cipher_suites_length = 2;
799 cipher_suites_length = (uint16_t)(*input << 8) | *(input + 1);
809 if ((cipher_suites_length % 2) != 0) {
813 const bool enable_ja3 =
820 if (ja3_cipher_suites == NULL)
824 uint16_t processed_len = 0;
826 while (processed_len < cipher_suites_length) {
834 uint16_t cipher_suite = (uint16_t)(*input << 8) | *(input + 1);
837 if (TLSDecodeValueIsGREASE(cipher_suite) != 1) {
840 SCTLSHandshakeAddCipher(ssl_state->
curr_connp->
hs, cipher_suite);
859 return (
int)(input - initial_input);
868 static inline int TLSDecodeHSHelloCompressionMethods(
SSLState *ssl_state,
869 const uint8_t *
const initial_input,
870 const uint32_t input_len)
872 const uint8_t *input = initial_input;
881 uint8_t compression_methods_length = *input;
884 if (!(
HAS_SPACE(compression_methods_length)))
887 input += compression_methods_length;
890 return (
int)(input - initial_input);
899 static inline int TLSDecodeHSHelloExtensionSni(
SSLState *ssl_state,
900 const uint8_t *
const initial_input,
901 const uint32_t input_len)
903 uint8_t *input = (uint8_t *)initial_input;
918 uint8_t sni_type = *input;
933 uint16_t sni_len = (uint16_t)(*input << 8) | *(input + 1);
939 if (!(
HAS_SPACE(sni_len)) || sni_len > 255 || sni_len == 0) {
952 return (
int)(input - initial_input);
960 const size_t consumed = input - initial_input;
961 if (SafeMemcpy(ssl_state->
curr_connp->
sni, 0, sni_len, initial_input, consumed, input_len,
969 return (
int)(input - initial_input);
980 static inline int TLSDecodeHSHelloExtensionSupportedVersions(
SSLState *ssl_state,
981 const uint8_t *
const initial_input,
982 const uint32_t input_len)
984 const uint8_t *input = initial_input;
994 uint8_t supported_ver_len = *input;
997 if (supported_ver_len < 2)
1001 goto invalid_length;
1006 while (i + 1 < (uint16_t)supported_ver_len) {
1007 uint16_t ver = (uint16_t)(input[i] << 8) | input[i + 1];
1008 if (TLSVersionValid(ver)) {
1010 SCTLSHandshakeSetTLSVersion(ssl_state->
curr_connp->
hs, ver);
1019 input += supported_ver_len;
1023 goto invalid_length;
1025 uint16_t ver = (uint16_t)(*input << 8) | *(input + 1);
1028 (ver > TLS_VERSION_12)) {
1036 return (
int)(input - initial_input);
1046 static inline int TLSDecodeHSHelloExtensionEllipticCurves(
SSLState *ssl_state,
1047 const uint8_t *
const initial_input,
1048 const uint32_t input_len,
1051 const uint8_t *input = initial_input;
1058 goto invalid_length;
1060 uint16_t elliptic_curves_len = (uint16_t)(*input << 8) | *(input + 1);
1064 goto invalid_length;
1067 uint16_t ec_processed_len = 0;
1069 while (ec_processed_len < elliptic_curves_len)
1072 goto invalid_length;
1074 uint16_t elliptic_curve = (uint16_t)(*input << 8) | *(input + 1);
1077 if (TLSDecodeValueIsGREASE(elliptic_curve) != 1) {
1084 ec_processed_len += 2;
1089 input += elliptic_curves_len;
1092 return (
int)(input - initial_input);
1102 static inline int TLSDecodeHSHelloExtensionEllipticCurvePF(
SSLState *ssl_state,
1103 const uint8_t *
const initial_input,
1104 const uint32_t input_len,
1107 const uint8_t *input = initial_input;
1114 goto invalid_length;
1116 uint8_t ec_pf_len = *input;
1120 goto invalid_length;
1123 uint8_t ec_pf_processed_len = 0;
1125 while (ec_pf_processed_len < ec_pf_len)
1127 uint8_t elliptic_curve_pf = *input;
1130 if (TLSDecodeValueIsGREASE(elliptic_curve_pf) != 1) {
1137 ec_pf_processed_len += 1;
1145 return (
int)(input - initial_input);
1155 static inline int TLSDecodeHSHelloExtensionSigAlgorithms(
1156 SSLState *ssl_state,
const uint8_t *
const initial_input,
const uint32_t input_len)
1158 const uint8_t *input = initial_input;
1165 goto invalid_length;
1167 uint16_t sigalgo_len = (uint16_t)(*input << 8) | *(input + 1);
1171 if ((sigalgo_len % 2) != 0) {
1172 goto invalid_length;
1176 goto invalid_length;
1179 uint16_t sigalgo_processed_len = 0;
1180 while (sigalgo_processed_len < sigalgo_len) {
1181 uint16_t sigalgo = (uint16_t)(*input << 8) | *(input + 1);
1183 sigalgo_processed_len += 2;
1185 SCTLSHandshakeAddSigAlgo(ssl_state->
curr_connp->
hs, sigalgo);
1189 input += sigalgo_len;
1192 return (
int)(input - initial_input);
1195 SCLogDebug(
"Signature algorithm list invalid length");
1201 static inline int TLSDecodeHSHelloExtensionALPN(
1202 SSLState *ssl_state,
const uint8_t *
const initial_input,
const uint32_t input_len)
1204 const uint8_t *input = initial_input;
1211 goto invalid_length;
1213 uint16_t alpn_len = (uint16_t)(*input << 8) | *(input + 1);
1217 goto invalid_length;
1221 uint32_t alpn_processed_len = 0;
1222 while (alpn_processed_len < alpn_len) {
1223 uint8_t protolen = *input;
1225 alpn_processed_len += 1;
1228 goto invalid_length;
1232 if (alpn_processed_len + protolen > ((uint32_t)alpn_len)) {
1233 input += alpn_len - alpn_processed_len;
1236 SCTLSHandshakeAddALPN(ssl_state->
curr_connp->
hs, (
const char *)input, protolen);
1238 alpn_processed_len += protolen;
1242 return (
int)(input - initial_input);
1251 static inline int TLSDecodeHSHelloExtensions(
SSLState *ssl_state,
1252 const uint8_t *
const initial_input,
1253 const uint32_t input_len)
1255 const uint8_t *input = initial_input;
1265 JA3Buffer *ja3_elliptic_curves_pf = NULL;
1269 if (ja3_extensions == NULL)
1274 if (ja3_elliptic_curves == NULL)
1278 if (ja3_elliptic_curves_pf == NULL)
1287 uint16_t extensions_len = (uint16_t)(*input << 8) | *(input + 1);
1291 goto invalid_length;
1293 uint16_t processed_len = 0;
1295 while (processed_len < extensions_len)
1298 goto invalid_length;
1300 uint16_t ext_type = (uint16_t)(*input << 8) | *(input + 1);
1304 goto invalid_length;
1306 uint16_t ext_len = (uint16_t)(*input << 8) | *(input + 1);
1310 goto invalid_length;
1316 ret = TLSDecodeHSHelloExtensionSni(ssl_state, input,
1329 ret = TLSDecodeHSHelloExtensionEllipticCurves(ssl_state, input,
1331 ja3_elliptic_curves);
1343 ret = TLSDecodeHSHelloExtensionEllipticCurvePF(ssl_state, input,
1345 ja3_elliptic_curves_pf);
1356 ret = TLSDecodeHSHelloExtensionSigAlgorithms(ssl_state, input, ext_len);
1367 ret = TLSDecodeHSHelloExtensionALPN(ssl_state, input, ext_len);
1391 ret = TLSDecodeHSHelloExtensionSupportedVersions(ssl_state, input,
1422 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1431 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1432 SCTLSHandshakeAddExtension(ssl_state->
curr_connp->
hs, ext_type);
1436 processed_len += ext_len + 4;
1448 &ja3_elliptic_curves);
1453 &ja3_elliptic_curves_pf);
1459 return (
int)(input - initial_input);
1467 if (ja3_extensions != NULL)
1469 if (ja3_elliptic_curves != NULL)
1471 if (ja3_elliptic_curves_pf != NULL)
1477 static int TLSDecodeHandshakeHello(
SSLState *ssl_state,
1478 const uint8_t *
const input,
1479 const uint32_t input_len)
1482 uint32_t parsed = 0;
1484 ret = TLSDecodeHSHelloVersion(ssl_state, input, input_len);
1490 ret = TLSDecodeHSHelloRandom(ssl_state, input + parsed, input_len - parsed);
1501 ret = TLSDecodeHSHelloSessionID(ssl_state, input + parsed,
1502 input_len - parsed);
1509 ret = TLSDecodeHSHelloCipherSuites(ssl_state, input + parsed,
1510 input_len - parsed);
1521 ret = TLSDecodeHSHelloCompressionMethods(ssl_state, input + parsed,
1522 input_len - parsed);
1529 ret = TLSDecodeHSHelloExtensions(ssl_state, input + parsed,
1530 input_len - parsed);
1547 #ifdef DEBUG_VALIDATION
1557 const uint8_t *
const initial_input,
const uint32_t input_len)
1559 int rc = TlsDecodeHSCertificates(ssl_state, connp, initial_input, input_len);
1564 }
else if (rc < 0) {
1565 SCLogDebug(
"error parsing cert, reset state");
1577 static int SupportedHandshakeType(
const uint8_t
type)
1607 static int SSLv3ParseHandshakeType(
SSLState *ssl_state,
const uint8_t *input,
1608 uint32_t input_len, uint8_t direction)
1610 const uint8_t *initial_input = input;
1622 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1634 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1648 rc = SSLv3ParseHandshakeTypeCertificate(ssl_state,
1688 static int SSLv3ParseHandshakeProtocol(
SSLState *ssl_state,
const uint8_t *input,
1689 uint32_t input_len, uint8_t direction)
1691 const uint8_t *initial_input = input;
1702 SCLogDebug(
"partial handshake record in place");
1705 const uint32_t add =
MIN(need, input_len);
1711 const uint32_t new_size = avail + (4096 - (avail % 4096));
1712 SCLogDebug(
"new_size %u, avail %u", new_size, avail);
1720 SCLogDebug(
"ssl_state->curr_connp->hs_buffer_offset %u "
1721 "ssl_state->curr_connp->hs_buffer_size %u",
1743 SCLogDebug(
"got all data now: handshake_type %u message_length %u",
1774 SCLogDebug(
"input %p input_len %u", input, input_len);
1776 if (input_len < 4) {
1783 SCLogDebug(
"handshake_type %u message len %u input %p input_len %u",
1793 SCLogDebug(
"supported_type %s handshake_type %u/%02x", supported_type ?
"true" :
"false",
1795 if (!supported_type) {
1796 uint32_t avail_record_len =
MIN(input_len, record_len);
1797 input += avail_record_len;
1798 input_len -= avail_record_len;
1812 if (record_len > input_len) {
1813 const uint32_t avail = input_len;
1814 const uint32_t size = avail + (4096 - (avail % 4096));
1815 SCLogDebug(
"initial buffer size %u, based on input %u", size, avail);
1824 if (input_len > 0) {
1832 SCLogDebug(
"opened record buffer %p size %u offset %u type %u msg_size %u",
1839 return (
int)(input - initial_input);
1843 int retval = SSLv3ParseHandshakeType(
1845 if (retval < 0 || retval > (
int)input_len) {
1849 SCLogDebug(
"retval %d input_len %u", retval, input_len);
1851 input_len -= retval;
1857 return (
int)(input - initial_input);
1871 static int SSLv3ParseAlertProtocol(
1872 SSLState *ssl_state,
const uint8_t *input, uint32_t input_len, uint8_t direction)
1874 if (input_len < 2) {
1880 if (input_len == 2) {
1881 uint8_t level = input[0];
1904 static int SSLv3ParseHeartbeatProtocol(
SSLState *ssl_state,
const uint8_t *input,
1905 uint32_t input_len, uint8_t direction)
1909 uint32_t padding_len;
1912 if (input_len < 3) {
1929 SCLogDebug(
"HeartBeat Record type sent in the toclient direction!");
1932 SCLogDebug(
"HeartBeat Record type sent in the toserver direction!");
1940 SCLogDebug(
"Encrypted HeartBeat Request In-flight. Storing len %u",
1945 payload_len = (uint16_t)(*input << 8) | *(input + 1);
1950 SCLogDebug(
"We have a short record in HeartBeat Request");
1958 if (padding_len < 16) {
1959 SCLogDebug(
"We have a short record in HeartBeat Request");
1973 SCLogDebug(
"Multiple in-flight server initiated HeartBeats");
1979 SCLogDebug(
"Multiple in-flight client initiated HeartBeats");
1997 SCLogDebug(
"My heart is bleeding.. OpenSSL HeartBleed response (%u)",
2016 static int SSLv3ParseRecord(uint8_t direction,
SSLState *ssl_state,
2017 const uint8_t *input, uint32_t input_len)
2019 const uint8_t *initial_input = input;
2021 if (input_len == 0) {
2025 uint8_t skip_version = 0;
2029 if (direction == 0) {
2043 if (input_len >= 5) {
2045 if (!skip_version) {
2054 if (--input_len == 0)
2060 if (!skip_version) {
2065 if (--input_len == 0)
2070 if (!skip_version) {
2075 if (--input_len == 0)
2081 if (--input_len == 0)
2087 if (--input_len == 0)
2095 return (
int)(input - initial_input);
2098 static int SSLv2ParseRecord(uint8_t direction,
SSLState *ssl_state,
2099 const uint8_t *input, uint32_t input_len)
2101 const uint8_t *initial_input = input;
2103 if (input_len == 0) {
2118 if (--input_len == 0)
2125 if (--input_len == 0)
2132 if (--input_len == 0)
2149 if (--input_len == 0)
2156 if (--input_len == 0)
2163 if (--input_len == 0)
2170 if (--input_len == 0)
2179 return (
int)(input - initial_input);
2186 const uint8_t *initial_input = input;
2188 if (ssl_state->curr_connp->bytes_processed == 0) {
2189 if (input[0] & 0x80) {
2190 ssl_state->curr_connp->record_lengths_length = 2;
2192 ssl_state->curr_connp->record_lengths_length = 3;
2200 SCLogDebug(
"direction %u ssl_state->curr_connp->record_lengths_length + 1 %u, "
2201 "ssl_state->curr_connp->bytes_processed %u",
2202 direction, ssl_state->curr_connp->record_lengths_length + 1,
2203 ssl_state->curr_connp->bytes_processed);
2206 if (ssl_state->curr_connp->bytes_processed <
2207 (ssl_state->curr_connp->record_lengths_length + 1)) {
2208 const int retval = SSLv2ParseRecord(direction, ssl_state, input, input_len);
2210 ssl_state->curr_connp->record_length);
2211 if (retval < 0 || retval > (
int)input_len) {
2218 ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length,
2227 if (ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length >
2228 input_len + ssl_state->curr_connp->bytes_processed) {
2229 uint32_t
needed = ssl_state->curr_connp->record_length;
2230 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2231 ssl_state->curr_connp->record_length, input_len, (uint32_t)(input - initial_input),
2236 if (input_len == 0) {
2241 if (ssl_state->curr_connp->record_length == 0) {
2248 if (ssl_state->curr_connp->record_lengths_length == 0) {
2249 SCLogDebug(
"SSLv2 record lengths length is zero");
2254 switch (ssl_state->curr_connp->content_type) {
2256 SCLogDebug(
"SSLV2_MT_ERROR msg_type received. Error encountered "
2257 "in establishing the sslv2 session, may be version");
2263 if (input_len < 6) {
2272 const uint16_t
version = (uint16_t)(input[0] << 8) | input[1];
2274 ssl_state->curr_connp->version =
version;
2275 uint16_t session_id_length = (input[5]) | (uint16_t)(input[4] << 8);
2278 ssl_state->curr_connp->bytes_processed += 6;
2279 if (session_id_length == 0) {
2286 SCLogDebug(
"Client hello is not seen before master key "
2294 if (direction == 1) {
2295 SCLogDebug(
"Incorrect SSL Record type sent in the toclient "
2305 if (direction == 0 &&
2306 !(ssl_state->curr_connp->content_type &
2308 SCLogDebug(
"Incorrect SSL Record type sent in the toserver "
2319 if (direction == 0) {
2322 SCLogDebug(
"SSLv2 client side has started the encryption");
2325 SCLogDebug(
"SSLv2 client side has started the encryption");
2329 SCLogDebug(
"SSLv2 Server side has started the encryption");
2343 SCLogDebug(
"SSLv2 No reassembly & inspection has been set");
2357 ssl_state->flags |= ssl_state->current_flags;
2359 if (input_len + ssl_state->curr_connp->bytes_processed >=
2360 (ssl_state->curr_connp->record_length +
2361 ssl_state->curr_connp->record_lengths_length)) {
2364 uint32_t diff = ssl_state->curr_connp->record_length +
2365 ssl_state->curr_connp->record_lengths_length + -
2366 ssl_state->curr_connp->bytes_processed;
2374 ssl_state->curr_connp->bytes_processed += input_len;
2383 uint32_t parsed = 0;
2384 uint32_t record_len;
2385 const bool first_call = (ssl_state->curr_connp->bytes_processed == 0);
2388 const uint16_t prev_version = ssl_state->curr_connp->version;
2390 int retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
2391 if (retval < 0 || retval > (
int)input_len) {
2399 SCLogDebug(
"%s input %p record_length %u", (direction == 0) ?
"toserver" :
"toclient",
2400 input, ssl_state->curr_connp->record_length);
2413 "incomplete header, return %u bytes consumed and wait for more data", parsed);
2420 record_len =
MIN(input_len - parsed, ssl_state->curr_connp->record_length);
2422 "record_len %u (input_len %u, parsed %u, ssl_state->curr_connp->record_length %u)",
2423 record_len, input_len, parsed, ssl_state->curr_connp->record_length);
2425 bool unknown_record =
false;
2426 switch (ssl_state->curr_connp->content_type) {
2434 unknown_record =
true;
2440 if (prev_version == TLS_VERSION_10 || prev_version == TLS_VERSION_11) {
2441 if (unknown_record) {
2445 ssl_state->curr_connp->bytes_processed = 0;
2446 ssl_state->curr_connp->content_type = 0;
2447 ssl_state->curr_connp->record_length = 0;
2449 ssl_state->curr_connp->version = prev_version;
2453 if (unknown_record) {
2461 if (ssl_state->curr_connp->record_length == 0) {
2467 if (!TLSVersionValid(ssl_state->curr_connp->version)) {
2468 SCLogDebug(
"ssl_state->curr_connp->version %04x", ssl_state->curr_connp->version);
2482 record_len = (ssl_state->curr_connp->record_length +
SSLV3_RECORD_HDR_LEN)- ssl_state->curr_connp->bytes_processed;
2483 record_len =
MIN(input_len, record_len);
2485 SCLogDebug(
"record length %u processed %u got %u",
2486 ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed, record_len);
2489 if (ssl_state->curr_connp->record_length > input_len - parsed) {
2495 uint32_t
needed = ssl_state->curr_connp->record_length;
2496 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2497 ssl_state->curr_connp->record_length, input_len, parsed,
needed);
2503 if (record_len == 0) {
2508 ssl_state->curr_connp->record_length, direction,
TLS_FRAME_DATA);
2510 switch (ssl_state->curr_connp->content_type) {
2529 int retval = SSLv3ParseAlertProtocol(ssl_state, input + parsed, record_len, direction);
2547 if (ssl_state->curr_connp == &ssl_state->client_connp) {
2554 SCLogDebug(
"setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
2561 SCLogDebug(
"setting APP_LAYER_PARSER_NO_REASSEMBLY");
2574 (ssl_state->client_connp.version > TLS_VERSION_12) &&
2583 if (ssl_state->curr_connp->record_length < 4) {
2586 SCLogDebug(
"record len < 4 => %u", ssl_state->curr_connp->record_length);
2590 int retval = SSLv3ParseHandshakeProtocol(ssl_state, input + parsed,
2591 record_len, direction);
2593 if (retval < 0 || retval > (
int)record_len) {
2605 int retval = SSLv3ParseHeartbeatProtocol(ssl_state, input + parsed,
2606 record_len, direction);
2620 parsed += record_len;
2621 ssl_state->curr_connp->bytes_processed += record_len;
2623 if (ssl_state->curr_connp->bytes_processed >=
2627 ssl_state->f, direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
2662 ssl_state->
tx_data.updated_tc =
true;
2663 ssl_state->
tx_data.updated_ts =
true;
2664 uint32_t counter = 0;
2666 const uint8_t *input = StreamSliceGetData(&stream_slice);
2667 const uint8_t *init_input = input;
2668 int32_t input_len = (int32_t)StreamSliceGetDataLen(&stream_slice);
2670 if ((input == NULL || input_len == 0) &&
2680 }
else if (input == NULL || input_len == 0) {
2696 while (input_len > 0) {
2697 if (counter > max_records) {
2698 SCLogDebug(
"Looks like we have looped quite a bit. Reset state "
2699 "and get out of here");
2710 if ((input[0] & 0x80) || (input[0] & 0x40)) {
2719 SCLogDebug(
"record %u: bytes_processed %u, version %02X, input_len %u", counter,
2726 SCLogDebug(
"Continuing parsing SSLv2 record");
2729 SSLv2Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2732 SCLogDebug(
"Error parsing SSLv2. Resetting parser "
2733 "state. Let's get outta here");
2740 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2741 (uintmax_t)(input - init_input), r.
needed);
2746 SCLogDebug(
"SSLv2 decoder consumed %d bytes: %u left", r.
retval, input_len);
2749 SCLogDebug(
"New TLS record: record_length %u",
2752 SCLogDebug(
"Continuing parsing TLS record: record_length %u, bytes_processed %u",
2756 SSLv3Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2759 SCLogDebug(
"Error parsing TLS. Resetting parser "
2760 "state. Let's get outta here");
2765 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2766 (uintmax_t)(input - init_input), r.
needed);
2810 return SSLDecode(f, 0 , alstate, pstate, stream_slice);
2816 return SSLDecode(f, 1 , alstate, pstate, stream_slice);
2823 static void *SSLStateAlloc(
void *orig_state,
AppProto proto_orig)
2831 return (
void *)ssl_state;
2848 static void SSLStateFree(
void *p)
2905 SCAppLayerTxDataCleanup(&ssl_state->
tx_data);
2927 static void SSLStateTransactionFree(
void *state, uint64_t tx_id)
2933 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t ilen, uint8_t *rdir)
2941 if ((input[0] & 0x80) && (input[2] == 0x01)) {
2948 static int SSLStateGetStateIdByName(
const char *
name,
const uint8_t direction)
2951 direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
2960 static const char *SSLStateGetStateNameById(
const int id,
const uint8_t direction)
2963 direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
2968 static int SSLStateGetFrameIdByName(
const char *frame_name)
2977 static const char *SSLStateGetFrameNameById(
const uint8_t frame_id)
2983 static int SSLStateGetEventInfo(
2993 static int SSLStateGetEventInfoById(
2997 if (*event_name == NULL) {
2999 "ssl's enum map table.",
3010 static int SSLRegisterPatternsForProtocolDetection(
void)
3013 STREAM_TOSERVER, SSLProbingParser, 0, 3) < 0) {
3019 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
3023 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
3029 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
3033 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
3039 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
3043 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
3049 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
3053 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
3060 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3064 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3068 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3074 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3078 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3082 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3088 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3092 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3096 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3102 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3106 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3110 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3120 "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
3125 "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
3135 static void CheckJA3Enabled(
void)
3137 const char *strval = NULL;
3140 if (
SCConfGet(
"app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
3142 }
else if (strcmp(strval,
"auto") == 0) {
3160 static void CheckJA4Enabled(
void)
3162 const char *strval = NULL;
3164 int enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3165 if (
SCConfGet(
"app-layer.protocols.tls.ja4-fingerprints", &strval) != 1) {
3166 enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3167 }
else if (strcmp(strval,
"auto") == 0) {
3168 enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3189 const char *proto_name =
"tls";
3197 if (SSLRegisterPatternsForProtocolDetection() < 0)
3202 IPPROTO_TCP,
"443",
ALPROTO_TLS, 0, 3, STREAM_TOSERVER, SSLProbingParser, NULL);
3205 0, 3, SSLProbingParser, NULL) == 0) {
3207 "enabling TLS detection on port 443.");
3209 STREAM_TOSERVER, SSLProbingParser, NULL);
3213 SCLogConfig(
"Protocol detection and parser disabled for %s protocol",
3220 SSLParseClientRecord);
3223 SSLParseServerRecord);
3225 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetStateIdByName, SSLStateGetStateNameById);
3227 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetFrameIdByName, SSLStateGetFrameNameById);
3250 if (enc_handle != NULL && enc_handle->
val != NULL) {
3251 SCLogDebug(
"have app-layer.protocols.tls.encryption-handling = %s", enc_handle->
val);
3252 if (strcmp(enc_handle->
val,
"full") == 0) {
3254 }
else if (strcmp(enc_handle->
val,
"bypass") == 0) {
3256 }
else if (strcmp(enc_handle->
val,
"track-only") == 0) {
3258 }
else if (strcmp(enc_handle->
val,
"default") == 0) {
3259 SCLogWarning(
"app-layer.protocols.tls.encryption-handling = default is deprecated "
3260 "and will be removed in Suricata 9, use \"track-only\" instead, "
3261 "(see ticket #7642)");
3268 if (
SCConfGetNode(
"app-layer.protocols.tls.no-reassemble") == NULL) {
3270 if (
SCConfGetBool(
"tls.no-reassemble", &value) == 1 && value == 1)
3274 if (
SCConfGetBool(
"app-layer.protocols.tls.no-reassemble", &value) == 1 &&
3290 SCLogWarning(
"MD5 calculation has been disabled, disabling JA3");
3294 SCLogWarning(
"Hashing has been disabled, disabling JA4");
3308 SCLogConfig(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);