60 "client_handshake_done",
88 "server_handshake_done",
153 {
"CERTIFICATE_INVALID_ALGORITHMIDENTIFIER",
188 #define SSL_CONFIG_DEFAULT_JA3 0
190 #define SSL_CONFIG_DEFAULT_JA4 0
212 #define SSLV3_CHANGE_CIPHER_SPEC 20
213 #define SSLV3_ALERT_PROTOCOL 21
214 #define SSLV3_HANDSHAKE_PROTOCOL 22
215 #define SSLV3_APPLICATION_PROTOCOL 23
216 #define SSLV3_HEARTBEAT_PROTOCOL 24
219 #define SSLV3_HS_HELLO_REQUEST 0
220 #define SSLV3_HS_CLIENT_HELLO 1
221 #define SSLV3_HS_SERVER_HELLO 2
222 #define SSLV3_HS_NEW_SESSION_TICKET 4
223 #define SSLV3_HS_CERTIFICATE 11
224 #define SSLV3_HS_SERVER_KEY_EXCHANGE 12
225 #define SSLV3_HS_CERTIFICATE_REQUEST 13
226 #define SSLV3_HS_SERVER_HELLO_DONE 14
227 #define SSLV3_HS_CERTIFICATE_VERIFY 15
228 #define SSLV3_HS_CLIENT_KEY_EXCHANGE 16
229 #define SSLV3_HS_FINISHED 20
230 #define SSLV3_HS_CERTIFICATE_URL 21
231 #define SSLV3_HS_CERTIFICATE_STATUS 22
234 #define SSLV2_MT_ERROR 0
235 #define SSLV2_MT_CLIENT_HELLO 1
236 #define SSLV2_MT_CLIENT_MASTER_KEY 2
237 #define SSLV2_MT_CLIENT_FINISHED 3
238 #define SSLV2_MT_SERVER_HELLO 4
239 #define SSLV2_MT_SERVER_VERIFY 5
240 #define SSLV2_MT_SERVER_FINISHED 6
241 #define SSLV2_MT_REQUEST_CERTIFICATE 7
242 #define SSLV2_MT_CLIENT_CERTIFICATE 8
244 #define SSLV3_RECORD_HDR_LEN 5
246 #define SSLV3_RECORD_MAX_LEN ((1 << 14) + 1024)
248 #define SSLV3_CLIENT_HELLO_VERSION_LEN 2
249 #define SSLV3_CLIENT_HELLO_RANDOM_LEN 32
252 #define TLS_HB_REQUEST 1
253 #define TLS_HB_RESPONSE 2
255 #define SSL_RECORD_MINIMUM_LENGTH 6
257 #define SHA1_STRING_LENGTH 60
259 #define HAS_SPACE(n) ((uint64_t)(input - initial_input) + (uint64_t)(n) <= (uint64_t)(input_len))
265 #define SSL_DECODER_ERROR(e) \
266 (struct SSLDecoderResult) \
270 #define SSL_DECODER_OK(c) \
271 (struct SSLDecoderResult) \
275 #define SSL_DECODER_INCOMPLETE(c, n) \
276 (struct SSLDecoderResult) \
281 static inline int SafeMemcpy(
void *
dst,
size_t dst_offset,
size_t dst_size,
282 const void *
src,
size_t src_offset,
size_t src_size,
size_t src_tocopy)
WARN_UNUSED;
284 static inline int SafeMemcpy(
void *
dst,
size_t dst_offset,
size_t dst_size,
285 const void *
src,
size_t src_offset,
size_t src_size,
size_t src_tocopy)
292 if (dst_offset < dst_size && src_offset < src_size &&
293 src_tocopy <= (src_size - src_offset) &&
294 src_tocopy <= (dst_size - dst_offset)) {
295 memcpy(
dst + dst_offset,
src + src_offset, src_tocopy);
301 #ifdef DEBUG_VALIDATION
302 #define ValidateRecordState(connp) \
304 DEBUG_VALIDATE_BUG_ON(((connp)->record_length + SSLV3_RECORD_HDR_LEN) < \
305 (connp)->bytes_processed); \
308 #define ValidateRecordState(...)
311 #define SSLParserHSReset(connp) \
313 (connp)->handshake_type = 0; \
314 (connp)->message_length = 0; \
317 #define SSLParserReset(state) \
319 SCLogDebug("resetting state"); \
320 (state)->curr_connp->bytes_processed = 0; \
321 SSLParserHSReset((state)->curr_connp); \
324 #define SSLSetEvent(ssl_state, event) \
326 SCLogDebug("setting event %u", (event)); \
327 if ((ssl_state) == NULL) { \
328 SCLogDebug("could not set decoder event %u", event); \
330 AppLayerDecoderEventsSetEventRaw(&(ssl_state)->tx_data.events, (event)); \
331 (ssl_state)->events++; \
337 static void *SSLGetTx(
void *state, uint64_t tx_id)
343 static uint64_t SSLGetTxCnt(
void *state)
356 SCLogDebug(
"toserver: state updated to %u from %u", s, old);
367 SCLogDebug(
"toclient: state updated to %u from %u", s, old);
371 static int SSLGetAlstateProgress(
void *tx, uint8_t direction)
374 if (direction & STREAM_TOCLIENT) {
387 static AppLayerStateData *SSLGetStateData(
void *vstate)
393 static void TlsDecodeHSCertificateErrSetEvent(
SSLState *ssl_state, uint32_t err)
436 static inline int TlsDecodeHSCertificateFingerprint(
437 SSLStateConnp *connp,
const uint8_t *input, uint32_t cert_len)
447 if (SCSha1HashBuffer(input, cert_len, hash,
sizeof(hash)) == 1) {
454 static inline int TlsDecodeHSCertificateAddCertToChain(
455 SSLStateConnp *connp,
const uint8_t *input, uint32_t cert_len)
469 const uint8_t *
const initial_input,
const uint32_t input_len,
const int certn)
471 const uint8_t *input = (uint8_t *)initial_input;
472 uint32_t err_code = 0;
479 uint32_t cert_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
488 x509 = SCX509Decode(input, cert_len, &err_code);
490 TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
494 char *
str = SCX509GetSubject(x509);
501 str = SCX509GetIssuer(x509);
514 sans[i] = SCX509GetSubjectAltNameAt(x509, i);
517 str = SCX509GetSerial(x509);
533 rc = TlsDecodeHSCertificateFingerprint(connp, input, cert_len);
535 SCLogDebug(
"TlsDecodeHSCertificateFingerprint failed with %d", rc);
540 rc = TlsDecodeHSCertificateAddCertToChain(connp, input, cert_len);
542 SCLogDebug(
"TlsDecodeHSCertificateAddCertToChain failed with %d", rc);
548 return (
int)(input - initial_input);
552 TlsDecodeHSCertificateErrSetEvent(ssl_state, err_code);
556 SSLStateCertSANFree(connp);
571 const uint8_t *
const initial_input,
const uint32_t input_len)
573 const uint8_t *input = (uint8_t *)initial_input;
578 const uint32_t cert_chain_len = *input << 16 | *(input + 1) << 8 | *(input + 2);
597 uint32_t processed_len = 0;
599 while (processed_len < cert_chain_len) {
600 int rc = TlsDecodeHSCertificate(ssl_state, connp, connp->
certs_buffer + processed_len,
606 if (processed_len + (uint32_t)rc > cert_chain_len) {
610 processed_len += (uint32_t)rc;
613 return processed_len + 3;
627 static inline int TLSDecodeValueIsGREASE(
const uint16_t value)
653 static inline int TLSDecodeHSHelloVersion(
SSLState *ssl_state,
654 const uint8_t *
const initial_input,
655 const uint32_t input_len)
657 uint8_t *input = (uint8_t *)initial_input;
666 uint16_t
version = (uint16_t)(*input << 8) | *(input + 1);
708 return (
int)(input - initial_input);
711 static inline int TLSDecodeHSHelloRandom(
SSLState *ssl_state,
712 const uint8_t *
const initial_input,
713 const uint32_t input_len)
715 uint8_t *input = (uint8_t *)initial_input;
735 return (
int)(input - initial_input);
738 static inline int TLSDecodeHSHelloSessionID(
SSLState *ssl_state,
739 const uint8_t *
const initial_input,
740 const uint32_t input_len)
742 uint8_t *input = (uint8_t *)initial_input;
747 uint8_t session_id_length = *input;
761 input, 0, input_len, session_id_length) != 0) {
778 input += session_id_length;
780 return (
int)(input - initial_input);
789 static inline int TLSDecodeHSHelloCipherSuites(
SSLState *ssl_state,
790 const uint8_t *
const initial_input,
791 const uint32_t input_len)
793 const uint8_t *input = initial_input;
798 uint16_t cipher_suites_length;
801 cipher_suites_length = 2;
803 cipher_suites_length = (uint16_t)(*input << 8) | *(input + 1);
813 if ((cipher_suites_length % 2) != 0) {
817 const bool enable_ja3 =
824 if (ja3_cipher_suites == NULL)
828 uint16_t processed_len = 0;
830 while (processed_len < cipher_suites_length) {
838 uint16_t cipher_suite = (uint16_t)(*input << 8) | *(input + 1);
841 if (TLSDecodeValueIsGREASE(cipher_suite) != 1) {
844 SCTLSHandshakeAddCipher(ssl_state->
curr_connp->
hs, cipher_suite);
863 return (
int)(input - initial_input);
872 static inline int TLSDecodeHSHelloCompressionMethods(
SSLState *ssl_state,
873 const uint8_t *
const initial_input,
874 const uint32_t input_len)
876 const uint8_t *input = initial_input;
885 uint8_t compression_methods_length = *input;
888 if (!(
HAS_SPACE(compression_methods_length)))
891 input += compression_methods_length;
894 return (
int)(input - initial_input);
903 static inline int TLSDecodeHSHelloExtensionSni(
SSLState *ssl_state,
904 const uint8_t *
const initial_input,
905 const uint32_t input_len)
907 uint8_t *input = (uint8_t *)initial_input;
922 uint8_t sni_type = *input;
937 uint16_t sni_len = (uint16_t)(*input << 8) | *(input + 1);
943 if (!(
HAS_SPACE(sni_len)) || sni_len > 255 || sni_len == 0) {
956 return (
int)(input - initial_input);
959 const size_t sni_strlen = sni_len + 1;
964 const size_t consumed = input - initial_input;
966 initial_input, consumed, input_len, sni_len) != 0) {
975 return (
int)(input - initial_input);
986 static inline int TLSDecodeHSHelloExtensionSupportedVersions(
SSLState *ssl_state,
987 const uint8_t *
const initial_input,
988 const uint32_t input_len)
990 const uint8_t *input = initial_input;
1000 uint8_t supported_ver_len = *input;
1003 if (supported_ver_len < 2)
1004 goto invalid_length;
1007 goto invalid_length;
1012 while (i + 1 < (uint16_t)supported_ver_len) {
1013 uint16_t ver = (uint16_t)(input[i] << 8) | input[i + 1];
1014 if (TLSVersionValid(ver)) {
1016 SCTLSHandshakeSetTLSVersion(ssl_state->
curr_connp->
hs, ver);
1025 input += supported_ver_len;
1029 goto invalid_length;
1031 uint16_t ver = (uint16_t)(*input << 8) | *(input + 1);
1034 (ver > TLS_VERSION_12)) {
1042 return (
int)(input - initial_input);
1052 static inline int TLSDecodeHSHelloExtensionEllipticCurves(
SSLState *ssl_state,
1053 const uint8_t *
const initial_input,
1054 const uint32_t input_len,
1057 const uint8_t *input = initial_input;
1064 goto invalid_length;
1066 uint16_t elliptic_curves_len = (uint16_t)(*input << 8) | *(input + 1);
1070 goto invalid_length;
1073 uint16_t ec_processed_len = 0;
1075 while (ec_processed_len < elliptic_curves_len)
1078 goto invalid_length;
1080 uint16_t elliptic_curve = (uint16_t)(*input << 8) | *(input + 1);
1083 if (TLSDecodeValueIsGREASE(elliptic_curve) != 1) {
1090 ec_processed_len += 2;
1095 input += elliptic_curves_len;
1098 return (
int)(input - initial_input);
1108 static inline int TLSDecodeHSHelloExtensionEllipticCurvePF(
SSLState *ssl_state,
1109 const uint8_t *
const initial_input,
1110 const uint32_t input_len,
1113 const uint8_t *input = initial_input;
1120 goto invalid_length;
1122 uint8_t ec_pf_len = *input;
1126 goto invalid_length;
1129 uint8_t ec_pf_processed_len = 0;
1131 while (ec_pf_processed_len < ec_pf_len)
1133 uint8_t elliptic_curve_pf = *input;
1136 if (TLSDecodeValueIsGREASE(elliptic_curve_pf) != 1) {
1143 ec_pf_processed_len += 1;
1151 return (
int)(input - initial_input);
1161 static inline int TLSDecodeHSHelloExtensionSigAlgorithms(
1162 SSLState *ssl_state,
const uint8_t *
const initial_input,
const uint32_t input_len)
1164 const uint8_t *input = initial_input;
1171 goto invalid_length;
1173 uint16_t sigalgo_len = (uint16_t)(*input << 8) | *(input + 1);
1177 if ((sigalgo_len % 2) != 0) {
1178 goto invalid_length;
1182 goto invalid_length;
1185 uint16_t sigalgo_processed_len = 0;
1186 while (sigalgo_processed_len < sigalgo_len) {
1187 uint16_t sigalgo = (uint16_t)(*input << 8) | *(input + 1);
1189 sigalgo_processed_len += 2;
1191 SCTLSHandshakeAddSigAlgo(ssl_state->
curr_connp->
hs, sigalgo);
1195 input += sigalgo_len;
1198 return (
int)(input - initial_input);
1201 SCLogDebug(
"Signature algorithm list invalid length");
1207 static inline int TLSDecodeHSHelloExtensionALPN(
1208 SSLState *ssl_state,
const uint8_t *
const initial_input,
const uint32_t input_len)
1210 const uint8_t *input = initial_input;
1217 goto invalid_length;
1219 uint16_t alpn_len = (uint16_t)(*input << 8) | *(input + 1);
1223 goto invalid_length;
1227 uint32_t alpn_processed_len = 0;
1228 while (alpn_processed_len < alpn_len) {
1229 uint8_t protolen = *input;
1231 alpn_processed_len += 1;
1234 goto invalid_length;
1238 if (alpn_processed_len + protolen > ((uint32_t)alpn_len)) {
1239 input += alpn_len - alpn_processed_len;
1242 SCTLSHandshakeAddALPN(ssl_state->
curr_connp->
hs, (
const char *)input, protolen);
1244 alpn_processed_len += protolen;
1248 return (
int)(input - initial_input);
1257 static inline int TLSDecodeHSHelloExtensions(
SSLState *ssl_state,
1258 const uint8_t *
const initial_input,
1259 const uint32_t input_len)
1261 const uint8_t *input = initial_input;
1271 JA3Buffer *ja3_elliptic_curves_pf = NULL;
1275 if (ja3_extensions == NULL)
1280 if (ja3_elliptic_curves == NULL)
1284 if (ja3_elliptic_curves_pf == NULL)
1293 uint16_t extensions_len = (uint16_t)(*input << 8) | *(input + 1);
1297 goto invalid_length;
1299 uint16_t processed_len = 0;
1301 while (processed_len < extensions_len)
1304 goto invalid_length;
1306 uint16_t ext_type = (uint16_t)(*input << 8) | *(input + 1);
1310 goto invalid_length;
1312 uint16_t ext_len = (uint16_t)(*input << 8) | *(input + 1);
1316 goto invalid_length;
1322 ret = TLSDecodeHSHelloExtensionSni(ssl_state, input,
1335 ret = TLSDecodeHSHelloExtensionEllipticCurves(ssl_state, input,
1337 ja3_elliptic_curves);
1349 ret = TLSDecodeHSHelloExtensionEllipticCurvePF(ssl_state, input,
1351 ja3_elliptic_curves_pf);
1362 ret = TLSDecodeHSHelloExtensionSigAlgorithms(ssl_state, input, ext_len);
1373 ret = TLSDecodeHSHelloExtensionALPN(ssl_state, input, ext_len);
1397 ret = TLSDecodeHSHelloExtensionSupportedVersions(ssl_state, input,
1428 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1437 if (TLSDecodeValueIsGREASE(ext_type) != 1) {
1438 SCTLSHandshakeAddExtension(ssl_state->
curr_connp->
hs, ext_type);
1442 processed_len += ext_len + 4;
1454 &ja3_elliptic_curves);
1459 &ja3_elliptic_curves_pf);
1465 return (
int)(input - initial_input);
1473 if (ja3_extensions != NULL)
1475 if (ja3_elliptic_curves != NULL)
1477 if (ja3_elliptic_curves_pf != NULL)
1483 static int TLSDecodeHandshakeHello(
SSLState *ssl_state,
1484 const uint8_t *
const input,
1485 const uint32_t input_len)
1488 uint32_t parsed = 0;
1490 ret = TLSDecodeHSHelloVersion(ssl_state, input, input_len);
1496 ret = TLSDecodeHSHelloRandom(ssl_state, input + parsed, input_len - parsed);
1507 ret = TLSDecodeHSHelloSessionID(ssl_state, input + parsed,
1508 input_len - parsed);
1515 ret = TLSDecodeHSHelloCipherSuites(ssl_state, input + parsed,
1516 input_len - parsed);
1527 ret = TLSDecodeHSHelloCompressionMethods(ssl_state, input + parsed,
1528 input_len - parsed);
1535 ret = TLSDecodeHSHelloExtensions(ssl_state, input + parsed,
1536 input_len - parsed);
1553 #ifdef DEBUG_VALIDATION
1563 const uint8_t *
const initial_input,
const uint32_t input_len)
1565 int rc = TlsDecodeHSCertificates(ssl_state, connp, initial_input, input_len);
1570 }
else if (rc < 0) {
1571 SCLogDebug(
"error parsing cert, reset state");
1583 static int SupportedHandshakeType(
const uint8_t
type)
1613 static int SSLv3ParseHandshakeType(
SSLState *ssl_state,
const uint8_t *input,
1614 uint32_t input_len, uint8_t direction)
1616 const uint8_t *initial_input = input;
1625 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1634 rc = TLSDecodeHandshakeHello(ssl_state, input, input_len);
1648 rc = SSLv3ParseHandshakeTypeCertificate(ssl_state,
1688 static int SSLv3ParseHandshakeProtocol(
SSLState *ssl_state,
const uint8_t *input,
1689 uint32_t input_len, uint8_t direction)
1691 const uint8_t *initial_input = input;
1702 SCLogDebug(
"partial handshake record in place");
1705 const uint32_t add =
MIN(need, input_len);
1711 const uint32_t new_size = avail + (4096 - (avail % 4096));
1712 SCLogDebug(
"new_size %u, avail %u", new_size, avail);
1720 SCLogDebug(
"ssl_state->curr_connp->hs_buffer_offset %u "
1721 "ssl_state->curr_connp->hs_buffer_size %u",
1743 SCLogDebug(
"got all data now: handshake_type %u message_length %u",
1774 SCLogDebug(
"input %p input_len %u", input, input_len);
1776 if (input_len < 4) {
1783 SCLogDebug(
"handshake_type %u message len %u input %p input_len %u",
1793 SCLogDebug(
"supported_type %s handshake_type %u/%02x", supported_type ?
"true" :
"false",
1795 if (!supported_type) {
1796 uint32_t avail_record_len =
MIN(input_len, record_len);
1797 input += avail_record_len;
1798 input_len -= avail_record_len;
1812 if (record_len > input_len) {
1813 const uint32_t avail = input_len;
1814 const uint32_t size = avail + (4096 - (avail % 4096));
1815 SCLogDebug(
"initial buffer size %u, based on input %u", size, avail);
1824 if (input_len > 0) {
1832 SCLogDebug(
"opened record buffer %p size %u offset %u type %u msg_size %u",
1839 return (
int)(input - initial_input);
1843 int retval = SSLv3ParseHandshakeType(
1845 if (retval < 0 || retval > (
int)input_len) {
1849 SCLogDebug(
"retval %d input_len %u", retval, input_len);
1851 input_len -= retval;
1857 return (
int)(input - initial_input);
1871 static int SSLv3ParseAlertProtocol(
1872 SSLState *ssl_state,
const uint8_t *input, uint32_t input_len, uint8_t direction)
1874 if (input_len < 2) {
1880 if (input_len == 2) {
1881 uint8_t level = input[0];
1904 static int SSLv3ParseHeartbeatProtocol(
SSLState *ssl_state,
const uint8_t *input,
1905 uint32_t input_len, uint8_t direction)
1909 uint32_t padding_len;
1912 if (input_len < 3) {
1929 SCLogDebug(
"HeartBeat Record type sent in the toclient direction!");
1932 SCLogDebug(
"HeartBeat Record type sent in the toserver direction!");
1940 SCLogDebug(
"Encrypted HeartBeat Request In-flight. Storing len %u",
1945 payload_len = (uint16_t)(*input << 8) | *(input + 1);
1950 SCLogDebug(
"We have a short record in HeartBeat Request");
1958 if (padding_len < 16) {
1959 SCLogDebug(
"We have a short record in HeartBeat Request");
1973 SCLogDebug(
"Multiple in-flight server initiated HeartBeats");
1979 SCLogDebug(
"Multiple in-flight client initiated HeartBeats");
1997 SCLogDebug(
"My heart is bleeding.. OpenSSL HeartBleed response (%u)",
2016 static int SSLv3ParseRecord(uint8_t direction,
SSLState *ssl_state,
2017 const uint8_t *input, uint32_t input_len)
2019 const uint8_t *initial_input = input;
2021 if (input_len == 0) {
2025 uint8_t skip_version = 0;
2029 if (direction == 0) {
2043 if (input_len >= 5) {
2045 if (!skip_version) {
2054 if (--input_len == 0)
2060 if (!skip_version) {
2065 if (--input_len == 0)
2070 if (!skip_version) {
2075 if (--input_len == 0)
2081 if (--input_len == 0)
2087 if (--input_len == 0)
2095 return (
int)(input - initial_input);
2098 static int SSLv2ParseRecord(uint8_t direction,
SSLState *ssl_state,
2099 const uint8_t *input, uint32_t input_len)
2101 const uint8_t *initial_input = input;
2103 if (input_len == 0) {
2118 if (--input_len == 0)
2125 if (--input_len == 0)
2132 if (--input_len == 0)
2149 if (--input_len == 0)
2156 if (--input_len == 0)
2163 if (--input_len == 0)
2170 if (--input_len == 0)
2179 return (
int)(input - initial_input);
2184 const StreamSlice stream_slice)
2186 const uint8_t *initial_input = input;
2188 if (ssl_state->curr_connp->bytes_processed == 0) {
2189 if (input[0] & 0x80) {
2190 ssl_state->curr_connp->record_lengths_length = 2;
2192 ssl_state->curr_connp->record_lengths_length = 3;
2200 SCLogDebug(
"direction %u ssl_state->curr_connp->record_lengths_length + 1 %u, "
2201 "ssl_state->curr_connp->bytes_processed %u",
2202 direction, ssl_state->curr_connp->record_lengths_length + 1,
2203 ssl_state->curr_connp->bytes_processed);
2206 if (ssl_state->curr_connp->bytes_processed <
2207 (ssl_state->curr_connp->record_lengths_length + 1)) {
2208 const int retval = SSLv2ParseRecord(direction, ssl_state, input, input_len);
2210 ssl_state->curr_connp->record_length);
2211 if (retval < 0 || retval > (
int)input_len) {
2218 ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length,
2227 if (ssl_state->curr_connp->record_lengths_length + ssl_state->curr_connp->record_length >
2228 input_len + ssl_state->curr_connp->bytes_processed) {
2229 uint32_t
needed = ssl_state->curr_connp->record_length;
2230 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2231 ssl_state->curr_connp->record_length, input_len, (uint32_t)(input - initial_input),
2236 if (input_len == 0) {
2241 if (ssl_state->curr_connp->record_length == 0) {
2248 if (ssl_state->curr_connp->record_lengths_length == 0) {
2249 SCLogDebug(
"SSLv2 record lengths length is zero");
2254 switch (ssl_state->curr_connp->content_type) {
2256 SCLogDebug(
"SSLV2_MT_ERROR msg_type received. Error encountered "
2257 "in establishing the sslv2 session, may be version");
2263 if (input_len < 6) {
2272 const uint16_t
version = (uint16_t)(input[0] << 8) | input[1];
2274 ssl_state->curr_connp->version =
version;
2275 uint16_t session_id_length = (input[5]) | (uint16_t)(input[4] << 8);
2278 ssl_state->curr_connp->bytes_processed += 6;
2279 if (session_id_length == 0) {
2286 SCLogDebug(
"Client hello is not seen before master key "
2294 if (direction == 1) {
2295 SCLogDebug(
"Incorrect SSL Record type sent in the toclient "
2305 if (direction == 0 &&
2306 !(ssl_state->curr_connp->content_type &
2308 SCLogDebug(
"Incorrect SSL Record type sent in the toserver "
2319 if (direction == 0) {
2322 SCLogDebug(
"SSLv2 client side has started the encryption");
2325 SCLogDebug(
"SSLv2 client side has started the encryption");
2329 SCLogDebug(
"SSLv2 Server side has started the encryption");
2344 SCLogDebug(
"SSLv2 No reassembly & inspection has been set");
2358 ssl_state->flags |= ssl_state->current_flags;
2360 if (input_len + ssl_state->curr_connp->bytes_processed >=
2361 (ssl_state->curr_connp->record_length +
2362 ssl_state->curr_connp->record_lengths_length)) {
2365 uint32_t diff = ssl_state->curr_connp->record_length +
2366 ssl_state->curr_connp->record_lengths_length + -
2367 ssl_state->curr_connp->bytes_processed;
2375 ssl_state->curr_connp->bytes_processed += input_len;
2382 const StreamSlice stream_slice)
2384 uint32_t parsed = 0;
2385 uint32_t record_len;
2386 const bool first_call = (ssl_state->curr_connp->bytes_processed == 0);
2389 const uint16_t prev_version = ssl_state->curr_connp->version;
2391 int retval = SSLv3ParseRecord(direction, ssl_state, input, input_len);
2392 if (retval < 0 || retval > (
int)input_len) {
2400 SCLogDebug(
"%s input %p record_length %u", (direction == 0) ?
"toserver" :
"toclient",
2401 input, ssl_state->curr_connp->record_length);
2414 "incomplete header, return %u bytes consumed and wait for more data", parsed);
2421 record_len =
MIN(input_len - parsed, ssl_state->curr_connp->record_length);
2423 "record_len %u (input_len %u, parsed %u, ssl_state->curr_connp->record_length %u)",
2424 record_len, input_len, parsed, ssl_state->curr_connp->record_length);
2426 bool unknown_record =
false;
2427 switch (ssl_state->curr_connp->content_type) {
2435 unknown_record =
true;
2441 if (prev_version == TLS_VERSION_10 || prev_version == TLS_VERSION_11) {
2442 if (unknown_record) {
2446 ssl_state->curr_connp->bytes_processed = 0;
2447 ssl_state->curr_connp->content_type = 0;
2448 ssl_state->curr_connp->record_length = 0;
2450 ssl_state->curr_connp->version = prev_version;
2454 if (unknown_record) {
2462 if (ssl_state->curr_connp->record_length == 0) {
2468 if (!TLSVersionValid(ssl_state->curr_connp->version)) {
2469 SCLogDebug(
"ssl_state->curr_connp->version %04x", ssl_state->curr_connp->version);
2483 record_len = (ssl_state->curr_connp->record_length +
SSLV3_RECORD_HDR_LEN)- ssl_state->curr_connp->bytes_processed;
2484 record_len =
MIN(input_len, record_len);
2486 SCLogDebug(
"record length %u processed %u got %u",
2487 ssl_state->curr_connp->record_length, ssl_state->curr_connp->bytes_processed, record_len);
2490 if (ssl_state->curr_connp->record_length > input_len - parsed) {
2496 uint32_t
needed = ssl_state->curr_connp->record_length;
2497 SCLogDebug(
"record len %u input_len %u parsed %u: need %u bytes more data",
2498 ssl_state->curr_connp->record_length, input_len, parsed,
needed);
2504 if (record_len == 0) {
2509 ssl_state->curr_connp->record_length, direction,
TLS_FRAME_DATA);
2511 switch (ssl_state->curr_connp->content_type) {
2530 int retval = SSLv3ParseAlertProtocol(ssl_state, input + parsed, record_len, direction);
2548 if (ssl_state->curr_connp == &ssl_state->client_connp) {
2555 SCLogDebug(
"setting APP_LAYER_PARSER_NO_INSPECTION_PAYLOAD");
2563 SCLogDebug(
"setting APP_LAYER_PARSER_NO_REASSEMBLY");
2579 (ssl_state->client_connp.version > TLS_VERSION_12) &&
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 ssl_state->
tx_data.updated_tc =
true;
2668 ssl_state->
tx_data.updated_ts =
true;
2669 uint32_t counter = 0;
2671 const uint8_t *input = StreamSliceGetData(&stream_slice);
2672 const uint8_t *init_input = input;
2673 int32_t input_len = (int32_t)StreamSliceGetDataLen(&stream_slice);
2675 if ((input == NULL || input_len == 0) &&
2685 }
else if (input == NULL || input_len == 0) {
2701 while (input_len > 0) {
2702 if (counter > max_records) {
2703 SCLogDebug(
"Looks like we have looped quite a bit. Reset state "
2704 "and get out of here");
2715 if ((input[0] & 0x80) || (input[0] & 0x40)) {
2724 SCLogDebug(
"record %u: bytes_processed %u, version %02X, input_len %u", counter,
2731 SCLogDebug(
"Continuing parsing SSLv2 record");
2734 SSLv2Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2737 SCLogDebug(
"Error parsing SSLv2. Resetting parser "
2738 "state. Let's get outta here");
2745 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2746 (uintmax_t)(input - init_input), r.
needed);
2751 SCLogDebug(
"SSLv2 decoder consumed %d bytes: %u left", r.
retval, input_len);
2754 SCLogDebug(
"New TLS record: record_length %u",
2757 SCLogDebug(
"Continuing parsing TLS record: record_length %u, bytes_processed %u",
2761 SSLv3Decode(direction, ssl_state, pstate, input, input_len, stream_slice);
2764 SCLogDebug(
"Error parsing TLS. Resetting parser "
2765 "state. Let's get outta here");
2770 SCLogDebug(
"returning consumed %" PRIuMAX
" needed %u",
2771 (uintmax_t)(input - init_input), r.
needed);
2813 StreamSlice stream_slice,
void *local_data)
2815 return SSLDecode(f, 0 , alstate, pstate, stream_slice);
2819 StreamSlice stream_slice,
void *local_data)
2821 return SSLDecode(f, 1 , alstate, pstate, stream_slice);
2828 static void *SSLStateAlloc(
void *orig_state,
AppProto proto_orig)
2842 return (
void *)ssl_state;
2859 static void SSLStateFree(
void *p)
2910 SCAppLayerTxDataCleanup(&ssl_state->
tx_data);
2932 static void SSLStateTransactionFree(
void *state, uint64_t tx_id)
2937 static AppProto SSLProbingParser(
Flow *f, uint8_t direction,
2938 const uint8_t *input, uint32_t ilen, uint8_t *rdir)
2946 if ((input[0] & 0x80) && (input[2] == 0x01)) {
2953 static int SSLStateGetStateIdByName(
const char *
name,
const uint8_t direction)
2956 direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
2965 static const char *SSLStateGetStateNameById(
const int id,
const uint8_t direction)
2968 direction == STREAM_TOSERVER ? tls_state_client_table : tls_state_server_table;
2973 static int SSLStateGetFrameIdByName(
const char *frame_name)
2982 static const char *SSLStateGetFrameNameById(
const uint8_t frame_id)
2988 static int SSLStateGetEventInfo(
2989 const char *event_name, uint8_t *event_id, AppLayerEventType *event_type)
2992 *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
2998 static int SSLStateGetEventInfoById(
2999 uint8_t event_id,
const char **event_name, AppLayerEventType *event_type)
3002 if (*event_name == NULL) {
3004 "ssl's enum map table.",
3010 *event_type = APP_LAYER_EVENT_TYPE_TRANSACTION;
3015 static int SSLRegisterPatternsForProtocolDetection(
void)
3018 STREAM_TOSERVER, SSLProbingParser, 0, 3) < 0) {
3024 "|01 03 00|", 3, 0, STREAM_TOSERVER) < 0)
3029 "|16 03 00|", 3, 0, STREAM_TOSERVER) < 0)
3036 "|01 03 01|", 3, 0, STREAM_TOSERVER) < 0)
3041 "|16 03 01|", 3, 0, STREAM_TOSERVER) < 0)
3048 "|01 03 02|", 3, 0, STREAM_TOSERVER) < 0)
3053 "|16 03 02|", 3, 0, STREAM_TOSERVER) < 0)
3060 "|01 03 03|", 3, 0, STREAM_TOSERVER) < 0)
3065 "|16 03 03|", 3, 0, STREAM_TOSERVER) < 0)
3073 "|15 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
3078 "|16 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
3083 "|17 03 00|", 3, 0, STREAM_TOCLIENT) < 0)
3090 "|15 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
3095 "|16 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
3100 "|17 03 01|", 3, 0, STREAM_TOCLIENT) < 0)
3107 "|15 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
3112 "|16 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
3117 "|17 03 02|", 3, 0, STREAM_TOCLIENT) < 0)
3124 "|15 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
3129 "|16 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
3134 "|17 03 03|", 3, 0, STREAM_TOCLIENT) < 0)
3145 "|01 03 00|", 5, 2, STREAM_TOSERVER) < 0)
3150 "|00 02|", 7, 5, STREAM_TOCLIENT) < 0)
3160 static void CheckJA3Enabled(
void)
3162 const char *strval = NULL;
3165 if (
SCConfGet(
"app-layer.protocols.tls.ja3-fingerprints", &strval) != 1) {
3167 }
else if (strcmp(strval,
"auto") == 0) {
3185 static void CheckJA4Enabled(
void)
3187 const char *strval = NULL;
3189 int enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3190 if (
SCConfGet(
"app-layer.protocols.tls.ja4-fingerprints", &strval) != 1) {
3191 enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3192 }
else if (strcmp(strval,
"auto") == 0) {
3193 enable_ja4 = SSL_CONFIG_DEFAULT_JA4;
3214 const char *proto_name =
"tls";
3222 if (SSLRegisterPatternsForProtocolDetection() < 0)
3231 SSLProbingParser, NULL);
3236 SSLProbingParser, NULL) == 0) {
3238 "enabling TLS detection on port 443.");
3244 SSLProbingParser, NULL);
3248 SCLogConfig(
"Protocol detection and parser disabled for %s protocol",
3255 SSLParseClientRecord);
3258 SSLParseServerRecord);
3260 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetStateIdByName, SSLStateGetStateNameById);
3262 IPPROTO_TCP,
ALPROTO_TLS, SSLStateGetFrameIdByName, SSLStateGetFrameNameById);
3284 if (enc_handle != NULL && enc_handle->
val != NULL) {
3285 SCLogDebug(
"have app-layer.protocols.tls.encryption-handling = %s", enc_handle->
val);
3286 if (strcmp(enc_handle->
val,
"full") == 0) {
3288 }
else if (strcmp(enc_handle->
val,
"bypass") == 0) {
3290 }
else if (strcmp(enc_handle->
val,
"track-only") == 0) {
3292 }
else if (strcmp(enc_handle->
val,
"default") == 0) {
3293 SCLogWarning(
"app-layer.protocols.tls.encryption-handling = default is deprecated "
3294 "and will be removed in Suricata 9, use \"track-only\" instead, "
3295 "(see ticket #7642)");
3302 if (
SCConfGetNode(
"app-layer.protocols.tls.no-reassemble") == NULL) {
3304 if (
SCConfGetBool(
"tls.no-reassemble", &value) == 1 && value == 1)
3308 if (
SCConfGetBool(
"app-layer.protocols.tls.no-reassemble", &value) == 1 &&
3324 SCLogWarning(
"MD5 calculation has been disabled, disabling JA3");
3328 SCLogWarning(
"Hashing has been disabled, disabling JA4");
3342 SCLogConfig(
"Parser disabled for %s protocol. Protocol detection still on.", proto_name);