1: 
  37: 
  38: 
  39: package ;
  40: 
  41: import ;
  42: import ;
  43: import ;
  44: import ;
  45: 
  46: import ;
  47: import ;
  48: 
  49: 
  75: public class GeneralName
  76: {
  77:   public static enum Kind
  78:   {
  79:     otherName (0),
  80:     rfc822Name (1),
  81:     dNSName (2),
  82:     x400Address (3),
  83:     directoryName (4),
  84:     ediPartyName (5),
  85:     uniformResourceIdentifier (6),
  86:     iPAddress (7),
  87:     registeredId (8);
  88: 
  89:     private int tag;
  90: 
  91:     private Kind(int tag)
  92:     {
  93:       this.tag = tag;
  94:     }
  95: 
  96:     public static Kind forTag(final int tag)
  97:     {
  98:       switch (tag)
  99:       {
 100:         case 0: return otherName;
 101:         case 1: return rfc822Name;
 102:         case 2: return dNSName;
 103:         case 3: return x400Address;
 104:         case 4: return directoryName;
 105:         case 5: return ediPartyName;
 106:         case 6: return uniformResourceIdentifier;
 107:         case 7: return iPAddress;
 108:         case 8: return registeredId;
 109:       }
 110: 
 111:       throw new IllegalArgumentException("invalid tag: " + tag);
 112:     }
 113: 
 114:     public int tag()
 115:     {
 116:       return tag;
 117:     }
 118:   };
 119: 
 120:   private final Kind kind;
 121:   private final byte[] name;
 122:   private final byte[] encoded;
 123: 
 124:   public GeneralName(byte[] encoded) throws IOException
 125:   {
 126:     DERReader reader = new DERReader(encoded);
 127:     DERValue value = reader.read();
 128: 
 129:     if (value.getTagClass() != DER.CONTEXT)
 130:       throw new IOException("malformed GeneralName");
 131: 
 132:     this.encoded = value.getEncoded();
 133: 
 134:     kind = Kind.forTag(value.getTag());
 135:     switch (kind)
 136:     {
 137:       case otherName:
 138:         name = value.getEncoded();
 139:         name[0] = (byte) (DER.CONSTRUCTED | DER.SEQUENCE);
 140:         
 141:         reader.read();  
 142:         reader.read();  
 143:         break;
 144: 
 145:       case rfc822Name:
 146:         name = (byte[]) value.getValue();
 147:         break;
 148: 
 149:       case dNSName:
 150:         name = (byte[]) value.getValue();
 151:         break;
 152: 
 153:       case x400Address:
 154:         name = (byte[]) value.getValue();
 155:         break;
 156: 
 157:       case directoryName:
 158:         name = value.getEncoded();
 159:         name[0] = (byte) (DER.CONSTRUCTED | DER.SEQUENCE);
 160:         break;
 161: 
 162:       case ediPartyName:
 163:         name = value.getEncoded();
 164:         name[0] = (byte) (DER.CONSTRUCTED | DER.SEQUENCE);
 165:         break;
 166: 
 167:       case uniformResourceIdentifier:
 168:         name = (byte[]) value.getValue();
 169:         break;
 170: 
 171:       case iPAddress:
 172:         name = (byte[]) value.getValue();
 173:         break;
 174: 
 175:       case registeredId:
 176:         name = value.getEncoded();
 177:         name[0] = DER.OBJECT_IDENTIFIER;
 178:         break;
 179: 
 180:       default:
 181:         name = null; 
 182:     }
 183:   }
 184: 
 185:   public GeneralName(Kind kind, byte[] name)
 186:   {
 187:     this.kind = kind;
 188:     this.name = (byte[]) name.clone();
 189:     this.encoded = null;
 190:   }
 191: 
 192:   public Kind kind()
 193:   {
 194:     return kind;
 195:   }
 196: 
 197:   public byte[] name()
 198:   {
 199:     return (byte[]) name.clone();
 200:   }
 201: 
 202:   public byte[] encoded()
 203:   {
 204:     try
 205:       {
 206:         return (byte[]) encoded.clone();
 207:       }
 208:     catch (NullPointerException npe)
 209:       {
 210:         return null;
 211:       }
 212:   }
 213: 
 214:   public boolean equals(Object o)
 215:   {
 216:     try
 217:       {
 218:         GeneralName that = (GeneralName) o;
 219:         return (that.kind() == kind() && Arrays.equals(name, that.name));
 220:       }
 221:     catch (ClassCastException cce)
 222:       {
 223:         return false;
 224:       }
 225:   }
 226: 
 227:   public String toString()
 228:   {
 229:     return (super.toString() + " [ kind=" + kind + "; name=" +
 230:             Util.hexDump(name, "") + " ]");
 231:   }
 232: }