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.9 2004/12/11 01:48:56 dtucker Exp $");
39 #include "pathnames.h"
41 #include "auth-options.h"
43 #include "monitor_wrap.h"
47 extern ServerOptions options;
48 extern u_char *session_id2;
49 extern u_int session_id2_len;
52 userauth_pubkey(Authctxt *authctxt)
58 u_int alen, blen, slen;
60 int authenticated = 0;
62 if (!authctxt->valid) {
63 debug2("userauth_pubkey: disabled because of invalid user");
66 have_sig = packet_get_char();
67 if (datafellows & SSH_BUG_PKAUTH) {
68 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
69 /* no explicit pkalg given */
70 pkblob = packet_get_string(&blen);
72 buffer_append(&b, pkblob, blen);
73 /* so we have to extract the pkalg from the pkblob */
74 pkalg = buffer_get_string(&b, &alen);
77 pkalg = packet_get_string(&alen);
78 pkblob = packet_get_string(&blen);
80 pktype = key_type_from_name(pkalg);
81 if (pktype == KEY_UNSPEC) {
82 /* this is perfectly legal */
83 logit("userauth_pubkey: unsupported public key algorithm: %s",
87 key = key_from_blob(pkblob, blen);
89 error("userauth_pubkey: cannot decode key: %s", pkalg);
92 if (key->type != pktype) {
93 error("userauth_pubkey: type mismatch for decoded key "
94 "(received %d, expected %d)", key->type, pktype);
98 sig = packet_get_string(&slen);
101 if (datafellows & SSH_OLD_SESSIONID) {
102 buffer_append(&b, session_id2, session_id2_len);
104 buffer_put_string(&b, session_id2, session_id2_len);
106 /* reconstruct packet */
107 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
108 buffer_put_cstring(&b, authctxt->user);
109 buffer_put_cstring(&b,
110 datafellows & SSH_BUG_PKSERVICE ?
113 if (datafellows & SSH_BUG_PKAUTH) {
114 buffer_put_char(&b, have_sig);
116 buffer_put_cstring(&b, "publickey");
117 buffer_put_char(&b, have_sig);
118 buffer_put_cstring(&b, pkalg);
120 buffer_put_string(&b, pkblob, blen);
124 /* test for correct signature */
126 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
127 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
128 buffer_len(&b))) == 1)
133 debug("test whether pkalg/pkblob are acceptable");
136 /* XXX fake reply and always send PK_OK ? */
138 * XXX this allows testing whether a user is allowed
139 * to login: if you happen to have a valid pubkey this
140 * message is sent. the message is NEVER sent at all
141 * if a user is not allowed to login. is this an
144 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
145 packet_start(SSH2_MSG_USERAUTH_PK_OK);
146 packet_put_string(pkalg, alen);
147 packet_put_string(pkblob, blen);
150 authctxt->postponed = 1;
153 if (authenticated != 1)
154 auth_clear_options();
156 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
162 if (check_nt_auth(0, authctxt->pw) == 0)
165 return authenticated;
168 /* return 1 if user allows given key */
170 user_key_allowed2(struct passwd *pw, Key *key, char *file)
172 char line[SSH_MAX_PUBKEY_BYTES];
180 /* Temporarily use the user's uid. */
181 temporarily_use_uid(pw);
183 debug("trying public key file %s", file);
185 /* Fail quietly if file does not exist */
186 if (stat(file, &st) < 0) {
187 /* Restore the privileged uid. */
191 /* Open the file containing the authorized keys. */
192 f = fopen(file, "r");
194 /* Restore the privileged uid. */
198 if (options.strict_modes &&
199 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
201 logit("Authentication refused: %s", line);
207 found = key_new(key->type);
209 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
210 char *cp, *key_options = NULL;
212 /* Skip leading whitespace, empty and comment lines. */
213 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
215 if (!*cp || *cp == '\n' || *cp == '#')
218 if (key_read(found, &cp) != 1) {
219 /* no key? check if there are options for this key */
221 debug2("user_key_allowed: check options: '%s'", cp);
223 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
224 if (*cp == '\\' && cp[1] == '"')
225 cp++; /* Skip both */
229 /* Skip remaining whitespace. */
230 for (; *cp == ' ' || *cp == '\t'; cp++)
232 if (key_read(found, &cp) != 1) {
233 debug2("user_key_allowed: advance: '%s'", cp);
234 /* still no key? advance to next line*/
238 if (key_equal(found, key) &&
239 auth_parse_options(pw, key_options, file, linenum) == 1) {
241 debug("matching key found: file %s, line %lu",
243 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
244 verbose("Found matching %s key: %s",
245 key_type(found), fp);
254 debug2("key not found");
258 /* check whether given key is in .ssh/authorized_keys* */
260 user_key_allowed(struct passwd *pw, Key *key)
265 file = authorized_keys_file(pw);
266 success = user_key_allowed2(pw, key, file);
271 /* try suffix "2" for backward compat, too */
272 file = authorized_keys_file2(pw);
273 success = user_key_allowed2(pw, key, file);
278 Authmethod method_pubkey = {
281 &options.pubkey_authentication