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

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.31    ! markus     17: RCSID("$OpenBSD: auth-rsa.c,v 1.30 2000/10/03 18:03:03 markus Exp $");
1.1       provos     18:
                     19: #include "rsa.h"
                     20: #include "packet.h"
                     21: #include "xmalloc.h"
                     22: #include "ssh.h"
                     23: #include "mpaux.h"
                     24: #include "uidswap.h"
1.19      markus     25: #include "match.h"
1.8       markus     26: #include "servconf.h"
1.25      markus     27: #include "auth-options.h"
1.1       provos     28:
1.20      markus     29: #include <openssl/rsa.h>
                     30: #include <openssl/md5.h>
1.1       provos     31:
1.30      markus     32:
                     33: /* import */
                     34: extern ServerOptions options;
                     35:
1.14      markus     36: /*
                     37:  * Session identifier that is used to bind key exchange and authentication
                     38:  * responses to a particular session.
                     39:  */
1.1       provos     40: extern unsigned char session_id[16];
                     41:
1.14      markus     42: /*
                     43:  * The .ssh/authorized_keys file contains public keys, one per line, in the
                     44:  * following format:
                     45:  *   options bits e n comment
                     46:  * where bits, e and n are decimal numbers,
                     47:  * and comment is any string of characters up to newline.  The maximum
                     48:  * length of a line is 8000 characters.  See the documentation for a
                     49:  * description of the options.
                     50:  */
                     51:
                     52: /*
                     53:  * Performs the RSA authentication challenge-response dialog with the client,
                     54:  * and returns true (non-zero) if the client gave the correct answer to
                     55:  * our challenge; returns zero if the client gives a wrong answer.
                     56:  */
1.1       provos     57:
                     58: int
1.19      markus     59: auth_rsa_challenge_dialog(RSA *pk)
1.1       provos     60: {
1.18      markus     61:        BIGNUM *challenge, *encrypted_challenge;
                     62:        BN_CTX *ctx;
1.12      markus     63:        unsigned char buf[32], mdbuf[16], response[16];
                     64:        MD5_CTX md;
                     65:        unsigned int i;
                     66:        int plen, len;
                     67:
                     68:        encrypted_challenge = BN_new();
                     69:        challenge = BN_new();
                     70:
                     71:        /* Generate a random challenge. */
                     72:        BN_rand(challenge, 256, 0, 0);
1.18      markus     73:        ctx = BN_CTX_new();
1.19      markus     74:        BN_mod(challenge, challenge, pk->n, ctx);
1.18      markus     75:        BN_CTX_free(ctx);
1.12      markus     76:
                     77:        /* Encrypt the challenge with the public key. */
                     78:        rsa_public_encrypt(encrypted_challenge, challenge, pk);
                     79:
                     80:        /* Send the encrypted challenge to the client. */
                     81:        packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
                     82:        packet_put_bignum(encrypted_challenge);
                     83:        packet_send();
1.18      markus     84:        BN_clear_free(encrypted_challenge);
1.12      markus     85:        packet_write_wait();
                     86:
1.18      markus     87:        /* Wait for a response. */
                     88:        packet_read_expect(&plen, SSH_CMSG_AUTH_RSA_RESPONSE);
                     89:        packet_integrity_check(plen, 16, SSH_CMSG_AUTH_RSA_RESPONSE);
                     90:        for (i = 0; i < 16; i++)
                     91:                response[i] = packet_get_char();
                     92:
1.12      markus     93:        /* The response is MD5 of decrypted challenge plus session id. */
                     94:        len = BN_num_bytes(challenge);
                     95:        if (len <= 0 || len > 32)
                     96:                fatal("auth_rsa_challenge_dialog: bad challenge length %d", len);
                     97:        memset(buf, 0, 32);
                     98:        BN_bn2bin(challenge, buf + 32 - len);
                     99:        MD5_Init(&md);
                    100:        MD5_Update(&md, buf, 32);
                    101:        MD5_Update(&md, session_id, 16);
                    102:        MD5_Final(mdbuf, &md);
                    103:        BN_clear_free(challenge);
                    104:
                    105:        /* Verify that the response is the original challenge. */
                    106:        if (memcmp(response, mdbuf, 16) != 0) {
                    107:                /* Wrong answer. */
                    108:                return 0;
                    109:        }
                    110:        /* Correct answer. */
                    111:        return 1;
1.1       provos    112: }
                    113:
1.14      markus    114: /*
                    115:  * Performs the RSA authentication dialog with the client.  This returns
                    116:  * 0 if the client could not be authenticated, and 1 if authentication was
                    117:  * successful.  This may exit if there is a serious protocol violation.
                    118:  */
1.1       provos    119:
                    120: int
1.8       markus    121: auth_rsa(struct passwd *pw, BIGNUM *client_n)
1.1       provos    122: {
1.12      markus    123:        char line[8192], file[1024];
                    124:        int authenticated;
                    125:        unsigned int bits;
                    126:        FILE *f;
                    127:        unsigned long linenum = 0;
                    128:        struct stat st;
1.19      markus    129:        RSA *pk;
1.30      markus    130:
                    131:        /* no user given */
                    132:        if (pw == NULL)
                    133:                return 0;
1.12      markus    134:
                    135:        /* Temporarily use the user's uid. */
                    136:        temporarily_use_uid(pw->pw_uid);
                    137:
                    138:        /* The authorized keys. */
                    139:        snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
                    140:                 SSH_USER_PERMITTED_KEYS);
                    141:
                    142:        /* Fail quietly if file does not exist */
                    143:        if (stat(file, &st) < 0) {
                    144:                /* Restore the privileged uid. */
                    145:                restore_uid();
                    146:                return 0;
                    147:        }
                    148:        /* Open the file containing the authorized keys. */
                    149:        f = fopen(file, "r");
                    150:        if (!f) {
                    151:                /* Restore the privileged uid. */
                    152:                restore_uid();
                    153:                packet_send_debug("Could not open %.900s for reading.", file);
                    154:                packet_send_debug("If your home is on an NFS volume, it may need to be world-readable.");
                    155:                return 0;
1.1       provos    156:        }
1.12      markus    157:        if (options.strict_modes) {
                    158:                int fail = 0;
                    159:                char buf[1024];
                    160:                /* Check open file in order to avoid open/stat races */
                    161:                if (fstat(fileno(f), &st) < 0 ||
                    162:                    (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
                    163:                    (st.st_mode & 022) != 0) {
                    164:                        snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: "
                    165:                                 "bad ownership or modes for '%s'.", pw->pw_name, file);
                    166:                        fail = 1;
                    167:                } else {
                    168:                        /* Check path to SSH_USER_PERMITTED_KEYS */
                    169:                        int i;
                    170:                        static const char *check[] = {
                    171:                                "", SSH_USER_DIR, NULL
                    172:                        };
                    173:                        for (i = 0; check[i]; i++) {
                    174:                                snprintf(line, sizeof line, "%.500s/%.100s", pw->pw_dir, check[i]);
                    175:                                if (stat(line, &st) < 0 ||
                    176:                                    (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
                    177:                                    (st.st_mode & 022) != 0) {
                    178:                                        snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: "
                    179:                                                 "bad ownership or modes for '%s'.", pw->pw_name, line);
                    180:                                        fail = 1;
                    181:                                        break;
                    182:                                }
                    183:                        }
                    184:                }
                    185:                if (fail) {
1.22      markus    186:                        fclose(f);
1.27      todd      187:                        log("%s",buf);
                    188:                        packet_send_debug("%s",buf);
1.12      markus    189:                        restore_uid();
                    190:                        return 0;
                    191:                }
1.1       provos    192:        }
1.12      markus    193:        /* Flag indicating whether authentication has succeeded. */
                    194:        authenticated = 0;
1.1       provos    195:
1.19      markus    196:        pk = RSA_new();
                    197:        pk->e = BN_new();
                    198:        pk->n = BN_new();
1.12      markus    199:
1.14      markus    200:        /*
                    201:         * Go though the accepted keys, looking for the current key.  If
                    202:         * found, perform a challenge-response dialog to verify that the
                    203:         * user really has the corresponding private key.
                    204:         */
1.12      markus    205:        while (fgets(line, sizeof(line), f)) {
                    206:                char *cp;
                    207:                char *options;
                    208:
                    209:                linenum++;
                    210:
1.14      markus    211:                /* Skip leading whitespace, empty and comment lines. */
                    212:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    213:                        ;
1.12      markus    214:                if (!*cp || *cp == '\n' || *cp == '#')
                    215:                        continue;
                    216:
1.14      markus    217:                /*
                    218:                 * Check if there are options for this key, and if so,
                    219:                 * save their starting address and skip the option part
                    220:                 * for now.  If there are no options, set the starting
                    221:                 * address to NULL.
                    222:                 */
1.12      markus    223:                if (*cp < '0' || *cp > '9') {
                    224:                        int quoted = 0;
                    225:                        options = cp;
                    226:                        for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    227:                                if (*cp == '\\' && cp[1] == '"')
                    228:                                        cp++;   /* Skip both */
                    229:                                else if (*cp == '"')
                    230:                                        quoted = !quoted;
                    231:                        }
                    232:                } else
                    233:                        options = NULL;
1.1       provos    234:
1.12      markus    235:                /* Parse the key from the line. */
1.19      markus    236:                if (!auth_rsa_read_key(&cp, &bits, pk->e, pk->n)) {
1.12      markus    237:                        debug("%.100s, line %lu: bad key syntax",
                    238:                              SSH_USER_PERMITTED_KEYS, linenum);
                    239:                        packet_send_debug("%.100s, line %lu: bad key syntax",
1.21      markus    240:                                          SSH_USER_PERMITTED_KEYS, linenum);
1.12      markus    241:                        continue;
1.1       provos    242:                }
1.12      markus    243:                /* cp now points to the comment part. */
                    244:
1.16      markus    245:                /* Check if the we have found the desired key (identified by its modulus). */
1.19      markus    246:                if (BN_cmp(pk->n, client_n) != 0)
1.16      markus    247:                        continue;
                    248:
1.12      markus    249:                /* check the real bits  */
1.19      markus    250:                if (bits != BN_num_bits(pk->n))
1.15      markus    251:                        log("Warning: %s, line %ld: keysize mismatch: "
                    252:                            "actual %d vs. announced %d.",
1.19      markus    253:                            file, linenum, BN_num_bits(pk->n), bits);
1.12      markus    254:
                    255:                /* We have found the desired key. */
1.19      markus    256:
1.12      markus    257:                /* Perform the challenge-response dialog for this key. */
1.19      markus    258:                if (!auth_rsa_challenge_dialog(pk)) {
1.12      markus    259:                        /* Wrong response. */
                    260:                        verbose("Wrong response to RSA authentication challenge.");
                    261:                        packet_send_debug("Wrong response to RSA authentication challenge.");
                    262:                        continue;
1.1       provos    263:                }
1.14      markus    264:                /*
                    265:                 * Correct response.  The client has been successfully
                    266:                 * authenticated. Note that we have not yet processed the
                    267:                 * options; this will be reset if the options cause the
                    268:                 * authentication to be rejected.
                    269:                 * Break out of the loop if authentication was successful;
                    270:                 * otherwise continue searching.
                    271:                 */
1.25      markus    272:                authenticated = auth_parse_options(pw, options, linenum);
1.12      markus    273:                if (authenticated)
1.1       provos    274:                        break;
                    275:        }
                    276:
1.12      markus    277:        /* Restore the privileged uid. */
                    278:        restore_uid();
                    279:
                    280:        /* Close the file. */
                    281:        fclose(f);
                    282:
1.19      markus    283:        RSA_free(pk);
1.1       provos    284:
1.12      markus    285:        if (authenticated)
                    286:                packet_send_debug("RSA authentication accepted.");
1.31    ! markus    287:        else
        !           288:                auth_clear_options();
1.1       provos    289:
1.12      markus    290:        /* Return authentication result. */
                    291:        return authenticated;
1.1       provos    292: }