2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
26 RCSID("$OpenBSD: auth2-pubkey.c,v 1.8 2004/12/06 11:41:03 dtucker Exp $");
39 #include "pathnames.h"
41 #include "auth-options.h"
43 #include "monitor_wrap.h"
46 extern ServerOptions options;
47 extern u_char *session_id2;
48 extern u_int session_id2_len;
51 userauth_pubkey(Authctxt *authctxt)
57 u_int alen, blen, slen;
59 int authenticated = 0;
61 if (!authctxt->valid) {
62 debug2("userauth_pubkey: disabled because of invalid user");
65 have_sig = packet_get_char();
66 if (datafellows & SSH_BUG_PKAUTH) {
67 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
68 /* no explicit pkalg given */
69 pkblob = packet_get_string(&blen);
71 buffer_append(&b, pkblob, blen);
72 /* so we have to extract the pkalg from the pkblob */
73 pkalg = buffer_get_string(&b, &alen);
76 pkalg = packet_get_string(&alen);
77 pkblob = packet_get_string(&blen);
79 pktype = key_type_from_name(pkalg);
80 if (pktype == KEY_UNSPEC) {
81 /* this is perfectly legal */
82 logit("userauth_pubkey: unsupported public key algorithm: %s",
86 key = key_from_blob(pkblob, blen);
88 error("userauth_pubkey: cannot decode key: %s", pkalg);
91 if (key->type != pktype) {
92 error("userauth_pubkey: type mismatch for decoded key "
93 "(received %d, expected %d)", key->type, pktype);
97 sig = packet_get_string(&slen);
100 if (datafellows & SSH_OLD_SESSIONID) {
101 buffer_append(&b, session_id2, session_id2_len);
103 buffer_put_string(&b, session_id2, session_id2_len);
105 /* reconstruct packet */
106 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
107 buffer_put_cstring(&b, authctxt->user);
108 buffer_put_cstring(&b,
109 datafellows & SSH_BUG_PKSERVICE ?
112 if (datafellows & SSH_BUG_PKAUTH) {
113 buffer_put_char(&b, have_sig);
115 buffer_put_cstring(&b, "publickey");
116 buffer_put_char(&b, have_sig);
117 buffer_put_cstring(&b, pkalg);
119 buffer_put_string(&b, pkblob, blen);
123 /* test for correct signature */
125 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
126 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
127 buffer_len(&b))) == 1)
132 debug("test whether pkalg/pkblob are acceptable");
135 /* XXX fake reply and always send PK_OK ? */
137 * XXX this allows testing whether a user is allowed
138 * to login: if you happen to have a valid pubkey this
139 * message is sent. the message is NEVER sent at all
140 * if a user is not allowed to login. is this an
143 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
144 packet_start(SSH2_MSG_USERAUTH_PK_OK);
145 packet_put_string(pkalg, alen);
146 packet_put_string(pkblob, blen);
149 authctxt->postponed = 1;
152 if (authenticated != 1)
153 auth_clear_options();
155 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
161 if (check_nt_auth(0, authctxt->pw) == 0)
164 return authenticated;
167 /* return 1 if user allows given key */
169 user_key_allowed2(struct passwd *pw, Key *key, char *file)
171 char line[SSH_MAX_PUBKEY_BYTES];
179 /* Temporarily use the user's uid. */
180 temporarily_use_uid(pw);
182 debug("trying public key file %s", file);
184 /* Fail quietly if file does not exist */
185 if (stat(file, &st) < 0) {
186 /* Restore the privileged uid. */
190 /* Open the file containing the authorized keys. */
191 f = fopen(file, "r");
193 /* Restore the privileged uid. */
197 if (options.strict_modes &&
198 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
200 logit("Authentication refused: %s", line);
206 found = key_new(key->type);
208 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
209 char *cp, *key_options = NULL;
211 /* Skip leading whitespace, empty and comment lines. */
212 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
214 if (!*cp || *cp == '\n' || *cp == '#')
217 if (key_read(found, &cp) != 1) {
218 /* no key? check if there are options for this key */
220 debug2("user_key_allowed: check options: '%s'", cp);
222 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
223 if (*cp == '\\' && cp[1] == '"')
224 cp++; /* Skip both */
228 /* Skip remaining whitespace. */
229 for (; *cp == ' ' || *cp == '\t'; cp++)
231 if (key_read(found, &cp) != 1) {
232 debug2("user_key_allowed: advance: '%s'", cp);
233 /* still no key? advance to next line*/
237 if (key_equal(found, key) &&
238 auth_parse_options(pw, key_options, file, linenum) == 1) {
240 debug("matching key found: file %s, line %lu",
242 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
243 verbose("Found matching %s key: %s",
244 key_type(found), fp);
253 debug2("key not found");
257 /* check whether given key is in .ssh/authorized_keys* */
259 user_key_allowed(struct passwd *pw, Key *key)
264 file = authorized_keys_file(pw);
265 success = user_key_allowed2(pw, key, file);
270 /* try suffix "2" for backward compat, too */
271 file = authorized_keys_file2(pw);
272 success = user_key_allowed2(pw, key, file);
277 Authmethod method_pubkey = {
280 &options.pubkey_authentication