1 /* $NetBSD: dnssec-verify.c,v 1.9 2015/07/08 17:28:55 christos Exp $ */
4 * Copyright (C) 2012, 2014, 2015 Internet Systems Consortium, Inc. ("ISC")
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
11 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
12 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
13 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
14 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
15 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16 * PERFORMANCE OF THIS SOFTWARE.
27 #include <isc/base32.h>
28 #include <isc/commandline.h>
29 #include <isc/entropy.h>
30 #include <isc/event.h>
35 #include <isc/mutex.h>
37 #include <isc/print.h>
38 #include <isc/random.h>
39 #include <isc/rwlock.h>
40 #include <isc/serial.h>
41 #include <isc/stdio.h>
42 #include <isc/stdlib.h>
43 #include <isc/string.h>
48 #include <dns/dbiterator.h>
50 #include <dns/dnssec.h>
52 #include <dns/fixedname.h>
53 #include <dns/keyvalues.h>
55 #include <dns/master.h>
56 #include <dns/masterdump.h>
58 #include <dns/nsec3.h>
59 #include <dns/rdata.h>
60 #include <dns/rdatalist.h>
61 #include <dns/rdataset.h>
62 #include <dns/rdataclass.h>
63 #include <dns/rdatasetiter.h>
64 #include <dns/rdatastruct.h>
65 #include <dns/rdatatype.h>
66 #include <dns/result.h>
73 #include <pk11/result.h>
76 #include "dnssectool.h"
78 const char *program
= "dnssec-verify";
81 static isc_stdtime_t now
;
82 static isc_mem_t
*mctx
= NULL
;
83 static isc_entropy_t
*ectx
= NULL
;
84 static dns_masterformat_t inputformat
= dns_masterformat_text
;
85 static dns_db_t
*gdb
; /* The database */
86 static dns_dbversion_t
*gversion
; /* The database version */
87 static dns_rdataclass_t gclass
; /* The class */
88 static dns_name_t
*gorigin
; /* The database origin */
89 static isc_boolean_t ignore_kskflag
= ISC_FALSE
;
90 static isc_boolean_t keyset_kskonly
= ISC_FALSE
;
93 * Load the zone file from disk
96 loadzone(char *file
, char *origin
, dns_rdataclass_t rdclass
, dns_db_t
**db
) {
99 dns_fixedname_t fname
;
103 len
= strlen(origin
);
104 isc_buffer_init(&b
, origin
, len
);
105 isc_buffer_add(&b
, len
);
107 dns_fixedname_init(&fname
);
108 name
= dns_fixedname_name(&fname
);
109 result
= dns_name_fromtext(name
, &b
, dns_rootname
, 0, NULL
);
110 if (result
!= ISC_R_SUCCESS
)
111 fatal("failed converting name '%s' to dns format: %s",
112 origin
, isc_result_totext(result
));
114 result
= dns_db_create(mctx
, "rbt", name
, dns_dbtype_zone
,
115 rdclass
, 0, NULL
, db
);
116 check_result(result
, "dns_db_create()");
118 result
= dns_db_load2(*db
, file
, inputformat
);
119 if (result
!= ISC_R_SUCCESS
&& result
!= DNS_R_SEENINCLUDE
)
120 fatal("failed loading zone from '%s': %s",
121 file
, isc_result_totext(result
));
124 ISC_PLATFORM_NORETURN_PRE
static void
125 usage(void) ISC_PLATFORM_NORETURN_POST
;
129 fprintf(stderr
, "Usage:\n");
130 fprintf(stderr
, "\t%s [options] zonefile [keys]\n", program
);
132 fprintf(stderr
, "\n");
134 fprintf(stderr
, "Version: %s\n", VERSION
);
136 fprintf(stderr
, "Options: (default value in parenthesis) \n");
137 fprintf(stderr
, "\t-v debuglevel (0)\n");
138 fprintf(stderr
, "\t-V:\tprint version information\n");
139 fprintf(stderr
, "\t-o origin:\n");
140 fprintf(stderr
, "\t\tzone origin (name of zonefile)\n");
141 fprintf(stderr
, "\t-I format:\n");
142 fprintf(stderr
, "\t\tfile format of input zonefile (text)\n");
143 fprintf(stderr
, "\t-c class (IN)\n");
144 fprintf(stderr
, "\t-E engine:\n");
145 #if defined(PKCS11CRYPTO)
146 fprintf(stderr
, "\t\tpath to PKCS#11 provider library "
147 "(default is %s)\n", PK11_LIB_LOCATION
);
148 #elif defined(USE_PKCS11)
149 fprintf(stderr
, "\t\tname of an OpenSSL engine to use "
150 "(default is \"pkcs11\")\n");
152 fprintf(stderr
, "\t\tname of an OpenSSL engine to use\n");
154 fprintf(stderr
, "\t-x:\tDNSKEY record signed with KSKs only, "
156 fprintf(stderr
, "\t-z:\tAll records signed with KSKs\n");
161 main(int argc
, char *argv
[]) {
162 char *origin
= NULL
, *file
= NULL
;
163 char *inputformatstr
= NULL
;
165 isc_log_t
*log
= NULL
;
167 const char *engine
= PKCS11_ENGINE
;
169 const char *engine
= NULL
;
171 char *classname
= NULL
;
172 dns_rdataclass_t rdclass
;
176 #define CMDLINE_FLAGS \
180 * Process memory debugging argument first.
182 while ((ch
= isc_commandline_parse(argc
, argv
, CMDLINE_FLAGS
)) != -1) {
185 if (strcasecmp(isc_commandline_argument
, "record") == 0)
186 isc_mem_debugging
|= ISC_MEM_DEBUGRECORD
;
187 if (strcasecmp(isc_commandline_argument
, "trace") == 0)
188 isc_mem_debugging
|= ISC_MEM_DEBUGTRACE
;
189 if (strcasecmp(isc_commandline_argument
, "usage") == 0)
190 isc_mem_debugging
|= ISC_MEM_DEBUGUSAGE
;
191 if (strcasecmp(isc_commandline_argument
, "size") == 0)
192 isc_mem_debugging
|= ISC_MEM_DEBUGSIZE
;
193 if (strcasecmp(isc_commandline_argument
, "mctx") == 0)
194 isc_mem_debugging
|= ISC_MEM_DEBUGCTX
;
200 isc_commandline_reset
= ISC_TRUE
;
201 check_result(isc_app_start(), "isc_app_start");
203 result
= isc_mem_create(0, 0, &mctx
);
204 if (result
!= ISC_R_SUCCESS
)
205 fatal("out of memory");
208 pk11_result_register();
210 dns_result_register();
212 isc_commandline_errprint
= ISC_FALSE
;
214 while ((ch
= isc_commandline_parse(argc
, argv
, CMDLINE_FLAGS
)) != -1) {
217 classname
= isc_commandline_argument
;
221 engine
= isc_commandline_argument
;
225 inputformatstr
= isc_commandline_argument
;
232 origin
= isc_commandline_argument
;
237 verbose
= strtol(isc_commandline_argument
, &endp
, 0);
239 fatal("verbose level must be numeric");
243 keyset_kskonly
= ISC_TRUE
;
247 ignore_kskflag
= ISC_TRUE
;
251 if (isc_commandline_option
!= '?')
252 fprintf(stderr
, "%s: invalid argument -%c\n",
253 program
, isc_commandline_option
);
257 /* Does not return. */
261 /* Does not return. */
265 fprintf(stderr
, "%s: unhandled option -%c\n",
266 program
, isc_commandline_option
);
272 setup_entropy(mctx
, NULL
, &ectx
);
274 result
= isc_hash_create(mctx
, ectx
, DNS_NAME_MAXWIRE
);
275 if (result
!= ISC_R_SUCCESS
)
276 fatal("could not create hash context");
278 result
= dst_lib_init2(mctx
, ectx
, engine
, ISC_ENTROPY_BLOCKING
);
279 if (result
!= ISC_R_SUCCESS
)
280 fatal("could not initialize dst: %s",
281 isc_result_totext(result
));
283 isc_stdtime_get(&now
);
285 rdclass
= strtoclass(classname
);
287 setup_logging(mctx
, &log
);
289 argc
-= isc_commandline_index
;
290 argv
+= isc_commandline_index
;
306 if (inputformatstr
!= NULL
) {
307 if (strcasecmp(inputformatstr
, "text") == 0)
308 inputformat
= dns_masterformat_text
;
309 else if (strcasecmp(inputformatstr
, "raw") == 0)
310 inputformat
= dns_masterformat_raw
;
312 fatal("unknown file format: %s\n", inputformatstr
);
316 fprintf(stderr
, "Loading zone '%s' from file '%s'\n", origin
, file
);
317 loadzone(file
, origin
, rdclass
, &gdb
);
318 gorigin
= dns_db_origin(gdb
);
319 gclass
= dns_db_class(gdb
);
322 result
= dns_db_newversion(gdb
, &gversion
);
323 check_result(result
, "dns_db_newversion()");
325 verifyzone(gdb
, gversion
, gorigin
, mctx
,
326 ignore_kskflag
, keyset_kskonly
);
328 dns_db_closeversion(gdb
, &gversion
, ISC_FALSE
);
331 cleanup_logging(&log
);
334 cleanup_entropy(&ectx
);
337 isc_mem_stats(mctx
, stdout
);
338 isc_mem_destroy(&mctx
);
340 (void) isc_app_finish();