/src/suricata8/src/detect-tls-cert-issuer.c
Line | Count | Source |
1 | | /* Copyright (C) 2007-2025 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-buffer.h" |
34 | | #include "detect-engine-mpm.h" |
35 | | #include "detect-engine-prefilter.h" |
36 | | #include "detect-content.h" |
37 | | #include "detect-pcre.h" |
38 | | |
39 | | #include "flow.h" |
40 | | #include "flow-util.h" |
41 | | #include "flow-var.h" |
42 | | |
43 | | #include "util-debug.h" |
44 | | #include "util-spm.h" |
45 | | #include "util-print.h" |
46 | | |
47 | | #include "stream-tcp.h" |
48 | | |
49 | | #include "app-layer.h" |
50 | | #include "app-layer-ssl.h" |
51 | | #include "detect-tls-cert-issuer.h" |
52 | | |
53 | | #include "util-unittest.h" |
54 | | #include "util-unittest-helper.h" |
55 | | |
56 | | static int DetectTlsIssuerSetup(DetectEngineCtx *, Signature *, const char *); |
57 | | #ifdef UNITTESTS |
58 | | static void DetectTlsIssuerRegisterTests(void); |
59 | | #endif |
60 | | static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx, |
61 | | const DetectEngineTransforms *transforms, |
62 | | Flow *f, const uint8_t flow_flags, |
63 | | void *txv, const int list_id); |
64 | | static int g_tls_cert_issuer_buffer_id = 0; |
65 | | |
66 | | /** |
67 | | * \brief Registration function for keyword: tls.cert_issuer |
68 | | */ |
69 | | void DetectTlsIssuerRegister(void) |
70 | 79 | { |
71 | 79 | sigmatch_table[DETECT_TLS_CERT_ISSUER].name = "tls.cert_issuer"; |
72 | 79 | sigmatch_table[DETECT_TLS_CERT_ISSUER].alias = "tls_cert_issuer"; |
73 | 79 | sigmatch_table[DETECT_TLS_CERT_ISSUER].desc = |
74 | 79 | "sticky buffer to match specifically and only on the TLS cert issuer buffer"; |
75 | 79 | sigmatch_table[DETECT_TLS_CERT_ISSUER].url = "/rules/tls-keywords.html#tls-cert-issuer"; |
76 | 79 | sigmatch_table[DETECT_TLS_CERT_ISSUER].Setup = DetectTlsIssuerSetup; |
77 | | #ifdef UNITTESTS |
78 | | sigmatch_table[DETECT_TLS_CERT_ISSUER].RegisterTests = DetectTlsIssuerRegisterTests; |
79 | | #endif |
80 | 79 | sigmatch_table[DETECT_TLS_CERT_ISSUER].flags |= SIGMATCH_NOOPT; |
81 | 79 | sigmatch_table[DETECT_TLS_CERT_ISSUER].flags |= SIGMATCH_INFO_STICKY_BUFFER; |
82 | | |
83 | 79 | DetectAppLayerInspectEngineRegister("tls.cert_issuer", ALPROTO_TLS, SIG_FLAG_TOSERVER, |
84 | 79 | TLS_STATE_CLIENT_CERT_DONE, DetectEngineInspectBufferGeneric, GetData); |
85 | | |
86 | 79 | DetectAppLayerMpmRegister("tls.cert_issuer", SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister, |
87 | 79 | GetData, ALPROTO_TLS, TLS_STATE_CLIENT_CERT_DONE); |
88 | | |
89 | 79 | DetectAppLayerInspectEngineRegister("tls.cert_issuer", ALPROTO_TLS, SIG_FLAG_TOCLIENT, |
90 | 79 | TLS_STATE_SERVER_CERT_DONE, DetectEngineInspectBufferGeneric, GetData); |
91 | | |
92 | 79 | DetectAppLayerMpmRegister("tls.cert_issuer", SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister, |
93 | 79 | GetData, ALPROTO_TLS, TLS_STATE_SERVER_CERT_DONE); |
94 | | |
95 | 79 | DetectBufferTypeSetDescriptionByName("tls.cert_issuer", |
96 | 79 | "TLS certificate issuer"); |
97 | | |
98 | 79 | g_tls_cert_issuer_buffer_id = DetectBufferTypeGetByName("tls.cert_issuer"); |
99 | 79 | } |
100 | | |
101 | | |
102 | | /** |
103 | | * \brief this function setup the tls_cert_issuer modifier keyword used in the rule |
104 | | * |
105 | | * \param de_ctx Pointer to the Detection Engine Context |
106 | | * \param s Pointer to the Signature to which the current keyword belongs |
107 | | * \param str Should hold an empty string always |
108 | | * |
109 | | * \retval 0 On success |
110 | | * \retval -1 On failure |
111 | | */ |
112 | | static int DetectTlsIssuerSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str) |
113 | 390 | { |
114 | 390 | if (SCDetectBufferSetActiveList(de_ctx, s, g_tls_cert_issuer_buffer_id) < 0) |
115 | 4 | return -1; |
116 | | |
117 | 386 | if (SCDetectSignatureSetAppProto(s, ALPROTO_TLS) < 0) |
118 | 135 | return -1; |
119 | | |
120 | 251 | return 0; |
121 | 386 | } |
122 | | |
123 | | static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx, |
124 | | const DetectEngineTransforms *transforms, Flow *f, |
125 | | const uint8_t flow_flags, void *txv, const int list_id) |
126 | 83 | { |
127 | 83 | InspectionBuffer *buffer = InspectionBufferGet(det_ctx, list_id); |
128 | 83 | if (buffer->inspect == NULL) { |
129 | 62 | const SSLState *ssl_state = (SSLState *)f->alstate; |
130 | 62 | const SSLStateConnp *connp; |
131 | 62 | if (flow_flags & STREAM_TOSERVER) { |
132 | 34 | connp = &ssl_state->client_connp; |
133 | 34 | } else { |
134 | 28 | connp = &ssl_state->server_connp; |
135 | 28 | } |
136 | | |
137 | 62 | if (connp->cert0_issuerdn == NULL) { |
138 | 38 | return NULL; |
139 | 38 | } |
140 | | |
141 | 24 | const uint32_t data_len = connp->cert0_issuerdn_len; |
142 | 24 | const uint8_t *data = connp->cert0_issuerdn; |
143 | | |
144 | 24 | InspectionBufferSetupAndApplyTransforms( |
145 | 24 | det_ctx, list_id, buffer, data, data_len, transforms); |
146 | 24 | } |
147 | | |
148 | 45 | return buffer; |
149 | 83 | } |
150 | | |
151 | | #ifdef UNITTESTS |
152 | | #include "tests/detect-tls-cert-issuer.c" |
153 | | #endif |