1 /* $OpenBSD: ssh-keysign.c,v 1.69 2021/11/13 17:26:13 deraadt 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 <stdarg.h> 36 #include <stdio.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 "ssherr.h" 56 57 extern char *__progname; 58 59 static int 60 valid_request(struct passwd *pw, char *host, struct sshkey **ret, 61 u_char *data, size_t datalen) 62 { 63 struct sshbuf *b; 64 struct sshkey *key = NULL; 65 u_char type, *pkblob; 66 char *p; 67 size_t blen, len; 68 char *pkalg, *luser; 69 int r, pktype, fail; 70 71 if (ret != NULL) 72 *ret = NULL; 73 fail = 0; 74 75 if ((b = sshbuf_from(data, datalen)) == NULL) 76 fatal_f("sshbuf_from failed"); 77 78 /* session id */ 79 if ((r = sshbuf_get_string(b, NULL, &len)) != 0) 80 fatal_fr(r, "parse session ID"); 81 if (len != 20 && /* SHA1 */ 82 len != 32 && /* SHA256 */ 83 len != 48 && /* SHA384 */ 84 len != 64) /* SHA512 */ 85 fail++; 86 87 if ((r = sshbuf_get_u8(b, &type)) != 0) 88 fatal_fr(r, "parse type"); 89 if (type != SSH2_MSG_USERAUTH_REQUEST) 90 fail++; 91 92 /* server user */ 93 if ((r = sshbuf_skip_string(b)) != 0) 94 fatal_fr(r, "parse user"); 95 96 /* service */ 97 if ((r = sshbuf_get_cstring(b, &p, NULL)) != 0) 98 fatal_fr(r, "parse service"); 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_fr(r, "parse method"); 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_fr(r, "parse pk"); 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_fr(r, "decode key"); 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_fr(r, "parse hostname"); 129 debug2_f("check expect chost %s got %s", 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_fr(r, "parse luser"); 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_f("fail %d", 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 if (pledge("stdio rpath getpw dns id", NULL) != 0) 175 fatal("%s: pledge: %s", __progname, strerror(errno)); 176 177 /* Ensure that stdin and stdout are connected */ 178 if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2) 179 exit(1); 180 /* Leave /dev/null fd iff it is attached to stderr */ 181 if (fd > 2) 182 close(fd); 183 184 i = 0; 185 /* XXX This really needs to read sshd_config for the paths */ 186 key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY); 187 key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY); 188 key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY); 189 key_fd[i++] = open(_PATH_HOST_XMSS_KEY_FILE, O_RDONLY); 190 key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY); 191 192 if ((pw = getpwuid(getuid())) == NULL) 193 fatal("getpwuid failed"); 194 pw = pwcopy(pw); 195 196 permanently_set_uid(pw); 197 198 #ifdef DEBUG_SSH_KEYSIGN 199 log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0); 200 #endif 201 202 /* verify that ssh-keysign is enabled by the admin */ 203 initialize_options(&options); 204 (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", "", 205 &options, 0, NULL); 206 (void)fill_default_options(&options); 207 if (options.enable_ssh_keysign != 1) 208 fatal("ssh-keysign not enabled in %s", 209 _PATH_HOST_CONFIG_FILE); 210 211 if (pledge("stdio dns", NULL) != 0) 212 fatal("%s: pledge: %s", __progname, strerror(errno)); 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 225 found = 0; 226 for (i = 0; i < NUM_KEYTYPES; i++) { 227 keys[i] = NULL; 228 if (key_fd[i] == -1) 229 continue; 230 r = sshkey_load_private_type_fd(key_fd[i], KEY_UNSPEC, 231 NULL, &key, NULL); 232 close(key_fd[i]); 233 if (r != 0) 234 debug_r(r, "parse key %d", i); 235 else if (key != NULL) { 236 keys[i] = key; 237 found = 1; 238 } 239 } 240 if (!found) 241 fatal("no hostkey found"); 242 243 if ((b = sshbuf_new()) == NULL) 244 fatal("%s: sshbuf_new failed", __progname); 245 if (ssh_msg_recv(STDIN_FILENO, b) < 0) 246 fatal("%s: ssh_msg_recv failed", __progname); 247 if ((r = sshbuf_get_u8(b, &rver)) != 0) 248 fatal_r(r, "%s: buffer error", __progname); 249 if (rver != version) 250 fatal("%s: bad version: received %d, expected %d", 251 __progname, rver, version); 252 if ((r = sshbuf_get_u32(b, (u_int *)&fd)) != 0) 253 fatal_r(r, "%s: buffer error", __progname); 254 if (fd < 0 || fd == STDIN_FILENO || fd == STDOUT_FILENO) 255 fatal("%s: bad fd = %d", __progname, fd); 256 if ((host = get_local_name(fd)) == NULL) 257 fatal("%s: cannot get local name for fd", __progname); 258 259 if ((r = sshbuf_get_string(b, &data, &dlen)) != 0) 260 fatal_r(r, "%s: buffer error", __progname); 261 if (valid_request(pw, host, &key, data, dlen) < 0) 262 fatal("%s: not a valid request", __progname); 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("%s: no matching hostkey found for key %s %s", __progname, 278 sshkey_type(key), fp ? fp : ""); 279 } 280 281 if ((r = sshkey_sign(keys[i], &signature, &slen, data, dlen, 282 NULL, NULL, NULL, 0)) != 0) 283 fatal_r(r, "%s: sshkey_sign failed", __progname); 284 free(data); 285 286 /* send reply */ 287 sshbuf_reset(b); 288 if ((r = sshbuf_put_string(b, signature, slen)) != 0) 289 fatal_r(r, "%s: buffer error", __progname); 290 if (ssh_msg_send(STDOUT_FILENO, version, b) == -1) 291 fatal("%s: ssh_msg_send failed", __progname); 292 293 return (0); 294 } 295