xref: /titanic_50/usr/src/cmd/ssh/sshd/auth2-pubkey.c (revision 4944376cd5de3dcd3b4feeaad9cbedbc024d1474)
1 /*
2  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  */
24 /*
25  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 #include "includes.h"
30 RCSID("$OpenBSD: auth2-pubkey.c,v 1.2 2002/05/31 11:35:15 markus Exp $");
31 
32 #include "ssh2.h"
33 #include "xmalloc.h"
34 #include "packet.h"
35 #include "buffer.h"
36 #include "log.h"
37 #include "servconf.h"
38 #include "compat.h"
39 #include "bufaux.h"
40 #include "auth.h"
41 #include "key.h"
42 #include "pathnames.h"
43 #include "uidswap.h"
44 #include "auth-options.h"
45 #include "canohost.h"
46 
47 #ifdef USE_PAM
48 #include <security/pam_appl.h>
49 #include "auth-pam.h"
50 #endif /* USE_PAM */
51 
52 /* import */
53 extern ServerOptions options;
54 extern u_char *session_id2;
55 extern int session_id2_len;
56 
57 static void
58 userauth_pubkey(Authctxt *authctxt)
59 {
60 	Buffer b;
61 	Key *key = NULL;
62 	char *pkalg;
63 	u_char *pkblob, *sig;
64 	u_int alen, blen, slen;
65 	int have_sig, pktype;
66 	int authenticated = 0;
67 
68 	if (!authctxt || !authctxt->method)
69 		fatal("%s: missing context", __func__);
70 
71 	have_sig = packet_get_char();
72 	if (datafellows & SSH_BUG_PKAUTH) {
73 		debug2("userauth_pubkey: SSH_BUG_PKAUTH");
74 		/* no explicit pkalg given */
75 		pkblob = packet_get_string(&blen);
76 		buffer_init(&b);
77 		buffer_append(&b, pkblob, blen);
78 		/* so we have to extract the pkalg from the pkblob */
79 		pkalg = buffer_get_string(&b, &alen);
80 		buffer_free(&b);
81 	} else {
82 		pkalg = packet_get_string(&alen);
83 		pkblob = packet_get_string(&blen);
84 	}
85 	pktype = key_type_from_name(pkalg);
86 	if (pktype == KEY_UNSPEC) {
87 		/* this is perfectly legal */
88 		log("userauth_pubkey: unsupported public key algorithm: %s",
89 		    pkalg);
90 		goto done;
91 	}
92 	key = key_from_blob(pkblob, blen);
93 	if (key == NULL) {
94 		error("userauth_pubkey: cannot decode key: %s", pkalg);
95 		goto done;
96 	}
97 	if (key->type != pktype) {
98 		error("userauth_pubkey: type mismatch for decoded key "
99 		    "(received %d, expected %d)", key->type, pktype);
100 		goto done;
101 	}
102 
103 	/* Detect and count abandonment */
104 	if (authctxt->method->method_data) {
105 		Key	*prev_key;
106 		unsigned char	*prev_pkblob;
107 		int	 prev_blen;
108 
109 		/*
110 		 * Check for earlier test of a key that was allowed but
111 		 * not followed up with a pubkey req for the same pubkey
112 		 * and with a signature.
113 		 */
114 		prev_key = authctxt->method->method_data;
115 		if ((prev_blen = key_to_blob(prev_key,
116 			    &prev_pkblob, NULL))) {
117 			if (prev_blen != blen ||
118 			    memcmp(prev_pkblob, pkblob, blen) != 0) {
119 				authctxt->method->abandons++;
120 				authctxt->method->attempts++;
121 			}
122 		}
123 		key_free(prev_key);
124 		authctxt->method->method_data = NULL;
125 	}
126 
127 	if (have_sig) {
128 		sig = packet_get_string(&slen);
129 		packet_check_eom();
130 		buffer_init(&b);
131 		if (datafellows & SSH_OLD_SESSIONID) {
132 			buffer_append(&b, session_id2, session_id2_len);
133 		} else {
134 			buffer_put_string(&b, session_id2, session_id2_len);
135 		}
136 		/* reconstruct packet */
137 		buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
138 		buffer_put_cstring(&b, authctxt->user);
139 		buffer_put_cstring(&b,
140 		    datafellows & SSH_BUG_PKSERVICE ?
141 		    "ssh-userauth" :
142 		    authctxt->service);
143 		if (datafellows & SSH_BUG_PKAUTH) {
144 			buffer_put_char(&b, have_sig);
145 		} else {
146 			buffer_put_cstring(&b, "publickey");
147 			buffer_put_char(&b, have_sig);
148 			buffer_put_cstring(&b, pkalg);
149 		}
150 		buffer_put_string(&b, pkblob, blen);
151 #ifdef DEBUG_PK
152 		buffer_dump(&b);
153 #endif
154 		/* test for correct signature */
155 		if (user_key_allowed(authctxt->pw, key) &&
156 		    key_verify(key, sig, slen, buffer_ptr(&b),
157 		    buffer_len(&b)) == 1) {
158 			authenticated = 1;
159 		}
160 		authctxt->method->postponed = 0;
161 		buffer_free(&b);
162 		xfree(sig);
163 	} else {
164 		debug("test whether pkalg/pkblob are acceptable");
165 		packet_check_eom();
166 
167 		/* XXX fake reply and always send PK_OK ? */
168 		/*
169 		 * XXX this allows testing whether a user is allowed
170 		 * to login: if you happen to have a valid pubkey this
171 		 * message is sent. the message is NEVER sent at all
172 		 * if a user is not allowed to login. is this an
173 		 * issue? -markus
174 		 */
175 		if (user_key_allowed(authctxt->pw, key)) {
176 			packet_start(SSH2_MSG_USERAUTH_PK_OK);
177 			packet_put_string(pkalg, alen);
178 			packet_put_string(pkblob, blen);
179 			packet_send();
180 			packet_write_wait();
181 			authctxt->method->postponed = 1;
182 			/*
183 			 * Remember key that was tried so we can
184 			 * correctly detect abandonment.  See above.
185 			 */
186 			authctxt->method->method_data = (void *) key;
187 			key = NULL;
188 		}
189 	}
190 	if (authenticated != 1)
191 		auth_clear_options();
192 
193 done:
194 	/*
195 	 * XXX TODO: add config options for specifying users for whom
196 	 * this userauth is insufficient and what userauths may
197 	 * continue.
198 	 */
199 #ifdef USE_PAM
200 	if (authenticated) {
201 		if (!do_pam_non_initial_userauth(authctxt))
202 			authenticated = 0;
203 	}
204 #endif /* USE_PAM */
205 
206 	debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
207 	if (key != NULL)
208 		key_free(key);
209 	xfree(pkalg);
210 	xfree(pkblob);
211 #ifdef HAVE_CYGWIN
212 	if (check_nt_auth(0, authctxt->pw) == 0)
213 		return;
214 #endif
215 	if (authenticated)
216 		authctxt->method->authenticated = 1;
217 }
218 
219 /* return 1 if user allows given key */
220 static int
221 user_key_allowed2(struct passwd *pw, Key *key, char *file)
222 {
223 	char line[8192];
224 	int found_key = 0;
225 	FILE *f;
226 	u_long linenum = 0;
227 	struct stat st;
228 	Key *found;
229 	char *fp;
230 
231 	if (pw == NULL)
232 		return 0;
233 
234 	/* Temporarily use the user's uid. */
235 	temporarily_use_uid(pw);
236 
237 	debug("trying public key file %s", file);
238 
239 	/* Fail quietly if file does not exist */
240 	if (stat(file, &st) < 0) {
241 		/* Restore the privileged uid. */
242 		restore_uid();
243 		return 0;
244 	}
245 	/* Open the file containing the authorized keys. */
246 	f = fopen(file, "r");
247 	if (!f) {
248 		/* Restore the privileged uid. */
249 		restore_uid();
250 		return 0;
251 	}
252 	if (options.strict_modes &&
253 	    secure_filename(f, file, pw, line, sizeof(line)) != 0) {
254 		(void) fclose(f);
255 		log("Authentication refused: %s", line);
256 		restore_uid();
257 		return 0;
258 	}
259 
260 	found_key = 0;
261 	found = key_new(key->type);
262 
263 	while (fgets(line, sizeof(line), f)) {
264 		char *cp, *options = NULL;
265 		linenum++;
266 		/* Skip leading whitespace, empty and comment lines. */
267 		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
268 			;
269 		if (!*cp || *cp == '\n' || *cp == '#')
270 			continue;
271 
272 		if (key_read(found, &cp) != 1) {
273 			/* no key?  check if there are options for this key */
274 			int quoted = 0;
275 			debug2("user_key_allowed: check options: '%s'", cp);
276 			options = cp;
277 			for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
278 				if (*cp == '\\' && cp[1] == '"')
279 					cp++;	/* Skip both */
280 				else if (*cp == '"')
281 					quoted = !quoted;
282 			}
283 			/* Skip remaining whitespace. */
284 			for (; *cp == ' ' || *cp == '\t'; cp++)
285 				;
286 			if (key_read(found, &cp) != 1) {
287 				debug2("user_key_allowed: advance: '%s'", cp);
288 				/* still no key?  advance to next line*/
289 				continue;
290 			}
291 		}
292 		if (key_equal(found, key) &&
293 		    auth_parse_options(pw, options, file, linenum) == 1) {
294 			found_key = 1;
295 			debug("matching key found: file %s, line %lu",
296 			    file, linenum);
297 			fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
298 			verbose("Found matching %s key: %s",
299 			    key_type(found), fp);
300 			xfree(fp);
301 			break;
302 		}
303 	}
304 	restore_uid();
305 	(void) fclose(f);
306 	key_free(found);
307 	if (!found_key)
308 		debug2("key not found");
309 	return found_key;
310 }
311 
312 /* check whether given key is in .ssh/authorized_keys* */
313 int
314 user_key_allowed(struct passwd *pw, Key *key)
315 {
316 	int success;
317 	char *file;
318 
319 	if (pw == NULL)
320 		return 0;
321 
322 	file = authorized_keys_file(pw);
323 	success = user_key_allowed2(pw, key, file);
324 	xfree(file);
325 	if (success)
326 		return success;
327 
328 	/* try suffix "2" for backward compat, too */
329 	file = authorized_keys_file2(pw);
330 	success = user_key_allowed2(pw, key, file);
331 	xfree(file);
332 	return success;
333 }
334 
335 static
336 void
337 userauth_pubkey_abandon(Authctxt *authctxt, Authmethod *method)
338 {
339 	if (!authctxt || !method)
340 		return;
341 
342 	if (method->method_data) {
343 		method->abandons++;
344 		method->attempts++;
345 		key_free((Key *) method->method_data);
346 		method->method_data = NULL;
347 	}
348 }
349 
350 Authmethod method_pubkey = {
351 	"publickey",
352 	&options.pubkey_authentication,
353 	userauth_pubkey,
354 	userauth_pubkey_abandon,
355 	NULL, NULL,	    /* method data and hist data */
356 	0,		    /* not initial userauth */
357 	0, 0, 0,	    /* counters */
358 	0, 0, 0, 0, 0, 0    /* state */
359 };
360