xref: /openbsd-src/usr.sbin/unbound/testcode/signit.c (revision 712b2f300c854afbcde4b11834961322e3c11161)
1*712b2f30Ssthen /*
2*712b2f30Ssthen  * testcode/signit.c - debug tool to sign rrsets with given keys.
3*712b2f30Ssthen  *
4*712b2f30Ssthen  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5*712b2f30Ssthen  *
6*712b2f30Ssthen  * This software is open source.
7*712b2f30Ssthen  *
8*712b2f30Ssthen  * Redistribution and use in source and binary forms, with or without
9*712b2f30Ssthen  * modification, are permitted provided that the following conditions
10*712b2f30Ssthen  * are met:
11*712b2f30Ssthen  *
12*712b2f30Ssthen  * Redistributions of source code must retain the above copyright notice,
13*712b2f30Ssthen  * this list of conditions and the following disclaimer.
14*712b2f30Ssthen  *
15*712b2f30Ssthen  * Redistributions in binary form must reproduce the above copyright notice,
16*712b2f30Ssthen  * this list of conditions and the following disclaimer in the documentation
17*712b2f30Ssthen  * and/or other materials provided with the distribution.
18*712b2f30Ssthen  *
19*712b2f30Ssthen  * Neither the name of the NLNET LABS nor the names of its contributors may
20*712b2f30Ssthen  * be used to endorse or promote products derived from this software without
21*712b2f30Ssthen  * specific prior written permission.
22*712b2f30Ssthen  *
23*712b2f30Ssthen  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*712b2f30Ssthen  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25*712b2f30Ssthen  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26*712b2f30Ssthen  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27*712b2f30Ssthen  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28*712b2f30Ssthen  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29*712b2f30Ssthen  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30*712b2f30Ssthen  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31*712b2f30Ssthen  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32*712b2f30Ssthen  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33*712b2f30Ssthen  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34*712b2f30Ssthen  */
35*712b2f30Ssthen 
36*712b2f30Ssthen /**
37*712b2f30Ssthen  * \file
38*712b2f30Ssthen  *
39*712b2f30Ssthen  * This program signs rrsets with the given keys. It can be used to
40*712b2f30Ssthen  * construct input to test the validator with.
41*712b2f30Ssthen  */
42*712b2f30Ssthen #include "config.h"
43*712b2f30Ssthen #include <ldns/ldns.h>
44*712b2f30Ssthen #include <assert.h>
45*712b2f30Ssthen 
46*712b2f30Ssthen #define DNSKEY_BIT_ZSK 0x0100
47*712b2f30Ssthen 
48*712b2f30Ssthen /**
49*712b2f30Ssthen  * Key settings
50*712b2f30Ssthen  */
51*712b2f30Ssthen struct keysets {
52*712b2f30Ssthen 	/** signature inception */
53*712b2f30Ssthen 	uint32_t incep;
54*712b2f30Ssthen 	/** signature expiration */
55*712b2f30Ssthen 	uint32_t expi;
56*712b2f30Ssthen 	/** owner name */
57*712b2f30Ssthen 	char* owner;
58*712b2f30Ssthen 	/** keytag */
59*712b2f30Ssthen 	uint16_t keytag;
60*712b2f30Ssthen 	/** DNSKEY flags */
61*712b2f30Ssthen 	uint16_t flags;
62*712b2f30Ssthen };
63*712b2f30Ssthen 
64*712b2f30Ssthen /** print usage and exit */
65*712b2f30Ssthen static void
usage(void)66*712b2f30Ssthen usage(void)
67*712b2f30Ssthen {
68*712b2f30Ssthen 	printf("usage:	signit expi ince keytag owner keyfile\n");
69*712b2f30Ssthen 	printf("present rrset data on stdin.\n");
70*712b2f30Ssthen 	printf("signed data is printed to stdout.\n");
71*712b2f30Ssthen 	printf("\n");
72*712b2f30Ssthen 	printf("Or use:	signit NSEC3PARAM hash flags iter salt\n");
73*712b2f30Ssthen 	printf("present names on stdin, hashed names are printed to stdout.\n");
74*712b2f30Ssthen 	exit(1);
75*712b2f30Ssthen }
76*712b2f30Ssthen 
77*712b2f30Ssthen static time_t
convert_timeval(const char * str)78*712b2f30Ssthen convert_timeval(const char* str)
79*712b2f30Ssthen {
80*712b2f30Ssthen 	time_t t;
81*712b2f30Ssthen 	struct tm tm;
82*712b2f30Ssthen 	memset(&tm, 0, sizeof(tm));
83*712b2f30Ssthen 	if(strlen(str) < 14)
84*712b2f30Ssthen 		return 0;
85*712b2f30Ssthen 	if(sscanf(str, "%4d%2d%2d%2d%2d%2d", &tm.tm_year, &tm.tm_mon,
86*712b2f30Ssthen 		&tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec) != 6)
87*712b2f30Ssthen 		return 0;
88*712b2f30Ssthen 	tm.tm_year -= 1900;
89*712b2f30Ssthen 	tm.tm_mon--;
90*712b2f30Ssthen 	/* Check values */
91*712b2f30Ssthen 	if (tm.tm_year < 70)	return 0;
92*712b2f30Ssthen 	if (tm.tm_mon < 0 || tm.tm_mon > 11)	return 0;
93*712b2f30Ssthen 	if (tm.tm_mday < 1 || tm.tm_mday > 31) 	return 0;
94*712b2f30Ssthen 	if (tm.tm_hour < 0 || tm.tm_hour > 23)	return 0;
95*712b2f30Ssthen 	if (tm.tm_min < 0 || tm.tm_min > 59)	return 0;
96*712b2f30Ssthen 	if (tm.tm_sec < 0 || tm.tm_sec > 59)	return 0;
97*712b2f30Ssthen 	/* call ldns conversion function */
98*712b2f30Ssthen 	t = ldns_mktime_from_utc(&tm);
99*712b2f30Ssthen 	return t;
100*712b2f30Ssthen }
101*712b2f30Ssthen 
fatal_exit(const char * format,...)102*712b2f30Ssthen static void fatal_exit(const char* format, ...)
103*712b2f30Ssthen {
104*712b2f30Ssthen 	va_list args;
105*712b2f30Ssthen 	va_start(args, format);
106*712b2f30Ssthen 	printf("fatal exit: ");
107*712b2f30Ssthen 	vprintf(format, args);
108*712b2f30Ssthen 	va_end(args);
109*712b2f30Ssthen 	exit(1);
110*712b2f30Ssthen }
111*712b2f30Ssthen 
112*712b2f30Ssthen /** read expi ince keytag owner from cmdline */
113*712b2f30Ssthen static void
parse_cmdline(char * argv[],struct keysets * s)114*712b2f30Ssthen parse_cmdline(char *argv[], struct keysets* s)
115*712b2f30Ssthen {
116*712b2f30Ssthen 	s->expi = convert_timeval(argv[1]);
117*712b2f30Ssthen 	s->incep = convert_timeval(argv[2]);
118*712b2f30Ssthen 	s->keytag = (uint16_t)atoi(argv[3]);
119*712b2f30Ssthen 	s->owner = argv[4];
120*712b2f30Ssthen 	s->flags = DNSKEY_BIT_ZSK; /* to enforce signing */
121*712b2f30Ssthen }
122*712b2f30Ssthen 
123*712b2f30Ssthen /** read all key files, exit on error */
124*712b2f30Ssthen static ldns_key_list*
read_keys(int num,char * names[],struct keysets * set)125*712b2f30Ssthen read_keys(int num, char* names[], struct keysets* set)
126*712b2f30Ssthen {
127*712b2f30Ssthen 	int i;
128*712b2f30Ssthen 	ldns_key_list* keys = ldns_key_list_new();
129*712b2f30Ssthen 	ldns_key* k;
130*712b2f30Ssthen 	ldns_rdf* rdf;
131*712b2f30Ssthen 	ldns_status s;
132*712b2f30Ssthen 	int b;
133*712b2f30Ssthen 	FILE* in;
134*712b2f30Ssthen 
135*712b2f30Ssthen 	if(!keys) fatal_exit("alloc failure");
136*712b2f30Ssthen 	for(i=0; i<num; i++) {
137*712b2f30Ssthen 		printf("read keyfile %s\n", names[i]);
138*712b2f30Ssthen 		in = fopen(names[i], "r");
139*712b2f30Ssthen 		if(!in) fatal_exit("could not open %s: %s", names[i],
140*712b2f30Ssthen 				strerror(errno));
141*712b2f30Ssthen 		s = ldns_key_new_frm_fp(&k, in);
142*712b2f30Ssthen 		fclose(in);
143*712b2f30Ssthen 		if(s != LDNS_STATUS_OK)
144*712b2f30Ssthen 			fatal_exit("bad keyfile %s: %s", names[i],
145*712b2f30Ssthen 				ldns_get_errorstr_by_id(s));
146*712b2f30Ssthen 		ldns_key_set_expiration(k, set->expi);
147*712b2f30Ssthen 		ldns_key_set_inception(k, set->incep);
148*712b2f30Ssthen 		s = ldns_str2rdf_dname(&rdf, set->owner);
149*712b2f30Ssthen 		if(s != LDNS_STATUS_OK)
150*712b2f30Ssthen 			fatal_exit("bad owner name %s: %s", set->owner,
151*712b2f30Ssthen 				ldns_get_errorstr_by_id(s));
152*712b2f30Ssthen 		ldns_key_set_pubkey_owner(k, rdf);
153*712b2f30Ssthen 		ldns_key_set_flags(k, set->flags);
154*712b2f30Ssthen 		ldns_key_set_keytag(k, set->keytag);
155*712b2f30Ssthen 		b = ldns_key_list_push_key(keys, k);
156*712b2f30Ssthen 		assert(b);
157*712b2f30Ssthen 	}
158*712b2f30Ssthen 	return keys;
159*712b2f30Ssthen }
160*712b2f30Ssthen 
161*712b2f30Ssthen /** read list of rrs from the file */
162*712b2f30Ssthen static ldns_rr_list*
read_rrs(FILE * in)163*712b2f30Ssthen read_rrs(FILE* in)
164*712b2f30Ssthen {
165*712b2f30Ssthen 	uint32_t my_ttl = 3600;
166*712b2f30Ssthen 	ldns_rdf *my_origin = NULL;
167*712b2f30Ssthen 	ldns_rdf *my_prev = NULL;
168*712b2f30Ssthen 	ldns_status s;
169*712b2f30Ssthen 	int line_nr = 1;
170*712b2f30Ssthen 	int b;
171*712b2f30Ssthen 
172*712b2f30Ssthen 	ldns_rr_list* list;
173*712b2f30Ssthen 	ldns_rr *rr;
174*712b2f30Ssthen 
175*712b2f30Ssthen 	list = ldns_rr_list_new();
176*712b2f30Ssthen 	if(!list) fatal_exit("alloc error");
177*712b2f30Ssthen 
178*712b2f30Ssthen 	while(!feof(in)) {
179*712b2f30Ssthen 		s = ldns_rr_new_frm_fp_l(&rr, in, &my_ttl, &my_origin,
180*712b2f30Ssthen 			&my_prev, &line_nr);
181*712b2f30Ssthen 		if(s == LDNS_STATUS_SYNTAX_TTL ||
182*712b2f30Ssthen 			s == LDNS_STATUS_SYNTAX_ORIGIN ||
183*712b2f30Ssthen 			s == LDNS_STATUS_SYNTAX_EMPTY)
184*712b2f30Ssthen 			continue;
185*712b2f30Ssthen 		else if(s != LDNS_STATUS_OK)
186*712b2f30Ssthen 			fatal_exit("parse error in line %d: %s", line_nr,
187*712b2f30Ssthen 				ldns_get_errorstr_by_id(s));
188*712b2f30Ssthen 		b = ldns_rr_list_push_rr(list, rr);
189*712b2f30Ssthen 		assert(b);
190*712b2f30Ssthen 	}
191*712b2f30Ssthen 	printf("read %d lines\n", line_nr);
192*712b2f30Ssthen 
193*712b2f30Ssthen 	return list;
194*712b2f30Ssthen }
195*712b2f30Ssthen 
196*712b2f30Ssthen /** sign the rrs with the keys */
197*712b2f30Ssthen static void
signit(ldns_rr_list * rrs,ldns_key_list * keys)198*712b2f30Ssthen signit(ldns_rr_list* rrs, ldns_key_list* keys)
199*712b2f30Ssthen {
200*712b2f30Ssthen 	ldns_rr_list* rrset;
201*712b2f30Ssthen 	ldns_rr_list* sigs;
202*712b2f30Ssthen 
203*712b2f30Ssthen 	while(ldns_rr_list_rr_count(rrs) > 0) {
204*712b2f30Ssthen 		rrset = ldns_rr_list_pop_rrset(rrs);
205*712b2f30Ssthen 		if(!rrset) fatal_exit("copy alloc failure");
206*712b2f30Ssthen 		sigs = ldns_sign_public(rrset, keys);
207*712b2f30Ssthen 		if(!sigs) fatal_exit("failed to sign");
208*712b2f30Ssthen 		ldns_rr_list_print(stdout, rrset);
209*712b2f30Ssthen 		ldns_rr_list_print(stdout, sigs);
210*712b2f30Ssthen 		printf("\n");
211*712b2f30Ssthen 		ldns_rr_list_free(rrset);
212*712b2f30Ssthen 		ldns_rr_list_free(sigs);
213*712b2f30Ssthen 	}
214*712b2f30Ssthen }
215*712b2f30Ssthen 
216*712b2f30Ssthen /** process keys and signit */
217*712b2f30Ssthen static void
process_keys(int argc,char * argv[])218*712b2f30Ssthen process_keys(int argc, char* argv[])
219*712b2f30Ssthen {
220*712b2f30Ssthen 	ldns_rr_list* rrs;
221*712b2f30Ssthen 	ldns_key_list* keys;
222*712b2f30Ssthen 	struct keysets settings;
223*712b2f30Ssthen 	assert(argc == 6);
224*712b2f30Ssthen 
225*712b2f30Ssthen 	parse_cmdline(argv, &settings);
226*712b2f30Ssthen 	keys = read_keys(1, argv+5, &settings);
227*712b2f30Ssthen 	rrs = read_rrs(stdin);
228*712b2f30Ssthen 	signit(rrs, keys);
229*712b2f30Ssthen 
230*712b2f30Ssthen 	ldns_rr_list_deep_free(rrs);
231*712b2f30Ssthen 	ldns_key_list_free(keys);
232*712b2f30Ssthen }
233*712b2f30Ssthen 
234*712b2f30Ssthen /** process nsec3 params and perform hashing */
235*712b2f30Ssthen static void
process_nsec3(int argc,char * argv[])236*712b2f30Ssthen process_nsec3(int argc, char* argv[])
237*712b2f30Ssthen {
238*712b2f30Ssthen 	char line[10240];
239*712b2f30Ssthen 	ldns_rdf* salt;
240*712b2f30Ssthen 	ldns_rdf* in, *out;
241*712b2f30Ssthen 	ldns_status status;
242*712b2f30Ssthen 	status = ldns_str2rdf_nsec3_salt(&salt, argv[5]);
243*712b2f30Ssthen 	if(status != LDNS_STATUS_OK)
244*712b2f30Ssthen 		fatal_exit("Could not parse salt %s: %s", argv[5],
245*712b2f30Ssthen 			ldns_get_errorstr_by_id(status));
246*712b2f30Ssthen 	assert(argc == 6);
247*712b2f30Ssthen 	while(fgets(line, (int)sizeof(line), stdin)) {
248*712b2f30Ssthen 		if(strlen(line) > 0)
249*712b2f30Ssthen 			line[strlen(line)-1] = 0; /* remove trailing newline */
250*712b2f30Ssthen 		if(line[0]==0)
251*712b2f30Ssthen 			continue;
252*712b2f30Ssthen 		status = ldns_str2rdf_dname(&in, line);
253*712b2f30Ssthen 		if(status != LDNS_STATUS_OK)
254*712b2f30Ssthen 			fatal_exit("Could not parse name %s: %s", line,
255*712b2f30Ssthen 				ldns_get_errorstr_by_id(status));
256*712b2f30Ssthen 		ldns_rdf_print(stdout, in);
257*712b2f30Ssthen 		printf(" -> ");
258*712b2f30Ssthen 		/* arg 3 is flags, unused */
259*712b2f30Ssthen 		out = ldns_nsec3_hash_name(in, (uint8_t)atoi(argv[2]),
260*712b2f30Ssthen 			(uint16_t)atoi(argv[4]),
261*712b2f30Ssthen 			ldns_rdf_data(salt)[0], ldns_rdf_data(salt)+1);
262*712b2f30Ssthen 		if(!out)
263*712b2f30Ssthen 			fatal_exit("Could not hash %s", line);
264*712b2f30Ssthen 		ldns_rdf_print(stdout, out);
265*712b2f30Ssthen 		printf("\n");
266*712b2f30Ssthen 		ldns_rdf_deep_free(in);
267*712b2f30Ssthen 		ldns_rdf_deep_free(out);
268*712b2f30Ssthen 	}
269*712b2f30Ssthen 	ldns_rdf_deep_free(salt);
270*712b2f30Ssthen }
271*712b2f30Ssthen 
272*712b2f30Ssthen /** main program */
main(int argc,char * argv[])273*712b2f30Ssthen int main(int argc, char* argv[])
274*712b2f30Ssthen {
275*712b2f30Ssthen 	if(argc != 6) {
276*712b2f30Ssthen 		usage();
277*712b2f30Ssthen 	}
278*712b2f30Ssthen 	if(strcmp(argv[1], "NSEC3PARAM") == 0) {
279*712b2f30Ssthen 		process_nsec3(argc, argv);
280*712b2f30Ssthen 		return 0;
281*712b2f30Ssthen 	}
282*712b2f30Ssthen 	process_keys(argc, argv);
283*712b2f30Ssthen 	return 0;
284*712b2f30Ssthen }
285