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