michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "secutil.h" michael@0: #include "nss.h" michael@0: #include michael@0: michael@0: #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4)) michael@0: #if !defined(WIN32) michael@0: extern int fprintf(FILE *, char *, ...); michael@0: #endif michael@0: #endif michael@0: #include "plgetopt.h" michael@0: michael@0: static void Usage(char *progName) michael@0: { michael@0: fprintf(stderr, michael@0: "Usage: %s [-r] [-i input] [-o output]\n", michael@0: progName); michael@0: fprintf(stderr, "%-20s For formatted items, dump raw bytes as well\n", michael@0: "-r"); michael@0: fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n", michael@0: "-i input"); michael@0: fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n", michael@0: "-o output"); michael@0: exit(-1); michael@0: } michael@0: michael@0: int main(int argc, char **argv) michael@0: { michael@0: char *progName; michael@0: FILE *outFile; michael@0: PRFileDesc *inFile; michael@0: SECItem der; michael@0: SECStatus rv; michael@0: PRInt16 xp_error; michael@0: PRBool raw = PR_FALSE; michael@0: PLOptState *optstate; michael@0: PLOptStatus status; michael@0: michael@0: progName = strrchr(argv[0], '/'); michael@0: progName = progName ? progName+1 : argv[0]; michael@0: michael@0: /* Parse command line arguments */ michael@0: inFile = 0; michael@0: outFile = 0; michael@0: optstate = PL_CreateOptState(argc, argv, "i:o:r"); michael@0: while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) { michael@0: switch (optstate->option) { michael@0: case 'i': michael@0: inFile = PR_Open(optstate->value, PR_RDONLY, 0); michael@0: if (!inFile) { michael@0: fprintf(stderr, "%s: unable to open \"%s\" for reading\n", michael@0: progName, optstate->value); michael@0: return -1; michael@0: } michael@0: break; michael@0: michael@0: case 'o': michael@0: outFile = fopen(optstate->value, "w"); michael@0: if (!outFile) { michael@0: fprintf(stderr, "%s: unable to open \"%s\" for writing\n", michael@0: progName, optstate->value); michael@0: return -1; michael@0: } michael@0: break; michael@0: michael@0: case 'r': michael@0: raw = PR_TRUE; michael@0: break; michael@0: michael@0: default: michael@0: Usage(progName); michael@0: break; michael@0: } michael@0: } michael@0: if (status == PL_OPT_BAD) michael@0: Usage(progName); michael@0: michael@0: if (!inFile) inFile = PR_STDIN; michael@0: if (!outFile) outFile = stdout; michael@0: michael@0: rv = NSS_NoDB_Init(NULL); /* XXX */ michael@0: if (rv != SECSuccess) { michael@0: SECU_PrintPRandOSError(progName); michael@0: return -1; michael@0: } michael@0: michael@0: rv = SECU_ReadDERFromFile(&der, inFile, PR_FALSE, PR_FALSE); michael@0: if (rv == SECSuccess) { michael@0: rv = DER_PrettyPrint(outFile, &der, raw); michael@0: if (rv == SECSuccess) michael@0: return 0; michael@0: } michael@0: michael@0: xp_error = PORT_GetError(); michael@0: if (xp_error) { michael@0: SECU_PrintError(progName, "error %d", xp_error); michael@0: } michael@0: if (errno) { michael@0: SECU_PrintSystemError(progName, "errno=%d", errno); michael@0: } michael@0: return 1; michael@0: }