114 {
"CERTIFICATE_INVALID_ALGORITHMIDENTIFIER",
149 #define SSL_CONFIG_DEFAULT_JA3 0
150 #define SSL_CONFIG_DEFAULT_JA4 0
171 #define SSLV3_CHANGE_CIPHER_SPEC 20
172 #define SSLV3_ALERT_PROTOCOL 21
173 #define SSLV3_HANDSHAKE_PROTOCOL 22
174 #define SSLV3_APPLICATION_PROTOCOL 23
175 #define SSLV3_HEARTBEAT_PROTOCOL 24
178 #define SSLV3_HS_HELLO_REQUEST 0
179 #define SSLV3_HS_CLIENT_HELLO 1
180 #define SSLV3_HS_SERVER_HELLO 2
181 #define SSLV3_HS_NEW_SESSION_TICKET 4
182 #define SSLV3_HS_CERTIFICATE 11
183 #define SSLV3_HS_SERVER_KEY_EXCHANGE 12
184 #define SSLV3_HS_CERTIFICATE_REQUEST 13
185 #define SSLV3_HS_SERVER_HELLO_DONE 14
186 #define SSLV3_HS_CERTIFICATE_VERIFY 15
187 #define SSLV3_HS_CLIENT_KEY_EXCHANGE 16
188 #define SSLV3_HS_FINISHED 20
189 #define SSLV3_HS_CERTIFICATE_URL 21
190 #define SSLV3_HS_CERTIFICATE_STATUS 22
193 #define SSLV2_MT_ERROR 0
194 #define SSLV2_MT_CLIENT_HELLO 1
195 #define SSLV2_MT_CLIENT_MASTER_KEY 2
196 #define SSLV2_MT_CLIENT_FINISHED 3
197 #define SSLV2_MT_SERVER_HELLO 4
198 #define SSLV2_MT_SERVER_VERIFY 5
199 #define SSLV2_MT_SERVER_FINISHED 6
200 #define SSLV2_MT_REQUEST_CERTIFICATE 7
201 #define SSLV2_MT_CLIENT_CERTIFICATE 8
203 #define SSLV3_RECORD_HDR_LEN 5
204 #define SSLV3_MESSAGE_HDR_LEN 4
206 #define SSLV3_RECORD_MAX_LEN ((1 << 14) + 1024)
208 #define SSLV3_CLIENT_HELLO_VERSION_LEN 2
209 #define SSLV3_CLIENT_HELLO_RANDOM_LEN 32
212 #define TLS_HB_REQUEST 1
213 #define TLS_HB_RESPONSE 2
215 #define SSL_RECORD_MINIMUM_LENGTH 6
217 #define SHA1_STRING_LENGTH 60
219 #define HAS_SPACE(n) ((uint64_t)(input - initial_input) + (uint64_t)(n) <= (uint64_t)(input_len))
225 #define SSL_DECODER_ERROR(e) \
226 (struct SSLDecoderResult) \
230 #define SSL_DECODER_OK(c) \
231 (struct SSLDecoderResult) \
235 #define SSL_DECODER_INCOMPLETE(c, n) \
236 (struct SSLDecoderResult) \
241 static inline int SafeMemcpy(
void *
dst,
size_t dst_offset,
size_t dst_size,
242 const void *
src,
size_t src_offset,
size_t src_size,
size_t src_tocopy)
WARN_UNUSED;
244 static inline int SafeMemcpy(
void *
dst,
size_t dst_offset,
size_t dst_size,
245 const void *
src,
size_t src_offset,
size_t src_size,
size_t src_tocopy)
252 if (dst_offset < dst_size && src_offset < src_size &&
253 src_tocopy <= (src_size - src_offset) &&
254 src_tocopy <= (dst_size - dst_offset)) {
255 memcpy(
dst + dst_offset,
src + src_offset, src_tocopy);
261 #ifdef DEBUG_VALIDATION
262 #define ValidateRecordState(connp) \
264 DEBUG_VALIDATE_BUG_ON(((connp)->record_length + SSLV3_RECORD_HDR_LEN) < \
265 (connp)->bytes_processed); \
268 #define ValidateRecordState(...)
271 #define SSLParserHSReset(connp) \
273 (connp)->handshake_type = 0; \
274 (connp)->message_length = 0; \
277 #define SSLParserReset(state) \
279 SCLogDebug("resetting state"); \
280 (state)->curr_connp->bytes_processed = 0; \
281 SSLParserHSReset((state)->curr_connp); \
284 #define SSLSetEvent(ssl_state, event) \
286 SCLogDebug("setting event %u", (event)); \
287 if ((ssl_state) == NULL) { \
288 SCLogDebug("could not set decoder event %u", event); \
290 AppLayerDecoderEventsSetEventRaw(&(ssl_state)->tx_data.events, (event)); \
291 (ssl_state)->events++; \
295 static void *SSLGetTx(
void *state, uint64_t tx_id)
301 static uint64_t SSLGetTxCnt(
void *state)
307 static int SSLGetAlstateProgress(
void *tx, uint8_t direction)
323 if (direction == STREAM_TOSERVER &&
339 static AppLayerStateData *SSLGetStateData(
void *vstate)
351 strlcat(buffer,
"UNDETERMINED", 13);
372 strlcat(buffer,
"TLS 1.3 draft-28", 17);
375 strlcat(buffer,
"TLS 1.3 draft-27", 17);
378 strlcat(buffer,
"TLS 1.3 draft-26", 17);
381 strlcat(buffer,
"TLS 1.3 draft-25", 17);
384 strlcat(buffer,
"TLS 1.3 draft-24", 17);
387 strlcat(buffer,
"TLS 1.3 draft-23", 17);
390 strlcat(buffer,
"TLS 1.3 draft-22", 17);
393 strlcat(buffer,
"TLS 1.3 draft-21", 17);
396 strlcat(buffer,
"TLS 1.3 draft-20", 17);
399 strlcat(buffer,
"TLS 1.3 draft-19", 17);
402 strlcat(buffer,
"TLS 1.3 draft-18", 17);
405 strlcat(buffer,
"TLS 1.3 draft-17", 17);
408 strlcat(buffer,
"TLS 1.3 draft-16", 17);
411 strlcat(buffer,
"TLS 1.3 draft-<16", 18);
414 strlcat(buffer,
"TLS 1.3 draft-20-fb", 20);
417 strlcat(buffer,
"TLS 1.3 draft-21-fb", 20);
420 strlcat(buffer,
"TLS 1.3 draft-22-fb", 20);
423 strlcat(buffer,
"TLS 1.3 draft-23-fb", 20);
426 strlcat(buffer,
"TLS 1.3 draft-26-fb", 20);
429 snprintf(buffer, 7,
"0x%04x",
version);
434 static void TlsDecodeHSCertificateErrSetEvent(
SSLState *ssl_state, uint32_t err)
477 static inline int TlsDecodeHSCertificateFingerprint(
478 SSLStateConnp *connp,
const uint8_t *input, uint32_t cert_len)
488 if (SCSha1HashBuffer(input, cert_len, hash,
sizeof(hash)) == 1) {
495 static inline int TlsDecodeHSCertificateAddCertToChain(
496 SSLStateConnp *connp,
const uint8_t *input, uint32_t cert_len)
510 const uint8_t *
const initial_input,
const uint32_t input_len,
const int certn)
512 const uint8_t *input = (uint8_t *)initial_input;
513 uint32_t err_code = 0;
520 uint32_t cert_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
529 x509 = rs_x509_decode(input, cert_len, &err_code);
531 TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
535 char *
str = rs_x509_get_subject(x509);
542 str = rs_x509_get_issuer(x509);
549 str = rs_x509_get_serial(x509);
565 rc = TlsDecodeHSCertificateFingerprint(connp, input, cert_len);
567 SCLogDebug(
"TlsDecodeHSCertificateFingerprint failed with %d", rc);
572 rc = TlsDecodeHSCertificateAddCertToChain(connp, input, cert_len);
574 SCLogDebug(
"TlsDecodeHSCertificateAddCertToChain failed with %d", rc);
580 return (input - initial_input);
584 TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
601 const uint8_t *
const initial_input,
const uint32_t input_len)
603 const uint8_t *input = (uint8_t *)initial_input;
608 const uint32_t cert_chain_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
627 uint32_t processed_len = 0;
629 while (processed_len < cert_chain_len) {
630 int rc = TlsDecodeHSCertificate(ssl_state, connp, connp->
certs_buffer + processed_len,
636 if (processed_len + (uint32_t)rc > cert_chain_len) {
640 processed_len += (uint32_t)rc;
643 return processed_len + 3;
657 static inline int TLSDecodeValueIsGREASE(
const uint16_t value)
683 static inline int TLSDecodeHSHelloVersion(
SSLState *ssl_state,
684 const uint8_t *
const initial_input,
685 const uint32_t input_len)
687 uint8_t *input = (uint8_t *)initial_input;
696 uint16_t
version = (uint16_t)(*input << 8) | *(input + 1);
738 return (input - initial_input);
741 static inline int TLSDecodeHSHelloRandom(
SSLState *ssl_state,
742 const uint8_t *
const initial_input,
743 const uint32_t input_len)
745 uint8_t *input = (uint8_t *)initial_input;
765 return (input - initial_input);
768 static inline int TLSDecodeHSHelloSessionID(
SSLState *ssl_state,
769 const uint8_t *
const initial_input,
770 const uint32_t input_len)
772 uint8_t *input = (uint8_t *)initial_input;
777 uint8_t session_id_length = *input;
791 input, 0, input_len, session_id_length) != 0) {
808 input += session_id_length;
810 return (input - initial_input);
819 static inline int TLSDecodeHSHelloCipherSuites(
SSLState *ssl_state,
820 const uint8_t *
const initial_input,
821 const uint32_t input_len)
823 const uint8_t *input = initial_input;
828 uint16_t cipher_suites_length;
831 cipher_suites_length = 2;
833 cipher_suites_length = (uint16_t)(*input << 8) | *(input + 1);
843 if ((cipher_suites_length % 2) != 0) {
854 if (ja3_cipher_suites == NULL)
858 uint16_t processed_len = 0;
860 while (processed_len < cipher_suites_length)
869 uint16_t cipher_suite = (uint16_t)(*input << 8) | *(input + 1);
872 if (TLSDecodeValueIsGREASE(cipher_suite) != 1) {
896 input += cipher_suites_length;
899 return (input - initial_input);
908 static inline int TLSDecodeHSHelloCompressionMethods(
SSLState *ssl_state,
909 const uint8_t *
const initial_input,
910 const uint32_t input_len)
912 const uint8_t *input = initial_input;
921 uint8_t compression_methods_length = *input;
924 if (!(
HAS_SPACE(compression_methods_length)))
927 input += compression_methods_length;
930 return (input - initial_input);
939 static inline int TLSDecodeHSHelloExtensionSni(
SSLState *ssl_state,
940 const uint8_t *
const initial_input,
941 const uint32_t input_len)
943 uint8_t *input = (uint8_t *)initial_input;
958 uint8_t sni_type = *input;
973 uint16_t sni_len = (uint16_t)(*input << 8) | *(input + 1);
979 if (!(
HAS_SPACE(sni_len)) || sni_len > 255 || sni_len == 0) {
992 return (input - initial_input);
995 const size_t sni_strlen = sni_len + 1;
1000 const size_t consumed = input - initial_input;
1002 initial_input, consumed, input_len, sni_len) != 0) {
1011 return (input - initial_input);
1022 static inline int TLSDecodeHSHelloExtensionSupportedVersions(
SSLState *ssl_state,
1023 const uint8_t *
const initial_input,
1024 const uint32_t input_len)
1026 const uint8_t *input = initial_input;
1034 goto invalid_length;
1036 uint8_t supported_ver_len = *input;
1039 if (supported_ver_len < 2)
1040 goto invalid_length;
1043 goto invalid_length;
1048 while (i + 1 < (uint16_t)supported_ver_len) {
1049 uint16_t ver = (uint16_t)(input[i] << 8) | input[i + 1];
1050 if (TLSVersionValid(ver)) {
1064 input += supported_ver_len;
1068 goto invalid_length;
1070 uint16_t ver = (uint16_t)(*input << 8) | *(input + 1);
1081 return (input - initial_input);
1091 static inline int TLSDecodeHSHelloExtensionEllipticCurves(
SSLState *ssl_state,
1092 const uint8_t *
const initial_input,
1093 const uint32_t input_len,
1096 const uint8_t *input = initial_input;
1103 goto invalid_length;
1105 uint16_t elliptic_curves_len = (uint16_t)(*input << 8) | *(input + 1);
1109 goto invalid_length;
1113 uint16_t ec_processed_len = 0;
1115 while (ec_processed_len < elliptic_curves_len)
1118 goto invalid_length;
1120 uint16_t elliptic_curve = (uint16_t)(*input << 8) | *(input + 1);
1123 if (TLSDecodeValueIsGREASE(elliptic_curve) != 1) {
1130 ec_processed_len += 2;
1135 input += elliptic_curves_len;
1138 return (input - initial_input);
1148 static inline int TLSDecodeHSHelloExtensionEllipticCurvePF(
SSLState *ssl_state,
1149 const uint8_t *
const initial_input,
1150 const uint32_t input_len,
1153 const uint8_t *input = initial_input;
1160 goto invalid_length;
1162 uint8_t ec_pf_len = *input;
1166 goto invalid_length;
1170 uint8_t ec_pf_processed_len = 0;
1172 while (ec_pf_processed_len < ec_pf_len)
1174 uint8_t elliptic_curve_pf = *input;
1177 if (TLSDecodeValueIsGREASE(elliptic_curve_pf) != 1) {
1184 ec_pf_processed_len += 1;
1192 return (input - initial_input);
1202 static inline int TLSDecodeHSHelloExtensionSigAlgorithms(
1203 SSLState *ssl_state,
const uint8_t *
const initial_input,
const uint32_t input_len)
1205 const uint8_t *input = initial_input;
1212 goto invalid_length;
1214 uint16_t sigalgo_len = (uint16_t)(*input << 8) | *(input + 1);
1218 if ((sigalgo_len % 2) != 0) {
1219 goto invalid_length;
1223 goto invalid_length;
1227 uint16_t sigalgo_processed_len = 0;
1228 while (sigalgo_processed_len < sigalgo_len) {
1229 uint16_t sigalgo = (uint16_t)(*input << 8) | *(input + 1);
1231 sigalgo_processed_len += 2;
1237 input += sigalgo_len;
1240 return (input - initial_input);
1243 SCLogDebug(
"Signature algorithm list invalid length");
1249 static inline int TLSDecodeHSHelloExtensionALPN(
1250 SSLState *ssl_state,
const uint8_t *
const initial_input,
const uint32_t input_len)
1252 const uint8_t *input = initial_input;
1259 goto invalid_length;
1261 uint16_t alpn_len = (uint16_t)(*input << 8) | *(input + 1);
1265 goto invalid_length;
1271 uint32_t alpn_processed_len = 0;
1272 while (alpn_processed_len < alpn_len) {
1273 uint8_t protolen = *input;
1275 alpn_processed_len += 1;
1278 goto invalid_length;
1282 if (alpn_processed_len + protolen > ((uint32_t)alpn_len)) {
1283 input += alpn_len - alpn_processed_len;
1288 if (alpn_processed_len == 1) {
1289 SCJA4SetALPN(ssl_state->
curr_connp->
ja4, (
const char *)input, protolen);
1292 alpn_processed_len += protolen;
1300 return (input - initial_input);
1309 static inline int TLSDecodeHSHelloExtensions(
SSLState *ssl_state,
1310 const uint8_t *
const initial_input,
1311 const uint32_t input_len)
1313 const uint8_t *input = initial_input;
1321 JA3Buffer *ja3_elliptic_curves_pf = NULL;
1325 if (ja3_extensions == NULL)
1330 if (ja3_elliptic_curves == NULL)
1334 if (ja3_elliptic_curves_pf == NULL)
1343 uint16_t extensions_len = (uint16_t)(*input << 8) | *(input + 1);
1347 goto invalid_length;
1349 uint16_t processed_len = 0;
1351 while (processed_len < extensions_len)
1354 goto invalid_length;
1356 uint16_t ext_type = (uint16_t)(*input << 8) | *(input + 1);
1360 goto invalid_length;
1362 uint16_t ext_len = (uint16_t)(*input << 8) | *(input + 1);
1366 goto invalid_length;
1372 ret = TLSDecodeHSHelloExtensionSni(ssl_state, input,
1385 ret = TLSDecodeHSHelloExtensionEllipticCurves(ssl_state, input,
1387 ja3_elliptic_curves);
1399 ret = TLSDecodeHSHelloExtensionEllipticCurvePF(ssl_state, input,
1401 ja3_elliptic_curves_pf);
1412 ret = TLSDecodeHSHelloExtensionSigAlgorithms(ssl_state, input, ext_len);
1423 ret = TLSDecodeHSHelloExtensionALPN(ssl_state, input, ext_len);
1447 ret = TLSDecodeHSHelloExtensionSupportedVersions(ssl_state, input,
1478 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1487 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1492 processed_len += ext_len + 4;
1504 &ja3_elliptic_curves);
1509 &ja3_elliptic_curves_pf);
1515 return (input - initial_input);
1523 if (ja3_extensions != NULL)
1525 if (ja3_elliptic_curves != NULL)
1527 if (ja3_elliptic_curves_pf != NULL)
1533 static int TLSDecodeHandshakeHello(
SSLState *ssl_state,
1534 const uint8_t *
const input,
1535 const uint32_t input_len)
1538 uint32_t parsed = 0;
1549 ret = TLSDecodeHSHelloVersion(ssl_state, input, input_len);
1555 ret = TLSDecodeHSHelloRandom(ssl_state, input + parsed, input_len - parsed);
1566 ret = TLSDecodeHSHelloSessionID(ssl_state, input + parsed,
1567 input_len - parsed);
1574 ret = TLSDecodeHSHelloCipherSuites(ssl_state, input + parsed,
1575 input_len - parsed);
1586 ret = TLSDecodeHSHelloCompressionMethods(ssl_state, input + parsed,
1587 input_len - parsed);
1594 ret = TLSDecodeHSHelloExtensions(ssl_state, input + parsed,
1595 input_len - parsed);
1607 #ifdef DEBUG_VALIDATION
1617 const uint8_t *
const initial_input,
const uint32_t input_len)
1619 int rc = TlsDecodeHSCertificates(ssl_state, connp, initial_input, input_len);
1624 }
else if (rc < 0) {
1625 SCLogDebug(
"error parsing cert, reset state");
1632 static int SupportedHandshakeType(
const uint8_t
type)
1661 static int SSLv3ParseHandshakeType(
SSLState *ssl_state,
const uint8_t *input,
1662 uint32_t input_len, uint8_t direction)
1664 const uint8_t *initial_input = input;
1667 if (input_len == 0) {
1676 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1685 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1700 rc = SSLv3ParseHandshakeTypeCertificate(ssl_state,
1737 static int SSLv3ParseHandshakeProtocol(
SSLState *ssl_state,
const uint8_t *input,
1738 uint32_t input_len, uint8_t direction)
1740 const uint8_t *initial_input = input;
1751 SCLogDebug(
"partial handshake record in place");
1754 const uint32_t add =
MIN(need, input_len);
1760 const uint32_t new_size = avail + (4096 - (avail % 4096));
1761 SCLogDebug(
"new_size %u, avail %u", new_size, avail);
1769 SCLogDebug(
"ssl_state->curr_connp->hs_buffer_offset %u "
1770 "ssl_state->curr_connp->hs_buffer_size %u",
1792 SCLogDebug(
"got all data now: handshake_type %u message_length %u",
1823 SCLogDebug(
"input %p input_len %u", input, input_len);
1825 if (input_len < 4) {
1832 SCLogDebug(
"handshake_type %u message len %u input %p input_len %u",
1842 SCLogDebug(
"supported_type %s handshake_type %u/%02x", supported_type ?
"true" :
"false",
1844 if (!supported_type) {
1845 uint32_t avail_record_len =
MIN(input_len, record_len);
1846 input += avail_record_len;
1847 input_len -= avail_record_len;
1861 if (record_len > input_len) {
1862 const uint32_t avail = input_len;
1863 const uint32_t size = avail + (4096 - (avail % 4096));
1864 SCLogDebug(
"initial buffer size %u, based on input %u", size, avail);
1873 if (input_len > 0) {
1881 SCLogDebug(
"opened record buffer %p size %u offset %u type %u msg_size %u",
1888 return (input - initial_input);
1892 int retval = SSLv3ParseHandshakeType(
1894 if (retval < 0 || retval > (
int)input_len) {
1898 SCLogDebug(
"retval %d input_len %u", retval, input_len);
1900 input_len -= retval;
1906 return (input - initial_input);
1920 static int SSLv3ParseHeartbeatProtocol(
SSLState *ssl_state,
const uint8_t *input,
1921 uint32_t input_len, uint8_t direction)
1925 uint32_t padding_len;
1928 if (input_len < 3) {
1945 SCLogDebug(
"HeartBeat Record type sent in the toclient direction!");
1948 SCLogDebug(
"HeartBeat Record type sent in the toserver direction!");
1956 SCLogDebug(
"Encrypted HeartBeat Request In-flight. Storing len %u",
1961 payload_len = (uint16_t)(*input << 8) | *(input + 1);
1966 SCLogDebug(
"We have a short record in HeartBeat Request");
1974 if (padding_len < 16) {
1975 SCLogDebug(
"We have a short record in HeartBeat Request");
1989 SCLogDebug(
"Multiple in-flight server initiated HeartBeats");
1995 SCLogDebug(
"Multiple in-flight client initiated HeartBeats");
2013 SCLogDebug(
"My heart is bleeding.. OpenSSL HeartBleed response (%u)",
2032 static int SSLv3ParseRecord(uint8_t direction,
SSLState *ssl_state,
2033 const uint8_t *input, uint32_t input_len)
2035 const uint8_t *initial_input = input;
2037 if (input_len == 0) {
2041 uint8_t skip_version = 0;
2045 if (direction == 0) {
2059 if (input_len >= 5) {
2061 if (!skip_version) {
2070 if (--input_len == 0)
2076 if (!skip_version) {
2081 if (--input_len == 0)
2086 if (!skip_version) {
2091 if (--input_len == 0)
2097 if (--input_len == 0)
2103 if (--input_len == 0)
2111 return (input - initial_input);
2114 static int SSLv2ParseRecord(uint8_t direction,
SSLState *ssl_state,
2115 const uint8_t *input, uint32_t input_len)
2117 const uint8_t *initial_input = input;
2119 if (input_len == 0) {
2134 if (--input_len == 0)
2141 if (--input_len == 0)
2148 if (--input_len == 0)
2165 if (--input_len == 0)
2172 if (--input_len == 0)
2179 if (--input_len == 0)
2186 if (--input_len == 0)
2195 return (input - initial_input);
2200 const StreamSlice stream_slice)
2202 const uint8_t *initial_input = input;
2204 if (ssl_state->curr_connp->bytes_processed == 0) {
2205 if (input[0] & 0x80) {
2206 ssl_state->curr_connp->record_lengths_length = 2;
2208 ssl_state->curr_connp->record_lengths_length = 3;
2216 SCLogDebug(
"direction %u ssl_state->curr_connp->record_lengths_length + 1 %u, "
2217 "ssl_state->curr_connp->bytes_processed %u",
2218 direction, ssl_state->curr_connp->record_lengths_length + 1,
2219 ssl_state->curr_connp->bytes_processed);
2222 if (ssl_state->curr_connp->bytes_processed <
2223 (ssl_state->curr_connp->record_lengths_length + 1)) {
2224 const int retval = SSLv2ParseRecord(direction, ssl_state, input, input_len);
2226 ssl_state->curr_connp->record_length);
2227 if (retval < 0 || retval > (
int)input_len) {
2234 ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length,
2243 if (ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length >
2244 input_len + ssl_state->curr_connp->bytes_processed) {
2245 uint32_t
needed = ssl_state->curr_connp->record_length;
2246 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2247 ssl_state->curr_connp->record_length, input_len, (uint32_t)(input - initial_input),
2252 if (input_len == 0) {
2257 if (ssl_state->curr_connp->record_length == 0) {
2264 if (ssl_state->curr_connp->record_lengths_length == 0) {
2265 SCLogDebug(
"SSLv2 record lengths length is zero");
2270 switch (ssl_state->curr_connp->content_type) {
2272 SCLogDebug(
"SSLV2_MT_ERROR msg_type received. Error encountered "
2273 "in establishing the sslv2 session, may be version");
2279 if (input_len < 6) {
2287 const uint16_t
version = (uint16_t)(input[0] << 8) | input[1];
2289 ssl_state->curr_connp->version =
version;
2290 uint16_t session_id_length = (input[5]) | (uint16_t)(input[4] << 8);
2293 ssl_state->curr_connp->bytes_processed += 6;
2294 if (session_id_length == 0) {
2301 SCLogDebug(
"Client hello is not seen before master key "
2309 if (direction == 1) {
2310 SCLogDebug(
"Incorrect SSL Record type sent in the toclient "
2319 if (direction == 0 &&
2320 !(ssl_state->curr_connp->content_type &
2322 SCLogDebug(
"Incorrect SSL Record type sent in the toserver "
2333 if (direction == 0) {
2336 SCLogDebug(
"SSLv2 client side has started the encryption");
2339 SCLogDebug(
"SSLv2 client side has started the encryption");
2343 SCLogDebug(
"SSLv2 Server side has started the encryption");
2358 SCLogDebug(
"SSLv2 No reassembly & inspection has been set");
2371 ssl_state->flags |= ssl_state->current_flags;
2373 if (input_len + ssl_state->curr_connp->bytes_processed >=
2374 (ssl_state->curr_connp->record_length +
2375 ssl_state->curr_connp->record_lengths_length)) {
2378 uint32_t diff = ssl_state->curr_connp->record_length +
2379 ssl_state->curr_connp->record_lengths_length + -
2380 ssl_state->curr_connp->bytes_processed;
2388 ssl_state->curr_connp->bytes_processed += input_len;
2395 const StreamSlice stream_slice)
2397 uint32_t parsed = 0;
2398 uint32_t record_len;
2399 const bool first_call = (ssl_state->curr_connp->bytes_processed == 0);
2402 const uint16_t prev_version = ssl_state->curr_connp->version;
2404 int retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
2405 if (retval < 0 || retval > (
int)input_len) {
2413 SCLogDebug(
"%s input %p record_length %u", (direction == 0) ?
"toserver" :
"toclient",
2414 input, ssl_state->curr_connp->record_length);
2427 "incomplete header, return %u bytes consumed and wait for more data", parsed);
2434 record_len =
MIN(input_len - parsed, ssl_state->curr_connp->record_length);
2436 "record_len %u (input_len %u, parsed %u, ssl_state->curr_connp->record_length %u)",
2437 record_len, input_len, parsed, ssl_state->curr_connp->record_length);
2439 bool unknown_record =
false;
2440 switch (ssl_state->curr_connp->content_type) {
2448 unknown_record =
true;
2455 if (unknown_record) {
2459 ssl_state->curr_connp->bytes_processed = 0;
2460 ssl_state->curr_connp->content_type = 0;
2461 ssl_state->curr_connp->record_length = 0;
2463 ssl_state->curr_connp->version = prev_version;
2467 if (unknown_record) {
2475 if (ssl_state->curr_connp->record_length == 0) {
2481 if (!TLSVersionValid(ssl_state->curr_connp->version)) {
2482 SCLogDebug(
"ssl_state->curr_connp->version %04x", ssl_state->curr_connp->version);
2496 record_len = (ssl_state->curr_connp->record_length +
SSLV3_RECORD_HDR_LEN)- ssl_state->curr_connp->bytes_processed;
2497 record_len =
MIN(input_len, record_len);
2499 SCLogDebug(
"record length %u processed %u got %u",
2500 ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed, record_len);
2503 if (ssl_state->curr_connp->record_length > input_len - parsed) {
2509 uint32_t
needed = ssl_state->curr_connp->record_length;
2510 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2511 ssl_state->curr_connp->record_length, input_len, parsed,
needed);
2517 if (record_len == 0) {
2522 ssl_state->curr_connp->record_length, direction,
TLS_FRAME_DATA);
2524 switch (ssl_state->curr_connp->content_type) {
2555 SCLogDebug(
"setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
2563 SCLogDebug(
"setting APP_LAYER_PARSER_NO_REASSEMBLY");
2588 if (ssl_state->curr_connp->record_length < 4) {
2591 SCLogDebug(
"record len < 4 => %u", ssl_state->curr_connp->record_length);
2595 int retval = SSLv3ParseHandshakeProtocol(ssl_state, input + parsed,
2596 record_len, direction);
2598 if (retval < 0 || retval > (
int)record_len) {
2610 int retval = SSLv3ParseHeartbeatProtocol(ssl_state, input + parsed,
2611 record_len, direction);
2625 parsed += record_len;
2626 ssl_state->curr_connp->bytes_processed += record_len;
2628 if (ssl_state->curr_connp->bytes_processed >=
2632 ssl_state->f, direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
2663 static AppLayerResult SSLDecode(
Flow *f, uint8_t direction,
void *alstate,
2667 uint32_t counter = 0;
2669 const uint8_t *input = StreamSliceGetData(&stream_slice);
2670 const uint8_t *init_input = input;
2671 int32_t input_len = (int32_t)StreamSliceGetDataLen(&stream_slice);
2673 if (input == NULL &&
2680 }
else if (input == NULL || input_len == 0) {
2696 while (input_len > 0) {
2697 if (counter > max_records) {
2698 SCLogDebug(
"Looks like we have looped quite a bit. Reset state "
2699 "and get out of here");
2710 if ((input[0] & 0x80) || (input[0] & 0x40)) {
2719 SCLogDebug(
"record %u: bytes_processed %u, version %02X, input_len %u", counter,
2726 SCLogDebug(
"Continuing parsing SSLv2 record");
2729 SSLv2Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2732 SCLogDebug(
"Error parsing SSLv2. Resetting parser "
2733 "state. Let's get outta here");
2740 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2741 (uintmax_t)(input - init_input), r.
needed);
2746 SCLogDebug(
"SSLv2 decoder consumed %d bytes: %u left", r.
retval, input_len);
2749 SCLogDebug(
"New TLS record: record_length %u",
2752 SCLogDebug(
"Continuing parsing TLS record: record_length %u, bytes_processed %u",
2756 SSLv3Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2759 SCLogDebug(
"Error parsing TLS. Resetting parser "
2760 "state. Let's get outta here");
2765 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2766 (uintmax_t)(input - init_input), r.
needed);
2805 StreamSlice stream_slice,
void *local_data)
2807 return SSLDecode(f, 0 , alstate, pstate, stream_slice);
2811 StreamSlice stream_slice,
void *local_data)
2813 return SSLDecode(f, 1 , alstate, pstate, stream_slice);
2820 static void *SSLStateAlloc(
void *orig_state,
AppProto proto_orig)
2832 return (
void *)ssl_state;
2839 static void SSLStateFree(
void *p)
2887 if (ssl_state->
tx_data.de_state != NULL) {
2913 static void SSLStateTransactionFree(
void *state, uint64_t tx_id)
2918 static AppProto SSLProbingParser(
Flow *f, uint8_t direction,
2919 const uint8_t *input, uint32_t ilen, uint8_t *rdir)
2927 if ((input[0] & 0x80) && (input[2] == 0x01)) {
2934 static int SSLStateGetFrameIdByName(
const char *frame_name)
2943 static const char *SSLStateGetFrameNameById(
const uint8_t frame_id)
2949 static int SSLStateGetEventInfo(
const char *event_name,
2950 int *event_id, AppLayerEventType *event_type)
2953 if (*event_id == -1) {
2955 "ssl's enum map table.",
2961 *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
2966 static int SSLStateGetEventInfoById(
int event_id,
const char **event_name,
2967 AppLayerEventType *event_type)
2970 if (*event_name == NULL) {
2972 "ssl's enum map table.",
2978 *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
2983 static int SSLRegisterPatternsForProtocolDetection(
void)
2986 STREAM_TOSERVER, SSLProbingParser, 0, 3) < 0) {
2992 "|01 03 00|", 3, 0, STREAM_TOSERVER) < 0)
2997 "|16 03 00|", 3, 0, STREAM_TOSERVER) < 0)
3004 "|01 03 01|", 3, 0, STREAM_TOSERVER) < 0)
3009 "|16 03 01|", 3, 0, STREAM_TOSERVER) < 0)
3016 "|01 03 02|", 3, 0, STREAM_TOSERVER) < 0)
3021 "|16 03 02|", 3, 0, STREAM_TOSERVER) < 0)
3028 "|01 03 03|", 3, 0, STREAM_TOSERVER) < 0)
3033 "|16 03 03|", 3, 0, STREAM_TOSERVER) < 0)
3041 "|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
3046 "|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
3051 "|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
3058 "|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
3063 "|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
3068 "|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
3075 "|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
3080 "|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
3085 "|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
3092 "|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
3097 "|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
3102 "|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
3113 "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
3118 "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
3128 static void CheckJA3Enabled(
void)
3130 const char *strval = NULL;
3133 if (
ConfGet(
"app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
3135 }
else if (strcmp(strval,
"auto") == 0) {
3153 static void CheckJA4Enabled(
void)
3155 const char *strval = NULL;
3158 if (
ConfGet(
"app-layer.protocols.tls.ja4-fingerprints", &strval) != 1) {
3160 }
else if (strcmp(strval,
"auto") == 0) {
3182 const char *proto_name =
"tls";
3190 if (SSLRegisterPatternsForProtocolDetection() < 0)
3199 SSLProbingParser, NULL);
3204 SSLProbingParser, NULL) == 0) {
3206 "enabling TLS detection on port 443.");
3212 SSLProbingParser, NULL);
3216 SCLogConfig(
"Protocol detection and parser disabled for %s protocol",
3223 SSLParseClientRecord);
3226 SSLParseServerRecord);
3229 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetFrameIdByName, SSLStateGetFrameNameById);
3251 if (enc_handle != NULL && enc_handle->
val != NULL) {
3252 SCLogDebug(
"have app-layer.protocols.tls.encryption-handling = %s", enc_handle->
val);
3253 if (strcmp(enc_handle->
val,
"full") == 0) {
3255 }
else if (strcmp(enc_handle->
val,
"bypass") == 0) {
3257 }
else if (strcmp(enc_handle->
val,
"default") == 0) {
3264 if (
ConfGetNode(
"app-layer.protocols.tls.no-reassemble") == NULL) {
3266 if (
ConfGetBool(
"tls.no-reassemble", &value) == 1 && value == 1)
3270 if (
ConfGetBool(
"app-layer.protocols.tls.no-reassemble", &value) == 1 && value == 1)
3285 SCLogWarning(
"MD5 calculation has been disabled, disabling JA3");
3289 SCLogWarning(
"Hashing has been disabled, disabling JA4");
3303 SCLogConfig(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);