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