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);
493 char *
str = SCX509GetSubject(x509);
500 str = SCX509GetIssuer(x509);
513 sans[i] = SCX509GetSubjectAltNameAt(x509, i);
516 str = SCX509GetSerial(x509);
532 rc = TlsDecodeHSCertificateFingerprint(connp, input, cert_len);
534 SCLogDebug(
"TlsDecodeHSCertificateFingerprint failed with %d", rc);
539 rc = TlsDecodeHSCertificateAddCertToChain(connp, input, cert_len);
541 SCLogDebug(
"TlsDecodeHSCertificateAddCertToChain failed with %d", rc);
547 return (
int)(input - initial_input);
551 TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
569 const uint8_t *
const initial_input,
const uint32_t input_len)
571 const uint8_t *input = (uint8_t *)initial_input;
576 const uint32_t cert_chain_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
595 uint32_t processed_len = 0;
597 while (processed_len < cert_chain_len) {
598 int rc = TlsDecodeHSCertificate(ssl_state, connp, connp->
certs_buffer + processed_len,
604 if (processed_len + (uint32_t)rc > cert_chain_len) {
608 processed_len += (uint32_t)rc;
611 return processed_len + 3;
625 static inline int TLSDecodeValueIsGREASE(
const uint16_t value)
651 static inline int TLSDecodeHSHelloVersion(
SSLState *ssl_state,
652 const uint8_t *
const initial_input,
653 const uint32_t input_len)
655 uint8_t *input = (uint8_t *)initial_input;
664 uint16_t
version = (uint16_t)(*input << 8) | *(input + 1);
706 return (
int)(input - initial_input);
709 static inline int TLSDecodeHSHelloRandom(
SSLState *ssl_state,
710 const uint8_t *
const initial_input,
711 const uint32_t input_len)
713 uint8_t *input = (uint8_t *)initial_input;
733 return (
int)(input - initial_input);
736 static inline int TLSDecodeHSHelloSessionID(
SSLState *ssl_state,
737 const uint8_t *
const initial_input,
738 const uint32_t input_len)
740 uint8_t *input = (uint8_t *)initial_input;
745 uint8_t session_id_length = *input;
759 input, 0, input_len, session_id_length) != 0) {
776 input += session_id_length;
778 return (
int)(input - initial_input);
787 static inline int TLSDecodeHSHelloCipherSuites(
SSLState *ssl_state,
788 const uint8_t *
const initial_input,
789 const uint32_t input_len)
791 const uint8_t *input = initial_input;
796 uint16_t cipher_suites_length;
799 cipher_suites_length = 2;
801 cipher_suites_length = (uint16_t)(*input << 8) | *(input + 1);
811 if ((cipher_suites_length % 2) != 0) {
815 const bool enable_ja3 =
822 if (ja3_cipher_suites == NULL)
826 uint16_t processed_len = 0;
828 while (processed_len < cipher_suites_length) {
836 uint16_t cipher_suite = (uint16_t)(*input << 8) | *(input + 1);
839 if (TLSDecodeValueIsGREASE(cipher_suite) != 1) {
842 SCTLSHandshakeAddCipher(ssl_state->
curr_connp->
hs, cipher_suite);
861 return (
int)(input - initial_input);
870 static inline int TLSDecodeHSHelloCompressionMethods(
SSLState *ssl_state,
871 const uint8_t *
const initial_input,
872 const uint32_t input_len)
874 const uint8_t *input = initial_input;
883 uint8_t compression_methods_length = *input;
886 if (!(
HAS_SPACE(compression_methods_length)))
889 input += compression_methods_length;
892 return (
int)(input - initial_input);
901 static inline int TLSDecodeHSHelloExtensionSni(
SSLState *ssl_state,
902 const uint8_t *
const initial_input,
903 const uint32_t input_len)
905 uint8_t *input = (uint8_t *)initial_input;
920 uint8_t sni_type = *input;
935 uint16_t sni_len = (uint16_t)(*input << 8) | *(input + 1);
941 if (!(
HAS_SPACE(sni_len)) || sni_len > 255 || sni_len == 0) {
954 return (
int)(input - initial_input);
957 const size_t sni_strlen = sni_len + 1;
962 const size_t consumed = input - initial_input;
964 initial_input, consumed, input_len, sni_len) != 0) {
973 return (
int)(input - initial_input);
984 static inline int TLSDecodeHSHelloExtensionSupportedVersions(
SSLState *ssl_state,
985 const uint8_t *
const initial_input,
986 const uint32_t input_len)
988 const uint8_t *input = initial_input;
998 uint8_t supported_ver_len = *input;
1001 if (supported_ver_len < 2)
1002 goto invalid_length;
1005 goto invalid_length;
1010 while (i + 1 < (uint16_t)supported_ver_len) {
1011 uint16_t ver = (uint16_t)(input[i] << 8) | input[i + 1];
1012 if (TLSVersionValid(ver)) {
1014 SCTLSHandshakeSetTLSVersion(ssl_state->
curr_connp->
hs, ver);
1023 input += supported_ver_len;
1027 goto invalid_length;
1029 uint16_t ver = (uint16_t)(*input << 8) | *(input + 1);
1032 (ver > TLS_VERSION_12)) {
1040 return (
int)(input - initial_input);
1050 static inline int TLSDecodeHSHelloExtensionEllipticCurves(
SSLState *ssl_state,
1051 const uint8_t *
const initial_input,
1052 const uint32_t input_len,
1055 const uint8_t *input = initial_input;
1062 goto invalid_length;
1064 uint16_t elliptic_curves_len = (uint16_t)(*input << 8) | *(input + 1);
1068 goto invalid_length;
1071 uint16_t ec_processed_len = 0;
1073 while (ec_processed_len < elliptic_curves_len)
1076 goto invalid_length;
1078 uint16_t elliptic_curve = (uint16_t)(*input << 8) | *(input + 1);
1081 if (TLSDecodeValueIsGREASE(elliptic_curve) != 1) {
1088 ec_processed_len += 2;
1093 input += elliptic_curves_len;
1096 return (
int)(input - initial_input);
1106 static inline int TLSDecodeHSHelloExtensionEllipticCurvePF(
SSLState *ssl_state,
1107 const uint8_t *
const initial_input,
1108 const uint32_t input_len,
1111 const uint8_t *input = initial_input;
1118 goto invalid_length;
1120 uint8_t ec_pf_len = *input;
1124 goto invalid_length;
1127 uint8_t ec_pf_processed_len = 0;
1129 while (ec_pf_processed_len < ec_pf_len)
1131 uint8_t elliptic_curve_pf = *input;
1134 if (TLSDecodeValueIsGREASE(elliptic_curve_pf) != 1) {
1141 ec_pf_processed_len += 1;
1149 return (
int)(input - initial_input);
1159 static inline int TLSDecodeHSHelloExtensionSigAlgorithms(
1160 SSLState *ssl_state,
const uint8_t *
const initial_input,
const uint32_t input_len)
1162 const uint8_t *input = initial_input;
1169 goto invalid_length;
1171 uint16_t sigalgo_len = (uint16_t)(*input << 8) | *(input + 1);
1175 if ((sigalgo_len % 2) != 0) {
1176 goto invalid_length;
1180 goto invalid_length;
1183 uint16_t sigalgo_processed_len = 0;
1184 while (sigalgo_processed_len < sigalgo_len) {
1185 uint16_t sigalgo = (uint16_t)(*input << 8) | *(input + 1);
1187 sigalgo_processed_len += 2;
1189 SCTLSHandshakeAddSigAlgo(ssl_state->
curr_connp->
hs, sigalgo);
1193 input += sigalgo_len;
1196 return (
int)(input - initial_input);
1199 SCLogDebug(
"Signature algorithm list invalid length");
1205 static inline int TLSDecodeHSHelloExtensionALPN(
1206 SSLState *ssl_state,
const uint8_t *
const initial_input,
const uint32_t input_len)
1208 const uint8_t *input = initial_input;
1215 goto invalid_length;
1217 uint16_t alpn_len = (uint16_t)(*input << 8) | *(input + 1);
1221 goto invalid_length;
1225 uint32_t alpn_processed_len = 0;
1226 while (alpn_processed_len < alpn_len) {
1227 uint8_t protolen = *input;
1229 alpn_processed_len += 1;
1232 goto invalid_length;
1236 if (alpn_processed_len + protolen > ((uint32_t)alpn_len)) {
1237 input += alpn_len - alpn_processed_len;
1240 SCTLSHandshakeAddALPN(ssl_state->
curr_connp->
hs, (
const char *)input, protolen);
1242 alpn_processed_len += protolen;
1246 return (
int)(input - initial_input);
1255 static inline int TLSDecodeHSHelloExtensions(
SSLState *ssl_state,
1256 const uint8_t *
const initial_input,
1257 const uint32_t input_len)
1259 const uint8_t *input = initial_input;
1269 JA3Buffer *ja3_elliptic_curves_pf = NULL;
1273 if (ja3_extensions == NULL)
1278 if (ja3_elliptic_curves == NULL)
1282 if (ja3_elliptic_curves_pf == NULL)
1291 uint16_t extensions_len = (uint16_t)(*input << 8) | *(input + 1);
1295 goto invalid_length;
1297 uint16_t processed_len = 0;
1299 while (processed_len < extensions_len)
1302 goto invalid_length;
1304 uint16_t ext_type = (uint16_t)(*input << 8) | *(input + 1);
1308 goto invalid_length;
1310 uint16_t ext_len = (uint16_t)(*input << 8) | *(input + 1);
1314 goto invalid_length;
1320 ret = TLSDecodeHSHelloExtensionSni(ssl_state, input,
1333 ret = TLSDecodeHSHelloExtensionEllipticCurves(ssl_state, input,
1335 ja3_elliptic_curves);
1347 ret = TLSDecodeHSHelloExtensionEllipticCurvePF(ssl_state, input,
1349 ja3_elliptic_curves_pf);
1360 ret = TLSDecodeHSHelloExtensionSigAlgorithms(ssl_state, input, ext_len);
1371 ret = TLSDecodeHSHelloExtensionALPN(ssl_state, input, ext_len);
1395 ret = TLSDecodeHSHelloExtensionSupportedVersions(ssl_state, input,
1426 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1435 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1436 SCTLSHandshakeAddExtension(ssl_state->
curr_connp->
hs, ext_type);
1440 processed_len += ext_len + 4;
1452 &ja3_elliptic_curves);
1457 &ja3_elliptic_curves_pf);
1463 return (
int)(input - initial_input);
1471 if (ja3_extensions != NULL)
1473 if (ja3_elliptic_curves != NULL)
1475 if (ja3_elliptic_curves_pf != NULL)
1481 static int TLSDecodeHandshakeHello(
SSLState *ssl_state,
1482 const uint8_t *
const input,
1483 const uint32_t input_len)
1486 uint32_t parsed = 0;
1488 ret = TLSDecodeHSHelloVersion(ssl_state, input, input_len);
1494 ret = TLSDecodeHSHelloRandom(ssl_state, input + parsed, input_len - parsed);
1505 ret = TLSDecodeHSHelloSessionID(ssl_state, input + parsed,
1506 input_len - parsed);
1513 ret = TLSDecodeHSHelloCipherSuites(ssl_state, input + parsed,
1514 input_len - parsed);
1525 ret = TLSDecodeHSHelloCompressionMethods(ssl_state, input + parsed,
1526 input_len - parsed);
1533 ret = TLSDecodeHSHelloExtensions(ssl_state, input + parsed,
1534 input_len - parsed);
1551 #ifdef DEBUG_VALIDATION
1561 const uint8_t *
const initial_input,
const uint32_t input_len)
1563 int rc = TlsDecodeHSCertificates(ssl_state, connp, initial_input, input_len);
1568 }
else if (rc < 0) {
1569 SCLogDebug(
"error parsing cert, reset state");
1581 static int SupportedHandshakeType(
const uint8_t
type)
1611 static int SSLv3ParseHandshakeType(
SSLState *ssl_state,
const uint8_t *input,
1612 uint32_t input_len, uint8_t direction)
1614 const uint8_t *initial_input = input;
1623 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");
2261 if (input_len < 6) {
2270 const uint16_t
version = (uint16_t)(input[0] << 8) | input[1];
2272 ssl_state->curr_connp->version =
version;
2273 uint16_t session_id_length = (input[5]) | (uint16_t)(input[4] << 8);
2276 ssl_state->curr_connp->bytes_processed += 6;
2277 if (session_id_length == 0) {
2284 SCLogDebug(
"Client hello is not seen before master key "
2292 if (direction == 1) {
2293 SCLogDebug(
"Incorrect SSL Record type sent in the toclient "
2303 if (direction == 0 &&
2304 !(ssl_state->curr_connp->content_type &
2306 SCLogDebug(
"Incorrect SSL Record type sent in the toserver "
2317 if (direction == 0) {
2320 SCLogDebug(
"SSLv2 client side has started the encryption");
2323 SCLogDebug(
"SSLv2 client side has started the encryption");
2327 SCLogDebug(
"SSLv2 Server side has started the encryption");
2341 SCLogDebug(
"SSLv2 No reassembly & inspection has been set");
2355 ssl_state->flags |= ssl_state->current_flags;
2357 if (input_len + ssl_state->curr_connp->bytes_processed >=
2358 (ssl_state->curr_connp->record_length +
2359 ssl_state->curr_connp->record_lengths_length)) {
2362 uint32_t diff = ssl_state->curr_connp->record_length +
2363 ssl_state->curr_connp->record_lengths_length + -
2364 ssl_state->curr_connp->bytes_processed;
2372 ssl_state->curr_connp->bytes_processed += input_len;
2381 uint32_t parsed = 0;
2382 uint32_t record_len;
2383 const bool first_call = (ssl_state->curr_connp->bytes_processed == 0);
2386 const uint16_t prev_version = ssl_state->curr_connp->version;
2388 int retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
2389 if (retval < 0 || retval > (
int)input_len) {
2397 SCLogDebug(
"%s input %p record_length %u", (direction == 0) ?
"toserver" :
"toclient",
2398 input, ssl_state->curr_connp->record_length);
2411 "incomplete header, return %u bytes consumed and wait for more data", parsed);
2418 record_len =
MIN(input_len - parsed, ssl_state->curr_connp->record_length);
2420 "record_len %u (input_len %u, parsed %u, ssl_state->curr_connp->record_length %u)",
2421 record_len, input_len, parsed, ssl_state->curr_connp->record_length);
2423 bool unknown_record =
false;
2424 switch (ssl_state->curr_connp->content_type) {
2432 unknown_record =
true;
2438 if (prev_version == TLS_VERSION_10 || prev_version == TLS_VERSION_11) {
2439 if (unknown_record) {
2443 ssl_state->curr_connp->bytes_processed = 0;
2444 ssl_state->curr_connp->content_type = 0;
2445 ssl_state->curr_connp->record_length = 0;
2447 ssl_state->curr_connp->version = prev_version;
2451 if (unknown_record) {
2459 if (ssl_state->curr_connp->record_length == 0) {
2465 if (!TLSVersionValid(ssl_state->curr_connp->version)) {
2466 SCLogDebug(
"ssl_state->curr_connp->version %04x", ssl_state->curr_connp->version);
2480 record_len = (ssl_state->curr_connp->record_length +
SSLV3_RECORD_HDR_LEN)- ssl_state->curr_connp->bytes_processed;
2481 record_len =
MIN(input_len, record_len);
2483 SCLogDebug(
"record length %u processed %u got %u",
2484 ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed, record_len);
2487 if (ssl_state->curr_connp->record_length > input_len - parsed) {
2493 uint32_t
needed = ssl_state->curr_connp->record_length;
2494 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2495 ssl_state->curr_connp->record_length, input_len, parsed,
needed);
2501 if (record_len == 0) {
2506 ssl_state->curr_connp->record_length, direction,
TLS_FRAME_DATA);
2508 switch (ssl_state->curr_connp->content_type) {
2527 int retval = SSLv3ParseAlertProtocol(ssl_state, input + parsed, record_len, direction);
2545 if (ssl_state->curr_connp == &ssl_state->client_connp) {
2552 SCLogDebug(
"setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
2559 SCLogDebug(
"setting APP_LAYER_PARSER_NO_REASSEMBLY");
2572 (ssl_state->client_connp.version > TLS_VERSION_12) &&
2581 if (ssl_state->curr_connp->record_length < 4) {
2584 SCLogDebug(
"record len < 4 => %u", ssl_state->curr_connp->record_length);
2588 int retval = SSLv3ParseHandshakeProtocol(ssl_state, input + parsed,
2589 record_len, direction);
2591 if (retval < 0 || retval > (
int)record_len) {
2603 int retval = SSLv3ParseHeartbeatProtocol(ssl_state, input + parsed,
2604 record_len, direction);
2618 parsed += record_len;
2619 ssl_state->curr_connp->bytes_processed += record_len;
2621 if (ssl_state->curr_connp->bytes_processed >=
2625 ssl_state->f, direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
2660 ssl_state->
tx_data.updated_tc =
true;
2661 ssl_state->
tx_data.updated_ts =
true;
2662 uint32_t counter = 0;
2664 const uint8_t *input = StreamSliceGetData(&stream_slice);
2665 const uint8_t *init_input = input;
2666 int32_t input_len = (int32_t)StreamSliceGetDataLen(&stream_slice);
2668 if ((input == NULL || input_len == 0) &&
2678 }
else if (input == NULL || input_len == 0) {
2694 while (input_len > 0) {
2695 if (counter > max_records) {
2696 SCLogDebug(
"Looks like we have looped quite a bit. Reset state "
2697 "and get out of here");
2708 if ((input[0] & 0x80) || (input[0] & 0x40)) {
2717 SCLogDebug(
"record %u: bytes_processed %u, version %02X, input_len %u", counter,
2724 SCLogDebug(
"Continuing parsing SSLv2 record");
2727 SSLv2Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2730 SCLogDebug(
"Error parsing SSLv2. Resetting parser "
2731 "state. Let's get outta here");
2738 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2739 (uintmax_t)(input - init_input), r.
needed);
2744 SCLogDebug(
"SSLv2 decoder consumed %d bytes: %u left", r.
retval, input_len);
2747 SCLogDebug(
"New TLS record: record_length %u",
2750 SCLogDebug(
"Continuing parsing TLS record: record_length %u, bytes_processed %u",
2754 SSLv3Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2757 SCLogDebug(
"Error parsing TLS. Resetting parser "
2758 "state. Let's get outta here");
2763 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2764 (uintmax_t)(input - init_input), r.
needed);
2808 return SSLDecode(f, 0 , alstate, pstate, stream_slice);
2814 return SSLDecode(f, 1 , alstate, pstate, stream_slice);
2821 static void *SSLStateAlloc(
void *orig_state,
AppProto proto_orig)
2835 return (
void *)ssl_state;
2852 static void SSLStateFree(
void *p)
2903 SCAppLayerTxDataCleanup(&ssl_state->
tx_data);
2925 static void SSLStateTransactionFree(
void *state, uint64_t tx_id)
2931 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t ilen, uint8_t *rdir)
2939 if ((input[0] & 0x80) && (input[2] == 0x01)) {
2946 static int SSLStateGetStateIdByName(
const char *
name,
const uint8_t direction)
2949 direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
2958 static const char *SSLStateGetStateNameById(
const int id,
const uint8_t direction)
2961 direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
2966 static int SSLStateGetFrameIdByName(
const char *frame_name)
2975 static const char *SSLStateGetFrameNameById(
const uint8_t frame_id)
2981 static int SSLStateGetEventInfo(
2985 *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
2991 static int SSLStateGetEventInfoById(
2995 if (*event_name == NULL) {
2997 "ssl's enum map table.",
3003 *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
3008 static int SSLRegisterPatternsForProtocolDetection(
void)
3011 STREAM_TOSERVER, SSLProbingParser, 0, 3) < 0) {
3017 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
3021 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
3027 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
3031 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
3037 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
3041 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
3047 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
3051 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
3058 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3062 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3066 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3072 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3076 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3080 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3086 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3090 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3094 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3100 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3104 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3108 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3118 "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
3123 "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
3133 static void CheckJA3Enabled(
void)
3135 const char *strval = NULL;
3138 if (
SCConfGet(
"app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
3140 }
else if (strcmp(strval,
"auto") == 0) {
3158 static void CheckJA4Enabled(
void)
3160 const char *strval = NULL;
3162 int enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3163 if (
SCConfGet(
"app-layer.protocols.tls.ja4-fingerprints", &strval) != 1) {
3164 enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3165 }
else if (strcmp(strval,
"auto") == 0) {
3166 enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3187 const char *proto_name =
"tls";
3195 if (SSLRegisterPatternsForProtocolDetection() < 0)
3200 IPPROTO_TCP,
"443",
ALPROTO_TLS, 0, 3, STREAM_TOSERVER, SSLProbingParser, NULL);
3203 0, 3, SSLProbingParser, NULL) == 0) {
3205 "enabling TLS detection on port 443.");
3207 STREAM_TOSERVER, SSLProbingParser, NULL);
3211 SCLogConfig(
"Protocol detection and parser disabled for %s protocol",
3218 SSLParseClientRecord);
3221 SSLParseServerRecord);
3223 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetStateIdByName, SSLStateGetStateNameById);
3225 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetFrameIdByName, SSLStateGetFrameNameById);
3248 if (enc_handle != NULL && enc_handle->
val != NULL) {
3249 SCLogDebug(
"have app-layer.protocols.tls.encryption-handling = %s", enc_handle->
val);
3250 if (strcmp(enc_handle->
val,
"full") == 0) {
3252 }
else if (strcmp(enc_handle->
val,
"bypass") == 0) {
3254 }
else if (strcmp(enc_handle->
val,
"track-only") == 0) {
3256 }
else if (strcmp(enc_handle->
val,
"default") == 0) {
3257 SCLogWarning(
"app-layer.protocols.tls.encryption-handling = default is deprecated "
3258 "and will be removed in Suricata 9, use \"track-only\" instead, "
3259 "(see ticket #7642)");
3266 if (
SCConfGetNode(
"app-layer.protocols.tls.no-reassemble") == NULL) {
3268 if (
SCConfGetBool(
"tls.no-reassemble", &value) == 1 && value == 1)
3272 if (
SCConfGetBool(
"app-layer.protocols.tls.no-reassemble", &value) == 1 &&
3288 SCLogWarning(
"MD5 calculation has been disabled, disabling JA3");
3292 SCLogWarning(
"Hashing has been disabled, disabling JA4");
3306 SCLogConfig(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);