1 19 20 package com.maverick.crypto.asn1.x509; 21 22 import java.util.Enumeration ; 23 import java.util.Hashtable ; 24 import java.util.Vector ; 25 26 import com.maverick.crypto.asn1.ASN1EncodableVector; 27 import com.maverick.crypto.asn1.ASN1OctetString; 28 import com.maverick.crypto.asn1.ASN1Sequence; 29 import com.maverick.crypto.asn1.ASN1TaggedObject; 30 import com.maverick.crypto.asn1.DERBoolean; 31 import com.maverick.crypto.asn1.DEREncodable; 32 import com.maverick.crypto.asn1.DERObject; 33 import com.maverick.crypto.asn1.DERObjectIdentifier; 34 import com.maverick.crypto.asn1.DERSequence; 35 36 public class X509Extensions 37 implements DEREncodable 38 { 39 42 public static final DERObjectIdentifier SubjectKeyIdentifier = new DERObjectIdentifier("2.5.29.14"); 43 44 47 public static final DERObjectIdentifier KeyUsage = new DERObjectIdentifier("2.5.29.15"); 48 49 52 public static final DERObjectIdentifier PrivateKeyUsagePeriod = new DERObjectIdentifier("2.5.29.16"); 53 54 57 public static final DERObjectIdentifier SubjectAlternativeName = new DERObjectIdentifier("2.5.29.17"); 58 59 62 public static final DERObjectIdentifier IssuerAlternativeName = new DERObjectIdentifier("2.5.29.18"); 63 64 67 public static final DERObjectIdentifier BasicConstraints = new DERObjectIdentifier("2.5.29.19"); 68 69 72 public static final DERObjectIdentifier CRLNumber = new DERObjectIdentifier("2.5.29.20"); 73 74 77 public static final DERObjectIdentifier ReasonCode = new DERObjectIdentifier("2.5.29.21"); 78 79 82 public static final DERObjectIdentifier InstructionCode = new DERObjectIdentifier("2.5.29.23"); 83 84 87 public static final DERObjectIdentifier InvalidityDate = new DERObjectIdentifier("2.5.29.24"); 88 89 92 public static final DERObjectIdentifier DeltaCRLIndicator = new DERObjectIdentifier("2.5.29.27"); 93 94 97 public static final DERObjectIdentifier IssuingDistributionPoint = new DERObjectIdentifier("2.5.29.28"); 98 99 102 public static final DERObjectIdentifier CertificateIssuer = new DERObjectIdentifier("2.5.29.29"); 103 104 107 public static final DERObjectIdentifier NameConstraints = new DERObjectIdentifier("2.5.29.30"); 108 109 112 public static final DERObjectIdentifier CRLDistributionPoints = new DERObjectIdentifier("2.5.29.31"); 113 114 117 public static final DERObjectIdentifier CertificatePolicies = new DERObjectIdentifier("2.5.29.32"); 118 119 122 public static final DERObjectIdentifier PolicyMappings = new DERObjectIdentifier("2.5.29.33"); 123 124 127 public static final DERObjectIdentifier AuthorityKeyIdentifier = new DERObjectIdentifier("2.5.29.35"); 128 129 132 public static final DERObjectIdentifier PolicyConstraints = new DERObjectIdentifier("2.5.29.36"); 133 134 137 public static final DERObjectIdentifier ExtendedKeyUsage = new DERObjectIdentifier("2.5.29.37"); 138 139 142 public static final DERObjectIdentifier InhibitAnyPolicy = new DERObjectIdentifier("2.5.29.54"); 143 144 147 public static final DERObjectIdentifier AuthorityInfoAccess= new DERObjectIdentifier("1.3.6.1.5.5.7.1.1"); 148 149 private Hashtable extensions = new Hashtable (); 150 private Vector ordering = new Vector (); 151 152 public static X509Extensions getInstance( 153 ASN1TaggedObject obj, 154 boolean explicit) 155 { 156 return getInstance(ASN1Sequence.getInstance(obj, explicit)); 157 } 158 159 public static X509Extensions getInstance( 160 Object obj) 161 { 162 if (obj == null || obj instanceof X509Extensions) 163 { 164 return (X509Extensions)obj; 165 } 166 167 if (obj instanceof ASN1Sequence) 168 { 169 return new X509Extensions((ASN1Sequence)obj); 170 } 171 172 if (obj instanceof ASN1TaggedObject) 173 { 174 return getInstance(((ASN1TaggedObject)obj).getObject()); 175 } 176 177 throw new IllegalArgumentException ("illegal object in getInstance: " + obj.getClass().getName()); 178 } 179 180 185 public X509Extensions( 186 ASN1Sequence seq) 187 { 188 Enumeration e = seq.getObjects(); 189 190 while (e.hasMoreElements()) 191 { 192 ASN1Sequence s = (ASN1Sequence)e.nextElement(); 193 194 if (s.size() == 3) 195 { 196 extensions.put(s.getObjectAt(0), new X509Extension((DERBoolean)s.getObjectAt(1), (ASN1OctetString)s.getObjectAt(2))); 197 } 198 else 199 { 200 extensions.put(s.getObjectAt(0), new X509Extension(false, (ASN1OctetString)s.getObjectAt(1))); 201 } 202 203 ordering.addElement(s.getObjectAt(0)); 204 } 205 } 206 207 212 public X509Extensions( 213 Hashtable extensions) 214 { 215 this(null, extensions); 216 } 217 218 223 public X509Extensions( 224 Vector ordering, 225 Hashtable extensions) 226 { 227 Enumeration e; 228 229 if (ordering == null) 230 { 231 e = extensions.keys(); 232 } 233 else 234 { 235 e = ordering.elements(); 236 } 237 238 while (e.hasMoreElements()) 239 { 240 this.ordering.addElement(e.nextElement()); 241 } 242 243 e = this.ordering.elements(); 244 245 while (e.hasMoreElements()) 246 { 247 DERObjectIdentifier oid = (DERObjectIdentifier)e.nextElement(); 248 X509Extension ext = (X509Extension)extensions.get(oid); 249 250 this.extensions.put(oid, ext); 251 } 252 } 253 254 257 public Enumeration oids() 258 { 259 return ordering.elements(); 260 } 261 262 268 public X509Extension getExtension( 269 DERObjectIdentifier oid) 270 { 271 return (X509Extension)extensions.get(oid); 272 } 273 274 public DERObject getDERObject() 275 { 276 ASN1EncodableVector vec = new ASN1EncodableVector(); 277 Enumeration e = ordering.elements(); 278 279 while (e.hasMoreElements()) 280 { 281 DERObjectIdentifier oid = (DERObjectIdentifier)e.nextElement(); 282 X509Extension ext = (X509Extension)extensions.get(oid); 283 ASN1EncodableVector v = new ASN1EncodableVector(); 284 285 v.add(oid); 286 287 if (ext.isCritical()) 288 { 289 v.add(new DERBoolean(true)); 290 } 291 292 v.add(ext.getValue()); 293 294 vec.add(new DERSequence(v)); 295 } 296 297 return new DERSequence(vec); 298 } 299 300 public int hashCode() 301 { 302 Enumeration e = extensions.keys(); 303 int hashCode = 0; 304 305 while (e.hasMoreElements()) 306 { 307 Object o = e.nextElement(); 308 309 hashCode ^= o.hashCode(); 310 hashCode ^= extensions.get(o).hashCode(); 311 } 312 313 return hashCode; 314 } 315 316 public boolean equals( 317 Object o) 318 { 319 if (o == null || !(o instanceof X509Extensions)) 320 { 321 return false; 322 } 323 324 X509Extensions other = (X509Extensions)o; 325 326 Enumeration e1 = extensions.keys(); 327 Enumeration e2 = other.extensions.keys(); 328 329 while (e1.hasMoreElements() && e2.hasMoreElements()) 330 { 331 Object o1 = e1.nextElement(); 332 Object o2 = e2.nextElement(); 333 334 if (!o1.equals(o2)) 335 { 336 return false; 337 } 338 } 339 340 if (e1.hasMoreElements() || e2.hasMoreElements()) 341 { 342 return false; 343 } 344 345 return true; 346 } 347 } 348 | Popular Tags |