1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/nss/cmd/derdump/derdump.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,105 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#include "secutil.h" 1.9 +#include "nss.h" 1.10 +#include <errno.h> 1.11 + 1.12 +#if defined(XP_WIN) || (defined(__sun) && !defined(SVR4)) 1.13 +#if !defined(WIN32) 1.14 +extern int fprintf(FILE *, char *, ...); 1.15 +#endif 1.16 +#endif 1.17 +#include "plgetopt.h" 1.18 + 1.19 +static void Usage(char *progName) 1.20 +{ 1.21 + fprintf(stderr, 1.22 + "Usage: %s [-r] [-i input] [-o output]\n", 1.23 + progName); 1.24 + fprintf(stderr, "%-20s For formatted items, dump raw bytes as well\n", 1.25 + "-r"); 1.26 + fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n", 1.27 + "-i input"); 1.28 + fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n", 1.29 + "-o output"); 1.30 + exit(-1); 1.31 +} 1.32 + 1.33 +int main(int argc, char **argv) 1.34 +{ 1.35 + char *progName; 1.36 + FILE *outFile; 1.37 + PRFileDesc *inFile; 1.38 + SECItem der; 1.39 + SECStatus rv; 1.40 + PRInt16 xp_error; 1.41 + PRBool raw = PR_FALSE; 1.42 + PLOptState *optstate; 1.43 + PLOptStatus status; 1.44 + 1.45 + progName = strrchr(argv[0], '/'); 1.46 + progName = progName ? progName+1 : argv[0]; 1.47 + 1.48 + /* Parse command line arguments */ 1.49 + inFile = 0; 1.50 + outFile = 0; 1.51 + optstate = PL_CreateOptState(argc, argv, "i:o:r"); 1.52 + while ((status = PL_GetNextOpt(optstate)) == PL_OPT_OK) { 1.53 + switch (optstate->option) { 1.54 + case 'i': 1.55 + inFile = PR_Open(optstate->value, PR_RDONLY, 0); 1.56 + if (!inFile) { 1.57 + fprintf(stderr, "%s: unable to open \"%s\" for reading\n", 1.58 + progName, optstate->value); 1.59 + return -1; 1.60 + } 1.61 + break; 1.62 + 1.63 + case 'o': 1.64 + outFile = fopen(optstate->value, "w"); 1.65 + if (!outFile) { 1.66 + fprintf(stderr, "%s: unable to open \"%s\" for writing\n", 1.67 + progName, optstate->value); 1.68 + return -1; 1.69 + } 1.70 + break; 1.71 + 1.72 + case 'r': 1.73 + raw = PR_TRUE; 1.74 + break; 1.75 + 1.76 + default: 1.77 + Usage(progName); 1.78 + break; 1.79 + } 1.80 + } 1.81 + if (status == PL_OPT_BAD) 1.82 + Usage(progName); 1.83 + 1.84 + if (!inFile) inFile = PR_STDIN; 1.85 + if (!outFile) outFile = stdout; 1.86 + 1.87 + rv = NSS_NoDB_Init(NULL); /* XXX */ 1.88 + if (rv != SECSuccess) { 1.89 + SECU_PrintPRandOSError(progName); 1.90 + return -1; 1.91 + } 1.92 + 1.93 + rv = SECU_ReadDERFromFile(&der, inFile, PR_FALSE, PR_FALSE); 1.94 + if (rv == SECSuccess) { 1.95 + rv = DER_PrettyPrint(outFile, &der, raw); 1.96 + if (rv == SECSuccess) 1.97 + return 0; 1.98 + } 1.99 + 1.100 + xp_error = PORT_GetError(); 1.101 + if (xp_error) { 1.102 + SECU_PrintError(progName, "error %d", xp_error); 1.103 + } 1.104 + if (errno) { 1.105 + SECU_PrintSystemError(progName, "errno=%d", errno); 1.106 + } 1.107 + return 1; 1.108 +}