File: dnskey.c

package info (click to toggle)
validns 0.8%2Bgit20160720-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,176 kB
  • sloc: ansic: 7,222; perl: 283; makefile: 160
file content (218 lines) | stat: -rw-r--r-- 4,906 bytes parent folder | download | duplicates (2)
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
/*
 * Part of DNS zone file validator `validns`.
 *
 * Copyright 2011-2014 Anton Berezin <tobez@tobez.org>
 * Modified BSD license.
 * (See LICENSE file in the distribution.)
 *
 */
#include <sys/types.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <openssl/bn.h>
#include <openssl/evp.h>
#include <openssl/rsa.h>

#include "common.h"
#include "textparse.h"
#include "mempool.h"
#include "carp.h"
#include "rr.h"

static struct rr_dnskey *all_dns_keys = NULL;

static struct rr* dnskey_parse(char *name, long ttl, int type, char *s)
{
	struct rr_dnskey *rr = getmem(sizeof(*rr));
	struct binary_data key;
	int flags, proto, algorithm;
	unsigned int ac;
	int i;
	static struct rr *result;

	flags = extract_integer(&s, "flags", NULL);
	if (flags < 0) return NULL;
	if (flags & 0xfe7e)
		return bitch("reserved flags bits are set");
	if (flags & 0x0001 && !(flags & 0x0100))
		return bitch("SEP bit is set but Zone Key bit is unset");
	rr->flags = flags;

	/* TODO validate that `name` is the name of the zone if flags have Zone Key bit set */

	proto = extract_integer(&s, "protocol", NULL);
	if (proto < 0) return NULL;
	if (proto != 3)
		return bitch("bad protocol value");
	rr->protocol = proto;

	algorithm = extract_algorithm(&s, "algorithm");
	if (algorithm == ALG_UNSUPPORTED)	return NULL;
	if (algorithm == ALG_PRIVATEDNS || algorithm == ALG_PRIVATEOID) {
		return bitch("private algorithms are not supported in DNSKEY");
	}
	rr->algorithm = algorithm;

	key = extract_base64_binary_data(&s, "public key");
	if (key.length < 0)	return NULL;
	/* TODO validate key length based on algorithm */
	rr->pubkey = key;

	ac = 0;
	ac += rr->flags;
	ac += rr->protocol << 8;
	ac += rr->algorithm;
	for (i = 0; i < rr->pubkey.length; i++) {
		ac += (i & 1) ? (unsigned char)rr->pubkey.data[i] : ((unsigned char)rr->pubkey.data[i]) << 8;
	}
	ac += (ac >> 16) & 0xFFFF;
	rr->key_tag = ac & 0xFFFF;

	rr->pkey_built = 0;
	rr->pkey = NULL;
	rr->key_type = KEY_TYPE_UNUSED;

	if (*s) {
		return bitch("garbage after valid DNSKEY data");
	}
	result = store_record(type, name, ttl, rr);
	if (result) {
		rr->next_key = all_dns_keys;
		all_dns_keys = rr;
	}
	return result;
}

static char* dnskey_human(struct rr *rrv)
{
	RRCAST(dnskey);
    char s[1024];

    snprintf(s, 1024, "%hu %d %d XXX ; key id = %hu",
			 rr->flags, rr->protocol, rr->algorithm, rr->key_tag);
    return quickstrdup_temp(s);
}

static struct binary_data dnskey_wirerdata(struct rr *rrv)
{
	RRCAST(dnskey);

	return compose_binary_data("211d", 1,
		rr->flags, rr->protocol, rr->algorithm,
		rr->pubkey);
}

static void *dnskey_validate(struct rr *rrv)
{
	RRCAST(dnskey);

	if (G.opt.policy_checks[POLICY_DNSKEY]) {
		if (algorithm_type(rr->algorithm) == ALG_RSA_FAMILY) {
			unsigned int e_bytes;
			unsigned char *pk;
			int l;

			pk = (unsigned char *)rr->pubkey.data;
			l = rr->pubkey.length;

			e_bytes = *pk++;
			l--;
			if (e_bytes == 0) {
				if (l < 2)
					return moan(rr->rr.file_name, rr->rr.line, "public key is too short");
				e_bytes = (*pk++)  << 8;
				e_bytes += *pk++;
				l -= 2;
			}
			if (l < e_bytes)
				return moan(rr->rr.file_name, rr->rr.line, "public key is too short");

			if (*pk == 0)
				return moan(rr->rr.file_name, rr->rr.line, "leading zero octets in public key exponent");
			pk += e_bytes;
			l -= e_bytes;
			if (l > 0 && *pk == 0)
				return moan(rr->rr.file_name, rr->rr.line, "leading zero octets in key modulus");
		}
	}
	return NULL;
}

struct rr_methods dnskey_methods = { dnskey_parse, dnskey_human, dnskey_wirerdata, NULL, dnskey_validate };

int dnskey_build_pkey(struct rr_dnskey *rr)
{
	if (rr->pkey_built)
		return rr->pkey ? 1 : 0;

	rr->pkey_built = 1;

	if (algorithm_type(rr->algorithm) == ALG_RSA_FAMILY) {
		RSA *rsa;
		EVP_PKEY *pkey;
		unsigned int e_bytes;
		unsigned char *pk;
		int l;
		BIGNUM *n, *e;

		rsa = RSA_new();
		if (!rsa)
			goto done;

		pk = (unsigned char *)rr->pubkey.data;
		l = rr->pubkey.length;

		e_bytes = *pk++;
		l--;
		if (e_bytes == 0) {
			if (l < 2) /* public key is too short */
				goto done;
			e_bytes = (*pk++)  << 8;
			e_bytes += *pk++;
			l -= 2;
		}
		if (l < e_bytes) /* public key is too short */
			goto done;

		e = BN_bin2bn(pk, e_bytes, NULL);
		pk += e_bytes;
		l -= e_bytes;

		n = BN_bin2bn(pk, l, NULL);
		if (!e || !n)
			goto done;

		RSA_set0_key(rsa, n, e, NULL);

		pkey = EVP_PKEY_new();
		if (!pkey)
			goto done;

		if (!EVP_PKEY_set1_RSA(pkey, rsa))
			goto done;

		rr->pkey = pkey;
	}
done:
	if (!rr->pkey) {
		moan(rr->rr.file_name, rr->rr.line, "error building pkey");
	}
	return rr->pkey ? 1 : 0;
}

void
dnskey_ksk_policy_check(void)
{
	struct rr_dnskey *rr = all_dns_keys;
	int ksk_found = 0;

	while (rr) {
		if (rr->key_type == KEY_TYPE_KSK)
			ksk_found = 1;
		rr = rr->next_key;
	}
	if (!ksk_found)
		moan(all_dns_keys->rr.file_name, all_dns_keys->rr.line, "No KSK found");
}