1 /* $OpenBSD: dnssec.c,v 1.23 2005/04/08 22:32:09 cloder Exp $ */ 2 3 /* 4 * Copyright (c) 2001 H�kan Olsson. 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 WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/types.h> 28 #include <netinet/in.h> 29 #include <arpa/nameser.h> 30 #include <arpa/inet.h> 31 #include <stdlib.h> 32 33 #include <openssl/rsa.h> 34 35 #ifdef LWRES 36 #include <lwres/netdb.h> 37 #include <dns/keyvalues.h> 38 #else 39 #include <netdb.h> 40 #endif 41 42 #include "dnssec.h" 43 #include "exchange.h" 44 #include "ipsec_num.h" 45 #include "libcrypto.h" 46 #include "log.h" 47 #include "message.h" 48 #include "transport.h" 49 #include "util.h" 50 51 #ifndef DNS_UFQDN_SEPARATOR 52 #define DNS_UFQDN_SEPARATOR "._ipsec." 53 #endif 54 55 /* adapted from <dns/rdatastruct.h> / RFC 2535 */ 56 struct dns_rdata_key { 57 u_int16_t flags; 58 u_int8_t protocol; 59 u_int8_t algorithm; 60 u_int16_t datalen; 61 unsigned char *data; 62 }; 63 64 void * 65 dns_get_key(int type, struct message *msg, int *keylen) 66 { 67 struct exchange *exchange = msg->exchange; 68 struct rrsetinfo *rr; 69 struct dns_rdata_key key_rr; 70 char name[MAXHOSTNAMELEN]; 71 in_addr_t ip4; 72 u_int8_t algorithm, *id, *umark; 73 size_t id_len; 74 int ret, i; 75 76 switch (type) { 77 case IKE_AUTH_RSA_SIG: 78 algorithm = DNS_KEYALG_RSA; 79 break; 80 81 case IKE_AUTH_RSA_ENC: 82 case IKE_AUTH_RSA_ENC_REV: 83 /* XXX Not yet. */ 84 /* algorithm = DNS_KEYALG_RSA; */ 85 return 0; 86 87 case IKE_AUTH_DSS: 88 /* XXX Not yet. */ 89 /* algorithm = DNS_KEYALG_DSS; */ 90 return 0; 91 92 case IKE_AUTH_PRE_SHARED: 93 default: 94 return 0; 95 } 96 97 id = exchange->initiator ? exchange->id_r : exchange->id_i; 98 id_len = exchange->initiator ? exchange->id_r_len : exchange->id_i_len; 99 bzero(name, sizeof name); 100 101 if (!id || id_len == 0) { 102 log_print("dns_get_key: ID is missing"); 103 return 0; 104 } 105 /* Exchanges (and SAs) don't carry the ID in ISAKMP form */ 106 id -= ISAKMP_GEN_SZ; 107 id_len += ISAKMP_GEN_SZ - ISAKMP_ID_DATA_OFF; 108 109 switch (GET_ISAKMP_ID_TYPE(id)) { 110 case IPSEC_ID_IPV4_ADDR: 111 /* We want to lookup a KEY RR in the reverse zone. */ 112 if (id_len < sizeof ip4) 113 return 0; 114 memcpy(&ip4, id + ISAKMP_ID_DATA_OFF, sizeof ip4); 115 snprintf(name, sizeof name, "%d.%d.%d.%d.in-addr.arpa.", ip4 116 >> 24, (ip4 >> 16) & 0xFF, (ip4 >> 8) & 0xFF, ip4 & 0xFF); 117 break; 118 119 case IPSEC_ID_IPV6_ADDR: 120 /* XXX Not yet. */ 121 return 0; 122 break; 123 124 case IPSEC_ID_FQDN: 125 if ((id_len + 1) >= sizeof name) 126 return 0; 127 /* ID is not NULL-terminated. Add trailing dot and NULL. */ 128 memcpy(name, id + ISAKMP_ID_DATA_OFF, id_len); 129 *(name + id_len) = '.'; 130 *(name + id_len + 1) = '\0'; 131 break; 132 133 case IPSEC_ID_USER_FQDN: 134 /* 135 * Some special handling here. We want to convert the ID 136 * 'user@host.domain' string into 'user._ipsec.host.domain.'. 137 */ 138 if ((id_len + sizeof(DNS_UFQDN_SEPARATOR)) >= sizeof name) 139 return 0; 140 /* Look for the '@' separator. */ 141 for (umark = id + ISAKMP_ID_DATA_OFF; (umark - id) < id_len; 142 umark++) 143 if (*umark == '@') 144 break; 145 if (*umark != '@') { 146 LOG_DBG((LOG_MISC, 50, "dns_get_key: bad UFQDN ID")); 147 return 0; 148 } 149 *umark++ = '\0'; 150 /* id is now terminated. 'umark', however, is not. */ 151 snprintf(name, sizeof name, "%s%s", id + ISAKMP_ID_DATA_OFF, 152 DNS_UFQDN_SEPARATOR); 153 memcpy(name + strlen(name), umark, id_len - strlen(id) - 1); 154 *(name + id_len + sizeof(DNS_UFQDN_SEPARATOR) - 2) = '.'; 155 *(name + id_len + sizeof(DNS_UFQDN_SEPARATOR) - 1) = '\0'; 156 break; 157 158 default: 159 return 0; 160 } 161 162 LOG_DBG((LOG_MISC, 50, "dns_get_key: trying KEY RR for %s", name)); 163 ret = getrrsetbyname(name, C_IN, T_KEY, 0, &rr); 164 165 if (ret) { 166 LOG_DBG((LOG_MISC, 30, "dns_get_key: no DNS responses " 167 "(error %d)", ret)); 168 return 0; 169 } 170 LOG_DBG((LOG_MISC, 80, 171 "dns_get_key: rrset class %d type %d ttl %d nrdatas %d nrsigs %d", 172 rr->rri_rdclass, rr->rri_rdtype, rr->rri_ttl, rr->rri_nrdatas, 173 rr->rri_nsigs)); 174 175 /* We don't accept unvalidated data. */ 176 if (!(rr->rri_flags & RRSET_VALIDATED)) { 177 LOG_DBG((LOG_MISC, 10, "dns_get_key: " 178 "got unvalidated response")); 179 freerrset(rr); 180 return 0; 181 } 182 /* Sanity. */ 183 if (rr->rri_nrdatas == 0 || rr->rri_rdtype != T_KEY) { 184 LOG_DBG((LOG_MISC, 30, "dns_get_key: no KEY RRs received")); 185 freerrset(rr); 186 return 0; 187 } 188 bzero(&key_rr, sizeof key_rr); 189 190 /* 191 * Find a key with the wanted algorithm, if any. 192 * XXX If there are several keys present, we currently only find the 193 * first. 194 */ 195 for (i = 0; i < rr->rri_nrdatas && key_rr.datalen == 0; i++) { 196 key_rr.flags = ntohs((u_int16_t) * rr->rri_rdatas[i].rdi_data); 197 key_rr.protocol = *(rr->rri_rdatas[i].rdi_data + 2); 198 key_rr.algorithm = *(rr->rri_rdatas[i].rdi_data + 3); 199 200 if (key_rr.protocol != DNS_KEYPROTO_IPSEC) { 201 LOG_DBG((LOG_MISC, 50, "dns_get_key: ignored " 202 "non-IPsec key")); 203 continue; 204 } 205 if (key_rr.algorithm != algorithm) { 206 LOG_DBG((LOG_MISC, 50, "dns_get_key: ignored " 207 "key with other alg")); 208 continue; 209 } 210 key_rr.datalen = rr->rri_rdatas[i].rdi_length - 4; 211 if (key_rr.datalen <= 0) { 212 LOG_DBG((LOG_MISC, 50, "dns_get_key: " 213 "ignored bad key")); 214 key_rr.datalen = 0; 215 continue; 216 } 217 /* This key seems to fit our requirements... */ 218 key_rr.data = (char *)malloc(key_rr.datalen); 219 if (!key_rr.data) { 220 log_error("dns_get_key: malloc (%d) failed", 221 key_rr.datalen); 222 freerrset(rr); 223 return 0; 224 } 225 memcpy(key_rr.data, rr->rri_rdatas[i].rdi_data + 4, 226 key_rr.datalen); 227 *keylen = key_rr.datalen; 228 } 229 230 freerrset(rr); 231 232 if (key_rr.datalen) 233 return key_rr.data; 234 return 0; 235 } 236 237 int 238 dns_RSA_dns_to_x509(u_int8_t *key, int keylen, RSA **rsa_key) 239 { 240 RSA *rsa; 241 int key_offset; 242 u_int8_t e_len; 243 244 if (!key || keylen <= 0) { 245 log_print("dns_RSA_dns_to_x509: invalid public key"); 246 return -1; 247 } 248 rsa = RSA_new(); 249 if (rsa == NULL) { 250 log_error("dns_RSA_dns_to_x509: " 251 "failed to allocate new RSA struct"); 252 return -1; 253 } 254 e_len = *key; 255 key_offset = 1; 256 257 if (e_len == 0) { 258 if (keylen < 3) { 259 log_print("dns_RSA_dns_to_x509: invalid public key"); 260 RSA_free(rsa); 261 return -1; 262 } 263 e_len = *(key + key_offset++) << 8; 264 e_len += *(key + key_offset++); 265 } 266 if (e_len > (keylen - key_offset)) { 267 log_print("dns_RSA_dns_to_x509: invalid public key"); 268 RSA_free(rsa); 269 return -1; 270 } 271 rsa->e = BN_bin2bn(key + key_offset, e_len, NULL); 272 key_offset += e_len; 273 274 /* XXX if (keylen <= key_offset) -> "invalid public key" ? */ 275 276 rsa->n = BN_bin2bn(key + key_offset, keylen - key_offset, NULL); 277 278 *rsa_key = rsa; 279 280 LOG_DBG((LOG_MISC, 30, "dns_RSA_dns_to_x509: got %d bits RSA key", 281 BN_num_bits(rsa->n))); 282 return 0; 283 } 284 285 #if notyet 286 int 287 dns_RSA_x509_to_dns(RSA *rsa_key, u_int8_t *key, int *keylen) 288 { 289 return 0; 290 } 291 #endif 292