suricata
detect-tls-subjectaltname.c
Go to the documentation of this file.
1 /* Copyright (C) 2024 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 Shivani Bhardwaj <shivani@oisf.net>
22  *
23  * Implements support for tls.subjectaltname 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"
38 #include "detect-engine-uint.h"
39 
40 #include "flow.h"
41 #include "flow-util.h"
42 #include "flow-var.h"
43 
44 #include "util-debug.h"
45 #include "util-spm.h"
46 #include "util-print.h"
47 
48 #include "stream-tcp.h"
49 
50 #include "app-layer.h"
51 #include "app-layer-ssl.h"
52 #include "util-profiling.h"
53 
54 static int DetectTlsSubjectAltNameSetup(DetectEngineCtx *, Signature *, const char *);
55 static InspectionBuffer *TlsSubjectAltNameGetData(DetectEngineThreadCtx *det_ctx,
56  const DetectEngineTransforms *transforms, Flow *f, uint8_t flags, void *txv, int list_id,
57  uint32_t index);
58 
59 static int g_tls_subjectaltname_buffer_id = 0;
60 
61 /**
62  * \brief Registration function for keyword: tls.subjectaltname
63  */
65 {
66  sigmatch_table[DETECT_AL_TLS_SUBJECTALTNAME].name = "tls.subjectaltname";
68  "sticky buffer to match the TLS Subject Alternative Name buffer";
70  "/rules/tls-keywords.html#tls-subjectaltname";
71  sigmatch_table[DETECT_AL_TLS_SUBJECTALTNAME].Setup = DetectTlsSubjectAltNameSetup;
74 
76  TlsSubjectAltNameGetData, 2, TLS_STATE_CERT_READY);
77 
78  DetectBufferTypeSetDescriptionByName("tls.subjectaltname", "TLS Subject Alternative Name");
79 
80  DetectBufferTypeSupportsMultiInstance("tls.subjectaltname");
81 
82  g_tls_subjectaltname_buffer_id = DetectBufferTypeGetByName("tls.subjectaltname");
83 }
84 
85 /**
86  * \brief This function setup the tls.subjectaltname sticky buffer keyword
87  *
88  * \param de_ctx Pointer to the Detect Engine Context
89  * \param s Pointer to the Signature to which the keyword belongs
90  * \param str Should hold an empty string always
91  *
92  * \retval 0 On success
93  * \retval -1 On failure
94  */
95 static int DetectTlsSubjectAltNameSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
96 {
97  if (DetectBufferSetActiveList(de_ctx, s, g_tls_subjectaltname_buffer_id) < 0)
98  return -1;
99 
101  return -1;
102 
103  return 0;
104 }
105 
106 static InspectionBuffer *TlsSubjectAltNameGetData(DetectEngineThreadCtx *det_ctx,
107  const DetectEngineTransforms *transforms, Flow *f, uint8_t flags, void *txv, int list_id,
108  uint32_t idx)
109 {
110  SCEnter();
111  InspectionBuffer *buffer = InspectionBufferMultipleForListGet(det_ctx, list_id, idx);
112  if (buffer == NULL || buffer->initialized)
113  return buffer;
114 
115  const SSLState *ssl_state = (SSLState *)f->alstate;
116  const SSLStateConnp *connp;
117 
118  connp = &ssl_state->server_connp;
119 
120  if (idx >= connp->cert0_sans_len) {
121  return NULL;
122  }
123 
124  InspectionBufferSetupMulti(buffer, transforms, (const uint8_t *)connp->cert0_sans[idx],
125  strlen(connp->cert0_sans[idx]));
126  buffer->flags = DETECT_CI_FLAGS_SINGLE;
127 
128  SCReturnPtr(buffer, "InspectionBuffer");
129 }
detect-engine-uint.h
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
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1309
InspectionBuffer::initialized
bool initialized
Definition: detect.h:377
stream-tcp.h
DetectEngineTransforms
Definition: detect.h:408
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
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1044
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:1294
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1094
DETECT_AL_TLS_SUBJECTALTNAME
@ DETECT_AL_TLS_SUBJECTALTNAME
Definition: detect-engine-register.h:256
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:2205
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:17
DetectEngineThreadCtx_
Definition: detect.h:1098
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
util-profiling.h
DetectTlsSubjectAltNameRegister
void DetectTlsSubjectAltNameRegister(void)
Registration function for keyword: tls.subjectaltname.
Definition: detect-tls-subjectaltname.c:64
TLS_STATE_CERT_READY
@ TLS_STATE_CERT_READY
Definition: app-layer-ssl.h:78
SCReturnPtr
#define SCReturnPtr(x, type)
Definition: util-debug.h:287
detect-engine-content-inspection.h
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
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:1581
str
#define str(s)
Definition: suricata-common.h:291
Flow_::alstate
void * alstate
Definition: flow.h:481
detect-parse.h
Signature_
Signature container.
Definition: detect.h:603
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:1521
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1493
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1191
flow.h
flow-var.h
detect-tls-subjectaltname.h
app-layer-ssl.h
app-layer.h