1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is the Netscape security libraries.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 1994-2000
19 * the Initial Developer. All Rights Reserved.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 2 or later (the "GPL"), or
25 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26 * in which case the provisions of the GPL or the LGPL are applicable instead
27 * of those above. If you wish to allow use of your version of this file only
28 * under the terms of either the GPL or the LGPL, and not to allow others to
29 * use your version of this file under the terms of the MPL, indicate your
30 * decision by deleting the provisions above and replace them with the notice
31 * and other provisions required by the GPL or the LGPL. If you do not delete
32 * the provisions above, a recipient may use your version of this file under
33 * the terms of any one of the MPL, the GPL or the LGPL.
35 * ***** END LICENSE BLOCK ***** */
38 * Pretty-print some well-known BER or DER encoded data (e.g. certificates,
41 * $Id: pp.c,v 1.9 2007/09/25 03:46:23 nelson%bolyard.com Exp $
46 #if defined(__sun) && !defined(SVR4)
47 extern int fprintf(FILE *, char *, ...);
56 static void Usage(char *progName
)
59 "Usage: %s -t type [-a] [-i input] [-o output]\n",
61 fprintf(stderr
, "%-20s Specify the input type (must be one of %s,\n",
62 "-t type", SEC_CT_PRIVATE_KEY
);
63 fprintf(stderr
, "%-20s %s, %s, %s,\n", "", SEC_CT_PUBLIC_KEY
,
64 SEC_CT_CERTIFICATE
, SEC_CT_CERTIFICATE_REQUEST
);
65 fprintf(stderr
, "%-20s %s or %s)\n", "", SEC_CT_PKCS7
, SEC_CT_CRL
);
66 fprintf(stderr
, "%-20s Input is in ascii encoded form (RFC1113)\n",
68 fprintf(stderr
, "%-20s Define an input file to use (default is stdin)\n",
70 fprintf(stderr
, "%-20s Define an output file to use (default is stdout)\n",
75 int main(int argc
, char **argv
)
85 progName
= strrchr(argv
[0], '/');
86 progName
= progName
? progName
+1 : argv
[0];
92 optstate
= PL_CreateOptState(argc
, argv
, "at:i:o:");
93 while ( PL_GetNextOpt(optstate
) == PL_OPT_OK
) {
94 switch (optstate
->option
) {
104 inFile
= PR_Open(optstate
->value
, PR_RDONLY
, 0);
106 fprintf(stderr
, "%s: unable to open \"%s\" for reading\n",
107 progName
, optstate
->value
);
113 outFile
= fopen(optstate
->value
, "w");
115 fprintf(stderr
, "%s: unable to open \"%s\" for writing\n",
116 progName
, optstate
->value
);
122 typeTag
= strdup(optstate
->value
);
126 PL_DestroyOptState(optstate
);
127 if (!typeTag
) Usage(progName
);
129 if (!inFile
) inFile
= PR_STDIN
;
130 if (!outFile
) outFile
= stdout
;
132 PR_Init(PR_SYSTEM_THREAD
, PR_PRIORITY_NORMAL
, 1);
133 rv
= NSS_NoDB_Init(NULL
);
134 if (rv
!= SECSuccess
) {
135 fprintf(stderr
, "%s: NSS_NoDB_Init failed (%s)\n",
136 progName
, SECU_Strerror(PORT_GetError()));
139 SECU_RegisterDynamicOids();
141 rv
= SECU_ReadDERFromFile(&der
, inFile
, ascii
);
142 if (rv
!= SECSuccess
) {
143 fprintf(stderr
, "%s: SECU_ReadDERFromFile failed\n", progName
);
147 /* Data is untyped, using the specified type */
148 data
.data
= der
.data
;
151 /* Pretty print it */
152 if (PORT_Strcmp(typeTag
, SEC_CT_CERTIFICATE
) == 0) {
153 rv
= SECU_PrintSignedData(outFile
, &data
, "Certificate", 0,
154 SECU_PrintCertificate
);
155 } else if (PORT_Strcmp(typeTag
, SEC_CT_CERTIFICATE_REQUEST
) == 0) {
156 rv
= SECU_PrintSignedData(outFile
, &data
, "Certificate Request", 0,
157 SECU_PrintCertificateRequest
);
158 } else if (PORT_Strcmp (typeTag
, SEC_CT_CRL
) == 0) {
159 rv
= SECU_PrintSignedData (outFile
, &data
, "CRL", 0, SECU_PrintCrl
);
160 #ifdef HAVE_EPV_TEMPLATE
161 } else if (PORT_Strcmp(typeTag
, SEC_CT_PRIVATE_KEY
) == 0) {
162 rv
= SECU_PrintPrivateKey(outFile
, &data
, "Private Key", 0);
164 } else if (PORT_Strcmp(typeTag
, SEC_CT_PUBLIC_KEY
) == 0) {
165 rv
= SECU_PrintSubjectPublicKeyInfo(outFile
, &data
, "Public Key", 0);
166 } else if (PORT_Strcmp(typeTag
, SEC_CT_PKCS7
) == 0) {
167 rv
= SECU_PrintPKCS7ContentInfo(outFile
, &data
,
168 "PKCS #7 Content Info", 0);
170 fprintf(stderr
, "%s: don't know how to print out '%s' files\n",
172 SECU_PrintAny(outFile
, &data
, "File contains", 0);
176 if (inFile
!= PR_STDIN
)
180 fprintf(stderr
, "%s: problem converting data (%s)\n",
181 progName
, SECU_Strerror(PORT_GetError()));
183 if (NSS_Shutdown() != SECSuccess
) {
184 fprintf(stderr
, "%s: NSS_Shutdown failed (%s)\n",
185 progName
, SECU_Strerror(PORT_GetError()));