summaryrefslogtreecommitdiffstats
path: root/src/utils/knsec3hash/knsec3hash.c
blob: a7bac978d46e61de0e00c2b1f7d180bc6b670018 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*  Copyright (C) 2022 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "contrib/base32hex.h"
#include "contrib/string.h"
#include "contrib/strtonum.h"
#include "libdnssec/error.h"
#include "libdnssec/nsec.h"
#include "libknot/libknot.h"
#include "utils/common/msg.h"
#include "utils/common/params.h"

#define PROGRAM_NAME	"knsec3hash"

/*!
 * \brief Print program help (and example).
 */
static void print_help(void)
{
	printf("Usage:   " PROGRAM_NAME " <salt> <algorithm> <iterations> <domain-name>\n");
	printf("Example: " PROGRAM_NAME " c01dcafe 1 10 knot-dns.cz\n");
	printf("Alternative usage: "PROGRAM_NAME " <algorithm> <flags> <iterations> <salt> <domain-name>\n");
	printf("Example: " PROGRAM_NAME " 1 0 10 c01dcafe knot-dns.cz\n");
}

/*!
 * \brief Parse NSEC3 salt.
 */
static int str_to_salt(const char *str, dnssec_binary_t *salt)
{
	if (strcmp(str, "-") == 0) {
		salt->size = 0;
		return DNSSEC_EOK;
	} else {
		salt->data = hex_to_bin(str, &salt->size);
		return (salt->data != NULL ? DNSSEC_EOK : DNSSEC_EINVAL);
	}
}

/*!
 * \brief Parse NSEC3 parameters and fill structure with NSEC3 parameters.
 */
static bool parse_nsec3_params(dnssec_nsec3_params_t *params, const char *salt_str,
			       const char *algorithm_str, const char *iterations_str)
{
	uint8_t algorithm = 0;
	int r = str_to_u8(algorithm_str, &algorithm);
	if (r != KNOT_EOK) {
		ERR2("invalid algorithm number");
		return false;
	}

	uint16_t iterations = 0;
	r = str_to_u16(iterations_str, &iterations);
	if (r != KNOT_EOK) {
		ERR2("invalid iteration count");
		return false;
	}

	dnssec_binary_t salt = { 0 };
	r = str_to_salt(salt_str, &salt);
	if (r != DNSSEC_EOK) {
		ERR2("invalid salt (%s)", knot_strerror(r));
		return false;
	}

	if (salt.size > UINT8_MAX) {
		ERR2("invalid salt, maximum length is %d bytes", UINT8_MAX);
		dnssec_binary_free(&salt);
		return false;
	}

	params->algorithm = algorithm;
	params->iterations = iterations;
	params->salt = salt;
	params->flags = 0;

	return true;
}

/*!
 * \brief Entry point of 'knsec3hash'.
 */
int main(int argc, char *argv[])
{
	struct option options[] = {
		{ "help",    no_argument, NULL, 'h' },
		{ "version", no_argument, NULL, 'V' },
		{ NULL }
	};

	int opt = 0;
	while ((opt = getopt_long(argc, argv, "hV", options, NULL)) != -1) {
		switch(opt) {
		case 'h':
			print_help();
			return EXIT_SUCCESS;
		case 'V':
			print_version(PROGRAM_NAME);
			return EXIT_SUCCESS;
		default:
			print_help();
			return EXIT_FAILURE;
		}
	}

	bool new_params = false;
	if (argc == 6) {
		// knsec3hash <algorithm> <flags> <iterations> <salt> <domain>
		new_params = true;
	} else if (argc != 5) {
		// knsec3hash <salt> <algorithm> <iterations> <domain>
		print_help();
		return EXIT_FAILURE;
	}

	int exit_code = EXIT_FAILURE;
	dnssec_nsec3_params_t nsec3_params = { 0 };

	dnssec_binary_t dname = { 0 };
	dnssec_binary_t digest = { 0 };
	dnssec_binary_t digest_print = { 0 };

	if (new_params) {
		if (!parse_nsec3_params(&nsec3_params, argv[4], argv[1], argv[3])) {
			goto fail;
		}
	} else {
		if (!parse_nsec3_params(&nsec3_params, argv[1], argv[2], argv[3])) {
			goto fail;
		}
	}

	dname.data = knot_dname_from_str_alloc(argv[new_params ? 5 : 4]);
	if (dname.data == NULL) {
		ERR2("cannot parse domain name");
		goto fail;
	}
	knot_dname_to_lower(dname.data);
	dname.size = knot_dname_size(dname.data);

	int r = dnssec_nsec3_hash(&dname, &nsec3_params, &digest);
	if (r != DNSSEC_EOK) {
		ERR2("cannot compute NSEC3 hash (%s)", knot_strerror(r));
		goto fail;
	}

	r = knot_base32hex_encode_alloc(digest.data, digest.size, &digest_print.data);
	if (r < 0) {
		ERR2("cannot encode computed hash (%s)", knot_strerror(r));
		goto fail;
	}
	digest_print.size = r;

	exit_code = EXIT_SUCCESS;

	printf("%.*s (salt=%s, hash=%d, iterations=%d)\n", (int)digest_print.size,
	       digest_print.data, argv[1], nsec3_params.algorithm,
	       nsec3_params.iterations);

fail:
	dnssec_nsec3_params_free(&nsec3_params);
	dnssec_binary_free(&dname);
	dnssec_binary_free(&digest);
	dnssec_binary_free(&digest_print);

	return exit_code;
}