1
10package org.jstk.pki;
11
12import java.io.*;
13
14import org.jstk.asn1.*;
15import org.jstk.pem.*;
16import org.jstk.JSTKOptions;
17
18
24public class Certificate extends ASN1Seq{
25 private TBSCertificate tbsCertificate = new TBSCertificate();
26 private AlgorithmIdentifier algorithm = new AlgorithmIdentifier();
27 private ASN1BitString signatureBytes = new ASN1BitString();
28
29 public Certificate(){
30 super();
31 add(tbsCertificate);
32 add(algorithm);
33 add(signatureBytes);
34 }
35
36 public TBSCertificate getTBSCertificate(){
37 return tbsCertificate;
38 }
39
40 public AlgorithmIdentifier getAlgorithm(){
41 return algorithm;
42 }
43
44 public ASN1BitString getSignatureBytes(){
45 return signatureBytes;
46 }
47
48 public String toString(){
49 StringBuffer sb = new StringBuffer();
50 sb.append("Certificate-SEQ(" + tbsCertificate.toString() + ", ");
51 sb.append(algorithm.toString() + ", " + signatureBytes.toString() + ")");
52 return sb.toString();
53 }
54 public static void main(String[] args) throws Exception {
55 if (args.length < 1){
56 System.out.println("Usage::java org.jstk.pki.Certificate <file>");
57 return;
58 }
59 String file = args[0];
60 InputStream is = null;
61 try { BufferedReader reader = new BufferedReader(new FileReader(file));
63 PEMData x = new PEMData(reader);
64 is = new ByteArrayInputStream(x.decode());
65 } catch (InvalidPEMFormatException exc){ is = new FileInputStream(file);
67 }
68 DefASN1PullParser parser = new DefASN1PullParser();
69 parser.setInput(is);
70
71 Certificate cert = new Certificate();
72 cert.decode(parser);
73 System.out.println(cert.toString());
74
75 JSTKOptions opts = new JSTKOptions();
76 opts.parse(args, 1);
77 String outfile = opts.get("encode");
78 if (outfile != null){
79 System.out.println("Writing the data in DER format to file: " + outfile);
80 FileOutputStream fos = new FileOutputStream(outfile);
81 byte[] encoded = cert.encode();
82 fos.write(encoded);
83 fos.close();
84 }
85 }
86}
87
88