1 /* $OpenBSD: auth2-pubkey.c,v 1.27 2010/11/20 05:12:38 deraadt Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/types.h>
50 #include "pathnames.h"
52 #include "auth-options.h"
57 #include "monitor_wrap.h"
63 extern ServerOptions options;
64 extern u_char *session_id2;
65 extern u_int session_id2_len;
68 userauth_pubkey(Authctxt *authctxt)
74 u_int alen, blen, slen;
76 int authenticated = 0;
78 if (!authctxt->valid) {
79 debug2("userauth_pubkey: disabled because of invalid user");
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);
88 buffer_append(&b, pkblob, blen);
89 /* so we have to extract the pkalg from the pkblob */
90 pkalg = buffer_get_string(&b, &alen);
93 pkalg = packet_get_string(&alen);
94 pkblob = packet_get_string(&blen);
96 pktype = key_type_from_name(pkalg);
97 if (pktype == KEY_UNSPEC) {
98 /* this is perfectly legal */
99 logit("userauth_pubkey: unsupported public key algorithm: %s",
103 key = key_from_blob(pkblob, blen);
105 error("userauth_pubkey: cannot decode key: %s", pkalg);
108 if (key->type != pktype) {
109 error("userauth_pubkey: type mismatch for decoded key "
110 "(received %d, expected %d)", key->type, pktype);
114 sig = packet_get_string(&slen);
117 if (datafellows & SSH_OLD_SESSIONID) {
118 buffer_append(&b, session_id2, session_id2_len);
120 buffer_put_string(&b, session_id2, session_id2_len);
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 ?
129 if (datafellows & SSH_BUG_PKAUTH) {
130 buffer_put_char(&b, have_sig);
132 buffer_put_cstring(&b, "publickey");
133 buffer_put_char(&b, have_sig);
134 buffer_put_cstring(&b, pkalg);
136 buffer_put_string(&b, pkblob, blen);
140 /* test for correct signature */
142 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
143 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
144 buffer_len(&b))) == 1)
149 debug("test whether pkalg/pkblob are acceptable");
152 /* XXX fake reply and always send PK_OK ? */
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
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);
166 authctxt->postponed = 1;
169 if (authenticated != 1)
170 auth_clear_options();
172 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
177 return authenticated;
181 match_principals_option(const char *principal_list, struct KeyCert *cert)
186 /* XXX percent_expand() sequences for authorized_principals? */
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\"",
201 match_principals_file(char *file, struct passwd *pw, struct KeyCert *cert)
204 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
208 temporarily_use_uid(pw);
209 debug("trying authorized principals file %s", file);
210 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
214 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
215 /* Skip leading whitespace. */
216 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
218 /* Skip blank and comment lines. */
219 if ((ep = strchr(cp, '#')) != NULL)
221 if (!*cp || *cp == '\n')
223 /* Trim trailing whitespace. */
224 ep = cp + strlen(cp) - 1;
225 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
228 * If the line has internal whitespace then assume it has
232 if ((ep = strrchr(cp, ' ')) != NULL ||
233 (ep = strrchr(cp, '\t')) != NULL) {
234 for (; *ep == ' ' || *ep == '\t'; ep++)
239 for (i = 0; i < cert->nprincipals; i++) {
240 if (strcmp(cp, cert->principals[i]) == 0) {
241 debug3("matched principal from file \"%.100s\"",
242 cert->principals[i]);
243 if (auth_parse_options(pw, line_opts,
257 /* return 1 if user allows given key */
259 user_key_allowed2(struct passwd *pw, Key *key, char *file)
261 char line[SSH_MAX_PUBKEY_BYTES];
269 /* Temporarily use the user's uid. */
270 temporarily_use_uid(pw);
272 debug("trying public key file %s", file);
273 f = auth_openkeyfile(file, pw, options.strict_modes);
281 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
283 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
284 char *cp, *key_options = NULL;
286 auth_clear_options();
288 /* Skip leading whitespace, empty and comment lines. */
289 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
291 if (!*cp || *cp == '\n' || *cp == '#')
294 if (key_read(found, &cp) != 1) {
295 /* no key? check if there are options for this key */
297 debug2("user_key_allowed: check options: '%s'", cp);
299 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
300 if (*cp == '\\' && cp[1] == '"')
301 cp++; /* Skip both */
305 /* Skip remaining whitespace. */
306 for (; *cp == ' ' || *cp == '\t'; cp++)
308 if (key_read(found, &cp) != 1) {
309 debug2("user_key_allowed: advance: '%s'", cp);
310 /* still no key? advance to next line*/
314 if (key_is_cert(key)) {
315 if (!key_equal(found, key->cert->signature_key))
317 if (auth_parse_options(pw, key_options, file,
320 if (!key_is_cert_authority)
322 fp = key_fingerprint(found, SSH_FP_MD5,
324 debug("matching CA found: file %s, line %lu, %s %s",
325 file, linenum, key_type(found), fp);
327 * If the user has specified a list of principals as
328 * a key option, then prefer that list to matching
329 * their username in the certificate principals list.
331 if (authorized_principals != NULL &&
332 !match_principals_option(authorized_principals,
334 reason = "Certificate does not contain an "
335 "authorized principal";
339 auth_debug_add("%s", reason);
342 if (key_cert_check_authority(key, 0, 0,
343 authorized_principals == NULL ? pw->pw_name : NULL,
346 if (auth_cert_options(key, pw) != 0) {
350 verbose("Accepted certificate ID \"%s\" "
351 "signed by %s CA %s via %s", key->cert->key_id,
352 key_type(found), fp, file);
356 } else if (key_equal(found, key)) {
357 if (auth_parse_options(pw, key_options, file,
360 if (key_is_cert_authority)
363 debug("matching key found: file %s, line %lu",
365 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
366 verbose("Found matching %s key: %s",
367 key_type(found), fp);
376 debug2("key not found");
380 /* Authenticate a certificate key against TrustedUserCAKeys */
382 user_cert_trusted_ca(struct passwd *pw, Key *key)
384 char *ca_fp, *principals_file = NULL;
388 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
391 ca_fp = key_fingerprint(key->cert->signature_key,
392 SSH_FP_MD5, SSH_FP_HEX);
394 if (key_in_file(key->cert->signature_key,
395 options.trusted_user_ca_keys, 1) != 1) {
396 debug2("%s: CA %s %s is not listed in %s", __func__,
397 key_type(key->cert->signature_key), ca_fp,
398 options.trusted_user_ca_keys);
402 * If AuthorizedPrincipals is in use, then compare the certificate
403 * principals against the names in that file rather than matching
404 * against the username.
406 if ((principals_file = authorized_principals_file(pw)) != NULL) {
407 if (!match_principals_file(principals_file, pw, key->cert)) {
408 reason = "Certificate does not contain an "
409 "authorized principal";
412 auth_debug_add("%s", reason);
416 if (key_cert_check_authority(key, 0, 1,
417 principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
419 if (auth_cert_options(key, pw) != 0)
422 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
423 key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
424 options.trusted_user_ca_keys);
428 if (principals_file != NULL)
429 xfree(principals_file);
435 /* check whether given key is in .ssh/authorized_keys* */
437 user_key_allowed(struct passwd *pw, Key *key)
442 if (auth_key_is_revoked(key))
444 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
447 success = user_cert_trusted_ca(pw, key);
451 file = authorized_keys_file(pw);
452 success = user_key_allowed2(pw, key, file);
457 /* try suffix "2" for backward compat, too */
458 file = authorized_keys_file2(pw);
459 success = user_key_allowed2(pw, key, file);
464 Authmethod method_pubkey = {
467 &options.pubkey_authentication