dnssec-dsfromkey.c revision 1.2.2.3 1 /* $NetBSD: dnssec-dsfromkey.c,v 1.2.2.3 2019/01/18 08:49:11 pgoyette 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 [-K dir] keyfile\n\n", program);
324 fprintf(stderr, " %s options [-K dir] [-c class] -s dnsname\n\n",
325 program);
326 fprintf(stderr, " %s options -f zonefile (as zone name)\n\n", program);
327 fprintf(stderr, " %s options -f zonefile zonename\n\n", program);
328 fprintf(stderr, "Version: %s\n", VERSION);
329 fprintf(stderr, "Options:\n");
330 fprintf(stderr, " -v <verbose level>\n");
331 fprintf(stderr, " -V: print version information\n");
332 fprintf(stderr, " -K <directory>: directory in which to find "
333 "key file or keyset file\n");
334 fprintf(stderr, " -a algorithm: digest algorithm "
335 "(SHA-1, SHA-256 or SHA-384)\n");
336 fprintf(stderr, " -1: use SHA-1\n");
337 fprintf(stderr, " -2: use SHA-256\n");
338 fprintf(stderr, " -C: print CDS record\n");
339 fprintf(stderr, " -l: add lookaside zone and print DLV records\n");
340 fprintf(stderr, " -s: read keyset from keyset-<dnsname> file\n");
341 fprintf(stderr, " -c class: rdata class for DS set (default: IN)\n");
342 fprintf(stderr, " -T TTL\n");
343 fprintf(stderr, " -f file: read keyset from zone file\n");
344 fprintf(stderr, " -A: when used with -f, "
345 "include all keys in DS set, not just KSKs\n");
346 fprintf(stderr, "Output: DS or DLV RRs\n");
347
348 exit (-1);
349 }
350
351 int
352 main(int argc, char **argv) {
353 char *classname = NULL;
354 char *filename = NULL, *dir = NULL, *namestr;
355 char *lookaside = NULL;
356 char *endp;
357 int ch;
358 unsigned int dtype = DNS_DSDIGEST_SHA1;
359 bool cds = false;
360 bool both = true;
361 bool usekeyset = false;
362 bool showall = false;
363 isc_result_t result;
364 isc_log_t *log = NULL;
365 dns_rdataset_t rdataset;
366 dns_rdata_t rdata;
367
368 dns_rdata_init(&rdata);
369
370 if (argc == 1)
371 usage();
372
373 result = isc_mem_create(0, 0, &mctx);
374 if (result != ISC_R_SUCCESS)
375 fatal("out of memory");
376
377 #if USE_PKCS11
378 pk11_result_register();
379 #endif
380 dns_result_register();
381
382 isc_commandline_errprint = false;
383
384 #define OPTIONS "12Aa:Cc:d:Ff:K:l:sT:v:hV"
385 while ((ch = isc_commandline_parse(argc, argv, OPTIONS)) != -1) {
386 switch (ch) {
387 case '1':
388 dtype = DNS_DSDIGEST_SHA1;
389 both = false;
390 break;
391 case '2':
392 dtype = DNS_DSDIGEST_SHA256;
393 both = false;
394 break;
395 case 'A':
396 showall = true;
397 break;
398 case 'a':
399 dtype = strtodsdigest(isc_commandline_argument);
400 both = false;
401 break;
402 case 'C':
403 if (lookaside != NULL)
404 fatal("lookaside and CDS are mutually"
405 " exclusive");
406 cds = true;
407 break;
408 case 'c':
409 classname = isc_commandline_argument;
410 break;
411 case 'd':
412 fprintf(stderr, "%s: the -d option is deprecated; "
413 "use -K\n", program);
414 /* fall through */
415 case 'K':
416 dir = isc_commandline_argument;
417 if (strlen(dir) == 0U)
418 fatal("directory must be non-empty string");
419 break;
420 case 'f':
421 filename = isc_commandline_argument;
422 break;
423 case 'l':
424 if (cds)
425 fatal("lookaside and CDS are mutually"
426 " exclusive");
427 lookaside = isc_commandline_argument;
428 if (strlen(lookaside) == 0U)
429 fatal("lookaside must be a non-empty string");
430 break;
431 case 's':
432 usekeyset = true;
433 break;
434 case 'T':
435 emitttl = true;
436 ttl = strtottl(isc_commandline_argument);
437 break;
438 case 'v':
439 verbose = strtol(isc_commandline_argument, &endp, 0);
440 if (*endp != '\0')
441 fatal("-v must be followed by a number");
442 break;
443 case 'F':
444 /* Reserved for FIPS mode */
445 /* FALLTHROUGH */
446 case '?':
447 if (isc_commandline_option != '?')
448 fprintf(stderr, "%s: invalid argument -%c\n",
449 program, isc_commandline_option);
450 /* FALLTHROUGH */
451 case 'h':
452 /* Does not return. */
453 usage();
454
455 case 'V':
456 /* Does not return. */
457 version(program);
458
459 default:
460 fprintf(stderr, "%s: unhandled option -%c\n",
461 program, isc_commandline_option);
462 exit(1);
463 }
464 }
465
466 rdclass = strtoclass(classname);
467
468 if (usekeyset && filename != NULL)
469 fatal("cannot use both -s and -f");
470
471 /* When not using -f, -A is implicit */
472 if (filename == NULL)
473 showall = true;
474
475 if (argc < isc_commandline_index + 1 && filename == NULL)
476 fatal("the key file name was not specified");
477 if (argc > isc_commandline_index + 1)
478 fatal("extraneous arguments");
479
480 result = dst_lib_init(mctx, NULL);
481 if (result != ISC_R_SUCCESS)
482 fatal("could not initialize dst: %s",
483 isc_result_totext(result));
484
485 setup_logging(mctx, &log);
486
487 dns_rdataset_init(&rdataset);
488
489 if (usekeyset || filename != NULL) {
490 if (argc < isc_commandline_index + 1 && filename != NULL) {
491 /* using zone name as the zone file name */
492 namestr = filename;
493 } else
494 namestr = argv[isc_commandline_index];
495
496 result = initname(namestr);
497 if (result != ISC_R_SUCCESS)
498 fatal("could not initialize name %s", namestr);
499
500 if (usekeyset)
501 result = loadkeyset(dir, &rdataset);
502 else
503 result = loadset(filename, &rdataset);
504
505 if (result != ISC_R_SUCCESS)
506 fatal("could not load DNSKEY set: %s\n",
507 isc_result_totext(result));
508
509 for (result = dns_rdataset_first(&rdataset);
510 result == ISC_R_SUCCESS;
511 result = dns_rdataset_next(&rdataset)) {
512 dns_rdata_init(&rdata);
513 dns_rdataset_current(&rdataset, &rdata);
514
515 if (verbose > 2)
516 logkey(&rdata);
517
518 if (both) {
519 emit(DNS_DSDIGEST_SHA1, showall, lookaside,
520 cds, &rdata);
521 emit(DNS_DSDIGEST_SHA256, showall, lookaside,
522 cds, &rdata);
523 } else
524 emit(dtype, showall, lookaside, cds, &rdata);
525 }
526 } else {
527 unsigned char key_buf[DST_KEY_MAXSIZE];
528
529 loadkey(argv[isc_commandline_index], key_buf,
530 DST_KEY_MAXSIZE, &rdata);
531
532 if (both) {
533 emit(DNS_DSDIGEST_SHA1, showall, lookaside, cds,
534 &rdata);
535 emit(DNS_DSDIGEST_SHA256, showall, lookaside, cds,
536 &rdata);
537 } else
538 emit(dtype, showall, lookaside, cds, &rdata);
539 }
540
541 if (dns_rdataset_isassociated(&rdataset))
542 dns_rdataset_disassociate(&rdataset);
543 cleanup_logging(&log);
544 dst_lib_destroy();
545 dns_name_destroy();
546 if (verbose > 10)
547 isc_mem_stats(mctx, stdout);
548 isc_mem_destroy(&mctx);
549
550 fflush(stdout);
551 if (ferror(stdout)) {
552 fprintf(stderr, "write error\n");
553 return (1);
554 } else
555 return (0);
556 }
557