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