2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * RSA-based authentication. This code determines whether to admit a login
6 * based on RSA authentication. This file also contains functions to check
7 * validity of the host key.
9 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
17 RCSID("$OpenBSD: auth-rsa.c,v 1.32 2000/10/14 12:19:45 markus Exp $");
28 #include "auth-options.h"
30 #include <openssl/rsa.h>
31 #include <openssl/md5.h>
35 extern ServerOptions options;
38 * Session identifier that is used to bind key exchange and authentication
39 * responses to a particular session.
41 extern unsigned char session_id[16];
44 * The .ssh/authorized_keys file contains public keys, one per line, in the
46 * options bits e n comment
47 * where bits, e and n are decimal numbers,
48 * and comment is any string of characters up to newline. The maximum
49 * length of a line is 8000 characters. See the documentation for a
50 * description of the options.
54 * Performs the RSA authentication challenge-response dialog with the client,
55 * and returns true (non-zero) if the client gave the correct answer to
56 * our challenge; returns zero if the client gives a wrong answer.
60 auth_rsa_challenge_dialog(RSA *pk)
62 BIGNUM *challenge, *encrypted_challenge;
64 unsigned char buf[32], mdbuf[16], response[16];
69 encrypted_challenge = BN_new();
72 /* Generate a random challenge. */
73 BN_rand(challenge, 256, 0, 0);
75 BN_mod(challenge, challenge, pk->n, ctx);
78 /* Encrypt the challenge with the public key. */
79 rsa_public_encrypt(encrypted_challenge, challenge, pk);
81 /* Send the encrypted challenge to the client. */
82 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
83 packet_put_bignum(encrypted_challenge);
85 BN_clear_free(encrypted_challenge);
88 /* Wait for a response. */
89 packet_read_expect(&plen, SSH_CMSG_AUTH_RSA_RESPONSE);
90 packet_integrity_check(plen, 16, SSH_CMSG_AUTH_RSA_RESPONSE);
91 for (i = 0; i < 16; i++)
92 response[i] = packet_get_char();
94 /* The response is MD5 of decrypted challenge plus session id. */
95 len = BN_num_bytes(challenge);
96 if (len <= 0 || len > 32)
97 fatal("auth_rsa_challenge_dialog: bad challenge length %d", len);
99 BN_bn2bin(challenge, buf + 32 - len);
101 MD5_Update(&md, buf, 32);
102 MD5_Update(&md, session_id, 16);
103 MD5_Final(mdbuf, &md);
104 BN_clear_free(challenge);
106 /* Verify that the response is the original challenge. */
107 if (memcmp(response, mdbuf, 16) != 0) {
111 /* Correct answer. */
116 * Performs the RSA authentication dialog with the client. This returns
117 * 0 if the client could not be authenticated, and 1 if authentication was
118 * successful. This may exit if there is a serious protocol violation.
122 auth_rsa(struct passwd *pw, BIGNUM *client_n)
124 char line[8192], file[1024];
128 unsigned long linenum = 0;
136 /* Temporarily use the user's uid. */
137 temporarily_use_uid(pw->pw_uid);
139 /* The authorized keys. */
140 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
141 SSH_USER_PERMITTED_KEYS);
143 /* Fail quietly if file does not exist */
144 if (stat(file, &st) < 0) {
145 /* Restore the privileged uid. */
149 /* Open the file containing the authorized keys. */
150 f = fopen(file, "r");
152 /* Restore the privileged uid. */
154 packet_send_debug("Could not open %.900s for reading.", file);
155 packet_send_debug("If your home is on an NFS volume, it may need to be world-readable.");
158 if (options.strict_modes) {
161 /* Check open file in order to avoid open/stat races */
162 if (fstat(fileno(f), &st) < 0 ||
163 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
164 (st.st_mode & 022) != 0) {
165 snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: "
166 "bad ownership or modes for '%s'.", pw->pw_name, file);
169 /* Check path to SSH_USER_PERMITTED_KEYS */
171 static const char *check[] = {
172 "", SSH_USER_DIR, NULL
174 for (i = 0; check[i]; i++) {
175 snprintf(line, sizeof line, "%.500s/%.100s", pw->pw_dir, check[i]);
176 if (stat(line, &st) < 0 ||
177 (st.st_uid != 0 && st.st_uid != pw->pw_uid) ||
178 (st.st_mode & 022) != 0) {
179 snprintf(buf, sizeof buf, "RSA authentication refused for %.100s: "
180 "bad ownership or modes for '%s'.", pw->pw_name, line);
189 packet_send_debug("%s",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 * If our options do not allow this key to be used,
237 * do not send challenge.
239 if (!auth_parse_options(pw, options, linenum))
242 /* Parse the key from the line. */
243 if (!auth_rsa_read_key(&cp, &bits, pk->e, pk->n)) {
244 debug("%.100s, line %lu: bad key syntax",
245 SSH_USER_PERMITTED_KEYS, linenum);
246 packet_send_debug("%.100s, line %lu: bad key syntax",
247 SSH_USER_PERMITTED_KEYS, linenum);
250 /* cp now points to the comment part. */
252 /* Check if the we have found the desired key (identified by its modulus). */
253 if (BN_cmp(pk->n, client_n) != 0)
256 /* check the real bits */
257 if (bits != BN_num_bits(pk->n))
258 log("Warning: %s, line %ld: keysize mismatch: "
259 "actual %d vs. announced %d.",
260 file, linenum, BN_num_bits(pk->n), bits);
262 /* We have found the desired key. */
264 /* Perform the challenge-response dialog for this key. */
265 if (!auth_rsa_challenge_dialog(pk)) {
266 /* Wrong response. */
267 verbose("Wrong response to RSA authentication challenge.");
268 packet_send_debug("Wrong response to RSA authentication challenge.");
272 * Correct response. The client has been successfully
273 * authenticated. Note that we have not yet processed the
274 * options; this will be reset if the options cause the
275 * authentication to be rejected.
276 * Break out of the loop if authentication was successful;
277 * otherwise continue searching.
283 /* Restore the privileged uid. */
286 /* Close the file. */
292 packet_send_debug("RSA authentication accepted.");
294 auth_clear_options();
296 /* Return authentication result. */
297 return authenticated;