[BACK]Return to ssh-keysign.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/ssh-keysign.c, Revision 1.46

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