blob: 51aa774872b37660d80e804725bb8080fe03f51c [file] [log] [blame]
Damien Miller1aed65e2010-03-04 21:53:35 +11001/* $OpenBSD: auth2-pubkey.c,v 1.21 2010/03/04 10:36:03 djm Exp $ */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110027
28#include <sys/types.h>
29#include <sys/stat.h>
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000030
Darren Tucker06db5842008-06-13 14:51:28 +100031#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100032#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100033#include <stdio.h>
Damien Millerd7834352006-08-05 12:39:39 +100034#include <stdarg.h>
Damien Miller0a80ca12010-02-27 07:55:05 +110035#include <string.h>
36#include <time.h>
Darren Tuckerd9526a52008-06-14 09:01:24 +100037#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100038
Damien Millerd7834352006-08-05 12:39:39 +100039#include "xmalloc.h"
Darren Tucker22cc7412004-12-06 22:47:41 +110040#include "ssh.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000041#include "ssh2.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000042#include "packet.h"
43#include "buffer.h"
44#include "log.h"
45#include "servconf.h"
46#include "compat.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000047#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100048#include "hostfile.h"
49#include "auth.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000050#include "pathnames.h"
51#include "uidswap.h"
52#include "auth-options.h"
53#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100054#ifdef GSSAPI
55#include "ssh-gss.h"
56#endif
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000057#include "monitor_wrap.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110058#include "misc.h"
Damien Miller1aed65e2010-03-04 21:53:35 +110059#include "authfile.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000060
61/* import */
62extern ServerOptions options;
63extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100064extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000065
66static int
67userauth_pubkey(Authctxt *authctxt)
68{
69 Buffer b;
70 Key *key = NULL;
71 char *pkalg;
72 u_char *pkblob, *sig;
73 u_int alen, blen, slen;
74 int have_sig, pktype;
75 int authenticated = 0;
76
77 if (!authctxt->valid) {
78 debug2("userauth_pubkey: disabled because of invalid user");
79 return 0;
80 }
81 have_sig = packet_get_char();
82 if (datafellows & SSH_BUG_PKAUTH) {
83 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
84 /* no explicit pkalg given */
85 pkblob = packet_get_string(&blen);
86 buffer_init(&b);
87 buffer_append(&b, pkblob, blen);
88 /* so we have to extract the pkalg from the pkblob */
89 pkalg = buffer_get_string(&b, &alen);
90 buffer_free(&b);
91 } else {
92 pkalg = packet_get_string(&alen);
93 pkblob = packet_get_string(&blen);
94 }
95 pktype = key_type_from_name(pkalg);
96 if (pktype == KEY_UNSPEC) {
97 /* this is perfectly legal */
Damien Miller996acd22003-04-09 20:59:48 +100098 logit("userauth_pubkey: unsupported public key algorithm: %s",
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000099 pkalg);
100 goto done;
101 }
102 key = key_from_blob(pkblob, blen);
103 if (key == NULL) {
104 error("userauth_pubkey: cannot decode key: %s", pkalg);
105 goto done;
106 }
107 if (key->type != pktype) {
108 error("userauth_pubkey: type mismatch for decoded key "
109 "(received %d, expected %d)", key->type, pktype);
110 goto done;
111 }
112 if (have_sig) {
113 sig = packet_get_string(&slen);
114 packet_check_eom();
115 buffer_init(&b);
116 if (datafellows & SSH_OLD_SESSIONID) {
117 buffer_append(&b, session_id2, session_id2_len);
118 } else {
119 buffer_put_string(&b, session_id2, session_id2_len);
120 }
121 /* reconstruct packet */
122 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
123 buffer_put_cstring(&b, authctxt->user);
124 buffer_put_cstring(&b,
125 datafellows & SSH_BUG_PKSERVICE ?
126 "ssh-userauth" :
127 authctxt->service);
128 if (datafellows & SSH_BUG_PKAUTH) {
129 buffer_put_char(&b, have_sig);
130 } else {
131 buffer_put_cstring(&b, "publickey");
132 buffer_put_char(&b, have_sig);
133 buffer_put_cstring(&b, pkalg);
134 }
135 buffer_put_string(&b, pkblob, blen);
136#ifdef DEBUG_PK
137 buffer_dump(&b);
138#endif
139 /* test for correct signature */
140 authenticated = 0;
141 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
142 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
Damien Millerfb1310e2004-01-21 11:02:50 +1100143 buffer_len(&b))) == 1)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000144 authenticated = 1;
Damien Millerfb1310e2004-01-21 11:02:50 +1100145 buffer_free(&b);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000146 xfree(sig);
147 } else {
148 debug("test whether pkalg/pkblob are acceptable");
149 packet_check_eom();
150
151 /* XXX fake reply and always send PK_OK ? */
152 /*
153 * XXX this allows testing whether a user is allowed
154 * to login: if you happen to have a valid pubkey this
155 * message is sent. the message is NEVER sent at all
156 * if a user is not allowed to login. is this an
157 * issue? -markus
158 */
159 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
160 packet_start(SSH2_MSG_USERAUTH_PK_OK);
161 packet_put_string(pkalg, alen);
162 packet_put_string(pkblob, blen);
163 packet_send();
164 packet_write_wait();
165 authctxt->postponed = 1;
166 }
167 }
168 if (authenticated != 1)
169 auth_clear_options();
170done:
171 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
172 if (key != NULL)
173 key_free(key);
174 xfree(pkalg);
175 xfree(pkblob);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000176 return authenticated;
177}
178
179/* return 1 if user allows given key */
180static int
181user_key_allowed2(struct passwd *pw, Key *key, char *file)
182{
Darren Tucker22cc7412004-12-06 22:47:41 +1100183 char line[SSH_MAX_PUBKEY_BYTES];
Damien Miller0a80ca12010-02-27 07:55:05 +1100184 const char *reason;
Darren Tucker33c787f2008-07-02 22:37:30 +1000185 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000186 FILE *f;
187 u_long linenum = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000188 Key *found;
189 char *fp;
190
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000191 /* Temporarily use the user's uid. */
192 temporarily_use_uid(pw);
193
194 debug("trying public key file %s", file);
Darren Tucker33c787f2008-07-02 22:37:30 +1000195 f = auth_openkeyfile(file, pw, options.strict_modes);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000196
Darren Tucker33c787f2008-07-02 22:37:30 +1000197 if (!f) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000198 restore_uid();
199 return 0;
200 }
201
202 found_key = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100203 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000204
Darren Tucker22cc7412004-12-06 22:47:41 +1100205 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000206 char *cp, *key_options = NULL;
Darren Tucker22cc7412004-12-06 22:47:41 +1100207
Damien Miller0a80ca12010-02-27 07:55:05 +1100208 auth_clear_options();
209
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000210 /* Skip leading whitespace, empty and comment lines. */
211 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
212 ;
213 if (!*cp || *cp == '\n' || *cp == '#')
214 continue;
215
216 if (key_read(found, &cp) != 1) {
217 /* no key? check if there are options for this key */
218 int quoted = 0;
219 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000220 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000221 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 /* Skip remaining whitespace. */
228 for (; *cp == ' ' || *cp == '\t'; cp++)
229 ;
230 if (key_read(found, &cp) != 1) {
231 debug2("user_key_allowed: advance: '%s'", cp);
232 /* still no key? advance to next line*/
233 continue;
234 }
235 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100236 if (auth_parse_options(pw, key_options, file, linenum) != 1)
237 continue;
238 if (key->type == KEY_RSA_CERT || key->type == KEY_DSA_CERT) {
239 if (!key_is_cert_authority)
240 continue;
241 if (!key_equal(found, key->cert->signature_key))
242 continue;
243 debug("matching CA found: file %s, line %lu",
244 file, linenum);
245 fp = key_fingerprint(found, SSH_FP_MD5,
246 SSH_FP_HEX);
247 verbose("Found matching %s CA: %s",
248 key_type(found), fp);
249 xfree(fp);
250 if (key_cert_check_authority(key, 0, 0, pw->pw_name,
251 &reason) != 0) {
252 error("%s", reason);
253 auth_debug_add("%s", reason);
254 continue;
255 }
256 if (auth_cert_constraints(&key->cert->constraints,
257 pw) != 0)
258 continue;
259 found_key = 1;
260 break;
261 } else if (!key_is_cert_authority && key_equal(found, key)) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000262 found_key = 1;
263 debug("matching key found: file %s, line %lu",
264 file, linenum);
265 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
266 verbose("Found matching %s key: %s",
267 key_type(found), fp);
268 xfree(fp);
269 break;
270 }
271 }
272 restore_uid();
273 fclose(f);
274 key_free(found);
275 if (!found_key)
276 debug2("key not found");
277 return found_key;
278}
279
Damien Miller1aed65e2010-03-04 21:53:35 +1100280/* Authenticate a certificate key against TrustedUserCAKeys */
281static int
282user_cert_trusted_ca(struct passwd *pw, Key *key)
283{
284 char *key_fp, *ca_fp;
285 const char *reason;
286 int ret = 0;
287
288 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
289 return 0;
290
291 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
292 ca_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
293
294 if (key_in_file(key->cert->signature_key,
295 options.trusted_user_ca_keys, 1) != 1) {
296 debug2("%s: CA %s %s is not listed in %s", __func__,
297 key_type(key->cert->signature_key), ca_fp,
298 options.trusted_user_ca_keys);
299 goto out;
300 }
301 if (key_cert_check_authority(key, 0, 1, pw->pw_name, &reason) != 0) {
302 error("%s", reason);
303 auth_debug_add("%s", reason);
304 goto out;
305 }
306 if (auth_cert_constraints(&key->cert->constraints, pw) != 0)
307 goto out;
308
309 verbose("%s certificate %s allowed by trusted %s key %s",
310 key_type(key), key_fp, key_type(key->cert->signature_key), ca_fp);
311 ret = 1;
312
313 out:
314 if (key_fp != NULL)
315 xfree(key_fp);
316 if (ca_fp != NULL)
317 xfree(ca_fp);
318 return ret;
319}
320
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000321/* check whether given key is in .ssh/authorized_keys* */
322int
323user_key_allowed(struct passwd *pw, Key *key)
324{
325 int success;
326 char *file;
327
Damien Miller1aed65e2010-03-04 21:53:35 +1100328 if (auth_key_is_revoked(key))
329 return 0;
330 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
331 return 0;
332
333 success = user_cert_trusted_ca(pw, key);
334 if (success)
335 return success;
336
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000337 file = authorized_keys_file(pw);
338 success = user_key_allowed2(pw, key, file);
339 xfree(file);
340 if (success)
341 return success;
342
343 /* try suffix "2" for backward compat, too */
344 file = authorized_keys_file2(pw);
345 success = user_key_allowed2(pw, key, file);
346 xfree(file);
347 return success;
348}
349
350Authmethod method_pubkey = {
351 "publickey",
352 userauth_pubkey,
353 &options.pubkey_authentication
354};