001: package org.bouncycastle.x509.extension;
002:
003: import org.bouncycastle.asn1.ASN1Encodable;
004: import org.bouncycastle.asn1.ASN1Object;
005: import org.bouncycastle.asn1.ASN1OctetString;
006: import org.bouncycastle.asn1.DERObjectIdentifier;
007: import org.bouncycastle.asn1.DEROctetString;
008: import org.bouncycastle.asn1.DERSequence;
009: import org.bouncycastle.asn1.DERString;
010: import org.bouncycastle.asn1.x509.GeneralName;
011: import org.bouncycastle.asn1.x509.X509Extensions;
012: import org.bouncycastle.asn1.x509.X509Name;
013:
014: import java.io.IOException;
015: import java.security.cert.CertificateParsingException;
016: import java.security.cert.X509Certificate;
017: import java.util.ArrayList;
018: import java.util.Collection;
019: import java.util.Collections;
020: import java.util.Enumeration;
021: import java.util.List;
022:
023: public class X509ExtensionUtil {
024: public static ASN1Encodable fromExtensionValue(byte[] encodedValue)
025: throws IOException {
026: ASN1OctetString octs = (ASN1OctetString) ASN1Object
027: .fromByteArray(encodedValue);
028:
029: return ASN1Object.fromByteArray(octs.getOctets());
030: }
031:
032: private static Collection getIssuerAlternativeNames(
033: X509Certificate cert) throws CertificateParsingException {
034: byte[] extVal = cert
035: .getExtensionValue(X509Extensions.IssuerAlternativeName
036: .getId());
037:
038: return getAlternativeName(extVal);
039: }
040:
041: private static Collection getSubjectAlternativeNames(
042: X509Certificate cert) throws CertificateParsingException {
043: byte[] extVal = cert
044: .getExtensionValue(X509Extensions.SubjectAlternativeName
045: .getId());
046:
047: return getAlternativeName(extVal);
048: }
049:
050: private static Collection getAlternativeName(byte[] extVal)
051: throws CertificateParsingException {
052: Collection temp = new ArrayList();
053: if (extVal == null) {
054: return Collections.EMPTY_LIST;
055: }
056: try {
057: byte[] extnValue = DEROctetString.getInstance(
058: ASN1Object.fromByteArray(extVal)).getOctets();
059: Enumeration it = DERSequence.getInstance(
060: ASN1Object.fromByteArray(extnValue)).getObjects();
061: while (it.hasMoreElements()) {
062: GeneralName genName = GeneralName.getInstance(it
063: .nextElement());
064: List list = new ArrayList();
065: list.add(new Integer(genName.getTagNo()));
066: switch (genName.getTagNo()) {
067: case GeneralName.ediPartyName:
068: case GeneralName.x400Address:
069: case GeneralName.otherName:
070: list.add(genName.getName().getDERObject());
071: break;
072: case GeneralName.directoryName:
073: list.add(X509Name.getInstance(genName.getName())
074: .toString());
075: break;
076: case GeneralName.dNSName:
077: case GeneralName.rfc822Name:
078: case GeneralName.uniformResourceIdentifier:
079: list.add(((DERString) genName.getName())
080: .getString());
081: break;
082: case GeneralName.registeredID:
083: list.add(DERObjectIdentifier.getInstance(
084: genName.getName()).getId());
085: break;
086: case GeneralName.iPAddress:
087: list.add(DEROctetString.getInstance(
088: genName.getName()).getOctets());
089: break;
090: default:
091: throw new IOException("Bad tag number: "
092: + genName.getTagNo());
093: }
094:
095: temp.add(list);
096: }
097: } catch (Exception e) {
098: throw new CertificateParsingException(e.getMessage());
099: }
100: return Collections.unmodifiableCollection(temp);
101: }
102: }
|