dnssectool.c revision 1.3 1 1.2 christos /* $NetBSD: dnssectool.c,v 1.3 2019/01/09 16:54:59 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 1.1 christos *
6 1.1 christos * This Source Code Form is subject to the terms of the Mozilla Public
7 1.1 christos * License, v. 2.0. If a copy of the MPL was not distributed with this
8 1.1 christos * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 1.1 christos *
10 1.1 christos * See the COPYRIGHT file distributed with this work for additional
11 1.1 christos * information regarding copyright ownership.
12 1.1 christos */
13 1.1 christos
14 1.1 christos /*! \file */
15 1.1 christos
16 1.1 christos /*%
17 1.1 christos * DNSSEC Support Routines.
18 1.1 christos */
19 1.1 christos
20 1.1 christos #include <config.h>
21 1.1 christos
22 1.3 christos #include <inttypes.h>
23 1.3 christos #include <stdbool.h>
24 1.1 christos #include <stdlib.h>
25 1.1 christos
26 1.1 christos #ifdef _WIN32
27 1.1 christos #include <Winsock2.h>
28 1.1 christos #endif
29 1.1 christos
30 1.1 christos #include <isc/base32.h>
31 1.1 christos #include <isc/buffer.h>
32 1.1 christos #include <isc/commandline.h>
33 1.1 christos #include <isc/dir.h>
34 1.1 christos #include <isc/file.h>
35 1.1 christos #include <isc/heap.h>
36 1.1 christos #include <isc/list.h>
37 1.1 christos #include <isc/mem.h>
38 1.1 christos #include <isc/platform.h>
39 1.1 christos #include <isc/print.h>
40 1.1 christos #include <isc/string.h>
41 1.1 christos #include <isc/time.h>
42 1.1 christos #include <isc/util.h>
43 1.1 christos
44 1.1 christos #include <dns/db.h>
45 1.1 christos #include <dns/dbiterator.h>
46 1.1 christos #include <dns/dnssec.h>
47 1.1 christos #include <dns/fixedname.h>
48 1.1 christos #include <dns/keyvalues.h>
49 1.1 christos #include <dns/log.h>
50 1.1 christos #include <dns/name.h>
51 1.1 christos #include <dns/nsec.h>
52 1.1 christos #include <dns/nsec3.h>
53 1.1 christos #include <dns/rdatastruct.h>
54 1.1 christos #include <dns/rdataclass.h>
55 1.1 christos #include <dns/rdataset.h>
56 1.1 christos #include <dns/rdatasetiter.h>
57 1.1 christos #include <dns/rdatatype.h>
58 1.1 christos #include <dns/result.h>
59 1.1 christos #include <dns/secalg.h>
60 1.1 christos #include <dns/time.h>
61 1.1 christos
62 1.1 christos #include "dnssectool.h"
63 1.1 christos
64 1.1 christos extern int verbose;
65 1.1 christos extern const char *program;
66 1.1 christos
67 1.1 christos static fatalcallback_t *fatalcallback = NULL;
68 1.1 christos
69 1.1 christos void
70 1.1 christos fatal(const char *format, ...) {
71 1.1 christos va_list args;
72 1.1 christos
73 1.1 christos fprintf(stderr, "%s: fatal: ", program);
74 1.1 christos va_start(args, format);
75 1.1 christos vfprintf(stderr, format, args);
76 1.1 christos va_end(args);
77 1.1 christos fprintf(stderr, "\n");
78 1.1 christos if (fatalcallback != NULL)
79 1.1 christos (*fatalcallback)();
80 1.1 christos exit(1);
81 1.1 christos }
82 1.1 christos
83 1.1 christos void
84 1.1 christos setfatalcallback(fatalcallback_t *callback) {
85 1.1 christos fatalcallback = callback;
86 1.1 christos }
87 1.1 christos
88 1.1 christos void
89 1.1 christos check_result(isc_result_t result, const char *message) {
90 1.1 christos if (result != ISC_R_SUCCESS)
91 1.1 christos fatal("%s: %s", message, isc_result_totext(result));
92 1.1 christos }
93 1.1 christos
94 1.1 christos void
95 1.1 christos vbprintf(int level, const char *fmt, ...) {
96 1.1 christos va_list ap;
97 1.1 christos if (level > verbose)
98 1.1 christos return;
99 1.1 christos va_start(ap, fmt);
100 1.1 christos fprintf(stderr, "%s: ", program);
101 1.1 christos vfprintf(stderr, fmt, ap);
102 1.1 christos va_end(ap);
103 1.1 christos }
104 1.1 christos
105 1.1 christos void
106 1.1 christos version(const char *name) {
107 1.1 christos fprintf(stderr, "%s %s\n", name, VERSION);
108 1.1 christos exit(0);
109 1.1 christos }
110 1.1 christos
111 1.1 christos void
112 1.1 christos sig_format(dns_rdata_rrsig_t *sig, char *cp, unsigned int size) {
113 1.1 christos char namestr[DNS_NAME_FORMATSIZE];
114 1.1 christos char algstr[DNS_NAME_FORMATSIZE];
115 1.1 christos
116 1.1 christos dns_name_format(&sig->signer, namestr, sizeof(namestr));
117 1.1 christos dns_secalg_format(sig->algorithm, algstr, sizeof(algstr));
118 1.1 christos snprintf(cp, size, "%s/%s/%d", namestr, algstr, sig->keyid);
119 1.1 christos }
120 1.1 christos
121 1.1 christos void
122 1.1 christos setup_logging(isc_mem_t *mctx, isc_log_t **logp) {
123 1.1 christos isc_result_t result;
124 1.1 christos isc_logdestination_t destination;
125 1.1 christos isc_logconfig_t *logconfig = NULL;
126 1.1 christos isc_log_t *log = NULL;
127 1.1 christos int level;
128 1.1 christos
129 1.1 christos if (verbose < 0)
130 1.1 christos verbose = 0;
131 1.1 christos switch (verbose) {
132 1.1 christos case 0:
133 1.1 christos /*
134 1.1 christos * We want to see warnings about things like out-of-zone
135 1.1 christos * data in the master file even when not verbose.
136 1.1 christos */
137 1.1 christos level = ISC_LOG_WARNING;
138 1.1 christos break;
139 1.1 christos case 1:
140 1.1 christos level = ISC_LOG_INFO;
141 1.1 christos break;
142 1.1 christos default:
143 1.1 christos level = ISC_LOG_DEBUG(verbose - 2 + 1);
144 1.1 christos break;
145 1.1 christos }
146 1.1 christos
147 1.1 christos RUNTIME_CHECK(isc_log_create(mctx, &log, &logconfig) == ISC_R_SUCCESS);
148 1.1 christos isc_log_setcontext(log);
149 1.1 christos dns_log_init(log);
150 1.1 christos dns_log_setcontext(log);
151 1.1 christos
152 1.1 christos RUNTIME_CHECK(isc_log_settag(logconfig, program) == ISC_R_SUCCESS);
153 1.1 christos
154 1.1 christos /*
155 1.1 christos * Set up a channel similar to default_stderr except:
156 1.1 christos * - the logging level is passed in
157 1.1 christos * - the program name and logging level are printed
158 1.1 christos * - no time stamp is printed
159 1.1 christos */
160 1.1 christos destination.file.stream = stderr;
161 1.1 christos destination.file.name = NULL;
162 1.1 christos destination.file.versions = ISC_LOG_ROLLNEVER;
163 1.1 christos destination.file.maximum_size = 0;
164 1.1 christos result = isc_log_createchannel(logconfig, "stderr",
165 1.1 christos ISC_LOG_TOFILEDESC,
166 1.1 christos level,
167 1.1 christos &destination,
168 1.1 christos ISC_LOG_PRINTTAG|ISC_LOG_PRINTLEVEL);
169 1.1 christos check_result(result, "isc_log_createchannel()");
170 1.1 christos
171 1.1 christos RUNTIME_CHECK(isc_log_usechannel(logconfig, "stderr",
172 1.1 christos NULL, NULL) == ISC_R_SUCCESS);
173 1.1 christos
174 1.1 christos *logp = log;
175 1.1 christos }
176 1.1 christos
177 1.1 christos void
178 1.1 christos cleanup_logging(isc_log_t **logp) {
179 1.1 christos isc_log_t *log;
180 1.1 christos
181 1.1 christos REQUIRE(logp != NULL);
182 1.1 christos
183 1.1 christos log = *logp;
184 1.1 christos *logp = NULL;
185 1.1 christos
186 1.1 christos if (log == NULL)
187 1.1 christos return;
188 1.1 christos
189 1.1 christos isc_log_destroy(&log);
190 1.1 christos isc_log_setcontext(NULL);
191 1.1 christos dns_log_setcontext(NULL);
192 1.1 christos }
193 1.1 christos
194 1.1 christos static isc_stdtime_t
195 1.1 christos time_units(isc_stdtime_t offset, char *suffix, const char *str) {
196 1.1 christos switch (suffix[0]) {
197 1.1 christos case 'Y': case 'y':
198 1.1 christos return (offset * (365 * 24 * 3600));
199 1.1 christos case 'M': case 'm':
200 1.1 christos switch (suffix[1]) {
201 1.1 christos case 'O': case 'o':
202 1.1 christos return (offset * (30 * 24 * 3600));
203 1.1 christos case 'I': case 'i':
204 1.1 christos return (offset * 60);
205 1.1 christos case '\0':
206 1.1 christos fatal("'%s' ambiguous: use 'mi' for minutes "
207 1.1 christos "or 'mo' for months", str);
208 1.1 christos default:
209 1.1 christos fatal("time value %s is invalid", str);
210 1.1 christos }
211 1.1 christos /* NOTREACHED */
212 1.1 christos break;
213 1.1 christos case 'W': case 'w':
214 1.1 christos return (offset * (7 * 24 * 3600));
215 1.1 christos case 'D': case 'd':
216 1.1 christos return (offset * (24 * 3600));
217 1.1 christos case 'H': case 'h':
218 1.1 christos return (offset * 3600);
219 1.1 christos case 'S': case 's': case '\0':
220 1.1 christos return (offset);
221 1.1 christos default:
222 1.1 christos fatal("time value %s is invalid", str);
223 1.1 christos }
224 1.1 christos /* NOTREACHED */
225 1.1 christos return(0); /* silence compiler warning */
226 1.1 christos }
227 1.1 christos
228 1.3 christos static inline bool
229 1.1 christos isnone(const char *str) {
230 1.3 christos return ((strcasecmp(str, "none") == 0) ||
231 1.3 christos (strcasecmp(str, "never") == 0));
232 1.1 christos }
233 1.1 christos
234 1.1 christos dns_ttl_t
235 1.1 christos strtottl(const char *str) {
236 1.1 christos const char *orig = str;
237 1.1 christos dns_ttl_t ttl;
238 1.1 christos char *endp;
239 1.1 christos
240 1.1 christos if (isnone(str))
241 1.1 christos return ((dns_ttl_t) 0);
242 1.1 christos
243 1.1 christos ttl = strtol(str, &endp, 0);
244 1.1 christos if (ttl == 0 && endp == str)
245 1.1 christos fatal("TTL must be numeric");
246 1.1 christos ttl = time_units(ttl, endp, orig);
247 1.1 christos return (ttl);
248 1.1 christos }
249 1.1 christos
250 1.1 christos isc_stdtime_t
251 1.3 christos strtotime(const char *str, int64_t now, int64_t base,
252 1.3 christos bool *setp)
253 1.1 christos {
254 1.3 christos int64_t val, offset;
255 1.1 christos isc_result_t result;
256 1.1 christos const char *orig = str;
257 1.1 christos char *endp;
258 1.1 christos size_t n;
259 1.1 christos
260 1.1 christos if (isnone(str)) {
261 1.1 christos if (setp != NULL)
262 1.3 christos *setp = false;
263 1.1 christos return ((isc_stdtime_t) 0);
264 1.1 christos }
265 1.1 christos
266 1.1 christos if (setp != NULL)
267 1.3 christos *setp = true;
268 1.1 christos
269 1.1 christos if ((str[0] == '0' || str[0] == '-') && str[1] == '\0')
270 1.1 christos return ((isc_stdtime_t) 0);
271 1.1 christos
272 1.1 christos /*
273 1.1 christos * We accept times in the following formats:
274 1.1 christos * now([+-]offset)
275 1.1 christos * YYYYMMDD([+-]offset)
276 1.1 christos * YYYYMMDDhhmmss([+-]offset)
277 1.1 christos * [+-]offset
278 1.1 christos */
279 1.1 christos n = strspn(str, "0123456789");
280 1.1 christos if ((n == 8u || n == 14u) &&
281 1.1 christos (str[n] == '\0' || str[n] == '-' || str[n] == '+'))
282 1.1 christos {
283 1.1 christos char timestr[15];
284 1.1 christos
285 1.1 christos strlcpy(timestr, str, sizeof(timestr));
286 1.1 christos timestr[n] = 0;
287 1.1 christos if (n == 8u)
288 1.1 christos strlcat(timestr, "000000", sizeof(timestr));
289 1.1 christos result = dns_time64_fromtext(timestr, &val);
290 1.1 christos if (result != ISC_R_SUCCESS)
291 1.1 christos fatal("time value %s is invalid: %s", orig,
292 1.1 christos isc_result_totext(result));
293 1.1 christos base = val;
294 1.1 christos str += n;
295 1.1 christos } else if (strncmp(str, "now", 3) == 0) {
296 1.1 christos base = now;
297 1.1 christos str += 3;
298 1.1 christos }
299 1.1 christos
300 1.1 christos if (str[0] == '\0')
301 1.1 christos return ((isc_stdtime_t) base);
302 1.1 christos else if (str[0] == '+') {
303 1.1 christos offset = strtol(str + 1, &endp, 0);
304 1.1 christos offset = time_units((isc_stdtime_t) offset, endp, orig);
305 1.1 christos val = base + offset;
306 1.1 christos } else if (str[0] == '-') {
307 1.1 christos offset = strtol(str + 1, &endp, 0);
308 1.1 christos offset = time_units((isc_stdtime_t) offset, endp, orig);
309 1.1 christos val = base - offset;
310 1.1 christos } else
311 1.1 christos fatal("time value %s is invalid", orig);
312 1.1 christos
313 1.1 christos return ((isc_stdtime_t) val);
314 1.1 christos }
315 1.1 christos
316 1.1 christos dns_rdataclass_t
317 1.1 christos strtoclass(const char *str) {
318 1.1 christos isc_textregion_t r;
319 1.1 christos dns_rdataclass_t rdclass;
320 1.1 christos isc_result_t ret;
321 1.1 christos
322 1.1 christos if (str == NULL)
323 1.1 christos return dns_rdataclass_in;
324 1.1 christos DE_CONST(str, r.base);
325 1.1 christos r.length = strlen(str);
326 1.1 christos ret = dns_rdataclass_fromtext(&rdclass, &r);
327 1.1 christos if (ret != ISC_R_SUCCESS)
328 1.1 christos fatal("unknown class %s", str);
329 1.1 christos return (rdclass);
330 1.1 christos }
331 1.1 christos
332 1.1 christos unsigned int
333 1.1 christos strtodsdigest(const char *algname) {
334 1.1 christos if (strcasecmp(algname, "SHA1") == 0 ||
335 1.1 christos strcasecmp(algname, "SHA-1") == 0)
336 1.1 christos {
337 1.1 christos return (DNS_DSDIGEST_SHA1);
338 1.1 christos } else if (strcasecmp(algname, "SHA256") == 0 ||
339 1.1 christos strcasecmp(algname, "SHA-256") == 0)
340 1.1 christos {
341 1.1 christos return (DNS_DSDIGEST_SHA256);
342 1.1 christos } else if (strcasecmp(algname, "SHA384") == 0 ||
343 1.1 christos strcasecmp(algname, "SHA-384") == 0)
344 1.1 christos {
345 1.1 christos return (DNS_DSDIGEST_SHA384);
346 1.1 christos } else {
347 1.1 christos fatal("unknown algorithm %s", algname);
348 1.1 christos }
349 1.1 christos }
350 1.1 christos
351 1.1 christos isc_result_t
352 1.1 christos try_dir(const char *dirname) {
353 1.1 christos isc_result_t result;
354 1.1 christos isc_dir_t d;
355 1.1 christos
356 1.1 christos isc_dir_init(&d);
357 1.1 christos result = isc_dir_open(&d, dirname);
358 1.1 christos if (result == ISC_R_SUCCESS) {
359 1.1 christos isc_dir_close(&d);
360 1.1 christos }
361 1.1 christos return (result);
362 1.1 christos }
363 1.1 christos
364 1.1 christos /*
365 1.1 christos * Check private key version compatibility.
366 1.1 christos */
367 1.1 christos void
368 1.1 christos check_keyversion(dst_key_t *key, char *keystr) {
369 1.1 christos int major, minor;
370 1.1 christos dst_key_getprivateformat(key, &major, &minor);
371 1.1 christos INSIST(major <= DST_MAJOR_VERSION); /* invalid private key */
372 1.1 christos
373 1.1 christos if (major < DST_MAJOR_VERSION || minor < DST_MINOR_VERSION)
374 1.1 christos fatal("Key %s has incompatible format version %d.%d, "
375 1.1 christos "use -f to force upgrade to new version.",
376 1.1 christos keystr, major, minor);
377 1.1 christos if (minor > DST_MINOR_VERSION)
378 1.1 christos fatal("Key %s has incompatible format version %d.%d, "
379 1.1 christos "use -f to force downgrade to current version.",
380 1.1 christos keystr, major, minor);
381 1.1 christos }
382 1.1 christos
383 1.1 christos void
384 1.1 christos set_keyversion(dst_key_t *key) {
385 1.1 christos int major, minor;
386 1.1 christos dst_key_getprivateformat(key, &major, &minor);
387 1.1 christos INSIST(major <= DST_MAJOR_VERSION);
388 1.1 christos
389 1.1 christos if (major != DST_MAJOR_VERSION || minor != DST_MINOR_VERSION)
390 1.1 christos dst_key_setprivateformat(key, DST_MAJOR_VERSION,
391 1.1 christos DST_MINOR_VERSION);
392 1.1 christos
393 1.1 christos /*
394 1.1 christos * If the key is from a version older than 1.3, set
395 1.1 christos * set the creation date
396 1.1 christos */
397 1.1 christos if (major < 1 || (major == 1 && minor <= 2)) {
398 1.1 christos isc_stdtime_t now;
399 1.1 christos isc_stdtime_get(&now);
400 1.1 christos dst_key_settime(key, DST_TIME_CREATED, now);
401 1.1 christos }
402 1.1 christos }
403 1.1 christos
404 1.3 christos bool
405 1.1 christos key_collision(dst_key_t *dstkey, dns_name_t *name, const char *dir,
406 1.3 christos isc_mem_t *mctx, bool *exact)
407 1.1 christos {
408 1.1 christos isc_result_t result;
409 1.3 christos bool conflict = false;
410 1.1 christos dns_dnsseckeylist_t matchkeys;
411 1.1 christos dns_dnsseckey_t *key = NULL;
412 1.3 christos uint16_t id, oldid;
413 1.3 christos uint32_t rid, roldid;
414 1.1 christos dns_secalg_t alg;
415 1.3 christos char filename[NAME_MAX];
416 1.1 christos isc_buffer_t fileb;
417 1.3 christos isc_stdtime_t now;
418 1.1 christos
419 1.1 christos if (exact != NULL)
420 1.3 christos *exact = false;
421 1.1 christos
422 1.1 christos id = dst_key_id(dstkey);
423 1.1 christos rid = dst_key_rid(dstkey);
424 1.1 christos alg = dst_key_alg(dstkey);
425 1.1 christos
426 1.1 christos /*
427 1.3 christos * For Diffie Hellman just check if there is a direct collision as
428 1.3 christos * they can't be revoked. Additionally dns_dnssec_findmatchingkeys
429 1.3 christos * only handles DNSKEY which is not used for HMAC.
430 1.1 christos */
431 1.3 christos if (alg == DST_ALG_DH) {
432 1.1 christos isc_buffer_init(&fileb, filename, sizeof(filename));
433 1.1 christos result = dst_key_buildfilename(dstkey, DST_TYPE_PRIVATE,
434 1.1 christos dir, &fileb);
435 1.1 christos if (result != ISC_R_SUCCESS)
436 1.3 christos return (true);
437 1.1 christos return (isc_file_exists(filename));
438 1.1 christos }
439 1.1 christos
440 1.1 christos ISC_LIST_INIT(matchkeys);
441 1.3 christos isc_stdtime_get(&now);
442 1.3 christos result = dns_dnssec_findmatchingkeys(name, dir, now, mctx, &matchkeys);
443 1.1 christos if (result == ISC_R_NOTFOUND)
444 1.3 christos return (false);
445 1.1 christos
446 1.1 christos while (!ISC_LIST_EMPTY(matchkeys) && !conflict) {
447 1.1 christos key = ISC_LIST_HEAD(matchkeys);
448 1.1 christos if (dst_key_alg(key->key) != alg)
449 1.1 christos goto next;
450 1.1 christos
451 1.1 christos oldid = dst_key_id(key->key);
452 1.1 christos roldid = dst_key_rid(key->key);
453 1.1 christos
454 1.1 christos if (oldid == rid || roldid == id || id == oldid) {
455 1.3 christos conflict = true;
456 1.1 christos if (id != oldid) {
457 1.1 christos if (verbose > 1)
458 1.1 christos fprintf(stderr, "Key ID %d could "
459 1.1 christos "collide with %d\n",
460 1.1 christos id, oldid);
461 1.1 christos } else {
462 1.1 christos if (exact != NULL)
463 1.3 christos *exact = true;
464 1.1 christos if (verbose > 1)
465 1.1 christos fprintf(stderr, "Key ID %d exists\n",
466 1.1 christos id);
467 1.1 christos }
468 1.1 christos }
469 1.1 christos
470 1.1 christos next:
471 1.1 christos ISC_LIST_UNLINK(matchkeys, key, link);
472 1.1 christos dns_dnsseckey_destroy(mctx, &key);
473 1.1 christos }
474 1.1 christos
475 1.1 christos /* Finish freeing the list */
476 1.1 christos while (!ISC_LIST_EMPTY(matchkeys)) {
477 1.1 christos key = ISC_LIST_HEAD(matchkeys);
478 1.1 christos ISC_LIST_UNLINK(matchkeys, key, link);
479 1.1 christos dns_dnsseckey_destroy(mctx, &key);
480 1.1 christos }
481 1.1 christos
482 1.1 christos return (conflict);
483 1.1 christos }
484 1.1 christos
485 1.3 christos bool
486 1.1 christos isoptarg(const char *arg, char **argv, void(*usage)(void)) {
487 1.1 christos if (!strcasecmp(isc_commandline_argument, arg)) {
488 1.1 christos if (argv[isc_commandline_index] == NULL) {
489 1.1 christos fprintf(stderr, "%s: missing argument -%c %s\n",
490 1.1 christos program, isc_commandline_option,
491 1.1 christos isc_commandline_argument);
492 1.1 christos usage();
493 1.1 christos }
494 1.1 christos isc_commandline_argument = argv[isc_commandline_index];
495 1.1 christos /* skip to next arguement */
496 1.1 christos isc_commandline_index++;
497 1.3 christos return (true);
498 1.1 christos }
499 1.3 christos return (false);
500 1.1 christos }
501 1.1 christos
502 1.1 christos #ifdef _WIN32
503 1.1 christos void
504 1.1 christos InitSockets(void) {
505 1.1 christos WORD wVersionRequested;
506 1.1 christos WSADATA wsaData;
507 1.1 christos int err;
508 1.1 christos
509 1.1 christos wVersionRequested = MAKEWORD(2, 0);
510 1.1 christos
511 1.1 christos err = WSAStartup( wVersionRequested, &wsaData );
512 1.1 christos if (err != 0) {
513 1.1 christos fprintf(stderr, "WSAStartup() failed: %d\n", err);
514 1.1 christos exit(1);
515 1.1 christos }
516 1.1 christos }
517 1.1 christos
518 1.1 christos void
519 1.1 christos DestroySockets(void) {
520 1.1 christos WSACleanup();
521 1.1 christos }
522 1.1 christos #endif
523