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