blob: 66ca5266b6537e3d136ee23e0ef66ec5f47e5a46 [file] [log] [blame]
Damien Miller0a80ca12010-02-27 07:55:05 +11001/* $OpenBSD: auth2-pubkey.c,v 1.20 2010/02/26 20:29:54 djm 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
Darren Tucker06db5842008-06-13 14:51:28 +100031#include <fcntl.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100032#include <pwd.h>
Damien Millera7a73ee2006-08-05 11:37:59 +100033#include <stdio.h>
Damien Millerd7834352006-08-05 12:39:39 +100034#include <stdarg.h>
Damien Miller0a80ca12010-02-27 07:55:05 +110035#include <string.h>
36#include <time.h>
Darren Tuckerd9526a52008-06-14 09:01:24 +100037#include <unistd.h>
Damien Miller9f2abc42006-07-10 20:53:08 +100038
Damien Millerd7834352006-08-05 12:39:39 +100039#include "xmalloc.h"
Darren Tucker22cc7412004-12-06 22:47:41 +110040#include "ssh.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000041#include "ssh2.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000042#include "packet.h"
43#include "buffer.h"
44#include "log.h"
45#include "servconf.h"
46#include "compat.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000047#include "key.h"
Damien Millerd7834352006-08-05 12:39:39 +100048#include "hostfile.h"
49#include "auth.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000050#include "pathnames.h"
51#include "uidswap.h"
52#include "auth-options.h"
53#include "canohost.h"
Damien Millerd7834352006-08-05 12:39:39 +100054#ifdef GSSAPI
55#include "ssh-gss.h"
56#endif
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000057#include "monitor_wrap.h"
Darren Tuckerf0f90982004-12-11 13:39:50 +110058#include "misc.h"
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000059
60/* import */
61extern ServerOptions options;
62extern u_char *session_id2;
Darren Tucker502d3842003-06-28 12:38:01 +100063extern u_int session_id2_len;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000064
65static int
66userauth_pubkey(Authctxt *authctxt)
67{
68 Buffer b;
69 Key *key = NULL;
70 char *pkalg;
71 u_char *pkblob, *sig;
72 u_int alen, blen, slen;
73 int have_sig, pktype;
74 int authenticated = 0;
75
76 if (!authctxt->valid) {
77 debug2("userauth_pubkey: disabled because of invalid user");
78 return 0;
79 }
80 have_sig = packet_get_char();
81 if (datafellows & SSH_BUG_PKAUTH) {
82 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
83 /* no explicit pkalg given */
84 pkblob = packet_get_string(&blen);
85 buffer_init(&b);
86 buffer_append(&b, pkblob, blen);
87 /* so we have to extract the pkalg from the pkblob */
88 pkalg = buffer_get_string(&b, &alen);
89 buffer_free(&b);
90 } else {
91 pkalg = packet_get_string(&alen);
92 pkblob = packet_get_string(&blen);
93 }
94 pktype = key_type_from_name(pkalg);
95 if (pktype == KEY_UNSPEC) {
96 /* this is perfectly legal */
Damien Miller996acd22003-04-09 20:59:48 +100097 logit("userauth_pubkey: unsupported public key algorithm: %s",
Ben Lindstrom855bf3a2002-06-06 20:27:55 +000098 pkalg);
99 goto done;
100 }
101 key = key_from_blob(pkblob, blen);
102 if (key == NULL) {
103 error("userauth_pubkey: cannot decode key: %s", pkalg);
104 goto done;
105 }
106 if (key->type != pktype) {
107 error("userauth_pubkey: type mismatch for decoded key "
108 "(received %d, expected %d)", key->type, pktype);
109 goto done;
110 }
111 if (have_sig) {
112 sig = packet_get_string(&slen);
113 packet_check_eom();
114 buffer_init(&b);
115 if (datafellows & SSH_OLD_SESSIONID) {
116 buffer_append(&b, session_id2, session_id2_len);
117 } else {
118 buffer_put_string(&b, session_id2, session_id2_len);
119 }
120 /* reconstruct packet */
121 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
122 buffer_put_cstring(&b, authctxt->user);
123 buffer_put_cstring(&b,
124 datafellows & SSH_BUG_PKSERVICE ?
125 "ssh-userauth" :
126 authctxt->service);
127 if (datafellows & SSH_BUG_PKAUTH) {
128 buffer_put_char(&b, have_sig);
129 } else {
130 buffer_put_cstring(&b, "publickey");
131 buffer_put_char(&b, have_sig);
132 buffer_put_cstring(&b, pkalg);
133 }
134 buffer_put_string(&b, pkblob, blen);
135#ifdef DEBUG_PK
136 buffer_dump(&b);
137#endif
138 /* test for correct signature */
139 authenticated = 0;
140 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
141 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
Damien Millerfb1310e2004-01-21 11:02:50 +1100142 buffer_len(&b))) == 1)
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000143 authenticated = 1;
Damien Millerfb1310e2004-01-21 11:02:50 +1100144 buffer_free(&b);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000145 xfree(sig);
146 } else {
147 debug("test whether pkalg/pkblob are acceptable");
148 packet_check_eom();
149
150 /* XXX fake reply and always send PK_OK ? */
151 /*
152 * XXX this allows testing whether a user is allowed
153 * to login: if you happen to have a valid pubkey this
154 * message is sent. the message is NEVER sent at all
155 * if a user is not allowed to login. is this an
156 * issue? -markus
157 */
158 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
159 packet_start(SSH2_MSG_USERAUTH_PK_OK);
160 packet_put_string(pkalg, alen);
161 packet_put_string(pkblob, blen);
162 packet_send();
163 packet_write_wait();
164 authctxt->postponed = 1;
165 }
166 }
167 if (authenticated != 1)
168 auth_clear_options();
169done:
170 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
171 if (key != NULL)
172 key_free(key);
173 xfree(pkalg);
174 xfree(pkblob);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000175 return authenticated;
176}
177
178/* return 1 if user allows given key */
179static int
180user_key_allowed2(struct passwd *pw, Key *key, char *file)
181{
Darren Tucker22cc7412004-12-06 22:47:41 +1100182 char line[SSH_MAX_PUBKEY_BYTES];
Damien Miller0a80ca12010-02-27 07:55:05 +1100183 const char *reason;
Darren Tucker33c787f2008-07-02 22:37:30 +1000184 int found_key = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000185 FILE *f;
186 u_long linenum = 0;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000187 Key *found;
188 char *fp;
189
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000190 /* Temporarily use the user's uid. */
191 temporarily_use_uid(pw);
192
193 debug("trying public key file %s", file);
Darren Tucker33c787f2008-07-02 22:37:30 +1000194 f = auth_openkeyfile(file, pw, options.strict_modes);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000195
Darren Tucker33c787f2008-07-02 22:37:30 +1000196 if (!f) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000197 restore_uid();
198 return 0;
199 }
200
201 found_key = 0;
Damien Miller0a80ca12010-02-27 07:55:05 +1100202 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000203
Darren Tucker22cc7412004-12-06 22:47:41 +1100204 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000205 char *cp, *key_options = NULL;
Darren Tucker22cc7412004-12-06 22:47:41 +1100206
Damien Miller0a80ca12010-02-27 07:55:05 +1100207 auth_clear_options();
208
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000209 /* Skip leading whitespace, empty and comment lines. */
210 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
211 ;
212 if (!*cp || *cp == '\n' || *cp == '#')
213 continue;
214
215 if (key_read(found, &cp) != 1) {
216 /* no key? check if there are options for this key */
217 int quoted = 0;
218 debug2("user_key_allowed: check options: '%s'", cp);
Darren Tucker3f9fdc72004-06-22 12:56:01 +1000219 key_options = cp;
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000220 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
221 if (*cp == '\\' && cp[1] == '"')
222 cp++; /* Skip both */
223 else if (*cp == '"')
224 quoted = !quoted;
225 }
226 /* Skip remaining whitespace. */
227 for (; *cp == ' ' || *cp == '\t'; cp++)
228 ;
229 if (key_read(found, &cp) != 1) {
230 debug2("user_key_allowed: advance: '%s'", cp);
231 /* still no key? advance to next line*/
232 continue;
233 }
234 }
Damien Miller0a80ca12010-02-27 07:55:05 +1100235 if (auth_parse_options(pw, key_options, file, linenum) != 1)
236 continue;
237 if (key->type == KEY_RSA_CERT || key->type == KEY_DSA_CERT) {
238 if (!key_is_cert_authority)
239 continue;
240 if (!key_equal(found, key->cert->signature_key))
241 continue;
242 debug("matching CA found: file %s, line %lu",
243 file, linenum);
244 fp = key_fingerprint(found, SSH_FP_MD5,
245 SSH_FP_HEX);
246 verbose("Found matching %s CA: %s",
247 key_type(found), fp);
248 xfree(fp);
249 if (key_cert_check_authority(key, 0, 0, pw->pw_name,
250 &reason) != 0) {
251 error("%s", reason);
252 auth_debug_add("%s", reason);
253 continue;
254 }
255 if (auth_cert_constraints(&key->cert->constraints,
256 pw) != 0)
257 continue;
258 found_key = 1;
259 break;
260 } else if (!key_is_cert_authority && key_equal(found, key)) {
Ben Lindstrom855bf3a2002-06-06 20:27:55 +0000261 found_key = 1;
262 debug("matching key found: file %s, line %lu",
263 file, linenum);
264 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
265 verbose("Found matching %s key: %s",
266 key_type(found), fp);
267 xfree(fp);
268 break;
269 }
270 }
271 restore_uid();
272 fclose(f);
273 key_free(found);
274 if (!found_key)
275 debug2("key not found");
276 return found_key;
277}
278
279/* check whether given key is in .ssh/authorized_keys* */
280int
281user_key_allowed(struct passwd *pw, Key *key)
282{
283 int success;
284 char *file;
285
286 file = authorized_keys_file(pw);
287 success = user_key_allowed2(pw, key, file);
288 xfree(file);
289 if (success)
290 return success;
291
292 /* try suffix "2" for backward compat, too */
293 file = authorized_keys_file2(pw);
294 success = user_key_allowed2(pw, key, file);
295 xfree(file);
296 return success;
297}
298
299Authmethod method_pubkey = {
300 "publickey",
301 userauth_pubkey,
302 &options.pubkey_authentication
303};