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