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