1 /* $OpenBSD: dns.c,v 1.4 2003/05/14 23:29:22 jakob Exp $ */
4 * Copyright (c) 2003 Wesley Griffin. All rights reserved.
5 * Copyright (c) 2003 Jakob Schlyter. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
32 #include <openssl/bn.h>
34 #include <lwres/netdb.h>
35 #include <dns/result.h>
46 extern char *__progname;
47 RCSID("$OpenBSD: dns.c,v 1.4 2003/05/14 23:29:22 jakob Exp $");
50 static const char *errset_text[] = {
51 "success", /* 0 ERRSET_SUCCESS */
52 "out of memory", /* 1 ERRSET_NOMEMORY */
53 "general failure", /* 2 ERRSET_FAIL */
54 "invalid parameter", /* 3 ERRSET_INVAL */
55 "name does not exist", /* 4 ERRSET_NONAME */
56 "data does not exist", /* 5 ERRSET_NODATA */
60 dns_result_totext(unsigned int error)
64 return errset_text[ERRSET_SUCCESS];
66 return errset_text[ERRSET_NOMEMORY];
68 return errset_text[ERRSET_FAIL];
70 return errset_text[ERRSET_INVAL];
72 return errset_text[ERRSET_NONAME];
74 return errset_text[ERRSET_NODATA];
76 return "unknown error";
83 * Read SSHFP parameters from key buffer.
86 dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type,
87 u_char **digest, u_int *digest_len, Key *key)
93 *algorithm = SSHFP_KEY_RSA;
96 *algorithm = SSHFP_KEY_DSA;
99 *algorithm = SSHFP_KEY_RESERVED;
103 *digest_type = SSHFP_HASH_SHA1;
104 *digest = key_fingerprint_raw(key, SSH_FP_SHA1, digest_len);
107 *digest_type = SSHFP_HASH_RESERVED;
117 * Read SSHFP parameters from rdata buffer.
120 dns_read_rdata(u_int8_t *algorithm, u_int8_t *digest_type,
121 u_char **digest, u_int *digest_len, u_char *rdata, int rdata_len)
125 *algorithm = SSHFP_KEY_RESERVED;
126 *digest_type = SSHFP_HASH_RESERVED;
128 if (rdata_len >= 2) {
129 *algorithm = rdata[0];
130 *digest_type = rdata[1];
131 *digest_len = rdata_len - 2;
133 if (*digest_len > 0) {
134 *digest = (u_char *) xmalloc(*digest_len);
135 memcpy(*digest, rdata + 2, *digest_len);
148 * Verify the given hostname, address and host key using DNS.
149 * Returns 0 if key verifies or -1 if key does NOT verify
152 verify_host_key_dns(const char *hostname, struct sockaddr *address,
157 struct rrsetinfo *fingerprints = NULL;
160 u_int8_t hostkey_algorithm;
161 u_int8_t hostkey_digest_type;
162 u_char *hostkey_digest;
163 u_int hostkey_digest_len;
165 u_int8_t dnskey_algorithm;
166 u_int8_t dnskey_digest_type;
167 u_char *dnskey_digest;
168 u_int dnskey_digest_len;
171 debug3("verify_hostkey_dns");
173 fatal("No key to look up!");
175 result = getrrsetbyname(hostname, DNS_RDATACLASS_IN,
176 DNS_RDATATYPE_SSHFP, 0, &fingerprints);
178 verbose("DNS lookup error: %s", dns_result_totext(result));
179 return DNS_VERIFY_ERROR;
183 /* Only accept validated answers */
184 if (!fingerprints->rri_flags & RRSET_VALIDATED) {
185 error("Ignored unvalidated fingerprint from DNS.");
186 return DNS_VERIFY_ERROR;
190 debug("found %d fingerprints in DNS", fingerprints->rri_nrdatas);
192 /* Initialize host key parameters */
193 if (!dns_read_key(&hostkey_algorithm, &hostkey_digest_type,
194 &hostkey_digest, &hostkey_digest_len, hostkey)) {
195 error("Error calculating host key fingerprint.");
196 return DNS_VERIFY_ERROR;
199 for (counter = 0 ; counter < fingerprints->rri_nrdatas ; counter++) {
201 * Extract the key from the answer. Ignore any badly
202 * formatted fingerprints.
204 if (!dns_read_rdata(&dnskey_algorithm, &dnskey_digest_type,
205 &dnskey_digest, &dnskey_digest_len,
206 fingerprints->rri_rdatas[counter].rdi_data,
207 fingerprints->rri_rdatas[counter].rdi_length)) {
208 verbose("Error parsing fingerprint from DNS.");
212 /* Check if the current key is the same as the given key */
213 if (hostkey_algorithm == dnskey_algorithm &&
214 hostkey_digest_type == dnskey_digest_type) {
216 if (hostkey_digest_len == dnskey_digest_len &&
217 memcmp(hostkey_digest, dnskey_digest,
218 hostkey_digest_len) == 0) {
220 /* Matching algoritm and digest. */
221 freerrset(fingerprints);
223 debug("matching host key fingerprint found in DNS");
224 return DNS_VERIFY_OK;
226 logit("Matching host key fingerprint found in DNS.");
227 return DNS_VERIFY_ERROR;
230 /* Correct algorithm but bad digest */
231 debug("verify_hostkey_dns: failed");
237 freerrset(fingerprints);
240 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
241 error("@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @");
242 error("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@");
243 error("IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!");
244 error("Someone could be eavesdropping on you right now (man-in-the-middle attack)!");
245 error("It is also possible that the %s host key has just been changed.",
247 error("Please contact your system administrator.");
248 return DNS_VERIFY_FAILED;
251 debug("fingerprints found in DNS, but none of them matched");
253 return DNS_VERIFY_ERROR;
258 * Export the fingerprint of a key as a DNS resource record
261 export_dns_rr(const char *hostname, Key *key, FILE *f, int generic)
263 u_int8_t rdata_pubkey_algorithm = 0;
264 u_int8_t rdata_digest_type = SSHFP_HASH_SHA1;
265 u_char *rdata_digest;
266 u_int rdata_digest_len;
271 if (dns_read_key(&rdata_pubkey_algorithm, &rdata_digest_type,
272 &rdata_digest, &rdata_digest_len, key)) {
275 fprintf(f, "%s IN TYPE%d \\# %d %02x %02x ", hostname,
276 DNS_RDATATYPE_SSHFP, 2 + rdata_digest_len,
277 rdata_pubkey_algorithm, rdata_digest_type);
279 fprintf(f, "%s IN SSHFP %d %d ", hostname,
280 rdata_pubkey_algorithm, rdata_digest_type);
282 for (i = 0; i < rdata_digest_len; i++)
283 fprintf(f, "%02x", rdata_digest[i]);
287 error("dns_export_rr: unsupported algorithm");