blob: ff9bf3b64207d5cd34dbf7e0f245d2b19fb03e9b [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 Lindstrom9c8aefe2002-03-22 01:12:58 +000017RCSID("$OpenBSD: auth-rsa.c,v 1.51 2002/03/14 16:56:33 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
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +000055static BIGNUM *
56auth_rsa_generate_challenge(Key *key)
57{
58 BIGNUM *challenge;
59 BN_CTX *ctx;
60
61 if ((challenge = BN_new()) == NULL)
62 fatal("auth_rsa_generate_challenge: BN_new() failed");
63 /* Generate a random challenge. */
64 BN_rand(challenge, 256, 0, 0);
65 if ((ctx = BN_CTX_new()) == NULL)
66 fatal("auth_rsa_generate_challenge: BN_CTX_new() failed");
67 BN_mod(challenge, challenge, key->rsa->n, ctx);
68 BN_CTX_free(ctx);
69
70 return challenge;
71}
72
73static int
74auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16])
75{
76 u_char buf[32], mdbuf[16];
77 MD5_CTX md;
78 int len;
79
80 /* The response is MD5 of decrypted challenge plus session id. */
81 len = BN_num_bytes(challenge);
82 if (len <= 0 || len > 32)
83 fatal("auth_rsa_verify_response: bad challenge length %d", len);
84 memset(buf, 0, 32);
85 BN_bn2bin(challenge, buf + 32 - len);
86 MD5_Init(&md);
87 MD5_Update(&md, buf, 32);
88 MD5_Update(&md, session_id, 16);
89 MD5_Final(mdbuf, &md);
90
91 /* Verify that the response is the original challenge. */
92 if (memcmp(response, mdbuf, 16) != 0) {
93 /* Wrong answer. */
94 return (0);
95 }
96 /* Correct answer. */
97 return (1);
98}
99
Damien Miller5428f641999-11-25 11:54:57 +1100100/*
101 * Performs the RSA authentication challenge-response dialog with the client,
102 * and returns true (non-zero) if the client gave the correct answer to
103 * our challenge; returns zero if the client gives a wrong answer.
104 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000105
106int
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000107auth_rsa_challenge_dialog(Key *key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000108{
Damien Miller98c7ad62000-03-09 21:27:49 +1100109 BIGNUM *challenge, *encrypted_challenge;
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000110 u_char response[16];
111 int i, success;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000112
Damien Millerda755162002-01-22 23:09:22 +1100113 if ((encrypted_challenge = BN_new()) == NULL)
114 fatal("auth_rsa_challenge_dialog: BN_new() failed");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000116 challenge = auth_rsa_generate_challenge(key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000117
Damien Miller95def091999-11-25 00:26:21 +1100118 /* Encrypt the challenge with the public key. */
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000119 rsa_public_encrypt(encrypted_challenge, challenge, key->rsa);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000120
Damien Miller95def091999-11-25 00:26:21 +1100121 /* Send the encrypted challenge to the client. */
122 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
123 packet_put_bignum(encrypted_challenge);
124 packet_send();
Damien Miller98c7ad62000-03-09 21:27:49 +1100125 BN_clear_free(encrypted_challenge);
Damien Miller95def091999-11-25 00:26:21 +1100126 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000127
Damien Miller98c7ad62000-03-09 21:27:49 +1100128 /* Wait for a response. */
Damien Millerdff50992002-01-22 23:16:32 +1100129 packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE);
Damien Miller98c7ad62000-03-09 21:27:49 +1100130 for (i = 0; i < 16; i++)
131 response[i] = packet_get_char();
Damien Miller48b03fc2002-01-22 23:11:40 +1100132 packet_check_eom();
Damien Miller98c7ad62000-03-09 21:27:49 +1100133
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000134 success = auth_rsa_verify_response(key, challenge, response);
Damien Miller95def091999-11-25 00:26:21 +1100135 BN_clear_free(challenge);
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000136 return (success);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000137}
138
Damien Miller5428f641999-11-25 11:54:57 +1100139/*
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000140 * check if there's user key matching client_n,
141 * return key if login is allowed, NULL otherwise
Damien Miller5428f641999-11-25 11:54:57 +1100142 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000143
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000144static int
145auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000146{
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000147 char line[8192], *file;
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000148 int allowed;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000149 u_int bits;
Damien Miller95def091999-11-25 00:26:21 +1100150 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000151 u_long linenum = 0;
Damien Miller95def091999-11-25 00:26:21 +1100152 struct stat st;
Damien Miller89681212001-12-21 12:52:39 +1100153 Key *key;
Damien Miller874d77b2000-10-14 16:23:11 +1100154
Damien Miller95def091999-11-25 00:26:21 +1100155 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000156 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000157
Damien Miller95def091999-11-25 00:26:21 +1100158 /* The authorized keys. */
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000159 file = authorized_keys_file(pw);
160 debug("trying public RSA key file %s", file);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000161
Damien Miller95def091999-11-25 00:26:21 +1100162 /* Fail quietly if file does not exist */
163 if (stat(file, &st) < 0) {
164 /* Restore the privileged uid. */
165 restore_uid();
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000166 xfree(file);
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000167 return (NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000168 }
Damien Miller95def091999-11-25 00:26:21 +1100169 /* Open the file containing the authorized keys. */
170 f = fopen(file, "r");
171 if (!f) {
172 /* Restore the privileged uid. */
173 restore_uid();
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000174 xfree(file);
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000175 return (NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000176 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000177 if (options.strict_modes &&
Ben Lindstrom248c0782001-07-04 03:40:39 +0000178 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000179 xfree(file);
180 fclose(f);
181 log("Authentication refused: %s", line);
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000182 restore_uid();
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000183 return (NULL);
Damien Miller95def091999-11-25 00:26:21 +1100184 }
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000185
186 /* Flag indicating whether the key is allowed. */
187 allowed = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000188
Damien Miller89681212001-12-21 12:52:39 +1100189 key = key_new(KEY_RSA1);
Damien Miller95def091999-11-25 00:26:21 +1100190
Damien Miller5428f641999-11-25 11:54:57 +1100191 /*
192 * Go though the accepted keys, looking for the current key. If
193 * found, perform a challenge-response dialog to verify that the
194 * user really has the corresponding private key.
195 */
Damien Miller95def091999-11-25 00:26:21 +1100196 while (fgets(line, sizeof(line), f)) {
197 char *cp;
198 char *options;
199
200 linenum++;
201
Damien Miller5428f641999-11-25 11:54:57 +1100202 /* Skip leading whitespace, empty and comment lines. */
203 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
204 ;
Damien Miller95def091999-11-25 00:26:21 +1100205 if (!*cp || *cp == '\n' || *cp == '#')
206 continue;
207
Damien Miller5428f641999-11-25 11:54:57 +1100208 /*
209 * Check if there are options for this key, and if so,
210 * save their starting address and skip the option part
211 * for now. If there are no options, set the starting
212 * address to NULL.
213 */
Damien Miller95def091999-11-25 00:26:21 +1100214 if (*cp < '0' || *cp > '9') {
215 int quoted = 0;
216 options = cp;
217 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
218 if (*cp == '\\' && cp[1] == '"')
219 cp++; /* Skip both */
220 else if (*cp == '"')
221 quoted = !quoted;
222 }
223 } else
224 options = NULL;
225
226 /* Parse the key from the line. */
Damien Miller89681212001-12-21 12:52:39 +1100227 if (hostfile_read_key(&cp, &bits, key) == 0) {
Ben Lindstromf96704d2001-06-25 04:17:12 +0000228 debug("%.100s, line %lu: non ssh1 key syntax",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000229 file, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100230 continue;
231 }
232 /* cp now points to the comment part. */
233
Damien Miller95def091999-11-25 00:26:21 +1100234 /* Check if the we have found the desired key (identified by its modulus). */
Damien Miller89681212001-12-21 12:52:39 +1100235 if (BN_cmp(key->rsa->n, client_n) != 0)
Damien Miller5428f641999-11-25 11:54:57 +1100236 continue;
Damien Miller95def091999-11-25 00:26:21 +1100237
Damien Milleraae6c611999-12-06 11:47:28 +1100238 /* check the real bits */
Damien Miller89681212001-12-21 12:52:39 +1100239 if (bits != BN_num_bits(key->rsa->n))
Ben Lindstrom940fb862001-08-06 21:01:49 +0000240 log("Warning: %s, line %lu: keysize mismatch: "
Damien Milleraae6c611999-12-06 11:47:28 +1100241 "actual %d vs. announced %d.",
Damien Miller89681212001-12-21 12:52:39 +1100242 file, linenum, BN_num_bits(key->rsa->n), bits);
Damien Milleraae6c611999-12-06 11:47:28 +1100243
Damien Miller95def091999-11-25 00:26:21 +1100244 /* We have found the desired key. */
Ben Lindstrom14920292000-11-21 21:24:55 +0000245 /*
246 * If our options do not allow this key to be used,
247 * do not send challenge.
248 */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000249 if (!auth_parse_options(pw, options, file, linenum))
Ben Lindstrom14920292000-11-21 21:24:55 +0000250 continue;
Damien Miller95def091999-11-25 00:26:21 +1100251
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000252 /* break out, this key is allowed */
253 allowed = 1;
Damien Miller50a41ed2000-10-16 12:14:42 +1100254 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000255 }
256
Damien Miller95def091999-11-25 00:26:21 +1100257 /* Restore the privileged uid. */
258 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000259
Damien Miller95def091999-11-25 00:26:21 +1100260 /* Close the file. */
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000261 xfree(file);
Damien Miller95def091999-11-25 00:26:21 +1100262 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000263
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000264 /* return key if allowed */
265 if (allowed && rkey != NULL)
266 *rkey = key;
267 else
268 key_free(key);
269 return (allowed);
270}
271
272/*
273 * Performs the RSA authentication dialog with the client. This returns
274 * 0 if the client could not be authenticated, and 1 if authentication was
275 * successful. This may exit if there is a serious protocol violation.
276 */
277int
278auth_rsa(struct passwd *pw, BIGNUM *client_n)
279{
280 Key *key;
281 char *fp;
282
283 /* no user given */
284 if (pw == NULL)
285 return 0;
286
287 if (auth_rsa_key_allowed(pw, client_n, &key) == 0) {
288 auth_clear_options();
289 return (0);
290 }
291
292 /* Perform the challenge-response dialog for this key. */
293 if (!auth_rsa_challenge_dialog(key)) {
294 /* Wrong response. */
295 verbose("Wrong response to RSA authentication challenge.");
296 packet_send_debug("Wrong response to RSA authentication challenge.");
297 /*
298 * Break out of the loop. Otherwise we might send
299 * another challenge and break the protocol.
300 */
301 key_free(key);
302 return (0);
303 }
304 /*
305 * Correct response. The client has been successfully
306 * authenticated. Note that we have not yet processed the
307 * options; this will be reset if the options cause the
308 * authentication to be rejected.
309 */
310 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
311 verbose("Found matching %s key: %s",
312 key_type(key), fp);
313 xfree(fp);
Damien Miller89681212001-12-21 12:52:39 +1100314 key_free(key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000315
Ben Lindstrom9c8aefe2002-03-22 01:12:58 +0000316 packet_send_debug("RSA authentication accepted.");
317 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318}