1 package org.bouncycastle.jce; 2 3 import java.io.IOException; 4 import java.security.Principal; 5 import java.util.Hashtable; 6 import java.util.Vector; 7 8 import org.bouncycastle.asn1.ASN1Encodable; 9 import org.bouncycastle.asn1.ASN1InputStream; 10 import org.bouncycastle.asn1.ASN1Sequence; 11 import org.bouncycastle.asn1.x509.X509Name; 12 13 /** 14 * a general extension of X509Name with a couple of extra methods and 15 * constructors. 16 * <p> 17 * Objects of this type can be created from certificates and CRLs using the 18 * PrincipalUtil class. 19 * </p> 20 * @see org.bouncycastle.jce.PrincipalUtil 21 */ 22 public class X509Principal 23 extends X509Name 24 implements Principal 25 { 26 private static ASN1Sequence readSequence( 27 ASN1InputStream aIn) 28 throws IOException 29 { 30 try 31 { 32 return ASN1Sequence.getInstance(aIn.readObject()); 33 } 34 catch (IllegalArgumentException e) 35 { 36 throw new IOException("not an ASN.1 Sequence: " + e); 37 } 38 } 39 40 /** 41 * Constructor from an encoded byte array. 42 */ 43 public X509Principal( 44 byte[] bytes) 45 throws IOException 46 { 47 super(readSequence(new ASN1InputStream(bytes))); 48 } 49 50 /** 51 * Constructor from an X509Name object. 52 */ 53 public X509Principal( 54 X509Name name) 55 { 56 super((ASN1Sequence)name.getDERObject()); 57 } 58 59 /** 60 * constructor from a table of attributes. 61 * <p> 62 * it's is assumed the table contains OID/String pairs. 63 */ 64 public X509Principal( 65 Hashtable attributes) 66 { 67 super(attributes); 68 } 69 70 /** 71 * constructor from a table of attributes and a vector giving the 72 * specific ordering required for encoding or conversion to a string. 73 * <p> 74 * it's is assumed the table contains OID/String pairs. 75 */ 76 public X509Principal( 77 Vector ordering, 78 Hashtable attributes) 79 { 80 super(ordering, attributes); 81 } 82 83 /** 84 * constructor from a vector of attribute values and a vector of OIDs. 85 */ 86 public X509Principal( 87 Vector oids, 88 Vector values) 89 { 90 super(oids, values); 91 } 92 93 /** 94 * takes an X509 dir name as a string of the format "C=AU,ST=Victoria", or 95 * some such, converting it into an ordered set of name attributes. 96 */ 97 public X509Principal( 98 String dirName) 99 { 100 super(dirName); 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. If reverse 106 * is false the dir name will be encoded in the order of the (name, value) pairs 107 * presented, otherwise the encoding will start with the last (name, value) pair 108 * and work back. 109 */ 110 public X509Principal( 111 boolean reverse, 112 String dirName) 113 { 114 super(reverse, dirName); 115 } 116 117 /** 118 * Takes an X509 dir name as a string of the format "C=AU, ST=Victoria", or 119 * some such, converting it into an ordered set of name attributes. lookUp 120 * should provide a table of lookups, indexed by lowercase only strings and 121 * yielding a DERObjectIdentifier, other than that OID. and numeric oids 122 * will be processed automatically. 123 * <p> 124 * If reverse is true, create the encoded version of the sequence starting 125 * from the last element in the string. 126 */ 127 public X509Principal( 128 boolean reverse, 129 Hashtable lookUp, 130 String dirName) 131 { 132 super(reverse, lookUp, dirName); 133 } 134 135 public String getName() 136 { 137 return this.toString(); 138 } 139 140 /** 141 * return a DER encoded byte array representing this object 142 */ 143 public byte[] getEncoded() 144 { 145 try 146 { 147 return this.getEncoded(ASN1Encodable.DER); 148 } 149 catch (IOException e) 150 { 151 throw new RuntimeException(e.toString()); 152 } 153 } 154 } 155