From 7c6abf4ec3eb64c141f222acf46db61baaef6c23 Mon Sep 17 00:00:00 2001 From: "nelson%bolyard.com" Date: Wed, 9 May 2007 03:14:58 +0000 Subject: [PATCH] =?UTF-8?q?Bug=20213359=20=C2=96=20enhance=20PK12util=20to?= =?UTF-8?q?=20extract=20certs=20from=20p12=20file.=20r=3DNeil.williams?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: svn://10.0.0.236/trunk@226170 18797224-902f-48f8-a5cc-f745e15eee43 --- mozilla/security/nss/cmd/pk12util/pk12util.c | 38 +++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/mozilla/security/nss/cmd/pk12util/pk12util.c b/mozilla/security/nss/cmd/pk12util/pk12util.c index 11fd975c806..a032813e7b2 100644 --- a/mozilla/security/nss/cmd/pk12util/pk12util.c +++ b/mozilla/security/nss/cmd/pk12util/pk12util.c @@ -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);