1 | package net.deterlab.abac; |
---|
2 | |
---|
3 | import java.io.*; |
---|
4 | import java.math.*; |
---|
5 | |
---|
6 | import java.util.*; |
---|
7 | import java.security.*; |
---|
8 | import java.security.cert.*; |
---|
9 | |
---|
10 | import javax.security.auth.x500.*; |
---|
11 | |
---|
12 | import org.bouncycastle.asn1.*; |
---|
13 | import org.bouncycastle.asn1.x509.*; |
---|
14 | import org.bouncycastle.x509.*; |
---|
15 | import org.bouncycastle.x509.util.*; |
---|
16 | import org.bouncycastle.openssl.*; |
---|
17 | |
---|
18 | /** |
---|
19 | * An ABAC credential, with or without an underlying certificate that |
---|
20 | * represents it. These are edges in proof graphs and can be constructed from |
---|
21 | * their constituent Roles. |
---|
22 | * @author <a href="http://abac.deterlab.net">ISI ABAC team</a> |
---|
23 | * @version 1.4 |
---|
24 | */ |
---|
25 | public abstract class Credential implements Comparable { |
---|
26 | /** Default validity period (in seconds) */ |
---|
27 | static public long defaultValidity = 3600L * 24L * 365L; |
---|
28 | /** The role at the head */ |
---|
29 | protected Role m_head |
---|
30 | /** The role at the tail */; |
---|
31 | protected Role m_tail; |
---|
32 | /** The identity that issued the certificate */ |
---|
33 | protected Identity id; |
---|
34 | /** The expiration time of the credential */ |
---|
35 | protected Date m_expiration; |
---|
36 | |
---|
37 | /** |
---|
38 | * Create an empty Credential. |
---|
39 | */ |
---|
40 | Credential() { |
---|
41 | m_head = m_tail = null; |
---|
42 | id = null; |
---|
43 | m_expiration = null; |
---|
44 | } |
---|
45 | /** |
---|
46 | * Create a credential from a head and tail role. This credential has no |
---|
47 | * underlying certificate, and cannot be exported or used in real proofs. |
---|
48 | * make_cert can create a certificate for a credential initialized this |
---|
49 | * way. |
---|
50 | * @param head the Role at the head of the credential |
---|
51 | * @param tail the Role at the tail of the credential |
---|
52 | */ |
---|
53 | Credential(Role head, Role tail) { |
---|
54 | m_head = head; |
---|
55 | m_tail = tail; |
---|
56 | id = null; |
---|
57 | m_expiration = null; |
---|
58 | } |
---|
59 | |
---|
60 | /** |
---|
61 | * Create a credential from an attribute cert in a file. Throws an |
---|
62 | * exception if the cert file can't be opened or if there's a format |
---|
63 | * problem with the cert. Note that catching |
---|
64 | * java.security.GeneralSecurityException catches most of the exceptions |
---|
65 | * this throws. |
---|
66 | * @param filename a String containing the filename to read |
---|
67 | * @param ids a Collection of Identities to use in validating the cert |
---|
68 | * @throws CertInvalidException if the stream is unparsable |
---|
69 | * @throws MissingIssuerException if none of the Identities can validate the |
---|
70 | * certificate |
---|
71 | * @throws BadSignatureException if the signature check fails |
---|
72 | */ |
---|
73 | Credential(String filename, Collection<Identity> ids) |
---|
74 | throws ABACException { this(); } |
---|
75 | |
---|
76 | /** |
---|
77 | * Create a credential from an attribute cert in a file. Throws an |
---|
78 | * exception if the cert file can't be opened or if there's a format |
---|
79 | * problem with the cert. Note that catching |
---|
80 | * java.security.GeneralSecurityException catches most of the exceptions |
---|
81 | * this throws. |
---|
82 | * @param file the File to read |
---|
83 | * @param ids a Collection of Identities to use in validating the cert |
---|
84 | * @throws CertInvalidException if the stream is unparsable |
---|
85 | * @throws MissingIssuerException if none of the Identities can validate the |
---|
86 | * certificate |
---|
87 | * @throws BadSignatureException if the signature check fails |
---|
88 | */ |
---|
89 | Credential(File file, Collection<Identity> ids) |
---|
90 | throws ABACException { |
---|
91 | this(); |
---|
92 | } |
---|
93 | |
---|
94 | /** |
---|
95 | * Create a credential from an InputStream. Throws an exception if the |
---|
96 | * stream can't be parsed or if there's a format problem with the cert. |
---|
97 | * Note that catching java.security.GeneralSecurityException catches most |
---|
98 | * of the exceptions this throws. |
---|
99 | * @param s the InputStream to read |
---|
100 | * @param ids a Collection of Identities to use in validating the cert |
---|
101 | * @throws CertInvalidException if the stream is unparsable |
---|
102 | * @throws MissingIssuerException if none of the Identities can validate the |
---|
103 | * certificate |
---|
104 | * @throws BadSignatureException if the signature check fails |
---|
105 | */ |
---|
106 | Credential(InputStream s, Collection<Identity> ids) |
---|
107 | throws ABACException { |
---|
108 | this(); |
---|
109 | } |
---|
110 | |
---|
111 | /** |
---|
112 | * Create a certificate from this credential issued by the given identity, |
---|
113 | * valid for the given time. |
---|
114 | * @param i the Identity that will issue the certificate |
---|
115 | * @param validity a long holding the number of seconds that the credential |
---|
116 | * is valid for. |
---|
117 | * @throws ABACException for Credential-specific errors |
---|
118 | * @throws MissingIssuerException the identity is invalid |
---|
119 | * @throws BadSignatureException if the signature creation fails |
---|
120 | */ |
---|
121 | public abstract void make_cert(Identity i, long validity) |
---|
122 | throws ABACException; |
---|
123 | /** |
---|
124 | * Create a certificate from this credential issued by the given identity, |
---|
125 | * valid for the default interval. |
---|
126 | * @param i the Identity that will issue the certificate |
---|
127 | * @throws ABACException for Credential-specific errors |
---|
128 | * @throws MissingIssuerException the identity is invalid |
---|
129 | * @throws BadSignatureException if the signature creation fails |
---|
130 | */ |
---|
131 | public abstract void make_cert(Identity i) |
---|
132 | throws ABACException; |
---|
133 | |
---|
134 | /** |
---|
135 | * Return true if 2 credentials represent the same ABAC. Two credentials |
---|
136 | * are the same if their roles are the same. |
---|
137 | * @param o an Object to compare |
---|
138 | * @return true if the Credentials have the Roles |
---|
139 | */ |
---|
140 | public boolean equals(Object o) { |
---|
141 | if ( o instanceof Credential ) { |
---|
142 | Credential c = (Credential) o; |
---|
143 | |
---|
144 | if (m_head == null || m_tail == null ) return false; |
---|
145 | else return (m_head.equals(c.head()) && m_tail.equals(c.tail())); |
---|
146 | } |
---|
147 | else return false; |
---|
148 | } |
---|
149 | |
---|
150 | /** |
---|
151 | * Return a hash code for the Credential - the hashes of its roles. |
---|
152 | * @return an int, the hashCode |
---|
153 | */ |
---|
154 | public int hashCode() { |
---|
155 | if ( m_head == null || m_tail == null) return super.hashCode(); |
---|
156 | |
---|
157 | return m_head.hashCode() + m_tail.hashCode(); |
---|
158 | } |
---|
159 | |
---|
160 | /** |
---|
161 | * Compare 2 credentials for sorting. They are ordered by their Roles, |
---|
162 | * head then tail. |
---|
163 | * @param o an Object to compare |
---|
164 | * @return -1 if this Credential is before, 0 if they are the same, and 1 |
---|
165 | * if this Credential is after the given object. |
---|
166 | */ |
---|
167 | public int compareTo(Object o) { |
---|
168 | if (o instanceof Credential) { |
---|
169 | Credential c = (Credential) o; |
---|
170 | |
---|
171 | if (head().equals(c.head())) return tail().compareTo(c.tail()); |
---|
172 | else return head().compareTo(c.head()); |
---|
173 | } |
---|
174 | else return 1; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | /** |
---|
179 | * Get the head role from the credential. |
---|
180 | * @return the Role in the head |
---|
181 | */ |
---|
182 | public Role head() { return m_head; } |
---|
183 | |
---|
184 | /** |
---|
185 | * Get the tail role from the credential |
---|
186 | * @return the Role in the tail |
---|
187 | */ |
---|
188 | public Role tail() { return m_tail; } |
---|
189 | |
---|
190 | /** |
---|
191 | * Get the expiration Date of the credential. |
---|
192 | */ |
---|
193 | public Date expiration() { return m_expiration; } |
---|
194 | |
---|
195 | /** |
---|
196 | * Return an untranslated string form of the credential. The format is head |
---|
197 | * <- tail. For example: A.r1 <- B.r2.r3. Principal names are key |
---|
198 | * identifiers. |
---|
199 | * @return the string form |
---|
200 | */ |
---|
201 | public String toString() { |
---|
202 | return m_head + " <- " + m_tail; |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * Return a translated string form of the credential. The format is head |
---|
207 | * <- tail. For example: A.r1 <- B.r2.r3. Principal names are |
---|
208 | * shortened to menmonics if the Context knows the identity. |
---|
209 | * @param c the Context to translate names in |
---|
210 | * @return the string form |
---|
211 | */ |
---|
212 | public String simpleString(Context c) { |
---|
213 | return m_head.simpleString(c) + " <- " + m_tail.simpleString(c); |
---|
214 | } |
---|
215 | |
---|
216 | /** |
---|
217 | * Output the external representation of the Credential to the OutputStream |
---|
218 | * given. Subclasses will overload this for their output format. |
---|
219 | * @param s the OutputStream on which to write |
---|
220 | * @throws IOException if there is an error writing. |
---|
221 | */ |
---|
222 | public abstract void write(OutputStream s) throws IOException; |
---|
223 | |
---|
224 | /** |
---|
225 | * Output the external representation of the Credential to the filename |
---|
226 | * given. Subclasses will overload this for their output format. |
---|
227 | * @param fn a String containing the output filename |
---|
228 | * @throws IOException if there is an error writing. |
---|
229 | */ |
---|
230 | public abstract void write(String fn) |
---|
231 | throws IOException, FileNotFoundException; |
---|
232 | |
---|
233 | /** |
---|
234 | * Return true if this Credential has a certificate associated. A jabac |
---|
235 | * extension. |
---|
236 | * @return true if this Credential has a certificate associated. |
---|
237 | */ |
---|
238 | public abstract boolean hasCertificate(); |
---|
239 | |
---|
240 | /** |
---|
241 | * Return the Identity that issued the underlying certificate (if any). A |
---|
242 | * jabac extension. |
---|
243 | * @return the Identity that issued the underlying certificate. |
---|
244 | */ |
---|
245 | public Identity issuer() { return id; } |
---|
246 | |
---|
247 | } |
---|