OpenDNSSEC-signer  2.0.2
keys.c
Go to the documentation of this file.
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 
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 
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 
72 key_type*
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 
94 key_type*
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 
130 static void
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 
146 void
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 
163 static void
164 key_delfunc(key_type* key)
165 {
166  if (!key) {
167  return;
168  }
169  /* ldns_rr_free(key->dnskey); */
170  hsm_sign_params_free(key->params);
171  free((void*) key->locator);
172 }
173 
174 
179 void
181 {
182  uint16_t i = 0;
183  if (!kl) {
184  return;
185  }
186  for (i=0; i < kl->count; i++) {
187  key_delfunc(&kl->keys[i]);
188  }
189  free(kl->keys);
190  free(kl);
191 }
192 
193 
198 static void
199 key_backup(FILE* fd, key_type* key, const char* version)
200 {
201  if (!fd || !key) {
202  return;
203  }
204  fprintf(fd, ";;Key: locator %s algorithm %u flags %u publish %i ksk %i zsk %i keytag %d\n", key->locator, (unsigned) key->algorithm,
205  (unsigned) key->flags, key->publish, key->ksk, key->zsk, ldns_calc_keytag(key->dnskey));
206  if (strcmp(version, ODS_SE_FILE_MAGIC_V2) == 0) {
207  if (key->dnskey) {
208  (void)util_rr_print(fd, key->dnskey);
209  }
210  fprintf(fd, ";;Keydone\n");
211  }
212 }
213 
214 
219 key_type*
221 {
222  const char* locator = NULL;
223  const char* resourcerecord = NULL;
224  uint8_t algorithm = 0;
225  uint32_t flags = 0;
226  int publish = 0;
227  int ksk = 0;
228  int zsk = 0;
229  int keytag = 0; /* We are not actually interested but we must
230  parse it to continue correctly in the stream.
231  When reading 1.4.8 or later version backup file, the real value of keytag is
232  rfc5011, but not importat due to not using it.*/
233  ods_log_assert(fd);
234 
235  if (!backup_read_check_str(fd, "locator") ||
236  !backup_read_str(fd, &locator) ||
237  !backup_read_check_str(fd, "algorithm") ||
238  !backup_read_uint8_t(fd, &algorithm) ||
239  !backup_read_check_str(fd, "flags") ||
240  !backup_read_uint32_t(fd, &flags) ||
241  !backup_read_check_str(fd, "publish") ||
242  !backup_read_int(fd, &publish) ||
243  !backup_read_check_str(fd, "ksk") ||
244  !backup_read_int(fd, &ksk) ||
245  !backup_read_check_str(fd, "zsk") ||
246  !backup_read_int(fd, &zsk) ||
247  !backup_read_check_str(fd, "keytag") ||
248  !backup_read_int(fd, &keytag)) {
249  if (locator) {
250  free((void*)locator);
251  locator = NULL;
252  }
253  return NULL;
254  }
255  /* key ok */
256  return keylist_push(kl, locator, resourcerecord, algorithm, flags, publish, ksk, zsk);
257 }
258 
259 
264 void
265 keylist_backup(FILE* fd, keylist_type* kl, const char* version)
266 {
267  uint16_t i = 0;
268  if (!fd || !kl || kl->count <= 0) {
269  return;
270  }
271  for (i=0; i < kl->count; i++) {
272  key_backup(fd, &kl->keys[i], version);
273  }
274 }
void keylist_cleanup(keylist_type *kl)
Definition: keys.c:180
int backup_read_str(FILE *in, const char **str)
Definition: backup.c:105
int publish
Definition: keys.h:57
int zsk
Definition: keys.h:59
void keylist_log(keylist_type *kl, const char *name)
Definition: keys.c:147
key_type * keylist_lookup_by_locator(keylist_type *kl, const char *locator)
Definition: keys.c:73
int backup_read_uint8_t(FILE *in, uint8_t *v)
Definition: backup.c:194
ldns_rr * dnskey
Definition: keys.h:51
keylist_type * keylist_create(signconf_type *signconf)
Definition: keys.c:48
int backup_read_int(FILE *in, int *v)
Definition: backup.c:177
signconf_type * sc
Definition: keys.h:67
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)
Definition: keys.c:95
const char * locator
Definition: keys.h:53
key_type * keys
Definition: keys.h:68
const char * resourcerecord
Definition: keys.h:54
size_t count
Definition: keys.h:69
int ksk
Definition: keys.h:58
uint8_t algorithm
Definition: keys.h:55
int backup_read_check_str(FILE *in, const char *str)
Definition: backup.c:77
hsm_sign_params_t * params
Definition: keys.h:52
uint32_t flags
Definition: keys.h:56
key_type * key_recover2(FILE *fd, keylist_type *kl)
Definition: keys.c:220
void keylist_backup(FILE *fd, keylist_type *kl, const char *version)
Definition: keys.c:265
int backup_read_uint32_t(FILE *in, uint32_t *v)
Definition: backup.c:211