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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
/* 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 <dirent.h>
#include <stdbool.h>
#include <inttypes.h>
#include "common/errcode.h"
#include "libknot/dname.h"
#include "libknot/consts.h"
#include "libknot/dnssec/nsec3.h"
#include "libknot/dnssec/sign.h"
#include "libknot/dnssec/zone-keys.h"
#include "libknot/rdata.h"
#include "libknot/util/debug.h"
/*!
* \brief Free DNSSEC signing context for each key.
*/
static void free_sign_contexts(knot_zone_keys_t *keys)
{
assert(keys);
for (int i = 0; i < keys->count; i++) {
knot_dnssec_sign_free(keys->keys[i].context);
keys->keys[i].context = NULL;
}
}
/*!
* \brief Initialize DNSSEC signing context for each key.
*/
static int init_sign_contexts(knot_zone_keys_t *keys)
{
assert(keys);
for (int i = 0; i < keys->count; i++) {
knot_zone_key_t *key = &keys->keys[i];
key->context = knot_dnssec_sign_init(&key->dnssec_key);
if (key->context == NULL) {
free_sign_contexts(keys);
return KNOT_ENOMEM;
}
}
return KNOT_EOK;
}
/*!
* \brief Get zone key by a keytag.
*/
const knot_zone_key_t *knot_get_zone_key(const knot_zone_keys_t *keys,
uint16_t keytag)
{
if (!keys) {
return NULL;
}
const knot_zone_key_t *result = NULL;
for (int i = 0; i < keys->count; i++) {
const knot_zone_key_t *key = &keys->keys[i];
if (key->dnssec_key.keytag == keytag) {
result = key;
break;
}
}
return result;
}
/*!
* \brief Get key feature flags from key parameters.
*/
static void set_zone_key_flags(const knot_key_params_t *params,
knot_zone_key_t *key)
{
assert(params);
assert(key);
uint32_t now = time(NULL);
uint32_t next_event = UINT32_MAX;
uint32_t timestamps[4] = {
params->time_publish,
params->time_activate,
params->time_inactive,
params->time_delete
};
for (int i = 0; i < 4; i++) {
uint32_t ts = timestamps[i];
if (ts != 0 && now <= ts && ts < next_event) {
next_event = ts;
}
}
key->next_event = next_event;
key->is_ksk = params->flags & KNOT_RDATA_DNSKEY_FLAG_KSK;
key->is_active = params->time_activate <= now &&
(params->time_inactive == 0 || now <= params->time_inactive);
key->is_public = params->time_publish <= now &&
(params->time_delete == 0 || now <= params->time_delete);
}
/*!
* \brief Check if key should be already removed from the zone.
*/
static bool was_removed(const knot_key_params_t *params)
{
assert(params);
time_t now = time(NULL);
return params->time_delete != 0 && now > params->time_delete;
}
/*!
* \brief Load zone keys from a key directory.
*
* \todo Maybe use dynamic list instead of fixed size array.
*/
int knot_load_zone_keys(const char *keydir_name, const knot_dname_t *zone_name,
bool nsec3_enabled, knot_zone_keys_t *keys)
{
if (!keydir_name || !zone_name || !keys) {
return KNOT_EINVAL;
}
DIR *keydir = opendir(keydir_name);
if (!keydir) {
return KNOT_DNSSEC_ENOKEYDIR;
}
char *zname = knot_dname_to_str(zone_name);
char *msgpref = sprintf_alloc("DNSSEC: Zone %s -", zname);
free(zname);
if (msgpref == NULL) {
closedir(keydir);
return KNOT_ENOMEM;
}
struct dirent entry_buf = { 0 };
struct dirent *entry = NULL;
while (keys->count < KNOT_MAX_ZONE_KEYS &&
readdir_r(keydir, &entry_buf, &entry) == 0 &&
entry != NULL) {
char *suffix = strrchr(entry->d_name, '.');
if (!suffix) {
continue;
}
if (strcmp(suffix, ".private") != 0) {
continue;
}
size_t path_len = strlen(keydir_name) + 1 + strlen(entry->d_name);
char *path = malloc((path_len + 1) * sizeof(char));
if (!path) {
ERR_ALLOC_FAILED;
closedir(keydir);
free(msgpref);
return KNOT_ENOMEM;
}
int written = snprintf(path, path_len + 1, "%s/%s",
keydir_name, entry->d_name);
UNUSED(written);
assert(written == path_len);
dbg_dnssec_detail("loading key '%s'\n", path);
knot_key_params_t params = { 0 };
int result = knot_load_key_params(path, ¶ms);
free(path);
if (result != KNOT_EOK) {
log_zone_warning("DNSSEC: Failed to load key %s: %s\n",
entry->d_name, knot_strerror(result));
knot_free_key_params(¶ms);
continue;
}
if (!knot_dname_is_equal(zone_name, params.name)) {
dbg_dnssec_detail("skipping key, different zone name\n");
knot_free_key_params(¶ms);
continue;
}
if (knot_get_key_type(¶ms) != KNOT_KEY_DNSSEC) {
dbg_dnssec_detail("skipping key, different purpose\n");
knot_free_key_params(¶ms);
continue;
}
knot_zone_key_t key;
memset(&key, '\0', sizeof(key));
set_zone_key_flags(¶ms, &key);
dbg_dnssec_detail("next key event %" PRIu32 "\n", key.next_event);
if (!key.is_active && !key.is_public && !was_removed(¶ms)) {
log_zone_notice("%s Ignoring key %d (%s): "
"%s, %s.\n", msgpref, params.keytag,
entry->d_name,
key.is_active ? "active" : "inactive",
key.is_public ? "public" : "not-public");
knot_free_key_params(¶ms);
continue;
}
if (!knot_dnssec_algorithm_is_zonesign(params.algorithm,
nsec3_enabled)
) {
log_zone_notice("%s Ignoring key %d (%s): unknown "
"algorithm or non-NSEC3 algorithm when"
" NSEC3 is requested.\n", msgpref,
params.keytag, entry->d_name);
knot_free_key_params(¶ms);
continue;
}
if (knot_get_zone_key(keys, params.keytag) != NULL) {
log_zone_notice("%s Ignoring key %d (%s): duplicate "
"keytag.\n", msgpref, params.keytag,
entry->d_name);
knot_free_key_params(¶ms);
continue;
}
result = knot_dnssec_key_from_params(¶ms, &key.dnssec_key);
if (result != KNOT_EOK) {
log_zone_error("%s Failed to process key %d (%s): %s\n",
msgpref, params.keytag, entry->d_name,
knot_strerror(result));
knot_free_key_params(¶ms);
continue;
}
log_zone_info("%s - Key is valid, tag %d, file %s, %s, %s, %s\n",
msgpref, params.keytag, entry->d_name,
key.is_ksk ? "KSK" : "ZSK",
key.is_active ? "active" : "inactive",
key.is_public ? "public" : "not-public");
keys->keys[keys->count] = key;
keys->count += 1;
knot_free_key_params(¶ms);
}
closedir(keydir);
if (keys->count == 0) {
free(msgpref);
return KNOT_DNSSEC_ENOKEY;
} else if (keys->count == KNOT_MAX_ZONE_KEYS) {
log_zone_notice("%s - Reached maximum count of keys.\n",
msgpref);
}
free(msgpref);
int result = init_sign_contexts(keys);
if (result != KNOT_EOK) {
knot_free_zone_keys(keys);
return result;
}
return KNOT_EOK;
}
/*!
* \brief Free structure with zone keys and associated DNSSEC contexts.
*/
void knot_free_zone_keys(knot_zone_keys_t *keys)
{
if (!keys) {
return;
}
free_sign_contexts(keys);
for (int i = 0; i < keys->count; i++) {
knot_dnssec_key_free(&keys->keys[i].dnssec_key);
}
memset(keys, '\0', sizeof(*keys));
}
/*!
* \brief Get timestamp of next key event.
*/
uint32_t knot_get_next_zone_key_event(const knot_zone_keys_t *keys)
{
uint32_t result = UINT32_MAX;
for (int i = 0; i < keys->count; i++) {
result = MIN(result, keys->keys[i].next_event);
}
return result;
}
|