1 package org.bouncycastle.x509.extension; 2 3 import java.io.IOException; 4 import java.security.cert.CertificateParsingException; 5 import java.security.cert.X509Certificate; 6 import java.util.ArrayList; 7 import java.util.Collection; 8 import java.util.Collections; 9 import java.util.Enumeration; 10 import java.util.List; 11 12 import org.bouncycastle.asn1.ASN1ObjectIdentifier; 13 import org.bouncycastle.asn1.ASN1OctetString; 14 import org.bouncycastle.asn1.ASN1Primitive; 15 import org.bouncycastle.asn1.ASN1String; 16 import org.bouncycastle.asn1.DEROctetString; 17 import org.bouncycastle.asn1.DERSequence; 18 import org.bouncycastle.asn1.x500.X500Name; 19 import org.bouncycastle.asn1.x509.GeneralName; 20 import org.bouncycastle.asn1.x509.X509Extension; 21 import org.bouncycastle.util.Integers; 22 23 24 public class X509ExtensionUtil 25 { 26 public static ASN1Primitive fromExtensionValue( 27 byte[] encodedValue) 28 throws IOException 29 { 30 ASN1OctetString octs = (ASN1OctetString)ASN1Primitive.fromByteArray(encodedValue); 31 32 return ASN1Primitive.fromByteArray(octs.getOctets()); 33 } 34 35 public static Collection getIssuerAlternativeNames(X509Certificate cert) 36 throws CertificateParsingException 37 { 38 byte[] extVal = cert.getExtensionValue(X509Extension.issuerAlternativeName.getId()); 39 40 return getAlternativeNames(extVal); 41 } 42 43 public static Collection getSubjectAlternativeNames(X509Certificate cert) 44 throws CertificateParsingException 45 { 46 byte[] extVal = cert.getExtensionValue(X509Extension.subjectAlternativeName.getId()); 47 48 return getAlternativeNames(extVal); 49 } 50 51 private static Collection getAlternativeNames(byte[] extVal) 52 throws CertificateParsingException 53 { 54 if (extVal == null) 55 { 56 return Collections.EMPTY_LIST; 57 } 58 try 59 { 60 Collection temp = new ArrayList(); 61 Enumeration it = DERSequence.getInstance(fromExtensionValue(extVal)).getObjects(); 62 while (it.hasMoreElements()) 63 { 64 GeneralName genName = GeneralName.getInstance(it.nextElement()); 65 List list = new ArrayList(); 66 list.add(Integers.valueOf(genName.getTagNo())); 67 switch (genName.getTagNo()) 68 { 69 case GeneralName.ediPartyName: 70 case GeneralName.x400Address: 71 case GeneralName.otherName: 72 list.add(genName.getName().toASN1Primitive()); 73 break; 74 case GeneralName.directoryName: 75 list.add(X500Name.getInstance(genName.getName()).toString()); 76 break; 77 case GeneralName.dNSName: 78 case GeneralName.rfc822Name: 79 case GeneralName.uniformResourceIdentifier: 80 list.add(((ASN1String)genName.getName()).getString()); 81 break; 82 case GeneralName.registeredID: 83 list.add(ASN1ObjectIdentifier.getInstance(genName.getName()).getId()); 84 break; 85 case GeneralName.iPAddress: 86 list.add(DEROctetString.getInstance(genName.getName()).getOctets()); 87 break; 88 default: 89 throw new IOException("Bad tag number: " + genName.getTagNo()); 90 } 91 92 temp.add(list); 93 } 94 return Collections.unmodifiableCollection(temp); 95 } 96 catch (Exception e) 97 { 98 throw new CertificateParsingException(e.getMessage()); 99 } 100 } 101 } 102