1
10package org.jstk.cert;
11
12import java.util.Iterator;
13import java.util.Collection;
14import java.util.HashMap;
15import java.util.List;
16import java.util.Set;
17import java.util.ListIterator;
18import java.security.cert.Certificate;
19import java.security.cert.X509Certificate;
20import java.security.cert.X509CRL;
21import java.security.cert.X509CRLEntry;
22import java.security.cert.CertificateException;
23import java.security.cert.CertificateParsingException;
24import java.security.cert.CRLException;
25import java.security.cert.CertPath;
26import java.security.cert.CertificateFactory;
27import java.io.File;
28import java.io.FileInputStream;
29import java.io.FileOutputStream;
30import java.io.BufferedInputStream;
31
32import org.jstk.*;
33
34public class CutCommand extends JSTKCommandAdapter {
35 private static HashMap defaults = new HashMap();
36 static {
37 }
39
40 public String briefDescription(){
41 String briefDesc = "take out a component of a certification path";
42 return briefDesc;
43 }
44
45 public String optionsDescription(){
46 String optionsDesc =
47 " -infile <infile> : File having the certification path.\n" +
48 " -outfile <outfile>: File to store the component.\n";
49 return optionsDesc;
50 }
51 public String[] useForms(){
52 String[] useForms = {
53 "-infile <infile> -outfile <outfile>"
54 };
55 return useForms;
56 }
57 public String[] sampleUses(){
58 String[] sampleUses = {
59 "-infile test.cer -outfile test1.cer"
60 };
61 return sampleUses;
62 }
63
64 public void writeCert(Certificate cert, String file) throws Exception {
65 FileOutputStream fos = new FileOutputStream(file);
66 fos.write(cert.getEncoded());
67 fos.close();
68 }
69
70 public Object execute(JSTKArgs args) throws JSTKException{
71 try {
72 args.setDefaults(defaults);
73 String infile = args.get("infile");
74 String outfile = args.get("outfile");
75 if (infile == null)
76 return new JSTKResult(null, false, "No input file. Specify -infile option.");
77
78 if (outfile == null)
79 return new JSTKResult(null, false, "No output file. Specify -outfile option.");
80
81 CertificateFactory cf = CertificateFactory.getInstance("X.509");
82
83 StringBuffer sb = new StringBuffer();
84
85 File file = new File(infile);
86 int bufsize = (int)file.length() + 1024; BufferedInputStream bis = new BufferedInputStream(new FileInputStream(infile), bufsize);
88 bis.mark(bufsize);
89
90 try {
91 Certificate cert = cf.generateCertificate(bis);
92 writeCert(cert, outfile);
93 return new JSTKResult(null, true, "Wrote certificate to file: " + outfile);
94 } catch (CertificateException ce) {
95 CertTool.logger.fine("Cannot parse input as a Certificate");
96 CertTool.logger.log(java.util.logging.Level.FINER, "Not a Certificate", ce);
97 }
99 bis.reset();
00 try {
01 CertPath cp = cf.generateCertPath(bis, "PkiPath");
02 List list = cp.getCertificates();
03 Certificate cert = (Certificate)list.get(0);
04 writeCert(cert, outfile);
05 return new JSTKResult(null, true, "Wrote certificate to file: " + outfile);
06 } catch (CertificateException ce) {
07 CertTool.logger.fine("Cannot parse input as a PkiPath Cert Path");
08 CertTool.logger.log(java.util.logging.Level.FINER, "Not a PkiPath Cert Path", ce);
09 }
11 bis.reset();
12 try {
13 CertPath cp = cf.generateCertPath(bis, "PKCS7");
14 List list = cp.getCertificates();
15 Certificate cert = (Certificate)list.get(0);
16 writeCert(cert, outfile);
17 return new JSTKResult(null, true, "Wrote certificate to file: " + outfile);
18 } catch (CertificateException ce) {
19 CertTool.logger.fine("Cannot parse input as a PKCS7 Cert Path");
20 CertTool.logger.log(java.util.logging.Level.FINER, "Not a PKCS7 Cert Path", ce);
21 }
23 return new JSTKResult(null, false, "Unknown format");
24 } catch (Exception exc){
25 throw new JSTKException("ShowCommand execution failed", exc);
26 }
27 }
28
29 public static void main(String[] args) throws Exception {
30 JSTKOptions opts = new JSTKOptions();
31 opts.parse(args, 0);
32 ShowCommand showCmd = new ShowCommand();
33 JSTKResult result = (JSTKResult)showCmd.execute(opts);
34 System.out.println(result.getText());
35 System.exit(result.isSuccess()? 0 : 1);
36 }
37}