]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/validator/val_kentry.c
2 * validator/val_kentry.c - validator key entry definition.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains functions for dealing with validator key entries.
42 #include "validator/val_kentry.h"
43 #include "util/data/packed_rrset.h"
44 #include "util/data/dname.h"
45 #include "util/storage/lookup3.h"
46 #include "util/regional.h"
47 #include "util/net_help.h"
48 #include "ldns/rrdef.h"
49 #include "ldns/keyraw.h"
52 key_entry_sizefunc(void* key
, void* data
)
54 struct key_entry_key
* kk
= (struct key_entry_key
*)key
;
55 struct key_entry_data
* kd
= (struct key_entry_data
*)data
;
56 size_t s
= sizeof(*kk
) + kk
->namelen
;
57 s
+= sizeof(*kd
) + lock_get_mem(&kk
->entry
.lock
);
59 s
+= packed_rrset_sizeof(kd
->rrset_data
);
61 s
+= strlen(kd
->reason
)+1;
63 s
+= strlen((char*)kd
->algo
)+1;
68 key_entry_compfunc(void* k1
, void* k2
)
70 struct key_entry_key
* n1
= (struct key_entry_key
*)k1
;
71 struct key_entry_key
* n2
= (struct key_entry_key
*)k2
;
72 if(n1
->key_class
!= n2
->key_class
) {
73 if(n1
->key_class
< n2
->key_class
)
77 return query_dname_compare(n1
->name
, n2
->name
);
81 key_entry_delkeyfunc(void* key
, void* ATTR_UNUSED(userarg
))
83 struct key_entry_key
* kk
= (struct key_entry_key
*)key
;
86 lock_rw_destroy(&kk
->entry
.lock
);
92 key_entry_deldatafunc(void* data
, void* ATTR_UNUSED(userarg
))
94 struct key_entry_data
* kd
= (struct key_entry_data
*)data
;
102 key_entry_hash(struct key_entry_key
* kk
)
104 kk
->entry
.hash
= 0x654;
105 kk
->entry
.hash
= hashlittle(&kk
->key_class
, sizeof(kk
->key_class
),
107 kk
->entry
.hash
= dname_query_hash(kk
->name
, kk
->entry
.hash
);
110 struct key_entry_key
*
111 key_entry_copy_toregion(struct key_entry_key
* kkey
, struct regional
* region
)
113 struct key_entry_key
* newk
;
114 newk
= regional_alloc_init(region
, kkey
, sizeof(*kkey
));
117 newk
->name
= regional_alloc_init(region
, kkey
->name
, kkey
->namelen
);
120 newk
->entry
.key
= newk
;
121 if(newk
->entry
.data
) {
122 /* copy data element */
123 struct key_entry_data
*d
= (struct key_entry_data
*)
125 struct key_entry_data
*newd
;
126 newd
= regional_alloc_init(region
, d
, sizeof(*d
));
131 newd
->rrset_data
= regional_alloc_init(region
,
133 packed_rrset_sizeof(d
->rrset_data
));
134 if(!newd
->rrset_data
)
136 packed_rrset_ptr_fixup(newd
->rrset_data
);
139 newd
->reason
= regional_strdup(region
, d
->reason
);
144 newd
->algo
= (uint8_t*)regional_strdup(region
,
149 newk
->entry
.data
= newd
;
154 struct key_entry_key
*
155 key_entry_copy(struct key_entry_key
* kkey
)
157 struct key_entry_key
* newk
;
160 newk
= memdup(kkey
, sizeof(*kkey
));
163 newk
->name
= memdup(kkey
->name
, kkey
->namelen
);
168 lock_rw_init(&newk
->entry
.lock
);
169 newk
->entry
.key
= newk
;
170 if(newk
->entry
.data
) {
171 /* copy data element */
172 struct key_entry_data
*d
= (struct key_entry_data
*)
174 struct key_entry_data
*newd
;
175 newd
= memdup(d
, sizeof(*d
));
183 newd
->rrset_data
= memdup(d
->rrset_data
,
184 packed_rrset_sizeof(d
->rrset_data
));
185 if(!newd
->rrset_data
) {
191 packed_rrset_ptr_fixup(newd
->rrset_data
);
194 newd
->reason
= strdup(d
->reason
);
196 free(newd
->rrset_data
);
204 newd
->algo
= (uint8_t*)strdup((char*)d
->algo
);
206 free(newd
->rrset_data
);
214 newk
->entry
.data
= newd
;
220 key_entry_isnull(struct key_entry_key
* kkey
)
222 struct key_entry_data
* d
= (struct key_entry_data
*)kkey
->entry
.data
;
223 return (!d
->isbad
&& d
->rrset_data
== NULL
);
227 key_entry_isgood(struct key_entry_key
* kkey
)
229 struct key_entry_data
* d
= (struct key_entry_data
*)kkey
->entry
.data
;
230 return (!d
->isbad
&& d
->rrset_data
!= NULL
);
234 key_entry_isbad(struct key_entry_key
* kkey
)
236 struct key_entry_data
* d
= (struct key_entry_data
*)kkey
->entry
.data
;
237 return (int)(d
->isbad
);
241 key_entry_set_reason(struct key_entry_key
* kkey
, char* reason
)
243 struct key_entry_data
* d
= (struct key_entry_data
*)kkey
->entry
.data
;
248 key_entry_get_reason(struct key_entry_key
* kkey
)
250 struct key_entry_data
* d
= (struct key_entry_data
*)kkey
->entry
.data
;
254 /** setup key entry in region */
256 key_entry_setup(struct regional
* region
,
257 uint8_t* name
, size_t namelen
, uint16_t dclass
,
258 struct key_entry_key
** k
, struct key_entry_data
** d
)
260 *k
= regional_alloc(region
, sizeof(**k
));
263 memset(*k
, 0, sizeof(**k
));
264 (*k
)->entry
.key
= *k
;
265 (*k
)->name
= regional_alloc_init(region
, name
, namelen
);
268 (*k
)->namelen
= namelen
;
269 (*k
)->key_class
= dclass
;
270 *d
= regional_alloc(region
, sizeof(**d
));
273 (*k
)->entry
.data
= *d
;
277 struct key_entry_key
*
278 key_entry_create_null(struct regional
* region
,
279 uint8_t* name
, size_t namelen
, uint16_t dclass
, time_t ttl
,
282 struct key_entry_key
* k
;
283 struct key_entry_data
* d
;
284 if(!key_entry_setup(region
, name
, namelen
, dclass
, &k
, &d
))
289 d
->rrset_type
= LDNS_RR_TYPE_DNSKEY
;
290 d
->rrset_data
= NULL
;
295 struct key_entry_key
*
296 key_entry_create_rrset(struct regional
* region
,
297 uint8_t* name
, size_t namelen
, uint16_t dclass
,
298 struct ub_packed_rrset_key
* rrset
, uint8_t* sigalg
, time_t now
)
300 struct key_entry_key
* k
;
301 struct key_entry_data
* d
;
302 struct packed_rrset_data
* rd
= (struct packed_rrset_data
*)
304 if(!key_entry_setup(region
, name
, namelen
, dclass
, &k
, &d
))
306 d
->ttl
= rd
->ttl
+ now
;
309 d
->rrset_type
= ntohs(rrset
->rk
.type
);
310 d
->rrset_data
= (struct packed_rrset_data
*)regional_alloc_init(region
,
311 rd
, packed_rrset_sizeof(rd
));
315 d
->algo
= (uint8_t*)regional_strdup(region
, (char*)sigalg
);
318 } else d
->algo
= NULL
;
319 packed_rrset_ptr_fixup(d
->rrset_data
);
323 struct key_entry_key
*
324 key_entry_create_bad(struct regional
* region
,
325 uint8_t* name
, size_t namelen
, uint16_t dclass
, time_t ttl
,
328 struct key_entry_key
* k
;
329 struct key_entry_data
* d
;
330 if(!key_entry_setup(region
, name
, namelen
, dclass
, &k
, &d
))
335 d
->rrset_type
= LDNS_RR_TYPE_DNSKEY
;
336 d
->rrset_data
= NULL
;
341 struct ub_packed_rrset_key
*
342 key_entry_get_rrset(struct key_entry_key
* kkey
, struct regional
* region
)
344 struct key_entry_data
* d
= (struct key_entry_data
*)kkey
->entry
.data
;
345 struct ub_packed_rrset_key
* rrk
;
346 struct packed_rrset_data
* rrd
;
347 if(!d
|| !d
->rrset_data
)
349 rrk
= regional_alloc(region
, sizeof(*rrk
));
352 memset(rrk
, 0, sizeof(*rrk
));
353 rrk
->rk
.dname
= regional_alloc_init(region
, kkey
->name
, kkey
->namelen
);
356 rrk
->rk
.dname_len
= kkey
->namelen
;
357 rrk
->rk
.type
= htons(d
->rrset_type
);
358 rrk
->rk
.rrset_class
= htons(kkey
->key_class
);
359 rrk
->entry
.key
= rrk
;
360 rrd
= regional_alloc_init(region
, d
->rrset_data
,
361 packed_rrset_sizeof(d
->rrset_data
));
364 rrk
->entry
.data
= rrd
;
365 packed_rrset_ptr_fixup(rrd
);
369 /** Get size of key in keyset */
371 dnskey_get_keysize(struct packed_rrset_data
* data
, size_t idx
)
374 unsigned int pklen
= 0;
376 if(data
->rr_len
[idx
] < 2+5)
378 algo
= (int)data
->rr_data
[idx
][2+3];
379 pk
= (unsigned char*)data
->rr_data
[idx
]+2+4;
380 pklen
= (unsigned)data
->rr_len
[idx
]-2-4;
381 return sldns_rr_dnskey_key_size_raw(pk
, pklen
, algo
);
384 /** get dnskey flags from data */
386 kd_get_flags(struct packed_rrset_data
* data
, size_t idx
)
389 if(data
->rr_len
[idx
] < 2+2)
391 memmove(&f
, data
->rr_data
[idx
]+2, 2);
397 key_entry_keysize(struct key_entry_key
* kkey
)
399 struct packed_rrset_data
* d
;
400 /* compute size of smallest ZSK key in the rrset */
403 if(!key_entry_isgood(kkey
))
405 d
= ((struct key_entry_data
*)kkey
->entry
.data
)->rrset_data
;
406 for(i
=0; i
<d
->count
; i
++) {
407 if(!(kd_get_flags(d
, i
) & DNSKEY_BIT_ZSK
))
409 if(i
==0 || dnskey_get_keysize(d
, i
) < bits
)
410 bits
= dnskey_get_keysize(d
, i
);