1 /*
2 * Copyright �, Aegeus Technology Limited. All rights reserved.
3 */
4 package jsdsi.certstore.jdbc;
5
6 import java.io.ByteArrayInputStream;
7 import java.io.IOException;
8 import java.security.cert.CertificateException;
9 import java.sql.ResultSet;
10 import java.sql.SQLException;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Iterator;
14 import java.util.LinkedList;
15 import java.util.List;
16
17 import javax.sql.DataSource;
18
19 import org.apache.commons.dbutils.QueryRunner;
20 import org.apache.commons.dbutils.ResultSetHandler;
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23
24 import jsdsi.AuthCertSelector;
25 import jsdsi.Cert;
26 import jsdsi.CertSelector;
27 import jsdsi.Certificate;
28 import jsdsi.CompatibleCertSelector;
29 import jsdsi.IssuerCertSelector;
30 import jsdsi.JsdsiRuntimeException;
31 import jsdsi.Name;
32 import jsdsi.NameCert;
33 import jsdsi.NameCertSelector;
34 import jsdsi.Principal;
35 import jsdsi.Sequence;
36 import jsdsi.Subject;
37 import jsdsi.SubjectCertSelector;
38 import jsdsi.certstore.CertificateDAO;
39 import jsdsi.certstore.JsdsiCertStoreException;
40 import jsdsi.sexp.ObjInputStream;
41 import jsdsi.sexp.SexpException;
42 import jsdsi.sexp.SexpParseException;
43
44 /***
45 * JDBC based implementation of {@link CertificateDAO}.
46 *
47 * @author Sean Radford
48 * @version $Revision: 1.1.4.3 $ $Date: 2004/12/12 17:01:39 $
49 */
50 public class JdbcCertificateDAO implements CertificateDAO {
51
52 private static final Log LOG = LogFactory.getLog(JdbcCertificateDAO.class);
53
54 private QueryRunner runner;
55
56 /***
57 * Constructor
58 *
59 * @param ds the DataSource to use
60 */
61 public JdbcCertificateDAO(DataSource ds) {
62 super();
63 this.runner = new QueryRunner(ds);
64 }
65
66 /***
67 * @see jsdsi.certstore.CertificateDAO#store(jsdsi.Certificate)
68 */
69 public void store(Certificate certificate) {
70 Cert cert = certificate.getCert();
71 byte[] issuer = getIssuer(certificate);
72 byte[] subject = getSubject(certificate);
73 byte[] compatible = getCompatible(certificate);
74 byte[] name = getName(certificate);
75 String localName = getLocalName(certificate);
76 Object[] params = new Object[] { certificate.getEncoded(),
77 issuer, subject, compatible,
78 localName, name };
79 try {
80 int rows = runner
81 .update(
82 "insert into CERTIFICATE_ENTRY(CERTIFICATE, ISSUER, SUBJECT, COMPATIBLE, LOCAL_NAME, FULL_NAME) values(?, ?, ?, ?, ?, ?)",
83 params);
84 } catch (SQLException e) {
85 throw new JsdsiCertStoreException("Error storing certificate", e);
86 }
87 }
88
89 /***
90 * @see jsdsi.certstore.CertificateDAO#retrieve(jsdsi.CertSelector)
91 */
92 public Collection retrieve(CertSelector selector) {
93 if (LOG.isInfoEnabled()) {
94 LOG.info("retrieve: " + selector);
95 }
96 Collection result = null;
97 if (selector instanceof IssuerCertSelector) {
98 result = retrieve((IssuerCertSelector) selector);
99 } else if (selector instanceof AuthCertSelector) {
100 result = retrieve((AuthCertSelector) selector);
101 } else if (selector instanceof NameCertSelector) {
102 result = retrieve((NameCertSelector) selector);
103 } else if (selector instanceof SubjectCertSelector) {
104 result = retrieve((SubjectCertSelector) selector);
105 } else if (selector instanceof CompatibleCertSelector) {
106 result = retrieve((CompatibleCertSelector) selector);
107 } else {
108 throw new JsdsiCertStoreException("Unsupported CertSelector: "
109 + selector.getClass().getName());
110 }
111 if (LOG.isInfoEnabled()) {
112 LOG.info(result.size() + " Certificates found");
113 }
114 return result;
115 }
116
117 private Collection retrieve(IssuerCertSelector selector) {
118 try {
119 return (Collection) this.runner.query(
120 "select CERTIFICATE from CERTIFICATE_ENTRY where ISSUER=?", selector
121 .getIssuer().toByteArray(),
122 new CertificateResultSetHandler(1));
123 } catch (SQLException e) {
124 throw new JsdsiCertStoreException("Error retrieving Certificates",
125 e);
126 }
127 }
128
129 private Collection retrieve(AuthCertSelector selector) {
130 try {
131 Collection certificates = (Collection) this.runner
132 .query(
133 "select CERTIFICATE from CERTIFICATE_ENTRY where ISSUER=? and LOCAL_NAME is null",
134 selector.getIssuer().toByteArray(),
135 new CertificateResultSetHandler(1));
136 if (selector.getAuth() != null) {
137 LinkedList result = new LinkedList();
138 Iterator it = certificates.iterator();
139 while (it.hasNext()) {
140 Certificate certificate = (Certificate) it.next();
141 if (selector.match(certificate)) {
142 result.add(certificate);
143 }
144 }
145 return result;
146 } else {
147 return certificates;
148 }
149 } catch (SQLException e) {
150 throw new JsdsiCertStoreException("Error retrieving Certificates",
151 e);
152 }
153 }
154
155 private Collection retrieve(NameCertSelector selector) {
156 Object[] params = new Object[] { selector.getIssuer().toByteArray(),
157 selector.getName() };
158 try {
159 Collection certificates = (Collection) this.runner.query(
160 "select CERTIFICATE from CERTIFICATE_ENTRY where ISSUER=? and LOCAL_NAME=?",
161 params,
162 new CertificateResultSetHandler(1));
163 return certificates;
164 } catch (SQLException e) {
165 throw new JsdsiCertStoreException("Error retrieving Certificates",
166 e);
167 }
168 }
169
170 private Collection retrieve(SubjectCertSelector selector) {
171 Subject subj = selector.getSubject();
172 byte[] bytes = null;
173 if (subj instanceof Principal) {
174 bytes = ((Principal) subj).toByteArray();
175 } else {
176 bytes = ((Name) subj).toByteArray();
177 }
178 Object[] params = new Object[] { bytes };
179 try {
180 Collection certificates = (Collection) this.runner.query(
181 "select CERTIFICATE from CERTIFICATE_ENTRY where SUBJECT=?", params,
182 new CertificateResultSetHandler(1));
183 return certificates;
184 } catch (SQLException e) {
185 throw new JsdsiCertStoreException("Error retrieving Certificates",
186 e);
187 }
188 }
189
190 private Collection retrieve(CompatibleCertSelector selector) {
191 Name name = selector.getFullName();
192 Object[] params = new Object[] { name.toByteArray() };
193 try {
194 Collection certificates = (Collection) this.runner.query(
195 "select CERTIFICATE from CERTIFICATE_ENTRY where COMPATIBLE=?", params,
196 new CertificateResultSetHandler(1));
197 return certificates;
198 } catch (SQLException e) {
199 throw new JsdsiCertStoreException("Error retrieving Certificates",
200 e);
201 }
202
203 }
204
205 private byte[] getIssuer(Certificate certificate) {
206 return certificate.getCert().getIssuer().toByteArray();
207 }
208
209 private byte[] getSubject(Certificate certificate) {
210 Subject subject = certificate.getCert().getSubject();
211 if (subject instanceof Principal) {
212 return ((Principal) subject).toByteArray();
213 } else {
214 return ((Name) subject).toByteArray();
215 }
216 }
217
218 private byte[] getCompatible(Certificate certificate) {
219 Subject subject = certificate.getCert().getSubject();
220 if (subject instanceof Name) {
221 Name name = ((Name) subject).prefix();
222 byte[] result = name.toByteArray();
223 return result;
224 } else {
225 return null;
226 }
227 }
228
229 private String getLocalName(Certificate certificate) {
230 Cert cert = certificate.getCert();
231 if (cert instanceof NameCert) {
232 return ((NameCert) cert).getName();
233 } else {
234 return null;
235 }
236 }
237
238 private byte[] getName(Certificate certificate) {
239 Cert cert = certificate.getCert();
240 if (cert instanceof NameCert) {
241 return ((NameCert) cert).getFullName().toByteArray();
242 } else {
243 return null;
244 }
245 }
246
247 private class CertificateResultSetHandler implements ResultSetHandler {
248
249 private int columnIndex = 1;
250
251 public CertificateResultSetHandler(int columnIndex) {
252 super();
253 this.columnIndex = columnIndex;
254 }
255
256 /***
257 * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet)
258 */
259 public Object handle(ResultSet rs) throws SQLException {
260 List result = new ArrayList();
261 while (rs.next()) {
262 byte[] bytes = rs.getBytes(columnIndex);
263 ObjInputStream ois = new ObjInputStream(new ByteArrayInputStream(bytes));
264 try {
265 Sequence sequence = (Sequence) ois.readObj();
266 result.add(Certificate.fromSequence(sequence));
267 } catch (SexpParseException e) {
268 throw new JsdsiRuntimeException(e);
269 } catch (SexpException e) {
270 throw new JsdsiRuntimeException(e);
271 } catch (IOException e) {
272 throw new JsdsiRuntimeException(e);
273 } catch (CertificateException e) {
274 throw new JsdsiRuntimeException(e);
275 }
276 }
277 return result;
278 }
279
280 }
281
282 }
This page was automatically generated by Maven