xref: /freebsd/crypto/openssh/auth2-pubkey.c (revision d056fa046c6a91b90cd98165face0e42a33a5173)
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 #include "includes.h"
26 RCSID("$OpenBSD: auth2-pubkey.c,v 1.9 2004/12/11 01:48:56 dtucker Exp $");
27 
28 #include "ssh.h"
29 #include "ssh2.h"
30 #include "xmalloc.h"
31 #include "packet.h"
32 #include "buffer.h"
33 #include "log.h"
34 #include "servconf.h"
35 #include "compat.h"
36 #include "bufaux.h"
37 #include "auth.h"
38 #include "key.h"
39 #include "pathnames.h"
40 #include "uidswap.h"
41 #include "auth-options.h"
42 #include "canohost.h"
43 #include "monitor_wrap.h"
44 #include "misc.h"
45 
46 /* import */
47 extern ServerOptions options;
48 extern u_char *session_id2;
49 extern u_int session_id2_len;
50 
51 static int
52 userauth_pubkey(Authctxt *authctxt)
53 {
54 	Buffer b;
55 	Key *key = NULL;
56 	char *pkalg;
57 	u_char *pkblob, *sig;
58 	u_int alen, blen, slen;
59 	int have_sig, pktype;
60 	int authenticated = 0;
61 
62 	if (!authctxt->valid) {
63 		debug2("userauth_pubkey: disabled because of invalid user");
64 		return 0;
65 	}
66 	have_sig = packet_get_char();
67 	if (datafellows & SSH_BUG_PKAUTH) {
68 		debug2("userauth_pubkey: SSH_BUG_PKAUTH");
69 		/* no explicit pkalg given */
70 		pkblob = packet_get_string(&blen);
71 		buffer_init(&b);
72 		buffer_append(&b, pkblob, blen);
73 		/* so we have to extract the pkalg from the pkblob */
74 		pkalg = buffer_get_string(&b, &alen);
75 		buffer_free(&b);
76 	} else {
77 		pkalg = packet_get_string(&alen);
78 		pkblob = packet_get_string(&blen);
79 	}
80 	pktype = key_type_from_name(pkalg);
81 	if (pktype == KEY_UNSPEC) {
82 		/* this is perfectly legal */
83 		logit("userauth_pubkey: unsupported public key algorithm: %s",
84 		    pkalg);
85 		goto done;
86 	}
87 	key = key_from_blob(pkblob, blen);
88 	if (key == NULL) {
89 		error("userauth_pubkey: cannot decode key: %s", pkalg);
90 		goto done;
91 	}
92 	if (key->type != pktype) {
93 		error("userauth_pubkey: type mismatch for decoded key "
94 		    "(received %d, expected %d)", key->type, pktype);
95 		goto done;
96 	}
97 	if (have_sig) {
98 		sig = packet_get_string(&slen);
99 		packet_check_eom();
100 		buffer_init(&b);
101 		if (datafellows & SSH_OLD_SESSIONID) {
102 			buffer_append(&b, session_id2, session_id2_len);
103 		} else {
104 			buffer_put_string(&b, session_id2, session_id2_len);
105 		}
106 		/* reconstruct packet */
107 		buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
108 		buffer_put_cstring(&b, authctxt->user);
109 		buffer_put_cstring(&b,
110 		    datafellows & SSH_BUG_PKSERVICE ?
111 		    "ssh-userauth" :
112 		    authctxt->service);
113 		if (datafellows & SSH_BUG_PKAUTH) {
114 			buffer_put_char(&b, have_sig);
115 		} else {
116 			buffer_put_cstring(&b, "publickey");
117 			buffer_put_char(&b, have_sig);
118 			buffer_put_cstring(&b, pkalg);
119 		}
120 		buffer_put_string(&b, pkblob, blen);
121 #ifdef DEBUG_PK
122 		buffer_dump(&b);
123 #endif
124 		/* test for correct signature */
125 		authenticated = 0;
126 		if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
127 		    PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
128 		    buffer_len(&b))) == 1)
129 			authenticated = 1;
130 		buffer_free(&b);
131 		xfree(sig);
132 	} else {
133 		debug("test whether pkalg/pkblob are acceptable");
134 		packet_check_eom();
135 
136 		/* XXX fake reply and always send PK_OK ? */
137 		/*
138 		 * XXX this allows testing whether a user is allowed
139 		 * to login: if you happen to have a valid pubkey this
140 		 * message is sent. the message is NEVER sent at all
141 		 * if a user is not allowed to login. is this an
142 		 * issue? -markus
143 		 */
144 		if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
145 			packet_start(SSH2_MSG_USERAUTH_PK_OK);
146 			packet_put_string(pkalg, alen);
147 			packet_put_string(pkblob, blen);
148 			packet_send();
149 			packet_write_wait();
150 			authctxt->postponed = 1;
151 		}
152 	}
153 	if (authenticated != 1)
154 		auth_clear_options();
155 done:
156 	debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
157 	if (key != NULL)
158 		key_free(key);
159 	xfree(pkalg);
160 	xfree(pkblob);
161 #ifdef HAVE_CYGWIN
162 	if (check_nt_auth(0, authctxt->pw) == 0)
163 		authenticated = 0;
164 #endif
165 	return authenticated;
166 }
167 
168 /* return 1 if user allows given key */
169 static int
170 user_key_allowed2(struct passwd *pw, Key *key, char *file)
171 {
172 	char line[SSH_MAX_PUBKEY_BYTES];
173 	int found_key = 0;
174 	FILE *f;
175 	u_long linenum = 0;
176 	struct stat st;
177 	Key *found;
178 	char *fp;
179 
180 	/* Temporarily use the user's uid. */
181 	temporarily_use_uid(pw);
182 
183 	debug("trying public key file %s", file);
184 
185 	/* Fail quietly if file does not exist */
186 	if (stat(file, &st) < 0) {
187 		/* Restore the privileged uid. */
188 		restore_uid();
189 		return 0;
190 	}
191 	/* Open the file containing the authorized keys. */
192 	f = fopen(file, "r");
193 	if (!f) {
194 		/* Restore the privileged uid. */
195 		restore_uid();
196 		return 0;
197 	}
198 	if (options.strict_modes &&
199 	    secure_filename(f, file, pw, line, sizeof(line)) != 0) {
200 		fclose(f);
201 		logit("Authentication refused: %s", line);
202 		restore_uid();
203 		return 0;
204 	}
205 
206 	found_key = 0;
207 	found = key_new(key->type);
208 
209 	while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
210 		char *cp, *key_options = NULL;
211 
212 		/* Skip leading whitespace, empty and comment lines. */
213 		for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
214 			;
215 		if (!*cp || *cp == '\n' || *cp == '#')
216 			continue;
217 
218 		if (key_read(found, &cp) != 1) {
219 			/* no key?  check if there are options for this key */
220 			int quoted = 0;
221 			debug2("user_key_allowed: check options: '%s'", cp);
222 			key_options = cp;
223 			for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
224 				if (*cp == '\\' && cp[1] == '"')
225 					cp++;	/* Skip both */
226 				else if (*cp == '"')
227 					quoted = !quoted;
228 			}
229 			/* Skip remaining whitespace. */
230 			for (; *cp == ' ' || *cp == '\t'; cp++)
231 				;
232 			if (key_read(found, &cp) != 1) {
233 				debug2("user_key_allowed: advance: '%s'", cp);
234 				/* still no key?  advance to next line*/
235 				continue;
236 			}
237 		}
238 		if (key_equal(found, key) &&
239 		    auth_parse_options(pw, key_options, file, linenum) == 1) {
240 			found_key = 1;
241 			debug("matching key found: file %s, line %lu",
242 			    file, linenum);
243 			fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
244 			verbose("Found matching %s key: %s",
245 			    key_type(found), fp);
246 			xfree(fp);
247 			break;
248 		}
249 	}
250 	restore_uid();
251 	fclose(f);
252 	key_free(found);
253 	if (!found_key)
254 		debug2("key not found");
255 	return found_key;
256 }
257 
258 /* check whether given key is in .ssh/authorized_keys* */
259 int
260 user_key_allowed(struct passwd *pw, Key *key)
261 {
262 	int success;
263 	char *file;
264 
265 	file = authorized_keys_file(pw);
266 	success = user_key_allowed2(pw, key, file);
267 	xfree(file);
268 	if (success)
269 		return success;
270 
271 	/* try suffix "2" for backward compat, too */
272 	file = authorized_keys_file2(pw);
273 	success = user_key_allowed2(pw, key, file);
274 	xfree(file);
275 	return success;
276 }
277 
278 Authmethod method_pubkey = {
279 	"publickey",
280 	userauth_pubkey,
281 	&options.pubkey_authentication
282 };
283