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 */
76 };
77 
84 };
85 
93 };
94 
95 /* Flag to indicate that server will now on send encrypted msgs */
96 #define SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC BIT_U32(0)
97 /* Flag to indicate that client will now on send encrypted msgs */
98 #define SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC BIT_U32(1)
99 #define SSL_AL_FLAG_CHANGE_CIPHER_SPEC BIT_U32(2)
100 
101 /* SSL related flags */
102 #define SSL_AL_FLAG_SSL_CLIENT_HS BIT_U32(3)
103 #define SSL_AL_FLAG_SSL_SERVER_HS BIT_U32(4)
104 #define SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY BIT_U32(5)
105 #define SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED BIT_U32(6)
106 #define SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED BIT_U32(7)
107 #define SSL_AL_FLAG_SSL_NO_SESSION_ID BIT_U32(8)
108 
109 /* flags specific to detect-ssl-state keyword */
110 #define SSL_AL_FLAG_STATE_CLIENT_HELLO BIT_U32(9)
111 #define SSL_AL_FLAG_STATE_SERVER_HELLO BIT_U32(10)
112 #define SSL_AL_FLAG_STATE_CLIENT_KEYX BIT_U32(11)
113 #define SSL_AL_FLAG_STATE_SERVER_KEYX BIT_U32(12)
114 #define SSL_AL_FLAG_STATE_UNKNOWN BIT_U32(13)
115 
116 /* flags specific to HeartBeat state */
117 #define SSL_AL_FLAG_HB_INFLIGHT BIT_U32(15)
118 #define SSL_AL_FLAG_HB_CLIENT_INIT BIT_U32(16)
119 #define SSL_AL_FLAG_HB_SERVER_INIT BIT_U32(17)
120 
121 /* Session resumed without a full handshake */
122 #define SSL_AL_FLAG_SESSION_RESUMED BIT_U32(20)
123 
124 /* Encountered a supported_versions extension in client hello */
125 #define SSL_AL_FLAG_CH_VERSION_EXTENSION BIT_U32(21)
126 
127 /* Log the session even without ever seeing a certificate. This is used
128  to log TLSv1.3 sessions. */
129 #define SSL_AL_FLAG_LOG_WITHOUT_CERT BIT_U32(22)
130 
131 /* Encountered a early data extension in client hello. This extension is
132  used by 0-RTT. */
133 #define SSL_AL_FLAG_EARLY_DATA BIT_U32(23)
134 
135 /* flag to indicate that server random was filled */
136 #define TLS_TS_RANDOM_SET BIT_U32(24)
137 
138 /* flag to indicate that client random was filled */
139 #define TLS_TC_RANDOM_SET BIT_U32(25)
140 
141 #define SSL_AL_FLAG_NEED_CLIENT_CERT BIT_U32(26)
142 
143 /* config flags */
144 #define SSL_TLS_LOG_PEM (1 << 0)
145 
146 /* extensions */
147 #define SSL_EXTENSION_SNI 0x0000
148 #define SSL_EXTENSION_ELLIPTIC_CURVES 0x000a
149 #define SSL_EXTENSION_EC_POINT_FORMATS 0x000b
150 #define SSL_EXTENSION_SIGNATURE_ALGORITHMS 0x000d
151 #define SSL_EXTENSION_ALPN 0x0010
152 #define SSL_EXTENSION_SESSION_TICKET 0x0023
153 #define SSL_EXTENSION_EARLY_DATA 0x002a
154 #define SSL_EXTENSION_SUPPORTED_VERSIONS 0x002b
155 
156 /* SNI types */
157 #define SSL_SNI_TYPE_HOST_NAME 0
158 
159 /* TLS random bytes for the sticky buffer */
160 #define TLS_RANDOM_LEN 32
161 
162 typedef struct SSLCertsChain_ {
163  uint8_t *cert_data;
164  uint32_t cert_len;
167 
168 typedef struct SSLSubjectAltName_ {
169  uint8_t *san;
170  uint32_t san_len;
172 
173 typedef struct SSLStateConnp_ {
174  /* record length */
175  uint32_t record_length;
176  /* record length's length for SSLv2 */
178 
179  /* offset of the beginning of the current message (including header) */
180  uint32_t message_length;
181 
182  uint16_t version;
183  uint8_t content_type;
184 
185  uint8_t handshake_type;
186 
187  /* the no of bytes processed in the currently parsed record */
188  uint32_t bytes_processed;
189 
191 
193  uint8_t *cert0_subject;
195  uint8_t *cert0_issuerdn;
197  uint8_t *cert0_serial;
202 
204  uint16_t cert0_sans_num;
205  /* ssl server name indication extension */
206  uint8_t *sni;
207  uint16_t sni_len;
208 
209  char *session_id;
210 
212 
213  uint8_t *certs_buffer;
215 
216  uint32_t cert_log_flag;
217 
219  char *ja3_hash;
220 
221  HandshakeParams *hs;
222 
223  /* handshake tls fragmentation buffer. Handshake messages can be fragmented over multiple
224  * TLS records. */
225  uint8_t *hs_buffer;
228  uint32_t hs_buffer_size; /**< allocation size */
229  uint32_t hs_buffer_offset; /**< write offset */
231 
232 /**
233  * \brief SSLv[2.0|3.[0|1|2|3]] state structure.
234  *
235  * Structure to store the SSL state values.
236  */
237 typedef struct SSLState_ {
238  Flow *f;
239 
242 
243  /* holds some state flags we need */
244  uint32_t flags;
245 
246  /* there might be a better place to store this*/
247  uint32_t hb_record_len;
248 
249  uint16_t events;
250 
251  uint32_t current_flags;
252 
254 
257 
261 
262 void RegisterSSLParsers(void);
263 void SSLEnableJA3(void);
264 bool SSLJA3IsEnabled(void);
265 void SSLEnableJA4(void);
266 bool SSLJA4IsEnabled(void);
267 
268 #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:237
SSLSubjectAltName_::san_len
uint32_t san_len
Definition: app-layer-ssl.h:170
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:164
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_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:247
SSLStateConnp_::message_length
uint32_t message_length
Definition: app-layer-ssl.h:180
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:258
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:188
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
SSLState_::server_connp
SSLStateConnp server_connp
Definition: app-layer-ssl.h:259
SSLStateConnp_::cert0_not_before
int64_t cert0_not_before
Definition: app-layer-ssl.h:199
SSLStateConnp_
Definition: app-layer-ssl.h:173
SSLStateConnp_::ja3_hash
char * ja3_hash
Definition: app-layer-ssl.h:219
SSLStateConnp_::session_id_length
uint16_t session_id_length
Definition: app-layer-ssl.h:190
AppLayerStateData
Definition: app-layer-parser.h:160
Flow_
Flow data structure.
Definition: flow.h:347
SSLState_::f
Flow * f
Definition: app-layer-ssl.h:238
SSLState_::tx_data
AppLayerTxData tx_data
Definition: app-layer-ssl.h:241
SSLState_::curr_connp
SSLStateConnp * curr_connp
Definition: app-layer-ssl.h:253
TLS_STATE_SERVER_HELLO_DONE
@ TLS_STATE_SERVER_HELLO_DONE
Definition: app-layer-ssl.h:90
rust.h
util-ja3.h
SSLState_::state_data
AppLayerStateData state_data
Definition: app-layer-ssl.h:240
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:226
TLS_DECODER_EVENT_INVALID_SSL_RECORD
@ TLS_DECODER_EVENT_INVALID_SSL_RECORD
Definition: app-layer-ssl.h:75
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:177
SSLSubjectAltName_::san
uint8_t * san
Definition: app-layer-ssl.h:169
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:228
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:200
SSLStateConnp
struct SSLStateConnp_ SSLStateConnp
SSLState_::current_flags
uint32_t current_flags
Definition: app-layer-ssl.h:251
SSLStateConnp_::cert0_sans
SSLSubjectAltName * cert0_sans
Definition: app-layer-ssl.h:203
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:82
AppLayerTxData
Definition: app-layer-parser.h:177
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:255
SSLEnableJA3
void SSLEnableJA3(void)
if not explicitly disabled in config, enable ja3 support
Definition: app-layer-ssl.c:3318
SSLStateConnp_::handshake_type
uint8_t handshake_type
Definition: app-layer-ssl.h:185
SSLStateConnp_::hs_buffer_offset
uint32_t hs_buffer_offset
Definition: app-layer-ssl.h:229
SSLStateConnp_::certs_buffer
uint8_t * certs_buffer
Definition: app-layer-ssl.h:213
TLS_STATE_CLIENT_HELLO_DONE
@ TLS_STATE_CLIENT_HELLO_DONE
Definition: app-layer-ssl.h:80
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:86
TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE
@ TLS_DECODER_EVENT_CERTIFICATE_INVALID_DATE
Definition: app-layer-ssl.h:68
SSLCertsChain_
Definition: app-layer-ssl.h:162
TlsFrameTypes
TlsFrameTypes
Definition: app-layer-ssl.h:33
SSLStateConnp_::cert0_serial_len
uint32_t cert0_serial_len
Definition: app-layer-ssl.h:198
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:78
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:92
TLS_RANDOM_LEN
#define TLS_RANDOM_LEN
Definition: app-layer-ssl.h:160
SSLStateConnp_::cert0_serial
uint8_t * cert0_serial
Definition: app-layer-ssl.h:197
app-layer-parser.h
TLS_STATE_CLIENT_IN_PROGRESS
@ TLS_STATE_CLIENT_IN_PROGRESS
Definition: app-layer-ssl.h:79
SSLState_::events
uint16_t events
Definition: app-layer-ssl.h:249
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:3187
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:195
SSLState_::server_state
enum TlsStateServer server_state
Definition: app-layer-ssl.h:256
SSLStateConnp_::record_length
uint32_t record_length
Definition: app-layer-ssl.h:175
TLS_STATE_SERVER_CERT_DONE
@ TLS_STATE_SERVER_CERT_DONE
Definition: app-layer-ssl.h:89
SSLStateConnp_::sni
uint8_t * sni
Definition: app-layer-ssl.h:206
SSLCertsChain_::cert_data
uint8_t * cert_data
Definition: app-layer-ssl.h:163
SSLStateConnp_::cert0_sans_num
uint16_t cert0_sans_num
Definition: app-layer-ssl.h:204
SSLStateConnp_::certs_buffer_size
uint32_t certs_buffer_size
Definition: app-layer-ssl.h:214
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:192
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:194
TLS_FRAME_PDU
@ TLS_FRAME_PDU
Definition: app-layer-ssl.h:34
SSLStateConnp_::content_type
uint8_t content_type
Definition: app-layer-ssl.h:183
SSLStateConnp_::hs_buffer_message_size
uint32_t hs_buffer_message_size
Definition: app-layer-ssl.h:227
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:3335
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:81
SSLSubjectAltName_
Definition: app-layer-ssl.h:168
TLS_STATE_SERVER_IN_PROGRESS
@ TLS_STATE_SERVER_IN_PROGRESS
Definition: app-layer-ssl.h:87
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:3355
SSLStateConnp_::TAILQ_HEAD
TAILQ_HEAD(, SSLCertsChain_) certs
SSLStateConnp_::cert_log_flag
uint32_t cert_log_flag
Definition: app-layer-ssl.h:216
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:221
SSLStateConnp_::cert0_issuerdn_len
uint32_t cert0_issuerdn_len
Definition: app-layer-ssl.h:196
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:88
SSLCertsChain_::TAILQ_ENTRY
TAILQ_ENTRY(SSLCertsChain_) next
SSLStateConnp_::session_id
char * session_id
Definition: app-layer-ssl.h:209
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:201
SSLStateConnp_::sni_len
uint16_t sni_len
Definition: app-layer-ssl.h:207
SSLStateConnp_::ja3_str
JA3Buffer * ja3_str
Definition: app-layer-ssl.h:218
TLS_STATE_SERVER_HANDSHAKE_DONE
@ TLS_STATE_SERVER_HANDSHAKE_DONE
Definition: app-layer-ssl.h:91
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:225
TLS_STATE_CLIENT_FINISHED
@ TLS_STATE_CLIENT_FINISHED
Definition: app-layer-ssl.h:83
SSLJA4IsEnabled
bool SSLJA4IsEnabled(void)
return whether ja4 is effectively enabled
Definition: app-layer-ssl.c:3368
SSLStateConnp_::cert0_subject
uint8_t * cert0_subject
Definition: app-layer-ssl.h:193
SSLState_::flags
uint32_t flags
Definition: app-layer-ssl.h:244
SSLStateConnp_::version
uint16_t version
Definition: app-layer-ssl.h:182