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