djm@openbsd.org | 6fdcaeb | 2014-10-20 03:43:01 +0000 | [diff] [blame] | 1 | /* $OpenBSD: hostfile.c,v 1.58 2014/10/20 03:43:01 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * Functions for manipulating the known hosts files. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 8 | * 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 Lindstrom | 4469723 | 2001-07-04 03:32:30 +0000 | [diff] [blame] | 15 | * Copyright (c) 1999, 2000 Markus Friedl. All rights reserved. |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 16 | * 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 37 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 38 | |
| 39 | #include "includes.h" |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 40 | |
Damien Miller | 8ec8c3e | 2006-07-10 20:35:38 +1000 | [diff] [blame] | 41 | #include <sys/types.h> |
| 42 | |
| 43 | #include <netinet/in.h> |
| 44 | |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 45 | #include <resolv.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 46 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 47 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 48 | #include <stdlib.h> |
| 49 | #include <string.h> |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 50 | #include <stdarg.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 51 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 52 | #include "xmalloc.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 53 | #include "match.h" |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 54 | #include "key.h" |
| 55 | #include "hostfile.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 56 | #include "log.h" |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 57 | #include "misc.h" |
Damien Miller | b3051d0 | 2014-01-10 10:58:53 +1100 | [diff] [blame] | 58 | #include "digest.h" |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 59 | #include "hmac.h" |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 60 | |
| 61 | struct hostkeys { |
| 62 | struct hostkey_entry *entries; |
| 63 | u_int num_entries; |
| 64 | }; |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 65 | |
| 66 | static int |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 67 | extract_salt(const char *s, u_int l, u_char *salt, size_t salt_len) |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 68 | { |
| 69 | char *p, *b64salt; |
| 70 | u_int b64len; |
| 71 | int ret; |
| 72 | |
| 73 | if (l < sizeof(HASH_MAGIC) - 1) { |
| 74 | debug2("extract_salt: string too short"); |
| 75 | return (-1); |
| 76 | } |
| 77 | if (strncmp(s, HASH_MAGIC, sizeof(HASH_MAGIC) - 1) != 0) { |
| 78 | debug2("extract_salt: invalid magic identifier"); |
| 79 | return (-1); |
| 80 | } |
| 81 | s += sizeof(HASH_MAGIC) - 1; |
| 82 | l -= sizeof(HASH_MAGIC) - 1; |
| 83 | if ((p = memchr(s, HASH_DELIM, l)) == NULL) { |
| 84 | debug2("extract_salt: missing salt termination character"); |
| 85 | return (-1); |
| 86 | } |
| 87 | |
| 88 | b64len = p - s; |
| 89 | /* Sanity check */ |
| 90 | if (b64len == 0 || b64len > 1024) { |
| 91 | debug2("extract_salt: bad encoded salt length %u", b64len); |
| 92 | return (-1); |
| 93 | } |
| 94 | b64salt = xmalloc(1 + b64len); |
| 95 | memcpy(b64salt, s, b64len); |
| 96 | b64salt[b64len] = '\0'; |
| 97 | |
| 98 | ret = __b64_pton(b64salt, salt, salt_len); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 99 | free(b64salt); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 100 | if (ret == -1) { |
| 101 | debug2("extract_salt: salt decode error"); |
| 102 | return (-1); |
| 103 | } |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 104 | if (ret != (int)ssh_hmac_bytes(SSH_DIGEST_SHA1)) { |
| 105 | debug2("extract_salt: expected salt len %zd, got %d", |
| 106 | ssh_hmac_bytes(SSH_DIGEST_SHA1), ret); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 107 | return (-1); |
| 108 | } |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 109 | |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 110 | return (0); |
| 111 | } |
| 112 | |
| 113 | char * |
| 114 | host_hash(const char *host, const char *name_from_hostfile, u_int src_len) |
| 115 | { |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 116 | struct ssh_hmac_ctx *ctx; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 117 | u_char salt[256], result[256]; |
| 118 | char uu_salt[512], uu_result[512]; |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 119 | static char encoded[1024]; |
| 120 | u_int i, len; |
| 121 | |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 122 | len = ssh_digest_bytes(SSH_DIGEST_SHA1); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 123 | |
| 124 | if (name_from_hostfile == NULL) { |
| 125 | /* Create new salt */ |
| 126 | for (i = 0; i < len; i++) |
| 127 | salt[i] = arc4random(); |
| 128 | } else { |
| 129 | /* Extract salt from known host entry */ |
| 130 | if (extract_salt(name_from_hostfile, src_len, salt, |
| 131 | sizeof(salt)) == -1) |
| 132 | return (NULL); |
| 133 | } |
| 134 | |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 135 | if ((ctx = ssh_hmac_start(SSH_DIGEST_SHA1)) == NULL || |
| 136 | ssh_hmac_init(ctx, salt, len) < 0 || |
| 137 | ssh_hmac_update(ctx, host, strlen(host)) < 0 || |
| 138 | ssh_hmac_final(ctx, result, sizeof(result))) |
| 139 | fatal("%s: ssh_hmac failed", __func__); |
| 140 | ssh_hmac_free(ctx); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 141 | |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 142 | if (__b64_ntop(salt, len, uu_salt, sizeof(uu_salt)) == -1 || |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 143 | __b64_ntop(result, len, uu_result, sizeof(uu_result)) == -1) |
Damien Miller | 4e8d937 | 2014-02-04 11:02:42 +1100 | [diff] [blame] | 144 | fatal("%s: __b64_ntop failed", __func__); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 145 | |
| 146 | snprintf(encoded, sizeof(encoded), "%s%s%c%s", HASH_MAGIC, uu_salt, |
| 147 | HASH_DELIM, uu_result); |
| 148 | |
| 149 | return (encoded); |
| 150 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 151 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 152 | /* |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 153 | * Parses an RSA (number of bits, e, n) or DSA key from a string. Moves the |
| 154 | * pointer over the key. Skips any whitespace at the beginning and at end. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 155 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 156 | |
Damien Miller | 5b2aea9 | 2001-12-21 12:47:09 +1100 | [diff] [blame] | 157 | int |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 158 | hostfile_read_key(char **cpp, int *bitsp, Key *ret) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 159 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 160 | char *cp; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 161 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 162 | /* Skip leading whitespace. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 163 | for (cp = *cpp; *cp == ' ' || *cp == '\t'; cp++) |
| 164 | ; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 165 | |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 166 | if (key_read(ret, &cp) != 1) |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 167 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 168 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 169 | /* Skip trailing whitespace. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 170 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 171 | ; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 172 | |
| 173 | /* Return results. */ |
| 174 | *cpp = cp; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 175 | if (bitsp != NULL) { |
| 176 | if ((*bitsp = key_size(ret)) <= 0) |
| 177 | return 0; |
| 178 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 179 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 180 | } |
| 181 | |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 182 | static int |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 183 | hostfile_check_key(int bits, const Key *key, const char *host, |
| 184 | const char *filename, u_long linenum) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 185 | { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 186 | #ifdef WITH_SSH1 |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 187 | if (key == NULL || key->type != KEY_RSA1 || key->rsa == NULL) |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 188 | return 1; |
| 189 | if (bits != BN_num_bits(key->rsa->n)) { |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 190 | logit("Warning: %s, line %lu: keysize mismatch for host %s: " |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 191 | "actual %d vs. announced %d.", |
| 192 | filename, linenum, host, BN_num_bits(key->rsa->n), bits); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 193 | logit("Warning: replace %d with %d in %s, line %lu.", |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 194 | bits, BN_num_bits(key->rsa->n), filename, linenum); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 195 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 196 | #endif |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 197 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 198 | } |
| 199 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 200 | static HostkeyMarker |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 201 | check_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 Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 235 | struct hostkeys * |
| 236 | init_hostkeys(void) |
| 237 | { |
| 238 | struct hostkeys *ret = xcalloc(1, sizeof(*ret)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 239 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 240 | ret->entries = NULL; |
| 241 | return ret; |
| 242 | } |
| 243 | |
| 244 | void |
| 245 | load_hostkeys(struct hostkeys *hostkeys, const char *host, const char *path) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 246 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 247 | FILE *f; |
| 248 | char line[8192]; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 249 | u_long linenum = 0, num_loaded = 0; |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 250 | char *cp, *cp2, *hashed_host; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 251 | HostkeyMarker marker; |
| 252 | Key *key; |
| 253 | int kbits; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 254 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 255 | 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 260 | cp = line; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 261 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 262 | /* Skip any leading whitespace, comments and empty lines. */ |
| 263 | for (; *cp == ' ' || *cp == '\t'; cp++) |
| 264 | ; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 265 | if (!*cp || *cp == '#' || *cp == '\n') |
| 266 | continue; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 267 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 268 | if ((marker = check_markers(&cp)) == MRK_ERROR) { |
| 269 | verbose("%s: invalid marker at %s:%lu", |
| 270 | __func__, path, linenum); |
Damien Miller | 1aed65e | 2010-03-04 21:53:35 +1100 | [diff] [blame] | 271 | continue; |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 272 | } |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 273 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 274 | /* Find the end of the host name portion. */ |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 275 | for (cp2 = cp; *cp2 && *cp2 != ' ' && *cp2 != '\t'; cp2++) |
| 276 | ; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 277 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 278 | /* Check if the host name matches. */ |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 279 | 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 Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 284 | debug("Invalid hashed host line %lu of %s", |
| 285 | linenum, path); |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 286 | continue; |
| 287 | } |
| 288 | if (strncmp(hashed_host, cp, (u_int) (cp2 - cp)) != 0) |
| 289 | continue; |
| 290 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 291 | |
| 292 | /* Got a match. Skip host name. */ |
| 293 | cp = cp2; |
| 294 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 295 | /* |
| 296 | * Extract the key from the line. This will skip any leading |
| 297 | * whitespace. Ignore badly formatted lines. |
| 298 | */ |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 299 | key = key_new(KEY_UNSPEC); |
| 300 | if (!hostfile_read_key(&cp, &kbits, key)) { |
| 301 | key_free(key); |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 302 | #ifdef WITH_SSH1 |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 303 | key = key_new(KEY_RSA1); |
| 304 | if (!hostfile_read_key(&cp, &kbits, key)) { |
| 305 | key_free(key); |
| 306 | continue; |
Damien Miller | 6db780e | 2006-03-26 13:52:20 +1100 | [diff] [blame] | 307 | } |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 308 | #else |
| 309 | continue; |
| 310 | #endif |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 311 | } |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 312 | if (!hostfile_check_key(kbits, key, host, path, linenum)) |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 313 | continue; |
| 314 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 315 | debug3("%s: found %skey type %s in file %s:%lu", __func__, |
| 316 | marker == MRK_NONE ? "" : |
| 317 | (marker == MRK_CA ? "ca " : "revoked "), |
| 318 | key_type(key), path, linenum); |
| 319 | hostkeys->entries = xrealloc(hostkeys->entries, |
| 320 | hostkeys->num_entries + 1, sizeof(*hostkeys->entries)); |
| 321 | hostkeys->entries[hostkeys->num_entries].host = xstrdup(host); |
| 322 | hostkeys->entries[hostkeys->num_entries].file = xstrdup(path); |
| 323 | hostkeys->entries[hostkeys->num_entries].line = linenum; |
| 324 | hostkeys->entries[hostkeys->num_entries].key = key; |
| 325 | hostkeys->entries[hostkeys->num_entries].marker = marker; |
| 326 | hostkeys->num_entries++; |
| 327 | num_loaded++; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 328 | } |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 329 | debug3("%s: loaded %lu keys", __func__, num_loaded); |
Darren Tucker | 094f1e9 | 2010-12-05 09:03:31 +1100 | [diff] [blame] | 330 | fclose(f); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 331 | return; |
djm@openbsd.org | 6fdcaeb | 2014-10-20 03:43:01 +0000 | [diff] [blame] | 332 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 333 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 334 | void |
| 335 | free_hostkeys(struct hostkeys *hostkeys) |
| 336 | { |
| 337 | u_int i; |
| 338 | |
| 339 | for (i = 0; i < hostkeys->num_entries; i++) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 340 | free(hostkeys->entries[i].host); |
| 341 | free(hostkeys->entries[i].file); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 342 | key_free(hostkeys->entries[i].key); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 343 | explicit_bzero(hostkeys->entries + i, sizeof(*hostkeys->entries)); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 344 | } |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 345 | free(hostkeys->entries); |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 346 | explicit_bzero(hostkeys, sizeof(*hostkeys)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 347 | free(hostkeys); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 348 | } |
| 349 | |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 350 | static int |
| 351 | check_key_not_revoked(struct hostkeys *hostkeys, Key *k) |
| 352 | { |
| 353 | int is_cert = key_is_cert(k); |
| 354 | u_int i; |
| 355 | |
| 356 | for (i = 0; i < hostkeys->num_entries; i++) { |
| 357 | if (hostkeys->entries[i].marker != MRK_REVOKE) |
| 358 | continue; |
| 359 | if (key_equal_public(k, hostkeys->entries[i].key)) |
| 360 | return -1; |
| 361 | if (is_cert && |
| 362 | key_equal_public(k->cert->signature_key, |
| 363 | hostkeys->entries[i].key)) |
| 364 | return -1; |
| 365 | } |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | /* |
| 370 | * Match keys against a specified key, or look one up by key type. |
| 371 | * |
| 372 | * If looking for a keytype (key == NULL) and one is found then return |
| 373 | * HOST_FOUND, otherwise HOST_NEW. |
| 374 | * |
| 375 | * If looking for a key (key != NULL): |
| 376 | * 1. If the key is a cert and a matching CA is found, return HOST_OK |
| 377 | * 2. If the key is not a cert and a matching key is found, return HOST_OK |
| 378 | * 3. If no key matches but a key with a different type is found, then |
| 379 | * return HOST_CHANGED |
| 380 | * 4. If no matching keys are found, then return HOST_NEW. |
| 381 | * |
| 382 | * Finally, check any found key is not revoked. |
| 383 | */ |
| 384 | static HostStatus |
| 385 | check_hostkeys_by_key_or_type(struct hostkeys *hostkeys, |
| 386 | Key *k, int keytype, const struct hostkey_entry **found) |
| 387 | { |
| 388 | u_int i; |
| 389 | HostStatus end_return = HOST_NEW; |
| 390 | int want_cert = key_is_cert(k); |
| 391 | HostkeyMarker want_marker = want_cert ? MRK_CA : MRK_NONE; |
| 392 | int proto = (k ? k->type : keytype) == KEY_RSA1 ? 1 : 2; |
| 393 | |
| 394 | if (found != NULL) |
| 395 | *found = NULL; |
| 396 | |
| 397 | for (i = 0; i < hostkeys->num_entries; i++) { |
| 398 | if (proto == 1 && hostkeys->entries[i].key->type != KEY_RSA1) |
| 399 | continue; |
| 400 | if (proto == 2 && hostkeys->entries[i].key->type == KEY_RSA1) |
| 401 | continue; |
| 402 | if (hostkeys->entries[i].marker != want_marker) |
| 403 | continue; |
| 404 | if (k == NULL) { |
| 405 | if (hostkeys->entries[i].key->type != keytype) |
| 406 | continue; |
| 407 | end_return = HOST_FOUND; |
| 408 | if (found != NULL) |
| 409 | *found = hostkeys->entries + i; |
| 410 | k = hostkeys->entries[i].key; |
| 411 | break; |
| 412 | } |
| 413 | if (want_cert) { |
| 414 | if (key_equal_public(k->cert->signature_key, |
| 415 | hostkeys->entries[i].key)) { |
| 416 | /* A matching CA exists */ |
| 417 | end_return = HOST_OK; |
| 418 | if (found != NULL) |
| 419 | *found = hostkeys->entries + i; |
| 420 | break; |
| 421 | } |
| 422 | } else { |
| 423 | if (key_equal(k, hostkeys->entries[i].key)) { |
| 424 | end_return = HOST_OK; |
| 425 | if (found != NULL) |
| 426 | *found = hostkeys->entries + i; |
| 427 | break; |
| 428 | } |
| 429 | /* A non-maching key exists */ |
| 430 | end_return = HOST_CHANGED; |
| 431 | if (found != NULL) |
| 432 | *found = hostkeys->entries + i; |
| 433 | } |
| 434 | } |
| 435 | if (check_key_not_revoked(hostkeys, k) != 0) { |
| 436 | end_return = HOST_REVOKED; |
| 437 | if (found != NULL) |
| 438 | *found = NULL; |
| 439 | } |
| 440 | return end_return; |
| 441 | } |
djm@openbsd.org | 6fdcaeb | 2014-10-20 03:43:01 +0000 | [diff] [blame] | 442 | |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 443 | HostStatus |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 444 | check_key_in_hostkeys(struct hostkeys *hostkeys, Key *key, |
| 445 | const struct hostkey_entry **found) |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 446 | { |
| 447 | if (key == NULL) |
| 448 | fatal("no key to look up"); |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 449 | return check_hostkeys_by_key_or_type(hostkeys, key, 0, found); |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | int |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 453 | lookup_key_in_hostkeys_by_type(struct hostkeys *hostkeys, int keytype, |
| 454 | const struct hostkey_entry **found) |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 455 | { |
Damien Miller | d925dcd | 2010-12-01 12:21:51 +1100 | [diff] [blame] | 456 | return (check_hostkeys_by_key_or_type(hostkeys, NULL, keytype, |
| 457 | found) == HOST_FOUND); |
Ben Lindstrom | 3ed6640 | 2002-08-01 01:21:56 +0000 | [diff] [blame] | 458 | } |
| 459 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 460 | /* |
| 461 | * Appends an entry to the host file. Returns false if the entry could not |
| 462 | * be appended. |
| 463 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 464 | |
| 465 | int |
Darren Tucker | 47eede7 | 2005-03-14 23:08:12 +1100 | [diff] [blame] | 466 | add_host_to_hostfile(const char *filename, const char *host, const Key *key, |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 467 | int store_hash) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 468 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 469 | FILE *f; |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 470 | int success = 0; |
Darren Tucker | 4085853 | 2005-08-02 17:07:07 +1000 | [diff] [blame] | 471 | char *hashed_host = NULL; |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 472 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 473 | if (key == NULL) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 474 | return 1; /* XXX ? */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 475 | f = fopen(filename, "a"); |
| 476 | if (!f) |
| 477 | return 0; |
Damien Miller | e177615 | 2005-03-01 21:47:37 +1100 | [diff] [blame] | 478 | |
| 479 | if (store_hash) { |
| 480 | if ((hashed_host = host_hash(host, NULL, 0)) == NULL) { |
| 481 | error("add_host_to_hostfile: host_hash failed"); |
| 482 | fclose(f); |
| 483 | return 0; |
| 484 | } |
| 485 | } |
| 486 | fprintf(f, "%s ", store_hash ? hashed_host : host); |
| 487 | |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 488 | if (key_write(key, f)) { |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 489 | success = 1; |
| 490 | } else { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 491 | error("add_host_to_hostfile: saving key in %s failed", filename); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 492 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 493 | fprintf(f, "\n"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 494 | fclose(f); |
Damien Miller | 450a7a1 | 2000-03-26 13:04:51 +1000 | [diff] [blame] | 495 | return success; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 496 | } |