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