1 /*
2  * @(#) $Id: ShowCert.java,v 1.2 2003/07/08 08:13:52 pankaj Exp $
3  *
4  * Copyright (c) 2002-03 by Pankaj Kumar (http://www.pankaj-k.net). 
5  * All rights reserved.
6  *
7  * The license governing the use of this file can be found in the 
8  * root directory of the containing software.
9  */
10import java.util.Iterator;
11import java.util.List;
12import java.security.cert.Certificate;
13import java.security.cert.X509Certificate;
14import java.security.cert.CertificateFactory;
15import java.security.cert.CertificateParsingException;
16import java.io.FileInputStream;
17
18public class ShowCert {
19    private static void display(String str){
20        System.out.println(str);
21    }
22    public static void printX509Cert(X509Certificate cert, String indent){
23        display(indent + "Certificate:");
24        display(indent + "  Data:");
25        display(indent + "    Version: " + cert.getVersion());
26        display(indent + "    Serial Number: " + cert.getSerialNumber());
27        display(indent + "    Signature Algorithm: " + cert.getSigAlgName());
28        display(indent + "    Issuer: " + cert.getIssuerX500Principal());
29        display(indent + "    Validity:");
30        display(indent + "      Not Before: " + cert.getNotBefore());
31        display(indent + "      Not After: " + cert.getNotAfter());
32        display(indent + "    Subject: " + cert.getSubjectX500Principal());
33        display(indent + "    Extensions: ");
34
35        display(indent + "      X509v3 Basic Constraints:");
36        int pathLen = cert.getBasicConstraints();
37        if (pathLen != -1)  // Not a CA
38            display(indent + "        CA: TRUE, pathLen: " + pathLen);
39        else
40            display(indent + "        CA: FALSE");
41
42        boolean[] keyUsage = cert.getKeyUsage();
43        if (keyUsage != null){
44            KeyUsage ku = new KeyUsage(keyUsage);
45            display(indent + "      Key Usage: " + ku.getKeyUsageString());
46        }
47
48        List list = null;
49        try {
50            list = cert.getExtendedKeyUsage();
51        } catch (CertificateParsingException cpe){ }
52
53        if (list != null){
54            display(indent + "      Extended Key Usage:");
55            Iterator li = list.iterator();
56            while (li.hasNext()){
57                display(indent + " ");
58                display(indent + (String)li.next());
59            }
60            display("");
61        }
62    }
63
64    public static void main(String[] args) throws Exception{
65        if (args.length < 1){
66            display("Usage:: java ShowCert <certfile>");
67            return;
68        }
69        String certfile = args[0];
70
71        CertificateFactory cf = CertificateFactory.getInstance("X.509");
72        FileInputStream fis = new FileInputStream(certfile);
73        Certificate cert = cf.generateCertificate(fis);
74        printX509Cert((X509Certificate)cert, "");
75    }
76}