1 /*
2  * @(#) $Id: AddCommand.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  */
10package org.jstk.cert.rep;
11
12import java.util.HashMap;
13import java.security.cert.Certificate;
14import java.security.cert.X509Certificate;
15import java.security.cert.X509CRL;
16import java.security.cert.CertificateException;
17import java.security.cert.CRLException;
18import java.security.cert.CertificateFactory;
19import java.io.File;
20import java.io.InputStream;
21import java.io.FileInputStream;
22import java.io.BufferedInputStream;
23
24import org.jstk.*;
25
26public class AddCommand extends JSTKCommandAdapter {
27    private static HashMap defaults = new HashMap();
28    static {
29        defaults.put("reptype", "JSTK");
30        defaults.put("repfile", "my.rep");
31    }
32
33    public String briefDescription(){
34        String briefDesc = "adds a Certificate or CRL to repository";
35        return briefDesc;
36    }
37
38    public String optionsDescription(){
39        String optionsDesc =
40            "  -repfile <repfile>: Repository file.[" +
41            defaults.get("repfile") + "]\n" +
42            "  -infile <infile>  : File having the Certificate or CRL.\n";
43        return optionsDesc;
44    }
45    public String[] useForms(){
46        String[] useForms = {
47            "-infile <infile> [-repfile <repfile>]"
48        };
49        return useForms;
50    }
51    public String[] sampleUses(){
52        String[] sampleUses = {
53            "-infile test.cer"
54        };
55        return sampleUses;
56    }
57
58    public Object execute(JSTKArgs args) throws JSTKException{
59        try {
60            args.setDefaults(defaults);
61            String infile = args.get("infile");
62            if (infile == null){
63                return new JSTKResult(null, false, "No input file. Specify -infile option.");
64            }
65            String repfile = args.get("repfile");
66            FileBasedRepository fbr = new FileBasedRepository(repfile);
67
68            CertificateFactory cf = CertificateFactory.getInstance("X.509");
69
70            StringBuffer sb = new StringBuffer();
71
72            File file = new File(infile);
73            int bufsize = (int)file.length() + 1024; // Added 1024 for extra safety.
74            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(infile), bufsize);
75            bis.mark(bufsize);
76
77            try {
78                X509Certificate cert = (X509Certificate)cf.generateCertificate(bis);
79                fbr.getRepository().add(cert);
80                fbr.save();
81                return new JSTKResult(null, true, "X509 Certificate added to repository: " + repfile);
82            } catch (CertificateException ce) {
83                RepTool.logger.fine("Cannot parse input as a Certificate");
84                RepTool.logger.log(java.util.logging.Level.FINER, "Not a Certificate", ce);
85            } // Fall through.
86
87            bis.reset();
88            try {
89                X509CRL crl = (X509CRL)cf.generateCRL(bis);
90                fbr.getRepository().add(crl);
91                fbr.save();
92                return new JSTKResult(null, true, "X509 CRL added to repository: " + repfile);
93            } catch (CRLException crle) {
94                RepTool.logger.fine("Cannot parse input as a CRL");
95                RepTool.logger.log(java.util.logging.Level.FINER, "Not a CRL", crle);
96            } // Fall through.
97
98            return new JSTKResult(null, false, "Unknown format");
99        } catch (Exception exc){
00            throw new JSTKException("ShowCommand execution failed", exc);
01        }
02    }
03
04    public static void main(String[] args) throws Exception {
05        JSTKOptions opts = new JSTKOptions();
06        opts.parse(args, 0);
07        AddCommand addCmd = new AddCommand();
08        JSTKResult result = (JSTKResult)addCmd.execute(opts);
09        System.out.println(result.getText());
10        System.exit(result.isSuccess()? 0 : 1);
11    }
12}