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-buffer.h"
34 #include "detect-engine-mpm.h"
37 #include "detect-content.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 #include "util-profiling.h"
54 
55 static int DetectTlsSubjectAltNameSetup(DetectEngineCtx *, Signature *, const char *);
56 static int g_tls_subjectaltname_buffer_id = 0;
57 
58 static bool TlsSubjectAltNameGetData(DetectEngineThreadCtx *det_ctx, const void *txv,
59  const uint8_t flags, uint32_t idx, const uint8_t **buf, uint32_t *buf_len)
60 {
61  const SSLState *ssl_state = (SSLState *)txv;
62  const SSLStateConnp *connp;
63 
64  connp = &ssl_state->server_connp;
65 
66  if (idx >= connp->cert0_sans_len) {
67  return false;
68  }
69 
70  *buf = (const uint8_t *)connp->cert0_sans[idx];
71  if (*buf) {
72  *buf_len = (uint32_t)strlen(connp->cert0_sans[idx]);
73  } else {
74  // happens if the altname had a zero character in it
75  *buf_len = 0;
76  }
77  return true;
78 }
79 
80 /**
81  * \brief Registration function for keyword: tls.subjectaltname
82  */
84 {
85  sigmatch_table[DETECT_TLS_SUBJECTALTNAME].name = "tls.subjectaltname";
87  "sticky buffer to match the TLS Subject Alternative Name buffer";
88  sigmatch_table[DETECT_TLS_SUBJECTALTNAME].url = "/rules/tls-keywords.html#tls-subjectaltname";
89  sigmatch_table[DETECT_TLS_SUBJECTALTNAME].Setup = DetectTlsSubjectAltNameSetup;
93 
95  TLS_STATE_SERVER_CERT_DONE, TlsSubjectAltNameGetData, 2);
96 
97  DetectBufferTypeSetDescriptionByName("tls.subjectaltname", "TLS Subject Alternative Name");
98 
99  DetectBufferTypeSupportsMultiInstance("tls.subjectaltname");
100 
101  g_tls_subjectaltname_buffer_id = DetectBufferTypeGetByName("tls.subjectaltname");
102 }
103 
104 /**
105  * \brief This function setup the tls.subjectaltname sticky buffer keyword
106  *
107  * \param de_ctx Pointer to the Detect Engine Context
108  * \param s Pointer to the Signature to which the keyword belongs
109  * \param str Should hold an empty string always
110  *
111  * \retval 0 On success
112  * \retval -1 On failure
113  */
114 static int DetectTlsSubjectAltNameSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
115 {
116  if (SCDetectBufferSetActiveList(de_ctx, s, g_tls_subjectaltname_buffer_id) < 0)
117  return -1;
118 
120  return -1;
121 
122  return 0;
123 }
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1460
SSLState_
SSLv[2.0|3.[0|1|2|3]] state structure.
Definition: app-layer-ssl.h:227
detect-content.h
detect-engine.h
SIGMATCH_INFO_STICKY_BUFFER
#define SIGMATCH_INFO_STICKY_BUFFER
Definition: detect.h:1674
SigTableElmt_::desc
const char * desc
Definition: detect.h:1459
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:79
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1457
stream-tcp.h
SigTableElmt_::flags
uint32_t flags
Definition: detect.h:1448
ALPROTO_TLS
@ ALPROTO_TLS
Definition: app-layer-protos.h:39
SSLState_::server_connp
SSLStateConnp server_connp
Definition: app-layer-ssl.h:249
SSLStateConnp_
Definition: app-layer-ssl.h:167
threads.h
DetectEngineCtx_
main detection engine ctx
Definition: detect.h:932
DetectBufferTypeSupportsMultiInstance
void DetectBufferTypeSupportsMultiInstance(const char *name)
Definition: detect-engine.c:1228
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:2229
SIG_FLAG_TOCLIENT
#define SIG_FLAG_TOCLIENT
Definition: detect.h:272
SigTableElmt_::Setup
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition: detect.h:1439
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1278
decode.h
util-debug.h
de_ctx
DetectEngineCtx * de_ctx
Definition: fuzz_siginit.c:18
DetectEngineThreadCtx_
Definition: detect.h:1244
util-print.h
detect-engine-mpm.h
detect.h
util-profiling.h
DetectTlsSubjectAltNameRegister
void DetectTlsSubjectAltNameRegister(void)
Registration function for keyword: tls.subjectaltname.
Definition: detect-tls-subjectaltname.c:83
TLS_STATE_SERVER_CERT_DONE
@ TLS_STATE_SERVER_CERT_DONE
Definition: app-layer-ssl.h:88
detect-engine-content-inspection.h
flags
uint8_t flags
Definition: decode-gre.h:0
suricata-common.h
util-spm.h
detect-engine-buffer.h
SIGMATCH_INFO_MULTI_BUFFER
#define SIGMATCH_INFO_MULTI_BUFFER
Definition: detect.h:1684
str
#define str(s)
Definition: suricata-common.h:308
detect-parse.h
Signature_
Signature container.
Definition: detect.h:668
SSLStateConnp_::cert0_sans_len
uint16_t cert0_sans_len
Definition: app-layer-ssl.h:195
DETECT_TLS_SUBJECTALTNAME
@ DETECT_TLS_SUBJECTALTNAME
Definition: detect-engine-register.h:252
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1649
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1375
DetectAppLayerMultiRegister
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectionMultiBufferGetDataPtr GetData, int priority)
Definition: detect-engine.c:2099
flow.h
flow-var.h
detect-tls-subjectaltname.h
app-layer-ssl.h
app-layer.h
SSLStateConnp_::cert0_sans
char ** cert0_sans
Definition: app-layer-ssl.h:194