nss: import at 3.0.1 beta 1
[mozilla-nss.git] / security / nss / cmd / pp / pp.c
blob1d3b2c0819cf463f56d5e1dcc7838e69a677587e
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
12 * License.
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.
21 * Contributor(s):
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,
39 * keys, pkcs7)
41 * $Id: pp.c,v 1.9 2007/09/25 03:46:23 nelson%bolyard.com Exp $
44 #include "secutil.h"
46 #if defined(__sun) && !defined(SVR4)
47 extern int fprintf(FILE *, char *, ...);
48 #endif
50 #include "plgetopt.h"
52 #include "pk11func.h"
53 #include "nspr.h"
54 #include "nss.h"
56 static void Usage(char *progName)
58 fprintf(stderr,
59 "Usage: %s -t type [-a] [-i input] [-o output]\n",
60 progName);
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",
67 "-a");
68 fprintf(stderr, "%-20s Define an input file to use (default is stdin)\n",
69 "-i input");
70 fprintf(stderr, "%-20s Define an output file to use (default is stdout)\n",
71 "-o output");
72 exit(-1);
75 int main(int argc, char **argv)
77 int rv, ascii;
78 char *progName;
79 FILE *outFile;
80 PRFileDesc *inFile;
81 SECItem der, data;
82 char *typeTag;
83 PLOptState *optstate;
85 progName = strrchr(argv[0], '/');
86 progName = progName ? progName+1 : argv[0];
88 ascii = 0;
89 inFile = 0;
90 outFile = 0;
91 typeTag = 0;
92 optstate = PL_CreateOptState(argc, argv, "at:i:o:");
93 while ( PL_GetNextOpt(optstate) == PL_OPT_OK ) {
94 switch (optstate->option) {
95 case '?':
96 Usage(progName);
97 break;
99 case 'a':
100 ascii = 1;
101 break;
103 case 'i':
104 inFile = PR_Open(optstate->value, PR_RDONLY, 0);
105 if (!inFile) {
106 fprintf(stderr, "%s: unable to open \"%s\" for reading\n",
107 progName, optstate->value);
108 return -1;
110 break;
112 case 'o':
113 outFile = fopen(optstate->value, "w");
114 if (!outFile) {
115 fprintf(stderr, "%s: unable to open \"%s\" for writing\n",
116 progName, optstate->value);
117 return -1;
119 break;
121 case 't':
122 typeTag = strdup(optstate->value);
123 break;
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()));
137 exit(1);
139 SECU_RegisterDynamicOids();
141 rv = SECU_ReadDERFromFile(&der, inFile, ascii);
142 if (rv != SECSuccess) {
143 fprintf(stderr, "%s: SECU_ReadDERFromFile failed\n", progName);
144 exit(1);
147 /* Data is untyped, using the specified type */
148 data.data = der.data;
149 data.len = der.len;
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);
163 #endif
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);
169 } else {
170 fprintf(stderr, "%s: don't know how to print out '%s' files\n",
171 progName, typeTag);
172 SECU_PrintAny(outFile, &data, "File contains", 0);
173 return -1;
176 if (inFile != PR_STDIN)
177 PR_Close(inFile);
178 PORT_Free(der.data);
179 if (rv) {
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()));
186 rv = SECFailure;
188 PR_Cleanup();
189 return rv;