addcert.c revision 5c51f1241dbbdf2656d0e10011981411ed0c9673
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <locale.h>
#include <pkglib.h>
#include <libinst.h>
#include <pkgerr.h>
#include <keystore.h>
#include "pkgadm.h"
#include "pkgadm_msgs.h"
typedef enum {
} VerifyStatus;
/*
* Name: addcert
* Desc: Imports a user certificate into the keystore, along with a
* private key.
* Returns: 0 on success, non-zero otherwise.
*/
int
{
int i;
char *keystore_base = NULL;
char *homedir;
char *import_passarg = NULL;
char *informat_str = NULL;
int ret = 1;
switch (i) {
case 'a':
break;
case 'k':
break;
case 'e':
break;
case 'f':
break;
case 'n':
break;
case 'P':
break;
case 'p':
break;
case 'R':
break;
case 't':
break;
case 'y':
break;
case ':':
/* LINTED fallthrough intentional */
case '?':
default:
goto cleanup;
}
}
/* for untrusted (user) certs, we require a name */
goto cleanup;
/* for trusted certs, we cannot have a name */
goto cleanup;
}
/* for trusted certs, we cannot have a private key */
goto cleanup;
}
/* last argument should be the path to the certificate */
goto cleanup;
infile = "stdin";
} else {
goto cleanup;
}
}
/*
* if specific key file supplied, open it, otherwise open
* default (stdin)
*/
goto cleanup;
}
} else {
inkeyfile = "stdin";
}
/* set up proper keystore */
goto cleanup;
}
goto cleanup;
}
}
if (keystore_base == NULL) {
/*
* If we have an alternate
* root, then we have no choice but to use
* root's keystore on that alternate root,
* since there is no way to resolve a
* user's home dir given an alternate root
*/
MAXPATHLEN) >= MAXPATHLEN) {
goto cleanup;
}
} else {
/*
* not superuser, but no home dir, so
* use superuser's keystore
*/
MAXPATHLEN) >= MAXPATHLEN) {
goto cleanup;
}
} else {
MAXPATHLEN) >= MAXPATHLEN) {
homedir);
goto cleanup;
}
MAXPATHLEN) >= MAXPATHLEN) {
goto cleanup;
}
}
}
} else {
MAXPATHLEN) >= MAXPATHLEN) {
goto cleanup;
}
}
/* figure out input format */
if (informat_str == NULL) {
} else {
} else {
goto cleanup;
}
}
err = pkgerr_new();
if (trusted) {
/* load all possible certs */
&trustcerts) != 0) {
goto cleanup;
}
/* we must have gotten at least one cert, if not, fail */
goto cleanup;
}
} else {
/* first, try to load user certificate and key */
goto cleanup;
}
/* we must have gotten a cert, if not, fail */
goto cleanup;
}
/*
* if we are importing a user cert, and did not get
* a key, try to load it from the key file
*/
goto cleanup;
} else {
"Loading private key <%s>", inkeyfile);
goto cleanup;
}
goto cleanup;
}
}
}
}
if (trusted) {
/* check validity date of all certificates */
for (i = 0; i < sk_X509_num(trustcerts); i++) {
/* LINTED pointer cast may result in improper algnmnt */
infile);
goto cleanup;
}
}
} else {
/* check validity date of user certificate */
goto cleanup;
}
}
if (trusted && !implicit_trust) {
/*
* if importing more than one cert, must use implicit trust,
* because we can't ask the user to individually trust
* each one, since there may be many
*/
goto cleanup;
} else {
/* LINTED pointer cast may result in improper algnmnt */
}
/* ask the user */
switch (verify_trust(cert)) {
case Accept:
/* user accepted */
break;
case Reject:
/* user aborted operation */
goto cleanup;
case VerifyFailed:
default:
goto cleanup;
}
}
/* now load the key store */
goto cleanup;
}
/* now merge the new cert into the keystore */
if (trusted) {
/* merge all trusted certs found */
for (i = 0; i < sk_X509_num(trustcerts); i++) {
/* LINTED pointer cast may result in improper algnmnt */
goto cleanup;
} else {
}
}
} else {
/* merge user cert */
goto cleanup;
}
}
/* now write it back out */
goto cleanup;
}
if (trusted) {
} else {
}
ret = 0;
/* fallthrough intentional */
return (ret);
}
/* Asks user to verify certificate data before proceeding */
{
char vfy_trust = 'y';
err = pkgerr_new();
/* print cert data */
ret = VerifyFailed;
goto cleanup;
}
ret = VerifyFailed;
goto cleanup;
}
/*
* The prompt is internationalized, but the valid
* response values are fixed, to avoid any complex
* multibyte processing that results in bugs
*/
"",
"yY", "nN",
UI_INPUT_FLAG_ECHO, &vfy_trust) <= 0) {
ret = VerifyFailed;
goto cleanup;
}
if (UI_process(ui) != 0) {
ret = VerifyFailed;
goto cleanup;
}
if (vfy_trust != 'y') {
goto cleanup;
}
/*
* if the cert does not appear to be a CA cert
* r is not self-signed, verify that as well
*/
if (!is_ca_cert(cert)) {
ret = VerifyFailed;
goto cleanup;
}
if (UI_add_input_boolean(ui,
"",
"yY", "nN",
UI_INPUT_FLAG_ECHO, &vfy_trust) <= 0) {
ret = VerifyFailed;
goto cleanup;
}
if (UI_process(ui) != 0) {
ret = VerifyFailed;
goto cleanup;
}
if (vfy_trust != 'y') {
goto cleanup;
}
}
return (ret);
}
/*
* Name: is_ca_cert
* Desc: Determines if a given certificate has the attributes
* of a CA certificate
* Returns: B_TRUE if certificate has attributes of a CA cert
* B_FALSE otherwise
*/
static boolean_t
is_ca_cert(X509 *x)
{
/*
* X509_check_purpose causes the extensions that we
* care about to be decoded and stored in the X509
* structure, so we must call it first
* before checking for CA extensions in the X509
* structure
*/
(void) X509_check_purpose(x, X509_PURPOSE_ANY, 0);
/* keyUsage if present should allow cert signing */
if ((x->ex_flags & EXFLAG_KUSAGE) &&
!(x->ex_kusage & KU_KEY_CERT_SIGN)) {
return (B_FALSE);
}
/* If basicConstraints says not a CA then say so */
if (x->ex_flags & EXFLAG_BCONS) {
return (B_FALSE);
}
}
/* no explicit not-a-CA flags set, so assume that it is */
return (B_TRUE);
}