blob: 40dbbd478bcb43ad0a48745d447e9c9a38c2611e [file] [log] [blame]
djm@openbsd.org1129dcf2015-01-15 09:40:00 +00001/* $OpenBSD: hostfile.c,v 1.59 2015/01/15 09:40:00 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
Damien Millere7a1e5c2006-08-05 11:34:19 +100045#include <resolv.h>
Damien Millerded319c2006-09-01 15:38:36 +100046#include <stdarg.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100047#include <stdio.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100048#include <stdlib.h>
49#include <string.h>
Damien Miller86687062014-07-02 15:28:02 +100050#include <stdarg.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100051
Damien Millerd7834352006-08-05 12:39:39 +100052#include "xmalloc.h"
Damien Miller450a7a12000-03-26 13:04:51 +100053#include "match.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000054#include "sshkey.h"
Damien Miller450a7a12000-03-26 13:04:51 +100055#include "hostfile.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000056#include "log.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110057#include "misc.h"
djm@openbsd.org1129dcf2015-01-15 09:40:00 +000058#include "ssherr.h"
Damien Millerb3051d02014-01-10 10:58:53 +110059#include "digest.h"
Damien Miller4e8d9372014-02-04 11:02:42 +110060#include "hmac.h"
Damien Millerd925dcd2010-12-01 12:21:51 +110061
62struct hostkeys {
63 struct hostkey_entry *entries;
64 u_int num_entries;
65};
Damien Millere1776152005-03-01 21:47:37 +110066
67static int
Damien Millerce986542013-07-18 16:12:44 +100068extract_salt(const char *s, u_int l, u_char *salt, size_t salt_len)
Damien Millere1776152005-03-01 21:47:37 +110069{
70 char *p, *b64salt;
71 u_int b64len;
72 int ret;
73
74 if (l < sizeof(HASH_MAGIC) - 1) {
75 debug2("extract_salt: string too short");
76 return (-1);
77 }
78 if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) {
79 debug2("extract_salt: invalid magic identifier");
80 return (-1);
81 }
82 s += sizeof(HASH_MAGIC) - 1;
83 l -= sizeof(HASH_MAGIC) - 1;
84 if ((p = memchr(s, HASH_DELIM, l)) == NULL) {
85 debug2("extract_salt: missing salt termination character");
86 return (-1);
87 }
88
89 b64len = p - s;
90 /* Sanity check */
91 if (b64len == 0 || b64len > 1024) {
92 debug2("extract_salt: bad encoded salt length %u", b64len);
93 return (-1);
94 }
95 b64salt = xmalloc(1 + b64len);
96 memcpy(b64salt, s, b64len);
97 b64salt[b64len] = '\0';
98
99 ret = __b64_pton(b64salt, salt, salt_len);
Darren Tuckera627d422013-06-02 07:31:17 +1000100 free(b64salt);
Damien Millere1776152005-03-01 21:47:37 +1100101 if (ret == -1) {
102 debug2("extract_salt: salt decode error");
103 return (-1);
104 }
Damien Miller4e8d9372014-02-04 11:02:42 +1100105 if (ret != (int)ssh_hmac_bytes(SSH_DIGEST_SHA1)) {
106 debug2("extract_salt: expected salt len %zd, got %d",
107 ssh_hmac_bytes(SSH_DIGEST_SHA1), ret);
Damien Millere1776152005-03-01 21:47:37 +1100108 return (-1);
109 }
Darren Tucker47eede72005-03-14 23:08:12 +1100110
Damien Millere1776152005-03-01 21:47:37 +1100111 return (0);
112}
113
114char *
115host_hash(const char *host, const char *name_from_hostfile, u_int src_len)
116{
Damien Miller4e8d9372014-02-04 11:02:42 +1100117 struct ssh_hmac_ctx *ctx;
Damien Millerce986542013-07-18 16:12:44 +1000118 u_char salt[256], result[256];
119 char uu_salt[512], uu_result[512];
Damien Millere1776152005-03-01 21:47:37 +1100120 static char encoded[1024];
121 u_int i, len;
122
Damien Miller4e8d9372014-02-04 11:02:42 +1100123 len = ssh_digest_bytes(SSH_DIGEST_SHA1);
Damien Millere1776152005-03-01 21:47:37 +1100124
125 if (name_from_hostfile == NULL) {
126 /* Create new salt */
127 for (i = 0; i < len; i++)
128 salt[i] = arc4random();
129 } else {
130 /* Extract salt from known host entry */
131 if (extract_salt(name_from_hostfile, src_len, salt,
132 sizeof(salt)) == -1)
133 return (NULL);
134 }
135
Damien Miller4e8d9372014-02-04 11:02:42 +1100136 if ((ctx = ssh_hmac_start(SSH_DIGEST_SHA1)) == NULL ||
137 ssh_hmac_init(ctx, salt, len) < 0 ||
138 ssh_hmac_update(ctx, host, strlen(host)) < 0 ||
139 ssh_hmac_final(ctx, result, sizeof(result)))
140 fatal("%s: ssh_hmac failed", __func__);
141 ssh_hmac_free(ctx);
Damien Millere1776152005-03-01 21:47:37 +1100142
Darren Tucker47eede72005-03-14 23:08:12 +1100143 if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 ||
Damien Millere1776152005-03-01 21:47:37 +1100144 __b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1)
Damien Miller4e8d9372014-02-04 11:02:42 +1100145 fatal("%s: __b64_ntop failed", __func__);
Damien Millere1776152005-03-01 21:47:37 +1100146
147 snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt,
148 HASH_DELIM, uu_result);
149
150 return (encoded);
151}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152
Damien Miller5428f641999-11-25 11:54:57 +1100153/*
Damien Miller450a7a12000-03-26 13:04:51 +1000154 * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the
155 * pointer over the key. Skips any whitespace at the beginning and at end.
Damien Miller5428f641999-11-25 11:54:57 +1100156 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157
Damien Miller5b2aea92001-12-21 12:47:09 +1100158int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000159hostfile_read_key(char **cpp, u_int *bitsp, struct sshkey *ret)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160{
Damien Miller95def091999-11-25 00:26:21 +1100161 char *cp;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000162 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163
Damien Miller95def091999-11-25 00:26:21 +1100164 /* Skip leading whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +1100165 for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++)
166 ;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000168 if ((r = sshkey_read(ret, &cp)) != 0)
Damien Miller95def091999-11-25 00:26:21 +1100169 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170
Damien Miller95def091999-11-25 00:26:21 +1100171 /* Skip trailing whitespace. */
Damien Miller5428f641999-11-25 11:54:57 +1100172 for (; *cp == ' ' || *cp == '\t'; cp++)
173 ;
Damien Miller95def091999-11-25 00:26:21 +1100174
175 /* Return results. */
176 *cpp = cp;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000177 if (bitsp != NULL)
178 *bitsp = sshkey_size(ret);
Damien Miller95def091999-11-25 00:26:21 +1100179 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180}
181
Ben Lindstrombba81212001-06-25 05:01:22 +0000182static int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000183hostfile_check_key(int bits, const struct sshkey *key, const char *host,
Damien Millerd925dcd2010-12-01 12:21:51 +1100184 const char *filename, u_long linenum)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185{
Damien Miller1f0311c2014-05-15 14:24:09 +1000186#ifdef WITH_SSH1
Damien Miller0bc1bd82000-11-13 22:57:25 +1100187 if (key == NULL || key->type != KEY_RSA1 || key->rsa == NULL)
Damien Miller450a7a12000-03-26 13:04:51 +1000188 return 1;
189 if (bits != BN_num_bits(key->rsa->n)) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100190 logit("Warning: %s, line %lu: keysize mismatch for host %s: "
Damien Miller450a7a12000-03-26 13:04:51 +1000191 "actual %d vs. announced %d.",
192 filename, linenum, host, BN_num_bits(key->rsa->n), bits);
Damien Millerd925dcd2010-12-01 12:21:51 +1100193 logit("Warning: replace %d with %d in %s, line %lu.",
Damien Miller450a7a12000-03-26 13:04:51 +1000194 bits, BN_num_bits(key->rsa->n), filename, linenum);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000195 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000196#endif
Damien Miller450a7a12000-03-26 13:04:51 +1000197 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000198}
199
Damien Millerd925dcd2010-12-01 12:21:51 +1100200static HostkeyMarker
Damien Miller1aed65e2010-03-04 21:53:35 +1100201check_markers(char **cpp)
202{
203 char marker[32], *sp, *cp = *cpp;
204 int ret = MRK_NONE;
205
206 while (*cp == '@') {
207 /* Only one marker is allowed */
208 if (ret != MRK_NONE)
209 return MRK_ERROR;
210 /* Markers are terminated by whitespace */
211 if ((sp = strchr(cp, ' ')) == NULL &&
212 (sp = strchr(cp, '\t')) == NULL)
213 return MRK_ERROR;
214 /* Extract marker for comparison */
215 if (sp <= cp + 1 || sp >= cp + sizeof(marker))
216 return MRK_ERROR;
217 memcpy(marker, cp, sp - cp);
218 marker[sp - cp] = '\0';
219 if (strcmp(marker, CA_MARKER) == 0)
220 ret = MRK_CA;
221 else if (strcmp(marker, REVOKE_MARKER) == 0)
222 ret = MRK_REVOKE;
223 else
224 return MRK_ERROR;
225
226 /* Skip past marker and any whitespace that follows it */
227 cp = sp;
228 for (; *cp == ' ' || *cp == '\t'; cp++)
229 ;
230 }
231 *cpp = cp;
232 return ret;
233}
234
Damien Millerd925dcd2010-12-01 12:21:51 +1100235struct hostkeys *
236init_hostkeys(void)
237{
238 struct hostkeys *ret = xcalloc(1, sizeof(*ret));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000239
Damien Millerd925dcd2010-12-01 12:21:51 +1100240 ret->entries = NULL;
241 return ret;
242}
243
244void
245load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000246{
Damien Miller95def091999-11-25 00:26:21 +1100247 FILE *f;
248 char line[8192];
Damien Millerd925dcd2010-12-01 12:21:51 +1100249 u_long linenum = 0, num_loaded = 0;
Damien Millere1776152005-03-01 21:47:37 +1100250 char *cp, *cp2, *hashed_host;
Damien Millerd925dcd2010-12-01 12:21:51 +1100251 HostkeyMarker marker;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000252 struct sshkey *key;
253 u_int kbits;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000254
Damien Millerd925dcd2010-12-01 12:21:51 +1100255 if ((f = fopen(path, "r")) == NULL)
256 return;
257 debug3("%s: loading entries for host \"%.100s\" from file \"%s\"",
258 __func__, host, path);
259 while (read_keyfile_line(f, path, line, sizeof(line), &linenum) == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100260 cp = line;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000261
Damien Miller5428f641999-11-25 11:54:57 +1100262 /* Skip any leading whitespace, comments and empty lines. */
263 for (; *cp == ' ' || *cp == '\t'; cp++)
264 ;
Damien Miller95def091999-11-25 00:26:21 +1100265 if (!*cp || *cp == '#' || *cp == '\n')
266 continue;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267
Damien Millerd925dcd2010-12-01 12:21:51 +1100268 if ((marker = check_markers(&cp)) == MRK_ERROR) {
269 verbose("%s: invalid marker at %s:%lu",
270 __func__, path, linenum);
Damien Miller1aed65e2010-03-04 21:53:35 +1100271 continue;
Damien Millerd925dcd2010-12-01 12:21:51 +1100272 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100273
Damien Miller95def091999-11-25 00:26:21 +1100274 /* Find the end of the host name portion. */
Damien Miller5428f641999-11-25 11:54:57 +1100275 for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++)
276 ;
Damien Miller7e8e8201999-11-16 13:37:16 +1100277
Damien Miller95def091999-11-25 00:26:21 +1100278 /* Check if the host name matches. */
Damien Millere1776152005-03-01 21:47:37 +1100279 if (match_hostname(host, cp, (u_int) (cp2 - cp)) != 1) {
280 if (*cp != HASH_DELIM)
281 continue;
282 hashed_host = host_hash(host, cp, (u_int) (cp2 - cp));
283 if (hashed_host == NULL) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100284 debug("Invalid hashed host line %lu of %s",
285 linenum, path);
Damien Millere1776152005-03-01 21:47:37 +1100286 continue;
287 }
288 if (strncmp(hashed_host, cp, (u_int) (cp2 - cp)) != 0)
289 continue;
290 }
Damien Miller95def091999-11-25 00:26:21 +1100291
292 /* Got a match. Skip host name. */
293 cp = cp2;
294
Damien Miller5428f641999-11-25 11:54:57 +1100295 /*
296 * Extract the key from the line. This will skip any leading
297 * whitespace. Ignore badly formatted lines.
298 */
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000299 if ((key = sshkey_new(KEY_UNSPEC)) == NULL) {
300 error("%s: sshkey_new failed", __func__);
301 break;
302 }
Damien Millerd925dcd2010-12-01 12:21:51 +1100303 if (!hostfile_read_key(&cp, &kbits, key)) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000304 sshkey_free(key);
Damien Miller1f0311c2014-05-15 14:24:09 +1000305#ifdef WITH_SSH1
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000306 if ((key = sshkey_new(KEY_RSA1)) == NULL) {
307 error("%s: sshkey_new failed", __func__);
308 break;
309 }
Damien Millerd925dcd2010-12-01 12:21:51 +1100310 if (!hostfile_read_key(&cp, &kbits, key)) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000311 sshkey_free(key);
Damien Millerd925dcd2010-12-01 12:21:51 +1100312 continue;
Damien Miller6db780e2006-03-26 13:52:20 +1100313 }
Damien Miller1f0311c2014-05-15 14:24:09 +1000314#else
315 continue;
316#endif
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000317 }
Damien Millerd925dcd2010-12-01 12:21:51 +1100318 if (!hostfile_check_key(kbits, key, host, path, linenum))
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000319 continue;
320
Damien Millerd925dcd2010-12-01 12:21:51 +1100321 debug3("%s: found %skey type %s in file %s:%lu", __func__,
322 marker == MRK_NONE ? "" :
323 (marker == MRK_CA ? "ca " : "revoked "),
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000324 sshkey_type(key), path, linenum);
Damien Millerd925dcd2010-12-01 12:21:51 +1100325 hostkeys->entries = xrealloc(hostkeys->entries,
326 hostkeys->num_entries + 1, sizeof(*hostkeys->entries));
327 hostkeys->entries[hostkeys->num_entries].host = xstrdup(host);
328 hostkeys->entries[hostkeys->num_entries].file = xstrdup(path);
329 hostkeys->entries[hostkeys->num_entries].line = linenum;
330 hostkeys->entries[hostkeys->num_entries].key = key;
331 hostkeys->entries[hostkeys->num_entries].marker = marker;
332 hostkeys->num_entries++;
333 num_loaded++;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000334 }
Damien Millerd925dcd2010-12-01 12:21:51 +1100335 debug3("%s: loaded %lu keys", __func__, num_loaded);
Darren Tucker094f1e92010-12-05 09:03:31 +1100336 fclose(f);
Damien Millerd925dcd2010-12-01 12:21:51 +1100337 return;
djm@openbsd.org6fdcaeb2014-10-20 03:43:01 +0000338}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000339
Damien Millerd925dcd2010-12-01 12:21:51 +1100340void
341free_hostkeys(struct hostkeys *hostkeys)
342{
343 u_int i;
344
345 for (i = 0; i < hostkeys->num_entries; i++) {
Darren Tuckera627d422013-06-02 07:31:17 +1000346 free(hostkeys->entries[i].host);
347 free(hostkeys->entries[i].file);
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000348 sshkey_free(hostkeys->entries[i].key);
Damien Miller1d2c4562014-02-04 11:18:20 +1100349 explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries));
Damien Millerd925dcd2010-12-01 12:21:51 +1100350 }
Darren Tuckera627d422013-06-02 07:31:17 +1000351 free(hostkeys->entries);
Damien Miller1d2c4562014-02-04 11:18:20 +1100352 explicit_bzero(hostkeys, sizeof(*hostkeys));
Darren Tuckera627d422013-06-02 07:31:17 +1000353 free(hostkeys);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000354}
355
Damien Millerd925dcd2010-12-01 12:21:51 +1100356static int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000357check_key_not_revoked(struct hostkeys *hostkeys, struct sshkey *k)
Damien Millerd925dcd2010-12-01 12:21:51 +1100358{
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000359 int is_cert = sshkey_is_cert(k);
Damien Millerd925dcd2010-12-01 12:21:51 +1100360 u_int i;
361
362 for (i = 0; i < hostkeys->num_entries; i++) {
363 if (hostkeys->entries[i].marker != MRK_REVOKE)
364 continue;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000365 if (sshkey_equal_public(k, hostkeys->entries[i].key))
Damien Millerd925dcd2010-12-01 12:21:51 +1100366 return -1;
367 if (is_cert &&
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000368 sshkey_equal_public(k->cert->signature_key,
Damien Millerd925dcd2010-12-01 12:21:51 +1100369 hostkeys->entries[i].key))
370 return -1;
371 }
372 return 0;
373}
374
375/*
376 * Match keys against a specified key, or look one up by key type.
377 *
378 * If looking for a keytype (key == NULL) and one is found then return
379 * HOST_FOUND, otherwise HOST_NEW.
380 *
381 * If looking for a key (key != NULL):
382 * 1. If the key is a cert and a matching CA is found, return HOST_OK
383 * 2. If the key is not a cert and a matching key is found, return HOST_OK
384 * 3. If no key matches but a key with a different type is found, then
385 * return HOST_CHANGED
386 * 4. If no matching keys are found, then return HOST_NEW.
387 *
388 * Finally, check any found key is not revoked.
389 */
390static HostStatus
391check_hostkeys_by_key_or_type(struct hostkeys *hostkeys,
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000392 struct sshkey *k, int keytype, const struct hostkey_entry **found)
Damien Millerd925dcd2010-12-01 12:21:51 +1100393{
394 u_int i;
395 HostStatus end_return = HOST_NEW;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000396 int want_cert = sshkey_is_cert(k);
Damien Millerd925dcd2010-12-01 12:21:51 +1100397 HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE;
398 int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2;
399
400 if (found != NULL)
401 *found = NULL;
402
403 for (i = 0; i < hostkeys->num_entries; i++) {
404 if (proto == 1 && hostkeys->entries[i].key->type != KEY_RSA1)
405 continue;
406 if (proto == 2 && hostkeys->entries[i].key->type == KEY_RSA1)
407 continue;
408 if (hostkeys->entries[i].marker != want_marker)
409 continue;
410 if (k == NULL) {
411 if (hostkeys->entries[i].key->type != keytype)
412 continue;
413 end_return = HOST_FOUND;
414 if (found != NULL)
415 *found = hostkeys->entries + i;
416 k = hostkeys->entries[i].key;
417 break;
418 }
419 if (want_cert) {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000420 if (sshkey_equal_public(k->cert->signature_key,
Damien Millerd925dcd2010-12-01 12:21:51 +1100421 hostkeys->entries[i].key)) {
422 /* A matching CA exists */
423 end_return = HOST_OK;
424 if (found != NULL)
425 *found = hostkeys->entries + i;
426 break;
427 }
428 } else {
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000429 if (sshkey_equal(k, hostkeys->entries[i].key)) {
Damien Millerd925dcd2010-12-01 12:21:51 +1100430 end_return = HOST_OK;
431 if (found != NULL)
432 *found = hostkeys->entries + i;
433 break;
434 }
435 /* A non-maching key exists */
436 end_return = HOST_CHANGED;
437 if (found != NULL)
438 *found = hostkeys->entries + i;
439 }
440 }
441 if (check_key_not_revoked(hostkeys, k) != 0) {
442 end_return = HOST_REVOKED;
443 if (found != NULL)
444 *found = NULL;
445 }
446 return end_return;
447}
djm@openbsd.org6fdcaeb2014-10-20 03:43:01 +0000448
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000449HostStatus
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000450check_key_in_hostkeys(struct hostkeys *hostkeys, struct sshkey *key,
Damien Millerd925dcd2010-12-01 12:21:51 +1100451 const struct hostkey_entry **found)
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000452{
453 if (key == NULL)
454 fatal("no key to look up");
Damien Millerd925dcd2010-12-01 12:21:51 +1100455 return check_hostkeys_by_key_or_type(hostkeys, key, 0, found);
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000456}
457
458int
Damien Millerd925dcd2010-12-01 12:21:51 +1100459lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype,
460 const struct hostkey_entry **found)
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000461{
Damien Millerd925dcd2010-12-01 12:21:51 +1100462 return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype,
463 found) == HOST_FOUND);
Ben Lindstrom3ed66402002-08-01 01:21:56 +0000464}
465
Damien Miller5428f641999-11-25 11:54:57 +1100466/*
467 * Appends an entry to the host file. Returns false if the entry could not
468 * be appended.
469 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470
471int
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000472add_host_to_hostfile(const char *filename, const char *host,
473 const struct sshkey *key, int store_hash)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474{
Damien Miller95def091999-11-25 00:26:21 +1100475 FILE *f;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000476 int r, success = 0;
Darren Tucker40858532005-08-02 17:07:07 +1000477 char *hashed_host = NULL;
Damien Millere1776152005-03-01 21:47:37 +1100478
Damien Miller450a7a12000-03-26 13:04:51 +1000479 if (key == NULL)
Damien Millereba71ba2000-04-29 23:57:08 +1000480 return 1; /* XXX ? */
Damien Miller95def091999-11-25 00:26:21 +1100481 f = fopen(filename, "a");
482 if (!f)
483 return 0;
Damien Millere1776152005-03-01 21:47:37 +1100484
485 if (store_hash) {
486 if ((hashed_host = host_hash(host, NULL, 0)) == NULL) {
487 error("add_host_to_hostfile: host_hash failed");
488 fclose(f);
489 return 0;
490 }
491 }
492 fprintf(f, "%s ", store_hash ? hashed_host : host);
493
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000494 if ((r = sshkey_write(key, f)) != 0) {
495 error("%s: saving key in %s failed: %s",
496 __func__, filename, ssh_err(r));
497 } else
Damien Miller450a7a12000-03-26 13:04:51 +1000498 success = 1;
djm@openbsd.org1129dcf2015-01-15 09:40:00 +0000499 fputs("\n", f);
Damien Miller95def091999-11-25 00:26:21 +1100500 fclose(f);
Damien Miller450a7a12000-03-26 13:04:51 +1000501 return success;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000502}