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

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