blob: 41763f7632c3ea31e88cc2cb8762eafed4a9ee2c [file] [log] [blame]
Damien Millera7a73ee2006-08-05 11:37:59 +10001/* $OpenBSD: auth2-pubkey.c,v 1.14 2006/08/01 23:22:47 stevesk Exp $ */
Ben Lindstrom855bf3a2002-06-06 20:27:55 +00002/*
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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.
24 */
25
26#include "includes.h"
Damien Millerf17883e2006-03-15 11:45:54 +110027
28#include <sys/types.h>
29#include <sys/stat.h>
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000030
Damien Miller9f2abc42006-07-10 20:53:08 +100031#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100032#include <stdio.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100033
Darren Tucker22cc7412004-12-06 22:47:41 +110034#include "ssh.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000035#include "ssh2.h"
36#include "xmalloc.h"
37#include "packet.h"
38#include "buffer.h"
39#include "log.h"
40#include "servconf.h"
41#include "compat.h"
42#include "bufaux.h"
43#include "auth.h"
44#include "key.h"
45#include "pathnames.h"
46#include "uidswap.h"
47#include "auth-options.h"
48#include "canohost.h"
49#include "monitor_wrap.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110050#include "misc.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000051
52/* import */
53extern ServerOptions options;
54extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100055extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000056
57static int
58userauth_pubkey(Authctxt *authctxt)
59{
60 Buffer b;
61 Key *key = NULL;
62 char *pkalg;
63 u_char *pkblob, *sig;
64 u_int alen, blen, slen;
65 int have_sig, pktype;
66 int authenticated = 0;
67
68 if (!authctxt->valid) {
69 debug2("userauth_pubkey: disabled because of invalid user");
70 return 0;
71 }
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);
77 buffer_init(&b);
78 buffer_append(&b, pkblob, blen);
79 /* so we have to extract the pkalg from the pkblob */
80 pkalg = buffer_get_string(&b, &alen);
81 buffer_free(&b);
82 } else {
83 pkalg = packet_get_string(&alen);
84 pkblob = packet_get_string(&blen);
85 }
86 pktype = key_type_from_name(pkalg);
87 if (pktype == KEY_UNSPEC) {
88 /* this is perfectly legal */
Damien Miller996acd22003-04-09 20:59:48 +100089 logit("userauth_pubkey: unsupported public key algorithm: %s",
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000090 pkalg);
91 goto done;
92 }
93 key = key_from_blob(pkblob, blen);
94 if (key == NULL) {
95 error("userauth_pubkey: cannot decode key: %s", pkalg);
96 goto done;
97 }
98 if (key->type != pktype) {
99 error("userauth_pubkey: type mismatch for decoded key "
100 "(received %d, expected %d)", key->type, pktype);
101 goto done;
102 }
103 if (have_sig) {
104 sig = packet_get_string(&slen);
105 packet_check_eom();
106 buffer_init(&b);
107 if (datafellows & SSH_OLD_SESSIONID) {
108 buffer_append(&b, session_id2, session_id2_len);
109 } else {
110 buffer_put_string(&b, session_id2, session_id2_len);
111 }
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 ?
117 "ssh-userauth" :
118 authctxt->service);
119 if (datafellows & SSH_BUG_PKAUTH) {
120 buffer_put_char(&b, have_sig);
121 } else {
122 buffer_put_cstring(&b, "publickey");
123 buffer_put_char(&b, have_sig);
124 buffer_put_cstring(&b, pkalg);
125 }
126 buffer_put_string(&b, pkblob, blen);
127#ifdef DEBUG_PK
128 buffer_dump(&b);
129#endif
130 /* test for correct signature */
131 authenticated = 0;
132 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
133 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
Damien Millerfb1310e2004-01-21 11:02:50 +1100134 buffer_len(&b))) == 1)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000135 authenticated = 1;
Damien Millerfb1310e2004-01-21 11:02:50 +1100136 buffer_free(&b);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000137 xfree(sig);
138 } else {
139 debug("test whether pkalg/pkblob are acceptable");
140 packet_check_eom();
141
142 /* XXX fake reply and always send PK_OK ? */
143 /*
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
148 * issue? -markus
149 */
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);
154 packet_send();
155 packet_write_wait();
156 authctxt->postponed = 1;
157 }
158 }
159 if (authenticated != 1)
160 auth_clear_options();
161done:
162 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
163 if (key != NULL)
164 key_free(key);
165 xfree(pkalg);
166 xfree(pkblob);
167#ifdef HAVE_CYGWIN
168 if (check_nt_auth(0, authctxt->pw) == 0)
Damien Miller47656792004-09-11 22:42:09 +1000169 authenticated = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000170#endif
171 return authenticated;
172}
173
174/* return 1 if user allows given key */
175static int
176user_key_allowed2(struct passwd *pw, Key *key, char *file)
177{
Darren Tucker22cc7412004-12-06 22:47:41 +1100178 char line[SSH_MAX_PUBKEY_BYTES];
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000179 int found_key = 0;
180 FILE *f;
181 u_long linenum = 0;
182 struct stat st;
183 Key *found;
184 char *fp;
185
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000186 /* Temporarily use the user's uid. */
187 temporarily_use_uid(pw);
188
189 debug("trying public key file %s", file);
190
191 /* Fail quietly if file does not exist */
192 if (stat(file, &st) < 0) {
193 /* Restore the privileged uid. */
194 restore_uid();
195 return 0;
196 }
197 /* Open the file containing the authorized keys. */
198 f = fopen(file, "r");
199 if (!f) {
200 /* Restore the privileged uid. */
201 restore_uid();
202 return 0;
203 }
204 if (options.strict_modes &&
205 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
206 fclose(f);
Damien Miller996acd22003-04-09 20:59:48 +1000207 logit("Authentication refused: %s", line);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000208 restore_uid();
209 return 0;
210 }
211
212 found_key = 0;
213 found = key_new(key->type);
214
Darren Tucker22cc7412004-12-06 22:47:41 +1100215 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000216 char *cp, *key_options = NULL;
Darren Tucker22cc7412004-12-06 22:47:41 +1100217
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000218 /* Skip leading whitespace, empty and comment lines. */
219 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
220 ;
221 if (!*cp || *cp == '\n' || *cp == '#')
222 continue;
223
224 if (key_read(found, &cp) != 1) {
225 /* no key? check if there are options for this key */
226 int quoted = 0;
227 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000228 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000229 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
230 if (*cp == '\\' && cp[1] == '"')
231 cp++; /* Skip both */
232 else if (*cp == '"')
233 quoted = !quoted;
234 }
235 /* Skip remaining whitespace. */
236 for (; *cp == ' ' || *cp == '\t'; cp++)
237 ;
238 if (key_read(found, &cp) != 1) {
239 debug2("user_key_allowed: advance: '%s'", cp);
240 /* still no key? advance to next line*/
241 continue;
242 }
243 }
244 if (key_equal(found, key) &&
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000245 auth_parse_options(pw, key_options, file, linenum) == 1) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000246 found_key = 1;
247 debug("matching key found: file %s, line %lu",
248 file, linenum);
249 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
250 verbose("Found matching %s key: %s",
251 key_type(found), fp);
252 xfree(fp);
253 break;
254 }
255 }
256 restore_uid();
257 fclose(f);
258 key_free(found);
259 if (!found_key)
260 debug2("key not found");
261 return found_key;
262}
263
264/* check whether given key is in .ssh/authorized_keys* */
265int
266user_key_allowed(struct passwd *pw, Key *key)
267{
268 int success;
269 char *file;
270
271 file = authorized_keys_file(pw);
272 success = user_key_allowed2(pw, key, file);
273 xfree(file);
274 if (success)
275 return success;
276
277 /* try suffix "2" for backward compat, too */
278 file = authorized_keys_file2(pw);
279 success = user_key_allowed2(pw, key, file);
280 xfree(file);
281 return success;
282}
283
284Authmethod method_pubkey = {
285 "publickey",
286 userauth_pubkey,
287 &options.pubkey_authentication
288};