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

1.86    ! markus      1: /* $OpenBSD: auth-rsa.c,v 1.85 2013/07/12 00:19:58 djm Exp $ */
1.1       provos      2: /*
1.13      deraadt     3:  * Author: Tatu Ylonen <ylo@cs.hut.fi>
                      4:  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
                      5:  *                    All rights reserved
                      6:  * RSA-based authentication.  This code determines whether to admit a login
                      7:  * based on RSA authentication.  This file also contains functions to check
                      8:  * validity of the host key.
1.21      markus      9:  *
1.28      deraadt    10:  * As far as I am concerned, the code I have written for this software
                     11:  * can be used freely for any purpose.  Any derived versions of this
                     12:  * software must be clearly marked as such, and if the derived work is
                     13:  * incompatible with the protocol description in the RFC file, it must be
                     14:  * called by a name other than "ssh" or "Secure Shell".
1.13      deraadt    15:  */
1.1       provos     16:
1.64      stevesk    17: #include <sys/types.h>
                     18: #include <sys/stat.h>
1.38      markus     19:
                     20: #include <openssl/rsa.h>
1.68      stevesk    21:
                     22: #include <pwd.h>
1.70      stevesk    23: #include <stdio.h>
1.69      stevesk    24: #include <string.h>
1.1       provos     25:
1.71      deraadt    26: #include "xmalloc.h"
1.1       provos     27: #include "rsa.h"
                     28: #include "packet.h"
1.35      markus     29: #include "ssh1.h"
1.1       provos     30: #include "uidswap.h"
1.19      markus     31: #include "match.h"
1.71      deraadt    32: #include "buffer.h"
1.35      markus     33: #include "pathnames.h"
1.38      markus     34: #include "log.h"
                     35: #include "servconf.h"
1.71      deraadt    36: #include "key.h"
1.75      djm        37: #include "auth-options.h"
1.71      deraadt    38: #include "hostfile.h"
1.38      markus     39: #include "auth.h"
1.71      deraadt    40: #ifdef GSSAPI
                     41: #include "ssh-gss.h"
                     42: #endif
1.52      provos     43: #include "monitor_wrap.h"
1.55      stevesk    44: #include "ssh.h"
1.62      dtucker    45: #include "misc.h"
1.30      markus     46:
1.86    ! markus     47: #include "digest.h"
        !            48:
1.30      markus     49: /* import */
                     50: extern ServerOptions options;
                     51:
1.14      markus     52: /*
                     53:  * Session identifier that is used to bind key exchange and authentication
                     54:  * responses to a particular session.
                     55:  */
1.34      markus     56: extern u_char session_id[16];
1.1       provos     57:
1.14      markus     58: /*
                     59:  * The .ssh/authorized_keys file contains public keys, one per line, in the
                     60:  * following format:
                     61:  *   options bits e n comment
                     62:  * where bits, e and n are decimal numbers,
                     63:  * and comment is any string of characters up to newline.  The maximum
1.61      dtucker    64:  * length of a line is SSH_MAX_PUBKEY_BYTES characters.  See sshd(8) for a
1.14      markus     65:  * description of the options.
                     66:  */
                     67:
1.52      provos     68: BIGNUM *
1.51      markus     69: auth_rsa_generate_challenge(Key *key)
                     70: {
                     71:        BIGNUM *challenge;
                     72:        BN_CTX *ctx;
                     73:
                     74:        if ((challenge = BN_new()) == NULL)
                     75:                fatal("auth_rsa_generate_challenge: BN_new() failed");
                     76:        /* Generate a random challenge. */
1.72      markus     77:        if (BN_rand(challenge, 256, 0, 0) == 0)
                     78:                fatal("auth_rsa_generate_challenge: BN_rand failed");
1.51      markus     79:        if ((ctx = BN_CTX_new()) == NULL)
1.72      markus     80:                fatal("auth_rsa_generate_challenge: BN_CTX_new failed");
                     81:        if (BN_mod(challenge, challenge, key->rsa->n, ctx) == 0)
                     82:                fatal("auth_rsa_generate_challenge: BN_mod failed");
1.51      markus     83:        BN_CTX_free(ctx);
                     84:
                     85:        return challenge;
                     86: }
                     87:
1.52      provos     88: int
1.51      markus     89: auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16])
                     90: {
                     91:        u_char buf[32], mdbuf[16];
1.86    ! markus     92:        struct ssh_digest_ctx *md;
1.51      markus     93:        int len;
1.74      djm        94:
1.54      markus     95:        /* don't allow short keys */
1.55      stevesk    96:        if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
1.86    ! markus     97:                error("%s: RSA modulus too small: %d < minimum %d bits",
        !            98:                    __func__,
1.56      stevesk    99:                    BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
1.54      markus    100:                return (0);
                    101:        }
1.51      markus    102:
                    103:        /* The response is MD5 of decrypted challenge plus session id. */
                    104:        len = BN_num_bytes(challenge);
                    105:        if (len <= 0 || len > 32)
1.86    ! markus    106:                fatal("%s: bad challenge length %d", __func__, len);
1.51      markus    107:        memset(buf, 0, 32);
                    108:        BN_bn2bin(challenge, buf + 32 - len);
1.86    ! markus    109:        if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL ||
        !           110:            ssh_digest_update(md, buf, 32) < 0 ||
        !           111:            ssh_digest_update(md, session_id, 16) < 0 ||
        !           112:            ssh_digest_final(md, mdbuf, sizeof(mdbuf)) < 0)
        !           113:                fatal("%s: md5 failed", __func__);
        !           114:        ssh_digest_free(md);
1.51      markus    115:
                    116:        /* Verify that the response is the original challenge. */
1.78      djm       117:        if (timingsafe_bcmp(response, mdbuf, 16) != 0) {
1.51      markus    118:                /* Wrong answer. */
                    119:                return (0);
                    120:        }
                    121:        /* Correct answer. */
                    122:        return (1);
                    123: }
                    124:
1.14      markus    125: /*
                    126:  * Performs the RSA authentication challenge-response dialog with the client,
                    127:  * and returns true (non-zero) if the client gave the correct answer to
                    128:  * our challenge; returns zero if the client gives a wrong answer.
                    129:  */
1.1       provos    130:
                    131: int
1.51      markus    132: auth_rsa_challenge_dialog(Key *key)
1.1       provos    133: {
1.18      markus    134:        BIGNUM *challenge, *encrypted_challenge;
1.51      markus    135:        u_char response[16];
                    136:        int i, success;
1.12      markus    137:
1.47      markus    138:        if ((encrypted_challenge = BN_new()) == NULL)
                    139:                fatal("auth_rsa_challenge_dialog: BN_new() failed");
1.12      markus    140:
1.52      provos    141:        challenge = PRIVSEP(auth_rsa_generate_challenge(key));
1.12      markus    142:
                    143:        /* Encrypt the challenge with the public key. */
1.51      markus    144:        rsa_public_encrypt(encrypted_challenge, challenge, key->rsa);
1.12      markus    145:
                    146:        /* Send the encrypted challenge to the client. */
                    147:        packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
                    148:        packet_put_bignum(encrypted_challenge);
                    149:        packet_send();
1.18      markus    150:        BN_clear_free(encrypted_challenge);
1.12      markus    151:        packet_write_wait();
                    152:
1.18      markus    153:        /* Wait for a response. */
1.50      markus    154:        packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE);
1.18      markus    155:        for (i = 0; i < 16; i++)
1.67      deraadt   156:                response[i] = (u_char)packet_get_char();
1.49      markus    157:        packet_check_eom();
1.18      markus    158:
1.52      provos    159:        success = PRIVSEP(auth_rsa_verify_response(key, challenge, response));
1.12      markus    160:        BN_clear_free(challenge);
1.51      markus    161:        return (success);
1.1       provos    162: }
                    163:
1.80      djm       164: static int
                    165: rsa_key_allowed_in_file(struct passwd *pw, char *file,
                    166:     const BIGNUM *client_n, Key **rkey)
1.1       provos    167: {
1.83      djm       168:        char *fp, line[SSH_MAX_PUBKEY_BYTES];
1.85      djm       169:        int allowed = 0, bits;
1.12      markus    170:        FILE *f;
1.34      markus    171:        u_long linenum = 0;
1.46      jakob     172:        Key *key;
1.12      markus    173:
1.41      markus    174:        debug("trying public RSA key file %s", file);
1.80      djm       175:        if ((f = auth_openkeyfile(file, pw, options.strict_modes)) == NULL)
                    176:                return 0;
1.12      markus    177:
1.14      markus    178:        /*
                    179:         * Go though the accepted keys, looking for the current key.  If
                    180:         * found, perform a challenge-response dialog to verify that the
                    181:         * user really has the corresponding private key.
                    182:         */
1.80      djm       183:        key = key_new(KEY_RSA1);
1.61      dtucker   184:        while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.12      markus    185:                char *cp;
1.60      avsm      186:                char *key_options;
1.63      djm       187:                int keybits;
1.12      markus    188:
1.14      markus    189:                /* Skip leading whitespace, empty and comment lines. */
                    190:                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
                    191:                        ;
1.12      markus    192:                if (!*cp || *cp == '\n' || *cp == '#')
                    193:                        continue;
                    194:
1.14      markus    195:                /*
                    196:                 * Check if there are options for this key, and if so,
                    197:                 * save their starting address and skip the option part
                    198:                 * for now.  If there are no options, set the starting
                    199:                 * address to NULL.
                    200:                 */
1.12      markus    201:                if (*cp < '0' || *cp > '9') {
                    202:                        int quoted = 0;
1.60      avsm      203:                        key_options = cp;
1.12      markus    204:                        for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
                    205:                                if (*cp == '\\' && cp[1] == '"')
                    206:                                        cp++;   /* Skip both */
                    207:                                else if (*cp == '"')
                    208:                                        quoted = !quoted;
                    209:                        }
                    210:                } else
1.60      avsm      211:                        key_options = NULL;
1.1       provos    212:
1.12      markus    213:                /* Parse the key from the line. */
1.46      jakob     214:                if (hostfile_read_key(&cp, &bits, key) == 0) {
1.42      markus    215:                        debug("%.100s, line %lu: non ssh1 key syntax",
1.36      markus    216:                            file, linenum);
1.12      markus    217:                        continue;
1.1       provos    218:                }
1.12      markus    219:                /* cp now points to the comment part. */
                    220:
1.80      djm       221:                /*
                    222:                 * Check if the we have found the desired key (identified
                    223:                 * by its modulus).
                    224:                 */
1.46      jakob     225:                if (BN_cmp(key->rsa->n, client_n) != 0)
1.16      markus    226:                        continue;
                    227:
1.12      markus    228:                /* check the real bits  */
1.63      djm       229:                keybits = BN_num_bits(key->rsa->n);
1.85      djm       230:                if (keybits < 0 || bits != keybits)
1.57      itojun    231:                        logit("Warning: %s, line %lu: keysize mismatch: "
1.15      markus    232:                            "actual %d vs. announced %d.",
1.46      jakob     233:                            file, linenum, BN_num_bits(key->rsa->n), bits);
1.79      djm       234:
1.83      djm       235:                fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
                    236:                debug("matching key found: file %s, line %lu %s %s",
                    237:                    file, linenum, key_type(key), fp);
                    238:                free(fp);
                    239:
1.79      djm       240:                /* Never accept a revoked key */
                    241:                if (auth_key_is_revoked(key))
                    242:                        break;
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.60      avsm      249:                if (!auth_parse_options(pw, key_options, file, linenum))
1.33      markus    250:                        continue;
1.76      djm       251:                if (key_is_cert_authority)
                    252:                        continue;
1.51      markus    253:                /* break out, this key is allowed */
                    254:                allowed = 1;
1.32      markus    255:                break;
1.1       provos    256:        }
                    257:
1.12      markus    258:        /* Close the file. */
                    259:        fclose(f);
                    260:
1.51      markus    261:        /* return key if allowed */
                    262:        if (allowed && rkey != NULL)
                    263:                *rkey = key;
                    264:        else
                    265:                key_free(key);
1.80      djm       266:
                    267:        return allowed;
                    268: }
                    269:
                    270: /*
                    271:  * check if there's user key matching client_n,
                    272:  * return key if login is allowed, NULL otherwise
                    273:  */
                    274:
                    275: int
                    276: auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
                    277: {
                    278:        char *file;
                    279:        u_int i, allowed = 0;
                    280:
                    281:        temporarily_use_uid(pw);
                    282:
                    283:        for (i = 0; !allowed && i < options.num_authkeys_files; i++) {
1.81      djm       284:                if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
                    285:                        continue;
1.80      djm       286:                file = expand_authorized_keys(
                    287:                    options.authorized_keys_files[i], pw);
                    288:                allowed = rsa_key_allowed_in_file(pw, file, client_n, rkey);
1.82      djm       289:                free(file);
1.80      djm       290:        }
                    291:
                    292:        restore_uid();
                    293:
                    294:        return allowed;
1.51      markus    295: }
                    296:
                    297: /*
                    298:  * Performs the RSA authentication dialog with the client.  This returns
                    299:  * 0 if the client could not be authenticated, and 1 if authentication was
                    300:  * successful.  This may exit if there is a serious protocol violation.
                    301:  */
                    302: int
1.58      djm       303: auth_rsa(Authctxt *authctxt, BIGNUM *client_n)
1.51      markus    304: {
                    305:        Key *key;
1.58      djm       306:        struct passwd *pw = authctxt->pw;
1.51      markus    307:
                    308:        /* no user given */
1.58      djm       309:        if (!authctxt->valid)
1.51      markus    310:                return 0;
1.1       provos    311:
1.52      provos    312:        if (!PRIVSEP(auth_rsa_key_allowed(pw, client_n, &key))) {
1.31      markus    313:                auth_clear_options();
1.51      markus    314:                return (0);
                    315:        }
                    316:
                    317:        /* Perform the challenge-response dialog for this key. */
                    318:        if (!auth_rsa_challenge_dialog(key)) {
                    319:                /* Wrong response. */
                    320:                verbose("Wrong response to RSA authentication challenge.");
                    321:                packet_send_debug("Wrong response to RSA authentication challenge.");
                    322:                /*
                    323:                 * Break out of the loop. Otherwise we might send
                    324:                 * another challenge and break the protocol.
                    325:                 */
                    326:                key_free(key);
                    327:                return (0);
                    328:        }
                    329:        /*
                    330:         * Correct response.  The client has been successfully
                    331:         * authenticated. Note that we have not yet processed the
                    332:         * options; this will be reset if the options cause the
                    333:         * authentication to be rejected.
                    334:         */
1.84      djm       335:        pubkey_auth_info(authctxt, key, NULL);
1.1       provos    336:
1.51      markus    337:        packet_send_debug("RSA authentication accepted.");
                    338:        return (1);
1.1       provos    339: }