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