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
188
189
190
191
192
193
|
/* Copyright (C) 2011 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 <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <assert.h>
#include <getopt.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <locale.h>
#include "utils/common/params.h"
#include "common/base32hex.h"
#include "common/errcode.h"
#include "common/hex.h"
#include "common/strtonum.h"
#include "libknot/dnssec/crypto.h"
#include "libknot/dnssec/nsec3.h"
#define PROGRAM_NAME "knsec3hash"
/*!
* \brief Print program usage (and example).
*/
static void usage(FILE *stream)
{
fprintf(stream, "usage: " PROGRAM_NAME " "
"<salt> <algorithm> <iterations> <domain-name>\n");
fprintf(stream, "example: " PROGRAM_NAME " "
"c01dcafe 1 10 knot-dns.cz\n");
}
/*!
* \brief Parse NSEC3 parameters and fill structure with NSEC3 parameters.
*/
static bool parse_nsec3_params(knot_nsec3_params_t *params, const char *salt,
const char *algorithm, const char *iterations)
{
int result;
result = knot_str2uint8t(algorithm, ¶ms->algorithm);
if (result != KNOT_EOK) {
fprintf(stderr, "Invalid algorithm number.\n");
return false;
}
result = knot_str2uint16t(iterations, ¶ms->iterations);
if (result != KNOT_EOK) {
fprintf(stderr, "Invalid iteration count: %s\n",
knot_strerror(result));
return false;
}
size_t salt_length = 0;
uint8_t *salt_data = NULL;
if (salt[0] != '\0') {
result = hex_decode(salt, &salt_data, &salt_length);
if (result != KNOT_EOK) {
fprintf(stderr, "Invalid salt: %s\n",
knot_strerror(result));
return false;
}
}
if (salt_length > UINT8_MAX) {
fprintf(stderr, "Invalid salt: Maximal length is %d bytes.\n",
UINT8_MAX);
free(salt_data);
return false;
}
params->salt = salt_data;
params->salt_length = (uint8_t)salt_length;
return true;
}
/*!
* \brief Entry point of 'knsec3hash'.
*/
int main(int argc, char *argv[])
{
bool enable_idn = true;
struct option options[] = {
{ "version", no_argument, 0, 'V' },
{ "help", no_argument, 0, 'h' },
{ NULL }
};
#ifdef LIBIDN
// Set up localization.
if (setlocale(LC_CTYPE, "") == NULL) {
enable_idn = false;
}
#endif
int opt = 0;
int li = 0;
while ((opt = getopt_long(argc, argv, "hV", options, &li)) != -1) {
switch(opt) {
case 'V':
printf("%s, version %s\n", PROGRAM_NAME, PACKAGE_VERSION);
return 0;
case 'h':
usage(stdout);
return 0;
default:
usage(stderr);
return 1;
}
}
// knsec3hash <salt> <algorithm> <iterations> <domain>
if (argc != 5) {
usage(stderr);
return 1;
}
atexit(knot_crypto_cleanup);
int exit_code = 1;
knot_nsec3_params_t nsec3_params = { 0 };
knot_dname_t *dname = NULL;
uint8_t *digest = NULL;
size_t digest_size = 0;
uint8_t *b32_digest = NULL;
int32_t b32_length = 0;
int result = 0;
if (!parse_nsec3_params(&nsec3_params, argv[1], argv[2], argv[3])) {
goto fail;
}
if (enable_idn) {
char *ascii_name = name_from_idn(argv[4]);
if (ascii_name == NULL) {
fprintf(stderr, "Cannot transform IDN domain name.\n");
goto fail;
}
dname = knot_dname_from_str(ascii_name);
free(ascii_name);
} else {
dname = knot_dname_from_str(argv[4]);
}
if (dname == NULL) {
fprintf(stderr, "Cannot parse domain name.\n");
goto fail;
}
result = knot_nsec3_hash(&nsec3_params, dname, knot_dname_size(dname),
&digest, &digest_size);
if (result != KNOT_EOK) {
fprintf(stderr, "Cannot compute hash: %s\n",
knot_strerror(result));
goto fail;
}
b32_length = base32hex_encode_alloc(digest, digest_size, &b32_digest);
if (b32_length < 0) {
fprintf(stderr, "Cannot encode computed hash: %s\n",
knot_strerror(b32_length));
goto fail;
}
exit_code = 0;
printf("%.*s (salt=%s, hash=%d, iterations=%d)\n", b32_length,
b32_digest, argv[1], nsec3_params.algorithm,
nsec3_params.iterations);
fail:
knot_nsec3_params_free(&nsec3_params);
knot_dname_free(&dname);
free(digest);
free(b32_digest);
return exit_code;
}
|