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