1 /* $OpenBSD: auth2-pubkey.c,v 1.38 2013/06/21 00:34:49 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>
56 #include "pathnames.h"
58 #include "auth-options.h"
63 #include "monitor_wrap.h"
69 extern ServerOptions options;
70 extern u_char *session_id2;
71 extern u_int session_id2_len;
74 userauth_pubkey(Authctxt *authctxt)
78 char *pkalg, *userstyle;
80 u_int alen, blen, slen;
82 int authenticated = 0;
84 if (!authctxt->valid) {
85 debug2("userauth_pubkey: disabled because of invalid user");
88 have_sig = packet_get_char();
89 if (datafellows & SSH_BUG_PKAUTH) {
90 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
91 /* no explicit pkalg given */
92 pkblob = packet_get_string(&blen);
94 buffer_append(&b, pkblob, blen);
95 /* so we have to extract the pkalg from the pkblob */
96 pkalg = buffer_get_string(&b, &alen);
99 pkalg = packet_get_string(&alen);
100 pkblob = packet_get_string(&blen);
102 pktype = key_type_from_name(pkalg);
103 if (pktype == KEY_UNSPEC) {
104 /* this is perfectly legal */
105 logit("userauth_pubkey: unsupported public key algorithm: %s",
109 key = key_from_blob(pkblob, blen);
111 error("userauth_pubkey: cannot decode key: %s", pkalg);
114 if (key->type != pktype) {
115 error("userauth_pubkey: type mismatch for decoded key "
116 "(received %d, expected %d)", key->type, pktype);
120 sig = packet_get_string(&slen);
123 if (datafellows & SSH_OLD_SESSIONID) {
124 buffer_append(&b, session_id2, session_id2_len);
126 buffer_put_string(&b, session_id2, session_id2_len);
128 /* reconstruct packet */
129 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
130 xasprintf(&userstyle, "%s%s%s", authctxt->user,
131 authctxt->style ? ":" : "",
132 authctxt->style ? authctxt->style : "");
133 buffer_put_cstring(&b, userstyle);
135 buffer_put_cstring(&b,
136 datafellows & SSH_BUG_PKSERVICE ?
139 if (datafellows & SSH_BUG_PKAUTH) {
140 buffer_put_char(&b, have_sig);
142 buffer_put_cstring(&b, "publickey");
143 buffer_put_char(&b, have_sig);
144 buffer_put_cstring(&b, pkalg);
146 buffer_put_string(&b, pkblob, blen);
150 pubkey_auth_info(authctxt, key, NULL);
152 /* test for correct signature */
154 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
155 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
156 buffer_len(&b))) == 1)
161 debug("test whether pkalg/pkblob are acceptable");
164 /* XXX fake reply and always send PK_OK ? */
166 * XXX this allows testing whether a user is allowed
167 * to login: if you happen to have a valid pubkey this
168 * message is sent. the message is NEVER sent at all
169 * if a user is not allowed to login. is this an
172 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
173 packet_start(SSH2_MSG_USERAUTH_PK_OK);
174 packet_put_string(pkalg, alen);
175 packet_put_string(pkblob, blen);
178 authctxt->postponed = 1;
181 if (authenticated != 1)
182 auth_clear_options();
184 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
189 return authenticated;
193 pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...)
202 i = vasprintf(&extra, fmt, ap);
204 if (i < 0 || extra == NULL)
205 fatal("%s: vasprintf failed", __func__);
208 if (key_is_cert(key)) {
209 fp = key_fingerprint(key->cert->signature_key,
210 SSH_FP_MD5, SSH_FP_HEX);
211 auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s",
212 key_type(key), key->cert->key_id,
213 (unsigned long long)key->cert->serial,
214 key_type(key->cert->signature_key), fp,
215 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
218 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
219 auth_info(authctxt, "%s %s%s%s", key_type(key), fp,
220 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
227 match_principals_option(const char *principal_list, struct KeyCert *cert)
232 /* XXX percent_expand() sequences for authorized_principals? */
234 for (i = 0; i < cert->nprincipals; i++) {
235 if ((result = match_list(cert->principals[i],
236 principal_list, NULL)) != NULL) {
237 debug3("matched principal from key options \"%.100s\"",
247 match_principals_file(char *file, struct passwd *pw, struct KeyCert *cert)
250 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
254 temporarily_use_uid(pw);
255 debug("trying authorized principals file %s", file);
256 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
260 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
261 /* Skip leading whitespace. */
262 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
264 /* Skip blank and comment lines. */
265 if ((ep = strchr(cp, '#')) != NULL)
267 if (!*cp || *cp == '\n')
269 /* Trim trailing whitespace. */
270 ep = cp + strlen(cp) - 1;
271 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
274 * If the line has internal whitespace then assume it has
278 if ((ep = strrchr(cp, ' ')) != NULL ||
279 (ep = strrchr(cp, '\t')) != NULL) {
280 for (; *ep == ' ' || *ep == '\t'; ep++)
285 for (i = 0; i < cert->nprincipals; i++) {
286 if (strcmp(cp, cert->principals[i]) == 0) {
287 debug3("matched principal \"%.100s\" "
288 "from file \"%s\" on line %lu",
289 cert->principals[i], file, linenum);
290 if (auth_parse_options(pw, line_opts,
305 * Checks whether key is allowed in authorized_keys-format file,
306 * returns 1 if the key is allowed or 0 otherwise.
309 check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
311 char line[SSH_MAX_PUBKEY_BYTES];
321 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
322 char *cp, *key_options = NULL;
325 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
326 auth_clear_options();
328 /* Skip leading whitespace, empty and comment lines. */
329 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
331 if (!*cp || *cp == '\n' || *cp == '#')
334 if (key_read(found, &cp) != 1) {
335 /* no key? check if there are options for this key */
337 debug2("user_key_allowed: check options: '%s'", cp);
339 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
340 if (*cp == '\\' && cp[1] == '"')
341 cp++; /* Skip both */
345 /* Skip remaining whitespace. */
346 for (; *cp == ' ' || *cp == '\t'; cp++)
348 if (key_read(found, &cp) != 1) {
349 debug2("user_key_allowed: advance: '%s'", cp);
350 /* still no key? advance to next line*/
354 if (key_is_cert(key)) {
355 if (!key_equal(found, key->cert->signature_key))
357 if (auth_parse_options(pw, key_options, file,
360 if (!key_is_cert_authority)
362 fp = key_fingerprint(found, SSH_FP_MD5,
364 debug("matching CA found: file %s, line %lu, %s %s",
365 file, linenum, key_type(found), fp);
367 * If the user has specified a list of principals as
368 * a key option, then prefer that list to matching
369 * their username in the certificate principals list.
371 if (authorized_principals != NULL &&
372 !match_principals_option(authorized_principals,
374 reason = "Certificate does not contain an "
375 "authorized principal";
379 auth_debug_add("%s", reason);
382 if (key_cert_check_authority(key, 0, 0,
383 authorized_principals == NULL ? pw->pw_name : NULL,
386 if (auth_cert_options(key, pw) != 0) {
390 verbose("Accepted certificate ID \"%s\" "
391 "signed by %s CA %s via %s", key->cert->key_id,
392 key_type(found), fp, file);
396 } else if (key_equal(found, key)) {
397 if (auth_parse_options(pw, key_options, file,
400 if (key_is_cert_authority)
403 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
404 debug("matching key found: file %s, line %lu %s %s",
405 file, linenum, key_type(found), fp);
413 debug2("key not found");
417 /* Authenticate a certificate key against TrustedUserCAKeys */
419 user_cert_trusted_ca(struct passwd *pw, Key *key)
421 char *ca_fp, *principals_file = NULL;
425 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
428 ca_fp = key_fingerprint(key->cert->signature_key,
429 SSH_FP_MD5, SSH_FP_HEX);
431 if (key_in_file(key->cert->signature_key,
432 options.trusted_user_ca_keys, 1) != 1) {
433 debug2("%s: CA %s %s is not listed in %s", __func__,
434 key_type(key->cert->signature_key), ca_fp,
435 options.trusted_user_ca_keys);
439 * If AuthorizedPrincipals is in use, then compare the certificate
440 * principals against the names in that file rather than matching
441 * against the username.
443 if ((principals_file = authorized_principals_file(pw)) != NULL) {
444 if (!match_principals_file(principals_file, pw, key->cert)) {
445 reason = "Certificate does not contain an "
446 "authorized principal";
449 auth_debug_add("%s", reason);
453 if (key_cert_check_authority(key, 0, 1,
454 principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
456 if (auth_cert_options(key, pw) != 0)
459 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
460 key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
461 options.trusted_user_ca_keys);
465 free(principals_file);
471 * Checks whether key is allowed in file.
472 * returns 1 if the key is allowed or 0 otherwise.
475 user_key_allowed2(struct passwd *pw, Key *key, char *file)
480 /* Temporarily use the user's uid. */
481 temporarily_use_uid(pw);
483 debug("trying public key file %s", file);
484 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
485 found_key = check_authkeys_file(f, file, key, pw);
494 * Checks whether key is allowed in output of command.
495 * returns 1 if the key is allowed or 0 otherwise.
498 user_key_command_allowed2(struct passwd *user_pw, Key *key)
501 int ok, found_key = 0;
504 int status, devnull, p[2], i;
506 char *username, errmsg[512];
508 if (options.authorized_keys_command == NULL ||
509 options.authorized_keys_command[0] != '/')
512 if (options.authorized_keys_command_user == NULL) {
513 error("No user for AuthorizedKeysCommand specified, skipping");
517 username = percent_expand(options.authorized_keys_command_user,
518 "u", user_pw->pw_name, (char *)NULL);
519 pw = getpwnam(username);
521 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
522 username, strerror(errno));
528 temporarily_use_uid(pw);
530 if (stat(options.authorized_keys_command, &st) < 0) {
531 error("Could not stat AuthorizedKeysCommand \"%s\": %s",
532 options.authorized_keys_command, strerror(errno));
535 if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0,
536 errmsg, sizeof(errmsg)) != 0) {
537 error("Unsafe AuthorizedKeysCommand: %s", errmsg);
542 error("%s: pipe: %s", __func__, strerror(errno));
546 debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"",
547 options.authorized_keys_command, user_pw->pw_name, pw->pw_name);
550 * Don't want to call this in the child, where it can fatal() and
551 * run cleanup_exit() code.
555 switch ((pid = fork())) {
557 error("%s: fork: %s", __func__, strerror(errno));
562 for (i = 0; i < NSIG; i++)
565 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
566 error("%s: open %s: %s", __func__, _PATH_DEVNULL,
570 /* Keep stderr around a while longer to catch errors */
571 if (dup2(devnull, STDIN_FILENO) == -1 ||
572 dup2(p[1], STDOUT_FILENO) == -1) {
573 error("%s: dup2: %s", __func__, strerror(errno));
576 closefrom(STDERR_FILENO + 1);
578 /* Don't use permanently_set_uid() here to avoid fatal() */
579 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
580 error("setresgid %u: %s", (u_int)pw->pw_gid,
584 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
585 error("setresuid %u: %s", (u_int)pw->pw_uid,
589 /* stdin is pointed to /dev/null at this point */
590 if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
591 error("%s: dup2: %s", __func__, strerror(errno));
595 execl(options.authorized_keys_command,
596 options.authorized_keys_command, user_pw->pw_name, NULL);
598 error("AuthorizedKeysCommand %s exec failed: %s",
599 options.authorized_keys_command, strerror(errno));
601 default: /* parent */
605 temporarily_use_uid(pw);
608 if ((f = fdopen(p[0], "r")) == NULL) {
609 error("%s: fdopen: %s", __func__, strerror(errno));
611 /* Don't leave zombie child */
613 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
617 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
620 while (waitpid(pid, &status, 0) == -1) {
621 if (errno != EINTR) {
622 error("%s: waitpid: %s", __func__, strerror(errno));
626 if (WIFSIGNALED(status)) {
627 error("AuthorizedKeysCommand %s exited on signal %d",
628 options.authorized_keys_command, WTERMSIG(status));
630 } else if (WEXITSTATUS(status) != 0) {
631 error("AuthorizedKeysCommand %s returned status %d",
632 options.authorized_keys_command, WEXITSTATUS(status));
642 * Check whether key authenticates and authorises the user.
645 user_key_allowed(struct passwd *pw, Key *key)
650 if (auth_key_is_revoked(key))
652 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
655 success = user_cert_trusted_ca(pw, key);
659 success = user_key_command_allowed2(pw, key);
663 for (i = 0; !success && i < options.num_authkeys_files; i++) {
665 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
667 file = expand_authorized_keys(
668 options.authorized_keys_files[i], pw);
670 success = user_key_allowed2(pw, key, file);
677 Authmethod method_pubkey = {
680 &options.pubkey_authentication