blob: bc53b049b2fb100fa7e7574715c53bf3cf07a25c [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 Milleraae6c611999-12-06 11:47:28 +110019RCSID("$Id: auth-rsa.c,v 1.11 1999/12/06 00:47:28 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 Miller95def091999-11-25 00:26:21 +110071 BIGNUM *challenge, *encrypted_challenge, *aux;
72 RSA *pk;
73 BN_CTX *ctx = BN_CTX_new();
74 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();
81 aux = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller95def091999-11-25 00:26:21 +110083 /* Generate a random challenge. */
84 BN_rand(challenge, 256, 0, 0);
85 BN_mod(challenge, challenge, n, ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller95def091999-11-25 00:26:21 +110087 /* Create the public key data structure. */
88 pk = RSA_new();
89 pk->e = BN_new();
90 BN_copy(pk->e, e);
91 pk->n = BN_new();
92 BN_copy(pk->n, n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100093
Damien Miller95def091999-11-25 00:26:21 +110094 /* Encrypt the challenge with the public key. */
95 rsa_public_encrypt(encrypted_challenge, challenge, pk);
96 RSA_free(pk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100097
Damien Miller95def091999-11-25 00:26:21 +110098 /* Send the encrypted challenge to the client. */
99 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
100 packet_put_bignum(encrypted_challenge);
101 packet_send();
102 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000103
Damien Miller95def091999-11-25 00:26:21 +1100104 /* The response is MD5 of decrypted challenge plus session id. */
105 len = BN_num_bytes(challenge);
106 if (len <= 0 || len > 32)
107 fatal("auth_rsa_challenge_dialog: bad challenge length %d", len);
108 memset(buf, 0, 32);
109 BN_bn2bin(challenge, buf + 32 - len);
110 MD5_Init(&md);
111 MD5_Update(&md, buf, 32);
112 MD5_Update(&md, session_id, 16);
113 MD5_Final(mdbuf, &md);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114
Damien Miller95def091999-11-25 00:26:21 +1100115 /* We will no longer need these. */
116 BN_clear_free(encrypted_challenge);
117 BN_clear_free(challenge);
118 BN_clear_free(aux);
119 BN_CTX_free(ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
Damien Miller95def091999-11-25 00:26:21 +1100121 /* Wait for a response. */
122 packet_read_expect(&plen, SSH_CMSG_AUTH_RSA_RESPONSE);
123 packet_integrity_check(plen, 16, SSH_CMSG_AUTH_RSA_RESPONSE);
124 for (i = 0; i < 16; i++)
125 response[i] = packet_get_char();
126
127 /* Verify that the response is the original challenge. */
128 if (memcmp(response, mdbuf, 16) != 0) {
129 /* Wrong answer. */
130 return 0;
131 }
132 /* Correct answer. */
133 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134}
135
Damien Miller5428f641999-11-25 11:54:57 +1100136/*
137 * Performs the RSA authentication dialog with the client. This returns
138 * 0 if the client could not be authenticated, and 1 if authentication was
139 * successful. This may exit if there is a serious protocol violation.
140 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
142int
Damien Miller6d7b2cd1999-11-12 15:19:27 +1100143auth_rsa(struct passwd *pw, BIGNUM *client_n)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144{
Damien Miller95def091999-11-25 00:26:21 +1100145 extern ServerOptions options;
146 char line[8192], file[1024];
147 int authenticated;
148 unsigned int bits;
149 FILE *f;
150 unsigned long linenum = 0;
151 struct stat st;
152 BIGNUM *e, *n;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153
Damien Miller95def091999-11-25 00:26:21 +1100154 /* Temporarily use the user's uid. */
155 temporarily_use_uid(pw->pw_uid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156
Damien Miller95def091999-11-25 00:26:21 +1100157 /* The authorized keys. */
158 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
159 SSH_USER_PERMITTED_KEYS);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000160
Damien Miller95def091999-11-25 00:26:21 +1100161 /* Fail quietly if file does not exist */
162 if (stat(file, &st) < 0) {
163 /* Restore the privileged uid. */
164 restore_uid();
165 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000166 }
Damien Miller95def091999-11-25 00:26:21 +1100167 /* Open the file containing the authorized keys. */
168 f = fopen(file, "r");
169 if (!f) {
170 /* Restore the privileged uid. */
171 restore_uid();
172 packet_send_debug("Could not open %.900s for reading.", file);
173 packet_send_debug("If your home is on an NFS volume, it may need to be world-readable.");
174 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000175 }
Damien Miller95def091999-11-25 00:26:21 +1100176 if (options.strict_modes) {
177 int fail = 0;
178 char buf[1024];
179 /* Check open file in order to avoid open/stat races */
180 if (fstat(fileno(f), &st) < 0 ||
181 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
182 (st.st_mode & 022) != 0) {
183 snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: "
184 "bad ownership or modes for '%s'.", pw->pw_name, file);
185 fail = 1;
186 } else {
187 /* Check path to SSH_USER_PERMITTED_KEYS */
188 int i;
189 static const char *check[] = {
190 "", SSH_USER_DIR, NULL
191 };
192 for (i = 0; check[i]; i++) {
193 snprintf(line, sizeof line, "%.500s/%.100s", pw->pw_dir, check[i]);
194 if (stat(line, &st) < 0 ||
195 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
196 (st.st_mode & 022) != 0) {
197 snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: "
198 "bad ownership or modes for '%s'.", pw->pw_name, line);
199 fail = 1;
200 break;
201 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000202 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000203 }
Damien Miller95def091999-11-25 00:26:21 +1100204 if (fail) {
205 log(buf);
206 packet_send_debug(buf);
207 restore_uid();
208 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209 }
Damien Miller95def091999-11-25 00:26:21 +1100210 }
211 /* Flag indicating whether authentication has succeeded. */
212 authenticated = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000213
Damien Miller95def091999-11-25 00:26:21 +1100214 e = BN_new();
215 n = BN_new();
216
Damien Miller5428f641999-11-25 11:54:57 +1100217 /*
218 * Go though the accepted keys, looking for the current key. If
219 * found, perform a challenge-response dialog to verify that the
220 * user really has the corresponding private key.
221 */
Damien Miller95def091999-11-25 00:26:21 +1100222 while (fgets(line, sizeof(line), f)) {
223 char *cp;
224 char *options;
225
226 linenum++;
227
Damien Miller5428f641999-11-25 11:54:57 +1100228 /* Skip leading whitespace, empty and comment lines. */
229 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
230 ;
Damien Miller95def091999-11-25 00:26:21 +1100231 if (!*cp || *cp == '\n' || *cp == '#')
232 continue;
233
Damien Miller5428f641999-11-25 11:54:57 +1100234 /*
235 * Check if there are options for this key, and if so,
236 * save their starting address and skip the option part
237 * for now. If there are no options, set the starting
238 * address to NULL.
239 */
Damien Miller95def091999-11-25 00:26:21 +1100240 if (*cp < '0' || *cp > '9') {
241 int quoted = 0;
242 options = cp;
243 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
244 if (*cp == '\\' && cp[1] == '"')
245 cp++; /* Skip both */
246 else if (*cp == '"')
247 quoted = !quoted;
248 }
249 } else
250 options = NULL;
251
252 /* Parse the key from the line. */
253 if (!auth_rsa_read_key(&cp, &bits, e, n)) {
254 debug("%.100s, line %lu: bad key syntax",
255 SSH_USER_PERMITTED_KEYS, linenum);
256 packet_send_debug("%.100s, line %lu: bad key syntax",
257 SSH_USER_PERMITTED_KEYS, linenum);
258 continue;
259 }
260 /* cp now points to the comment part. */
261
Damien Miller95def091999-11-25 00:26:21 +1100262 /* Check if the we have found the desired key (identified by its modulus). */
263 if (BN_cmp(n, client_n) != 0)
Damien Miller5428f641999-11-25 11:54:57 +1100264 continue;
Damien Miller95def091999-11-25 00:26:21 +1100265
Damien Milleraae6c611999-12-06 11:47:28 +1100266 /* check the real bits */
267 if (bits != BN_num_bits(n))
268 log("Warning: %s, line %ld: keysize mismatch: "
269 "actual %d vs. announced %d.",
270 file, linenum, BN_num_bits(n), bits);
271
Damien Miller95def091999-11-25 00:26:21 +1100272 /* We have found the desired key. */
273
274 /* Perform the challenge-response dialog for this key. */
275 if (!auth_rsa_challenge_dialog(e, n)) {
276 /* Wrong response. */
277 verbose("Wrong response to RSA authentication challenge.");
278 packet_send_debug("Wrong response to RSA authentication challenge.");
279 continue;
280 }
Damien Miller5428f641999-11-25 11:54:57 +1100281 /*
282 * Correct response. The client has been successfully
283 * authenticated. Note that we have not yet processed the
284 * options; this will be reset if the options cause the
285 * authentication to be rejected.
286 */
Damien Miller95def091999-11-25 00:26:21 +1100287 authenticated = 1;
288
289 /* RSA part of authentication was accepted. Now process the options. */
290 if (options) {
291 while (*options && *options != ' ' && *options != '\t') {
292 cp = "no-port-forwarding";
293 if (strncmp(options, cp, strlen(cp)) == 0) {
294 packet_send_debug("Port forwarding disabled.");
295 no_port_forwarding_flag = 1;
296 options += strlen(cp);
297 goto next_option;
298 }
299 cp = "no-agent-forwarding";
300 if (strncmp(options, cp, strlen(cp)) == 0) {
301 packet_send_debug("Agent forwarding disabled.");
302 no_agent_forwarding_flag = 1;
303 options += strlen(cp);
304 goto next_option;
305 }
306 cp = "no-X11-forwarding";
307 if (strncmp(options, cp, strlen(cp)) == 0) {
308 packet_send_debug("X11 forwarding disabled.");
309 no_x11_forwarding_flag = 1;
310 options += strlen(cp);
311 goto next_option;
312 }
313 cp = "no-pty";
314 if (strncmp(options, cp, strlen(cp)) == 0) {
315 packet_send_debug("Pty allocation disabled.");
316 no_pty_flag = 1;
317 options += strlen(cp);
318 goto next_option;
319 }
320 cp = "command=\"";
321 if (strncmp(options, cp, strlen(cp)) == 0) {
322 int i;
323 options += strlen(cp);
324 forced_command = xmalloc(strlen(options) + 1);
325 i = 0;
326 while (*options) {
327 if (*options == '"')
328 break;
329 if (*options == '\\' && options[1] == '"') {
330 options += 2;
331 forced_command[i++] = '"';
332 continue;
333 }
334 forced_command[i++] = *options++;
335 }
336 if (!*options) {
337 debug("%.100s, line %lu: missing end quote",
338 SSH_USER_PERMITTED_KEYS, linenum);
339 packet_send_debug("%.100s, line %lu: missing end quote",
340 SSH_USER_PERMITTED_KEYS, linenum);
341 continue;
342 }
343 forced_command[i] = 0;
344 packet_send_debug("Forced command: %.900s", forced_command);
345 options++;
346 goto next_option;
347 }
348 cp = "environment=\"";
349 if (strncmp(options, cp, strlen(cp)) == 0) {
350 int i;
351 char *s;
352 struct envstring *new_envstring;
353 options += strlen(cp);
354 s = xmalloc(strlen(options) + 1);
355 i = 0;
356 while (*options) {
357 if (*options == '"')
358 break;
359 if (*options == '\\' && options[1] == '"') {
360 options += 2;
361 s[i++] = '"';
362 continue;
363 }
364 s[i++] = *options++;
365 }
366 if (!*options) {
367 debug("%.100s, line %lu: missing end quote",
368 SSH_USER_PERMITTED_KEYS, linenum);
369 packet_send_debug("%.100s, line %lu: missing end quote",
370 SSH_USER_PERMITTED_KEYS, linenum);
371 continue;
372 }
373 s[i] = 0;
374 packet_send_debug("Adding to environment: %.900s", s);
375 debug("Adding to environment: %.900s", s);
376 options++;
377 new_envstring = xmalloc(sizeof(struct envstring));
378 new_envstring->s = s;
379 new_envstring->next = custom_environment;
380 custom_environment = new_envstring;
381 goto next_option;
382 }
383 cp = "from=\"";
384 if (strncmp(options, cp, strlen(cp)) == 0) {
385 char *patterns = xmalloc(strlen(options) + 1);
386 int i;
387 options += strlen(cp);
388 i = 0;
389 while (*options) {
390 if (*options == '"')
391 break;
392 if (*options == '\\' && options[1] == '"') {
393 options += 2;
394 patterns[i++] = '"';
395 continue;
396 }
397 patterns[i++] = *options++;
398 }
399 if (!*options) {
400 debug("%.100s, line %lu: missing end quote",
401 SSH_USER_PERMITTED_KEYS, linenum);
402 packet_send_debug("%.100s, line %lu: missing end quote",
403 SSH_USER_PERMITTED_KEYS, linenum);
404 continue;
405 }
406 patterns[i] = 0;
407 options++;
408 if (!match_hostname(get_canonical_hostname(), patterns,
409 strlen(patterns)) &&
410 !match_hostname(get_remote_ipaddr(), patterns,
411 strlen(patterns))) {
412 log("RSA authentication tried for %.100s with correct key but not from a permitted host (host=%.200s, ip=%.200s).",
413 pw->pw_name, get_canonical_hostname(),
414 get_remote_ipaddr());
415 packet_send_debug("Your host '%.200s' is not permitted to use this key for login.",
416 get_canonical_hostname());
417 xfree(patterns);
418 authenticated = 0;
419 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 Miller95def091999-11-25 00:26:21 +1100463 BN_clear_free(n);
464 BN_clear_free(e);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000465
Damien Miller95def091999-11-25 00:26:21 +1100466 if (authenticated)
467 packet_send_debug("RSA authentication accepted.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000468
Damien Miller95def091999-11-25 00:26:21 +1100469 /* Return authentication result. */
470 return authenticated;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000471}