1 /* $OpenBSD: auth2-pubkey.c,v 1.21 2010/03/04 10:36:03 djm 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"
62 extern ServerOptions options;
63 extern u_char *session_id2;
64 extern u_int session_id2_len;
67 userauth_pubkey(Authctxt *authctxt)
73 u_int alen, blen, slen;
75 int authenticated = 0;
77 if (!authctxt->valid) {
78 debug2("userauth_pubkey: disabled because of invalid user");
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);
87 buffer_append(&b, pkblob, blen);
88 /* so we have to extract the pkalg from the pkblob */
89 pkalg = buffer_get_string(&b, &alen);
92 pkalg = packet_get_string(&alen);
93 pkblob = packet_get_string(&blen);
95 pktype = key_type_from_name(pkalg);
96 if (pktype == KEY_UNSPEC) {
97 /* this is perfectly legal */
98 logit("userauth_pubkey: unsupported public key algorithm: %s",
102 key = key_from_blob(pkblob, blen);
104 error("userauth_pubkey: cannot decode key: %s", pkalg);
107 if (key->type != pktype) {
108 error("userauth_pubkey: type mismatch for decoded key "
109 "(received %d, expected %d)", key->type, pktype);
113 sig = packet_get_string(&slen);
116 if (datafellows & SSH_OLD_SESSIONID) {
117 buffer_append(&b, session_id2, session_id2_len);
119 buffer_put_string(&b, session_id2, session_id2_len);
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 ?
128 if (datafellows & SSH_BUG_PKAUTH) {
129 buffer_put_char(&b, have_sig);
131 buffer_put_cstring(&b, "publickey");
132 buffer_put_char(&b, have_sig);
133 buffer_put_cstring(&b, pkalg);
135 buffer_put_string(&b, pkblob, blen);
139 /* test for correct signature */
141 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
142 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
143 buffer_len(&b))) == 1)
148 debug("test whether pkalg/pkblob are acceptable");
151 /* XXX fake reply and always send PK_OK ? */
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
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);
165 authctxt->postponed = 1;
168 if (authenticated != 1)
169 auth_clear_options();
171 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
176 return authenticated;
179 /* return 1 if user allows given key */
181 user_key_allowed2(struct passwd *pw, Key *key, char *file)
183 char line[SSH_MAX_PUBKEY_BYTES];
191 /* Temporarily use the user's uid. */
192 temporarily_use_uid(pw);
194 debug("trying public key file %s", file);
195 f = auth_openkeyfile(file, pw, options.strict_modes);
203 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
205 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
206 char *cp, *key_options = NULL;
208 auth_clear_options();
210 /* Skip leading whitespace, empty and comment lines. */
211 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
213 if (!*cp || *cp == '\n' || *cp == '#')
216 if (key_read(found, &cp) != 1) {
217 /* no key? check if there are options for this key */
219 debug2("user_key_allowed: check options: '%s'", cp);
221 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
222 if (*cp == '\\' && cp[1] == '"')
223 cp++; /* Skip both */
227 /* Skip remaining whitespace. */
228 for (; *cp == ' ' || *cp == '\t'; cp++)
230 if (key_read(found, &cp) != 1) {
231 debug2("user_key_allowed: advance: '%s'", cp);
232 /* still no key? advance to next line*/
236 if (auth_parse_options(pw, key_options, file, linenum) != 1)
238 if (key->type == KEY_RSA_CERT || key->type == KEY_DSA_CERT) {
239 if (!key_is_cert_authority)
241 if (!key_equal(found, key->cert->signature_key))
243 debug("matching CA found: file %s, line %lu",
245 fp = key_fingerprint(found, SSH_FP_MD5,
247 verbose("Found matching %s CA: %s",
248 key_type(found), fp);
250 if (key_cert_check_authority(key, 0, 0, pw->pw_name,
253 auth_debug_add("%s", reason);
256 if (auth_cert_constraints(&key->cert->constraints,
261 } else if (!key_is_cert_authority && key_equal(found, key)) {
263 debug("matching key found: file %s, line %lu",
265 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
266 verbose("Found matching %s key: %s",
267 key_type(found), fp);
276 debug2("key not found");
280 /* Authenticate a certificate key against TrustedUserCAKeys */
282 user_cert_trusted_ca(struct passwd *pw, Key *key)
284 char *key_fp, *ca_fp;
288 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
291 key_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
292 ca_fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
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);
301 if (key_cert_check_authority(key, 0, 1, pw->pw_name, &reason) != 0) {
303 auth_debug_add("%s", reason);
306 if (auth_cert_constraints(&key->cert->constraints, pw) != 0)
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);
321 /* check whether given key is in .ssh/authorized_keys* */
323 user_key_allowed(struct passwd *pw, Key *key)
328 if (auth_key_is_revoked(key))
330 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
333 success = user_cert_trusted_ca(pw, key);
337 file = authorized_keys_file(pw);
338 success = user_key_allowed2(pw, key, file);
343 /* try suffix "2" for backward compat, too */
344 file = authorized_keys_file2(pw);
345 success = user_key_allowed2(pw, key, file);
350 Authmethod method_pubkey = {
353 &options.pubkey_authentication