1 /* $OpenBSD: auth2-pubkey.c,v 1.39 2013/12/30 23:52:27 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);
119 if (key_type_plain(key->type) == KEY_RSA &&
120 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
121 logit("Refusing RSA key because client uses unsafe "
126 sig = packet_get_string(&slen);
129 if (datafellows & SSH_OLD_SESSIONID) {
130 buffer_append(&b, session_id2, session_id2_len);
132 buffer_put_string(&b, session_id2, session_id2_len);
134 /* reconstruct packet */
135 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
136 xasprintf(&userstyle, "%s%s%s", authctxt->user,
137 authctxt->style ? ":" : "",
138 authctxt->style ? authctxt->style : "");
139 buffer_put_cstring(&b, userstyle);
141 buffer_put_cstring(&b,
142 datafellows & SSH_BUG_PKSERVICE ?
145 if (datafellows & SSH_BUG_PKAUTH) {
146 buffer_put_char(&b, have_sig);
148 buffer_put_cstring(&b, "publickey");
149 buffer_put_char(&b, have_sig);
150 buffer_put_cstring(&b, pkalg);
152 buffer_put_string(&b, pkblob, blen);
156 pubkey_auth_info(authctxt, key, NULL);
158 /* test for correct signature */
160 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
161 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
162 buffer_len(&b))) == 1)
167 debug("test whether pkalg/pkblob are acceptable");
170 /* XXX fake reply and always send PK_OK ? */
172 * XXX this allows testing whether a user is allowed
173 * to login: if you happen to have a valid pubkey this
174 * message is sent. the message is NEVER sent at all
175 * if a user is not allowed to login. is this an
178 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
179 packet_start(SSH2_MSG_USERAUTH_PK_OK);
180 packet_put_string(pkalg, alen);
181 packet_put_string(pkblob, blen);
184 authctxt->postponed = 1;
187 if (authenticated != 1)
188 auth_clear_options();
190 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
195 return authenticated;
199 pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...)
208 i = vasprintf(&extra, fmt, ap);
210 if (i < 0 || extra == NULL)
211 fatal("%s: vasprintf failed", __func__);
214 if (key_is_cert(key)) {
215 fp = key_fingerprint(key->cert->signature_key,
216 SSH_FP_MD5, SSH_FP_HEX);
217 auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s",
218 key_type(key), key->cert->key_id,
219 (unsigned long long)key->cert->serial,
220 key_type(key->cert->signature_key), fp,
221 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
224 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
225 auth_info(authctxt, "%s %s%s%s", key_type(key), fp,
226 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
233 match_principals_option(const char *principal_list, struct KeyCert *cert)
238 /* XXX percent_expand() sequences for authorized_principals? */
240 for (i = 0; i < cert->nprincipals; i++) {
241 if ((result = match_list(cert->principals[i],
242 principal_list, NULL)) != NULL) {
243 debug3("matched principal from key options \"%.100s\"",
253 match_principals_file(char *file, struct passwd *pw, struct KeyCert *cert)
256 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
260 temporarily_use_uid(pw);
261 debug("trying authorized principals file %s", file);
262 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
266 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
267 /* Skip leading whitespace. */
268 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
270 /* Skip blank and comment lines. */
271 if ((ep = strchr(cp, '#')) != NULL)
273 if (!*cp || *cp == '\n')
275 /* Trim trailing whitespace. */
276 ep = cp + strlen(cp) - 1;
277 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
280 * If the line has internal whitespace then assume it has
284 if ((ep = strrchr(cp, ' ')) != NULL ||
285 (ep = strrchr(cp, '\t')) != NULL) {
286 for (; *ep == ' ' || *ep == '\t'; ep++)
291 for (i = 0; i < cert->nprincipals; i++) {
292 if (strcmp(cp, cert->principals[i]) == 0) {
293 debug3("matched principal \"%.100s\" "
294 "from file \"%s\" on line %lu",
295 cert->principals[i], file, linenum);
296 if (auth_parse_options(pw, line_opts,
311 * Checks whether key is allowed in authorized_keys-format file,
312 * returns 1 if the key is allowed or 0 otherwise.
315 check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
317 char line[SSH_MAX_PUBKEY_BYTES];
327 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
328 char *cp, *key_options = NULL;
331 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
332 auth_clear_options();
334 /* Skip leading whitespace, empty and comment lines. */
335 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
337 if (!*cp || *cp == '\n' || *cp == '#')
340 if (key_read(found, &cp) != 1) {
341 /* no key? check if there are options for this key */
343 debug2("user_key_allowed: check options: '%s'", cp);
345 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
346 if (*cp == '\\' && cp[1] == '"')
347 cp++; /* Skip both */
351 /* Skip remaining whitespace. */
352 for (; *cp == ' ' || *cp == '\t'; cp++)
354 if (key_read(found, &cp) != 1) {
355 debug2("user_key_allowed: advance: '%s'", cp);
356 /* still no key? advance to next line*/
360 if (key_is_cert(key)) {
361 if (!key_equal(found, key->cert->signature_key))
363 if (auth_parse_options(pw, key_options, file,
366 if (!key_is_cert_authority)
368 fp = key_fingerprint(found, SSH_FP_MD5,
370 debug("matching CA found: file %s, line %lu, %s %s",
371 file, linenum, key_type(found), fp);
373 * If the user has specified a list of principals as
374 * a key option, then prefer that list to matching
375 * their username in the certificate principals list.
377 if (authorized_principals != NULL &&
378 !match_principals_option(authorized_principals,
380 reason = "Certificate does not contain an "
381 "authorized principal";
385 auth_debug_add("%s", reason);
388 if (key_cert_check_authority(key, 0, 0,
389 authorized_principals == NULL ? pw->pw_name : NULL,
392 if (auth_cert_options(key, pw) != 0) {
396 verbose("Accepted certificate ID \"%s\" "
397 "signed by %s CA %s via %s", key->cert->key_id,
398 key_type(found), fp, file);
402 } else if (key_equal(found, key)) {
403 if (auth_parse_options(pw, key_options, file,
406 if (key_is_cert_authority)
409 fp = key_fingerprint(found, SSH_FP_MD5, SSH_FP_HEX);
410 debug("matching key found: file %s, line %lu %s %s",
411 file, linenum, key_type(found), fp);
419 debug2("key not found");
423 /* Authenticate a certificate key against TrustedUserCAKeys */
425 user_cert_trusted_ca(struct passwd *pw, Key *key)
427 char *ca_fp, *principals_file = NULL;
431 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
434 ca_fp = key_fingerprint(key->cert->signature_key,
435 SSH_FP_MD5, SSH_FP_HEX);
437 if (key_in_file(key->cert->signature_key,
438 options.trusted_user_ca_keys, 1) != 1) {
439 debug2("%s: CA %s %s is not listed in %s", __func__,
440 key_type(key->cert->signature_key), ca_fp,
441 options.trusted_user_ca_keys);
445 * If AuthorizedPrincipals is in use, then compare the certificate
446 * principals against the names in that file rather than matching
447 * against the username.
449 if ((principals_file = authorized_principals_file(pw)) != NULL) {
450 if (!match_principals_file(principals_file, pw, key->cert)) {
451 reason = "Certificate does not contain an "
452 "authorized principal";
455 auth_debug_add("%s", reason);
459 if (key_cert_check_authority(key, 0, 1,
460 principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
462 if (auth_cert_options(key, pw) != 0)
465 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
466 key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
467 options.trusted_user_ca_keys);
471 free(principals_file);
477 * Checks whether key is allowed in file.
478 * returns 1 if the key is allowed or 0 otherwise.
481 user_key_allowed2(struct passwd *pw, Key *key, char *file)
486 /* Temporarily use the user's uid. */
487 temporarily_use_uid(pw);
489 debug("trying public key file %s", file);
490 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
491 found_key = check_authkeys_file(f, file, key, pw);
500 * Checks whether key is allowed in output of command.
501 * returns 1 if the key is allowed or 0 otherwise.
504 user_key_command_allowed2(struct passwd *user_pw, Key *key)
507 int ok, found_key = 0;
510 int status, devnull, p[2], i;
512 char *username, errmsg[512];
514 if (options.authorized_keys_command == NULL ||
515 options.authorized_keys_command[0] != '/')
518 if (options.authorized_keys_command_user == NULL) {
519 error("No user for AuthorizedKeysCommand specified, skipping");
523 username = percent_expand(options.authorized_keys_command_user,
524 "u", user_pw->pw_name, (char *)NULL);
525 pw = getpwnam(username);
527 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
528 username, strerror(errno));
534 temporarily_use_uid(pw);
536 if (stat(options.authorized_keys_command, &st) < 0) {
537 error("Could not stat AuthorizedKeysCommand \"%s\": %s",
538 options.authorized_keys_command, strerror(errno));
541 if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0,
542 errmsg, sizeof(errmsg)) != 0) {
543 error("Unsafe AuthorizedKeysCommand: %s", errmsg);
548 error("%s: pipe: %s", __func__, strerror(errno));
552 debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"",
553 options.authorized_keys_command, user_pw->pw_name, pw->pw_name);
556 * Don't want to call this in the child, where it can fatal() and
557 * run cleanup_exit() code.
561 switch ((pid = fork())) {
563 error("%s: fork: %s", __func__, strerror(errno));
568 for (i = 0; i < NSIG; i++)
571 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
572 error("%s: open %s: %s", __func__, _PATH_DEVNULL,
576 /* Keep stderr around a while longer to catch errors */
577 if (dup2(devnull, STDIN_FILENO) == -1 ||
578 dup2(p[1], STDOUT_FILENO) == -1) {
579 error("%s: dup2: %s", __func__, strerror(errno));
582 closefrom(STDERR_FILENO + 1);
584 /* Don't use permanently_set_uid() here to avoid fatal() */
585 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
586 error("setresgid %u: %s", (u_int)pw->pw_gid,
590 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
591 error("setresuid %u: %s", (u_int)pw->pw_uid,
595 /* stdin is pointed to /dev/null at this point */
596 if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
597 error("%s: dup2: %s", __func__, strerror(errno));
601 execl(options.authorized_keys_command,
602 options.authorized_keys_command, user_pw->pw_name, NULL);
604 error("AuthorizedKeysCommand %s exec failed: %s",
605 options.authorized_keys_command, strerror(errno));
607 default: /* parent */
611 temporarily_use_uid(pw);
614 if ((f = fdopen(p[0], "r")) == NULL) {
615 error("%s: fdopen: %s", __func__, strerror(errno));
617 /* Don't leave zombie child */
619 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
623 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
626 while (waitpid(pid, &status, 0) == -1) {
627 if (errno != EINTR) {
628 error("%s: waitpid: %s", __func__, strerror(errno));
632 if (WIFSIGNALED(status)) {
633 error("AuthorizedKeysCommand %s exited on signal %d",
634 options.authorized_keys_command, WTERMSIG(status));
636 } else if (WEXITSTATUS(status) != 0) {
637 error("AuthorizedKeysCommand %s returned status %d",
638 options.authorized_keys_command, WEXITSTATUS(status));
648 * Check whether key authenticates and authorises the user.
651 user_key_allowed(struct passwd *pw, Key *key)
656 if (auth_key_is_revoked(key))
658 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
661 success = user_cert_trusted_ca(pw, key);
665 success = user_key_command_allowed2(pw, key);
669 for (i = 0; !success && i < options.num_authkeys_files; i++) {
671 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
673 file = expand_authorized_keys(
674 options.authorized_keys_files[i], pw);
676 success = user_key_allowed2(pw, key, file);
683 Authmethod method_pubkey = {
686 &options.pubkey_authentication