summaryrefslogtreecommitdiffstats
path: root/pki/base/migrate/63ToTxt/src/Main.java
blob: 1a13f833be44ec43ece6b99c3a382763750b54f9 (plain)
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// --- 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 ---
//
//  "63ToTxt/src/Main.java" is based upon a copy "62ToTxt/src/Main.java".
//
//  Always comment any new code sections with a "CMS 6.3" header, and
//  apply these changes forward to all other "*ToTxt/src/Main.java" files
//  (including this comment header) so that these differences will only
//  appear when this file is diffed against an earlier "*ToTxt" version.
//
//  This file should always be maintained by executing the following command:
//
//      diff 62ToTxt/src/Main.java 63ToTxt/src/Main.java
//

import java.io.*;
import java.math.*;
import java.util.*;
import sun.misc.*;
import org.mozilla.jss.*;               // CMS 4.5 and later
import org.mozilla.jss.crypto.*;        // CMS 4.5 and later
import netscape.security.util.*;

public class Main
{
	public static void main(String args[]) 
	{
		try {
			// initialize CryptoManager in CMS 4.5 and later
			CryptoManager.initialize(".");
			// load JSS provider in CMS 4.5 and later
			java.security.Security.removeProvider("SUN version 1.2");
			// The following call to "java.security.Security.insertProviderAt()"
			// is no longer commented out in CMS 4.5 and later
			java.security.Security.insertProviderAt(
				new netscape.security.provider.CMS(), 0); 
			java.security.Provider ps[] = 
				java.security.Security.getProviders();
			if (ps == null || ps.length <= 0) { 
				System.err.println("Java Security Provider NONE"); 
			} else { 
				for (int x = 0; x < ps.length; x++) { 
					System.err.println("Java Security Provider " + x + " class=" + ps[x]); 
				} 
			}

			// Parse the File
			CMS63LdifParser parser = null;
			if (args.length == 1) {
			  parser = new CMS63LdifParser(args[0]);
			} else if (args.length == 2) {
			  parser = new CMS63LdifParser(args[0], args[1]);
			} else {
			  throw new IOException("Invalid Parameters");
			}
			parser.parse();
		} catch (Exception e) {
			System.err.println("ERROR: " + e.toString());
			e.printStackTrace();
		}
	}
}

class CMS63LdifParser
{
	// constants
	private static final String DN = 
		"dn:";
	// Directory Servers in CMS 4.7 and later use "requestAttributes"
	private static final String REQUEST_ATTRIBUTES = 
		"requestAttributes::";
	private static final String BEGIN = 
		"--- BEGIN ATTRIBUTES ---";
	private static final String END = 
		"--- END ATTRIBUTES ---";

	// variables
	private String mFilename = null;
	private String mErrorFilename = null;
	private PrintWriter mErrorPrintWriter = null;

	public CMS63LdifParser(String filename)
	{
		mFilename = filename;
	}

	public CMS63LdifParser(String filename, String errorFilename)
	{
		mFilename = filename;
		mErrorFilename = errorFilename;
	}

	public void parse() throws Exception
	{ 
		if (mErrorFilename != null) {
		    mErrorPrintWriter = new PrintWriter(new FileOutputStream(mErrorFilename));
		}
		BufferedReader reader = new BufferedReader(
			new FileReader(mFilename)); 
		String line = null; 
		String dn = null; 
		StringBuffer requestAttributes = null;
		while ((line = reader.readLine()) != null) { 
			if (line.startsWith(DN)) {
				dn = line;
			}
			if (line.startsWith(REQUEST_ATTRIBUTES)) {
				requestAttributes = new StringBuffer();
				// System.out.println(line);
				requestAttributes.append(
					line.substring(REQUEST_ATTRIBUTES.length(), 
					line.length()).trim());
				continue;
			}
			if (requestAttributes == null) {
				System.out.println(line);
				continue;
			}
			if (line.startsWith(" ")) {
				// System.out.println(line);
				requestAttributes.append(line.trim());
			} else {
				parseAttributes(dn, requestAttributes);
				requestAttributes = null;
				System.out.println(line);
			}
		} 
	}

	public void parseAttributes(String dn, StringBuffer attrs) throws Exception
	{
		BASE64Decoder decoder = new BASE64Decoder();
		decodeHashtable(dn, decoder.decodeBuffer(attrs.toString()));
		
//		System.out.println(attrs);
	}

	public Object decode(byte[] data) throws 
		ObjectStreamException, 
		IOException, 
		ClassNotFoundException 
	{ 
		ByteArrayInputStream bis = new ByteArrayInputStream(data); 
		ObjectInputStream is = new ObjectInputStream(bis); 
		return is.readObject(); 
	}

	public void decodeHashtable(String dn, byte[] data) throws Exception
	{
		ByteArrayInputStream bis = new ByteArrayInputStream(data); 
		ObjectInputStream is = new ObjectInputStream(bis); 

		System.out.println(BEGIN);
		String key = null; 
		while (true) 
		{ 
			key = (String)is.readObject(); 
			// end of table is marked with null 
			if (key == null) break; 
			try {
			  byte[] bytes = (byte[])is.readObject(); 
			  Object obj = decode(bytes);
			  output(key, obj);
			} catch (Exception e) {
			  if (mErrorPrintWriter != null) {
			      if (dn != null) {
			          mErrorPrintWriter.println(dn);
			      }
			      mErrorPrintWriter.println("Skipped " + key);
			  }
			}
		} 
		System.out.println(END);
	}

	public void output(String key, Object obj) throws Exception
	{
		if (obj instanceof String) {
			System.out.println(" " +
				key + ":" + obj.getClass().getName() + "=" + 
				obj);
		} else if (obj instanceof netscape.security.x509.CertificateX509Key) {
			netscape.security.x509.CertificateX509Key o =
				(netscape.security.x509.CertificateX509Key)obj;
				ByteArrayOutputStream bos = 
					new ByteArrayOutputStream();
				o.encode(bos);
				BASE64Encoder encoder = new BASE64Encoder();
				System.out.println(" " +
				key + ":" + o.getClass().getName() + "=" + 
				encoder.encode(bos.toByteArray()));
		} else if (obj instanceof netscape.security.x509.CertificateSubjectName) {
			netscape.security.x509.CertificateSubjectName o =
				(netscape.security.x509.CertificateSubjectName)obj;
				ByteArrayOutputStream bos = 
					new ByteArrayOutputStream();
				o.encode(bos);
				BASE64Encoder encoder = new BASE64Encoder();
				System.out.println(" " +
				key + ":" + o.getClass().getName() + "=" +
				encoder.encode(bos.toByteArray()));
		} else if (obj instanceof netscape.security.x509.CertificateExtensions) {
			netscape.security.x509.CertificateExtensions o =
				(netscape.security.x509.CertificateExtensions)obj;
				ByteArrayOutputStream bos = 
					new ByteArrayOutputStream();
				o.encode(bos);
				BASE64Encoder encoder = new BASE64Encoder();
				System.out.println(" " +
				key + ":" + o.getClass().getName() + "=" +
				encoder.encode(bos.toByteArray()));
		} else if (obj instanceof netscape.security.x509.X509CertInfo) {
			netscape.security.x509.X509CertInfo o =
				(netscape.security.x509.X509CertInfo)obj;
				ByteArrayOutputStream bos = 
					new ByteArrayOutputStream();
				o.encode(bos);
				BASE64Encoder encoder = new BASE64Encoder();
				System.out.println(" " +
				key + ":" + o.getClass().getName() + "=" +
				encoder.encode(bos.toByteArray()));
		} else if (obj instanceof netscape.security.x509.X509CertImpl) {
			netscape.security.x509.X509CertImpl o =
				(netscape.security.x509.X509CertImpl)obj;
				ByteArrayOutputStream bos = 
					new ByteArrayOutputStream();
				o.encode(bos);
				BASE64Encoder encoder = new BASE64Encoder();
				System.out.println(" " +
				key + ":" + o.getClass().getName() + "=" + 
				encoder.encode(bos.toByteArray()));
		} else if (obj instanceof netscape.security.x509.CertificateChain) {
			netscape.security.x509.CertificateChain o =
				(netscape.security.x509.CertificateChain)obj;
				ByteArrayOutputStream bos = 
					new ByteArrayOutputStream();
				o.encode(bos);
				BASE64Encoder encoder = new BASE64Encoder();
				System.out.println(" " +
				key + ":" + o.getClass().getName() + "=" + 
				encoder.encode(bos.toByteArray()));
		} else if (obj instanceof netscape.security.x509.X509CertImpl[]) {
			netscape.security.x509.X509CertImpl o[] =
				(netscape.security.x509.X509CertImpl[])obj;
			for (int i = 0; i < o.length; i++) {
				ByteArrayOutputStream bos = 
					new ByteArrayOutputStream();
				o[i].encode(bos);
				BASE64Encoder encoder = new BASE64Encoder();
				System.out.println(" " +
				key + ":" + o[i].getClass().getName() +"["+o.length+","+i+"]" + "=" + 
				encoder.encode(bos.toByteArray()));
			}
		} else if (obj instanceof netscape.security.x509.X509CertInfo[]) {
			netscape.security.x509.X509CertInfo o[] =
				(netscape.security.x509.X509CertInfo[])obj;
			for (int i = 0; i < o.length; i++) {
				ByteArrayOutputStream bos = 
					new ByteArrayOutputStream();
				o[i].encode(bos);
				BASE64Encoder encoder = new BASE64Encoder();
				System.out.println(" " +
				key + ":" + o[i].getClass().getName() + "["+o.length + "," + i+"]"+"=" + 
				encoder.encodeBuffer(bos.toByteArray()));
			}
		} else if (obj instanceof netscape.security.x509.RevokedCertImpl[]) {
			netscape.security.x509.RevokedCertImpl o[] =
				(netscape.security.x509.RevokedCertImpl[])obj;
			for (int i = 0; i < o.length; i++) {
				DerOutputStream bos = 
					new DerOutputStream();
				o[i].encode(bos);
				BASE64Encoder encoder = new BASE64Encoder();
				System.out.println(" " +
				key + ":" + o[i].getClass().getName() +"["+o.length+","+i+"]" + "=" + 
				encoder.encode(bos.toByteArray()));
			}
		} else if (obj instanceof java.security.cert.Certificate[]) {
			java.security.cert.Certificate o[] =
				(java.security.cert.Certificate[])obj;
			for (int i = 0; i < o.length; i++) {
				ByteArrayOutputStream bos = 
					new ByteArrayOutputStream();
				BASE64Encoder encoder = new BASE64Encoder();
				System.out.println(" " +
				key + ":" + o[i].getClass().getName() +"["+o.length+","+i+"]" + "=" + 
				encoder.encode(o[i].getEncoded()));
			}
		} else if (obj instanceof com.netscape.cmscore.base.ArgBlock) {
			// CMS 6.1:  created new "com.netscape.certsrv.base.IArgBlock" and
			//           moved old "com.netscape.certsrv.base.ArgBlock"
			//           to "com.netscape.cmscore.base.ArgBlock"
			com.netscape.cmscore.base.ArgBlock o =
			(com.netscape.cmscore.base.ArgBlock)obj;
			Enumeration e = o.elements();
			while (e.hasMoreElements()) {
				String k = (String)e.nextElement();
				System.out.println(" " +
				key + ":" + o.getClass().getName() + "=" +
				k + "=" +(String)o.get(k));
			}
		} else if (obj instanceof com.netscape.cmscore.dbs.KeyRecord) {
			// CMS 6.0:  moved "com.netscape.certsrv.dbs.keydb.KeyRecord"
			//           to "com.netscape.cmscore.dbs.KeyRecord"
			com.netscape.cmscore.dbs.KeyRecord o =
			(com.netscape.cmscore.dbs.KeyRecord)obj;
			Enumeration e = o.getElements();
			while (e.hasMoreElements()) {
				String k = (String)e.nextElement();
				Object ob = o.get(k);
				if (ob != null) {
				if (ob instanceof java.util.Date) {
					System.out.println(" " +
					key + ":" + o.getClass().getName() + "=" +
					k + ":" + ob.getClass().getName() + "=" + ((java.util.Date)ob).getTime());
				} else if (ob instanceof byte[]) {
					BASE64Encoder encoder = new BASE64Encoder();
					System.out.println(" " +
					key + ":" + o.getClass().getName() + "=" +
					k + ":" + ob.getClass().getName() + "=" + encoder.encode((byte[])ob));

				} else {
					System.out.println(" " +
					key + ":" + o.getClass().getName() + "=" +
					k + ":" + ob.getClass().getName() + "=" + ob);
				}
				}
			}
		} else if (obj instanceof com.netscape.cmscore.kra.ProofOfArchival) {
			// CMS 6.0:  moved "com.netscape.certsrv.kra.ProofOfArchival"
			//           to "com.netscape.cmscore.kra.ProofOfArchival"
			com.netscape.cmscore.kra.ProofOfArchival o =
				(com.netscape.cmscore.kra.ProofOfArchival)obj;
				DerOutputStream bos = 
					new DerOutputStream();
				o.encode(bos);
				BASE64Encoder encoder = new BASE64Encoder();
				System.out.println(" " +
				key + ":" + o.getClass().getName() + "=" + 
				encoder.encode(bos.toByteArray()));
		} else if (obj instanceof com.netscape.certsrv.request.AgentApprovals) {
			com.netscape.certsrv.request.AgentApprovals o =
			(com.netscape.certsrv.request.AgentApprovals)obj;
			Enumeration e = o.elements();
			while (e.hasMoreElements()) {
				com.netscape.certsrv.request.AgentApproval approval = (com.netscape.certsrv.request.AgentApproval)e.nextElement();
				System.out.println(" " +
				key + ":" + o.getClass().getName() + "=" +
				approval.getUserName() + ";" + approval.getDate().getTime());
			}
		} else if (obj instanceof com.netscape.certsrv.authentication.AuthToken) {
			com.netscape.certsrv.authentication.AuthToken o =
			(com.netscape.certsrv.authentication.AuthToken)obj;
			Enumeration e = o.getElements();
			while (e.hasMoreElements()) {
				String k = (String)e.nextElement();
				Object ob = o.get(k);
				if (ob instanceof java.util.Date) {
					System.out.println(" " +
					key + ":" + o.getClass().getName() + "=" +
					k + ":" + ob.getClass().getName() + "=" + ((java.util.Date)ob).getTime());
                } else if (ob instanceof java.math.BigInteger[]) {
                    // Bugzilla Bug #225031 (a.k.a. - Raidzilla Bug #58356)
                    java.math.BigInteger in[] = (java.math.BigInteger[])ob;
                    String numbers = "";
                    for (int i = 0; i < in.length; i++) {
                      if (numbers.equals("")) {
                        numbers = in[i].toString();
                      } else {
                        numbers = numbers + "," + in[i].toString();
                      }
                    }
                    System.out.println(" " +
                    key + ":" + "com.netscape.certsrv.authentication.AuthToken" + "=" +
                    k + ":java.lang.String=" + numbers);
               } else if (ob instanceof String[]) {
                    // Bugzilla Bug #224763 (a.k.a. - Raidzilla Bug #57949)
                    // Bugzilla Bug #252240
                    String str[] = (String[])ob;
                    String v = "";
                    if (str != null) {
                      for (int i = 0; i < str.length; i++) {
                        if (i != 0) {
                          v += ",";
                        }
                        v += str[i];
                      }
                    }
                    System.out.println(" " +
                    key + ":" + o.getClass().getName() + "=" +
                    k + ":" + "java.lang.String" + "=" + v);
				} else {
					System.out.println(" " +
					key + ":" + o.getClass().getName() + "=" +
					k + ":" + ob.getClass().getName() + "=" + ob);
				}
			}
		} else if (obj instanceof byte[]) {
			BASE64Encoder encoder = new BASE64Encoder();
			System.out.println(" " + key + ":byte[]="+
			encoder.encode((byte[])obj));
		} else if (obj instanceof Integer[]) {
			Integer in[] = (Integer[])obj;
			for (int i = 0; i < in.length; i++) {
				System.out.println(" " + key + ":Integer[" + in.length + "," + i + "]="+ in[i]);
			}
        } else if (obj instanceof BigInteger[]) {
            // Bugzilla Bug #238779
            BigInteger in[] = (BigInteger[])obj;
            for (int i = 0; i < in.length; i++) {
                System.out.println(" " + key + ":java.math.BigInteger[" + in.length + "," + i + "]="+ in[i]);
            }
        } else if (obj instanceof String[]) {
            // Bugzilla Bug #223360 (a.k.a - Raidzilla Bug #58086)
            String str[] = (String[])obj;
            for (int i = 0; i < str.length; i++) {
                System.out.println(" " + key + ":java.lang.String[" + str.length + "," + i + "]="+ str[i]);
            }
		} else if (obj instanceof netscape.security.x509.CertificateAlgorithmId) {
			netscape.security.x509.CertificateAlgorithmId o =
			(netscape.security.x509.CertificateAlgorithmId)obj;
			ByteArrayOutputStream bos = 
				new ByteArrayOutputStream();
			o.encode(bos);
			BASE64Encoder encoder = new BASE64Encoder();
			System.out.println(" " + key + 
			":netscape.security.x509.CertificateAlgorithmId="+ 
			encoder.encode(bos.toByteArray()));
		} else if (obj instanceof netscape.security.x509.CertificateValidity) {
			netscape.security.x509.CertificateValidity o =
			(netscape.security.x509.CertificateValidity)obj;
			ByteArrayOutputStream bos = 
				new ByteArrayOutputStream();
			o.encode(bos);
			BASE64Encoder encoder = new BASE64Encoder();
			System.out.println(" " + key + 
			":netscape.security.x509.CertificateValidity="+ 
			encoder.encode(bos.toByteArray()));
        } else if (obj instanceof byte[]) {
            // Since 6.1's profile framework,
            // req_archive_options is a byte array
            BASE64Encoder encoder = new BASE64Encoder();
            System.out.println(" " + key +
            ":byte[]="+
            encoder.encode((byte[])obj));
        } else if (obj instanceof java.util.Hashtable) {
            // Bugzilla Bug #224800 (a.k.a - Raidzilla Bug #56953)
            //
            // Example:  fingerprints:java.util.Hashtable=
            //           {SHA1=[B@52513a, MD5=[B@52c4d9, MD2=[B@799ff5}
            //
            java.util.Hashtable o = (java.util.Hashtable)obj;
            BASE64Encoder encoder = new BASE64Encoder();
            Enumeration e = o.elements();
            while (e.hasMoreElements()) {
                String k = (String)e.nextElement();
                System.out.println(" " +
                key + ":" + o.getClass().getName() + "=" +
                k + "=" + encoder.encode((byte[])o.get(k)));
            }
		} else {
			System.out.println(" " +
				key + ":" + obj.getClass().getName() + "=" + 
				obj);
		}
	}
}