OpenDNSSEC-signer 1.2.1
|
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 "signer/rrsigs.h" 00036 #include "signer/se_key.h" 00037 #include "util/log.h" 00038 #include "util/se_malloc.h" 00039 #include "util/util.h" 00040 00041 #include <ldns/ldns.h> 00042 00043 00048 rrsigs_type* 00049 rrsigs_create(void) 00050 { 00051 rrsigs_type* rrsigs = (rrsigs_type*) se_calloc(1, sizeof(rrsigs_type)); 00052 rrsigs->rr = NULL; 00053 rrsigs->key_locator = NULL; 00054 rrsigs->key_flags = 0; 00055 rrsigs->next = NULL; 00056 return rrsigs; 00057 } 00058 00059 00064 int 00065 rrsigs_add_sig(rrsigs_type* rrsigs, ldns_rr* rr, const char* locator, 00066 uint32_t flags) 00067 { 00068 int cmp; 00069 rrsigs_type* new_rrsigs = NULL; 00070 ldns_status status = LDNS_STATUS_OK; 00071 00072 se_log_assert(rrsigs); 00073 se_log_assert(rr); 00074 00075 if (!rrsigs->rr) { 00076 rrsigs->rr = rr; 00077 if (locator) { 00078 rrsigs->key_locator = se_strdup(locator); 00079 } 00080 rrsigs->key_flags = flags; 00081 return 0; 00082 } 00083 00084 status = util_dnssec_rrs_compare(rrsigs->rr, rr, &cmp); 00085 if (status != LDNS_STATUS_OK) { 00086 return 1; 00087 } 00088 00089 if (cmp < 0) { 00090 if (rrsigs->next) { 00091 return rrsigs_add_sig(rrsigs->next, rr, locator, flags); 00092 } else { 00093 new_rrsigs = rrsigs_create(); 00094 new_rrsigs->rr = rr; 00095 if (locator) { 00096 new_rrsigs->key_locator = se_strdup(locator); 00097 } 00098 new_rrsigs->key_flags = flags; 00099 00100 rrsigs->next = new_rrsigs; 00101 return 0; 00102 } 00103 } else if (cmp > 0) { 00104 /* put the current old rr in the new next, put the new 00105 rr in the current container */ 00106 new_rrsigs = rrsigs_create(); 00107 new_rrsigs->rr = rrsigs->rr; 00108 new_rrsigs->key_locator = rrsigs->key_locator; 00109 new_rrsigs->key_flags = rrsigs->key_flags; 00110 new_rrsigs->next = rrsigs->next; 00111 00112 rrsigs->rr = rr; 00113 rrsigs->next = new_rrsigs; 00114 if (locator) { 00115 rrsigs->key_locator = se_strdup(locator); 00116 } 00117 rrsigs->key_flags = flags; 00118 return 0; 00119 } else { 00120 /* should we error on equal? or free memory of rr */ 00121 se_log_warning("adding duplicate RRSIG?"); 00122 return 2; 00123 } 00124 return 0; 00125 } 00126 00127 00128 /* 00129 * Clean up signature set. 00130 * 00131 */ 00132 void 00133 rrsigs_cleanup(rrsigs_type* rrsigs) 00134 { 00135 if (rrsigs) { 00136 if (rrsigs->next) { 00137 rrsigs_cleanup(rrsigs->next); 00138 rrsigs->next = NULL; 00139 } 00140 if (rrsigs->rr) { 00141 ldns_rr_free(rrsigs->rr); 00142 rrsigs->rr = NULL; 00143 } 00144 if (rrsigs->key_locator) { 00145 se_free((void*)rrsigs->key_locator); 00146 rrsigs->key_locator = NULL; 00147 } 00148 se_free((void*) rrsigs); 00149 } else { 00150 se_log_warning("cleanup empty rrsigs"); 00151 } 00152 return; 00153 } 00154 00155 00160 void 00161 rrsigs_print(FILE* fd, rrsigs_type* rrsigs, int print_key) 00162 { 00163 rrsigs_type* print = NULL; 00164 00165 se_log_assert(fd); 00166 00167 print = rrsigs; 00168 while (print) { 00169 if (print_key) { 00170 fprintf(fd, ";RRSIG %s %u\n", 00171 rrsigs->key_locator?rrsigs->key_locator:"(null)", 00172 rrsigs->key_flags); 00173 } 00174 ldns_rr_print(fd, print->rr); 00175 00176 print = print->next; 00177 } 00178 return; 00179 }