1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43: import ;
44: import ;
45:
46: import ;
47: import ;
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53:
54: import ;
55:
56: public class GeneralNames
57: {
58:
59:
60:
61:
62: public static final int OTHER_NAME = 0;
63: public static final int RFC822_NAME = 1;
64: public static final int DNS_NAME = 2;
65: public static final int X400_ADDRESS = 3;
66: public static final int DIRECTORY_NAME = 4;
67: public static final int EDI_PARTY_NAME = 5;
68: public static final int URI = 6;
69: public static final int IP_ADDRESS = 7;
70: public static final int REGISTERED_ID = 8;
71:
72: private List names;
73:
74:
75:
76:
77: public GeneralNames(final byte[] encoded) throws IOException
78: {
79: names = new LinkedList();
80: DERReader der = new DERReader(encoded);
81: DERValue nameList = der.read();
82: if (!nameList.isConstructed())
83: throw new IOException("malformed GeneralNames");
84: int len = 0;
85: int i = 0;
86: while (len < nameList.getLength())
87: {
88: DERValue name = der.read();
89: List namePair = new ArrayList(2);
90: int tagClass = name.getTagClass();
91: if (tagClass != DER.CONTEXT)
92: throw new IOException("malformed GeneralName: Tag class is " + tagClass);
93: namePair.add(Integer.valueOf(name.getTag()));
94: DERValue val = null;
95: switch (name.getTag())
96: {
97: case RFC822_NAME:
98: case DNS_NAME:
99: case X400_ADDRESS:
100: case URI:
101: namePair.add(new String((byte[]) name.getValue()));
102: break;
103:
104: case OTHER_NAME:
105:
106: byte[] anotherName = name.getEncoded();
107: anotherName[0] = (byte) (DER.CONSTRUCTED|DER.SEQUENCE);
108: namePair.add(anotherName);
109:
110: DERValue skip = der.read();
111: skip = der.read();
112: break;
113:
114: case EDI_PARTY_NAME:
115: namePair.add(name.getValue());
116: break;
117:
118: case DIRECTORY_NAME:
119: byte[] b = name.getEncoded();
120: b[0] = (byte) (DER.CONSTRUCTED|DER.SEQUENCE);
121: DERReader r = new DERReader (b);
122: r.read ();
123: namePair.add(new X500Principal(r.read ().getEncoded ()).toString());
124: break;
125:
126: case IP_ADDRESS:
127: namePair.add(InetAddress.getByAddress((byte[]) name.getValue())
128: .getHostAddress());
129: break;
130:
131: case REGISTERED_ID:
132: byte[] bb = name.getEncoded();
133: bb[0] = (byte) DER.OBJECT_IDENTIFIER;
134: namePair.add(new OID(bb).toString());
135: break;
136:
137: default:
138: throw new IOException("unknown tag " + name.getTag());
139: }
140: names.add(namePair);
141: len += name.getEncodedLength();
142: }
143: }
144:
145:
146:
147:
148: public List getNames()
149: {
150: List l = new ArrayList(names.size());
151: for (Iterator it = names.iterator(); it.hasNext(); )
152: {
153: List ll = (List) it.next();
154: List pair = new ArrayList(2);
155: pair.add(ll.get(0));
156: if (ll.get(1) instanceof byte[])
157: pair.add(((byte[]) ll.get(1)).clone());
158: else
159: pair.add(ll.get(1));
160: l.add(Collections.unmodifiableList(pair));
161: }
162: return Collections.unmodifiableList(l);
163: }
164:
165: public String toString()
166: {
167: return GeneralNames.class.getName() + " [ " + names + " ]";
168: }
169: }