1package org.bouncycastle.jce;
2
3import java.io.ByteArrayOutputStream;
4import java.io.IOException;
5import java.security.Principal;
6import java.util.Hashtable;
7import java.util.Vector;
8
9import org.bouncycastle.asn1.ASN1InputStream;
10import org.bouncycastle.asn1.ASN1Sequence;
11import org.bouncycastle.asn1.DEROutputStream;
12import org.bouncycastle.asn1.x509.X509Name;
13
14public class X509Principal
15    extends X509Name
16    implements Principal
17{
18    private static ASN1Sequence readSequence(
19        ASN1InputStream aIn)
20        throws IOException
21    {
22        try
23        {
24            return ASN1Sequence.getInstance(aIn.readObject());
25        }
26        catch (IllegalArgumentException e)
27        {
28            throw new IOException("not an ASN.1 Sequence: " + e);
29        }
30    }
31
32    /**
33     * Constructor from an encoded byte array.
34     */
35    public X509Principal(
36        byte[]  bytes)
37        throws IOException
38    {
39        super(readSequence(new ASN1InputStream(bytes)));
40    }
41
42    /**
43     * Constructor from an X509Name object.
44     */
45    public X509Principal(
46        X509Name  name)
47    {
48        super((ASN1Sequence)name.getDERObject());
49    }
50
51    /**
52     * constructor from a table of attributes.
53     * <p>
54     * it's is assumed the table contains OID/String pairs.
55     */
56    public X509Principal(
57        Hashtable  attributes)
58    {
59        super(attributes);
60    }
61
62    /**
63     * constructor from a table of attributes and a vector giving the
64     * specific ordering required for encoding or conversion to a string.
65     * <p>
66     * it's is assumed the table contains OID/String pairs.
67     */
68    public X509Principal(
69        Vector      ordering,
70        Hashtable   attributes)
71    {
72        super(ordering, attributes);
73    }
74
75    /**
76     * constructor from a vector of attribute values and a vector of OIDs.
77     */
78    public X509Principal(
79        Vector      oids,
80        Vector      values)
81    {
82        super(oids, values);
83    }
84
85    /**
86     * takes an X509 dir name as a string of the format "C=AU,ST=Victoria", or
87     * some such, converting it into an ordered set of name attributes.
88     */
89    public X509Principal(
90        String  dirName)
91    {
92        super(dirName);
93    }
94
95    /**
96     * Takes an X509 dir name as a string of the format "C=AU,ST=Victoria", or
97     * some such, converting it into an ordered set of name attributes. If reverse
98     * is false the dir name will be encoded in the order of the (name, value) pairs
99     * presented, otherwise the encoding will start with the last (name, value) pair
100     * and work back.
101     */
102    public X509Principal(
103        boolean reverse,
104        String  dirName)
105    {
106        super(reverse, dirName);
107    }
108
109    /**
110     * Takes an X509 dir name as a string of the format "C=AU, ST=Victoria", or
111     * some such, converting it into an ordered set of name attributes. lookUp
112     * should provide a table of lookups, indexed by lowercase only strings and
113     * yielding a DERObjectIdentifier, other than that OID. and numeric oids
114     * will be processed automatically.
115     * <p>
116     * If reverse is true, create the encoded version of the sequence starting
117     * from the last element in the string.
118     */
119    public X509Principal(
120        boolean     reverse,
121        Hashtable   lookUp,
122        String      dirName)
123    {
124        super(reverse, lookUp, dirName);
125    }
126
127    public String getName()
128    {
129        return this.toString();
130    }
131
132    /**
133     * return a DER encoded byte array representing this object
134     */
135    public byte[] getEncoded()
136    {
137        ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
138        DEROutputStream         dOut = new DEROutputStream(bOut);
139
140        try
141        {
142            dOut.writeObject(this);
143        }
144        catch (IOException e)
145        {
146            throw new RuntimeException(e.toString());
147        }
148
149        return bOut.toByteArray();
150    }
151}
152