]> andersk Git - openssh.git/blame - auth-rsa.c
- Better tests for OpenSSL w/ RSAref
[openssh.git] / auth-rsa.c
CommitLineData
8efc0c15 1/*
5260325f 2 *
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 */
8efc0c15 17
18#include "includes.h"
19RCSID("$Id$");
20
21#include "rsa.h"
22#include "packet.h"
23#include "xmalloc.h"
24#include "ssh.h"
25#include "mpaux.h"
26#include "uidswap.h"
6fa724bc 27#include "servconf.h"
8efc0c15 28
5881cd60 29#ifdef HAVE_OPENSSL
8efc0c15 30#include <openssl/rsa.h>
31#include <openssl/md5.h>
5881cd60 32#endif
33#ifdef HAVE_SSL
34#include <ssl/rsa.h>
35#include <ssl/md5.h>
36#endif
8efc0c15 37
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
aa3378df 46/*
47 * Session identifier that is used to bind key exchange and authentication
48 * responses to a particular session.
49 */
8efc0c15 50extern unsigned char session_id[16];
51
aa3378df 52/*
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 */
8efc0c15 61
aa3378df 62/*
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 */
8efc0c15 67
68int
4d195447 69auth_rsa_challenge_dialog(BIGNUM *e, BIGNUM *n)
8efc0c15 70{
c8d54615 71 BIGNUM *challenge, *encrypted_challenge;
5260325f 72 RSA *pk;
c8d54615 73 BN_CTX *ctx;
5260325f 74 unsigned char buf[32], mdbuf[16], response[16];
75 MD5_CTX md;
76 unsigned int i;
77 int plen, len;
78
79 encrypted_challenge = BN_new();
80 challenge = BN_new();
5260325f 81
82 /* Generate a random challenge. */
83 BN_rand(challenge, 256, 0, 0);
c8d54615 84 ctx = BN_CTX_new();
5260325f 85 BN_mod(challenge, challenge, n, ctx);
c8d54615 86 BN_CTX_free(ctx);
5260325f 87
88 /* 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);
94
95 /* Encrypt the challenge with the public key. */
96 rsa_public_encrypt(encrypted_challenge, challenge, pk);
97 RSA_free(pk);
98
99 /* Send the encrypted challenge to the client. */
100 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
101 packet_put_bignum(encrypted_challenge);
102 packet_send();
c8d54615 103 BN_clear_free(encrypted_challenge);
5260325f 104 packet_write_wait();
105
c8d54615 106 /* 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
5260325f 112 /* 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);
5260325f 122 BN_clear_free(challenge);
5260325f 123
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;
8efc0c15 131}
132
aa3378df 133/*
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 */
8efc0c15 138
139int
6fa724bc 140auth_rsa(struct passwd *pw, BIGNUM *client_n)
8efc0c15 141{
5260325f 142 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;
150
151 /* Temporarily use the user's uid. */
152 temporarily_use_uid(pw->pw_uid);
153
154 /* The authorized keys. */
155 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
156 SSH_USER_PERMITTED_KEYS);
157
158 /* Fail quietly if file does not exist */
159 if (stat(file, &st) < 0) {
160 /* Restore the privileged uid. */
161 restore_uid();
162 return 0;
8efc0c15 163 }
5260325f 164 /* 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;
8efc0c15 172 }
5260325f 173 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 }
199 }
8efc0c15 200 }
5260325f 201 if (fail) {
202 log(buf);
203 packet_send_debug(buf);
204 restore_uid();
205 return 0;
8efc0c15 206 }
5260325f 207 }
208 /* Flag indicating whether authentication has succeeded. */
209 authenticated = 0;
210
5260325f 211 e = BN_new();
212 n = BN_new();
213
aa3378df 214 /*
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 */
5260325f 219 while (fgets(line, sizeof(line), f)) {
220 char *cp;
221 char *options;
222
223 linenum++;
224
aa3378df 225 /* Skip leading whitespace, empty and comment lines. */
226 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
227 ;
5260325f 228 if (!*cp || *cp == '\n' || *cp == '#')
229 continue;
230
aa3378df 231 /*
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 */
5260325f 237 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;
8efc0c15 245 }
5260325f 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;
8efc0c15 256 }
5260325f 257 /* cp now points to the comment part. */
258
5260325f 259 /* Check if the we have found the desired key (identified by its modulus). */
260 if (BN_cmp(n, client_n) != 0)
aa3378df 261 continue;
5260325f 262
57112b5a 263 /* 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
5260325f 269 /* 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;
8efc0c15 277 }
aa3378df 278 /*
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 */
5260325f 284 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);
7b2ea3a1 415 /* key invalid for this host, reset flags */
5260325f 416 authenticated = 0;
7b2ea3a1 417 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 }
5260325f 431 break;
432 }
433 xfree(patterns);
434 /* Host name matches. */
435 goto next_option;
436 }
437 bad_option:
5260325f 438 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:
aa3378df 446 /*
447 * Skip the comma, and move to the next option
448 * (or break out if there are no more).
449 */
5260325f 450 if (!*options)
451 fatal("Bugs in auth-rsa.c option processing.");
452 if (*options == ' ' || *options == '\t')
aa3378df 453 break; /* End of options. */
5260325f 454 if (*options != ',')
455 goto bad_option;
456 options++;
457 /* Process the next option. */
458 continue;
8efc0c15 459 }
8efc0c15 460 }
aa3378df 461 /*
462 * Break out of the loop if authentication was successful;
463 * otherwise continue searching.
464 */
5260325f 465 if (authenticated)
466 break;
8efc0c15 467 }
468
5260325f 469 /* Restore the privileged uid. */
470 restore_uid();
8efc0c15 471
5260325f 472 /* Close the file. */
473 fclose(f);
8efc0c15 474
5260325f 475 BN_clear_free(n);
476 BN_clear_free(e);
8efc0c15 477
5260325f 478 if (authenticated)
479 packet_send_debug("RSA authentication accepted.");
8efc0c15 480
5260325f 481 /* Return authentication result. */
482 return authenticated;
8efc0c15 483}
This page took 1.249201 seconds and 5 git commands to generate.