blob: f7ae03cfdaf13009c5b28b2e5fc6d5dd92a86aa6 [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"
Damien Millerdff50992002-01-22 23:16:32 +110017RCSID("$OpenBSD: auth-rsa.c,v 1.50 2001/12/28 14:50:54 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 Miller89681212001-12-21 12:52:39 +110034#include "hostfile.h"
Damien Miller874d77b2000-10-14 16:23:11 +110035
36/* import */
37extern ServerOptions options;
38
Damien Miller5428f641999-11-25 11:54:57 +110039/*
40 * Session identifier that is used to bind key exchange and authentication
41 * responses to a particular session.
42 */
Ben Lindstrom46c16222000-12-22 01:43:59 +000043extern u_char session_id[16];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
Damien Miller5428f641999-11-25 11:54:57 +110045/*
46 * The .ssh/authorized_keys file contains public keys, one per line, in the
47 * following format:
48 * options bits e n comment
49 * where bits, e and n are decimal numbers,
50 * and comment is any string of characters up to newline. The maximum
51 * length of a line is 8000 characters. See the documentation for a
52 * description of the options.
53 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100054
Damien Miller5428f641999-11-25 11:54:57 +110055/*
56 * Performs the RSA authentication challenge-response dialog with the client,
57 * and returns true (non-zero) if the client gave the correct answer to
58 * our challenge; returns zero if the client gives a wrong answer.
59 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100060
61int
Damien Miller450a7a12000-03-26 13:04:51 +100062auth_rsa_challenge_dialog(RSA *pk)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063{
Damien Miller98c7ad62000-03-09 21:27:49 +110064 BIGNUM *challenge, *encrypted_challenge;
Damien Miller98c7ad62000-03-09 21:27:49 +110065 BN_CTX *ctx;
Ben Lindstrom46c16222000-12-22 01:43:59 +000066 u_char buf[32], mdbuf[16], response[16];
Damien Miller95def091999-11-25 00:26:21 +110067 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +000068 u_int i;
Damien Millerdff50992002-01-22 23:16:32 +110069 int len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Damien Millerda755162002-01-22 23:09:22 +110071 if ((encrypted_challenge = BN_new()) == NULL)
72 fatal("auth_rsa_challenge_dialog: BN_new() failed");
73 if ((challenge = BN_new()) == NULL)
74 fatal("auth_rsa_challenge_dialog: BN_new() failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075
Damien Miller95def091999-11-25 00:26:21 +110076 /* Generate a random challenge. */
77 BN_rand(challenge, 256, 0, 0);
Damien Millerda755162002-01-22 23:09:22 +110078 if ((ctx = BN_CTX_new()) == NULL)
79 fatal("auth_rsa_challenge_dialog: BN_CTX_new() failed");
Damien Miller450a7a12000-03-26 13:04:51 +100080 BN_mod(challenge, challenge, pk->n, ctx);
Damien Miller98c7ad62000-03-09 21:27:49 +110081 BN_CTX_free(ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller95def091999-11-25 00:26:21 +110083 /* Encrypt the challenge with the public key. */
84 rsa_public_encrypt(encrypted_challenge, challenge, pk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100085
Damien Miller95def091999-11-25 00:26:21 +110086 /* Send the encrypted challenge to the client. */
87 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
88 packet_put_bignum(encrypted_challenge);
89 packet_send();
Damien Miller98c7ad62000-03-09 21:27:49 +110090 BN_clear_free(encrypted_challenge);
Damien Miller95def091999-11-25 00:26:21 +110091 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Miller98c7ad62000-03-09 21:27:49 +110093 /* Wait for a response. */
Damien Millerdff50992002-01-22 23:16:32 +110094 packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE);
Damien Miller98c7ad62000-03-09 21:27:49 +110095 for (i = 0; i < 16; i++)
96 response[i] = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +110097 packet_check_eom();
Damien Miller98c7ad62000-03-09 21:27:49 +110098
Damien Miller95def091999-11-25 00:26:21 +110099 /* The response is MD5 of decrypted challenge plus session id. */
100 len = BN_num_bytes(challenge);
101 if (len <= 0 || len > 32)
102 fatal("auth_rsa_challenge_dialog: bad challenge length %d", len);
103 memset(buf, 0, 32);
104 BN_bn2bin(challenge, buf + 32 - len);
105 MD5_Init(&md);
106 MD5_Update(&md, buf, 32);
107 MD5_Update(&md, session_id, 16);
108 MD5_Final(mdbuf, &md);
Damien Miller95def091999-11-25 00:26:21 +1100109 BN_clear_free(challenge);
Damien Miller95def091999-11-25 00:26:21 +1100110
111 /* Verify that the response is the original challenge. */
112 if (memcmp(response, mdbuf, 16) != 0) {
113 /* Wrong answer. */
114 return 0;
115 }
116 /* Correct answer. */
117 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118}
119
Damien Miller5428f641999-11-25 11:54:57 +1100120/*
121 * Performs the RSA authentication dialog with the client. This returns
122 * 0 if the client could not be authenticated, and 1 if authentication was
123 * successful. This may exit if there is a serious protocol violation.
124 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125
126int
Damien Miller6d7b2cd1999-11-12 15:19:27 +1100127auth_rsa(struct passwd *pw, BIGNUM *client_n)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000128{
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000129 char line[8192], *file;
Damien Miller95def091999-11-25 00:26:21 +1100130 int authenticated;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000131 u_int bits;
Damien Miller95def091999-11-25 00:26:21 +1100132 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000133 u_long linenum = 0;
Damien Miller95def091999-11-25 00:26:21 +1100134 struct stat st;
Damien Miller89681212001-12-21 12:52:39 +1100135 Key *key;
136 char *fp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137
Damien Miller874d77b2000-10-14 16:23:11 +1100138 /* no user given */
139 if (pw == NULL)
140 return 0;
141
Damien Miller95def091999-11-25 00:26:21 +1100142 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000143 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller95def091999-11-25 00:26:21 +1100145 /* The authorized keys. */
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000146 file = authorized_keys_file(pw);
147 debug("trying public RSA key file %s", file);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000148
Damien Miller95def091999-11-25 00:26:21 +1100149 /* Fail quietly if file does not exist */
150 if (stat(file, &st) < 0) {
151 /* Restore the privileged uid. */
152 restore_uid();
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000153 xfree(file);
Damien Miller95def091999-11-25 00:26:21 +1100154 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000155 }
Damien Miller95def091999-11-25 00:26:21 +1100156 /* Open the file containing the authorized keys. */
157 f = fopen(file, "r");
158 if (!f) {
159 /* Restore the privileged uid. */
160 restore_uid();
161 packet_send_debug("Could not open %.900s for reading.", file);
162 packet_send_debug("If your home is on an NFS volume, it may need to be world-readable.");
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000163 xfree(file);
Damien Miller95def091999-11-25 00:26:21 +1100164 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000166 if (options.strict_modes &&
Ben Lindstrom248c0782001-07-04 03:40:39 +0000167 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000168 xfree(file);
169 fclose(f);
170 log("Authentication refused: %s", line);
171 packet_send_debug("Authentication refused: %s", line);
172 restore_uid();
173 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100174 }
175 /* Flag indicating whether authentication has succeeded. */
176 authenticated = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000177
Damien Miller89681212001-12-21 12:52:39 +1100178 key = key_new(KEY_RSA1);
Damien Miller95def091999-11-25 00:26:21 +1100179
Damien Miller5428f641999-11-25 11:54:57 +1100180 /*
181 * Go though the accepted keys, looking for the current key. If
182 * found, perform a challenge-response dialog to verify that the
183 * user really has the corresponding private key.
184 */
Damien Miller95def091999-11-25 00:26:21 +1100185 while (fgets(line, sizeof(line), f)) {
186 char *cp;
187 char *options;
188
189 linenum++;
190
Damien Miller5428f641999-11-25 11:54:57 +1100191 /* Skip leading whitespace, empty and comment lines. */
192 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
193 ;
Damien Miller95def091999-11-25 00:26:21 +1100194 if (!*cp || *cp == '\n' || *cp == '#')
195 continue;
196
Damien Miller5428f641999-11-25 11:54:57 +1100197 /*
198 * Check if there are options for this key, and if so,
199 * save their starting address and skip the option part
200 * for now. If there are no options, set the starting
201 * address to NULL.
202 */
Damien Miller95def091999-11-25 00:26:21 +1100203 if (*cp < '0' || *cp > '9') {
204 int quoted = 0;
205 options = cp;
206 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
207 if (*cp == '\\' && cp[1] == '"')
208 cp++; /* Skip both */
209 else if (*cp == '"')
210 quoted = !quoted;
211 }
212 } else
213 options = NULL;
214
215 /* Parse the key from the line. */
Damien Miller89681212001-12-21 12:52:39 +1100216 if (hostfile_read_key(&cp, &bits, key) == 0) {
Ben Lindstromf96704d2001-06-25 04:17:12 +0000217 debug("%.100s, line %lu: non ssh1 key syntax",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000218 file, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100219 continue;
220 }
221 /* cp now points to the comment part. */
222
Damien Miller95def091999-11-25 00:26:21 +1100223 /* Check if the we have found the desired key (identified by its modulus). */
Damien Miller89681212001-12-21 12:52:39 +1100224 if (BN_cmp(key->rsa->n, client_n) != 0)
Damien Miller5428f641999-11-25 11:54:57 +1100225 continue;
Damien Miller95def091999-11-25 00:26:21 +1100226
Damien Milleraae6c611999-12-06 11:47:28 +1100227 /* check the real bits */
Damien Miller89681212001-12-21 12:52:39 +1100228 if (bits != BN_num_bits(key->rsa->n))
Ben Lindstrom940fb862001-08-06 21:01:49 +0000229 log("Warning: %s, line %lu: keysize mismatch: "
Damien Milleraae6c611999-12-06 11:47:28 +1100230 "actual %d vs. announced %d.",
Damien Miller89681212001-12-21 12:52:39 +1100231 file, linenum, BN_num_bits(key->rsa->n), bits);
Damien Milleraae6c611999-12-06 11:47:28 +1100232
Damien Miller95def091999-11-25 00:26:21 +1100233 /* We have found the desired key. */
Ben Lindstrom14920292000-11-21 21:24:55 +0000234 /*
235 * If our options do not allow this key to be used,
236 * do not send challenge.
237 */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000238 if (!auth_parse_options(pw, options, file, linenum))
Ben Lindstrom14920292000-11-21 21:24:55 +0000239 continue;
Damien Miller95def091999-11-25 00:26:21 +1100240
241 /* Perform the challenge-response dialog for this key. */
Damien Miller89681212001-12-21 12:52:39 +1100242 if (!auth_rsa_challenge_dialog(key->rsa)) {
Damien Miller95def091999-11-25 00:26:21 +1100243 /* Wrong response. */
244 verbose("Wrong response to RSA authentication challenge.");
245 packet_send_debug("Wrong response to RSA authentication challenge.");
Ben Lindstrom57fe5b52001-12-06 17:41:25 +0000246 /*
247 * Break out of the loop. Otherwise we might send
248 * another challenge and break the protocol.
249 */
250 break;
Damien Miller95def091999-11-25 00:26:21 +1100251 }
Damien Miller5428f641999-11-25 11:54:57 +1100252 /*
253 * Correct response. The client has been successfully
254 * authenticated. Note that we have not yet processed the
255 * options; this will be reset if the options cause the
256 * authentication to be rejected.
Damien Miller5428f641999-11-25 11:54:57 +1100257 * Break out of the loop if authentication was successful;
258 * otherwise continue searching.
259 */
Damien Miller50a41ed2000-10-16 12:14:42 +1100260 authenticated = 1;
Damien Miller89681212001-12-21 12:52:39 +1100261
262 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
263 verbose("Found matching %s key: %s",
264 key_type(key), fp);
265 xfree(fp);
266
Damien Miller50a41ed2000-10-16 12:14:42 +1100267 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000268 }
269
Damien Miller95def091999-11-25 00:26:21 +1100270 /* Restore the privileged uid. */
271 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000272
Damien Miller95def091999-11-25 00:26:21 +1100273 /* Close the file. */
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000274 xfree(file);
Damien Miller95def091999-11-25 00:26:21 +1100275 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000276
Damien Miller89681212001-12-21 12:52:39 +1100277 key_free(key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000278
Damien Miller95def091999-11-25 00:26:21 +1100279 if (authenticated)
280 packet_send_debug("RSA authentication accepted.");
Damien Miller874d77b2000-10-14 16:23:11 +1100281 else
282 auth_clear_options();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283
Damien Miller95def091999-11-25 00:26:21 +1100284 /* Return authentication result. */
285 return authenticated;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000286}