suricata
detect-tls-cert-issuer.c
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 Mats Klepsland <mats.klepsland@gmail.com>
22  *
23  * Implements support for tls.cert_issuer 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"
35 #include "detect-content.h"
36 #include "detect-pcre.h"
37 
38 #include "flow.h"
39 #include "flow-util.h"
40 #include "flow-var.h"
41 
42 #include "util-debug.h"
43 #include "util-spm.h"
44 #include "util-print.h"
45 
46 #include "stream-tcp.h"
47 
48 #include "app-layer.h"
49 #include "app-layer-ssl.h"
50 #include "detect-tls-cert-issuer.h"
51 
52 #include "util-unittest.h"
53 #include "util-unittest-helper.h"
54 
55 static int DetectTlsIssuerSetup(DetectEngineCtx *, Signature *, const char *);
56 #ifdef UNITTESTS
57 static void DetectTlsIssuerRegisterTests(void);
58 #endif
59 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
60  const DetectEngineTransforms *transforms,
61  Flow *f, const uint8_t flow_flags,
62  void *txv, const int list_id);
63 static int g_tls_cert_issuer_buffer_id = 0;
64 
65 /**
66  * \brief Registration function for keyword: tls.cert_issuer
67  */
69 {
70  sigmatch_table[DETECT_AL_TLS_CERT_ISSUER].name = "tls.cert_issuer";
71  sigmatch_table[DETECT_AL_TLS_CERT_ISSUER].alias = "tls_cert_issuer";
73  "sticky buffer to match specifically and only on the TLS cert issuer buffer";
74  sigmatch_table[DETECT_AL_TLS_CERT_ISSUER].url = "/rules/tls-keywords.html#tls-cert-issuer";
75  sigmatch_table[DETECT_AL_TLS_CERT_ISSUER].Setup = DetectTlsIssuerSetup;
76 #ifdef UNITTESTS
77  sigmatch_table[DETECT_AL_TLS_CERT_ISSUER].RegisterTests = DetectTlsIssuerRegisterTests;
78 #endif
81 
84 
87 
90 
93 
94  DetectBufferTypeSetDescriptionByName("tls.cert_issuer",
95  "TLS certificate issuer");
96 
97  g_tls_cert_issuer_buffer_id = DetectBufferTypeGetByName("tls.cert_issuer");
98 }
99 
100 
101 /**
102  * \brief this function setup the tls_cert_issuer modifier keyword used in the rule
103  *
104  * \param de_ctx Pointer to the Detection Engine Context
105  * \param s Pointer to the Signature to which the current keyword belongs
106  * \param str Should hold an empty string always
107  *
108  * \retval 0 On success
109  * \retval -1 On failure
110  */
111 static int DetectTlsIssuerSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
112 {
113  if (DetectBufferSetActiveList(de_ctx, s, g_tls_cert_issuer_buffer_id) < 0)
114  return -1;
115 
117  return -1;
118 
119  return 0;
120 }
121 
122 static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
123  const DetectEngineTransforms *transforms, Flow *f,
124  const uint8_t flow_flags, void *txv, const int list_id)
125 {
126  InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id);
127  if (buffer->inspect == NULL) {
128  const SSLState *ssl_state = (SSLState *)f->alstate;
129  const SSLStateConnp *connp;
130  if (flow_flags & STREAM_TOSERVER) {
131  connp = &ssl_state->client_connp;
132  } else {
133  connp = &ssl_state->server_connp;
134  }
135 
136  if (connp->cert0_issuerdn == NULL) {
137  return NULL;
138  }
139 
140  const uint32_t data_len = strlen(connp->cert0_issuerdn);
141  const uint8_t *data = (uint8_t *)connp->cert0_issuerdn;
142 
143  InspectionBufferSetup(det_ctx, list_id, buffer, data, data_len);
144  InspectionBufferApplyTransforms(buffer, transforms);
145  }
146 
147  return buffer;
148 }
149 
150 #ifdef UNITTESTS
152 #endif
SigTableElmt_::url
const char * url
Definition: detect.h:1312
DetectSignatureSetAppProto
int DetectSignatureSetAppProto(Signature *s, AppProto alproto)
Definition: detect-parse.c:1738
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:1517
SigTableElmt_::desc
const char * desc
Definition: detect.h:1311
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:128
DetectEngineInspectBufferGeneric
uint8_t DetectEngineInspectBufferGeneric(DetectEngineCtx *de_ctx, DetectEngineThreadCtx *det_ctx, const 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:2155
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1309
stream-tcp.h
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
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:1357
SSLStateConnp_
Definition: app-layer-ssl.h:235
InspectionBuffer
Definition: detect.h:373
threads.h
Flow_
Flow data structure.
Definition: flow.h:356
SigTableElmt_::flags
uint16_t flags
Definition: detect.h:1303
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:843
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:268
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1294
detect-pcre.h
detect-engine-prefilter.h
util-unittest.h
InspectionBufferGet
InspectionBuffer * InspectionBufferGet(DetectEngineThreadCtx *det_ctx, const int list_id)
Definition: detect-engine.c:1501
util-unittest-helper.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1094
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1098
DETECT_AL_TLS_CERT_ISSUER
@ DETECT_AL_TLS_CERT_ISSUER
Definition: detect-engine-register.h:252
util-print.h
detect-engine-mpm.h
detect.h
PrefilterGenericMpmRegister
int PrefilterGenericMpmRegister(DetectEngineCtx *de_ctx, SigGroupHead *sgh, MpmCtx *mpm_ctx, const DetectBufferMpmRegistry *mpm_reg, int list_id)
Definition: detect-engine-prefilter.c:750
DetectAppLayerMpmRegister
void DetectAppLayerMpmRegister(const char *name, int direction, int priority, PrefilterRegisterFunc PrefilterRegister, InspectionBufferGetDataPtr GetData, AppProto alproto, int tx_min_progress)
register an app layer keyword for mpm
Definition: detect-engine-mpm.c:151
TLS_STATE_CERT_READY
@ TLS_STATE_CERT_READY
Definition: app-layer-ssl.h:78
DetectTlsIssuerRegister
void DetectTlsIssuerRegister(void)
Registration function for keyword: tls.cert_issuer.
Definition: detect-tls-cert-issuer.c:68
SigTableElmt_::alias
const char * alias
Definition: detect.h:1310
suricata-common.h
InspectionBufferApplyTransforms
void InspectionBufferApplyTransforms(InspectionBuffer *buffer, const DetectEngineTransforms *transforms)
Definition: detect-engine.c:1712
util-spm.h
InspectionBuffer::inspect
const uint8_t * inspect
Definition: detect.h:374
str
#define str(s)
Definition: suricata-common.h:291
detect-tls-cert-issuer.h
InspectionBufferSetup
void InspectionBufferSetup(DetectEngineThreadCtx *det_ctx, const int list_id, InspectionBuffer *buffer, const uint8_t *data, const uint32_t data_len)
setup the buffer with our initial data
Definition: detect-engine.c:1596
Flow_::alstate
void * alstate
Definition: flow.h:481
detect-parse.h
Signature_
Signature container.
Definition: detect.h:603
detect-tls-cert-issuer.c
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1493
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:240
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1191
flow.h
flow-var.h
app-layer-ssl.h
SigTableElmt_::RegisterTests
void(* RegisterTests)(void)
Definition: detect.h:1301
app-layer.h