blob: aa8be2bb061cc7e4550056096b2ee95df13ff013 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * auth-rsa.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Mon Mar 27 01:46:52 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * 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.
Damien Miller4af51302000-04-16 11:18:38 +100015 *
Damien Miller95def091999-11-25 00:26:21 +110016 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100017
18#include "includes.h"
Damien Miller5f056372000-04-16 12:31:48 +100019RCSID("$Id: auth-rsa.c,v 1.17 2000/04/16 02:31:49 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 Miller450a7a12000-03-26 13:04:51 +100027#include "match.h"
Damien Miller6d7b2cd1999-11-12 15:19:27 +110028#include "servconf.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100029
30#include <openssl/rsa.h>
31#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100032
33/* Flags that may be set in authorized_keys options. */
34extern int no_port_forwarding_flag;
35extern int no_agent_forwarding_flag;
36extern int no_x11_forwarding_flag;
37extern int no_pty_flag;
38extern char *forced_command;
39extern struct envstring *custom_environment;
40
Damien Miller5428f641999-11-25 11:54:57 +110041/*
42 * Session identifier that is used to bind key exchange and authentication
43 * responses to a particular session.
44 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100045extern unsigned char session_id[16];
46
Damien Miller5428f641999-11-25 11:54:57 +110047/*
48 * The .ssh/authorized_keys file contains public keys, one per line, in the
49 * following format:
50 * options bits e n comment
51 * where bits, e and n are decimal numbers,
52 * and comment is any string of characters up to newline. The maximum
53 * length of a line is 8000 characters. See the documentation for a
54 * description of the options.
55 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100056
Damien Miller5428f641999-11-25 11:54:57 +110057/*
58 * Performs the RSA authentication challenge-response dialog with the client,
59 * and returns true (non-zero) if the client gave the correct answer to
60 * our challenge; returns zero if the client gives a wrong answer.
61 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062
63int
Damien Miller450a7a12000-03-26 13:04:51 +100064auth_rsa_challenge_dialog(RSA *pk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100065{
Damien Miller98c7ad62000-03-09 21:27:49 +110066 BIGNUM *challenge, *encrypted_challenge;
Damien Miller98c7ad62000-03-09 21:27:49 +110067 BN_CTX *ctx;
Damien Miller95def091999-11-25 00:26:21 +110068 unsigned char buf[32], mdbuf[16], response[16];
69 MD5_CTX md;
70 unsigned int i;
71 int plen, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
Damien Miller95def091999-11-25 00:26:21 +110073 encrypted_challenge = BN_new();
74 challenge = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Miller95def091999-11-25 00:26:21 +110076 /* Generate a random challenge. */
77 BN_rand(challenge, 256, 0, 0);
Damien Miller98c7ad62000-03-09 21:27:49 +110078 ctx = BN_CTX_new();
Damien Miller450a7a12000-03-26 13:04:51 +100079 BN_mod(challenge, challenge, pk->n, ctx);
Damien Miller98c7ad62000-03-09 21:27:49 +110080 BN_CTX_free(ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller95def091999-11-25 00:26:21 +110082 /* Encrypt the challenge with the public key. */
83 rsa_public_encrypt(encrypted_challenge, challenge, pk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
Damien Miller95def091999-11-25 00:26:21 +110085 /* Send the encrypted challenge to the client. */
86 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
87 packet_put_bignum(encrypted_challenge);
88 packet_send();
Damien Miller98c7ad62000-03-09 21:27:49 +110089 BN_clear_free(encrypted_challenge);
Damien Miller95def091999-11-25 00:26:21 +110090 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091
Damien Miller98c7ad62000-03-09 21:27:49 +110092 /* Wait for a response. */
93 packet_read_expect(&plen, SSH_CMSG_AUTH_RSA_RESPONSE);
94 packet_integrity_check(plen, 16, SSH_CMSG_AUTH_RSA_RESPONSE);
95 for (i = 0; i < 16; i++)
96 response[i] = packet_get_char();
97
Damien Miller95def091999-11-25 00:26:21 +110098 /* The response is MD5 of decrypted challenge plus session id. */
99 len = BN_num_bytes(challenge);
100 if (len <= 0 || len > 32)
101 fatal("auth_rsa_challenge_dialog: bad challenge length %d", len);
102 memset(buf, 0, 32);
103 BN_bn2bin(challenge, buf + 32 - len);
104 MD5_Init(&md);
105 MD5_Update(&md, buf, 32);
106 MD5_Update(&md, session_id, 16);
107 MD5_Final(mdbuf, &md);
Damien Miller95def091999-11-25 00:26:21 +1100108 BN_clear_free(challenge);
Damien Miller95def091999-11-25 00:26:21 +1100109
110 /* Verify that the response is the original challenge. */
111 if (memcmp(response, mdbuf, 16) != 0) {
112 /* Wrong answer. */
113 return 0;
114 }
115 /* Correct answer. */
116 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117}
118
Damien Miller5428f641999-11-25 11:54:57 +1100119/*
120 * Performs the RSA authentication dialog with the client. This returns
121 * 0 if the client could not be authenticated, and 1 if authentication was
122 * successful. This may exit if there is a serious protocol violation.
123 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124
125int
Damien Miller6d7b2cd1999-11-12 15:19:27 +1100126auth_rsa(struct passwd *pw, BIGNUM *client_n)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127{
Damien Miller95def091999-11-25 00:26:21 +1100128 extern ServerOptions options;
129 char line[8192], file[1024];
130 int authenticated;
131 unsigned int bits;
132 FILE *f;
133 unsigned long linenum = 0;
134 struct stat st;
Damien Miller450a7a12000-03-26 13:04:51 +1000135 RSA *pk;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000136
Damien Miller95def091999-11-25 00:26:21 +1100137 /* Temporarily use the user's uid. */
138 temporarily_use_uid(pw->pw_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller95def091999-11-25 00:26:21 +1100140 /* The authorized keys. */
141 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
142 SSH_USER_PERMITTED_KEYS);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Damien Miller95def091999-11-25 00:26:21 +1100144 /* Fail quietly if file does not exist */
145 if (stat(file, &st) < 0) {
146 /* Restore the privileged uid. */
147 restore_uid();
148 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149 }
Damien Miller95def091999-11-25 00:26:21 +1100150 /* Open the file containing the authorized keys. */
151 f = fopen(file, "r");
152 if (!f) {
153 /* Restore the privileged uid. */
154 restore_uid();
155 packet_send_debug("Could not open %.900s for reading.", file);
156 packet_send_debug("If your home is on an NFS volume, it may need to be world-readable.");
157 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158 }
Damien Miller95def091999-11-25 00:26:21 +1100159 if (options.strict_modes) {
160 int fail = 0;
161 char buf[1024];
162 /* Check open file in order to avoid open/stat races */
163 if (fstat(fileno(f), &st) < 0 ||
164 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
165 (st.st_mode & 022) != 0) {
166 snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: "
167 "bad ownership or modes for '%s'.", pw->pw_name, file);
168 fail = 1;
169 } else {
170 /* Check path to SSH_USER_PERMITTED_KEYS */
171 int i;
172 static const char *check[] = {
173 "", SSH_USER_DIR, NULL
174 };
175 for (i = 0; check[i]; i++) {
176 snprintf(line, sizeof line, "%.500s/%.100s", pw->pw_dir, check[i]);
177 if (stat(line, &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, line);
182 fail = 1;
183 break;
184 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186 }
Damien Miller95def091999-11-25 00:26:21 +1100187 if (fail) {
188 log(buf);
189 packet_send_debug(buf);
190 restore_uid();
191 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000192 }
Damien Miller95def091999-11-25 00:26:21 +1100193 }
194 /* Flag indicating whether authentication has succeeded. */
195 authenticated = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196
Damien Miller450a7a12000-03-26 13:04:51 +1000197 pk = RSA_new();
198 pk->e = BN_new();
199 pk->n = BN_new();
Damien Miller95def091999-11-25 00:26:21 +1100200
Damien Miller5428f641999-11-25 11:54:57 +1100201 /*
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 */
Damien Miller95def091999-11-25 00:26:21 +1100206 while (fgets(line, sizeof(line), f)) {
207 char *cp;
208 char *options;
209
210 linenum++;
211
Damien Miller5428f641999-11-25 11:54:57 +1100212 /* Skip leading whitespace, empty and comment lines. */
213 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
214 ;
Damien Miller95def091999-11-25 00:26:21 +1100215 if (!*cp || *cp == '\n' || *cp == '#')
216 continue;
217
Damien Miller5428f641999-11-25 11:54:57 +1100218 /*
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 */
Damien Miller95def091999-11-25 00:26:21 +1100224 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 /* Parse the key from the line. */
Damien Miller450a7a12000-03-26 13:04:51 +1000237 if (!auth_rsa_read_key(&cp, &bits, pk->e, pk->n)) {
Damien Miller95def091999-11-25 00:26:21 +1100238 debug("%.100s, line %lu: bad key syntax",
239 SSH_USER_PERMITTED_KEYS, linenum);
240 packet_send_debug("%.100s, line %lu: bad key syntax",
Damien Miller4af51302000-04-16 11:18:38 +1000241 SSH_USER_PERMITTED_KEYS, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100242 continue;
243 }
244 /* cp now points to the comment part. */
245
Damien Miller95def091999-11-25 00:26:21 +1100246 /* Check if the we have found the desired key (identified by its modulus). */
Damien Miller450a7a12000-03-26 13:04:51 +1000247 if (BN_cmp(pk->n, client_n) != 0)
Damien Miller5428f641999-11-25 11:54:57 +1100248 continue;
Damien Miller95def091999-11-25 00:26:21 +1100249
Damien Milleraae6c611999-12-06 11:47:28 +1100250 /* check the real bits */
Damien Miller450a7a12000-03-26 13:04:51 +1000251 if (bits != BN_num_bits(pk->n))
Damien Milleraae6c611999-12-06 11:47:28 +1100252 log("Warning: %s, line %ld: keysize mismatch: "
253 "actual %d vs. announced %d.",
Damien Miller450a7a12000-03-26 13:04:51 +1000254 file, linenum, BN_num_bits(pk->n), bits);
Damien Milleraae6c611999-12-06 11:47:28 +1100255
Damien Miller95def091999-11-25 00:26:21 +1100256 /* We have found the desired key. */
257
Damien Miller450a7a12000-03-26 13:04:51 +1000258
Damien Miller95def091999-11-25 00:26:21 +1100259 /* Perform the challenge-response dialog for this key. */
Damien Miller450a7a12000-03-26 13:04:51 +1000260 if (!auth_rsa_challenge_dialog(pk)) {
Damien Miller95def091999-11-25 00:26:21 +1100261 /* Wrong response. */
262 verbose("Wrong response to RSA authentication challenge.");
263 packet_send_debug("Wrong response to RSA authentication challenge.");
264 continue;
265 }
Damien Miller5428f641999-11-25 11:54:57 +1100266 /*
267 * Correct response. The client has been successfully
268 * authenticated. Note that we have not yet processed the
269 * options; this will be reset if the options cause the
270 * authentication to be rejected.
271 */
Damien Miller95def091999-11-25 00:26:21 +1100272 authenticated = 1;
273
274 /* RSA part of authentication was accepted. Now process the options. */
275 if (options) {
276 while (*options && *options != ' ' && *options != '\t') {
277 cp = "no-port-forwarding";
278 if (strncmp(options, cp, strlen(cp)) == 0) {
279 packet_send_debug("Port forwarding disabled.");
280 no_port_forwarding_flag = 1;
281 options += strlen(cp);
282 goto next_option;
283 }
284 cp = "no-agent-forwarding";
285 if (strncmp(options, cp, strlen(cp)) == 0) {
286 packet_send_debug("Agent forwarding disabled.");
287 no_agent_forwarding_flag = 1;
288 options += strlen(cp);
289 goto next_option;
290 }
291 cp = "no-X11-forwarding";
292 if (strncmp(options, cp, strlen(cp)) == 0) {
293 packet_send_debug("X11 forwarding disabled.");
294 no_x11_forwarding_flag = 1;
295 options += strlen(cp);
296 goto next_option;
297 }
298 cp = "no-pty";
299 if (strncmp(options, cp, strlen(cp)) == 0) {
300 packet_send_debug("Pty allocation disabled.");
301 no_pty_flag = 1;
302 options += strlen(cp);
303 goto next_option;
304 }
305 cp = "command=\"";
306 if (strncmp(options, cp, strlen(cp)) == 0) {
307 int i;
308 options += strlen(cp);
309 forced_command = xmalloc(strlen(options) + 1);
310 i = 0;
311 while (*options) {
312 if (*options == '"')
313 break;
314 if (*options == '\\' && options[1] == '"') {
315 options += 2;
316 forced_command[i++] = '"';
317 continue;
318 }
319 forced_command[i++] = *options++;
320 }
321 if (!*options) {
322 debug("%.100s, line %lu: missing end quote",
323 SSH_USER_PERMITTED_KEYS, linenum);
324 packet_send_debug("%.100s, line %lu: missing end quote",
325 SSH_USER_PERMITTED_KEYS, linenum);
326 continue;
327 }
328 forced_command[i] = 0;
329 packet_send_debug("Forced command: %.900s", forced_command);
330 options++;
331 goto next_option;
332 }
333 cp = "environment=\"";
334 if (strncmp(options, cp, strlen(cp)) == 0) {
335 int i;
336 char *s;
337 struct envstring *new_envstring;
338 options += strlen(cp);
339 s = xmalloc(strlen(options) + 1);
340 i = 0;
341 while (*options) {
342 if (*options == '"')
343 break;
344 if (*options == '\\' && options[1] == '"') {
345 options += 2;
346 s[i++] = '"';
347 continue;
348 }
349 s[i++] = *options++;
350 }
351 if (!*options) {
352 debug("%.100s, line %lu: missing end quote",
353 SSH_USER_PERMITTED_KEYS, linenum);
354 packet_send_debug("%.100s, line %lu: missing end quote",
355 SSH_USER_PERMITTED_KEYS, linenum);
356 continue;
357 }
358 s[i] = 0;
359 packet_send_debug("Adding to environment: %.900s", s);
360 debug("Adding to environment: %.900s", s);
361 options++;
362 new_envstring = xmalloc(sizeof(struct envstring));
363 new_envstring->s = s;
364 new_envstring->next = custom_environment;
365 custom_environment = new_envstring;
366 goto next_option;
367 }
368 cp = "from=\"";
369 if (strncmp(options, cp, strlen(cp)) == 0) {
370 char *patterns = xmalloc(strlen(options) + 1);
371 int i;
372 options += strlen(cp);
373 i = 0;
374 while (*options) {
375 if (*options == '"')
376 break;
377 if (*options == '\\' && options[1] == '"') {
378 options += 2;
379 patterns[i++] = '"';
380 continue;
381 }
382 patterns[i++] = *options++;
383 }
384 if (!*options) {
385 debug("%.100s, line %lu: missing end quote",
386 SSH_USER_PERMITTED_KEYS, linenum);
387 packet_send_debug("%.100s, line %lu: missing end quote",
388 SSH_USER_PERMITTED_KEYS, linenum);
389 continue;
390 }
391 patterns[i] = 0;
392 options++;
393 if (!match_hostname(get_canonical_hostname(), patterns,
394 strlen(patterns)) &&
395 !match_hostname(get_remote_ipaddr(), patterns,
396 strlen(patterns))) {
397 log("RSA authentication tried for %.100s with correct key but not from a permitted host (host=%.200s, ip=%.200s).",
398 pw->pw_name, get_canonical_hostname(),
399 get_remote_ipaddr());
400 packet_send_debug("Your host '%.200s' is not permitted to use this key for login.",
401 get_canonical_hostname());
402 xfree(patterns);
Damien Miller396691a2000-01-20 22:44:08 +1100403 /* key invalid for this host, reset flags */
Damien Miller95def091999-11-25 00:26:21 +1100404 authenticated = 0;
Damien Miller396691a2000-01-20 22:44:08 +1100405 no_agent_forwarding_flag = 0;
406 no_port_forwarding_flag = 0;
407 no_pty_flag = 0;
408 no_x11_forwarding_flag = 0;
409 while (custom_environment) {
410 struct envstring *ce = custom_environment;
411 custom_environment = ce->next;
412 xfree(ce->s);
413 xfree(ce);
414 }
415 if (forced_command) {
416 xfree(forced_command);
417 forced_command = NULL;
418 }
Damien Miller95def091999-11-25 00:26:21 +1100419 break;
420 }
421 xfree(patterns);
422 /* Host name matches. */
423 goto next_option;
424 }
425 bad_option:
Damien Miller95def091999-11-25 00:26:21 +1100426 log("Bad options in %.100s file, line %lu: %.50s",
427 SSH_USER_PERMITTED_KEYS, linenum, options);
428 packet_send_debug("Bad options in %.100s file, line %lu: %.50s",
429 SSH_USER_PERMITTED_KEYS, linenum, options);
430 authenticated = 0;
431 break;
432
433 next_option:
Damien Miller5428f641999-11-25 11:54:57 +1100434 /*
435 * Skip the comma, and move to the next option
436 * (or break out if there are no more).
437 */
Damien Miller95def091999-11-25 00:26:21 +1100438 if (!*options)
439 fatal("Bugs in auth-rsa.c option processing.");
440 if (*options == ' ' || *options == '\t')
Damien Miller5428f641999-11-25 11:54:57 +1100441 break; /* End of options. */
Damien Miller95def091999-11-25 00:26:21 +1100442 if (*options != ',')
443 goto bad_option;
444 options++;
445 /* Process the next option. */
446 continue;
447 }
448 }
Damien Miller5428f641999-11-25 11:54:57 +1100449 /*
450 * Break out of the loop if authentication was successful;
451 * otherwise continue searching.
452 */
Damien Miller95def091999-11-25 00:26:21 +1100453 if (authenticated)
454 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000455 }
456
Damien Miller95def091999-11-25 00:26:21 +1100457 /* Restore the privileged uid. */
458 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459
Damien Miller95def091999-11-25 00:26:21 +1100460 /* Close the file. */
461 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462
Damien Miller450a7a12000-03-26 13:04:51 +1000463 RSA_free(pk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000464
Damien Miller95def091999-11-25 00:26:21 +1100465 if (authenticated)
466 packet_send_debug("RSA authentication accepted.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000467
Damien Miller95def091999-11-25 00:26:21 +1100468 /* Return authentication result. */
469 return authenticated;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000470}