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