View Javadoc

1   /**
2    *
3    *  Licensed to the Apache Software Foundation (ASF) under one or more
4    *  contributor license agreements.  See the NOTICE file distributed with
5    *  this work for additional information regarding copyright ownership.
6    *  The ASF licenses this file to You under the Apache License, Version 2.0
7    *  (the "License"); you may not use this file except in compliance with
8    *  the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *  Unless required by applicable law or agreed to in writing, software
13   *  distributed under the License is distributed on an "AS IS" BASIS,
14   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   *  See the License for the specific language governing permissions and
16   *  limitations under the License.
17   */
18  
19  package org.apache.geronimo.util.jce;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.ByteArrayOutputStream;
23  import java.io.IOException;
24  import java.security.Principal;
25  import java.util.Hashtable;
26  import java.util.Vector;
27  
28  import org.apache.geronimo.util.asn1.ASN1InputStream;
29  import org.apache.geronimo.util.asn1.ASN1Sequence;
30  import org.apache.geronimo.util.asn1.DEROutputStream;
31  import org.apache.geronimo.util.asn1.x509.X509Name;
32  
33  public class X509Principal
34      extends X509Name
35      implements Principal
36  {
37      /**
38       * Constructor from an encoded byte array.
39       */
40      public X509Principal(
41          byte[]  bytes)
42          throws IOException
43      {
44          super((ASN1Sequence)(new ASN1InputStream(new ByteArrayInputStream(bytes)).readObject()));
45      }
46  
47      /**
48       * Constructor from an X509Name object.
49       */
50      public X509Principal(
51          X509Name  name)
52      {
53          super((ASN1Sequence)name.getDERObject());
54      }
55  
56      /**
57       * constructor from a table of attributes.
58       * <p>
59       * it's is assumed the table contains OID/String pairs.
60       */
61      public X509Principal(
62          Hashtable  attributes)
63      {
64          super(attributes);
65      }
66  
67      /**
68       * constructor from a table of attributes and a vector giving the
69       * specific ordering required for encoding or conversion to a string.
70       * <p>
71       * it's is assumed the table contains OID/String pairs.
72       */
73      public X509Principal(
74          Vector      ordering,
75          Hashtable   attributes)
76      {
77          super(ordering, attributes);
78      }
79  
80      /**
81       * constructor from a vector of attribute values and a vector of OIDs.
82       */
83      public X509Principal(
84          Vector      oids,
85          Vector      values)
86      {
87          super(oids, values);
88      }
89  
90      /**
91       * takes an X509 dir name as a string of the format "C=AU,ST=Victoria", or
92       * some such, converting it into an ordered set of name attributes.
93       */
94      public X509Principal(
95          String  dirName)
96      {
97          super(dirName);
98      }
99  
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 }