dns.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /* $OpenBSD: dns.c,v 1.38 2018/02/23 15:58:37 markus Exp $ */
  2. /*
  3. * Copyright (c) 2003 Wesley Griffin. All rights reserved.
  4. * Copyright (c) 2003 Jakob Schlyter. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "includes.h"
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <netdb.h>
  30. #include <stdarg.h>
  31. #include <stdio.h>
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include "xmalloc.h"
  35. #include "sshkey.h"
  36. #include "ssherr.h"
  37. #include "dns.h"
  38. #include "log.h"
  39. #include "digest.h"
  40. static const char *errset_text[] = {
  41. "success", /* 0 ERRSET_SUCCESS */
  42. "out of memory", /* 1 ERRSET_NOMEMORY */
  43. "general failure", /* 2 ERRSET_FAIL */
  44. "invalid parameter", /* 3 ERRSET_INVAL */
  45. "name does not exist", /* 4 ERRSET_NONAME */
  46. "data does not exist", /* 5 ERRSET_NODATA */
  47. };
  48. static const char *
  49. dns_result_totext(unsigned int res)
  50. {
  51. switch (res) {
  52. case ERRSET_SUCCESS:
  53. return errset_text[ERRSET_SUCCESS];
  54. case ERRSET_NOMEMORY:
  55. return errset_text[ERRSET_NOMEMORY];
  56. case ERRSET_FAIL:
  57. return errset_text[ERRSET_FAIL];
  58. case ERRSET_INVAL:
  59. return errset_text[ERRSET_INVAL];
  60. case ERRSET_NONAME:
  61. return errset_text[ERRSET_NONAME];
  62. case ERRSET_NODATA:
  63. return errset_text[ERRSET_NODATA];
  64. default:
  65. return "unknown error";
  66. }
  67. }
  68. /*
  69. * Read SSHFP parameters from key buffer.
  70. */
  71. static int
  72. dns_read_key(u_int8_t *algorithm, u_int8_t *digest_type,
  73. u_char **digest, size_t *digest_len, struct sshkey *key)
  74. {
  75. int r, success = 0;
  76. int fp_alg = -1;
  77. switch (key->type) {
  78. case KEY_RSA:
  79. *algorithm = SSHFP_KEY_RSA;
  80. if (!*digest_type)
  81. *digest_type = SSHFP_HASH_SHA1;
  82. break;
  83. case KEY_DSA:
  84. *algorithm = SSHFP_KEY_DSA;
  85. if (!*digest_type)
  86. *digest_type = SSHFP_HASH_SHA1;
  87. break;
  88. case KEY_ECDSA:
  89. *algorithm = SSHFP_KEY_ECDSA;
  90. if (!*digest_type)
  91. *digest_type = SSHFP_HASH_SHA256;
  92. break;
  93. case KEY_ED25519:
  94. *algorithm = SSHFP_KEY_ED25519;
  95. if (!*digest_type)
  96. *digest_type = SSHFP_HASH_SHA256;
  97. break;
  98. case KEY_XMSS:
  99. *algorithm = SSHFP_KEY_XMSS;
  100. if (!*digest_type)
  101. *digest_type = SSHFP_HASH_SHA256;
  102. break;
  103. default:
  104. *algorithm = SSHFP_KEY_RESERVED; /* 0 */
  105. *digest_type = SSHFP_HASH_RESERVED; /* 0 */
  106. }
  107. switch (*digest_type) {
  108. case SSHFP_HASH_SHA1:
  109. fp_alg = SSH_DIGEST_SHA1;
  110. break;
  111. case SSHFP_HASH_SHA256:
  112. fp_alg = SSH_DIGEST_SHA256;
  113. break;
  114. default:
  115. *digest_type = SSHFP_HASH_RESERVED; /* 0 */
  116. }
  117. if (*algorithm && *digest_type) {
  118. if ((r = sshkey_fingerprint_raw(key, fp_alg, digest,
  119. digest_len)) != 0)
  120. fatal("%s: sshkey_fingerprint_raw: %s", __func__,
  121. ssh_err(r));
  122. success = 1;
  123. } else {
  124. *digest = NULL;
  125. *digest_len = 0;
  126. success = 0;
  127. }
  128. return success;
  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, size_t *digest_len, u_char *rdata, int rdata_len)
  136. {
  137. int success = 0;
  138. *algorithm = SSHFP_KEY_RESERVED;
  139. *digest_type = SSHFP_HASH_RESERVED;
  140. if (rdata_len >= 2) {
  141. *algorithm = rdata[0];
  142. *digest_type = rdata[1];
  143. *digest_len = rdata_len - 2;
  144. if (*digest_len > 0) {
  145. *digest = xmalloc(*digest_len);
  146. memcpy(*digest, rdata + 2, *digest_len);
  147. } else {
  148. *digest = (u_char *)xstrdup("");
  149. }
  150. success = 1;
  151. }
  152. return success;
  153. }
  154. /*
  155. * Check if hostname is numerical.
  156. * Returns -1 if hostname is numeric, 0 otherwise
  157. */
  158. static int
  159. is_numeric_hostname(const char *hostname)
  160. {
  161. struct addrinfo hints, *ai;
  162. /*
  163. * We shouldn't ever get a null host but if we do then log an error
  164. * and return -1 which stops DNS key fingerprint processing.
  165. */
  166. if (hostname == NULL) {
  167. error("is_numeric_hostname called with NULL hostname");
  168. return -1;
  169. }
  170. memset(&hints, 0, sizeof(hints));
  171. hints.ai_socktype = SOCK_DGRAM;
  172. hints.ai_flags = AI_NUMERICHOST;
  173. if (getaddrinfo(hostname, NULL, &hints, &ai) == 0) {
  174. freeaddrinfo(ai);
  175. return -1;
  176. }
  177. return 0;
  178. }
  179. /*
  180. * Verify the given hostname, address and host key using DNS.
  181. * Returns 0 if lookup succeeds, -1 otherwise
  182. */
  183. int
  184. verify_host_key_dns(const char *hostname, struct sockaddr *address,
  185. struct sshkey *hostkey, int *flags)
  186. {
  187. u_int counter;
  188. int result;
  189. unsigned int rrset_flags = 0;
  190. struct rrsetinfo *fingerprints = NULL;
  191. u_int8_t hostkey_algorithm;
  192. u_int8_t hostkey_digest_type = SSHFP_HASH_RESERVED;
  193. u_char *hostkey_digest;
  194. size_t hostkey_digest_len;
  195. u_int8_t dnskey_algorithm;
  196. u_int8_t dnskey_digest_type;
  197. u_char *dnskey_digest;
  198. size_t dnskey_digest_len;
  199. int verify_hosts = *flags;
  200. *flags = 0;
  201. debug3("verify_host_key_dns");
  202. if (hostkey == NULL)
  203. fatal("No key to look up!");
  204. if (is_numeric_hostname(hostname)) {
  205. debug("skipped DNS lookup for numerical hostname");
  206. return -1;
  207. }
  208. /*
  209. * Original getrrsetbyname function, found on OpenBSD for example,
  210. * doesn't accept any flag and prerequisite for obtaining AD bit in
  211. * DNS response is set by "options edns0" in resolv.conf.
  212. *
  213. * Our version is more clever and use RRSET_FORCE_EDNS0 flag.
  214. */
  215. #ifndef HAVE_GETRRSETBYNAME
  216. rrset_flags |= RRSET_FORCE_EDNS0;
  217. #endif
  218. result = getrrsetbyname(hostname, DNS_RDATACLASS_IN,
  219. DNS_RDATATYPE_SSHFP, rrset_flags, &fingerprints);
  220. if (result) {
  221. verbose("DNS lookup error: %s", dns_result_totext(result));
  222. return -1;
  223. }
  224. if (fingerprints->rri_flags & RRSET_VALIDATED) {
  225. *flags |= DNS_VERIFY_SECURE;
  226. debug("found %d secure fingerprints in DNS",
  227. fingerprints->rri_nrdatas);
  228. } else {
  229. debug("found %d insecure fingerprints in DNS",
  230. fingerprints->rri_nrdatas);
  231. if (verify_hosts == 1 &&
  232. (fingerprints->rri_flags & RRSET_SECURE_UNSUPPORTED))
  233. error("System does not support secure DNS, "
  234. "try options edns0 in resolv.conf");
  235. }
  236. /* Initialize default host key parameters */
  237. if (!dns_read_key(&hostkey_algorithm, &hostkey_digest_type,
  238. &hostkey_digest, &hostkey_digest_len, hostkey)) {
  239. error("Error calculating host key fingerprint.");
  240. freerrset(fingerprints);
  241. return -1;
  242. }
  243. if (fingerprints->rri_nrdatas)
  244. *flags |= DNS_VERIFY_FOUND;
  245. for (counter = 0; counter < fingerprints->rri_nrdatas; counter++) {
  246. /*
  247. * Extract the key from the answer. Ignore any badly
  248. * formatted fingerprints.
  249. */
  250. if (!dns_read_rdata(&dnskey_algorithm, &dnskey_digest_type,
  251. &dnskey_digest, &dnskey_digest_len,
  252. fingerprints->rri_rdatas[counter].rdi_data,
  253. fingerprints->rri_rdatas[counter].rdi_length)) {
  254. verbose("Error parsing fingerprint from DNS.");
  255. continue;
  256. }
  257. if (hostkey_digest_type != dnskey_digest_type) {
  258. hostkey_digest_type = dnskey_digest_type;
  259. free(hostkey_digest);
  260. /* Initialize host key parameters */
  261. if (!dns_read_key(&hostkey_algorithm,
  262. &hostkey_digest_type, &hostkey_digest,
  263. &hostkey_digest_len, hostkey)) {
  264. error("Error calculating key fingerprint.");
  265. freerrset(fingerprints);
  266. return -1;
  267. }
  268. }
  269. /* Check if the current key is the same as the given key */
  270. if (hostkey_algorithm == dnskey_algorithm &&
  271. hostkey_digest_type == dnskey_digest_type) {
  272. if (hostkey_digest_len == dnskey_digest_len &&
  273. timingsafe_bcmp(hostkey_digest, dnskey_digest,
  274. hostkey_digest_len) == 0)
  275. *flags |= DNS_VERIFY_MATCH;
  276. }
  277. free(dnskey_digest);
  278. }
  279. free(hostkey_digest); /* from sshkey_fingerprint_raw() */
  280. freerrset(fingerprints);
  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. return 0;
  289. }
  290. /*
  291. * Export the fingerprint of a key as a DNS resource record
  292. */
  293. int
  294. export_dns_rr(const char *hostname, struct sshkey *key, FILE *f, int generic)
  295. {
  296. u_int8_t rdata_pubkey_algorithm = 0;
  297. u_int8_t rdata_digest_type = SSHFP_HASH_RESERVED;
  298. u_int8_t dtype;
  299. u_char *rdata_digest;
  300. size_t i, rdata_digest_len;
  301. int success = 0;
  302. for (dtype = SSHFP_HASH_SHA1; dtype < SSHFP_HASH_MAX; dtype++) {
  303. rdata_digest_type = dtype;
  304. if (dns_read_key(&rdata_pubkey_algorithm, &rdata_digest_type,
  305. &rdata_digest, &rdata_digest_len, key)) {
  306. if (generic) {
  307. fprintf(f, "%s IN TYPE%d \\# %zu %02x %02x ",
  308. hostname, DNS_RDATATYPE_SSHFP,
  309. 2 + rdata_digest_len,
  310. rdata_pubkey_algorithm, rdata_digest_type);
  311. } else {
  312. fprintf(f, "%s IN SSHFP %d %d ", hostname,
  313. rdata_pubkey_algorithm, rdata_digest_type);
  314. }
  315. for (i = 0; i < rdata_digest_len; i++)
  316. fprintf(f, "%02x", rdata_digest[i]);
  317. fprintf(f, "\n");
  318. free(rdata_digest); /* from sshkey_fingerprint_raw() */
  319. success = 1;
  320. }
  321. }
  322. /* No SSHFP record was generated at all */
  323. if (success == 0) {
  324. error("%s: unsupported algorithm and/or digest_type", __func__);
  325. }
  326. return success;
  327. }