1
10package org.jstk.cert.ca;
11
12import java.io.IOException;
13import java.io.FileNotFoundException;
14import java.io.File;
15import java.io.FileOutputStream;
16import java.io.FileWriter;
17import java.io.PrintWriter;
18import java.io.FileReader;
19import java.io.BufferedReader;
20import java.util.Calendar;
21import java.security.cert.Certificate;
22import java.util.Iterator;
23import java.security.cert.X509Certificate;
24import java.math.BigInteger;
25
26public class FileBasedRevokedCerts implements RevokedCerts {
27 private String indexFileName;
28 private String dir;
29
30 public class FileBasedRevokedCertsIterator implements Iterator {
31 private String[] records = null;
32 private BufferedReader br = null;
33 public FileBasedRevokedCertsIterator() {
34 try {
35 br = new BufferedReader(new FileReader(indexFileName));
36 String line = br.readLine();
37 if (line != null)
38 records = line.split(CADatabase.escapedRecordSeparator);
39 } catch (FileNotFoundException fnfe){
40 } catch (IOException ioe){
42 System.err.println("Unexpected exception: " + ioe);
43 records = null;
44 }
45 }
46
47 public boolean hasNext(){
48 return (records != null);
49 }
50
51 public Object next(){
52 RevokedCert rc = new RevokedCert(records[0], records[1]);
53 try {
54 String line = br.readLine();
55 if (line != null)
56 records = line.split(CADatabase.escapedRecordSeparator);
57 else
58 records = null;
59 } catch (IOException ioe){
60 System.err.println("Unexpected exception: " + ioe);
61 records = null;
62 }
63 return rc;
64 }
65
66 public void remove(){
67 }
68 }
69
70 public FileBasedRevokedCerts(String indexFileName, String dir){
71 this.indexFileName = indexFileName;
72 this.dir = dir;
73 }
74 public void add(Certificate cert) throws CADatabaseException {
75 X509Certificate x509Cert;
76 if (cert == null)
77 throw new IllegalArgumentException("null argument");
78
79 if (cert instanceof X509Certificate){
80 x509Cert = (X509Certificate)cert;
81 } else {
82 throw new CADatabaseException("unsupported certificate type: " + cert.getType());
83 }
84 add(x509Cert.getSerialNumber());
85 }
86 public void add(BigInteger serialNo) throws CADatabaseException {
87 String certFileName = dir + File.separator + serialNo.toString() + ".cer";
88
89
90 StringBuffer sb = new StringBuffer();
91 Calendar cal = Calendar.getInstance();
92 sb.append(cal.getTime().toString() + CADatabase.recordSeparator);
93 sb.append(serialNo.toString() + CADatabase.recordSeparator);
94
95 try {
96 PrintWriter pw = new PrintWriter(new FileWriter(indexFileName, true)); pw.println(sb.toString());
98 pw.close();
99 } catch (Exception exc){
00 throw new CADatabaseException("cannot write to index file: " + indexFileName, exc);
01 }
02 }
03
04 public boolean exists(Certificate cert) throws CADatabaseException {
05 try {
06 BufferedReader br = new BufferedReader(new FileReader(indexFileName));
07 X509Certificate x509Cert = (X509Certificate)cert;
08 String serialNo = x509Cert.getSerialNumber().toString();
09 String line = null;
10 while ((line = br.readLine()) != null){
11 String[] records = line.split(CADatabase.escapedRecordSeparator);
12 if (serialNo.equals(records[1]))
13 return true;
14 }
15 } catch (FileNotFoundException fnfe){
16 return false;
17 } catch (IOException ioe){
18 throw new CADatabaseException("CA database corrupted.", ioe);
19 }
20 return false;
21 }
22
23 public Iterator iterator(){
24 return new FileBasedRevokedCertsIterator();
25 }
26}