1 /*
2  * @(#) $Id: ShowCertPath.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.CertificateException;
15import java.security.cert.CertPath;
16import java.security.cert.CertificateFactory;
17import java.io.File;
18import java.io.FileInputStream;
19import java.io.BufferedInputStream;
20
21public class ShowCertPath{
22    public static void printCertPath(CertPath cp){
23        List list = cp.getCertificates();
24        Iterator li = list.iterator();
25        System.out.println("CertPath:");
26        int index = 0;
27        while (li.hasNext()){
28            System.out.println("CertPath Component: " + index );
29            X509Certificate cert = (X509Certificate)li.next();
30            ShowCert.printX509Cert(cert, "  ");
31            ++index;
32        }
33    }
34
35    public static void main(String[] args) throws Exception{
36        if (args.length < 1){
37            System.out.println("Usage:: java ShowCertPath <certpathfile>");
38            return;
39        }
40        String certpathfile = args[0];
41        CertificateFactory cf = CertificateFactory.getInstance("X.509");
42        FileInputStream fis = new FileInputStream(certpathfile);
43
44        File file = new File(certpathfile);
45        int bufsize = (int)file.length() + 1024; // Added 1024 for extra safety.
46        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file), bufsize);
47        bis.mark(bufsize);
48
49        CertPath cp = null;
50        try {
51            cp = cf.generateCertPath(bis, "PkiPath");
52        } catch (CertificateException ce) {
53            bis.reset();
54            try {
55                cp = cf.generateCertPath(bis, "PKCS7");
56            } catch (CertificateException cei) {
57                System.out.println("CertPath format not recognized.");
58                return;
59            }
60        }
61        printCertPath(cp);
62    }
63}