001: package org.bouncycastle.jce;
002:
003: import org.bouncycastle.asn1.ASN1InputStream;
004: import org.bouncycastle.asn1.ASN1Sequence;
005: import org.bouncycastle.asn1.DEROutputStream;
006: import org.bouncycastle.asn1.x509.X509Name;
007:
008: import java.io.ByteArrayOutputStream;
009: import java.io.IOException;
010: import java.security.Principal;
011: import java.util.Hashtable;
012: import java.util.Vector;
013:
014: /**
015: * a general extension of X509Name with a couple of extra methods and
016: * constructors.
017: * <p>
018: * Objects of this type can be created from certificates and CRLs using the
019: * PrincipalUtil class.
020: * </p>
021: * @see org.bouncycastle.jce.PrincipalUtil
022: */
023: public class X509Principal extends X509Name implements Principal {
024: private static ASN1Sequence readSequence(ASN1InputStream aIn)
025: throws IOException {
026: try {
027: return ASN1Sequence.getInstance(aIn.readObject());
028: } catch (IllegalArgumentException e) {
029: throw new IOException("not an ASN.1 Sequence: " + e);
030: }
031: }
032:
033: /**
034: * Constructor from an encoded byte array.
035: */
036: public X509Principal(byte[] bytes) throws IOException {
037: super (readSequence(new ASN1InputStream(bytes)));
038: }
039:
040: /**
041: * Constructor from an X509Name object.
042: */
043: public X509Principal(X509Name name) {
044: super ((ASN1Sequence) name.getDERObject());
045: }
046:
047: /**
048: * constructor from a table of attributes.
049: * <p>
050: * it's is assumed the table contains OID/String pairs.
051: */
052: public X509Principal(Hashtable attributes) {
053: super (attributes);
054: }
055:
056: /**
057: * constructor from a table of attributes and a vector giving the
058: * specific ordering required for encoding or conversion to a string.
059: * <p>
060: * it's is assumed the table contains OID/String pairs.
061: */
062: public X509Principal(Vector ordering, Hashtable attributes) {
063: super (ordering, attributes);
064: }
065:
066: /**
067: * constructor from a vector of attribute values and a vector of OIDs.
068: */
069: public X509Principal(Vector oids, Vector values) {
070: super (oids, values);
071: }
072:
073: /**
074: * takes an X509 dir name as a string of the format "C=AU,ST=Victoria", or
075: * some such, converting it into an ordered set of name attributes.
076: */
077: public X509Principal(String dirName) {
078: super (dirName);
079: }
080:
081: /**
082: * Takes an X509 dir name as a string of the format "C=AU,ST=Victoria", or
083: * some such, converting it into an ordered set of name attributes. If reverse
084: * is false the dir name will be encoded in the order of the (name, value) pairs
085: * presented, otherwise the encoding will start with the last (name, value) pair
086: * and work back.
087: */
088: public X509Principal(boolean reverse, String dirName) {
089: super (reverse, dirName);
090: }
091:
092: /**
093: * Takes an X509 dir name as a string of the format "C=AU, ST=Victoria", or
094: * some such, converting it into an ordered set of name attributes. lookUp
095: * should provide a table of lookups, indexed by lowercase only strings and
096: * yielding a DERObjectIdentifier, other than that OID. and numeric oids
097: * will be processed automatically.
098: * <p>
099: * If reverse is true, create the encoded version of the sequence starting
100: * from the last element in the string.
101: */
102: public X509Principal(boolean reverse, Hashtable lookUp,
103: String dirName) {
104: super (reverse, lookUp, dirName);
105: }
106:
107: public String getName() {
108: return this .toString();
109: }
110:
111: /**
112: * return a DER encoded byte array representing this object
113: */
114: public byte[] getEncoded() {
115: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
116: DEROutputStream dOut = new DEROutputStream(bOut);
117:
118: try {
119: dOut.writeObject(this );
120: } catch (IOException e) {
121: throw new RuntimeException(e.toString());
122: }
123:
124: return bOut.toByteArray();
125: }
126: }
|