OpenDNSSEC-signer  1.3.9
rrsigs.c
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2009 NLNet Labs. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
34 #include "config.h"
35 #include "shared/allocator.h"
36 #include "shared/file.h"
37 #include "shared/log.h"
38 #include "shared/util.h"
39 #include "signer/rrsigs.h"
40 #include "signer/keys.h"
41 
42 #include <ldns/ldns.h>
43 
44 static const char* rrsigs_str = "rrsig";
45 
46 
53 {
54  allocator_type* allocator = NULL;
55  rrsigs_type* rrsigs = NULL;
56 
57  allocator = allocator_create(malloc, free);
58  if (!allocator) {
59  ods_log_error("[%s] unable to create RRSIGs: create allocator "
60  "failed", rrsigs_str);
61  return NULL;
62  }
63  ods_log_assert(allocator);
64 
65  rrsigs = (rrsigs_type*) allocator_alloc(allocator, sizeof(rrsigs_type));
66  if (!rrsigs) {
67  ods_log_error("[%s] unable to create RRSIGs: allocator failed",
68  rrsigs_str);
69  allocator_cleanup(allocator);
70  return NULL;
71  }
72  ods_log_assert(rrsigs);
73 
74  rrsigs->allocator = allocator;
75  rrsigs->rr = NULL;
76  rrsigs->key_locator = NULL;
77  rrsigs->key_flags = 0;
78  rrsigs->next = NULL;
79  return rrsigs;
80 }
81 
82 
88 rrsigs_add_sig(rrsigs_type* rrsigs, ldns_rr* rr, const char* l, uint32_t f)
89 {
90  int cmp;
91  rrsigs_type* new_rrsigs = NULL;
92  ldns_status status = LDNS_STATUS_OK;
93 
94  if (!rrsigs) {
95  ods_log_error("[%s] unable to add RRSIG: no storage", rrsigs_str);
96  return ODS_STATUS_ASSERT_ERR;
97  }
98  ods_log_assert(rrsigs);
99 
100  if (!rr) {
101  ods_log_error("[%s] unable to add RRSIG: no RRSIG RR", rrsigs_str);
102  return ODS_STATUS_ASSERT_ERR;
103  }
104  ods_log_assert(rr);
105 
106  if (!rrsigs->rr) {
107  rrsigs->rr = rr;
108  if (l) {
109  rrsigs->key_locator = allocator_strdup(rrsigs->allocator, l);
110  }
111  rrsigs->key_flags = f;
112  return ODS_STATUS_OK;
113  }
114 
115  status = util_dnssec_rrs_compare(rrsigs->rr, rr, &cmp);
116  if (status != LDNS_STATUS_OK) {
117  return ODS_STATUS_ERR;
118  }
119  if (cmp < 0) {
120  if (rrsigs->next) {
121  return rrsigs_add_sig(rrsigs->next, rr, l, f);
122  } else {
123  new_rrsigs = rrsigs_create();
124  new_rrsigs->rr = rr;
125  if (l) {
126  new_rrsigs->key_locator = allocator_strdup(
127  rrsigs->allocator, l);
128  }
129  new_rrsigs->key_flags = f;
130  rrsigs->next = new_rrsigs;
131  return ODS_STATUS_OK;
132  }
133  } else if (cmp > 0) {
134  /* put the current old rr in the new next, put the new
135  rr in the current container */
136  new_rrsigs = rrsigs_create();
137  new_rrsigs->rr = rrsigs->rr;
138  new_rrsigs->key_locator = rrsigs->key_locator;
139  new_rrsigs->key_flags = rrsigs->key_flags;
140  new_rrsigs->next = rrsigs->next;
141 
142  rrsigs->rr = rr;
143  rrsigs->next = new_rrsigs;
144  if (l) {
145  rrsigs->key_locator = allocator_strdup(rrsigs->allocator, l);
146  }
147  rrsigs->key_flags = f;
148  return ODS_STATUS_OK;
149  } else {
150  /* should we error on equal? or free memory of rr */
151  ods_log_warning("[%s] adding duplicate RRSIG?", rrsigs_str);
152  return ODS_STATUS_UNCHANGED;
153  }
154  /* not reached */
155  return ODS_STATUS_ERR;
156 }
157 
158 
163 void
165 {
166  allocator_type* allocator;
167  if (!rrsigs) {
168  return;
169  }
170  if (rrsigs->next) {
171  rrsigs_cleanup(rrsigs->next);
172  rrsigs->next = NULL;
173  }
174  if (rrsigs->rr) {
175  ldns_rr_free(rrsigs->rr);
176  rrsigs->rr = NULL;
177  }
178  allocator = rrsigs->allocator;
179  allocator_deallocate(allocator, (void*) rrsigs->key_locator);
180  allocator_deallocate(allocator, (void*) rrsigs);
181  allocator_cleanup(allocator);
182  return;
183 }
184 
185 
190 void
191 rrsigs_print(FILE* fd, rrsigs_type* rrsigs, int print_key)
192 {
193  rrsigs_type* print = NULL;
194 
195  if (!fd) {
196  ods_log_error("[%s] unable to print: no fd", rrsigs_str);
197  return;
198  }
199  ods_log_assert(fd);
200 
201  print = rrsigs;
202  while (print) {
203  if (print_key) {
204  fprintf(fd, ";;RRSIG %s %u\n",
205  rrsigs->key_locator?rrsigs->key_locator:"(null)",
206  rrsigs->key_flags);
207  }
208  if (print->rr) {
209  ldns_rr_print(fd, print->rr);
210  }
211  print = print->next;
212  }
213  return;
214 }