1 /* $OpenBSD: ssh-keysign.c,v 1.49 2015/07/03 03:56:25 djm Exp $ */ 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> 37 38 #ifdef WITH_OPENSSL 39 #include <openssl/evp.h> 40 #include <openssl/rand.h> 41 #include <openssl/rsa.h> 42 #endif 43 44 #include "xmalloc.h" 45 #include "log.h" 46 #include "sshkey.h" 47 #include "ssh.h" 48 #include "ssh2.h" 49 #include "misc.h" 50 #include "sshbuf.h" 51 #include "authfile.h" 52 #include "msg.h" 53 #include "canohost.h" 54 #include "pathnames.h" 55 #include "readconf.h" 56 #include "uidswap.h" 57 #include "sshkey.h" 58 #include "ssherr.h" 59 60 struct ssh *active_state = NULL; /* XXX needed for linking */ 61 62 /* XXX readconf.c needs these */ 63 uid_t original_real_uid; 64 65 extern char *__progname; 66 67 static int 68 valid_request(struct passwd *pw, char *host, struct sshkey **ret, 69 u_char *data, size_t datalen) 70 { 71 struct sshbuf *b; 72 struct sshkey *key = NULL; 73 u_char type, *pkblob; 74 char *p; 75 size_t blen, len; 76 char *pkalg, *luser; 77 int r, pktype, fail; 78 79 if (ret != NULL) 80 *ret = NULL; 81 fail = 0; 82 83 if ((b = sshbuf_from(data, datalen)) == NULL) 84 fatal("%s: sshbuf_from failed", __func__); 85 86 /* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */ 87 if ((r = sshbuf_get_string(b, NULL, &len)) != 0) 88 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 89 if (len != 20 && len != 32) 90 fail++; 91 92 if ((r = sshbuf_get_u8(b, &type)) != 0) 93 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 94 if (type != SSH2_MSG_USERAUTH_REQUEST) 95 fail++; 96 97 /* server user */ 98 if ((r = sshbuf_skip_string(b)) != 0) 99 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 100 101 /* service */ 102 if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0) 103 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 104 if (strcmp("ssh-connection", p) != 0) 105 fail++; 106 free(p); 107 108 /* method */ 109 if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0) 110 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 111 if (strcmp("hostbased", p) != 0) 112 fail++; 113 free(p); 114 115 /* pubkey */ 116 if ((r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 || 117 (r = sshbuf_get_string(b, &pkblob, &blen)) != 0) 118 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 119 120 pktype = sshkey_type_from_name(pkalg); 121 if (pktype == KEY_UNSPEC) 122 fail++; 123 else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { 124 error("%s: bad key blob: %s", __func__, ssh_err(r)); 125 fail++; 126 } else if (key->type != pktype) 127 fail++; 128 free(pkalg); 129 free(pkblob); 130 131 /* client host name, handle trailing dot */ 132 if ((r = sshbuf_get_cstring(b, &p, &len)) != 0) 133 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 134 debug2("%s: check expect chost %s got %s", __func__, host, p); 135 if (strlen(host) != len - 1) 136 fail++; 137 else if (p[len - 1] != '.') 138 fail++; 139 else if (strncasecmp(host, p, len - 1) != 0) 140 fail++; 141 free(p); 142 143 /* local user */ 144 if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0) 145 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 146 147 if (strcmp(pw->pw_name, luser) != 0) 148 fail++; 149 free(luser); 150 151 /* end of message */ 152 if (sshbuf_len(b) != 0) 153 fail++; 154 sshbuf_free(b); 155 156 debug3("%s: fail %d", __func__, fail); 157 158 if (fail && key != NULL) 159 sshkey_free(key); 160 else if (ret != NULL) 161 *ret = key; 162 163 return (fail ? -1 : 0); 164 } 165 166 int 167 main(int argc, char **argv) 168 { 169 struct sshbuf *b; 170 Options options; 171 #define NUM_KEYTYPES 4 172 struct sshkey *keys[NUM_KEYTYPES], *key = NULL; 173 struct passwd *pw; 174 int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd; 175 u_char *signature, *data, rver; 176 char *host, *fp; 177 size_t slen, dlen; 178 #ifdef WITH_OPENSSL 179 u_int32_t rnd[256]; 180 #endif 181 182 /* Ensure that stdin and stdout are connected */ 183 if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2) 184 exit(1); 185 /* Leave /dev/null fd iff it is attached to stderr */ 186 if (fd > 2) 187 close(fd); 188 189 i = 0; 190 /* XXX This really needs to read sshd_config for the paths */ 191 key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY); 192 key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY); 193 key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY); 194 key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY); 195 196 original_real_uid = getuid(); /* XXX readconf.c needs this */ 197 if ((pw = getpwuid(original_real_uid)) == NULL) 198 fatal("getpwuid failed"); 199 pw = pwcopy(pw); 200 201 permanently_set_uid(pw); 202 203 seed_rng(); 204 205 #ifdef DEBUG_SSH_KEYSIGN 206 log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0); 207 #endif 208 209 /* verify that ssh-keysign is enabled by the admin */ 210 initialize_options(&options); 211 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "", &options, 0); 212 fill_default_options(&options); 213 if (options.enable_ssh_keysign != 1) 214 fatal("ssh-keysign not enabled in %s", 215 _PATH_HOST_CONFIG_FILE); 216 217 for (i = found = 0; i < NUM_KEYTYPES; i++) { 218 if (key_fd[i] != -1) 219 found = 1; 220 } 221 if (found == 0) 222 fatal("could not open any host key"); 223 224 #ifdef WITH_OPENSSL 225 OpenSSL_add_all_algorithms(); 226 arc4random_buf(rnd, sizeof(rnd)); 227 RAND_seed(rnd, sizeof(rnd)); 228 #endif 229 230 found = 0; 231 for (i = 0; i < NUM_KEYTYPES; i++) { 232 keys[i] = NULL; 233 if (key_fd[i] == -1) 234 continue; 235 r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC, 236 NULL, &key, NULL); 237 close(key_fd[i]); 238 if (r != 0) 239 debug("parse key %d: %s", i, ssh_err(r)); 240 else if (key != NULL) { 241 keys[i] = key; 242 found = 1; 243 } 244 } 245 if (!found) 246 fatal("no hostkey found"); 247 248 if ((b = sshbuf_new()) == NULL) 249 fatal("%s: sshbuf_new failed", __func__); 250 if (ssh_msg_recv(STDIN_FILENO, b) < 0) 251 fatal("ssh_msg_recv failed"); 252 if ((r = sshbuf_get_u8(b, &rver)) != 0) 253 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 254 if (rver != version) 255 fatal("bad version: received %d, expected %d", rver, version); 256 if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0) 257 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 258 if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO) 259 fatal("bad fd"); 260 if ((host = get_local_name(fd)) == NULL) 261 fatal("cannot get local name for fd"); 262 263 if ((r = sshbuf_get_string(b, &data, &dlen)) != 0) 264 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 265 if (valid_request(pw, host, &key, data, dlen) < 0) 266 fatal("not a valid request"); 267 free(host); 268 269 found = 0; 270 for (i = 0; i < NUM_KEYTYPES; i++) { 271 if (keys[i] != NULL && 272 sshkey_equal_public(key, keys[i])) { 273 found = 1; 274 break; 275 } 276 } 277 if (!found) { 278 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash, 279 SSH_FP_DEFAULT)) == NULL) 280 fatal("%s: sshkey_fingerprint failed", __func__); 281 fatal("no matching hostkey found for key %s %s", 282 sshkey_type(key), fp ? fp : ""); 283 } 284 285 if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen, 0)) != 0) 286 fatal("sshkey_sign failed: %s", ssh_err(r)); 287 free(data); 288 289 /* send reply */ 290 sshbuf_reset(b); 291 if ((r = sshbuf_put_string(b, signature, slen)) != 0) 292 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 293 if (ssh_msg_send(STDOUT_FILENO, version, b) == -1) 294 fatal("ssh_msg_send failed"); 295 296 return (0); 297 } 298