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