/*
* 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
*/
/*
*/
/*
* This file comprises the main driver for this tool.
* Upon parsing the command verbs from user input, it
* branches to the appropriate modules to perform the
* requested task.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <libintl.h>
#include <libgen.h>
#include <errno.h>
#include <cryptoutil.h>
#include <security/cryptoki.h>
#include "common.h"
/*
* The verbcmd construct allows genericizing information about a verb so
* that it is easier to manipulate. Makes parsing code easier to read,
* fix, and extend with new verbs.
*/
typedef struct verbcmd_s {
char *verb;
int (*action)(int, char *[]);
int mode;
char *summary;
char *synopsis;
} verbcmd;
/* External declarations for supported verb actions. */
/* Forward declarations for "built-in" verb actions. */
#define TOKEN_IDX 0
"for keystore access")
#define SETPIN_SYN \
"setpin [ keystore=pkcs11 ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ usertype=so|user ]\n\t" \
\
"setpin keystore=nss\n\t\t" \
"[ token=token ]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t"
#define LIST_SYN \
"list [ token=token[:manuf[:serial]]]\n\t\t" \
"[ objtype=private|public|both ]\n\t\t" \
"[ label=label ]\n\t" \
\
"list objtype=cert[:[public | private | both ]]\n\t\t" \
"[ subject=subject-DN ]\n\t\t" \
"[ keystore=pkcs11 ]\n\t\t" \
"[ issuer=issuer-DN ]\n\t\t" \
"[ serial=serial number ]\n\t\t" \
"[ label=cert-label ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ criteria=valid|expired|both ]\n\t" \
\
"list objtype=key[:[public | private | both ]]\n\t\t" \
"[ keystore=pkcs11 ]\n\t\t" \
"[ label=key-label ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t" \
\
"list keystore=pkcs11 objtype=crl\n\t\t" \
"infile=crl-fn\n\t" \
\
"list keystore=nss objtype=cert\n\t\t" \
"[ subject=subject-DN ]\n\t\t" \
"[ issuer=issuer-DN ]\n\t\t" \
"[ serial=serial number ]\n\t\t" \
"[ nickname=cert-nickname ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t\t" \
"[ criteria=valid|expired|both ]\n\t" \
\
"list keystore=nss objtype=key\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t\t" \
"[ nickname=key-nickname ]\n\t" \
\
"list keystore=file objtype=cert\n\t\t" \
"[ subject=subject-DN ]\n\t\t" \
"[ issuer=issuer-DN ]\n\t\t" \
"[ serial=serial number ]\n\t\t" \
"[ infile=cert-fn ]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ criteria=valid|expired|both ]\n\t" \
\
"list keystore=file objtype=key\n\t\t" \
"[ infile=key-fn ]\n\t\t" \
"[ dir=directory-path ]\n\t" \
\
"list keystore=file objtype=crl\n\t\t" \
"infile=crl-fn\n\t"
#define DELETE_SYN \
"delete [ token=token[:manuf[:serial]]]\n\t\t" \
"[ objtype=private|public|both ]\n\t\t" \
"[ label=object-label ]\n\t" \
\
"delete keystore=nss objtype=cert\n\t\t" \
"[ subject=subject-DN ]\n\t\t" \
"[ issuer=issuer-DN ]\n\t\t" \
"[ serial=serial number ]\n\t\t" \
"[ label=cert-label ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t\t" \
"[ criteria=valid|expired|both ]\n\t" \
\
"delete keystore=nss objtype=key\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t\t" \
"[ nickname=key-nickname ]\n\t\t" \
\
"delete keystore=nss objtype=crl\n\t\t" \
"[ nickname=issuer-nickname ]\n\t\t" \
"[ subject=subject-DN ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t" \
\
"delete keystore=pkcs11 " \
"objtype=cert[:[public | private | both]]\n\t\t" \
"[ subject=subject-DN ]\n\t\t" \
"[ issuer=issuer-DN ]\n\t\t" \
"[ serial=serial number ]\n\t\t" \
"[ label=cert-label ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ criteria=valid|expired|both ]\n\t" \
\
"delete keystore=pkcs11 " \
"objtype=key[:[public | private | both]]\n\t\t" \
"[ label=key-label ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t" \
\
"delete keystore=pkcs11 objtype=crl\n\t\t" \
"infile=crl-fn\n\t" \
\
"delete keystore=file objtype=cert\n\t\t" \
"[ subject=subject-DN ]\n\t\t" \
"[ issuer=issuer-DN ]\n\t\t" \
"[ serial=serial number ]\n\t\t" \
"[ infile=cert-fn ]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ criteria=valid|expired|both ]\n\t" \
\
"delete keystore=file objtype=key\n\t\t" \
"[ infile=key-fn ]\n\t\t" \
"[ dir=directory-path ]\n\t" \
\
"delete keystore=file objtype=crl\n\t\t" \
"infile=crl-fn\n\t"
#define IMPORT_SYN \
"import [token=token[:manuf[:serial]]]\n\t\t" \
"infile=input-fn\n\t" \
\
"import keystore=nss objtype=cert\n\t\t" \
"infile=input-fn\n\t\t" \
"label=cert-label\n\t\t" \
"[ trust=trust-value ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t" \
\
"import keystore=nss objtype=crl\n\t\t" \
"infile=input-fn\n\t\t" \
"[ verifycrl=y|n ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t" \
\
"import keystore=pkcs11\n\t\t" \
"infile=input-fn\n\t\t" \
"label=label\n\t\t" \
"[ objtype=cert|key ]\n\t\t" \
"[ keytype=aes|arcfour|des|3des|generic ]\n\t\t" \
"[ sensitive=y|n ]\n\t\t" \
"[ extractable=y|n ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t" \
\
"import keystore=pkcs11 objtype=crl\n\t\t" \
"infile=input-crl-fn\n\t\t" \
"outcrl=output-crl-fn\n\t\t" \
"outformat=pem|der\n\t" \
\
"import keystore=file\n\t\t" \
"infile=input-fn\n\t\t" \
"outkey=output-key-fn\n\t\t" \
"outcert=output-cert-fn\n\t\t" \
"[ outformat=pem|der|pkcs12 ]\n\t" \
\
"import keystore=file objtype=crl\n\t\t" \
"infile=input-crl-fn\n\t\t" \
"outcrl=output-crl-fn\n\t\t" \
"outformat=pem|der\n\t"
#define EXPORT_SYN \
"export [token=token[:manuf[:serial]]]\n\t\t" \
"outfile=output-fn\n\t" \
\
"export keystore=nss\n\t\t" \
"outfile=output-fn\n\t\t" \
"[ objtype=cert|key ]\n\t\t" \
"[ subject=subject-DN ]\n\t\t" \
"[ issuer=issuer-DN ]\n\t\t" \
"[ serial=serial number ]\n\t\t" \
"[ nickname=cert-nickname ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBPrefix ]\n\t\t" \
"[ outformat=pem|der|pkcs12 ]\n\t" \
\
"export keystore=pkcs11\n\t\t" \
"outfile=output-fn\n\t\t" \
"[ objtype=cert|key ]\n\t\t" \
"[ label=label ]\n\t\t" \
"[ subject=subject-DN ]\n\t\t" \
"[ issuer=issuer-DN ]\n\t\t" \
"[ serial=serial number ]\n\t\t" \
"[ outformat=pem|der|pkcs12|raw ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t" \
\
"export keystore=file\n\t\t" \
"certfile=cert-input-fn\n\t\t" \
"keyfile=key-input-fn\n\t\t" \
"outfile=output-pkcs12-fn\n\t"
#define GENCERT_SYN \
"gencert listcurves\n\t" \
\
"gencert keystore=nss\n\t\t" \
"label=cert-nickname\n\t\t" \
"serial=serial number hex string\n\t\t" \
"[ -i ] | [subject=subject-DN]\n\t\t" \
"[ altname=[critical:]SubjectAltName ]\n\t\t" \
"[ keyusage=[critical:]usage,usage,...]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t\t" \
"[ keytype=rsa | ec [curve=ECC Curve Name] " \
"[hash=md5 | sha1 | sha256 | sha384 | sha512]]\n\t\t" \
"[ keytype=dsa [hash=sha1]]\n\t\t" \
"[ keylen=key-size ]\n\t\t" \
"[ trust=trust-value ]\n\t\t" \
"[ eku=[critical:]EKU name,...]\n\t\t" \
"[ lifetime=number-hour|number-day|number-year ]\n\t" \
\
"gencert [ keystore=pkcs11 ]\n\t\t" \
"label=key/cert-label\n\t\t" \
"serial=serial number hex string\n\t\t" \
"[ -i ] | [subject=subject-DN]\n\t\t" \
"[ altname=[critical:]SubjectAltName ]\n\t\t" \
"[ keyusage=[critical:]usage,usage,...]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ keytype=rsa | ec [curve=ECC Curve Name] " \
"[hash=md5 | sha1 | sha256 | sha384 | sha512]]\n\t\t" \
"[ keytype=dsa [hash=sha1 | sha256 ]]\n\t\t" \
"[ keylen=key-size ]\n\t\t" \
"[ eku=[critical:]EKU name,...]\n\t\t" \
"[ lifetime=number-hour|number-day|number-year ]\n\t" \
\
"gencert keystore=file\n\t\t" \
"outcert=cert_filename\n\t\t" \
"outkey=key_filename\n\t\t" \
"serial=serial number hex string\n\t\t" \
"[ -i ] | [subject=subject-DN]\n\t\t" \
"[ altname=[critical:]SubjectAltName ]\n\t\t" \
"[ keyusage=[critical:]usage,usage,...]\n\t\t" \
"[ format=der|pem ]\n\t\t" \
"[ keytype=rsa [hash=md5 | sha1 | sha256 | sha384 | sha512]]\n\t\t" \
"[ keytype=dsa [hash=sha1 | sha256 ]]\n\t\t" \
"[ keylen=key-size ]\n\t\t" \
"[ eku=[critical:]EKU name,...]\n\t\t" \
"[ lifetime=number-hour|number-day|number-year ]\n\t"
"request file")
#define GENCSR_SYN \
"gencsr listcurves\n\t" \
\
"gencsr keystore=nss \n\t\t" \
"nickname=cert-nickname\n\t\t" \
"outcsr=csr-fn\n\t\t" \
"[ -i ] | [subject=subject-DN]\n\t\t" \
"[ altname=[critical:]SubjectAltName ]\n\t\t" \
"[ keyusage=[critical:]usage,usage,...]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t\t" \
"[ keytype=rsa | ec [curve=ECC Curve Name] " \
"[hash=md5 | sha1 | sha256 | sha384 | sha512]]\n\t\t" \
"[ keytype=dsa [hash=sha1]]\n\t\t" \
"[ keylen=key-size ]\n\t\t" \
"[ eku=[critical:]EKU name,...]\n\t\t" \
"[ format=pem|der ]\n\t" \
\
"gencsr [ keystore=pkcs11 ]\n\t\t" \
"label=key-label\n\t\t" \
"outcsr=csr-fn\n\t\t" \
"[ -i ] | [subject=subject-DN]\n\t\t" \
"[ altname=[critical:]SubjectAltName ]\n\t\t" \
"[ keyusage=[critical:]usage,usage,...]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ keytype=rsa | ec [curve=ECC Curve Name] " \
"[hash=md5 | sha1 | sha256 | sha384 | sha512]]\n\t\t" \
"[ keytype=dsa [hash=sha1 | sha256 ]]\n\t\t" \
"[ keylen=key-size ]\n\t\t" \
"[ eku=[critical:]EKU name,...]\n\t\t" \
"[ format=pem|der ]]\n\t" \
\
"gencsr keystore=file\n\t\t" \
"outcsr=csr-fn\n\t\t" \
"outkey=key-fn\n\t\t" \
"[ -i ] | [subject=subject-DN]\n\t\t" \
"[ altname=[critical:]SubjectAltName ]\n\t\t" \
"[ keyusage=[critical:]usage,usage,...]\n\t\t" \
"[ keytype=rsa [hash=md5 | sha1 | sha256 | sha384 | sha512]]\n\t\t" \
"[ keytype=dsa [hash=sha1 | sha256 ]]\n\t\t" \
"[ keylen=key-size ]\n\t\t" \
"[ eku=[critical:]EKU name,...]\n\t\t" \
"[ format=pem|der ]\n\t"
"from an external source")
#define DOWNLOAD_SYN \
"download url=url_str\n\t\t" \
"[ objtype=crl|cert ]\n\t\t" \
"[ http_proxy=proxy_str ]\n\t\t" \
"[ outfile = outfile ]\n\t"
#define GENKEY_SYN \
"genkey [ keystore=pkcs11 ]\n\t\t" \
"label=key-label\n\t\t" \
"[ keytype=aes|arcfour|des|3des|generic ]\n\t\t" \
"[ keylen=key-size (AES, ARCFOUR or GENERIC only)]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ sensitive=y|n ]\n\t\t" \
"[ extractable=y|n ]\n\t\t" \
"[ print=y|n ]\n\t" \
\
"genkey keystore=nss\n\t\t" \
"label=key-label\n\t\t" \
"[ keytype=aes|arcfour|des|3des|generic ]\n\t\t" \
"[ keylen=key-size (AES, ARCFOUR or GENERIC only)]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t" \
\
"genkey keystore=file\n\t\t" \
"outkey=key-fn\n\t\t" \
"[ keytype=aes|arcfour|des|3des|generic ]\n\t\t" \
"[ keylen=key-size (AES, ARCFOUR or GENERIC only)]\n\t\t" \
"[ print=y|n ]\n\t"
#define SIGNCSR_SYN \
"signcsr keystore=pkcs11\n\t\t" \
"signkey=label (label of signing key)\n\t\t" \
"csr=CSR filename\n\t\t" \
"serial=serial number hex string\n\t\t" \
"outcert=filename for final certificate\n\t\t" \
"issuer=issuer-DN\n\t\t" \
"[ store=y|n ] (store the new cert on the token, default=n)\n\t\t" \
"[ outlabel=certificate label ]\n\t\t" \
"[ format=pem|der ] (output format)\n\t\t" \
"[ subject=subject-DN ] (new subject name)\n\t\t" \
"[ altname=subjectAltName ]\n\t\t" \
"[ keyusage=[critical:]usage,...]\n\t\t" \
"[ eku=[critical:]EKU Name,...]\n\t\t" \
"[ lifetime=number-hour|number-day|number-year ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t" \
\
"signcsr keystore=file\n\t\t" \
"signkey=filename\n\t\t" \
"csr=CSR filename\n\t\t" \
"serial=serial number hex string\n\t\t" \
"outcert=filename for final certificate\n\t\t" \
"issuer=issuer-DN\n\t\t" \
"[ format=pem|der ] (output format)\n\t\t" \
"[ subject=subject-DN ] (new subject name)\n\t\t" \
"[ altname=subjectAltName ]\n\t\t" \
"[ keyusage=[critical:]usage,...]\n\t\t" \
"[ lifetime=number-hour|number-day|number-year ]\n\t\t" \
"[ eku=[critical:]EKU Name,...]\n\t" \
\
"signcsr keystore=nss\n\t\t" \
"signkey=label (label of signing key)\n\t\t" \
"csr=CSR filename\n\t\t" \
"serial=serial number hex string\n\t\t" \
"outcert=filename for final certificate\n\t\t" \
"issuer=issuer-DN\n\t\t" \
"[ store=y|n ] (store the new cert in NSS DB, default=n)\n\t\t" \
"[ outlabel=certificate label ]\n\t\t" \
"[ format=pem|der ] (output format)\n\t\t" \
"[ subject=subject-DN ] (new subject name)\n\t\t" \
"[ altname=subjectAltName ]\n\t\t" \
"[ keyusage=[critical:]usage,...]\n\t\t" \
"[ eku=[critical:]EKU Name,...]\n\t\t" \
"[ lifetime=number-hour|number-day|number-year ]\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t"
#define INITTOKEN_SYN \
"inittoken \n\t\t" \
"[ currlabel=token[:manuf[:serial]]]\n\t\t" \
"[ newlabel=new token label ]\n\t"
#define GENKEYPAIR_SYN \
"genkeypair listcurves\n\t" \
\
"genkeypair keystore=nss\n\t\t" \
"label=key-nickname\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ dir=directory-path ]\n\t\t" \
"[ prefix=DBprefix ]\n\t\t" \
"[ keytype=rsa | dsa | ec [curve=ECC Curve Name]]\n\t\t" \
"[ keylen=key-size ]\n\t" \
\
"genkeypair [ keystore=pkcs11 ]\n\t\t" \
"label=key-label\n\t\t" \
"[ token=token[:manuf[:serial]]]\n\t\t" \
"[ keytype=rsa | dsa | ec [curve=ECC Curve Name]]\n\t\t" \
"[ keylen=key-size ]\n\t" \
\
"genkeypair keystore=file\n\t\t" \
"outkey=key_filename\n\t\t" \
"[ format=der|pem ]\n\t\t" \
"[ keytype=rsa|dsa ]\n\t\t" \
"[ keylen=key-size ]\n\t"
};
static char *prog;
static void usage(int);
static void
{
}
/*
* Usage information. This function must be updated when new verbs or
* options are added.
*/
static void
{
int i;
/* Display this block only in command-line mode. */
prog);
prog);
/* Display only those verbs that match the current tool mode. */
if (idx == -1) {
for (i = 0; i < num_cmds; i++) {
}
"Ex: pktool gencert help\n\n",
gettext("\nFurther details on the "
"subcommands can be found by adding"));
} else {
}
}
/*
* Provide help, in the form of displaying the usage.
*/
static int
/* ARGSUSED */
{
usage(-1);
return (0);
}
/*
* Process arguments from the argfile and create a new
*/
static int
{
char *p;
int nargs = 0;
gettext("Cannot read argfile %s: %s\n"),
return (errno);
}
int j;
/* remove trailing whitespace */
argline[j] = 0;
j--;
}
/* If it was a blank line, get the next one. */
continue;
(nargs + 1) * sizeof (char *));
perror("memory error");
return (errno);
}
if (p == NULL) {
perror("memory error");
return (errno);
}
nargs++;
}
return (0);
}
/*
* MAIN() -- where all the action is
*/
int
/* ARGSUSED2 */
{
int rv;
int pk_argc = 0;
int save_errno = 0;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D. */
#endif
(void) textdomain(TEXT_DOMAIN);
/* Get program base name and move pointer over 0th arg. */
/* Set up for debug and error output. */
if (argc == 0) {
usage(-1);
return (1);
}
/* Check for help options. For CLIP-compliance. */
if (rv)
return (rv);
usage(-1);
return (1);
}
/* Always turns off Metaslot so that we can see softtoken. */
save_errno = errno;
gettext("Disabling Metaslot failed (%s)."),
return (1);
}
/* Begin parsing command line. */
}
/* Check for valid verb (or an abbreviation of it). */
found = -1;
for (i = 0; i < num_cmds; i++) {
if (found < 0) {
found = i;
break;
}
}
}
/* Stop here if no valid verb found. */
if (found < 0) {
pk_argv[0]);
return (1);
}
/* Get to work! */
switch (rv) {
case PK_ERR_NONE:
break; /* Command succeeded, do nothing. */
case PK_ERR_USAGE:
break;
case PK_ERR_QUIT:
exit(0);
/* NOTREACHED */
case PK_ERR_PK11:
case PK_ERR_SYSTEM:
case PK_ERR_OPENSSL:
case PK_ERR_NSS:
default:
break;
}
return (rv);
}