auth-rsa.c revision 181111
1/* $OpenBSD: auth-rsa.c,v 1.73 2008/07/02 12:03:51 dtucker Exp $ */
2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 *                    All rights reserved
6 * RSA-based authentication.  This code determines whether to admit a login
7 * based on RSA authentication.  This file also contains functions to check
8 * validity of the host key.
9 *
10 * As far as I am concerned, the code I have written for this software
11 * can be used freely for any purpose.  Any derived versions of this
12 * software must be clearly marked as such, and if the derived work is
13 * incompatible with the protocol description in the RFC file, it must be
14 * called by a name other than "ssh" or "Secure Shell".
15 */
16
17#include "includes.h"
18
19#include <sys/types.h>
20#include <sys/stat.h>
21
22#include <openssl/rsa.h>
23#include <openssl/md5.h>
24
25#include <pwd.h>
26#include <stdio.h>
27#include <stdarg.h>
28#include <string.h>
29
30#include "xmalloc.h"
31#include "rsa.h"
32#include "packet.h"
33#include "ssh1.h"
34#include "uidswap.h"
35#include "match.h"
36#include "buffer.h"
37#include "auth-options.h"
38#include "pathnames.h"
39#include "log.h"
40#include "servconf.h"
41#include "key.h"
42#include "hostfile.h"
43#include "auth.h"
44#ifdef GSSAPI
45#include "ssh-gss.h"
46#endif
47#include "monitor_wrap.h"
48#include "ssh.h"
49#include "misc.h"
50
51/* import */
52extern ServerOptions options;
53
54/*
55 * Session identifier that is used to bind key exchange and authentication
56 * responses to a particular session.
57 */
58extern u_char session_id[16];
59
60/*
61 * The .ssh/authorized_keys file contains public keys, one per line, in the
62 * following format:
63 *   options bits e n comment
64 * where bits, e and n are decimal numbers,
65 * and comment is any string of characters up to newline.  The maximum
66 * length of a line is SSH_MAX_PUBKEY_BYTES characters.  See sshd(8) for a
67 * description of the options.
68 */
69
70BIGNUM *
71auth_rsa_generate_challenge(Key *key)
72{
73	BIGNUM *challenge;
74	BN_CTX *ctx;
75
76	if ((challenge = BN_new()) == NULL)
77		fatal("auth_rsa_generate_challenge: BN_new() failed");
78	/* Generate a random challenge. */
79	if (BN_rand(challenge, 256, 0, 0) == 0)
80		fatal("auth_rsa_generate_challenge: BN_rand failed");
81	if ((ctx = BN_CTX_new()) == NULL)
82		fatal("auth_rsa_generate_challenge: BN_CTX_new failed");
83	if (BN_mod(challenge, challenge, key->rsa->n, ctx) == 0)
84		fatal("auth_rsa_generate_challenge: BN_mod failed");
85	BN_CTX_free(ctx);
86
87	return challenge;
88}
89
90int
91auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16])
92{
93	u_char buf[32], mdbuf[16];
94	MD5_CTX md;
95	int len;
96
97	/* don't allow short keys */
98	if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
99		error("auth_rsa_verify_response: RSA modulus too small: %d < minimum %d bits",
100		    BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
101		return (0);
102	}
103
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_verify_response: bad challenge length %d", len);
108	memset(buf, 0, 32);
109	BN_bn2bin(challenge, buf + 32 - len);
110	MD5_Init(&md);
111	MD5_Update(&md, buf, 32);
112	MD5_Update(&md, session_id, 16);
113	MD5_Final(mdbuf, &md);
114
115	/* Verify that the response is the original challenge. */
116	if (memcmp(response, mdbuf, 16) != 0) {
117		/* Wrong answer. */
118		return (0);
119	}
120	/* Correct answer. */
121	return (1);
122}
123
124/*
125 * Performs the RSA authentication challenge-response dialog with the client,
126 * and returns true (non-zero) if the client gave the correct answer to
127 * our challenge; returns zero if the client gives a wrong answer.
128 */
129
130int
131auth_rsa_challenge_dialog(Key *key)
132{
133	BIGNUM *challenge, *encrypted_challenge;
134	u_char response[16];
135	int i, success;
136
137	if ((encrypted_challenge = BN_new()) == NULL)
138		fatal("auth_rsa_challenge_dialog: BN_new() failed");
139
140	challenge = PRIVSEP(auth_rsa_generate_challenge(key));
141
142	/* Encrypt the challenge with the public key. */
143	rsa_public_encrypt(encrypted_challenge, challenge, key->rsa);
144
145	/* Send the encrypted challenge to the client. */
146	packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
147	packet_put_bignum(encrypted_challenge);
148	packet_send();
149	BN_clear_free(encrypted_challenge);
150	packet_write_wait();
151
152	/* Wait for a response. */
153	packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE);
154	for (i = 0; i < 16; i++)
155		response[i] = (u_char)packet_get_char();
156	packet_check_eom();
157
158	success = PRIVSEP(auth_rsa_verify_response(key, challenge, response));
159	BN_clear_free(challenge);
160	return (success);
161}
162
163/*
164 * check if there's user key matching client_n,
165 * return key if login is allowed, NULL otherwise
166 */
167
168int
169auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
170{
171	char line[SSH_MAX_PUBKEY_BYTES], *file;
172	int allowed = 0;
173	u_int bits;
174	FILE *f;
175	u_long linenum = 0;
176	Key *key;
177
178	/* Temporarily use the user's uid. */
179	temporarily_use_uid(pw);
180
181	/* The authorized keys. */
182	file = authorized_keys_file(pw);
183	debug("trying public RSA key file %s", file);
184	f = auth_openkeyfile(file, pw, options.strict_modes);
185	if (!f) {
186		xfree(file);
187		restore_uid();
188		return (0);
189	}
190
191	/* Flag indicating whether the key is allowed. */
192	allowed = 0;
193
194	key = key_new(KEY_RSA1);
195
196	/*
197	 * Go though the accepted keys, looking for the current key.  If
198	 * found, perform a challenge-response dialog to verify that the
199	 * user really has the corresponding private key.
200	 */
201	while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
202		char *cp;
203		char *key_options;
204		int keybits;
205
206		/* Skip leading whitespace, empty and comment lines. */
207		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
208			;
209		if (!*cp || *cp == '\n' || *cp == '#')
210			continue;
211
212		/*
213		 * Check if there are options for this key, and if so,
214		 * save their starting address and skip the option part
215		 * for now.  If there are no options, set the starting
216		 * address to NULL.
217		 */
218		if (*cp < '0' || *cp > '9') {
219			int quoted = 0;
220			key_options = cp;
221			for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
222				if (*cp == '\\' && cp[1] == '"')
223					cp++;	/* Skip both */
224				else if (*cp == '"')
225					quoted = !quoted;
226			}
227		} else
228			key_options = NULL;
229
230		/* Parse the key from the line. */
231		if (hostfile_read_key(&cp, &bits, key) == 0) {
232			debug("%.100s, line %lu: non ssh1 key syntax",
233			    file, linenum);
234			continue;
235		}
236		/* cp now points to the comment part. */
237
238		/* Check if the we have found the desired key (identified by its modulus). */
239		if (BN_cmp(key->rsa->n, client_n) != 0)
240			continue;
241
242		/* check the real bits  */
243		keybits = BN_num_bits(key->rsa->n);
244		if (keybits < 0 || bits != (u_int)keybits)
245			logit("Warning: %s, line %lu: keysize mismatch: "
246			    "actual %d vs. announced %d.",
247			    file, linenum, BN_num_bits(key->rsa->n), bits);
248
249		/* We have found the desired key. */
250		/*
251		 * If our options do not allow this key to be used,
252		 * do not send challenge.
253		 */
254		if (!auth_parse_options(pw, key_options, file, linenum))
255			continue;
256
257		/* break out, this key is allowed */
258		allowed = 1;
259		break;
260	}
261
262	/* Restore the privileged uid. */
263	restore_uid();
264
265	/* Close the file. */
266	xfree(file);
267	fclose(f);
268
269	/* return key if allowed */
270	if (allowed && rkey != NULL)
271		*rkey = key;
272	else
273		key_free(key);
274	return (allowed);
275}
276
277/*
278 * Performs the RSA authentication dialog with the client.  This returns
279 * 0 if the client could not be authenticated, and 1 if authentication was
280 * successful.  This may exit if there is a serious protocol violation.
281 */
282int
283auth_rsa(Authctxt *authctxt, BIGNUM *client_n)
284{
285	Key *key;
286	char *fp;
287	struct passwd *pw = authctxt->pw;
288
289	/* no user given */
290	if (!authctxt->valid)
291		return 0;
292
293	if (!PRIVSEP(auth_rsa_key_allowed(pw, client_n, &key))) {
294		auth_clear_options();
295		return (0);
296	}
297
298	/* Perform the challenge-response dialog for this key. */
299	if (!auth_rsa_challenge_dialog(key)) {
300		/* Wrong response. */
301		verbose("Wrong response to RSA authentication challenge.");
302		packet_send_debug("Wrong response to RSA authentication challenge.");
303		/*
304		 * Break out of the loop. Otherwise we might send
305		 * another challenge and break the protocol.
306		 */
307		key_free(key);
308		return (0);
309	}
310	/*
311	 * Correct response.  The client has been successfully
312	 * authenticated. Note that we have not yet processed the
313	 * options; this will be reset if the options cause the
314	 * authentication to be rejected.
315	 */
316	fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
317	verbose("Found matching %s key: %s",
318	    key_type(key), fp);
319	xfree(fp);
320	key_free(key);
321
322	packet_send_debug("RSA authentication accepted.");
323	return (1);
324}
325