]>
Commit | Line | Data |
---|---|---|
8efc0c15 | 1 | /* |
5260325f | 2 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
5260325f | 3 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
4 | * All rights reserved | |
5260325f | 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. | |
6ae2364d | 8 | * |
bcbf86ec | 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". | |
5260325f | 14 | */ |
8efc0c15 | 15 | |
16 | #include "includes.h" | |
46f1eece | 17 | RCSID("$OpenBSD: auth-rsa.c,v 1.51 2002/03/14 16:56:33 markus Exp $"); |
42f11eb2 | 18 | |
19 | #include <openssl/rsa.h> | |
20 | #include <openssl/md5.h> | |
8efc0c15 | 21 | |
22 | #include "rsa.h" | |
23 | #include "packet.h" | |
24 | #include "xmalloc.h" | |
42f11eb2 | 25 | #include "ssh1.h" |
8efc0c15 | 26 | #include "mpaux.h" |
27 | #include "uidswap.h" | |
4fe2af09 | 28 | #include "match.h" |
38c295d6 | 29 | #include "auth-options.h" |
42f11eb2 | 30 | #include "pathnames.h" |
31 | #include "log.h" | |
32 | #include "servconf.h" | |
33 | #include "auth.h" | |
46df736f | 34 | #include "hostfile.h" |
94ec8c6b | 35 | |
36 | /* import */ | |
37 | extern ServerOptions options; | |
38 | ||
aa3378df | 39 | /* |
40 | * Session identifier that is used to bind key exchange and authentication | |
41 | * responses to a particular session. | |
42 | */ | |
1e3b8b07 | 43 | extern u_char session_id[16]; |
8efc0c15 | 44 | |
aa3378df | 45 | /* |
46 | * The .ssh/authorized_keys file contains public keys, one per line, in the | |
47 | * following format: | |
48 | * options bits e n comment | |
49 | * where bits, e and n are decimal numbers, | |
50 | * and comment is any string of characters up to newline. The maximum | |
51 | * length of a line is 8000 characters. See the documentation for a | |
52 | * description of the options. | |
53 | */ | |
8efc0c15 | 54 | |
46f1eece | 55 | static BIGNUM * |
56 | auth_rsa_generate_challenge(Key *key) | |
57 | { | |
58 | BIGNUM *challenge; | |
59 | BN_CTX *ctx; | |
60 | ||
61 | if ((challenge = BN_new()) == NULL) | |
62 | fatal("auth_rsa_generate_challenge: BN_new() failed"); | |
63 | /* Generate a random challenge. */ | |
64 | BN_rand(challenge, 256, 0, 0); | |
65 | if ((ctx = BN_CTX_new()) == NULL) | |
66 | fatal("auth_rsa_generate_challenge: BN_CTX_new() failed"); | |
67 | BN_mod(challenge, challenge, key->rsa->n, ctx); | |
68 | BN_CTX_free(ctx); | |
69 | ||
70 | return challenge; | |
71 | } | |
72 | ||
73 | static int | |
74 | auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16]) | |
75 | { | |
76 | u_char buf[32], mdbuf[16]; | |
77 | MD5_CTX md; | |
78 | int len; | |
79 | ||
80 | /* The response is MD5 of decrypted challenge plus session id. */ | |
81 | len = BN_num_bytes(challenge); | |
82 | if (len <= 0 || len > 32) | |
83 | fatal("auth_rsa_verify_response: bad challenge length %d", len); | |
84 | memset(buf, 0, 32); | |
85 | BN_bn2bin(challenge, buf + 32 - len); | |
86 | MD5_Init(&md); | |
87 | MD5_Update(&md, buf, 32); | |
88 | MD5_Update(&md, session_id, 16); | |
89 | MD5_Final(mdbuf, &md); | |
90 | ||
91 | /* Verify that the response is the original challenge. */ | |
92 | if (memcmp(response, mdbuf, 16) != 0) { | |
93 | /* Wrong answer. */ | |
94 | return (0); | |
95 | } | |
96 | /* Correct answer. */ | |
97 | return (1); | |
98 | } | |
99 | ||
aa3378df | 100 | /* |
101 | * Performs the RSA authentication challenge-response dialog with the client, | |
102 | * and returns true (non-zero) if the client gave the correct answer to | |
103 | * our challenge; returns zero if the client gives a wrong answer. | |
104 | */ | |
8efc0c15 | 105 | |
106 | int | |
46f1eece | 107 | auth_rsa_challenge_dialog(Key *key) |
8efc0c15 | 108 | { |
c8d54615 | 109 | BIGNUM *challenge, *encrypted_challenge; |
46f1eece | 110 | u_char response[16]; |
111 | int i, success; | |
5260325f | 112 | |
b775c6f2 | 113 | if ((encrypted_challenge = BN_new()) == NULL) |
114 | fatal("auth_rsa_challenge_dialog: BN_new() failed"); | |
5260325f | 115 | |
46f1eece | 116 | challenge = auth_rsa_generate_challenge(key); |
5260325f | 117 | |
5260325f | 118 | /* Encrypt the challenge with the public key. */ |
46f1eece | 119 | rsa_public_encrypt(encrypted_challenge, challenge, key->rsa); |
5260325f | 120 | |
121 | /* Send the encrypted challenge to the client. */ | |
122 | packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE); | |
123 | packet_put_bignum(encrypted_challenge); | |
124 | packet_send(); | |
c8d54615 | 125 | BN_clear_free(encrypted_challenge); |
5260325f | 126 | packet_write_wait(); |
127 | ||
c8d54615 | 128 | /* Wait for a response. */ |
54a5250f | 129 | packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE); |
c8d54615 | 130 | for (i = 0; i < 16; i++) |
131 | response[i] = packet_get_char(); | |
95500969 | 132 | packet_check_eom(); |
c8d54615 | 133 | |
46f1eece | 134 | success = auth_rsa_verify_response(key, challenge, response); |
5260325f | 135 | BN_clear_free(challenge); |
46f1eece | 136 | return (success); |
8efc0c15 | 137 | } |
138 | ||
aa3378df | 139 | /* |
46f1eece | 140 | * check if there's user key matching client_n, |
141 | * return key if login is allowed, NULL otherwise | |
aa3378df | 142 | */ |
8efc0c15 | 143 | |
46f1eece | 144 | static int |
145 | auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey) | |
8efc0c15 | 146 | { |
c8445989 | 147 | char line[8192], *file; |
46f1eece | 148 | int allowed; |
1e3b8b07 | 149 | u_int bits; |
5260325f | 150 | FILE *f; |
1e3b8b07 | 151 | u_long linenum = 0; |
5260325f | 152 | struct stat st; |
46df736f | 153 | Key *key; |
94ec8c6b | 154 | |
5260325f | 155 | /* Temporarily use the user's uid. */ |
63bd8c36 | 156 | temporarily_use_uid(pw); |
5260325f | 157 | |
158 | /* The authorized keys. */ | |
c8445989 | 159 | file = authorized_keys_file(pw); |
160 | debug("trying public RSA key file %s", file); | |
5260325f | 161 | |
162 | /* Fail quietly if file does not exist */ | |
163 | if (stat(file, &st) < 0) { | |
164 | /* Restore the privileged uid. */ | |
165 | restore_uid(); | |
c8445989 | 166 | xfree(file); |
46f1eece | 167 | return (NULL); |
8efc0c15 | 168 | } |
5260325f | 169 | /* Open the file containing the authorized keys. */ |
170 | f = fopen(file, "r"); | |
171 | if (!f) { | |
172 | /* Restore the privileged uid. */ | |
173 | restore_uid(); | |
c8445989 | 174 | xfree(file); |
46f1eece | 175 | return (NULL); |
8efc0c15 | 176 | } |
c8445989 | 177 | if (options.strict_modes && |
6978866a | 178 | secure_filename(f, file, pw, line, sizeof(line)) != 0) { |
c8445989 | 179 | xfree(file); |
180 | fclose(f); | |
181 | log("Authentication refused: %s", line); | |
c8445989 | 182 | restore_uid(); |
46f1eece | 183 | return (NULL); |
5260325f | 184 | } |
46f1eece | 185 | |
186 | /* Flag indicating whether the key is allowed. */ | |
187 | allowed = 0; | |
5260325f | 188 | |
46df736f | 189 | key = key_new(KEY_RSA1); |
5260325f | 190 | |
aa3378df | 191 | /* |
192 | * Go though the accepted keys, looking for the current key. If | |
193 | * found, perform a challenge-response dialog to verify that the | |
194 | * user really has the corresponding private key. | |
195 | */ | |
5260325f | 196 | while (fgets(line, sizeof(line), f)) { |
197 | char *cp; | |
198 | char *options; | |
199 | ||
200 | linenum++; | |
201 | ||
aa3378df | 202 | /* Skip leading whitespace, empty and comment lines. */ |
203 | for (cp = line; *cp == ' ' || *cp == '\t'; cp++) | |
204 | ; | |
5260325f | 205 | if (!*cp || *cp == '\n' || *cp == '#') |
206 | continue; | |
207 | ||
aa3378df | 208 | /* |
209 | * Check if there are options for this key, and if so, | |
210 | * save their starting address and skip the option part | |
211 | * for now. If there are no options, set the starting | |
212 | * address to NULL. | |
213 | */ | |
5260325f | 214 | if (*cp < '0' || *cp > '9') { |
215 | int quoted = 0; | |
216 | options = cp; | |
217 | for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) { | |
218 | if (*cp == '\\' && cp[1] == '"') | |
219 | cp++; /* Skip both */ | |
220 | else if (*cp == '"') | |
221 | quoted = !quoted; | |
8efc0c15 | 222 | } |
5260325f | 223 | } else |
224 | options = NULL; | |
225 | ||
226 | /* Parse the key from the line. */ | |
46df736f | 227 | if (hostfile_read_key(&cp, &bits, key) == 0) { |
96a7b0cc | 228 | debug("%.100s, line %lu: non ssh1 key syntax", |
42f11eb2 | 229 | file, linenum); |
5260325f | 230 | continue; |
8efc0c15 | 231 | } |
5260325f | 232 | /* cp now points to the comment part. */ |
233 | ||
5260325f | 234 | /* Check if the we have found the desired key (identified by its modulus). */ |
46df736f | 235 | if (BN_cmp(key->rsa->n, client_n) != 0) |
aa3378df | 236 | continue; |
5260325f | 237 | |
57112b5a | 238 | /* check the real bits */ |
46df736f | 239 | if (bits != BN_num_bits(key->rsa->n)) |
1bdee08c | 240 | log("Warning: %s, line %lu: keysize mismatch: " |
57112b5a | 241 | "actual %d vs. announced %d.", |
46df736f | 242 | file, linenum, BN_num_bits(key->rsa->n), bits); |
57112b5a | 243 | |
5260325f | 244 | /* We have found the desired key. */ |
d343d900 | 245 | /* |
246 | * If our options do not allow this key to be used, | |
247 | * do not send challenge. | |
248 | */ | |
42f11eb2 | 249 | if (!auth_parse_options(pw, options, file, linenum)) |
d343d900 | 250 | continue; |
5260325f | 251 | |
46f1eece | 252 | /* break out, this key is allowed */ |
253 | allowed = 1; | |
33de75a3 | 254 | break; |
8efc0c15 | 255 | } |
256 | ||
5260325f | 257 | /* Restore the privileged uid. */ |
258 | restore_uid(); | |
8efc0c15 | 259 | |
5260325f | 260 | /* Close the file. */ |
c8445989 | 261 | xfree(file); |
5260325f | 262 | fclose(f); |
8efc0c15 | 263 | |
46f1eece | 264 | /* return key if allowed */ |
265 | if (allowed && rkey != NULL) | |
266 | *rkey = key; | |
94ec8c6b | 267 | else |
46f1eece | 268 | key_free(key); |
269 | return (allowed); | |
270 | } | |
271 | ||
272 | /* | |
273 | * Performs the RSA authentication dialog with the client. This returns | |
274 | * 0 if the client could not be authenticated, and 1 if authentication was | |
275 | * successful. This may exit if there is a serious protocol violation. | |
276 | */ | |
277 | int | |
278 | auth_rsa(struct passwd *pw, BIGNUM *client_n) | |
279 | { | |
280 | Key *key; | |
281 | char *fp; | |
282 | ||
283 | /* no user given */ | |
284 | if (pw == NULL) | |
285 | return 0; | |
286 | ||
287 | if (auth_rsa_key_allowed(pw, client_n, &key) == 0) { | |
94ec8c6b | 288 | auth_clear_options(); |
46f1eece | 289 | return (0); |
290 | } | |
291 | ||
292 | /* Perform the challenge-response dialog for this key. */ | |
293 | if (!auth_rsa_challenge_dialog(key)) { | |
294 | /* Wrong response. */ | |
295 | verbose("Wrong response to RSA authentication challenge."); | |
296 | packet_send_debug("Wrong response to RSA authentication challenge."); | |
297 | /* | |
298 | * Break out of the loop. Otherwise we might send | |
299 | * another challenge and break the protocol. | |
300 | */ | |
301 | key_free(key); | |
302 | return (0); | |
303 | } | |
304 | /* | |
305 | * Correct response. The client has been successfully | |
306 | * authenticated. Note that we have not yet processed the | |
307 | * options; this will be reset if the options cause the | |
308 | * authentication to be rejected. | |
309 | */ | |
310 | fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX); | |
311 | verbose("Found matching %s key: %s", | |
312 | key_type(key), fp); | |
313 | xfree(fp); | |
314 | key_free(key); | |
8efc0c15 | 315 | |
46f1eece | 316 | packet_send_debug("RSA authentication accepted."); |
317 | return (1); | |
8efc0c15 | 318 | } |