summaryrefslogtreecommitdiffstats
path: root/src/knot/modules/onlinesign/nsec_next.c
blob: 2205f6b93bf9941392aa690e945904a8d85cbd6b (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
/*  Copyright (C) 2019 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 <assert.h>
#include <stdbool.h>
#include <stdlib.h>

#include "knot/modules/onlinesign/nsec_next.h"
#include "libknot/libknot.h"

static bool inc_label(const uint8_t *buffer, uint8_t **label_ptr)
{
	assert(buffer);
	assert(label_ptr && *label_ptr);
	assert(buffer <= *label_ptr && *label_ptr < buffer + KNOT_DNAME_MAXLEN);

	const uint8_t *label = *label_ptr;
	const uint8_t len    = *label;
	const uint8_t *first = *label_ptr + 1;
	const uint8_t *last  = *label_ptr + len;

	assert(len <= KNOT_DNAME_MAXLABELLEN);

	// jump over trailing 0xff chars
	uint8_t *scan = (uint8_t *)last;
	while (scan >= first && *scan == 0xff) {
		scan -= 1;
	}

	// increase in place
	if (scan >= first) {
		if (*scan == 'A' - 1) {
			*scan = 'Z' + 1;
		} else {
			*scan += 1;
		}
		memset(scan + 1, 0x00, last - scan);
		return true;
	}

	// check name and label boundaries
	if (scan - 1 < buffer || len == KNOT_DNAME_MAXLABELLEN) {
		return false;
	}

	// append a zero byte at the end of the label
	scan -= 1;
	scan[0] = len + 1;
	memmove(scan + 1, first, len);
	scan[len + 1] = 0x00;

	*label_ptr = scan;

	return true;
}

static void strip_label(uint8_t **name_ptr)
{
	assert(name_ptr && *name_ptr);

	uint8_t len = **name_ptr;
	*name_ptr += 1 + len;
}

knot_dname_t *online_nsec_next(const knot_dname_t *dname, const knot_dname_t *apex)
{
	assert(dname);
	assert(apex);

	// right aligned copy of the domain name
	knot_dname_storage_t copy = { 0 };
	const size_t dname_len = knot_dname_size(dname);
	const size_t empty_len = sizeof(copy) - dname_len;
	memmove(copy + empty_len, dname, dname_len);

	// add new zero-byte label
	if (empty_len >= 2) {
		uint8_t *pos = copy + empty_len - 2;
		pos[0] = 0x01;
		pos[1] = 0x00;
		return knot_dname_copy(pos, NULL);
	}

	// find apex position in the buffer
	size_t apex_len = knot_dname_size(apex);
	const uint8_t *apex_pos = copy + sizeof(copy) - apex_len;
	assert(knot_dname_is_equal(apex, apex_pos));

	// find first label which can be incremented
	uint8_t *pos = copy + empty_len;
	while (pos != apex_pos) {
		if (inc_label(copy, &pos)) {
			return knot_dname_copy(pos, NULL);
		}
		strip_label(&pos);
	}

	// apex completes the chain
	return knot_dname_copy(pos, NULL);
}