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