1 /* $OpenBSD: auth2-pubkey.c,v 1.14 2006/08/01 23:22:47 stevesk 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>
45 #include "pathnames.h"
47 #include "auth-options.h"
49 #include "monitor_wrap.h"
53 extern ServerOptions options;
54 extern u_char *session_id2;
55 extern u_int session_id2_len;
58 userauth_pubkey(Authctxt *authctxt)
64 u_int alen, blen, slen;
66 int authenticated = 0;
68 if (!authctxt->valid) {
69 debug2("userauth_pubkey: disabled because of invalid user");
72 have_sig = packet_get_char();
73 if (datafellows & SSH_BUG_PKAUTH) {
74 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
75 /* no explicit pkalg given */
76 pkblob = packet_get_string(&blen);
78 buffer_append(&b, pkblob, blen);
79 /* so we have to extract the pkalg from the pkblob */
80 pkalg = buffer_get_string(&b, &alen);
83 pkalg = packet_get_string(&alen);
84 pkblob = packet_get_string(&blen);
86 pktype = key_type_from_name(pkalg);
87 if (pktype == KEY_UNSPEC) {
88 /* this is perfectly legal */
89 logit("userauth_pubkey: unsupported public key algorithm: %s",
93 key = key_from_blob(pkblob, blen);
95 error("userauth_pubkey: cannot decode key: %s", pkalg);
98 if (key->type != pktype) {
99 error("userauth_pubkey: type mismatch for decoded key "
100 "(received %d, expected %d)", key->type, pktype);
104 sig = packet_get_string(&slen);
107 if (datafellows & SSH_OLD_SESSIONID) {
108 buffer_append(&b, session_id2, session_id2_len);
110 buffer_put_string(&b, session_id2, session_id2_len);
112 /* reconstruct packet */
113 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
114 buffer_put_cstring(&b, authctxt->user);
115 buffer_put_cstring(&b,
116 datafellows & SSH_BUG_PKSERVICE ?
119 if (datafellows & SSH_BUG_PKAUTH) {
120 buffer_put_char(&b, have_sig);
122 buffer_put_cstring(&b, "publickey");
123 buffer_put_char(&b, have_sig);
124 buffer_put_cstring(&b, pkalg);
126 buffer_put_string(&b, pkblob, blen);
130 /* test for correct signature */
132 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
133 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
134 buffer_len(&b))) == 1)
139 debug("test whether pkalg/pkblob are acceptable");
142 /* XXX fake reply and always send PK_OK ? */
144 * XXX this allows testing whether a user is allowed
145 * to login: if you happen to have a valid pubkey this
146 * message is sent. the message is NEVER sent at all
147 * if a user is not allowed to login. is this an
150 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
151 packet_start(SSH2_MSG_USERAUTH_PK_OK);
152 packet_put_string(pkalg, alen);
153 packet_put_string(pkblob, blen);
156 authctxt->postponed = 1;
159 if (authenticated != 1)
160 auth_clear_options();
162 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
168 if (check_nt_auth(0, authctxt->pw) == 0)
171 return authenticated;
174 /* return 1 if user allows given key */
176 user_key_allowed2(struct passwd *pw, Key *key, char *file)
178 char line[SSH_MAX_PUBKEY_BYTES];
186 /* Temporarily use the user's uid. */
187 temporarily_use_uid(pw);
189 debug("trying public key file %s", file);
191 /* Fail quietly if file does not exist */
192 if (stat(file, &st) < 0) {
193 /* Restore the privileged uid. */
197 /* Open the file containing the authorized keys. */
198 f = fopen(file, "r");
200 /* Restore the privileged uid. */
204 if (options.strict_modes &&
205 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
207 logit("Authentication refused: %s", line);
213 found = key_new(key->type);
215 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
216 char *cp, *key_options = NULL;
218 /* Skip leading whitespace, empty and comment lines. */
219 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
221 if (!*cp || *cp == '\n' || *cp == '#')
224 if (key_read(found, &cp) != 1) {
225 /* no key? check if there are options for this key */
227 debug2("user_key_allowed: check options: '%s'", cp);
229 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
230 if (*cp == '\\' && cp[1] == '"')
231 cp++; /* Skip both */
235 /* Skip remaining whitespace. */
236 for (; *cp == ' ' || *cp == '\t'; cp++)
238 if (key_read(found, &cp) != 1) {
239 debug2("user_key_allowed: advance: '%s'", cp);
240 /* still no key? advance to next line*/
244 if (key_equal(found, key) &&
245 auth_parse_options(pw, key_options, file, linenum) == 1) {
247 debug("matching key found: file %s, line %lu",
249 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
250 verbose("Found matching %s key: %s",
251 key_type(found), fp);
260 debug2("key not found");
264 /* check whether given key is in .ssh/authorized_keys* */
266 user_key_allowed(struct passwd *pw, Key *key)
271 file = authorized_keys_file(pw);
272 success = user_key_allowed2(pw, key, file);
277 /* try suffix "2" for backward compat, too */
278 file = authorized_keys_file2(pw);
279 success = user_key_allowed2(pw, key, file);
284 Authmethod method_pubkey = {
287 &options.pubkey_authentication