-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnssec.c
319 lines (292 loc) · 6.59 KB
/
dnssec.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include "dnssec.h"
#define LEN(a) (sizeof(a) / sizeof((a)[0]))
static size_t
find_string(const char *str[], size_t len, const char *s)
{
for (size_t i = 1; i < len; ++i) {
if (str[i] && strcmp(str[i], s) == 0)
return i;
}
return 0;
}
static const char *type_names[] = {
[TYPE_A] = "A",
[TYPE_NS] = "NS",
[TYPE_CNAME] = "CNAME",
[TYPE_SOA] = "SOA",
[TYPE_MX] = "MX",
[TYPE_TXT] = "TXT",
[TYPE_AAAA] = "AAAA",
[TYPE_SRV] = "SRV",
[TYPE_DS] = "DS",
[TYPE_SSHFP] = "SSHFP",
[TYPE_RRSIG] = "RRSIG",
[TYPE_NSEC] = "NSEC",
[TYPE_DNSKEY] = "DNSKEY",
[TYPE_TLSA] = "TLSA",
};
int
type_from_string(const char *s)
{
int type = find_string(type_names, LEN(type_names), s);
if (!type && strncmp(s, "TYPE", 4) == 0) {
char *end;
type = strtoul(s + 4, &end, 10);
if (*end)
type = 0;
}
return type;
}
const char *
type_to_string(int type)
{
static char buf[4 + 5 + 1];
if (type < LEN(type_names) && type_names[type])
return type_names[type];
snprintf(buf, sizeof(buf), "TYPE%d", type);
return buf;
}
static const char *class_names[] = {
[CLASS_IN] = "IN",
};
int
class_from_string(const char *s)
{
int class = find_string(class_names, LEN(class_names), s);
if (!class && strncmp(s, "CLASS", 5) == 0) {
char *end;
class = strtoul(s + 5, &end, 10);
if (*end)
class = 0;
}
return class;
}
const char *
class_to_string(int class)
{
static char buf[5 + 5 + 1];
if (class < LEN(class_names) && class_names[class])
return class_names[class];
snprintf(buf, sizeof(buf), "CLASS%d", class);
return buf;
}
static const char *algorithm_names[] = {
[ALGORITHM_RSASHA1] = "RSASHA1",
[ALGORITHM_RSASHA256] = "RSASHA256",
[ALGORITHM_RSASHA512] = "RSASHA512",
[ALGORITHM_ECDSAP256SHA256] = "ECDSAP256SHA256",
[ALGORITHM_ECDSAP384SHA384] = "ECDSAP384SHA384",
};
int
algorithm_from_string(const char *s)
{
size_t i = find_string(algorithm_names, LEN(algorithm_names), s);
if (!i)
errx(1, "unknown algorithm '%s'", s);
return i;
}
const char *
algorithm_to_string(int algorithm)
{
return algorithm_names[algorithm];
}
static const char *digest_names[] = {
[DIGEST_SHA1] = "SHA1",
[DIGEST_SHA256] = "SHA256",
[DIGEST_SHA384] = "SHA384",
};
int
digest_from_string(const char *s)
{
size_t i = find_string(digest_names, LEN(digest_names), s);
if (!i)
errx(1, "unknown digest '%s'", s);
return i;
}
size_t
dname_parse(const char *s, char **end, unsigned char dn[static DNAME_MAX], const unsigned char *origin, size_t origin_len)
{
size_t len = 0, label_len;
int rel;
if (s[0] == '@') {
++s;
rel = 1;
goto out;
}
for (;;) {
label_len = 0;
switch (s[0]) {
case '.':
if (len != 0)
return 0;
++s;
rel = 0;
goto out;
case ' ':
case '\t':
case '\0':
if (len == 0)
return 0;
rel = 0;
goto out;
case '"':
while (*++s != '"') {
if (*s == '\\')
++s;
if (len + label_len == DNAME_MAX - 2 || label_len == LABEL_MAX)
return 0;
dn[len + 1 + label_len++] = tolower(*s);
}
++s;
break;
default:
while (*s && *s != ' ' && *s != '\t' && *s != '.') {
int c;
if (*s == '\\' && isdigit(*++s)) {
if (!isdigit(s[1]) || !isdigit(s[2]))
return 0;
c = (s[0] - '0') * 100 + (s[1] - '0') * 10 + (s[2] - '0');
s += 3;
} else if (*s) {
c = *s++;
} else {
return 0;
}
if (len + label_len == DNAME_MAX - 2 || label_len == LABEL_MAX)
return 0;
dn[len + 1 + label_len++] = tolower(c);
}
}
if (label_len == 0)
return 0;
dn[len] = label_len;
len += 1 + label_len;
if (*s != '.') {
rel = 1;
break;
}
++s;
}
out:
if (*s && *s != ' ' && *s != '\t')
return 0;
if (rel) {
if (!origin_len || len + origin_len > DNAME_MAX)
return 0;
memcpy(dn + len, origin, origin_len);
len += origin_len;
} else {
dn[len++] = 0;
}
if (end)
*end = (char *)s;
return len;
}
int
dname_compare(const unsigned char *n1, const unsigned char *n2)
{
unsigned char l1[DNAME_MAX], l2[DNAME_MAX], *p1 = l1, *p2 = l2;
size_t l;
int r;
/* get label lengths */
for (; *n1; n1 += 1 + *n1)
*p1++ = *n1;
for (; *n2; n2 += 1 + *n2)
*p2++ = *n2;
for (; p1 > l1 && p2 > l2; --n1, --n2) {
l = *--p1 <= *--p2 ? *p1 : *p2;
r = memcmp(n1 -= *p1, n2 -= *p2, l);
if (r != 0)
return r;
if (l < *p1)
return 1;
if (l < *p2)
return -1;
}
return (p1 > l1) - (p2 > l2);
}
int
dname_print(const unsigned char *dn)
{
for (; *dn; dn += 1 + *dn) {
if (fwrite(dn + 1, 1, *dn, stdout) != *dn || fputc('.', stdout) == EOF)
return -1;
}
return 0;
}
int
dname_labels(const unsigned char *dn)
{
int labels = 0;
if (dn[0] == 1 && dn[1] == '*')
dn += 2;
for (; *dn; dn += 1 + *dn)
++labels;
return labels;
}
struct dnskey *
dnskey_new(unsigned flags, const struct key *sk)
{
br_ec_public_key pk;
unsigned char buf[BR_EC_KBUF_PUB_MAX_SIZE];
struct dnskey *k;
switch (sk->algorithm) {
case ALGORITHM_RSASHA1:
case ALGORITHM_RSASHA256:
case ALGORITHM_RSASHA512: {
uint32_t e = br_rsa_compute_pubexp_get_default()(&sk->rsa);
if (!e)
errx(1, "failed to compute public exponent of RSA key");
size_t nlen = br_rsa_compute_modulus_get_default()(NULL, &sk->rsa);
if (!nlen)
errx(1, "failed to compute public modulus of RSA key");
if (!(k = malloc(sizeof(*k) + 5 + nlen)))
err(1, "malloc");
/* leading zeros in exponent are prohibited */
for (k->data[0] = 4; !(e & 0xff000000); --k->data[0])
e <<= 8;
k->data_len = 1 + k->data[0] + nlen;
memcpy(k->data + 1, BE32(e), k->data[0]);
br_rsa_compute_modulus_get_default()(k->data + 1 + k->data[0], &sk->rsa);
break;
}
case ALGORITHM_ECDSAP256SHA256:
if (br_ec_compute_pub(br_ec_get_default(), &pk, buf, &sk->ec) != 65)
errx(1, "unexpected public key size");
if (!(k = malloc(sizeof(*k) + 64)))
err(1, "malloc");
k->data_len = 64;
memcpy(k->data, buf + 1, 64);
break;
case ALGORITHM_ECDSAP384SHA384:
if (br_ec_compute_pub(br_ec_get_default(), &pk, buf, &sk->ec) != 97)
errx(1, "unexpected public key size");
if (!(k = malloc(sizeof(*k) + 96)))
err(1, "malloc");
k->data_len = 96;
memcpy(k->data, buf + 1, 96);
break;
default:
errx(1, "unsupported algorithm %s", algorithm_to_string(sk->algorithm));
}
k->flags = flags;
k->algorithm = sk->algorithm;
k->protocol = 3;
return k;
}
unsigned
dnskey_tag(const struct dnskey *k)
{
unsigned long x;
const unsigned char *p;
size_t i;
x = k->flags + (k->protocol << 8) + k->algorithm;
for (i = 0, p = k->data; i < k->data_len; ++p, ++i)
x += i & 1 ? *p : (unsigned long)*p << 8;
return (x + (x >> 16)) & 0xffff;
}