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

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.26    ! markus     20: RCSID("$OpenBSD: authfd.c,v 1.25 2000/08/19 21:34:42 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);
1.26    ! markus    358:        buffer_put_int(&msg, 0);                                /* flags, unused */
1.25      markus    359:        xfree(blob);
                    360:
                    361:        if (ssh_request_reply(auth, &msg, &msg) == 0) {
                    362:                buffer_free(&msg);
                    363:                return -1;
                    364:        }
                    365:        type = buffer_get_char(&msg);
                    366:        if (type == SSH_AGENT_FAILURE) {
                    367:                log("Agent admitted failure to sign using the key.");
                    368:        } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
                    369:                fatal("Bad authentication response: %d", type);
                    370:        } else {
                    371:                ret = 0;
                    372:                *sigp = buffer_get_string(&msg, lenp);
                    373:        }
                    374:        buffer_free(&msg);
                    375:        return ret;
                    376: }
                    377:
1.22      markus    378: /* Encode key for a message to the agent. */
                    379:
                    380: void
                    381: ssh_encode_identity_rsa(Buffer *b, RSA *key, const char *comment)
                    382: {
                    383:        buffer_clear(b);
                    384:        buffer_put_char(b, SSH_AGENTC_ADD_RSA_IDENTITY);
                    385:        buffer_put_int(b, BN_num_bits(key->n));
                    386:        buffer_put_bignum(b, key->n);
                    387:        buffer_put_bignum(b, key->e);
                    388:        buffer_put_bignum(b, key->d);
                    389:        /* To keep within the protocol: p < q for ssh. in SSL p > q */
                    390:        buffer_put_bignum(b, key->iqmp);        /* ssh key->u */
                    391:        buffer_put_bignum(b, key->q);   /* ssh key->p, SSL key->q */
                    392:        buffer_put_bignum(b, key->p);   /* ssh key->q, SSL key->p */
                    393:        buffer_put_string(b, comment, strlen(comment));
                    394: }
                    395:
                    396: void
                    397: ssh_encode_identity_dsa(Buffer *b, DSA *key, const char *comment)
                    398: {
                    399:        buffer_clear(b);
                    400:        buffer_put_char(b, SSH2_AGENTC_ADD_IDENTITY);
                    401:        buffer_put_cstring(b, KEX_DSS);
                    402:        buffer_put_bignum2(b, key->p);
                    403:        buffer_put_bignum2(b, key->q);
                    404:        buffer_put_bignum2(b, key->g);
                    405:        buffer_put_bignum2(b, key->pub_key);
                    406:        buffer_put_bignum2(b, key->priv_key);
                    407:        buffer_put_string(b, comment, strlen(comment));
                    408: }
                    409:
1.14      markus    410: /*
                    411:  * Adds an identity to the authentication server.  This call is not meant to
                    412:  * be used by normal applications.
                    413:  */
1.1       deraadt   414:
1.18      markus    415: int
1.22      markus    416: ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment)
1.1       deraadt   417: {
1.25      markus    418:        Buffer msg;
1.24      markus    419:        int type;
1.12      markus    420:
1.25      markus    421:        buffer_init(&msg);
1.22      markus    422:
                    423:        switch (key->type) {
                    424:        case KEY_RSA:
1.25      markus    425:                ssh_encode_identity_rsa(&msg, key->rsa, comment);
1.22      markus    426:                break;
                    427:        case KEY_DSA:
1.25      markus    428:                ssh_encode_identity_dsa(&msg, key->dsa, comment);
1.22      markus    429:                break;
                    430:        default:
1.25      markus    431:                buffer_free(&msg);
1.22      markus    432:                return 0;
                    433:                break;
                    434:        }
1.25      markus    435:        if (ssh_request_reply(auth, &msg, &msg) == 0) {
                    436:                buffer_free(&msg);
1.12      markus    437:                return 0;
                    438:        }
1.25      markus    439:        type = buffer_get_char(&msg);
                    440:        buffer_free(&msg);
1.24      markus    441:        return decode_reply(type);
1.12      markus    442: }
1.1       deraadt   443:
1.14      markus    444: /*
                    445:  * Removes an identity from the authentication server.  This call is not
                    446:  * meant to be used by normal applications.
                    447:  */
1.1       deraadt   448:
1.18      markus    449: int
1.25      markus    450: ssh_remove_identity(AuthenticationConnection *auth, Key *key)
1.1       deraadt   451: {
1.25      markus    452:        Buffer msg;
1.24      markus    453:        int type;
1.25      markus    454:        unsigned char *blob;
                    455:        unsigned int blen;
1.12      markus    456:
1.25      markus    457:        buffer_init(&msg);
1.12      markus    458:
1.25      markus    459:        if (key->type == KEY_RSA) {
                    460:                buffer_put_char(&msg, SSH_AGENTC_REMOVE_RSA_IDENTITY);
                    461:                buffer_put_int(&msg, BN_num_bits(key->rsa->n));
                    462:                buffer_put_bignum(&msg, key->rsa->e);
                    463:                buffer_put_bignum(&msg, key->rsa->n);
                    464:        } else if (key->type == KEY_DSA) {
                    465:                dsa_make_key_blob(key, &blob, &blen);
                    466:                buffer_put_char(&msg, SSH2_AGENTC_REMOVE_IDENTITY);
                    467:                buffer_put_string(&msg, blob, blen);
                    468:                xfree(blob);
                    469:        } else {
                    470:                buffer_free(&msg);
                    471:                return 0;
                    472:        }
                    473:        if (ssh_request_reply(auth, &msg, &msg) == 0) {
                    474:                buffer_free(&msg);
1.12      markus    475:                return 0;
                    476:        }
1.25      markus    477:        type = buffer_get_char(&msg);
                    478:        buffer_free(&msg);
1.24      markus    479:        return decode_reply(type);
1.12      markus    480: }
                    481:
1.14      markus    482: /*
                    483:  * Removes all identities from the agent.  This call is not meant to be used
                    484:  * by normal applications.
                    485:  */
1.1       deraadt   486:
1.18      markus    487: int
1.25      markus    488: ssh_remove_all_identities(AuthenticationConnection *auth, int version)
1.1       deraadt   489: {
1.25      markus    490:        Buffer msg;
1.24      markus    491:        int type;
1.25      markus    492:        int code = (version==1) ?
                    493:                SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
                    494:                SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
1.12      markus    495:
1.25      markus    496:        buffer_init(&msg);
                    497:        buffer_put_char(&msg, code);
1.12      markus    498:
1.25      markus    499:        if (ssh_request_reply(auth, &msg, &msg) == 0) {
                    500:                buffer_free(&msg);
1.12      markus    501:                return 0;
                    502:        }
1.25      markus    503:        type = buffer_get_char(&msg);
                    504:        buffer_free(&msg);
1.24      markus    505:        return decode_reply(type);
1.21      markus    506: }
                    507:
                    508: int
1.24      markus    509: decode_reply(int type)
1.21      markus    510: {
1.12      markus    511:        switch (type) {
                    512:        case SSH_AGENT_FAILURE:
1.24      markus    513:                log("SSH_AGENT_FAILURE");
1.12      markus    514:                return 0;
                    515:        case SSH_AGENT_SUCCESS:
                    516:                return 1;
                    517:        default:
1.21      markus    518:                fatal("Bad response from authentication agent: %d", type);
1.12      markus    519:        }
                    520:        /* NOTREACHED */
                    521:        return 0;
                    522: }