suricata
app-layer-ssl.h
Go to the documentation of this file.
1 /* Copyright (C) 2007-2022 Open Information Security Foundation
2  *
3  * You can copy, redistribute or modify this Program under the terms of
4  * the GNU General Public License version 2 as published by the Free
5  * Software Foundation.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * version 2 along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
15  * 02110-1301, USA.
16  */
17 
18 /**
19  * \file
20  *
21  * \author Anoop Saldanha <anoopsaldanha@gmail.com>
22  * \author Pierre Chifflier <pierre.chifflier@ssi.gouv.fr>
23  *
24  */
25 
26 #ifndef SURICATA_APP_LAYER_SSL_H
27 #define SURICATA_APP_LAYER_SSL_H
28 
29 #include "util-ja3.h"
30 #include "rust.h"
31 #include "app-layer-parser.h"
32 
34  TLS_FRAME_PDU = 0, /**< whole PDU, so header + data */
35  TLS_FRAME_HDR, /**< only header portion */
36  TLS_FRAME_DATA, /**< only data portion */
41 };
42 
43 enum {
44  /* TLS protocol messages */
61  /* Certificates decoding messages */
77 };
78 
85 };
86 
94 };
95 
96 /* Flag to indicate that server will now on send encrypted msgs */
97 #define SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC BIT_U32(0)
98 /* Flag to indicate that client will now on send encrypted msgs */
99 #define SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC BIT_U32(1)
100 #define SSL_AL_FLAG_CHANGE_CIPHER_SPEC BIT_U32(2)
101 
102 /* SSL related flags */
103 #define SSL_AL_FLAG_SSL_CLIENT_HS BIT_U32(3)
104 #define SSL_AL_FLAG_SSL_SERVER_HS BIT_U32(4)
105 #define SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY BIT_U32(5)
106 #define SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED BIT_U32(6)
107 #define SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED BIT_U32(7)
108 #define SSL_AL_FLAG_SSL_NO_SESSION_ID BIT_U32(8)
109 
110 /* flags specific to detect-ssl-state keyword */
111 #define SSL_AL_FLAG_STATE_CLIENT_HELLO BIT_U32(9)
112 #define SSL_AL_FLAG_STATE_SERVER_HELLO BIT_U32(10)
113 #define SSL_AL_FLAG_STATE_CLIENT_KEYX BIT_U32(11)
114 #define SSL_AL_FLAG_STATE_SERVER_KEYX BIT_U32(12)
115 #define SSL_AL_FLAG_STATE_UNKNOWN BIT_U32(13)
116 
117 /* flags specific to HeartBeat state */
118 #define SSL_AL_FLAG_HB_INFLIGHT BIT_U32(15)
119 #define SSL_AL_FLAG_HB_CLIENT_INIT BIT_U32(16)
120 #define SSL_AL_FLAG_HB_SERVER_INIT BIT_U32(17)
121 
122 /* Session resumed without a full handshake */
123 #define SSL_AL_FLAG_SESSION_RESUMED BIT_U32(20)
124 
125 /* Encountered a supported_versions extension in client hello */
126 #define SSL_AL_FLAG_CH_VERSION_EXTENSION BIT_U32(21)
127 
128 /* Log the session even without ever seeing a certificate. This is used
129  to log TLSv1.3 sessions. */
130 #define SSL_AL_FLAG_LOG_WITHOUT_CERT BIT_U32(22)
131 
132 /* Encountered a early data extension in client hello. This extension is
133  used by 0-RTT. */
134 #define SSL_AL_FLAG_EARLY_DATA BIT_U32(23)
135 
136 /* flag to indicate that server random was filled */
137 #define TLS_TS_RANDOM_SET BIT_U32(24)
138 
139 /* flag to indicate that client random was filled */
140 #define TLS_TC_RANDOM_SET BIT_U32(25)
141 
142 #define SSL_AL_FLAG_NEED_CLIENT_CERT BIT_U32(26)
143 
144 /* config flags */
145 #define SSL_TLS_LOG_PEM (1 << 0)
146 
147 /* extensions */
148 #define SSL_EXTENSION_SNI 0x0000
149 #define SSL_EXTENSION_ELLIPTIC_CURVES 0x000a
150 #define SSL_EXTENSION_EC_POINT_FORMATS 0x000b
151 #define SSL_EXTENSION_SIGNATURE_ALGORITHMS 0x000d
152 #define SSL_EXTENSION_ALPN 0x0010
153 #define SSL_EXTENSION_SESSION_TICKET 0x0023
154 #define SSL_EXTENSION_EARLY_DATA 0x002a
155 #define SSL_EXTENSION_SUPPORTED_VERSIONS 0x002b
156 
157 /* SNI types */
158 #define SSL_SNI_TYPE_HOST_NAME 0
159 
160 /* TLS random bytes for the sticky buffer */
161 #define TLS_RANDOM_LEN 32
162 
163 typedef struct SSLCertsChain_ {
164  uint8_t *cert_data;
165  uint32_t cert_len;
168 
169 typedef struct SSLSubjectAltName_ {
170  uint8_t *san;
171  uint32_t san_len;
173 
174 typedef struct SSLStateConnp_ {
175  /* record length */
176  uint32_t record_length;
177  /* record length's length for SSLv2 */
179 
180  /* offset of the beginning of the current message (including header) */
181  uint32_t message_length;
182 
183  uint16_t version;
184  uint8_t content_type;
185 
186  uint8_t handshake_type;
187 
188  /* the no of bytes processed in the currently parsed record */
189  uint32_t bytes_processed;
190 
192 
194  uint8_t *cert0_subject;
196  uint8_t *cert0_issuerdn;
198  uint8_t *cert0_serial;
203 
205  uint16_t cert0_sans_num;
206  /* ssl server name indication extension */
207  uint8_t *sni;
208  uint16_t sni_len;
209 
210  char *session_id;
211 
213 
214  uint8_t *certs_buffer;
216 
217  uint32_t cert_log_flag;
218 
220  char *ja3_hash;
221 
222  HandshakeParams *hs;
223 
224  /* handshake tls fragmentation buffer. Handshake messages can be fragmented over multiple
225  * TLS records. */
226  uint8_t *hs_buffer;
229  uint32_t hs_buffer_size; /**< allocation size */
230  uint32_t hs_buffer_offset; /**< write offset */
232 
233 /**
234  * \brief SSLv[2.0|3.[0|1|2|3]] state structure.
235  *
236  * Structure to store the SSL state values.
237  */
238 typedef struct SSLState_ {
239  Flow *f;
240 
243 
244  /* holds some state flags we need */
245  uint32_t flags;
246 
247  /* there might be a better place to store this*/
248  uint32_t hb_record_len;
249 
250  uint16_t events;
251 
252  uint32_t current_flags;
253 
255 
258 
262 
263 void RegisterSSLParsers(void);
264 void SSLEnableJA3(void);
265 bool SSLJA3IsEnabled(void);
266 void SSLEnableJA4(void);
267 bool SSLJA4IsEnabled(void);
268 
269 #endif /* SURICATA_APP_LAYER_SSL_H */
TLS_DECODER_EVENT_CERTIFICATE_INVALID_ISSUER
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_ISSUER
Definition: app-layer-ssl.h:72
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:238
SSLSubjectAltName_::san_len
uint32_t san_len
Definition: app-layer-ssl.h:171
JA3Buffer_
Definition: util-ja3.h:31
SSLSubjectAltName
struct SSLSubjectAltName_ SSLSubjectAltName
TLS_DECODER_EVENT_DATALEAK_HEARTBEAT_MISMATCH
@ TLS_DECODER_EVENT_DATALEAK_HEARTBEAT_MISMATCH
Definition: app-layer-ssl.h:54
SSLCertsChain_::cert_len
uint32_t cert_len
Definition: app-layer-ssl.h:165
TLS_DECODER_EVENT_HEARTBEAT
@ TLS_DECODER_EVENT_HEARTBEAT
Definition: app-layer-ssl.h:51
TLS_DECODER_EVENT_OVERFLOW_HEARTBEAT
@ TLS_DECODER_EVENT_OVERFLOW_HEARTBEAT
Definition: app-layer-ssl.h:53
TLS_DECODER_EVENT_TOO_MANY_SUBJECT_ALTERNATIVE_NAMES
@ TLS_DECODER_EVENT_TOO_MANY_SUBJECT_ALTERNATIVE_NAMES
Definition: app-layer-ssl.h:75
TLS_DECODER_EVENT_ERROR_MSG_ENCOUNTERED
@ TLS_DECODER_EVENT_ERROR_MSG_ENCOUNTERED
Definition: app-layer-ssl.h:74
SSLState_::hb_record_len
uint32_t hb_record_len
Definition: app-layer-ssl.h:248
SSLStateConnp_::message_length
uint32_t message_length
Definition: app-layer-ssl.h:181
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:259
TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE
@ TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE
Definition: app-layer-ssl.h:50
TLS_FRAME_DATA
@ TLS_FRAME_DATA
Definition: app-layer-ssl.h:36
SSLStateConnp_::bytes_processed
uint32_t bytes_processed
Definition: app-layer-ssl.h:189
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
SSLState_::server_connp
SSLStateConnp server_connp
Definition: app-layer-ssl.h:260
SSLStateConnp_::cert0_not_before
int64_t cert0_not_before
Definition: app-layer-ssl.h:200
SSLStateConnp_
Definition: app-layer-ssl.h:174
SSLStateConnp_::ja3_hash
char * ja3_hash
Definition: app-layer-ssl.h:220
SSLStateConnp_::session_id_length
uint16_t session_id_length
Definition: app-layer-ssl.h:191
AppLayerStateData
Definition: app-layer-parser.h:149
Flow_
Flow data structure.
Definition: flow.h:355
SSLState_::f
Flow * f
Definition: app-layer-ssl.h:239
SSLState_::tx_data
AppLayerTxData tx_data
Definition: app-layer-ssl.h:242
SSLState_::curr_connp
SSLStateConnp * curr_connp
Definition: app-layer-ssl.h:254
TLS_STATE_SERVER_HELLO_DONE
@ TLS_STATE_SERVER_HELLO_DONE
Definition: app-layer-ssl.h:91
rust.h
util-ja3.h
SSLState_::state_data
AppLayerStateData state_data
Definition: app-layer-ssl.h:241
TLS_DECODER_EVENT_INVALID_TLS_HEADER
@ TLS_DECODER_EVENT_INVALID_TLS_HEADER
Definition: app-layer-ssl.h:46
SSLStateConnp_::hs_buffer_message_type
uint8_t hs_buffer_message_type
Definition: app-layer-ssl.h:227
TLS_DECODER_EVENT_INVALID_SSL_RECORD
@ TLS_DECODER_EVENT_INVALID_SSL_RECORD
Definition: app-layer-ssl.h:76
TLS_DECODER_EVENT_TOO_MANY_RECORDS_IN_PACKET
@ TLS_DECODER_EVENT_TOO_MANY_RECORDS_IN_PACKET
Definition: app-layer-ssl.h:59
TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS
@ TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS
Definition: app-layer-ssl.h:56
SSLStateConnp_::record_lengths_length
uint32_t record_lengths_length
Definition: app-layer-ssl.h:178
SSLSubjectAltName_::san
uint8_t * san
Definition: app-layer-ssl.h:170
TLS_FRAME_SSLV2_HDR
@ TLS_FRAME_SSLV2_HDR
Definition: app-layer-ssl.h:39
SSLStateConnp_::hs_buffer_size
uint32_t hs_buffer_size
Definition: app-layer-ssl.h:229
TLS_DECODER_EVENT_CERTIFICATE_INVALID_DER
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_DER
Definition: app-layer-ssl.h:70
TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH
Definition: app-layer-ssl.h:63
TLS_FRAME_HB_DATA
@ TLS_FRAME_HB_DATA
Definition: app-layer-ssl.h:38
SSLStateConnp_::cert0_not_after
int64_t cert0_not_after
Definition: app-layer-ssl.h:201
SSLStateConnp
struct SSLStateConnp_ SSLStateConnp
SSLState_::current_flags
uint32_t current_flags
Definition: app-layer-ssl.h:252
SSLStateConnp_::cert0_sans
SSLSubjectAltName * cert0_sans
Definition: app-layer-ssl.h:204
TLS_DECODER_EVENT_CERTIFICATE_INVALID_VALIDITY
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_VALIDITY
Definition: app-layer-ssl.h:73
TLS_STATE_CLIENT_HANDSHAKE_DONE
@ TLS_STATE_CLIENT_HANDSHAKE_DONE
Definition: app-layer-ssl.h:83
AppLayerTxData
Definition: app-layer-parser.h:166
TLS_DECODER_EVENT_INVALID_RECORD_TYPE
@ TLS_DECODER_EVENT_INVALID_RECORD_TYPE
Definition: app-layer-ssl.h:48
SSLState_::client_state
enum TlsStateClient client_state
Definition: app-layer-ssl.h:256
SSLEnableJA3
void SSLEnableJA3(void)
if not explicitly disabled in config, enable ja3 support
Definition: app-layer-ssl.c:3336
SSLStateConnp_::handshake_type
uint8_t handshake_type
Definition: app-layer-ssl.h:186
SSLStateConnp_::hs_buffer_offset
uint32_t hs_buffer_offset
Definition: app-layer-ssl.h:230
SSLStateConnp_::certs_buffer
uint8_t * certs_buffer
Definition: app-layer-ssl.h:214
TLS_STATE_CLIENT_HELLO_DONE
@ TLS_STATE_CLIENT_HELLO_DONE
Definition: app-layer-ssl.h:81
TLS_DECODER_EVENT_INVALID_RECORD_VERSION
@ TLS_DECODER_EVENT_INVALID_RECORD_VERSION
Definition: app-layer-ssl.h:47
TlsStateServer
TlsStateServer
Definition: app-layer-ssl.h:87
TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE
Definition: app-layer-ssl.h:68
SSLCertsChain_
Definition: app-layer-ssl.h:163
TlsFrameTypes
TlsFrameTypes
Definition: app-layer-ssl.h:33
SSLStateConnp_::cert0_serial_len
uint32_t cert0_serial_len
Definition: app-layer-ssl.h:199
TLS_DECODER_EVENT_CERTIFICATE_INVALID_SERIAL
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_SERIAL
Definition: app-layer-ssl.h:65
TlsStateClient
TlsStateClient
Definition: app-layer-ssl.h:79
TLS_DECODER_EVENT_CERTIFICATE_INVALID_SUBJECT
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_SUBJECT
Definition: app-layer-ssl.h:71
TLS_STATE_SERVER_FINISHED
@ TLS_STATE_SERVER_FINISHED
Definition: app-layer-ssl.h:93
TLS_RANDOM_LEN
#define TLS_RANDOM_LEN
Definition: app-layer-ssl.h:161
SSLStateConnp_::cert0_serial
uint8_t * cert0_serial
Definition: app-layer-ssl.h:198
app-layer-parser.h
TLS_STATE_CLIENT_IN_PROGRESS
@ TLS_STATE_CLIENT_IN_PROGRESS
Definition: app-layer-ssl.h:80
SSLState_::events
uint16_t events
Definition: app-layer-ssl.h:250
TLS_FRAME_SSLV2_PDU
@ TLS_FRAME_SSLV2_PDU
Definition: app-layer-ssl.h:40
RegisterSSLParsers
void RegisterSSLParsers(void)
Function to register the SSL protocol parser and other functions.
Definition: app-layer-ssl.c:3205
TLS_DECODER_EVENT_INVALID_SSLV2_HEADER
@ TLS_DECODER_EVENT_INVALID_SSLV2_HEADER
Definition: app-layer-ssl.h:45
TLS_DECODER_EVENT_INVALID_RECORD_LENGTH
@ TLS_DECODER_EVENT_INVALID_RECORD_LENGTH
Definition: app-layer-ssl.h:49
SSLStateConnp_::cert0_issuerdn
uint8_t * cert0_issuerdn
Definition: app-layer-ssl.h:196
SSLState_::server_state
enum TlsStateServer server_state
Definition: app-layer-ssl.h:257
SSLStateConnp_::record_length
uint32_t record_length
Definition: app-layer-ssl.h:176
TLS_STATE_SERVER_CERT_DONE
@ TLS_STATE_SERVER_CERT_DONE
Definition: app-layer-ssl.h:90
SSLStateConnp_::sni
uint8_t * sni
Definition: app-layer-ssl.h:207
SSLCertsChain_::cert_data
uint8_t * cert_data
Definition: app-layer-ssl.h:164
SSLStateConnp_::cert0_sans_num
uint16_t cert0_sans_num
Definition: app-layer-ssl.h:205
SSLStateConnp_::certs_buffer_size
uint32_t certs_buffer_size
Definition: app-layer-ssl.h:215
TLS_DECODER_EVENT_INVALID_HEARTBEAT
@ TLS_DECODER_EVENT_INVALID_HEARTBEAT
Definition: app-layer-ssl.h:52
TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH
@ TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH
Definition: app-layer-ssl.h:55
TLS_DECODER_EVENT_CERTIFICATE_INVALID_VERSION
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_VERSION
Definition: app-layer-ssl.h:64
SSLStateConnp_::random
uint8_t random[TLS_RANDOM_LEN]
Definition: app-layer-ssl.h:193
TLS_DECODER_EVENT_INVALID_CERTIFICATE
@ TLS_DECODER_EVENT_INVALID_CERTIFICATE
Definition: app-layer-ssl.h:62
SSLStateConnp_::cert0_subject_len
uint32_t cert0_subject_len
Definition: app-layer-ssl.h:195
TLS_FRAME_PDU
@ TLS_FRAME_PDU
Definition: app-layer-ssl.h:34
SSLStateConnp_::content_type
uint8_t content_type
Definition: app-layer-ssl.h:184
SSLStateConnp_::hs_buffer_message_size
uint32_t hs_buffer_message_size
Definition: app-layer-ssl.h:228
TLS_DECODER_EVENT_CERTIFICATE_INVALID_ALGORITHMIDENTIFIER
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_ALGORITHMIDENTIFIER
Definition: app-layer-ssl.h:66
SSLEnableJA4
void SSLEnableJA4(void)
if not explicitly disabled in config, enable ja4 support
Definition: app-layer-ssl.c:3353
TLS_DECODER_EVENT_INVALID_SNI_TYPE
@ TLS_DECODER_EVENT_INVALID_SNI_TYPE
Definition: app-layer-ssl.h:57
TLS_STATE_CLIENT_CERT_DONE
@ TLS_STATE_CLIENT_CERT_DONE
Definition: app-layer-ssl.h:82
SSLSubjectAltName_
Definition: app-layer-ssl.h:169
TLS_STATE_SERVER_IN_PROGRESS
@ TLS_STATE_SERVER_IN_PROGRESS
Definition: app-layer-ssl.h:88
TLS_DECODER_EVENT_CERTIFICATE_INVALID_X509NAME
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_X509NAME
Definition: app-layer-ssl.h:67
SSLJA3IsEnabled
bool SSLJA3IsEnabled(void)
return whether ja3 is effectively enabled
Definition: app-layer-ssl.c:3373
SSLStateConnp_::TAILQ_HEAD
TAILQ_HEAD(, SSLCertsChain_) certs
SSLStateConnp_::cert_log_flag
uint32_t cert_log_flag
Definition: app-layer-ssl.h:217
TLS_DECODER_EVENT_CERTIFICATE_INVALID_EXTENSIONS
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_EXTENSIONS
Definition: app-layer-ssl.h:69
TLS_DECODER_EVENT_INVALID_SNI_LENGTH
@ TLS_DECODER_EVENT_INVALID_SNI_LENGTH
Definition: app-layer-ssl.h:58
SSLStateConnp_::hs
HandshakeParams * hs
Definition: app-layer-ssl.h:222
SSLStateConnp_::cert0_issuerdn_len
uint32_t cert0_issuerdn_len
Definition: app-layer-ssl.h:197
TLS_FRAME_ALERT_DATA
@ TLS_FRAME_ALERT_DATA
Definition: app-layer-ssl.h:37
TLS_STATE_SERVER_HELLO
@ TLS_STATE_SERVER_HELLO
Definition: app-layer-ssl.h:89
SSLCertsChain_::TAILQ_ENTRY
TAILQ_ENTRY(SSLCertsChain_) next
SSLStateConnp_::session_id
char * session_id
Definition: app-layer-ssl.h:210
TLS_FRAME_HDR
@ TLS_FRAME_HDR
Definition: app-layer-ssl.h:35
TLS_DECODER_EVENT_INVALID_ALERT
@ TLS_DECODER_EVENT_INVALID_ALERT
Definition: app-layer-ssl.h:60
SSLStateConnp_::cert0_fingerprint
char * cert0_fingerprint
Definition: app-layer-ssl.h:202
SSLStateConnp_::sni_len
uint16_t sni_len
Definition: app-layer-ssl.h:208
SSLStateConnp_::ja3_str
JA3Buffer * ja3_str
Definition: app-layer-ssl.h:219
TLS_STATE_SERVER_HANDSHAKE_DONE
@ TLS_STATE_SERVER_HANDSHAKE_DONE
Definition: app-layer-ssl.h:92
SSLState
struct SSLState_ SSLState
SSLv[2.0|3.[0|1|2|3]] state structure.
SSLCertsChain
struct SSLCertsChain_ SSLCertsChain
SSLStateConnp_::hs_buffer
uint8_t * hs_buffer
Definition: app-layer-ssl.h:226
TLS_STATE_CLIENT_FINISHED
@ TLS_STATE_CLIENT_FINISHED
Definition: app-layer-ssl.h:84
SSLJA4IsEnabled
bool SSLJA4IsEnabled(void)
return whether ja4 is effectively enabled
Definition: app-layer-ssl.c:3386
SSLStateConnp_::cert0_subject
uint8_t * cert0_subject
Definition: app-layer-ssl.h:194
SSLState_::flags
uint32_t flags
Definition: app-layer-ssl.h:245
SSLStateConnp_::version
uint16_t version
Definition: app-layer-ssl.h:183