dnssec-dsfromkey.c revision 1.1.1.9 1 /* $NetBSD: dnssec-dsfromkey.c,v 1.1.1.9 2022/09/23 12:09:08 christos Exp $ */
2
3 /*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16 /*! \file */
17
18 #include <inttypes.h>
19 #include <stdbool.h>
20 #include <stdlib.h>
21
22 #include <isc/buffer.h>
23 #include <isc/commandline.h>
24 #include <isc/hash.h>
25 #include <isc/mem.h>
26 #include <isc/print.h>
27 #include <isc/string.h>
28 #include <isc/util.h>
29
30 #include <dns/callbacks.h>
31 #include <dns/db.h>
32 #include <dns/dbiterator.h>
33 #include <dns/ds.h>
34 #include <dns/fixedname.h>
35 #include <dns/keyvalues.h>
36 #include <dns/log.h>
37 #include <dns/master.h>
38 #include <dns/name.h>
39 #include <dns/rdata.h>
40 #include <dns/rdataclass.h>
41 #include <dns/rdataset.h>
42 #include <dns/rdatasetiter.h>
43 #include <dns/rdatatype.h>
44 #include <dns/result.h>
45
46 #include <dst/dst.h>
47
48 #if USE_PKCS11
49 #include <pk11/result.h>
50 #endif /* if USE_PKCS11 */
51
52 #include "dnssectool.h"
53
54 const char *program = "dnssec-dsfromkey";
55
56 static dns_rdataclass_t rdclass;
57 static dns_fixedname_t fixed;
58 static dns_name_t *name = NULL;
59 static isc_mem_t *mctx = NULL;
60 static uint32_t ttl;
61 static bool emitttl = false;
62
63 static isc_result_t
64 initname(char *setname) {
65 isc_result_t result;
66 isc_buffer_t buf;
67
68 name = dns_fixedname_initname(&fixed);
69
70 isc_buffer_init(&buf, setname, strlen(setname));
71 isc_buffer_add(&buf, strlen(setname));
72 result = dns_name_fromtext(name, &buf, dns_rootname, 0, NULL);
73 return (result);
74 }
75
76 static void
77 db_load_from_stream(dns_db_t *db, FILE *fp) {
78 isc_result_t result;
79 dns_rdatacallbacks_t callbacks;
80
81 dns_rdatacallbacks_init(&callbacks);
82 result = dns_db_beginload(db, &callbacks);
83 if (result != ISC_R_SUCCESS) {
84 fatal("dns_db_beginload failed: %s", isc_result_totext(result));
85 }
86
87 result = dns_master_loadstream(fp, name, name, rdclass, 0, &callbacks,
88 mctx);
89 if (result != ISC_R_SUCCESS) {
90 fatal("can't load from input: %s", isc_result_totext(result));
91 }
92
93 result = dns_db_endload(db, &callbacks);
94 if (result != ISC_R_SUCCESS) {
95 fatal("dns_db_endload failed: %s", isc_result_totext(result));
96 }
97 }
98
99 static isc_result_t
100 loadset(const char *filename, dns_rdataset_t *rdataset) {
101 isc_result_t result;
102 dns_db_t *db = NULL;
103 dns_dbnode_t *node = NULL;
104 char setname[DNS_NAME_FORMATSIZE];
105
106 dns_name_format(name, setname, sizeof(setname));
107
108 result = dns_db_create(mctx, "rbt", name, dns_dbtype_zone, rdclass, 0,
109 NULL, &db);
110 if (result != ISC_R_SUCCESS) {
111 fatal("can't create database");
112 }
113
114 if (strcmp(filename, "-") == 0) {
115 db_load_from_stream(db, stdin);
116 filename = "input";
117 } else {
118 result = dns_db_load(db, filename, dns_masterformat_text, 0);
119 if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE) {
120 fatal("can't load %s: %s", filename,
121 isc_result_totext(result));
122 }
123 }
124
125 result = dns_db_findnode(db, name, false, &node);
126 if (result != ISC_R_SUCCESS) {
127 fatal("can't find %s node in %s", setname, filename);
128 }
129
130 result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_dnskey, 0, 0,
131 rdataset, NULL);
132
133 if (result == ISC_R_NOTFOUND) {
134 fatal("no DNSKEY RR for %s in %s", setname, filename);
135 } else if (result != ISC_R_SUCCESS) {
136 fatal("dns_db_findrdataset");
137 }
138
139 if (node != NULL) {
140 dns_db_detachnode(db, &node);
141 }
142 if (db != NULL) {
143 dns_db_detach(&db);
144 }
145 return (result);
146 }
147
148 static isc_result_t
149 loadkeyset(char *dirname, dns_rdataset_t *rdataset) {
150 isc_result_t result;
151 char filename[PATH_MAX + 1];
152 isc_buffer_t buf;
153
154 dns_rdataset_init(rdataset);
155
156 isc_buffer_init(&buf, filename, sizeof(filename));
157 if (dirname != NULL) {
158 /* allow room for a trailing slash */
159 if (strlen(dirname) >= isc_buffer_availablelength(&buf)) {
160 return (ISC_R_NOSPACE);
161 }
162 isc_buffer_putstr(&buf, dirname);
163 if (dirname[strlen(dirname) - 1] != '/') {
164 isc_buffer_putstr(&buf, "/");
165 }
166 }
167
168 if (isc_buffer_availablelength(&buf) < 7) {
169 return (ISC_R_NOSPACE);
170 }
171 isc_buffer_putstr(&buf, "keyset-");
172
173 result = dns_name_tofilenametext(name, false, &buf);
174 check_result(result, "dns_name_tofilenametext()");
175 if (isc_buffer_availablelength(&buf) == 0) {
176 return (ISC_R_NOSPACE);
177 }
178 isc_buffer_putuint8(&buf, 0);
179
180 return (loadset(filename, rdataset));
181 }
182
183 static void
184 loadkey(char *filename, unsigned char *key_buf, unsigned int key_buf_size,
185 dns_rdata_t *rdata) {
186 isc_result_t result;
187 dst_key_t *key = NULL;
188 isc_buffer_t keyb;
189 isc_region_t r;
190
191 dns_rdata_init(rdata);
192
193 isc_buffer_init(&keyb, key_buf, key_buf_size);
194
195 result = dst_key_fromnamedfile(filename, NULL, DST_TYPE_PUBLIC, mctx,
196 &key);
197 if (result != ISC_R_SUCCESS) {
198 fatal("can't load %s.key: %s", filename,
199 isc_result_totext(result));
200 }
201
202 if (verbose > 2) {
203 char keystr[DST_KEY_FORMATSIZE];
204
205 dst_key_format(key, keystr, sizeof(keystr));
206 fprintf(stderr, "%s: %s\n", program, keystr);
207 }
208
209 result = dst_key_todns(key, &keyb);
210 if (result != ISC_R_SUCCESS) {
211 fatal("can't decode key");
212 }
213
214 isc_buffer_usedregion(&keyb, &r);
215 dns_rdata_fromregion(rdata, dst_key_class(key), dns_rdatatype_dnskey,
216 &r);
217
218 rdclass = dst_key_class(key);
219
220 name = dns_fixedname_initname(&fixed);
221 dns_name_copynf(dst_key_name(key), name);
222
223 dst_key_free(&key);
224 }
225
226 static void
227 logkey(dns_rdata_t *rdata) {
228 isc_result_t result;
229 dst_key_t *key = NULL;
230 isc_buffer_t buf;
231 char keystr[DST_KEY_FORMATSIZE];
232
233 isc_buffer_init(&buf, rdata->data, rdata->length);
234 isc_buffer_add(&buf, rdata->length);
235 result = dst_key_fromdns(name, rdclass, &buf, mctx, &key);
236 if (result != ISC_R_SUCCESS) {
237 return;
238 }
239
240 dst_key_format(key, keystr, sizeof(keystr));
241 fprintf(stderr, "%s: %s\n", program, keystr);
242
243 dst_key_free(&key);
244 }
245
246 static void
247 emit(dns_dsdigest_t dt, bool showall, bool cds, dns_rdata_t *rdata) {
248 isc_result_t result;
249 unsigned char buf[DNS_DS_BUFFERSIZE];
250 char text_buf[DST_KEY_MAXTEXTSIZE];
251 char name_buf[DNS_NAME_MAXWIRE];
252 char class_buf[10];
253 isc_buffer_t textb, nameb, classb;
254 isc_region_t r;
255 dns_rdata_t ds;
256 dns_rdata_dnskey_t dnskey;
257
258 isc_buffer_init(&textb, text_buf, sizeof(text_buf));
259 isc_buffer_init(&nameb, name_buf, sizeof(name_buf));
260 isc_buffer_init(&classb, class_buf, sizeof(class_buf));
261
262 dns_rdata_init(&ds);
263
264 result = dns_rdata_tostruct(rdata, &dnskey, NULL);
265 if (result != ISC_R_SUCCESS) {
266 fatal("can't convert DNSKEY");
267 }
268
269 if ((dnskey.flags & DNS_KEYFLAG_REVOKE) != 0) {
270 return;
271 }
272
273 if ((dnskey.flags & DNS_KEYFLAG_KSK) == 0 && !showall) {
274 return;
275 }
276
277 result = dns_ds_buildrdata(name, rdata, dt, buf, &ds);
278 if (result != ISC_R_SUCCESS) {
279 fatal("can't build record");
280 }
281
282 result = dns_name_totext(name, false, &nameb);
283 if (result != ISC_R_SUCCESS) {
284 fatal("can't print name");
285 }
286
287 result = dns_rdata_tofmttext(&ds, (dns_name_t *)NULL, 0, 0, 0, "",
288 &textb);
289
290 if (result != ISC_R_SUCCESS) {
291 fatal("can't print rdata");
292 }
293
294 result = dns_rdataclass_totext(rdclass, &classb);
295 if (result != ISC_R_SUCCESS) {
296 fatal("can't print class");
297 }
298
299 isc_buffer_usedregion(&nameb, &r);
300 printf("%.*s ", (int)r.length, r.base);
301
302 if (emitttl) {
303 printf("%u ", ttl);
304 }
305
306 isc_buffer_usedregion(&classb, &r);
307 printf("%.*s", (int)r.length, r.base);
308
309 if (cds) {
310 printf(" CDS ");
311 } else {
312 printf(" DS ");
313 }
314
315 isc_buffer_usedregion(&textb, &r);
316 printf("%.*s\n", (int)r.length, r.base);
317 }
318
319 static void
320 emits(bool showall, bool cds, dns_rdata_t *rdata) {
321 unsigned i, n;
322
323 n = sizeof(dtype) / sizeof(dtype[0]);
324 for (i = 0; i < n; i++) {
325 if (dtype[i] != 0) {
326 emit(dtype[i], showall, cds, rdata);
327 }
328 }
329 }
330
331 ISC_PLATFORM_NORETURN_PRE static void
332 usage(void) ISC_PLATFORM_NORETURN_POST;
333
334 static void
335 usage(void) {
336 fprintf(stderr, "Usage:\n");
337 fprintf(stderr, " %s [options] keyfile\n\n", program);
338 fprintf(stderr, " %s [options] -f zonefile [zonename]\n\n", program);
339 fprintf(stderr, " %s [options] -s dnsname\n\n", program);
340 fprintf(stderr, " %s [-h|-V]\n\n", program);
341 fprintf(stderr, "Version: %s\n", VERSION);
342 fprintf(stderr, "Options:\n"
343 " -1: digest algorithm SHA-1\n"
344 " -2: digest algorithm SHA-256\n"
345 " -a algorithm: digest algorithm (SHA-1, SHA-256 or "
346 "SHA-384)\n"
347 " -A: include all keys in DS set, not just KSKs (-f "
348 "only)\n"
349 " -c class: rdata class for DS set (default IN) (-f "
350 "or -s only)\n"
351 " -C: print CDS records\n"
352 " -f zonefile: read keys from a zone file\n"
353 " -h: print help information\n"
354 " -K directory: where to find key or keyset files\n"
355 " -s: read keys from keyset-<dnsname> file\n"
356 " -T: TTL of output records (omitted by default)\n"
357 " -v level: verbosity\n"
358 " -V: print version information\n");
359 fprintf(stderr, "Output: DS or CDS RRs\n");
360
361 exit(-1);
362 }
363
364 int
365 main(int argc, char **argv) {
366 char *classname = NULL;
367 char *filename = NULL, *dir = NULL, *namestr;
368 char *endp, *arg1;
369 int ch;
370 bool cds = false;
371 bool usekeyset = false;
372 bool showall = false;
373 isc_result_t result;
374 isc_log_t *log = NULL;
375 dns_rdataset_t rdataset;
376 dns_rdata_t rdata;
377
378 dns_rdata_init(&rdata);
379
380 if (argc == 1) {
381 usage();
382 }
383
384 isc_mem_create(&mctx);
385
386 #if USE_PKCS11
387 pk11_result_register();
388 #endif /* if USE_PKCS11 */
389 dns_result_register();
390
391 isc_commandline_errprint = false;
392
393 #define OPTIONS "12Aa:Cc:d:Ff:K:l:sT:v:hV"
394 while ((ch = isc_commandline_parse(argc, argv, OPTIONS)) != -1) {
395 switch (ch) {
396 case '1':
397 add_dtype(DNS_DSDIGEST_SHA1);
398 break;
399 case '2':
400 add_dtype(DNS_DSDIGEST_SHA256);
401 break;
402 case 'A':
403 showall = true;
404 break;
405 case 'a':
406 add_dtype(strtodsdigest(isc_commandline_argument));
407 break;
408 case 'C':
409 cds = true;
410 break;
411 case 'c':
412 classname = isc_commandline_argument;
413 break;
414 case 'd':
415 fprintf(stderr,
416 "%s: the -d option is deprecated; "
417 "use -K\n",
418 program);
419 /* fall through */
420 case 'K':
421 dir = isc_commandline_argument;
422 if (strlen(dir) == 0U) {
423 fatal("directory must be non-empty string");
424 }
425 break;
426 case 'f':
427 filename = isc_commandline_argument;
428 break;
429 case 'l':
430 fatal("-l option (DLV lookaside) is obsolete");
431 break;
432 case 's':
433 usekeyset = true;
434 break;
435 case 'T':
436 emitttl = true;
437 ttl = strtottl(isc_commandline_argument);
438 break;
439 case 'v':
440 verbose = strtol(isc_commandline_argument, &endp, 0);
441 if (*endp != '\0') {
442 fatal("-v must be followed by a number");
443 }
444 break;
445 case 'F':
446 /* Reserved for FIPS mode */
447 FALLTHROUGH;
448 case '?':
449 if (isc_commandline_option != '?') {
450 fprintf(stderr, "%s: invalid argument -%c\n",
451 program, isc_commandline_option);
452 }
453 FALLTHROUGH;
454 case 'h':
455 /* Does not return. */
456 usage();
457
458 case 'V':
459 /* Does not return. */
460 version(program);
461
462 default:
463 fprintf(stderr, "%s: unhandled option -%c\n", program,
464 isc_commandline_option);
465 exit(1);
466 }
467 }
468
469 rdclass = strtoclass(classname);
470
471 if (usekeyset && filename != NULL) {
472 fatal("cannot use both -s and -f");
473 }
474
475 /* When not using -f, -A is implicit */
476 if (filename == NULL) {
477 showall = true;
478 }
479
480 /* Default digest type if none specified. */
481 if (dtype[0] == 0) {
482 dtype[0] = DNS_DSDIGEST_SHA256;
483 }
484
485 /*
486 * Use local variable arg1 so that clang can correctly analyse
487 * reachable paths rather than 'argc < isc_commandline_index + 1'.
488 */
489 arg1 = argv[isc_commandline_index];
490 if (arg1 == NULL && filename == NULL) {
491 fatal("the key file name was not specified");
492 }
493 if (arg1 != NULL && argv[isc_commandline_index + 1] != NULL) {
494 fatal("extraneous arguments");
495 }
496
497 result = dst_lib_init(mctx, NULL);
498 if (result != ISC_R_SUCCESS) {
499 fatal("could not initialize dst: %s",
500 isc_result_totext(result));
501 }
502
503 setup_logging(mctx, &log);
504
505 dns_rdataset_init(&rdataset);
506
507 if (usekeyset || filename != NULL) {
508 if (arg1 == NULL) {
509 /* using file name as the zone name */
510 namestr = filename;
511 } else {
512 namestr = arg1;
513 }
514
515 result = initname(namestr);
516 if (result != ISC_R_SUCCESS) {
517 fatal("could not initialize name %s", namestr);
518 }
519
520 if (usekeyset) {
521 result = loadkeyset(dir, &rdataset);
522 } else {
523 INSIST(filename != NULL);
524 result = loadset(filename, &rdataset);
525 }
526
527 if (result != ISC_R_SUCCESS) {
528 fatal("could not load DNSKEY set: %s\n",
529 isc_result_totext(result));
530 }
531
532 for (result = dns_rdataset_first(&rdataset);
533 result == ISC_R_SUCCESS;
534 result = dns_rdataset_next(&rdataset))
535 {
536 dns_rdata_init(&rdata);
537 dns_rdataset_current(&rdataset, &rdata);
538
539 if (verbose > 2) {
540 logkey(&rdata);
541 }
542
543 emits(showall, cds, &rdata);
544 }
545 } else {
546 unsigned char key_buf[DST_KEY_MAXSIZE];
547
548 loadkey(arg1, key_buf, DST_KEY_MAXSIZE, &rdata);
549
550 emits(showall, cds, &rdata);
551 }
552
553 if (dns_rdataset_isassociated(&rdataset)) {
554 dns_rdataset_disassociate(&rdataset);
555 }
556 cleanup_logging(&log);
557 dst_lib_destroy();
558 if (verbose > 10) {
559 isc_mem_stats(mctx, stdout);
560 }
561 isc_mem_destroy(&mctx);
562
563 fflush(stdout);
564 if (ferror(stdout)) {
565 fprintf(stderr, "write error\n");
566 return (1);
567 } else {
568 return (0);
569 }
570 }
571