Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 1 | /* $OpenBSD: ssh-keysign.c,v 1.52 2016/02/15 09:47:49 dtucker Exp $ */ |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [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 | */ |
| 25 | |
| 26 | #include "includes.h" |
| 27 | |
| 28 | #include <fcntl.h> |
| 29 | #ifdef HAVE_PATHS_H |
| 30 | #include <paths.h> |
| 31 | #endif |
| 32 | #include <pwd.h> |
| 33 | #include <stdarg.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <string.h> |
| 36 | #include <unistd.h> |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 37 | #include <errno.h> |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 38 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 39 | #ifdef WITH_OPENSSL |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 40 | #include <openssl/evp.h> |
| 41 | #include <openssl/rand.h> |
| 42 | #include <openssl/rsa.h> |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 43 | #endif |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 44 | |
| 45 | #include "xmalloc.h" |
| 46 | #include "log.h" |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 47 | #include "sshkey.h" |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 48 | #include "ssh.h" |
| 49 | #include "ssh2.h" |
| 50 | #include "misc.h" |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 51 | #include "sshbuf.h" |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 52 | #include "authfile.h" |
| 53 | #include "msg.h" |
| 54 | #include "canohost.h" |
| 55 | #include "pathnames.h" |
| 56 | #include "readconf.h" |
| 57 | #include "uidswap.h" |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 58 | #include "sshkey.h" |
| 59 | #include "ssherr.h" |
| 60 | |
| 61 | struct ssh *active_state = NULL; /* XXX needed for linking */ |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 62 | |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 63 | extern char *__progname; |
| 64 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 65 | /* XXX readconf.c needs these */ |
| 66 | uid_t original_real_uid; |
| 67 | |
| 68 | extern char *__progname; |
| 69 | |
| 70 | static int |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 71 | valid_request(struct passwd *pw, char *host, struct sshkey **ret, |
| 72 | u_char *data, size_t datalen) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 73 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [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; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 81 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 82 | if (ret != NULL) |
| 83 | *ret = NULL; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 84 | fail = 0; |
| 85 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 86 | if ((b = sshbuf_from(data, datalen)) == NULL) |
| 87 | fatal("%s: sshbuf_from failed", __func__); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 88 | |
| 89 | /* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 90 | if ((r = sshbuf_get_string(b, NULL, &len)) != 0) |
| 91 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 92 | if (len != 20 && len != 32) |
| 93 | fail++; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 94 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [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) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 98 | fail++; |
| 99 | |
| 100 | /* server user */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 101 | if ((r = sshbuf_skip_string(b)) != 0) |
| 102 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 103 | |
| 104 | /* service */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 105 | if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0) |
| 106 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 107 | if (strcmp("ssh-connection", p) != 0) |
| 108 | fail++; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 109 | free(p); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 110 | |
| 111 | /* method */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 112 | if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0) |
| 113 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 114 | if (strcmp("hostbased", p) != 0) |
| 115 | fail++; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 116 | free(p); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 117 | |
| 118 | /* pubkey */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [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)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 122 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 123 | pktype = sshkey_type_from_name(pkalg); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 124 | if (pktype == KEY_UNSPEC) |
| 125 | fail++; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 126 | else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { |
| 127 | error("%s: bad key blob: %s", __func__, ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 128 | fail++; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 129 | } else if (key->type != pktype) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 130 | fail++; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 131 | free(pkalg); |
| 132 | free(pkblob); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 133 | |
| 134 | /* client host name, handle trailing dot */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [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); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 138 | if (strlen(host) != len - 1) |
| 139 | fail++; |
| 140 | else if (p[len - 1] != '.') |
| 141 | fail++; |
| 142 | else if (strncasecmp(host, p, len - 1) != 0) |
| 143 | fail++; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 144 | free(p); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 145 | |
| 146 | /* local user */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 147 | if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0) |
| 148 | fatal("%s: buffer error: %s", __func__, ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 149 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 150 | if (strcmp(pw->pw_name, luser) != 0) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 151 | fail++; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 152 | free(luser); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 153 | |
| 154 | /* end of message */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 155 | if (sshbuf_len(b) != 0) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 156 | fail++; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 157 | sshbuf_free(b); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 158 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 159 | debug3("%s: fail %d", __func__, fail); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 160 | |
| 161 | if (fail && key != NULL) |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 162 | sshkey_free(key); |
Greg Hartman | ccacbc9 | 2016-02-03 09:59:44 -0800 | [diff] [blame] | 163 | else if (ret != NULL) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 164 | *ret = key; |
| 165 | |
| 166 | return (fail ? -1 : 0); |
| 167 | } |
| 168 | |
| 169 | int |
| 170 | main(int argc, char **argv) |
| 171 | { |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 172 | struct sshbuf *b; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 173 | Options options; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 174 | #define NUM_KEYTYPES 4 |
| 175 | struct sshkey *keys[NUM_KEYTYPES], *key = NULL; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 176 | struct passwd *pw; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 177 | int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd; |
| 178 | u_char *signature, *data, rver; |
| 179 | char *host, *fp; |
| 180 | size_t slen, dlen; |
| 181 | #ifdef WITH_OPENSSL |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 182 | u_int32_t rnd[256]; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 183 | #endif |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 184 | |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 185 | ssh_malloc_init(); /* must be called before any mallocs */ |
| 186 | if (pledge("stdio rpath getpw dns id", NULL) != 0) |
| 187 | fatal("%s: pledge: %s", __progname, strerror(errno)); |
| 188 | |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [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 | |
| 196 | i = 0; |
Greg Hartman | ccacbc9 | 2016-02-03 09:59:44 -0800 | [diff] [blame] | 197 | /* XXX This really needs to read sshd_config for the paths */ |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [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); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 200 | key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 201 | key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY); |
| 202 | |
| 203 | original_real_uid = getuid(); /* XXX readconf.c needs this */ |
| 204 | if ((pw = getpwuid(original_real_uid)) == NULL) |
| 205 | fatal("getpwuid failed"); |
| 206 | pw = pwcopy(pw); |
| 207 | |
| 208 | permanently_set_uid(pw); |
| 209 | |
| 210 | seed_rng(); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 211 | |
| 212 | #ifdef DEBUG_SSH_KEYSIGN |
| 213 | log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0); |
| 214 | #endif |
| 215 | |
| 216 | /* verify that ssh-keysign is enabled by the admin */ |
| 217 | initialize_options(&options); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 218 | (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "", &options, 0); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 219 | fill_default_options(&options); |
| 220 | if (options.enable_ssh_keysign != 1) |
| 221 | fatal("ssh-keysign not enabled in %s", |
| 222 | _PATH_HOST_CONFIG_FILE); |
| 223 | |
| 224 | for (i = found = 0; i < NUM_KEYTYPES; i++) { |
| 225 | if (key_fd[i] != -1) |
| 226 | found = 1; |
| 227 | } |
| 228 | if (found == 0) |
| 229 | fatal("could not open any host key"); |
| 230 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 231 | #ifdef WITH_OPENSSL |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 232 | OpenSSL_add_all_algorithms(); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 233 | arc4random_buf(rnd, sizeof(rnd)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 234 | RAND_seed(rnd, sizeof(rnd)); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 235 | #endif |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 236 | |
| 237 | found = 0; |
| 238 | for (i = 0; i < NUM_KEYTYPES; i++) { |
| 239 | keys[i] = NULL; |
| 240 | if (key_fd[i] == -1) |
| 241 | continue; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 242 | r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC, |
| 243 | NULL, &key, NULL); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 244 | close(key_fd[i]); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [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; |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 249 | found = 1; |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 250 | } |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 251 | } |
| 252 | if (!found) |
| 253 | fatal("no hostkey found"); |
| 254 | |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 255 | if (pledge("stdio dns", NULL) != 0) |
| 256 | fatal("%s: pledge: %s", __progname, strerror(errno)); |
| 257 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 258 | if ((b = sshbuf_new()) == NULL) |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 259 | fatal("%s: sshbuf_new failed", __progname); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 260 | if (ssh_msg_recv(STDIN_FILENO, b) < 0) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 261 | fatal("ssh_msg_recv failed"); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 262 | if ((r = sshbuf_get_u8(b, &rver)) != 0) |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 263 | fatal("%s: buffer error: %s", __progname, ssh_err(r)); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [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) |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 267 | fatal("%s: buffer error: %s", __progname, ssh_err(r)); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 268 | if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 269 | fatal("bad fd"); |
| 270 | if ((host = get_local_name(fd)) == NULL) |
| 271 | fatal("cannot get local name for fd"); |
| 272 | |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 273 | if ((r = sshbuf_get_string(b, &data, &dlen)) != 0) |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 274 | fatal("%s: buffer error: %s", __progname, ssh_err(r)); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 275 | if (valid_request(pw, host, &key, data, dlen) < 0) |
| 276 | fatal("not a valid request"); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 277 | free(host); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 278 | |
| 279 | found = 0; |
| 280 | for (i = 0; i < NUM_KEYTYPES; i++) { |
| 281 | if (keys[i] != NULL && |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 282 | sshkey_equal_public(key, keys[i])) { |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 283 | found = 1; |
| 284 | break; |
| 285 | } |
| 286 | } |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 287 | if (!found) { |
| 288 | if ((fp = sshkey_fingerprint(key, options.fingerprint_hash, |
| 289 | SSH_FP_DEFAULT)) == NULL) |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 290 | fatal("%s: sshkey_fingerprint failed", __progname); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 291 | fatal("no matching hostkey found for key %s %s", |
| 292 | sshkey_type(key), fp ? fp : ""); |
| 293 | } |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 294 | |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 295 | if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen, NULL, 0)) |
| 296 | != 0) |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 297 | fatal("sshkey_sign failed: %s", ssh_err(r)); |
| 298 | free(data); |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 299 | |
| 300 | /* send reply */ |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 301 | sshbuf_reset(b); |
| 302 | if ((r = sshbuf_put_string(b, signature, slen)) != 0) |
Greg Hartman | 9768ca4 | 2017-06-22 20:49:52 -0700 | [diff] [blame] | 303 | fatal("%s: buffer error: %s", __progname, ssh_err(r)); |
Adam Langley | d059297 | 2015-03-30 14:49:51 -0700 | [diff] [blame] | 304 | if (ssh_msg_send(STDOUT_FILENO, version, b) == -1) |
Greg Hartman | bd77cf7 | 2015-02-25 13:21:06 -0800 | [diff] [blame] | 305 | fatal("ssh_msg_send failed"); |
| 306 | |
| 307 | return (0); |
| 308 | } |