]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - contrib/bind9/bin/dnssec/dnssec-dsfromkey.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / contrib / bind9 / bin / dnssec / dnssec-dsfromkey.c
1 /*
2  * Copyright (C) 2008, 2009  Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /* $Id: dnssec-dsfromkey.c,v 1.2.14.3 2009/03/02 02:54:15 marka Exp $ */
18
19 /*! \file */
20
21 #include <config.h>
22
23 #include <stdlib.h>
24
25 #include <isc/buffer.h>
26 #include <isc/commandline.h>
27 #include <isc/entropy.h>
28 #include <isc/hash.h>
29 #include <isc/mem.h>
30 #include <isc/print.h>
31 #include <isc/string.h>
32 #include <isc/util.h>
33
34 #include <dns/db.h>
35 #include <dns/dbiterator.h>
36 #include <dns/ds.h>
37 #include <dns/fixedname.h>
38 #include <dns/log.h>
39 #include <dns/name.h>
40 #include <dns/rdata.h>
41 #include <dns/rdataclass.h>
42 #include <dns/rdataset.h>
43 #include <dns/rdatasetiter.h>
44 #include <dns/rdatatype.h>
45 #include <dns/result.h>
46
47 #include <dst/dst.h>
48
49 #include "dnssectool.h"
50
51 const char *program = "dnssec-dsfromkey";
52 int verbose;
53
54 static dns_rdataclass_t rdclass;
55 static dns_fixedname_t  fixed;
56 static dns_name_t       *name = NULL;
57 static dns_db_t         *db = NULL;
58 static dns_dbnode_t     *node = NULL;
59 static dns_rdataset_t   keyset;
60 static isc_mem_t        *mctx = NULL;
61
62 static void
63 loadkeys(char *dirname, char *setname)
64 {
65         isc_result_t     result;
66         char             filename[1024];
67         isc_buffer_t     buf;
68
69         dns_rdataset_init(&keyset);
70         dns_fixedname_init(&fixed);
71         name = dns_fixedname_name(&fixed);
72
73         isc_buffer_init(&buf, setname, strlen(setname));
74         isc_buffer_add(&buf, strlen(setname));
75         result = dns_name_fromtext(name, &buf, dns_rootname, ISC_FALSE, NULL);
76         if (result != ISC_R_SUCCESS)
77                 fatal("can't convert DNS name %s", setname);
78
79         isc_buffer_init(&buf, filename, sizeof(filename));
80         if (dirname != NULL) {
81                 isc_buffer_putstr(&buf, dirname);
82                 if (dirname[strlen(dirname) - 1] != '/')
83                         isc_buffer_putstr(&buf, "/");
84         }
85         isc_buffer_putstr(&buf, "keyset-");
86         result = dns_name_tofilenametext(name, ISC_FALSE, &buf);
87         check_result(result, "dns_name_tofilenametext()");
88         if (isc_buffer_availablelength(&buf) == 0)
89                 fatal("name %s too long", setname);
90         isc_buffer_putuint8(&buf, 0);
91
92         result = dns_db_create(mctx, "rbt", name, dns_dbtype_zone,
93                                rdclass, 0, NULL, &db);
94         if (result != ISC_R_SUCCESS)
95                 fatal("can't create database");
96
97         result = dns_db_load(db, filename);
98         if (result != ISC_R_SUCCESS && result != DNS_R_SEENINCLUDE)
99                 fatal("can't load %s: %s", filename, isc_result_totext(result));
100
101         result = dns_db_findnode(db, name, ISC_FALSE, &node);
102         if (result != ISC_R_SUCCESS)
103                 fatal("can't find %s node in %s", setname, filename);
104
105         result = dns_db_findrdataset(db, node, NULL, dns_rdatatype_dnskey,
106                                      0, 0, &keyset, NULL);
107         if (result == ISC_R_NOTFOUND)
108                 fatal("no DNSKEY RR for %s in %s", setname, filename);
109         else if (result != ISC_R_SUCCESS)
110                 fatal("dns_db_findrdataset");
111 }
112
113 static void
114 loadkey(char *filename, unsigned char *key_buf, unsigned int key_buf_size,
115         dns_rdata_t *rdata)
116 {
117         isc_result_t  result;
118         dst_key_t     *key = NULL;
119         isc_buffer_t  keyb;
120         isc_region_t  r;
121
122         dns_rdataset_init(&keyset);
123         dns_rdata_init(rdata);
124
125         isc_buffer_init(&keyb, key_buf, key_buf_size);
126
127         result = dst_key_fromnamedfile(filename, DST_TYPE_PUBLIC, mctx, &key);
128         if (result != ISC_R_SUCCESS)
129                 fatal("invalid keyfile name %s: %s",
130                       filename, isc_result_totext(result));
131
132         if (verbose > 2) {
133                 char keystr[KEY_FORMATSIZE];
134
135                 key_format(key, keystr, sizeof(keystr));
136                 fprintf(stderr, "%s: %s\n", program, keystr);
137         }
138
139         result = dst_key_todns(key, &keyb);
140         if (result != ISC_R_SUCCESS)
141                 fatal("can't decode key");
142
143         isc_buffer_usedregion(&keyb, &r);
144         dns_rdata_fromregion(rdata, dst_key_class(key),
145                              dns_rdatatype_dnskey, &r);
146
147         rdclass = dst_key_class(key);
148
149         dns_fixedname_init(&fixed);
150         name = dns_fixedname_name(&fixed);
151         result = dns_name_copy(dst_key_name(key), name, NULL);
152         if (result != ISC_R_SUCCESS)
153                 fatal("can't copy name");
154
155         dst_key_free(&key);
156 }
157
158 static void
159 logkey(dns_rdata_t *rdata)
160 {
161         isc_result_t result;
162         dst_key_t    *key = NULL;
163         isc_buffer_t buf;
164         char         keystr[KEY_FORMATSIZE];
165
166         isc_buffer_init(&buf, rdata->data, rdata->length);
167         isc_buffer_add(&buf, rdata->length);
168         result = dst_key_fromdns(name, rdclass, &buf, mctx, &key);
169         if (result != ISC_R_SUCCESS)
170                 return;
171
172         key_format(key, keystr, sizeof(keystr));
173         fprintf(stderr, "%s: %s\n", program, keystr);
174
175         dst_key_free(&key);
176 }
177
178 static void
179 emitds(unsigned int dtype, dns_rdata_t *rdata)
180 {
181         isc_result_t   result;
182         unsigned char  buf[DNS_DS_BUFFERSIZE];
183         char           text_buf[DST_KEY_MAXTEXTSIZE];
184         char           class_buf[10];
185         isc_buffer_t   textb, classb;
186         isc_region_t   r;
187         dns_rdata_t    ds;
188
189         isc_buffer_init(&textb, text_buf, sizeof(text_buf));
190         isc_buffer_init(&classb, class_buf, sizeof(class_buf));
191
192         dns_rdata_init(&ds);
193
194         result = dns_ds_buildrdata(name, rdata, dtype, buf, &ds);
195         if (result != ISC_R_SUCCESS)
196                 fatal("can't build DS");
197
198         result = dns_rdata_totext(&ds, (dns_name_t *) NULL, &textb);
199         if (result != ISC_R_SUCCESS)
200                 fatal("can't print DS rdata");
201
202         result = dns_rdataclass_totext(rdclass, &classb);
203         if (result != ISC_R_SUCCESS)
204                 fatal("can't print DS class");
205
206         result = dns_name_print(name, stdout);
207         if (result != ISC_R_SUCCESS)
208                 fatal("can't print DS name");
209
210         putchar(' ');
211
212         isc_buffer_usedregion(&classb, &r);
213         fwrite(r.base, 1, r.length, stdout);
214
215         printf(" DS ");
216
217         isc_buffer_usedregion(&textb, &r);
218         fwrite(r.base, 1, r.length, stdout);
219         putchar('\n');
220 }
221
222 static void
223 usage(void) {
224         fprintf(stderr, "Usage:\n");
225         fprintf(stderr, "    %s options keyfile\n\n", program);
226         fprintf(stderr, "    %s options [-c class] [-d dir] -s dnsname\n\n",
227                 program);
228         fprintf(stderr, "Version: %s\n", VERSION);
229         fprintf(stderr, "Options:\n");
230         fprintf(stderr, "    -v <verbose level>\n");
231         fprintf(stderr, "    -1: use SHA-1\n");
232         fprintf(stderr, "    -2: use SHA-256\n");
233         fprintf(stderr, "    -a algorithm: use algorithm\n");
234         fprintf(stderr, "Keyset options:\n");
235         fprintf(stderr, "    -s: keyset mode\n");
236         fprintf(stderr, "    -c class\n");
237         fprintf(stderr, "    -d directory\n");
238         fprintf(stderr, "Output: DS RRs\n");
239
240         exit (-1);
241 }
242
243 int
244 main(int argc, char **argv) {
245         char           *algname = NULL, *classname = NULL, *dirname = NULL;
246         char           *endp;
247         int            ch;
248         unsigned int   dtype = DNS_DSDIGEST_SHA1;
249         isc_boolean_t  both = ISC_TRUE;
250         isc_boolean_t  usekeyset = ISC_FALSE;
251         isc_result_t   result;
252         isc_log_t      *log = NULL;
253         isc_entropy_t  *ectx = NULL;
254         dns_rdata_t    rdata;
255
256         dns_rdata_init(&rdata);
257
258         if (argc == 1)
259                 usage();
260
261         result = isc_mem_create(0, 0, &mctx);
262         if (result != ISC_R_SUCCESS)
263                 fatal("out of memory");
264
265         dns_result_register();
266
267         isc_commandline_errprint = ISC_FALSE;
268
269         while ((ch = isc_commandline_parse(argc, argv,
270                                            "12a:c:d:sv:h")) != -1) {
271                 switch (ch) {
272                 case '1':
273                         dtype = DNS_DSDIGEST_SHA1;
274                         both = ISC_FALSE;
275                         break;
276                 case '2':
277                         dtype = DNS_DSDIGEST_SHA256;
278                         both = ISC_FALSE;
279                         break;
280                 case 'a':
281                         algname = isc_commandline_argument;
282                         both = ISC_FALSE;
283                         break;
284                 case 'c':
285                         classname = isc_commandline_argument;
286                         break;
287                 case 'd':
288                         dirname = isc_commandline_argument;
289                         break;
290                 case 's':
291                         usekeyset = ISC_TRUE;
292                         break;
293                 case 'v':
294                         verbose = strtol(isc_commandline_argument, &endp, 0);
295                         if (*endp != '\0')
296                                 fatal("-v must be followed by a number");
297                         break;
298                 case '?':
299                         if (isc_commandline_option != '?')
300                                 fprintf(stderr, "%s: invalid argument -%c\n",
301                                         program, isc_commandline_option);
302                         /* Falls into */
303                 case 'h':
304                         usage();
305
306                 default:
307                         fprintf(stderr, "%s: unhandled option -%c\n",
308                                 program, isc_commandline_option);
309                         exit(1);
310                 }
311         }
312
313         if (algname != NULL) {
314                 if (strcasecmp(algname, "SHA1") == 0 ||
315                     strcasecmp(algname, "SHA-1") == 0)
316                         dtype = DNS_DSDIGEST_SHA1;
317                 else if (strcasecmp(algname, "SHA256") == 0 ||
318                          strcasecmp(algname, "SHA-256") == 0)
319                         dtype = DNS_DSDIGEST_SHA256;
320                 else
321                         fatal("unknown algorithm %s", algname);
322         }
323
324         rdclass = strtoclass(classname);
325
326         if (argc < isc_commandline_index + 1)
327                 fatal("the key file name was not specified");
328         if (argc > isc_commandline_index + 1)
329                 fatal("extraneous arguments");
330
331         if (ectx == NULL)
332                 setup_entropy(mctx, NULL, &ectx);
333         result = isc_hash_create(mctx, ectx, DNS_NAME_MAXWIRE);
334         if (result != ISC_R_SUCCESS)
335                 fatal("could not initialize hash");
336         result = dst_lib_init(mctx, ectx,
337                               ISC_ENTROPY_BLOCKING | ISC_ENTROPY_GOODONLY);
338         if (result != ISC_R_SUCCESS)
339                 fatal("could not initialize dst");
340         isc_entropy_stopcallbacksources(ectx);
341
342         setup_logging(verbose, mctx, &log);
343
344         if (usekeyset) {
345                 loadkeys(dirname, argv[isc_commandline_index]);
346
347                 for (result = dns_rdataset_first(&keyset);
348                      result == ISC_R_SUCCESS;
349                      result = dns_rdataset_next(&keyset)) {
350                         dns_rdata_init(&rdata);
351                         dns_rdataset_current(&keyset, &rdata);
352
353                         if (verbose > 2)
354                                 logkey(&rdata);
355
356                         if (both) {
357                                 emitds(DNS_DSDIGEST_SHA1, &rdata);
358                                 emitds(DNS_DSDIGEST_SHA256, &rdata);
359                         } else
360                                 emitds(dtype, &rdata);
361                 }
362         } else {
363                 unsigned char key_buf[DST_KEY_MAXSIZE];
364
365                 loadkey(argv[isc_commandline_index], key_buf,
366                         DST_KEY_MAXSIZE, &rdata);
367
368                 if (both) {
369                         emitds(DNS_DSDIGEST_SHA1, &rdata);
370                         emitds(DNS_DSDIGEST_SHA256, &rdata);
371                 } else
372                         emitds(dtype, &rdata);
373         }
374
375         if (dns_rdataset_isassociated(&keyset))
376                 dns_rdataset_disassociate(&keyset);
377         if (node != NULL)
378                 dns_db_detachnode(db, &node);
379         if (db != NULL)
380                 dns_db_detach(&db);
381         cleanup_logging(&log);
382         dst_lib_destroy();
383         isc_hash_destroy();
384         cleanup_entropy(&ectx);
385         dns_name_destroy();
386         if (verbose > 10)
387                 isc_mem_stats(mctx, stdout);
388         isc_mem_destroy(&mctx);
389
390         fflush(stdout);
391         if (ferror(stdout)) {
392                 fprintf(stderr, "write error\n");
393                 return (1);
394         } else
395                 return (0);
396 }