keytable.c 8.03 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
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
/*
 * Copyright (C) 2000  Internet Software Consortium.
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#include <config.h>

#include <stddef.h>
#include <string.h>

#include <isc/assertions.h>
#include <isc/buffer.h>
#include <isc/magic.h>
#include <isc/rwlock.h>
#include <isc/result.h>
#include <isc/util.h>

#include <dns/keytable.h>
#include <dns/fixedname.h>
#include <dns/name.h>
#include <dns/rbt.h>

35
36
#include <dst/dst.h>

Bob Halley's avatar
Bob Halley committed
37
38
39
40
41
42
43
44
45
46
47
48
49
struct dns_keytable {
	/* Unlocked. */
        unsigned int		magic;
	isc_mem_t		*mctx;
	isc_mutex_t		lock;
	isc_rwlock_t		rwlock;
	/* Locked by lock. */
	isc_uint32_t		active_nodes;
	/* Locked by rwlock. */
	isc_uint32_t		references;
	dns_rbt_t		*table; 
};

Bob Halley's avatar
Bob Halley committed
50
51
#define KEYTABLE_MAGIC			0x4b54626cU	/* KTbl */
#define VALID_KEYTABLE(kt)	 	ISC_MAGIC_VALID(kt, KEYTABLE_MAGIC)
Bob Halley's avatar
Bob Halley committed
52
53
54
55
56
57
58

struct dns_keynode {
        unsigned int		magic;
	dst_key_t *		key;
	struct dns_keynode *	next;
};

Bob Halley's avatar
Bob Halley committed
59
60
#define KEYNODE_MAGIC			0x4b4e6f64U	/* KNod */
#define VALID_KEYNODE(kn)	 	ISC_MAGIC_VALID(kn, KEYNODE_MAGIC)
Bob Halley's avatar
Bob Halley committed
61

62
63
64
65
66
67
68
69
70
71
static void
free_keynode(void *node, void *arg) {
	dns_keynode_t *keynode = node;
	isc_mem_t *mctx = arg;

	REQUIRE(VALID_KEYNODE(keynode));
	dst_key_free(keynode->key);
	isc_mem_put(mctx, keynode, sizeof(dns_keynode_t));
}

Bob Halley's avatar
Bob Halley committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
isc_result_t
dns_keytable_create(isc_mem_t *mctx, dns_keytable_t **keytablep) {
	dns_keytable_t *keytable;
	isc_result_t result;

	/*
	 * Create a keytable.
	 */

	REQUIRE(keytablep != NULL && *keytablep == NULL);

	keytable = isc_mem_get(mctx, sizeof *keytable);
	if (keytable == NULL)
		return (DNS_R_NOMEMORY);

	keytable->table = NULL;
88
	result = dns_rbt_create(mctx, free_keynode, mctx, &keytable->table);
Bob Halley's avatar
Bob Halley committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
	if (result != ISC_R_SUCCESS)
		goto cleanup_keytable;

	result = isc_mutex_init(&keytable->lock);
	if (result != ISC_R_SUCCESS) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "isc_mutex_init() failed: %s",
				 isc_result_totext(result));
		result = ISC_R_UNEXPECTED;
		goto cleanup_rbt;
	}

	result = isc_rwlock_init(&keytable->rwlock, 0, 0);
	if (result != ISC_R_SUCCESS) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "isc_rwlock_init() failed: %s",
				 isc_result_totext(result));
		result = ISC_R_UNEXPECTED;
		goto cleanup_lock;
	}

	keytable->mctx = mctx;
	keytable->active_nodes = 0;
	keytable->references = 1;
Bob Halley's avatar
Bob Halley committed
113
	keytable->magic = KEYTABLE_MAGIC;
Bob Halley's avatar
Bob Halley committed
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
	*keytablep = keytable;

	return (ISC_R_SUCCESS);

   cleanup_lock:
	isc_mutex_destroy(&keytable->lock);

   cleanup_rbt:
	dns_rbt_destroy(&keytable->table);

   cleanup_keytable:
	isc_mem_put(mctx, keytable, sizeof *keytable);

	return (result);
}


void
dns_keytable_attach(dns_keytable_t *source, dns_keytable_t **targetp) {

	/*
	 * Attach *targetp to source.
	 */

	REQUIRE(VALID_KEYTABLE(source));
	REQUIRE(targetp != NULL && *targetp == NULL);

	RWLOCK(&source->rwlock, isc_rwlocktype_write);

	INSIST(source->references > 0);
	source->references++;
	INSIST(source->references != 0);

	RWUNLOCK(&source->rwlock, isc_rwlocktype_write);

	*targetp = source;
}

void
dns_keytable_detach(dns_keytable_t **keytablep) {
	isc_boolean_t destroy = ISC_FALSE;
	dns_keytable_t *keytable;

	/*
	 * Detach *keytablep from its keytable.
	 */

	REQUIRE(keytablep != NULL && VALID_KEYTABLE(*keytablep));

	keytable = *keytablep;

	RWLOCK(&keytable->rwlock, isc_rwlocktype_write);
	
	INSIST(keytable->references > 0);
	keytable->references--;
	LOCK(&keytable->lock);
	if (keytable->references == 0 && keytable->active_nodes == 0)
		destroy = ISC_TRUE;
	UNLOCK(&keytable->lock);

	RWUNLOCK(&keytable->rwlock, isc_rwlocktype_write);

	if (destroy) {
		dns_rbt_destroy(&keytable->table);
		isc_rwlock_destroy(&keytable->rwlock);
		isc_mutex_destroy(&keytable->lock);
		keytable->magic = 0;
		isc_mem_put(keytable->mctx, keytable, sizeof *keytable);
	}

	*keytablep = NULL;
}

isc_result_t
dns_keytable_add(dns_keytable_t *keytable, dst_key_t **keyp) {
	isc_result_t result;
	dns_keynode_t *knode;
	dns_rbtnode_t *node;
	dns_fixedname_t fname;
	char *keyname;
	isc_buffer_t buffer;
	size_t len;

	/*
	 * Add '*keyp' to 'keytable'.
	 */

	REQUIRE(VALID_KEYTABLE(keytable));
	REQUIRE(keyp != NULL);

	keyname = dst_key_name(*keyp);
	INSIST(keyname != NULL);
	len = strlen(keyname);
	isc_buffer_init(&buffer, keyname, len, ISC_BUFFERTYPE_TEXT);
	isc_buffer_add(&buffer, len);
	dns_fixedname_init(&fname);
	result = dns_name_fromtext(dns_fixedname_name(&fname), &buffer,
				   dns_rootname, ISC_FALSE, NULL);
	if (result != ISC_R_SUCCESS)
		return (result);

	knode = isc_mem_get(keytable->mctx, sizeof *knode);
	if (knode == NULL)
		return (ISC_R_NOMEMORY);

	RWLOCK(&keytable->rwlock, isc_rwlocktype_write);

	node = NULL;
	result = dns_rbt_addnode(keytable->table, dns_fixedname_name(&fname),
				 &node);

	if (result == ISC_R_SUCCESS || result == ISC_R_EXISTS) {
Bob Halley's avatar
Bob Halley committed
226
		knode->magic = KEYNODE_MAGIC;
Bob Halley's avatar
Bob Halley committed
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
322
323
324
325
326
327
328
329
		knode->key = *keyp;
		knode->next = node->data;
		node->data = knode;
		*keyp = NULL;
		knode = NULL;
	}

	RWUNLOCK(&keytable->rwlock, isc_rwlocktype_write);

	if (knode != NULL)
		isc_mem_put(keytable->mctx, knode, sizeof *knode);

	return (result);
}

isc_result_t
dns_keytable_findkeynode(dns_keytable_t *keytable, dns_name_t *name,
			 dns_secalg_t algorithm, dns_keytag_t tag,
			 dns_keynode_t **keynodep)
{
	isc_result_t result;
	dns_keynode_t *knode;
	void *data;

	/*
	 * Search for a key named 'name', matching 'algorithm' and 'tag' in
	 * 'keytable'.
	 */

	REQUIRE(VALID_KEYTABLE(keytable));
	REQUIRE(dns_name_isabsolute(name));
	REQUIRE(keynodep != NULL && *keynodep == NULL);

	RWLOCK(&keytable->rwlock, isc_rwlocktype_read);

	knode = NULL;
	data = NULL;
	result = dns_rbt_findname(keytable->table, name, NULL, &data);

	if (result == ISC_R_SUCCESS) {
		INSIST(data != NULL);
		for (knode = data; knode != NULL; knode = knode->next) {
			if (algorithm == (dns_secalg_t)dst_key_alg(knode->key)
			    && tag == (dns_keytag_t)dst_key_id(knode->key))
				break;
		}
		if (knode != NULL) {
			LOCK(&keytable->lock);
			keytable->active_nodes++;
			UNLOCK(&keytable->lock);
			*keynodep = knode;
		} else
			result = ISC_R_NOTFOUND;
	} else if (result == DNS_R_PARTIALMATCH)
		result = ISC_R_NOTFOUND;

	RWUNLOCK(&keytable->rwlock, isc_rwlocktype_read);

	return (result);
}

void
dns_keytable_detachkeynode(dns_keytable_t *keytable,
			   dns_keynode_t **keynodep)
{
	/*
	 * Give back a keynode found via dns_keytable_findkeynode().
	 */

	REQUIRE(VALID_KEYTABLE(keytable));
	REQUIRE(keynodep != NULL && VALID_KEYNODE(*keynodep));

	LOCK(&keytable->lock);
	INSIST(keytable->active_nodes > 0);
	keytable->active_nodes--;
	UNLOCK(&keytable->lock);

	*keynodep = NULL;
}

isc_result_t
dns_keytable_issecuredomain(dns_keytable_t *keytable, dns_name_t *name,
			    isc_boolean_t *wantdnssecp)
{
	isc_result_t result;
	void *data;

	/*
	 * Is 'name' at or beneath a trusted key?
	 */

	REQUIRE(VALID_KEYTABLE(keytable));
	REQUIRE(dns_name_isabsolute(name));
	REQUIRE(wantdnssecp != NULL);

	RWLOCK(&keytable->rwlock, isc_rwlocktype_read);

	data = NULL;
	result = dns_rbt_findname(keytable->table, name, NULL, &data);

	if (result == ISC_R_SUCCESS || result == DNS_R_PARTIALMATCH) {
		INSIST(data != NULL);
		*wantdnssecp = ISC_TRUE;
330
		result = ISC_R_SUCCESS;
Bob Halley's avatar
Bob Halley committed
331
332
	} else if (result == ISC_R_NOTFOUND) {
		*wantdnssecp = ISC_FALSE;
333
		result = ISC_R_SUCCESS;
Bob Halley's avatar
Bob Halley committed
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
	}

	RWUNLOCK(&keytable->rwlock, isc_rwlocktype_read);

	return (result);
}

dst_key_t *
dns_keynode_key(dns_keynode_t *keynode) {

	/*
	 * Get the DST key associated with keynode.
	 */

	REQUIRE(VALID_KEYNODE(keynode));

	return (keynode->key);
}