source: java/net/deterlab/abac/Credential.java @ b89cfd8

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

Allow users to set validity periods.

  • Property mode set to 100644
File size: 8.1 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.4
24 */
25public 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     * Compare 2 credentials for sorting.  They are ordered by their Roles,
152     * head then tail.
153     * @param o an Object to compare
154     * @return -1 if this Credential is before, 0 if they are the same, and 1
155     *              if this Credential is after the given object.
156     */
157    public int compareTo(Object o) {
158        if (o instanceof Credential) {
159            Credential c = (Credential) o;
160
161            if (head().equals(c.head())) return tail().compareTo(c.tail());
162            else return head().compareTo(c.head());
163        }
164        else return 1;
165    }
166
167
168    /**
169     * Get the head role from the credential.
170     * @return the Role in the head
171     */
172    public Role head() { return m_head; }
173
174    /**
175     * Get the tail role from the credential
176     * @return the Role in the tail
177     */
178    public Role tail() { return m_tail; }
179
180    /**
181     * Get the expiration Date of the credential.
182     */
183    public Date expiration() { return m_expiration; }
184
185    /**
186     * Return an untranslated string form of the credential. The format is head
187     * &lt;- tail. For example: A.r1 &lt;- B.r2.r3.  Principal names are key
188     * identifiers.
189     * @return the string form
190     */
191    public String toString() {
192        return m_head + " <- " + m_tail;
193    }
194
195    /**
196     * Return a translated string form of the credential. The format is head
197     * &lt;- tail. For example: A.r1 &lt;- B.r2.r3.  Principal names are
198     * shortened to menmonics if the Context knows the identity.
199     * @param c the Context to translate names in
200     * @return the string form
201     */
202    public String simpleString(Context c) {
203        return m_head.simpleString(c) + " <- " + m_tail.simpleString(c);
204    }
205
206    /**
207     * Output the external representation of the Credential to the OutputStream
208     * given. Subclasses will overload this for their output format.
209     * @param s the OutputStream on which to write
210     * @throws IOException if there is an error writing.
211     */
212    public abstract void write(OutputStream s) throws IOException;
213
214    /**
215     * Output the external representation of the Credential to the filename
216     * given. Subclasses will overload this for their output format.
217     * @param fn a String containing the output filename
218     * @throws IOException if there is an error writing.
219     */
220    public abstract void write(String fn) 
221        throws IOException, FileNotFoundException;
222
223    /**
224     * Return true if this Credential has a certificate associated.  A jabac
225     * extension.
226     * @return true if this Credential has a certificate associated.
227     */
228    public abstract boolean hasCertificate();
229
230    /**
231     * Return the Identity that issued the underlying certificate (if any).  A
232     * jabac extension.
233     * @return the Identity that issued the underlying certificate.
234     */
235    public Identity issuer() { return id; }
236
237}
Note: See TracBrowser for help on using the repository browser.