blob: 59bee18bd3f9fa76865bd029a2992f29ace74325 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11003 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11005 * 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.
Damien Miller4af51302000-04-16 11:18:38 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * 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".
Damien Miller95def091999-11-25 00:26:21 +110014 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +000017RCSID("$OpenBSD: auth-rsa.c,v 1.40 2001/04/06 21:00:07 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include <openssl/rsa.h>
20#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
Ben Lindstrom226cfa02001-01-22 05:34:40 +000022#include "rsa.h"
23#include "packet.h"
24#include "xmalloc.h"
25#include "ssh1.h"
26#include "mpaux.h"
27#include "uidswap.h"
28#include "match.h"
29#include "auth-options.h"
30#include "pathnames.h"
31#include "log.h"
32#include "servconf.h"
33#include "auth.h"
Damien Miller874d77b2000-10-14 16:23:11 +110034
35/* import */
36extern ServerOptions options;
37
Damien Miller5428f641999-11-25 11:54:57 +110038/*
39 * Session identifier that is used to bind key exchange and authentication
40 * responses to a particular session.
41 */
Ben Lindstrom46c16222000-12-22 01:43:59 +000042extern u_char session_id[16];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100043
Damien Miller5428f641999-11-25 11:54:57 +110044/*
45 * The .ssh/authorized_keys file contains public keys, one per line, in the
46 * following format:
47 * options bits e n comment
48 * where bits, e and n are decimal numbers,
49 * and comment is any string of characters up to newline. The maximum
50 * length of a line is 8000 characters. See the documentation for a
51 * description of the options.
52 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Damien Miller5428f641999-11-25 11:54:57 +110054/*
55 * Performs the RSA authentication challenge-response dialog with the client,
56 * and returns true (non-zero) if the client gave the correct answer to
57 * our challenge; returns zero if the client gives a wrong answer.
58 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
60int
Damien Miller450a7a12000-03-26 13:04:51 +100061auth_rsa_challenge_dialog(RSA *pk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100062{
Damien Miller98c7ad62000-03-09 21:27:49 +110063 BIGNUM *challenge, *encrypted_challenge;
Damien Miller98c7ad62000-03-09 21:27:49 +110064 BN_CTX *ctx;
Ben Lindstrom46c16222000-12-22 01:43:59 +000065 u_char buf[32], mdbuf[16], response[16];
Damien Miller95def091999-11-25 00:26:21 +110066 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +000067 u_int i;
Damien Miller95def091999-11-25 00:26:21 +110068 int plen, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100069
Damien Miller95def091999-11-25 00:26:21 +110070 encrypted_challenge = BN_new();
71 challenge = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100072
Damien Miller95def091999-11-25 00:26:21 +110073 /* Generate a random challenge. */
74 BN_rand(challenge, 256, 0, 0);
Damien Miller98c7ad62000-03-09 21:27:49 +110075 ctx = BN_CTX_new();
Damien Miller450a7a12000-03-26 13:04:51 +100076 BN_mod(challenge, challenge, pk->n, ctx);
Damien Miller98c7ad62000-03-09 21:27:49 +110077 BN_CTX_free(ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100078
Damien Miller95def091999-11-25 00:26:21 +110079 /* Encrypt the challenge with the public key. */
80 rsa_public_encrypt(encrypted_challenge, challenge, pk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100081
Damien Miller95def091999-11-25 00:26:21 +110082 /* Send the encrypted challenge to the client. */
83 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
84 packet_put_bignum(encrypted_challenge);
85 packet_send();
Damien Miller98c7ad62000-03-09 21:27:49 +110086 BN_clear_free(encrypted_challenge);
Damien Miller95def091999-11-25 00:26:21 +110087 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100088
Damien Miller98c7ad62000-03-09 21:27:49 +110089 /* Wait for a response. */
90 packet_read_expect(&plen, SSH_CMSG_AUTH_RSA_RESPONSE);
91 packet_integrity_check(plen, 16, SSH_CMSG_AUTH_RSA_RESPONSE);
92 for (i = 0; i < 16; i++)
93 response[i] = packet_get_char();
94
Damien Miller95def091999-11-25 00:26:21 +110095 /* The response is MD5 of decrypted challenge plus session id. */
96 len = BN_num_bytes(challenge);
97 if (len <= 0 || len > 32)
98 fatal("auth_rsa_challenge_dialog: bad challenge length %d", len);
99 memset(buf, 0, 32);
100 BN_bn2bin(challenge, buf + 32 - len);
101 MD5_Init(&md);
102 MD5_Update(&md, buf, 32);
103 MD5_Update(&md, session_id, 16);
104 MD5_Final(mdbuf, &md);
Damien Miller95def091999-11-25 00:26:21 +1100105 BN_clear_free(challenge);
Damien Miller95def091999-11-25 00:26:21 +1100106
107 /* Verify that the response is the original challenge. */
108 if (memcmp(response, mdbuf, 16) != 0) {
109 /* Wrong answer. */
110 return 0;
111 }
112 /* Correct answer. */
113 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000114}
115
Damien Miller5428f641999-11-25 11:54:57 +1100116/*
117 * Performs the RSA authentication dialog with the client. This returns
118 * 0 if the client could not be authenticated, and 1 if authentication was
119 * successful. This may exit if there is a serious protocol violation.
120 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000121
122int
Damien Miller6d7b2cd1999-11-12 15:19:27 +1100123auth_rsa(struct passwd *pw, BIGNUM *client_n)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000124{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000125 char line[8192], file[MAXPATHLEN];
Damien Miller95def091999-11-25 00:26:21 +1100126 int authenticated;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000127 u_int bits;
Damien Miller95def091999-11-25 00:26:21 +1100128 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000129 u_long linenum = 0;
Damien Miller95def091999-11-25 00:26:21 +1100130 struct stat st;
Damien Miller450a7a12000-03-26 13:04:51 +1000131 RSA *pk;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000132
Damien Miller874d77b2000-10-14 16:23:11 +1100133 /* no user given */
134 if (pw == NULL)
135 return 0;
136
Damien Miller95def091999-11-25 00:26:21 +1100137 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000138 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000139
Damien Miller95def091999-11-25 00:26:21 +1100140 /* The authorized keys. */
141 snprintf(file, sizeof file, "%.500s/%.100s", pw->pw_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000142 _PATH_SSH_USER_PERMITTED_KEYS);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Damien Miller95def091999-11-25 00:26:21 +1100144 /* Fail quietly if file does not exist */
145 if (stat(file, &st) < 0) {
146 /* Restore the privileged uid. */
147 restore_uid();
148 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000149 }
Damien Miller95def091999-11-25 00:26:21 +1100150 /* Open the file containing the authorized keys. */
151 f = fopen(file, "r");
152 if (!f) {
153 /* Restore the privileged uid. */
154 restore_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.");
157 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158 }
Damien Miller95def091999-11-25 00:26:21 +1100159 if (options.strict_modes) {
160 int fail = 0;
161 char buf[1024];
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);
168 fail = 1;
169 } else {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000170 /* Check path to _PATH_SSH_USER_PERMITTED_KEYS */
Damien Miller95def091999-11-25 00:26:21 +1100171 int i;
172 static const char *check[] = {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000173 "", _PATH_SSH_USER_DIR, NULL
Damien Miller95def091999-11-25 00:26:21 +1100174 };
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);
182 fail = 1;
183 break;
184 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000186 }
Damien Miller95def091999-11-25 00:26:21 +1100187 if (fail) {
Damien Millereba71ba2000-04-29 23:57:08 +1000188 fclose(f);
Ben Lindstrom204e4882001-03-05 06:47:00 +0000189 log("%s", buf);
190 packet_send_debug("%s", buf);
Damien Miller95def091999-11-25 00:26:21 +1100191 restore_uid();
192 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193 }
Damien Miller95def091999-11-25 00:26:21 +1100194 }
195 /* Flag indicating whether authentication has succeeded. */
196 authenticated = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197
Damien Miller450a7a12000-03-26 13:04:51 +1000198 pk = RSA_new();
199 pk->e = BN_new();
200 pk->n = BN_new();
Damien Miller95def091999-11-25 00:26:21 +1100201
Damien Miller5428f641999-11-25 11:54:57 +1100202 /*
203 * Go though the accepted keys, looking for the current key. If
204 * found, perform a challenge-response dialog to verify that the
205 * user really has the corresponding private key.
206 */
Damien Miller95def091999-11-25 00:26:21 +1100207 while (fgets(line, sizeof(line), f)) {
208 char *cp;
209 char *options;
210
211 linenum++;
212
Damien Miller5428f641999-11-25 11:54:57 +1100213 /* Skip leading whitespace, empty and comment lines. */
214 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
215 ;
Damien Miller95def091999-11-25 00:26:21 +1100216 if (!*cp || *cp == '\n' || *cp == '#')
217 continue;
218
Damien Miller5428f641999-11-25 11:54:57 +1100219 /*
220 * Check if there are options for this key, and if so,
221 * save their starting address and skip the option part
222 * for now. If there are no options, set the starting
223 * address to NULL.
224 */
Damien Miller95def091999-11-25 00:26:21 +1100225 if (*cp < '0' || *cp > '9') {
226 int quoted = 0;
227 options = cp;
228 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
229 if (*cp == '\\' && cp[1] == '"')
230 cp++; /* Skip both */
231 else if (*cp == '"')
232 quoted = !quoted;
233 }
234 } else
235 options = NULL;
236
237 /* Parse the key from the line. */
Damien Miller450a7a12000-03-26 13:04:51 +1000238 if (!auth_rsa_read_key(&cp, &bits, pk->e, pk->n)) {
Damien Miller95def091999-11-25 00:26:21 +1100239 debug("%.100s, line %lu: bad key syntax",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000240 file, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100241 packet_send_debug("%.100s, line %lu: bad key syntax",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000242 file, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100243 continue;
244 }
245 /* cp now points to the comment part. */
246
Damien Miller95def091999-11-25 00:26:21 +1100247 /* Check if the we have found the desired key (identified by its modulus). */
Damien Miller450a7a12000-03-26 13:04:51 +1000248 if (BN_cmp(pk->n, client_n) != 0)
Damien Miller5428f641999-11-25 11:54:57 +1100249 continue;
Damien Miller95def091999-11-25 00:26:21 +1100250
Damien Milleraae6c611999-12-06 11:47:28 +1100251 /* check the real bits */
Damien Miller450a7a12000-03-26 13:04:51 +1000252 if (bits != BN_num_bits(pk->n))
Damien Milleraae6c611999-12-06 11:47:28 +1100253 log("Warning: %s, line %ld: keysize mismatch: "
254 "actual %d vs. announced %d.",
Damien Miller450a7a12000-03-26 13:04:51 +1000255 file, linenum, BN_num_bits(pk->n), bits);
Damien Milleraae6c611999-12-06 11:47:28 +1100256
Damien Miller95def091999-11-25 00:26:21 +1100257 /* We have found the desired key. */
Ben Lindstrom14920292000-11-21 21:24:55 +0000258 /*
259 * If our options do not allow this key to be used,
260 * do not send challenge.
261 */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000262 if (!auth_parse_options(pw, options, file, linenum))
Ben Lindstrom14920292000-11-21 21:24:55 +0000263 continue;
Damien Miller95def091999-11-25 00:26:21 +1100264
265 /* Perform the challenge-response dialog for this key. */
Damien Miller450a7a12000-03-26 13:04:51 +1000266 if (!auth_rsa_challenge_dialog(pk)) {
Damien Miller95def091999-11-25 00:26:21 +1100267 /* Wrong response. */
268 verbose("Wrong response to RSA authentication challenge.");
269 packet_send_debug("Wrong response to RSA authentication challenge.");
270 continue;
271 }
Damien Miller5428f641999-11-25 11:54:57 +1100272 /*
273 * Correct response. The client has been successfully
274 * authenticated. Note that we have not yet processed the
275 * options; this will be reset if the options cause the
276 * authentication to be rejected.
Damien Miller5428f641999-11-25 11:54:57 +1100277 * Break out of the loop if authentication was successful;
278 * otherwise continue searching.
279 */
Damien Miller50a41ed2000-10-16 12:14:42 +1100280 authenticated = 1;
281 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282 }
283
Damien Miller95def091999-11-25 00:26:21 +1100284 /* Restore the privileged uid. */
285 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286
Damien Miller95def091999-11-25 00:26:21 +1100287 /* Close the file. */
288 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000289
Damien Miller450a7a12000-03-26 13:04:51 +1000290 RSA_free(pk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291
Damien Miller95def091999-11-25 00:26:21 +1100292 if (authenticated)
293 packet_send_debug("RSA authentication accepted.");
Damien Miller874d77b2000-10-14 16:23:11 +1100294 else
295 auth_clear_options();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296
Damien Miller95def091999-11-25 00:26:21 +1100297 /* Return authentication result. */
298 return authenticated;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299}