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

package org.bouncycastle.asn1.bc;

import org.bouncycastle.asn1.DERSequence;
import org.bouncycastle.asn1.DERTaggedObject;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.x509.GeneralNames;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.asn1.x509.DigestInfo;
import org.bouncycastle.asn1.ASN1Object;

public class LinkedCertificate extends ASN1Object
{
    private final DigestInfo digest;
    private final GeneralName certLocation;
    private X500Name certIssuer;
    private GeneralNames cACerts;
    
    public LinkedCertificate(final DigestInfo digestInfo, final GeneralName generalName) {
        this(digestInfo, generalName, null, null);
    }
    
    public LinkedCertificate(final DigestInfo digest, final GeneralName certLocation, final X500Name certIssuer, final GeneralNames caCerts) {
        this.digest = digest;
        this.certLocation = certLocation;
        this.certIssuer = certIssuer;
        this.cACerts = caCerts;
    }
    
    private LinkedCertificate(final ASN1Sequence asn1Sequence) {
        this.digest = DigestInfo.getInstance(asn1Sequence.getObjectAt(0));
        this.certLocation = GeneralName.getInstance(asn1Sequence.getObjectAt(1));
        if (asn1Sequence.size() > 2) {
            for (int i = 2; i != asn1Sequence.size(); ++i) {
                final ASN1TaggedObject instance = ASN1TaggedObject.getInstance(asn1Sequence.getObjectAt(i));
                switch (instance.getTagNo()) {
                    case 0: {
                        this.certIssuer = X500Name.getInstance(instance, true);
                        break;
                    }
                    case 1: {
                        this.cACerts = GeneralNames.getInstance(instance, false);
                        break;
                    }
                    default: {
                        throw new IllegalArgumentException("unknown tag in tagged field");
                    }
                }
            }
        }
    }
    
    public static LinkedCertificate getInstance(final Object o) {
        if (o instanceof LinkedCertificate) {
            return (LinkedCertificate)o;
        }
        if (o != null) {
            return new LinkedCertificate(ASN1Sequence.getInstance(o));
        }
        return null;
    }
    
    public DigestInfo getDigest() {
        return this.digest;
    }
    
    public GeneralName getCertLocation() {
        return this.certLocation;
    }
    
    public X500Name getCertIssuer() {
        return this.certIssuer;
    }
    
    public GeneralNames getCACerts() {
        return this.cACerts;
    }
    
    @Override
    public ASN1Primitive toASN1Primitive() {
        final ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector(4);
        asn1EncodableVector.add(this.digest);
        asn1EncodableVector.add(this.certLocation);
        if (this.certIssuer != null) {
            asn1EncodableVector.add(new DERTaggedObject(true, 0, this.certIssuer));
        }
        if (this.cACerts != null) {
            asn1EncodableVector.add(new DERTaggedObject(false, 1, this.cACerts));
        }
        return new DERSequence(asn1EncodableVector);
    }
}
