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"
34#include "detect-engine-mpm.h"
37#include "detect-content.h"
38#include "detect-tls-alpn.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
55static int DetectTlsAlpnSetup(DetectEngineCtx *, Signature *, const char *);
56static int g_tls_alpn_buffer_id = 0;
57
58static bool TlsAlpnGetData(DetectEngineThreadCtx *det_ctx, const void *txv, const uint8_t flags,
59 uint32_t idx, const uint8_t **buf, uint32_t *buf_len)
60{
61 SCEnter();
62
63 const SSLState *ssl_state = (SSLState *)txv;
64 const SSLStateConnp *connp;
65 CStringData d;
66
67 if (flags & STREAM_TOSERVER) {
68 connp = &ssl_state->client_connp;
69 } else {
70 connp = &ssl_state->server_connp;
71 }
72
73 if (SCTLSHandshakeGetALPN(connp->hs, idx, &d)) {
74 *buf = d.data;
75 *buf_len = (uint32_t)d.len;
76 return true;
77 } else {
78 return false;
79 }
80}
81
82/**
83 * \brief Registration function for keyword: tls.alpn
84 */
86{
88 sigmatch_table[DETECT_TLS_ALPN].desc = "sticky buffer to match the TLS ALPN buffer";
89 sigmatch_table[DETECT_TLS_ALPN].url = "/rules/tls-keywords.html#tls-alpn";
90 sigmatch_table[DETECT_TLS_ALPN].Setup = DetectTlsAlpnSetup;
93
95 TLS_STATE_CLIENT_HELLO_DONE, TlsAlpnGetData, 2);
97 "tls.alpn", ALPROTO_TLS, SIG_FLAG_TOCLIENT, TLS_STATE_SERVER_HELLO, TlsAlpnGetData, 2);
98
99 DetectBufferTypeSetDescriptionByName("tls.alpn", "TLS APLN");
100
102
103 g_tls_alpn_buffer_id = DetectBufferTypeGetByName("tls.alpn");
104}
105
106/**
107 * \brief This function setup the tls.alpn sticky buffer keyword
108 *
109 * \param de_ctx Pointer to the Detect Engine Context
110 * \param s Pointer to the Signature to which the keyword belongs
111 * \param str Should hold an empty string always
112 *
113 * \retval 0 On success
114 * \retval -1 On failure
115 */
116static int DetectTlsAlpnSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
117{
118 if (SCDetectBufferSetActiveList(de_ctx, s, g_tls_alpn_buffer_id) < 0)
119 return -1;
120
122 return -1;
123
124 return 0;
125}
@ ALPROTO_TLS
@ TLS_STATE_CLIENT_HELLO_DONE
@ TLS_STATE_SERVER_HELLO
uint8_t flags
Definition decode-gre.h:0
int SCDetectBufferSetActiveList(DetectEngineCtx *de_ctx, Signature *s, const int list)
void DetectBufferTypeSetDescriptionByName(const char *name, const char *desc)
void DetectBufferTypeSupportsMultiInstance(const char *name)
void DetectAppLayerMultiRegister(const char *name, AppProto alproto, uint32_t dir, int progress, InspectionMultiBufferGetDataPtr GetData, int priority)
int DetectBufferTypeGetByName(const char *name)
int SCDetectSignatureSetAppProto(Signature *s, AppProto alproto)
SigTableElmt * sigmatch_table
void DetectTlsAlpnRegister(void)
Registration function for keyword: tls.alpn.
#define SIGMATCH_NOOPT
Definition detect.h:1651
#define SIG_FLAG_TOCLIENT
Definition detect.h:272
#define SIGMATCH_INFO_STICKY_BUFFER
Definition detect.h:1676
#define SIG_FLAG_TOSERVER
Definition detect.h:271
DetectEngineCtx * de_ctx
main detection engine ctx
Definition detect.h:932
HandshakeParams * hs
SSLv[2.0|3.[0|1|2|3]] state structure.
SSLStateConnp server_connp
SSLStateConnp client_connp
const char * url
Definition detect.h:1462
int(* Setup)(DetectEngineCtx *, Signature *, const char *)
Definition detect.h:1441
uint16_t flags
Definition detect.h:1450
const char * desc
Definition detect.h:1461
const char * name
Definition detect.h:1459
Signature container.
Definition detect.h:668
#define str(s)
#define SCEnter(...)
Definition util-debug.h:277