OpenDNSSEC-signer 1.3.0
|
00001 /* 00002 * $Id$ 00003 * 00004 * Copyright (c) 2009 NLNet Labs. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00016 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00019 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00021 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00023 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00025 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 */ 00028 00034 #include "config.h" 00035 #include "shared/allocator.h" 00036 #include "shared/file.h" 00037 #include "shared/log.h" 00038 #include "shared/util.h" 00039 #include "signer/rrsigs.h" 00040 #include "signer/keys.h" 00041 00042 #include <ldns/ldns.h> 00043 00044 static const char* rrsigs_str = "rrsig"; 00045 00046 00051 rrsigs_type* 00052 rrsigs_create(void) 00053 { 00054 allocator_type* allocator = NULL; 00055 rrsigs_type* rrsigs = NULL; 00056 00057 allocator = allocator_create(malloc, free); 00058 if (!allocator) { 00059 ods_log_error("[%s] unable to create RRSIGs: create allocator " 00060 "failed", rrsigs_str); 00061 return NULL; 00062 } 00063 ods_log_assert(allocator); 00064 00065 rrsigs = (rrsigs_type*) allocator_alloc(allocator, sizeof(rrsigs_type)); 00066 if (!rrsigs) { 00067 ods_log_error("[%s] unable to create RRSIGs: allocator failed", 00068 rrsigs_str); 00069 allocator_cleanup(allocator); 00070 return NULL; 00071 } 00072 ods_log_assert(rrsigs); 00073 00074 rrsigs->allocator = allocator; 00075 rrsigs->rr = NULL; 00076 rrsigs->key_locator = NULL; 00077 rrsigs->key_flags = 0; 00078 rrsigs->next = NULL; 00079 return rrsigs; 00080 } 00081 00082 00087 ods_status 00088 rrsigs_add_sig(rrsigs_type* rrsigs, ldns_rr* rr, const char* l, uint32_t f) 00089 { 00090 int cmp; 00091 rrsigs_type* new_rrsigs = NULL; 00092 ldns_status status = LDNS_STATUS_OK; 00093 00094 if (!rrsigs) { 00095 ods_log_error("[%s] unable to add RRSIG: no storage", rrsigs_str); 00096 return ODS_STATUS_ASSERT_ERR; 00097 } 00098 ods_log_assert(rrsigs); 00099 00100 if (!rr) { 00101 ods_log_error("[%s] unable to add RRSIG: no RRSIG RR", rrsigs_str); 00102 return ODS_STATUS_ASSERT_ERR; 00103 } 00104 ods_log_assert(rr); 00105 00106 if (!rrsigs->rr) { 00107 rrsigs->rr = rr; 00108 if (l) { 00109 rrsigs->key_locator = allocator_strdup(rrsigs->allocator, l); 00110 } 00111 rrsigs->key_flags = f; 00112 return ODS_STATUS_OK; 00113 } 00114 00115 status = util_dnssec_rrs_compare(rrsigs->rr, rr, &cmp); 00116 if (status != LDNS_STATUS_OK) { 00117 return ODS_STATUS_ERR; 00118 } 00119 if (cmp < 0) { 00120 if (rrsigs->next) { 00121 return rrsigs_add_sig(rrsigs->next, rr, l, f); 00122 } else { 00123 new_rrsigs = rrsigs_create(); 00124 new_rrsigs->rr = rr; 00125 if (l) { 00126 new_rrsigs->key_locator = allocator_strdup( 00127 rrsigs->allocator, l); 00128 } 00129 new_rrsigs->key_flags = f; 00130 rrsigs->next = new_rrsigs; 00131 return ODS_STATUS_OK; 00132 } 00133 } else if (cmp > 0) { 00134 /* put the current old rr in the new next, put the new 00135 rr in the current container */ 00136 new_rrsigs = rrsigs_create(); 00137 new_rrsigs->rr = rrsigs->rr; 00138 new_rrsigs->key_locator = rrsigs->key_locator; 00139 new_rrsigs->key_flags = rrsigs->key_flags; 00140 new_rrsigs->next = rrsigs->next; 00141 00142 rrsigs->rr = rr; 00143 rrsigs->next = new_rrsigs; 00144 if (l) { 00145 rrsigs->key_locator = allocator_strdup(rrsigs->allocator, l); 00146 } 00147 rrsigs->key_flags = f; 00148 return ODS_STATUS_OK; 00149 } else { 00150 /* should we error on equal? or free memory of rr */ 00151 ods_log_warning("[%s] adding duplicate RRSIG?", rrsigs_str); 00152 return ODS_STATUS_UNCHANGED; 00153 } 00154 /* not reached */ 00155 return ODS_STATUS_ERR; 00156 } 00157 00158 00163 void 00164 rrsigs_cleanup(rrsigs_type* rrsigs) 00165 { 00166 allocator_type* allocator; 00167 if (!rrsigs) { 00168 return; 00169 } 00170 if (rrsigs->next) { 00171 rrsigs_cleanup(rrsigs->next); 00172 rrsigs->next = NULL; 00173 } 00174 if (rrsigs->rr) { 00175 ldns_rr_free(rrsigs->rr); 00176 rrsigs->rr = NULL; 00177 } 00178 allocator = rrsigs->allocator; 00179 allocator_deallocate(allocator, (void*) rrsigs->key_locator); 00180 allocator_deallocate(allocator, (void*) rrsigs); 00181 allocator_cleanup(allocator); 00182 return; 00183 } 00184 00185 00190 void 00191 rrsigs_print(FILE* fd, rrsigs_type* rrsigs, int print_key) 00192 { 00193 rrsigs_type* print = NULL; 00194 00195 if (!fd) { 00196 ods_log_error("[%s] unable to print: no fd", rrsigs_str); 00197 return; 00198 } 00199 ods_log_assert(fd); 00200 00201 print = rrsigs; 00202 while (print) { 00203 if (print_key) { 00204 fprintf(fd, ";;RRSIG %s %u\n", 00205 rrsigs->key_locator?rrsigs->key_locator:"(null)", 00206 rrsigs->key_flags); 00207 } 00208 if (print->rr) { 00209 ldns_rr_print(fd, print->rr); 00210 } 00211 print = print->next; 00212 } 00213 return; 00214 }