Coverage Report

Created: 2026-05-16 07:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/suricata/src/detect-tls-cert-serial.c
Line
Count
Source
1
/* Copyright (C) 2017-2022 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_serial 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-serial.h"
52
53
#include "util-unittest.h"
54
#include "util-unittest-helper.h"
55
56
static int DetectTlsSerialSetup(DetectEngineCtx *, Signature *, const char *);
57
#ifdef UNITTESTS
58
static void DetectTlsSerialRegisterTests(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 void DetectTlsSerialSetupCallback(
65
        const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map);
66
static bool DetectTlsSerialValidateCallback(
67
        const Signature *s, const char **sigerror, const DetectBufferType *dbt);
68
static int g_tls_cert_serial_buffer_id = 0;
69
70
/**
71
 * \brief Registration function for keyword: tls.cert_serial
72
 */
73
void DetectTlsSerialRegister(void)
74
73
{
75
73
    sigmatch_table[DETECT_TLS_CERT_SERIAL].name = "tls.cert_serial";
76
73
    sigmatch_table[DETECT_TLS_CERT_SERIAL].alias = "tls_cert_serial";
77
73
    sigmatch_table[DETECT_TLS_CERT_SERIAL].desc =
78
73
            "sticky buffer to match the TLS cert serial buffer";
79
73
    sigmatch_table[DETECT_TLS_CERT_SERIAL].url = "/rules/tls-keywords.html#tls-cert-serial";
80
73
    sigmatch_table[DETECT_TLS_CERT_SERIAL].Setup = DetectTlsSerialSetup;
81
#ifdef UNITTESTS
82
    sigmatch_table[DETECT_TLS_CERT_SERIAL].RegisterTests = DetectTlsSerialRegisterTests;
83
#endif
84
73
    sigmatch_table[DETECT_TLS_CERT_SERIAL].flags |= SIGMATCH_NOOPT;
85
73
    sigmatch_table[DETECT_TLS_CERT_SERIAL].flags |= SIGMATCH_INFO_STICKY_BUFFER;
86
87
73
    DetectAppLayerInspectEngineRegister("tls.cert_serial", ALPROTO_TLS, SIG_FLAG_TOCLIENT,
88
73
            TLS_STATE_SERVER_CERT_DONE, DetectEngineInspectBufferGeneric, GetData);
89
90
73
    DetectAppLayerMpmRegister("tls.cert_serial", SIG_FLAG_TOCLIENT, 2, PrefilterGenericMpmRegister,
91
73
            GetData, ALPROTO_TLS, TLS_STATE_SERVER_CERT_DONE);
92
93
73
    DetectAppLayerInspectEngineRegister("tls.cert_serial", ALPROTO_TLS, SIG_FLAG_TOSERVER,
94
73
            TLS_STATE_CLIENT_CERT_DONE, DetectEngineInspectBufferGeneric, GetData);
95
96
73
    DetectAppLayerMpmRegister("tls.cert_serial", SIG_FLAG_TOSERVER, 2, PrefilterGenericMpmRegister,
97
73
            GetData, ALPROTO_TLS, TLS_STATE_CLIENT_CERT_DONE);
98
99
73
    DetectBufferTypeSetDescriptionByName("tls.cert_serial",
100
73
            "TLS certificate serial number");
101
102
73
    DetectBufferTypeRegisterSetupCallback("tls.cert_serial",
103
73
            DetectTlsSerialSetupCallback);
104
105
73
    DetectBufferTypeRegisterValidateCallback("tls.cert_serial",
106
73
            DetectTlsSerialValidateCallback);
107
108
73
    g_tls_cert_serial_buffer_id = DetectBufferTypeGetByName("tls.cert_serial");
109
73
}
110
111
/**
112
 * \brief this function setup the tls_cert_serial modifier keyword used in the rule
113
 *
114
 * \param de_ctx   Pointer to the Detection Engine Context
115
 * \param s        Pointer to the Signature to which the current keyword belongs
116
 * \param str      Should hold an empty string always
117
 *
118
 * \retval 0  On success
119
 * \retval -1 On failure
120
 */
121
static int DetectTlsSerialSetup(DetectEngineCtx *de_ctx, Signature *s, const char *str)
122
5.24k
{
123
5.24k
    if (SCDetectBufferSetActiveList(de_ctx, s, g_tls_cert_serial_buffer_id) < 0)
124
4
        return -1;
125
126
5.24k
    if (SCDetectSignatureSetAppProto(s, ALPROTO_TLS) < 0)
127
67
        return -1;
128
129
5.17k
    return 0;
130
5.24k
}
131
132
static InspectionBuffer *GetData(DetectEngineThreadCtx *det_ctx,
133
        const DetectEngineTransforms *transforms, Flow *f,
134
        const uint8_t flow_flags, void *txv, const int list_id)
135
181
{
136
181
    InspectionBuffer *buffer = SCInspectionBufferGet(det_ctx, list_id);
137
181
    if (buffer->inspect == NULL) {
138
175
        const SSLState *ssl_state = (SSLState *)f->alstate;
139
175
        const SSLStateConnp *connp;
140
141
175
        if (flow_flags & STREAM_TOSERVER) {
142
93
            connp = &ssl_state->client_connp;
143
93
        } else {
144
82
            connp = &ssl_state->server_connp;
145
82
        }
146
147
175
        if (connp->cert0_serial == NULL) {
148
101
            return NULL;
149
101
        }
150
151
74
        const uint32_t data_len = connp->cert0_serial_len;
152
74
        const uint8_t *data = connp->cert0_serial;
153
154
74
        SCInspectionBufferSetupAndApplyTransforms(
155
74
                det_ctx, list_id, buffer, data, data_len, transforms);
156
74
    }
157
158
80
    return buffer;
159
181
}
160
161
static bool DetectTlsSerialValidateCallback(
162
        const Signature *s, const char **sigerror, const DetectBufferType *dbt)
163
2.39k
{
164
7.09k
    for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
165
4.87k
        if (s->init_data->buffers[x].id != (uint32_t)dbt->id)
166
4.58k
            continue;
167
298
        const SigMatch *sm = s->init_data->buffers[x].head;
168
1.62k
        for (; sm != NULL; sm = sm->next) {
169
1.50k
            if (sm->type != DETECT_CONTENT)
170
1.32k
                continue;
171
172
177
            const DetectContentData *cd = (DetectContentData *)sm->ctx;
173
174
177
            if (cd->flags & DETECT_CONTENT_NOCASE) {
175
3
                *sigerror = "tls.cert_serial should not be used together "
176
3
                            "with nocase, since the rule is automatically "
177
3
                            "uppercased anyway which makes nocase redundant.";
178
3
                SCLogWarning("rule %u: %s", s->id, *sigerror);
179
3
            }
180
181
            /* no need to worry about this if the content is short enough */
182
177
            if (cd->content_len <= 2)
183
102
                return true;
184
185
75
            uint32_t u;
186
1.03k
            for (u = 0; u < cd->content_len; u++)
187
1.00k
                if (cd->content[u] == ':')
188
47
                    return true;
189
190
28
            *sigerror = "No colon delimiters ':' detected in content after "
191
28
                        "tls.cert_serial. This rule will therefore never "
192
28
                        "match.";
193
28
            SCLogWarning("rule %u: %s", s->id, *sigerror);
194
195
28
            return false;
196
75
        }
197
298
    }
198
2.22k
    return true;
199
2.39k
}
200
201
static void DetectTlsSerialSetupCallback(
202
        const DetectEngineCtx *de_ctx, Signature *s, const DetectBufferType *map)
203
4.29k
{
204
14.5k
    for (uint32_t x = 0; x < s->init_data->buffer_index; x++) {
205
10.2k
        if (s->init_data->buffers[x].id != (uint32_t)g_tls_cert_serial_buffer_id)
206
9.61k
            continue;
207
661
        SigMatch *sm = s->init_data->buffers[x].head;
208
6.58k
        for (; sm != NULL; sm = sm->next) {
209
5.92k
            if (sm->type != DETECT_CONTENT)
210
4.50k
                continue;
211
212
1.42k
            DetectContentData *cd = (DetectContentData *)sm->ctx;
213
214
1.42k
            bool changed = false;
215
1.42k
            uint32_t u;
216
6.85k
            for (u = 0; u < cd->content_len; u++) {
217
5.43k
                if (islower(cd->content[u])) {
218
1.02k
                    cd->content[u] = u8_toupper(cd->content[u]);
219
1.02k
                    changed = true;
220
1.02k
                }
221
5.43k
            }
222
223
            /* recreate the context if changes were made */
224
1.42k
            if (changed) {
225
211
                SpmDestroyCtx(cd->spm_ctx);
226
211
                cd->spm_ctx =
227
211
                        SpmInitCtx(cd->content, cd->content_len, 1, de_ctx->spm_global_thread_ctx);
228
211
            }
229
1.42k
        }
230
661
    }
231
4.29k
}
232
233
#ifdef UNITTESTS
234
#include "tests/detect-tls-cert-serial.c"
235
#endif