2
* The contents of this file are subject to the Mozilla Public
3
* License Version 1.1 (the "License"); you may not use this file
4
* except in compliance with the License. You may obtain a copy of
5
* the License at http://www.mozilla.org/MPL/
7
* Software distributed under the License is distributed on an "AS
8
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
9
* implied. See the License for the specific language governing
10
* rights and limitations under the License.
12
* The Original Code is the Netscape security libraries.
14
* The Initial Developer of the Original Code is Netscape
15
* Communications Corporation. Portions created by Netscape are
16
* Copyright (C) 1994-2000 Netscape Communications Corporation. All
21
* Alternatively, the contents of this file may be used under the
22
* terms of the GNU General Public License Version 2 or later (the
23
* "GPL"), in which case the provisions of the GPL are applicable
24
* instead of those above. If you wish to allow use of your
25
* version of this file only under the terms of the GPL and not to
26
* allow others to use your version of this file under the MPL,
27
* indicate your decision by deleting the provisions above and
28
* replace them with the notice and other provisions required by
29
* the GPL. If you do not delete the provisions above, a recipient
30
* may use your version of this file under either the MPL or the
35
* Pretty-print some well-known BER or DER encoded data (e.g. certificates,
38
* $Id: pp.c,v 1.4 2003/11/04 01:51:54 nelsonb%netscape.com Exp $
43
#if defined(__sun) && !defined(SVR4)
44
extern int fprintf(FILE *, char *, ...);
53
static void Usage(char *progName)
56
"Usage: %s -t type [-a] [-i input] [-o output]\n",
58
fprintf(stderr, "%-20s Specify the input type (must be one of %s,\n",
59
"-t type", SEC_CT_PRIVATE_KEY);
60
fprintf(stderr, "%-20s %s, %s, %s,\n", "", SEC_CT_PUBLIC_KEY,
61
SEC_CT_CERTIFICATE, SEC_CT_CERTIFICATE_REQUEST);
62
fprintf(stderr, "%-20s %s or %s)\n", "", SEC_CT_PKCS7, SEC_CT_CRL);
63
fprintf(stderr, "%-20s Input is in ascii encoded form (RFC1113)\n",
65
fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
67
fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
72
int main(int argc, char **argv)
82
progName = strrchr(argv[0], '/');
83
progName = progName ? progName+1 : argv[0];
89
optstate = PL_CreateOptState(argc, argv, "at:i:o:");
90
while ( PL_GetNextOpt(optstate) == PL_OPT_OK ) {
91
switch (optstate->option) {
101
inFile = PR_Open(optstate->value, PR_RDONLY, 0);
103
fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
104
progName, optstate->value);
110
outFile = fopen(optstate->value, "w");
112
fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
113
progName, optstate->value);
119
typeTag = strdup(optstate->value);
123
PL_DestroyOptState(optstate);
124
if (!typeTag) Usage(progName);
126
if (!inFile) inFile = PR_STDIN;
127
if (!outFile) outFile = stdout;
129
PR_Init(PR_SYSTEM_THREAD, PR_PRIORITY_NORMAL, 1);
130
rv = NSS_NoDB_Init(NULL);
131
if (rv != SECSuccess) {
132
fprintf(stderr, "%s: NSS_NoDB_Init failed\n", progName);
136
rv = SECU_ReadDERFromFile(&der, inFile, ascii);
137
if (rv != SECSuccess) {
138
fprintf(stderr, "%s: SECU_ReadDERFromFile failed\n", progName);
142
/* Data is untyped, using the specified type */
143
data.data = der.data;
146
/* Pretty print it */
147
if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE) == 0) {
148
rv = SECU_PrintSignedData(outFile, &data, "Certificate", 0,
149
SECU_PrintCertificate);
150
} else if (PORT_Strcmp(typeTag, SEC_CT_CERTIFICATE_REQUEST) == 0) {
151
rv = SECU_PrintSignedData(outFile, &data, "Certificate Request", 0,
152
SECU_PrintCertificateRequest);
153
} else if (PORT_Strcmp (typeTag, SEC_CT_CRL) == 0) {
154
rv = SECU_PrintSignedData (outFile, &data, "CRL", 0, SECU_PrintCrl);
155
#ifdef HAVE_EPV_TEMPLATE
156
} else if (PORT_Strcmp(typeTag, SEC_CT_PRIVATE_KEY) == 0) {
157
rv = SECU_PrintPrivateKey(outFile, &data, "Private Key", 0);
159
} else if (PORT_Strcmp(typeTag, SEC_CT_PUBLIC_KEY) == 0) {
160
rv = SECU_PrintPublicKey(outFile, &data, "Public Key", 0);
161
} else if (PORT_Strcmp(typeTag, SEC_CT_PKCS7) == 0) {
162
rv = SECU_PrintPKCS7ContentInfo(outFile, &data,
163
"PKCS #7 Content Info", 0);
165
fprintf(stderr, "%s: don't know how to print out '%s' files\n",
170
if (inFile != PR_STDIN)
174
fprintf(stderr, "%s: problem converting data (%s)\n",
175
progName, SECU_Strerror(PORT_GetError()));
177
if (NSS_Shutdown() != SECSuccess) {
178
fprintf(stderr, "%s: NSS_Shutdown failed (%s)\n",
179
progName, SECU_Strerror(PORT_GetError()));