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: */package org.apache.geronimo.crypto.jce;
017:
018: import java.io.ByteArrayInputStream;
019: import java.io.ByteArrayOutputStream;
020: import java.io.IOException;
021: import java.security.Principal;
022: import java.util.Hashtable;
023: import java.util.Vector;
024:
025: import org.apache.geronimo.crypto.asn1.ASN1InputStream;
026: import org.apache.geronimo.crypto.asn1.ASN1Sequence;
027: import org.apache.geronimo.crypto.asn1.DEROutputStream;
028: import org.apache.geronimo.crypto.asn1.x509.X509Name;
029:
030: public class X509Principal extends X509Name implements Principal {
031: /**
032: * Constructor from an encoded byte array.
033: */
034: public X509Principal(byte[] bytes) throws IOException {
035: super ((ASN1Sequence) (new ASN1InputStream(
036: new ByteArrayInputStream(bytes)).readObject()));
037: }
038:
039: /**
040: * Constructor from an X509Name object.
041: */
042: public X509Principal(X509Name name) {
043: super ((ASN1Sequence) name.getDERObject());
044: }
045:
046: /**
047: * constructor from a table of attributes.
048: * <p>
049: * it's is assumed the table contains OID/String pairs.
050: */
051: public X509Principal(Hashtable attributes) {
052: super (attributes);
053: }
054:
055: /**
056: * constructor from a table of attributes and a vector giving the
057: * specific ordering required for encoding or conversion to a string.
058: * <p>
059: * it's is assumed the table contains OID/String pairs.
060: */
061: public X509Principal(Vector ordering, Hashtable attributes) {
062: super (ordering, attributes);
063: }
064:
065: /**
066: * constructor from a vector of attribute values and a vector of OIDs.
067: */
068: public X509Principal(Vector oids, Vector values) {
069: super (oids, values);
070: }
071:
072: /**
073: * takes an X509 dir name as a string of the format "C=AU,ST=Victoria", or
074: * some such, converting it into an ordered set of name attributes.
075: */
076: public X509Principal(String dirName) {
077: super (dirName);
078: }
079:
080: /**
081: * Takes an X509 dir name as a string of the format "C=AU,ST=Victoria", or
082: * some such, converting it into an ordered set of name attributes. If reverse
083: * is false the dir name will be encoded in the order of the (name, value) pairs
084: * presented, otherwise the encoding will start with the last (name, value) pair
085: * and work back.
086: */
087: public X509Principal(boolean reverse, String dirName) {
088: super (reverse, dirName);
089: }
090:
091: /**
092: * Takes an X509 dir name as a string of the format "C=AU, ST=Victoria", or
093: * some such, converting it into an ordered set of name attributes. lookUp
094: * should provide a table of lookups, indexed by lowercase only strings and
095: * yielding a DERObjectIdentifier, other than that OID. and numeric oids
096: * will be processed automatically.
097: * <p>
098: * If reverse is true, create the encoded version of the sequence starting
099: * from the last element in the string.
100: */
101: public X509Principal(boolean reverse, Hashtable lookUp,
102: String dirName) {
103: super (reverse, lookUp, dirName);
104: }
105:
106: public String getName() {
107: return this .toString();
108: }
109:
110: /**
111: * return a DER encoded byte array representing this object
112: */
113: public byte[] getEncoded() {
114: ByteArrayOutputStream bOut = new ByteArrayOutputStream();
115: DEROutputStream dOut = new DEROutputStream(bOut);
116:
117: try {
118: dOut.writeObject(this );
119: } catch (IOException e) {
120: throw new RuntimeException(e.getMessage(), e);
121: }
122:
123: return bOut.toByteArray();
124: }
125: }
|