Bug 213359 – enhance PK12util to extract certs from p12 file. r=Neil.williams

git-svn-id: svn://10.0.0.236/trunk@226170 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
nelson%bolyard.com 2007-05-09 03:14:58 +00:00
parent 9c1042b2f2
commit 7c6abf4ec3

View File

@ -48,6 +48,7 @@
static char *progName;
PRBool pk12_debugging = PR_FALSE;
PRBool dumpRawFile;
PRIntn pk12uErrno = 0;
@ -55,16 +56,18 @@ static void
Usage(char *progName)
{
#define FPS PR_fprintf(PR_STDERR,
FPS "Usage: %s -i importfile [-d certdir] [-P dbprefix] [-h tokenname]\n",
FPS "Usage: %s -i importfile [-d certdir] [-P dbprefix] [-h tokenname] [-v]\n",
progName);
FPS "\t\t [-k slotpwfile | -K slotpw] [-w p12filepwfile | -W p12filepw]\n");
FPS "\t\t [-v]\n");
FPS "Usage: %s -l listfile [-d certdir] [-P dbprefix] [-h tokenname]\n",
FPS "Usage: %s -l listfile [-d certdir] [-P dbprefix] [-h tokenname] [-r]\n",
progName);
FPS "\t\t [-k slotpwfile | -K slotpw] [-w p12filepwfile | -W p12filepw]\n");
FPS "Usage: %s -o exportfile -n certname [-d certdir] [-P dbprefix]\n", progName);
FPS "Usage: %s -o exportfile -n certname [-d certdir] [-P dbprefix] [-v]\n",
progName);
FPS "\t\t [-k slotpwfile | -K slotpw] [-w p12filepwfile | -W p12filepw]\n");
FPS "\t\t [-v]\n");
exit(PK12UERR_USAGE);
}
@ -490,7 +493,6 @@ P12U_ImportPKCS12Object(char *in_file, PK11SlotInfo *slot,
SEC_PKCS12DecoderContext *p12dcx = NULL;
SECItem uniPwitem = { 0 };
SECStatus rv = SECFailure;
int error;
rv = P12U_InitSlot(slot, slotPw);
if (rv != SECSuccess) {
@ -754,12 +756,27 @@ P12U_ListPKCS12File(char *in_file, PK11SlotInfo *slot,
SECU_PrintError(progName,"PKCS12 decode iterate bags failed");
pk12uErrno = PK12UERR_DECODEIMPTBAGS;
rv = SECFailure;
}
else {
} else {
int fileCounter = 0;
while (SEC_PKCS12DecoderIterateNext(p12dcx, &dip) == SECSuccess) {
switch (dip->type) {
case SEC_OID_PKCS12_V1_CERT_BAG_ID:
printf("Certificate");
if (dumpRawFile) {
PRFileDesc * fd;
char fileName[20];
sprintf(fileName, "file%04d.der", ++fileCounter);
fd = PR_Open(fileName,
PR_CREATE_FILE | PR_RDWR | PR_TRUNCATE,
0600);
if (!fd) {
SECU_PrintError(progName,
"Cannot create output file");
} else {
PR_Write(fd, dip->der->data, dip->der->len);
PR_Close(fd);
}
} else
if (SECU_PrintSignedData(stdout, dip->der,
(dip->hasKey) ? "(has private key)" : "",
0, SECU_PrintCertificate) != 0) {
@ -851,6 +868,7 @@ enum {
opt_List,
opt_Nickname,
opt_Export,
opt_Raw,
opt_P12FilePWFile,
opt_P12FilePW,
opt_DBPrefix,
@ -867,6 +885,7 @@ static secuCommandFlag pk12util_options[] =
{ /* opt_List */ 'l', PR_TRUE, 0, PR_FALSE },
{ /* opt_Nickname */ 'n', PR_TRUE, 0, PR_FALSE },
{ /* opt_Export */ 'o', PR_TRUE, 0, PR_FALSE },
{ /* opt_Raw */ 'r', PR_FALSE, 0, PR_FALSE },
{ /* opt_P12FilePWFile */ 'w', PR_TRUE, 0, PR_FALSE },
{ /* opt_P12FilePW */ 'W', PR_TRUE, 0, PR_FALSE },
{ /* opt_DBPrefix */ 'P', PR_TRUE, 0, PR_FALSE },
@ -945,6 +964,9 @@ main(int argc, char **argv)
if (pk12util.options[opt_DBPrefix].activated) {
dbprefix = pk12util.options[opt_DBPrefix].arg;
}
if (pk12util.options[opt_Raw].activated) {
dumpRawFile = PR_TRUE;
}
P12U_Init(SECU_ConfigDirectory(NULL), dbprefix,
pk12util.options[opt_List].activated);