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

1.103   ! naddy       1: /* $OpenBSD: authfd.c,v 1.102 2017/05/04 06:10:57 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.77      stevesk    47: #include <unistd.h>
1.94      djm        48: #include <errno.h>
1.1       deraadt    49:
1.80      deraadt    50: #include "xmalloc.h"
1.1       deraadt    51: #include "ssh.h"
                     52: #include "rsa.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.1       deraadt    84: /* Returns the number of the authentication fd, or -1 if there is none. */
1.2       provos     85: int
1.94      djm        86: ssh_get_authentication_socket(int *fdp)
1.1       deraadt    87: {
1.12      markus     88:        const char *authsocket;
1.94      djm        89:        int sock, oerrno;
1.12      markus     90:        struct sockaddr_un sunaddr;
                     91:
1.94      djm        92:        if (fdp != NULL)
                     93:                *fdp = -1;
                     94:
1.12      markus     95:        authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
                     96:        if (!authsocket)
1.94      djm        97:                return SSH_ERR_AGENT_NOT_PRESENT;
1.12      markus     98:
1.92      tedu       99:        memset(&sunaddr, 0, sizeof(sunaddr));
1.12      markus    100:        sunaddr.sun_family = AF_UNIX;
                    101:        strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
                    102:
1.94      djm       103:        if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
                    104:                return SSH_ERR_SYSTEM_ERROR;
1.12      markus    105:
                    106:        /* close on exec */
1.94      djm       107:        if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 ||
                    108:            connect(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) {
                    109:                oerrno = errno;
1.12      markus    110:                close(sock);
1.94      djm       111:                errno = oerrno;
                    112:                return SSH_ERR_SYSTEM_ERROR;
1.12      markus    113:        }
1.94      djm       114:        if (fdp != NULL)
                    115:                *fdp = sock;
                    116:        else
1.12      markus    117:                close(sock);
1.94      djm       118:        return 0;
1.1       deraadt   119: }
                    120:
1.94      djm       121: /* Communicate with agent: send request and read reply */
1.41      itojun    122: static int
1.94      djm       123: ssh_request_reply(int sock, struct sshbuf *request, struct sshbuf *reply)
1.24      markus    124: {
1.94      djm       125:        int r;
                    126:        size_t l, len;
1.24      markus    127:        char buf[1024];
                    128:
                    129:        /* Get the length of the message, and format it in the buffer. */
1.94      djm       130:        len = sshbuf_len(request);
1.74      djm       131:        put_u32(buf, len);
1.24      markus    132:
                    133:        /* Send the length and then the packet to the agent. */
1.94      djm       134:        if (atomicio(vwrite, sock, buf, 4) != 4 ||
                    135:            atomicio(vwrite, sock, (u_char *)sshbuf_ptr(request),
                    136:            sshbuf_len(request)) != sshbuf_len(request))
                    137:                return SSH_ERR_AGENT_COMMUNICATION;
1.24      markus    138:        /*
                    139:         * Wait for response from the agent.  First read the length of the
                    140:         * response packet.
                    141:         */
1.94      djm       142:        if (atomicio(read, sock, buf, 4) != 4)
                    143:            return SSH_ERR_AGENT_COMMUNICATION;
1.24      markus    144:
                    145:        /* Extract the length, and check it for sanity. */
1.74      djm       146:        len = get_u32(buf);
1.94      djm       147:        if (len > MAX_AGENT_REPLY_LEN)
                    148:                return SSH_ERR_INVALID_FORMAT;
1.24      markus    149:
                    150:        /* Read the rest of the response in to the buffer. */
1.94      djm       151:        sshbuf_reset(reply);
1.24      markus    152:        while (len > 0) {
                    153:                l = len;
                    154:                if (l > sizeof(buf))
                    155:                        l = sizeof(buf);
1.94      djm       156:                if (atomicio(read, sock, buf, l) != l)
                    157:                        return SSH_ERR_AGENT_COMMUNICATION;
                    158:                if ((r = sshbuf_put(reply, buf, l)) != 0)
                    159:                        return r;
1.24      markus    160:                len -= l;
                    161:        }
1.94      djm       162:        return 0;
1.24      markus    163: }
                    164:
1.14      markus    165: /*
                    166:  * Closes the agent socket if it should be closed (depends on how it was
                    167:  * obtained).  The argument must have been returned by
                    168:  * ssh_get_authentication_socket().
                    169:  */
1.18      markus    170: void
1.12      markus    171: ssh_close_authentication_socket(int sock)
1.1       deraadt   172: {
1.12      markus    173:        if (getenv(SSH_AUTHSOCKET_ENV_NAME))
                    174:                close(sock);
1.1       deraadt   175: }
                    176:
1.94      djm       177: /* Lock/unlock agent */
                    178: int
                    179: ssh_lock_agent(int sock, int lock, const char *password)
1.1       deraadt   180: {
1.94      djm       181:        int r;
                    182:        u_char type = lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK;
                    183:        struct sshbuf *msg;
                    184:
                    185:        if ((msg = sshbuf_new()) == NULL)
                    186:                return SSH_ERR_ALLOC_FAIL;
                    187:        if ((r = sshbuf_put_u8(msg, type)) != 0 ||
                    188:            (r = sshbuf_put_cstring(msg, password)) != 0)
                    189:                goto out;
                    190:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    191:                goto out;
                    192:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    193:                goto out;
                    194:        r = decode_reply(type);
                    195:  out:
                    196:        sshbuf_free(msg);
                    197:        return r;
1.1       deraadt   198: }
                    199:
1.50      markus    200:
1.94      djm       201: static int
                    202: deserialise_identity2(struct sshbuf *ids, struct sshkey **keyp, char **commentp)
1.50      markus    203: {
1.94      djm       204:        int r;
                    205:        char *comment = NULL;
                    206:        const u_char *blob;
                    207:        size_t blen;
                    208:
                    209:        if ((r = sshbuf_get_string_direct(ids, &blob, &blen)) != 0 ||
                    210:            (r = sshbuf_get_cstring(ids, &comment, NULL)) != 0)
                    211:                goto out;
                    212:        if ((r = sshkey_from_blob(blob, blen, keyp)) != 0)
                    213:                goto out;
                    214:        if (commentp != NULL) {
                    215:                *commentp = comment;
                    216:                comment = NULL;
                    217:        }
                    218:        r = 0;
                    219:  out:
                    220:        free(comment);
                    221:        return r;
1.1       deraadt   222: }
                    223:
1.14      markus    224: /*
1.94      djm       225:  * Fetch list of identities held by the agent.
1.14      markus    226:  */
1.30      markus    227: int
1.103   ! naddy     228: ssh_fetch_identitylist(int sock, struct ssh_identitylist **idlp)
1.1       deraadt   229: {
1.103   ! naddy     230:        u_char type;
1.94      djm       231:        u_int32_t num, i;
                    232:        struct sshbuf *msg;
                    233:        struct ssh_identitylist *idl = NULL;
                    234:        int r;
1.25      markus    235:
1.14      markus    236:        /*
                    237:         * Send a message to the agent requesting for a list of the
                    238:         * identities it can represent.
                    239:         */
1.94      djm       240:        if ((msg = sshbuf_new()) == NULL)
                    241:                return SSH_ERR_ALLOC_FAIL;
1.103   ! naddy     242:        if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_REQUEST_IDENTITIES)) != 0)
1.94      djm       243:                goto out;
1.12      markus    244:
1.94      djm       245:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    246:                goto out;
1.1       deraadt   247:
1.12      markus    248:        /* Get message type, and verify that we got a proper answer. */
1.94      djm       249:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    250:                goto out;
1.29      markus    251:        if (agent_failed(type)) {
1.94      djm       252:                r = SSH_ERR_AGENT_FAILURE;
                    253:                goto out;
1.103   ! naddy     254:        } else if (type != SSH2_AGENT_IDENTITIES_ANSWER) {
1.94      djm       255:                r = SSH_ERR_INVALID_FORMAT;
                    256:                goto out;
1.25      markus    257:        }
1.12      markus    258:
                    259:        /* Get the number of entries in the response and check it for sanity. */
1.94      djm       260:        if ((r = sshbuf_get_u32(msg, &num)) != 0)
                    261:                goto out;
                    262:        if (num > MAX_AGENT_IDENTITIES) {
                    263:                r = SSH_ERR_INVALID_FORMAT;
                    264:                goto out;
                    265:        }
                    266:        if (num == 0) {
                    267:                r = SSH_ERR_AGENT_NO_IDENTITIES;
                    268:                goto out;
                    269:        }
                    270:
                    271:        /* Deserialise the response into a list of keys/comments */
                    272:        if ((idl = calloc(1, sizeof(*idl))) == NULL ||
                    273:            (idl->keys = calloc(num, sizeof(*idl->keys))) == NULL ||
                    274:            (idl->comments = calloc(num, sizeof(*idl->comments))) == NULL) {
                    275:                r = SSH_ERR_ALLOC_FAIL;
                    276:                goto out;
                    277:        }
                    278:        for (i = 0; i < num;) {
1.103   ! naddy     279:                if ((r = deserialise_identity2(msg, &(idl->keys[i]),
        !           280:                    &(idl->comments[i]))) != 0) {
        !           281:                        if (r == SSH_ERR_KEY_TYPE_UNKNOWN) {
        !           282:                                /* Gracefully skip unknown key types */
        !           283:                                num--;
        !           284:                                continue;
        !           285:                        } else
        !           286:                                goto out;
1.94      djm       287:                }
                    288:                i++;
                    289:        }
                    290:        idl->nkeys = num;
                    291:        *idlp = idl;
                    292:        idl = NULL;
                    293:        r = 0;
                    294:  out:
                    295:        sshbuf_free(msg);
                    296:        if (idl != NULL)
                    297:                ssh_free_identitylist(idl);
                    298:        return r;
1.30      markus    299: }
                    300:
1.94      djm       301: void
                    302: ssh_free_identitylist(struct ssh_identitylist *idl)
1.30      markus    303: {
1.94      djm       304:        size_t i;
1.1       deraadt   305:
1.94      djm       306:        if (idl == NULL)
                    307:                return;
                    308:        for (i = 0; i < idl->nkeys; i++) {
                    309:                if (idl->keys != NULL)
                    310:                        sshkey_free(idl->keys[i]);
                    311:                if (idl->comments != NULL)
                    312:                        free(idl->comments[i]);
1.25      markus    313:        }
1.94      djm       314:        free(idl);
1.1       deraadt   315: }
                    316:
1.14      markus    317: /*
1.94      djm       318:  * Sends a challenge (typically from a server via ssh(1)) to the agent,
                    319:  * and waits for a response from the agent.
                    320:  * Returns true (non-zero) if the agent gave the correct answer, zero
                    321:  * otherwise.
1.14      markus    322:  */
1.1       deraadt   323:
                    324:
1.100     markus    325: /* encode signature algoritm in flag bits, so we can keep the msg format */
                    326: static u_int
                    327: agent_encode_alg(struct sshkey *key, const char *alg)
                    328: {
                    329:        if (alg != NULL && key->type == KEY_RSA) {
                    330:                if (strcmp(alg, "rsa-sha2-256") == 0)
                    331:                        return SSH_AGENT_RSA_SHA2_256;
                    332:                else if (strcmp(alg, "rsa-sha2-512") == 0)
                    333:                        return SSH_AGENT_RSA_SHA2_512;
                    334:        }
                    335:        return 0;
                    336: }
                    337:
1.94      djm       338: /* ask agent to sign data, returns err.h code on error, 0 on success */
1.25      markus    339: int
1.94      djm       340: ssh_agent_sign(int sock, struct sshkey *key,
                    341:     u_char **sigp, size_t *lenp,
1.100     markus    342:     const u_char *data, size_t datalen, const char *alg, u_int compat)
1.94      djm       343: {
                    344:        struct sshbuf *msg;
                    345:        u_char *blob = NULL, type;
                    346:        size_t blen = 0, len = 0;
                    347:        u_int flags = 0;
                    348:        int r = SSH_ERR_INTERNAL_ERROR;
                    349:
1.97      markus    350:        *sigp = NULL;
                    351:        *lenp = 0;
1.94      djm       352:
                    353:        if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
                    354:                return SSH_ERR_INVALID_ARGUMENT;
                    355:        if (compat & SSH_BUG_SIGBLOB)
                    356:                flags |= SSH_AGENT_OLD_SIGNATURE;
                    357:        if ((msg = sshbuf_new()) == NULL)
                    358:                return SSH_ERR_ALLOC_FAIL;
                    359:        if ((r = sshkey_to_blob(key, &blob, &blen)) != 0)
                    360:                goto out;
1.100     markus    361:        flags |= agent_encode_alg(key, alg);
1.94      djm       362:        if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
                    363:            (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
                    364:            (r = sshbuf_put_string(msg, data, datalen)) != 0 ||
                    365:            (r = sshbuf_put_u32(msg, flags)) != 0)
                    366:                goto out;
1.99      jsg       367:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
1.94      djm       368:                goto out;
                    369:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    370:                goto out;
1.29      markus    371:        if (agent_failed(type)) {
1.94      djm       372:                r = SSH_ERR_AGENT_FAILURE;
                    373:                goto out;
1.25      markus    374:        } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
1.94      djm       375:                r = SSH_ERR_INVALID_FORMAT;
                    376:                goto out;
                    377:        }
                    378:        if ((r = sshbuf_get_string(msg, sigp, &len)) != 0)
                    379:                goto out;
1.97      markus    380:        *lenp = len;
1.94      djm       381:        r = 0;
                    382:  out:
                    383:        if (blob != NULL) {
                    384:                explicit_bzero(blob, blen);
                    385:                free(blob);
1.25      markus    386:        }
1.94      djm       387:        sshbuf_free(msg);
                    388:        return r;
1.25      markus    389: }
                    390:
1.22      markus    391: /* Encode key for a message to the agent. */
                    392:
                    393:
1.94      djm       394: static int
                    395: ssh_encode_identity_ssh2(struct sshbuf *b, struct sshkey *key,
                    396:     const char *comment)
1.22      markus    397: {
1.94      djm       398:        int r;
                    399:
                    400:        if ((r = sshkey_private_serialize(key, b)) != 0 ||
                    401:            (r = sshbuf_put_cstring(b, comment)) != 0)
                    402:                return r;
                    403:        return 0;
                    404: }
                    405:
                    406: static int
                    407: encode_constraints(struct sshbuf *m, u_int life, u_int confirm)
                    408: {
                    409:        int r;
                    410:
                    411:        if (life != 0) {
                    412:                if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_LIFETIME)) != 0 ||
                    413:                    (r = sshbuf_put_u32(m, life)) != 0)
                    414:                        goto out;
                    415:        }
                    416:        if (confirm != 0) {
                    417:                if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_CONFIRM)) != 0)
                    418:                        goto out;
                    419:        }
                    420:        r = 0;
                    421:  out:
                    422:        return r;
1.22      markus    423: }
                    424:
1.14      markus    425: /*
1.94      djm       426:  * Adds an identity to the authentication server.
                    427:  * This call is intended only for use by ssh-add(1) and like applications.
1.14      markus    428:  */
1.18      markus    429: int
1.94      djm       430: ssh_add_identity_constrained(int sock, struct sshkey *key, const char *comment,
                    431:     u_int life, u_int confirm)
1.1       deraadt   432: {
1.94      djm       433:        struct sshbuf *msg;
                    434:        int r, constrained = (life || confirm);
                    435:        u_char type;
1.12      markus    436:
1.94      djm       437:        if ((msg = sshbuf_new()) == NULL)
                    438:                return SSH_ERR_ALLOC_FAIL;
1.22      markus    439:
                    440:        switch (key->type) {
1.93      markus    441: #ifdef WITH_OPENSSL
1.22      markus    442:        case KEY_RSA:
1.82      djm       443:        case KEY_RSA_CERT:
1.22      markus    444:        case KEY_DSA:
1.82      djm       445:        case KEY_DSA_CERT:
1.84      djm       446:        case KEY_ECDSA:
                    447:        case KEY_ECDSA_CERT:
1.93      markus    448: #endif
1.90      markus    449:        case KEY_ED25519:
                    450:        case KEY_ED25519_CERT:
1.54      markus    451:                type = constrained ?
                    452:                    SSH2_AGENTC_ADD_ID_CONSTRAINED :
                    453:                    SSH2_AGENTC_ADD_IDENTITY;
1.94      djm       454:                if ((r = sshbuf_put_u8(msg, type)) != 0 ||
                    455:                    (r = ssh_encode_identity_ssh2(msg, key, comment)) != 0)
                    456:                        goto out;
1.22      markus    457:                break;
                    458:        default:
1.94      djm       459:                r = SSH_ERR_INVALID_ARGUMENT;
                    460:                goto out;
1.12      markus    461:        }
1.94      djm       462:        if (constrained &&
                    463:            (r = encode_constraints(msg, life, confirm)) != 0)
                    464:                goto out;
                    465:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    466:                goto out;
                    467:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    468:                goto out;
                    469:        r = decode_reply(type);
                    470:  out:
                    471:        sshbuf_free(msg);
                    472:        return r;
1.54      markus    473: }
                    474:
1.14      markus    475: /*
1.94      djm       476:  * Removes an identity from the authentication server.
                    477:  * This call is intended only for use by ssh-add(1) and like applications.
1.14      markus    478:  */
1.18      markus    479: int
1.94      djm       480: ssh_remove_identity(int sock, struct sshkey *key)
1.1       deraadt   481: {
1.94      djm       482:        struct sshbuf *msg;
                    483:        int r;
                    484:        u_char type, *blob = NULL;
                    485:        size_t blen;
1.12      markus    486:
1.94      djm       487:        if ((msg = sshbuf_new()) == NULL)
                    488:                return SSH_ERR_ALLOC_FAIL;
1.12      markus    489:
1.93      markus    490:        if (key->type != KEY_UNSPEC) {
1.94      djm       491:                if ((r = sshkey_to_blob(key, &blob, &blen)) != 0)
                    492:                        goto out;
                    493:                if ((r = sshbuf_put_u8(msg,
                    494:                    SSH2_AGENTC_REMOVE_IDENTITY)) != 0 ||
                    495:                    (r = sshbuf_put_string(msg, blob, blen)) != 0)
                    496:                        goto out;
1.51      markus    497:        } else {
1.94      djm       498:                r = SSH_ERR_INVALID_ARGUMENT;
                    499:                goto out;
1.51      markus    500:        }
1.94      djm       501:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    502:                goto out;
                    503:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    504:                goto out;
                    505:        r = decode_reply(type);
                    506:  out:
                    507:        if (blob != NULL) {
                    508:                explicit_bzero(blob, blen);
                    509:                free(blob);
1.42      markus    510:        }
1.94      djm       511:        sshbuf_free(msg);
                    512:        return r;
1.42      markus    513: }
                    514:
1.94      djm       515: /*
                    516:  * Add/remove an token-based identity from the authentication server.
                    517:  * This call is intended only for use by ssh-add(1) and like applications.
                    518:  */
1.42      markus    519: int
1.94      djm       520: ssh_update_card(int sock, int add, const char *reader_id, const char *pin,
                    521:     u_int life, u_int confirm)
1.42      markus    522: {
1.94      djm       523:        struct sshbuf *msg;
                    524:        int r, constrained = (life || confirm);
                    525:        u_char type;
1.60      djm       526:
                    527:        if (add) {
                    528:                type = constrained ?
                    529:                    SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED :
                    530:                    SSH_AGENTC_ADD_SMARTCARD_KEY;
                    531:        } else
                    532:                type = SSH_AGENTC_REMOVE_SMARTCARD_KEY;
1.42      markus    533:
1.94      djm       534:        if ((msg = sshbuf_new()) == NULL)
                    535:                return SSH_ERR_ALLOC_FAIL;
                    536:        if ((r = sshbuf_put_u8(msg, type)) != 0 ||
                    537:            (r = sshbuf_put_cstring(msg, reader_id)) != 0 ||
                    538:            (r = sshbuf_put_cstring(msg, pin)) != 0)
                    539:                goto out;
                    540:        if (constrained &&
                    541:            (r = encode_constraints(msg, life, confirm)) != 0)
                    542:                goto out;
                    543:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    544:                goto out;
                    545:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    546:                goto out;
                    547:        r = decode_reply(type);
                    548:  out:
                    549:        sshbuf_free(msg);
                    550:        return r;
1.12      markus    551: }
                    552:
1.14      markus    553: /*
1.94      djm       554:  * Removes all identities from the agent.
                    555:  * This call is intended only for use by ssh-add(1) and like applications.
1.102     djm       556:  *
                    557:  * This supports the SSH protocol 1 message to because, when clearing all
                    558:  * keys from an agent, we generally want to clear both protocol v1 and v2
                    559:  * keys.
1.14      markus    560:  */
1.18      markus    561: int
1.94      djm       562: ssh_remove_all_identities(int sock, int version)
1.1       deraadt   563: {
1.94      djm       564:        struct sshbuf *msg;
                    565:        u_char type = (version == 1) ?
                    566:            SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
                    567:            SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
                    568:        int r;
                    569:
                    570:        if ((msg = sshbuf_new()) == NULL)
                    571:                return SSH_ERR_ALLOC_FAIL;
                    572:        if ((r = sshbuf_put_u8(msg, type)) != 0)
                    573:                goto out;
                    574:        if ((r = ssh_request_reply(sock, msg, msg)) != 0)
                    575:                goto out;
                    576:        if ((r = sshbuf_get_u8(msg, &type)) != 0)
                    577:                goto out;
                    578:        r = decode_reply(type);
                    579:  out:
                    580:        sshbuf_free(msg);
                    581:        return r;
1.12      markus    582: }