1*4d3fc8b0SEd Maste /* $OpenBSD: dns.c,v 1.44 2023/03/10 04:06:21 dtucker Exp $ */
2d95e11bfSDag-Erling Smørgrav
3d95e11bfSDag-Erling Smørgrav /*
4d95e11bfSDag-Erling Smørgrav * Copyright (c) 2003 Wesley Griffin. All rights reserved.
5d95e11bfSDag-Erling Smørgrav * Copyright (c) 2003 Jakob Schlyter. All rights reserved.
6d95e11bfSDag-Erling Smørgrav *
7d95e11bfSDag-Erling Smørgrav * Redistribution and use in source and binary forms, with or without
8d95e11bfSDag-Erling Smørgrav * modification, are permitted provided that the following conditions
9d95e11bfSDag-Erling Smørgrav * are met:
10d95e11bfSDag-Erling Smørgrav * 1. Redistributions of source code must retain the above copyright
11d95e11bfSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer.
12d95e11bfSDag-Erling Smørgrav * 2. Redistributions in binary form must reproduce the above copyright
13d95e11bfSDag-Erling Smørgrav * notice, this list of conditions and the following disclaimer in the
14d95e11bfSDag-Erling Smørgrav * documentation and/or other materials provided with the distribution.
15d95e11bfSDag-Erling Smørgrav *
16d95e11bfSDag-Erling Smørgrav * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17d95e11bfSDag-Erling Smørgrav * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18d95e11bfSDag-Erling Smørgrav * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19d95e11bfSDag-Erling Smørgrav * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20d95e11bfSDag-Erling Smørgrav * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21d95e11bfSDag-Erling Smørgrav * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22d95e11bfSDag-Erling Smørgrav * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23d95e11bfSDag-Erling Smørgrav * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24d95e11bfSDag-Erling Smørgrav * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25d95e11bfSDag-Erling Smørgrav * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26d95e11bfSDag-Erling Smørgrav */
27d95e11bfSDag-Erling Smørgrav
28d95e11bfSDag-Erling Smørgrav #include "includes.h"
29761efaa7SDag-Erling Smørgrav
30761efaa7SDag-Erling Smørgrav #include <sys/types.h>
31761efaa7SDag-Erling Smørgrav #include <sys/socket.h>
32d95e11bfSDag-Erling Smørgrav
33d95e11bfSDag-Erling Smørgrav #include <netdb.h>
34761efaa7SDag-Erling Smørgrav #include <stdarg.h>
35761efaa7SDag-Erling Smørgrav #include <stdio.h>
36761efaa7SDag-Erling Smørgrav #include <string.h>
37a0ee8cc6SDag-Erling Smørgrav #include <stdlib.h>
38d95e11bfSDag-Erling Smørgrav
39d95e11bfSDag-Erling Smørgrav #include "xmalloc.h"
40bc5531deSDag-Erling Smørgrav #include "sshkey.h"
41bc5531deSDag-Erling Smørgrav #include "ssherr.h"
42d95e11bfSDag-Erling Smørgrav #include "dns.h"
43d95e11bfSDag-Erling Smørgrav #include "log.h"
44bc5531deSDag-Erling Smørgrav #include "digest.h"
45d95e11bfSDag-Erling Smørgrav
461323ec57SEd Maste static const char * const errset_text[] = {
47d95e11bfSDag-Erling Smørgrav "success", /* 0 ERRSET_SUCCESS */
48d95e11bfSDag-Erling Smørgrav "out of memory", /* 1 ERRSET_NOMEMORY */
49d95e11bfSDag-Erling Smørgrav "general failure", /* 2 ERRSET_FAIL */
50d95e11bfSDag-Erling Smørgrav "invalid parameter", /* 3 ERRSET_INVAL */
51d95e11bfSDag-Erling Smørgrav "name does not exist", /* 4 ERRSET_NONAME */
52d95e11bfSDag-Erling Smørgrav "data does not exist", /* 5 ERRSET_NODATA */
53d95e11bfSDag-Erling Smørgrav };
54d95e11bfSDag-Erling Smørgrav
55d95e11bfSDag-Erling Smørgrav static const char *
dns_result_totext(unsigned int res)56d74d50a8SDag-Erling Smørgrav dns_result_totext(unsigned int res)
57d95e11bfSDag-Erling Smørgrav {
58d74d50a8SDag-Erling Smørgrav switch (res) {
59d95e11bfSDag-Erling Smørgrav case ERRSET_SUCCESS:
60d95e11bfSDag-Erling Smørgrav return errset_text[ERRSET_SUCCESS];
61d95e11bfSDag-Erling Smørgrav case ERRSET_NOMEMORY:
62d95e11bfSDag-Erling Smørgrav return errset_text[ERRSET_NOMEMORY];
63d95e11bfSDag-Erling Smørgrav case ERRSET_FAIL:
64d95e11bfSDag-Erling Smørgrav return errset_text[ERRSET_FAIL];
65d95e11bfSDag-Erling Smørgrav case ERRSET_INVAL:
66d95e11bfSDag-Erling Smørgrav return errset_text[ERRSET_INVAL];
67d95e11bfSDag-Erling Smørgrav case ERRSET_NONAME:
68d95e11bfSDag-Erling Smørgrav return errset_text[ERRSET_NONAME];
69d95e11bfSDag-Erling Smørgrav case ERRSET_NODATA:
70d95e11bfSDag-Erling Smørgrav return errset_text[ERRSET_NODATA];
71d95e11bfSDag-Erling Smørgrav default:
72d95e11bfSDag-Erling Smørgrav return "unknown error";
73d95e11bfSDag-Erling Smørgrav }
74d95e11bfSDag-Erling Smørgrav }
75d95e11bfSDag-Erling Smørgrav
76d95e11bfSDag-Erling Smørgrav /*
77d95e11bfSDag-Erling Smørgrav * Read SSHFP parameters from key buffer.
7819261079SEd Maste * Caller must free digest which is allocated by sshkey_fingerprint_raw().
79d95e11bfSDag-Erling Smørgrav */
80d95e11bfSDag-Erling Smørgrav static int
dns_read_key(u_int8_t * algorithm,u_int8_t * digest_type,u_char ** digest,size_t * digest_len,struct sshkey * key)81d95e11bfSDag-Erling Smørgrav dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type,
82bc5531deSDag-Erling Smørgrav u_char **digest, size_t *digest_len, struct sshkey *key)
83d95e11bfSDag-Erling Smørgrav {
84bc5531deSDag-Erling Smørgrav int r, success = 0;
85bc5531deSDag-Erling Smørgrav int fp_alg = -1;
86d95e11bfSDag-Erling Smørgrav
87d95e11bfSDag-Erling Smørgrav switch (key->type) {
88d95e11bfSDag-Erling Smørgrav case KEY_RSA:
89d95e11bfSDag-Erling Smørgrav *algorithm = SSHFP_KEY_RSA;
90d95e11bfSDag-Erling Smørgrav break;
91d95e11bfSDag-Erling Smørgrav case KEY_DSA:
92d95e11bfSDag-Erling Smørgrav *algorithm = SSHFP_KEY_DSA;
93d95e11bfSDag-Erling Smørgrav break;
94462c32cbSDag-Erling Smørgrav case KEY_ECDSA:
95462c32cbSDag-Erling Smørgrav *algorithm = SSHFP_KEY_ECDSA;
96462c32cbSDag-Erling Smørgrav break;
97a0ee8cc6SDag-Erling Smørgrav case KEY_ED25519:
98a0ee8cc6SDag-Erling Smørgrav *algorithm = SSHFP_KEY_ED25519;
99a0ee8cc6SDag-Erling Smørgrav break;
10047dd1d1bSDag-Erling Smørgrav case KEY_XMSS:
10147dd1d1bSDag-Erling Smørgrav *algorithm = SSHFP_KEY_XMSS;
10247dd1d1bSDag-Erling Smørgrav break;
103d95e11bfSDag-Erling Smørgrav default:
104021d409fSDag-Erling Smørgrav *algorithm = SSHFP_KEY_RESERVED; /* 0 */
105d95e11bfSDag-Erling Smørgrav }
106d95e11bfSDag-Erling Smørgrav
107462c32cbSDag-Erling Smørgrav switch (*digest_type) {
108462c32cbSDag-Erling Smørgrav case SSHFP_HASH_SHA1:
109bc5531deSDag-Erling Smørgrav fp_alg = SSH_DIGEST_SHA1;
110462c32cbSDag-Erling Smørgrav break;
111462c32cbSDag-Erling Smørgrav case SSHFP_HASH_SHA256:
112bc5531deSDag-Erling Smørgrav fp_alg = SSH_DIGEST_SHA256;
113462c32cbSDag-Erling Smørgrav break;
114462c32cbSDag-Erling Smørgrav default:
115462c32cbSDag-Erling Smørgrav *digest_type = SSHFP_HASH_RESERVED; /* 0 */
116462c32cbSDag-Erling Smørgrav }
117462c32cbSDag-Erling Smørgrav
118462c32cbSDag-Erling Smørgrav if (*algorithm && *digest_type) {
119bc5531deSDag-Erling Smørgrav if ((r = sshkey_fingerprint_raw(key, fp_alg, digest,
120bc5531deSDag-Erling Smørgrav digest_len)) != 0)
12119261079SEd Maste fatal_fr(r, "sshkey_fingerprint_raw");
122d95e11bfSDag-Erling Smørgrav success = 1;
123d95e11bfSDag-Erling Smørgrav } else {
124d95e11bfSDag-Erling Smørgrav *digest = NULL;
125d95e11bfSDag-Erling Smørgrav *digest_len = 0;
126d95e11bfSDag-Erling Smørgrav }
127d95e11bfSDag-Erling Smørgrav
128d95e11bfSDag-Erling Smørgrav return success;
129d95e11bfSDag-Erling Smørgrav }
130d95e11bfSDag-Erling Smørgrav
131d95e11bfSDag-Erling Smørgrav /*
132d95e11bfSDag-Erling Smørgrav * Read SSHFP parameters from rdata buffer.
133d95e11bfSDag-Erling Smørgrav */
134d95e11bfSDag-Erling Smørgrav static int
dns_read_rdata(u_int8_t * algorithm,u_int8_t * digest_type,u_char ** digest,size_t * digest_len,u_char * rdata,int rdata_len)135d95e11bfSDag-Erling Smørgrav dns_read_rdata(u_int8_t *algorithm, u_int8_t *digest_type,
136bc5531deSDag-Erling Smørgrav u_char **digest, size_t *digest_len, u_char *rdata, int rdata_len)
137d95e11bfSDag-Erling Smørgrav {
138d95e11bfSDag-Erling Smørgrav int success = 0;
139d95e11bfSDag-Erling Smørgrav
140d95e11bfSDag-Erling Smørgrav *algorithm = SSHFP_KEY_RESERVED;
141d95e11bfSDag-Erling Smørgrav *digest_type = SSHFP_HASH_RESERVED;
142d95e11bfSDag-Erling Smørgrav
143d95e11bfSDag-Erling Smørgrav if (rdata_len >= 2) {
144d95e11bfSDag-Erling Smørgrav *algorithm = rdata[0];
145d95e11bfSDag-Erling Smørgrav *digest_type = rdata[1];
146d95e11bfSDag-Erling Smørgrav *digest_len = rdata_len - 2;
147d95e11bfSDag-Erling Smørgrav
148d95e11bfSDag-Erling Smørgrav if (*digest_len > 0) {
149fc1ba28aSDag-Erling Smørgrav *digest = xmalloc(*digest_len);
150d95e11bfSDag-Erling Smørgrav memcpy(*digest, rdata + 2, *digest_len);
151d95e11bfSDag-Erling Smørgrav } else {
152761efaa7SDag-Erling Smørgrav *digest = (u_char *)xstrdup("");
153d95e11bfSDag-Erling Smørgrav }
154d95e11bfSDag-Erling Smørgrav
155d95e11bfSDag-Erling Smørgrav success = 1;
156d95e11bfSDag-Erling Smørgrav }
157d95e11bfSDag-Erling Smørgrav
158d95e11bfSDag-Erling Smørgrav return success;
159d95e11bfSDag-Erling Smørgrav }
160d95e11bfSDag-Erling Smørgrav
161043840dfSDag-Erling Smørgrav /*
162043840dfSDag-Erling Smørgrav * Check if hostname is numerical.
163043840dfSDag-Erling Smørgrav * Returns -1 if hostname is numeric, 0 otherwise
164043840dfSDag-Erling Smørgrav */
165043840dfSDag-Erling Smørgrav static int
is_numeric_hostname(const char * hostname)166043840dfSDag-Erling Smørgrav is_numeric_hostname(const char *hostname)
167043840dfSDag-Erling Smørgrav {
168043840dfSDag-Erling Smørgrav struct addrinfo hints, *ai;
169043840dfSDag-Erling Smørgrav
170d4af9e69SDag-Erling Smørgrav /*
171d4af9e69SDag-Erling Smørgrav * We shouldn't ever get a null host but if we do then log an error
172d4af9e69SDag-Erling Smørgrav * and return -1 which stops DNS key fingerprint processing.
173d4af9e69SDag-Erling Smørgrav */
174d4af9e69SDag-Erling Smørgrav if (hostname == NULL) {
175d4af9e69SDag-Erling Smørgrav error("is_numeric_hostname called with NULL hostname");
176d4af9e69SDag-Erling Smørgrav return -1;
177d4af9e69SDag-Erling Smørgrav }
178d4af9e69SDag-Erling Smørgrav
179043840dfSDag-Erling Smørgrav memset(&hints, 0, sizeof(hints));
180043840dfSDag-Erling Smørgrav hints.ai_socktype = SOCK_DGRAM;
181043840dfSDag-Erling Smørgrav hints.ai_flags = AI_NUMERICHOST;
182043840dfSDag-Erling Smørgrav
183d4af9e69SDag-Erling Smørgrav if (getaddrinfo(hostname, NULL, &hints, &ai) == 0) {
184043840dfSDag-Erling Smørgrav freeaddrinfo(ai);
185043840dfSDag-Erling Smørgrav return -1;
186043840dfSDag-Erling Smørgrav }
187043840dfSDag-Erling Smørgrav
188043840dfSDag-Erling Smørgrav return 0;
189043840dfSDag-Erling Smørgrav }
190d95e11bfSDag-Erling Smørgrav
191d95e11bfSDag-Erling Smørgrav /*
192d95e11bfSDag-Erling Smørgrav * Verify the given hostname, address and host key using DNS.
193efcad6b7SDag-Erling Smørgrav * Returns 0 if lookup succeeds, -1 otherwise
194d95e11bfSDag-Erling Smørgrav */
195d95e11bfSDag-Erling Smørgrav int
verify_host_key_dns(const char * hostname,struct sockaddr * address,struct sshkey * hostkey,int * flags)196d95e11bfSDag-Erling Smørgrav verify_host_key_dns(const char *hostname, struct sockaddr *address,
197bc5531deSDag-Erling Smørgrav struct sshkey *hostkey, int *flags)
198d95e11bfSDag-Erling Smørgrav {
199043840dfSDag-Erling Smørgrav u_int counter;
200d95e11bfSDag-Erling Smørgrav int result;
201d95e11bfSDag-Erling Smørgrav struct rrsetinfo *fingerprints = NULL;
202d95e11bfSDag-Erling Smørgrav
203d95e11bfSDag-Erling Smørgrav u_int8_t hostkey_algorithm;
204d95e11bfSDag-Erling Smørgrav u_char *hostkey_digest;
205bc5531deSDag-Erling Smørgrav size_t hostkey_digest_len;
206d95e11bfSDag-Erling Smørgrav
207d95e11bfSDag-Erling Smørgrav u_int8_t dnskey_algorithm;
208d95e11bfSDag-Erling Smørgrav u_int8_t dnskey_digest_type;
209d95e11bfSDag-Erling Smørgrav u_char *dnskey_digest;
210bc5531deSDag-Erling Smørgrav size_t dnskey_digest_len;
211d95e11bfSDag-Erling Smørgrav
212efcad6b7SDag-Erling Smørgrav *flags = 0;
213d95e11bfSDag-Erling Smørgrav
214021d409fSDag-Erling Smørgrav debug3("verify_host_key_dns");
215d95e11bfSDag-Erling Smørgrav if (hostkey == NULL)
216d95e11bfSDag-Erling Smørgrav fatal("No key to look up!");
217d95e11bfSDag-Erling Smørgrav
218043840dfSDag-Erling Smørgrav if (is_numeric_hostname(hostname)) {
219043840dfSDag-Erling Smørgrav debug("skipped DNS lookup for numerical hostname");
220043840dfSDag-Erling Smørgrav return -1;
221043840dfSDag-Erling Smørgrav }
222043840dfSDag-Erling Smørgrav
223d95e11bfSDag-Erling Smørgrav result = getrrsetbyname(hostname, DNS_RDATACLASS_IN,
224d95e11bfSDag-Erling Smørgrav DNS_RDATATYPE_SSHFP, 0, &fingerprints);
225d95e11bfSDag-Erling Smørgrav if (result) {
226d95e11bfSDag-Erling Smørgrav verbose("DNS lookup error: %s", dns_result_totext(result));
227efcad6b7SDag-Erling Smørgrav return -1;
228d95e11bfSDag-Erling Smørgrav }
229d95e11bfSDag-Erling Smørgrav
230efcad6b7SDag-Erling Smørgrav if (fingerprints->rri_flags & RRSET_VALIDATED) {
231efcad6b7SDag-Erling Smørgrav *flags |= DNS_VERIFY_SECURE;
232efcad6b7SDag-Erling Smørgrav debug("found %d secure fingerprints in DNS",
233efcad6b7SDag-Erling Smørgrav fingerprints->rri_nrdatas);
234efcad6b7SDag-Erling Smørgrav } else {
235efcad6b7SDag-Erling Smørgrav debug("found %d insecure fingerprints in DNS",
236efcad6b7SDag-Erling Smørgrav fingerprints->rri_nrdatas);
237d95e11bfSDag-Erling Smørgrav }
238d95e11bfSDag-Erling Smørgrav
239efcad6b7SDag-Erling Smørgrav if (fingerprints->rri_nrdatas)
240efcad6b7SDag-Erling Smørgrav *flags |= DNS_VERIFY_FOUND;
241efcad6b7SDag-Erling Smørgrav
242d95e11bfSDag-Erling Smørgrav for (counter = 0; counter < fingerprints->rri_nrdatas; counter++) {
243d95e11bfSDag-Erling Smørgrav /*
244d95e11bfSDag-Erling Smørgrav * Extract the key from the answer. Ignore any badly
245d95e11bfSDag-Erling Smørgrav * formatted fingerprints.
246d95e11bfSDag-Erling Smørgrav */
247d95e11bfSDag-Erling Smørgrav if (!dns_read_rdata(&dnskey_algorithm, &dnskey_digest_type,
248d95e11bfSDag-Erling Smørgrav &dnskey_digest, &dnskey_digest_len,
249d95e11bfSDag-Erling Smørgrav fingerprints->rri_rdatas[counter].rdi_data,
250d95e11bfSDag-Erling Smørgrav fingerprints->rri_rdatas[counter].rdi_length)) {
251d95e11bfSDag-Erling Smørgrav verbose("Error parsing fingerprint from DNS.");
252d95e11bfSDag-Erling Smørgrav continue;
253d95e11bfSDag-Erling Smørgrav }
25419261079SEd Maste debug3_f("checking SSHFP type %d fptype %d", dnskey_algorithm,
25519261079SEd Maste dnskey_digest_type);
256d95e11bfSDag-Erling Smørgrav
25719261079SEd Maste /* Calculate host key fingerprint. */
25819261079SEd Maste if (!dns_read_key(&hostkey_algorithm, &dnskey_digest_type,
25919261079SEd Maste &hostkey_digest, &hostkey_digest_len, hostkey)) {
260462c32cbSDag-Erling Smørgrav error("Error calculating key fingerprint.");
261*4d3fc8b0SEd Maste free(dnskey_digest);
262462c32cbSDag-Erling Smørgrav freerrset(fingerprints);
263462c32cbSDag-Erling Smørgrav return -1;
264462c32cbSDag-Erling Smørgrav }
265462c32cbSDag-Erling Smørgrav
266d95e11bfSDag-Erling Smørgrav /* Check if the current key is the same as the given key */
267d95e11bfSDag-Erling Smørgrav if (hostkey_algorithm == dnskey_algorithm &&
26819261079SEd Maste hostkey_digest_len == dnskey_digest_len) {
26919261079SEd Maste if (timingsafe_bcmp(hostkey_digest, dnskey_digest,
27019261079SEd Maste hostkey_digest_len) == 0) {
27119261079SEd Maste debug_f("matched SSHFP type %d fptype %d",
27219261079SEd Maste dnskey_algorithm, dnskey_digest_type);
273efcad6b7SDag-Erling Smørgrav *flags |= DNS_VERIFY_MATCH;
27419261079SEd Maste } else {
27519261079SEd Maste debug_f("failed SSHFP type %d fptype %d",
27619261079SEd Maste dnskey_algorithm, dnskey_digest_type);
27719261079SEd Maste *flags |= DNS_VERIFY_FAILED;
27819261079SEd Maste }
279efcad6b7SDag-Erling Smørgrav }
280e4a9863fSDag-Erling Smørgrav free(dnskey_digest);
28119261079SEd Maste free(hostkey_digest); /* from sshkey_fingerprint_raw() */
282efcad6b7SDag-Erling Smørgrav }
283efcad6b7SDag-Erling Smørgrav
284d95e11bfSDag-Erling Smørgrav freerrset(fingerprints);
285efcad6b7SDag-Erling Smørgrav
28619261079SEd Maste /* If any fingerprint failed to validate, return failure. */
28719261079SEd Maste if (*flags & DNS_VERIFY_FAILED)
28819261079SEd Maste *flags &= ~DNS_VERIFY_MATCH;
28919261079SEd Maste
290efcad6b7SDag-Erling Smørgrav if (*flags & DNS_VERIFY_FOUND)
291efcad6b7SDag-Erling Smørgrav if (*flags & DNS_VERIFY_MATCH)
292d95e11bfSDag-Erling Smørgrav debug("matching host key fingerprint found in DNS");
293efcad6b7SDag-Erling Smørgrav else
294efcad6b7SDag-Erling Smørgrav debug("mismatching host key fingerprint found in DNS");
295efcad6b7SDag-Erling Smørgrav else
296efcad6b7SDag-Erling Smørgrav debug("no host key fingerprint found in DNS");
297d95e11bfSDag-Erling Smørgrav
298efcad6b7SDag-Erling Smørgrav return 0;
299d95e11bfSDag-Erling Smørgrav }
300d95e11bfSDag-Erling Smørgrav
301d95e11bfSDag-Erling Smørgrav /*
302d95e11bfSDag-Erling Smørgrav * Export the fingerprint of a key as a DNS resource record
303d95e11bfSDag-Erling Smørgrav */
304d95e11bfSDag-Erling Smørgrav int
export_dns_rr(const char * hostname,struct sshkey * key,FILE * f,int generic,int alg)305*4d3fc8b0SEd Maste export_dns_rr(const char *hostname, struct sshkey *key, FILE *f, int generic,
306*4d3fc8b0SEd Maste int alg)
307d95e11bfSDag-Erling Smørgrav {
308d95e11bfSDag-Erling Smørgrav u_int8_t rdata_pubkey_algorithm = 0;
309462c32cbSDag-Erling Smørgrav u_int8_t rdata_digest_type = SSHFP_HASH_RESERVED;
310462c32cbSDag-Erling Smørgrav u_int8_t dtype;
311d95e11bfSDag-Erling Smørgrav u_char *rdata_digest;
312bc5531deSDag-Erling Smørgrav size_t i, rdata_digest_len;
313d95e11bfSDag-Erling Smørgrav int success = 0;
314d95e11bfSDag-Erling Smørgrav
315462c32cbSDag-Erling Smørgrav for (dtype = SSHFP_HASH_SHA1; dtype < SSHFP_HASH_MAX; dtype++) {
316*4d3fc8b0SEd Maste if (alg != -1 && dtype != alg)
317*4d3fc8b0SEd Maste continue;
318462c32cbSDag-Erling Smørgrav rdata_digest_type = dtype;
319d95e11bfSDag-Erling Smørgrav if (dns_read_key(&rdata_pubkey_algorithm, &rdata_digest_type,
320d95e11bfSDag-Erling Smørgrav &rdata_digest, &rdata_digest_len, key)) {
321462c32cbSDag-Erling Smørgrav if (generic) {
322bc5531deSDag-Erling Smørgrav fprintf(f, "%s IN TYPE%d \\# %zu %02x %02x ",
323462c32cbSDag-Erling Smørgrav hostname, DNS_RDATATYPE_SSHFP,
324462c32cbSDag-Erling Smørgrav 2 + rdata_digest_len,
325d95e11bfSDag-Erling Smørgrav rdata_pubkey_algorithm, rdata_digest_type);
326462c32cbSDag-Erling Smørgrav } else {
327d95e11bfSDag-Erling Smørgrav fprintf(f, "%s IN SSHFP %d %d ", hostname,
328d95e11bfSDag-Erling Smørgrav rdata_pubkey_algorithm, rdata_digest_type);
329462c32cbSDag-Erling Smørgrav }
330d95e11bfSDag-Erling Smørgrav for (i = 0; i < rdata_digest_len; i++)
331d95e11bfSDag-Erling Smørgrav fprintf(f, "%02x", rdata_digest[i]);
332d95e11bfSDag-Erling Smørgrav fprintf(f, "\n");
333bc5531deSDag-Erling Smørgrav free(rdata_digest); /* from sshkey_fingerprint_raw() */
334d95e11bfSDag-Erling Smørgrav success = 1;
335462c32cbSDag-Erling Smørgrav }
336462c32cbSDag-Erling Smørgrav }
337462c32cbSDag-Erling Smørgrav
338462c32cbSDag-Erling Smørgrav /* No SSHFP record was generated at all */
339462c32cbSDag-Erling Smørgrav if (success == 0) {
34019261079SEd Maste error_f("unsupported algorithm and/or digest_type");
341d95e11bfSDag-Erling Smørgrav }
342d95e11bfSDag-Erling Smørgrav
343d95e11bfSDag-Erling Smørgrav return success;
344d95e11bfSDag-Erling Smørgrav }
345