5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * CDDL HEADER START
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * The contents of this file are subject to the terms of the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Common Development and Distribution License (the "License").
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * You may not use this file except in compliance with the License.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * or http://www.opensolaris.org/os/licensing.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * See the License for the specific language governing permissions
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and limitations under the License.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If applicable, add the following below this CDDL HEADER, with the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * CDDL HEADER END
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Use is subject to license terms.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandtypedef enum {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Name: addcert
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Desc: Imports a user certificate into the keystore, along with a
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * private key.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Returns: 0 on success, non-zero otherwise.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland keystore_encoding_format_t informat = NULL;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland while ((i = getopt(argc, argv, ":a:k:e:f:n:P:p:R:ty")) != EOF) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_MISSING_OPERAND, optopt);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* LINTED fallthrough intentional */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* for untrusted (user) certs, we require a name */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* for trusted certs, we cannot have a name */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* for trusted certs, we cannot have a private key */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* last argument should be the path to the certificate */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_DEBUG, "Loading stdin certificate");
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_DEBUG, "Loading <%s> certificate",
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((certfile = fopen(infile, "r")) == NULL) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * if specific key file supplied, open it, otherwise open
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * default (stdin)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if ((keyfile = fopen(inkeyfile, "r")) == NULL) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* set up proper keystore */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (strlcpy(keystore_file, altroot, MAXPATHLEN) >= MAXPATHLEN) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_TOO_LONG, altroot);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (strlcat(keystore_file, "/", MAXPATHLEN) >= MAXPATHLEN) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_TOO_LONG, altroot);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If we have an alternate
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * root, then we have no choice but to use
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * root's keystore on that alternate root,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * since there is no way to resolve a
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * user's home dir given an alternate root
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * not superuser, but no home dir, so
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * use superuser's keystore
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (strlcat(keystore_file, "/.pkg/security",
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* figure out input format */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland } else if (ci_streq(informat_str, "der")) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_BAD_FORMAT, informat_str);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* load all possible certs */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (load_all_certs(err, certfile, informat, import_passarg,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* we must have gotten at least one cert, if not, fail */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_NO_CERTS, infile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* first, try to load user certificate and key */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (load_cert_and_key(err, certfile, informat, import_passarg,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* we must have gotten a cert, if not, fail */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_NO_CERTS, infile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * if we are importing a user cert, and did not get
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * a key, try to load it from the key file
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_NEED_KEY, infile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (load_cert_and_key(err, keyfile, informat,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* check validity date of all certificates */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland for (i = 0; i < sk_X509_num(trustcerts); i++) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* LINTED pointer cast may result in improper algnmnt */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* check validity date of user certificate */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (check_cert_and_key(err, cert, key) != 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * if importing more than one cert, must use implicit trust,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * because we can't ask the user to individually trust
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * each one, since there may be many
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_MULTIPLE_TRUST, infile, "-y");
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* LINTED pointer cast may result in improper algnmnt */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* ask the user */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* user accepted */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* user aborted operation */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* now load the key store */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_DEBUG, "Loading keystore <%s>", keystore_file);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland set_passphrase_prompt(MSG_KEYSTORE_PASSPROMPT);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (open_keystore(err, keystore_file, prog, pkg_passphrase_cb,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland KEYSTORE_ACCESS_READWRITE | KEYSTORE_PATH_HARD, &keystore) != 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* now merge the new cert into the keystore */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_DEBUG, "Merging certificate <%s>",
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* merge all trusted certs found */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland for (i = 0; i < sk_X509_num(trustcerts); i++) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* LINTED pointer cast may result in improper algnmnt */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (merge_ca_cert(err, cert, keystore) != 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* merge user cert */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (merge_cert_and_key(err, cert, key, alias, keystore) != 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* now write it back out */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_DEBUG, "Closing keystore");
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland set_passphrase_prompt(MSG_KEYSTORE_PASSOUTPROMPT);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (close_keystore(err, keystore, pkg_passphrase_cb) != 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_ERR, MSG_NO_ADDCERT, infile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_INFO, MSG_TRUSTED, infile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland log_msg(LOG_MSG_INFO, MSG_ADDED, infile, alias);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* fallthrough intentional */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* Asks user to verify certificate data before proceeding */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlandstatic VerifyStatus verify_trust(X509 *cert)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* print cert data */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (print_cert(err, cert, KEYSTORE_FORMAT_TEXT,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland get_subject_display_name(cert), B_TRUE, stdout) != 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * The prompt is internationalized, but the valid
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * response values are fixed, to avoid any complex
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * multibyte processing that results in bugs
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (UI_add_input_boolean(ui, MSG_VERIFY_TRUST,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * if the cert does not appear to be a CA cert
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * r is not self-signed, verify that as well
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Name: is_ca_cert
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Desc: Determines if a given certificate has the attributes
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * of a CA certificate
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Returns: B_TRUE if certificate has attributes of a CA cert
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * B_FALSE otherwise
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * X509_check_purpose causes the extensions that we
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * care about to be decoded and stored in the X509
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * structure, so we must call it first
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * before checking for CA extensions in the X509
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) X509_check_purpose(x, X509_PURPOSE_ANY, 0);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* keyUsage if present should allow cert signing */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* If basicConstraints says not a CA then say so */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* no explicit not-a-CA flags set, so assume that it is */