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

package org.bouncycastle.jcajce.provider.asymmetric.x509;

import org.bouncycastle.asn1.x509.IssuingDistributionPoint;
import org.bouncycastle.asn1.x509.Extension;
import org.bouncycastle.asn1.ASN1Encodable;
import java.io.IOException;
import org.bouncycastle.asn1.ASN1BitString;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.util.Arrays;
import java.security.cert.CRLException;
import org.bouncycastle.asn1.x509.CertificateList;
import org.bouncycastle.jcajce.util.JcaJceHelper;

class X509CRLObject extends X509CRLImpl
{
    private final Object cacheLock;
    private X509CRLInternal internalCRLValue;
    private volatile boolean hashValueSet;
    private volatile int hashValue;
    
    X509CRLObject(final JcaJceHelper jcaJceHelper, final CertificateList list) throws CRLException {
        super(jcaJceHelper, list, createSigAlgName(list), createSigAlgParams(list), isIndirectCRL(list));
        this.cacheLock = new Object();
    }
    
    @Override
    public byte[] getEncoded() throws CRLException {
        return Arrays.clone(this.getInternalCRL().getEncoded());
    }
    
    @Override
    public boolean equals(final Object other) {
        if (this == other) {
            return true;
        }
        if (other instanceof X509CRLObject) {
            final X509CRLObject x509CRLObject = (X509CRLObject)other;
            if (this.hashValueSet && x509CRLObject.hashValueSet) {
                if (this.hashValue != x509CRLObject.hashValue) {
                    return false;
                }
            }
            else if (null == this.internalCRLValue || null == x509CRLObject.internalCRLValue) {
                final ASN1BitString signature = this.c.getSignature();
                if (null != signature && !signature.equals(x509CRLObject.c.getSignature())) {
                    return false;
                }
            }
            return this.getInternalCRL().equals(x509CRLObject.getInternalCRL());
        }
        return this.getInternalCRL().equals(other);
    }
    
    @Override
    public int hashCode() {
        if (!this.hashValueSet) {
            this.hashValue = this.getInternalCRL().hashCode();
            this.hashValueSet = true;
        }
        return this.hashValue;
    }
    
    private X509CRLInternal getInternalCRL() {
        synchronized (this.cacheLock) {
            if (null != this.internalCRLValue) {
                return this.internalCRLValue;
            }
        }
        byte[] encoded = null;
        CRLException ex = null;
        try {
            encoded = this.c.getEncoded("DER");
        }
        catch (final IOException ex2) {
            ex = new X509CRLException(ex2);
        }
        final X509CRLInternal internalCRLValue = new X509CRLInternal(this.bcHelper, this.c, this.sigAlgName, this.sigAlgParams, this.isIndirect, encoded, ex);
        synchronized (this.cacheLock) {
            if (null == this.internalCRLValue) {
                this.internalCRLValue = internalCRLValue;
            }
            return this.internalCRLValue;
        }
    }
    
    private static String createSigAlgName(final CertificateList list) throws CRLException {
        try {
            return X509SignatureUtil.getSignatureName(list.getSignatureAlgorithm());
        }
        catch (final Exception ex) {
            throw new X509CRLException("CRL contents invalid: " + ex.getMessage(), ex);
        }
    }
    
    private static byte[] createSigAlgParams(final CertificateList list) throws CRLException {
        try {
            final ASN1Encodable parameters = list.getSignatureAlgorithm().getParameters();
            if (null == parameters) {
                return null;
            }
            return parameters.toASN1Primitive().getEncoded("DER");
        }
        catch (final Exception obj) {
            throw new CRLException("CRL contents invalid: " + obj);
        }
    }
    
    private static boolean isIndirectCRL(final CertificateList list) throws CRLException {
        try {
            final byte[] extensionOctets = X509CRLImpl.getExtensionOctets(list, Extension.issuingDistributionPoint);
            return null != extensionOctets && IssuingDistributionPoint.getInstance(extensionOctets).isIndirectCRL();
        }
        catch (final Exception ex) {
            throw new ExtCRLException("Exception reading IssuingDistributionPoint", ex);
        }
    }
    
    private static class X509CRLException extends CRLException
    {
        private final Throwable cause;
        
        X509CRLException(final String message, final Throwable cause) {
            super(message);
            this.cause = cause;
        }
        
        X509CRLException(final Throwable cause) {
            this.cause = cause;
        }
        
        @Override
        public Throwable getCause() {
            return this.cause;
        }
    }
}
