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