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

package org.bouncycastle.cms;

import org.bouncycastle.asn1.rosstandart.RosstandartObjectIdentifiers;
import org.bouncycastle.asn1.cryptopro.CryptoProObjectIdentifiers;
import org.bouncycastle.asn1.sec.SECObjectIdentifiers;
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
import java.util.HashSet;
import org.bouncycastle.asn1.ASN1SequenceParser;
import org.bouncycastle.asn1.cms.AttributeTable;
import org.bouncycastle.asn1.ASN1SetParser;
import org.bouncycastle.asn1.ASN1Object;
import java.util.Collections;
import org.bouncycastle.operator.OutputAEADEncryptor;
import java.util.Map;
import org.bouncycastle.asn1.cms.OriginatorInfo;
import org.bouncycastle.asn1.BERSequenceGenerator;
import org.bouncycastle.operator.GenericKey;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.BEROctetString;
import org.bouncycastle.asn1.cms.EncryptedContentInfo;
import org.bouncycastle.operator.OutputEncryptor;
import org.bouncycastle.util.io.TeeOutputStream;
import org.bouncycastle.util.io.TeeInputStream;
import org.bouncycastle.operator.DigestCalculator;
import org.bouncycastle.util.io.Streams;
import java.io.IOException;
import org.bouncycastle.asn1.BEROctetStringGenerator;
import java.io.OutputStream;
import org.bouncycastle.asn1.DERSet;
import org.bouncycastle.asn1.BERSet;
import org.bouncycastle.asn1.ASN1EncodableVector;
import java.util.Collection;
import org.bouncycastle.asn1.ocsp.OCSPResponse;
import org.bouncycastle.asn1.cms.CMSObjectIdentifiers;
import org.bouncycastle.asn1.ASN1TaggedObject;
import org.bouncycastle.asn1.cms.OtherRevocationInfoFormat;
import org.bouncycastle.cert.X509CRLHolder;
import org.bouncycastle.asn1.DERTaggedObject;
import org.bouncycastle.cert.X509AttributeCertificateHolder;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.util.Selector;
import java.util.ArrayList;
import java.util.List;
import org.bouncycastle.util.Store;
import java.util.Iterator;
import org.bouncycastle.operator.DigestAlgorithmIdentifierFinder;
import org.bouncycastle.asn1.DLSet;
import org.bouncycastle.asn1.ASN1Set;
import java.io.InputStream;
import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.cms.ContentInfo;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.asn1.DERNull;
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
import org.bouncycastle.util.Strings;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import java.util.Set;

class CMSUtils
{
    private static final Set<String> des;
    private static final Set mqvAlgs;
    private static final Set ecAlgs;
    private static final Set gostAlgs;
    
    static boolean isMQV(final ASN1ObjectIdentifier asn1ObjectIdentifier) {
        return CMSUtils.mqvAlgs.contains(asn1ObjectIdentifier);
    }
    
    static boolean isEC(final ASN1ObjectIdentifier asn1ObjectIdentifier) {
        return CMSUtils.ecAlgs.contains(asn1ObjectIdentifier);
    }
    
    static boolean isGOST(final ASN1ObjectIdentifier asn1ObjectIdentifier) {
        return CMSUtils.gostAlgs.contains(asn1ObjectIdentifier);
    }
    
    static boolean isRFC2631(final ASN1ObjectIdentifier asn1ObjectIdentifier) {
        return asn1ObjectIdentifier.equals(PKCSObjectIdentifiers.id_alg_ESDH) || asn1ObjectIdentifier.equals(PKCSObjectIdentifiers.id_alg_SSDH);
    }
    
    static boolean isDES(final String s) {
        return CMSUtils.des.contains(Strings.toUpperCase(s));
    }
    
    static boolean isEquivalent(final AlgorithmIdentifier algorithmIdentifier, final AlgorithmIdentifier algorithmIdentifier2) {
        if (algorithmIdentifier == null || algorithmIdentifier2 == null) {
            return false;
        }
        if (!algorithmIdentifier.getAlgorithm().equals(algorithmIdentifier2.getAlgorithm())) {
            return false;
        }
        final ASN1Encodable parameters = algorithmIdentifier.getParameters();
        final ASN1Encodable parameters2 = algorithmIdentifier2.getParameters();
        if (parameters != null) {
            return parameters.equals(parameters2) || (parameters.equals(DERNull.INSTANCE) && parameters2 == null);
        }
        return parameters2 == null || parameters2.equals(DERNull.INSTANCE);
    }
    
    static ContentInfo readContentInfo(final byte[] array) throws CMSException {
        return readContentInfo(new ASN1InputStream(array));
    }
    
    static ContentInfo readContentInfo(final InputStream inputStream) throws CMSException {
        return readContentInfo(new ASN1InputStream(inputStream));
    }
    
    static ASN1Set convertToDlSet(final Set<AlgorithmIdentifier> set) {
        return new DLSet(set.toArray(new AlgorithmIdentifier[set.size()]));
    }
    
    static void addDigestAlgs(final Set<AlgorithmIdentifier> set, final SignerInformation signerInformation, final DigestAlgorithmIdentifierFinder digestAlgorithmIdentifierFinder) {
        set.add(CMSSignedHelper.INSTANCE.fixDigestAlgID(signerInformation.getDigestAlgorithmID(), digestAlgorithmIdentifierFinder));
        final Iterator<SignerInformation> iterator = signerInformation.getCounterSignatures().iterator();
        while (iterator.hasNext()) {
            set.add(CMSSignedHelper.INSTANCE.fixDigestAlgID(iterator.next().getDigestAlgorithmID(), digestAlgorithmIdentifierFinder));
        }
    }
    
    static List getCertificatesFromStore(final Store store) throws CMSException {
        final ArrayList list = new ArrayList();
        try {
            final Iterator iterator = store.getMatches(null).iterator();
            while (iterator.hasNext()) {
                list.add(((X509CertificateHolder)iterator.next()).toASN1Structure());
            }
            return list;
        }
        catch (final ClassCastException ex) {
            throw new CMSException("error processing certs", ex);
        }
    }
    
    static List getAttributeCertificatesFromStore(final Store store) throws CMSException {
        final ArrayList list = new ArrayList();
        try {
            final Iterator iterator = store.getMatches(null).iterator();
            while (iterator.hasNext()) {
                list.add(new DERTaggedObject(false, 2, ((X509AttributeCertificateHolder)iterator.next()).toASN1Structure()));
            }
            return list;
        }
        catch (final ClassCastException ex) {
            throw new CMSException("error processing certs", ex);
        }
    }
    
    static List getCRLsFromStore(final Store store) throws CMSException {
        final ArrayList list = new ArrayList();
        try {
            for (final Object next : store.getMatches(null)) {
                if (next instanceof X509CRLHolder) {
                    list.add(((X509CRLHolder)next).toASN1Structure());
                }
                else if (next instanceof OtherRevocationInfoFormat) {
                    final OtherRevocationInfoFormat instance = OtherRevocationInfoFormat.getInstance(next);
                    validateInfoFormat(instance);
                    list.add(new DERTaggedObject(false, 1, instance));
                }
                else {
                    if (!(next instanceof ASN1TaggedObject)) {
                        continue;
                    }
                    list.add(next);
                }
            }
            return list;
        }
        catch (final ClassCastException ex) {
            throw new CMSException("error processing certs", ex);
        }
    }
    
    static void validateInfoFormat(final OtherRevocationInfoFormat otherRevocationInfoFormat) {
        if (CMSObjectIdentifiers.id_ri_ocsp_response.equals(otherRevocationInfoFormat.getInfoFormat()) && 0 != OCSPResponse.getInstance(otherRevocationInfoFormat.getInfo()).getResponseStatus().getIntValue()) {
            throw new IllegalArgumentException("cannot add unsuccessful OCSP response to CMS SignedData");
        }
    }
    
    static Collection getOthersFromStore(final ASN1ObjectIdentifier asn1ObjectIdentifier, final Store store) {
        final ArrayList list = new ArrayList();
        final Iterator iterator = store.getMatches(null).iterator();
        while (iterator.hasNext()) {
            final OtherRevocationInfoFormat otherRevocationInfoFormat = new OtherRevocationInfoFormat(asn1ObjectIdentifier, (ASN1Encodable)iterator.next());
            validateInfoFormat(otherRevocationInfoFormat);
            list.add(new DERTaggedObject(false, 1, otherRevocationInfoFormat));
        }
        return list;
    }
    
    static ASN1Set createBerSetFromList(final List list) {
        final ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector();
        final Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            asn1EncodableVector.add((ASN1Encodable)iterator.next());
        }
        return new BERSet(asn1EncodableVector);
    }
    
    static ASN1Set createDlSetFromList(final List list) {
        final ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector();
        final Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            asn1EncodableVector.add((ASN1Encodable)iterator.next());
        }
        return new DLSet(asn1EncodableVector);
    }
    
    static ASN1Set createDerSetFromList(final List list) {
        final ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector();
        final Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            asn1EncodableVector.add((ASN1Encodable)iterator.next());
        }
        return new DERSet(asn1EncodableVector);
    }
    
    static OutputStream createBEROctetOutputStream(final OutputStream outputStream, final int n, final boolean b, final int n2) throws IOException {
        final BEROctetStringGenerator berOctetStringGenerator = new BEROctetStringGenerator(outputStream, n, b);
        if (n2 != 0) {
            return berOctetStringGenerator.getOctetOutputStream(new byte[n2]);
        }
        return berOctetStringGenerator.getOctetOutputStream();
    }
    
    private static ContentInfo readContentInfo(final ASN1InputStream asn1InputStream) throws CMSException {
        try {
            final ContentInfo instance = ContentInfo.getInstance(asn1InputStream.readObject());
            if (instance == null) {
                throw new CMSException("No content found.");
            }
            return instance;
        }
        catch (final IOException ex) {
            throw new CMSException("IOException reading content.", ex);
        }
        catch (final ClassCastException ex2) {
            throw new CMSException("Malformed content.", ex2);
        }
        catch (final IllegalArgumentException ex3) {
            throw new CMSException("Malformed content.", ex3);
        }
    }
    
    public static byte[] streamToByteArray(final InputStream inputStream) throws IOException {
        return Streams.readAll(inputStream);
    }
    
    public static byte[] streamToByteArray(final InputStream inputStream, final int n) throws IOException {
        return Streams.readAllLimited(inputStream, n);
    }
    
    static InputStream attachDigestsToInputStream(final Collection collection, final InputStream inputStream) {
        InputStream inputStream2 = inputStream;
        final Iterator iterator = collection.iterator();
        while (iterator.hasNext()) {
            inputStream2 = new TeeInputStream(inputStream2, ((DigestCalculator)iterator.next()).getOutputStream());
        }
        return inputStream2;
    }
    
    static OutputStream attachSignersToOutputStream(final Collection collection, final OutputStream outputStream) {
        OutputStream safeTeeOutputStream = outputStream;
        final Iterator iterator = collection.iterator();
        while (iterator.hasNext()) {
            safeTeeOutputStream = getSafeTeeOutputStream(safeTeeOutputStream, ((SignerInfoGenerator)iterator.next()).getCalculatingOutputStream());
        }
        return safeTeeOutputStream;
    }
    
    static OutputStream getSafeOutputStream(final OutputStream outputStream) {
        return (outputStream == null) ? new NullOutputStream() : outputStream;
    }
    
    static OutputStream getSafeTeeOutputStream(final OutputStream outputStream, final OutputStream outputStream2) {
        return (outputStream == null) ? getSafeOutputStream(outputStream2) : ((outputStream2 == null) ? getSafeOutputStream(outputStream) : new TeeOutputStream(outputStream, outputStream2));
    }
    
    static EncryptedContentInfo getEncryptedContentInfo(final CMSTypedData cmsTypedData, final OutputEncryptor outputEncryptor, final byte[] array) {
        return getEncryptedContentInfo(cmsTypedData.getContentType(), outputEncryptor.getAlgorithmIdentifier(), array);
    }
    
    static EncryptedContentInfo getEncryptedContentInfo(final ASN1ObjectIdentifier asn1ObjectIdentifier, final AlgorithmIdentifier algorithmIdentifier, final byte[] array) {
        return new EncryptedContentInfo(asn1ObjectIdentifier, algorithmIdentifier, new BEROctetString(array));
    }
    
    static ASN1EncodableVector getRecipentInfos(final GenericKey genericKey, final List list) throws CMSException {
        final ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector();
        final Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            asn1EncodableVector.add(((RecipientInfoGenerator)iterator.next()).generate(genericKey));
        }
        return asn1EncodableVector;
    }
    
    static void addRecipientInfosToGenerator(final ASN1EncodableVector asn1EncodableVector, final BERSequenceGenerator berSequenceGenerator, final boolean b) throws IOException {
        if (b) {
            berSequenceGenerator.getRawOutputStream().write(new BERSet(asn1EncodableVector).getEncoded());
        }
        else {
            berSequenceGenerator.getRawOutputStream().write(new DERSet(asn1EncodableVector).getEncoded());
        }
    }
    
    static void addOriginatorInfoToGenerator(final BERSequenceGenerator berSequenceGenerator, final OriginatorInfo originatorInfo) throws IOException {
        if (originatorInfo != null) {
            berSequenceGenerator.addObject(new DERTaggedObject(false, 0, originatorInfo));
        }
    }
    
    static void addAttriSetToGenerator(final BERSequenceGenerator berSequenceGenerator, final CMSAttributeTableGenerator cmsAttributeTableGenerator, final int n, final Map map) throws IOException {
        if (cmsAttributeTableGenerator != null) {
            berSequenceGenerator.addObject(new DERTaggedObject(false, n, new BERSet(cmsAttributeTableGenerator.getAttributes(map).toASN1EncodableVector())));
        }
    }
    
    static ASN1Set processAuthAttrSet(final CMSAttributeTableGenerator cmsAttributeTableGenerator, final OutputAEADEncryptor outputAEADEncryptor) throws IOException {
        ASN1Object asn1Object = null;
        if (cmsAttributeTableGenerator != null) {
            asn1Object = new DERSet(cmsAttributeTableGenerator.getAttributes(Collections.EMPTY_MAP).toASN1EncodableVector());
            outputAEADEncryptor.getAADStream().write(asn1Object.getEncoded("DER"));
        }
        return (ASN1Set)asn1Object;
    }
    
    static AttributeTable getAttributesTable(final ASN1SetParser asn1SetParser) throws IOException {
        if (asn1SetParser != null) {
            final ASN1EncodableVector asn1EncodableVector = new ASN1EncodableVector();
            ASN1Encodable object;
            while ((object = asn1SetParser.readObject()) != null) {
                asn1EncodableVector.add(((ASN1SequenceParser)object).toASN1Primitive());
            }
            return new AttributeTable(new DERSet(asn1EncodableVector));
        }
        return null;
    }
    
    static ASN1Set getAttrDLSet(final CMSAttributeTableGenerator cmsAttributeTableGenerator) {
        return (cmsAttributeTableGenerator != null) ? new DLSet(cmsAttributeTableGenerator.getAttributes(Collections.EMPTY_MAP).toASN1EncodableVector()) : null;
    }
    
    static ASN1Set getAttrBERSet(final CMSAttributeTableGenerator cmsAttributeTableGenerator) {
        return (cmsAttributeTableGenerator != null) ? new BERSet(cmsAttributeTableGenerator.getAttributes(Collections.EMPTY_MAP).toASN1EncodableVector()) : null;
    }
    
    static byte[] encodeObj(final ASN1Encodable asn1Encodable) throws IOException {
        if (asn1Encodable != null) {
            return asn1Encodable.toASN1Primitive().getEncoded();
        }
        return null;
    }
    
    static {
        des = new HashSet<String>();
        mqvAlgs = new HashSet();
        ecAlgs = new HashSet();
        gostAlgs = new HashSet();
        CMSUtils.des.add("DES");
        CMSUtils.des.add("DESEDE");
        CMSUtils.des.add(OIWObjectIdentifiers.desCBC.getId());
        CMSUtils.des.add(PKCSObjectIdentifiers.des_EDE3_CBC.getId());
        CMSUtils.des.add(PKCSObjectIdentifiers.id_alg_CMS3DESwrap.getId());
        CMSUtils.mqvAlgs.add(X9ObjectIdentifiers.mqvSinglePass_sha1kdf_scheme);
        CMSUtils.mqvAlgs.add(SECObjectIdentifiers.mqvSinglePass_sha224kdf_scheme);
        CMSUtils.mqvAlgs.add(SECObjectIdentifiers.mqvSinglePass_sha256kdf_scheme);
        CMSUtils.mqvAlgs.add(SECObjectIdentifiers.mqvSinglePass_sha384kdf_scheme);
        CMSUtils.mqvAlgs.add(SECObjectIdentifiers.mqvSinglePass_sha512kdf_scheme);
        CMSUtils.ecAlgs.add(X9ObjectIdentifiers.dhSinglePass_cofactorDH_sha1kdf_scheme);
        CMSUtils.ecAlgs.add(X9ObjectIdentifiers.dhSinglePass_stdDH_sha1kdf_scheme);
        CMSUtils.ecAlgs.add(SECObjectIdentifiers.dhSinglePass_cofactorDH_sha224kdf_scheme);
        CMSUtils.ecAlgs.add(SECObjectIdentifiers.dhSinglePass_stdDH_sha224kdf_scheme);
        CMSUtils.ecAlgs.add(SECObjectIdentifiers.dhSinglePass_cofactorDH_sha256kdf_scheme);
        CMSUtils.ecAlgs.add(SECObjectIdentifiers.dhSinglePass_stdDH_sha256kdf_scheme);
        CMSUtils.ecAlgs.add(SECObjectIdentifiers.dhSinglePass_cofactorDH_sha384kdf_scheme);
        CMSUtils.ecAlgs.add(SECObjectIdentifiers.dhSinglePass_stdDH_sha384kdf_scheme);
        CMSUtils.ecAlgs.add(SECObjectIdentifiers.dhSinglePass_cofactorDH_sha512kdf_scheme);
        CMSUtils.ecAlgs.add(SECObjectIdentifiers.dhSinglePass_stdDH_sha512kdf_scheme);
        CMSUtils.gostAlgs.add(CryptoProObjectIdentifiers.gostR3410_2001_CryptoPro_ESDH);
        CMSUtils.gostAlgs.add(RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_256);
        CMSUtils.gostAlgs.add(RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_512);
    }
}
