1 /* $OpenBSD: dns.c,v 1.30 2014/04/20 09:24:26 logan Exp $ */ 2 3 /* 4 * Copyright (c) 2003 Wesley Griffin. All rights reserved. 5 * Copyright (c) 2003 Jakob Schlyter. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include <sys/types.h> 29 #include <sys/socket.h> 30 31 #include <netdb.h> 32 #include <stdio.h> 33 #include <string.h> 34 35 #include "xmalloc.h" 36 #include "key.h" 37 #include "dns.h" 38 #include "log.h" 39 40 static const char *errset_text[] = { 41 "success", /* 0 ERRSET_SUCCESS */ 42 "out of memory", /* 1 ERRSET_NOMEMORY */ 43 "general failure", /* 2 ERRSET_FAIL */ 44 "invalid parameter", /* 3 ERRSET_INVAL */ 45 "name does not exist", /* 4 ERRSET_NONAME */ 46 "data does not exist", /* 5 ERRSET_NODATA */ 47 }; 48 49 static const char * 50 dns_result_totext(unsigned int res) 51 { 52 switch (res) { 53 case ERRSET_SUCCESS: 54 return errset_text[ERRSET_SUCCESS]; 55 case ERRSET_NOMEMORY: 56 return errset_text[ERRSET_NOMEMORY]; 57 case ERRSET_FAIL: 58 return errset_text[ERRSET_FAIL]; 59 case ERRSET_INVAL: 60 return errset_text[ERRSET_INVAL]; 61 case ERRSET_NONAME: 62 return errset_text[ERRSET_NONAME]; 63 case ERRSET_NODATA: 64 return errset_text[ERRSET_NODATA]; 65 default: 66 return "unknown error"; 67 } 68 } 69 70 /* 71 * Read SSHFP parameters from key buffer. 72 */ 73 static int 74 dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type, 75 u_char **digest, u_int *digest_len, Key *key) 76 { 77 int success = 0; 78 enum fp_type fp_type = 0; 79 80 switch (key->type) { 81 case KEY_RSA: 82 *algorithm = SSHFP_KEY_RSA; 83 if (!*digest_type) 84 *digest_type = SSHFP_HASH_SHA1; 85 break; 86 case KEY_DSA: 87 *algorithm = SSHFP_KEY_DSA; 88 if (!*digest_type) 89 *digest_type = SSHFP_HASH_SHA1; 90 break; 91 case KEY_ECDSA: 92 *algorithm = SSHFP_KEY_ECDSA; 93 if (!*digest_type) 94 *digest_type = SSHFP_HASH_SHA256; 95 break; 96 case KEY_ED25519: 97 *algorithm = SSHFP_KEY_ED25519; 98 if (!*digest_type) 99 *digest_type = SSHFP_HASH_SHA256; 100 break; 101 default: 102 *algorithm = SSHFP_KEY_RESERVED; /* 0 */ 103 *digest_type = SSHFP_HASH_RESERVED; /* 0 */ 104 } 105 106 switch (*digest_type) { 107 case SSHFP_HASH_SHA1: 108 fp_type = SSH_FP_SHA1; 109 break; 110 case SSHFP_HASH_SHA256: 111 fp_type = SSH_FP_SHA256; 112 break; 113 default: 114 *digest_type = SSHFP_HASH_RESERVED; /* 0 */ 115 } 116 117 if (*algorithm && *digest_type) { 118 *digest = key_fingerprint_raw(key, fp_type, digest_len); 119 if (*digest == NULL) 120 fatal("dns_read_key: null from key_fingerprint_raw()"); 121 success = 1; 122 } else { 123 *digest = NULL; 124 *digest_len = 0; 125 success = 0; 126 } 127 128 return success; 129 } 130 131 /* 132 * Read SSHFP parameters from rdata buffer. 133 */ 134 static int 135 dns_read_rdata(u_int8_t *algorithm, u_int8_t *digest_type, 136 u_char **digest, u_int *digest_len, u_char *rdata, int rdata_len) 137 { 138 int success = 0; 139 140 *algorithm = SSHFP_KEY_RESERVED; 141 *digest_type = SSHFP_HASH_RESERVED; 142 143 if (rdata_len >= 2) { 144 *algorithm = rdata[0]; 145 *digest_type = rdata[1]; 146 *digest_len = rdata_len - 2; 147 148 if (*digest_len > 0) { 149 *digest = (u_char *) xmalloc(*digest_len); 150 memcpy(*digest, rdata + 2, *digest_len); 151 } else { 152 *digest = (u_char *)xstrdup(""); 153 } 154 155 success = 1; 156 } 157 158 return success; 159 } 160 161 /* 162 * Check if hostname is numerical. 163 * Returns -1 if hostname is numeric, 0 otherwise 164 */ 165 static int 166 is_numeric_hostname(const char *hostname) 167 { 168 struct addrinfo hints, *ai; 169 170 /* 171 * We shouldn't ever get a null host but if we do then log an error 172 * and return -1 which stops DNS key fingerprint processing. 173 */ 174 if (hostname == NULL) { 175 error("is_numeric_hostname called with NULL hostname"); 176 return -1; 177 } 178 179 memset(&hints, 0, sizeof(hints)); 180 hints.ai_socktype = SOCK_DGRAM; 181 hints.ai_flags = AI_NUMERICHOST; 182 183 if (getaddrinfo(hostname, NULL, &hints, &ai) == 0) { 184 freeaddrinfo(ai); 185 return -1; 186 } 187 188 return 0; 189 } 190 191 /* 192 * Verify the given hostname, address and host key using DNS. 193 * Returns 0 if lookup succeeds, -1 otherwise 194 */ 195 int 196 verify_host_key_dns(const char *hostname, struct sockaddr *address, 197 Key *hostkey, int *flags) 198 { 199 u_int counter; 200 int result; 201 struct rrsetinfo *fingerprints = NULL; 202 203 u_int8_t hostkey_algorithm; 204 u_int8_t hostkey_digest_type = SSHFP_HASH_RESERVED; 205 u_char *hostkey_digest; 206 u_int hostkey_digest_len; 207 208 u_int8_t dnskey_algorithm; 209 u_int8_t dnskey_digest_type; 210 u_char *dnskey_digest; 211 u_int dnskey_digest_len; 212 213 *flags = 0; 214 215 debug3("verify_host_key_dns"); 216 if (hostkey == NULL) 217 fatal("No key to look up!"); 218 219 if (is_numeric_hostname(hostname)) { 220 debug("skipped DNS lookup for numerical hostname"); 221 return -1; 222 } 223 224 result = getrrsetbyname(hostname, DNS_RDATACLASS_IN, 225 DNS_RDATATYPE_SSHFP, 0, &fingerprints); 226 if (result) { 227 verbose("DNS lookup error: %s", dns_result_totext(result)); 228 return -1; 229 } 230 231 if (fingerprints->rri_flags & RRSET_VALIDATED) { 232 *flags |= DNS_VERIFY_SECURE; 233 debug("found %d secure fingerprints in DNS", 234 fingerprints->rri_nrdatas); 235 } else { 236 debug("found %d insecure fingerprints in DNS", 237 fingerprints->rri_nrdatas); 238 } 239 240 /* Initialize default host key parameters */ 241 if (!dns_read_key(&hostkey_algorithm, &hostkey_digest_type, 242 &hostkey_digest, &hostkey_digest_len, hostkey)) { 243 error("Error calculating host key fingerprint."); 244 freerrset(fingerprints); 245 return -1; 246 } 247 248 if (fingerprints->rri_nrdatas) 249 *flags |= DNS_VERIFY_FOUND; 250 251 for (counter = 0; counter < fingerprints->rri_nrdatas; counter++) { 252 /* 253 * Extract the key from the answer. Ignore any badly 254 * formatted fingerprints. 255 */ 256 if (!dns_read_rdata(&dnskey_algorithm, &dnskey_digest_type, 257 &dnskey_digest, &dnskey_digest_len, 258 fingerprints->rri_rdatas[counter].rdi_data, 259 fingerprints->rri_rdatas[counter].rdi_length)) { 260 verbose("Error parsing fingerprint from DNS."); 261 continue; 262 } 263 264 if (hostkey_digest_type != dnskey_digest_type) { 265 hostkey_digest_type = dnskey_digest_type; 266 free(hostkey_digest); 267 268 /* Initialize host key parameters */ 269 if (!dns_read_key(&hostkey_algorithm, 270 &hostkey_digest_type, &hostkey_digest, 271 &hostkey_digest_len, hostkey)) { 272 error("Error calculating key fingerprint."); 273 freerrset(fingerprints); 274 return -1; 275 } 276 } 277 278 /* Check if the current key is the same as the given key */ 279 if (hostkey_algorithm == dnskey_algorithm && 280 hostkey_digest_type == dnskey_digest_type) { 281 if (hostkey_digest_len == dnskey_digest_len && 282 timingsafe_bcmp(hostkey_digest, dnskey_digest, 283 hostkey_digest_len) == 0) 284 *flags |= DNS_VERIFY_MATCH; 285 } 286 free(dnskey_digest); 287 } 288 289 free(hostkey_digest); /* from key_fingerprint_raw() */ 290 freerrset(fingerprints); 291 292 if (*flags & DNS_VERIFY_FOUND) 293 if (*flags & DNS_VERIFY_MATCH) 294 debug("matching host key fingerprint found in DNS"); 295 else 296 debug("mismatching host key fingerprint found in DNS"); 297 else 298 debug("no host key fingerprint found in DNS"); 299 300 return 0; 301 } 302 303 /* 304 * Export the fingerprint of a key as a DNS resource record 305 */ 306 int 307 export_dns_rr(const char *hostname, Key *key, FILE *f, int generic) 308 { 309 u_int8_t rdata_pubkey_algorithm = 0; 310 u_int8_t rdata_digest_type = SSHFP_HASH_RESERVED; 311 u_int8_t dtype; 312 u_char *rdata_digest; 313 u_int i, rdata_digest_len; 314 int success = 0; 315 316 for (dtype = SSHFP_HASH_SHA1; dtype < SSHFP_HASH_MAX; dtype++) { 317 rdata_digest_type = dtype; 318 if (dns_read_key(&rdata_pubkey_algorithm, &rdata_digest_type, 319 &rdata_digest, &rdata_digest_len, key)) { 320 if (generic) { 321 fprintf(f, "%s IN TYPE%d \\# %d %02x %02x ", 322 hostname, DNS_RDATATYPE_SSHFP, 323 2 + rdata_digest_len, 324 rdata_pubkey_algorithm, rdata_digest_type); 325 } else { 326 fprintf(f, "%s IN SSHFP %d %d ", hostname, 327 rdata_pubkey_algorithm, rdata_digest_type); 328 } 329 for (i = 0; i < rdata_digest_len; i++) 330 fprintf(f, "%02x", rdata_digest[i]); 331 fprintf(f, "\n"); 332 free(rdata_digest); /* from key_fingerprint_raw() */ 333 success = 1; 334 } 335 } 336 337 /* No SSHFP record was generated at all */ 338 if (success == 0) { 339 error("%s: unsupported algorithm and/or digest_type", __func__); 340 } 341 342 return success; 343 } 344