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, *aux;
73 BN_CTX *ctx = BN_CTX_new();
74 unsigned char buf[32], mdbuf[16], response[16];
79 encrypted_challenge = BN_new();
83 /* Generate a random challenge. */
84 BN_rand(challenge, 256, 0, 0);
85 BN_mod(challenge, challenge, n, ctx);
87 /* Create the public key data structure. */
94 /* Encrypt the challenge with the public key. */
95 rsa_public_encrypt(encrypted_challenge, challenge, pk);
98 /* Send the encrypted challenge to the client. */
99 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
100 packet_put_bignum(encrypted_challenge);
104 /* 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);
109 BN_bn2bin(challenge, buf + 32 - len);
111 MD5_Update(&md, buf, 32);
112 MD5_Update(&md, session_id, 16);
113 MD5_Final(mdbuf, &md);
115 /* We will no longer need these. */
116 BN_clear_free(encrypted_challenge);
117 BN_clear_free(challenge);
121 /* 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();
127 /* Verify that the response is the original challenge. */
128 if (memcmp(response, mdbuf, 16) != 0) {
132 /* Correct answer. */
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.
143 auth_rsa(struct passwd *pw, BIGNUM *client_n)
145 extern ServerOptions options;
146 char line[8192], file[1024];
150 unsigned long linenum = 0;
154 /* Temporarily use the user's uid. */
155 temporarily_use_uid(pw->pw_uid);
157 /* The authorized keys. */
158 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
159 SSH_USER_PERMITTED_KEYS);
161 /* Fail quietly if file does not exist */
162 if (stat(file, &st) < 0) {
163 /* Restore the privileged uid. */
167 /* Open the file containing the authorized keys. */
168 f = fopen(file, "r");
170 /* Restore the privileged 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.");
176 if (options.strict_modes) {
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);
187 /* Check path to SSH_USER_PERMITTED_KEYS */
189 static const char *check[] = {
190 "", SSH_USER_DIR, NULL
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);
206 packet_send_debug(buf);
211 /* Flag indicating whether authentication has succeeded. */
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.
222 while (fgets(line, sizeof(line), f)) {
228 /* Skip leading whitespace, empty and comment lines. */
229 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
231 if (!*cp || *cp == '\n' || *cp == '#')
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
240 if (*cp < '0' || *cp > '9') {
243 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
244 if (*cp == '\\' && cp[1] == '"')
245 cp++; /* Skip both */
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);
260 /* cp now points to the comment part. */
262 /* check the real bits */
263 if (bits != BN_num_bits(n))
264 error("Warning: error in %s, line %ld: keysize mismatch: "
265 "actual size %d vs. announced %d.",
266 file, linenum, BN_num_bits(n), bits);
268 /* Check if the we have found the desired key (identified by its modulus). */
269 if (BN_cmp(n, client_n) != 0)
272 /* We have found the desired key. */
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.");
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.
289 /* RSA part of authentication was accepted. Now process the 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);
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);
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);
314 if (strncmp(options, cp, strlen(cp)) == 0) {
315 packet_send_debug("Pty allocation disabled.");
317 options += strlen(cp);
321 if (strncmp(options, cp, strlen(cp)) == 0) {
323 options += strlen(cp);
324 forced_command = xmalloc(strlen(options) + 1);
329 if (*options == '\\' && options[1] == '"') {
331 forced_command[i++] = '"';
334 forced_command[i++] = *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);
343 forced_command[i] = 0;
344 packet_send_debug("Forced command: %.900s", forced_command);
348 cp = "environment=\"";
349 if (strncmp(options, cp, strlen(cp)) == 0) {
352 struct envstring *new_envstring;
353 options += strlen(cp);
354 s = xmalloc(strlen(options) + 1);
359 if (*options == '\\' && options[1] == '"') {
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);
374 packet_send_debug("Adding to environment: %.900s", s);
375 debug("Adding to environment: %.900s", s);
377 new_envstring = xmalloc(sizeof(struct envstring));
378 new_envstring->s = s;
379 new_envstring->next = custom_environment;
380 custom_environment = new_envstring;
384 if (strncmp(options, cp, strlen(cp)) == 0) {
385 char *patterns = xmalloc(strlen(options) + 1);
387 options += strlen(cp);
392 if (*options == '\\' && options[1] == '"') {
397 patterns[i++] = *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);
408 if (!match_hostname(get_canonical_hostname(), patterns,
410 !match_hostname(get_remote_ipaddr(), 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());
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. */
467 packet_send_debug("RSA authentication accepted.");
469 /* Return authentication result. */
470 return authenticated;