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