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 uint32_t processed_len = 0;
1295 while (processed_len < (uint32_t)extensions_len) {
1297 goto invalid_length;
1299 uint16_t ext_type = (uint16_t)(*input << 8) | *(input + 1);
1303 goto invalid_length;
1305 uint16_t ext_len = (uint16_t)(*input << 8) | *(input + 1);
1309 goto invalid_length;
1311 if (processed_len + 4UL + (uint32_t)ext_len > (uint32_t)extensions_len)
1312 goto invalid_length;
1318 ret = TLSDecodeHSHelloExtensionSni(ssl_state, input,
1331 ret = TLSDecodeHSHelloExtensionEllipticCurves(ssl_state, input,
1333 ja3_elliptic_curves);
1345 ret = TLSDecodeHSHelloExtensionEllipticCurvePF(ssl_state, input,
1347 ja3_elliptic_curves_pf);
1358 ret = TLSDecodeHSHelloExtensionSigAlgorithms(ssl_state, input, ext_len);
1369 ret = TLSDecodeHSHelloExtensionALPN(ssl_state, input, ext_len);
1393 ret = TLSDecodeHSHelloExtensionSupportedVersions(ssl_state, input,
1424 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1433 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1434 SCTLSHandshakeAddExtension(ssl_state->
curr_connp->
hs, ext_type);
1438 processed_len += (uint32_t)ext_len + 4UL;
1450 &ja3_elliptic_curves);
1455 &ja3_elliptic_curves_pf);
1461 return (
int)(input - initial_input);
1469 if (ja3_extensions != NULL)
1471 if (ja3_elliptic_curves != NULL)
1473 if (ja3_elliptic_curves_pf != NULL)
1479 static int TLSDecodeHandshakeHello(
SSLState *ssl_state,
1480 const uint8_t *
const input,
1481 const uint32_t input_len)
1484 uint32_t parsed = 0;
1486 ret = TLSDecodeHSHelloVersion(ssl_state, input, input_len);
1492 ret = TLSDecodeHSHelloRandom(ssl_state, input + parsed, input_len - parsed);
1503 ret = TLSDecodeHSHelloSessionID(ssl_state, input + parsed,
1504 input_len - parsed);
1511 ret = TLSDecodeHSHelloCipherSuites(ssl_state, input + parsed,
1512 input_len - parsed);
1523 ret = TLSDecodeHSHelloCompressionMethods(ssl_state, input + parsed,
1524 input_len - parsed);
1531 ret = TLSDecodeHSHelloExtensions(ssl_state, input + parsed,
1532 input_len - parsed);
1549 #ifdef DEBUG_VALIDATION
1559 const uint8_t *
const initial_input,
const uint32_t input_len)
1561 int rc = TlsDecodeHSCertificates(ssl_state, connp, initial_input, input_len);
1566 }
else if (rc < 0) {
1567 SCLogDebug(
"error parsing cert, reset state");
1579 static int SupportedHandshakeType(
const uint8_t
type)
1609 static int SSLv3ParseHandshakeType(
SSLState *ssl_state,
const uint8_t *input,
1610 uint32_t input_len, uint8_t direction)
1612 const uint8_t *initial_input = input;
1624 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1636 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1650 rc = SSLv3ParseHandshakeTypeCertificate(ssl_state,
1690 static int SSLv3ParseHandshakeProtocol(
SSLState *ssl_state,
const uint8_t *input,
1691 uint32_t input_len, uint8_t direction)
1693 const uint8_t *initial_input = input;
1704 SCLogDebug(
"partial handshake record in place");
1707 const uint32_t add =
MIN(need, input_len);
1713 const uint32_t new_size = avail + (4096 - (avail % 4096));
1714 SCLogDebug(
"new_size %u, avail %u", new_size, avail);
1722 SCLogDebug(
"ssl_state->curr_connp->hs_buffer_offset %u "
1723 "ssl_state->curr_connp->hs_buffer_size %u",
1745 SCLogDebug(
"got all data now: handshake_type %u message_length %u",
1776 SCLogDebug(
"input %p input_len %u", input, input_len);
1778 if (input_len < 4) {
1785 SCLogDebug(
"handshake_type %u message len %u input %p input_len %u",
1795 SCLogDebug(
"supported_type %s handshake_type %u/%02x", supported_type ?
"true" :
"false",
1797 if (!supported_type) {
1798 uint32_t avail_record_len =
MIN(input_len, record_len);
1799 input += avail_record_len;
1800 input_len -= avail_record_len;
1814 if (record_len > input_len) {
1815 const uint32_t avail = input_len;
1816 const uint32_t size = avail + (4096 - (avail % 4096));
1817 SCLogDebug(
"initial buffer size %u, based on input %u", size, avail);
1826 if (input_len > 0) {
1834 SCLogDebug(
"opened record buffer %p size %u offset %u type %u msg_size %u",
1841 return (
int)(input - initial_input);
1845 int retval = SSLv3ParseHandshakeType(
1847 if (retval < 0 || retval > (
int)input_len) {
1851 SCLogDebug(
"retval %d input_len %u", retval, input_len);
1853 input_len -= retval;
1859 return (
int)(input - initial_input);
1873 static int SSLv3ParseAlertProtocol(
1874 SSLState *ssl_state,
const uint8_t *input, uint32_t input_len, uint8_t direction)
1876 if (input_len < 2) {
1882 if (input_len == 2) {
1883 uint8_t level = input[0];
1906 static int SSLv3ParseHeartbeatProtocol(
SSLState *ssl_state,
const uint8_t *input,
1907 uint32_t input_len, uint8_t direction)
1911 uint32_t padding_len;
1914 if (input_len < 3) {
1931 SCLogDebug(
"HeartBeat Record type sent in the toclient direction!");
1934 SCLogDebug(
"HeartBeat Record type sent in the toserver direction!");
1942 SCLogDebug(
"Encrypted HeartBeat Request In-flight. Storing len %u",
1947 payload_len = (uint16_t)(*input << 8) | *(input + 1);
1952 SCLogDebug(
"We have a short record in HeartBeat Request");
1960 if (padding_len < 16) {
1961 SCLogDebug(
"We have a short record in HeartBeat Request");
1975 SCLogDebug(
"Multiple in-flight server initiated HeartBeats");
1981 SCLogDebug(
"Multiple in-flight client initiated HeartBeats");
1999 SCLogDebug(
"My heart is bleeding.. OpenSSL HeartBleed response (%u)",
2018 static int SSLv3ParseRecord(uint8_t direction,
SSLState *ssl_state,
2019 const uint8_t *input, uint32_t input_len)
2021 const uint8_t *initial_input = input;
2023 if (input_len == 0) {
2027 uint8_t skip_version = 0;
2031 if (direction == 0) {
2045 if (input_len >= 5) {
2047 if (!skip_version) {
2056 if (--input_len == 0)
2062 if (!skip_version) {
2067 if (--input_len == 0)
2072 if (!skip_version) {
2077 if (--input_len == 0)
2083 if (--input_len == 0)
2089 if (--input_len == 0)
2097 return (
int)(input - initial_input);
2100 static int SSLv2ParseRecord(uint8_t direction,
SSLState *ssl_state,
2101 const uint8_t *input, uint32_t input_len)
2103 const uint8_t *initial_input = input;
2105 if (input_len == 0) {
2120 if (--input_len == 0)
2127 if (--input_len == 0)
2134 if (--input_len == 0)
2151 if (--input_len == 0)
2158 if (--input_len == 0)
2165 if (--input_len == 0)
2172 if (--input_len == 0)
2181 return (
int)(input - initial_input);
2188 const uint8_t *initial_input = input;
2190 if (ssl_state->curr_connp->bytes_processed == 0) {
2191 if (input[0] & 0x80) {
2192 ssl_state->curr_connp->record_lengths_length = 2;
2194 ssl_state->curr_connp->record_lengths_length = 3;
2202 SCLogDebug(
"direction %u ssl_state->curr_connp->record_lengths_length + 1 %u, "
2203 "ssl_state->curr_connp->bytes_processed %u",
2204 direction, ssl_state->curr_connp->record_lengths_length + 1,
2205 ssl_state->curr_connp->bytes_processed);
2208 if (ssl_state->curr_connp->bytes_processed <
2209 (ssl_state->curr_connp->record_lengths_length + 1)) {
2210 const int retval = SSLv2ParseRecord(direction, ssl_state, input, input_len);
2212 ssl_state->curr_connp->record_length);
2213 if (retval < 0 || retval > (
int)input_len) {
2220 ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length,
2229 if (ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length >
2230 input_len + ssl_state->curr_connp->bytes_processed) {
2231 uint32_t
needed = ssl_state->curr_connp->record_length;
2232 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2233 ssl_state->curr_connp->record_length, input_len, (uint32_t)(input - initial_input),
2238 if (input_len == 0) {
2243 if (ssl_state->curr_connp->record_length == 0) {
2250 if (ssl_state->curr_connp->record_lengths_length == 0) {
2251 SCLogDebug(
"SSLv2 record lengths length is zero");
2256 switch (ssl_state->curr_connp->content_type) {
2258 SCLogDebug(
"SSLV2_MT_ERROR msg_type received. Error encountered "
2259 "in establishing the sslv2 session, may be version");
2265 if (input_len < 6) {
2274 const uint16_t
version = (uint16_t)(input[0] << 8) | input[1];
2276 ssl_state->curr_connp->version =
version;
2277 uint16_t session_id_length = (input[5]) | (uint16_t)(input[4] << 8);
2280 ssl_state->curr_connp->bytes_processed += 6;
2281 if (session_id_length == 0) {
2288 SCLogDebug(
"Client hello is not seen before master key "
2296 if (direction == 1) {
2297 SCLogDebug(
"Incorrect SSL Record type sent in the toclient "
2307 if (direction == 0 &&
2308 !(ssl_state->curr_connp->content_type &
2310 SCLogDebug(
"Incorrect SSL Record type sent in the toserver "
2321 if (direction == 0) {
2324 SCLogDebug(
"SSLv2 client side has started the encryption");
2327 SCLogDebug(
"SSLv2 client side has started the encryption");
2331 SCLogDebug(
"SSLv2 Server side has started the encryption");
2345 SCLogDebug(
"SSLv2 No reassembly & inspection has been set");
2359 ssl_state->flags |= ssl_state->current_flags;
2361 if (input_len + ssl_state->curr_connp->bytes_processed >=
2362 (ssl_state->curr_connp->record_length +
2363 ssl_state->curr_connp->record_lengths_length)) {
2366 uint32_t diff = ssl_state->curr_connp->record_length +
2367 ssl_state->curr_connp->record_lengths_length + -
2368 ssl_state->curr_connp->bytes_processed;
2376 ssl_state->curr_connp->bytes_processed += input_len;
2385 uint32_t parsed = 0;
2386 uint32_t record_len;
2387 const bool first_call = (ssl_state->curr_connp->bytes_processed == 0);
2390 const uint16_t prev_version = ssl_state->curr_connp->version;
2392 int retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
2393 if (retval < 0 || retval > (
int)input_len) {
2401 SCLogDebug(
"%s input %p record_length %u", (direction == 0) ?
"toserver" :
"toclient",
2402 input, ssl_state->curr_connp->record_length);
2415 "incomplete header, return %u bytes consumed and wait for more data", parsed);
2422 record_len =
MIN(input_len - parsed, ssl_state->curr_connp->record_length);
2424 "record_len %u (input_len %u, parsed %u, ssl_state->curr_connp->record_length %u)",
2425 record_len, input_len, parsed, ssl_state->curr_connp->record_length);
2427 bool unknown_record =
false;
2428 switch (ssl_state->curr_connp->content_type) {
2436 unknown_record =
true;
2442 if (prev_version == TLS_VERSION_10 || prev_version == TLS_VERSION_11) {
2443 if (unknown_record) {
2447 ssl_state->curr_connp->bytes_processed = 0;
2448 ssl_state->curr_connp->content_type = 0;
2449 ssl_state->curr_connp->record_length = 0;
2451 ssl_state->curr_connp->version = prev_version;
2455 if (unknown_record) {
2463 if (ssl_state->curr_connp->record_length == 0) {
2469 if (!TLSVersionValid(ssl_state->curr_connp->version)) {
2470 SCLogDebug(
"ssl_state->curr_connp->version %04x", ssl_state->curr_connp->version);
2484 record_len = (ssl_state->curr_connp->record_length +
SSLV3_RECORD_HDR_LEN)- ssl_state->curr_connp->bytes_processed;
2485 record_len =
MIN(input_len, record_len);
2487 SCLogDebug(
"record length %u processed %u got %u",
2488 ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed, record_len);
2491 if (ssl_state->curr_connp->record_length > input_len - parsed) {
2497 uint32_t
needed = ssl_state->curr_connp->record_length;
2498 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2499 ssl_state->curr_connp->record_length, input_len, parsed,
needed);
2505 if (record_len == 0) {
2510 ssl_state->curr_connp->record_length, direction,
TLS_FRAME_DATA);
2512 switch (ssl_state->curr_connp->content_type) {
2531 int retval = SSLv3ParseAlertProtocol(ssl_state, input + parsed, record_len, direction);
2549 if (ssl_state->curr_connp == &ssl_state->client_connp) {
2556 SCLogDebug(
"setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
2563 SCLogDebug(
"setting APP_LAYER_PARSER_NO_REASSEMBLY");
2576 (ssl_state->client_connp.version > TLS_VERSION_12) &&
2585 if (ssl_state->curr_connp->record_length < 4) {
2588 SCLogDebug(
"record len < 4 => %u", ssl_state->curr_connp->record_length);
2592 int retval = SSLv3ParseHandshakeProtocol(ssl_state, input + parsed,
2593 record_len, direction);
2595 if (retval < 0 || retval > (
int)record_len) {
2607 int retval = SSLv3ParseHeartbeatProtocol(ssl_state, input + parsed,
2608 record_len, direction);
2622 parsed += record_len;
2623 ssl_state->curr_connp->bytes_processed += record_len;
2625 if (ssl_state->curr_connp->bytes_processed >=
2629 ssl_state->f, direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
2666 uint32_t counter = 0;
2668 const uint8_t *input = StreamSliceGetData(&stream_slice);
2669 const uint8_t *init_input = input;
2670 int32_t input_len = (int32_t)StreamSliceGetDataLen(&stream_slice);
2672 if ((input == NULL || input_len == 0) &&
2682 }
else if (input == NULL || input_len == 0) {
2698 while (input_len > 0) {
2699 if (counter > max_records) {
2700 SCLogDebug(
"Looks like we have looped quite a bit. Reset state "
2701 "and get out of here");
2712 if ((input[0] & 0x80) || (input[0] & 0x40)) {
2721 SCLogDebug(
"record %u: bytes_processed %u, version %02X, input_len %u", counter,
2728 SCLogDebug(
"Continuing parsing SSLv2 record");
2731 SSLv2Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2734 SCLogDebug(
"Error parsing SSLv2. Resetting parser "
2735 "state. Let's get outta here");
2742 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2743 (uintmax_t)(input - init_input), r.
needed);
2748 SCLogDebug(
"SSLv2 decoder consumed %d bytes: %u left", r.
retval, input_len);
2751 SCLogDebug(
"New TLS record: record_length %u",
2754 SCLogDebug(
"Continuing parsing TLS record: record_length %u, bytes_processed %u",
2758 SSLv3Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2761 SCLogDebug(
"Error parsing TLS. Resetting parser "
2762 "state. Let's get outta here");
2767 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2768 (uintmax_t)(input - init_input), r.
needed);
2812 return SSLDecode(f, 0 , alstate, pstate, stream_slice);
2818 return SSLDecode(f, 1 , alstate, pstate, stream_slice);
2825 static void *SSLStateAlloc(
void *orig_state,
AppProto proto_orig)
2833 return (
void *)ssl_state;
2850 static void SSLStateFree(
void *
p)
2929 static void SSLStateTransactionFree(
void *state, uint64_t tx_id)
2935 const Flow *f, uint8_t direction,
const uint8_t *input, uint32_t ilen, uint8_t *rdir)
2943 if ((input[0] & 0x80) && (input[2] == 0x01)) {
2950 static int SSLStateGetStateIdByName(
const char *
name,
const uint8_t direction)
2953 direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
2962 static const char *SSLStateGetStateNameById(
const int id,
const uint8_t direction)
2965 direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
2970 static int SSLStateGetFrameIdByName(
const char *frame_name)
2979 static const char *SSLStateGetFrameNameById(
const uint8_t frame_id)
2985 static int SSLStateGetEventInfo(
2995 static int SSLStateGetEventInfoById(
2999 if (*event_name == NULL) {
3001 "ssl's enum map table.",
3012 static int SSLRegisterPatternsForProtocolDetection(
void)
3015 STREAM_TOSERVER, SSLProbingParser, 0, 3) < 0) {
3021 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
3025 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 00|", 3, 0, STREAM_TOSERVER) < 0) {
3031 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
3035 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 01|", 3, 0, STREAM_TOSERVER) < 0) {
3041 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
3045 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 02|", 3, 0, STREAM_TOSERVER) < 0) {
3051 IPPROTO_TCP,
ALPROTO_TLS,
"|01 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
3055 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 03|", 3, 0, STREAM_TOSERVER) < 0) {
3062 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3066 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3070 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0) {
3076 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3080 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3084 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0) {
3090 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3094 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3098 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0) {
3104 IPPROTO_TCP,
ALPROTO_TLS,
"|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3108 IPPROTO_TCP,
ALPROTO_TLS,
"|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3112 IPPROTO_TCP,
ALPROTO_TLS,
"|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0) {
3122 "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
3127 "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
3137 static void CheckJA3Enabled(
void)
3139 const char *strval = NULL;
3142 if (
SCConfGetNonNull(
"app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
3144 }
else if (strcmp(strval,
"auto") == 0) {
3162 static void CheckJA4Enabled(
void)
3164 const char *strval = NULL;
3166 int enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3167 if (
SCConfGetNonNull(
"app-layer.protocols.tls.ja4-fingerprints", &strval) != 1) {
3168 enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3169 }
else if (strcmp(strval,
"auto") == 0) {
3170 enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3191 const char *proto_name =
"tls";
3199 if (SSLRegisterPatternsForProtocolDetection() < 0)
3204 IPPROTO_TCP,
"443",
ALPROTO_TLS, 0, 3, STREAM_TOSERVER, SSLProbingParser, NULL);
3207 0, 3, SSLProbingParser, NULL) == 0) {
3209 "enabling TLS detection on port 443.");
3211 STREAM_TOSERVER, SSLProbingParser, NULL);
3215 SCLogConfig(
"Protocol detection and parser disabled for %s protocol",
3222 SSLParseClientRecord);
3225 SSLParseServerRecord);
3227 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetStateIdByName, SSLStateGetStateNameById);
3229 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetFrameIdByName, SSLStateGetFrameNameById);
3252 if (enc_handle != NULL && enc_handle->
val != NULL) {
3253 SCLogDebug(
"have app-layer.protocols.tls.encryption-handling = %s", enc_handle->
val);
3254 if (strcmp(enc_handle->
val,
"full") == 0) {
3256 }
else if (strcmp(enc_handle->
val,
"bypass") == 0) {
3258 }
else if (strcmp(enc_handle->
val,
"track-only") == 0) {
3260 }
else if (strcmp(enc_handle->
val,
"default") == 0) {
3261 SCLogWarning(
"app-layer.protocols.tls.encryption-handling = default is deprecated "
3262 "and will be removed in Suricata 9, use \"track-only\" instead, "
3263 "(see ticket #7642)");
3270 if (
SCConfGetNode(
"app-layer.protocols.tls.no-reassemble") == NULL) {
3272 if (
SCConfGetBool(
"tls.no-reassemble", &value) == 1 && value == 1)
3276 if (
SCConfGetBool(
"app-layer.protocols.tls.no-reassemble", &value) == 1 &&
3292 SCLogWarning(
"MD5 calculation has been disabled, disabling JA3");
3296 SCLogWarning(
"Hashing has been disabled, disabling JA4");
3310 SCLogConfig(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);