blob: 0eae9ed8545c3a87099447f6d478088f253e2467 [file] [log] [blame]
Giovanni Bajoe292e932012-04-22 14:32:02 +02001
2#include "dnsmasq.h"
3#include <assert.h>
4
5#define SERIAL_UNDEF -100
6#define SERIAL_EQ 0
7#define SERIAL_LT -1
8#define SERIAL_GT 1
9
10#define countof(x) (long)(sizeof(x) / sizeof(x[0]))
11#define MIN(a,b) ((a) < (b) ? (a) : (b))
12
13/*
14 * vtable for a signature verification algorithm.
15 *
16 * Each algorithm verifies that a certain signature over a (possibly non-contigous)
17 * array of data has been made with the specified key.
18 *
19 * Sample of usage:
20 *
21 * // First, set the signature we need to check. Notice: data is not copied
22 * // nor consumed, so the pointer must stay valid.
23 * alg->set_signature(sig, 16);
24 *
25 * // Second, push the data in; data is consumed immediately, so the buffer
26 * // can be freed or modified.
27 * alg->begin_data();
28 * alg->add_data(buf1, 123);
29 * alg->add_data(buf2, 45);
30 * alg->add_data(buf3, 678);
31 * alg->end_data();
32 *
33 * // Third, verify if we got the correct key for this signature.
34 * alg->verify(key1, 16);
35 * alg->verify(key2, 16);
36 */
37typedef struct
38{
39 int (*set_signature)(unsigned char *data, unsigned len);
40 void (*begin_data)(void);
41 void (*add_data)(void *data, unsigned len);
42 void (*end_data)(void);
43 int (*verify)(unsigned char *key, unsigned key_len);
44} VerifyAlg;
45
Giovanni Bajo970ce222012-04-22 15:22:07 +020046/* Updated registry that merges various RFCs:
47 https://www.iana.org/assignments/dns-sec-alg-numbers/dns-sec-alg-numbers.xml */
48static const VerifyAlg valgs[] =
Giovanni Bajoe292e932012-04-22 14:32:02 +020049{
Giovanni Bajo970ce222012-04-22 15:22:07 +020050 {0,0,0,0,0}, /* 0: reserved */
51 {0,0,0,0,0}, /* 1: RSAMD5 */
52 {0,0,0,0,0}, /* 2: DH */
53 {0,0,0,0,0}, /* 3: DSA */
54 {0,0,0,0,0}, /* 4: ECC */
55 {0,0,0,0,0}, /* 5: RSASHA1 */
56 {0,0,0,0,0}, /* 6: DSA-NSEC3-SHA1 */
57 {0,0,0,0,0}, /* 7: RSASHA1-NSEC3-SHA1 */
58 {0,0,0,0,0}, /* 8: RSASHA256 */
59 {0,0,0,0,0}, /* 9: unassigned */
60 {0,0,0,0,0}, /* 10: RSASHA512 */
61 {0,0,0,0,0}, /* 11: unassigned */
62 {0,0,0,0,0}, /* 12: ECC-GOST */
63 {0,0,0,0,0}, /* 13: ECDSAP256SHA256 */
64 {0,0,0,0,0}, /* 14: ECDSAP384SHA384 */
Giovanni Bajoe292e932012-04-22 14:32:02 +020065};
66
67/* Implement RFC1982 wrapped compare for 32-bit numbers */
68static int serial_compare_32(unsigned long s1, unsigned long s2)
69{
70 if (s1 == s2)
71 return SERIAL_EQ;
72
73 if ((s1 < s2 && (s2 - s1) < (1UL<<31)) ||
74 (s1 > s2 && (s1 - s2) > (1UL<<31)))
75 return SERIAL_LT;
76 if ((s1 < s2 && (s2 - s1) > (1UL<<31)) ||
77 (s1 > s2 && (s1 - s2) < (1UL<<31)))
78 return SERIAL_GT;
79 return SERIAL_UNDEF;
80}
81
82/* Extract a DNS name from wire format, without handling compression. This is
83 faster than extract_name() and does not require access to the full dns
84 packet. */
85static int extract_name_no_compression(unsigned char *rr, int maxlen, char *buf)
86{
87 unsigned char *start=rr, *end = rr+maxlen;
88 int count;
89
90 while (rr < end && *rr != 0)
91 {
92 count = *rr++;
93 while (count-- >= 0 && rr < end)
94 {
95 *buf = *rr++;
96 if (*buf >= 'A' && *buf <= 'Z')
97 *buf += 'a' - 'A';
98 buf++;
99 }
100 *buf++ = '.';
101 }
102 *buf = 0;
103 if (rr == end)
104 return 0;
105 return rr-start;
106}
107
108/* Check whether today/now is between date_start and date_end */
109static int check_date_range(unsigned long date_start, unsigned long date_end)
110{
111 /* TODO: double-check that time(0) is the correct time we are looking for */
112 /* TODO: dnssec requires correct timing; implement SNTP in dnsmasq? */
113 unsigned long curtime = time(0);
114
115 /* We must explicitly check against wanted values, because of SERIAL_UNDEF */
116 if (serial_compare_32(curtime, date_start) != SERIAL_GT)
117 return 0;
118 if (serial_compare_32(curtime, date_end) != SERIAL_LT)
119 return 0;
120 return 1;
121}
122
123/* Sort RRs within a RRset in canonical order, according to RFC4034, ยง6.3
124 Notice that the RRDATA sections have been already normalized, so a memcpy
125 is sufficient.
126 NOTE: r1/r2 point immediately after the owner name. */
127static int rrset_canonical_order(const void *r1, const void *r2)
128{
129 int r1len, r2len, res;
130 const unsigned char *pr1=r1, *pr2=r2;
131
132 pr1 += 8; pr2 += 8;
133 GETSHORT(r1len, pr1); GETSHORT(r2len, pr2);
134
135 /* Lexicographically compare RDATA (thus, if equal, smaller length wins) */
136 res = memcmp(pr1, pr2, MIN(r1len, r2len));
137 if (res == 0)
138 {
139 if (r1len < r2len)
140 return -1;
141 else
142 /* NOTE: RFC2181 says that an RRset is not allowed to contain duplicate
143 records. If it happens, it is a protocol error and anything goes. */
144 return 1;
145 }
146
147 return res;
148}
149
150static int validate_rrsig(struct dns_header *header, size_t pktlen,
151 unsigned char *reply, int count, char *owner,
152 int sigclass, int sigrdlen, unsigned char *sig)
153{
154 int i, res;
155 int sigtype, sigalg, siglbl;
156 unsigned char *sigrdata = sig;
157 unsigned long sigttl, date_end, date_start;
158 unsigned char* p = reply;
159 char* signer_name = daemon->namebuff;
160 int keytag;
161 void *rrset[16]; /* TODO: max RRset size? */
162 int rrsetidx = 0;
163
164 if (sigrdlen < 18)
165 return 0;
166 GETSHORT(sigtype, sig);
167 sigalg = *sig++;
168 siglbl = *sig++;
169 GETLONG(sigttl, sig);
170 GETLONG(date_end, sig);
171 GETLONG(date_start, sig);
172 GETSHORT(keytag, sig);
173 sigrdlen -= 18;
174
175 if (sigalg >= countof(valgs) || !valgs[sigalg].set_signature)
176 {
177 printf("RRSIG algorithm not supported: %d\n", sigalg);
178 return 0;
179 }
180
181 if (!check_date_range(ntohl(date_start), ntohl(date_end)))
182 {
183 printf("RRSIG outside date range\n");
184 return 0;
185 }
186
187 /* Iterate within the answer and find the RRsets matching the current RRsig */
188 for (i = 0; i < count; ++i)
189 {
190 int qtype, qclass, rdlen;
191 if (!(res = extract_name(header, pktlen, &p, owner, 0, 10)))
192 return 0;
193 rrset[rrsetidx] = p;
194 GETSHORT(qtype, p);
195 GETSHORT(qclass, p);
196 p += 4; /* skip ttl */
197 GETSHORT(rdlen, p);
198 if (res == 1 && qtype == sigtype && qclass == sigclass)
199 {
200 ++rrsetidx;
201 assert(rrsetidx < countof(rrset));
202 /* TODO: here we should convert to lowercase domain names within
203 RDATA. We can do it in place. */
204 }
205 p += rdlen;
206 }
207
208 /* Sort RRset records in canonical order. */
209 qsort(rrset, rrsetidx, sizeof(void*), rrset_canonical_order);
210
211 /* Extract the signer name (we need to query DNSKEY of this name) */
212 if (!(res = extract_name_no_compression(sig, sigrdlen, signer_name)))
213 return 0;
214 sig += res; sigrdlen -= res;
215
216 /* Now initialize the signature verification algorithm and process the whole
217 RRset */
218 const VerifyAlg *alg = &valgs[sigalg];
219 if (!alg->set_signature(sig, sigrdlen))
220 return 0;
221
222 alg->begin_data();
223 alg->add_data(sigrdata, 18);
224 alg->add_data(signer_name, strlen(signer_name)-1); /* remove trailing dot */
225 for (i = 0; i < rrsetidx; ++i)
226 {
227 int rdlen;
228
229 alg->add_data(owner, strlen(owner));
230 alg->add_data(&sigtype, 2);
231 alg->add_data(&sigclass, 2);
232 alg->add_data(&sigttl, 4);
233
234 p = (unsigned char*)(rrset[i]);
235 p += 8;
236 GETSHORT(rdlen, p);
237 alg->add_data(p-2, rdlen+2);
238 }
239 alg->end_data();
240
241 /* TODO: now we need to fetch the DNSKEY of signer_name with the specified
242 keytag, and check whether it validates with the current algorithm. */
243 /*
244 pseudo-code:
245
246 char *key; int keylen;
247 if (!fetch_dnskey(signer_name, keytag, &key, &keylen))
248 return 0;
249 return alg->verify(key, keylen);
250 */
251 return 0;
252}
253
254
255int dnssec_validate(struct dns_header *header, size_t pktlen)
256{
257 unsigned char *p, *reply;
258 char *owner = daemon->namebuff;
259 int i, qtype, qclass, rdlen;
260 unsigned long ttl;
261
262 if (header->ancount == 0)
263 return 0;
264 if (!(reply = p = skip_questions(header, pktlen)))
265 return 0;
266 for (i = 0; i < ntohs(header->ancount); i++)
267 {
268 if (!extract_name(header, pktlen, &p, owner, 1, 10))
269 return 0;
270 GETSHORT(qtype, p);
271 GETSHORT(qclass, p);
272 GETLONG(ttl, p);
273 GETSHORT(rdlen, p);
274 if (qtype == T_RRSIG)
275 {
276 printf("RRSIG found\n");
277 /* TODO: missing logic. We should only validate RRSIGs for which we
278 have a valid DNSKEY that is referenced by a DS record upstream.
279 There is a memory vs CPU conflict here; should we validate everything
280 to save memory and thus waste CPU, or better first acquire all information
281 (wasting memory) and then doing the minimum CPU computations required? */
282 validate_rrsig(header, pktlen, reply, ntohs(header->ancount), owner, qclass, rdlen, p);
283 }
284 p += rdlen;
285 }
286 return 1;
287}