[BACK]Return to auth-rsa.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/auth-rsa.c, Revision 1.51

1.1       provos      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
                      5:  * RSA-based authentication.  This code determines whether to admit a login
                      6:  * based on RSA authentication.  This file also contains functions to check
                      7:  * validity of the host key.
1.21      markus      8:  *
1.28      deraadt     9:  * As far as I am concerned, the code I have written for this software
                     10:  * can be used freely for any purpose.  Any derived versions of this
                     11:  * software must be clearly marked as such, and if the derived work is
                     12:  * incompatible with the protocol description in the RFC file, it must be
                     13:  * called by a name other than "ssh" or "Secure Shell".
1.13      deraadt    14:  */
1.1       provos     15:
                     16: #include "includes.h"
1.51    ! markus     17: RCSID("$OpenBSD: auth-rsa.c,v 1.50 2001/12/28 14:50:54 markus Exp $");
1.38      markus     18:
                     19: #include <openssl/rsa.h>
                     20: #include <openssl/md5.h>
1.1       provos     21:
                     22: #include "rsa.h"
                     23: #include "packet.h"
                     24: #include "xmalloc.h"
1.35      markus     25: #include "ssh1.h"
1.1       provos     26: #include "mpaux.h"
                     27: #include "uidswap.h"
1.19      markus     28: #include "match.h"
1.25      markus     29: #include "auth-options.h"
1.35      markus     30: #include "pathnames.h"
1.38      markus     31: #include "log.h"
                     32: #include "servconf.h"
                     33: #include "auth.h"
1.46      jakob      34: #include "hostfile.h"
1.30      markus     35:
                     36: /* import */
                     37: extern ServerOptions options;
                     38:
1.14      markus     39: /*
                     40:  * Session identifier that is used to bind key exchange and authentication
                     41:  * responses to a particular session.
                     42:  */
1.34      markus     43: extern u_char session_id[16];
1.1       provos     44:
1.14      markus     45: /*
                     46:  * The .ssh/authorized_keys file contains public keys, one per line, in the
                     47:  * following format:
                     48:  *   options bits e n comment
                     49:  * where bits, e and n are decimal numbers,
                     50:  * and comment is any string of characters up to newline.  The maximum
                     51:  * length of a line is 8000 characters.  See the documentation for a
                     52:  * description of the options.
                     53:  */
                     54:
1.51    ! markus     55: static BIGNUM *
        !            56: auth_rsa_generate_challenge(Key *key)
        !            57: {
        !            58:        BIGNUM *challenge;
        !            59:        BN_CTX *ctx;
        !            60:
        !            61:        if ((challenge = BN_new()) == NULL)
        !            62:                fatal("auth_rsa_generate_challenge: BN_new() failed");
        !            63:        /* Generate a random challenge. */
        !            64:        BN_rand(challenge, 256, 0, 0);
        !            65:        if ((ctx = BN_CTX_new()) == NULL)
        !            66:                fatal("auth_rsa_generate_challenge: BN_CTX_new() failed");
        !            67:        BN_mod(challenge, challenge, key->rsa->n, ctx);
        !            68:        BN_CTX_free(ctx);
        !            69:
        !            70:        return challenge;
        !            71: }
        !            72:
        !            73: static int
        !            74: auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16])
        !            75: {
        !            76:        u_char buf[32], mdbuf[16];
        !            77:        MD5_CTX md;
        !            78:        int len;
        !            79:
        !            80:        /* The response is MD5 of decrypted challenge plus session id. */
        !            81:        len = BN_num_bytes(challenge);
        !            82:        if (len <= 0 || len > 32)
        !            83:                fatal("auth_rsa_verify_response: bad challenge length %d", len);
        !            84:        memset(buf, 0, 32);
        !            85:        BN_bn2bin(challenge, buf + 32 - len);
        !            86:        MD5_Init(&md);
        !            87:        MD5_Update(&md, buf, 32);
        !            88:        MD5_Update(&md, session_id, 16);
        !            89:        MD5_Final(mdbuf, &md);
        !            90:
        !            91:        /* Verify that the response is the original challenge. */
        !            92:        if (memcmp(response, mdbuf, 16) != 0) {
        !            93:                /* Wrong answer. */
        !            94:                return (0);
        !            95:        }
        !            96:        /* Correct answer. */
        !            97:        return (1);
        !            98: }
        !            99:
1.14      markus    100: /*
                    101:  * Performs the RSA authentication challenge-response dialog with the client,
                    102:  * and returns true (non-zero) if the client gave the correct answer to
                    103:  * our challenge; returns zero if the client gives a wrong answer.
                    104:  */
1.1       provos    105:
                    106: int
1.51    ! markus    107: auth_rsa_challenge_dialog(Key *key)
1.1       provos    108: {
1.18      markus    109:        BIGNUM *challenge, *encrypted_challenge;
1.51    ! markus    110:        u_char response[16];
        !           111:        int i, success;
1.12      markus    112:
1.47      markus    113:        if ((encrypted_challenge = BN_new()) == NULL)
                    114:                fatal("auth_rsa_challenge_dialog: BN_new() failed");
1.12      markus    115:
1.51    ! markus    116:        challenge = auth_rsa_generate_challenge(key);
1.12      markus    117:
                    118:        /* Encrypt the challenge with the public key. */
1.51    ! markus    119:        rsa_public_encrypt(encrypted_challenge, challenge, key->rsa);
1.12      markus    120:
                    121:        /* Send the encrypted challenge to the client. */
                    122:        packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
                    123:        packet_put_bignum(encrypted_challenge);
                    124:        packet_send();
1.18      markus    125:        BN_clear_free(encrypted_challenge);
1.12      markus    126:        packet_write_wait();
                    127:
1.18      markus    128:        /* Wait for a response. */
1.50      markus    129:        packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE);
1.18      markus    130:        for (i = 0; i < 16; i++)
                    131:                response[i] = packet_get_char();
1.49      markus    132:        packet_check_eom();
1.18      markus    133:
1.51    ! markus    134:        success = auth_rsa_verify_response(key, challenge, response);
1.12      markus    135:        BN_clear_free(challenge);
1.51    ! markus    136:        return (success);
1.1       provos    137: }
                    138:
1.14      markus    139: /*
1.51    ! markus    140:  * check if there's user key matching client_n,
        !           141:  * return key if login is allowed, NULL otherwise
1.14      markus    142:  */
1.1       provos    143:
1.51    ! markus    144: static int
        !           145: auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1.1       provos    146: {
1.41      markus    147:        char line[8192], *file;
1.51    ! markus    148:        int allowed;
1.34      markus    149:        u_int bits;
1.12      markus    150:        FILE *f;
1.34      markus    151:        u_long linenum = 0;
1.12      markus    152:        struct stat st;
1.46      jakob     153:        Key *key;
1.12      markus    154:
                    155:        /* Temporarily use the user's uid. */
1.40      markus    156:        temporarily_use_uid(pw);
1.12      markus    157:
                    158:        /* The authorized keys. */
1.41      markus    159:        file = authorized_keys_file(pw);
                    160:        debug("trying public RSA key file %s", file);
1.12      markus    161:
                    162:        /* Fail quietly if file does not exist */
                    163:        if (stat(file, &st) < 0) {
                    164:                /* Restore the privileged uid. */
                    165:                restore_uid();
1.41      markus    166:                xfree(file);
1.51    ! markus    167:                return (NULL);
1.12      markus    168:        }
                    169:        /* Open the file containing the authorized keys. */
                    170:        f = fopen(file, "r");
                    171:        if (!f) {
                    172:                /* Restore the privileged uid. */
                    173:                restore_uid();
1.41      markus    174:                xfree(file);
1.51    ! markus    175:                return (NULL);
1.1       provos    176:        }
1.41      markus    177:        if (options.strict_modes &&
1.43      provos    178:            secure_filename(f, file, pw, line, sizeof(line)) != 0) {
1.41      markus    179:                xfree(file);
                    180:                fclose(f);
                    181:                log("Authentication refused: %s", line);
                    182:                restore_uid();
1.51    ! markus    183:                return (NULL);
1.1       provos    184:        }
1.51    ! markus    185:
        !           186:        /* Flag indicating whether the key is allowed. */
        !           187:        allowed = 0;
1.1       provos    188:
1.46      jakob     189:        key = key_new(KEY_RSA1);
1.12      markus    190:
1.14      markus    191:        /*
                    192:         * Go though the accepted keys, looking for the current key.  If
                    193:         * found, perform a challenge-response dialog to verify that the
                    194:         * user really has the corresponding private key.
                    195:         */
1.12      markus    196:        while (fgets(line, sizeof(line), f)) {
                    197:                char *cp;
                    198:                char *options;
                    199:
                    200:                linenum++;
                    201:
1.14      markus    202:                /* Skip leading whitespace, empty and comment lines. */
                    203:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    204:                        ;
1.12      markus    205:                if (!*cp || *cp == '\n' || *cp == '#')
                    206:                        continue;
                    207:
1.14      markus    208:                /*
                    209:                 * Check if there are options for this key, and if so,
                    210:                 * save their starting address and skip the option part
                    211:                 * for now.  If there are no options, set the starting
                    212:                 * address to NULL.
                    213:                 */
1.12      markus    214:                if (*cp < '0' || *cp > '9') {
                    215:                        int quoted = 0;
                    216:                        options = cp;
                    217:                        for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    218:                                if (*cp == '\\' && cp[1] == '"')
                    219:                                        cp++;   /* Skip both */
                    220:                                else if (*cp == '"')
                    221:                                        quoted = !quoted;
                    222:                        }
                    223:                } else
                    224:                        options = NULL;
1.1       provos    225:
1.12      markus    226:                /* Parse the key from the line. */
1.46      jakob     227:                if (hostfile_read_key(&cp, &bits, key) == 0) {
1.42      markus    228:                        debug("%.100s, line %lu: non ssh1 key syntax",
1.36      markus    229:                            file, linenum);
1.12      markus    230:                        continue;
1.1       provos    231:                }
1.12      markus    232:                /* cp now points to the comment part. */
                    233:
1.16      markus    234:                /* Check if the we have found the desired key (identified by its modulus). */
1.46      jakob     235:                if (BN_cmp(key->rsa->n, client_n) != 0)
1.16      markus    236:                        continue;
                    237:
1.12      markus    238:                /* check the real bits  */
1.46      jakob     239:                if (bits != BN_num_bits(key->rsa->n))
1.44      stevesk   240:                        log("Warning: %s, line %lu: keysize mismatch: "
1.15      markus    241:                            "actual %d vs. announced %d.",
1.46      jakob     242:                            file, linenum, BN_num_bits(key->rsa->n), bits);
1.12      markus    243:
                    244:                /* We have found the desired key. */
1.33      markus    245:                /*
                    246:                 * If our options do not allow this key to be used,
                    247:                 * do not send challenge.
                    248:                 */
1.36      markus    249:                if (!auth_parse_options(pw, options, file, linenum))
1.33      markus    250:                        continue;
1.19      markus    251:
1.51    ! markus    252:                /* break out, this key is allowed */
        !           253:                allowed = 1;
1.32      markus    254:                break;
1.1       provos    255:        }
                    256:
1.12      markus    257:        /* Restore the privileged uid. */
                    258:        restore_uid();
                    259:
                    260:        /* Close the file. */
1.41      markus    261:        xfree(file);
1.12      markus    262:        fclose(f);
                    263:
1.51    ! markus    264:        /* return key if allowed */
        !           265:        if (allowed && rkey != NULL)
        !           266:                *rkey = key;
        !           267:        else
        !           268:                key_free(key);
        !           269:        return (allowed);
        !           270: }
        !           271:
        !           272: /*
        !           273:  * Performs the RSA authentication dialog with the client.  This returns
        !           274:  * 0 if the client could not be authenticated, and 1 if authentication was
        !           275:  * successful.  This may exit if there is a serious protocol violation.
        !           276:  */
        !           277: int
        !           278: auth_rsa(struct passwd *pw, BIGNUM *client_n)
        !           279: {
        !           280:        Key *key;
        !           281:        char *fp;
        !           282:
        !           283:        /* no user given */
        !           284:        if (pw == NULL)
        !           285:                return 0;
1.1       provos    286:
1.51    ! markus    287:        if (auth_rsa_key_allowed(pw, client_n, &key) == 0) {
1.31      markus    288:                auth_clear_options();
1.51    ! markus    289:                return (0);
        !           290:        }
        !           291:
        !           292:        /* Perform the challenge-response dialog for this key. */
        !           293:        if (!auth_rsa_challenge_dialog(key)) {
        !           294:                /* Wrong response. */
        !           295:                verbose("Wrong response to RSA authentication challenge.");
        !           296:                packet_send_debug("Wrong response to RSA authentication challenge.");
        !           297:                /*
        !           298:                 * Break out of the loop. Otherwise we might send
        !           299:                 * another challenge and break the protocol.
        !           300:                 */
        !           301:                key_free(key);
        !           302:                return (0);
        !           303:        }
        !           304:        /*
        !           305:         * Correct response.  The client has been successfully
        !           306:         * authenticated. Note that we have not yet processed the
        !           307:         * options; this will be reset if the options cause the
        !           308:         * authentication to be rejected.
        !           309:         */
        !           310:        fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
        !           311:        verbose("Found matching %s key: %s",
        !           312:            key_type(key), fp);
        !           313:        xfree(fp);
        !           314:        key_free(key);
1.1       provos    315:
1.51    ! markus    316:        packet_send_debug("RSA authentication accepted.");
        !           317:        return (1);
1.1       provos    318: }