Add blacklist feature for weak Debian-generated ssh keys.
[dragonfly.git] / crypto / openssh-5 / auth2-pubkey.c
1 /* $OpenBSD: auth2-pubkey.c,v 1.15 2006/08/03 03:34:41 deraadt Exp $ */
2 /*
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"
27
28 #include <sys/types.h>
29 #include <sys/stat.h>
30
31 #include <pwd.h>
32 #include <stdio.h>
33 #include <stdarg.h>
34
35 #include "xmalloc.h"
36 #include "ssh.h"
37 #include "ssh2.h"
38 #include "packet.h"
39 #include "buffer.h"
40 #include "log.h"
41 #include "servconf.h"
42 #include "compat.h"
43 #include "key.h"
44 #include "hostfile.h"
45 #include "authfile.h"
46 #include "auth.h"
47 #include "pathnames.h"
48 #include "uidswap.h"
49 #include "auth-options.h"
50 #include "canohost.h"
51 #ifdef GSSAPI
52 #include "ssh-gss.h"
53 #endif
54 #include "monitor_wrap.h"
55 #include "misc.h"
56
57 /* import */
58 extern ServerOptions options;
59 extern u_char *session_id2;
60 extern u_int session_id2_len;
61
62 static int
63 userauth_pubkey(Authctxt *authctxt)
64 {
65         Buffer b;
66         Key *key = NULL;
67         char *pkalg;
68         u_char *pkblob, *sig;
69         u_int alen, blen, slen;
70         int have_sig, pktype;
71         int authenticated = 0;
72
73         if (!authctxt->valid) {
74                 debug2("userauth_pubkey: disabled because of invalid user");
75                 return 0;
76         }
77         have_sig = packet_get_char();
78         if (datafellows & SSH_BUG_PKAUTH) {
79                 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
80                 /* no explicit pkalg given */
81                 pkblob = packet_get_string(&blen);
82                 buffer_init(&b);
83                 buffer_append(&b, pkblob, blen);
84                 /* so we have to extract the pkalg from the pkblob */
85                 pkalg = buffer_get_string(&b, &alen);
86                 buffer_free(&b);
87         } else {
88                 pkalg = packet_get_string(&alen);
89                 pkblob = packet_get_string(&blen);
90         }
91         pktype = key_type_from_name(pkalg);
92         if (pktype == KEY_UNSPEC) {
93                 /* this is perfectly legal */
94                 logit("userauth_pubkey: unsupported public key algorithm: %s",
95                     pkalg);
96                 goto done;
97         }
98         key = key_from_blob(pkblob, blen);
99         if (key == NULL) {
100                 error("userauth_pubkey: cannot decode key: %s", pkalg);
101                 goto done;
102         }
103         if (key->type != pktype) {
104                 error("userauth_pubkey: type mismatch for decoded key "
105                     "(received %d, expected %d)", key->type, pktype);
106                 goto done;
107         }
108         if (have_sig) {
109                 sig = packet_get_string(&slen);
110                 packet_check_eom();
111                 buffer_init(&b);
112                 if (datafellows & SSH_OLD_SESSIONID) {
113                         buffer_append(&b, session_id2, session_id2_len);
114                 } else {
115                         buffer_put_string(&b, session_id2, session_id2_len);
116                 }
117                 /* reconstruct packet */
118                 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
119                 buffer_put_cstring(&b, authctxt->user);
120                 buffer_put_cstring(&b,
121                     datafellows & SSH_BUG_PKSERVICE ?
122                     "ssh-userauth" :
123                     authctxt->service);
124                 if (datafellows & SSH_BUG_PKAUTH) {
125                         buffer_put_char(&b, have_sig);
126                 } else {
127                         buffer_put_cstring(&b, "publickey");
128                         buffer_put_char(&b, have_sig);
129                         buffer_put_cstring(&b, pkalg);
130                 }
131                 buffer_put_string(&b, pkblob, blen);
132 #ifdef DEBUG_PK
133                 buffer_dump(&b);
134 #endif
135                 /* test for correct signature */
136                 authenticated = 0;
137                 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
138                     PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
139                     buffer_len(&b))) == 1)
140                         authenticated = 1;
141                 buffer_free(&b);
142                 xfree(sig);
143         } else {
144                 debug("test whether pkalg/pkblob are acceptable");
145                 packet_check_eom();
146
147                 /* XXX fake reply and always send PK_OK ? */
148                 /*
149                  * XXX this allows testing whether a user is allowed
150                  * to login: if you happen to have a valid pubkey this
151                  * message is sent. the message is NEVER sent at all
152                  * if a user is not allowed to login. is this an
153                  * issue? -markus
154                  */
155                 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
156                         packet_start(SSH2_MSG_USERAUTH_PK_OK);
157                         packet_put_string(pkalg, alen);
158                         packet_put_string(pkblob, blen);
159                         packet_send();
160                         packet_write_wait();
161                         authctxt->postponed = 1;
162                 }
163         }
164         if (authenticated != 1)
165                 auth_clear_options();
166 done:
167         debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
168         if (key != NULL)
169                 key_free(key);
170         xfree(pkalg);
171         xfree(pkblob);
172 #ifdef HAVE_CYGWIN
173         if (check_nt_auth(0, authctxt->pw) == 0)
174                 authenticated = 0;
175 #endif
176         return authenticated;
177 }
178
179 /* return 1 if user allows given key */
180 static int
181 user_key_allowed2(struct passwd *pw, Key *key, char *file)
182 {
183         char line[SSH_MAX_PUBKEY_BYTES];
184         int found_key = 0;
185         FILE *f;
186         u_long linenum = 0;
187         struct stat st;
188         Key *found;
189         char *fp;
190
191         /* Temporarily use the user's uid. */
192         temporarily_use_uid(pw);
193
194         debug("trying public key file %s", file);
195
196         /* Fail quietly if file does not exist */
197         if (stat(file, &st) < 0) {
198                 /* Restore the privileged uid. */
199                 restore_uid();
200                 return 0;
201         }
202         /* Open the file containing the authorized keys. */
203         f = fopen(file, "r");
204         if (!f) {
205                 /* Restore the privileged uid. */
206                 restore_uid();
207                 return 0;
208         }
209         if (options.strict_modes &&
210             secure_filename(f, file, pw, line, sizeof(line)) != 0) {
211                 fclose(f);
212                 logit("Authentication refused: %s", line);
213                 restore_uid();
214                 return 0;
215         }
216
217         found_key = 0;
218         found = key_new(key->type);
219
220         while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
221                 char *cp, *key_options = NULL;
222
223                 /* Skip leading whitespace, empty and comment lines. */
224                 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
225                         ;
226                 if (!*cp || *cp == '\n' || *cp == '#')
227                         continue;
228
229                 if (key_read(found, &cp) != 1) {
230                         /* no key?  check if there are options for this key */
231                         int quoted = 0;
232                         debug2("user_key_allowed: check options: '%s'", cp);
233                         key_options = cp;
234                         for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
235                                 if (*cp == '\\' && cp[1] == '"')
236                                         cp++;   /* Skip both */
237                                 else if (*cp == '"')
238                                         quoted = !quoted;
239                         }
240                         /* Skip remaining whitespace. */
241                         for (; *cp == ' ' || *cp == '\t'; cp++)
242                                 ;
243                         if (key_read(found, &cp) != 1) {
244                                 debug2("user_key_allowed: advance: '%s'", cp);
245                                 /* still no key?  advance to next line*/
246                                 continue;
247                         }
248                 }
249                 if (key_equal(found, key) &&
250                     auth_parse_options(pw, key_options, file, linenum) == 1) {
251                         found_key = 1;
252                         debug("matching key found: file %s, line %lu",
253                             file, linenum);
254                         fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
255                         verbose("Found matching %s key: %s",
256                             key_type(found), fp);
257                         xfree(fp);
258                         break;
259                 }
260         }
261         restore_uid();
262         fclose(f);
263         key_free(found);
264         if (!found_key)
265                 debug2("key not found");
266         return found_key;
267 }
268
269 /* check whether given key is in .ssh/authorized_keys* */
270 int
271 user_key_allowed(struct passwd *pw, Key *key)
272 {
273         char *fp;
274         int success;
275         char *file;
276
277         if (blacklisted_key(key)) {
278                 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
279                 if (options.permit_blacklisted_keys)
280                         logit("Public key %s blacklisted (see "
281                             "ssh-vulnkey(1)); continuing anyway", fp);
282                 else
283                         logit("Public key %s blacklisted (see "
284                             "ssh-vulnkey(1))", fp);
285                 xfree(fp);
286                 if (!options.permit_blacklisted_keys)
287                         return 0;
288         }
289
290         file = authorized_keys_file(pw);
291         success = user_key_allowed2(pw, key, file);
292         xfree(file);
293         if (success)
294                 return success;
295
296         /* try suffix "2" for backward compat, too */
297         file = authorized_keys_file2(pw);
298         success = user_key_allowed2(pw, key, file);
299         xfree(file);
300         return success;
301 }
302
303 Authmethod method_pubkey = {
304         "publickey",
305         userauth_pubkey,
306         &options.pubkey_authentication
307 };