1 /* $NetBSD: key.c,v 1.5 2014/12/10 04:37:58 christos Exp $ */
2
3 /*
4 * Copyright (C) 2004-2007, 2011 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2001 Internet Software Consortium.
6 *
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.
10 *
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.
18 */
19
20 /* Id: key.c,v 1.11 2011/10/20 21:20:02 marka Exp */
21
22 #include <config.h>
23
24 #include <stddef.h>
25 #include <stdlib.h>
26
27 #include <isc/region.h>
28 #include <isc/util.h>
29
30 #include <dns/keyvalues.h>
31
32 #include <dst/dst.h>
33
34 #include "dst_internal.h"
35
36 isc_uint16_t
dst_region_computeid(const isc_region_t * source,unsigned int alg)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;
41
42 REQUIRE(source != NULL);
43 REQUIRE(source->length >= 4);
44
45 p = source->base;
46 size = source->length;
47
48 if (alg == DST_ALG_RSAMD5)
49 return ((p[size - 3] << 8) + p[size - 2]);
50
51 for (ac = 0; size > 1; size -= 2, p += 2)
52 ac += ((*p) << 8) + *(p + 1);
53
54 if (size > 0)
55 ac += ((*p) << 8);
56 ac += (ac >> 16) & 0xffff;
57
58 return ((isc_uint16_t)(ac & 0xffff));
59 }
60
61 isc_uint16_t
dst_region_computerid(const isc_region_t * source,unsigned int alg)62 dst_region_computerid(const isc_region_t *source, unsigned int alg) {
63 isc_uint32_t ac;
64 const unsigned char *p;
65 int size;
66
67 REQUIRE(source != NULL);
68 REQUIRE(source->length >= 4);
69
70 p = source->base;
71 size = source->length;
72
73 if (alg == DST_ALG_RSAMD5)
74 return ((p[size - 3] << 8) + p[size - 2]);
75
76 ac = ((*p) << 8) + *(p + 1);
77 ac |= DNS_KEYFLAG_REVOKE;
78 for (size -= 2, p +=2; size > 1; size -= 2, p += 2)
79 ac += ((*p) << 8) + *(p + 1);
80
81 if (size > 0)
82 ac += ((*p) << 8);
83 ac += (ac >> 16) & 0xffff;
84
85 return ((isc_uint16_t)(ac & 0xffff));
86 }
87
88 dns_name_t *
dst_key_name(const dst_key_t * key)89 dst_key_name(const dst_key_t *key) {
90 REQUIRE(VALID_KEY(key));
91 return (key->key_name);
92 }
93
94 unsigned int
dst_key_size(const dst_key_t * key)95 dst_key_size(const dst_key_t *key) {
96 REQUIRE(VALID_KEY(key));
97 return (key->key_size);
98 }
99
100 unsigned int
dst_key_proto(const dst_key_t * key)101 dst_key_proto(const dst_key_t *key) {
102 REQUIRE(VALID_KEY(key));
103 return (key->key_proto);
104 }
105
106 unsigned int
dst_key_alg(const dst_key_t * key)107 dst_key_alg(const dst_key_t *key) {
108 REQUIRE(VALID_KEY(key));
109 return (key->key_alg);
110 }
111
112 isc_uint32_t
dst_key_flags(const dst_key_t * key)113 dst_key_flags(const dst_key_t *key) {
114 REQUIRE(VALID_KEY(key));
115 return (key->key_flags);
116 }
117
118 dns_keytag_t
dst_key_id(const dst_key_t * key)119 dst_key_id(const dst_key_t *key) {
120 REQUIRE(VALID_KEY(key));
121 return (key->key_id);
122 }
123
124 dns_keytag_t
dst_key_rid(const dst_key_t * key)125 dst_key_rid(const dst_key_t *key) {
126 REQUIRE(VALID_KEY(key));
127 return (key->key_rid);
128 }
129
130 dns_rdataclass_t
dst_key_class(const dst_key_t * key)131 dst_key_class(const dst_key_t *key) {
132 REQUIRE(VALID_KEY(key));
133 return (key->key_class);
134 }
135
136 isc_boolean_t
dst_key_iszonekey(const dst_key_t * key)137 dst_key_iszonekey(const dst_key_t *key) {
138 REQUIRE(VALID_KEY(key));
139
140 if ((key->key_flags & DNS_KEYTYPE_NOAUTH) != 0)
141 return (ISC_FALSE);
142 if ((key->key_flags & DNS_KEYFLAG_OWNERMASK) != DNS_KEYOWNER_ZONE)
143 return (ISC_FALSE);
144 if (key->key_proto != DNS_KEYPROTO_DNSSEC &&
145 key->key_proto != DNS_KEYPROTO_ANY)
146 return (ISC_FALSE);
147 return (ISC_TRUE);
148 }
149
150 isc_boolean_t
dst_key_isnullkey(const dst_key_t * key)151 dst_key_isnullkey(const dst_key_t *key) {
152 REQUIRE(VALID_KEY(key));
153
154 if ((key->key_flags & DNS_KEYFLAG_TYPEMASK) != DNS_KEYTYPE_NOKEY)
155 return (ISC_FALSE);
156 if ((key->key_flags & DNS_KEYFLAG_OWNERMASK) != DNS_KEYOWNER_ZONE)
157 return (ISC_FALSE);
158 if (key->key_proto != DNS_KEYPROTO_DNSSEC &&
159 key->key_proto != DNS_KEYPROTO_ANY)
160 return (ISC_FALSE);
161 return (ISC_TRUE);
162 }
163
164 void
dst_key_setbits(dst_key_t * key,isc_uint16_t bits)165 dst_key_setbits(dst_key_t *key, isc_uint16_t bits) {
166 unsigned int maxbits;
167 REQUIRE(VALID_KEY(key));
168 if (bits != 0) {
169 RUNTIME_CHECK(dst_key_sigsize(key, &maxbits) == ISC_R_SUCCESS);
170 maxbits *= 8;
171 REQUIRE(bits <= maxbits);
172 }
173 key->key_bits = bits;
174 }
175
176 isc_uint16_t
dst_key_getbits(const dst_key_t * key)177 dst_key_getbits(const dst_key_t *key) {
178 REQUIRE(VALID_KEY(key));
179 return (key->key_bits);
180 }
181
182 void
dst_key_setttl(dst_key_t * key,dns_ttl_t ttl)183 dst_key_setttl(dst_key_t *key, dns_ttl_t ttl) {
184 REQUIRE(VALID_KEY(key));
185 key->key_ttl = ttl;
186 }
187
188 dns_ttl_t
dst_key_getttl(const dst_key_t * key)189 dst_key_getttl(const dst_key_t *key) {
190 REQUIRE(VALID_KEY(key));
191 return (key->key_ttl);
192 }
193
194 /*! \file */
195