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

Annotation of src/usr.bin/ssh/authfd.c, Revision 1.129

1.129   ! djm         1: /* $OpenBSD: authfd.c,v 1.128 2021/12/19 22:08:48 djm Exp $ */
1.1       deraadt     2: /*
1.13      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
1.27      deraadt     6:  * Functions for connecting the local authentication agent.
1.18      markus      7:  *
1.27      deraadt     8:  * As far as I am concerned, the code I have written for this software
                      9:  * can be used freely for any purpose.  Any derived versions of this
                     10:  * software must be clearly marked as such, and if the derived work is
                     11:  * incompatible with the protocol description in the RFC file, it must be
                     12:  * called by a name other than "ssh" or "Secure Shell".
1.18      markus     13:  *
1.25      markus     14:  * SSH2 implementation,
1.27      deraadt    15:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1.25      markus     16:  *
1.27      deraadt    17:  * Redistribution and use in source and binary forms, with or without
                     18:  * modification, are permitted provided that the following conditions
                     19:  * are met:
                     20:  * 1. Redistributions of source code must retain the above copyright
                     21:  *    notice, this list of conditions and the following disclaimer.
                     22:  * 2. Redistributions in binary form must reproduce the above copyright
                     23:  *    notice, this list of conditions and the following disclaimer in the
                     24:  *    documentation and/or other materials provided with the distribution.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     27:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     28:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     29:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     30:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     31:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     32:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     33:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     35:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.13      deraadt    36:  */
1.1       deraadt    37:
1.67      stevesk    38:
                     39: #include <sys/types.h>
                     40: #include <sys/un.h>
1.75      stevesk    41: #include <sys/socket.h>
1.33      markus     42:
1.76      stevesk    43: #include <fcntl.h>
1.79      stevesk    44: #include <stdlib.h>
1.80      deraadt    45: #include <signal.h>
1.78      stevesk    46: #include <string.h>
1.120     deraadt    47: #include <stdarg.h>
1.77      stevesk    48: #include <unistd.h>
1.94      djm        49: #include <errno.h>
1.1       deraadt    50:
1.80      deraadt    51: #include "xmalloc.h"
1.1       deraadt    52: #include "ssh.h"
1.94      djm        53: #include "sshbuf.h"
                     54: #include "sshkey.h"
1.22      markus     55: #include "authfd.h"
1.33      markus     56: #include "cipher.h"
1.28      markus     57: #include "compat.h"
1.33      markus     58: #include "log.h"
                     59: #include "atomicio.h"
1.74      djm        60: #include "misc.h"
1.94      djm        61: #include "ssherr.h"
1.2       provos     62:
1.94      djm        63: #define MAX_AGENT_IDENTITIES   2048            /* Max keys in agent reply */
1.126     djm        64: #define MAX_AGENT_REPLY_LEN    (256 * 1024)    /* Max bytes in agent reply */
1.21      markus     65:
1.29      markus     66: /* macro to check for "agent failure" message */
                     67: #define agent_failed(x) \
1.94      djm        68:     ((x == SSH_AGENT_FAILURE) || \
                     69:     (x == SSH_COM_AGENT2_FAILURE) || \
1.55      deraadt    70:     (x == SSH2_AGENT_FAILURE))
1.29      markus     71:
1.94      djm        72: /* Convert success/failure response from agent to a err.h status */
                     73: static int
                     74: decode_reply(u_char type)
1.57      stevesk    75: {
1.94      djm        76:        if (agent_failed(type))
                     77:                return SSH_ERR_AGENT_FAILURE;
                     78:        else if (type == SSH_AGENT_SUCCESS)
1.57      stevesk    79:                return 0;
1.94      djm        80:        else
                     81:                return SSH_ERR_INVALID_FORMAT;
1.57      stevesk    82: }
                     83:
1.121     djm        84: /*
                     85:  * Opens an authentication socket at the provided path and stores the file
                     86:  * descriptor in fdp. Returns 0 on success and an error on failure.
                     87:  */
1.2       provos     88: int
1.121     djm        89: ssh_get_authentication_socket_path(const char *authsocket, int *fdp)
1.1       deraadt    90: {
1.94      djm        91:        int sock, oerrno;
1.12      markus     92:        struct sockaddr_un sunaddr;
                     93:
1.92      tedu       94:        memset(&sunaddr, 0, sizeof(sunaddr));
1.12      markus     95:        sunaddr.sun_family = AF_UNIX;
                     96:        strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
                     97:
1.115     deraadt    98:        if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
1.94      djm        99:                return SSH_ERR_SYSTEM_ERROR;
1.12      markus    100:
                    101:        /* close on exec */
1.94      djm       102:        if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 ||
1.115     deraadt   103:            connect(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
1.94      djm       104:                oerrno = errno;
1.12      markus    105:                close(sock);
1.94      djm       106:                errno = oerrno;
                    107:                return SSH_ERR_SYSTEM_ERROR;
1.12      markus    108:        }
1.94      djm       109:        if (fdp != NULL)
                    110:                *fdp = sock;
                    111:        else
1.12      markus    112:                close(sock);
1.94      djm       113:        return 0;
1.121     djm       114: }
                    115:
                    116: /*
                    117:  * Opens the default authentication socket and stores the file descriptor in
                    118:  * fdp. Returns 0 on success and an error on failure.
                    119:  */
                    120: int
                    121: ssh_get_authentication_socket(int *fdp)
                    122: {
                    123:        const char *authsocket;
                    124:
                    125:        if (fdp != NULL)
                    126:                *fdp = -1;
                    127:
                    128:        authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
                    129:        if (authsocket == NULL || *authsocket == '\0')
                    130:                return SSH_ERR_AGENT_NOT_PRESENT;
                    131:
                    132:        return ssh_get_authentication_socket_path(authsocket, fdp);
1.1       deraadt   133: }
                    134:
1.94      djm       135: /* Communicate with agent: send request and read reply */
1.41      itojun    136: static int
1.94      djm       137: ssh_request_reply(int sock, struct sshbuf *request, struct sshbuf *reply)
1.24      markus    138: {
1.94      djm       139:        int r;
                    140:        size_t l, len;
1.24      markus    141:        char buf[1024];
                    142:
                    143:        /* Get the length of the message, and format it in the buffer. */
1.94      djm       144:        len = sshbuf_len(request);
1.108     markus    145:        POKE_U32(buf, len);
1.24      markus    146:
                    147:        /* Send the length and then the packet to the agent. */
1.94      djm       148:        if (atomicio(vwrite, sock, buf, 4) != 4 ||
1.111     markus    149:            atomicio(vwrite, sock, sshbuf_mutable_ptr(request),
1.94      djm       150:            sshbuf_len(request)) != sshbuf_len(request))
                    151:                return SSH_ERR_AGENT_COMMUNICATION;
1.24      markus    152:        /*
                    153:         * Wait for response from the agent.  First read the length of the
                    154:         * response packet.
                    155:         */
1.94      djm       156:        if (atomicio(read, sock, buf, 4) != 4)
                    157:            return SSH_ERR_AGENT_COMMUNICATION;
1.24      markus    158:
                    159:        /* Extract the length, and check it for sanity. */
1.108     markus    160:        len = PEEK_U32(buf);
1.94      djm       161:        if (len > MAX_AGENT_REPLY_LEN)
                    162:                return SSH_ERR_INVALID_FORMAT;
1.24      markus    163:
                    164:        /* Read the rest of the response in to the buffer. */
1.94      djm       165:        sshbuf_reset(reply);
1.24      markus    166:        while (len > 0) {
                    167:                l = len;
                    168:                if (l > sizeof(buf))
                    169:                        l = sizeof(buf);
1.94      djm       170:                if (atomicio(read, sock, buf, l) != l)
                    171:                        return SSH_ERR_AGENT_COMMUNICATION;
                    172:                if ((r = sshbuf_put(reply, buf, l)) != 0)
                    173:                        return r;
1.24      markus    174:                len -= l;
                    175:        }
1.94      djm       176:        return 0;
1.24      markus    177: }
                    178:
1.127     djm       179: /* Communicate with agent: sent request, read and decode status reply */
                    180: static int
                    181: ssh_request_reply_decode(int sock, struct sshbuf *request)
                    182: {
                    183:        struct sshbuf *reply;
                    184:        int r;
                    185:        u_char type;
                    186:
                    187:        if ((reply = sshbuf_new()) == NULL)
                    188:                return SSH_ERR_ALLOC_FAIL;
                    189:        if ((r = ssh_request_reply(sock, request, reply)) != 0 ||
                    190:            (r = sshbuf_get_u8(reply, &type)) != 0 ||
                    191:            (r = decode_reply(type)) != 0)
                    192:                goto out;
                    193:        /* success */
                    194:        r = 0;
                    195:  out:
                    196:        sshbuf_free(reply);
                    197:        return r;
                    198: }
                    199:
1.14      markus    200: /*
                    201:  * Closes the agent socket if it should be closed (depends on how it was
                    202:  * obtained).  The argument must have been returned by
                    203:  * ssh_get_authentication_socket().
                    204:  */
1.18      markus    205: void
1.12      markus    206: ssh_close_authentication_socket(int sock)
1.1       deraadt   207: {
1.12      markus    208:        if (getenv(SSH_AUTHSOCKET_ENV_NAME))
                    209:                close(sock);
1.1       deraadt   210: }
                    211:
1.94      djm       212: /* Lock/unlock agent */
                    213: int
                    214: ssh_lock_agent(int sock, int lock, const char *password)
1.1       deraadt   215: {
1.94      djm       216:        int r;
                    217:        u_char type = lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK;
                    218:        struct sshbuf *msg;
                    219:
                    220:        if ((msg = sshbuf_new()) == NULL)
                    221:                return SSH_ERR_ALLOC_FAIL;
                    222:        if ((r = sshbuf_put_u8(msg, type)) != 0 ||
1.127     djm       223:            (r = sshbuf_put_cstring(msg, password)) != 0 ||
                    224:            (r = ssh_request_reply_decode(sock, msg)) != 0)
1.94      djm       225:                goto out;
1.127     djm       226:        /* success */
                    227:        r = 0;
1.94      djm       228:  out:
                    229:        sshbuf_free(msg);
                    230:        return r;
1.1       deraadt   231: }
                    232:
1.50      markus    233:
1.94      djm       234: static int
                    235: deserialise_identity2(struct sshbuf *ids, struct sshkey **keyp, char **commentp)
1.50      markus    236: {
1.94      djm       237:        int r;
                    238:        char *comment = NULL;
                    239:        const u_char *blob;
                    240:        size_t blen;
                    241:
                    242:        if ((r = sshbuf_get_string_direct(ids, &blob, &blen)) != 0 ||
                    243:            (r = sshbuf_get_cstring(ids, &comment, NULL)) != 0)
                    244:                goto out;
                    245:        if ((r = sshkey_from_blob(blob, blen, keyp)) != 0)
                    246:                goto out;
                    247:        if (commentp != NULL) {
                    248:                *commentp = comment;
                    249:                comment = NULL;
                    250:        }
                    251:        r = 0;
                    252:  out:
                    253:        free(comment);
                    254:        return r;
1.1       deraadt   255: }
                    256:
1.14      markus    257: /*
1.94      djm       258:  * Fetch list of identities held by the agent.
1.14      markus    259:  */
1.30      markus    260: int
1.103     naddy     261: ssh_fetch_identitylist(int sock, struct ssh_identitylist **idlp)
1.1       deraadt   262: {
1.103     naddy     263:        u_char type;
1.94      djm       264:        u_int32_t num, i;
                    265:        struct sshbuf *msg;
                    266:        struct ssh_identitylist *idl = NULL;
                    267:        int r;
1.25      markus    268:
1.14      markus    269:        /*
                    270:         * Send a message to the agent requesting for a list of the
                    271:         * identities it can represent.
                    272:         */
1.94      djm       273:        if ((msg = sshbuf_new()) == NULL)
                    274:                return SSH_ERR_ALLOC_FAIL;
1.103     naddy     275:        if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_REQUEST_IDENTITIES)) != 0)
1.94      djm       276:                goto out;
1.12      markus    277:
1.94      djm       278:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    279:                goto out;
1.1       deraadt   280:
1.12      markus    281:        /* Get message type, and verify that we got a proper answer. */
1.94      djm       282:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    283:                goto out;
1.29      markus    284:        if (agent_failed(type)) {
1.94      djm       285:                r = SSH_ERR_AGENT_FAILURE;
                    286:                goto out;
1.103     naddy     287:        } else if (type != SSH2_AGENT_IDENTITIES_ANSWER) {
1.94      djm       288:                r = SSH_ERR_INVALID_FORMAT;
                    289:                goto out;
1.25      markus    290:        }
1.12      markus    291:
                    292:        /* Get the number of entries in the response and check it for sanity. */
1.94      djm       293:        if ((r = sshbuf_get_u32(msg, &num)) != 0)
                    294:                goto out;
                    295:        if (num > MAX_AGENT_IDENTITIES) {
                    296:                r = SSH_ERR_INVALID_FORMAT;
                    297:                goto out;
                    298:        }
                    299:        if (num == 0) {
                    300:                r = SSH_ERR_AGENT_NO_IDENTITIES;
                    301:                goto out;
                    302:        }
                    303:
                    304:        /* Deserialise the response into a list of keys/comments */
                    305:        if ((idl = calloc(1, sizeof(*idl))) == NULL ||
                    306:            (idl->keys = calloc(num, sizeof(*idl->keys))) == NULL ||
                    307:            (idl->comments = calloc(num, sizeof(*idl->comments))) == NULL) {
                    308:                r = SSH_ERR_ALLOC_FAIL;
                    309:                goto out;
                    310:        }
                    311:        for (i = 0; i < num;) {
1.103     naddy     312:                if ((r = deserialise_identity2(msg, &(idl->keys[i]),
                    313:                    &(idl->comments[i]))) != 0) {
                    314:                        if (r == SSH_ERR_KEY_TYPE_UNKNOWN) {
                    315:                                /* Gracefully skip unknown key types */
                    316:                                num--;
                    317:                                continue;
                    318:                        } else
                    319:                                goto out;
1.94      djm       320:                }
                    321:                i++;
                    322:        }
                    323:        idl->nkeys = num;
                    324:        *idlp = idl;
                    325:        idl = NULL;
                    326:        r = 0;
                    327:  out:
                    328:        sshbuf_free(msg);
                    329:        if (idl != NULL)
                    330:                ssh_free_identitylist(idl);
                    331:        return r;
1.30      markus    332: }
                    333:
1.94      djm       334: void
                    335: ssh_free_identitylist(struct ssh_identitylist *idl)
1.30      markus    336: {
1.94      djm       337:        size_t i;
1.1       deraadt   338:
1.94      djm       339:        if (idl == NULL)
                    340:                return;
                    341:        for (i = 0; i < idl->nkeys; i++) {
                    342:                if (idl->keys != NULL)
                    343:                        sshkey_free(idl->keys[i]);
                    344:                if (idl->comments != NULL)
                    345:                        free(idl->comments[i]);
1.25      markus    346:        }
1.116     djm       347:        free(idl->keys);
                    348:        free(idl->comments);
1.94      djm       349:        free(idl);
1.117     djm       350: }
                    351:
                    352: /*
                    353:  * Check if the ssh agent has a given key.
                    354:  * Returns 0 if found, or a negative SSH_ERR_* error code on failure.
                    355:  */
                    356: int
1.124     djm       357: ssh_agent_has_key(int sock, const struct sshkey *key)
1.117     djm       358: {
                    359:        int r, ret = SSH_ERR_KEY_NOT_FOUND;
                    360:        size_t i;
                    361:        struct ssh_identitylist *idlist = NULL;
                    362:
1.123     markus    363:        if ((r = ssh_fetch_identitylist(sock, &idlist)) != 0) {
1.117     djm       364:                return r;
                    365:        }
                    366:
                    367:        for (i = 0; i < idlist->nkeys; i++) {
                    368:                if (sshkey_equal_public(idlist->keys[i], key)) {
                    369:                        ret = 0;
                    370:                        break;
                    371:                }
                    372:        }
                    373:
                    374:        ssh_free_identitylist(idlist);
                    375:        return ret;
1.1       deraadt   376: }
                    377:
1.14      markus    378: /*
1.94      djm       379:  * Sends a challenge (typically from a server via ssh(1)) to the agent,
                    380:  * and waits for a response from the agent.
                    381:  * Returns true (non-zero) if the agent gave the correct answer, zero
                    382:  * otherwise.
1.14      markus    383:  */
1.1       deraadt   384:
                    385:
1.109     djm       386: /* encode signature algorithm in flag bits, so we can keep the msg format */
1.100     markus    387: static u_int
1.104     djm       388: agent_encode_alg(const struct sshkey *key, const char *alg)
1.100     markus    389: {
1.113     djm       390:        if (alg != NULL && sshkey_type_plain(key->type) == KEY_RSA) {
                    391:                if (strcmp(alg, "rsa-sha2-256") == 0 ||
                    392:                    strcmp(alg, "rsa-sha2-256-cert-v01@openssh.com") == 0)
1.100     markus    393:                        return SSH_AGENT_RSA_SHA2_256;
1.113     djm       394:                if (strcmp(alg, "rsa-sha2-512") == 0 ||
                    395:                    strcmp(alg, "rsa-sha2-512-cert-v01@openssh.com") == 0)
1.100     markus    396:                        return SSH_AGENT_RSA_SHA2_512;
                    397:        }
                    398:        return 0;
                    399: }
                    400:
1.94      djm       401: /* ask agent to sign data, returns err.h code on error, 0 on success */
1.25      markus    402: int
1.104     djm       403: ssh_agent_sign(int sock, const struct sshkey *key,
1.94      djm       404:     u_char **sigp, size_t *lenp,
1.100     markus    405:     const u_char *data, size_t datalen, const char *alg, u_int compat)
1.94      djm       406: {
                    407:        struct sshbuf *msg;
1.110     djm       408:        u_char *sig = NULL, type = 0;
                    409:        size_t len = 0;
1.94      djm       410:        u_int flags = 0;
                    411:        int r = SSH_ERR_INTERNAL_ERROR;
                    412:
1.97      markus    413:        *sigp = NULL;
                    414:        *lenp = 0;
1.94      djm       415:
                    416:        if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
                    417:                return SSH_ERR_INVALID_ARGUMENT;
                    418:        if ((msg = sshbuf_new()) == NULL)
                    419:                return SSH_ERR_ALLOC_FAIL;
1.100     markus    420:        flags |= agent_encode_alg(key, alg);
1.94      djm       421:        if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
1.110     djm       422:            (r = sshkey_puts(key, msg)) != 0 ||
1.94      djm       423:            (r = sshbuf_put_string(msg, data, datalen)) != 0 ||
                    424:            (r = sshbuf_put_u32(msg, flags)) != 0)
                    425:                goto out;
1.99      jsg       426:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
1.94      djm       427:                goto out;
                    428:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    429:                goto out;
1.29      markus    430:        if (agent_failed(type)) {
1.94      djm       431:                r = SSH_ERR_AGENT_FAILURE;
                    432:                goto out;
1.25      markus    433:        } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
1.94      djm       434:                r = SSH_ERR_INVALID_FORMAT;
                    435:                goto out;
                    436:        }
1.110     djm       437:        if ((r = sshbuf_get_string(msg, &sig, &len)) != 0)
                    438:                goto out;
                    439:        /* Check what we actually got back from the agent. */
                    440:        if ((r = sshkey_check_sigtype(sig, len, alg)) != 0)
1.94      djm       441:                goto out;
1.110     djm       442:        /* success */
                    443:        *sigp = sig;
1.97      markus    444:        *lenp = len;
1.110     djm       445:        sig = NULL;
                    446:        len = 0;
1.94      djm       447:        r = 0;
                    448:  out:
1.110     djm       449:        freezero(sig, len);
1.94      djm       450:        sshbuf_free(msg);
                    451:        return r;
1.25      markus    452: }
                    453:
1.22      markus    454: /* Encode key for a message to the agent. */
                    455:
1.129   ! djm       456: static int
        !           457: encode_dest_constraint_hop(struct sshbuf *m,
        !           458:     const struct dest_constraint_hop *dch)
        !           459: {
        !           460:        struct sshbuf *b;
        !           461:        u_int i;
        !           462:        int r;
        !           463:
        !           464:        if ((b = sshbuf_new()) == NULL)
        !           465:                return SSH_ERR_ALLOC_FAIL;
        !           466:        if ((r = sshbuf_put_cstring(b, dch->user)) != 0 ||
        !           467:            (r = sshbuf_put_cstring(b, dch->hostname)) != 0 ||
        !           468:            (r = sshbuf_put_string(b, NULL, 0)) != 0) /* reserved */
        !           469:                goto out;
        !           470:        for (i = 0; i < dch->nkeys; i++) {
        !           471:                if ((r = sshkey_puts(dch->keys[i], b)) != 0 ||
        !           472:                    (r = sshbuf_put_u8(b, dch->key_is_ca[i] != 0)) != 0)
        !           473:                        goto out;
        !           474:        }
        !           475:        if ((r = sshbuf_put_stringb(m, b)) != 0)
        !           476:                goto out;
        !           477:        /* success */
        !           478:        r = 0;
        !           479:  out:
        !           480:        sshbuf_free(b);
        !           481:        return r;
        !           482: }
        !           483:
        !           484: static int
        !           485: encode_dest_constraint(struct sshbuf *m, const struct dest_constraint *dc)
        !           486: {
        !           487:        struct sshbuf *b;
        !           488:        int r;
        !           489:
        !           490:        if ((b = sshbuf_new()) == NULL)
        !           491:                return SSH_ERR_ALLOC_FAIL;
        !           492:        if ((r = encode_dest_constraint_hop(b, &dc->from) != 0) ||
        !           493:            (r = encode_dest_constraint_hop(b, &dc->to) != 0) ||
        !           494:            (r = sshbuf_put_string(b, NULL, 0)) != 0) /* reserved */
        !           495:                goto out;
        !           496:        if ((r = sshbuf_put_stringb(m, b)) != 0)
        !           497:                goto out;
        !           498:        /* success */
        !           499:        r = 0;
        !           500:  out:
        !           501:        sshbuf_free(b);
        !           502:        return r;
        !           503: }
1.22      markus    504:
1.94      djm       505: static int
1.118     djm       506: encode_constraints(struct sshbuf *m, u_int life, u_int confirm, u_int maxsign,
1.129   ! djm       507:     const char *provider, struct dest_constraint **dest_constraints,
        !           508:     size_t ndest_constraints)
1.94      djm       509: {
                    510:        int r;
1.129   ! djm       511:        struct sshbuf *b = NULL;
        !           512:        size_t i;
1.94      djm       513:
                    514:        if (life != 0) {
                    515:                if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_LIFETIME)) != 0 ||
                    516:                    (r = sshbuf_put_u32(m, life)) != 0)
                    517:                        goto out;
                    518:        }
                    519:        if (confirm != 0) {
                    520:                if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_CONFIRM)) != 0)
                    521:                        goto out;
                    522:        }
1.108     markus    523:        if (maxsign != 0) {
                    524:                if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_MAXSIGN)) != 0 ||
                    525:                    (r = sshbuf_put_u32(m, maxsign)) != 0)
                    526:                        goto out;
                    527:        }
1.118     djm       528:        if (provider != NULL) {
                    529:                if ((r = sshbuf_put_u8(m,
                    530:                    SSH_AGENT_CONSTRAIN_EXTENSION)) != 0 ||
                    531:                    (r = sshbuf_put_cstring(m,
                    532:                    "sk-provider@openssh.com")) != 0 ||
                    533:                    (r = sshbuf_put_cstring(m, provider)) != 0)
                    534:                        goto out;
                    535:        }
1.129   ! djm       536:        if (dest_constraints != NULL && ndest_constraints > 0) {
        !           537:                if ((b = sshbuf_new()) == NULL) {
        !           538:                        r = SSH_ERR_ALLOC_FAIL;
        !           539:                        goto out;
        !           540:                }
        !           541:                for (i = 0; i < ndest_constraints; i++) {
        !           542:                        if ((r = encode_dest_constraint(b,
        !           543:                            dest_constraints[i])) != 0)
        !           544:                                goto out;
        !           545:                }
        !           546:                if ((r = sshbuf_put_u8(m,
        !           547:                    SSH_AGENT_CONSTRAIN_EXTENSION)) != 0 ||
        !           548:                    (r = sshbuf_put_cstring(m,
        !           549:                    "restrict-destination-v00@openssh.com")) != 0 ||
        !           550:                    (r = sshbuf_put_stringb(m, b)) != 0)
        !           551:                        goto out;
        !           552:        }
1.94      djm       553:        r = 0;
                    554:  out:
1.129   ! djm       555:        sshbuf_free(b);
1.94      djm       556:        return r;
1.22      markus    557: }
                    558:
1.14      markus    559: /*
1.94      djm       560:  * Adds an identity to the authentication server.
                    561:  * This call is intended only for use by ssh-add(1) and like applications.
1.14      markus    562:  */
1.18      markus    563: int
1.114     djm       564: ssh_add_identity_constrained(int sock, struct sshkey *key,
1.118     djm       565:     const char *comment, u_int life, u_int confirm, u_int maxsign,
1.129   ! djm       566:     const char *provider, struct dest_constraint **dest_constraints,
        !           567:     size_t ndest_constraints)
1.1       deraadt   568: {
1.94      djm       569:        struct sshbuf *msg;
1.129   ! djm       570:        int r, constrained = (life || confirm || maxsign ||
        !           571:            provider || dest_constraints);
1.94      djm       572:        u_char type;
1.12      markus    573:
1.94      djm       574:        if ((msg = sshbuf_new()) == NULL)
                    575:                return SSH_ERR_ALLOC_FAIL;
1.22      markus    576:
                    577:        switch (key->type) {
1.93      markus    578: #ifdef WITH_OPENSSL
1.22      markus    579:        case KEY_RSA:
1.82      djm       580:        case KEY_RSA_CERT:
1.22      markus    581:        case KEY_DSA:
1.82      djm       582:        case KEY_DSA_CERT:
1.84      djm       583:        case KEY_ECDSA:
                    584:        case KEY_ECDSA_CERT:
1.118     djm       585:        case KEY_ECDSA_SK:
                    586:        case KEY_ECDSA_SK_CERT:
1.93      markus    587: #endif
1.90      markus    588:        case KEY_ED25519:
                    589:        case KEY_ED25519_CERT:
1.119     markus    590:        case KEY_ED25519_SK:
                    591:        case KEY_ED25519_SK_CERT:
1.108     markus    592:        case KEY_XMSS:
                    593:        case KEY_XMSS_CERT:
1.54      markus    594:                type = constrained ?
                    595:                    SSH2_AGENTC_ADD_ID_CONSTRAINED :
                    596:                    SSH2_AGENTC_ADD_IDENTITY;
1.94      djm       597:                if ((r = sshbuf_put_u8(msg, type)) != 0 ||
1.108     markus    598:                    (r = sshkey_private_serialize_maxsign(key, msg, maxsign,
1.125     dtucker   599:                    0)) != 0 ||
1.108     markus    600:                    (r = sshbuf_put_cstring(msg, comment)) != 0)
1.94      djm       601:                        goto out;
1.22      markus    602:                break;
                    603:        default:
1.94      djm       604:                r = SSH_ERR_INVALID_ARGUMENT;
                    605:                goto out;
1.12      markus    606:        }
1.94      djm       607:        if (constrained &&
1.118     djm       608:            (r = encode_constraints(msg, life, confirm, maxsign,
1.129   ! djm       609:            provider, dest_constraints, ndest_constraints)) != 0)
1.94      djm       610:                goto out;
1.127     djm       611:        if ((r = ssh_request_reply_decode(sock, msg)) != 0)
1.94      djm       612:                goto out;
1.127     djm       613:        /* success */
                    614:        r = 0;
1.94      djm       615:  out:
                    616:        sshbuf_free(msg);
                    617:        return r;
1.54      markus    618: }
                    619:
1.14      markus    620: /*
1.94      djm       621:  * Removes an identity from the authentication server.
                    622:  * This call is intended only for use by ssh-add(1) and like applications.
1.14      markus    623:  */
1.18      markus    624: int
1.124     djm       625: ssh_remove_identity(int sock, const struct sshkey *key)
1.1       deraadt   626: {
1.94      djm       627:        struct sshbuf *msg;
                    628:        int r;
1.127     djm       629:        u_char *blob = NULL;
1.94      djm       630:        size_t blen;
1.12      markus    631:
1.94      djm       632:        if ((msg = sshbuf_new()) == NULL)
                    633:                return SSH_ERR_ALLOC_FAIL;
1.12      markus    634:
1.93      markus    635:        if (key->type != KEY_UNSPEC) {
1.94      djm       636:                if ((r = sshkey_to_blob(key, &blob, &blen)) != 0)
                    637:                        goto out;
                    638:                if ((r = sshbuf_put_u8(msg,
                    639:                    SSH2_AGENTC_REMOVE_IDENTITY)) != 0 ||
                    640:                    (r = sshbuf_put_string(msg, blob, blen)) != 0)
                    641:                        goto out;
1.51      markus    642:        } else {
1.94      djm       643:                r = SSH_ERR_INVALID_ARGUMENT;
                    644:                goto out;
1.51      markus    645:        }
1.127     djm       646:        if ((r = ssh_request_reply_decode(sock, msg)) != 0)
1.94      djm       647:                goto out;
1.127     djm       648:        /* success */
                    649:        r = 0;
1.94      djm       650:  out:
1.122     jsg       651:        if (blob != NULL)
                    652:                freezero(blob, blen);
1.94      djm       653:        sshbuf_free(msg);
                    654:        return r;
1.42      markus    655: }
                    656:
1.94      djm       657: /*
                    658:  * Add/remove an token-based identity from the authentication server.
                    659:  * This call is intended only for use by ssh-add(1) and like applications.
                    660:  */
1.42      markus    661: int
1.94      djm       662: ssh_update_card(int sock, int add, const char *reader_id, const char *pin,
1.129   ! djm       663:     u_int life, u_int confirm,
        !           664:     struct dest_constraint **dest_constraints, size_t ndest_constraints)
1.42      markus    665: {
1.94      djm       666:        struct sshbuf *msg;
                    667:        int r, constrained = (life || confirm);
                    668:        u_char type;
1.60      djm       669:
                    670:        if (add) {
                    671:                type = constrained ?
                    672:                    SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED :
                    673:                    SSH_AGENTC_ADD_SMARTCARD_KEY;
                    674:        } else
                    675:                type = SSH_AGENTC_REMOVE_SMARTCARD_KEY;
1.42      markus    676:
1.94      djm       677:        if ((msg = sshbuf_new()) == NULL)
                    678:                return SSH_ERR_ALLOC_FAIL;
                    679:        if ((r = sshbuf_put_u8(msg, type)) != 0 ||
                    680:            (r = sshbuf_put_cstring(msg, reader_id)) != 0 ||
                    681:            (r = sshbuf_put_cstring(msg, pin)) != 0)
                    682:                goto out;
                    683:        if (constrained &&
1.129   ! djm       684:            (r = encode_constraints(msg, life, confirm, 0, NULL,
        !           685:            dest_constraints, ndest_constraints)) != 0)
1.94      djm       686:                goto out;
1.127     djm       687:        if ((r = ssh_request_reply_decode(sock, msg)) != 0)
1.94      djm       688:                goto out;
1.127     djm       689:        /* success */
                    690:        r = 0;
1.94      djm       691:  out:
                    692:        sshbuf_free(msg);
                    693:        return r;
1.12      markus    694: }
                    695:
1.14      markus    696: /*
1.94      djm       697:  * Removes all identities from the agent.
                    698:  * This call is intended only for use by ssh-add(1) and like applications.
1.102     djm       699:  *
                    700:  * This supports the SSH protocol 1 message to because, when clearing all
                    701:  * keys from an agent, we generally want to clear both protocol v1 and v2
                    702:  * keys.
1.14      markus    703:  */
1.18      markus    704: int
1.94      djm       705: ssh_remove_all_identities(int sock, int version)
1.1       deraadt   706: {
1.94      djm       707:        struct sshbuf *msg;
                    708:        u_char type = (version == 1) ?
                    709:            SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
                    710:            SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
                    711:        int r;
                    712:
                    713:        if ((msg = sshbuf_new()) == NULL)
                    714:                return SSH_ERR_ALLOC_FAIL;
                    715:        if ((r = sshbuf_put_u8(msg, type)) != 0)
1.128     djm       716:                goto out;
                    717:        if ((r = ssh_request_reply_decode(sock, msg)) != 0)
                    718:                goto out;
                    719:        /* success */
                    720:        r = 0;
                    721:  out:
                    722:        sshbuf_free(msg);
                    723:        return r;
                    724: }
                    725:
                    726: /* Binds a session ID to a hostkey via the initial KEX signature. */
                    727: int
                    728: ssh_agent_bind_hostkey(int sock, const struct sshkey *key,
                    729:     const struct sshbuf *session_id, const struct sshbuf *signature,
                    730:     int forwarding)
                    731: {
                    732:        struct sshbuf *msg;
                    733:        int r;
                    734:
                    735:        if (key == NULL || session_id == NULL || signature == NULL)
                    736:                return SSH_ERR_INVALID_ARGUMENT;
                    737:        if ((msg = sshbuf_new()) == NULL)
                    738:                return SSH_ERR_ALLOC_FAIL;
                    739:        if ((r = sshbuf_put_u8(msg, SSH_AGENTC_EXTENSION)) != 0 ||
                    740:            (r = sshbuf_put_cstring(msg, "session-bind@openssh.com")) != 0 ||
                    741:            (r = sshkey_puts(key, msg)) != 0 ||
                    742:            (r = sshbuf_put_stringb(msg, session_id)) != 0 ||
                    743:            (r = sshbuf_put_stringb(msg, signature)) != 0 ||
                    744:            (r = sshbuf_put_u8(msg, forwarding ? 1 : 0)) != 0)
1.94      djm       745:                goto out;
1.127     djm       746:        if ((r = ssh_request_reply_decode(sock, msg)) != 0)
1.94      djm       747:                goto out;
1.127     djm       748:        /* success */
                    749:        r = 0;
1.94      djm       750:  out:
                    751:        sshbuf_free(msg);
                    752:        return r;
1.12      markus    753: }