blob: 5f0366310d541baa3e257482c5e288180e21ba4d [file] [log] [blame]
djm@openbsd.orgc29811c2015-01-18 21:40:23 +00001/* $OpenBSD: hostfile.c,v 1.60 2015/01/18 21:40:23 djm Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for manipulating the known hosts files.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
13 *
14 *
Ben Lindstrom44697232001-07-04 03:32:30 +000015 * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110016 * Copyright (c) 1999 Niels Provos. All rights reserved.
17 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Damien Millere1776152005-03-01 21:47:37 +110040
Damien Miller8ec8c3e2006-07-10 20:35:38 +100041#include <sys/types.h>
42
43#include <netinet/in.h>
44
djm@openbsd.orgc29811c2015-01-18 21:40:23 +000045#include <errno.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100046#include <resolv.h>
Damien Millerded319c2006-09-01 15:38:36 +100047#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100048#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100049#include <stdlib.h>
50#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100051#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100052
Damien Millerd7834352006-08-05 12:39:39 +100053#include "xmalloc.h"
Damien Miller450a7a12000-03-26 13:04:51 +100054#include "match.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000055#include "sshkey.h"
Damien Miller450a7a12000-03-26 13:04:51 +100056#include "hostfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "log.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110058#include "misc.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000059#include "ssherr.h"
Damien Millerb3051d02014-01-10 10:58:53 +110060#include "digest.h"
Damien Miller4e8d9372014-02-04 11:02:42 +110061#include "hmac.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110062
63struct hostkeys {
64 struct hostkey_entry *entries;
65 u_int num_entries;
66};
Damien Millere1776152005-03-01 21:47:37 +110067
djm@openbsd.orgc29811c2015-01-18 21:40:23 +000068/* XXX hmac is too easy to dictionary attack; use bcrypt? */
69
Damien Millere1776152005-03-01 21:47:37 +110070static int
Damien Millerce986542013-07-18 16:12:44 +100071extract_salt(const char *s, u_int l, u_char *salt, size_t salt_len)
Damien Millere1776152005-03-01 21:47:37 +110072{
73 char *p, *b64salt;
74 u_int b64len;
75 int ret;
76
77 if (l < sizeof(HASH_MAGIC) - 1) {
78 debug2("extract_salt: string too short");
79 return (-1);
80 }
81 if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) {
82 debug2("extract_salt: invalid magic identifier");
83 return (-1);
84 }
85 s += sizeof(HASH_MAGIC) - 1;
86 l -= sizeof(HASH_MAGIC) - 1;
87 if ((p = memchr(s, HASH_DELIM, l)) == NULL) {
88 debug2("extract_salt: missing salt termination character");
89 return (-1);
90 }
91
92 b64len = p - s;
93 /* Sanity check */
94 if (b64len == 0 || b64len > 1024) {
95 debug2("extract_salt: bad encoded salt length %u", b64len);
96 return (-1);
97 }
98 b64salt = xmalloc(1 + b64len);
99 memcpy(b64salt, s, b64len);
100 b64salt[b64len] = '\0';
101
102 ret = __b64_pton(b64salt, salt, salt_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000103 free(b64salt);
Damien Millere1776152005-03-01 21:47:37 +1100104 if (ret == -1) {
105 debug2("extract_salt: salt decode error");
106 return (-1);
107 }
Damien Miller4e8d9372014-02-04 11:02:42 +1100108 if (ret != (int)ssh_hmac_bytes(SSH_DIGEST_SHA1)) {
109 debug2("extract_salt: expected salt len %zd, got %d",
110 ssh_hmac_bytes(SSH_DIGEST_SHA1), ret);
Damien Millere1776152005-03-01 21:47:37 +1100111 return (-1);
112 }
Darren Tucker47eede72005-03-14 23:08:12 +1100113
Damien Millere1776152005-03-01 21:47:37 +1100114 return (0);
115}
116
117char *
118host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
119{
Damien Miller4e8d9372014-02-04 11:02:42 +1100120 struct ssh_hmac_ctx *ctx;
Damien Millerce986542013-07-18 16:12:44 +1000121 u_char salt[256], result[256];
122 char uu_salt[512], uu_result[512];
Damien Millere1776152005-03-01 21:47:37 +1100123 static char encoded[1024];
124 u_int i, len;
125
Damien Miller4e8d9372014-02-04 11:02:42 +1100126 len = ssh_digest_bytes(SSH_DIGEST_SHA1);
Damien Millere1776152005-03-01 21:47:37 +1100127
128 if (name_from_hostfile == NULL) {
129 /* Create new salt */
130 for (i = 0; i < len; i++)
131 salt[i] = arc4random();
132 } else {
133 /* Extract salt from known host entry */
134 if (extract_salt(name_from_hostfile, src_len, salt,
135 sizeof(salt)) == -1)
136 return (NULL);
137 }
138
Damien Miller4e8d9372014-02-04 11:02:42 +1100139 if ((ctx = ssh_hmac_start(SSH_DIGEST_SHA1)) == NULL ||
140 ssh_hmac_init(ctx, salt, len) < 0 ||
141 ssh_hmac_update(ctx, host, strlen(host)) < 0 ||
142 ssh_hmac_final(ctx, result, sizeof(result)))
143 fatal("%s: ssh_hmac failed", __func__);
144 ssh_hmac_free(ctx);
Damien Millere1776152005-03-01 21:47:37 +1100145
Darren Tucker47eede72005-03-14 23:08:12 +1100146 if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 ||
Damien Millere1776152005-03-01 21:47:37 +1100147 __b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1)
Damien Miller4e8d9372014-02-04 11:02:42 +1100148 fatal("%s: __b64_ntop failed", __func__);
Damien Millere1776152005-03-01 21:47:37 +1100149
150 snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt,
151 HASH_DELIM, uu_result);
152
153 return (encoded);
154}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155
Damien Miller5428f641999-11-25 11:54:57 +1100156/*
Damien Miller450a7a12000-03-26 13:04:51 +1000157 * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the
158 * pointer over the key. Skips any whitespace at the beginning and at end.
Damien Miller5428f641999-11-25 11:54:57 +1100159 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller5b2aea92001-12-21 12:47:09 +1100161int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000162hostfile_read_key(char **cpp, u_int *bitsp, struct sshkey *ret)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163{
Damien Miller95def091999-11-25 00:26:21 +1100164 char *cp;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000165 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166
Damien Miller95def091999-11-25 00:26:21 +1100167 /* Skip leading whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +1100168 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
169 ;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000171 if ((r = sshkey_read(ret, &cp)) != 0)
Damien Miller95def091999-11-25 00:26:21 +1100172 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000173
Damien Miller95def091999-11-25 00:26:21 +1100174 /* Skip trailing whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +1100175 for (; *cp == ' ' || *cp == '\t'; cp++)
176 ;
Damien Miller95def091999-11-25 00:26:21 +1100177
178 /* Return results. */
179 *cpp = cp;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000180 if (bitsp != NULL)
181 *bitsp = sshkey_size(ret);
Damien Miller95def091999-11-25 00:26:21 +1100182 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183}
184
Ben Lindstrombba81212001-06-25 05:01:22 +0000185static int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000186hostfile_check_key(int bits, const struct sshkey *key, const char *host,
Damien Millerd925dcd2010-12-01 12:21:51 +1100187 const char *filename, u_long linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188{
Damien Miller1f0311c2014-05-15 14:24:09 +1000189#ifdef WITH_SSH1
Damien Miller0bc1bd82000-11-13 22:57:25 +1100190 if (key == NULL || key->type != KEY_RSA1 || key->rsa == NULL)
Damien Miller450a7a12000-03-26 13:04:51 +1000191 return 1;
192 if (bits != BN_num_bits(key->rsa->n)) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100193 logit("Warning: %s, line %lu: keysize mismatch for host %s: "
Damien Miller450a7a12000-03-26 13:04:51 +1000194 "actual %d vs. announced %d.",
195 filename, linenum, host, BN_num_bits(key->rsa->n), bits);
Damien Millerd925dcd2010-12-01 12:21:51 +1100196 logit("Warning: replace %d with %d in %s, line %lu.",
Damien Miller450a7a12000-03-26 13:04:51 +1000197 bits, BN_num_bits(key->rsa->n), filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000199#endif
Damien Miller450a7a12000-03-26 13:04:51 +1000200 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000201}
202
Damien Millerd925dcd2010-12-01 12:21:51 +1100203static HostkeyMarker
Damien Miller1aed65e2010-03-04 21:53:35 +1100204check_markers(char **cpp)
205{
206 char marker[32], *sp, *cp = *cpp;
207 int ret = MRK_NONE;
208
209 while (*cp == '@') {
210 /* Only one marker is allowed */
211 if (ret != MRK_NONE)
212 return MRK_ERROR;
213 /* Markers are terminated by whitespace */
214 if ((sp = strchr(cp, ' ')) == NULL &&
215 (sp = strchr(cp, '\t')) == NULL)
216 return MRK_ERROR;
217 /* Extract marker for comparison */
218 if (sp <= cp + 1 || sp >= cp + sizeof(marker))
219 return MRK_ERROR;
220 memcpy(marker, cp, sp - cp);
221 marker[sp - cp] = '\0';
222 if (strcmp(marker, CA_MARKER) == 0)
223 ret = MRK_CA;
224 else if (strcmp(marker, REVOKE_MARKER) == 0)
225 ret = MRK_REVOKE;
226 else
227 return MRK_ERROR;
228
229 /* Skip past marker and any whitespace that follows it */
230 cp = sp;
231 for (; *cp == ' ' || *cp == '\t'; cp++)
232 ;
233 }
234 *cpp = cp;
235 return ret;
236}
237
Damien Millerd925dcd2010-12-01 12:21:51 +1100238struct hostkeys *
239init_hostkeys(void)
240{
241 struct hostkeys *ret = xcalloc(1, sizeof(*ret));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000242
Damien Millerd925dcd2010-12-01 12:21:51 +1100243 ret->entries = NULL;
244 return ret;
245}
246
247void
248load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000249{
Damien Miller95def091999-11-25 00:26:21 +1100250 FILE *f;
251 char line[8192];
Damien Millerd925dcd2010-12-01 12:21:51 +1100252 u_long linenum = 0, num_loaded = 0;
Damien Millere1776152005-03-01 21:47:37 +1100253 char *cp, *cp2, *hashed_host;
Damien Millerd925dcd2010-12-01 12:21:51 +1100254 HostkeyMarker marker;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000255 struct sshkey *key;
256 u_int kbits;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000257
Damien Millerd925dcd2010-12-01 12:21:51 +1100258 if ((f = fopen(path, "r")) == NULL)
259 return;
260 debug3("%s: loading entries for host \"%.100s\" from file \"%s\"",
261 __func__, host, path);
262 while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100263 cp = line;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000264
Damien Miller5428f641999-11-25 11:54:57 +1100265 /* Skip any leading whitespace, comments and empty lines. */
266 for (; *cp == ' ' || *cp == '\t'; cp++)
267 ;
Damien Miller95def091999-11-25 00:26:21 +1100268 if (!*cp || *cp == '#' || *cp == '\n')
269 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000270
Damien Millerd925dcd2010-12-01 12:21:51 +1100271 if ((marker = check_markers(&cp)) == MRK_ERROR) {
272 verbose("%s: invalid marker at %s:%lu",
273 __func__, path, linenum);
Damien Miller1aed65e2010-03-04 21:53:35 +1100274 continue;
Damien Millerd925dcd2010-12-01 12:21:51 +1100275 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100276
Damien Miller95def091999-11-25 00:26:21 +1100277 /* Find the end of the host name portion. */
Damien Miller5428f641999-11-25 11:54:57 +1100278 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
279 ;
Damien Miller7e8e8201999-11-16 13:37:16 +1100280
Damien Miller95def091999-11-25 00:26:21 +1100281 /* Check if the host name matches. */
Damien Millere1776152005-03-01 21:47:37 +1100282 if (match_hostname(host, cp, (u_int) (cp2 - cp)) != 1) {
283 if (*cp != HASH_DELIM)
284 continue;
285 hashed_host = host_hash(host, cp, (u_int) (cp2 - cp));
286 if (hashed_host == NULL) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100287 debug("Invalid hashed host line %lu of %s",
288 linenum, path);
Damien Millere1776152005-03-01 21:47:37 +1100289 continue;
290 }
291 if (strncmp(hashed_host, cp, (u_int) (cp2 - cp)) != 0)
292 continue;
293 }
Damien Miller95def091999-11-25 00:26:21 +1100294
295 /* Got a match. Skip host name. */
296 cp = cp2;
297
Damien Miller5428f641999-11-25 11:54:57 +1100298 /*
299 * Extract the key from the line. This will skip any leading
300 * whitespace. Ignore badly formatted lines.
301 */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000302 if ((key = sshkey_new(KEY_UNSPEC)) == NULL) {
303 error("%s: sshkey_new failed", __func__);
304 break;
305 }
Damien Millerd925dcd2010-12-01 12:21:51 +1100306 if (!hostfile_read_key(&cp, &kbits, key)) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000307 sshkey_free(key);
Damien Miller1f0311c2014-05-15 14:24:09 +1000308#ifdef WITH_SSH1
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000309 if ((key = sshkey_new(KEY_RSA1)) == NULL) {
310 error("%s: sshkey_new failed", __func__);
311 break;
312 }
Damien Millerd925dcd2010-12-01 12:21:51 +1100313 if (!hostfile_read_key(&cp, &kbits, key)) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000314 sshkey_free(key);
Damien Millerd925dcd2010-12-01 12:21:51 +1100315 continue;
Damien Miller6db780e2006-03-26 13:52:20 +1100316 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000317#else
318 continue;
319#endif
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000320 }
Damien Millerd925dcd2010-12-01 12:21:51 +1100321 if (!hostfile_check_key(kbits, key, host, path, linenum))
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000322 continue;
323
Damien Millerd925dcd2010-12-01 12:21:51 +1100324 debug3("%s: found %skey type %s in file %s:%lu", __func__,
325 marker == MRK_NONE ? "" :
326 (marker == MRK_CA ? "ca " : "revoked "),
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000327 sshkey_type(key), path, linenum);
Damien Millerd925dcd2010-12-01 12:21:51 +1100328 hostkeys->entries = xrealloc(hostkeys->entries,
329 hostkeys->num_entries + 1, sizeof(*hostkeys->entries));
330 hostkeys->entries[hostkeys->num_entries].host = xstrdup(host);
331 hostkeys->entries[hostkeys->num_entries].file = xstrdup(path);
332 hostkeys->entries[hostkeys->num_entries].line = linenum;
333 hostkeys->entries[hostkeys->num_entries].key = key;
334 hostkeys->entries[hostkeys->num_entries].marker = marker;
335 hostkeys->num_entries++;
336 num_loaded++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337 }
Damien Millerd925dcd2010-12-01 12:21:51 +1100338 debug3("%s: loaded %lu keys", __func__, num_loaded);
Darren Tucker094f1e92010-12-05 09:03:31 +1100339 fclose(f);
Damien Millerd925dcd2010-12-01 12:21:51 +1100340 return;
djm@openbsd.org6fdcaeb2014-10-20 03:43:01 +0000341}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342
Damien Millerd925dcd2010-12-01 12:21:51 +1100343void
344free_hostkeys(struct hostkeys *hostkeys)
345{
346 u_int i;
347
348 for (i = 0; i < hostkeys->num_entries; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000349 free(hostkeys->entries[i].host);
350 free(hostkeys->entries[i].file);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000351 sshkey_free(hostkeys->entries[i].key);
Damien Miller1d2c4562014-02-04 11:18:20 +1100352 explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
Damien Millerd925dcd2010-12-01 12:21:51 +1100353 }
Darren Tuckera627d422013-06-02 07:31:17 +1000354 free(hostkeys->entries);
Damien Miller1d2c4562014-02-04 11:18:20 +1100355 explicit_bzero(hostkeys, sizeof(*hostkeys));
Darren Tuckera627d422013-06-02 07:31:17 +1000356 free(hostkeys);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000357}
358
Damien Millerd925dcd2010-12-01 12:21:51 +1100359static int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000360check_key_not_revoked(struct hostkeys *hostkeys, struct sshkey *k)
Damien Millerd925dcd2010-12-01 12:21:51 +1100361{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000362 int is_cert = sshkey_is_cert(k);
Damien Millerd925dcd2010-12-01 12:21:51 +1100363 u_int i;
364
365 for (i = 0; i < hostkeys->num_entries; i++) {
366 if (hostkeys->entries[i].marker != MRK_REVOKE)
367 continue;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000368 if (sshkey_equal_public(k, hostkeys->entries[i].key))
Damien Millerd925dcd2010-12-01 12:21:51 +1100369 return -1;
370 if (is_cert &&
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000371 sshkey_equal_public(k->cert->signature_key,
Damien Millerd925dcd2010-12-01 12:21:51 +1100372 hostkeys->entries[i].key))
373 return -1;
374 }
375 return 0;
376}
377
378/*
379 * Match keys against a specified key, or look one up by key type.
380 *
381 * If looking for a keytype (key == NULL) and one is found then return
382 * HOST_FOUND, otherwise HOST_NEW.
383 *
384 * If looking for a key (key != NULL):
385 * 1. If the key is a cert and a matching CA is found, return HOST_OK
386 * 2. If the key is not a cert and a matching key is found, return HOST_OK
387 * 3. If no key matches but a key with a different type is found, then
388 * return HOST_CHANGED
389 * 4. If no matching keys are found, then return HOST_NEW.
390 *
391 * Finally, check any found key is not revoked.
392 */
393static HostStatus
394check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000395 struct sshkey *k, int keytype, const struct hostkey_entry **found)
Damien Millerd925dcd2010-12-01 12:21:51 +1100396{
397 u_int i;
398 HostStatus end_return = HOST_NEW;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000399 int want_cert = sshkey_is_cert(k);
Damien Millerd925dcd2010-12-01 12:21:51 +1100400 HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE;
401 int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2;
402
403 if (found != NULL)
404 *found = NULL;
405
406 for (i = 0; i < hostkeys->num_entries; i++) {
407 if (proto == 1 && hostkeys->entries[i].key->type != KEY_RSA1)
408 continue;
409 if (proto == 2 && hostkeys->entries[i].key->type == KEY_RSA1)
410 continue;
411 if (hostkeys->entries[i].marker != want_marker)
412 continue;
413 if (k == NULL) {
414 if (hostkeys->entries[i].key->type != keytype)
415 continue;
416 end_return = HOST_FOUND;
417 if (found != NULL)
418 *found = hostkeys->entries + i;
419 k = hostkeys->entries[i].key;
420 break;
421 }
422 if (want_cert) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000423 if (sshkey_equal_public(k->cert->signature_key,
Damien Millerd925dcd2010-12-01 12:21:51 +1100424 hostkeys->entries[i].key)) {
425 /* A matching CA exists */
426 end_return = HOST_OK;
427 if (found != NULL)
428 *found = hostkeys->entries + i;
429 break;
430 }
431 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000432 if (sshkey_equal(k, hostkeys->entries[i].key)) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100433 end_return = HOST_OK;
434 if (found != NULL)
435 *found = hostkeys->entries + i;
436 break;
437 }
438 /* A non-maching key exists */
439 end_return = HOST_CHANGED;
440 if (found != NULL)
441 *found = hostkeys->entries + i;
442 }
443 }
444 if (check_key_not_revoked(hostkeys, k) != 0) {
445 end_return = HOST_REVOKED;
446 if (found != NULL)
447 *found = NULL;
448 }
449 return end_return;
450}
djm@openbsd.org6fdcaeb2014-10-20 03:43:01 +0000451
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000452HostStatus
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000453check_key_in_hostkeys(struct hostkeys *hostkeys, struct sshkey *key,
Damien Millerd925dcd2010-12-01 12:21:51 +1100454 const struct hostkey_entry **found)
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000455{
456 if (key == NULL)
457 fatal("no key to look up");
Damien Millerd925dcd2010-12-01 12:21:51 +1100458 return check_hostkeys_by_key_or_type(hostkeys, key, 0, found);
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000459}
460
461int
Damien Millerd925dcd2010-12-01 12:21:51 +1100462lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
463 const struct hostkey_entry **found)
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000464{
Damien Millerd925dcd2010-12-01 12:21:51 +1100465 return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype,
466 found) == HOST_FOUND);
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000467}
468
Damien Miller5428f641999-11-25 11:54:57 +1100469/*
470 * Appends an entry to the host file. Returns false if the entry could not
471 * be appended.
472 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473
474int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000475add_host_to_hostfile(const char *filename, const char *host,
476 const struct sshkey *key, int store_hash)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477{
Damien Miller95def091999-11-25 00:26:21 +1100478 FILE *f;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000479 int r, success = 0;
Darren Tucker40858532005-08-02 17:07:07 +1000480 char *hashed_host = NULL;
Damien Millere1776152005-03-01 21:47:37 +1100481
Damien Miller450a7a12000-03-26 13:04:51 +1000482 if (key == NULL)
Damien Millereba71ba2000-04-29 23:57:08 +1000483 return 1; /* XXX ? */
Damien Miller95def091999-11-25 00:26:21 +1100484 f = fopen(filename, "a");
485 if (!f)
486 return 0;
Damien Millere1776152005-03-01 21:47:37 +1100487
488 if (store_hash) {
489 if ((hashed_host = host_hash(host, NULL, 0)) == NULL) {
490 error("add_host_to_hostfile: host_hash failed");
491 fclose(f);
492 return 0;
493 }
494 }
495 fprintf(f, "%s ", store_hash ? hashed_host : host);
496
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000497 if ((r = sshkey_write(key, f)) != 0) {
498 error("%s: saving key in %s failed: %s",
499 __func__, filename, ssh_err(r));
500 } else
Damien Miller450a7a12000-03-26 13:04:51 +1000501 success = 1;
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000502 fputc('\n', f);
Damien Miller95def091999-11-25 00:26:21 +1100503 fclose(f);
Damien Miller450a7a12000-03-26 13:04:51 +1000504 return success;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505}
djm@openbsd.orgc29811c2015-01-18 21:40:23 +0000506
507static int
508match_maybe_hashed(const char *host, const char *names, int *was_hashed)
509{
510 int hashed = *names == HASH_DELIM;
511 const char *hashed_host;
512 size_t nlen = strlen(names);
513
514 if (was_hashed != NULL)
515 *was_hashed = hashed;
516 if (hashed) {
517 if ((hashed_host = host_hash(host, names, nlen)) == NULL)
518 return -1;
519 return nlen == strlen(hashed_host) &&
520 strncmp(hashed_host, names, nlen) == 0;
521 }
522 return match_hostname(host, names, nlen) == 1;
523}
524
525int
526hostkeys_foreach(const char *path, hostkeys_foreach_fn *callback, void *ctx,
527 const char *host, u_int options)
528{
529 FILE *f;
530 char line[8192], oline[8192];
531 u_long linenum = 0;
532 char *cp, *cp2;
533 u_int kbits;
534 int s, r = 0;
535 struct hostkey_foreach_line lineinfo;
536
537 memset(&lineinfo, 0, sizeof(lineinfo));
538 if (host == NULL && (options & HKF_WANT_MATCH_HOST) != 0)
539 return SSH_ERR_INVALID_ARGUMENT;
540 if ((f = fopen(path, "r")) == NULL)
541 return SSH_ERR_SYSTEM_ERROR;
542
543 debug3("%s: reading file \"%s\"", __func__, path);
544 while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) {
545 line[strcspn(line, "\n")] = '\0';
546 strlcpy(oline, line, sizeof(oline));
547
548 sshkey_free(lineinfo.key);
549 memset(&lineinfo, 0, sizeof(lineinfo));
550 lineinfo.path = path;
551 lineinfo.linenum = linenum;
552 lineinfo.line = oline;
553 lineinfo.status = HKF_STATUS_OK;
554
555 /* Skip any leading whitespace, comments and empty lines. */
556 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
557 ;
558 if (!*cp || *cp == '#' || *cp == '\n') {
559 if ((options & HKF_WANT_MATCH_HOST) == 0) {
560 lineinfo.status = HKF_STATUS_COMMENT;
561 if ((r = callback(&lineinfo, ctx)) != 0)
562 break;
563 }
564 continue;
565 }
566
567 if ((lineinfo.marker = check_markers(&cp)) == MRK_ERROR) {
568 verbose("%s: invalid marker at %s:%lu",
569 __func__, path, linenum);
570 if ((options & HKF_WANT_MATCH_HOST) == 0)
571 goto bad;
572 continue;
573 }
574
575 /* Find the end of the host name portion. */
576 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
577 ;
578 lineinfo.hosts = cp;
579 *cp2++ = '\0';
580
581 /* Check if the host name matches. */
582 if (host != NULL) {
583 s = match_maybe_hashed(host, lineinfo.hosts,
584 &lineinfo.was_hashed);
585 if (s == 1)
586 lineinfo.status = HKF_STATUS_HOST_MATCHED;
587 else if ((options & HKF_WANT_MATCH_HOST) != 0)
588 continue;
589 else if (s == -1) {
590 debug2("%s: %s:%ld: bad host hash \"%.32s\"",
591 __func__, path, linenum, lineinfo.hosts);
592 goto bad;
593 }
594 }
595
596 /* Got a match. Skip host name and any following whitespace */
597 for (; *cp2 == ' ' || *cp2 == '\t'; cp2++)
598 ;
599 if (*cp2 == '\0' || *cp2 == '#') {
600 debug2("%s:%ld: truncated before key", path, linenum);
601 goto bad;
602 }
603 lineinfo.rawkey = cp = cp2;
604
605 if ((options & HKF_WANT_PARSE_KEY) != 0) {
606 /*
607 * Extract the key from the line. This will skip
608 * any leading whitespace. Ignore badly formatted
609 * lines.
610 */
611 if ((lineinfo.key = sshkey_new(KEY_UNSPEC)) == NULL) {
612 error("%s: sshkey_new failed", __func__);
613 return SSH_ERR_ALLOC_FAIL;
614 }
615 if (!hostfile_read_key(&cp, &kbits, lineinfo.key)) {
616#ifdef WITH_SSH1
617 sshkey_free(lineinfo.key);
618 lineinfo.key = sshkey_new(KEY_RSA1);
619 if (lineinfo.key == NULL) {
620 error("%s: sshkey_new fail", __func__);
621 return SSH_ERR_ALLOC_FAIL;
622 }
623 if (!hostfile_read_key(&cp, &kbits,
624 lineinfo.key))
625 goto bad;
626#else
627 goto bad;
628#endif
629 }
630 if (!hostfile_check_key(kbits, lineinfo.key, host,
631 path, linenum)) {
632 bad:
633 lineinfo.status = HKF_STATUS_INVALID;
634 if ((r = callback(&lineinfo, ctx)) != 0)
635 break;
636 continue;
637 }
638 }
639 if ((r = callback(&lineinfo, ctx)) != 0)
640 break;
641 }
642 sshkey_free(lineinfo.key);
643 fclose(f);
644 return r;
645}