dtucker@openbsd.org | ffb1e7e | 2016-02-15 09:47:49 +0000 | [diff] [blame] | 1 | /* $OpenBSD: ssh-keysign.c,v 1.52 2016/02/15 09:47:49 dtucker Exp $ */ |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2002 Markus Friedl. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
| 13 | * |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 16 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 17 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 18 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 19 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 20 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 21 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 23 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 25 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 26 | #include "includes.h" |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 27 | |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 28 | #include <fcntl.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 29 | #ifdef HAVE_PATHS_H |
Damien Miller | 03e2003 | 2006-03-15 11:16:59 +1100 | [diff] [blame] | 30 | #include <paths.h> |
Damien Miller | 6645e7a | 2006-03-15 14:42:54 +1100 | [diff] [blame] | 31 | #endif |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 32 | #include <pwd.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 33 | #include <stdarg.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 34 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 35 | #include <string.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 36 | #include <unistd.h> |
djm@openbsd.org | 452c0b6 | 2015-11-29 22:18:37 +0000 | [diff] [blame] | 37 | #include <errno.h> |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 38 | |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 39 | #ifdef WITH_OPENSSL |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 40 | #include <openssl/evp.h> |
Ben Lindstrom | 43ce2c8 | 2002-07-04 00:17:33 +0000 | [diff] [blame] | 41 | #include <openssl/rand.h> |
| 42 | #include <openssl/rsa.h> |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 43 | #endif |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 44 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 45 | #include "xmalloc.h" |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 46 | #include "log.h" |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 47 | #include "sshkey.h" |
Ben Lindstrom | 5d35a2f | 2002-07-04 00:19:40 +0000 | [diff] [blame] | 48 | #include "ssh.h" |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 49 | #include "ssh2.h" |
| 50 | #include "misc.h" |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 51 | #include "sshbuf.h" |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 52 | #include "authfile.h" |
| 53 | #include "msg.h" |
| 54 | #include "canohost.h" |
| 55 | #include "pathnames.h" |
Ben Lindstrom | 5d35a2f | 2002-07-04 00:19:40 +0000 | [diff] [blame] | 56 | #include "readconf.h" |
Darren Tucker | 25f60a7 | 2004-08-15 17:23:34 +1000 | [diff] [blame] | 57 | #include "uidswap.h" |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 58 | #include "sshkey.h" |
| 59 | #include "ssherr.h" |
Ben Lindstrom | 5d35a2f | 2002-07-04 00:19:40 +0000 | [diff] [blame] | 60 | |
Damien Miller | ade31d7 | 2015-01-27 23:06:23 +1100 | [diff] [blame] | 61 | struct ssh *active_state = NULL; /* XXX needed for linking */ |
| 62 | |
djm@openbsd.org | 452c0b6 | 2015-11-29 22:18:37 +0000 | [diff] [blame] | 63 | extern char *__progname; |
| 64 | |
Damien Miller | 20a8f97 | 2003-05-18 20:50:30 +1000 | [diff] [blame] | 65 | /* XXX readconf.c needs these */ |
| 66 | uid_t original_real_uid; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 67 | |
Ben Lindstrom | 3545352 | 2002-06-07 14:37:00 +0000 | [diff] [blame] | 68 | extern char *__progname; |
Ben Lindstrom | 3545352 | 2002-06-07 14:37:00 +0000 | [diff] [blame] | 69 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 70 | static int |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 71 | valid_request(struct passwd *pw, char *host, struct sshkey **ret, |
| 72 | u_char *data, size_t datalen) |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 73 | { |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 74 | struct sshbuf *b; |
| 75 | struct sshkey *key = NULL; |
| 76 | u_char type, *pkblob; |
| 77 | char *p; |
| 78 | size_t blen, len; |
| 79 | char *pkalg, *luser; |
| 80 | int r, pktype, fail; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 81 | |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 82 | if (ret != NULL) |
| 83 | *ret = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 84 | fail = 0; |
| 85 | |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 86 | if ((b = sshbuf_from(data, datalen)) == NULL) |
| 87 | fatal("%s: sshbuf_from failed", __func__); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 88 | |
Damien Miller | 603e68f | 2006-04-23 12:05:32 +1000 | [diff] [blame] | 89 | /* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */ |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 90 | if ((r = sshbuf_get_string(b, NULL, &len)) != 0) |
| 91 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Damien Miller | 603e68f | 2006-04-23 12:05:32 +1000 | [diff] [blame] | 92 | if (len != 20 && len != 32) |
Ben Lindstrom | a207157 | 2002-06-09 20:01:48 +0000 | [diff] [blame] | 93 | fail++; |
Ben Lindstrom | a207157 | 2002-06-09 20:01:48 +0000 | [diff] [blame] | 94 | |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 95 | if ((r = sshbuf_get_u8(b, &type)) != 0) |
| 96 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 97 | if (type != SSH2_MSG_USERAUTH_REQUEST) |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 98 | fail++; |
| 99 | |
| 100 | /* server user */ |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 101 | if ((r = sshbuf_skip_string(b)) != 0) |
| 102 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 103 | |
| 104 | /* service */ |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 105 | if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0) |
| 106 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 107 | if (strcmp("ssh-connection", p) != 0) |
| 108 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 109 | free(p); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 110 | |
| 111 | /* method */ |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 112 | if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0) |
| 113 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 114 | if (strcmp("hostbased", p) != 0) |
| 115 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 116 | free(p); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 117 | |
| 118 | /* pubkey */ |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 119 | if ((r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 || |
| 120 | (r = sshbuf_get_string(b, &pkblob, &blen)) != 0) |
| 121 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 122 | |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 123 | pktype = sshkey_type_from_name(pkalg); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 124 | if (pktype == KEY_UNSPEC) |
| 125 | fail++; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 126 | else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { |
| 127 | error("%s: bad key blob: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 128 | fail++; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 129 | } else if (key->type != pktype) |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 130 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 131 | free(pkalg); |
| 132 | free(pkblob); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 133 | |
| 134 | /* client host name, handle trailing dot */ |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 135 | if ((r = sshbuf_get_cstring(b, &p, &len)) != 0) |
| 136 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
| 137 | debug2("%s: check expect chost %s got %s", __func__, host, p); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 138 | if (strlen(host) != len - 1) |
| 139 | fail++; |
| 140 | else if (p[len - 1] != '.') |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 141 | fail++; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 142 | else if (strncasecmp(host, p, len - 1) != 0) |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 143 | fail++; |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 144 | free(p); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 145 | |
| 146 | /* local user */ |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 147 | if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0) |
| 148 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 149 | |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 150 | if (strcmp(pw->pw_name, luser) != 0) |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 151 | fail++; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 152 | free(luser); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 153 | |
| 154 | /* end of message */ |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 155 | if (sshbuf_len(b) != 0) |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 156 | fail++; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 157 | sshbuf_free(b); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 158 | |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 159 | debug3("%s: fail %d", __func__, fail); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 160 | |
| 161 | if (fail && key != NULL) |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 162 | sshkey_free(key); |
markus@openbsd.org | 7d4f96f | 2015-03-24 20:09:11 +0000 | [diff] [blame] | 163 | else if (ret != NULL) |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 164 | *ret = key; |
| 165 | |
| 166 | return (fail ? -1 : 0); |
| 167 | } |
| 168 | |
| 169 | int |
| 170 | main(int argc, char **argv) |
| 171 | { |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 172 | struct sshbuf *b; |
Ben Lindstrom | 5d35a2f | 2002-07-04 00:19:40 +0000 | [diff] [blame] | 173 | Options options; |
Damien Miller | 5be9d9e | 2013-12-07 11:24:01 +1100 | [diff] [blame] | 174 | #define NUM_KEYTYPES 4 |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 175 | struct sshkey *keys[NUM_KEYTYPES], *key = NULL; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 176 | struct passwd *pw; |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 177 | int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 178 | u_char *signature, *data, rver; |
Damien Miller | fcd62c0 | 2014-04-20 13:23:21 +1000 | [diff] [blame] | 179 | char *host, *fp; |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 180 | size_t slen, dlen; |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 181 | #ifdef WITH_OPENSSL |
Ben Lindstrom | 43ce2c8 | 2002-07-04 00:17:33 +0000 | [diff] [blame] | 182 | u_int32_t rnd[256]; |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 183 | #endif |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 184 | |
dtucker@openbsd.org | ffb1e7e | 2016-02-15 09:47:49 +0000 | [diff] [blame] | 185 | ssh_malloc_init(); /* must be called before any mallocs */ |
djm@openbsd.org | 452c0b6 | 2015-11-29 22:18:37 +0000 | [diff] [blame] | 186 | if (pledge("stdio rpath getpw dns id", NULL) != 0) |
| 187 | fatal("%s: pledge: %s", __progname, strerror(errno)); |
| 188 | |
Darren Tucker | ce321d8 | 2005-10-03 18:11:24 +1000 | [diff] [blame] | 189 | /* Ensure that stdin and stdout are connected */ |
| 190 | if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2) |
| 191 | exit(1); |
| 192 | /* Leave /dev/null fd iff it is attached to stderr */ |
| 193 | if (fd > 2) |
| 194 | close(fd); |
| 195 | |
Damien Miller | 0588beb | 2011-02-18 09:18:45 +1100 | [diff] [blame] | 196 | i = 0; |
djm@openbsd.org | b1f383d | 2015-07-03 03:56:25 +0000 | [diff] [blame] | 197 | /* XXX This really needs to read sshd_config for the paths */ |
Damien Miller | 0588beb | 2011-02-18 09:18:45 +1100 | [diff] [blame] | 198 | key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY); |
| 199 | key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY); |
Damien Miller | 5be9d9e | 2013-12-07 11:24:01 +1100 | [diff] [blame] | 200 | key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY); |
Damien Miller | 0588beb | 2011-02-18 09:18:45 +1100 | [diff] [blame] | 201 | key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 202 | |
Darren Tucker | 34620d6 | 2004-08-29 16:32:59 +1000 | [diff] [blame] | 203 | original_real_uid = getuid(); /* XXX readconf.c needs this */ |
| 204 | if ((pw = getpwuid(original_real_uid)) == NULL) |
Darren Tucker | 25f60a7 | 2004-08-15 17:23:34 +1000 | [diff] [blame] | 205 | fatal("getpwuid failed"); |
| 206 | pw = pwcopy(pw); |
| 207 | |
| 208 | permanently_set_uid(pw); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 209 | |
Ben Lindstrom | 5a6abda | 2002-06-09 19:41:48 +0000 | [diff] [blame] | 210 | seed_rng(); |
Ben Lindstrom | db41d23 | 2002-06-07 03:11:38 +0000 | [diff] [blame] | 211 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 212 | #ifdef DEBUG_SSH_KEYSIGN |
| 213 | log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 214 | #endif |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 215 | |
Ben Lindstrom | 5d35a2f | 2002-07-04 00:19:40 +0000 | [diff] [blame] | 216 | /* verify that ssh-keysign is enabled by the admin */ |
Ben Lindstrom | 5d35a2f | 2002-07-04 00:19:40 +0000 | [diff] [blame] | 217 | initialize_options(&options); |
djm@openbsd.org | 957fbce | 2014-10-08 22:20:25 +0000 | [diff] [blame] | 218 | (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "", &options, 0); |
Ben Lindstrom | 5d35a2f | 2002-07-04 00:19:40 +0000 | [diff] [blame] | 219 | fill_default_options(&options); |
Ben Lindstrom | b6df73b | 2002-11-09 15:52:31 +0000 | [diff] [blame] | 220 | if (options.enable_ssh_keysign != 1) |
| 221 | fatal("ssh-keysign not enabled in %s", |
Ben Lindstrom | 5d35a2f | 2002-07-04 00:19:40 +0000 | [diff] [blame] | 222 | _PATH_HOST_CONFIG_FILE); |
| 223 | |
Damien Miller | 0588beb | 2011-02-18 09:18:45 +1100 | [diff] [blame] | 224 | for (i = found = 0; i < NUM_KEYTYPES; i++) { |
| 225 | if (key_fd[i] != -1) |
| 226 | found = 1; |
| 227 | } |
| 228 | if (found == 0) |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 229 | fatal("could not open any host key"); |
| 230 | |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 231 | #ifdef WITH_OPENSSL |
Damien Miller | 4314c2b | 2010-09-10 11:12:09 +1000 | [diff] [blame] | 232 | OpenSSL_add_all_algorithms(); |
Damien Miller | fcd62c0 | 2014-04-20 13:23:21 +1000 | [diff] [blame] | 233 | arc4random_buf(rnd, sizeof(rnd)); |
Ben Lindstrom | 43ce2c8 | 2002-07-04 00:17:33 +0000 | [diff] [blame] | 234 | RAND_seed(rnd, sizeof(rnd)); |
Damien Miller | 72ef7c1 | 2015-01-15 02:21:31 +1100 | [diff] [blame] | 235 | #endif |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 236 | |
| 237 | found = 0; |
Damien Miller | 0588beb | 2011-02-18 09:18:45 +1100 | [diff] [blame] | 238 | for (i = 0; i < NUM_KEYTYPES; i++) { |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 239 | keys[i] = NULL; |
| 240 | if (key_fd[i] == -1) |
| 241 | continue; |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 242 | r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC, |
| 243 | NULL, &key, NULL); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 244 | close(key_fd[i]); |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 245 | if (r != 0) |
| 246 | debug("parse key %d: %s", i, ssh_err(r)); |
| 247 | else if (key != NULL) { |
| 248 | keys[i] = key; |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 249 | found = 1; |
djm@openbsd.org | 1195f4c | 2015-01-08 10:14:08 +0000 | [diff] [blame] | 250 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 251 | } |
| 252 | if (!found) |
| 253 | fatal("no hostkey found"); |
| 254 | |
djm@openbsd.org | 452c0b6 | 2015-11-29 22:18:37 +0000 | [diff] [blame] | 255 | if (pledge("stdio dns", NULL) != 0) |
| 256 | fatal("%s: pledge: %s", __progname, strerror(errno)); |
| 257 | |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 258 | if ((b = sshbuf_new()) == NULL) |
djm@openbsd.org | 452c0b6 | 2015-11-29 22:18:37 +0000 | [diff] [blame] | 259 | fatal("%s: sshbuf_new failed", __progname); |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 260 | if (ssh_msg_recv(STDIN_FILENO, b) < 0) |
Damien Miller | 901119b | 2002-10-04 11:10:04 +1000 | [diff] [blame] | 261 | fatal("ssh_msg_recv failed"); |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 262 | if ((r = sshbuf_get_u8(b, &rver)) != 0) |
djm@openbsd.org | 452c0b6 | 2015-11-29 22:18:37 +0000 | [diff] [blame] | 263 | fatal("%s: buffer error: %s", __progname, ssh_err(r)); |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 264 | if (rver != version) |
| 265 | fatal("bad version: received %d, expected %d", rver, version); |
| 266 | if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0) |
djm@openbsd.org | 452c0b6 | 2015-11-29 22:18:37 +0000 | [diff] [blame] | 267 | fatal("%s: buffer error: %s", __progname, ssh_err(r)); |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 268 | if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO) |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 269 | fatal("bad fd"); |
| 270 | if ((host = get_local_name(fd)) == NULL) |
Darren Tucker | daaa450 | 2010-01-13 22:43:33 +1100 | [diff] [blame] | 271 | fatal("cannot get local name for fd"); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 272 | |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 273 | if ((r = sshbuf_get_string(b, &data, &dlen)) != 0) |
djm@openbsd.org | 452c0b6 | 2015-11-29 22:18:37 +0000 | [diff] [blame] | 274 | fatal("%s: buffer error: %s", __progname, ssh_err(r)); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 275 | if (valid_request(pw, host, &key, data, dlen) < 0) |
| 276 | fatal("not a valid request"); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 277 | free(host); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 278 | |
| 279 | found = 0; |
Damien Miller | 0588beb | 2011-02-18 09:18:45 +1100 | [diff] [blame] | 280 | for (i = 0; i < NUM_KEYTYPES; i++) { |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 281 | if (keys[i] != NULL && |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 282 | sshkey_equal_public(key, keys[i])) { |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 283 | found = 1; |
| 284 | break; |
| 285 | } |
| 286 | } |
Damien Miller | fcd62c0 | 2014-04-20 13:23:21 +1000 | [diff] [blame] | 287 | if (!found) { |
djm@openbsd.org | 9ce86c9 | 2015-01-28 22:36:00 +0000 | [diff] [blame] | 288 | if ((fp = sshkey_fingerprint(key, options.fingerprint_hash, |
| 289 | SSH_FP_DEFAULT)) == NULL) |
djm@openbsd.org | 452c0b6 | 2015-11-29 22:18:37 +0000 | [diff] [blame] | 290 | fatal("%s: sshkey_fingerprint failed", __progname); |
Damien Miller | fcd62c0 | 2014-04-20 13:23:21 +1000 | [diff] [blame] | 291 | fatal("no matching hostkey found for key %s %s", |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 292 | sshkey_type(key), fp ? fp : ""); |
Damien Miller | fcd62c0 | 2014-04-20 13:23:21 +1000 | [diff] [blame] | 293 | } |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 294 | |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 295 | if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen, NULL, 0)) |
| 296 | != 0) |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 297 | fatal("sshkey_sign failed: %s", ssh_err(r)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 298 | free(data); |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 299 | |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 300 | /* send reply */ |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 301 | sshbuf_reset(b); |
| 302 | if ((r = sshbuf_put_string(b, signature, slen)) != 0) |
djm@openbsd.org | 452c0b6 | 2015-11-29 22:18:37 +0000 | [diff] [blame] | 303 | fatal("%s: buffer error: %s", __progname, ssh_err(r)); |
djm@openbsd.org | 1129dcf | 2015-01-15 09:40:00 +0000 | [diff] [blame] | 304 | if (ssh_msg_send(STDOUT_FILENO, version, b) == -1) |
Damien Miller | 51bf11f | 2003-11-17 21:20:47 +1100 | [diff] [blame] | 305 | fatal("ssh_msg_send failed"); |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 306 | |
| 307 | return (0); |
| 308 | } |