1 /* $NetBSD: ssh-keysign.c,v 1.18 2019/10/12 18:32:22 christos Exp $ */ 2 /* $OpenBSD: ssh-keysign.c,v 1.61 2019/10/02 00:42:30 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.18 2019/10/12 18:32:22 christos Exp $"); 29 #include <sys/types.h> 30 31 #ifdef WITH_OPENSSL 32 #include <openssl/evp.h> 33 #endif 34 35 #include <fcntl.h> 36 #include <paths.h> 37 #include <pwd.h> 38 #include <stdlib.h> 39 #include <stdio.h> 40 #include <string.h> 41 #include <unistd.h> 42 #include <errno.h> 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 "ssherr.h" 58 59 extern char *__progname; 60 61 static int 62 valid_request(struct passwd *pw, char *host, struct sshkey **ret, 63 u_char *data, size_t datalen) 64 { 65 struct sshbuf *b; 66 struct sshkey *key = NULL; 67 u_char type, *pkblob; 68 char *p; 69 size_t blen, len; 70 char *pkalg, *luser; 71 int r, pktype, fail; 72 73 if (ret != NULL) 74 *ret = NULL; 75 fail = 0; 76 77 if ((b = sshbuf_from(data, datalen)) == NULL) 78 fatal("%s: sshbuf_from failed", __func__); 79 80 /* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */ 81 if ((r = sshbuf_get_string(b, NULL, &len)) != 0) 82 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 83 if (len != 20 && len != 32) 84 fail++; 85 86 if ((r = sshbuf_get_u8(b, &type)) != 0) 87 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 88 if (type != SSH2_MSG_USERAUTH_REQUEST) 89 fail++; 90 91 /* server user */ 92 if ((r = sshbuf_skip_string(b)) != 0) 93 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 94 95 /* service */ 96 if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0) 97 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 98 if (strcmp("ssh-connection", p) != 0) 99 fail++; 100 free(p); 101 102 /* method */ 103 if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0) 104 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 105 if (strcmp("hostbased", p) != 0) 106 fail++; 107 free(p); 108 109 /* pubkey */ 110 if ((r = sshbuf_get_cstring(b, &pkalg, NULL)) != 0 || 111 (r = sshbuf_get_string(b, &pkblob, &blen)) != 0) 112 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 113 114 pktype = sshkey_type_from_name(pkalg); 115 if (pktype == KEY_UNSPEC) 116 fail++; 117 else if ((r = sshkey_from_blob(pkblob, blen, &key)) != 0) { 118 error("%s: bad key blob: %s", __func__, ssh_err(r)); 119 fail++; 120 } else if (key->type != pktype) 121 fail++; 122 free(pkalg); 123 free(pkblob); 124 125 /* client host name, handle trailing dot */ 126 if ((r = sshbuf_get_cstring(b, &p, &len)) != 0) 127 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 128 debug2("%s: check expect chost %s got %s", __func__, host, p); 129 if (strlen(host) != len - 1) 130 fail++; 131 else if (p[len - 1] != '.') 132 fail++; 133 else if (strncasecmp(host, p, len - 1) != 0) 134 fail++; 135 free(p); 136 137 /* local user */ 138 if ((r = sshbuf_get_cstring(b, &luser, NULL)) != 0) 139 fatal("%s: buffer error: %s", __func__, ssh_err(r)); 140 141 if (strcmp(pw->pw_name, luser) != 0) 142 fail++; 143 free(luser); 144 145 /* end of message */ 146 if (sshbuf_len(b) != 0) 147 fail++; 148 sshbuf_free(b); 149 150 debug3("%s: fail %d", __func__, fail); 151 152 if (fail) 153 sshkey_free(key); 154 else if (ret != NULL) 155 *ret = key; 156 157 return (fail ? -1 : 0); 158 } 159 160 int 161 main(int argc, char **argv) 162 { 163 struct sshbuf *b; 164 Options options; 165 #define NUM_KEYTYPES 5 166 struct sshkey *keys[NUM_KEYTYPES], *key = NULL; 167 struct passwd *pw; 168 int r, key_fd[NUM_KEYTYPES], i, found, version = 2, fd; 169 u_char *signature, *data, rver; 170 char *host, *fp; 171 size_t slen, dlen; 172 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 #ifdef WITH_OPENSSL 220 OpenSSL_add_all_algorithms(); 221 #endif 222 found = 0; 223 for (i = 0; i < NUM_KEYTYPES; i++) { 224 keys[i] = NULL; 225 if (key_fd[i] == -1) 226 continue; 227 r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC, 228 NULL, &key, NULL); 229 close(key_fd[i]); 230 if (r != 0) 231 debug("parse key %d: %s", i, ssh_err(r)); 232 else if (key != NULL) { 233 keys[i] = key; 234 found = 1; 235 } 236 } 237 if (!found) 238 fatal("no hostkey found"); 239 240 #ifdef __OpenBSD__ 241 if (pledge("stdio dns", NULL) != 0) 242 fatal("%s: pledge: %s", __progname, strerror(errno)); 243 #endif 244 245 if ((b = sshbuf_new()) == NULL) 246 fatal("%s: sshbuf_new failed", __progname); 247 if (ssh_msg_recv(STDIN_FILENO, b) < 0) 248 fatal("ssh_msg_recv failed"); 249 if ((r = sshbuf_get_u8(b, &rver)) != 0) 250 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 251 if (rver != version) 252 fatal("bad version: received %d, expected %d", rver, version); 253 if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0) 254 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 255 if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO) 256 fatal("bad fd = %d", fd); 257 if ((host = get_local_name(fd)) == NULL) 258 fatal("cannot get local name for fd"); 259 260 if ((r = sshbuf_get_string(b, &data, &dlen)) != 0) 261 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 262 if (valid_request(pw, host, &key, data, dlen) < 0) 263 fatal("not a valid request"); 264 free(host); 265 266 found = 0; 267 for (i = 0; i < NUM_KEYTYPES; i++) { 268 if (keys[i] != NULL && 269 sshkey_equal_public(key, keys[i])) { 270 found = 1; 271 break; 272 } 273 } 274 if (!found) { 275 if ((fp = sshkey_fingerprint(key, options.fingerprint_hash, 276 SSH_FP_DEFAULT)) == NULL) 277 fatal("%s: sshkey_fingerprint failed", __progname); 278 fatal("no matching hostkey found for key %s %s", 279 sshkey_type(key), fp ? fp : ""); 280 } 281 282 if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen, NULL, 0)) 283 != 0) 284 fatal("sshkey_sign failed: %s", ssh_err(r)); 285 free(data); 286 287 /* send reply */ 288 sshbuf_reset(b); 289 if ((r = sshbuf_put_string(b, signature, slen)) != 0) 290 fatal("%s: buffer error: %s", __progname, ssh_err(r)); 291 if (ssh_msg_send(STDOUT_FILENO, version, b) == -1) 292 fatal("ssh_msg_send failed"); 293 294 return (0); 295 } 296