[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.122

1.122   ! jsg         1: /* $OpenBSD: authfd.c,v 1.121 2019/12/21 02:19:13 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 */
                     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.14      markus    179: /*
                    180:  * Closes the agent socket if it should be closed (depends on how it was
                    181:  * obtained).  The argument must have been returned by
                    182:  * ssh_get_authentication_socket().
                    183:  */
1.18      markus    184: void
1.12      markus    185: ssh_close_authentication_socket(int sock)
1.1       deraadt   186: {
1.12      markus    187:        if (getenv(SSH_AUTHSOCKET_ENV_NAME))
                    188:                close(sock);
1.1       deraadt   189: }
                    190:
1.94      djm       191: /* Lock/unlock agent */
                    192: int
                    193: ssh_lock_agent(int sock, int lock, const char *password)
1.1       deraadt   194: {
1.94      djm       195:        int r;
                    196:        u_char type = lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK;
                    197:        struct sshbuf *msg;
                    198:
                    199:        if ((msg = sshbuf_new()) == NULL)
                    200:                return SSH_ERR_ALLOC_FAIL;
                    201:        if ((r = sshbuf_put_u8(msg, type)) != 0 ||
                    202:            (r = sshbuf_put_cstring(msg, password)) != 0)
                    203:                goto out;
                    204:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    205:                goto out;
                    206:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    207:                goto out;
                    208:        r = decode_reply(type);
                    209:  out:
                    210:        sshbuf_free(msg);
                    211:        return r;
1.1       deraadt   212: }
                    213:
1.50      markus    214:
1.94      djm       215: static int
                    216: deserialise_identity2(struct sshbuf *ids, struct sshkey **keyp, char **commentp)
1.50      markus    217: {
1.94      djm       218:        int r;
                    219:        char *comment = NULL;
                    220:        const u_char *blob;
                    221:        size_t blen;
                    222:
                    223:        if ((r = sshbuf_get_string_direct(ids, &blob, &blen)) != 0 ||
                    224:            (r = sshbuf_get_cstring(ids, &comment, NULL)) != 0)
                    225:                goto out;
                    226:        if ((r = sshkey_from_blob(blob, blen, keyp)) != 0)
                    227:                goto out;
                    228:        if (commentp != NULL) {
                    229:                *commentp = comment;
                    230:                comment = NULL;
                    231:        }
                    232:        r = 0;
                    233:  out:
                    234:        free(comment);
                    235:        return r;
1.1       deraadt   236: }
                    237:
1.14      markus    238: /*
1.94      djm       239:  * Fetch list of identities held by the agent.
1.14      markus    240:  */
1.30      markus    241: int
1.103     naddy     242: ssh_fetch_identitylist(int sock, struct ssh_identitylist **idlp)
1.1       deraadt   243: {
1.103     naddy     244:        u_char type;
1.94      djm       245:        u_int32_t num, i;
                    246:        struct sshbuf *msg;
                    247:        struct ssh_identitylist *idl = NULL;
                    248:        int r;
1.25      markus    249:
1.14      markus    250:        /*
                    251:         * Send a message to the agent requesting for a list of the
                    252:         * identities it can represent.
                    253:         */
1.94      djm       254:        if ((msg = sshbuf_new()) == NULL)
                    255:                return SSH_ERR_ALLOC_FAIL;
1.103     naddy     256:        if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_REQUEST_IDENTITIES)) != 0)
1.94      djm       257:                goto out;
1.12      markus    258:
1.94      djm       259:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    260:                goto out;
1.1       deraadt   261:
1.12      markus    262:        /* Get message type, and verify that we got a proper answer. */
1.94      djm       263:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    264:                goto out;
1.29      markus    265:        if (agent_failed(type)) {
1.94      djm       266:                r = SSH_ERR_AGENT_FAILURE;
                    267:                goto out;
1.103     naddy     268:        } else if (type != SSH2_AGENT_IDENTITIES_ANSWER) {
1.94      djm       269:                r = SSH_ERR_INVALID_FORMAT;
                    270:                goto out;
1.25      markus    271:        }
1.12      markus    272:
                    273:        /* Get the number of entries in the response and check it for sanity. */
1.94      djm       274:        if ((r = sshbuf_get_u32(msg, &num)) != 0)
                    275:                goto out;
                    276:        if (num > MAX_AGENT_IDENTITIES) {
                    277:                r = SSH_ERR_INVALID_FORMAT;
                    278:                goto out;
                    279:        }
                    280:        if (num == 0) {
                    281:                r = SSH_ERR_AGENT_NO_IDENTITIES;
                    282:                goto out;
                    283:        }
                    284:
                    285:        /* Deserialise the response into a list of keys/comments */
                    286:        if ((idl = calloc(1, sizeof(*idl))) == NULL ||
                    287:            (idl->keys = calloc(num, sizeof(*idl->keys))) == NULL ||
                    288:            (idl->comments = calloc(num, sizeof(*idl->comments))) == NULL) {
                    289:                r = SSH_ERR_ALLOC_FAIL;
                    290:                goto out;
                    291:        }
                    292:        for (i = 0; i < num;) {
1.103     naddy     293:                if ((r = deserialise_identity2(msg, &(idl->keys[i]),
                    294:                    &(idl->comments[i]))) != 0) {
                    295:                        if (r == SSH_ERR_KEY_TYPE_UNKNOWN) {
                    296:                                /* Gracefully skip unknown key types */
                    297:                                num--;
                    298:                                continue;
                    299:                        } else
                    300:                                goto out;
1.94      djm       301:                }
                    302:                i++;
                    303:        }
                    304:        idl->nkeys = num;
                    305:        *idlp = idl;
                    306:        idl = NULL;
                    307:        r = 0;
                    308:  out:
                    309:        sshbuf_free(msg);
                    310:        if (idl != NULL)
                    311:                ssh_free_identitylist(idl);
                    312:        return r;
1.30      markus    313: }
                    314:
1.94      djm       315: void
                    316: ssh_free_identitylist(struct ssh_identitylist *idl)
1.30      markus    317: {
1.94      djm       318:        size_t i;
1.1       deraadt   319:
1.94      djm       320:        if (idl == NULL)
                    321:                return;
                    322:        for (i = 0; i < idl->nkeys; i++) {
                    323:                if (idl->keys != NULL)
                    324:                        sshkey_free(idl->keys[i]);
                    325:                if (idl->comments != NULL)
                    326:                        free(idl->comments[i]);
1.25      markus    327:        }
1.116     djm       328:        free(idl->keys);
                    329:        free(idl->comments);
1.94      djm       330:        free(idl);
1.117     djm       331: }
                    332:
                    333: /*
                    334:  * Check if the ssh agent has a given key.
                    335:  * Returns 0 if found, or a negative SSH_ERR_* error code on failure.
                    336:  */
                    337: int
                    338: ssh_agent_has_key(int sock, struct sshkey *key)
                    339: {
                    340:        int r, ret = SSH_ERR_KEY_NOT_FOUND;
                    341:        size_t i;
                    342:        struct ssh_identitylist *idlist = NULL;
                    343:
                    344:        if ((r = ssh_fetch_identitylist(sock, &idlist)) < 0) {
                    345:                return r;
                    346:        }
                    347:
                    348:        for (i = 0; i < idlist->nkeys; i++) {
                    349:                if (sshkey_equal_public(idlist->keys[i], key)) {
                    350:                        ret = 0;
                    351:                        break;
                    352:                }
                    353:        }
                    354:
                    355:        ssh_free_identitylist(idlist);
                    356:        return ret;
1.1       deraadt   357: }
                    358:
1.14      markus    359: /*
1.94      djm       360:  * Sends a challenge (typically from a server via ssh(1)) to the agent,
                    361:  * and waits for a response from the agent.
                    362:  * Returns true (non-zero) if the agent gave the correct answer, zero
                    363:  * otherwise.
1.14      markus    364:  */
1.1       deraadt   365:
                    366:
1.109     djm       367: /* encode signature algorithm in flag bits, so we can keep the msg format */
1.100     markus    368: static u_int
1.104     djm       369: agent_encode_alg(const struct sshkey *key, const char *alg)
1.100     markus    370: {
1.113     djm       371:        if (alg != NULL && sshkey_type_plain(key->type) == KEY_RSA) {
                    372:                if (strcmp(alg, "rsa-sha2-256") == 0 ||
                    373:                    strcmp(alg, "rsa-sha2-256-cert-v01@openssh.com") == 0)
1.100     markus    374:                        return SSH_AGENT_RSA_SHA2_256;
1.113     djm       375:                if (strcmp(alg, "rsa-sha2-512") == 0 ||
                    376:                    strcmp(alg, "rsa-sha2-512-cert-v01@openssh.com") == 0)
1.100     markus    377:                        return SSH_AGENT_RSA_SHA2_512;
                    378:        }
                    379:        return 0;
                    380: }
                    381:
1.94      djm       382: /* ask agent to sign data, returns err.h code on error, 0 on success */
1.25      markus    383: int
1.104     djm       384: ssh_agent_sign(int sock, const struct sshkey *key,
1.94      djm       385:     u_char **sigp, size_t *lenp,
1.100     markus    386:     const u_char *data, size_t datalen, const char *alg, u_int compat)
1.94      djm       387: {
                    388:        struct sshbuf *msg;
1.110     djm       389:        u_char *sig = NULL, type = 0;
                    390:        size_t len = 0;
1.94      djm       391:        u_int flags = 0;
                    392:        int r = SSH_ERR_INTERNAL_ERROR;
                    393:
1.97      markus    394:        *sigp = NULL;
                    395:        *lenp = 0;
1.94      djm       396:
                    397:        if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
                    398:                return SSH_ERR_INVALID_ARGUMENT;
                    399:        if ((msg = sshbuf_new()) == NULL)
                    400:                return SSH_ERR_ALLOC_FAIL;
1.100     markus    401:        flags |= agent_encode_alg(key, alg);
1.94      djm       402:        if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
1.110     djm       403:            (r = sshkey_puts(key, msg)) != 0 ||
1.94      djm       404:            (r = sshbuf_put_string(msg, data, datalen)) != 0 ||
                    405:            (r = sshbuf_put_u32(msg, flags)) != 0)
                    406:                goto out;
1.99      jsg       407:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
1.94      djm       408:                goto out;
                    409:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    410:                goto out;
1.29      markus    411:        if (agent_failed(type)) {
1.94      djm       412:                r = SSH_ERR_AGENT_FAILURE;
                    413:                goto out;
1.25      markus    414:        } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
1.94      djm       415:                r = SSH_ERR_INVALID_FORMAT;
                    416:                goto out;
                    417:        }
1.110     djm       418:        if ((r = sshbuf_get_string(msg, &sig, &len)) != 0)
                    419:                goto out;
                    420:        /* Check what we actually got back from the agent. */
                    421:        if ((r = sshkey_check_sigtype(sig, len, alg)) != 0)
1.94      djm       422:                goto out;
1.110     djm       423:        /* success */
                    424:        *sigp = sig;
1.97      markus    425:        *lenp = len;
1.110     djm       426:        sig = NULL;
                    427:        len = 0;
1.94      djm       428:        r = 0;
                    429:  out:
1.110     djm       430:        freezero(sig, len);
1.94      djm       431:        sshbuf_free(msg);
                    432:        return r;
1.25      markus    433: }
                    434:
1.22      markus    435: /* Encode key for a message to the agent. */
                    436:
                    437:
1.94      djm       438: static int
1.118     djm       439: encode_constraints(struct sshbuf *m, u_int life, u_int confirm, u_int maxsign,
                    440:     const char *provider)
1.94      djm       441: {
                    442:        int r;
                    443:
                    444:        if (life != 0) {
                    445:                if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_LIFETIME)) != 0 ||
                    446:                    (r = sshbuf_put_u32(m, life)) != 0)
                    447:                        goto out;
                    448:        }
                    449:        if (confirm != 0) {
                    450:                if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_CONFIRM)) != 0)
                    451:                        goto out;
                    452:        }
1.108     markus    453:        if (maxsign != 0) {
                    454:                if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_MAXSIGN)) != 0 ||
                    455:                    (r = sshbuf_put_u32(m, maxsign)) != 0)
                    456:                        goto out;
                    457:        }
1.118     djm       458:        if (provider != NULL) {
                    459:                if ((r = sshbuf_put_u8(m,
                    460:                    SSH_AGENT_CONSTRAIN_EXTENSION)) != 0 ||
                    461:                    (r = sshbuf_put_cstring(m,
                    462:                    "sk-provider@openssh.com")) != 0 ||
                    463:                    (r = sshbuf_put_cstring(m, provider)) != 0)
                    464:                        goto out;
                    465:        }
1.94      djm       466:        r = 0;
                    467:  out:
                    468:        return r;
1.22      markus    469: }
                    470:
1.14      markus    471: /*
1.94      djm       472:  * Adds an identity to the authentication server.
                    473:  * This call is intended only for use by ssh-add(1) and like applications.
1.14      markus    474:  */
1.18      markus    475: int
1.114     djm       476: ssh_add_identity_constrained(int sock, struct sshkey *key,
1.118     djm       477:     const char *comment, u_int life, u_int confirm, u_int maxsign,
                    478:     const char *provider)
1.1       deraadt   479: {
1.94      djm       480:        struct sshbuf *msg;
1.118     djm       481:        int r, constrained = (life || confirm || maxsign || provider);
1.94      djm       482:        u_char type;
1.12      markus    483:
1.94      djm       484:        if ((msg = sshbuf_new()) == NULL)
                    485:                return SSH_ERR_ALLOC_FAIL;
1.22      markus    486:
                    487:        switch (key->type) {
1.93      markus    488: #ifdef WITH_OPENSSL
1.22      markus    489:        case KEY_RSA:
1.82      djm       490:        case KEY_RSA_CERT:
1.22      markus    491:        case KEY_DSA:
1.82      djm       492:        case KEY_DSA_CERT:
1.84      djm       493:        case KEY_ECDSA:
                    494:        case KEY_ECDSA_CERT:
1.118     djm       495:        case KEY_ECDSA_SK:
                    496:        case KEY_ECDSA_SK_CERT:
1.93      markus    497: #endif
1.90      markus    498:        case KEY_ED25519:
                    499:        case KEY_ED25519_CERT:
1.119     markus    500:        case KEY_ED25519_SK:
                    501:        case KEY_ED25519_SK_CERT:
1.108     markus    502:        case KEY_XMSS:
                    503:        case KEY_XMSS_CERT:
1.54      markus    504:                type = constrained ?
                    505:                    SSH2_AGENTC_ADD_ID_CONSTRAINED :
                    506:                    SSH2_AGENTC_ADD_IDENTITY;
1.94      djm       507:                if ((r = sshbuf_put_u8(msg, type)) != 0 ||
1.108     markus    508:                    (r = sshkey_private_serialize_maxsign(key, msg, maxsign,
                    509:                    NULL)) != 0 ||
                    510:                    (r = sshbuf_put_cstring(msg, comment)) != 0)
1.94      djm       511:                        goto out;
1.22      markus    512:                break;
                    513:        default:
1.94      djm       514:                r = SSH_ERR_INVALID_ARGUMENT;
                    515:                goto out;
1.12      markus    516:        }
1.94      djm       517:        if (constrained &&
1.118     djm       518:            (r = encode_constraints(msg, life, confirm, maxsign,
                    519:            provider)) != 0)
1.94      djm       520:                goto out;
                    521:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    522:                goto out;
                    523:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    524:                goto out;
                    525:        r = decode_reply(type);
                    526:  out:
                    527:        sshbuf_free(msg);
                    528:        return r;
1.54      markus    529: }
                    530:
1.14      markus    531: /*
1.94      djm       532:  * Removes an identity from the authentication server.
                    533:  * This call is intended only for use by ssh-add(1) and like applications.
1.14      markus    534:  */
1.18      markus    535: int
1.94      djm       536: ssh_remove_identity(int sock, struct sshkey *key)
1.1       deraadt   537: {
1.94      djm       538:        struct sshbuf *msg;
                    539:        int r;
                    540:        u_char type, *blob = NULL;
                    541:        size_t blen;
1.12      markus    542:
1.94      djm       543:        if ((msg = sshbuf_new()) == NULL)
                    544:                return SSH_ERR_ALLOC_FAIL;
1.12      markus    545:
1.93      markus    546:        if (key->type != KEY_UNSPEC) {
1.94      djm       547:                if ((r = sshkey_to_blob(key, &blob, &blen)) != 0)
                    548:                        goto out;
                    549:                if ((r = sshbuf_put_u8(msg,
                    550:                    SSH2_AGENTC_REMOVE_IDENTITY)) != 0 ||
                    551:                    (r = sshbuf_put_string(msg, blob, blen)) != 0)
                    552:                        goto out;
1.51      markus    553:        } else {
1.94      djm       554:                r = SSH_ERR_INVALID_ARGUMENT;
                    555:                goto out;
1.51      markus    556:        }
1.94      djm       557:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    558:                goto out;
                    559:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    560:                goto out;
                    561:        r = decode_reply(type);
                    562:  out:
1.122   ! jsg       563:        if (blob != NULL)
        !           564:                freezero(blob, blen);
1.94      djm       565:        sshbuf_free(msg);
                    566:        return r;
1.42      markus    567: }
                    568:
1.94      djm       569: /*
                    570:  * Add/remove an token-based identity from the authentication server.
                    571:  * This call is intended only for use by ssh-add(1) and like applications.
                    572:  */
1.42      markus    573: int
1.94      djm       574: ssh_update_card(int sock, int add, const char *reader_id, const char *pin,
                    575:     u_int life, u_int confirm)
1.42      markus    576: {
1.94      djm       577:        struct sshbuf *msg;
                    578:        int r, constrained = (life || confirm);
                    579:        u_char type;
1.60      djm       580:
                    581:        if (add) {
                    582:                type = constrained ?
                    583:                    SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED :
                    584:                    SSH_AGENTC_ADD_SMARTCARD_KEY;
                    585:        } else
                    586:                type = SSH_AGENTC_REMOVE_SMARTCARD_KEY;
1.42      markus    587:
1.94      djm       588:        if ((msg = sshbuf_new()) == NULL)
                    589:                return SSH_ERR_ALLOC_FAIL;
                    590:        if ((r = sshbuf_put_u8(msg, type)) != 0 ||
                    591:            (r = sshbuf_put_cstring(msg, reader_id)) != 0 ||
                    592:            (r = sshbuf_put_cstring(msg, pin)) != 0)
                    593:                goto out;
                    594:        if (constrained &&
1.118     djm       595:            (r = encode_constraints(msg, life, confirm, 0, NULL)) != 0)
1.94      djm       596:                goto out;
                    597:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    598:                goto out;
                    599:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    600:                goto out;
                    601:        r = decode_reply(type);
                    602:  out:
                    603:        sshbuf_free(msg);
                    604:        return r;
1.12      markus    605: }
                    606:
1.14      markus    607: /*
1.94      djm       608:  * Removes all identities from the agent.
                    609:  * This call is intended only for use by ssh-add(1) and like applications.
1.102     djm       610:  *
                    611:  * This supports the SSH protocol 1 message to because, when clearing all
                    612:  * keys from an agent, we generally want to clear both protocol v1 and v2
                    613:  * keys.
1.14      markus    614:  */
1.18      markus    615: int
1.94      djm       616: ssh_remove_all_identities(int sock, int version)
1.1       deraadt   617: {
1.94      djm       618:        struct sshbuf *msg;
                    619:        u_char type = (version == 1) ?
                    620:            SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
                    621:            SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
                    622:        int r;
                    623:
                    624:        if ((msg = sshbuf_new()) == NULL)
                    625:                return SSH_ERR_ALLOC_FAIL;
                    626:        if ((r = sshbuf_put_u8(msg, type)) != 0)
                    627:                goto out;
                    628:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    629:                goto out;
                    630:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    631:                goto out;
                    632:        r = decode_reply(type);
                    633:  out:
                    634:        sshbuf_free(msg);
                    635:        return r;
1.12      markus    636: }