5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * Created: Mon Mar 27 01:46:52 1995 ylo
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.
30 #include <openssl/rsa.h>
31 #include <openssl/md5.h>
38 /* Flags that may be set in authorized_keys options. */
39 extern int no_port_forwarding_flag;
40 extern int no_agent_forwarding_flag;
41 extern int no_x11_forwarding_flag;
42 extern int no_pty_flag;
43 extern char *forced_command;
44 extern struct envstring *custom_environment;
47 * Session identifier that is used to bind key exchange and authentication
48 * responses to a particular session.
50 extern unsigned char session_id[16];
53 * The .ssh/authorized_keys file contains public keys, one per line, in the
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.
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.
69 auth_rsa_challenge_dialog(BIGNUM *e, BIGNUM *n)
71 BIGNUM *challenge, *encrypted_challenge;
74 unsigned char buf[32], mdbuf[16], response[16];
79 encrypted_challenge = BN_new();
82 /* Generate a random challenge. */
83 BN_rand(challenge, 256, 0, 0);
85 BN_mod(challenge, challenge, n, ctx);
88 /* Create the public key data structure. */
95 /* Encrypt the challenge with the public key. */
96 rsa_public_encrypt(encrypted_challenge, challenge, pk);
99 /* Send the encrypted challenge to the client. */
100 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
101 packet_put_bignum(encrypted_challenge);
103 BN_clear_free(encrypted_challenge);
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();
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);
117 BN_bn2bin(challenge, buf + 32 - len);
119 MD5_Update(&md, buf, 32);
120 MD5_Update(&md, session_id, 16);
121 MD5_Final(mdbuf, &md);
122 BN_clear_free(challenge);
124 /* Verify that the response is the original challenge. */
125 if (memcmp(response, mdbuf, 16) != 0) {
129 /* Correct answer. */
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.
140 auth_rsa(struct passwd *pw, BIGNUM *client_n)
142 extern ServerOptions options;
143 char line[8192], file[1024];
147 unsigned long linenum = 0;
151 /* Temporarily use the user's uid. */
152 temporarily_use_uid(pw->pw_uid);
154 /* The authorized keys. */
155 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
156 SSH_USER_PERMITTED_KEYS);
158 /* Fail quietly if file does not exist */
159 if (stat(file, &st) < 0) {
160 /* Restore the privileged uid. */
164 /* Open the file containing the authorized keys. */
165 f = fopen(file, "r");
167 /* Restore the privileged 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.");
173 if (options.strict_modes) {
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);
184 /* Check path to SSH_USER_PERMITTED_KEYS */
186 static const char *check[] = {
187 "", SSH_USER_DIR, NULL
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);
203 packet_send_debug(buf);
208 /* Flag indicating whether authentication has succeeded. */
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.
219 while (fgets(line, sizeof(line), f)) {
225 /* Skip leading whitespace, empty and comment lines. */
226 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
228 if (!*cp || *cp == '\n' || *cp == '#')
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
237 if (*cp < '0' || *cp > '9') {
240 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
241 if (*cp == '\\' && cp[1] == '"')
242 cp++; /* Skip both */
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);
257 /* cp now points to the comment part. */
259 /* Check if the we have found the desired key (identified by its modulus). */
260 if (BN_cmp(n, client_n) != 0)
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);
269 /* We have found the desired key. */
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.");
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.
286 /* RSA part of authentication was accepted. Now process the 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);
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);
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);
311 if (strncmp(options, cp, strlen(cp)) == 0) {
312 packet_send_debug("Pty allocation disabled.");
314 options += strlen(cp);
318 if (strncmp(options, cp, strlen(cp)) == 0) {
320 options += strlen(cp);
321 forced_command = xmalloc(strlen(options) + 1);
326 if (*options == '\\' && options[1] == '"') {
328 forced_command[i++] = '"';
331 forced_command[i++] = *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);
340 forced_command[i] = 0;
341 packet_send_debug("Forced command: %.900s", forced_command);
345 cp = "environment=\"";
346 if (strncmp(options, cp, strlen(cp)) == 0) {
349 struct envstring *new_envstring;
350 options += strlen(cp);
351 s = xmalloc(strlen(options) + 1);
356 if (*options == '\\' && options[1] == '"') {
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);
371 packet_send_debug("Adding to environment: %.900s", s);
372 debug("Adding to environment: %.900s", s);
374 new_envstring = xmalloc(sizeof(struct envstring));
375 new_envstring->s = s;
376 new_envstring->next = custom_environment;
377 custom_environment = new_envstring;
381 if (strncmp(options, cp, strlen(cp)) == 0) {
382 char *patterns = xmalloc(strlen(options) + 1);
384 options += strlen(cp);
389 if (*options == '\\' && options[1] == '"') {
394 patterns[i++] = *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);
405 if (!match_hostname(get_canonical_hostname(), patterns,
407 !match_hostname(get_remote_ipaddr(), 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());
415 /* key invalid for this host, reset flags */
417 no_agent_forwarding_flag = 0;
418 no_port_forwarding_flag = 0;
420 no_x11_forwarding_flag = 0;
421 while (custom_environment) {
422 struct envstring *ce = custom_environment;
423 custom_environment = ce->next;
427 if (forced_command) {
428 xfree(forced_command);
429 forced_command = NULL;
434 /* Host name matches. */
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);
447 * Skip the comma, and move to the next option
448 * (or break out if there are no more).
451 fatal("Bugs in auth-rsa.c option processing.");
452 if (*options == ' ' || *options == '\t')
453 break; /* End of options. */
457 /* Process the next option. */
462 * Break out of the loop if authentication was successful;
463 * otherwise continue searching.
469 /* Restore the privileged uid. */
472 /* Close the file. */
479 packet_send_debug("RSA authentication accepted.");
481 /* Return authentication result. */
482 return authenticated;