security/nss/cmd/derdump/derdump.c

Wed, 31 Dec 2014 06:55:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:50 +0100
changeset 2
7e26c7da4463
permissions
-rw-r--r--

Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 4
michael@0 5 #include "secutil.h"
michael@0 6 #include "nss.h"
michael@0 7 #include <errno.h>
michael@0 8
michael@0 9 #if defined(XP_WIN) || (defined(__sun) && !defined(SVR4))
michael@0 10 #if !defined(WIN32)
michael@0 11 extern int fprintf(FILE *, char *, ...);
michael@0 12 #endif
michael@0 13 #endif
michael@0 14 #include "plgetopt.h"
michael@0 15
michael@0 16 static void Usage(char *progName)
michael@0 17 {
michael@0 18 fprintf(stderr,
michael@0 19 "Usage: %s [-r] [-i input] [-o output]\n",
michael@0 20 progName);
michael@0 21 fprintf(stderr, "%-20s For formatted items, dump raw bytes as well\n",
michael@0 22 "-r");
michael@0 23 fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
michael@0 24 "-i input");
michael@0 25 fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
michael@0 26 "-o output");
michael@0 27 exit(-1);
michael@0 28 }
michael@0 29
michael@0 30 int main(int argc, char **argv)
michael@0 31 {
michael@0 32 char *progName;
michael@0 33 FILE *outFile;
michael@0 34 PRFileDesc *inFile;
michael@0 35 SECItem der;
michael@0 36 SECStatus rv;
michael@0 37 PRInt16 xp_error;
michael@0 38 PRBool raw = PR_FALSE;
michael@0 39 PLOptState *optstate;
michael@0 40 PLOptStatus status;
michael@0 41
michael@0 42 progName = strrchr(argv[0], '/');
michael@0 43 progName = progName ? progName+1 : argv[0];
michael@0 44
michael@0 45 /* Parse command line arguments */
michael@0 46 inFile = 0;
michael@0 47 outFile = 0;
michael@0 48 optstate = PL_CreateOptState(argc, argv, "i:o:r");
michael@0 49 while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) {
michael@0 50 switch (optstate->option) {
michael@0 51 case 'i':
michael@0 52 inFile = PR_Open(optstate->value, PR_RDONLY, 0);
michael@0 53 if (!inFile) {
michael@0 54 fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
michael@0 55 progName, optstate->value);
michael@0 56 return -1;
michael@0 57 }
michael@0 58 break;
michael@0 59
michael@0 60 case 'o':
michael@0 61 outFile = fopen(optstate->value, "w");
michael@0 62 if (!outFile) {
michael@0 63 fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
michael@0 64 progName, optstate->value);
michael@0 65 return -1;
michael@0 66 }
michael@0 67 break;
michael@0 68
michael@0 69 case 'r':
michael@0 70 raw = PR_TRUE;
michael@0 71 break;
michael@0 72
michael@0 73 default:
michael@0 74 Usage(progName);
michael@0 75 break;
michael@0 76 }
michael@0 77 }
michael@0 78 if (status == PL_OPT_BAD)
michael@0 79 Usage(progName);
michael@0 80
michael@0 81 if (!inFile) inFile = PR_STDIN;
michael@0 82 if (!outFile) outFile = stdout;
michael@0 83
michael@0 84 rv = NSS_NoDB_Init(NULL); /* XXX */
michael@0 85 if (rv != SECSuccess) {
michael@0 86 SECU_PrintPRandOSError(progName);
michael@0 87 return -1;
michael@0 88 }
michael@0 89
michael@0 90 rv = SECU_ReadDERFromFile(&der, inFile, PR_FALSE, PR_FALSE);
michael@0 91 if (rv == SECSuccess) {
michael@0 92 rv = DER_PrettyPrint(outFile, &der, raw);
michael@0 93 if (rv == SECSuccess)
michael@0 94 return 0;
michael@0 95 }
michael@0 96
michael@0 97 xp_error = PORT_GetError();
michael@0 98 if (xp_error) {
michael@0 99 SECU_PrintError(progName, "error %d", xp_error);
michael@0 100 }
michael@0 101 if (errno) {
michael@0 102 SECU_PrintSystemError(progName, "errno=%d", errno);
michael@0 103 }
michael@0 104 return 1;
michael@0 105 }

mercurial