]> andersk Git - openssh.git/blob - auth-rsa.c
- markus@cvs.openbsd.org 2002/03/14 16:56:33
[openssh.git] / auth-rsa.c
1 /*
2  * Author: Tatu Ylonen <ylo@cs.hut.fi>
3  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4  *                    All rights reserved
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.
8  *
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".
14  */
15
16 #include "includes.h"
17 RCSID("$OpenBSD: auth-rsa.c,v 1.51 2002/03/14 16:56:33 markus Exp $");
18
19 #include <openssl/rsa.h>
20 #include <openssl/md5.h>
21
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"
34 #include "hostfile.h"
35
36 /* import */
37 extern ServerOptions options;
38
39 /*
40  * Session identifier that is used to bind key exchange and authentication
41  * responses to a particular session.
42  */
43 extern u_char session_id[16];
44
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  */
54
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
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  */
105
106 int
107 auth_rsa_challenge_dialog(Key *key)
108 {
109         BIGNUM *challenge, *encrypted_challenge;
110         u_char response[16];
111         int i, success;
112
113         if ((encrypted_challenge = BN_new()) == NULL)
114                 fatal("auth_rsa_challenge_dialog: BN_new() failed");
115
116         challenge = auth_rsa_generate_challenge(key);
117
118         /* Encrypt the challenge with the public key. */
119         rsa_public_encrypt(encrypted_challenge, challenge, key->rsa);
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();
125         BN_clear_free(encrypted_challenge);
126         packet_write_wait();
127
128         /* Wait for a response. */
129         packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE);
130         for (i = 0; i < 16; i++)
131                 response[i] = packet_get_char();
132         packet_check_eom();
133
134         success = auth_rsa_verify_response(key, challenge, response);
135         BN_clear_free(challenge);
136         return (success);
137 }
138
139 /*
140  * check if there's user key matching client_n,
141  * return key if login is allowed, NULL otherwise
142  */
143
144 static int
145 auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
146 {
147         char line[8192], *file;
148         int allowed;
149         u_int bits;
150         FILE *f;
151         u_long linenum = 0;
152         struct stat st;
153         Key *key;
154
155         /* Temporarily use the user's uid. */
156         temporarily_use_uid(pw);
157
158         /* The authorized keys. */
159         file = authorized_keys_file(pw);
160         debug("trying public RSA key file %s", file);
161
162         /* Fail quietly if file does not exist */
163         if (stat(file, &st) < 0) {
164                 /* Restore the privileged uid. */
165                 restore_uid();
166                 xfree(file);
167                 return (NULL);
168         }
169         /* Open the file containing the authorized keys. */
170         f = fopen(file, "r");
171         if (!f) {
172                 /* Restore the privileged uid. */
173                 restore_uid();
174                 xfree(file);
175                 return (NULL);
176         }
177         if (options.strict_modes &&
178             secure_filename(f, file, pw, line, sizeof(line)) != 0) {
179                 xfree(file);
180                 fclose(f);
181                 log("Authentication refused: %s", line);
182                 restore_uid();
183                 return (NULL);
184         }
185
186         /* Flag indicating whether the key is allowed. */
187         allowed = 0;
188
189         key = key_new(KEY_RSA1);
190
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          */
196         while (fgets(line, sizeof(line), f)) {
197                 char *cp;
198                 char *options;
199
200                 linenum++;
201
202                 /* Skip leading whitespace, empty and comment lines. */
203                 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
204                         ;
205                 if (!*cp || *cp == '\n' || *cp == '#')
206                         continue;
207
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                  */
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;
222                         }
223                 } else
224                         options = NULL;
225
226                 /* Parse the key from the line. */
227                 if (hostfile_read_key(&cp, &bits, key) == 0) {
228                         debug("%.100s, line %lu: non ssh1 key syntax",
229                             file, linenum);
230                         continue;
231                 }
232                 /* cp now points to the comment part. */
233
234                 /* Check if the we have found the desired key (identified by its modulus). */
235                 if (BN_cmp(key->rsa->n, client_n) != 0)
236                         continue;
237
238                 /* check the real bits  */
239                 if (bits != BN_num_bits(key->rsa->n))
240                         log("Warning: %s, line %lu: keysize mismatch: "
241                             "actual %d vs. announced %d.",
242                             file, linenum, BN_num_bits(key->rsa->n), bits);
243
244                 /* We have found the desired key. */
245                 /*
246                  * If our options do not allow this key to be used,
247                  * do not send challenge.
248                  */
249                 if (!auth_parse_options(pw, options, file, linenum))
250                         continue;
251
252                 /* break out, this key is allowed */
253                 allowed = 1;
254                 break;
255         }
256
257         /* Restore the privileged uid. */
258         restore_uid();
259
260         /* Close the file. */
261         xfree(file);
262         fclose(f);
263
264         /* return key if allowed */
265         if (allowed && rkey != NULL)
266                 *rkey = key;
267         else
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) {
288                 auth_clear_options();
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);
315
316         packet_send_debug("RSA authentication accepted.");
317         return (1);
318 }
This page took 0.132609 seconds and 5 git commands to generate.