Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / dns / key.c
blobdec679e0e2f4ee0896a49f2524ef298e1e34a169
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004-2007 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2001 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: key.c,v 1.8 2007/06/19 23:47:16 tbox Exp */
22 #include <config.h>
24 #include <stddef.h>
25 #include <stdlib.h>
27 #include <isc/region.h>
28 #include <isc/util.h>
30 #include <dns/keyvalues.h>
32 #include <dst/dst.h>
34 #include "dst_internal.h"
36 isc_uint16_t
37 dst_region_computeid(const isc_region_t *source, unsigned int alg) {
38 isc_uint32_t ac;
39 const unsigned char *p;
40 int size;
42 REQUIRE(source != NULL);
43 REQUIRE(source->length >= 4);
45 p = source->base;
46 size = source->length;
48 if (alg == DST_ALG_RSAMD5)
49 return ((p[size - 3] << 8) + p[size - 2]);
51 for (ac = 0; size > 1; size -= 2, p += 2)
52 ac += ((*p) << 8) + *(p + 1);
54 if (size > 0)
55 ac += ((*p) << 8);
56 ac += (ac >> 16) & 0xffff;
58 return ((isc_uint16_t)(ac & 0xffff));
61 dns_name_t *
62 dst_key_name(const dst_key_t *key) {
63 REQUIRE(VALID_KEY(key));
64 return (key->key_name);
67 unsigned int
68 dst_key_size(const dst_key_t *key) {
69 REQUIRE(VALID_KEY(key));
70 return (key->key_size);
73 unsigned int
74 dst_key_proto(const dst_key_t *key) {
75 REQUIRE(VALID_KEY(key));
76 return (key->key_proto);
79 unsigned int
80 dst_key_alg(const dst_key_t *key) {
81 REQUIRE(VALID_KEY(key));
82 return (key->key_alg);
85 isc_uint32_t
86 dst_key_flags(const dst_key_t *key) {
87 REQUIRE(VALID_KEY(key));
88 return (key->key_flags);
91 dns_keytag_t
92 dst_key_id(const dst_key_t *key) {
93 REQUIRE(VALID_KEY(key));
94 return (key->key_id);
97 dns_rdataclass_t
98 dst_key_class(const dst_key_t *key) {
99 REQUIRE(VALID_KEY(key));
100 return (key->key_class);
103 isc_boolean_t
104 dst_key_iszonekey(const dst_key_t *key) {
105 REQUIRE(VALID_KEY(key));
107 if ((key->key_flags & DNS_KEYTYPE_NOAUTH) != 0)
108 return (ISC_FALSE);
109 if ((key->key_flags & DNS_KEYFLAG_OWNERMASK) != DNS_KEYOWNER_ZONE)
110 return (ISC_FALSE);
111 if (key->key_proto != DNS_KEYPROTO_DNSSEC &&
112 key->key_proto != DNS_KEYPROTO_ANY)
113 return (ISC_FALSE);
114 return (ISC_TRUE);
117 isc_boolean_t
118 dst_key_isnullkey(const dst_key_t *key) {
119 REQUIRE(VALID_KEY(key));
121 if ((key->key_flags & DNS_KEYFLAG_TYPEMASK) != DNS_KEYTYPE_NOKEY)
122 return (ISC_FALSE);
123 if ((key->key_flags & DNS_KEYFLAG_OWNERMASK) != DNS_KEYOWNER_ZONE)
124 return (ISC_FALSE);
125 if (key->key_proto != DNS_KEYPROTO_DNSSEC &&
126 key->key_proto != DNS_KEYPROTO_ANY)
127 return (ISC_FALSE);
128 return (ISC_TRUE);
131 void
132 dst_key_setbits(dst_key_t *key, isc_uint16_t bits) {
133 unsigned int maxbits;
134 REQUIRE(VALID_KEY(key));
135 if (bits != 0) {
136 RUNTIME_CHECK(dst_key_sigsize(key, &maxbits) == ISC_R_SUCCESS);
137 maxbits *= 8;
138 REQUIRE(bits <= maxbits);
140 key->key_bits = bits;
143 isc_uint16_t
144 dst_key_getbits(const dst_key_t *key) {
145 REQUIRE(VALID_KEY(key));
146 return (key->key_bits);
149 /*! \file */