]> andersk Git - openssh.git/blobdiff - auth-rsa.c
- djm@cvs.openbsd.org 2010/01/30 02:54:53
[openssh.git] / auth-rsa.c
index 16369d47c80de83c6113af809e78eeff54b61aa8..bf54620760ad62a8221765ec55e315bd9fa99ed9 100644 (file)
@@ -1,3 +1,4 @@
+/* $OpenBSD: auth-rsa.c,v 1.73 2008/07/02 12:03:51 dtucker Exp $ */
 /*
  * Author: Tatu Ylonen <ylo@cs.hut.fi>
  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
  */
 
 #include "includes.h"
-RCSID("$OpenBSD: auth-rsa.c,v 1.60 2004/06/21 17:36:31 avsm Exp $");
+
+#include <sys/types.h>
+#include <sys/stat.h>
 
 #include <openssl/rsa.h>
 #include <openssl/md5.h>
 
+#include <pwd.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+
+#include "xmalloc.h"
 #include "rsa.h"
 #include "packet.h"
-#include "xmalloc.h"
 #include "ssh1.h"
 #include "uidswap.h"
 #include "match.h"
+#include "buffer.h"
 #include "auth-options.h"
 #include "pathnames.h"
 #include "log.h"
 #include "servconf.h"
-#include "auth.h"
+#include "key.h"
 #include "hostfile.h"
+#include "auth.h"
+#ifdef GSSAPI
+#include "ssh-gss.h"
+#endif
 #include "monitor_wrap.h"
 #include "ssh.h"
+#include "misc.h"
 
 /* import */
 extern ServerOptions options;
@@ -49,7 +63,7 @@ extern u_char session_id[16];
  *   options bits e n comment
  * where bits, e and n are decimal numbers,
  * and comment is any string of characters up to newline.  The maximum
- * length of a line is 8000 characters.  See the documentation for a
+ * length of a line is SSH_MAX_PUBKEY_BYTES characters.  See sshd(8) for a
  * description of the options.
  */
 
@@ -62,10 +76,12 @@ auth_rsa_generate_challenge(Key *key)
        if ((challenge = BN_new()) == NULL)
                fatal("auth_rsa_generate_challenge: BN_new() failed");
        /* Generate a random challenge. */
-       BN_rand(challenge, 256, 0, 0);
+       if (BN_rand(challenge, 256, 0, 0) == 0)
+               fatal("auth_rsa_generate_challenge: BN_rand failed");
        if ((ctx = BN_CTX_new()) == NULL)
-               fatal("auth_rsa_generate_challenge: BN_CTX_new() failed");
-       BN_mod(challenge, challenge, key->rsa->n, ctx);
+               fatal("auth_rsa_generate_challenge: BN_CTX_new failed");
+       if (BN_mod(challenge, challenge, key->rsa->n, ctx) == 0)
+               fatal("auth_rsa_generate_challenge: BN_mod failed");
        BN_CTX_free(ctx);
 
        return challenge;
@@ -136,7 +152,7 @@ auth_rsa_challenge_dialog(Key *key)
        /* Wait for a response. */
        packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE);
        for (i = 0; i < 16; i++)
-               response[i] = packet_get_char();
+               response[i] = (u_char)packet_get_char();
        packet_check_eom();
 
        success = PRIVSEP(auth_rsa_verify_response(key, challenge, response));
@@ -152,12 +168,11 @@ auth_rsa_challenge_dialog(Key *key)
 int
 auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
 {
-       char line[8192], *file;
+       char line[SSH_MAX_PUBKEY_BYTES], *file;
        int allowed = 0;
        u_int bits;
        FILE *f;
        u_long linenum = 0;
-       struct stat st;
        Key *key;
 
        /* Temporarily use the user's uid. */
@@ -166,27 +181,9 @@ auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
        /* The authorized keys. */
        file = authorized_keys_file(pw);
        debug("trying public RSA key file %s", file);
-
-       /* Fail quietly if file does not exist */
-       if (stat(file, &st) < 0) {
-               /* Restore the privileged uid. */
-               restore_uid();
-               xfree(file);
-               return (0);
-       }
-       /* Open the file containing the authorized keys. */
-       f = fopen(file, "r");
+       f = auth_openkeyfile(file, pw, options.strict_modes);
        if (!f) {
-               /* Restore the privileged uid. */
-               restore_uid();
                xfree(file);
-               return (0);
-       }
-       if (options.strict_modes &&
-           secure_filename(f, file, pw, line, sizeof(line)) != 0) {
-               xfree(file);
-               fclose(f);
-               logit("Authentication refused: %s", line);
                restore_uid();
                return (0);
        }
@@ -201,11 +198,10 @@ auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
         * found, perform a challenge-response dialog to verify that the
         * user really has the corresponding private key.
         */
-       while (fgets(line, sizeof(line), f)) {
+       while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
                char *cp;
                char *key_options;
-
-               linenum++;
+               int keybits;
 
                /* Skip leading whitespace, empty and comment lines. */
                for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
@@ -244,7 +240,8 @@ auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
                        continue;
 
                /* check the real bits  */
-               if (bits != BN_num_bits(key->rsa->n))
+               keybits = BN_num_bits(key->rsa->n);
+               if (keybits < 0 || bits != (u_int)keybits)
                        logit("Warning: %s, line %lu: keysize mismatch: "
                            "actual %d vs. announced %d.",
                            file, linenum, BN_num_bits(key->rsa->n), bits);
This page took 1.686559 seconds and 4 git commands to generate.