1 /*
2 * Copyright (c) 2009 NLNet Labs. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *
25 */
26
27 /**
28 * Signing keys.
29 *
30 */
31
32 #include "file.h"
33 #include "log.h"
34 #include "util.h"
35 #include "signer/backup.h"
36 #include "signer/keys.h"
37 #include "signer/signconf.h"
38 #include "status.h"
39
40 static const char* key_str = "keys";
41
42
43 /**
44 * Create a new key list.
45 *
46 */
47 keylist_type*
keylist_create(signconf_type * signconf)48 keylist_create(signconf_type* signconf)
49 {
50 keylist_type* kl = NULL;
51
52 if (!signconf) {
53 return NULL;
54 }
55 CHECKALLOC(kl = (keylist_type*) malloc((sizeof(keylist_type))));
56 if (!kl) {
57 ods_log_error("[%s] create list failed: allocator_alloc() failed",
58 key_str);
59 return NULL;
60 }
61 kl->sc = signconf;
62 kl->count = 0;
63 kl->keys = NULL;
64 return kl;
65 }
66
67
68 /**
69 * Lookup a key in the key list by locator.
70 *
71 */
72 key_type*
keylist_lookup_by_locator(keylist_type * kl,const char * locator)73 keylist_lookup_by_locator(keylist_type* kl, const char* locator)
74 {
75 uint16_t i = 0;
76 if (!kl || !locator || kl->count <= 0) {
77 return NULL;
78 }
79 for (i=0; i < kl->count; i++) {
80 if (&kl->keys[i] && kl->keys[i].locator) {
81 if (ods_strcmp(kl->keys[i].locator, locator) == 0) {
82 return &kl->keys[i];
83 }
84 }
85 }
86 return NULL;
87 }
88
89
90 /**
91 * Push a key to the key list.
92 *
93 */
94 key_type*
keylist_push(keylist_type * kl,const char * locator,const char * resourcerecord,uint8_t algorithm,uint32_t flags,int publish,int ksk,int zsk)95 keylist_push(keylist_type* kl, const char* locator, const char* resourcerecord,
96 uint8_t algorithm, uint32_t flags, int publish, int ksk, int zsk)
97 {
98 key_type* keys_old = NULL;
99
100 ods_log_assert(kl);
101
102 keys_old = kl->keys;
103 CHECKALLOC(kl->keys = (key_type*) malloc((kl->count + 1) * sizeof(key_type)));
104 if (!kl->keys) {
105 ods_fatal_exit("[%s] unable to add key: allocator_alloc() failed",
106 key_str);
107 }
108 if (keys_old) {
109 memcpy(kl->keys, keys_old, (kl->count) * sizeof(key_type));
110 }
111 free(keys_old);
112 kl->count++;
113 kl->keys[kl->count -1].locator = locator;
114 kl->keys[kl->count -1].resourcerecord = resourcerecord;
115 kl->keys[kl->count -1].algorithm = algorithm;
116 kl->keys[kl->count -1].flags = flags;
117 kl->keys[kl->count -1].publish = publish;
118 kl->keys[kl->count -1].ksk = ksk;
119 kl->keys[kl->count -1].zsk = zsk;
120 kl->keys[kl->count -1].dnskey = NULL;
121 kl->keys[kl->count -1].params = NULL;
122 return &kl->keys[kl->count -1];
123 }
124
125
126 /**
127 * Log key.
128 *
129 */
130 static void
key_log(key_type * key,const char * name)131 key_log(key_type* key, const char* name)
132 {
133 if (!key) {
134 return;
135 }
136 ods_log_debug("[%s] zone %s key: LOCATOR[%s] FLAGS[%u] ALGORITHM[%u] "
137 "KSK[%i] ZSK[%i] PUBLISH[%i]", key_str, name?name:"(null)", key->locator,
138 key->flags, key->algorithm, key->ksk, key->zsk, key->publish);
139 }
140
141
142 /**
143 * Log key list.
144 *
145 */
146 void
keylist_log(keylist_type * kl,const char * name)147 keylist_log(keylist_type* kl, const char* name)
148 {
149 uint16_t i = 0;
150 if (!kl || kl->count <= 0) {
151 return;
152 }
153 for (i=0; i < kl->count; i++) {
154 key_log(&kl->keys[i], name);
155 }
156 }
157
158
159 /**
160 * Clean up key.
161 *
162 */
163 static void
key_delfunc(key_type * key)164 key_delfunc(key_type* key)
165 {
166 if (!key) {
167 return;
168 }
169 /*We leak this every time the signconf is reloaded. Although the IXFR structure*/
170 /*copies this RR there is a race condition between this func and the ixfr_del*/
171 /*function to copy / delete it. */
172 /*ldns_rr_free(key->dnskey);*/
173 hsm_sign_params_free(key->params);
174 free((void*) key->locator);
175 }
176
177
178 /**
179 * Clean up key list.
180 *
181 */
182 void
keylist_cleanup(keylist_type * kl)183 keylist_cleanup(keylist_type* kl)
184 {
185 uint16_t i = 0;
186 if (!kl) {
187 return;
188 }
189 for (i=0; i < kl->count; i++) {
190 key_delfunc(&kl->keys[i]);
191 }
192 free(kl->keys);
193 free(kl);
194 }
195
196
197 /**
198 * Backup key.
199 *
200 */
201 static void
key_backup(FILE * fd,key_type * key,const char * version)202 key_backup(FILE* fd, key_type* key, const char* version)
203 {
204 if (!fd || !key) {
205 return;
206 }
207 fprintf(fd, ";;Key: locator %s algorithm %u flags %u publish %i ksk %i zsk %i keytag %d\n", key->locator, (unsigned) key->algorithm,
208 (unsigned) key->flags, key->publish, key->ksk, key->zsk, ldns_calc_keytag(key->dnskey));
209 if (strcmp(version, ODS_SE_FILE_MAGIC_V2) == 0) {
210 if (key->dnskey) {
211 (void)util_rr_print(fd, key->dnskey);
212 }
213 fprintf(fd, ";;Keydone\n");
214 }
215 }
216
217
218 /**
219 * Recover key from backup.
220 *
221 */
222 key_type*
key_recover2(FILE * fd,keylist_type * kl)223 key_recover2(FILE* fd, keylist_type* kl)
224 {
225 const char* locator = NULL;
226 const char* resourcerecord = NULL;
227 uint8_t algorithm = 0;
228 uint32_t flags = 0;
229 int publish = 0;
230 int ksk = 0;
231 int zsk = 0;
232 int keytag = 0; /* We are not actually interested but we must
233 parse it to continue correctly in the stream.
234 When reading 1.4.8 or later version backup file, the real value of keytag is
235 rfc5011, but not importat due to not using it.*/
236 ods_log_assert(fd);
237
238 if (!backup_read_check_str(fd, "locator") ||
239 !backup_read_str(fd, &locator) ||
240 !backup_read_check_str(fd, "algorithm") ||
241 !backup_read_uint8_t(fd, &algorithm) ||
242 !backup_read_check_str(fd, "flags") ||
243 !backup_read_uint32_t(fd, &flags) ||
244 !backup_read_check_str(fd, "publish") ||
245 !backup_read_int(fd, &publish) ||
246 !backup_read_check_str(fd, "ksk") ||
247 !backup_read_int(fd, &ksk) ||
248 !backup_read_check_str(fd, "zsk") ||
249 !backup_read_int(fd, &zsk) ||
250 !backup_read_check_str(fd, "keytag") ||
251 !backup_read_int(fd, &keytag)) {
252 if (locator) {
253 free((void*)locator);
254 locator = NULL;
255 }
256 return NULL;
257 }
258 /* key ok */
259 return keylist_push(kl, locator, resourcerecord, algorithm, flags, publish, ksk, zsk);
260 }
261
262
263 /**
264 * Backup key list.
265 *
266 */
267 void
keylist_backup(FILE * fd,keylist_type * kl,const char * version)268 keylist_backup(FILE* fd, keylist_type* kl, const char* version)
269 {
270 uint16_t i = 0;
271 if (!fd || !kl || kl->count <= 0) {
272 return;
273 }
274 for (i=0; i < kl->count; i++) {
275 key_backup(fd, &kl->keys[i], version);
276 }
277 }
278