-
Notifications
You must be signed in to change notification settings - Fork 6
/
libxt_sslpin.c
219 lines (180 loc) · 5.95 KB
/
libxt_sslpin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/*
* libxt_sslpin.c
*
* Copyright (C) 2010-2013 fredburger (github.com/fredburger)
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program; if not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <xtables.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <string.h>
#include "xt_sslpin.h"
#include "hexutils.h"
/* parameter definitions */
static const struct option sslpin_mt_opts[] = {
{ .name = "pubkey", .has_arg = true, .val = 'p' },
{ .name = "debug", .has_arg = false, .val = 'd' },
{ NULL },
};
/* parse pubkey option (<alg>:<key-hex>) */
static bool parse_pubkey_opt(struct sslpin_mtruleinfo *mtruleinfo, char *optarg)
{
const struct sslpin_pubkeyalg *pubkeyalg = pubkeyalgs; /* see xt_sslpin.h */
char *colon, *hex;
int alg_len, pk_len, i;
__u8 *pk, *pk_max;
__u8 ms, ls;
/* find pubkeyalg */
colon = strchr(optarg, ':');
if (!colon) {
return false;
}
if (!(alg_len = colon - optarg)) {
return false;
}
for (i = 0; i < SSLPIN_PUBLIC_KEY_ALGS_CNT; i++, pubkeyalg++) {
if ((!strncmp(optarg, pubkeyalg->name, alg_len)) && (!pubkeyalg->name[alg_len])) {
break;
}
}
/* unknown algorithm */
if (i == SSLPIN_PUBLIC_KEY_ALGS_CNT) {
return false;
}
mtruleinfo->pk_alg = *pubkeyalg;
/* parse pubkey hex string to bytes */
hex = colon + 1;
pk = mtruleinfo->pk;
pk_max = pk + sizeof(mtruleinfo->pk);
while (*hex) {
if ((!hex[1]) || (pk == pk_max)) {
return false;
}
ms = hexc2int(hex[0]);
ls = hexc2int(hex[1]);
if ((ms > 15) || (ls > 15)) {
return false;
}
*pk++ = (ms << 4) | ls;
hex += 2;
}
pk_len = pk - mtruleinfo->pk;
if (pk_len < SSLPIN_MIN_PUBLIC_KEY_BYTELEN) {
return false;
}
mtruleinfo->pk_len = pk_len;
return true;
}
/* xtables_register_match() module init callback */ /* not needed */
/*
static void sslpin_mt_init(struct xt_entry_match *match)
{
}
*/
/* invoked by iptables -m sslpin -h */
static void sslpin_mt_help(void)
{
printf(
"sslpin match options:\n"
"[!] --pubkey\t<pubkey-alg>:<pubkey-hex>\n"
"\t\t\t\tSSL/TLS Certificate Public Key specification\n"
"\t\t\t\tpubkey-alg: either \"rsa\", \"ec\" or \"dsa\"\n"
" --debug\t\t\tverbose mode (see kernel log)\n"
"\n"
);
}
/* parse options */
static int sslpin_mt_parse(int c, char **argv, int invert, unsigned int *flags, const void *entry,
struct xt_entry_match **match)
{
struct sslpin_mtruleinfo *mtruleinfo = (struct sslpin_mtruleinfo*)(*match)->data;
switch (c) {
case 'p':
if (*flags) {
xtables_error(PARAMETER_PROBLEM, "sslpin: --pubkey can only be specified once");
}
if (!parse_pubkey_opt(mtruleinfo, optarg)) {
xtables_error(PARAMETER_PROBLEM, "sslpin: unable to parse --pubkey argument");
}
if (invert) {
mtruleinfo->flags |= SSLPIN_RULE_FLAG_INVERT;
}
*flags = 1; /* pubkey has been set, see sslpin_mt_check() */
break;
case 'd':
mtruleinfo->flags |= SSLPIN_RULE_FLAG_DEBUG;
break;
default:
return false;
}
return true;
}
/* check options after parsing */
static void sslpin_mt_check(unsigned int flags)
{
if (flags == 0) {
xtables_error(PARAMETER_PROBLEM, "sslpin: must specify --pubkey");
}
}
/* invoked for iptables --list; print options in human-friendly format */
static void sslpin_mt_print(const void *entry, const struct xt_entry_match *match, int numeric)
{
struct sslpin_mtruleinfo *mtruleinfo = (struct sslpin_mtruleinfo*)(match->data);
printf(" sslpin:");
if (mtruleinfo->flags & SSLPIN_RULE_FLAG_DEBUG) {
printf(" debug");
}
if (mtruleinfo->flags & SSLPIN_RULE_FLAG_INVERT) {
printf(" !");
}
printf(" alg: ");
printf("%s", mtruleinfo->pk_alg.name);
printf(" pk: (hex)");
}
/* invoked for iptables-save and iptables --list-rules; print options in exact format */
static void sslpin_mt_save(const void *entry, const struct xt_entry_match *match)
{
struct sslpin_mtruleinfo *mtruleinfo = (struct sslpin_mtruleinfo*)(match->data);
if (mtruleinfo->flags & SSLPIN_RULE_FLAG_DEBUG) {
printf(" --debug");
}
if (mtruleinfo->flags & SSLPIN_RULE_FLAG_INVERT) {
printf(" !");
}
printf(" --pubkey ");
printf("%s", mtruleinfo->pk_alg.name);
printf(":");
printhex(mtruleinfo->pk, mtruleinfo->pk_len);
}
/* xtables_register_match() module info */
static struct xtables_match sslpin_mt_reg = {
.name = "sslpin",
.family = NFPROTO_IPV4,
.version = XTABLES_VERSION,
.revision = 0,
.size = SSLPIN_MTRULEINFO_KERN_SIZE,
.userspacesize = SSLPIN_MTRULEINFO_USER_SIZE,
.help = sslpin_mt_help,
.parse = sslpin_mt_parse,
.final_check = sslpin_mt_check,
.print = sslpin_mt_print,
.save = sslpin_mt_save,
.extra_opts = sslpin_mt_opts,
/* .init = sslpin_mt_init, */ /* not needed */
};
/* init function (module loaded by iptables) */
void _init(void)
{
xtables_register_match(&sslpin_mt_reg);
}