suricata
detect-tls-alpn.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 Victor Julien <vjulien@oisf.net>
22  *
23  * Implements support for tls.alpn 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-tls-alpn.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 DetectTlsAlpnSetup(DetectEngineCtx *, Signature *, const char *);
55 static InspectionBuffer *TlsAlpnGetData(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_alpn_buffer_id = 0;
60 
61 /**
62  * \brief Registration function for keyword: tls.alpn
63  */
65 {
66  sigmatch_table[DETECT_TLS_ALPN].name = "tls.alpn";
67  sigmatch_table[DETECT_TLS_ALPN].desc = "sticky buffer to match the TLS ALPN buffer";
68  sigmatch_table[DETECT_TLS_ALPN].url = "/rules/tls-keywords.html#tls-alpn";
69  sigmatch_table[DETECT_TLS_ALPN].Setup = DetectTlsAlpnSetup;
72 
73  DetectAppLayerMultiRegister("tls.alpn", ALPROTO_TLS, SIG_FLAG_TOSERVER, 0, TlsAlpnGetData, 2,
76  "tls.alpn", ALPROTO_TLS, SIG_FLAG_TOCLIENT, 0, TlsAlpnGetData, 2, TLS_STATE_CERT_READY);
77 
78  DetectBufferTypeSetDescriptionByName("tls.alpn", "TLS APLN");
79 
81 
82  g_tls_alpn_buffer_id = DetectBufferTypeGetByName("tls.alpn");
83 }
84 
85 /**
86  * \brief This function setup the tls.alpn 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 DetectTlsAlpnSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
96 {
97  if (DetectBufferSetActiveList(de_ctx, s, g_tls_alpn_buffer_id) < 0)
98  return -1;
99 
101  return -1;
102 
103  return 0;
104 }
105 
106 static InspectionBuffer *TlsAlpnGetData(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  if (flags & STREAM_TOSERVER) {
119  connp = &ssl_state->client_connp;
120  } else {
121  connp = &ssl_state->server_connp;
122  }
123 
124  if (TAILQ_EMPTY(&connp->alpns)) {
126  return NULL;
127  }
128 
129  SSLAlpns *a;
130  if (idx == 0) {
131  a = TAILQ_FIRST(&connp->alpns);
132  } else {
133  // TODO optimize ?
134  a = TAILQ_FIRST(&connp->alpns);
135  for (uint32_t i = 0; i < idx; i++) {
136  a = TAILQ_NEXT(a, next);
137  }
138  }
139  if (a == NULL) {
141  return NULL;
142  }
143 
144  InspectionBufferSetupMulti(buffer, transforms, a->alpn, a->size);
145  buffer->flags = DETECT_CI_FLAGS_SINGLE;
146 
147  SCReturnPtr(buffer, "InspectionBuffer");
148 }
detect-engine-uint.h
SigTableElmt_::url
const char * url
Definition: detect.h:1307
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:1512
SigTableElmt_::desc
const char * desc
Definition: detect.h:1306
sigmatch_table
SigTableElmt * sigmatch_table
Definition: detect-parse.c:127
flow-util.h
SigTableElmt_::name
const char * name
Definition: detect.h:1304
InspectionBuffer::initialized
bool initialized
Definition: detect.h:377
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
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:1356
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:1298
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:1043
SSLAlpns_::size
uint32_t size
Definition: app-layer-ssl.h:231
SSLAlpns_
Definition: app-layer-ssl.h:229
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:1289
detect-engine-prefilter.h
DetectBufferTypeGetByName
int DetectBufferTypeGetByName(const char *name)
Definition: detect-engine.c:1093
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:2201
SIG_FLAG_TOSERVER
#define SIG_FLAG_TOSERVER
Definition: detect.h:267
InspectionBufferSetupMultiEmpty
void InspectionBufferSetupMultiEmpty(InspectionBuffer *buffer)
setup the buffer empty
Definition: detect-engine.c:1567
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:1093
util-print.h
SCEnter
#define SCEnter(...)
Definition: util-debug.h:271
detect-engine-mpm.h
detect.h
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
detect-engine-content-inspection.h
DETECT_TLS_ALPN
@ DETECT_TLS_ALPN
Definition: detect-engine-register.h:144
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
DetectTlsAlpnRegister
void DetectTlsAlpnRegister(void)
Registration function for keyword: tls.alpn.
Definition: detect-tls-alpn.c:64
SSLAlpns_::alpn
uint8_t alpn[]
Definition: app-layer-ssl.h:232
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:1580
detect-tls-alpn.h
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:601
TLS_STATE_IN_PROGRESS
@ TLS_STATE_IN_PROGRESS
Definition: app-layer-ssl.h:77
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:1520
SIGMATCH_NOOPT
#define SIGMATCH_NOOPT
Definition: detect.h:1488
DetectBufferTypeSetDescriptionByName
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
Definition: detect-engine.c:1190
flow.h
flow-var.h
app-layer-ssl.h
app-layer.h