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

Annotation of src/usr.bin/ssh/ssh-sk-helper.c, Revision 1.1

1.1     ! djm         1: /* $OpenBSD$ */
        !             2: /*
        !             3:  * Copyright (c) 2019 Google LLC
        !             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:
        !            18: /*
        !            19:  * This is a tiny program used to isolate the address space used for
        !            20:  * security key middleware signing operations from ssh-agent. It is similar
        !            21:  * to ssh-pkcs11-helper.c but considerably simpler as the signing operation
        !            22:  * for this case are stateless.
        !            23:  *
        !            24:  * It receives a signing request (key, provider, message, flags) from
        !            25:  * stdin, attempts to perform a signature using the security key provider
        !            26:  * and returns the resultant signature via stdout.
        !            27:  *
        !            28:  * In the future, this program might gain additional functions to support
        !            29:  * FIDO2 tokens such as enumerating resident keys. When this happens it will
        !            30:  * be necessary to crank SSH_SK_HELPER_VERSION below.
        !            31:  */
        !            32:
        !            33: #include <stdarg.h>
        !            34: #include <stdio.h>
        !            35: #include <stdlib.h>
        !            36: #include <string.h>
        !            37: #include <unistd.h>
        !            38: #include <errno.h>
        !            39:
        !            40: #include "xmalloc.h"
        !            41: #include "log.h"
        !            42: #include "sshkey.h"
        !            43: #include "authfd.h"
        !            44: #include "misc.h"
        !            45: #include "sshbuf.h"
        !            46: #include "msg.h"
        !            47: #include "uidswap.h"
        !            48: #include "sshkey.h"
        !            49: #include "ssherr.h"
        !            50: #include "ssh-sk.h"
        !            51:
        !            52: extern char *__progname;
        !            53:
        !            54: int
        !            55: main(int argc, char **argv)
        !            56: {
        !            57:        SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
        !            58:        LogLevel log_level = SYSLOG_LEVEL_ERROR;
        !            59:        struct sshbuf *req, *resp, *kbuf;
        !            60:        struct sshkey *key;
        !            61:        uint32_t compat;
        !            62:        const u_char *message;
        !            63:        u_char version, *sig;
        !            64:        size_t msglen, siglen;
        !            65:        char *provider;
        !            66:        int in, out, ch, r, log_stderr = 0;
        !            67:
        !            68:        sanitise_stdfd();
        !            69:        log_init(__progname, log_level, log_facility, log_stderr);
        !            70:
        !            71:        while ((ch = getopt(argc, argv, "v")) != -1) {
        !            72:                switch (ch) {
        !            73:                case 'v':
        !            74:                        log_stderr = 1;
        !            75:                        if (log_level == SYSLOG_LEVEL_ERROR)
        !            76:                                log_level = SYSLOG_LEVEL_DEBUG1;
        !            77:                        else if (log_level < SYSLOG_LEVEL_DEBUG3)
        !            78:                                log_level++;
        !            79:                        break;
        !            80:                default:
        !            81:                        fprintf(stderr, "usage: %s [-v]\n", __progname);
        !            82:                        exit(1);
        !            83:                }
        !            84:        }
        !            85:        log_init(__progname, log_level, log_facility, log_stderr);
        !            86:
        !            87:        /*
        !            88:         * Rearrange our file descriptors a little; we don't trust the
        !            89:         * providers not to fiddle with stdin/out.
        !            90:         */
        !            91:        closefrom(STDERR_FILENO + 1);
        !            92:        if ((in = dup(STDIN_FILENO)) == -1 || (out = dup(STDOUT_FILENO)) == -1)
        !            93:                fatal("%s: dup: %s", __progname, strerror(errno));
        !            94:        close(STDIN_FILENO);
        !            95:        close(STDOUT_FILENO);
        !            96:        sanitise_stdfd(); /* resets to /dev/null */
        !            97:
        !            98:        if ((req = sshbuf_new()) == NULL || (resp = sshbuf_new()) == NULL)
        !            99:                fatal("%s: sshbuf_new failed", __progname);
        !           100:        if (ssh_msg_recv(in, req) < 0)
        !           101:                fatal("ssh_msg_recv failed");
        !           102:        close(in);
        !           103:        debug("%s: received message len %zu", __progname, sshbuf_len(req));
        !           104:
        !           105:        if ((r = sshbuf_get_u8(req, &version)) != 0)
        !           106:                fatal("%s: buffer error: %s", __progname, ssh_err(r));
        !           107:        if (version != SSH_SK_HELPER_VERSION) {
        !           108:                fatal("unsupported version: received %d, expected %d",
        !           109:                    version, SSH_SK_HELPER_VERSION);
        !           110:        }
        !           111:        if ((r = sshbuf_froms(req, &kbuf)) != 0 ||
        !           112:            (r = sshkey_private_deserialize(kbuf, &key)) != 0)
        !           113:                fatal("Unable to parse key: %s", ssh_err(r));
        !           114:        if (sshkey_type_plain(key->type) != KEY_ECDSA_SK)
        !           115:                fatal("Unsupported key type %s", sshkey_ssh_name(key));
        !           116:
        !           117:        if ((r = sshbuf_get_cstring(req, &provider, NULL)) != 0 ||
        !           118:            (r = sshbuf_get_string_direct(req, &message, &msglen)) != 0 ||
        !           119:            (r = sshbuf_get_u32(req, &compat)) != 0)
        !           120:                fatal("%s: buffer error: %s", __progname, ssh_err(r));
        !           121:        if (sshbuf_len(req) != 0)
        !           122:                fatal("%s: trailing data in request", __progname);
        !           123:
        !           124:        debug("%s: ready to sign with key %s, provider %s: "
        !           125:            "msg len %zu, compat 0x%lx", __progname, sshkey_type(key),
        !           126:            provider, msglen, (u_long)compat);
        !           127:
        !           128:        if ((r = sshsk_ecdsa_sign(provider, key, &sig, &siglen,
        !           129:            message, msglen, compat)) != 0)
        !           130:                fatal("Signing failed: %s", ssh_err(r));
        !           131:
        !           132:        /* send reply */
        !           133:        if ((r = sshbuf_put_string(resp, sig, siglen)) != 0)
        !           134:                fatal("%s: buffer error: %s", __progname, ssh_err(r));
        !           135:        debug("%s: reply len %zu", __progname, sshbuf_len(resp));
        !           136:        if (ssh_msg_send(out, SSH_SK_HELPER_VERSION, resp) == -1)
        !           137:                fatal("ssh_msg_send failed");
        !           138:        close(out);
        !           139:
        !           140:        return (0);
        !           141: }