]> andersk Git - openssh.git/blob - auth-rsa.c
- jakob@cvs.openbsd.org 2001/12/18 10:06:24
[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.46 2001/12/18 10:06:24 jakob 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 /*
56  * Performs the RSA authentication challenge-response dialog with the client,
57  * and returns true (non-zero) if the client gave the correct answer to
58  * our challenge; returns zero if the client gives a wrong answer.
59  */
60
61 int
62 auth_rsa_challenge_dialog(RSA *pk)
63 {
64         BIGNUM *challenge, *encrypted_challenge;
65         BN_CTX *ctx;
66         u_char buf[32], mdbuf[16], response[16];
67         MD5_CTX md;
68         u_int i;
69         int plen, len;
70
71         encrypted_challenge = BN_new();
72         challenge = BN_new();
73
74         /* Generate a random challenge. */
75         BN_rand(challenge, 256, 0, 0);
76         ctx = BN_CTX_new();
77         BN_mod(challenge, challenge, pk->n, ctx);
78         BN_CTX_free(ctx);
79
80         /* Encrypt the challenge with the public key. */
81         rsa_public_encrypt(encrypted_challenge, challenge, pk);
82
83         /* Send the encrypted challenge to the client. */
84         packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
85         packet_put_bignum(encrypted_challenge);
86         packet_send();
87         BN_clear_free(encrypted_challenge);
88         packet_write_wait();
89
90         /* Wait for a response. */
91         packet_read_expect(&plen, SSH_CMSG_AUTH_RSA_RESPONSE);
92         packet_integrity_check(plen, 16, SSH_CMSG_AUTH_RSA_RESPONSE);
93         for (i = 0; i < 16; i++)
94                 response[i] = packet_get_char();
95
96         /* The response is MD5 of decrypted challenge plus session id. */
97         len = BN_num_bytes(challenge);
98         if (len <= 0 || len > 32)
99                 fatal("auth_rsa_challenge_dialog: bad challenge length %d", len);
100         memset(buf, 0, 32);
101         BN_bn2bin(challenge, buf + 32 - len);
102         MD5_Init(&md);
103         MD5_Update(&md, buf, 32);
104         MD5_Update(&md, session_id, 16);
105         MD5_Final(mdbuf, &md);
106         BN_clear_free(challenge);
107
108         /* Verify that the response is the original challenge. */
109         if (memcmp(response, mdbuf, 16) != 0) {
110                 /* Wrong answer. */
111                 return 0;
112         }
113         /* Correct answer. */
114         return 1;
115 }
116
117 /*
118  * Performs the RSA authentication dialog with the client.  This returns
119  * 0 if the client could not be authenticated, and 1 if authentication was
120  * successful.  This may exit if there is a serious protocol violation.
121  */
122
123 int
124 auth_rsa(struct passwd *pw, BIGNUM *client_n)
125 {
126         char line[8192], *file;
127         int authenticated;
128         u_int bits;
129         FILE *f;
130         u_long linenum = 0;
131         struct stat st;
132         Key *key;
133         char *fp;
134
135         /* no user given */
136         if (pw == NULL)
137                 return 0;
138
139         /* Temporarily use the user's uid. */
140         temporarily_use_uid(pw);
141
142         /* The authorized keys. */
143         file = authorized_keys_file(pw);
144         debug("trying public RSA key file %s", file);
145
146         /* Fail quietly if file does not exist */
147         if (stat(file, &st) < 0) {
148                 /* Restore the privileged uid. */
149                 restore_uid();
150                 xfree(file);
151                 return 0;
152         }
153         /* Open the file containing the authorized keys. */
154         f = fopen(file, "r");
155         if (!f) {
156                 /* Restore the privileged uid. */
157                 restore_uid();
158                 packet_send_debug("Could not open %.900s for reading.", file);
159                 packet_send_debug("If your home is on an NFS volume, it may need to be world-readable.");
160                 xfree(file);
161                 return 0;
162         }
163         if (options.strict_modes &&
164             secure_filename(f, file, pw, line, sizeof(line)) != 0) {
165                 xfree(file);
166                 fclose(f);
167                 log("Authentication refused: %s", line);
168                 packet_send_debug("Authentication refused: %s", line);
169                 restore_uid();
170                 return 0;
171         }
172         /* Flag indicating whether authentication has succeeded. */
173         authenticated = 0;
174
175         key = key_new(KEY_RSA1);
176
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          */
182         while (fgets(line, sizeof(line), f)) {
183                 char *cp;
184                 char *options;
185
186                 linenum++;
187
188                 /* Skip leading whitespace, empty and comment lines. */
189                 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
190                         ;
191                 if (!*cp || *cp == '\n' || *cp == '#')
192                         continue;
193
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                  */
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. */
213                 if (hostfile_read_key(&cp, &bits, key) == 0) {
214                         debug("%.100s, line %lu: non ssh1 key syntax",
215                             file, linenum);
216                         continue;
217                 }
218                 /* cp now points to the comment part. */
219
220                 /* Check if the we have found the desired key (identified by its modulus). */
221                 if (BN_cmp(key->rsa->n, client_n) != 0)
222                         continue;
223
224                 /* check the real bits  */
225                 if (bits != BN_num_bits(key->rsa->n))
226                         log("Warning: %s, line %lu: keysize mismatch: "
227                             "actual %d vs. announced %d.",
228                             file, linenum, BN_num_bits(key->rsa->n), bits);
229
230                 /* We have found the desired key. */
231                 /*
232                  * If our options do not allow this key to be used,
233                  * do not send challenge.
234                  */
235                 if (!auth_parse_options(pw, options, file, linenum))
236                         continue;
237
238                 /* Perform the challenge-response dialog for this key. */
239                 if (!auth_rsa_challenge_dialog(key->rsa)) {
240                         /* Wrong response. */
241                         verbose("Wrong response to RSA authentication challenge.");
242                         packet_send_debug("Wrong response to RSA authentication challenge.");
243                         /*
244                          * Break out of the loop. Otherwise we might send
245                          * another challenge and break the protocol.
246                          */
247                         break;
248                 }
249                 /*
250                  * Correct response.  The client has been successfully
251                  * authenticated. Note that we have not yet processed the
252                  * options; this will be reset if the options cause the
253                  * authentication to be rejected.
254                  * Break out of the loop if authentication was successful;
255                  * otherwise continue searching.
256                  */
257                 authenticated = 1;
258
259                 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
260                 verbose("Found matching %s key: %s",
261                     key_type(key), fp);
262                 xfree(fp);
263
264                 break;
265         }
266
267         /* Restore the privileged uid. */
268         restore_uid();
269
270         /* Close the file. */
271         xfree(file);
272         fclose(f);
273
274         key_free(key);
275
276         if (authenticated)
277                 packet_send_debug("RSA authentication accepted.");
278         else
279                 auth_clear_options();
280
281         /* Return authentication result. */
282         return authenticated;
283 }
This page took 0.332403 seconds and 5 git commands to generate.