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