]> git.ipfire.org Git - people/ms/suricata.git/blob - src/app-layer-ssl.h
app-layer-ssl: add better session id support
[people/ms/suricata.git] / src / app-layer-ssl.h
1 /* Copyright (C) 2007-2012 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 __APP_LAYER_SSL_H__
27 #define __APP_LAYER_SSL_H__
28
29 #include "app-layer-protos.h"
30 #include "app-layer-parser.h"
31 #include "decode-events.h"
32 #include "util-ja3.h"
33 #include "queue.h"
34
35 enum {
36 /* TLS protocol messages */
37 TLS_DECODER_EVENT_INVALID_SSLV2_HEADER,
38 TLS_DECODER_EVENT_INVALID_TLS_HEADER,
39 TLS_DECODER_EVENT_INVALID_RECORD_VERSION,
40 TLS_DECODER_EVENT_INVALID_RECORD_TYPE,
41 TLS_DECODER_EVENT_INVALID_HANDSHAKE_MESSAGE,
42 TLS_DECODER_EVENT_HEARTBEAT,
43 TLS_DECODER_EVENT_INVALID_HEARTBEAT,
44 TLS_DECODER_EVENT_OVERFLOW_HEARTBEAT,
45 TLS_DECODER_EVENT_DATALEAK_HEARTBEAT_MISMATCH,
46 TLS_DECODER_EVENT_HANDSHAKE_INVALID_LENGTH,
47 TLS_DECODER_EVENT_MULTIPLE_SNI_EXTENSIONS,
48 TLS_DECODER_EVENT_INVALID_SNI_TYPE,
49 TLS_DECODER_EVENT_INVALID_SNI_LENGTH,
50 TLS_DECODER_EVENT_TOO_MANY_RECORDS_IN_PACKET,
51 /* Certificates decoding messages */
52 TLS_DECODER_EVENT_INVALID_CERTIFICATE,
53 TLS_DECODER_EVENT_CERTIFICATE_MISSING_ELEMENT,
54 TLS_DECODER_EVENT_CERTIFICATE_UNKNOWN_ELEMENT,
55 TLS_DECODER_EVENT_CERTIFICATE_INVALID_LENGTH,
56 TLS_DECODER_EVENT_CERTIFICATE_INVALID_STRING,
57 TLS_DECODER_EVENT_ERROR_MSG_ENCOUNTERED,
58 TLS_DECODER_EVENT_INVALID_SSL_RECORD,
59 };
60
61 enum {
62 TLS_STATE_IN_PROGRESS = 0,
63 TLS_STATE_CERT_READY = 1,
64 TLS_HANDSHAKE_DONE = 2,
65 TLS_STATE_FINISHED = 3
66 };
67
68 /* Flag to indicate that server will now on send encrypted msgs */
69 #define SSL_AL_FLAG_SERVER_CHANGE_CIPHER_SPEC BIT_U32(0)
70 /* Flag to indicate that client will now on send encrypted msgs */
71 #define SSL_AL_FLAG_CLIENT_CHANGE_CIPHER_SPEC BIT_U32(1)
72 #define SSL_AL_FLAG_CHANGE_CIPHER_SPEC BIT_U32(2)
73
74 /* SSL related flags */
75 #define SSL_AL_FLAG_SSL_CLIENT_HS BIT_U32(3)
76 #define SSL_AL_FLAG_SSL_SERVER_HS BIT_U32(4)
77 #define SSL_AL_FLAG_SSL_CLIENT_MASTER_KEY BIT_U32(5)
78 #define SSL_AL_FLAG_SSL_CLIENT_SSN_ENCRYPTED BIT_U32(6)
79 #define SSL_AL_FLAG_SSL_SERVER_SSN_ENCRYPTED BIT_U32(7)
80 #define SSL_AL_FLAG_SSL_NO_SESSION_ID BIT_U32(8)
81
82 /* flags specific to detect-ssl-state keyword */
83 #define SSL_AL_FLAG_STATE_CLIENT_HELLO BIT_U32(9)
84 #define SSL_AL_FLAG_STATE_SERVER_HELLO BIT_U32(10)
85 #define SSL_AL_FLAG_STATE_CLIENT_KEYX BIT_U32(11)
86 #define SSL_AL_FLAG_STATE_SERVER_KEYX BIT_U32(12)
87 #define SSL_AL_FLAG_STATE_UNKNOWN BIT_U32(13)
88
89 /* flag to indicate that session is finished */
90 #define SSL_AL_FLAG_STATE_FINISHED BIT_U32(14)
91
92 /* flags specific to HeartBeat state */
93 #define SSL_AL_FLAG_HB_INFLIGHT BIT_U32(15)
94 #define SSL_AL_FLAG_HB_CLIENT_INIT BIT_U32(16)
95 #define SSL_AL_FLAG_HB_SERVER_INIT BIT_U32(17)
96
97 /* flag to indicate that handshake is done */
98 #define SSL_AL_FLAG_HANDSHAKE_DONE BIT_U32(18)
99
100 /* A session ID in the Client Hello message, indicating the client
101 wants to resume a session */
102 #define SSL_AL_FLAG_SSL_CLIENT_SESSION_ID BIT_U32(19)
103 /* Session resumed without a full handshake */
104 #define SSL_AL_FLAG_SESSION_RESUMED BIT_U32(20)
105
106 /* config flags */
107 #define SSL_TLS_LOG_PEM (1 << 0)
108
109 /* extensions */
110 #define SSL_EXTENSION_SNI 0x0000
111 #define SSL_EXTENSION_ELLIPTIC_CURVES 0x000a
112 #define SSL_EXTENSION_EC_POINT_FORMATS 0x000b
113
114 /* SNI types */
115 #define SSL_SNI_TYPE_HOST_NAME 0
116
117 /* SSL versions. We'll use a unified format for all, with the top byte
118 * holding the major version and the lower byte the minor version */
119 enum {
120 TLS_VERSION_UNKNOWN = 0x0000,
121 SSL_VERSION_2 = 0x0200,
122 SSL_VERSION_3 = 0x0300,
123 TLS_VERSION_10 = 0x0301,
124 TLS_VERSION_11 = 0x0302,
125 TLS_VERSION_12 = 0x0303,
126 };
127
128 typedef struct SSLCertsChain_ {
129 uint8_t *cert_data;
130 uint32_t cert_len;
131 TAILQ_ENTRY(SSLCertsChain_) next;
132 } SSLCertsChain;
133
134
135 typedef struct SSLStateConnp_ {
136 /* record length */
137 uint32_t record_length;
138 /* record length's length for SSLv2 */
139 uint32_t record_lengths_length;
140
141 /* offset of the beginning of the current message (including header) */
142 uint32_t message_start;
143 uint32_t message_length;
144
145 uint16_t version;
146 uint8_t content_type;
147
148 uint8_t handshake_type;
149 uint32_t handshake_length;
150
151 /* the no of bytes processed in the currently parsed record */
152 uint16_t bytes_processed;
153 /* the no of bytes processed in the currently parsed handshake */
154 uint16_t hs_bytes_processed;
155
156 uint16_t session_id_length;
157
158 char *cert0_subject;
159 char *cert0_issuerdn;
160 char *cert0_serial;
161 time_t cert0_not_before;
162 time_t cert0_not_after;
163 char *cert0_fingerprint;
164
165 /* ssl server name indication extension */
166 char *sni;
167
168 char *session_id;
169
170 TAILQ_HEAD(, SSLCertsChain_) certs;
171
172 uint32_t cert_log_flag;
173
174 /* buffer for the tls record.
175 * We use a malloced buffer, if the record is fragmented */
176 uint8_t *trec;
177 uint32_t trec_len;
178 uint32_t trec_pos;
179 } SSLStateConnp;
180
181 /**
182 * \brief SSLv[2.0|3.[0|1|2|3]] state structure.
183 *
184 * Structure to store the SSL state values.
185 */
186 typedef struct SSLState_ {
187 Flow *f;
188
189 /* holds some state flags we need */
190 uint32_t flags;
191
192 /* specifies which loggers are done logging */
193 uint32_t logged;
194
195 /* detect flags */
196 uint64_t detect_flags_ts;
197 uint64_t detect_flags_tc;
198
199 /* there might be a better place to store this*/
200 uint16_t hb_record_len;
201
202 uint16_t events;
203
204 uint32_t current_flags;
205
206 JA3Buffer *ja3_str;
207 char *ja3_hash;
208
209 SSLStateConnp *curr_connp;
210
211 SSLStateConnp client_connp;
212 SSLStateConnp server_connp;
213
214 DetectEngineState *de_state;
215 AppLayerDecoderEvents *decoder_events;
216 } SSLState;
217
218 void RegisterSSLParsers(void);
219 void SSLParserRegisterTests(void);
220 void SSLSetEvent(SSLState *ssl_state, uint8_t event);
221
222 #endif /* __APP_LAYER_SSL_H__ */