1 package jsdsi;
2
3 import java.security.cert.CertStoreException;
4 import java.util.Collection;
5 import java.util.Iterator;
6 import java.util.Set;
7
8 import jsdsi.util.MultiMap;
9
10 /***
11 * Given a statement (a <code>Cert</code>) and a <code>CertStore</code>,
12 * attempts to construct a <code>Proof</code> that the statement holds
13 * using certificates from the store.
14 *
15 * @author Sameer Ajmani
16 * @version $Revision: 1.1.6.2 $ $Date: 2005/11/08 03:12:52 $
17 */
18 abstract class Prover {
19 /***
20 * Thrown when the Prover <i>finds a proof</i>. This is an abuse of
21 * exceptions, but it makes it easy to return a proof from deep
22 * within a recursive call. Ugly, but effective.
23 *
24 * @author Sameer Ajmani
25 * @version $Revision: 1.1.6.2 $ $Date: 2005/11/08 03:12:52 $
26 */
27 static class ProofFoundException extends Exception {
28 Proof proof;
29 ProofFoundException(Proof p) {
30 super("found proof: " + p);
31 proof = p;
32 }
33 Proof getProof() {
34 return proof;
35 }
36 }
37
38 /***
39 * Statement to prove.
40 */
41 Cert provee;
42
43 /***
44 * The <code>CertStore</code> used by this <code>Proof</code>.
45 */
46 java.security.cert.CertStore store;
47
48 /***
49 * cert -> set of proof(cert)
50 */
51 MultiMap check = new MultiMap();
52
53 /***
54 * name -> set of proof(name -> principal)
55 */
56 MultiMap value = new MultiMap();
57
58 /***
59 * name -> set of proof(LHS -> name+X)
60 */
61 MultiMap compatible = new MultiMap();
62 // name -> set of proof(LHS -> name+X)
63
64 /***
65 * issuer -> set of proof(issuer -> RHS)
66 */
67 MultiMap issuer = new MultiMap();
68
69 /***
70 * subject -> set of proof(LHS -> subject)
71 */
72 MultiMap reverse = new MultiMap();
73 // subject -> set of proof(LHS -> subject)
74
75 /***
76 * Number of certs fetched from cert store.
77 */
78 private int numFetched = 0;
79
80 /***
81 * Returns the number of certificates fetched from the cert-store.
82 *
83 * @return the number of certificates fetched from the cert-store.
84 */
85 int getNumFetched() {
86 return numFetched;
87 }
88
89 /***
90 * Creates a new <code>Prover</code> from a given <code>Cert</code> and
91 * a given <code>CertStore</code>.
92 *
93 * @param c <code>Cert</code> for this prover.
94 * @param s <code>CertStore</code> for this prover.
95 */
96 Prover(Cert c, java.security.cert.CertStore s) {
97 provee = c;
98 store = s;
99 }
100
101 /***
102 * Indicates if there has already be an attempt to find a proof.
103 */
104 private boolean attempted = false;
105
106 /***
107 * The proof found by this prover.
108 */
109 private Proof proof; // the proof; null if no proof found
110
111 /***
112 * Returns the proof found by this prover.
113 *
114 * @return the proof found by this prover.
115 */
116 public final Proof getProof() {
117 if (!attempted) {
118 attempted = true;
119 proof = makeProof();
120 }
121 return proof;
122 }
123
124 /***
125 * Creates a new <code>Proof</code>.
126 *
127 * @return a new <code>Proof</code>.
128 */
129 abstract Proof makeProof();
130
131 /***
132 * Inserts the certificates of a given <code>Proof</code> to this
133 * <code>Proof</code>.
134 *
135 * @param p <code>Proof</code> containing certificates to add to
136 * this <code>Proof</code>.
137 * @throws ProofFoundException if a <i>proof is found</i>.
138 */
139 abstract void insert(Proof p) throws ProofFoundException;
140
141 /***
142 * Provides new certificates for this prover. Adds the given certificates
143 * to the certificates already used.
144 *
145 * @param certs certificates to add to the prover.
146 * @throws ProofFoundException if a <i>proof is found</i> while
147 * adding the certificates.
148 */
149 void insertCertificates(Collection certs) throws ProofFoundException {
150 // insert stored proofs
151 //System.out.println("INSERT: inserting certs "+certs.size());
152 Iterator i = certs.iterator();
153 while (i.hasNext()) {
154 insert(new Proof((Certificate) i.next()));
155 }
156 }
157
158 /***
159 * If a given set does not contain a given object, all certficates
160 * from the cert-store for a given <code>CertSelector</code> will
161 * be added to this proof's certificates.
162 *
163 * @param cache set with objects.
164 * @param key key to search in <code>cache</code>.
165 * @param sel cert selector to add certificates from to this proof's
166 * certificates.
167 * @param map multi-map with sets stored for keys.
168 * @return the set stored in <code>map</code> for <code>key</code> if
169 * no error occures.
170 * @throws ProofFoundException if a <i>proof is found</i>.
171 */
172 Set load(Set cache, Object key, CertSelector sel, MultiMap map)
173 throws ProofFoundException {
174 try {
175 if (!cache.contains(key)) {
176 cache.add(key);
177 // fetch stored proofs
178 try {
179 Collection stored = store.getCertificates(sel);
180 numFetched += stored.size();
181 insertCertificates(stored);
182 } catch (CertStoreException e) {
183 throw new Error(e);
184 }
185 }
186 return map.get(key);
187 } catch (ProofFoundException e) {
188 // invalidate cache
189 cache.remove(key);
190 throw e;
191 }
192 }
193 }
This page was automatically generated by Maven