signcsr.c revision d00756ccb34596a328f8a15d1965da5412d366d0
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * CDDL HEADER START
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * The contents of this file are subject to the terms of the
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * Common Development and Distribution License (the "License").
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * You may not use this file except in compliance with the License.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * See the License for the specific language governing permissions
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * and limitations under the License.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * When distributing Covered Code, include this CDDL HEADER in each
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * If applicable, add the following below this CDDL HEADER, with the
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * fields enclosed by brackets "[]" replaced with your own identifying
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * information: Portions Copyright [yyyy] [name of copyright owner]
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * CDDL HEADER END
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * Use is subject to license terms.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys#pragma ident "%Z%%M% %I% %E% SMI"
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * This file implements the sign CSR operation for this tool.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys#define SET_VALUE(f, s) \
d00756ccb34596a328f8a15d1965da5412d366d0wyllysread_csrdata(KMF_HANDLE_T handle, char *csrfile, KMF_CSR_DATA *csrdata)
d00756ccb34596a328f8a15d1965da5412d366d0wyllys rv = kmf_read_input_file(handle, csrfile, &csrfiledata);
d00756ccb34596a328f8a15d1965da5412d366d0wyllys rv = kmf_pem_to_der(csrfiledata.Data, csrfiledata.Length,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * If the CSR is ok, now we can generate the final certificate.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys (void) memset(signedCert, 0, sizeof (KMF_X509_CERTIFICATE));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys SET_VALUE(kmf_set_cert_version(signedCert, 2), "version number");
d00756ccb34596a328f8a15d1965da5412d366d0wyllys SET_VALUE(kmf_set_cert_serial(signedCert, serial), "serial number");
d00756ccb34596a328f8a15d1965da5412d366d0wyllys SET_VALUE(kmf_set_cert_validity(signedCert, NULL, ltime),
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "validity time");
d00756ccb34596a328f8a15d1965da5412d366d0wyllys SET_VALUE(kmf_set_cert_issuer(signedCert, &issuerDN),
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "Issuer Name");
d00756ccb34596a328f8a15d1965da5412d366d0wyllys SET_VALUE(kmf_set_cert_subject(signedCert, &subjectDN),
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "Subject Name");
d00756ccb34596a328f8a15d1965da5412d366d0wyllys signedCert->certificate.subject = csrdata->csr.subject;
d00756ccb34596a328f8a15d1965da5412d366d0wyllys signedCert->certificate.extensions = csrdata->csr.extensions;
d00756ccb34596a328f8a15d1965da5412d366d0wyllys if (kubits != 0) {
d00756ccb34596a328f8a15d1965da5412d366d0wyllys SET_VALUE(kmf_set_cert_ku(signedCert, kucrit, kubits),
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "KeyUsage");
d00756ccb34596a328f8a15d1965da5412d366d0wyllys for (i = 0; rv == KMF_OK && i < ekulist->eku_count; i++) {
d00756ccb34596a328f8a15d1965da5412d366d0wyllyspk_sign_cert(KMF_HANDLE_T handle, KMF_X509_CERTIFICATE *cert,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_KEYSTORE_TYPE_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_KEY_HANDLE_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys /* cert data that is to be signed */
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_X509_CERTIFICATE_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys /* output buffer for the signed cert */
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_CERT_DATA_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys if ((rv = kmf_sign_cert(handle, numattr, attrlist)) != KMF_OK) {
d00756ccb34596a328f8a15d1965da5412d366d0wyllys /* verify the signature first */
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_CSR_DATA_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "verification failed.\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys rv = build_cert_from_csr(&csrdata, &signedCert, serial, ltime,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * Find the signing key.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_KEYSTORE_TYPE_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_KEY_FILENAME_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_KEYCLASS_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_KEY_HANDLE_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_COUNT_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "Error finding CA signing key\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys rv = pk_sign_cert(handle, &signedCert, &cakey, &certdata);
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "Error signing certificate.\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys KMF_BIGINT *serial, char *certfile, char *issuer, char *subject,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys char *altname, KMF_GENERALNAMECHOICES alttype, int altcrit,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys /* verify the signature first */
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_CSR_DATA_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "verification failed.\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * Find the signing key.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_KEYSTORE_TYPE_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_TOKEN_LABEL_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_KEYLABEL_ATTR, signkey,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_PRIVATE_BOOL_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_TOKEN_BOOL_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_KEYCLASS_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_CREDENTIAL_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_COUNT_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_KEY_HANDLE_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * If we found the key, now we can sign the cert.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys rv = pk_sign_cert(handle, &signedCert, &casignkey, &outcert);
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "Error signing certificate.\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * Store it on the token if the user asked for it.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_KEYSTORE_TYPE_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys kmf_set_attr_at_index(attrlist, numattr, KMF_CERT_DATA_ATTR,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "on PKCS#11 token.\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys /* Not fatal, we can still write it to a file. */
d00756ccb34596a328f8a15d1965da5412d366d0wyllys * sign a CSR and generate an x509v3 certificate file.
d00756ccb34596a328f8a15d1965da5412d366d0wyllys extern char *optarg_av;
d00756ccb34596a328f8a15d1965da5412d366d0wyllys /* Parse command line options. Do NOT i18n/l10n. */
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "k:(keystore)c:(csr)T:(token)d:(dir)"
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "p:(prefix)S:(serial)s:(subject)a:(altname)"
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "t:(store)F:(format)K:(keyusage)l:(signkey)"
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "L:(lifetime)e:(eku)i:(issuer)"
d00756ccb34596a328f8a15d1965da5412d366d0wyllys /* No additional args allowed. */
d00756ccb34596a328f8a15d1965da5412d366d0wyllys if ((rv = kmf_initialize(&kmfhandle, NULL, NULL)) != KMF_OK) {
d00756ccb34596a328f8a15d1965da5412d366d0wyllys /* Assume keystore = PKCS#11 if not specified. */
d00756ccb34596a328f8a15d1965da5412d366d0wyllys (void) fprintf(stderr, gettext("The signing key label "
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "or filename was not specified\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys (void) fprintf(stderr, gettext("The CSR filename was not"
d00756ccb34596a328f8a15d1965da5412d366d0wyllys " specified\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys (void) fprintf(stderr, gettext("The output certificate file "
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "was not specified\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "was not specified\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys if (kstype == KMF_KEYSTORE_PK11TOKEN && EMPTYSTRING(token_spec)) {
d00756ccb34596a328f8a15d1965da5412d366d0wyllys } else if (kstype == KMF_KEYSTORE_NSS && EMPTYSTRING(token_spec)) {
d00756ccb34596a328f8a15d1965da5412d366d0wyllys rv = kmf_hexstr_to_bytes((uchar_t *)serstr, &bytes, &bytelen);
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "must be specified as a hex number "
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "(ex: 0x0102030405ffeeddee)\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys (void) fprintf(stderr, gettext("The serial number was not"
d00756ccb34596a328f8a15d1965da5412d366d0wyllys " specified\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys /* Need to get password for private key access */
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "be specified as a comma-separated list. "
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "See the man page for details.\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "must be specified as a name=value pair. "
d00756ccb34596a328f8a15d1965da5412d366d0wyllys "See the man page for details.\n"));
d00756ccb34596a328f8a15d1965da5412d366d0wyllys /* advance the altname past the '=' sign */
d00756ccb34596a328f8a15d1965da5412d366d0wyllys if (format && (fmt = Str2Format(format)) == KMF_FORMAT_UNDEF) {
d00756ccb34596a328f8a15d1965da5412d366d0wyllys signkey, csrfile, &serial, certfile, issuer, subject,
d00756ccb34596a328f8a15d1965da5412d366d0wyllys signkey, csrfile, &serial, certfile, issuer, subject,