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