suricata
detect-tls-certs.c
Go to the documentation of this file.
1 /* Copyright (C) 2019-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 Mats Klepsland <mats.klepsland@gmail.com>
22  *
23  * Implements support for tls.certs keyword.
24  */
25 
26 #include "suricata-common.h"
27 #include "threads.h"
28 #include "decode.h"
29 #include "detect.h"
30 
31 #include "detect-parse.h"
32 #include "detect-engine.h"
33 #include "detect-engine-buffer.h"
34 #include "detect-engine-mpm.h"
37 #include "detect-content.h"
38 #include "detect-pcre.h"
39 #include "detect-tls-certs.h"
40 #include "detect-engine-uint.h"
41 
42 #include "flow.h"
43 #include "flow-util.h"
44 #include "flow-var.h"
45 
46 #include "util-debug.h"
47 #include "util-spm.h"
48 #include "util-print.h"
49 
50 #include "stream-tcp.h"
51 
52 #include "app-layer.h"
53 #include "app-layer-ssl.h"
54 
55 #include "util-profiling.h"
56 #include "util-unittest-helper.h"
57 
58 static int DetectTlsCertsSetup(DetectEngineCtx *, Signature *, const char *);
59 #ifdef UNITTESTS
60 static void DetectTlsCertsRegisterTests(void);
61 #endif
62 
63 static int g_tls_certs_buffer_id = 0;
64 
65 static bool TlsCertsGetData(DetectEngineThreadCtx *det_ctx, const void *txv, const uint8_t flags,
66  uint32_t local_id, const uint8_t **buf, uint32_t *buf_len)
67 {
68  const SSLState *ssl_state = (SSLState *)txv;
69  const SSLStateConnp *connp;
70 
71  if (flags & STREAM_TOSERVER) {
72  connp = &ssl_state->client_connp;
73  } else {
74  connp = &ssl_state->server_connp;
75  }
76 
77  if (TAILQ_EMPTY(&connp->certs)) {
78  return false;
79  }
80 
81  SSLCertsChain *cert;
82  if (local_id == 0) {
83  cert = TAILQ_FIRST(&connp->certs);
84  } else {
85  // TODO optimize ?
86  cert = TAILQ_FIRST(&connp->certs);
87  for (uint32_t i = 0; i < local_id; i++) {
88  cert = TAILQ_NEXT(cert, next);
89  }
90  }
91  if (cert == NULL) {
92  return false;
93  }
94 
95  *buf = cert->cert_data;
96  *buf_len = cert->cert_len;
97  return true;
98 }
99 
100 /**
101  * \brief Registration function for keyword: tls.certs
102  */
104 {
105  sigmatch_table[DETECT_TLS_CERTS].name = "tls.certs";
106  sigmatch_table[DETECT_TLS_CERTS].desc = "sticky buffer to match the TLS certificate buffer";
107  sigmatch_table[DETECT_TLS_CERTS].url = "/rules/tls-keywords.html#tls-certs";
108  sigmatch_table[DETECT_TLS_CERTS].Setup = DetectTlsCertsSetup;
109 #ifdef UNITTESTS
110  sigmatch_table[DETECT_TLS_CERTS].RegisterTests = DetectTlsCertsRegisterTests;
111 #endif
115 
117  "tls.certs", ALPROTO_TLS, SIG_FLAG_TOCLIENT, TLS_STATE_SERVER_CERT, TlsCertsGetData, 2);
119  "tls.certs", ALPROTO_TLS, SIG_FLAG_TOSERVER, TLS_STATE_CLIENT_CERT, TlsCertsGetData, 2);
120 
121  DetectBufferTypeSetDescriptionByName("tls.certs", "TLS certificate");
122 
124 
125  g_tls_certs_buffer_id = DetectBufferTypeGetByName("tls.certs");
126 }
127 
128 /**
129  * \brief This function setup the tls.certs modifier keyword
130  *
131  * \param de_ctx Pointer to the Detect Engine Context
132  * \param s Pointer to the Signature to which the keyword belongs
133  * \param str Should hold an empty string always
134  *
135  * \retval 0 On success
136  * \retval -1 On failure
137  */
138 static int DetectTlsCertsSetup(DetectEngineCtx *de_ctx, Signature *s,
139  const char *str)
140 {
141  if (SCDetectBufferSetActiveList(de_ctx, s, g_tls_certs_buffer_id) < 0)
142  return -1;
143 
145  return -1;
146 
147  return 0;
148 }
149 
150 static int g_tls_cert_buffer_id = 0;
151 #define BUFFER_NAME "tls:server_cert:generic"
152 #define KEYWORD_ID DETECT_TLS_CHAIN_LEN
153 #define KEYWORD_NAME "tls.cert_chain_len"
154 #define KEYWORD_DESC "match TLS certificate chain length"
155 #define KEYWORD_URL "/rules/tls-keywords.html#tls-cert-chain-len"
156 
157 /**
158  * \internal
159  * \brief Function to match cert chain length in TLS
160  *
161  * \param t Pointer to thread vars.
162  * \param det_ctx Pointer to the pattern matcher thread.
163  * \param f Pointer to the current flow.
164  * \param flags Flags.
165  * \param state App layer state.
166  * \param s Pointer to the Signature.
167  * \param m Pointer to the sigmatch that we will cast into
168  * DetectU64Data.
169  *
170  * \retval 0 no match.
171  * \retval 1 match.
172  */
173 static int DetectTLSCertChainLenMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
174  void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
175 {
176  SCEnter();
177 
178  SSLState *ssl_state = state;
179  if (flags & STREAM_TOCLIENT) {
180  SSLStateConnp *connp = &ssl_state->server_connp;
181  /* count the chain only when the certificate data is final:
182  * a Certificate message that failed to decode leaves a partial
183  * chain with cert_data_incomplete set, sticky across later
184  * phases (an application-data record advances the track and
185  * rules at server_cert are still evaluated mid-walk);
186  * within the cert phase the queue may also simply not be
187  * parsed yet (the track enters it at ServerHello completion,
188  * before any certificate byte) */
189  if (connp->cert_data_incomplete ||
190  (ssl_state->server_state == TLS_STATE_SERVER_CERT && !connp->cert_chain_final)) {
191  SCReturnInt(0);
192  }
193  uint32_t cnt = 0;
194  SSLCertsChain *cert;
195  TAILQ_FOREACH (cert, &connp->certs, next) {
196  cnt++;
197  }
198  SCLogDebug("%u certs in chain", cnt);
199 
200  const DetectU32Data *dd = (const DetectU32Data *)ctx;
201  if (DetectU32Match(cnt, dd)) {
202  SCReturnInt(1);
203  }
204  }
205  SCReturnInt(0);
206 }
207 
208 /**
209  * \internal
210  * \brief Function to free memory associated with DetectU64Data.
211  *
212  * \param de_ptr Pointer to DetectU64Data.
213  */
214 static void DetectTLSCertChainLenFree(DetectEngineCtx *de_ctx, void *ptr)
215 {
216  SCDetectU32Free(ptr);
217 }
218 
219 /**
220  * \brief Function to add the parsed tls cert chain len field into the current signature.
221  *
222  * \param de_ctx Pointer to the Detection Engine Context.
223  * \param s Pointer to the Current Signature.
224  * \param rawstr Pointer to the user provided flags options.
225  * \param type Defines if this is notBefore or notAfter.
226  *
227  * \retval 0 on Success.
228  * \retval -1 on Failure.
229  */
230 static int DetectTLSCertChainLenSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
231 {
233  return -1;
234 
235  DetectU32Data *dd = DetectU32Parse(rawstr);
236  if (dd == NULL) {
237  SCLogError("Parsing \'%s\' failed for %s", rawstr, sigmatch_table[KEYWORD_ID].name);
238  return -1;
239  }
240 
241  if (SCSigMatchAppendSMToList(de_ctx, s, KEYWORD_ID, (SigMatchCtx *)dd, g_tls_cert_buffer_id) ==
242  NULL) {
243  SCDetectU32Free(dd);
244  return -1;
245  }
246  return 0;
247 }
248 
250 {
254  sigmatch_table[KEYWORD_ID].AppLayerTxMatch = DetectTLSCertChainLenMatch;
255  sigmatch_table[KEYWORD_ID].Setup = DetectTLSCertChainLenSetup;
256  sigmatch_table[KEYWORD_ID].Free = DetectTLSCertChainLenFree;
258 
259  g_tls_cert_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
260 }
261 
262 #ifdef UNITTESTS
263 #include "tests/detect-tls-certs.c"
264 #endif
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1545
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:263
detect-content.h
detect-engine.h
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect-engine-register.h:308
DetectU32Match
int DetectU32Match(const uint32_t parg, const DetectUintData_u32 *du32)
Definition: detect-engine-uint.c:31
SSLCertsChain_::cert_len
uint32_t cert_len
Definition: app-layer-ssl.h:168
SigTableElmt_::desc
const char * desc
Definition: detect.h:1544
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:78
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1529
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1542
TLS_STATE_SERVER_CERT
@ TLS_STATE_SERVER_CERT
Definition: app-layer-ssl.h:90
stream-tcp.h
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:284
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1533
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:39
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:282
DetectU32Parse
DetectUintData_u32 * DetectU32Parse(const char *u32str)
This function is used to parse u32 options passed via some u32 keyword.
Definition: detect-engine-uint.c:45
next
struct HtpBodyChunk_ * next
Definition: app-layer-htp.h:0
SSLState_::server_connp
SSLStateConnp server_connp
Definition: app-layer-ssl.h:285
name
const char * name
Definition: detect-engine-proto.c:47
SSLStateConnp_
Definition: app-layer-ssl.h:177
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-tls-certs.c:153
threads.h
Flow_
Flow data structure.
Definition: flow.h:359
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:995
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:248
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1402
TAILQ_FOREACH
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:252
SigTableElmt_::AppLayerTxMatch
int(* AppLayerTxMatch)(DetectEngineThreadCtx *, Flow *, uint8_t flags, void *alstate, void *txv, const Signature *, const SigMatchCtx *)
Definition: detect.h:1507
SCDetectBufferSetActiveList
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine-buffer.c:29
SCDetectSignatureSetAppProto
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:2619
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:275
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1524
detect-pcre.h
detect-engine-prefilter.h
util-unittest-helper.h
KEYWORD_ID
#define KEYWORD_ID
Definition: detect-tls-certs.c:152
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1452
SIGMATCH_INFO_MULTI_BUFFER
#define SIGMATCH_INFO_MULTI_BUFFER
Definition: detect-engine-register.h:340
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:274
KEYWORD_DESC
#define KEYWORD_DESC
Definition: detect-tls-certs.c:154
decode.h
util-debug.h
TAILQ_FIRST
#define TAILQ_FIRST(head)
Definition: queue.h:250
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:22
DetectEngineThreadCtx_
Definition: detect.h:1316
SSLCertsChain_
Definition: app-layer-ssl.h:166
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:284
detect-engine-mpm.h
SCSigMatchAppendSMToList
SigMatch * SCSigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:424
detect.h
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-tls-certs.c:151
SSLStateConnp_::cert_data_incomplete
bool cert_data_incomplete
Definition: app-layer-ssl.h:213
util-profiling.h
TLS_STATE_CLIENT_CERT
@ TLS_STATE_CLIENT_CERT
Definition: app-layer-ssl.h:82
SSLState_::server_state
enum TlsStateServer server_state
Definition: app-layer-ssl.h:282
DetectTlsCertsRegister
void DetectTlsCertsRegister(void)
Registration function for keyword: tls.certs.
Definition: detect-tls-certs.c:103
SSLCertsChain_::cert_data
uint8_t * cert_data
Definition: app-layer-ssl.h:167
detect-engine-content-inspection.h
SigMatchCtx_
Used to start a pointer to SigMatch context Should never be dereferenced without casting to something...
Definition: detect.h:355
detect-tls-certs.c
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
flags
uint8_t flags
Definition: decode-gre.h:0
DetectAppLayerMultiRegister
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, uint8_t progress, InspectionMultiBufferGetDataPtr GetData, int priority)
Definition: detect-engine.c:2329
suricata-common.h
DetectU32Data
DetectUintData_u32 DetectU32Data
Definition: detect-engine-uint.h:41
TAILQ_NEXT
#define TAILQ_NEXT(elm, field)
Definition: queue.h:307
util-spm.h
detect-engine-buffer.h
KEYWORD_URL
#define KEYWORD_URL
Definition: detect-tls-certs.c:155
detect-tls-certs.h
str
#define str(s)
Definition: suricata-common.h:313
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:274
SIGMATCH_INFO_UINT32
#define SIGMATCH_INFO_UINT32
Definition: detect-engine-register.h:346
detect-parse.h
Signature_
Signature container.
Definition: detect.h:692
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect-engine-register.h:330
DETECT_TLS_CERTS
@ DETECT_TLS_CERTS
Definition: detect-engine-register.h:241
DetectTlsCertChainLenRegister
void DetectTlsCertChainLenRegister(void)
Definition: detect-tls-certs.c:249
SIGMATCH_SUPPORT_FIREWALL
#define SIGMATCH_SUPPORT_FIREWALL
Definition: detect-engine-register.h:336
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1549
flow.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:288
flow-var.h
SSLStateConnp_::cert_chain_final
bool cert_chain_final
Definition: app-layer-ssl.h:219
app-layer-ssl.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1531
app-layer.h
f
Flow f
Definition: fuzz_dataset.c:32