source: java/net/deterlab/abac/Credential.java @ 7b33c9b

abac0-leakabac0-meimei-idmei-rt0-nmei_rt0tvf-new-xml
Last change on this file since 7b33c9b was 7b33c9b, checked in by Ted Faber <faber@…>, 11 years ago

More sane class struture

  • Property mode set to 100644
File size: 9.2 KB
Line 
1package net.deterlab.abac;
2
3import java.io.*;
4import java.math.*;
5
6import java.util.*;
7import java.security.*;
8import java.security.cert.*;
9
10import javax.security.auth.x500.*;
11
12import org.bouncycastle.asn1.*;
13import org.bouncycastle.asn1.x509.*;
14import org.bouncycastle.x509.*;
15import org.bouncycastle.x509.util.*;
16import 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.3
24 */
25public abstract class Credential implements Comparable {
26    /** The role at the head */
27    protected Role m_head
28    /** The role at the tail */;
29    protected Role m_tail;
30    /** The identity that issued the certificate */
31    protected Identity id;
32
33    /**
34     * Create an empty Credential.
35     */
36    public Credential() {
37        m_head = m_tail = null;
38        id = null;
39    }
40    /**
41     * Create a credential from a head and tail role.  This credential has no
42     * underlying certificate, and cannot be exported or used in real proofs.
43     * make_cert can create a certificate for a credential initialized this
44     * way.
45     * @param head the Role at the head of the credential
46     * @param tail the Role at the tail of the credential
47     */
48    public Credential(Role head, Role tail) {
49        m_head = head;
50        m_tail = tail;
51        id = null;
52    }
53
54    /**
55     * Create a credential from an attribute cert in a file. Throws an
56     * exception if the cert file can't be opened or if there's a format
57     * problem with the cert.  Note that catching
58     * java.security.GeneralSecurityException catches most of the exceptions
59     * this throws.
60     * @param filename a String containing the filename to read
61     * @param ids a Collection of Identities to use in validating the cert
62     * @throws StreamParsingException if the stream is unparsable
63     * @throws CertificateException if the certificate is badly formatted
64     * @throws InvalidKeyException if none of the Identities can validate the
65     *                              certificate
66     * @throws NoSuchAlgorithmException if the credential uses an unknown
67     *                              signature algorithm
68     * @throws NoSuchProviderException if the provider of the signature
69     *                              algorithm is unavailable
70     * @throws SignatureException if the signature check fails
71     * @throws IOException if the certificate is unparsable.
72     */
73    public Credential(String filename, Collection<Identity> ids) 
74        throws Exception { 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 StreamParsingException if the stream is unparsable
85     * @throws CertificateException if the certificate is badly formatted
86     * @throws InvalidKeyException if none of the Identities can validate the
87     *                              certificate
88     * @throws NoSuchAlgorithmException if the credential uses an unknown
89     *                              signature algorithm
90     * @throws NoSuchProviderException if the provider of the signature
91     *                              algorithm is unavailable
92     * @throws SignatureException if the signature check fails
93     * @throws IOException if the certificate is unparsable.
94     */
95    public Credential(File file, Collection<Identity> ids) 
96            throws CertificateException, InvalidKeyException, 
97                NoSuchAlgorithmException, NoSuchProviderException,
98                SignatureException, StreamParsingException, IOException {
99         this();
100    }
101
102    /**
103     * Create a credential from an InputStream.  Throws an exception if the
104     * stream can't be parsed or if there's a format problem with the cert.
105     * Note that catching java.security.GeneralSecurityException catches most
106     * of the exceptions this throws.
107     * @param s the InputStream to read
108     * @param ids a Collection of Identities to use in validating the cert
109     * @throws StreamParsingException if the stream is unparsable
110     * @throws CertificateException if the certificate is badly formatted
111     * @throws InvalidKeyException if none of the Identities can validate the
112     *                              certificate
113     * @throws NoSuchAlgorithmException if the credential uses an unknown
114     *                              signature algorithm
115     * @throws NoSuchProviderException if the provider of the signature
116     *                              algorithm is unavailable
117     * @throws SignatureException if the signature check fails
118     * @throws IOException if the certificate is unparsable.
119     */
120    public Credential(InputStream s, Collection<Identity> ids) 
121            throws CertificateException, InvalidKeyException, 
122                NoSuchAlgorithmException, NoSuchProviderException,
123                SignatureException, StreamParsingException, IOException {
124         this();
125    }
126
127
128    /**
129     * Create a certificate from this credential issued by the given identity.
130     * Note that catching java.security.GeneralSecurityException catches most
131     * of the exceptions this throws.
132     * @param i the Identity that will issue the certificate
133     * @throws IOException reading or writing problems
134     * @throws CertificateEncodingException Problem creating certificate
135     * @throws InvalidKeyException if none of the Identities can sign the
136     *                              certificate
137     * @throws NoSuchAlgorithmException if the credential uses an unknown
138     *                              signature algorithm
139     * @throws NoSuchProviderException if the provider of the signature
140     *                              algorithm is unavailable
141     * @throws SignatureException if the signature creation fails
142     */
143    public abstract void make_cert(Identity i) 
144            throws IOException, CertificateEncodingException,
145               NoSuchProviderException, NoSuchAlgorithmException,
146               SignatureException, InvalidKeyException;
147
148    /**
149     * Two credentials are the same if their roles are the same.
150     * @param o an Object to compare
151     * @return true if the Credentials have the Roles
152     */
153    public boolean equals(Object o) {
154        if ( o instanceof Credential ) {
155            Credential c = (Credential) o;
156
157            if (m_head == null || m_tail == null ) return false;
158            else return (m_head.equals(c.head()) && m_tail.equals(c.tail()));
159        }
160        else return false;
161    }
162
163    /**
164     * Allow credentials to be compared.  They are ordered by their Roles, head
165     * then tail.
166     * @param o an Object to compare
167     * @return -1 if this Credential is before, 0 if they are the same, and 1
168     *              if this Credential is after the given object.
169     */
170    public int compareTo(Object o) {
171        if (o instanceof Credential) {
172            Credential c = (Credential) o;
173
174            if (head().equals(c.head())) return tail().compareTo(c.tail());
175            else return head().compareTo(c.head());
176        }
177        else return 1;
178    }
179
180
181    /**
182     * Get the head role from the credential.
183     * @return the Role in the head
184     */
185    public Role head() { return m_head; }
186
187    /**
188     * Get the tail role from the credential
189     * @return the Role in the tail
190     */
191    public Role tail() { return m_tail; }
192
193    /**
194     * Turn the credential into string form. The format is head &lt;- tail. For
195     * example: A.r1 &lt;- B.r2.r3.  Principal names are key identifiers.
196     * @return the string form
197     */
198    public String toString() {
199        return m_head + " <- " + m_tail;
200    }
201
202    /**
203     * Turn the credential into string form. The format is head &lt;- tail. For
204     * example: A.r1 &lt;- B.r2.r3.  Principal names are shortened to menmonics
205     * if the Context knows the identity.
206     * @param c the Context to translate names in
207     * @return the string form
208     */
209    public String simpleString(Context c) {
210        return m_head.simpleString(c) + " <- " + m_tail.simpleString(c);
211    }
212
213    /**
214     * Output the DER formatted attribute certificate associated with this
215     * Credential to the OutputStream.
216     * @param s the OutputStream on which to write
217     * @throws IOException if there is an error writing.
218     */
219    public abstract void write(OutputStream s) throws IOException;
220
221    /**
222     * Output the DER formatted attribute certificate associated with this
223     * Credential to the filename given.
224     * @param fn a String containing the output filename
225     * @throws IOException if there is an error writing.
226     */
227    public abstract void write(String fn) 
228        throws IOException, FileNotFoundException;
229
230    /**
231     * Return true if this Credential has a certificate associated.  A jabac
232     * extension.
233     * @return true if this Credential has a certificate associated.
234     */
235    public abstract boolean hasCertificate();
236
237    /**
238     * Return the Identity that issued the underlying certificate.  A jabac
239     * extension.
240     * @return the Identity that issued the underlying certificate.
241     */
242    public Identity issuer() { return id; }
243    /**
244     * Return the X509Certificate that issued the underlying certificate.
245     * @return the X509Certificate that issued the underlying certificate.
246     */
247    public X509Certificate issuerCert() { return id.getCertificate(); }
248}
Note: See TracBrowser for help on using the repository browser.