1 /* $OpenBSD: auth2-pubkey.c,v 1.22 2010/03/10 23:27:17 djm 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>
50 #include "pathnames.h"
52 #include "auth-options.h"
57 #include "monitor_wrap.h"
62 extern ServerOptions options
;
63 extern u_char
*session_id2
;
64 extern u_int session_id2_len
;
67 userauth_pubkey(Authctxt
*authctxt
)
73 u_int alen
, blen
, slen
;
75 int authenticated
= 0;
77 if (!authctxt
->valid
) {
78 debug2("userauth_pubkey: disabled because of invalid user");
81 have_sig
= packet_get_char();
82 if (datafellows
& SSH_BUG_PKAUTH
) {
83 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
84 /* no explicit pkalg given */
85 pkblob
= packet_get_string(&blen
);
87 buffer_append(&b
, pkblob
, blen
);
88 /* so we have to extract the pkalg from the pkblob */
89 pkalg
= buffer_get_string(&b
, &alen
);
92 pkalg
= packet_get_string(&alen
);
93 pkblob
= packet_get_string(&blen
);
95 pktype
= key_type_from_name(pkalg
);
96 if (pktype
== KEY_UNSPEC
) {
97 /* this is perfectly legal */
98 logit("userauth_pubkey: unsupported public key algorithm: %s",
102 key
= key_from_blob(pkblob
, blen
);
104 error("userauth_pubkey: cannot decode key: %s", pkalg
);
107 if (key
->type
!= pktype
) {
108 error("userauth_pubkey: type mismatch for decoded key "
109 "(received %d, expected %d)", key
->type
, pktype
);
113 sig
= packet_get_string(&slen
);
116 if (datafellows
& SSH_OLD_SESSIONID
) {
117 buffer_append(&b
, session_id2
, session_id2_len
);
119 buffer_put_string(&b
, session_id2
, session_id2_len
);
121 /* reconstruct packet */
122 buffer_put_char(&b
, SSH2_MSG_USERAUTH_REQUEST
);
123 buffer_put_cstring(&b
, authctxt
->user
);
124 buffer_put_cstring(&b
,
125 datafellows
& SSH_BUG_PKSERVICE
?
128 if (datafellows
& SSH_BUG_PKAUTH
) {
129 buffer_put_char(&b
, have_sig
);
131 buffer_put_cstring(&b
, "publickey");
132 buffer_put_char(&b
, have_sig
);
133 buffer_put_cstring(&b
, pkalg
);
135 buffer_put_string(&b
, pkblob
, blen
);
139 /* test for correct signature */
141 if (PRIVSEP(user_key_allowed(authctxt
->pw
, key
)) &&
142 PRIVSEP(key_verify(key
, sig
, slen
, buffer_ptr(&b
),
143 buffer_len(&b
))) == 1)
148 debug("test whether pkalg/pkblob are acceptable");
151 /* XXX fake reply and always send PK_OK ? */
153 * XXX this allows testing whether a user is allowed
154 * to login: if you happen to have a valid pubkey this
155 * message is sent. the message is NEVER sent at all
156 * if a user is not allowed to login. is this an
159 if (PRIVSEP(user_key_allowed(authctxt
->pw
, key
))) {
160 packet_start(SSH2_MSG_USERAUTH_PK_OK
);
161 packet_put_string(pkalg
, alen
);
162 packet_put_string(pkblob
, blen
);
165 authctxt
->postponed
= 1;
168 if (authenticated
!= 1)
169 auth_clear_options();
171 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated
, pkalg
);
176 return authenticated
;
179 /* return 1 if user allows given key */
181 user_key_allowed2(struct passwd
*pw
, Key
*key
, char *file
)
183 char line
[SSH_MAX_PUBKEY_BYTES
];
191 /* Temporarily use the user's uid. */
192 temporarily_use_uid(pw
);
194 debug("trying public key file %s", file
);
195 f
= auth_openkeyfile(file
, pw
, options
.strict_modes
);
203 found
= key_new(key_is_cert(key
) ? KEY_UNSPEC
: key
->type
);
205 while (read_keyfile_line(f
, file
, line
, sizeof(line
), &linenum
) != -1) {
206 char *cp
, *key_options
= NULL
;
208 auth_clear_options();
210 /* Skip leading whitespace, empty and comment lines. */
211 for (cp
= line
; *cp
== ' ' || *cp
== '\t'; cp
++)
213 if (!*cp
|| *cp
== '\n' || *cp
== '#')
216 if (key_read(found
, &cp
) != 1) {
217 /* no key? check if there are options for this key */
219 debug2("user_key_allowed: check options: '%s'", cp
);
221 for (; *cp
&& (quoted
|| (*cp
!= ' ' && *cp
!= '\t')); cp
++) {
222 if (*cp
== '\\' && cp
[1] == '"')
223 cp
++; /* Skip both */
227 /* Skip remaining whitespace. */
228 for (; *cp
== ' ' || *cp
== '\t'; cp
++)
230 if (key_read(found
, &cp
) != 1) {
231 debug2("user_key_allowed: advance: '%s'", cp
);
232 /* still no key? advance to next line*/
236 if (auth_parse_options(pw
, key_options
, file
, linenum
) != 1)
238 if (key
->type
== KEY_RSA_CERT
|| key
->type
== KEY_DSA_CERT
) {
239 if (!key_is_cert_authority
)
241 if (!key_equal(found
, key
->cert
->signature_key
))
243 fp
= key_fingerprint(found
, SSH_FP_MD5
,
245 debug("matching CA found: file %s, line %lu, %s %s",
246 file
, linenum
, key_type(found
), fp
);
247 if (key_cert_check_authority(key
, 0, 0, pw
->pw_name
,
251 auth_debug_add("%s", reason
);
254 if (auth_cert_constraints(&key
->cert
->constraints
,
259 verbose("Accepted certificate ID \"%s\" "
260 "signed by %s CA %s via %s", key
->cert
->key_id
,
261 key_type(found
), fp
, file
);
265 } else if (!key_is_cert_authority
&& key_equal(found
, key
)) {
267 debug("matching key found: file %s, line %lu",
269 fp
= key_fingerprint(found
, SSH_FP_MD5
, SSH_FP_HEX
);
270 verbose("Found matching %s key: %s",
271 key_type(found
), fp
);
280 debug2("key not found");
284 /* Authenticate a certificate key against TrustedUserCAKeys */
286 user_cert_trusted_ca(struct passwd
*pw
, Key
*key
)
292 if (!key_is_cert(key
) || options
.trusted_user_ca_keys
== NULL
)
295 ca_fp
= key_fingerprint(key
->cert
->signature_key
,
296 SSH_FP_MD5
, SSH_FP_HEX
);
298 if (key_in_file(key
->cert
->signature_key
,
299 options
.trusted_user_ca_keys
, 1) != 1) {
300 debug2("%s: CA %s %s is not listed in %s", __func__
,
301 key_type(key
->cert
->signature_key
), ca_fp
,
302 options
.trusted_user_ca_keys
);
305 if (key_cert_check_authority(key
, 0, 1, pw
->pw_name
, &reason
) != 0) {
307 auth_debug_add("%s", reason
);
310 if (auth_cert_constraints(&key
->cert
->constraints
, pw
) != 0)
313 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
314 key
->cert
->key_id
, key_type(key
->cert
->signature_key
), ca_fp
,
315 options
.trusted_user_ca_keys
);
324 /* check whether given key is in .ssh/authorized_keys* */
326 user_key_allowed(struct passwd
*pw
, Key
*key
)
331 if (auth_key_is_revoked(key
))
333 if (key_is_cert(key
) && auth_key_is_revoked(key
->cert
->signature_key
))
336 success
= user_cert_trusted_ca(pw
, key
);
340 file
= authorized_keys_file(pw
);
341 success
= user_key_allowed2(pw
, key
, file
);
346 /* try suffix "2" for backward compat, too */
347 file
= authorized_keys_file2(pw
);
348 success
= user_key_allowed2(pw
, key
, file
);
353 Authmethod method_pubkey
= {
356 &options
.pubkey_authentication