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

package org.bouncycastle.asn1.cms;

import org.bouncycastle.asn1.BERSequence;
import org.bouncycastle.asn1.DERTaggedObject;
import org.bouncycastle.asn1.ASN1EncodableVector;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.ASN1Set;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1Object;

public class EnvelopedData extends ASN1Object
{
    private ASN1Integer version;
    private OriginatorInfo originatorInfo;
    private ASN1Set recipientInfos;
    private EncryptedContentInfo encryptedContentInfo;
    private ASN1Set unprotectedAttrs;
    
    public EnvelopedData(final OriginatorInfo originatorInfo, final ASN1Set recipientInfos, final EncryptedContentInfo encryptedContentInfo, final ASN1Set unprotectedAttrs) {
        this.version = new ASN1Integer(calculateVersion(originatorInfo, recipientInfos, unprotectedAttrs));
        this.originatorInfo = originatorInfo;
        this.recipientInfos = recipientInfos;
        this.encryptedContentInfo = encryptedContentInfo;
        this.unprotectedAttrs = unprotectedAttrs;
    }
    
    public EnvelopedData(final OriginatorInfo originatorInfo, final ASN1Set recipientInfos, final EncryptedContentInfo encryptedContentInfo, final Attributes attributes) {
        this.version = new ASN1Integer(calculateVersion(originatorInfo, recipientInfos, ASN1Set.getInstance(attributes)));
        this.originatorInfo = originatorInfo;
        this.recipientInfos = recipientInfos;
        this.encryptedContentInfo = encryptedContentInfo;
        this.unprotectedAttrs = ASN1Set.getInstance(attributes);
    }
    
    private EnvelopedData(final ASN1Sequence asn1Sequence) {
        int n = 0;
        this.version = (ASN1Integer)asn1Sequence.getObjectAt(n++);
        ASN1Encodable asn1Encodable = asn1Sequence.getObjectAt(n++);
        if (asn1Encodable instanceof ASN1TaggedObject) {
            this.originatorInfo = OriginatorInfo.getInstance((ASN1TaggedObject)asn1Encodable, false);
            asn1Encodable = asn1Sequence.getObjectAt(n++);
        }
        this.recipientInfos = ASN1Set.getInstance(asn1Encodable);
        this.encryptedContentInfo = EncryptedContentInfo.getInstance(asn1Sequence.getObjectAt(n++));
        if (asn1Sequence.size() > n) {
            this.unprotectedAttrs = ASN1Set.getInstance((ASN1TaggedObject)asn1Sequence.getObjectAt(n), false);
        }
    }
    
    public static EnvelopedData getInstance(final ASN1TaggedObject asn1TaggedObject, final boolean b) {
        return getInstance(ASN1Sequence.getInstance(asn1TaggedObject, b));
    }
    
    public static EnvelopedData getInstance(final Object o) {
        if (o instanceof EnvelopedData) {
            return (EnvelopedData)o;
        }
        if (o != null) {
            return new EnvelopedData(ASN1Sequence.getInstance(o));
        }
        return null;
    }
    
    public ASN1Integer getVersion() {
        return this.version;
    }
    
    public OriginatorInfo getOriginatorInfo() {
        return this.originatorInfo;
    }
    
    public ASN1Set getRecipientInfos() {
        return this.recipientInfos;
    }
    
    public EncryptedContentInfo getEncryptedContentInfo() {
        return this.encryptedContentInfo;
    }
    
    public ASN1Set getUnprotectedAttrs() {
        return this.unprotectedAttrs;
    }
    
    @Override
    public ASN1Primitive toASN1Primitive() {
        final ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector(5);
        asn1EncodableVector.add(this.version);
        if (this.originatorInfo != null) {
            asn1EncodableVector.add(new DERTaggedObject(false, 0, this.originatorInfo));
        }
        asn1EncodableVector.add(this.recipientInfos);
        asn1EncodableVector.add(this.encryptedContentInfo);
        if (this.unprotectedAttrs != null) {
            asn1EncodableVector.add(new DERTaggedObject(false, 1, this.unprotectedAttrs));
        }
        return new BERSequence(asn1EncodableVector);
    }
    
    public static int calculateVersion(final OriginatorInfo originatorInfo, final ASN1Set set, final ASN1Set set2) {
        if (originatorInfo != null) {
            final ASN1Set crLs = originatorInfo.getCRLs();
            if (crLs != null) {
                for (int i = 0; i < crLs.size(); ++i) {
                    final ASN1Encodable object = crLs.getObjectAt(i);
                    if (object instanceof ASN1TaggedObject && ((ASN1TaggedObject)object).hasContextTag(1)) {
                        return 4;
                    }
                }
            }
            final ASN1Set certificates = originatorInfo.getCertificates();
            if (certificates != null) {
                boolean b = false;
                for (int j = 0; j < certificates.size(); ++j) {
                    final ASN1Encodable object2 = certificates.getObjectAt(j);
                    if (object2 instanceof ASN1TaggedObject) {
                        final ASN1TaggedObject asn1TaggedObject = (ASN1TaggedObject)object2;
                        if (asn1TaggedObject.hasContextTag(3)) {
                            return 4;
                        }
                        b = (b || asn1TaggedObject.hasContextTag(2));
                    }
                }
                if (b) {
                    return 3;
                }
            }
        }
        boolean b2 = true;
        for (int k = 0; k < set.size(); ++k) {
            final RecipientInfo instance = RecipientInfo.getInstance(set.getObjectAt(k));
            if (instance.isPasswordOrOther()) {
                return 3;
            }
            b2 = (b2 && instance.isKeyTransV0());
        }
        if (originatorInfo == null && set2 == null && b2) {
            return 0;
        }
        return 2;
    }
}
