blob: faab0e7714226434f51935d43e578e0f0ef00489 [file] [log] [blame]
Damien Miller84399552010-05-21 14:58:12 +10001/* $OpenBSD: auth2-pubkey.c,v 1.25 2010/05/20 11:25:26 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"
Damien Miller30da3442010-05-10 11:58:03 +100060#include "match.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000061
62/* import */
63extern ServerOptions options;
64extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100065extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000066
67static int
68userauth_pubkey(Authctxt *authctxt)
69{
70 Buffer b;
71 Key *key = NULL;
72 char *pkalg;
73 u_char *pkblob, *sig;
74 u_int alen, blen, slen;
75 int have_sig, pktype;
76 int authenticated = 0;
77
78 if (!authctxt->valid) {
79 debug2("userauth_pubkey: disabled because of invalid user");
80 return 0;
81 }
82 have_sig = packet_get_char();
83 if (datafellows & SSH_BUG_PKAUTH) {
84 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
85 /* no explicit pkalg given */
86 pkblob = packet_get_string(&blen);
87 buffer_init(&b);
88 buffer_append(&b, pkblob, blen);
89 /* so we have to extract the pkalg from the pkblob */
90 pkalg = buffer_get_string(&b, &alen);
91 buffer_free(&b);
92 } else {
93 pkalg = packet_get_string(&alen);
94 pkblob = packet_get_string(&blen);
95 }
96 pktype = key_type_from_name(pkalg);
97 if (pktype == KEY_UNSPEC) {
98 /* this is perfectly legal */
Damien Miller996acd22003-04-09 20:59:48 +100099 logit("userauth_pubkey: unsupported public key algorithm: %s",
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000100 pkalg);
101 goto done;
102 }
103 key = key_from_blob(pkblob, blen);
104 if (key == NULL) {
105 error("userauth_pubkey: cannot decode key: %s", pkalg);
106 goto done;
107 }
108 if (key->type != pktype) {
109 error("userauth_pubkey: type mismatch for decoded key "
110 "(received %d, expected %d)", key->type, pktype);
111 goto done;
112 }
113 if (have_sig) {
114 sig = packet_get_string(&slen);
115 packet_check_eom();
116 buffer_init(&b);
117 if (datafellows & SSH_OLD_SESSIONID) {
118 buffer_append(&b, session_id2, session_id2_len);
119 } else {
120 buffer_put_string(&b, session_id2, session_id2_len);
121 }
122 /* reconstruct packet */
123 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
124 buffer_put_cstring(&b, authctxt->user);
125 buffer_put_cstring(&b,
126 datafellows & SSH_BUG_PKSERVICE ?
127 "ssh-userauth" :
128 authctxt->service);
129 if (datafellows & SSH_BUG_PKAUTH) {
130 buffer_put_char(&b, have_sig);
131 } else {
132 buffer_put_cstring(&b, "publickey");
133 buffer_put_char(&b, have_sig);
134 buffer_put_cstring(&b, pkalg);
135 }
136 buffer_put_string(&b, pkblob, blen);
137#ifdef DEBUG_PK
138 buffer_dump(&b);
139#endif
140 /* test for correct signature */
141 authenticated = 0;
142 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
143 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
Damien Millerfb1310e2004-01-21 11:02:50 +1100144 buffer_len(&b))) == 1)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000145 authenticated = 1;
Damien Millerfb1310e2004-01-21 11:02:50 +1100146 buffer_free(&b);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000147 xfree(sig);
148 } else {
149 debug("test whether pkalg/pkblob are acceptable");
150 packet_check_eom();
151
152 /* XXX fake reply and always send PK_OK ? */
153 /*
154 * XXX this allows testing whether a user is allowed
155 * to login: if you happen to have a valid pubkey this
156 * message is sent. the message is NEVER sent at all
157 * if a user is not allowed to login. is this an
158 * issue? -markus
159 */
160 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
161 packet_start(SSH2_MSG_USERAUTH_PK_OK);
162 packet_put_string(pkalg, alen);
163 packet_put_string(pkblob, blen);
164 packet_send();
165 packet_write_wait();
166 authctxt->postponed = 1;
167 }
168 }
169 if (authenticated != 1)
170 auth_clear_options();
171done:
172 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
173 if (key != NULL)
174 key_free(key);
175 xfree(pkalg);
176 xfree(pkblob);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000177 return authenticated;
178}
179
Damien Miller30da3442010-05-10 11:58:03 +1000180static int
181match_principals_option(const char *principal_list, struct KeyCert *cert)
182{
183 char *result;
184 u_int i;
185
186 /* XXX percent_expand() sequences for authorized_principals? */
187
188 for (i = 0; i < cert->nprincipals; i++) {
189 if ((result = match_list(cert->principals[i],
190 principal_list, NULL)) != NULL) {
191 debug3("matched principal from key options \"%.100s\"",
192 result);
193 xfree(result);
194 return 1;
195 }
196 }
197 return 0;
198}
199
200static int
201match_principals_file(const char *file, struct passwd *pw, struct KeyCert *cert)
202{
203 FILE *f;
204 char line[SSH_MAX_PUBKEY_BYTES], *cp;
205 u_long linenum = 0;
206 u_int i;
207
208 temporarily_use_uid(pw);
209 debug("trying authorized principals file %s", file);
210 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
211 restore_uid();
212 return 0;
213 }
214 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
215 /* Skip leading whitespace, empty and comment lines. */
216 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
217 ;
218 if (!*cp || *cp == '\n' || *cp == '#')
219 continue;
220 line[strcspn(line, "\n")] = '\0';
221
222 for (i = 0; i < cert->nprincipals; i++) {
223 if (strcmp(cp, cert->principals[i]) == 0) {
224 debug3("matched principal from file \"%.100s\"",
225 cert->principals[i]);
226 fclose(f);
227 restore_uid();
228 return 1;
229 }
230 }
231 }
232 fclose(f);
233 restore_uid();
234 return 0;
235}
236
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000237/* return 1 if user allows given key */
238static int
239user_key_allowed2(struct passwd *pw, Key *key, char *file)
240{
Darren Tucker22cc7412004-12-06 22:47:41 +1100241 char line[SSH_MAX_PUBKEY_BYTES];
Damien Miller0a80ca12010-02-27 07:55:05 +1100242 const char *reason;
Darren Tucker33c787f2008-07-02 22:37:30 +1000243 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000244 FILE *f;
245 u_long linenum = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000246 Key *found;
247 char *fp;
248
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000249 /* Temporarily use the user's uid. */
250 temporarily_use_uid(pw);
251
252 debug("trying public key file %s", file);
Darren Tucker33c787f2008-07-02 22:37:30 +1000253 f = auth_openkeyfile(file, pw, options.strict_modes);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000254
Darren Tucker33c787f2008-07-02 22:37:30 +1000255 if (!f) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000256 restore_uid();
257 return 0;
258 }
259
260 found_key = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100261 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000262
Darren Tucker22cc7412004-12-06 22:47:41 +1100263 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000264 char *cp, *key_options = NULL;
Darren Tucker22cc7412004-12-06 22:47:41 +1100265
Damien Miller0a80ca12010-02-27 07:55:05 +1100266 auth_clear_options();
267
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000268 /* Skip leading whitespace, empty and comment lines. */
269 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
270 ;
271 if (!*cp || *cp == '\n' || *cp == '#')
272 continue;
273
274 if (key_read(found, &cp) != 1) {
275 /* no key? check if there are options for this key */
276 int quoted = 0;
277 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000278 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000279 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
280 if (*cp == '\\' && cp[1] == '"')
281 cp++; /* Skip both */
282 else if (*cp == '"')
283 quoted = !quoted;
284 }
285 /* Skip remaining whitespace. */
286 for (; *cp == ' ' || *cp == '\t'; cp++)
287 ;
288 if (key_read(found, &cp) != 1) {
289 debug2("user_key_allowed: advance: '%s'", cp);
290 /* still no key? advance to next line*/
291 continue;
292 }
293 }
Damien Miller4e270b02010-04-16 15:56:21 +1000294 if (key_is_cert(key)) {
Damien Miller0a80ca12010-02-27 07:55:05 +1100295 if (!key_equal(found, key->cert->signature_key))
296 continue;
Damien Miller84399552010-05-21 14:58:12 +1000297 if (auth_parse_options(pw, key_options, file,
298 linenum) != 1)
299 continue;
300 if (!key_is_cert_authority)
301 continue;
Damien Miller0a80ca12010-02-27 07:55:05 +1100302 fp = key_fingerprint(found, SSH_FP_MD5,
303 SSH_FP_HEX);
Damien Millere513a912010-03-22 05:51:21 +1100304 debug("matching CA found: file %s, line %lu, %s %s",
305 file, linenum, key_type(found), fp);
Damien Miller30da3442010-05-10 11:58:03 +1000306 /*
307 * If the user has specified a list of principals as
308 * a key option, then prefer that list to matching
309 * their username in the certificate principals list.
310 */
311 if (authorized_principals != NULL &&
312 !match_principals_option(authorized_principals,
313 key->cert)) {
314 reason = "Certificate does not contain an "
315 "authorized principal";
316 fail_reason:
Damien Millere513a912010-03-22 05:51:21 +1100317 xfree(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100318 error("%s", reason);
319 auth_debug_add("%s", reason);
320 continue;
321 }
Damien Miller30da3442010-05-10 11:58:03 +1000322 if (key_cert_check_authority(key, 0, 0,
323 authorized_principals == NULL ? pw->pw_name : NULL,
324 &reason) != 0)
325 goto fail_reason;
Damien Miller4e270b02010-04-16 15:56:21 +1000326 if (auth_cert_options(key, pw) != 0) {
Damien Millere513a912010-03-22 05:51:21 +1100327 xfree(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100328 continue;
Damien Millere513a912010-03-22 05:51:21 +1100329 }
330 verbose("Accepted certificate ID \"%s\" "
331 "signed by %s CA %s via %s", key->cert->key_id,
332 key_type(found), fp, file);
333 xfree(fp);
Damien Miller0a80ca12010-02-27 07:55:05 +1100334 found_key = 1;
335 break;
Damien Miller84399552010-05-21 14:58:12 +1000336 } else if (key_equal(found, key)) {
337 if (auth_parse_options(pw, key_options, file,
338 linenum) != 1)
339 continue;
340 if (key_is_cert_authority)
341 continue;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000342 found_key = 1;
343 debug("matching key found: file %s, line %lu",
344 file, linenum);
345 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
346 verbose("Found matching %s key: %s",
347 key_type(found), fp);
348 xfree(fp);
349 break;
350 }
351 }
352 restore_uid();
353 fclose(f);
354 key_free(found);
355 if (!found_key)
356 debug2("key not found");
357 return found_key;
358}
359
Damien Miller1aed65e2010-03-04 21:53:35 +1100360/* Authenticate a certificate key against TrustedUserCAKeys */
361static int
362user_cert_trusted_ca(struct passwd *pw, Key *key)
363{
Damien Miller30da3442010-05-10 11:58:03 +1000364 char *ca_fp, *principals_file = NULL;
Damien Miller1aed65e2010-03-04 21:53:35 +1100365 const char *reason;
366 int ret = 0;
367
368 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
369 return 0;
370
Damien Millere513a912010-03-22 05:51:21 +1100371 ca_fp = key_fingerprint(key->cert->signature_key,
372 SSH_FP_MD5, SSH_FP_HEX);
Damien Miller1aed65e2010-03-04 21:53:35 +1100373
374 if (key_in_file(key->cert->signature_key,
375 options.trusted_user_ca_keys, 1) != 1) {
376 debug2("%s: CA %s %s is not listed in %s", __func__,
377 key_type(key->cert->signature_key), ca_fp,
378 options.trusted_user_ca_keys);
379 goto out;
380 }
Damien Miller30da3442010-05-10 11:58:03 +1000381 /*
382 * If AuthorizedPrincipals is in use, then compare the certificate
383 * principals against the names in that file rather than matching
384 * against the username.
385 */
386 if ((principals_file = authorized_principals_file(pw)) != NULL) {
387 if (!match_principals_file(principals_file, pw, key->cert)) {
388 reason = "Certificate does not contain an "
389 "authorized principal";
390 fail_reason:
391 error("%s", reason);
392 auth_debug_add("%s", reason);
393 goto out;
394 }
Damien Miller1aed65e2010-03-04 21:53:35 +1100395 }
Damien Miller30da3442010-05-10 11:58:03 +1000396 if (key_cert_check_authority(key, 0, 1,
397 principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
398 goto fail_reason;
Damien Miller4e270b02010-04-16 15:56:21 +1000399 if (auth_cert_options(key, pw) != 0)
Damien Miller1aed65e2010-03-04 21:53:35 +1100400 goto out;
401
Damien Millere513a912010-03-22 05:51:21 +1100402 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
403 key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
404 options.trusted_user_ca_keys);
Damien Miller1aed65e2010-03-04 21:53:35 +1100405 ret = 1;
406
407 out:
Damien Miller30da3442010-05-10 11:58:03 +1000408 if (principals_file != NULL)
409 xfree(principals_file);
Damien Miller1aed65e2010-03-04 21:53:35 +1100410 if (ca_fp != NULL)
411 xfree(ca_fp);
412 return ret;
413}
414
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000415/* check whether given key is in .ssh/authorized_keys* */
416int
417user_key_allowed(struct passwd *pw, Key *key)
418{
419 int success;
420 char *file;
421
Damien Miller1aed65e2010-03-04 21:53:35 +1100422 if (auth_key_is_revoked(key))
423 return 0;
424 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
425 return 0;
426
427 success = user_cert_trusted_ca(pw, key);
428 if (success)
429 return success;
430
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000431 file = authorized_keys_file(pw);
432 success = user_key_allowed2(pw, key, file);
433 xfree(file);
434 if (success)
435 return success;
436
437 /* try suffix "2" for backward compat, too */
438 file = authorized_keys_file2(pw);
439 success = user_key_allowed2(pw, key, file);
440 xfree(file);
441 return success;
442}
443
444Authmethod method_pubkey = {
445 "publickey",
446 userauth_pubkey,
447 &options.pubkey_authentication
448};