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.ASN1Object; 13 import org.bouncycastle.asn1.ASN1OctetString; 14 import org.bouncycastle.asn1.ASN1String; 15 import org.bouncycastle.asn1.DERObjectIdentifier; 16 import org.bouncycastle.asn1.DEROctetString; 17 import org.bouncycastle.asn1.DERSequence; 18 import org.bouncycastle.asn1.x509.GeneralName; 19 import org.bouncycastle.asn1.x509.X509Extensions; 20 import org.bouncycastle.asn1.x509.X509Name; 21 22 23 public class X509ExtensionUtil 24 { fromExtensionValue( byte[] encodedValue)25 public static ASN1Object fromExtensionValue( 26 byte[] encodedValue) 27 throws IOException 28 { 29 ASN1OctetString octs = (ASN1OctetString)ASN1Object.fromByteArray(encodedValue); 30 31 return ASN1Object.fromByteArray(octs.getOctets()); 32 } 33 getIssuerAlternativeNames(X509Certificate cert)34 public static Collection getIssuerAlternativeNames(X509Certificate cert) 35 throws CertificateParsingException 36 { 37 byte[] extVal = cert.getExtensionValue(X509Extensions.IssuerAlternativeName.getId()); 38 39 return getAlternativeNames(extVal); 40 } 41 getSubjectAlternativeNames(X509Certificate cert)42 public static Collection getSubjectAlternativeNames(X509Certificate cert) 43 throws CertificateParsingException 44 { 45 byte[] extVal = cert.getExtensionValue(X509Extensions.SubjectAlternativeName.getId()); 46 47 return getAlternativeNames(extVal); 48 } 49 getAlternativeNames(byte[] extVal)50 private static Collection getAlternativeNames(byte[] extVal) 51 throws CertificateParsingException 52 { 53 if (extVal == null) 54 { 55 return Collections.EMPTY_LIST; 56 } 57 try 58 { 59 Collection temp = new ArrayList(); 60 Enumeration it = DERSequence.getInstance(fromExtensionValue(extVal)).getObjects(); 61 while (it.hasMoreElements()) 62 { 63 GeneralName genName = GeneralName.getInstance(it.nextElement()); 64 List list = new ArrayList(); 65 // BEGIN android-changed 66 list.add(Integer.valueOf(genName.getTagNo())); 67 // END android-changed 68 switch (genName.getTagNo()) 69 { 70 case GeneralName.ediPartyName: 71 case GeneralName.x400Address: 72 case GeneralName.otherName: 73 list.add(genName.getName().getDERObject()); 74 break; 75 case GeneralName.directoryName: 76 list.add(X509Name.getInstance(genName.getName()).toString()); 77 break; 78 case GeneralName.dNSName: 79 case GeneralName.rfc822Name: 80 case GeneralName.uniformResourceIdentifier: 81 list.add(((ASN1String)genName.getName()).getString()); 82 break; 83 case GeneralName.registeredID: 84 list.add(DERObjectIdentifier.getInstance(genName.getName()).getId()); 85 break; 86 case GeneralName.iPAddress: 87 list.add(DEROctetString.getInstance(genName.getName()).getOctets()); 88 break; 89 default: 90 throw new IOException("Bad tag number: " + genName.getTagNo()); 91 } 92 93 temp.add(list); 94 } 95 return Collections.unmodifiableCollection(temp); 96 } 97 catch (Exception e) 98 { 99 throw new CertificateParsingException(e.getMessage()); 100 } 101 } 102 } 103