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

1.1       deraadt     1: /*
1.13      deraadt     2:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      3:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      4:  *                    All rights reserved
1.27      deraadt     5:  * Functions for connecting the local authentication agent.
1.18      markus      6:  *
1.27      deraadt     7:  * As far as I am concerned, the code I have written for this software
                      8:  * can be used freely for any purpose.  Any derived versions of this
                      9:  * software must be clearly marked as such, and if the derived work is
                     10:  * incompatible with the protocol description in the RFC file, it must be
                     11:  * called by a name other than "ssh" or "Secure Shell".
1.18      markus     12:  *
1.25      markus     13:  * SSH2 implementation,
1.27      deraadt    14:  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
1.25      markus     15:  *
1.27      deraadt    16:  * Redistribution and use in source and binary forms, with or without
                     17:  * modification, are permitted provided that the following conditions
                     18:  * are met:
                     19:  * 1. Redistributions of source code must retain the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer.
                     21:  * 2. Redistributions in binary form must reproduce the above copyright
                     22:  *    notice, this list of conditions and the following disclaimer in the
                     23:  *    documentation and/or other materials provided with the distribution.
                     24:  *
                     25:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     26:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
                     27:  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
                     28:  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
                     29:  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
                     30:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
                     31:  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
                     32:  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
                     33:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
                     34:  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.13      deraadt    35:  */
1.1       deraadt    36:
                     37: #include "includes.h"
1.44    ! markus     38: RCSID("$OpenBSD: authfd.c,v 1.43 2001/08/01 22:03:33 markus Exp $");
1.33      markus     39:
                     40: #include <openssl/evp.h>
1.1       deraadt    41:
                     42: #include "ssh.h"
                     43: #include "rsa.h"
                     44: #include "buffer.h"
                     45: #include "bufaux.h"
                     46: #include "xmalloc.h"
                     47: #include "getput.h"
1.22      markus     48: #include "key.h"
                     49: #include "authfd.h"
1.33      markus     50: #include "cipher.h"
1.22      markus     51: #include "kex.h"
1.28      markus     52: #include "compat.h"
1.33      markus     53: #include "log.h"
                     54: #include "atomicio.h"
1.2       provos     55:
1.21      markus     56: /* helper */
1.24      markus     57: int    decode_reply(int type);
1.21      markus     58:
1.29      markus     59: /* macro to check for "agent failure" message */
                     60: #define agent_failed(x) \
1.44    ! markus     61:     ((x == SSH_AGENT_FAILURE) || (x == SSH_COM_AGENT2_FAILURE) || \
        !            62:      (x == SSH2_AGENT_FAILURE))
1.29      markus     63:
1.1       deraadt    64: /* Returns the number of the authentication fd, or -1 if there is none. */
                     65:
1.2       provos     66: int
1.32      markus     67: ssh_get_authentication_socket(void)
1.1       deraadt    68: {
1.12      markus     69:        const char *authsocket;
1.23      deraadt    70:        int sock, len;
1.12      markus     71:        struct sockaddr_un sunaddr;
                     72:
                     73:        authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
                     74:        if (!authsocket)
                     75:                return -1;
                     76:
                     77:        sunaddr.sun_family = AF_UNIX;
                     78:        strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
1.36      deraadt    79:        len = SUN_LEN(&sunaddr)+1;
                     80:        sunaddr.sun_len = len;
1.12      markus     81:
                     82:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                     83:        if (sock < 0)
                     84:                return -1;
                     85:
                     86:        /* close on exec */
                     87:        if (fcntl(sock, F_SETFD, 1) == -1) {
                     88:                close(sock);
                     89:                return -1;
                     90:        }
1.23      deraadt    91:        if (connect(sock, (struct sockaddr *) & sunaddr, len) < 0) {
1.12      markus     92:                close(sock);
                     93:                return -1;
                     94:        }
                     95:        return sock;
1.1       deraadt    96: }
                     97:
1.41      itojun     98: static int
1.25      markus     99: ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply)
1.24      markus    100: {
                    101:        int l, len;
                    102:        char buf[1024];
                    103:
                    104:        /* Get the length of the message, and format it in the buffer. */
                    105:        len = buffer_len(request);
                    106:        PUT_32BIT(buf, len);
                    107:
                    108:        /* Send the length and then the packet to the agent. */
                    109:        if (atomicio(write, auth->fd, buf, 4) != 4 ||
                    110:            atomicio(write, auth->fd, buffer_ptr(request),
                    111:            buffer_len(request)) != buffer_len(request)) {
                    112:                error("Error writing to authentication socket.");
                    113:                return 0;
                    114:        }
                    115:        /*
                    116:         * Wait for response from the agent.  First read the length of the
                    117:         * response packet.
                    118:         */
                    119:        len = 4;
                    120:        while (len > 0) {
                    121:                l = read(auth->fd, buf + 4 - len, len);
1.38      deraadt   122:                if (l == -1 && (errno == EAGAIN || errno == EINTR))
1.39      markus    123:                        continue;
1.24      markus    124:                if (l <= 0) {
                    125:                        error("Error reading response length from authentication socket.");
                    126:                        return 0;
                    127:                }
                    128:                len -= l;
                    129:        }
                    130:
                    131:        /* Extract the length, and check it for sanity. */
                    132:        len = GET_32BIT(buf);
                    133:        if (len > 256 * 1024)
                    134:                fatal("Authentication response too long: %d", len);
                    135:
                    136:        /* Read the rest of the response in to the buffer. */
                    137:        buffer_clear(reply);
                    138:        while (len > 0) {
                    139:                l = len;
                    140:                if (l > sizeof(buf))
                    141:                        l = sizeof(buf);
                    142:                l = read(auth->fd, buf, l);
1.38      deraadt   143:                if (l == -1 && (errno == EAGAIN || errno == EINTR))
1.39      markus    144:                        continue;
1.24      markus    145:                if (l <= 0) {
                    146:                        error("Error reading response from authentication socket.");
                    147:                        return 0;
                    148:                }
                    149:                buffer_append(reply, (char *) buf, l);
                    150:                len -= l;
                    151:        }
                    152:        return 1;
                    153: }
                    154:
1.14      markus    155: /*
                    156:  * Closes the agent socket if it should be closed (depends on how it was
                    157:  * obtained).  The argument must have been returned by
                    158:  * ssh_get_authentication_socket().
                    159:  */
1.1       deraadt   160:
1.18      markus    161: void
1.12      markus    162: ssh_close_authentication_socket(int sock)
1.1       deraadt   163: {
1.12      markus    164:        if (getenv(SSH_AUTHSOCKET_ENV_NAME))
                    165:                close(sock);
1.1       deraadt   166: }
                    167:
1.14      markus    168: /*
                    169:  * Opens and connects a private socket for communication with the
                    170:  * authentication agent.  Returns the file descriptor (which must be
                    171:  * shut down and closed by the caller when no longer needed).
                    172:  * Returns NULL if an error occurred and the connection could not be
                    173:  * opened.
                    174:  */
1.1       deraadt   175:
1.12      markus    176: AuthenticationConnection *
1.32      markus    177: ssh_get_authentication_connection(void)
1.1       deraadt   178: {
1.12      markus    179:        AuthenticationConnection *auth;
                    180:        int sock;
1.1       deraadt   181:
1.12      markus    182:        sock = ssh_get_authentication_socket();
                    183:
1.14      markus    184:        /*
                    185:         * Fail if we couldn't obtain a connection.  This happens if we
                    186:         * exited due to a timeout.
                    187:         */
1.12      markus    188:        if (sock < 0)
                    189:                return NULL;
                    190:
                    191:        auth = xmalloc(sizeof(*auth));
                    192:        auth->fd = sock;
                    193:        buffer_init(&auth->identities);
                    194:        auth->howmany = 0;
                    195:
                    196:        return auth;
1.1       deraadt   197: }
                    198:
1.14      markus    199: /*
                    200:  * Closes the connection to the authentication agent and frees any associated
                    201:  * memory.
                    202:  */
1.1       deraadt   203:
1.18      markus    204: void
1.25      markus    205: ssh_close_authentication_connection(AuthenticationConnection *auth)
1.1       deraadt   206: {
1.25      markus    207:        buffer_free(&auth->identities);
                    208:        close(auth->fd);
                    209:        xfree(auth);
1.1       deraadt   210: }
                    211:
1.14      markus    212: /*
                    213:  * Returns the first authentication identity held by the agent.
                    214:  */
1.1       deraadt   215:
1.30      markus    216: int
                    217: ssh_get_num_identities(AuthenticationConnection *auth, int version)
1.1       deraadt   218: {
1.25      markus    219:        int type, code1 = 0, code2 = 0;
1.24      markus    220:        Buffer request;
1.25      markus    221:
                    222:        switch(version){
                    223:        case 1:
                    224:                code1 = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
                    225:                code2 = SSH_AGENT_RSA_IDENTITIES_ANSWER;
                    226:                break;
                    227:        case 2:
                    228:                code1 = SSH2_AGENTC_REQUEST_IDENTITIES;
                    229:                code2 = SSH2_AGENT_IDENTITIES_ANSWER;
                    230:                break;
                    231:        default:
1.30      markus    232:                return 0;
1.25      markus    233:        }
1.1       deraadt   234:
1.14      markus    235:        /*
                    236:         * Send a message to the agent requesting for a list of the
                    237:         * identities it can represent.
                    238:         */
1.24      markus    239:        buffer_init(&request);
1.25      markus    240:        buffer_put_char(&request, code1);
1.12      markus    241:
                    242:        buffer_clear(&auth->identities);
1.24      markus    243:        if (ssh_request_reply(auth, &request, &auth->identities) == 0) {
                    244:                buffer_free(&request);
1.30      markus    245:                return 0;
1.12      markus    246:        }
1.24      markus    247:        buffer_free(&request);
1.1       deraadt   248:
1.12      markus    249:        /* Get message type, and verify that we got a proper answer. */
1.24      markus    250:        type = buffer_get_char(&auth->identities);
1.29      markus    251:        if (agent_failed(type)) {
1.30      markus    252:                return 0;
1.25      markus    253:        } else if (type != code2) {
1.24      markus    254:                fatal("Bad authentication reply message type: %d", type);
1.25      markus    255:        }
1.12      markus    256:
                    257:        /* Get the number of entries in the response and check it for sanity. */
                    258:        auth->howmany = buffer_get_int(&auth->identities);
                    259:        if (auth->howmany > 1024)
1.37      millert   260:                fatal("Too many identities in authentication reply: %d",
1.24      markus    261:                    auth->howmany);
1.12      markus    262:
1.30      markus    263:        return auth->howmany;
                    264: }
                    265:
                    266: Key *
                    267: ssh_get_first_identity(AuthenticationConnection *auth, char **comment, int version)
                    268: {
                    269:        /* get number of identities and return the first entry (if any). */
                    270:        if (ssh_get_num_identities(auth, version) > 0)
                    271:                return ssh_get_next_identity(auth, comment, version);
                    272:        return NULL;
1.1       deraadt   273: }
                    274:
1.25      markus    275: Key *
                    276: ssh_get_next_identity(AuthenticationConnection *auth, char **comment, int version)
1.1       deraadt   277: {
1.31      markus    278:        u_int bits;
                    279:        u_char *blob;
                    280:        u_int blen;
1.25      markus    281:        Key *key = NULL;
1.9       markus    282:
1.12      markus    283:        /* Return failure if no more entries. */
                    284:        if (auth->howmany <= 0)
1.25      markus    285:                return NULL;
1.12      markus    286:
1.14      markus    287:        /*
                    288:         * Get the next entry from the packet.  These will abort with a fatal
                    289:         * error if the packet is too short or contains corrupt data.
                    290:         */
1.25      markus    291:        switch(version){
                    292:        case 1:
1.30      markus    293:                key = key_new(KEY_RSA1);
1.25      markus    294:                bits = buffer_get_int(&auth->identities);
                    295:                buffer_get_bignum(&auth->identities, key->rsa->e);
                    296:                buffer_get_bignum(&auth->identities, key->rsa->n);
                    297:                *comment = buffer_get_string(&auth->identities, NULL);
                    298:                if (bits != BN_num_bits(key->rsa->n))
                    299:                        log("Warning: identity keysize mismatch: actual %d, announced %u",
                    300:                            BN_num_bits(key->rsa->n), bits);
                    301:                break;
                    302:        case 2:
                    303:                blob = buffer_get_string(&auth->identities, &blen);
                    304:                *comment = buffer_get_string(&auth->identities, NULL);
1.30      markus    305:                key = key_from_blob(blob, blen);
1.25      markus    306:                xfree(blob);
                    307:                break;
                    308:        default:
                    309:                return NULL;
                    310:                break;
                    311:        }
1.12      markus    312:        /* Decrement the number of remaining entries. */
                    313:        auth->howmany--;
1.25      markus    314:        return key;
1.1       deraadt   315: }
                    316:
1.14      markus    317: /*
                    318:  * Generates a random challenge, sends it to the agent, and waits for
                    319:  * response from the agent.  Returns true (non-zero) if the agent gave the
                    320:  * correct answer, zero otherwise.  Response type selects the style of
                    321:  * response desired, with 0 corresponding to protocol version 1.0 (no longer
                    322:  * supported) and 1 corresponding to protocol version 1.1.
                    323:  */
1.1       deraadt   324:
1.2       provos    325: int
                    326: ssh_decrypt_challenge(AuthenticationConnection *auth,
1.25      markus    327:     Key* key, BIGNUM *challenge,
1.31      markus    328:     u_char session_id[16],
                    329:     u_int response_type,
                    330:     u_char response[16])
1.1       deraadt   331: {
1.12      markus    332:        Buffer buffer;
1.24      markus    333:        int success = 0;
                    334:        int i;
                    335:        int type;
1.12      markus    336:
1.30      markus    337:        if (key->type != KEY_RSA1)
1.25      markus    338:                return 0;
                    339:        if (response_type == 0) {
                    340:                log("Compatibility with ssh protocol version 1.0 no longer supported.");
                    341:                return 0;
                    342:        }
1.12      markus    343:        buffer_init(&buffer);
1.24      markus    344:        buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE);
1.25      markus    345:        buffer_put_int(&buffer, BN_num_bits(key->rsa->n));
                    346:        buffer_put_bignum(&buffer, key->rsa->e);
                    347:        buffer_put_bignum(&buffer, key->rsa->n);
1.12      markus    348:        buffer_put_bignum(&buffer, challenge);
                    349:        buffer_append(&buffer, (char *) session_id, 16);
                    350:        buffer_put_int(&buffer, response_type);
                    351:
1.24      markus    352:        if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
1.12      markus    353:                buffer_free(&buffer);
                    354:                return 0;
                    355:        }
1.24      markus    356:        type = buffer_get_char(&buffer);
1.12      markus    357:
1.29      markus    358:        if (agent_failed(type)) {
1.12      markus    359:                log("Agent admitted failure to authenticate using the key.");
1.24      markus    360:        } else if (type != SSH_AGENT_RSA_RESPONSE) {
                    361:                fatal("Bad authentication response: %d", type);
                    362:        } else {
                    363:                success = 1;
                    364:                /*
                    365:                 * Get the response from the packet.  This will abort with a
                    366:                 * fatal error if the packet is corrupt.
                    367:                 */
                    368:                for (i = 0; i < 16; i++)
                    369:                        response[i] = buffer_get_char(&buffer);
1.12      markus    370:        }
                    371:        buffer_free(&buffer);
1.24      markus    372:        return success;
1.12      markus    373: }
1.1       deraadt   374:
1.25      markus    375: /* ask agent to sign data, returns -1 on error, 0 on success */
                    376: int
                    377: ssh_agent_sign(AuthenticationConnection *auth,
                    378:     Key *key,
1.31      markus    379:     u_char **sigp, int *lenp,
                    380:     u_char *data, int datalen)
1.25      markus    381: {
1.28      markus    382:        extern int datafellows;
1.25      markus    383:        Buffer msg;
1.31      markus    384:        u_char *blob;
                    385:        u_int blen;
1.28      markus    386:        int type, flags = 0;
1.25      markus    387:        int ret = -1;
                    388:
1.30      markus    389:        if (key_to_blob(key, &blob, &blen) == 0)
1.25      markus    390:                return -1;
                    391:
1.28      markus    392:        if (datafellows & SSH_BUG_SIGBLOB)
                    393:                flags = SSH_AGENT_OLD_SIGNATURE;
                    394:
1.25      markus    395:        buffer_init(&msg);
                    396:        buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
                    397:        buffer_put_string(&msg, blob, blen);
                    398:        buffer_put_string(&msg, data, datalen);
1.28      markus    399:        buffer_put_int(&msg, flags);
1.25      markus    400:        xfree(blob);
                    401:
                    402:        if (ssh_request_reply(auth, &msg, &msg) == 0) {
                    403:                buffer_free(&msg);
                    404:                return -1;
                    405:        }
                    406:        type = buffer_get_char(&msg);
1.29      markus    407:        if (agent_failed(type)) {
1.25      markus    408:                log("Agent admitted failure to sign using the key.");
                    409:        } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
                    410:                fatal("Bad authentication response: %d", type);
                    411:        } else {
                    412:                ret = 0;
                    413:                *sigp = buffer_get_string(&msg, lenp);
                    414:        }
                    415:        buffer_free(&msg);
                    416:        return ret;
                    417: }
                    418:
1.22      markus    419: /* Encode key for a message to the agent. */
                    420:
1.41      itojun    421: static void
1.30      markus    422: ssh_encode_identity_rsa1(Buffer *b, RSA *key, const char *comment)
1.22      markus    423: {
                    424:        buffer_clear(b);
                    425:        buffer_put_char(b, SSH_AGENTC_ADD_RSA_IDENTITY);
                    426:        buffer_put_int(b, BN_num_bits(key->n));
                    427:        buffer_put_bignum(b, key->n);
                    428:        buffer_put_bignum(b, key->e);
                    429:        buffer_put_bignum(b, key->d);
                    430:        /* To keep within the protocol: p < q for ssh. in SSL p > q */
                    431:        buffer_put_bignum(b, key->iqmp);        /* ssh key->u */
                    432:        buffer_put_bignum(b, key->q);   /* ssh key->p, SSL key->q */
                    433:        buffer_put_bignum(b, key->p);   /* ssh key->q, SSL key->p */
1.40      markus    434:        buffer_put_cstring(b, comment);
1.22      markus    435: }
                    436:
1.41      itojun    437: static void
1.30      markus    438: ssh_encode_identity_ssh2(Buffer *b, Key *key, const char *comment)
1.22      markus    439: {
                    440:        buffer_clear(b);
                    441:        buffer_put_char(b, SSH2_AGENTC_ADD_IDENTITY);
1.30      markus    442:        buffer_put_cstring(b, key_ssh_name(key));
                    443:        switch(key->type){
                    444:        case KEY_RSA:
                    445:                buffer_put_bignum2(b, key->rsa->n);
                    446:                buffer_put_bignum2(b, key->rsa->e);
                    447:                buffer_put_bignum2(b, key->rsa->d);
                    448:                buffer_put_bignum2(b, key->rsa->iqmp);
                    449:                buffer_put_bignum2(b, key->rsa->p);
                    450:                buffer_put_bignum2(b, key->rsa->q);
                    451:                break;
                    452:        case KEY_DSA:
                    453:                buffer_put_bignum2(b, key->dsa->p);
                    454:                buffer_put_bignum2(b, key->dsa->q);
                    455:                buffer_put_bignum2(b, key->dsa->g);
                    456:                buffer_put_bignum2(b, key->dsa->pub_key);
                    457:                buffer_put_bignum2(b, key->dsa->priv_key);
                    458:                break;
                    459:        }
                    460:        buffer_put_cstring(b, comment);
1.22      markus    461: }
                    462:
1.14      markus    463: /*
                    464:  * Adds an identity to the authentication server.  This call is not meant to
                    465:  * be used by normal applications.
                    466:  */
1.1       deraadt   467:
1.18      markus    468: int
1.22      markus    469: ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment)
1.1       deraadt   470: {
1.25      markus    471:        Buffer msg;
1.24      markus    472:        int type;
1.12      markus    473:
1.25      markus    474:        buffer_init(&msg);
1.22      markus    475:
                    476:        switch (key->type) {
1.30      markus    477:        case KEY_RSA1:
                    478:                ssh_encode_identity_rsa1(&msg, key->rsa, comment);
                    479:                break;
1.22      markus    480:        case KEY_RSA:
                    481:        case KEY_DSA:
1.30      markus    482:                ssh_encode_identity_ssh2(&msg, key, comment);
1.22      markus    483:                break;
                    484:        default:
1.25      markus    485:                buffer_free(&msg);
1.22      markus    486:                return 0;
                    487:                break;
                    488:        }
1.25      markus    489:        if (ssh_request_reply(auth, &msg, &msg) == 0) {
                    490:                buffer_free(&msg);
1.12      markus    491:                return 0;
                    492:        }
1.25      markus    493:        type = buffer_get_char(&msg);
                    494:        buffer_free(&msg);
1.24      markus    495:        return decode_reply(type);
1.12      markus    496: }
1.1       deraadt   497:
1.14      markus    498: /*
                    499:  * Removes an identity from the authentication server.  This call is not
                    500:  * meant to be used by normal applications.
                    501:  */
1.1       deraadt   502:
1.18      markus    503: int
1.25      markus    504: ssh_remove_identity(AuthenticationConnection *auth, Key *key)
1.1       deraadt   505: {
1.25      markus    506:        Buffer msg;
1.24      markus    507:        int type;
1.31      markus    508:        u_char *blob;
                    509:        u_int blen;
1.12      markus    510:
1.25      markus    511:        buffer_init(&msg);
1.12      markus    512:
1.30      markus    513:        if (key->type == KEY_RSA1) {
1.25      markus    514:                buffer_put_char(&msg, SSH_AGENTC_REMOVE_RSA_IDENTITY);
                    515:                buffer_put_int(&msg, BN_num_bits(key->rsa->n));
                    516:                buffer_put_bignum(&msg, key->rsa->e);
                    517:                buffer_put_bignum(&msg, key->rsa->n);
1.30      markus    518:        } else if (key->type == KEY_DSA || key->type == KEY_RSA) {
                    519:                key_to_blob(key, &blob, &blen);
1.25      markus    520:                buffer_put_char(&msg, SSH2_AGENTC_REMOVE_IDENTITY);
                    521:                buffer_put_string(&msg, blob, blen);
                    522:                xfree(blob);
                    523:        } else {
                    524:                buffer_free(&msg);
                    525:                return 0;
                    526:        }
1.42      markus    527:        if (ssh_request_reply(auth, &msg, &msg) == 0) {
                    528:                buffer_free(&msg);
                    529:                return 0;
                    530:        }
                    531:        type = buffer_get_char(&msg);
                    532:        buffer_free(&msg);
                    533:        return decode_reply(type);
                    534: }
                    535:
                    536: int
1.43      markus    537: ssh_update_card(AuthenticationConnection *auth, int add, const char *reader_id)
1.42      markus    538: {
                    539:        Buffer msg;
                    540:        int type;
                    541:
                    542:        buffer_init(&msg);
                    543:        buffer_put_char(&msg, add ? SSH_AGENTC_ADD_SMARTCARD_KEY :
                    544:            SSH_AGENTC_REMOVE_SMARTCARD_KEY);
1.43      markus    545:        buffer_put_cstring(&msg, reader_id);
1.25      markus    546:        if (ssh_request_reply(auth, &msg, &msg) == 0) {
                    547:                buffer_free(&msg);
1.12      markus    548:                return 0;
                    549:        }
1.25      markus    550:        type = buffer_get_char(&msg);
                    551:        buffer_free(&msg);
1.24      markus    552:        return decode_reply(type);
1.12      markus    553: }
                    554:
1.14      markus    555: /*
                    556:  * Removes all identities from the agent.  This call is not meant to be used
                    557:  * by normal applications.
                    558:  */
1.1       deraadt   559:
1.18      markus    560: int
1.25      markus    561: ssh_remove_all_identities(AuthenticationConnection *auth, int version)
1.1       deraadt   562: {
1.25      markus    563:        Buffer msg;
1.24      markus    564:        int type;
1.25      markus    565:        int code = (version==1) ?
                    566:                SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
                    567:                SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
1.12      markus    568:
1.25      markus    569:        buffer_init(&msg);
                    570:        buffer_put_char(&msg, code);
1.12      markus    571:
1.25      markus    572:        if (ssh_request_reply(auth, &msg, &msg) == 0) {
                    573:                buffer_free(&msg);
1.12      markus    574:                return 0;
                    575:        }
1.25      markus    576:        type = buffer_get_char(&msg);
                    577:        buffer_free(&msg);
1.24      markus    578:        return decode_reply(type);
1.21      markus    579: }
                    580:
1.35      stevesk   581: int
1.24      markus    582: decode_reply(int type)
1.21      markus    583: {
1.12      markus    584:        switch (type) {
                    585:        case SSH_AGENT_FAILURE:
1.29      markus    586:        case SSH_COM_AGENT2_FAILURE:
1.44    ! markus    587:        case SSH2_AGENT_FAILURE:
1.24      markus    588:                log("SSH_AGENT_FAILURE");
1.12      markus    589:                return 0;
                    590:        case SSH_AGENT_SUCCESS:
                    591:                return 1;
                    592:        default:
1.21      markus    593:                fatal("Bad response from authentication agent: %d", type);
1.12      markus    594:        }
                    595:        /* NOTREACHED */
                    596:        return 0;
                    597: }