blob: a97d0f430b93f298b0111ac6e8eeb8d78db15caf [file] [log] [blame]
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00001/*
2 * Copyright (c) 2000 Markus Friedl. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
23 */
24
25#include "includes.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110026RCSID("$OpenBSD: auth2-pubkey.c,v 1.9 2004/12/11 01:48:56 dtucker Exp $");
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000027
Darren Tucker22cc7412004-12-06 22:47:41 +110028#include "ssh.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000029#include "ssh2.h"
30#include "xmalloc.h"
31#include "packet.h"
32#include "buffer.h"
33#include "log.h"
34#include "servconf.h"
35#include "compat.h"
36#include "bufaux.h"
37#include "auth.h"
38#include "key.h"
39#include "pathnames.h"
40#include "uidswap.h"
41#include "auth-options.h"
42#include "canohost.h"
43#include "monitor_wrap.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110044#include "misc.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000045
46/* import */
47extern ServerOptions options;
48extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100049extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000050
51static int
52userauth_pubkey(Authctxt *authctxt)
53{
54 Buffer b;
55 Key *key = NULL;
56 char *pkalg;
57 u_char *pkblob, *sig;
58 u_int alen, blen, slen;
59 int have_sig, pktype;
60 int authenticated = 0;
61
62 if (!authctxt->valid) {
63 debug2("userauth_pubkey: disabled because of invalid user");
64 return 0;
65 }
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);
71 buffer_init(&b);
72 buffer_append(&b, pkblob, blen);
73 /* so we have to extract the pkalg from the pkblob */
74 pkalg = buffer_get_string(&b, &alen);
75 buffer_free(&b);
76 } else {
77 pkalg = packet_get_string(&alen);
78 pkblob = packet_get_string(&blen);
79 }
80 pktype = key_type_from_name(pkalg);
81 if (pktype == KEY_UNSPEC) {
82 /* this is perfectly legal */
Damien Miller996acd22003-04-09 20:59:48 +100083 logit("userauth_pubkey: unsupported public key algorithm: %s",
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000084 pkalg);
85 goto done;
86 }
87 key = key_from_blob(pkblob, blen);
88 if (key == NULL) {
89 error("userauth_pubkey: cannot decode key: %s", pkalg);
90 goto done;
91 }
92 if (key->type != pktype) {
93 error("userauth_pubkey: type mismatch for decoded key "
94 "(received %d, expected %d)", key->type, pktype);
95 goto done;
96 }
97 if (have_sig) {
98 sig = packet_get_string(&slen);
99 packet_check_eom();
100 buffer_init(&b);
101 if (datafellows & SSH_OLD_SESSIONID) {
102 buffer_append(&b, session_id2, session_id2_len);
103 } else {
104 buffer_put_string(&b, session_id2, session_id2_len);
105 }
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 ?
111 "ssh-userauth" :
112 authctxt->service);
113 if (datafellows & SSH_BUG_PKAUTH) {
114 buffer_put_char(&b, have_sig);
115 } else {
116 buffer_put_cstring(&b, "publickey");
117 buffer_put_char(&b, have_sig);
118 buffer_put_cstring(&b, pkalg);
119 }
120 buffer_put_string(&b, pkblob, blen);
121#ifdef DEBUG_PK
122 buffer_dump(&b);
123#endif
124 /* test for correct signature */
125 authenticated = 0;
126 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
127 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
Damien Millerfb1310e2004-01-21 11:02:50 +1100128 buffer_len(&b))) == 1)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000129 authenticated = 1;
Damien Millerfb1310e2004-01-21 11:02:50 +1100130 buffer_free(&b);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000131 xfree(sig);
132 } else {
133 debug("test whether pkalg/pkblob are acceptable");
134 packet_check_eom();
135
136 /* XXX fake reply and always send PK_OK ? */
137 /*
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
142 * issue? -markus
143 */
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);
148 packet_send();
149 packet_write_wait();
150 authctxt->postponed = 1;
151 }
152 }
153 if (authenticated != 1)
154 auth_clear_options();
155done:
156 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
157 if (key != NULL)
158 key_free(key);
159 xfree(pkalg);
160 xfree(pkblob);
161#ifdef HAVE_CYGWIN
162 if (check_nt_auth(0, authctxt->pw) == 0)
Damien Miller47656792004-09-11 22:42:09 +1000163 authenticated = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000164#endif
165 return authenticated;
166}
167
168/* return 1 if user allows given key */
169static int
170user_key_allowed2(struct passwd *pw, Key *key, char *file)
171{
Darren Tucker22cc7412004-12-06 22:47:41 +1100172 char line[SSH_MAX_PUBKEY_BYTES];
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000173 int found_key = 0;
174 FILE *f;
175 u_long linenum = 0;
176 struct stat st;
177 Key *found;
178 char *fp;
179
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000180 /* Temporarily use the user's uid. */
181 temporarily_use_uid(pw);
182
183 debug("trying public key file %s", file);
184
185 /* Fail quietly if file does not exist */
186 if (stat(file, &st) < 0) {
187 /* Restore the privileged uid. */
188 restore_uid();
189 return 0;
190 }
191 /* Open the file containing the authorized keys. */
192 f = fopen(file, "r");
193 if (!f) {
194 /* Restore the privileged uid. */
195 restore_uid();
196 return 0;
197 }
198 if (options.strict_modes &&
199 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
200 fclose(f);
Damien Miller996acd22003-04-09 20:59:48 +1000201 logit("Authentication refused: %s", line);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000202 restore_uid();
203 return 0;
204 }
205
206 found_key = 0;
207 found = key_new(key->type);
208
Darren Tucker22cc7412004-12-06 22:47:41 +1100209 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000210 char *cp, *key_options = NULL;
Darren Tucker22cc7412004-12-06 22:47:41 +1100211
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000212 /* Skip leading whitespace, empty and comment lines. */
213 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
214 ;
215 if (!*cp || *cp == '\n' || *cp == '#')
216 continue;
217
218 if (key_read(found, &cp) != 1) {
219 /* no key? check if there are options for this key */
220 int quoted = 0;
221 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000222 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000223 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
224 if (*cp == '\\' && cp[1] == '"')
225 cp++; /* Skip both */
226 else if (*cp == '"')
227 quoted = !quoted;
228 }
229 /* Skip remaining whitespace. */
230 for (; *cp == ' ' || *cp == '\t'; cp++)
231 ;
232 if (key_read(found, &cp) != 1) {
233 debug2("user_key_allowed: advance: '%s'", cp);
234 /* still no key? advance to next line*/
235 continue;
236 }
237 }
238 if (key_equal(found, key) &&
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000239 auth_parse_options(pw, key_options, file, linenum) == 1) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000240 found_key = 1;
241 debug("matching key found: file %s, line %lu",
242 file, linenum);
243 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
244 verbose("Found matching %s key: %s",
245 key_type(found), fp);
246 xfree(fp);
247 break;
248 }
249 }
250 restore_uid();
251 fclose(f);
252 key_free(found);
253 if (!found_key)
254 debug2("key not found");
255 return found_key;
256}
257
258/* check whether given key is in .ssh/authorized_keys* */
259int
260user_key_allowed(struct passwd *pw, Key *key)
261{
262 int success;
263 char *file;
264
265 file = authorized_keys_file(pw);
266 success = user_key_allowed2(pw, key, file);
267 xfree(file);
268 if (success)
269 return success;
270
271 /* try suffix "2" for backward compat, too */
272 file = authorized_keys_file2(pw);
273 success = user_key_allowed2(pw, key, file);
274 xfree(file);
275 return success;
276}
277
278Authmethod method_pubkey = {
279 "publickey",
280 userauth_pubkey,
281 &options.pubkey_authentication
282};