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