001    /**
002     *
003     *  Licensed to the Apache Software Foundation (ASF) under one or more
004     *  contributor license agreements.  See the NOTICE file distributed with
005     *  this work for additional information regarding copyright ownership.
006     *  The ASF licenses this file to You under the Apache License, Version 2.0
007     *  (the "License"); you may not use this file except in compliance with
008     *  the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *  Unless required by applicable law or agreed to in writing, software
013     *  distributed under the License is distributed on an "AS IS" BASIS,
014     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     *  See the License for the specific language governing permissions and
016     *  limitations under the License.
017     */
018    
019    package org.apache.geronimo.util.jce;
020    
021    import java.io.ByteArrayInputStream;
022    import java.io.ByteArrayOutputStream;
023    import java.io.IOException;
024    import java.security.Principal;
025    import java.util.Hashtable;
026    import java.util.Vector;
027    
028    import org.apache.geronimo.util.asn1.ASN1InputStream;
029    import org.apache.geronimo.util.asn1.ASN1Sequence;
030    import org.apache.geronimo.util.asn1.DEROutputStream;
031    import org.apache.geronimo.util.asn1.x509.X509Name;
032    
033    public class X509Principal
034        extends X509Name
035        implements Principal
036    {
037        /**
038         * Constructor from an encoded byte array.
039         */
040        public X509Principal(
041            byte[]  bytes)
042            throws IOException
043        {
044            super((ASN1Sequence)(new ASN1InputStream(new ByteArrayInputStream(bytes)).readObject()));
045        }
046    
047        /**
048         * Constructor from an X509Name object.
049         */
050        public X509Principal(
051            X509Name  name)
052        {
053            super((ASN1Sequence)name.getDERObject());
054        }
055    
056        /**
057         * constructor from a table of attributes.
058         * <p>
059         * it's is assumed the table contains OID/String pairs.
060         */
061        public X509Principal(
062            Hashtable  attributes)
063        {
064            super(attributes);
065        }
066    
067        /**
068         * constructor from a table of attributes and a vector giving the
069         * specific ordering required for encoding or conversion to a string.
070         * <p>
071         * it's is assumed the table contains OID/String pairs.
072         */
073        public X509Principal(
074            Vector      ordering,
075            Hashtable   attributes)
076        {
077            super(ordering, attributes);
078        }
079    
080        /**
081         * constructor from a vector of attribute values and a vector of OIDs.
082         */
083        public X509Principal(
084            Vector      oids,
085            Vector      values)
086        {
087            super(oids, values);
088        }
089    
090        /**
091         * takes an X509 dir name as a string of the format "C=AU,ST=Victoria", or
092         * some such, converting it into an ordered set of name attributes.
093         */
094        public X509Principal(
095            String  dirName)
096        {
097            super(dirName);
098        }
099    
100        /**
101         * Takes an X509 dir name as a string of the format "C=AU,ST=Victoria", or
102         * some such, converting it into an ordered set of name attributes. If reverse
103         * is false the dir name will be encoded in the order of the (name, value) pairs
104         * presented, otherwise the encoding will start with the last (name, value) pair
105         * and work back.
106         */
107        public X509Principal(
108            boolean reverse,
109            String  dirName)
110        {
111            super(reverse, 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. lookUp
117         * should provide a table of lookups, indexed by lowercase only strings and
118         * yielding a DERObjectIdentifier, other than that OID. and numeric oids
119         * will be processed automatically.
120         * <p>
121         * If reverse is true, create the encoded version of the sequence starting
122         * from the last element in the string.
123         */
124        public X509Principal(
125            boolean     reverse,
126            Hashtable   lookUp,
127            String      dirName)
128        {
129            super(reverse, lookUp, dirName);
130        }
131    
132        public String getName()
133        {
134            return this.toString();
135        }
136    
137        /**
138         * return a DER encoded byte array representing this object
139         */
140        public byte[] getEncoded()
141        {
142            ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
143            DEROutputStream         dOut = new DEROutputStream(bOut);
144    
145            try
146            {
147                dOut.writeObject(this);
148            }
149            catch (IOException e)
150            {
151                throw new RuntimeException(e.toString());
152            }
153    
154            return bOut.toByteArray();
155        }
156    }