blob: 5846a0662c9fc38132ef1bc839cccc8a732dce98 [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 Miller89681212001-12-21 12:52:39 +110017RCSID("$OpenBSD: auth-rsa.c,v 1.46 2001/12/18 10:06:24 jakob 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 Miller95def091999-11-25 00:26:21 +110069 int plen, len;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070
Damien Miller95def091999-11-25 00:26:21 +110071 encrypted_challenge = BN_new();
72 challenge = BN_new();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100073
Damien Miller95def091999-11-25 00:26:21 +110074 /* Generate a random challenge. */
75 BN_rand(challenge, 256, 0, 0);
Damien Miller98c7ad62000-03-09 21:27:49 +110076 ctx = BN_CTX_new();
Damien Miller450a7a12000-03-26 13:04:51 +100077 BN_mod(challenge, challenge, pk->n, ctx);
Damien Miller98c7ad62000-03-09 21:27:49 +110078 BN_CTX_free(ctx);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100079
Damien Miller95def091999-11-25 00:26:21 +110080 /* Encrypt the challenge with the public key. */
81 rsa_public_encrypt(encrypted_challenge, challenge, pk);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100082
Damien Miller95def091999-11-25 00:26:21 +110083 /* Send the encrypted challenge to the client. */
84 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
85 packet_put_bignum(encrypted_challenge);
86 packet_send();
Damien Miller98c7ad62000-03-09 21:27:49 +110087 BN_clear_free(encrypted_challenge);
Damien Miller95def091999-11-25 00:26:21 +110088 packet_write_wait();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
Damien Miller98c7ad62000-03-09 21:27:49 +110090 /* Wait for a response. */
91 packet_read_expect(&plen, SSH_CMSG_AUTH_RSA_RESPONSE);
92 packet_integrity_check(plen, 16, SSH_CMSG_AUTH_RSA_RESPONSE);
93 for (i = 0; i < 16; i++)
94 response[i] = packet_get_char();
95
Damien Miller95def091999-11-25 00:26:21 +110096 /* The response is MD5 of decrypted challenge plus session id. */
97 len = BN_num_bytes(challenge);
98 if (len <= 0 || len > 32)
99 fatal("auth_rsa_challenge_dialog: bad challenge length %d", len);
100 memset(buf, 0, 32);
101 BN_bn2bin(challenge, buf + 32 - len);
102 MD5_Init(&md);
103 MD5_Update(&md, buf, 32);
104 MD5_Update(&md, session_id, 16);
105 MD5_Final(mdbuf, &md);
Damien Miller95def091999-11-25 00:26:21 +1100106 BN_clear_free(challenge);
Damien Miller95def091999-11-25 00:26:21 +1100107
108 /* Verify that the response is the original challenge. */
109 if (memcmp(response, mdbuf, 16) != 0) {
110 /* Wrong answer. */
111 return 0;
112 }
113 /* Correct answer. */
114 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115}
116
Damien Miller5428f641999-11-25 11:54:57 +1100117/*
118 * Performs the RSA authentication dialog with the client. This returns
119 * 0 if the client could not be authenticated, and 1 if authentication was
120 * successful. This may exit if there is a serious protocol violation.
121 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000122
123int
Damien Miller6d7b2cd1999-11-12 15:19:27 +1100124auth_rsa(struct passwd *pw, BIGNUM *client_n)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000125{
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000126 char line[8192], *file;
Damien Miller95def091999-11-25 00:26:21 +1100127 int authenticated;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000128 u_int bits;
Damien Miller95def091999-11-25 00:26:21 +1100129 FILE *f;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000130 u_long linenum = 0;
Damien Miller95def091999-11-25 00:26:21 +1100131 struct stat st;
Damien Miller89681212001-12-21 12:52:39 +1100132 Key *key;
133 char *fp;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000134
Damien Miller874d77b2000-10-14 16:23:11 +1100135 /* no user given */
136 if (pw == NULL)
137 return 0;
138
Damien Miller95def091999-11-25 00:26:21 +1100139 /* Temporarily use the user's uid. */
Ben Lindstrom3fcf1a22001-04-08 18:26:59 +0000140 temporarily_use_uid(pw);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141
Damien Miller95def091999-11-25 00:26:21 +1100142 /* The authorized keys. */
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000143 file = authorized_keys_file(pw);
144 debug("trying public RSA key file %s", file);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000145
Damien Miller95def091999-11-25 00:26:21 +1100146 /* Fail quietly if file does not exist */
147 if (stat(file, &st) < 0) {
148 /* Restore the privileged uid. */
149 restore_uid();
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000150 xfree(file);
Damien Miller95def091999-11-25 00:26:21 +1100151 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000152 }
Damien Miller95def091999-11-25 00:26:21 +1100153 /* Open the file containing the authorized keys. */
154 f = fopen(file, "r");
155 if (!f) {
156 /* Restore the privileged uid. */
157 restore_uid();
158 packet_send_debug("Could not open %.900s for reading.", file);
159 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 +0000160 xfree(file);
Damien Miller95def091999-11-25 00:26:21 +1100161 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162 }
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000163 if (options.strict_modes &&
Ben Lindstrom248c0782001-07-04 03:40:39 +0000164 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000165 xfree(file);
166 fclose(f);
167 log("Authentication refused: %s", line);
168 packet_send_debug("Authentication refused: %s", line);
169 restore_uid();
170 return 0;
Damien Miller95def091999-11-25 00:26:21 +1100171 }
172 /* Flag indicating whether authentication has succeeded. */
173 authenticated = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000174
Damien Miller89681212001-12-21 12:52:39 +1100175 key = key_new(KEY_RSA1);
Damien Miller95def091999-11-25 00:26:21 +1100176
Damien Miller5428f641999-11-25 11:54:57 +1100177 /*
178 * Go though the accepted keys, looking for the current key. If
179 * found, perform a challenge-response dialog to verify that the
180 * user really has the corresponding private key.
181 */
Damien Miller95def091999-11-25 00:26:21 +1100182 while (fgets(line, sizeof(line), f)) {
183 char *cp;
184 char *options;
185
186 linenum++;
187
Damien Miller5428f641999-11-25 11:54:57 +1100188 /* Skip leading whitespace, empty and comment lines. */
189 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
190 ;
Damien Miller95def091999-11-25 00:26:21 +1100191 if (!*cp || *cp == '\n' || *cp == '#')
192 continue;
193
Damien Miller5428f641999-11-25 11:54:57 +1100194 /*
195 * Check if there are options for this key, and if so,
196 * save their starting address and skip the option part
197 * for now. If there are no options, set the starting
198 * address to NULL.
199 */
Damien Miller95def091999-11-25 00:26:21 +1100200 if (*cp < '0' || *cp > '9') {
201 int quoted = 0;
202 options = cp;
203 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
204 if (*cp == '\\' && cp[1] == '"')
205 cp++; /* Skip both */
206 else if (*cp == '"')
207 quoted = !quoted;
208 }
209 } else
210 options = NULL;
211
212 /* Parse the key from the line. */
Damien Miller89681212001-12-21 12:52:39 +1100213 if (hostfile_read_key(&cp, &bits, key) == 0) {
Ben Lindstromf96704d2001-06-25 04:17:12 +0000214 debug("%.100s, line %lu: non ssh1 key syntax",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000215 file, linenum);
Damien Miller95def091999-11-25 00:26:21 +1100216 continue;
217 }
218 /* cp now points to the comment part. */
219
Damien Miller95def091999-11-25 00:26:21 +1100220 /* Check if the we have found the desired key (identified by its modulus). */
Damien Miller89681212001-12-21 12:52:39 +1100221 if (BN_cmp(key->rsa->n, client_n) != 0)
Damien Miller5428f641999-11-25 11:54:57 +1100222 continue;
Damien Miller95def091999-11-25 00:26:21 +1100223
Damien Milleraae6c611999-12-06 11:47:28 +1100224 /* check the real bits */
Damien Miller89681212001-12-21 12:52:39 +1100225 if (bits != BN_num_bits(key->rsa->n))
Ben Lindstrom940fb862001-08-06 21:01:49 +0000226 log("Warning: %s, line %lu: keysize mismatch: "
Damien Milleraae6c611999-12-06 11:47:28 +1100227 "actual %d vs. announced %d.",
Damien Miller89681212001-12-21 12:52:39 +1100228 file, linenum, BN_num_bits(key->rsa->n), bits);
Damien Milleraae6c611999-12-06 11:47:28 +1100229
Damien Miller95def091999-11-25 00:26:21 +1100230 /* We have found the desired key. */
Ben Lindstrom14920292000-11-21 21:24:55 +0000231 /*
232 * If our options do not allow this key to be used,
233 * do not send challenge.
234 */
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000235 if (!auth_parse_options(pw, options, file, linenum))
Ben Lindstrom14920292000-11-21 21:24:55 +0000236 continue;
Damien Miller95def091999-11-25 00:26:21 +1100237
238 /* Perform the challenge-response dialog for this key. */
Damien Miller89681212001-12-21 12:52:39 +1100239 if (!auth_rsa_challenge_dialog(key->rsa)) {
Damien Miller95def091999-11-25 00:26:21 +1100240 /* Wrong response. */
241 verbose("Wrong response to RSA authentication challenge.");
242 packet_send_debug("Wrong response to RSA authentication challenge.");
Ben Lindstrom57fe5b52001-12-06 17:41:25 +0000243 /*
244 * Break out of the loop. Otherwise we might send
245 * another challenge and break the protocol.
246 */
247 break;
Damien Miller95def091999-11-25 00:26:21 +1100248 }
Damien Miller5428f641999-11-25 11:54:57 +1100249 /*
250 * Correct response. The client has been successfully
251 * authenticated. Note that we have not yet processed the
252 * options; this will be reset if the options cause the
253 * authentication to be rejected.
Damien Miller5428f641999-11-25 11:54:57 +1100254 * Break out of the loop if authentication was successful;
255 * otherwise continue searching.
256 */
Damien Miller50a41ed2000-10-16 12:14:42 +1100257 authenticated = 1;
Damien Miller89681212001-12-21 12:52:39 +1100258
259 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
260 verbose("Found matching %s key: %s",
261 key_type(key), fp);
262 xfree(fp);
263
Damien Miller50a41ed2000-10-16 12:14:42 +1100264 break;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000265 }
266
Damien Miller95def091999-11-25 00:26:21 +1100267 /* Restore the privileged uid. */
268 restore_uid();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000269
Damien Miller95def091999-11-25 00:26:21 +1100270 /* Close the file. */
Ben Lindstrombfb3a0e2001-06-05 20:25:05 +0000271 xfree(file);
Damien Miller95def091999-11-25 00:26:21 +1100272 fclose(f);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000273
Damien Miller89681212001-12-21 12:52:39 +1100274 key_free(key);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000275
Damien Miller95def091999-11-25 00:26:21 +1100276 if (authenticated)
277 packet_send_debug("RSA authentication accepted.");
Damien Miller874d77b2000-10-14 16:23:11 +1100278 else
279 auth_clear_options();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280
Damien Miller95def091999-11-25 00:26:21 +1100281 /* Return authentication result. */
282 return authenticated;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000283}