KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > ui > cli > Asn1Dump


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13  
14 package org.ejbca.ui.cli;
15
16 import java.io.FileInputStream JavaDoc;
17 import java.security.cert.X509Certificate JavaDoc;
18 import java.util.Collection JavaDoc;
19 import java.util.Iterator JavaDoc;
20
21 import org.bouncycastle.asn1.ASN1InputStream;
22 import org.bouncycastle.asn1.DERObject;
23 import org.bouncycastle.asn1.util.ASN1Dump;
24 import org.ejbca.util.CertTools;
25
26
27 /**
28  * Implements the CA command line interface
29  *
30  * @version $Id: Asn1Dump.java,v 1.1 2006/05/28 14:51:20 anatom Exp $
31  */

32 public class Asn1Dump extends BaseCommand {
33     
34     public Asn1Dump(String JavaDoc[] args) {
35         this.args = args;
36     }
37     
38     /**
39      * Main
40      *
41      * @param args command line arguments
42      */

43     public static void main(String JavaDoc[] args) {
44         try {
45             if (args.length == 1) {
46                 Asn1Dump d = new Asn1Dump(args);
47                 d.execute();
48             } else {
49                 System.out.println(
50                     "Usage: asn1dump filename-of-pem-encoded-certs||filename-of-der-encoded-asn1");
51             }
52         } catch (Exception JavaDoc e) {
53             System.out.println(e.getMessage());
54             //e.printStackTrace();
55
System.exit(-1);
56         }
57     }
58     
59     public void execute() throws ErrorAdminCommandException {
60         try {
61             String JavaDoc filename = args[0];
62             boolean iscert = true;
63             Collection JavaDoc coll = null;
64             CertTools.installBCProvider();
65             try {
66                 coll = CertTools.getCertsFromPEM(filename);
67                 if (coll.isEmpty()) {
68                     iscert = false;
69                 }
70             } catch (Exception JavaDoc e) {
71                 iscert = false;
72             }
73             if (!iscert) {
74                 ASN1InputStream ais = new ASN1InputStream(new FileInputStream JavaDoc(filename));
75                 DERObject obj = ais.readObject();
76                 String JavaDoc dump = ASN1Dump.dumpAsString(obj);
77                 getOutputStream().println(dump);
78             } else {
79                 Iterator JavaDoc iter = coll.iterator();
80                 while (iter.hasNext()) {
81                     X509Certificate JavaDoc cert = (X509Certificate JavaDoc)iter.next();
82                     String JavaDoc dump = ASN1Dump.dumpAsString(cert);
83                     getOutputStream().println(dump);
84                 }
85             }
86         } catch (Exception JavaDoc e) {
87             throw new ErrorAdminCommandException(e);
88         }
89         
90     }
91 }
92
93
94 //ca
95
Popular Tags