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

1.1       deraadt     1: /*
1.18      markus      2:  *
1.13      deraadt     3:  * authfd.c
1.18      markus      4:  *
1.13      deraadt     5:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
1.18      markus      6:  *
1.13      deraadt     7:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      8:  *                    All rights reserved
1.18      markus      9:  *
1.13      deraadt    10:  * Created: Wed Mar 29 01:30:28 1995 ylo
1.18      markus     11:  *
1.13      deraadt    12:  * Functions for connecting the local authentication agent.
1.18      markus     13:  *
1.13      deraadt    14:  */
1.1       deraadt    15:
                     16: #include "includes.h"
1.23    ! deraadt    17: RCSID("$OpenBSD: authfd.c,v 1.22 2000/07/16 08:27:20 markus Exp $");
1.1       deraadt    18:
                     19: #include "ssh.h"
                     20: #include "rsa.h"
                     21: #include "buffer.h"
                     22: #include "bufaux.h"
                     23: #include "xmalloc.h"
                     24: #include "getput.h"
                     25:
1.17      markus     26: #include <openssl/rsa.h>
1.22      markus     27: #include <openssl/dsa.h>
                     28: #include <openssl/evp.h>
                     29: #include "key.h"
                     30: #include "authfd.h"
                     31: #include "kex.h"
1.2       provos     32:
1.21      markus     33: /* helper */
                     34: int ssh_agent_get_reply(AuthenticationConnection *auth);
                     35:
1.1       deraadt    36: /* Returns the number of the authentication fd, or -1 if there is none. */
                     37:
1.2       provos     38: int
1.8       markus     39: ssh_get_authentication_socket()
1.1       deraadt    40: {
1.12      markus     41:        const char *authsocket;
1.23    ! deraadt    42:        int sock, len;
1.12      markus     43:        struct sockaddr_un sunaddr;
                     44:
                     45:        authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
                     46:        if (!authsocket)
                     47:                return -1;
                     48:
                     49:        sunaddr.sun_family = AF_UNIX;
                     50:        strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
1.23    ! deraadt    51:        sunaddr.sun_len = len = SUN_LEN(&sunaddr)+1;
1.12      markus     52:
                     53:        sock = socket(AF_UNIX, SOCK_STREAM, 0);
                     54:        if (sock < 0)
                     55:                return -1;
                     56:
                     57:        /* close on exec */
                     58:        if (fcntl(sock, F_SETFD, 1) == -1) {
                     59:                close(sock);
                     60:                return -1;
                     61:        }
1.23    ! deraadt    62:        if (connect(sock, (struct sockaddr *) & sunaddr, len) < 0) {
1.12      markus     63:                close(sock);
                     64:                return -1;
                     65:        }
                     66:        return sock;
1.1       deraadt    67: }
                     68:
1.14      markus     69: /*
                     70:  * Closes the agent socket if it should be closed (depends on how it was
                     71:  * obtained).  The argument must have been returned by
                     72:  * ssh_get_authentication_socket().
                     73:  */
1.1       deraadt    74:
1.18      markus     75: void
1.12      markus     76: ssh_close_authentication_socket(int sock)
1.1       deraadt    77: {
1.12      markus     78:        if (getenv(SSH_AUTHSOCKET_ENV_NAME))
                     79:                close(sock);
1.1       deraadt    80: }
                     81:
1.14      markus     82: /*
                     83:  * Opens and connects a private socket for communication with the
                     84:  * authentication agent.  Returns the file descriptor (which must be
                     85:  * shut down and closed by the caller when no longer needed).
                     86:  * Returns NULL if an error occurred and the connection could not be
                     87:  * opened.
                     88:  */
1.1       deraadt    89:
1.12      markus     90: AuthenticationConnection *
                     91: ssh_get_authentication_connection()
1.1       deraadt    92: {
1.12      markus     93:        AuthenticationConnection *auth;
                     94:        int sock;
1.1       deraadt    95:
1.12      markus     96:        sock = ssh_get_authentication_socket();
                     97:
1.14      markus     98:        /*
                     99:         * Fail if we couldn't obtain a connection.  This happens if we
                    100:         * exited due to a timeout.
                    101:         */
1.12      markus    102:        if (sock < 0)
                    103:                return NULL;
                    104:
                    105:        auth = xmalloc(sizeof(*auth));
                    106:        auth->fd = sock;
                    107:        buffer_init(&auth->packet);
                    108:        buffer_init(&auth->identities);
                    109:        auth->howmany = 0;
                    110:
                    111:        return auth;
1.1       deraadt   112: }
                    113:
1.14      markus    114: /*
                    115:  * Closes the connection to the authentication agent and frees any associated
                    116:  * memory.
                    117:  */
1.1       deraadt   118:
1.18      markus    119: void
1.12      markus    120: ssh_close_authentication_connection(AuthenticationConnection *ac)
1.1       deraadt   121: {
1.12      markus    122:        buffer_free(&ac->packet);
                    123:        buffer_free(&ac->identities);
                    124:        close(ac->fd);
                    125:        xfree(ac);
1.1       deraadt   126: }
                    127:
1.14      markus    128: /*
                    129:  * Returns the first authentication identity held by the agent.
                    130:  * Returns true if an identity is available, 0 otherwise.
                    131:  * The caller must initialize the integers before the call, and free the
                    132:  * comment after a successful call (before calling ssh_get_next_identity).
                    133:  */
1.1       deraadt   134:
1.2       provos    135: int
                    136: ssh_get_first_identity(AuthenticationConnection *auth,
1.9       markus    137:                       BIGNUM *e, BIGNUM *n, char **comment)
1.1       deraadt   138: {
1.12      markus    139:        unsigned char msg[8192];
                    140:        int len, l;
1.1       deraadt   141:
1.14      markus    142:        /*
                    143:         * Send a message to the agent requesting for a list of the
                    144:         * identities it can represent.
                    145:         */
1.22      markus    146:        PUT_32BIT(msg, 1);
1.12      markus    147:        msg[4] = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
1.15      deraadt   148:        if (atomicio(write, auth->fd, msg, 5) != 5) {
1.12      markus    149:                error("write auth->fd: %.100s", strerror(errno));
                    150:                return 0;
                    151:        }
                    152:        /* Read the length of the response.  XXX implement timeouts here. */
                    153:        len = 4;
                    154:        while (len > 0) {
                    155:                l = read(auth->fd, msg + 4 - len, len);
                    156:                if (l <= 0) {
                    157:                        error("read auth->fd: %.100s", strerror(errno));
                    158:                        return 0;
                    159:                }
                    160:                len -= l;
                    161:        }
                    162:
1.14      markus    163:        /*
                    164:         * Extract the length, and check it for sanity.  (We cannot trust
                    165:         * authentication agents).
                    166:         */
1.12      markus    167:        len = GET_32BIT(msg);
                    168:        if (len < 1 || len > 256 * 1024)
                    169:                fatal("Authentication reply message too long: %d\n", len);
                    170:
                    171:        /* Read the packet itself. */
                    172:        buffer_clear(&auth->identities);
                    173:        while (len > 0) {
                    174:                l = len;
                    175:                if (l > sizeof(msg))
                    176:                        l = sizeof(msg);
                    177:                l = read(auth->fd, msg, l);
                    178:                if (l <= 0)
                    179:                        fatal("Incomplete authentication reply.");
                    180:                buffer_append(&auth->identities, (char *) msg, l);
                    181:                len -= l;
                    182:        }
1.1       deraadt   183:
1.12      markus    184:        /* Get message type, and verify that we got a proper answer. */
                    185:        buffer_get(&auth->identities, (char *) msg, 1);
                    186:        if (msg[0] != SSH_AGENT_RSA_IDENTITIES_ANSWER)
                    187:                fatal("Bad authentication reply message type: %d", msg[0]);
                    188:
                    189:        /* Get the number of entries in the response and check it for sanity. */
                    190:        auth->howmany = buffer_get_int(&auth->identities);
                    191:        if (auth->howmany > 1024)
                    192:                fatal("Too many identities in authentication reply: %d\n", auth->howmany);
                    193:
                    194:        /* Return the first entry (if any). */
                    195:        return ssh_get_next_identity(auth, e, n, comment);
1.1       deraadt   196: }
                    197:
1.14      markus    198: /*
                    199:  * Returns the next authentication identity for the agent.  Other functions
                    200:  * can be called between this and ssh_get_first_identity or two calls of this
                    201:  * function.  This returns 0 if there are no more identities.  The caller
                    202:  * must free comment after a successful return.
                    203:  */
1.1       deraadt   204:
1.2       provos    205: int
                    206: ssh_get_next_identity(AuthenticationConnection *auth,
1.9       markus    207:                      BIGNUM *e, BIGNUM *n, char **comment)
1.1       deraadt   208: {
1.12      markus    209:        unsigned int bits;
1.9       markus    210:
1.12      markus    211:        /* Return failure if no more entries. */
                    212:        if (auth->howmany <= 0)
                    213:                return 0;
                    214:
1.14      markus    215:        /*
                    216:         * Get the next entry from the packet.  These will abort with a fatal
                    217:         * error if the packet is too short or contains corrupt data.
                    218:         */
1.12      markus    219:        bits = buffer_get_int(&auth->identities);
                    220:        buffer_get_bignum(&auth->identities, e);
                    221:        buffer_get_bignum(&auth->identities, n);
                    222:        *comment = buffer_get_string(&auth->identities, NULL);
                    223:
                    224:        if (bits != BN_num_bits(n))
1.19      markus    225:                log("Warning: identity keysize mismatch: actual %d, announced %u",
                    226:                    BN_num_bits(n), bits);
1.9       markus    227:
1.12      markus    228:        /* Decrement the number of remaining entries. */
                    229:        auth->howmany--;
1.1       deraadt   230:
1.12      markus    231:        return 1;
1.1       deraadt   232: }
                    233:
1.14      markus    234: /*
                    235:  * Generates a random challenge, sends it to the agent, and waits for
                    236:  * response from the agent.  Returns true (non-zero) if the agent gave the
                    237:  * correct answer, zero otherwise.  Response type selects the style of
                    238:  * response desired, with 0 corresponding to protocol version 1.0 (no longer
                    239:  * supported) and 1 corresponding to protocol version 1.1.
                    240:  */
1.1       deraadt   241:
1.2       provos    242: int
                    243: ssh_decrypt_challenge(AuthenticationConnection *auth,
1.12      markus    244:                      BIGNUM* e, BIGNUM *n, BIGNUM *challenge,
1.2       provos    245:                      unsigned char session_id[16],
                    246:                      unsigned int response_type,
                    247:                      unsigned char response[16])
1.1       deraadt   248: {
1.12      markus    249:        Buffer buffer;
                    250:        unsigned char buf[8192];
                    251:        int len, l, i;
                    252:
                    253:        /* Response type 0 is no longer supported. */
                    254:        if (response_type == 0)
                    255:                fatal("Compatibility with ssh protocol version 1.0 no longer supported.");
                    256:
                    257:        /* Format a message to the agent. */
                    258:        buf[0] = SSH_AGENTC_RSA_CHALLENGE;
                    259:        buffer_init(&buffer);
                    260:        buffer_append(&buffer, (char *) buf, 1);
                    261:        buffer_put_int(&buffer, BN_num_bits(n));
                    262:        buffer_put_bignum(&buffer, e);
                    263:        buffer_put_bignum(&buffer, n);
                    264:        buffer_put_bignum(&buffer, challenge);
                    265:        buffer_append(&buffer, (char *) session_id, 16);
                    266:        buffer_put_int(&buffer, response_type);
                    267:
                    268:        /* Get the length of the message, and format it in the buffer. */
                    269:        len = buffer_len(&buffer);
                    270:        PUT_32BIT(buf, len);
                    271:
                    272:        /* Send the length and then the packet to the agent. */
1.15      deraadt   273:        if (atomicio(write, auth->fd, buf, 4) != 4 ||
                    274:            atomicio(write, auth->fd, buffer_ptr(&buffer),
                    275:            buffer_len(&buffer)) != buffer_len(&buffer)) {
1.12      markus    276:                error("Error writing to authentication socket.");
                    277: error_cleanup:
                    278:                buffer_free(&buffer);
                    279:                return 0;
                    280:        }
1.14      markus    281:        /*
                    282:         * Wait for response from the agent.  First read the length of the
                    283:         * response packet.
                    284:         */
1.12      markus    285:        len = 4;
                    286:        while (len > 0) {
                    287:                l = read(auth->fd, buf + 4 - len, len);
                    288:                if (l <= 0) {
                    289:                        error("Error reading response length from authentication socket.");
                    290:                        goto error_cleanup;
                    291:                }
                    292:                len -= l;
                    293:        }
                    294:
                    295:        /* Extract the length, and check it for sanity. */
                    296:        len = GET_32BIT(buf);
                    297:        if (len > 256 * 1024)
                    298:                fatal("Authentication response too long: %d", len);
                    299:
                    300:        /* Read the rest of the response in tothe buffer. */
                    301:        buffer_clear(&buffer);
                    302:        while (len > 0) {
                    303:                l = len;
                    304:                if (l > sizeof(buf))
                    305:                        l = sizeof(buf);
                    306:                l = read(auth->fd, buf, l);
                    307:                if (l <= 0) {
                    308:                        error("Error reading response from authentication socket.");
                    309:                        goto error_cleanup;
                    310:                }
                    311:                buffer_append(&buffer, (char *) buf, l);
                    312:                len -= l;
                    313:        }
                    314:
                    315:        /* Get the type of the packet. */
                    316:        buffer_get(&buffer, (char *) buf, 1);
                    317:
                    318:        /* Check for agent failure message. */
                    319:        if (buf[0] == SSH_AGENT_FAILURE) {
                    320:                log("Agent admitted failure to authenticate using the key.");
                    321:                goto error_cleanup;
                    322:        }
                    323:        /* Now it must be an authentication response packet. */
                    324:        if (buf[0] != SSH_AGENT_RSA_RESPONSE)
                    325:                fatal("Bad authentication response: %d", buf[0]);
                    326:
1.14      markus    327:        /*
                    328:         * Get the response from the packet.  This will abort with a fatal
                    329:         * error if the packet is corrupt.
                    330:         */
1.12      markus    331:        for (i = 0; i < 16; i++)
                    332:                response[i] = buffer_get_char(&buffer);
                    333:
                    334:        /* The buffer containing the packet is no longer needed. */
                    335:        buffer_free(&buffer);
                    336:
                    337:        /* Correct answer. */
                    338:        return 1;
                    339: }
1.1       deraadt   340:
1.22      markus    341: /* Encode key for a message to the agent. */
                    342:
                    343: void
                    344: ssh_encode_identity_rsa(Buffer *b, RSA *key, const char *comment)
                    345: {
                    346:        buffer_clear(b);
                    347:        buffer_put_char(b, SSH_AGENTC_ADD_RSA_IDENTITY);
                    348:        buffer_put_int(b, BN_num_bits(key->n));
                    349:        buffer_put_bignum(b, key->n);
                    350:        buffer_put_bignum(b, key->e);
                    351:        buffer_put_bignum(b, key->d);
                    352:        /* To keep within the protocol: p < q for ssh. in SSL p > q */
                    353:        buffer_put_bignum(b, key->iqmp);        /* ssh key->u */
                    354:        buffer_put_bignum(b, key->q);   /* ssh key->p, SSL key->q */
                    355:        buffer_put_bignum(b, key->p);   /* ssh key->q, SSL key->p */
                    356:        buffer_put_string(b, comment, strlen(comment));
                    357: }
                    358:
                    359: void
                    360: ssh_encode_identity_dsa(Buffer *b, DSA *key, const char *comment)
                    361: {
                    362:        buffer_clear(b);
                    363:        buffer_put_char(b, SSH2_AGENTC_ADD_IDENTITY);
                    364:        buffer_put_cstring(b, KEX_DSS);
                    365:        buffer_put_bignum2(b, key->p);
                    366:        buffer_put_bignum2(b, key->q);
                    367:        buffer_put_bignum2(b, key->g);
                    368:        buffer_put_bignum2(b, key->pub_key);
                    369:        buffer_put_bignum2(b, key->priv_key);
                    370:        buffer_put_string(b, comment, strlen(comment));
                    371: }
                    372:
1.14      markus    373: /*
                    374:  * Adds an identity to the authentication server.  This call is not meant to
                    375:  * be used by normal applications.
                    376:  */
1.1       deraadt   377:
1.18      markus    378: int
1.22      markus    379: ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment)
1.1       deraadt   380: {
1.12      markus    381:        Buffer buffer;
                    382:        unsigned char buf[8192];
1.21      markus    383:        int len;
1.12      markus    384:
                    385:        buffer_init(&buffer);
1.22      markus    386:
                    387:        switch (key->type) {
                    388:        case KEY_RSA:
                    389:                ssh_encode_identity_rsa(&buffer, key->rsa, comment);
                    390:                break;
                    391:        case KEY_DSA:
                    392:                ssh_encode_identity_dsa(&buffer, key->dsa, comment);
                    393:                break;
                    394:        default:
                    395:                buffer_free(&buffer);
                    396:                return 0;
                    397:                break;
                    398:        }
1.12      markus    399:
                    400:        /* Get the length of the message, and format it in the buffer. */
                    401:        len = buffer_len(&buffer);
                    402:        PUT_32BIT(buf, len);
                    403:
                    404:        /* Send the length and then the packet to the agent. */
1.15      deraadt   405:        if (atomicio(write, auth->fd, buf, 4) != 4 ||
                    406:            atomicio(write, auth->fd, buffer_ptr(&buffer),
                    407:            buffer_len(&buffer)) != buffer_len(&buffer)) {
1.12      markus    408:                error("Error writing to authentication socket.");
                    409:                buffer_free(&buffer);
                    410:                return 0;
                    411:        }
1.21      markus    412:        buffer_free(&buffer);
                    413:        return ssh_agent_get_reply(auth);
1.12      markus    414: }
1.1       deraadt   415:
1.14      markus    416: /*
                    417:  * Removes an identity from the authentication server.  This call is not
                    418:  * meant to be used by normal applications.
                    419:  */
1.1       deraadt   420:
1.18      markus    421: int
1.12      markus    422: ssh_remove_identity(AuthenticationConnection *auth, RSA *key)
1.1       deraadt   423: {
1.12      markus    424:        Buffer buffer;
1.21      markus    425:        unsigned char buf[5];
                    426:        int len;
1.12      markus    427:
                    428:        /* Format a message to the agent. */
                    429:        buffer_init(&buffer);
                    430:        buffer_put_char(&buffer, SSH_AGENTC_REMOVE_RSA_IDENTITY);
                    431:        buffer_put_int(&buffer, BN_num_bits(key->n));
                    432:        buffer_put_bignum(&buffer, key->e);
                    433:        buffer_put_bignum(&buffer, key->n);
                    434:
                    435:        /* Get the length of the message, and format it in the buffer. */
                    436:        len = buffer_len(&buffer);
                    437:        PUT_32BIT(buf, len);
                    438:
                    439:        /* Send the length and then the packet to the agent. */
1.15      deraadt   440:        if (atomicio(write, auth->fd, buf, 4) != 4 ||
                    441:            atomicio(write, auth->fd, buffer_ptr(&buffer),
                    442:            buffer_len(&buffer)) != buffer_len(&buffer)) {
1.12      markus    443:                error("Error writing to authentication socket.");
                    444:                buffer_free(&buffer);
                    445:                return 0;
                    446:        }
1.21      markus    447:        buffer_free(&buffer);
                    448:        return ssh_agent_get_reply(auth);
1.12      markus    449: }
                    450:
1.14      markus    451: /*
                    452:  * Removes all identities from the agent.  This call is not meant to be used
                    453:  * by normal applications.
                    454:  */
1.1       deraadt   455:
1.18      markus    456: int
1.12      markus    457: ssh_remove_all_identities(AuthenticationConnection *auth)
1.1       deraadt   458: {
1.21      markus    459:        unsigned char buf[5];
1.12      markus    460:
                    461:        /* Get the length of the message, and format it in the buffer. */
                    462:        PUT_32BIT(buf, 1);
                    463:        buf[4] = SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES;
                    464:
                    465:        /* Send the length and then the packet to the agent. */
1.15      deraadt   466:        if (atomicio(write, auth->fd, buf, 5) != 5) {
1.12      markus    467:                error("Error writing to authentication socket.");
                    468:                return 0;
                    469:        }
1.21      markus    470:        return ssh_agent_get_reply(auth);
                    471: }
                    472:
                    473: /*
                    474:  * Read for reply from agent. returns 1 for success, 0 on error
                    475:  */
                    476:
                    477: int
                    478: ssh_agent_get_reply(AuthenticationConnection *auth)
                    479: {
                    480:        Buffer buffer;
                    481:        unsigned char buf[8192];
                    482:        int len, l, type;
                    483:
1.14      markus    484:        /*
                    485:         * Wait for response from the agent.  First read the length of the
                    486:         * response packet.
                    487:         */
1.12      markus    488:        len = 4;
                    489:        while (len > 0) {
                    490:                l = read(auth->fd, buf + 4 - len, len);
                    491:                if (l <= 0) {
                    492:                        error("Error reading response length from authentication socket.");
1.21      markus    493:                        buffer_free(&buffer);
1.12      markus    494:                        return 0;
                    495:                }
                    496:                len -= l;
                    497:        }
                    498:
                    499:        /* Extract the length, and check it for sanity. */
                    500:        len = GET_32BIT(buf);
                    501:        if (len > 256 * 1024)
1.21      markus    502:                fatal("Response from agent too long: %d", len);
1.12      markus    503:
1.21      markus    504:        /* Read the rest of the response in to the buffer. */
1.12      markus    505:        buffer_init(&buffer);
                    506:        while (len > 0) {
                    507:                l = len;
                    508:                if (l > sizeof(buf))
                    509:                        l = sizeof(buf);
                    510:                l = read(auth->fd, buf, l);
                    511:                if (l <= 0) {
                    512:                        error("Error reading response from authentication socket.");
                    513:                        buffer_free(&buffer);
                    514:                        return 0;
                    515:                }
                    516:                buffer_append(&buffer, (char *) buf, l);
                    517:                len -= l;
                    518:        }
                    519:
                    520:        /* Get the type of the packet. */
                    521:        type = buffer_get_char(&buffer);
1.21      markus    522:        buffer_free(&buffer);
1.12      markus    523:        switch (type) {
                    524:        case SSH_AGENT_FAILURE:
1.22      markus    525: log("SSH_AGENT_FAILURE");
1.12      markus    526:                return 0;
                    527:        case SSH_AGENT_SUCCESS:
                    528:                return 1;
                    529:        default:
1.21      markus    530:                fatal("Bad response from authentication agent: %d", type);
1.12      markus    531:        }
                    532:        /* NOTREACHED */
                    533:        return 0;
                    534: }