]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - crypto/openssh/auth-rsa.c
This commit was generated by cvs2svn to compensate for changes in r79847,
[FreeBSD/FreeBSD.git] / crypto / openssh / 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.40 2001/04/06 21:00:07 markus Exp $");
18 RCSID("$FreeBSD$");
19
20 #include <openssl/rsa.h>
21 #include <openssl/md5.h>
22
23 #include "rsa.h"
24 #include "packet.h"
25 #include "xmalloc.h"
26 #include "ssh1.h"
27 #include "mpaux.h"
28 #include "uidswap.h"
29 #include "match.h"
30 #include "auth-options.h"
31 #include "pathnames.h"
32 #include "log.h"
33 #include "servconf.h"
34 #include "auth.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[MAXPATHLEN];
127         int authenticated;
128         u_int bits;
129         FILE *f;
130         u_long linenum = 0;
131         struct stat st;
132         RSA *pk;
133
134         /* no user given */
135         if (pw == NULL)
136                 return 0;
137
138         /* Temporarily use the user's uid. */
139         temporarily_use_uid(pw);
140
141         /* The authorized keys. */
142         snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
143                  _PATH_SSH_USER_PERMITTED_KEYS);
144
145         /* Fail quietly if file does not exist */
146         if (stat(file, &st) < 0) {
147                 /* Restore the privileged uid. */
148                 restore_uid();
149                 return 0;
150         }
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.");
158                 return 0;
159         }
160         if (options.strict_modes) {
161                 int fail = 0;
162                 char buf[1024];
163                 /* Check open file in order to avoid open/stat races */
164                 if (fstat(fileno(f), &st) < 0 ||
165                     (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
166                     (st.st_mode & 022) != 0) {
167                         snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: "
168                                  "bad ownership or modes for '%s'.", pw->pw_name, file);
169                         fail = 1;
170                 } else {
171                         /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
172                         int i;
173                         static const char *check[] = {
174                                 "", _PATH_SSH_USER_DIR, NULL
175                         };
176                         for (i = 0; check[i]; i++) {
177                                 snprintf(line, sizeof line, "%.500s/%.100s", pw->pw_dir, check[i]);
178                                 if (stat(line, &st) < 0 ||
179                                     (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
180                                     (st.st_mode & 022) != 0) {
181                                         snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: "
182                                                  "bad ownership or modes for '%s'.", pw->pw_name, line);
183                                         fail = 1;
184                                         break;
185                                 }
186                         }
187                 }
188                 if (fail) {
189                         fclose(f);
190                         log("%s", buf);
191                         packet_send_debug("%s", buf);
192                         restore_uid();
193                         return 0;
194                 }
195         }
196         /* Flag indicating whether authentication has succeeded. */
197         authenticated = 0;
198
199         pk = RSA_new();
200         pk->e = BN_new();
201         pk->n = BN_new();
202
203         /*
204          * Go though the accepted keys, looking for the current key.  If
205          * found, perform a challenge-response dialog to verify that the
206          * user really has the corresponding private key.
207          */
208         while (fgets(line, sizeof(line), f)) {
209                 char *cp;
210                 char *options;
211
212                 linenum++;
213
214                 /* Skip leading whitespace, empty and comment lines. */
215                 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
216                         ;
217                 if (!*cp || *cp == '\n' || *cp == '#')
218                         continue;
219
220                 /*
221                  * Check if there are options for this key, and if so,
222                  * save their starting address and skip the option part
223                  * for now.  If there are no options, set the starting
224                  * address to NULL.
225                  */
226                 if (*cp < '0' || *cp > '9') {
227                         int quoted = 0;
228                         options = cp;
229                         for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
230                                 if (*cp == '\\' && cp[1] == '"')
231                                         cp++;   /* Skip both */
232                                 else if (*cp == '"')
233                                         quoted = !quoted;
234                         }
235                 } else
236                         options = NULL;
237
238                 /* Parse the key from the line. */
239                 if (!auth_rsa_read_key(&cp, &bits, pk->e, pk->n)) {
240                         debug("%.100s, line %lu: bad key syntax",
241                             file, linenum);
242                         packet_send_debug("%.100s, line %lu: bad key syntax",
243                             file, linenum);
244                         continue;
245                 }
246                 /* cp now points to the comment part. */
247
248                 /* Check if the we have found the desired key (identified by its modulus). */
249                 if (BN_cmp(pk->n, client_n) != 0)
250                         continue;
251
252                 /* check the real bits  */
253                 if (bits != BN_num_bits(pk->n))
254                         log("Warning: %s, line %ld: keysize mismatch: "
255                             "actual %d vs. announced %d.",
256                             file, linenum, BN_num_bits(pk->n), bits);
257
258                 /* We have found the desired key. */
259                 /*
260                  * If our options do not allow this key to be used,
261                  * do not send challenge.
262                  */
263                 if (!auth_parse_options(pw, options, file, linenum))
264                         continue;
265
266                 /* Perform the challenge-response dialog for this key. */
267                 if (!auth_rsa_challenge_dialog(pk)) {
268                         /* Wrong response. */
269                         verbose("Wrong response to RSA authentication challenge.");
270                         packet_send_debug("Wrong response to RSA authentication challenge.");
271                         continue;
272                 }
273                 /*
274                  * Correct response.  The client has been successfully
275                  * authenticated. Note that we have not yet processed the
276                  * options; this will be reset if the options cause the
277                  * authentication to be rejected.
278                  * Break out of the loop if authentication was successful;
279                  * otherwise continue searching.
280                  */
281                 authenticated = 1;
282                 break;
283         }
284
285         /* Restore the privileged uid. */
286         restore_uid();
287
288         /* Close the file. */
289         fclose(f);
290
291         RSA_free(pk);
292
293         if (authenticated)
294                 packet_send_debug("RSA authentication accepted.");
295         else
296                 auth_clear_options();
297
298         /* Return authentication result. */
299         return authenticated;
300 }