blob: ef7a2274ebf0a451f19a93719c38966ad8b4fcdc [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
3 * auth-rsa.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Mon Mar 27 01:46:52 1995 ylo
11 *
12 * RSA-based authentication. This code determines whether to admit a login
13 * based on RSA authentication. This file also contains functions to check
14 * validity of the host key.
15 *
16 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017
18#include "includes.h"
Damien Miller98c7ad62000-03-09 21:27:49 +110019RCSID("$Id: auth-rsa.c,v 1.13 2000/03/09 10:27:50 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100020
21#include "rsa.h"
22#include "packet.h"
23#include "xmalloc.h"
24#include "ssh.h"
25#include "mpaux.h"
26#include "uidswap.h"
Damien Miller6d7b2cd1999-11-12 15:19:27 +110027#include "servconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028
Damien Miller7f6ea021999-10-28 13:25:17 +100029#ifdef HAVE_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +100030#include <openssl/rsa.h>
31#include <openssl/md5.h>
Damien Miller7f6ea021999-10-28 13:25:17 +100032#endif
33#ifdef HAVE_SSL
34#include <ssl/rsa.h>
35#include <ssl/md5.h>
36#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38/* Flags that may be set in authorized_keys options. */
39extern int no_port_forwarding_flag;
40extern int no_agent_forwarding_flag;
41extern int no_x11_forwarding_flag;
42extern int no_pty_flag;
43extern char *forced_command;
44extern struct envstring *custom_environment;
45
Damien Miller5428f641999-11-25 11:54:57 +110046/*
47 * Session identifier that is used to bind key exchange and authentication
48 * responses to a particular session.
49 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100050extern unsigned char session_id[16];
51
Damien Miller5428f641999-11-25 11:54:57 +110052/*
53 * The .ssh/authorized_keys file contains public keys, one per line, in the
54 * following format:
55 * options bits e n comment
56 * where bits, e and n are decimal numbers,
57 * and comment is any string of characters up to newline. The maximum
58 * length of a line is 8000 characters. See the documentation for a
59 * description of the options.
60 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100061
Damien Miller5428f641999-11-25 11:54:57 +110062/*
63 * Performs the RSA authentication challenge-response dialog with the client,
64 * and returns true (non-zero) if the client gave the correct answer to
65 * our challenge; returns zero if the client gives a wrong answer.
66 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067
68int
Damien Miller7e8e8201999-11-16 13:37:16 +110069auth_rsa_challenge_dialog(BIGNUM *e, BIGNUM *n)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070{
Damien Miller98c7ad62000-03-09 21:27:49 +110071 BIGNUM *challenge, *encrypted_challenge;
Damien Miller95def091999-11-25 00:26:21 +110072 RSA *pk;
Damien Miller98c7ad62000-03-09 21:27:49 +110073 BN_CTX *ctx;
Damien Miller95def091999-11-25 00:26:21 +110074 unsigned char buf[32], mdbuf[16], response[16];
75 MD5_CTX md;
76 unsigned int i;
77 int plen, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Miller95def091999-11-25 00:26:21 +110079 encrypted_challenge = BN_new();
80 challenge = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller95def091999-11-25 00:26:21 +110082 /* Generate a random challenge. */
83 BN_rand(challenge, 256, 0, 0);
Damien Miller98c7ad62000-03-09 21:27:49 +110084 ctx = BN_CTX_new();
Damien Miller95def091999-11-25 00:26:21 +110085 BN_mod(challenge, challenge, n, ctx);
Damien Miller98c7ad62000-03-09 21:27:49 +110086 BN_CTX_free(ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100087
Damien Miller95def091999-11-25 00:26:21 +110088 /* Create the public key data structure. */
89 pk = RSA_new();
90 pk->e = BN_new();
91 BN_copy(pk->e, e);
92 pk->n = BN_new();
93 BN_copy(pk->n, n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Damien Miller95def091999-11-25 00:26:21 +110095 /* Encrypt the challenge with the public key. */
96 rsa_public_encrypt(encrypted_challenge, challenge, pk);
97 RSA_free(pk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100098
Damien Miller95def091999-11-25 00:26:21 +110099 /* Send the encrypted challenge to the client. */
100 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
101 packet_put_bignum(encrypted_challenge);
102 packet_send();
Damien Miller98c7ad62000-03-09 21:27:49 +1100103 BN_clear_free(encrypted_challenge);
Damien Miller95def091999-11-25 00:26:21 +1100104 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
Damien Miller98c7ad62000-03-09 21:27:49 +1100106 /* Wait for a response. */
107 packet_read_expect(&plen, SSH_CMSG_AUTH_RSA_RESPONSE);
108 packet_integrity_check(plen, 16, SSH_CMSG_AUTH_RSA_RESPONSE);
109 for (i = 0; i < 16; i++)
110 response[i] = packet_get_char();
111
Damien Miller95def091999-11-25 00:26:21 +1100112 /* The response is MD5 of decrypted challenge plus session id. */
113 len = BN_num_bytes(challenge);
114 if (len <= 0 || len > 32)
115 fatal("auth_rsa_challenge_dialog: bad challenge length %d", len);
116 memset(buf, 0, 32);
117 BN_bn2bin(challenge, buf + 32 - len);
118 MD5_Init(&md);
119 MD5_Update(&md, buf, 32);
120 MD5_Update(&md, session_id, 16);
121 MD5_Final(mdbuf, &md);
Damien Miller95def091999-11-25 00:26:21 +1100122 BN_clear_free(challenge);
Damien Miller95def091999-11-25 00:26:21 +1100123
124 /* Verify that the response is the original challenge. */
125 if (memcmp(response, mdbuf, 16) != 0) {
126 /* Wrong answer. */
127 return 0;
128 }
129 /* Correct answer. */
130 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131}
132
Damien Miller5428f641999-11-25 11:54:57 +1100133/*
134 * Performs the RSA authentication dialog with the client. This returns
135 * 0 if the client could not be authenticated, and 1 if authentication was
136 * successful. This may exit if there is a serious protocol violation.
137 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
139int
Damien Miller6d7b2cd1999-11-12 15:19:27 +1100140auth_rsa(struct passwd *pw, BIGNUM *client_n)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141{
Damien Miller95def091999-11-25 00:26:21 +1100142 extern ServerOptions options;
143 char line[8192], file[1024];
144 int authenticated;
145 unsigned int bits;
146 FILE *f;
147 unsigned long linenum = 0;
148 struct stat st;
149 BIGNUM *e, *n;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller95def091999-11-25 00:26:21 +1100151 /* Temporarily use the user's uid. */
152 temporarily_use_uid(pw->pw_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153
Damien Miller95def091999-11-25 00:26:21 +1100154 /* The authorized keys. */
155 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
156 SSH_USER_PERMITTED_KEYS);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157
Damien Miller95def091999-11-25 00:26:21 +1100158 /* Fail quietly if file does not exist */
159 if (stat(file, &st) < 0) {
160 /* Restore the privileged uid. */
161 restore_uid();
162 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000163 }
Damien Miller95def091999-11-25 00:26:21 +1100164 /* Open the file containing the authorized keys. */
165 f = fopen(file, "r");
166 if (!f) {
167 /* Restore the privileged uid. */
168 restore_uid();
169 packet_send_debug("Could not open %.900s for reading.", file);
170 packet_send_debug("If your home is on an NFS volume, it may need to be world-readable.");
171 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172 }
Damien Miller95def091999-11-25 00:26:21 +1100173 if (options.strict_modes) {
174 int fail = 0;
175 char buf[1024];
176 /* Check open file in order to avoid open/stat races */
177 if (fstat(fileno(f), &st) < 0 ||
178 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
179 (st.st_mode & 022) != 0) {
180 snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: "
181 "bad ownership or modes for '%s'.", pw->pw_name, file);
182 fail = 1;
183 } else {
184 /* Check path to SSH_USER_PERMITTED_KEYS */
185 int i;
186 static const char *check[] = {
187 "", SSH_USER_DIR, NULL
188 };
189 for (i = 0; check[i]; i++) {
190 snprintf(line, sizeof line, "%.500s/%.100s", pw->pw_dir, check[i]);
191 if (stat(line, &st) < 0 ||
192 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
193 (st.st_mode & 022) != 0) {
194 snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: "
195 "bad ownership or modes for '%s'.", pw->pw_name, line);
196 fail = 1;
197 break;
198 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000199 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000200 }
Damien Miller95def091999-11-25 00:26:21 +1100201 if (fail) {
202 log(buf);
203 packet_send_debug(buf);
204 restore_uid();
205 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000206 }
Damien Miller95def091999-11-25 00:26:21 +1100207 }
208 /* Flag indicating whether authentication has succeeded. */
209 authenticated = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000210
Damien Miller95def091999-11-25 00:26:21 +1100211 e = BN_new();
212 n = BN_new();
213
Damien Miller5428f641999-11-25 11:54:57 +1100214 /*
215 * Go though the accepted keys, looking for the current key. If
216 * found, perform a challenge-response dialog to verify that the
217 * user really has the corresponding private key.
218 */
Damien Miller95def091999-11-25 00:26:21 +1100219 while (fgets(line, sizeof(line), f)) {
220 char *cp;
221 char *options;
222
223 linenum++;
224
Damien Miller5428f641999-11-25 11:54:57 +1100225 /* Skip leading whitespace, empty and comment lines. */
226 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
227 ;
Damien Miller95def091999-11-25 00:26:21 +1100228 if (!*cp || *cp == '\n' || *cp == '#')
229 continue;
230
Damien Miller5428f641999-11-25 11:54:57 +1100231 /*
232 * Check if there are options for this key, and if so,
233 * save their starting address and skip the option part
234 * for now. If there are no options, set the starting
235 * address to NULL.
236 */
Damien Miller95def091999-11-25 00:26:21 +1100237 if (*cp < '0' || *cp > '9') {
238 int quoted = 0;
239 options = cp;
240 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
241 if (*cp == '\\' && cp[1] == '"')
242 cp++; /* Skip both */
243 else if (*cp == '"')
244 quoted = !quoted;
245 }
246 } else
247 options = NULL;
248
249 /* Parse the key from the line. */
250 if (!auth_rsa_read_key(&cp, &bits, e, n)) {
251 debug("%.100s, line %lu: bad key syntax",
252 SSH_USER_PERMITTED_KEYS, linenum);
253 packet_send_debug("%.100s, line %lu: bad key syntax",
254 SSH_USER_PERMITTED_KEYS, linenum);
255 continue;
256 }
257 /* cp now points to the comment part. */
258
Damien Miller95def091999-11-25 00:26:21 +1100259 /* Check if the we have found the desired key (identified by its modulus). */
260 if (BN_cmp(n, client_n) != 0)
Damien Miller5428f641999-11-25 11:54:57 +1100261 continue;
Damien Miller95def091999-11-25 00:26:21 +1100262
Damien Milleraae6c611999-12-06 11:47:28 +1100263 /* check the real bits */
264 if (bits != BN_num_bits(n))
265 log("Warning: %s, line %ld: keysize mismatch: "
266 "actual %d vs. announced %d.",
267 file, linenum, BN_num_bits(n), bits);
268
Damien Miller95def091999-11-25 00:26:21 +1100269 /* We have found the desired key. */
270
271 /* Perform the challenge-response dialog for this key. */
272 if (!auth_rsa_challenge_dialog(e, n)) {
273 /* Wrong response. */
274 verbose("Wrong response to RSA authentication challenge.");
275 packet_send_debug("Wrong response to RSA authentication challenge.");
276 continue;
277 }
Damien Miller5428f641999-11-25 11:54:57 +1100278 /*
279 * Correct response. The client has been successfully
280 * authenticated. Note that we have not yet processed the
281 * options; this will be reset if the options cause the
282 * authentication to be rejected.
283 */
Damien Miller95def091999-11-25 00:26:21 +1100284 authenticated = 1;
285
286 /* RSA part of authentication was accepted. Now process the options. */
287 if (options) {
288 while (*options && *options != ' ' && *options != '\t') {
289 cp = "no-port-forwarding";
290 if (strncmp(options, cp, strlen(cp)) == 0) {
291 packet_send_debug("Port forwarding disabled.");
292 no_port_forwarding_flag = 1;
293 options += strlen(cp);
294 goto next_option;
295 }
296 cp = "no-agent-forwarding";
297 if (strncmp(options, cp, strlen(cp)) == 0) {
298 packet_send_debug("Agent forwarding disabled.");
299 no_agent_forwarding_flag = 1;
300 options += strlen(cp);
301 goto next_option;
302 }
303 cp = "no-X11-forwarding";
304 if (strncmp(options, cp, strlen(cp)) == 0) {
305 packet_send_debug("X11 forwarding disabled.");
306 no_x11_forwarding_flag = 1;
307 options += strlen(cp);
308 goto next_option;
309 }
310 cp = "no-pty";
311 if (strncmp(options, cp, strlen(cp)) == 0) {
312 packet_send_debug("Pty allocation disabled.");
313 no_pty_flag = 1;
314 options += strlen(cp);
315 goto next_option;
316 }
317 cp = "command=\"";
318 if (strncmp(options, cp, strlen(cp)) == 0) {
319 int i;
320 options += strlen(cp);
321 forced_command = xmalloc(strlen(options) + 1);
322 i = 0;
323 while (*options) {
324 if (*options == '"')
325 break;
326 if (*options == '\\' && options[1] == '"') {
327 options += 2;
328 forced_command[i++] = '"';
329 continue;
330 }
331 forced_command[i++] = *options++;
332 }
333 if (!*options) {
334 debug("%.100s, line %lu: missing end quote",
335 SSH_USER_PERMITTED_KEYS, linenum);
336 packet_send_debug("%.100s, line %lu: missing end quote",
337 SSH_USER_PERMITTED_KEYS, linenum);
338 continue;
339 }
340 forced_command[i] = 0;
341 packet_send_debug("Forced command: %.900s", forced_command);
342 options++;
343 goto next_option;
344 }
345 cp = "environment=\"";
346 if (strncmp(options, cp, strlen(cp)) == 0) {
347 int i;
348 char *s;
349 struct envstring *new_envstring;
350 options += strlen(cp);
351 s = xmalloc(strlen(options) + 1);
352 i = 0;
353 while (*options) {
354 if (*options == '"')
355 break;
356 if (*options == '\\' && options[1] == '"') {
357 options += 2;
358 s[i++] = '"';
359 continue;
360 }
361 s[i++] = *options++;
362 }
363 if (!*options) {
364 debug("%.100s, line %lu: missing end quote",
365 SSH_USER_PERMITTED_KEYS, linenum);
366 packet_send_debug("%.100s, line %lu: missing end quote",
367 SSH_USER_PERMITTED_KEYS, linenum);
368 continue;
369 }
370 s[i] = 0;
371 packet_send_debug("Adding to environment: %.900s", s);
372 debug("Adding to environment: %.900s", s);
373 options++;
374 new_envstring = xmalloc(sizeof(struct envstring));
375 new_envstring->s = s;
376 new_envstring->next = custom_environment;
377 custom_environment = new_envstring;
378 goto next_option;
379 }
380 cp = "from=\"";
381 if (strncmp(options, cp, strlen(cp)) == 0) {
382 char *patterns = xmalloc(strlen(options) + 1);
383 int i;
384 options += strlen(cp);
385 i = 0;
386 while (*options) {
387 if (*options == '"')
388 break;
389 if (*options == '\\' && options[1] == '"') {
390 options += 2;
391 patterns[i++] = '"';
392 continue;
393 }
394 patterns[i++] = *options++;
395 }
396 if (!*options) {
397 debug("%.100s, line %lu: missing end quote",
398 SSH_USER_PERMITTED_KEYS, linenum);
399 packet_send_debug("%.100s, line %lu: missing end quote",
400 SSH_USER_PERMITTED_KEYS, linenum);
401 continue;
402 }
403 patterns[i] = 0;
404 options++;
405 if (!match_hostname(get_canonical_hostname(), patterns,
406 strlen(patterns)) &&
407 !match_hostname(get_remote_ipaddr(), patterns,
408 strlen(patterns))) {
409 log("RSA authentication tried for %.100s with correct key but not from a permitted host (host=%.200s, ip=%.200s).",
410 pw->pw_name, get_canonical_hostname(),
411 get_remote_ipaddr());
412 packet_send_debug("Your host '%.200s' is not permitted to use this key for login.",
413 get_canonical_hostname());
414 xfree(patterns);
Damien Miller396691a2000-01-20 22:44:08 +1100415 /* key invalid for this host, reset flags */
Damien Miller95def091999-11-25 00:26:21 +1100416 authenticated = 0;
Damien Miller396691a2000-01-20 22:44:08 +1100417 no_agent_forwarding_flag = 0;
418 no_port_forwarding_flag = 0;
419 no_pty_flag = 0;
420 no_x11_forwarding_flag = 0;
421 while (custom_environment) {
422 struct envstring *ce = custom_environment;
423 custom_environment = ce->next;
424 xfree(ce->s);
425 xfree(ce);
426 }
427 if (forced_command) {
428 xfree(forced_command);
429 forced_command = NULL;
430 }
Damien Miller95def091999-11-25 00:26:21 +1100431 break;
432 }
433 xfree(patterns);
434 /* Host name matches. */
435 goto next_option;
436 }
437 bad_option:
Damien Miller95def091999-11-25 00:26:21 +1100438 log("Bad options in %.100s file, line %lu: %.50s",
439 SSH_USER_PERMITTED_KEYS, linenum, options);
440 packet_send_debug("Bad options in %.100s file, line %lu: %.50s",
441 SSH_USER_PERMITTED_KEYS, linenum, options);
442 authenticated = 0;
443 break;
444
445 next_option:
Damien Miller5428f641999-11-25 11:54:57 +1100446 /*
447 * Skip the comma, and move to the next option
448 * (or break out if there are no more).
449 */
Damien Miller95def091999-11-25 00:26:21 +1100450 if (!*options)
451 fatal("Bugs in auth-rsa.c option processing.");
452 if (*options == ' ' || *options == '\t')
Damien Miller5428f641999-11-25 11:54:57 +1100453 break; /* End of options. */
Damien Miller95def091999-11-25 00:26:21 +1100454 if (*options != ',')
455 goto bad_option;
456 options++;
457 /* Process the next option. */
458 continue;
459 }
460 }
Damien Miller5428f641999-11-25 11:54:57 +1100461 /*
462 * Break out of the loop if authentication was successful;
463 * otherwise continue searching.
464 */
Damien Miller95def091999-11-25 00:26:21 +1100465 if (authenticated)
466 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467 }
468
Damien Miller95def091999-11-25 00:26:21 +1100469 /* Restore the privileged uid. */
470 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471
Damien Miller95def091999-11-25 00:26:21 +1100472 /* Close the file. */
473 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474
Damien Miller95def091999-11-25 00:26:21 +1100475 BN_clear_free(n);
476 BN_clear_free(e);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000477
Damien Miller95def091999-11-25 00:26:21 +1100478 if (authenticated)
479 packet_send_debug("RSA authentication accepted.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480
Damien Miller95def091999-11-25 00:26:21 +1100481 /* Return authentication result. */
482 return authenticated;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000483}