2014-07-02 07:28:02 +02:00
|
|
|
/* $OpenBSD: dns.c,v 1.31 2014/06/24 01:13:21 djm Exp $ */
|
2003-05-15 02:19:46 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2003 Wesley Griffin. All rights reserved.
|
|
|
|
* Copyright (c) 2003 Jakob Schlyter. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
|
|
|
|
2006-07-10 13:08:03 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
2006-07-24 06:51:00 +02:00
|
|
|
#include <netdb.h>
|
2006-09-01 07:38:36 +02:00
|
|
|
#include <stdarg.h>
|
2006-08-05 03:37:59 +02:00
|
|
|
#include <stdio.h>
|
2006-07-24 06:13:33 +02:00
|
|
|
#include <string.h>
|
2014-07-02 07:28:02 +02:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdlib.h>
|
2003-05-15 02:19:46 +02:00
|
|
|
|
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "key.h"
|
|
|
|
#include "dns.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
static const char *errset_text[] = {
|
|
|
|
"success", /* 0 ERRSET_SUCCESS */
|
|
|
|
"out of memory", /* 1 ERRSET_NOMEMORY */
|
|
|
|
"general failure", /* 2 ERRSET_FAIL */
|
|
|
|
"invalid parameter", /* 3 ERRSET_INVAL */
|
|
|
|
"name does not exist", /* 4 ERRSET_NONAME */
|
|
|
|
"data does not exist", /* 5 ERRSET_NODATA */
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char *
|
2004-06-22 04:56:01 +02:00
|
|
|
dns_result_totext(unsigned int res)
|
2003-05-15 02:19:46 +02:00
|
|
|
{
|
2004-06-22 04:56:01 +02:00
|
|
|
switch (res) {
|
2003-05-15 02:19:46 +02:00
|
|
|
case ERRSET_SUCCESS:
|
|
|
|
return errset_text[ERRSET_SUCCESS];
|
|
|
|
case ERRSET_NOMEMORY:
|
|
|
|
return errset_text[ERRSET_NOMEMORY];
|
|
|
|
case ERRSET_FAIL:
|
|
|
|
return errset_text[ERRSET_FAIL];
|
|
|
|
case ERRSET_INVAL:
|
|
|
|
return errset_text[ERRSET_INVAL];
|
|
|
|
case ERRSET_NONAME:
|
|
|
|
return errset_text[ERRSET_NONAME];
|
|
|
|
case ERRSET_NODATA:
|
|
|
|
return errset_text[ERRSET_NODATA];
|
|
|
|
default:
|
|
|
|
return "unknown error";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read SSHFP parameters from key buffer.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type,
|
2010-02-26 21:55:05 +01:00
|
|
|
u_char **digest, u_int *digest_len, Key *key)
|
2003-05-15 02:19:46 +02:00
|
|
|
{
|
|
|
|
int success = 0;
|
2012-06-20 13:51:11 +02:00
|
|
|
enum fp_type fp_type = 0;
|
2003-05-15 02:19:46 +02:00
|
|
|
|
|
|
|
switch (key->type) {
|
|
|
|
case KEY_RSA:
|
|
|
|
*algorithm = SSHFP_KEY_RSA;
|
2012-06-20 13:51:11 +02:00
|
|
|
if (!*digest_type)
|
|
|
|
*digest_type = SSHFP_HASH_SHA1;
|
2003-05-15 02:19:46 +02:00
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
*algorithm = SSHFP_KEY_DSA;
|
2012-06-20 13:51:11 +02:00
|
|
|
if (!*digest_type)
|
|
|
|
*digest_type = SSHFP_HASH_SHA1;
|
|
|
|
break;
|
|
|
|
case KEY_ECDSA:
|
|
|
|
*algorithm = SSHFP_KEY_ECDSA;
|
|
|
|
if (!*digest_type)
|
|
|
|
*digest_type = SSHFP_HASH_SHA256;
|
2003-05-15 02:19:46 +02:00
|
|
|
break;
|
2014-05-15 05:45:58 +02:00
|
|
|
case KEY_ED25519:
|
|
|
|
*algorithm = SSHFP_KEY_ED25519;
|
|
|
|
if (!*digest_type)
|
|
|
|
*digest_type = SSHFP_HASH_SHA256;
|
|
|
|
break;
|
2003-05-15 02:19:46 +02:00
|
|
|
default:
|
2005-11-05 05:09:27 +01:00
|
|
|
*algorithm = SSHFP_KEY_RESERVED; /* 0 */
|
2012-06-20 13:51:11 +02:00
|
|
|
*digest_type = SSHFP_HASH_RESERVED; /* 0 */
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (*digest_type) {
|
|
|
|
case SSHFP_HASH_SHA1:
|
|
|
|
fp_type = SSH_FP_SHA1;
|
|
|
|
break;
|
|
|
|
case SSHFP_HASH_SHA256:
|
|
|
|
fp_type = SSH_FP_SHA256;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*digest_type = SSHFP_HASH_RESERVED; /* 0 */
|
2003-05-15 02:19:46 +02:00
|
|
|
}
|
|
|
|
|
2012-06-20 13:51:11 +02:00
|
|
|
if (*algorithm && *digest_type) {
|
|
|
|
*digest = key_fingerprint_raw(key, fp_type, digest_len);
|
2005-11-05 05:09:27 +01:00
|
|
|
if (*digest == NULL)
|
|
|
|
fatal("dns_read_key: null from key_fingerprint_raw()");
|
2003-05-15 02:19:46 +02:00
|
|
|
success = 1;
|
|
|
|
} else {
|
|
|
|
*digest = NULL;
|
|
|
|
*digest_len = 0;
|
|
|
|
success = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read SSHFP parameters from rdata buffer.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
dns_read_rdata(u_int8_t *algorithm, u_int8_t *digest_type,
|
|
|
|
u_char **digest, u_int *digest_len, u_char *rdata, int rdata_len)
|
|
|
|
{
|
|
|
|
int success = 0;
|
|
|
|
|
|
|
|
*algorithm = SSHFP_KEY_RESERVED;
|
|
|
|
*digest_type = SSHFP_HASH_RESERVED;
|
|
|
|
|
|
|
|
if (rdata_len >= 2) {
|
|
|
|
*algorithm = rdata[0];
|
|
|
|
*digest_type = rdata[1];
|
|
|
|
*digest_len = rdata_len - 2;
|
|
|
|
|
|
|
|
if (*digest_len > 0) {
|
|
|
|
*digest = (u_char *) xmalloc(*digest_len);
|
|
|
|
memcpy(*digest, rdata + 2, *digest_len);
|
|
|
|
} else {
|
2006-03-25 14:06:48 +01:00
|
|
|
*digest = (u_char *)xstrdup("");
|
2003-05-15 02:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
success = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2005-05-26 04:03:31 +02:00
|
|
|
/*
|
|
|
|
* Check if hostname is numerical.
|
|
|
|
* Returns -1 if hostname is numeric, 0 otherwise
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
is_numeric_hostname(const char *hostname)
|
|
|
|
{
|
|
|
|
struct addrinfo hints, *ai;
|
|
|
|
|
2008-06-12 20:46:45 +02:00
|
|
|
/*
|
|
|
|
* We shouldn't ever get a null host but if we do then log an error
|
|
|
|
* and return -1 which stops DNS key fingerprint processing.
|
|
|
|
*/
|
|
|
|
if (hostname == NULL) {
|
|
|
|
error("is_numeric_hostname called with NULL hostname");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-05-26 04:03:31 +02:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
hints.ai_flags = AI_NUMERICHOST;
|
|
|
|
|
2008-06-12 20:46:45 +02:00
|
|
|
if (getaddrinfo(hostname, NULL, &hints, &ai) == 0) {
|
2005-05-26 04:03:31 +02:00
|
|
|
freeaddrinfo(ai);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2003-05-15 02:19:46 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify the given hostname, address and host key using DNS.
|
2003-11-21 13:48:55 +01:00
|
|
|
* Returns 0 if lookup succeeds, -1 otherwise
|
2003-05-15 02:19:46 +02:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
verify_host_key_dns(const char *hostname, struct sockaddr *address,
|
2010-02-26 21:55:05 +01:00
|
|
|
Key *hostkey, int *flags)
|
2003-05-15 02:19:46 +02:00
|
|
|
{
|
2005-06-17 04:59:34 +02:00
|
|
|
u_int counter;
|
2003-05-15 02:19:46 +02:00
|
|
|
int result;
|
|
|
|
struct rrsetinfo *fingerprints = NULL;
|
|
|
|
|
|
|
|
u_int8_t hostkey_algorithm;
|
2012-06-20 13:51:11 +02:00
|
|
|
u_int8_t hostkey_digest_type = SSHFP_HASH_RESERVED;
|
2003-05-15 02:19:46 +02:00
|
|
|
u_char *hostkey_digest;
|
|
|
|
u_int hostkey_digest_len;
|
|
|
|
|
|
|
|
u_int8_t dnskey_algorithm;
|
|
|
|
u_int8_t dnskey_digest_type;
|
|
|
|
u_char *dnskey_digest;
|
|
|
|
u_int dnskey_digest_len;
|
|
|
|
|
2003-11-17 11:19:29 +01:00
|
|
|
*flags = 0;
|
2003-05-15 02:19:46 +02:00
|
|
|
|
2005-11-05 05:11:15 +01:00
|
|
|
debug3("verify_host_key_dns");
|
2003-05-15 02:19:46 +02:00
|
|
|
if (hostkey == NULL)
|
|
|
|
fatal("No key to look up!");
|
|
|
|
|
2005-05-26 04:03:31 +02:00
|
|
|
if (is_numeric_hostname(hostname)) {
|
|
|
|
debug("skipped DNS lookup for numerical hostname");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2003-05-15 02:19:46 +02:00
|
|
|
result = getrrsetbyname(hostname, DNS_RDATACLASS_IN,
|
|
|
|
DNS_RDATATYPE_SSHFP, 0, &fingerprints);
|
|
|
|
if (result) {
|
|
|
|
verbose("DNS lookup error: %s", dns_result_totext(result));
|
2003-11-17 11:19:29 +01:00
|
|
|
return -1;
|
2003-05-15 02:19:46 +02:00
|
|
|
}
|
|
|
|
|
2003-11-17 11:19:29 +01:00
|
|
|
if (fingerprints->rri_flags & RRSET_VALIDATED) {
|
|
|
|
*flags |= DNS_VERIFY_SECURE;
|
|
|
|
debug("found %d secure fingerprints in DNS",
|
|
|
|
fingerprints->rri_nrdatas);
|
|
|
|
} else {
|
|
|
|
debug("found %d insecure fingerprints in DNS",
|
|
|
|
fingerprints->rri_nrdatas);
|
2003-05-15 02:19:46 +02:00
|
|
|
}
|
|
|
|
|
2012-06-20 13:51:11 +02:00
|
|
|
/* Initialize default host key parameters */
|
2003-05-15 02:19:46 +02:00
|
|
|
if (!dns_read_key(&hostkey_algorithm, &hostkey_digest_type,
|
|
|
|
&hostkey_digest, &hostkey_digest_len, hostkey)) {
|
|
|
|
error("Error calculating host key fingerprint.");
|
2003-05-15 05:27:28 +02:00
|
|
|
freerrset(fingerprints);
|
2003-11-17 11:19:29 +01:00
|
|
|
return -1;
|
2003-05-15 02:19:46 +02:00
|
|
|
}
|
|
|
|
|
2003-11-17 11:19:29 +01:00
|
|
|
if (fingerprints->rri_nrdatas)
|
|
|
|
*flags |= DNS_VERIFY_FOUND;
|
|
|
|
|
2007-01-05 06:30:16 +01:00
|
|
|
for (counter = 0; counter < fingerprints->rri_nrdatas; counter++) {
|
2003-05-15 02:19:46 +02:00
|
|
|
/*
|
|
|
|
* Extract the key from the answer. Ignore any badly
|
|
|
|
* formatted fingerprints.
|
|
|
|
*/
|
|
|
|
if (!dns_read_rdata(&dnskey_algorithm, &dnskey_digest_type,
|
|
|
|
&dnskey_digest, &dnskey_digest_len,
|
|
|
|
fingerprints->rri_rdatas[counter].rdi_data,
|
|
|
|
fingerprints->rri_rdatas[counter].rdi_length)) {
|
|
|
|
verbose("Error parsing fingerprint from DNS.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2012-06-20 13:51:11 +02:00
|
|
|
if (hostkey_digest_type != dnskey_digest_type) {
|
|
|
|
hostkey_digest_type = dnskey_digest_type;
|
2013-06-01 23:31:17 +02:00
|
|
|
free(hostkey_digest);
|
2012-06-20 13:51:11 +02:00
|
|
|
|
|
|
|
/* Initialize host key parameters */
|
|
|
|
if (!dns_read_key(&hostkey_algorithm,
|
|
|
|
&hostkey_digest_type, &hostkey_digest,
|
|
|
|
&hostkey_digest_len, hostkey)) {
|
|
|
|
error("Error calculating key fingerprint.");
|
|
|
|
freerrset(fingerprints);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-05-15 02:19:46 +02:00
|
|
|
/* Check if the current key is the same as the given key */
|
|
|
|
if (hostkey_algorithm == dnskey_algorithm &&
|
|
|
|
hostkey_digest_type == dnskey_digest_type) {
|
|
|
|
if (hostkey_digest_len == dnskey_digest_len &&
|
2012-06-20 13:51:11 +02:00
|
|
|
timingsafe_bcmp(hostkey_digest, dnskey_digest,
|
|
|
|
hostkey_digest_len) == 0)
|
2003-11-17 11:19:29 +01:00
|
|
|
*flags |= DNS_VERIFY_MATCH;
|
2003-05-15 02:19:46 +02:00
|
|
|
}
|
2013-06-01 23:31:17 +02:00
|
|
|
free(dnskey_digest);
|
2003-05-15 02:19:46 +02:00
|
|
|
}
|
|
|
|
|
2013-06-01 23:31:17 +02:00
|
|
|
free(hostkey_digest); /* from key_fingerprint_raw() */
|
2003-05-15 02:19:46 +02:00
|
|
|
freerrset(fingerprints);
|
|
|
|
|
2003-11-17 11:19:29 +01:00
|
|
|
if (*flags & DNS_VERIFY_FOUND)
|
|
|
|
if (*flags & DNS_VERIFY_MATCH)
|
|
|
|
debug("matching host key fingerprint found in DNS");
|
|
|
|
else
|
|
|
|
debug("mismatching host key fingerprint found in DNS");
|
|
|
|
else
|
|
|
|
debug("no host key fingerprint found in DNS");
|
2003-05-15 02:19:46 +02:00
|
|
|
|
2003-11-17 11:19:29 +01:00
|
|
|
return 0;
|
2003-05-15 02:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Export the fingerprint of a key as a DNS resource record
|
|
|
|
*/
|
|
|
|
int
|
2010-02-26 21:55:05 +01:00
|
|
|
export_dns_rr(const char *hostname, Key *key, FILE *f, int generic)
|
2003-05-15 02:19:46 +02:00
|
|
|
{
|
|
|
|
u_int8_t rdata_pubkey_algorithm = 0;
|
2012-06-20 13:51:11 +02:00
|
|
|
u_int8_t rdata_digest_type = SSHFP_HASH_RESERVED;
|
|
|
|
u_int8_t dtype;
|
2003-05-15 02:19:46 +02:00
|
|
|
u_char *rdata_digest;
|
2012-06-20 13:51:11 +02:00
|
|
|
u_int i, rdata_digest_len;
|
2003-05-15 02:19:46 +02:00
|
|
|
int success = 0;
|
|
|
|
|
2012-06-20 13:51:11 +02:00
|
|
|
for (dtype = SSHFP_HASH_SHA1; dtype < SSHFP_HASH_MAX; dtype++) {
|
|
|
|
rdata_digest_type = dtype;
|
|
|
|
if (dns_read_key(&rdata_pubkey_algorithm, &rdata_digest_type,
|
|
|
|
&rdata_digest, &rdata_digest_len, key)) {
|
|
|
|
if (generic) {
|
|
|
|
fprintf(f, "%s IN TYPE%d \\# %d %02x %02x ",
|
|
|
|
hostname, DNS_RDATATYPE_SSHFP,
|
|
|
|
2 + rdata_digest_len,
|
|
|
|
rdata_pubkey_algorithm, rdata_digest_type);
|
|
|
|
} else {
|
|
|
|
fprintf(f, "%s IN SSHFP %d %d ", hostname,
|
|
|
|
rdata_pubkey_algorithm, rdata_digest_type);
|
|
|
|
}
|
|
|
|
for (i = 0; i < rdata_digest_len; i++)
|
|
|
|
fprintf(f, "%02x", rdata_digest[i]);
|
|
|
|
fprintf(f, "\n");
|
2013-06-01 23:31:17 +02:00
|
|
|
free(rdata_digest); /* from key_fingerprint_raw() */
|
2012-06-20 13:51:11 +02:00
|
|
|
success = 1;
|
|
|
|
}
|
|
|
|
}
|
2003-05-15 02:19:46 +02:00
|
|
|
|
2012-06-20 13:51:11 +02:00
|
|
|
/* No SSHFP record was generated at all */
|
|
|
|
if (success == 0) {
|
|
|
|
error("%s: unsupported algorithm and/or digest_type", __func__);
|
2003-05-15 02:19:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
|
|
|
}
|