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,
const uint32_t input_len,
1050 const uint8_t *input = initial_input;
1057 goto invalid_length;
1059 uint16_t elliptic_curves_len = (uint16_t)(*input << 8) | *(input + 1);
1063 goto invalid_length;
1066 *ja3_elliptic_curves != NULL) {
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) {
1083 ec_processed_len += 2;
1088 input += elliptic_curves_len;
1091 return (
int)(input - initial_input);
1101 static inline int TLSDecodeHSHelloExtensionEllipticCurvePF(
SSLState *ssl_state,
1102 const uint8_t *
const initial_input,
const uint32_t input_len,
1105 const uint8_t *input = initial_input;
1112 goto invalid_length;
1114 uint8_t ec_pf_len = *input;
1118 goto invalid_length;
1121 *ja3_elliptic_curves_pf != NULL) {
1122 uint8_t ec_pf_processed_len = 0;
1124 while (ec_pf_processed_len < ec_pf_len)
1126 uint8_t elliptic_curve_pf = *input;
1129 if (TLSDecodeValueIsGREASE(elliptic_curve_pf) != 1) {
1135 ec_pf_processed_len += 1;
1143 return (
int)(input - initial_input);
1153 static inline int TLSDecodeHSHelloExtensionSigAlgorithms(
1154 SSLState *ssl_state,
const uint8_t *
const initial_input,
const uint32_t input_len)
1156 const uint8_t *input = initial_input;
1163 goto invalid_length;
1165 uint16_t sigalgo_len = (uint16_t)(*input << 8) | *(input + 1);
1169 if ((sigalgo_len % 2) != 0) {
1170 goto invalid_length;
1174 goto invalid_length;
1177 uint16_t sigalgo_processed_len = 0;
1178 while (sigalgo_processed_len < sigalgo_len) {
1179 uint16_t sigalgo = (uint16_t)(*input << 8) | *(input + 1);
1181 sigalgo_processed_len += 2;
1183 SCTLSHandshakeAddSigAlgo(ssl_state->
curr_connp->
hs, sigalgo);
1187 input += sigalgo_len;
1190 return (
int)(input - initial_input);
1193 SCLogDebug(
"Signature algorithm list invalid length");
1199 static inline int TLSDecodeHSHelloExtensionALPN(
1200 SSLState *ssl_state,
const uint8_t *
const initial_input,
const uint32_t input_len)
1202 const uint8_t *input = initial_input;
1209 goto invalid_length;
1211 uint16_t alpn_len = (uint16_t)(*input << 8) | *(input + 1);
1215 goto invalid_length;
1219 uint32_t alpn_processed_len = 0;
1220 while (alpn_processed_len < alpn_len) {
1221 uint8_t protolen = *input;
1223 alpn_processed_len += 1;
1226 goto invalid_length;
1230 if (alpn_processed_len + protolen > ((uint32_t)alpn_len)) {
1231 input += alpn_len - alpn_processed_len;
1234 SCTLSHandshakeAddALPN(ssl_state->
curr_connp->
hs, (
const char *)input, protolen);
1236 alpn_processed_len += protolen;
1240 return (
int)(input - initial_input);
1249 static inline int TLSDecodeHSHelloExtensions(
SSLState *ssl_state,
1250 const uint8_t *
const initial_input,
1251 const uint32_t input_len)
1253 const uint8_t *input = initial_input;
1263 JA3Buffer *ja3_elliptic_curves_pf = NULL;
1267 if (ja3_extensions == NULL)
1272 if (ja3_elliptic_curves == NULL)
1276 if (ja3_elliptic_curves_pf == NULL)
1285 uint16_t extensions_len = (uint16_t)(*input << 8) | *(input + 1);
1289 goto invalid_length;
1291 uint32_t processed_len = 0;
1293 while (processed_len < (uint32_t)extensions_len) {
1295 goto invalid_length;
1297 uint16_t ext_type = (uint16_t)(*input << 8) | *(input + 1);
1301 goto invalid_length;
1303 uint16_t ext_len = (uint16_t)(*input << 8) | *(input + 1);
1307 goto invalid_length;
1309 if (processed_len + 4UL + (uint32_t)ext_len > (uint32_t)extensions_len)
1310 goto invalid_length;
1316 ret = TLSDecodeHSHelloExtensionSni(ssl_state, input,
1329 ret = TLSDecodeHSHelloExtensionEllipticCurves(
1330 ssl_state, input, ext_len, &ja3_elliptic_curves);
1342 ret = TLSDecodeHSHelloExtensionEllipticCurvePF(
1343 ssl_state, input, ext_len, &ja3_elliptic_curves_pf);
1354 ret = TLSDecodeHSHelloExtensionSigAlgorithms(ssl_state, input, ext_len);
1365 ret = TLSDecodeHSHelloExtensionALPN(ssl_state, input, ext_len);
1389 ret = TLSDecodeHSHelloExtensionSupportedVersions(ssl_state, input,
1420 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1429 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1430 SCTLSHandshakeAddExtension(ssl_state->
curr_connp->
hs, ext_type);
1434 processed_len += (uint32_t)ext_len + 4UL;
1446 &ja3_elliptic_curves);
1451 &ja3_elliptic_curves_pf);
1457 return (
int)(input - initial_input);
1465 if (ja3_extensions != NULL)
1467 if (ja3_elliptic_curves != NULL)
1469 if (ja3_elliptic_curves_pf != NULL)
1475 static int TLSDecodeHandshakeHello(
SSLState *ssl_state,
1476 const uint8_t *
const input,
1477 const uint32_t input_len)
1480 uint32_t parsed = 0;
1482 ret = TLSDecodeHSHelloVersion(ssl_state, input, input_len);
1488 ret = TLSDecodeHSHelloRandom(ssl_state, input + parsed, input_len - parsed);
1499 ret = TLSDecodeHSHelloSessionID(ssl_state, input + parsed,
1500 input_len - parsed);
1507 ret = TLSDecodeHSHelloCipherSuites(ssl_state, input + parsed,
1508 input_len - parsed);
1519 ret = TLSDecodeHSHelloCompressionMethods(ssl_state, input + parsed,
1520 input_len - parsed);
1527 ret = TLSDecodeHSHelloExtensions(ssl_state, input + parsed,
1528 input_len - parsed);
1545 #ifdef DEBUG_VALIDATION
1555 const uint8_t *
const initial_input,
const uint32_t input_len)
1557 int rc = TlsDecodeHSCertificates(ssl_state, connp, initial_input, input_len);
1562 }
else if (rc < 0) {
1563 SCLogDebug(
"error parsing cert, reset state");
1575 static int SupportedHandshakeType(
const uint8_t
type)
1605 static int SSLv3ParseHandshakeType(
SSLState *ssl_state,
const uint8_t *input,
1606 uint32_t input_len, uint8_t direction)
1608 const uint8_t *initial_input = input;
1620 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1632 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1646 rc = SSLv3ParseHandshakeTypeCertificate(ssl_state,
1686 static int SSLv3ParseHandshakeProtocol(
SSLState *ssl_state,
const uint8_t *input,
1687 uint32_t input_len, uint8_t direction)
1689 const uint8_t *initial_input = input;
1700 SCLogDebug(
"partial handshake record in place");
1703 const uint32_t add =
MIN(need, input_len);
1709 const uint32_t new_size = avail + (4096 - (avail % 4096));
1710 SCLogDebug(
"new_size %u, avail %u", new_size, avail);
1718 SCLogDebug(
"ssl_state->curr_connp->hs_buffer_offset %u "
1719 "ssl_state->curr_connp->hs_buffer_size %u",
1741 SCLogDebug(
"got all data now: handshake_type %u message_length %u",
1772 SCLogDebug(
"input %p input_len %u", input, input_len);
1774 if (input_len < 4) {
1781 SCLogDebug(
"handshake_type %u message len %u input %p input_len %u",
1791 SCLogDebug(
"supported_type %s handshake_type %u/%02x", supported_type ?
"true" :
"false",
1793 if (!supported_type) {
1794 uint32_t avail_record_len =
MIN(input_len, record_len);
1795 input += avail_record_len;
1796 input_len -= avail_record_len;
1810 if (record_len > input_len) {
1811 const uint32_t avail = input_len;
1812 const uint32_t size = avail + (4096 - (avail % 4096));
1813 SCLogDebug(
"initial buffer size %u, based on input %u", size, avail);
1822 if (input_len > 0) {
1830 SCLogDebug(
"opened record buffer %p size %u offset %u type %u msg_size %u",
1837 return (
int)(input - initial_input);
1841 int retval = SSLv3ParseHandshakeType(
1843 if (retval < 0 || retval > (
int)input_len) {
1847 SCLogDebug(
"retval %d input_len %u", retval, input_len);
1849 input_len -= retval;
1855 return (
int)(input - initial_input);
1869 static int SSLv3ParseAlertProtocol(
1870 SSLState *ssl_state,
const uint8_t *input, uint32_t input_len, uint8_t direction)
1872 if (input_len < 2) {
1878 if (input_len == 2) {
1879 uint8_t level = input[0];
1902 static int SSLv3ParseHeartbeatProtocol(
SSLState *ssl_state,
const uint8_t *input,
1903 uint32_t input_len, uint8_t direction)
1907 uint32_t padding_len;
1910 if (input_len < 3) {
1927 SCLogDebug(
"HeartBeat Record type sent in the toclient direction!");
1930 SCLogDebug(
"HeartBeat Record type sent in the toserver direction!");
1938 SCLogDebug(
"Encrypted HeartBeat Request In-flight. Storing len %u",
1943 payload_len = (uint16_t)(*input << 8) | *(input + 1);
1948 SCLogDebug(
"We have a short record in HeartBeat Request");
1956 if (padding_len < 16) {
1957 SCLogDebug(
"We have a short record in HeartBeat Request");
1971 SCLogDebug(
"Multiple in-flight server initiated HeartBeats");
1977 SCLogDebug(
"Multiple in-flight client initiated HeartBeats");
1995 SCLogDebug(
"My heart is bleeding.. OpenSSL HeartBleed response (%u)",
2014 static int SSLv3ParseRecord(uint8_t direction,
SSLState *ssl_state,
2015 const uint8_t *input, uint32_t input_len)
2017 const uint8_t *initial_input = input;
2019 if (input_len == 0) {
2023 uint8_t skip_version = 0;
2027 if (direction == 0) {
2041 if (input_len >= 5) {
2043 if (!skip_version) {
2052 if (--input_len == 0)
2058 if (!skip_version) {
2063 if (--input_len == 0)
2068 if (!skip_version) {
2073 if (--input_len == 0)
2079 if (--input_len == 0)
2085 if (--input_len == 0)
2093 return (
int)(input - initial_input);
2096 static int SSLv2ParseRecord(uint8_t direction,
SSLState *ssl_state,
2097 const uint8_t *input, uint32_t input_len)
2099 const uint8_t *initial_input = input;
2101 if (input_len == 0) {
2116 if (--input_len == 0)
2123 if (--input_len == 0)
2130 if (--input_len == 0)
2147 if (--input_len == 0)
2154 if (--input_len == 0)
2161 if (--input_len == 0)
2168 if (--input_len == 0)
2177 return (
int)(input - initial_input);
2184 const uint8_t *initial_input = input;
2186 if (ssl_state->curr_connp->bytes_processed == 0) {
2187 if (input[0] & 0x80) {
2188 ssl_state->curr_connp->record_lengths_length = 2;
2190 ssl_state->curr_connp->record_lengths_length = 3;
2198 SCLogDebug(
"direction %u ssl_state->curr_connp->record_lengths_length + 1 %u, "
2199 "ssl_state->curr_connp->bytes_processed %u",
2200 direction, ssl_state->curr_connp->record_lengths_length + 1,
2201 ssl_state->curr_connp->bytes_processed);
2204 if (ssl_state->curr_connp->bytes_processed <
2205 (ssl_state->curr_connp->record_lengths_length + 1)) {
2206 const int retval = SSLv2ParseRecord(direction, ssl_state, input, input_len);
2208 ssl_state->curr_connp->record_length);
2209 if (retval < 0 || retval > (
int)input_len) {
2216 ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length,
2225 if (ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length >
2226 input_len + ssl_state->curr_connp->bytes_processed) {
2227 uint32_t
needed = ssl_state->curr_connp->record_length;
2228 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2229 ssl_state->curr_connp->record_length, input_len, (uint32_t)(input - initial_input),
2234 if (input_len == 0) {
2239 if (ssl_state->curr_connp->record_length == 0) {
2246 if (ssl_state->curr_connp->record_lengths_length == 0) {
2247 SCLogDebug(
"SSLv2 record lengths length is zero");
2252 switch (ssl_state->curr_connp->content_type) {
2254 SCLogDebug(
"SSLV2_MT_ERROR msg_type received. Error encountered "
2255 "in establishing the sslv2 session, may be version");
2266 if (input_len < 6 || ssl_state->curr_connp->
record_length < 7) {
2275 const uint16_t
version = (uint16_t)(input[0] << 8) | input[1];
2277 ssl_state->curr_connp->version =
version;
2278 uint16_t session_id_length = (input[5]) | (uint16_t)(input[4] << 8);
2281 ssl_state->curr_connp->bytes_processed += 6;
2282 if (session_id_length == 0) {
2289 SCLogDebug(
"Client hello is not seen before master key "
2297 if (direction == 1) {
2298 SCLogDebug(
"Incorrect SSL Record type sent in the toclient "
2308 if (direction == 0 &&
2309 !(ssl_state->curr_connp->content_type &
2311 SCLogDebug(
"Incorrect SSL Record type sent in the toserver "
2322 if (direction == 0) {
2325 SCLogDebug(
"SSLv2 client side has started the encryption");
2328 SCLogDebug(
"SSLv2 client side has started the encryption");
2332 SCLogDebug(
"SSLv2 Server side has started the encryption");
2346 SCLogDebug(
"SSLv2 No reassembly & inspection has been set");
2360 ssl_state->flags |= ssl_state->current_flags;
2362 if (ssl_state->curr_connp->bytes_processed >
2363 ssl_state->curr_connp->record_length + ssl_state->curr_connp->record_lengths_length) {
2364 SCLogDebug(
"SSLv2 bytes_processed (%u) exceeds record+hdr "
2365 "len (record_length=%u, lengths_length=%u)",
2366 ssl_state->curr_connp->bytes_processed, ssl_state->curr_connp->record_length,
2367 ssl_state->curr_connp->record_lengths_length);
2372 if (input_len + ssl_state->curr_connp->bytes_processed >=
2373 (ssl_state->curr_connp->record_length +
2374 ssl_state->curr_connp->record_lengths_length)) {
2377 uint32_t diff = ssl_state->curr_connp->record_length +
2378 ssl_state->curr_connp->record_lengths_length + -
2379 ssl_state->curr_connp->bytes_processed;
2387 ssl_state->curr_connp->bytes_processed += input_len;
2396 uint32_t parsed = 0;
2397 uint32_t record_len;
2398 const bool first_call = (ssl_state->curr_connp->bytes_processed == 0);
2401 const uint16_t prev_version = ssl_state->curr_connp->version;
2403 int retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
2404 if (retval < 0 || retval > (
int)input_len) {
2412 SCLogDebug(
"%s input %p record_length %u", (direction == 0) ?
"toserver" :
"toclient",
2413 input, ssl_state->curr_connp->record_length);
2426 "incomplete header, return %u bytes consumed and wait for more data", parsed);
2433 record_len =
MIN(input_len - parsed, ssl_state->curr_connp->record_length);
2435 "record_len %u (input_len %u, parsed %u, ssl_state->curr_connp->record_length %u)",
2436 record_len, input_len, parsed, ssl_state->curr_connp->record_length);
2438 bool unknown_record =
false;
2439 switch (ssl_state->curr_connp->content_type) {
2447 unknown_record =
true;
2453 if (prev_version == TLS_VERSION_10 || prev_version == TLS_VERSION_11) {
2454 if (unknown_record) {
2458 ssl_state->curr_connp->bytes_processed = 0;
2459 ssl_state->curr_connp->content_type = 0;
2460 ssl_state->curr_connp->record_length = 0;
2462 ssl_state->curr_connp->version = prev_version;
2466 if (unknown_record) {
2474 if (ssl_state->curr_connp->record_length == 0) {
2480 if (!TLSVersionValid(ssl_state->curr_connp->version)) {
2481 SCLogDebug(
"ssl_state->curr_connp->version %04x", ssl_state->curr_connp->version);
2495 record_len = (ssl_state->curr_connp->record_length +
SSLV3_RECORD_HDR_LEN)- ssl_state->curr_connp->bytes_processed;
2496 record_len =
MIN(input_len, record_len);
2498 SCLogDebug(
"record length %u processed %u got %u",
2499 ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed, record_len);
2502 if (ssl_state->curr_connp->record_length > input_len - parsed) {
2508 uint32_t
needed = ssl_state->curr_connp->record_length;
2509 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2510 ssl_state->curr_connp->record_length, input_len, parsed,
needed);
2516 if (record_len == 0) {
2521 ssl_state->curr_connp->record_length, direction,
TLS_FRAME_DATA);
2523 switch (ssl_state->curr_connp->content_type) {
2542 int retval = SSLv3ParseAlertProtocol(ssl_state, input + parsed, record_len, direction);
2560 if (ssl_state->curr_connp == &ssl_state->client_connp) {
2567 SCLogDebug(
"setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
2574 SCLogDebug(
"setting APP_LAYER_PARSER_NO_REASSEMBLY");
2587 (ssl_state->client_connp.version > TLS_VERSION_12) &&
2596 if (ssl_state->curr_connp->record_length < 4) {
2599 SCLogDebug(
"record len < 4 => %u", ssl_state->curr_connp->record_length);
2603 int retval = SSLv3ParseHandshakeProtocol(ssl_state, input + parsed,
2604 record_len, direction);
2606 if (retval < 0 || retval > (
int)record_len) {
2618 int retval = SSLv3ParseHeartbeatProtocol(ssl_state, input + parsed,
2619 record_len, direction);
2633 parsed += record_len;
2634 ssl_state->curr_connp->bytes_processed += record_len;
2636 if (ssl_state->curr_connp->bytes_processed >=
2640 ssl_state->f, direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
2677 uint32_t counter = 0;
2679 const uint8_t *input = StreamSliceGetData(&stream_slice);
2680 const uint8_t *init_input = input;
2681 int32_t input_len = (int32_t)StreamSliceGetDataLen(&stream_slice);
2683 if ((input == NULL || input_len == 0) &&
2693 }
else if (input == NULL || input_len == 0) {
2709 while (input_len > 0) {
2710 if (counter > max_records) {
2711 SCLogDebug(
"Looks like we have looped quite a bit. Reset state "
2712 "and get out of here");
2723 if ((input[0] & 0x80) || (input[0] & 0x40)) {
2732 SCLogDebug(
"record %u: bytes_processed %u, version %02X, input_len %u", counter,
2739 SCLogDebug(
"Continuing parsing SSLv2 record");
2742 SSLv2Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2745 SCLogDebug(
"Error parsing SSLv2. Resetting parser "
2746 "state. Let's get outta here");
2753 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2754 (uintmax_t)(input - init_input), r.
needed);
2759 SCLogDebug(
"SSLv2 decoder consumed %d bytes: %u left", r.
retval, input_len);
2762 SCLogDebug(
"New TLS record: record_length %u",
2765 SCLogDebug(
"Continuing parsing TLS record: record_length %u, bytes_processed %u",
2769 SSLv3Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2772 SCLogDebug(
"Error parsing TLS. Resetting parser "
2773 "state. Let's get outta here");
2778 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2779 (uintmax_t)(input - init_input), r.
needed);
2823 return SSLDecode(f, 0 , alstate, pstate, stream_slice);
2829 return SSLDecode(f, 1 , alstate, pstate, stream_slice);
2836 static void *SSLStateAlloc(
void *orig_state,
AppProto proto_orig)
2844 return (
void *)ssl_state;
2861 static void SSLStateFree(
void *
p)
2940 static void SSLStateTransactionFree(
void *state, uint64_t tx_id)
2946 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t ilen, uint8_t *rdir)
2954 if ((input[0] & 0x80) && (input[2] == 0x01)) {
2961 static int SSLStateGetStateIdByName(
const char *
name,
const uint8_t direction)
2964 direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
2973 static const char *SSLStateGetStateNameById(
const int id,
const uint8_t direction)
2976 direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
2981 static int SSLStateGetFrameIdByName(
const char *frame_name)
2990 static const char *SSLStateGetFrameNameById(
const uint8_t frame_id)
2996 static int SSLStateGetEventInfo(
3006 static int SSLStateGetEventInfoById(
3010 if (*event_name == NULL) {
3012 "ssl's enum map table.",
3023 static int SSLRegisterPatternsForProtocolDetection(
void)
3026 STREAM_TOSERVER, SSLProbingParser, 0, 3) < 0) {
3032 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
3036 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
3042 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
3046 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
3052 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
3056 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
3062 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
3066 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
3073 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3077 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3081 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3087 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3091 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3095 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3101 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3105 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3109 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3115 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3119 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3123 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3133 "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
3138 "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
3148 static void CheckJA3Enabled(
void)
3150 const char *strval = NULL;
3153 if (
SCConfGetNonNull(
"app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
3155 }
else if (strcmp(strval,
"auto") == 0) {
3173 static void CheckJA4Enabled(
void)
3175 const char *strval = NULL;
3177 int enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3178 if (
SCConfGetNonNull(
"app-layer.protocols.tls.ja4-fingerprints", &strval) != 1) {
3179 enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3180 }
else if (strcmp(strval,
"auto") == 0) {
3181 enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3202 const char *proto_name =
"tls";
3210 if (SSLRegisterPatternsForProtocolDetection() < 0)
3215 IPPROTO_TCP,
"443",
ALPROTO_TLS, 0, 3, STREAM_TOSERVER, SSLProbingParser, NULL);
3218 0, 3, SSLProbingParser, NULL) == 0) {
3220 "enabling TLS detection on port 443.");
3222 STREAM_TOSERVER, SSLProbingParser, NULL);
3226 SCLogConfig(
"Protocol detection and parser disabled for %s protocol",
3233 SSLParseClientRecord);
3236 SSLParseServerRecord);
3238 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetStateIdByName, SSLStateGetStateNameById);
3240 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetFrameIdByName, SSLStateGetFrameNameById);
3263 if (enc_handle != NULL && enc_handle->
val != NULL) {
3264 SCLogDebug(
"have app-layer.protocols.tls.encryption-handling = %s", enc_handle->
val);
3265 if (strcmp(enc_handle->
val,
"full") == 0) {
3267 }
else if (strcmp(enc_handle->
val,
"bypass") == 0) {
3269 }
else if (strcmp(enc_handle->
val,
"track-only") == 0) {
3271 }
else if (strcmp(enc_handle->
val,
"default") == 0) {
3272 SCLogWarning(
"app-layer.protocols.tls.encryption-handling = default is deprecated "
3273 "and will be removed in Suricata 9, use \"track-only\" instead, "
3274 "(see ticket #7642)");
3281 if (
SCConfGetNode(
"app-layer.protocols.tls.no-reassemble") == NULL) {
3283 if (
SCConfGetBool(
"tls.no-reassemble", &value) == 1 && value == 1)
3287 if (
SCConfGetBool(
"app-layer.protocols.tls.no-reassemble", &value) == 1 &&
3303 SCLogWarning(
"MD5 calculation has been disabled, disabling JA3");
3307 SCLogWarning(
"Hashing has been disabled, disabling JA4");
3321 SCLogConfig(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);