1 /* $OpenBSD: auth2-pubkey.c,v 1.47 2015/02/17 00:14:05 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>
58 #include "pathnames.h"
60 #include "auth-options.h"
65 #include "monitor_wrap.h"
70 extern ServerOptions options;
71 extern u_char *session_id2;
72 extern u_int session_id2_len;
75 userauth_pubkey(Authctxt *authctxt)
79 char *pkalg, *userstyle;
81 u_int alen, blen, slen;
83 int authenticated = 0;
85 if (!authctxt->valid) {
86 debug2("userauth_pubkey: disabled because of invalid user");
89 have_sig = packet_get_char();
90 if (datafellows & SSH_BUG_PKAUTH) {
91 debug2("userauth_pubkey: SSH_BUG_PKAUTH");
92 /* no explicit pkalg given */
93 pkblob = packet_get_string(&blen);
95 buffer_append(&b, pkblob, blen);
96 /* so we have to extract the pkalg from the pkblob */
97 pkalg = buffer_get_string(&b, &alen);
100 pkalg = packet_get_string(&alen);
101 pkblob = packet_get_string(&blen);
103 pktype = key_type_from_name(pkalg);
104 if (pktype == KEY_UNSPEC) {
105 /* this is perfectly legal */
106 logit("userauth_pubkey: unsupported public key algorithm: %s",
110 key = key_from_blob(pkblob, blen);
112 error("userauth_pubkey: cannot decode key: %s", pkalg);
115 if (key->type != pktype) {
116 error("userauth_pubkey: type mismatch for decoded key "
117 "(received %d, expected %d)", key->type, pktype);
120 if (key_type_plain(key->type) == KEY_RSA &&
121 (datafellows & SSH_BUG_RSASIGMD5) != 0) {
122 logit("Refusing RSA key because client uses unsafe "
126 if (auth2_userkey_already_used(authctxt, key)) {
127 logit("refusing previously-used %s key", key_type(key));
130 if (match_pattern_list(sshkey_ssh_name(key), options.pubkey_key_types,
131 strlen(options.pubkey_key_types), 0) != 1) {
132 logit("%s: key type %s not in PubkeyAcceptedKeyTypes",
133 __func__, sshkey_ssh_name(key));
138 sig = packet_get_string(&slen);
141 if (datafellows & SSH_OLD_SESSIONID) {
142 buffer_append(&b, session_id2, session_id2_len);
144 buffer_put_string(&b, session_id2, session_id2_len);
146 /* reconstruct packet */
147 buffer_put_char(&b, SSH2_MSG_USERAUTH_REQUEST);
148 xasprintf(&userstyle, "%s%s%s", authctxt->user,
149 authctxt->style ? ":" : "",
150 authctxt->style ? authctxt->style : "");
151 buffer_put_cstring(&b, userstyle);
153 buffer_put_cstring(&b,
154 datafellows & SSH_BUG_PKSERVICE ?
157 if (datafellows & SSH_BUG_PKAUTH) {
158 buffer_put_char(&b, have_sig);
160 buffer_put_cstring(&b, "publickey");
161 buffer_put_char(&b, have_sig);
162 buffer_put_cstring(&b, pkalg);
164 buffer_put_string(&b, pkblob, blen);
168 pubkey_auth_info(authctxt, key, NULL);
170 /* test for correct signature */
172 if (PRIVSEP(user_key_allowed(authctxt->pw, key)) &&
173 PRIVSEP(key_verify(key, sig, slen, buffer_ptr(&b),
174 buffer_len(&b))) == 1) {
176 /* Record the successful key to prevent reuse */
177 auth2_record_userkey(authctxt, key);
178 key = NULL; /* Don't free below */
183 debug("test whether pkalg/pkblob are acceptable");
186 /* XXX fake reply and always send PK_OK ? */
188 * XXX this allows testing whether a user is allowed
189 * to login: if you happen to have a valid pubkey this
190 * message is sent. the message is NEVER sent at all
191 * if a user is not allowed to login. is this an
194 if (PRIVSEP(user_key_allowed(authctxt->pw, key))) {
195 packet_start(SSH2_MSG_USERAUTH_PK_OK);
196 packet_put_string(pkalg, alen);
197 packet_put_string(pkblob, blen);
200 authctxt->postponed = 1;
203 if (authenticated != 1)
204 auth_clear_options();
206 debug2("userauth_pubkey: authenticated %d pkalg %s", authenticated, pkalg);
211 return authenticated;
215 pubkey_auth_info(Authctxt *authctxt, const Key *key, const char *fmt, ...)
224 i = vasprintf(&extra, fmt, ap);
226 if (i < 0 || extra == NULL)
227 fatal("%s: vasprintf failed", __func__);
230 if (key_is_cert(key)) {
231 fp = sshkey_fingerprint(key->cert->signature_key,
232 options.fingerprint_hash, SSH_FP_DEFAULT);
233 auth_info(authctxt, "%s ID %s (serial %llu) CA %s %s%s%s",
234 key_type(key), key->cert->key_id,
235 (unsigned long long)key->cert->serial,
236 key_type(key->cert->signature_key),
237 fp == NULL ? "(null)" : fp,
238 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
241 fp = sshkey_fingerprint(key, options.fingerprint_hash,
243 auth_info(authctxt, "%s %s%s%s", key_type(key),
244 fp == NULL ? "(null)" : fp,
245 extra == NULL ? "" : ", ", extra == NULL ? "" : extra);
252 match_principals_option(const char *principal_list, struct sshkey_cert *cert)
257 /* XXX percent_expand() sequences for authorized_principals? */
259 for (i = 0; i < cert->nprincipals; i++) {
260 if ((result = match_list(cert->principals[i],
261 principal_list, NULL)) != NULL) {
262 debug3("matched principal from key options \"%.100s\"",
272 match_principals_file(char *file, struct passwd *pw, struct sshkey_cert *cert)
275 char line[SSH_MAX_PUBKEY_BYTES], *cp, *ep, *line_opts;
279 temporarily_use_uid(pw);
280 debug("trying authorized principals file %s", file);
281 if ((f = auth_openprincipals(file, pw, options.strict_modes)) == NULL) {
285 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
286 /* Skip leading whitespace. */
287 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
289 /* Skip blank and comment lines. */
290 if ((ep = strchr(cp, '#')) != NULL)
292 if (!*cp || *cp == '\n')
294 /* Trim trailing whitespace. */
295 ep = cp + strlen(cp) - 1;
296 while (ep > cp && (*ep == '\n' || *ep == ' ' || *ep == '\t'))
299 * If the line has internal whitespace then assume it has
303 if ((ep = strrchr(cp, ' ')) != NULL ||
304 (ep = strrchr(cp, '\t')) != NULL) {
305 for (; *ep == ' ' || *ep == '\t'; ep++)
310 for (i = 0; i < cert->nprincipals; i++) {
311 if (strcmp(cp, cert->principals[i]) == 0) {
312 debug3("matched principal \"%.100s\" "
313 "from file \"%s\" on line %lu",
314 cert->principals[i], file, linenum);
315 if (auth_parse_options(pw, line_opts,
330 * Checks whether key is allowed in authorized_keys-format file,
331 * returns 1 if the key is allowed or 0 otherwise.
334 check_authkeys_file(FILE *f, char *file, Key* key, struct passwd *pw)
336 char line[SSH_MAX_PUBKEY_BYTES];
346 while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
347 char *cp, *key_options = NULL;
350 found = key_new(key_is_cert(key) ? KEY_UNSPEC : key->type);
351 auth_clear_options();
353 /* Skip leading whitespace, empty and comment lines. */
354 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
356 if (!*cp || *cp == '\n' || *cp == '#')
359 if (key_read(found, &cp) != 1) {
360 /* no key? check if there are options for this key */
362 debug2("user_key_allowed: check options: '%s'", cp);
364 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
365 if (*cp == '\\' && cp[1] == '"')
366 cp++; /* Skip both */
370 /* Skip remaining whitespace. */
371 for (; *cp == ' ' || *cp == '\t'; cp++)
373 if (key_read(found, &cp) != 1) {
374 debug2("user_key_allowed: advance: '%s'", cp);
375 /* still no key? advance to next line*/
379 if (key_is_cert(key)) {
380 if (!key_equal(found, key->cert->signature_key))
382 if (auth_parse_options(pw, key_options, file,
385 if (!key_is_cert_authority)
387 if ((fp = sshkey_fingerprint(found,
388 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
390 debug("matching CA found: file %s, line %lu, %s %s",
391 file, linenum, key_type(found), fp);
393 * If the user has specified a list of principals as
394 * a key option, then prefer that list to matching
395 * their username in the certificate principals list.
397 if (authorized_principals != NULL &&
398 !match_principals_option(authorized_principals,
400 reason = "Certificate does not contain an "
401 "authorized principal";
405 auth_debug_add("%s", reason);
408 if (key_cert_check_authority(key, 0, 0,
409 authorized_principals == NULL ? pw->pw_name : NULL,
412 if (auth_cert_options(key, pw) != 0) {
416 verbose("Accepted certificate ID \"%s\" "
417 "signed by %s CA %s via %s", key->cert->key_id,
418 key_type(found), fp, file);
422 } else if (key_equal(found, key)) {
423 if (auth_parse_options(pw, key_options, file,
426 if (key_is_cert_authority)
428 if ((fp = sshkey_fingerprint(found,
429 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
431 debug("matching key found: file %s, line %lu %s %s",
432 file, linenum, key_type(found), fp);
441 debug2("key not found");
445 /* Authenticate a certificate key against TrustedUserCAKeys */
447 user_cert_trusted_ca(struct passwd *pw, Key *key)
449 char *ca_fp, *principals_file = NULL;
453 if (!key_is_cert(key) || options.trusted_user_ca_keys == NULL)
456 if ((ca_fp = sshkey_fingerprint(key->cert->signature_key,
457 options.fingerprint_hash, SSH_FP_DEFAULT)) == NULL)
460 if (sshkey_in_file(key->cert->signature_key,
461 options.trusted_user_ca_keys, 1, 0) != 0) {
462 debug2("%s: CA %s %s is not listed in %s", __func__,
463 key_type(key->cert->signature_key), ca_fp,
464 options.trusted_user_ca_keys);
468 * If AuthorizedPrincipals is in use, then compare the certificate
469 * principals against the names in that file rather than matching
470 * against the username.
472 if ((principals_file = authorized_principals_file(pw)) != NULL) {
473 if (!match_principals_file(principals_file, pw, key->cert)) {
474 reason = "Certificate does not contain an "
475 "authorized principal";
478 auth_debug_add("%s", reason);
482 if (key_cert_check_authority(key, 0, 1,
483 principals_file == NULL ? pw->pw_name : NULL, &reason) != 0)
485 if (auth_cert_options(key, pw) != 0)
488 verbose("Accepted certificate ID \"%s\" signed by %s CA %s via %s",
489 key->cert->key_id, key_type(key->cert->signature_key), ca_fp,
490 options.trusted_user_ca_keys);
494 free(principals_file);
500 * Checks whether key is allowed in file.
501 * returns 1 if the key is allowed or 0 otherwise.
504 user_key_allowed2(struct passwd *pw, Key *key, char *file)
509 /* Temporarily use the user's uid. */
510 temporarily_use_uid(pw);
512 debug("trying public key file %s", file);
513 if ((f = auth_openkeyfile(file, pw, options.strict_modes)) != NULL) {
514 found_key = check_authkeys_file(f, file, key, pw);
523 * Checks whether key is allowed in output of command.
524 * returns 1 if the key is allowed or 0 otherwise.
527 user_key_command_allowed2(struct passwd *user_pw, Key *key)
530 int ok, found_key = 0;
533 int status, devnull, p[2], i;
535 char *username, errmsg[512];
537 if (options.authorized_keys_command == NULL ||
538 options.authorized_keys_command[0] != '/')
541 if (options.authorized_keys_command_user == NULL) {
542 error("No user for AuthorizedKeysCommand specified, skipping");
546 username = percent_expand(options.authorized_keys_command_user,
547 "u", user_pw->pw_name, (char *)NULL);
548 pw = getpwnam(username);
550 error("AuthorizedKeysCommandUser \"%s\" not found: %s",
551 username, strerror(errno));
557 temporarily_use_uid(pw);
559 if (stat(options.authorized_keys_command, &st) < 0) {
560 error("Could not stat AuthorizedKeysCommand \"%s\": %s",
561 options.authorized_keys_command, strerror(errno));
564 if (auth_secure_path(options.authorized_keys_command, &st, NULL, 0,
565 errmsg, sizeof(errmsg)) != 0) {
566 error("Unsafe AuthorizedKeysCommand: %s", errmsg);
571 error("%s: pipe: %s", __func__, strerror(errno));
575 debug3("Running AuthorizedKeysCommand: \"%s %s\" as \"%s\"",
576 options.authorized_keys_command, user_pw->pw_name, pw->pw_name);
579 * Don't want to call this in the child, where it can fatal() and
580 * run cleanup_exit() code.
584 switch ((pid = fork())) {
586 error("%s: fork: %s", __func__, strerror(errno));
591 for (i = 0; i < NSIG; i++)
594 if ((devnull = open(_PATH_DEVNULL, O_RDWR)) == -1) {
595 error("%s: open %s: %s", __func__, _PATH_DEVNULL,
599 /* Keep stderr around a while longer to catch errors */
600 if (dup2(devnull, STDIN_FILENO) == -1 ||
601 dup2(p[1], STDOUT_FILENO) == -1) {
602 error("%s: dup2: %s", __func__, strerror(errno));
605 closefrom(STDERR_FILENO + 1);
607 /* Don't use permanently_set_uid() here to avoid fatal() */
608 if (setresgid(pw->pw_gid, pw->pw_gid, pw->pw_gid) != 0) {
609 error("setresgid %u: %s", (u_int)pw->pw_gid,
613 if (setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid) != 0) {
614 error("setresuid %u: %s", (u_int)pw->pw_uid,
618 /* stdin is pointed to /dev/null at this point */
619 if (dup2(STDIN_FILENO, STDERR_FILENO) == -1) {
620 error("%s: dup2: %s", __func__, strerror(errno));
624 execl(options.authorized_keys_command,
625 options.authorized_keys_command, user_pw->pw_name, NULL);
627 error("AuthorizedKeysCommand %s exec failed: %s",
628 options.authorized_keys_command, strerror(errno));
630 default: /* parent */
634 temporarily_use_uid(pw);
637 if ((f = fdopen(p[0], "r")) == NULL) {
638 error("%s: fdopen: %s", __func__, strerror(errno));
640 /* Don't leave zombie child */
642 while (waitpid(pid, NULL, 0) == -1 && errno == EINTR)
646 ok = check_authkeys_file(f, options.authorized_keys_command, key, pw);
649 while (waitpid(pid, &status, 0) == -1) {
650 if (errno != EINTR) {
651 error("%s: waitpid: %s", __func__, strerror(errno));
655 if (WIFSIGNALED(status)) {
656 error("AuthorizedKeysCommand %s exited on signal %d",
657 options.authorized_keys_command, WTERMSIG(status));
659 } else if (WEXITSTATUS(status) != 0) {
660 error("AuthorizedKeysCommand %s returned status %d",
661 options.authorized_keys_command, WEXITSTATUS(status));
671 * Check whether key authenticates and authorises the user.
674 user_key_allowed(struct passwd *pw, Key *key)
679 if (auth_key_is_revoked(key))
681 if (key_is_cert(key) && auth_key_is_revoked(key->cert->signature_key))
684 success = user_cert_trusted_ca(pw, key);
688 success = user_key_command_allowed2(pw, key);
692 for (i = 0; !success && i < options.num_authkeys_files; i++) {
694 if (strcasecmp(options.authorized_keys_files[i], "none") == 0)
696 file = expand_authorized_keys(
697 options.authorized_keys_files[i], pw);
699 success = user_key_allowed2(pw, key, file);
706 /* Records a public key in the list of previously-successful keys */
708 auth2_record_userkey(Authctxt *authctxt, struct sshkey *key)
712 if (authctxt->nprev_userkeys >= INT_MAX ||
713 (tmp = reallocarray(authctxt->prev_userkeys,
714 authctxt->nprev_userkeys + 1, sizeof(*tmp))) == NULL)
715 fatal("%s: reallocarray failed", __func__);
716 authctxt->prev_userkeys = tmp;
717 authctxt->prev_userkeys[authctxt->nprev_userkeys] = key;
718 authctxt->nprev_userkeys++;
721 /* Checks whether a key has already been used successfully for authentication */
723 auth2_userkey_already_used(Authctxt *authctxt, struct sshkey *key)
727 for (i = 0; i < authctxt->nprev_userkeys; i++) {
728 if (sshkey_equal_public(key, authctxt->prev_userkeys[i])) {
735 Authmethod method_pubkey = {
738 &options.pubkey_authentication