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

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