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-mpm.h"
36 #include "detect-content.h"
37 #include "detect-pcre.h"
38 #include "detect-tls-certs.h"
39 #include "detect-engine-uint.h"
40 
41 #include "flow.h"
42 #include "flow-util.h"
43 #include "flow-var.h"
44 
45 #include "util-debug.h"
46 #include "util-spm.h"
47 #include "util-print.h"
48 
49 #include "stream-tcp.h"
50 
51 #include "app-layer.h"
52 #include "app-layer-ssl.h"
53 
54 #include "util-profiling.h"
55 #include "util-unittest.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 InspectionBuffer *TlsCertsGetData(DetectEngineThreadCtx *det_ctx,
66  const DetectEngineTransforms *transforms, Flow *f, const uint8_t flags, void *txv,
67  int list_id, uint32_t local_id)
68 {
69  SCEnter();
70 
71  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, local_id);
72  if (buffer == NULL || buffer->initialized)
73  return buffer;
74 
75  const SSLState *ssl_state = (SSLState *)f->alstate;
76  const SSLStateConnp *connp;
77 
78  if (flags & STREAM_TOSERVER) {
79  connp = &ssl_state->client_connp;
80  } else {
81  connp = &ssl_state->server_connp;
82  }
83 
84  if (TAILQ_EMPTY(&connp->certs)) {
86  return NULL;
87  }
88 
89  SSLCertsChain *cert;
90  if (local_id == 0) {
91  cert = TAILQ_FIRST(&connp->certs);
92  } else {
93  // TODO optimize ?
94  cert = TAILQ_FIRST(&connp->certs);
95  for (uint32_t i = 0; i < local_id; i++) {
96  cert = TAILQ_NEXT(cert, next);
97  }
98  }
99  if (cert == NULL) {
101  return NULL;
102  }
103 
104  InspectionBufferSetupMulti(buffer, transforms, cert->cert_data, cert->cert_len);
105  buffer->flags = DETECT_CI_FLAGS_SINGLE;
106 
107  SCReturnPtr(buffer, "InspectionBuffer");
108 }
109 
110 /**
111  * \brief Registration function for keyword: tls.certs
112  */
114 {
115  sigmatch_table[DETECT_AL_TLS_CERTS].name = "tls.certs";
116  sigmatch_table[DETECT_AL_TLS_CERTS].desc = "sticky buffer to match the TLS certificate buffer";
117  sigmatch_table[DETECT_AL_TLS_CERTS].url = "/rules/tls-keywords.html#tls-certs";
118  sigmatch_table[DETECT_AL_TLS_CERTS].Setup = DetectTlsCertsSetup;
119 #ifdef UNITTESTS
120  sigmatch_table[DETECT_AL_TLS_CERTS].RegisterTests = DetectTlsCertsRegisterTests;
121 #endif
124 
126  TlsCertsGetData, 2, 1);
128  TlsCertsGetData, 2, 1);
129 
130  DetectBufferTypeSetDescriptionByName("tls.certs", "TLS certificate");
131 
133 
134  g_tls_certs_buffer_id = DetectBufferTypeGetByName("tls.certs");
135 }
136 
137 /**
138  * \brief This function setup the tls.certs modifier keyword
139  *
140  * \param de_ctx Pointer to the Detect Engine Context
141  * \param s Pointer to the Signature to which the keyword belongs
142  * \param str Should hold an empty string always
143  *
144  * \retval 0 On success
145  * \retval -1 On failure
146  */
147 static int DetectTlsCertsSetup(DetectEngineCtx *de_ctx, Signature *s,
148  const char *str)
149 {
150  if (DetectBufferSetActiveList(de_ctx, s, g_tls_certs_buffer_id) < 0)
151  return -1;
152 
154  return -1;
155 
156  return 0;
157 }
158 
159 static int g_tls_cert_buffer_id = 0;
160 #define BUFFER_NAME "tls_validity"
161 #define KEYWORD_ID DETECT_AL_TLS_CHAIN_LEN
162 #define KEYWORD_NAME "tls.cert_chain_len"
163 #define KEYWORD_DESC "match TLS certificate chain length"
164 #define KEYWORD_URL "/rules/tls-keywords.html#tls-cert-chain-len"
165 
166 /**
167  * \internal
168  * \brief Function to match cert chain length in TLS
169  *
170  * \param t Pointer to thread vars.
171  * \param det_ctx Pointer to the pattern matcher thread.
172  * \param f Pointer to the current flow.
173  * \param flags Flags.
174  * \param state App layer state.
175  * \param s Pointer to the Signature.
176  * \param m Pointer to the sigmatch that we will cast into
177  * DetectU64Data.
178  *
179  * \retval 0 no match.
180  * \retval 1 match.
181  */
182 static int DetectTLSCertChainLenMatch(DetectEngineThreadCtx *det_ctx, Flow *f, uint8_t flags,
183  void *state, void *txv, const Signature *s, const SigMatchCtx *ctx)
184 {
185  SCEnter();
186 
187  SSLState *ssl_state = state;
188  if (flags & STREAM_TOCLIENT) {
189  SSLStateConnp *connp = &ssl_state->server_connp;
190  uint32_t cnt = 0;
191  SSLCertsChain *cert;
192  TAILQ_FOREACH (cert, &connp->certs, next) {
193  cnt++;
194  }
195  SCLogDebug("%u certs in chain", cnt);
196 
197  const DetectU32Data *dd = (const DetectU32Data *)ctx;
198  if (DetectU32Match(cnt, dd)) {
199  SCReturnInt(1);
200  }
201  }
202  SCReturnInt(0);
203 }
204 
205 /**
206  * \internal
207  * \brief Function to free memory associated with DetectU64Data.
208  *
209  * \param de_ptr Pointer to DetectU64Data.
210  */
211 static void DetectTLSCertChainLenFree(DetectEngineCtx *de_ctx, void *ptr)
212 {
213  rs_detect_u32_free(ptr);
214 }
215 
216 /**
217  * \brief Function to add the parsed tls cert chain len field into the current signature.
218  *
219  * \param de_ctx Pointer to the Detection Engine Context.
220  * \param s Pointer to the Current Signature.
221  * \param rawstr Pointer to the user provided flags options.
222  * \param type Defines if this is notBefore or notAfter.
223  *
224  * \retval 0 on Success.
225  * \retval -1 on Failure.
226  */
227 static int DetectTLSCertChainLenSetup(DetectEngineCtx *de_ctx, Signature *s, const char *rawstr)
228 {
230  return -1;
231 
232  DetectU32Data *dd = DetectU32Parse(rawstr);
233  if (dd == NULL) {
234  SCLogError("Parsing \'%s\' failed for %s", rawstr, sigmatch_table[KEYWORD_ID].name);
235  return -1;
236  }
237 
238  if (SigMatchAppendSMToList(de_ctx, s, KEYWORD_ID, (SigMatchCtx *)dd, g_tls_cert_buffer_id) ==
239  NULL) {
240  rs_detect_u32_free(dd);
241  return -1;
242  }
243  return 0;
244 }
245 
247 {
251  sigmatch_table[KEYWORD_ID].AppLayerTxMatch = DetectTLSCertChainLenMatch;
252  sigmatch_table[KEYWORD_ID].Setup = DetectTLSCertChainLenSetup;
253  sigmatch_table[KEYWORD_ID].Free = DetectTLSCertChainLenFree;
254 
257 
258  g_tls_cert_buffer_id = DetectBufferTypeGetByName(BUFFER_NAME);
259 }
260 
261 #ifdef UNITTESTS
262 #include "tests/detect-tls-certs.c"
263 #endif
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1304
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1737
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:296
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1509
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:225
SigTableElmt_::desc
const char * desc
Definition: detect.h:1303
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
SigTableElmt_::Free
void(* Free)(DetectEngineCtx *, void *)
Definition: detect.h:1291
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1301
InspectionBuffer::initialized
bool initialized
Definition: detect.h:377
stream-tcp.h
DETECT_AL_TLS_CERTS
@ DETECT_AL_TLS_CERTS
Definition: detect-engine-register.h:248
DetectEngineTransforms
Definition: detect.h:408
SSLState_::client_connp
SSLStateConnp client_connp
Definition: app-layer-ssl.h:314
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:33
SCLogDebug
#define SCLogDebug(...)
Definition: util-debug.h:269
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:315
DetectBufferSetActiveList
int DetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
Definition: detect-engine.c:1354
SSLStateConnp_
Definition: app-layer-ssl.h:235
InspectionBuffer
Definition: detect.h:373
KEYWORD_NAME
#define KEYWORD_NAME
Definition: detect-tls-certs.c:162
threads.h
Flow_
Flow data structure.
Definition: flow.h:360
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1295
ctx
struct Thresholds ctx
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:841
TAILQ_EMPTY
#define TAILQ_EMPTY(head)
Definition: queue.h:248
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1041
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:1272
InspectionBuffer::flags
uint8_t flags
Definition: detect.h:378
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:268
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1286
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
util-unittest-helper.h
KEYWORD_ID
#define KEYWORD_ID
Definition: detect-tls-certs.c:161
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1091
DetectAppLayerMultiRegister
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectionMultiBufferGetDataPtr GetData, int priority, int tx_min_progress)
Definition: detect-engine.c:2187
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine.c:1565
KEYWORD_DESC
#define KEYWORD_DESC
Definition: detect-tls-certs.c:163
decode.h
util-debug.h
TAILQ_FIRST
#define TAILQ_FIRST(head)
Definition: queue.h:250
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1090
SSLCertsChain_
Definition: app-layer-ssl.h:223
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
BUFFER_NAME
#define BUFFER_NAME
Definition: detect-tls-certs.c:160
util-profiling.h
TLS_STATE_CERT_READY
@ TLS_STATE_CERT_READY
Definition: app-layer-ssl.h:78
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:287
DetectTlsCertsRegister
void DetectTlsCertsRegister(void)
Registration function for keyword: tls.certs.
Definition: detect-tls-certs.c:113
SSLCertsChain_::cert_data
uint8_t * cert_data
Definition: app-layer-ssl.h:224
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:344
detect-tls-certs.c
cnt
uint32_t cnt
Definition: tmqh-packetpool.h:7
DETECT_CI_FLAGS_SINGLE
#define DETECT_CI_FLAGS_SINGLE
Definition: detect-engine-content-inspection.h:49
flags
uint8_t flags
Definition: decode-gre.h:0
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
InspectionBufferSetupMulti
void InspectionBufferSetupMulti(InspectionBuffer *buffer, const DetectEngineTransforms *transforms, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1578
DetectEngineInspectGenericList
uint8_t DetectEngineInspectGenericList(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const struct DetectEngineAppInspectionEngine_ *engine, const Signature *s, Flow *f, uint8_t flags, void *alstate, void *txv, uint64_t tx_id)
Do the content inspection & validation for a signature.
Definition: detect-engine.c:2097
KEYWORD_URL
#define KEYWORD_URL
Definition: detect-tls-certs.c:164
detect-tls-certs.h
str
#define str(s)
Definition: suricata-common.h:291
SCLogError
#define SCLogError(...)
Macro used to log ERROR messages.
Definition: util-debug.h:261
Flow_::alstate
void * alstate
Definition: flow.h:485
detect-parse.h
Signature_
Signature container.
Definition: detect.h:601
DetectTlsCertChainLenRegister
void DetectTlsCertChainLenRegister(void)
Definition: detect-tls-certs.c:246
InspectionBufferMultipleForListGet
InspectionBuffer * InspectionBufferMultipleForListGet(DetectEngineThreadCtx *det_ctx, const int list_id, const uint32_t local_id)
for a InspectionBufferMultipleForList get a InspectionBuffer
Definition: detect-engine.c:1518
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1485
DetectAppLayerInspectEngineRegister
void DetectAppLayerInspectEngineRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectEngineFuncPtr Callback, InspectionBufferGetDataPtr GetData)
Registers an app inspection engine.
Definition: detect-engine.c:238
SigMatchAppendSMToList
SigMatch * SigMatchAppendSMToList(DetectEngineCtx *de_ctx, Signature *s, uint16_t type, SigMatchCtx *ctx, const int list)
Append a SigMatch to the list type.
Definition: detect-parse.c:436
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1188
flow.h
SCReturnInt
#define SCReturnInt(x)
Definition: util-debug.h:275
flow-var.h
app-layer-ssl.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1293
app-layer.h