1 /*
2  * @(#) $Id: FileBasedIssuedCerts.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.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;
24
25public class FileBasedIssuedCerts implements IssuedCerts {
26    private String indexFileName;
27    private String dir;
28    public FileBasedIssuedCerts(String indexFileName, String dir){
29        this.indexFileName = indexFileName;
30        this.dir = dir;
31    }
32    public void add(Certificate cert) throws CADatabaseException {
33        X509Certificate x509Cert;
34        if (cert == null)
35            throw new IllegalArgumentException("null argument");
36
37        if (cert instanceof X509Certificate){
38            x509Cert = (X509Certificate)cert;
39        } else {
40            throw new CADatabaseException("unsupported certificate type: " + cert.getType());
41        }
42        String certFileName = dir + File.separator + x509Cert.getSerialNumber().toString() + ".cer";
43        File certFile = new File(certFileName);
44        if (certFile.exists()){
45            throw new CADatabaseException("certificate file exists: " + certFileName);
46        }
47
48        try {
49            FileOutputStream fis = new FileOutputStream(certFileName);
50            byte[] certBytes = x509Cert.getEncoded();
51            fis.write(certBytes);
52            fis.close();
53        } catch (Exception exc){
54            throw new CADatabaseException("cannot write certificate to file: " + certFileName, exc);
55        }
56
57
58        StringBuffer sb = new StringBuffer();
59        Calendar cal = Calendar.getInstance();
60        sb.append(cal.getTime().toString() + CADatabase.recordSeparator);
61        sb.append(x509Cert.getSerialNumber().toString() + CADatabase.recordSeparator);
62        sb.append(x509Cert.getNotBefore().toString() + CADatabase.recordSeparator);
63        sb.append(x509Cert.getNotAfter().toString() + CADatabase.recordSeparator);
64        sb.append(x509Cert.getSubjectDN().toString());
65
66        try {
67            PrintWriter pw = new PrintWriter(new FileWriter(indexFileName, true)); // Append mode
68            pw.println(sb.toString());
69            pw.close();
70        } catch (Exception exc){
71            throw new CADatabaseException("cannot write to index file: " + indexFileName, exc);
72        }
73    }
74    public boolean exists(Certificate cert) throws CADatabaseException {
75        try {
76            BufferedReader br = new BufferedReader(new FileReader(indexFileName));
77            X509Certificate x509Cert = (X509Certificate)cert;
78            String serialNo = x509Cert.getSerialNumber().toString();
79            String line = null;
80            while ((line = br.readLine()) != null){
81                String[] records = line.split(CADatabase.escapedRecordSeparator);
82                if (serialNo.equals(records[1]))
83                    return true;
84            }
85        } catch (FileNotFoundException fnfe){
86            return false;
87        } catch (IOException ioe){
88            throw new CADatabaseException("CA database corrupted.", ioe);
89        }
90        return false;
91    }
92
93    // TODO
94    public Iterator iterator(){
95        return null;
96    }
97}