djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 1 | /* $OpenBSD: auth-rsa.c,v 1.89 2014/12/21 22:27:56 djm Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 9 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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". |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 15 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 16 | |
| 17 | #include "includes.h" |
Damien Miller | f17883e | 2006-03-15 11:45:54 +1100 | [diff] [blame] | 18 | |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/stat.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 21 | |
| 22 | #include <openssl/rsa.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 23 | |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 24 | #include <pwd.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 25 | #include <stdio.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 26 | #include <stdarg.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 27 | #include <string.h> |
Damien Miller | 9f2abc4 | 2006-07-10 20:53:08 +1000 | [diff] [blame] | 28 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 29 | #include "xmalloc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 30 | #include "rsa.h" |
| 31 | #include "packet.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 32 | #include "ssh1.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 33 | #include "uidswap.h" |
| 34 | #include "match.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 35 | #include "buffer.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 36 | #include "pathnames.h" |
| 37 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 38 | #include "misc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 39 | #include "servconf.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 40 | #include "key.h" |
Damien Miller | 4e270b0 | 2010-04-16 15:56:21 +1000 | [diff] [blame] | 41 | #include "auth-options.h" |
Damien Miller | 8968121 | 2001-12-21 12:52:39 +1100 | [diff] [blame] | 42 | #include "hostfile.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 43 | #include "auth.h" |
| 44 | #ifdef GSSAPI |
| 45 | #include "ssh-gss.h" |
| 46 | #endif |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 47 | #include "monitor_wrap.h" |
Ben Lindstrom | 03f3932 | 2002-04-02 20:43:11 +0000 | [diff] [blame] | 48 | #include "ssh.h" |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 49 | |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 50 | #include "digest.h" |
| 51 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 52 | /* import */ |
| 53 | extern ServerOptions options; |
| 54 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 55 | /* |
| 56 | * Session identifier that is used to bind key exchange and authentication |
| 57 | * responses to a particular session. |
| 58 | */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 59 | extern u_char session_id[16]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 60 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 67 | * length of a line is SSH_MAX_PUBKEY_BYTES characters. See sshd(8) for a |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 68 | * description of the options. |
| 69 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 70 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 71 | BIGNUM * |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 72 | auth_rsa_generate_challenge(Key *key) |
| 73 | { |
| 74 | BIGNUM *challenge; |
| 75 | BN_CTX *ctx; |
| 76 | |
| 77 | if ((challenge = BN_new()) == NULL) |
| 78 | fatal("auth_rsa_generate_challenge: BN_new() failed"); |
| 79 | /* Generate a random challenge. */ |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 80 | if (BN_rand(challenge, 256, 0, 0) == 0) |
| 81 | fatal("auth_rsa_generate_challenge: BN_rand failed"); |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 82 | if ((ctx = BN_CTX_new()) == NULL) |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 83 | fatal("auth_rsa_generate_challenge: BN_CTX_new failed"); |
| 84 | if (BN_mod(challenge, challenge, key->rsa->n, ctx) == 0) |
| 85 | fatal("auth_rsa_generate_challenge: BN_mod failed"); |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 86 | BN_CTX_free(ctx); |
| 87 | |
| 88 | return challenge; |
| 89 | } |
| 90 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 91 | int |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 92 | auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16]) |
| 93 | { |
| 94 | u_char buf[32], mdbuf[16]; |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 95 | struct ssh_digest_ctx *md; |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 96 | int len; |
| 97 | |
Ben Lindstrom | e1f9e32 | 2002-03-27 17:38:43 +0000 | [diff] [blame] | 98 | /* don't allow short keys */ |
Ben Lindstrom | 03f3932 | 2002-04-02 20:43:11 +0000 | [diff] [blame] | 99 | if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) { |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 100 | error("%s: RSA modulus too small: %d < minimum %d bits", |
| 101 | __func__, |
Ben Lindstrom | 2779d28 | 2002-06-11 15:47:42 +0000 | [diff] [blame] | 102 | BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE); |
Ben Lindstrom | e1f9e32 | 2002-03-27 17:38:43 +0000 | [diff] [blame] | 103 | return (0); |
| 104 | } |
| 105 | |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 106 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 107 | len = BN_num_bytes(challenge); |
| 108 | if (len <= 0 || len > 32) |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 109 | fatal("%s: bad challenge length %d", __func__, len); |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 110 | memset(buf, 0, 32); |
| 111 | BN_bn2bin(challenge, buf + 32 - len); |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 112 | if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL || |
| 113 | ssh_digest_update(md, buf, 32) < 0 || |
| 114 | ssh_digest_update(md, session_id, 16) < 0 || |
| 115 | ssh_digest_final(md, mdbuf, sizeof(mdbuf)) < 0) |
| 116 | fatal("%s: md5 failed", __func__); |
| 117 | ssh_digest_free(md); |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 118 | |
| 119 | /* Verify that the response is the original challenge. */ |
Damien Miller | ea1651c | 2010-07-16 13:58:37 +1000 | [diff] [blame] | 120 | if (timingsafe_bcmp(response, mdbuf, 16) != 0) { |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 121 | /* Wrong answer. */ |
| 122 | return (0); |
| 123 | } |
| 124 | /* Correct answer. */ |
| 125 | return (1); |
| 126 | } |
| 127 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 128 | /* |
| 129 | * Performs the RSA authentication challenge-response dialog with the client, |
| 130 | * and returns true (non-zero) if the client gave the correct answer to |
| 131 | * our challenge; returns zero if the client gives a wrong answer. |
| 132 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 133 | |
| 134 | int |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 135 | auth_rsa_challenge_dialog(Key *key) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 136 | { |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 137 | BIGNUM *challenge, *encrypted_challenge; |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 138 | u_char response[16]; |
| 139 | int i, success; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 140 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 141 | if ((encrypted_challenge = BN_new()) == NULL) |
| 142 | fatal("auth_rsa_challenge_dialog: BN_new() failed"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 143 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 144 | challenge = PRIVSEP(auth_rsa_generate_challenge(key)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 145 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 146 | /* Encrypt the challenge with the public key. */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 147 | if (rsa_public_encrypt(encrypted_challenge, challenge, key->rsa) != 0) |
| 148 | fatal("%s: rsa_public_encrypt failed", __func__); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 149 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 150 | /* Send the encrypted challenge to the client. */ |
| 151 | packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE); |
| 152 | packet_put_bignum(encrypted_challenge); |
| 153 | packet_send(); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 154 | BN_clear_free(encrypted_challenge); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 155 | packet_write_wait(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 156 | |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 157 | /* Wait for a response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 158 | packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 159 | for (i = 0; i < 16; i++) |
Damien Miller | 8ba29fe | 2006-03-26 14:25:19 +1100 | [diff] [blame] | 160 | response[i] = (u_char)packet_get_char(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 161 | packet_check_eom(); |
Damien Miller | 98c7ad6 | 2000-03-09 21:27:49 +1100 | [diff] [blame] | 162 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 163 | success = PRIVSEP(auth_rsa_verify_response(key, challenge, response)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 164 | BN_clear_free(challenge); |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 165 | return (success); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 166 | } |
| 167 | |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 168 | static int |
| 169 | rsa_key_allowed_in_file(struct passwd *pw, char *file, |
| 170 | const BIGNUM *client_n, Key **rkey) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 171 | { |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 172 | char *fp, line[SSH_MAX_PUBKEY_BYTES]; |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 173 | int allowed = 0, bits; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 174 | FILE *f; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 175 | u_long linenum = 0; |
Damien Miller | 8968121 | 2001-12-21 12:52:39 +1100 | [diff] [blame] | 176 | Key *key; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 177 | |
Ben Lindstrom | bfb3a0e | 2001-06-05 20:25:05 +0000 | [diff] [blame] | 178 | debug("trying public RSA key file %s", file); |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 179 | if ((f = auth_openkeyfile(file, pw, options.strict_modes)) == NULL) |
| 180 | return 0; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 181 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 182 | /* |
| 183 | * Go though the accepted keys, looking for the current key. If |
| 184 | * found, perform a challenge-response dialog to verify that the |
| 185 | * user really has the corresponding private key. |
| 186 | */ |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 187 | key = key_new(KEY_RSA1); |
Darren Tucker | 22cc741 | 2004-12-06 22:47:41 +1100 | [diff] [blame] | 188 | while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 189 | char *cp; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 190 | char *key_options; |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 191 | int keybits; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 192 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 193 | /* Skip leading whitespace, empty and comment lines. */ |
| 194 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) |
| 195 | ; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 196 | if (!*cp || *cp == '\n' || *cp == '#') |
| 197 | continue; |
| 198 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 199 | /* |
| 200 | * Check if there are options for this key, and if so, |
| 201 | * save their starting address and skip the option part |
| 202 | * for now. If there are no options, set the starting |
| 203 | * address to NULL. |
| 204 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 205 | if (*cp < '0' || *cp > '9') { |
| 206 | int quoted = 0; |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 207 | key_options = cp; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 208 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { |
| 209 | if (*cp == '\\' && cp[1] == '"') |
| 210 | cp++; /* Skip both */ |
| 211 | else if (*cp == '"') |
| 212 | quoted = !quoted; |
| 213 | } |
| 214 | } else |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 215 | key_options = NULL; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 216 | |
| 217 | /* Parse the key from the line. */ |
Damien Miller | 8968121 | 2001-12-21 12:52:39 +1100 | [diff] [blame] | 218 | if (hostfile_read_key(&cp, &bits, key) == 0) { |
Ben Lindstrom | f96704d | 2001-06-25 04:17:12 +0000 | [diff] [blame] | 219 | debug("%.100s, line %lu: non ssh1 key syntax", |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 220 | file, linenum); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 221 | continue; |
| 222 | } |
| 223 | /* cp now points to the comment part. */ |
| 224 | |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 225 | /* |
| 226 | * Check if the we have found the desired key (identified |
| 227 | * by its modulus). |
| 228 | */ |
Damien Miller | 8968121 | 2001-12-21 12:52:39 +1100 | [diff] [blame] | 229 | if (BN_cmp(key->rsa->n, client_n) != 0) |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 230 | continue; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 231 | |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 232 | /* check the real bits */ |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 233 | keybits = BN_num_bits(key->rsa->n); |
Damien Miller | ce98654 | 2013-07-18 16:12:44 +1000 | [diff] [blame] | 234 | if (keybits < 0 || bits != keybits) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 235 | logit("Warning: %s, line %lu: keysize mismatch: " |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 236 | "actual %d vs. announced %d.", |
Damien Miller | 8968121 | 2001-12-21 12:52:39 +1100 | [diff] [blame] | 237 | file, linenum, BN_num_bits(key->rsa->n), bits); |
Damien Miller | aae6c61 | 1999-12-06 11:47:28 +1100 | [diff] [blame] | 238 | |
djm@openbsd.org | 56d1c83 | 2014-12-21 22:27:55 +0000 | [diff] [blame] | 239 | fp = key_fingerprint(key, options.fingerprint_hash, |
| 240 | SSH_FP_DEFAULT); |
Darren Tucker | 0acca37 | 2013-06-02 07:41:51 +1000 | [diff] [blame] | 241 | debug("matching key found: file %s, line %lu %s %s", |
| 242 | file, linenum, key_type(key), fp); |
| 243 | free(fp); |
| 244 | |
Darren Tucker | adab6f1 | 2010-12-05 09:01:47 +1100 | [diff] [blame] | 245 | /* Never accept a revoked key */ |
| 246 | if (auth_key_is_revoked(key)) |
| 247 | break; |
| 248 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 249 | /* We have found the desired key. */ |
Ben Lindstrom | 1492029 | 2000-11-21 21:24:55 +0000 | [diff] [blame] | 250 | /* |
| 251 | * If our options do not allow this key to be used, |
| 252 | * do not send challenge. |
| 253 | */ |
Darren Tucker | 3f9fdc7 | 2004-06-22 12:56:01 +1000 | [diff] [blame] | 254 | if (!auth_parse_options(pw, key_options, file, linenum)) |
Ben Lindstrom | 1492029 | 2000-11-21 21:24:55 +0000 | [diff] [blame] | 255 | continue; |
Damien Miller | 3b90382 | 2010-05-21 14:56:25 +1000 | [diff] [blame] | 256 | if (key_is_cert_authority) |
| 257 | continue; |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 258 | /* break out, this key is allowed */ |
| 259 | allowed = 1; |
Damien Miller | 50a41ed | 2000-10-16 12:14:42 +1100 | [diff] [blame] | 260 | break; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 261 | } |
| 262 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 263 | /* Close the file. */ |
| 264 | fclose(f); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 265 | |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 266 | /* return key if allowed */ |
| 267 | if (allowed && rkey != NULL) |
| 268 | *rkey = key; |
| 269 | else |
| 270 | key_free(key); |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 271 | |
| 272 | return allowed; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * check if there's user key matching client_n, |
| 277 | * return key if login is allowed, NULL otherwise |
| 278 | */ |
| 279 | |
| 280 | int |
| 281 | auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey) |
| 282 | { |
| 283 | char *file; |
| 284 | u_int i, allowed = 0; |
| 285 | |
| 286 | temporarily_use_uid(pw); |
| 287 | |
| 288 | for (i = 0; !allowed && i < options.num_authkeys_files; i++) { |
Damien Miller | 09d3e12 | 2012-10-31 08:58:58 +1100 | [diff] [blame] | 289 | if (strcasecmp(options.authorized_keys_files[i], "none") == 0) |
| 290 | continue; |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 291 | file = expand_authorized_keys( |
| 292 | options.authorized_keys_files[i], pw); |
| 293 | allowed = rsa_key_allowed_in_file(pw, file, client_n, rkey); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 294 | free(file); |
Damien Miller | d8478b6 | 2011-05-29 21:39:36 +1000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | restore_uid(); |
| 298 | |
| 299 | return allowed; |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* |
| 303 | * Performs the RSA authentication dialog with the client. This returns |
| 304 | * 0 if the client could not be authenticated, and 1 if authentication was |
| 305 | * successful. This may exit if there is a serious protocol violation. |
| 306 | */ |
| 307 | int |
Damien Miller | 3e3b514 | 2003-11-17 21:13:40 +1100 | [diff] [blame] | 308 | auth_rsa(Authctxt *authctxt, BIGNUM *client_n) |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 309 | { |
| 310 | Key *key; |
Damien Miller | 3e3b514 | 2003-11-17 21:13:40 +1100 | [diff] [blame] | 311 | struct passwd *pw = authctxt->pw; |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 312 | |
| 313 | /* no user given */ |
Damien Miller | 3e3b514 | 2003-11-17 21:13:40 +1100 | [diff] [blame] | 314 | if (!authctxt->valid) |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 315 | return 0; |
| 316 | |
Ben Lindstrom | 7a2073c | 2002-03-22 02:30:41 +0000 | [diff] [blame] | 317 | if (!PRIVSEP(auth_rsa_key_allowed(pw, client_n, &key))) { |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 318 | auth_clear_options(); |
| 319 | return (0); |
| 320 | } |
| 321 | |
| 322 | /* Perform the challenge-response dialog for this key. */ |
| 323 | if (!auth_rsa_challenge_dialog(key)) { |
| 324 | /* Wrong response. */ |
| 325 | verbose("Wrong response to RSA authentication challenge."); |
| 326 | packet_send_debug("Wrong response to RSA authentication challenge."); |
| 327 | /* |
| 328 | * Break out of the loop. Otherwise we might send |
| 329 | * another challenge and break the protocol. |
| 330 | */ |
| 331 | key_free(key); |
| 332 | return (0); |
| 333 | } |
| 334 | /* |
| 335 | * Correct response. The client has been successfully |
| 336 | * authenticated. Note that we have not yet processed the |
| 337 | * options; this will be reset if the options cause the |
| 338 | * authentication to be rejected. |
| 339 | */ |
Damien Miller | 20bdcd7 | 2013-07-18 16:10:09 +1000 | [diff] [blame] | 340 | pubkey_auth_info(authctxt, key, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 341 | |
Ben Lindstrom | 9c8aefe | 2002-03-22 01:12:58 +0000 | [diff] [blame] | 342 | packet_send_debug("RSA authentication accepted."); |
| 343 | return (1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 344 | } |