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>
33 /* Flags that may be set in authorized_keys options. */
34 extern int no_port_forwarding_flag;
35 extern int no_agent_forwarding_flag;
36 extern int no_x11_forwarding_flag;
37 extern int no_pty_flag;
38 extern char *forced_command;
39 extern struct envstring *custom_environment;
42 * Session identifier that is used to bind key exchange and authentication
43 * responses to a particular session.
45 extern unsigned char session_id[16];
48 * The .ssh/authorized_keys file contains public keys, one per line, in the
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.
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.
64 auth_rsa_challenge_dialog(RSA *pk)
66 BIGNUM *challenge, *encrypted_challenge;
68 unsigned char buf[32], mdbuf[16], response[16];
73 encrypted_challenge = BN_new();
76 /* Generate a random challenge. */
77 BN_rand(challenge, 256, 0, 0);
79 BN_mod(challenge, challenge, pk->n, ctx);
82 /* Encrypt the challenge with the public key. */
83 rsa_public_encrypt(encrypted_challenge, challenge, pk);
85 /* Send the encrypted challenge to the client. */
86 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
87 packet_put_bignum(encrypted_challenge);
89 BN_clear_free(encrypted_challenge);
92 /* 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();
98 /* 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);
103 BN_bn2bin(challenge, buf + 32 - len);
105 MD5_Update(&md, buf, 32);
106 MD5_Update(&md, session_id, 16);
107 MD5_Final(mdbuf, &md);
108 BN_clear_free(challenge);
110 /* Verify that the response is the original challenge. */
111 if (memcmp(response, mdbuf, 16) != 0) {
115 /* Correct answer. */
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.
126 auth_rsa(struct passwd *pw, BIGNUM *client_n)
128 extern ServerOptions options;
129 char line[8192], file[1024];
133 unsigned long linenum = 0;
137 /* Temporarily use the user's uid. */
138 temporarily_use_uid(pw->pw_uid);
140 /* The authorized keys. */
141 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
142 SSH_USER_PERMITTED_KEYS);
144 /* Fail quietly if file does not exist */
145 if (stat(file, &st) < 0) {
146 /* Restore the privileged uid. */
150 /* Open the file containing the authorized keys. */
151 f = fopen(file, "r");
153 /* Restore the privileged 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.");
159 if (options.strict_modes) {
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);
170 /* Check path to SSH_USER_PERMITTED_KEYS */
172 static const char *check[] = {
173 "", SSH_USER_DIR, NULL
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);
189 packet_send_debug(buf);
194 /* Flag indicating whether authentication has succeeded. */
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.
206 while (fgets(line, sizeof(line), f)) {
212 /* Skip leading whitespace, empty and comment lines. */
213 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
215 if (!*cp || *cp == '\n' || *cp == '#')
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
224 if (*cp < '0' || *cp > '9') {
227 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
228 if (*cp == '\\' && cp[1] == '"')
229 cp++; /* Skip both */
236 /* Parse the key from the line. */
237 if (!auth_rsa_read_key(&cp, &bits, pk->e, pk->n)) {
238 debug("%.100s, line %lu: bad key syntax",
239 SSH_USER_PERMITTED_KEYS, linenum);
240 packet_send_debug("%.100s, line %lu: bad key syntax",
241 SSH_USER_PERMITTED_KEYS, linenum);
244 /* cp now points to the comment part. */
246 /* Check if the we have found the desired key (identified by its modulus). */
247 if (BN_cmp(pk->n, client_n) != 0)
250 /* check the real bits */
251 if (bits != BN_num_bits(pk->n))
252 log("Warning: %s, line %ld: keysize mismatch: "
253 "actual %d vs. announced %d.",
254 file, linenum, BN_num_bits(pk->n), bits);
256 /* We have found the desired key. */
259 /* Perform the challenge-response dialog for this key. */
260 if (!auth_rsa_challenge_dialog(pk)) {
261 /* Wrong response. */
262 verbose("Wrong response to RSA authentication challenge.");
263 packet_send_debug("Wrong response to RSA authentication challenge.");
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.
274 /* RSA part of authentication was accepted. Now process the 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);
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);
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);
299 if (strncmp(options, cp, strlen(cp)) == 0) {
300 packet_send_debug("Pty allocation disabled.");
302 options += strlen(cp);
306 if (strncmp(options, cp, strlen(cp)) == 0) {
308 options += strlen(cp);
309 forced_command = xmalloc(strlen(options) + 1);
314 if (*options == '\\' && options[1] == '"') {
316 forced_command[i++] = '"';
319 forced_command[i++] = *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);
328 forced_command[i] = 0;
329 packet_send_debug("Forced command: %.900s", forced_command);
333 cp = "environment=\"";
334 if (strncmp(options, cp, strlen(cp)) == 0) {
337 struct envstring *new_envstring;
338 options += strlen(cp);
339 s = xmalloc(strlen(options) + 1);
344 if (*options == '\\' && options[1] == '"') {
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);
359 packet_send_debug("Adding to environment: %.900s", s);
360 debug("Adding to environment: %.900s", s);
362 new_envstring = xmalloc(sizeof(struct envstring));
363 new_envstring->s = s;
364 new_envstring->next = custom_environment;
365 custom_environment = new_envstring;
369 if (strncmp(options, cp, strlen(cp)) == 0) {
370 char *patterns = xmalloc(strlen(options) + 1);
372 options += strlen(cp);
377 if (*options == '\\' && options[1] == '"') {
382 patterns[i++] = *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);
393 if (!match_hostname(get_canonical_hostname(), patterns,
395 !match_hostname(get_remote_ipaddr(), 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());
403 /* key invalid for this host, reset flags */
405 no_agent_forwarding_flag = 0;
406 no_port_forwarding_flag = 0;
408 no_x11_forwarding_flag = 0;
409 while (custom_environment) {
410 struct envstring *ce = custom_environment;
411 custom_environment = ce->next;
415 if (forced_command) {
416 xfree(forced_command);
417 forced_command = NULL;
422 /* Host name matches. */
426 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);
435 * Skip the comma, and move to the next option
436 * (or break out if there are no more).
439 fatal("Bugs in auth-rsa.c option processing.");
440 if (*options == ' ' || *options == '\t')
441 break; /* End of options. */
445 /* Process the next option. */
450 * Break out of the loop if authentication was successful;
451 * otherwise continue searching.
457 /* Restore the privileged uid. */
460 /* Close the file. */
466 packet_send_debug("RSA authentication accepted.");
468 /* Return authentication result. */
469 return authenticated;