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

Annotation of src/usr.bin/ssh/ssh-pkcs11-helper.c, Revision 1.18

1.18    ! dtucker     1: /* $OpenBSD: ssh-pkcs11-helper.c,v 1.17 2019/01/23 02:01:10 djm Exp $ */
1.1       markus      2: /*
                      3:  * Copyright (c) 2010 Markus Friedl.  All rights reserved.
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17:
1.9       djm        18: #include <sys/types.h>
1.1       markus     19: #include <sys/queue.h>
                     20: #include <sys/time.h>
                     21:
1.17      djm        22: #include <errno.h>
                     23: #include <poll.h>
1.1       markus     24: #include <stdarg.h>
                     25: #include <string.h>
                     26: #include <unistd.h>
                     27:
                     28: #include "xmalloc.h"
1.14      markus     29: #include "sshbuf.h"
1.1       markus     30: #include "log.h"
                     31: #include "misc.h"
1.14      markus     32: #include "sshkey.h"
1.1       markus     33: #include "authfd.h"
                     34: #include "ssh-pkcs11.h"
1.14      markus     35: #include "ssherr.h"
1.1       markus     36:
                     37: /* borrows code from sftp-server and ssh-agent */
                     38:
                     39: struct pkcs11_keyinfo {
1.13      markus     40:        struct sshkey   *key;
1.1       markus     41:        char            *providername;
                     42:        TAILQ_ENTRY(pkcs11_keyinfo) next;
                     43: };
                     44:
                     45: TAILQ_HEAD(, pkcs11_keyinfo) pkcs11_keylist;
                     46:
                     47: #define MAX_MSG_LENGTH         10240 /*XXX*/
                     48:
                     49: /* input and output queue */
1.14      markus     50: struct sshbuf *iqueue;
                     51: struct sshbuf *oqueue;
1.1       markus     52:
                     53: static void
1.13      markus     54: add_key(struct sshkey *k, char *name)
1.1       markus     55: {
                     56:        struct pkcs11_keyinfo *ki;
                     57:
                     58:        ki = xcalloc(1, sizeof(*ki));
                     59:        ki->providername = xstrdup(name);
                     60:        ki->key = k;
                     61:        TAILQ_INSERT_TAIL(&pkcs11_keylist, ki, next);
                     62: }
                     63:
                     64: static void
                     65: del_keys_by_name(char *name)
                     66: {
                     67:        struct pkcs11_keyinfo *ki, *nxt;
                     68:
                     69:        for (ki = TAILQ_FIRST(&pkcs11_keylist); ki; ki = nxt) {
                     70:                nxt = TAILQ_NEXT(ki, next);
                     71:                if (!strcmp(ki->providername, name)) {
                     72:                        TAILQ_REMOVE(&pkcs11_keylist, ki, next);
1.6       djm        73:                        free(ki->providername);
1.14      markus     74:                        sshkey_free(ki->key);
1.1       markus     75:                        free(ki);
                     76:                }
                     77:        }
                     78: }
                     79:
                     80: /* lookup matching 'private' key */
1.13      markus     81: static struct sshkey *
                     82: lookup_key(struct sshkey *k)
1.1       markus     83: {
                     84:        struct pkcs11_keyinfo *ki;
                     85:
                     86:        TAILQ_FOREACH(ki, &pkcs11_keylist, next) {
                     87:                debug("check %p %s", ki, ki->providername);
1.14      markus     88:                if (sshkey_equal(k, ki->key))
1.1       markus     89:                        return (ki->key);
                     90:        }
                     91:        return (NULL);
                     92: }
                     93:
                     94: static void
1.14      markus     95: send_msg(struct sshbuf *m)
1.1       markus     96: {
1.14      markus     97:        int r;
1.1       markus     98:
1.14      markus     99:        if ((r = sshbuf_put_stringb(oqueue, m)) != 0)
                    100:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       markus    101: }
                    102:
                    103: static void
                    104: process_add(void)
                    105: {
                    106:        char *name, *pin;
1.15      djm       107:        struct sshkey **keys = NULL;
1.14      markus    108:        int r, i, nkeys;
1.1       markus    109:        u_char *blob;
1.14      markus    110:        size_t blen;
                    111:        struct sshbuf *msg;
1.1       markus    112:
1.14      markus    113:        if ((msg = sshbuf_new()) == NULL)
                    114:                fatal("%s: sshbuf_new failed", __func__);
                    115:        if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
                    116:            (r = sshbuf_get_cstring(iqueue, &pin, NULL)) != 0)
                    117:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       markus    118:        if ((nkeys = pkcs11_add_provider(name, pin, &keys)) > 0) {
1.14      markus    119:                if ((r = sshbuf_put_u8(msg,
                    120:                    SSH2_AGENT_IDENTITIES_ANSWER)) != 0 ||
                    121:                    (r = sshbuf_put_u32(msg, nkeys)) != 0)
                    122:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       markus    123:                for (i = 0; i < nkeys; i++) {
1.14      markus    124:                        if ((r = sshkey_to_blob(keys[i], &blob, &blen)) != 0) {
                    125:                                debug("%s: sshkey_to_blob: %s",
                    126:                                    __func__, ssh_err(r));
1.7       djm       127:                                continue;
1.14      markus    128:                        }
                    129:                        if ((r = sshbuf_put_string(msg, blob, blen)) != 0 ||
                    130:                            (r = sshbuf_put_cstring(msg, name)) != 0)
                    131:                                fatal("%s: buffer error: %s",
                    132:                                    __func__, ssh_err(r));
1.6       djm       133:                        free(blob);
1.1       markus    134:                        add_key(keys[i], name);
                    135:                }
                    136:        } else {
1.14      markus    137:                if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
                    138:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.15      djm       139:                if ((r = sshbuf_put_u32(msg, -nkeys)) != 0)
                    140:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       markus    141:        }
1.15      djm       142:        free(keys);
1.6       djm       143:        free(pin);
                    144:        free(name);
1.14      markus    145:        send_msg(msg);
                    146:        sshbuf_free(msg);
1.1       markus    147: }
                    148:
                    149: static void
                    150: process_del(void)
                    151: {
                    152:        char *name, *pin;
1.14      markus    153:        struct sshbuf *msg;
                    154:        int r;
1.1       markus    155:
1.14      markus    156:        if ((msg = sshbuf_new()) == NULL)
                    157:                fatal("%s: sshbuf_new failed", __func__);
                    158:        if ((r = sshbuf_get_cstring(iqueue, &name, NULL)) != 0 ||
                    159:            (r = sshbuf_get_cstring(iqueue, &pin, NULL)) != 0)
                    160:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       markus    161:        del_keys_by_name(name);
1.14      markus    162:        if ((r = sshbuf_put_u8(msg, pkcs11_del_provider(name) == 0 ?
                    163:            SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE)) != 0)
                    164:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.6       djm       165:        free(pin);
                    166:        free(name);
1.14      markus    167:        send_msg(msg);
                    168:        sshbuf_free(msg);
1.1       markus    169: }
                    170:
                    171: static void
                    172: process_sign(void)
                    173: {
                    174:        u_char *blob, *data, *signature = NULL;
1.14      markus    175:        size_t blen, dlen, slen = 0;
                    176:        int r, ok = -1;
1.13      markus    177:        struct sshkey *key, *found;
1.14      markus    178:        struct sshbuf *msg;
1.1       markus    179:
1.14      markus    180:        /* XXX support SHA2 signature flags */
                    181:        if ((r = sshbuf_get_string(iqueue, &blob, &blen)) != 0 ||
                    182:            (r = sshbuf_get_string(iqueue, &data, &dlen)) != 0 ||
                    183:            (r = sshbuf_get_u32(iqueue, NULL)) != 0)
                    184:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
                    185:
                    186:        if ((r = sshkey_from_blob(blob, blen, &key)) != 0)
                    187:                error("%s: sshkey_from_blob: %s", __func__, ssh_err(r));
                    188:        else {
1.1       markus    189:                if ((found = lookup_key(key)) != NULL) {
1.8       djm       190: #ifdef WITH_OPENSSL
                    191:                        int ret;
                    192:
1.15      djm       193:                        if (key->type == KEY_RSA) {
                    194:                                slen = RSA_size(key->rsa);
                    195:                                signature = xmalloc(slen);
                    196:                                ret = RSA_private_encrypt(dlen, data, signature,
                    197:                                    found->rsa, RSA_PKCS1_PADDING);
                    198:                                if (ret != -1) {
                    199:                                        slen = ret;
                    200:                                        ok = 0;
                    201:                                }
                    202:                        } else if (key->type == KEY_ECDSA) {
1.18    ! dtucker   203:                                u_int xslen = ECDSA_size(key->ecdsa);
        !           204:
1.15      djm       205:                                signature = xmalloc(xslen);
                    206:                                /* "The parameter type is ignored." */
                    207:                                ret = ECDSA_sign(-1, data, dlen, signature,
                    208:                                    &xslen, found->ecdsa);
                    209:                                if (ret != 0)
                    210:                                        ok = 0;
                    211:                                else
                    212:                                        error("%s: ECDSA_sign"
                    213:                                            " returns %d", __func__, ret);
                    214:                                slen = xslen;
                    215:                        } else
                    216:                                error("%s: don't know how to sign with key "
                    217:                                    "type %d", __func__, (int)key->type);
1.8       djm       218: #endif /* WITH_OPENSSL */
1.1       markus    219:                }
1.14      markus    220:                sshkey_free(key);
1.1       markus    221:        }
1.14      markus    222:        if ((msg = sshbuf_new()) == NULL)
                    223:                fatal("%s: sshbuf_new failed", __func__);
1.1       markus    224:        if (ok == 0) {
1.14      markus    225:                if ((r = sshbuf_put_u8(msg, SSH2_AGENT_SIGN_RESPONSE)) != 0 ||
                    226:                    (r = sshbuf_put_string(msg, signature, slen)) != 0)
                    227:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       markus    228:        } else {
1.14      markus    229:                if ((r = sshbuf_put_u8(msg, SSH2_AGENT_FAILURE)) != 0)
                    230:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       markus    231:        }
1.6       djm       232:        free(data);
                    233:        free(blob);
                    234:        free(signature);
1.14      markus    235:        send_msg(msg);
                    236:        sshbuf_free(msg);
1.1       markus    237: }
                    238:
                    239: static void
                    240: process(void)
                    241: {
                    242:        u_int msg_len;
                    243:        u_int buf_len;
                    244:        u_int consumed;
1.14      markus    245:        u_char type;
                    246:        const u_char *cp;
                    247:        int r;
1.1       markus    248:
1.14      markus    249:        buf_len = sshbuf_len(iqueue);
1.1       markus    250:        if (buf_len < 5)
                    251:                return;         /* Incomplete message. */
1.14      markus    252:        cp = sshbuf_ptr(iqueue);
1.1       markus    253:        msg_len = get_u32(cp);
                    254:        if (msg_len > MAX_MSG_LENGTH) {
                    255:                error("bad message len %d", msg_len);
                    256:                cleanup_exit(11);
                    257:        }
                    258:        if (buf_len < msg_len + 4)
                    259:                return;
1.14      markus    260:        if ((r = sshbuf_consume(iqueue, 4)) != 0 ||
                    261:            (r = sshbuf_get_u8(iqueue, &type)) != 0)
                    262:                fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       markus    263:        buf_len -= 4;
                    264:        switch (type) {
                    265:        case SSH_AGENTC_ADD_SMARTCARD_KEY:
                    266:                debug("process_add");
                    267:                process_add();
                    268:                break;
                    269:        case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
                    270:                debug("process_del");
                    271:                process_del();
                    272:                break;
                    273:        case SSH2_AGENTC_SIGN_REQUEST:
                    274:                debug("process_sign");
                    275:                process_sign();
                    276:                break;
                    277:        default:
                    278:                error("Unknown message %d", type);
                    279:                break;
                    280:        }
                    281:        /* discard the remaining bytes from the current packet */
1.14      markus    282:        if (buf_len < sshbuf_len(iqueue)) {
1.1       markus    283:                error("iqueue grew unexpectedly");
                    284:                cleanup_exit(255);
                    285:        }
1.14      markus    286:        consumed = buf_len - sshbuf_len(iqueue);
1.1       markus    287:        if (msg_len < consumed) {
                    288:                error("msg_len %d < consumed %d", msg_len, consumed);
                    289:                cleanup_exit(255);
                    290:        }
1.14      markus    291:        if (msg_len > consumed) {
                    292:                if ((r = sshbuf_consume(iqueue, msg_len - consumed)) != 0)
                    293:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
                    294:        }
1.1       markus    295: }
                    296:
                    297: void
                    298: cleanup_exit(int i)
                    299: {
                    300:        /* XXX */
                    301:        _exit(i);
                    302: }
                    303:
1.16      djm       304:
1.1       markus    305: int
                    306: main(int argc, char **argv)
                    307: {
1.16      djm       308:        int r, ch, in, out, max, log_stderr = 0;
1.17      djm       309:        ssize_t len;
1.1       markus    310:        SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
                    311:        LogLevel log_level = SYSLOG_LEVEL_ERROR;
                    312:        char buf[4*4096];
1.2       deraadt   313:        extern char *__progname;
1.17      djm       314:        struct pollfd pfd[2];
1.1       markus    315:
1.12      dtucker   316:        ssh_malloc_init();      /* must be called before any mallocs */
1.1       markus    317:        TAILQ_INIT(&pkcs11_keylist);
                    318:
                    319:        log_init(__progname, log_level, log_facility, log_stderr);
                    320:
1.16      djm       321:        while ((ch = getopt(argc, argv, "v")) != -1) {
                    322:                switch (ch) {
                    323:                case 'v':
                    324:                        log_stderr = 1;
                    325:                        if (log_level == SYSLOG_LEVEL_ERROR)
                    326:                                log_level = SYSLOG_LEVEL_DEBUG1;
                    327:                        else if (log_level < SYSLOG_LEVEL_DEBUG3)
                    328:                                log_level++;
                    329:                        break;
                    330:                default:
                    331:                        fprintf(stderr, "usage: %s [-v]\n", __progname);
                    332:                        exit(1);
                    333:                }
                    334:        }
                    335:
                    336:        log_init(__progname, log_level, log_facility, log_stderr);
                    337:
                    338:        pkcs11_init(0);
1.1       markus    339:        in = STDIN_FILENO;
                    340:        out = STDOUT_FILENO;
                    341:
                    342:        max = 0;
                    343:        if (in > max)
                    344:                max = in;
                    345:        if (out > max)
                    346:                max = out;
                    347:
1.14      markus    348:        if ((iqueue = sshbuf_new()) == NULL)
                    349:                fatal("%s: sshbuf_new failed", __func__);
                    350:        if ((oqueue = sshbuf_new()) == NULL)
                    351:                fatal("%s: sshbuf_new failed", __func__);
1.1       markus    352:
1.17      djm       353:        while (1) {
                    354:                memset(pfd, 0, sizeof(pfd));
                    355:                pfd[0].fd = in;
                    356:                pfd[1].fd = out;
1.1       markus    357:
                    358:                /*
                    359:                 * Ensure that we can read a full buffer and handle
                    360:                 * the worst-case length packet it can generate,
                    361:                 * otherwise apply backpressure by stopping reads.
                    362:                 */
1.14      markus    363:                if ((r = sshbuf_check_reserve(iqueue, sizeof(buf))) == 0 &&
                    364:                    (r = sshbuf_check_reserve(oqueue, MAX_MSG_LENGTH)) == 0)
1.17      djm       365:                        pfd[0].events = POLLIN;
1.14      markus    366:                else if (r != SSH_ERR_NO_BUFFER_SPACE)
                    367:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       markus    368:
1.17      djm       369:                if (sshbuf_len(oqueue) > 0)
                    370:                        pfd[1].events = POLLOUT;
1.1       markus    371:
1.17      djm       372:                if ((r = poll(pfd, 2, -1 /* INFTIM */)) <= 0) {
                    373:                        if (r == 0 || errno == EINTR)
1.1       markus    374:                                continue;
1.17      djm       375:                        fatal("poll: %s", strerror(errno));
1.1       markus    376:                }
                    377:
                    378:                /* copy stdin to iqueue */
1.17      djm       379:                if ((pfd[0].revents & (POLLIN|POLLERR)) != 0) {
1.1       markus    380:                        len = read(in, buf, sizeof buf);
                    381:                        if (len == 0) {
                    382:                                debug("read eof");
                    383:                                cleanup_exit(0);
                    384:                        } else if (len < 0) {
                    385:                                error("read: %s", strerror(errno));
                    386:                                cleanup_exit(1);
1.14      markus    387:                        } else if ((r = sshbuf_put(iqueue, buf, len)) != 0) {
                    388:                                fatal("%s: buffer error: %s",
                    389:                                    __func__, ssh_err(r));
1.1       markus    390:                        }
                    391:                }
                    392:                /* send oqueue to stdout */
1.17      djm       393:                if ((pfd[1].revents & (POLLOUT|POLLHUP)) != 0) {
                    394:                        len = write(out, sshbuf_ptr(oqueue),
                    395:                            sshbuf_len(oqueue));
1.1       markus    396:                        if (len < 0) {
                    397:                                error("write: %s", strerror(errno));
                    398:                                cleanup_exit(1);
1.14      markus    399:                        } else if ((r = sshbuf_consume(oqueue, len)) != 0) {
                    400:                                fatal("%s: buffer error: %s",
                    401:                                    __func__, ssh_err(r));
1.1       markus    402:                        }
                    403:                }
                    404:
                    405:                /*
                    406:                 * Process requests from client if we can fit the results
                    407:                 * into the output buffer, otherwise stop processing input
                    408:                 * and let the output queue drain.
                    409:                 */
1.14      markus    410:                if ((r = sshbuf_check_reserve(oqueue, MAX_MSG_LENGTH)) == 0)
1.1       markus    411:                        process();
1.14      markus    412:                else if (r != SSH_ERR_NO_BUFFER_SPACE)
                    413:                        fatal("%s: buffer error: %s", __func__, ssh_err(r));
1.1       markus    414:        }
                    415: }