1 /* $OpenBSD: auth2-pubkey.c,v 1.19 2008/07/03 21:46:58 otto Exp $ */
3 * Copyright (c) 2000 Markus Friedl. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
28 #include <sys/types.h>
48 #include "pathnames.h"
50 #include "auth-options.h"
55 #include "monitor_wrap.h"
59 extern ServerOptions options
;
60 extern u_char
*session_id2
;
61 extern u_int session_id2_len
;
64 userauth_pubkey(Authctxt
*authctxt
)
70 u_int alen
, blen
, slen
;
72 int authenticated
= 0;
74 if (!authctxt
->valid
) {
75 debug2("userauth_pubkey: disabled because of invalid user");
78 have_sig
= packet_get_char();
79 if (datafellows
& SSH_BUG_PKAUTH
) {
80 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
81 /* no explicit pkalg given */
82 pkblob
= packet_get_string(&blen
);
84 buffer_append(&b
, pkblob
, blen
);
85 /* so we have to extract the pkalg from the pkblob */
86 pkalg
= buffer_get_string(&b
, &alen
);
89 pkalg
= packet_get_string(&alen
);
90 pkblob
= packet_get_string(&blen
);
92 pktype
= key_type_from_name(pkalg
);
93 if (pktype
== KEY_UNSPEC
) {
94 /* this is perfectly legal */
95 logit("userauth_pubkey: unsupported public key algorithm: %s",
99 key
= key_from_blob(pkblob
, blen
);
101 error("userauth_pubkey: cannot decode key: %s", pkalg
);
104 if (key
->type
!= pktype
) {
105 error("userauth_pubkey: type mismatch for decoded key "
106 "(received %d, expected %d)", key
->type
, pktype
);
110 sig
= packet_get_string(&slen
);
113 if (datafellows
& SSH_OLD_SESSIONID
) {
114 buffer_append(&b
, session_id2
, session_id2_len
);
116 buffer_put_string(&b
, session_id2
, session_id2_len
);
118 /* reconstruct packet */
119 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
120 buffer_put_cstring(&b
, authctxt
->user
);
121 buffer_put_cstring(&b
,
122 datafellows
& SSH_BUG_PKSERVICE
?
125 if (datafellows
& SSH_BUG_PKAUTH
) {
126 buffer_put_char(&b
, have_sig
);
128 buffer_put_cstring(&b
, "publickey");
129 buffer_put_char(&b
, have_sig
);
130 buffer_put_cstring(&b
, pkalg
);
132 buffer_put_string(&b
, pkblob
, blen
);
136 /* test for correct signature */
138 if (PRIVSEP(user_key_allowed(authctxt
->pw
, key
)) &&
139 PRIVSEP(key_verify(key
, sig
, slen
, buffer_ptr(&b
),
140 buffer_len(&b
))) == 1)
145 debug("test whether pkalg/pkblob are acceptable");
148 /* XXX fake reply and always send PK_OK ? */
150 * XXX this allows testing whether a user is allowed
151 * to login: if you happen to have a valid pubkey this
152 * message is sent. the message is NEVER sent at all
153 * if a user is not allowed to login. is this an
156 if (PRIVSEP(user_key_allowed(authctxt
->pw
, key
))) {
157 packet_start(SSH2_MSG_USERAUTH_PK_OK
);
158 packet_put_string(pkalg
, alen
);
159 packet_put_string(pkblob
, blen
);
162 authctxt
->postponed
= 1;
165 if (authenticated
!= 1)
166 auth_clear_options();
168 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated
, pkalg
);
173 return authenticated
;
176 /* return 1 if user allows given key */
178 user_key_allowed2(struct passwd
*pw
, Key
*key
, char *file
)
180 char line
[SSH_MAX_PUBKEY_BYTES
];
187 /* Temporarily use the user's uid. */
188 temporarily_use_uid(pw
);
190 debug("trying public key file %s", file
);
191 f
= auth_openkeyfile(file
, pw
, options
.strict_modes
);
199 found
= key_new(key
->type
);
201 while (read_keyfile_line(f
, file
, line
, sizeof(line
), &linenum
) != -1) {
202 char *cp
, *key_options
= NULL
;
204 /* Skip leading whitespace, empty and comment lines. */
205 for (cp
= line
; *cp
== ' ' || *cp
== '\t'; cp
++)
207 if (!*cp
|| *cp
== '\n' || *cp
== '#')
210 if (key_read(found
, &cp
) != 1) {
211 /* no key? check if there are options for this key */
213 debug2("user_key_allowed: check options: '%s'", cp
);
215 for (; *cp
&& (quoted
|| (*cp
!= ' ' && *cp
!= '\t')); cp
++) {
216 if (*cp
== '\\' && cp
[1] == '"')
217 cp
++; /* Skip both */
221 /* Skip remaining whitespace. */
222 for (; *cp
== ' ' || *cp
== '\t'; cp
++)
224 if (key_read(found
, &cp
) != 1) {
225 debug2("user_key_allowed: advance: '%s'", cp
);
226 /* still no key? advance to next line*/
230 if (key_equal(found
, key
) &&
231 auth_parse_options(pw
, key_options
, file
, linenum
) == 1) {
233 debug("matching key found: file %s, line %lu",
235 fp
= key_fingerprint(found
, SSH_FP_MD5
, SSH_FP_HEX
);
236 verbose("Found matching %s key: %s",
237 key_type(found
), fp
);
246 debug2("key not found");
250 /* check whether given key is in .ssh/authorized_keys* */
252 user_key_allowed(struct passwd
*pw
, Key
*key
)
257 file
= authorized_keys_file(pw
);
258 success
= user_key_allowed2(pw
, key
, file
);
263 /* try suffix "2" for backward compat, too */
264 file
= authorized_keys_file2(pw
);
265 success
= user_key_allowed2(pw
, key
, file
);
270 Authmethod method_pubkey
= {
273 &options
.pubkey_authentication