1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
|
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmstools;
import java.io.*;
import java.util.*;
import java.security.*;
import java.security.cert.*;
import netscape.security.x509.*;
import netscape.security.util.*;
/**
* The PrettyPrintCert class is a utility program designed to "pretty print"
* a certificate. It assumes that the name of a data file is passed to the
* program via the command line, and that the contents contain a certificate
* encoded in an ASCII BASE 64 format. Note that the data file may contain
* an optional "-----BEGIN" header and/or an optional "-----END" trailer.
*
* <P>
* The program may be invoked as follows:
* <PRE>
*
* PrettyPrintCert <input filename> [output filename]
*
* NOTE: <input filename> must contain an ASCII
* BASE 64 encoded certificate
*
* [output filename] contains a certificate displayed
* in a "pretty print" ASCII format
* </PRE>
*
* @version $Revision$, $Date$
*/
public class PrettyPrintCert {
// Define constants
public static final int ARGC = 2;
public static final String HEADER = "-----BEGIN";
public static final String TRAILER = "-----END";
public static void usageAndExit() {
System.out.println("Usage: PrettyPrintCert " +
"[options] " +
"<input filename> " +
"[output filename]");
System.out.println("\n options: ");
System.out.println(" -simpleinfo : prints limited cert info in easy to parse format");
System.exit(0);
}
public static void main(String argv[]) {
BufferedReader inputCert = null;
String encodedBASE64CertChunk = new String();
String encodedBASE64Cert = new String();
byte decodedBASE64Cert[] = null;
X509CertImpl cert = null;
Locale aLocale = null;
CertPrettyPrint certDetails = null;
String pp = new String();
FileOutputStream outputCert = null;
boolean mSimpleInfo = false;
String inputfile = null;
String outputfile = null;
// parse arguments
for (int i = 0; i < argv.length; i++) {
// deal with empty arguments passed in by script
if (argv[i].equals("")) {
continue;
}
// parse options
if (argv[i].charAt(0) == '-') {
if (argv[i].equals("-simpleinfo")) {
mSimpleInfo = true;
continue;
} else {
System.out.println("Illegal option: " + argv[i]);
usageAndExit();
}
}
// deal with filename
if (inputfile == null) {
inputfile = argv[i];
continue;
}
if (outputfile == null) {
outputfile = argv[i];
continue;
}
System.out.println("Error - Too many arguments");
System.exit(0);
}
if (inputfile == null) {
usageAndExit();
}
// (2) Create a DataInputStream() object to the BASE 64
// encoded certificate contained within the file
// specified on the command line
try {
inputCert = new BufferedReader(new InputStreamReader(
new BufferedInputStream(
new FileInputStream(
inputfile))));
} catch (FileNotFoundException e) {
System.out.println("PrettyPrintCert: can't find file " +
inputfile + ":\n" + e);
return;
}
// (3) Read the entire contents of the specified BASE 64 encoded
// certificate into a String() object throwing away any
// headers beginning with HEADER and any trailers beginning
// with TRAILER
try {
while ((encodedBASE64CertChunk = inputCert.readLine()) != null) {
if (!(encodedBASE64CertChunk.startsWith(HEADER)) &&
!(encodedBASE64CertChunk.startsWith(TRAILER))) {
encodedBASE64Cert += encodedBASE64CertChunk.trim();
}
}
} catch (IOException e) {
System.out.println("PrettyPrintCert: Unexpected BASE64 " +
"encoded error encountered in readLine():\n" +
e);
}
// (4) Close the DataInputStream() object
try {
inputCert.close();
} catch (IOException e) {
System.out.println("PrettyPrintCert: Unexpected BASE64 " +
"encoded error encountered in close():\n" + e);
}
// (5) Decode the ASCII BASE 64 certificate enclosed in the
// String() object into a BINARY BASE 64 byte[] object
decodedBASE64Cert = com.netscape.osutil.OSUtil.AtoB(encodedBASE64Cert);
// (6) Create an X509CertImpl() object from the BINARY BASE 64
// byte[] object
try {
cert = new X509CertImpl(decodedBASE64Cert);
} catch (CertificateException e) {
System.out.println("PrettyPrintCert: Error encountered " +
"on parsing certificate :\n" + e);
}
if (mSimpleInfo) {
try {
X509CertInfo certinfo = (X509CertInfo) cert.get("x509.INFO");
CertificateSubjectName csn = (CertificateSubjectName)
certinfo.get(X509CertInfo.SUBJECT);
Enumeration en = csn.getElements();
X500Name dname = (X500Name) csn.get(CertificateSubjectName.DN_NAME);
pp = "";
RDN[] rdns = dname.getNames();
for (int i = rdns.length - 1; i >= 0; i--) {
pp = pp + rdns[i] + "\n";
}
} catch (Exception e) {
System.out.println("ERROR");
e.printStackTrace();
}
} else {
// (7) For this utility, always specify the default Locale
aLocale = Locale.getDefault();
// (8) Create a CertPrettyPrint() object
certDetails = new CertPrettyPrint(cert);
// (9) Convert the CertPrettyPrint() object into a String() object
pp = certDetails.toString(aLocale);
}
// (10) Finally, "pretty print" the actual certificate to the console
// unless an output file has been specified
if (outputfile == null) {
System.out.println(pp);
} else {
try {
outputCert = new FileOutputStream(outputfile);
} catch (IOException e) {
System.out.println("PrettyPrintCert: unable to open file " +
argv[1] + " for writing:\n" + e);
return;
}
try {
outputCert.write(pp.getBytes());
outputCert.close();
} catch (IOException e) {
System.out.println("PrettyPrintCert: Unexpected error " +
"encountered while attempting to close() " +
outputfile + ":\n" + e);
}
}
}
}
|