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