xref: /minix3/external/bsd/bind/dist/bin/tools/nsec3hash.c (revision 00b67f09dd46474d133c95011a48590a8e8f94c7)
1 /*	$NetBSD: nsec3hash.c,v 1.6 2014/12/10 04:37:54 christos Exp $	*/
2 
3 /*
4  * Copyright (C) 2006, 2008, 2009, 2011, 2014  Internet Systems Consortium, Inc. ("ISC")
5  *
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.
9  *
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.
17  */
18 
19 /* Id: nsec3hash.c,v 1.8 2011/11/02 23:46:24 tbox Exp  */
20 
21 #include <config.h>
22 
23 #include <stdlib.h>
24 #include <stdarg.h>
25 
26 #include <isc/base32.h>
27 #include <isc/buffer.h>
28 #include <isc/hex.h>
29 #include <isc/iterated_hash.h>
30 #include <isc/print.h>
31 #include <isc/result.h>
32 #include <isc/string.h>
33 #include <isc/types.h>
34 
35 #include <dns/fixedname.h>
36 #include <dns/name.h>
37 #include <dns/nsec3.h>
38 #include <dns/types.h>
39 
40 const char *program = "nsec3hash";
41 
42 ISC_PLATFORM_NORETURN_PRE static void
43 fatal(const char *format, ...) ISC_PLATFORM_NORETURN_POST;
44 
45 static void
fatal(const char * format,...)46 fatal(const char *format, ...) {
47 	va_list args;
48 
49 	fprintf(stderr, "%s: ", program);
50 	va_start(args, format);
51 	vfprintf(stderr, format, args);
52 	va_end(args);
53 	fprintf(stderr, "\n");
54 	exit(1);
55 }
56 
57 static void
check_result(isc_result_t result,const char * message)58 check_result(isc_result_t result, const char *message) {
59 	if (result != ISC_R_SUCCESS)
60 		fatal("%s: %s", message, isc_result_totext(result));
61 }
62 
63 static void
usage(void)64 usage(void) {
65 	fprintf(stderr, "Usage: %s salt algorithm iterations domain\n",
66 		program);
67 	exit(1);
68 }
69 
70 int
main(int argc,char ** argv)71 main(int argc, char **argv) {
72 	dns_fixedname_t fixed;
73 	dns_name_t *name;
74 	isc_buffer_t buffer;
75 	isc_region_t region;
76 	isc_result_t result;
77 	unsigned char hash[NSEC3_MAX_HASH_LENGTH];
78 	unsigned char salt[DNS_NSEC3_SALTSIZE];
79 	unsigned char text[1024];
80 	unsigned int hash_alg;
81 	unsigned int length;
82 	unsigned int iterations;
83 	unsigned int salt_length;
84 
85 	if (argc != 5)
86 		usage();
87 
88 	if (strcmp(argv[1], "-") == 0) {
89 		salt_length = 0;
90 		salt[0] = 0;
91 	} else {
92 		isc_buffer_init(&buffer, salt, sizeof(salt));
93 		result = isc_hex_decodestring(argv[1], &buffer);
94 		check_result(result, "isc_hex_decodestring(salt)");
95 		salt_length = isc_buffer_usedlength(&buffer);
96 		if (salt_length > DNS_NSEC3_SALTSIZE)
97 			fatal("salt too long");
98 	}
99 	hash_alg = atoi(argv[2]);
100 	if (hash_alg > 255U)
101 		fatal("hash algorithm too large");
102 	iterations = atoi(argv[3]);
103 	if (iterations > 0xffffU)
104 		fatal("iterations to large");
105 
106 	dns_fixedname_init(&fixed);
107 	name = dns_fixedname_name(&fixed);
108 	isc_buffer_init(&buffer, argv[4], strlen(argv[4]));
109 	isc_buffer_add(&buffer, strlen(argv[4]));
110 	result = dns_name_fromtext(name, &buffer, dns_rootname, 0, NULL);
111 	check_result(result, "dns_name_fromtext() failed");
112 
113 	dns_name_downcase(name, name, NULL);
114 	length = isc_iterated_hash(hash, hash_alg, iterations,  salt,
115 				   salt_length, name->ndata, name->length);
116 	if (length == 0)
117 		fatal("isc_iterated_hash failed");
118 	region.base = hash;
119 	region.length = length;
120 	isc_buffer_init(&buffer, text, sizeof(text));
121 	isc_base32hexnp_totext(&region, 1, "", &buffer);
122 	fprintf(stdout, "%.*s (salt=%s, hash=%u, iterations=%u)\n",
123 		(int)isc_buffer_usedlength(&buffer), text, argv[1], hash_alg, iterations);
124 	return(0);
125 }
126