source: java/net/deterlab/abac/Credential.java @ 484bb5a

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

Document cleanup. Change a few visibilities.

  • Property mode set to 100644
File size: 7.3 KB
RevLine 
[31b67d5]1package net.deterlab.abac;
2
[7ef13e3]3import java.io.*;
[281158a]4import java.math.*;
[7ef13e3]5
6import java.util.*;
7import java.security.*;
8import java.security.cert.*;
9
[e36ea1d]10import javax.security.auth.x500.*;
[90f939f]11
12import org.bouncycastle.asn1.*;
[e9360e2]13import org.bouncycastle.asn1.x509.*;
[90f939f]14import org.bouncycastle.x509.*;
[e36ea1d]15import org.bouncycastle.x509.util.*;
16import org.bouncycastle.openssl.*;
17
18/**
19 * An ABAC credential, with or without an underlying certificate that
[3797bbe]20 * represents it.  These are edges in proof graphs and can be constructed from
[e36ea1d]21 * their constituent Roles.
22 * @author <a href="http://abac.deterlab.net">ISI ABAC team</a>
[4560b65]23 * @version 1.4
[e36ea1d]24 */
[7b33c9b]25public abstract class Credential implements Comparable {
[e36ea1d]26    /** The role at the head */
27    protected Role m_head
28    /** The role at the tail */;
[d69593c]29    protected Role m_tail;
[e36ea1d]30    /** The identity that issued the certificate */
[d69593c]31    protected Identity id;
[0595372]32
[9394f1f]33    /**
[e36ea1d]34     * Create an empty Credential.
[9394f1f]35     */
[a7f73b5]36    Credential() {
[9394f1f]37        m_head = m_tail = null;
[0595372]38        id = null;
[9394f1f]39    }
[31b67d5]40    /**
[e36ea1d]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
[31b67d5]47     */
[a7f73b5]48    Credential(Role head, Role tail) {
[31b67d5]49        m_head = head;
50        m_tail = tail;
[0595372]51        id = null;
[31b67d5]52    }
53
[7ef13e3]54    /**
[e36ea1d]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
[44896b5]62     * @throws CertInvalidException if the stream is unparsable
63     * @throws MissingIssuerException if none of the Identities can validate the
[e36ea1d]64     *                              certificate
[44896b5]65     * @throws BadSignatureException if the signature check fails
[7ef13e3]66     */
[4d5f56d]67    Credential(String filename, Collection<Identity> ids) 
[44896b5]68        throws ABACException { this(); }
[7ef13e3]69
70    /**
[e36ea1d]71     * Create a credential from an attribute cert in a file. Throws an
72     * exception if the cert file can't be opened or if there's a format
73     * problem with the cert.  Note that catching
74     * java.security.GeneralSecurityException catches most of the exceptions
75     * this throws.
76     * @param file the File to read
77     * @param ids a Collection of Identities to use in validating the cert
[44896b5]78     * @throws CertInvalidException if the stream is unparsable
79     * @throws MissingIssuerException if none of the Identities can validate the
[e36ea1d]80     *                              certificate
[44896b5]81     * @throws BadSignatureException if the signature check fails
[7ef13e3]82     */
[4d5f56d]83    Credential(File file, Collection<Identity> ids) 
[44896b5]84            throws ABACException {
[7b33c9b]85         this();
[e36ea1d]86    }
[1a7e6d3]87
88    /**
[e36ea1d]89     * Create a credential from an InputStream.  Throws an exception if the
90     * stream can't be parsed or if there's a format problem with the cert.
91     * Note that catching java.security.GeneralSecurityException catches most
92     * of the exceptions this throws.
93     * @param s the InputStream to read
94     * @param ids a Collection of Identities to use in validating the cert
[44896b5]95     * @throws CertInvalidException if the stream is unparsable
96     * @throws MissingIssuerException if none of the Identities can validate the
[e36ea1d]97     *                              certificate
[44896b5]98     * @throws BadSignatureException if the signature check fails
[1a7e6d3]99     */
[4d5f56d]100    Credential(InputStream s, Collection<Identity> ids) 
[44896b5]101            throws ABACException {
[7b33c9b]102         this();
[7ef13e3]103    }
104
[84f0e7a]105
[e9360e2]106    /**
107     * Create a certificate from this credential issued by the given identity.
[e36ea1d]108     * Note that catching java.security.GeneralSecurityException catches most
109     * of the exceptions this throws.
110     * @param i the Identity that will issue the certificate
[44896b5]111     * @throws ABACException for Credential-specific errors
112     * @throws MissingIssuerException the identity is invalid
113     * @throws BadSignatureException if the signature creation fails
[e9360e2]114     */
[7b33c9b]115    public abstract void make_cert(Identity i) 
[44896b5]116            throws ABACException;
[90f939f]117
[cfcdcb4b]118    /**
[a7f73b5]119     * Return true if 2 credentials represent the same ABAC. Two credentials
120     * are the same if their roles are the same.
[e36ea1d]121     * @param o an Object to compare
122     * @return true if the Credentials have the Roles
[cfcdcb4b]123     */
124    public boolean equals(Object o) {
125        if ( o instanceof Credential ) {
126            Credential c = (Credential) o;
127
128            if (m_head == null || m_tail == null ) return false;
129            else return (m_head.equals(c.head()) && m_tail.equals(c.tail()));
130        }
131        else return false;
132    }
133
[e36ea1d]134    /**
[a7f73b5]135     * Compare 2 credentials for sorting.  They are ordered by their Roles,
136     * head then tail.
[e36ea1d]137     * @param o an Object to compare
138     * @return -1 if this Credential is before, 0 if they are the same, and 1
139     *              if this Credential is after the given object.
140     */
[88e139a]141    public int compareTo(Object o) {
142        if (o instanceof Credential) {
143            Credential c = (Credential) o;
144
145            if (head().equals(c.head())) return tail().compareTo(c.tail());
146            else return head().compareTo(c.head());
147        }
148        else return 1;
149    }
150
151
[31b67d5]152    /**
153     * Get the head role from the credential.
[e36ea1d]154     * @return the Role in the head
[31b67d5]155     */
[0595372]156    public Role head() { return m_head; }
[31b67d5]157
158    /**
159     * Get the tail role from the credential
[e36ea1d]160     * @return the Role in the tail
[31b67d5]161     */
[0595372]162    public Role tail() { return m_tail; }
[31b67d5]163
164    /**
[a7f73b5]165     * Return an untranslated string form of the credential. The format is head
166     * &lt;- tail. For example: A.r1 &lt;- B.r2.r3.  Principal names are key
167     * identifiers.
[e36ea1d]168     * @return the string form
[31b67d5]169     */
170    public String toString() {
171        return m_head + " <- " + m_tail;
172    }
173
[e36ea1d]174    /**
[a7f73b5]175     * Return a translated string form of the credential. The format is head
176     * &lt;- tail. For example: A.r1 &lt;- B.r2.r3.  Principal names are
177     * shortened to menmonics if the Context knows the identity.
[e36ea1d]178     * @param c the Context to translate names in
179     * @return the string form
180     */
[84f0e7a]181    public String simpleString(Context c) {
182        return m_head.simpleString(c) + " <- " + m_tail.simpleString(c);
[de63a31]183    }
184
[e36ea1d]185    /**
[a7f73b5]186     * Output the external representation of the Credential to the OutputStream
187     * given. Subclasses will overload this for their output format.
[e36ea1d]188     * @param s the OutputStream on which to write
189     * @throws IOException if there is an error writing.
190     */
[7b33c9b]191    public abstract void write(OutputStream s) throws IOException;
[1a7e6d3]192
[e36ea1d]193    /**
[a7f73b5]194     * Output the external representation of the Credential to the filename
195     * given. Subclasses will overload this for their output format.
[e36ea1d]196     * @param fn a String containing the output filename
197     * @throws IOException if there is an error writing.
198     */
[7b33c9b]199    public abstract void write(String fn) 
200        throws IOException, FileNotFoundException;
[1a7e6d3]201
[e36ea1d]202    /**
203     * Return true if this Credential has a certificate associated.  A jabac
204     * extension.
205     * @return true if this Credential has a certificate associated.
206     */
[7b33c9b]207    public abstract boolean hasCertificate();
[5cf72cc]208
[e36ea1d]209    /**
[a7f73b5]210     * Return the Identity that issued the underlying certificate (if any).  A
211     * jabac extension.
212     * @return the Identity that issued the underlying certificate.
[e36ea1d]213     */
[d69593c]214    public Identity issuer() { return id; }
[f84d71e]215
[31b67d5]216}
Note: See TracBrowser for help on using the repository browser.