101 {
"CERTIFICATE_INVALID_ALGORITHMIDENTIFIER",
136 #define SSL_CONFIG_DEFAULT_JA3 0
137 #define SSL_CONFIG_DEFAULT_JA4 0
158 #define SSLV3_CHANGE_CIPHER_SPEC 20
159 #define SSLV3_ALERT_PROTOCOL 21
160 #define SSLV3_HANDSHAKE_PROTOCOL 22
161 #define SSLV3_APPLICATION_PROTOCOL 23
162 #define SSLV3_HEARTBEAT_PROTOCOL 24
165 #define SSLV3_HS_HELLO_REQUEST 0
166 #define SSLV3_HS_CLIENT_HELLO 1
167 #define SSLV3_HS_SERVER_HELLO 2
168 #define SSLV3_HS_NEW_SESSION_TICKET 4
169 #define SSLV3_HS_CERTIFICATE 11
170 #define SSLV3_HS_SERVER_KEY_EXCHANGE 12
171 #define SSLV3_HS_CERTIFICATE_REQUEST 13
172 #define SSLV3_HS_SERVER_HELLO_DONE 14
173 #define SSLV3_HS_CERTIFICATE_VERIFY 15
174 #define SSLV3_HS_CLIENT_KEY_EXCHANGE 16
175 #define SSLV3_HS_FINISHED 20
176 #define SSLV3_HS_CERTIFICATE_URL 21
177 #define SSLV3_HS_CERTIFICATE_STATUS 22
180 #define SSLV2_MT_ERROR 0
181 #define SSLV2_MT_CLIENT_HELLO 1
182 #define SSLV2_MT_CLIENT_MASTER_KEY 2
183 #define SSLV2_MT_CLIENT_FINISHED 3
184 #define SSLV2_MT_SERVER_HELLO 4
185 #define SSLV2_MT_SERVER_VERIFY 5
186 #define SSLV2_MT_SERVER_FINISHED 6
187 #define SSLV2_MT_REQUEST_CERTIFICATE 7
188 #define SSLV2_MT_CLIENT_CERTIFICATE 8
190 #define SSLV3_RECORD_HDR_LEN 5
192 #define SSLV3_RECORD_MAX_LEN ((1 << 14) + 1024)
194 #define SSLV3_CLIENT_HELLO_VERSION_LEN 2
195 #define SSLV3_CLIENT_HELLO_RANDOM_LEN 32
198 #define TLS_HB_REQUEST 1
199 #define TLS_HB_RESPONSE 2
201 #define SSL_RECORD_MINIMUM_LENGTH 6
203 #define SHA1_STRING_LENGTH 60
205 #define HAS_SPACE(n) ((uint64_t)(input - initial_input) + (uint64_t)(n) <= (uint64_t)(input_len))
211 #define SSL_DECODER_ERROR(e) \
212 (struct SSLDecoderResult) \
216 #define SSL_DECODER_OK(c) \
217 (struct SSLDecoderResult) \
221 #define SSL_DECODER_INCOMPLETE(c, n) \
222 (struct SSLDecoderResult) \
227 static inline int SafeMemcpy(
void *
dst,
size_t dst_offset,
size_t dst_size,
228 const void *
src,
size_t src_offset,
size_t src_size,
size_t src_tocopy)
WARN_UNUSED;
230 static inline int SafeMemcpy(
void *
dst,
size_t dst_offset,
size_t dst_size,
231 const void *
src,
size_t src_offset,
size_t src_size,
size_t src_tocopy)
238 if (dst_offset < dst_size && src_offset < src_size &&
239 src_tocopy <= (src_size - src_offset) &&
240 src_tocopy <= (dst_size - dst_offset)) {
241 memcpy(
dst + dst_offset,
src + src_offset, src_tocopy);
247 #ifdef DEBUG_VALIDATION
248 #define ValidateRecordState(connp) \
250 DEBUG_VALIDATE_BUG_ON(((connp)->record_length + SSLV3_RECORD_HDR_LEN) < \
251 (connp)->bytes_processed); \
254 #define ValidateRecordState(...)
257 #define SSLParserHSReset(connp) \
259 (connp)->handshake_type = 0; \
260 (connp)->message_length = 0; \
263 #define SSLParserReset(state) \
265 SCLogDebug("resetting state"); \
266 (state)->curr_connp->bytes_processed = 0; \
267 SSLParserHSReset((state)->curr_connp); \
270 #define SSLSetEvent(ssl_state, event) \
272 SCLogDebug("setting event %u", (event)); \
273 if ((ssl_state) == NULL) { \
274 SCLogDebug("could not set decoder event %u", event); \
276 AppLayerDecoderEventsSetEventRaw(&(ssl_state)->tx_data.events, (event)); \
277 (ssl_state)->events++; \
283 static void *SSLGetTx(
void *state, uint64_t tx_id)
289 static uint64_t SSLGetTxCnt(
void *state)
295 static int SSLGetAlstateProgress(
void *tx, uint8_t direction)
311 if (direction == STREAM_TOSERVER &&
327 static AppLayerStateData *SSLGetStateData(
void *vstate)
339 strlcat(buffer,
"UNDETERMINED", 13);
360 strlcat(buffer,
"TLS 1.3 draft-28", 17);
363 strlcat(buffer,
"TLS 1.3 draft-27", 17);
366 strlcat(buffer,
"TLS 1.3 draft-26", 17);
369 strlcat(buffer,
"TLS 1.3 draft-25", 17);
372 strlcat(buffer,
"TLS 1.3 draft-24", 17);
375 strlcat(buffer,
"TLS 1.3 draft-23", 17);
378 strlcat(buffer,
"TLS 1.3 draft-22", 17);
381 strlcat(buffer,
"TLS 1.3 draft-21", 17);
384 strlcat(buffer,
"TLS 1.3 draft-20", 17);
387 strlcat(buffer,
"TLS 1.3 draft-19", 17);
390 strlcat(buffer,
"TLS 1.3 draft-18", 17);
393 strlcat(buffer,
"TLS 1.3 draft-17", 17);
396 strlcat(buffer,
"TLS 1.3 draft-16", 17);
399 strlcat(buffer,
"TLS 1.3 draft-<16", 18);
402 strlcat(buffer,
"TLS 1.3 draft-20-fb", 20);
405 strlcat(buffer,
"TLS 1.3 draft-21-fb", 20);
408 strlcat(buffer,
"TLS 1.3 draft-22-fb", 20);
411 strlcat(buffer,
"TLS 1.3 draft-23-fb", 20);
414 strlcat(buffer,
"TLS 1.3 draft-26-fb", 20);
417 snprintf(buffer, 7,
"0x%04x",
version);
422 static void TlsDecodeHSCertificateErrSetEvent(
SSLState *ssl_state, uint32_t err)
465 static inline int TlsDecodeHSCertificateFingerprint(
466 SSLStateConnp *connp,
const uint8_t *input, uint32_t cert_len)
476 if (SCSha1HashBuffer(input, cert_len, hash,
sizeof(hash)) == 1) {
483 static inline int TlsDecodeHSCertificateAddCertToChain(
484 SSLStateConnp *connp,
const uint8_t *input, uint32_t cert_len)
498 const uint8_t *
const initial_input,
const uint32_t input_len,
const int certn)
500 const uint8_t *input = (uint8_t *)initial_input;
501 uint32_t err_code = 0;
508 uint32_t cert_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
517 x509 = rs_x509_decode(input, cert_len, &err_code);
519 TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
523 char *
str = rs_x509_get_subject(x509);
530 str = rs_x509_get_issuer(x509);
543 sans[i] = rs_x509_get_subjectaltname_at(x509, i);
546 str = rs_x509_get_serial(x509);
562 rc = TlsDecodeHSCertificateFingerprint(connp, input, cert_len);
564 SCLogDebug(
"TlsDecodeHSCertificateFingerprint failed with %d", rc);
569 rc = TlsDecodeHSCertificateAddCertToChain(connp, input, cert_len);
571 SCLogDebug(
"TlsDecodeHSCertificateAddCertToChain failed with %d", rc);
577 return (
int)(input - initial_input);
581 TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
585 SSLStateCertSANFree(connp);
600 const uint8_t *
const initial_input,
const uint32_t input_len)
602 const uint8_t *input = (uint8_t *)initial_input;
607 const uint32_t cert_chain_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
626 uint32_t processed_len = 0;
628 while (processed_len < cert_chain_len) {
629 int rc = TlsDecodeHSCertificate(ssl_state, connp, connp->
certs_buffer + processed_len,
635 if (processed_len + (uint32_t)rc > cert_chain_len) {
639 processed_len += (uint32_t)rc;
642 return processed_len + 3;
656 static inline int TLSDecodeValueIsGREASE(
const uint16_t value)
682 static inline int TLSDecodeHSHelloVersion(
SSLState *ssl_state,
683 const uint8_t *
const initial_input,
684 const uint32_t input_len)
686 uint8_t *input = (uint8_t *)initial_input;
695 uint16_t
version = (uint16_t)(*input << 8) | *(input + 1);
737 return (
int)(input - initial_input);
740 static inline int TLSDecodeHSHelloRandom(
SSLState *ssl_state,
741 const uint8_t *
const initial_input,
742 const uint32_t input_len)
744 uint8_t *input = (uint8_t *)initial_input;
764 return (
int)(input - initial_input);
767 static inline int TLSDecodeHSHelloSessionID(
SSLState *ssl_state,
768 const uint8_t *
const initial_input,
769 const uint32_t input_len)
771 uint8_t *input = (uint8_t *)initial_input;
776 uint8_t session_id_length = *input;
790 input, 0, input_len, session_id_length) != 0) {
807 input += session_id_length;
809 return (
int)(input - initial_input);
818 static inline int TLSDecodeHSHelloCipherSuites(
SSLState *ssl_state,
819 const uint8_t *
const initial_input,
820 const uint32_t input_len)
822 const uint8_t *input = initial_input;
827 uint16_t cipher_suites_length;
830 cipher_suites_length = 2;
832 cipher_suites_length = (uint16_t)(*input << 8) | *(input + 1);
842 if ((cipher_suites_length % 2) != 0) {
846 const bool enable_ja3 =
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 (
int)(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 (
int)(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 (
int)(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 (
int)(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 (
int)(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;
1112 uint16_t ec_processed_len = 0;
1114 while (ec_processed_len < elliptic_curves_len)
1117 goto invalid_length;
1119 uint16_t elliptic_curve = (uint16_t)(*input << 8) | *(input + 1);
1122 if (TLSDecodeValueIsGREASE(elliptic_curve) != 1) {
1129 ec_processed_len += 2;
1134 input += elliptic_curves_len;
1137 return (
int)(input - initial_input);
1147 static inline int TLSDecodeHSHelloExtensionEllipticCurvePF(
SSLState *ssl_state,
1148 const uint8_t *
const initial_input,
1149 const uint32_t input_len,
1152 const uint8_t *input = initial_input;
1159 goto invalid_length;
1161 uint8_t ec_pf_len = *input;
1165 goto invalid_length;
1168 uint8_t ec_pf_processed_len = 0;
1170 while (ec_pf_processed_len < ec_pf_len)
1172 uint8_t elliptic_curve_pf = *input;
1175 if (TLSDecodeValueIsGREASE(elliptic_curve_pf) != 1) {
1182 ec_pf_processed_len += 1;
1190 return (
int)(input - initial_input);
1200 static inline int TLSDecodeHSHelloExtensionSigAlgorithms(
1201 SSLState *ssl_state,
const uint8_t *
const initial_input,
const uint32_t input_len)
1203 const uint8_t *input = initial_input;
1210 goto invalid_length;
1212 uint16_t sigalgo_len = (uint16_t)(*input << 8) | *(input + 1);
1216 if ((sigalgo_len % 2) != 0) {
1217 goto invalid_length;
1221 goto invalid_length;
1225 uint16_t sigalgo_processed_len = 0;
1226 while (sigalgo_processed_len < sigalgo_len) {
1227 uint16_t sigalgo = (uint16_t)(*input << 8) | *(input + 1);
1229 sigalgo_processed_len += 2;
1235 input += sigalgo_len;
1238 return (
int)(input - initial_input);
1241 SCLogDebug(
"Signature algorithm list invalid length");
1247 static void StoreALPN(
SSLStateConnp *connp,
const uint8_t *alpn,
const uint32_t size)
1252 memcpy(a->
alpn, alpn, size);
1259 static inline int TLSDecodeHSHelloExtensionALPN(
1260 SSLState *ssl_state,
const uint8_t *
const initial_input,
const uint32_t input_len)
1262 const uint8_t *input = initial_input;
1269 goto invalid_length;
1271 uint16_t alpn_len = (uint16_t)(*input << 8) | *(input + 1);
1275 goto invalid_length;
1279 uint32_t alpn_processed_len = 0;
1280 while (alpn_processed_len < alpn_len) {
1281 uint8_t protolen = *input;
1283 alpn_processed_len += 1;
1286 goto invalid_length;
1290 if (alpn_processed_len + protolen > ((uint32_t)alpn_len)) {
1291 input += alpn_len - alpn_processed_len;
1298 if (alpn_processed_len == 1) {
1299 SCJA4SetALPN(ssl_state->
curr_connp->
ja4, (
const char *)input, protolen);
1302 StoreALPN(ssl_state->
curr_connp, input, protolen);
1304 alpn_processed_len += protolen;
1308 return (
int)(input - initial_input);
1317 static inline int TLSDecodeHSHelloExtensions(
SSLState *ssl_state,
1318 const uint8_t *
const initial_input,
1319 const uint32_t input_len)
1321 const uint8_t *input = initial_input;
1331 JA3Buffer *ja3_elliptic_curves_pf = NULL;
1335 if (ja3_extensions == NULL)
1340 if (ja3_elliptic_curves == NULL)
1344 if (ja3_elliptic_curves_pf == NULL)
1353 uint16_t extensions_len = (uint16_t)(*input << 8) | *(input + 1);
1357 goto invalid_length;
1359 uint16_t processed_len = 0;
1361 while (processed_len < extensions_len)
1364 goto invalid_length;
1366 uint16_t ext_type = (uint16_t)(*input << 8) | *(input + 1);
1370 goto invalid_length;
1372 uint16_t ext_len = (uint16_t)(*input << 8) | *(input + 1);
1376 goto invalid_length;
1382 ret = TLSDecodeHSHelloExtensionSni(ssl_state, input,
1395 ret = TLSDecodeHSHelloExtensionEllipticCurves(ssl_state, input,
1397 ja3_elliptic_curves);
1409 ret = TLSDecodeHSHelloExtensionEllipticCurvePF(ssl_state, input,
1411 ja3_elliptic_curves_pf);
1422 ret = TLSDecodeHSHelloExtensionSigAlgorithms(ssl_state, input, ext_len);
1433 ret = TLSDecodeHSHelloExtensionALPN(ssl_state, input, ext_len);
1457 ret = TLSDecodeHSHelloExtensionSupportedVersions(ssl_state, input,
1488 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1497 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1502 processed_len += ext_len + 4;
1514 &ja3_elliptic_curves);
1519 &ja3_elliptic_curves_pf);
1525 return (
int)(input - initial_input);
1533 if (ja3_extensions != NULL)
1535 if (ja3_elliptic_curves != NULL)
1537 if (ja3_elliptic_curves_pf != NULL)
1543 static int TLSDecodeHandshakeHello(
SSLState *ssl_state,
1544 const uint8_t *
const input,
1545 const uint32_t input_len)
1548 uint32_t parsed = 0;
1559 ret = TLSDecodeHSHelloVersion(ssl_state, input, input_len);
1565 ret = TLSDecodeHSHelloRandom(ssl_state, input + parsed, input_len - parsed);
1576 ret = TLSDecodeHSHelloSessionID(ssl_state, input + parsed,
1577 input_len - parsed);
1584 ret = TLSDecodeHSHelloCipherSuites(ssl_state, input + parsed,
1585 input_len - parsed);
1596 ret = TLSDecodeHSHelloCompressionMethods(ssl_state, input + parsed,
1597 input_len - parsed);
1604 ret = TLSDecodeHSHelloExtensions(ssl_state, input + parsed,
1605 input_len - parsed);
1617 #ifdef DEBUG_VALIDATION
1627 const uint8_t *
const initial_input,
const uint32_t input_len)
1629 int rc = TlsDecodeHSCertificates(ssl_state, connp, initial_input, input_len);
1634 }
else if (rc < 0) {
1635 SCLogDebug(
"error parsing cert, reset state");
1642 static int SupportedHandshakeType(
const uint8_t
type)
1672 static int SSLv3ParseHandshakeType(
SSLState *ssl_state,
const uint8_t *input,
1673 uint32_t input_len, uint8_t direction)
1675 const uint8_t *initial_input = input;
1684 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1693 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1708 rc = SSLv3ParseHandshakeTypeCertificate(ssl_state,
1745 static int SSLv3ParseHandshakeProtocol(
SSLState *ssl_state,
const uint8_t *input,
1746 uint32_t input_len, uint8_t direction)
1748 const uint8_t *initial_input = input;
1759 SCLogDebug(
"partial handshake record in place");
1762 const uint32_t add =
MIN(need, input_len);
1768 const uint32_t new_size = avail + (4096 - (avail % 4096));
1769 SCLogDebug(
"new_size %u, avail %u", new_size, avail);
1777 SCLogDebug(
"ssl_state->curr_connp->hs_buffer_offset %u "
1778 "ssl_state->curr_connp->hs_buffer_size %u",
1800 SCLogDebug(
"got all data now: handshake_type %u message_length %u",
1831 SCLogDebug(
"input %p input_len %u", input, input_len);
1833 if (input_len < 4) {
1840 SCLogDebug(
"handshake_type %u message len %u input %p input_len %u",
1850 SCLogDebug(
"supported_type %s handshake_type %u/%02x", supported_type ?
"true" :
"false",
1852 if (!supported_type) {
1853 uint32_t avail_record_len =
MIN(input_len, record_len);
1854 input += avail_record_len;
1855 input_len -= avail_record_len;
1869 if (record_len > input_len) {
1870 const uint32_t avail = input_len;
1871 const uint32_t size = avail + (4096 - (avail % 4096));
1872 SCLogDebug(
"initial buffer size %u, based on input %u", size, avail);
1881 if (input_len > 0) {
1889 SCLogDebug(
"opened record buffer %p size %u offset %u type %u msg_size %u",
1896 return (
int)(input - initial_input);
1900 int retval = SSLv3ParseHandshakeType(
1902 if (retval < 0 || retval > (
int)input_len) {
1906 SCLogDebug(
"retval %d input_len %u", retval, input_len);
1908 input_len -= retval;
1914 return (
int)(input - initial_input);
1928 static int SSLv3ParseAlertProtocol(
1929 SSLState *ssl_state,
const uint8_t *input, uint32_t input_len, uint8_t direction)
1931 if (input_len < 2) {
1937 if (input_len == 2) {
1938 uint8_t level = input[0];
1960 static int SSLv3ParseHeartbeatProtocol(
SSLState *ssl_state,
const uint8_t *input,
1961 uint32_t input_len, uint8_t direction)
1965 uint32_t padding_len;
1968 if (input_len < 3) {
1985 SCLogDebug(
"HeartBeat Record type sent in the toclient direction!");
1988 SCLogDebug(
"HeartBeat Record type sent in the toserver direction!");
1996 SCLogDebug(
"Encrypted HeartBeat Request In-flight. Storing len %u",
2001 payload_len = (uint16_t)(*input << 8) | *(input + 1);
2006 SCLogDebug(
"We have a short record in HeartBeat Request");
2014 if (padding_len < 16) {
2015 SCLogDebug(
"We have a short record in HeartBeat Request");
2029 SCLogDebug(
"Multiple in-flight server initiated HeartBeats");
2035 SCLogDebug(
"Multiple in-flight client initiated HeartBeats");
2053 SCLogDebug(
"My heart is bleeding.. OpenSSL HeartBleed response (%u)",
2072 static int SSLv3ParseRecord(uint8_t direction,
SSLState *ssl_state,
2073 const uint8_t *input, uint32_t input_len)
2075 const uint8_t *initial_input = input;
2077 if (input_len == 0) {
2081 uint8_t skip_version = 0;
2085 if (direction == 0) {
2099 if (input_len >= 5) {
2101 if (!skip_version) {
2110 if (--input_len == 0)
2116 if (!skip_version) {
2121 if (--input_len == 0)
2126 if (!skip_version) {
2131 if (--input_len == 0)
2137 if (--input_len == 0)
2143 if (--input_len == 0)
2151 return (
int)(input - initial_input);
2154 static int SSLv2ParseRecord(uint8_t direction,
SSLState *ssl_state,
2155 const uint8_t *input, uint32_t input_len)
2157 const uint8_t *initial_input = input;
2159 if (input_len == 0) {
2174 if (--input_len == 0)
2181 if (--input_len == 0)
2188 if (--input_len == 0)
2205 if (--input_len == 0)
2212 if (--input_len == 0)
2219 if (--input_len == 0)
2226 if (--input_len == 0)
2235 return (
int)(input - initial_input);
2240 const StreamSlice stream_slice)
2242 const uint8_t *initial_input = input;
2244 if (ssl_state->curr_connp->bytes_processed == 0) {
2245 if (input[0] & 0x80) {
2246 ssl_state->curr_connp->record_lengths_length = 2;
2248 ssl_state->curr_connp->record_lengths_length = 3;
2256 SCLogDebug(
"direction %u ssl_state->curr_connp->record_lengths_length + 1 %u, "
2257 "ssl_state->curr_connp->bytes_processed %u",
2258 direction, ssl_state->curr_connp->record_lengths_length + 1,
2259 ssl_state->curr_connp->bytes_processed);
2262 if (ssl_state->curr_connp->bytes_processed <
2263 (ssl_state->curr_connp->record_lengths_length + 1)) {
2264 const int retval = SSLv2ParseRecord(direction, ssl_state, input, input_len);
2266 ssl_state->curr_connp->record_length);
2267 if (retval < 0 || retval > (
int)input_len) {
2274 ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length,
2283 if (ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length >
2284 input_len + ssl_state->curr_connp->bytes_processed) {
2285 uint32_t
needed = ssl_state->curr_connp->record_length;
2286 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2287 ssl_state->curr_connp->record_length, input_len, (uint32_t)(input - initial_input),
2292 if (input_len == 0) {
2297 if (ssl_state->curr_connp->record_length == 0) {
2304 if (ssl_state->curr_connp->record_lengths_length == 0) {
2305 SCLogDebug(
"SSLv2 record lengths length is zero");
2310 switch (ssl_state->curr_connp->content_type) {
2312 SCLogDebug(
"SSLV2_MT_ERROR msg_type received. Error encountered "
2313 "in establishing the sslv2 session, may be version");
2319 if (input_len < 6) {
2327 const uint16_t
version = (uint16_t)(input[0] << 8) | input[1];
2329 ssl_state->curr_connp->version =
version;
2330 uint16_t session_id_length = (input[5]) | (uint16_t)(input[4] << 8);
2333 ssl_state->curr_connp->bytes_processed += 6;
2334 if (session_id_length == 0) {
2341 SCLogDebug(
"Client hello is not seen before master key "
2349 if (direction == 1) {
2350 SCLogDebug(
"Incorrect SSL Record type sent in the toclient "
2359 if (direction == 0 &&
2360 !(ssl_state->curr_connp->content_type &
2362 SCLogDebug(
"Incorrect SSL Record type sent in the toserver "
2373 if (direction == 0) {
2376 SCLogDebug(
"SSLv2 client side has started the encryption");
2379 SCLogDebug(
"SSLv2 client side has started the encryption");
2383 SCLogDebug(
"SSLv2 Server side has started the encryption");
2398 SCLogDebug(
"SSLv2 No reassembly & inspection has been set");
2411 ssl_state->flags |= ssl_state->current_flags;
2413 if (input_len + ssl_state->curr_connp->bytes_processed >=
2414 (ssl_state->curr_connp->record_length +
2415 ssl_state->curr_connp->record_lengths_length)) {
2418 uint32_t diff = ssl_state->curr_connp->record_length +
2419 ssl_state->curr_connp->record_lengths_length + -
2420 ssl_state->curr_connp->bytes_processed;
2428 ssl_state->curr_connp->bytes_processed += input_len;
2435 const StreamSlice stream_slice)
2437 uint32_t parsed = 0;
2438 uint32_t record_len;
2439 const bool first_call = (ssl_state->curr_connp->bytes_processed == 0);
2442 const uint16_t prev_version = ssl_state->curr_connp->version;
2444 int retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
2445 if (retval < 0 || retval > (
int)input_len) {
2453 SCLogDebug(
"%s input %p record_length %u", (direction == 0) ?
"toserver" :
"toclient",
2454 input, ssl_state->curr_connp->record_length);
2467 "incomplete header, return %u bytes consumed and wait for more data", parsed);
2474 record_len =
MIN(input_len - parsed, ssl_state->curr_connp->record_length);
2476 "record_len %u (input_len %u, parsed %u, ssl_state->curr_connp->record_length %u)",
2477 record_len, input_len, parsed, ssl_state->curr_connp->record_length);
2479 bool unknown_record =
false;
2480 switch (ssl_state->curr_connp->content_type) {
2488 unknown_record =
true;
2495 if (unknown_record) {
2499 ssl_state->curr_connp->bytes_processed = 0;
2500 ssl_state->curr_connp->content_type = 0;
2501 ssl_state->curr_connp->record_length = 0;
2503 ssl_state->curr_connp->version = prev_version;
2507 if (unknown_record) {
2515 if (ssl_state->curr_connp->record_length == 0) {
2521 if (!TLSVersionValid(ssl_state->curr_connp->version)) {
2522 SCLogDebug(
"ssl_state->curr_connp->version %04x", ssl_state->curr_connp->version);
2536 record_len = (ssl_state->curr_connp->record_length +
SSLV3_RECORD_HDR_LEN)- ssl_state->curr_connp->bytes_processed;
2537 record_len =
MIN(input_len, record_len);
2539 SCLogDebug(
"record length %u processed %u got %u",
2540 ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed, record_len);
2543 if (ssl_state->curr_connp->record_length > input_len - parsed) {
2549 uint32_t
needed = ssl_state->curr_connp->record_length;
2550 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2551 ssl_state->curr_connp->record_length, input_len, parsed,
needed);
2557 if (record_len == 0) {
2562 ssl_state->curr_connp->record_length, direction,
TLS_FRAME_DATA);
2564 switch (ssl_state->curr_connp->content_type) {
2580 int retval = SSLv3ParseAlertProtocol(ssl_state, input + parsed, record_len, direction);
2601 SCLogDebug(
"setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
2609 SCLogDebug(
"setting APP_LAYER_PARSER_NO_REASSEMBLY");
2634 if (ssl_state->curr_connp->record_length < 4) {
2637 SCLogDebug(
"record len < 4 => %u", ssl_state->curr_connp->record_length);
2641 int retval = SSLv3ParseHandshakeProtocol(ssl_state, input + parsed,
2642 record_len, direction);
2644 if (retval < 0 || retval > (
int)record_len) {
2656 int retval = SSLv3ParseHeartbeatProtocol(ssl_state, input + parsed,
2657 record_len, direction);
2671 parsed += record_len;
2672 ssl_state->curr_connp->bytes_processed += record_len;
2674 if (ssl_state->curr_connp->bytes_processed >=
2678 ssl_state->f, direction == 0 ? STREAM_TOSERVER : STREAM_TOCLIENT);
2709 static AppLayerResult SSLDecode(
Flow *f, uint8_t direction,
void *alstate,
2713 ssl_state->
tx_data.updated_tc =
true;
2714 ssl_state->
tx_data.updated_ts =
true;
2715 uint32_t counter = 0;
2717 const uint8_t *input = StreamSliceGetData(&stream_slice);
2718 const uint8_t *init_input = input;
2719 int32_t input_len = (int32_t)StreamSliceGetDataLen(&stream_slice);
2721 if ((input == NULL || input_len == 0) &&
2728 }
else if (input == NULL || input_len == 0) {
2744 while (input_len > 0) {
2745 if (counter > max_records) {
2746 SCLogDebug(
"Looks like we have looped quite a bit. Reset state "
2747 "and get out of here");
2758 if ((input[0] & 0x80) || (input[0] & 0x40)) {
2767 SCLogDebug(
"record %u: bytes_processed %u, version %02X, input_len %u", counter,
2774 SCLogDebug(
"Continuing parsing SSLv2 record");
2777 SSLv2Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2780 SCLogDebug(
"Error parsing SSLv2. Resetting parser "
2781 "state. Let's get outta here");
2788 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2789 (uintmax_t)(input - init_input), r.
needed);
2794 SCLogDebug(
"SSLv2 decoder consumed %d bytes: %u left", r.
retval, input_len);
2797 SCLogDebug(
"New TLS record: record_length %u",
2800 SCLogDebug(
"Continuing parsing TLS record: record_length %u, bytes_processed %u",
2804 SSLv3Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2807 SCLogDebug(
"Error parsing TLS. Resetting parser "
2808 "state. Let's get outta here");
2813 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2814 (uintmax_t)(input - init_input), r.
needed);
2853 StreamSlice stream_slice,
void *local_data)
2855 return SSLDecode(f, 0 , alstate, pstate, stream_slice);
2859 StreamSlice stream_slice,
void *local_data)
2861 return SSLDecode(f, 1 , alstate, pstate, stream_slice);
2868 static void *SSLStateAlloc(
void *orig_state,
AppProto proto_orig)
2882 return (
void *)ssl_state;
2899 static void SSLStateFree(
void *p)
2948 SCAppLayerTxDataCleanup(&ssl_state->
tx_data);
2982 static void SSLStateTransactionFree(
void *state, uint64_t tx_id)
2987 static AppProto SSLProbingParser(
Flow *f, uint8_t direction,
2988 const uint8_t *input, uint32_t ilen, uint8_t *rdir)
2996 if ((input[0] & 0x80) && (input[2] == 0x01)) {
3003 static int SSLStateGetFrameIdByName(
const char *frame_name)
3012 static const char *SSLStateGetFrameNameById(
const uint8_t frame_id)
3018 static int SSLStateGetEventInfo(
3019 const char *event_name, uint8_t *event_id, AppLayerEventType *event_type)
3022 *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
3028 static int SSLStateGetEventInfoById(
3029 uint8_t event_id,
const char **event_name, AppLayerEventType *event_type)
3032 if (*event_name == NULL) {
3034 "ssl's enum map table.",
3040 *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
3045 static int SSLRegisterPatternsForProtocolDetection(
void)
3048 STREAM_TOSERVER, SSLProbingParser, 0, 3) < 0) {
3054 "|01 03 00|", 3, 0, STREAM_TOSERVER) < 0)
3059 "|16 03 00|", 3, 0, STREAM_TOSERVER) < 0)
3066 "|01 03 01|", 3, 0, STREAM_TOSERVER) < 0)
3071 "|16 03 01|", 3, 0, STREAM_TOSERVER) < 0)
3078 "|01 03 02|", 3, 0, STREAM_TOSERVER) < 0)
3083 "|16 03 02|", 3, 0, STREAM_TOSERVER) < 0)
3090 "|01 03 03|", 3, 0, STREAM_TOSERVER) < 0)
3095 "|16 03 03|", 3, 0, STREAM_TOSERVER) < 0)
3103 "|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
3108 "|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
3113 "|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
3120 "|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
3125 "|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
3130 "|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
3137 "|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
3142 "|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
3147 "|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
3154 "|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
3159 "|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
3164 "|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
3175 "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
3180 "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
3190 static void CheckJA3Enabled(
void)
3192 const char *strval = NULL;
3195 if (
ConfGet(
"app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
3197 }
else if (strcmp(strval,
"auto") == 0) {
3215 static void CheckJA4Enabled(
void)
3217 const char *strval = NULL;
3220 if (
ConfGet(
"app-layer.protocols.tls.ja4-fingerprints", &strval) != 1) {
3222 }
else if (strcmp(strval,
"auto") == 0) {
3244 const char *proto_name =
"tls";
3252 if (SSLRegisterPatternsForProtocolDetection() < 0)
3261 SSLProbingParser, NULL);
3266 SSLProbingParser, NULL) == 0) {
3268 "enabling TLS detection on port 443.");
3274 SSLProbingParser, NULL);
3278 SCLogConfig(
"Protocol detection and parser disabled for %s protocol",
3285 SSLParseClientRecord);
3288 SSLParseServerRecord);
3291 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetFrameIdByName, SSLStateGetFrameNameById);
3313 if (enc_handle != NULL && enc_handle->
val != NULL) {
3314 SCLogDebug(
"have app-layer.protocols.tls.encryption-handling = %s", enc_handle->
val);
3315 if (strcmp(enc_handle->
val,
"full") == 0) {
3317 }
else if (strcmp(enc_handle->
val,
"bypass") == 0) {
3319 }
else if (strcmp(enc_handle->
val,
"default") == 0) {
3326 if (
ConfGetNode(
"app-layer.protocols.tls.no-reassemble") == NULL) {
3328 if (
ConfGetBool(
"tls.no-reassemble", &value) == 1 && value == 1)
3332 if (
ConfGetBool(
"app-layer.protocols.tls.no-reassemble", &value) == 1 && value == 1)
3347 SCLogWarning(
"MD5 calculation has been disabled, disabling JA3");
3351 SCLogWarning(
"Hashing has been disabled, disabling JA4");
3365 SCLogConfig(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);