// 
// Decompiled by Procyon v0.6.0
// 

package org.bouncycastle.cert.jcajce;

import java.io.OutputStream;
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import java.io.ByteArrayOutputStream;
import java.util.Enumeration;
import java.net.UnknownHostException;
import java.net.InetAddress;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.asn1.ASN1String;
import org.bouncycastle.asn1.x500.style.RFC4519Style;
import org.bouncycastle.util.Integers;
import org.bouncycastle.asn1.ASN1Sequence;
import java.util.ArrayList;
import java.util.Collections;
import java.security.cert.CertificateParsingException;
import org.bouncycastle.asn1.x509.Extension;
import java.util.Collection;
import java.io.IOException;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.x509.SubjectKeyIdentifier;
import org.bouncycastle.asn1.x509.GeneralNames;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x500.X500Name;
import java.math.BigInteger;
import javax.security.auth.x500.X500Principal;
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
import java.security.PublicKey;
import java.security.cert.CertificateEncodingException;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.asn1.x509.AuthorityKeyIdentifier;
import java.security.cert.X509Certificate;
import java.security.NoSuchAlgorithmException;
import org.bouncycastle.operator.DigestCalculator;
import java.security.MessageDigest;
import org.bouncycastle.cert.X509ExtensionUtils;

public class JcaX509ExtensionUtils extends X509ExtensionUtils
{
    public JcaX509ExtensionUtils() throws NoSuchAlgorithmException {
        super(new SHA1DigestCalculator(MessageDigest.getInstance("SHA1")));
    }
    
    public JcaX509ExtensionUtils(final DigestCalculator digestCalculator) {
        super(digestCalculator);
    }
    
    public AuthorityKeyIdentifier createAuthorityKeyIdentifier(final X509Certificate x509Certificate) throws CertificateEncodingException {
        return super.createAuthorityKeyIdentifier(new JcaX509CertificateHolder(x509Certificate));
    }
    
    public AuthorityKeyIdentifier createAuthorityKeyIdentifier(final PublicKey publicKey) {
        return super.createAuthorityKeyIdentifier(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()));
    }
    
    public AuthorityKeyIdentifier createAuthorityKeyIdentifier(final PublicKey publicKey, final X500Principal x500Principal, final BigInteger bigInteger) {
        return super.createAuthorityKeyIdentifier(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()), new GeneralNames(new GeneralName(X500Name.getInstance(x500Principal.getEncoded()))), bigInteger);
    }
    
    public AuthorityKeyIdentifier createAuthorityKeyIdentifier(final PublicKey publicKey, final GeneralNames generalNames, final BigInteger bigInteger) {
        return super.createAuthorityKeyIdentifier(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()), generalNames, bigInteger);
    }
    
    public SubjectKeyIdentifier createSubjectKeyIdentifier(final PublicKey publicKey) {
        return super.createSubjectKeyIdentifier(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()));
    }
    
    public SubjectKeyIdentifier createTruncatedSubjectKeyIdentifier(final PublicKey publicKey) {
        return super.createTruncatedSubjectKeyIdentifier(SubjectPublicKeyInfo.getInstance(publicKey.getEncoded()));
    }
    
    public static ASN1Primitive parseExtensionValue(final byte[] array) throws IOException {
        return ASN1Primitive.fromByteArray(ASN1OctetString.getInstance(array).getOctets());
    }
    
    public static Collection getIssuerAlternativeNames(final X509Certificate x509Certificate) throws CertificateParsingException {
        return getAlternativeNames(x509Certificate.getExtensionValue(Extension.issuerAlternativeName.getId()));
    }
    
    public static Collection getSubjectAlternativeNames(final X509Certificate x509Certificate) throws CertificateParsingException {
        return getAlternativeNames(x509Certificate.getExtensionValue(Extension.subjectAlternativeName.getId()));
    }
    
    private static Collection getAlternativeNames(final byte[] array) throws CertificateParsingException {
        if (array == null) {
            return Collections.EMPTY_LIST;
        }
        try {
            final ArrayList c = new ArrayList();
            final Enumeration objects = ASN1Sequence.getInstance(parseExtensionValue(array)).getObjects();
            while (objects.hasMoreElements()) {
                final GeneralName instance = GeneralName.getInstance(objects.nextElement());
                final ArrayList list = new ArrayList();
                list.add(Integers.valueOf(instance.getTagNo()));
                switch (instance.getTagNo()) {
                    case 0:
                    case 3:
                    case 5: {
                        list.add(instance.getEncoded());
                        break;
                    }
                    case 4: {
                        list.add(X500Name.getInstance(RFC4519Style.INSTANCE, instance.getName()).toString());
                        break;
                    }
                    case 1:
                    case 2:
                    case 6: {
                        list.add(((ASN1String)instance.getName()).getString());
                        break;
                    }
                    case 8: {
                        list.add(ASN1ObjectIdentifier.getInstance(instance.getName()).getId());
                        break;
                    }
                    case 7: {
                        final byte[] octets = ASN1OctetString.getInstance(instance.getName()).getOctets();
                        String hostAddress;
                        try {
                            hostAddress = InetAddress.getByAddress(octets).getHostAddress();
                        }
                        catch (final UnknownHostException ex) {
                            continue;
                        }
                        list.add(hostAddress);
                        break;
                    }
                    default: {
                        throw new IOException("Bad tag number: " + instance.getTagNo());
                    }
                }
                c.add(list);
            }
            return Collections.unmodifiableCollection((Collection<?>)c);
        }
        catch (final Exception ex2) {
            throw new CertificateParsingException(ex2.getMessage());
        }
    }
    
    private static class SHA1DigestCalculator implements DigestCalculator
    {
        private ByteArrayOutputStream bOut;
        private MessageDigest digest;
        
        public SHA1DigestCalculator(final MessageDigest digest) {
            this.bOut = new ByteArrayOutputStream();
            this.digest = digest;
        }
        
        @Override
        public AlgorithmIdentifier getAlgorithmIdentifier() {
            return new AlgorithmIdentifier(OIWObjectIdentifiers.idSHA1);
        }
        
        @Override
        public OutputStream getOutputStream() {
            return this.bOut;
        }
        
        @Override
        public byte[] getDigest() {
            final byte[] digest = this.digest.digest(this.bOut.toByteArray());
            this.bOut.reset();
            return digest;
        }
    }
}
