1262566Sdes/* $OpenBSD: ssh-keysign.c,v 1.39 2013/12/06 13:39:49 markus Exp $ */ 298675Sdes/* 398675Sdes * Copyright (c) 2002 Markus Friedl. All rights reserved. 498675Sdes * 598675Sdes * Redistribution and use in source and binary forms, with or without 698675Sdes * modification, are permitted provided that the following conditions 798675Sdes * are met: 898675Sdes * 1. Redistributions of source code must retain the above copyright 998675Sdes * notice, this list of conditions and the following disclaimer. 1098675Sdes * 2. Redistributions in binary form must reproduce the above copyright 1198675Sdes * notice, this list of conditions and the following disclaimer in the 1298675Sdes * documentation and/or other materials provided with the distribution. 1398675Sdes * 1498675Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1598675Sdes * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1698675Sdes * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1798675Sdes * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1898675Sdes * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1998675Sdes * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2098675Sdes * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2198675Sdes * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2298675Sdes * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 2398675Sdes * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2498675Sdes */ 25162852Sdes 2698675Sdes#include "includes.h" 2798675Sdes 28162852Sdes#include <fcntl.h> 29162852Sdes#ifdef HAVE_PATHS_H 30162852Sdes#include <paths.h> 31162852Sdes#endif 32162852Sdes#include <pwd.h> 33162852Sdes#include <stdarg.h> 34162852Sdes#include <stdlib.h> 35162852Sdes#include <string.h> 36162852Sdes#include <unistd.h> 37162852Sdes 3898675Sdes#include <openssl/evp.h> 39106121Sdes#include <openssl/rand.h> 40106121Sdes#include <openssl/rsa.h> 4198675Sdes 42162852Sdes#include "xmalloc.h" 4398675Sdes#include "log.h" 4498675Sdes#include "key.h" 45106121Sdes#include "ssh.h" 4698675Sdes#include "ssh2.h" 4798675Sdes#include "misc.h" 4898675Sdes#include "buffer.h" 4998675Sdes#include "authfile.h" 5098675Sdes#include "msg.h" 5198675Sdes#include "canohost.h" 5298675Sdes#include "pathnames.h" 53106121Sdes#include "readconf.h" 54137015Sdes#include "uidswap.h" 5598675Sdes 56124208Sdes/* XXX readconf.c needs these */ 57124208Sdesuid_t original_real_uid; 58106121Sdes 5998937Sdesextern char *__progname; 6098937Sdes 6198675Sdesstatic int 6298675Sdesvalid_request(struct passwd *pw, char *host, Key **ret, u_char *data, 6398675Sdes u_int datalen) 6498675Sdes{ 6598675Sdes Buffer b; 66124208Sdes Key *key = NULL; 6798675Sdes u_char *pkblob; 6898675Sdes u_int blen, len; 6998675Sdes char *pkalg, *p; 7098675Sdes int pktype, fail; 7198675Sdes 7298675Sdes fail = 0; 7398675Sdes 7498675Sdes buffer_init(&b); 7598675Sdes buffer_append(&b, data, datalen); 7698675Sdes 77162852Sdes /* session id, currently limited to SHA1 (20 bytes) or SHA256 (32) */ 7898675Sdes p = buffer_get_string(&b, &len); 79162852Sdes if (len != 20 && len != 32) 8098675Sdes fail++; 81255767Sdes free(p); 8298675Sdes 8398675Sdes if (buffer_get_char(&b) != SSH2_MSG_USERAUTH_REQUEST) 8498675Sdes fail++; 8598675Sdes 8698675Sdes /* server user */ 8798675Sdes buffer_skip_string(&b); 8898675Sdes 8998675Sdes /* service */ 9098675Sdes p = buffer_get_string(&b, NULL); 9198675Sdes if (strcmp("ssh-connection", p) != 0) 9298675Sdes fail++; 93255767Sdes free(p); 9498675Sdes 9598675Sdes /* method */ 9698675Sdes p = buffer_get_string(&b, NULL); 9798675Sdes if (strcmp("hostbased", p) != 0) 9898675Sdes fail++; 99255767Sdes free(p); 10098675Sdes 10198675Sdes /* pubkey */ 10298675Sdes pkalg = buffer_get_string(&b, NULL); 10398675Sdes pkblob = buffer_get_string(&b, &blen); 10498675Sdes 10598675Sdes pktype = key_type_from_name(pkalg); 10698675Sdes if (pktype == KEY_UNSPEC) 10798675Sdes fail++; 10898675Sdes else if ((key = key_from_blob(pkblob, blen)) == NULL) 10998675Sdes fail++; 11098675Sdes else if (key->type != pktype) 11198675Sdes fail++; 112255767Sdes free(pkalg); 113255767Sdes free(pkblob); 11498675Sdes 11598675Sdes /* client host name, handle trailing dot */ 11698675Sdes p = buffer_get_string(&b, &len); 11798675Sdes debug2("valid_request: check expect chost %s got %s", host, p); 11898675Sdes if (strlen(host) != len - 1) 11998675Sdes fail++; 12098675Sdes else if (p[len - 1] != '.') 12198675Sdes fail++; 12298675Sdes else if (strncasecmp(host, p, len - 1) != 0) 12398675Sdes fail++; 124255767Sdes free(p); 12598675Sdes 12698675Sdes /* local user */ 12798675Sdes p = buffer_get_string(&b, NULL); 12898675Sdes 12998675Sdes if (strcmp(pw->pw_name, p) != 0) 13098675Sdes fail++; 131255767Sdes free(p); 13298675Sdes 13398675Sdes /* end of message */ 13498675Sdes if (buffer_len(&b) != 0) 13598675Sdes fail++; 136126274Sdes buffer_free(&b); 13798675Sdes 13898675Sdes debug3("valid_request: fail %d", fail); 13998675Sdes 14098675Sdes if (fail && key != NULL) 14198675Sdes key_free(key); 14298675Sdes else 14398675Sdes *ret = key; 14498675Sdes 14598675Sdes return (fail ? -1 : 0); 14698675Sdes} 14798675Sdes 14898675Sdesint 14998675Sdesmain(int argc, char **argv) 15098675Sdes{ 15198675Sdes Buffer b; 152106121Sdes Options options; 153262566Sdes#define NUM_KEYTYPES 4 154226046Sdes Key *keys[NUM_KEYTYPES], *key = NULL; 15598675Sdes struct passwd *pw; 156226046Sdes int key_fd[NUM_KEYTYPES], i, found, version = 2, fd; 15798675Sdes u_char *signature, *data; 15898675Sdes char *host; 15998675Sdes u_int slen, dlen; 160106121Sdes u_int32_t rnd[256]; 16198675Sdes 162157016Sdes /* Ensure that stdin and stdout are connected */ 163157016Sdes if ((fd = open(_PATH_DEVNULL, O_RDWR)) < 2) 164157016Sdes exit(1); 165157016Sdes /* Leave /dev/null fd iff it is attached to stderr */ 166157016Sdes if (fd > 2) 167157016Sdes close(fd); 168157016Sdes 169226046Sdes i = 0; 170226046Sdes key_fd[i++] = open(_PATH_HOST_DSA_KEY_FILE, O_RDONLY); 171226046Sdes key_fd[i++] = open(_PATH_HOST_ECDSA_KEY_FILE, O_RDONLY); 172262566Sdes key_fd[i++] = open(_PATH_HOST_ED25519_KEY_FILE, O_RDONLY); 173226046Sdes key_fd[i++] = open(_PATH_HOST_RSA_KEY_FILE, O_RDONLY); 17498675Sdes 175146998Sdes original_real_uid = getuid(); /* XXX readconf.c needs this */ 176146998Sdes if ((pw = getpwuid(original_real_uid)) == NULL) 177137015Sdes fatal("getpwuid failed"); 178137015Sdes pw = pwcopy(pw); 17998675Sdes 180137015Sdes permanently_set_uid(pw); 181137015Sdes 18298937Sdes seed_rng(); 18398937Sdes 18498675Sdes#ifdef DEBUG_SSH_KEYSIGN 18598675Sdes log_init("ssh-keysign", SYSLOG_LEVEL_DEBUG3, SYSLOG_FACILITY_AUTH, 0); 18698675Sdes#endif 18798675Sdes 188106121Sdes /* verify that ssh-keysign is enabled by the admin */ 189106121Sdes initialize_options(&options); 190262566Sdes (void)read_config_file(_PATH_HOST_CONFIG_FILE, pw, "", &options, 0); 191106121Sdes fill_default_options(&options); 192113908Sdes if (options.enable_ssh_keysign != 1) 193113908Sdes fatal("ssh-keysign not enabled in %s", 194106121Sdes _PATH_HOST_CONFIG_FILE); 195106121Sdes 196226046Sdes for (i = found = 0; i < NUM_KEYTYPES; i++) { 197226046Sdes if (key_fd[i] != -1) 198226046Sdes found = 1; 199226046Sdes } 200226046Sdes if (found == 0) 20198675Sdes fatal("could not open any host key"); 20298675Sdes 203221420Sdes OpenSSL_add_all_algorithms(); 204106121Sdes for (i = 0; i < 256; i++) 205106121Sdes rnd[i] = arc4random(); 206106121Sdes RAND_seed(rnd, sizeof(rnd)); 20798675Sdes 20898675Sdes found = 0; 209226046Sdes for (i = 0; i < NUM_KEYTYPES; i++) { 21098675Sdes keys[i] = NULL; 21198675Sdes if (key_fd[i] == -1) 21298675Sdes continue; 21398675Sdes keys[i] = key_load_private_pem(key_fd[i], KEY_UNSPEC, 21498675Sdes NULL, NULL); 21598675Sdes close(key_fd[i]); 21698675Sdes if (keys[i] != NULL) 21798675Sdes found = 1; 21898675Sdes } 21998675Sdes if (!found) 22098675Sdes fatal("no hostkey found"); 22198675Sdes 22298675Sdes buffer_init(&b); 223106121Sdes if (ssh_msg_recv(STDIN_FILENO, &b) < 0) 224106121Sdes fatal("ssh_msg_recv failed"); 22598675Sdes if (buffer_get_char(&b) != version) 22698675Sdes fatal("bad version"); 22798675Sdes fd = buffer_get_int(&b); 22898675Sdes if ((fd == STDIN_FILENO) || (fd == STDOUT_FILENO)) 22998675Sdes fatal("bad fd"); 23098675Sdes if ((host = get_local_name(fd)) == NULL) 231204917Sdes fatal("cannot get local name for fd"); 23298675Sdes 23398675Sdes data = buffer_get_string(&b, &dlen); 23498675Sdes if (valid_request(pw, host, &key, data, dlen) < 0) 23598675Sdes fatal("not a valid request"); 236255767Sdes free(host); 23798675Sdes 23898675Sdes found = 0; 239226046Sdes for (i = 0; i < NUM_KEYTYPES; i++) { 24098675Sdes if (keys[i] != NULL && 241215116Sdes key_equal_public(key, keys[i])) { 24298675Sdes found = 1; 24398675Sdes break; 24498675Sdes } 24598675Sdes } 24698675Sdes if (!found) 24798675Sdes fatal("no matching hostkey found"); 24898675Sdes 24998675Sdes if (key_sign(keys[i], &signature, &slen, data, dlen) != 0) 25098675Sdes fatal("key_sign failed"); 251255767Sdes free(data); 25298675Sdes 25398675Sdes /* send reply */ 25498675Sdes buffer_clear(&b); 25598675Sdes buffer_put_string(&b, signature, slen); 256126274Sdes if (ssh_msg_send(STDOUT_FILENO, version, &b) == -1) 257126274Sdes fatal("ssh_msg_send failed"); 25898675Sdes 25998675Sdes return (0); 26098675Sdes} 261