1 /* $OpenBSD: dns.c,v 1.44 2023/03/10 04:06:21 dtucker 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 <stdarg.h> 33 #include <stdio.h> 34 #include <string.h> 35 #include <stdlib.h> 36 37 #include "xmalloc.h" 38 #include "sshkey.h" 39 #include "ssherr.h" 40 #include "dns.h" 41 #include "log.h" 42 #include "digest.h" 43 44 static const char * const errset_text[] = { 45 "success", /* 0 ERRSET_SUCCESS */ 46 "out of memory", /* 1 ERRSET_NOMEMORY */ 47 "general failure", /* 2 ERRSET_FAIL */ 48 "invalid parameter", /* 3 ERRSET_INVAL */ 49 "name does not exist", /* 4 ERRSET_NONAME */ 50 "data does not exist", /* 5 ERRSET_NODATA */ 51 }; 52 53 static const char * 54 dns_result_totext(unsigned int res) 55 { 56 switch (res) { 57 case ERRSET_SUCCESS: 58 return errset_text[ERRSET_SUCCESS]; 59 case ERRSET_NOMEMORY: 60 return errset_text[ERRSET_NOMEMORY]; 61 case ERRSET_FAIL: 62 return errset_text[ERRSET_FAIL]; 63 case ERRSET_INVAL: 64 return errset_text[ERRSET_INVAL]; 65 case ERRSET_NONAME: 66 return errset_text[ERRSET_NONAME]; 67 case ERRSET_NODATA: 68 return errset_text[ERRSET_NODATA]; 69 default: 70 return "unknown error"; 71 } 72 } 73 74 /* 75 * Read SSHFP parameters from key buffer. 76 * Caller must free digest which is allocated by sshkey_fingerprint_raw(). 77 */ 78 static int 79 dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type, 80 u_char **digest, size_t *digest_len, struct sshkey *key) 81 { 82 int r, success = 0; 83 int fp_alg = -1; 84 85 switch (key->type) { 86 case KEY_RSA: 87 *algorithm = SSHFP_KEY_RSA; 88 break; 89 case KEY_DSA: 90 *algorithm = SSHFP_KEY_DSA; 91 break; 92 case KEY_ECDSA: 93 *algorithm = SSHFP_KEY_ECDSA; 94 break; 95 case KEY_ED25519: 96 *algorithm = SSHFP_KEY_ED25519; 97 break; 98 case KEY_XMSS: 99 *algorithm = SSHFP_KEY_XMSS; 100 break; 101 default: 102 *algorithm = SSHFP_KEY_RESERVED; /* 0 */ 103 } 104 105 switch (*digest_type) { 106 case SSHFP_HASH_SHA1: 107 fp_alg = SSH_DIGEST_SHA1; 108 break; 109 case SSHFP_HASH_SHA256: 110 fp_alg = SSH_DIGEST_SHA256; 111 break; 112 default: 113 *digest_type = SSHFP_HASH_RESERVED; /* 0 */ 114 } 115 116 if (*algorithm && *digest_type) { 117 if ((r = sshkey_fingerprint_raw(key, fp_alg, digest, 118 digest_len)) != 0) 119 fatal_fr(r, "sshkey_fingerprint_raw"); 120 success = 1; 121 } else { 122 *digest = NULL; 123 *digest_len = 0; 124 } 125 126 return success; 127 } 128 129 /* 130 * Read SSHFP parameters from rdata buffer. 131 */ 132 static int 133 dns_read_rdata(u_int8_t *algorithm, u_int8_t *digest_type, 134 u_char **digest, size_t *digest_len, u_char *rdata, int rdata_len) 135 { 136 int success = 0; 137 138 *algorithm = SSHFP_KEY_RESERVED; 139 *digest_type = SSHFP_HASH_RESERVED; 140 141 if (rdata_len >= 2) { 142 *algorithm = rdata[0]; 143 *digest_type = rdata[1]; 144 *digest_len = rdata_len - 2; 145 146 if (*digest_len > 0) { 147 *digest = xmalloc(*digest_len); 148 memcpy(*digest, rdata + 2, *digest_len); 149 } else { 150 *digest = (u_char *)xstrdup(""); 151 } 152 153 success = 1; 154 } 155 156 return success; 157 } 158 159 /* 160 * Check if hostname is numerical. 161 * Returns -1 if hostname is numeric, 0 otherwise 162 */ 163 static int 164 is_numeric_hostname(const char *hostname) 165 { 166 struct addrinfo hints, *ai; 167 168 /* 169 * We shouldn't ever get a null host but if we do then log an error 170 * and return -1 which stops DNS key fingerprint processing. 171 */ 172 if (hostname == NULL) { 173 error("is_numeric_hostname called with NULL hostname"); 174 return -1; 175 } 176 177 memset(&hints, 0, sizeof(hints)); 178 hints.ai_socktype = SOCK_DGRAM; 179 hints.ai_flags = AI_NUMERICHOST; 180 181 if (getaddrinfo(hostname, NULL, &hints, &ai) == 0) { 182 freeaddrinfo(ai); 183 return -1; 184 } 185 186 return 0; 187 } 188 189 /* 190 * Verify the given hostname, address and host key using DNS. 191 * Returns 0 if lookup succeeds, -1 otherwise 192 */ 193 int 194 verify_host_key_dns(const char *hostname, struct sockaddr *address, 195 struct sshkey *hostkey, int *flags) 196 { 197 u_int counter; 198 int result; 199 struct rrsetinfo *fingerprints = NULL; 200 201 u_int8_t hostkey_algorithm; 202 u_char *hostkey_digest; 203 size_t hostkey_digest_len; 204 205 u_int8_t dnskey_algorithm; 206 u_int8_t dnskey_digest_type; 207 u_char *dnskey_digest; 208 size_t dnskey_digest_len; 209 210 *flags = 0; 211 212 debug3("verify_host_key_dns"); 213 if (hostkey == NULL) 214 fatal("No key to look up!"); 215 216 if (is_numeric_hostname(hostname)) { 217 debug("skipped DNS lookup for numerical hostname"); 218 return -1; 219 } 220 221 result = getrrsetbyname(hostname, DNS_RDATACLASS_IN, 222 DNS_RDATATYPE_SSHFP, 0, &fingerprints); 223 if (result) { 224 verbose("DNS lookup error: %s", dns_result_totext(result)); 225 return -1; 226 } 227 228 if (fingerprints->rri_flags & RRSET_VALIDATED) { 229 *flags |= DNS_VERIFY_SECURE; 230 debug("found %d secure fingerprints in DNS", 231 fingerprints->rri_nrdatas); 232 } else { 233 debug("found %d insecure fingerprints in DNS", 234 fingerprints->rri_nrdatas); 235 } 236 237 if (fingerprints->rri_nrdatas) 238 *flags |= DNS_VERIFY_FOUND; 239 240 for (counter = 0; counter < fingerprints->rri_nrdatas; counter++) { 241 /* 242 * Extract the key from the answer. Ignore any badly 243 * formatted fingerprints. 244 */ 245 if (!dns_read_rdata(&dnskey_algorithm, &dnskey_digest_type, 246 &dnskey_digest, &dnskey_digest_len, 247 fingerprints->rri_rdatas[counter].rdi_data, 248 fingerprints->rri_rdatas[counter].rdi_length)) { 249 verbose("Error parsing fingerprint from DNS."); 250 continue; 251 } 252 debug3_f("checking SSHFP type %d fptype %d", dnskey_algorithm, 253 dnskey_digest_type); 254 255 /* Calculate host key fingerprint. */ 256 if (!dns_read_key(&hostkey_algorithm, &dnskey_digest_type, 257 &hostkey_digest, &hostkey_digest_len, hostkey)) { 258 error("Error calculating key fingerprint."); 259 free(dnskey_digest); 260 freerrset(fingerprints); 261 return -1; 262 } 263 264 /* Check if the current key is the same as the given key */ 265 if (hostkey_algorithm == dnskey_algorithm && 266 hostkey_digest_len == dnskey_digest_len) { 267 if (timingsafe_bcmp(hostkey_digest, dnskey_digest, 268 hostkey_digest_len) == 0) { 269 debug_f("matched SSHFP type %d fptype %d", 270 dnskey_algorithm, dnskey_digest_type); 271 *flags |= DNS_VERIFY_MATCH; 272 } else { 273 debug_f("failed SSHFP type %d fptype %d", 274 dnskey_algorithm, dnskey_digest_type); 275 *flags |= DNS_VERIFY_FAILED; 276 } 277 } 278 free(dnskey_digest); 279 free(hostkey_digest); /* from sshkey_fingerprint_raw() */ 280 } 281 282 freerrset(fingerprints); 283 284 /* If any fingerprint failed to validate, return failure. */ 285 if (*flags & DNS_VERIFY_FAILED) 286 *flags &= ~DNS_VERIFY_MATCH; 287 288 if (*flags & DNS_VERIFY_FOUND) 289 if (*flags & DNS_VERIFY_MATCH) 290 debug("matching host key fingerprint found in DNS"); 291 else 292 debug("mismatching host key fingerprint found in DNS"); 293 else 294 debug("no host key fingerprint found in DNS"); 295 296 return 0; 297 } 298 299 /* 300 * Export the fingerprint of a key as a DNS resource record 301 */ 302 int 303 export_dns_rr(const char *hostname, struct sshkey *key, FILE *f, int generic, 304 int alg) 305 { 306 u_int8_t rdata_pubkey_algorithm = 0; 307 u_int8_t rdata_digest_type = SSHFP_HASH_RESERVED; 308 u_int8_t dtype; 309 u_char *rdata_digest; 310 size_t i, rdata_digest_len; 311 int success = 0; 312 313 for (dtype = SSHFP_HASH_SHA1; dtype < SSHFP_HASH_MAX; dtype++) { 314 if (alg != -1 && dtype != alg) 315 continue; 316 rdata_digest_type = dtype; 317 if (dns_read_key(&rdata_pubkey_algorithm, &rdata_digest_type, 318 &rdata_digest, &rdata_digest_len, key)) { 319 if (generic) { 320 fprintf(f, "%s IN TYPE%d \\# %zu %02x %02x ", 321 hostname, DNS_RDATATYPE_SSHFP, 322 2 + rdata_digest_len, 323 rdata_pubkey_algorithm, rdata_digest_type); 324 } else { 325 fprintf(f, "%s IN SSHFP %d %d ", hostname, 326 rdata_pubkey_algorithm, rdata_digest_type); 327 } 328 for (i = 0; i < rdata_digest_len; i++) 329 fprintf(f, "%02x", rdata_digest[i]); 330 fprintf(f, "\n"); 331 free(rdata_digest); /* from sshkey_fingerprint_raw() */ 332 success = 1; 333 } 334 } 335 336 /* No SSHFP record was generated at all */ 337 if (success == 0) { 338 error_f("unsupported algorithm and/or digest_type"); 339 } 340 341 return success; 342 } 343