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

package org.bouncycastle.cms.jcajce;

import org.bouncycastle.asn1.iso.ISOIECObjectIdentifiers;
import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
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 java.util.HashMap;
import java.util.HashSet;
import org.bouncycastle.cms.CMSAlgorithm;
import java.security.GeneralSecurityException;
import org.bouncycastle.operator.OperatorCreationException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import org.bouncycastle.jcajce.util.JcaJceHelper;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import org.bouncycastle.operator.GenericKey;
import java.io.IOException;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.jcajce.util.AlgorithmParametersUtils;
import org.bouncycastle.asn1.ASN1Encodable;
import java.security.AlgorithmParameters;
import java.security.Provider;
import org.bouncycastle.asn1.ASN1OctetString;
import org.bouncycastle.asn1.x509.Extension;
import java.security.cert.CertificateEncodingException;
import org.bouncycastle.asn1.x509.Certificate;
import org.bouncycastle.asn1.cms.IssuerAndSerialNumber;
import java.security.cert.X509Certificate;
import org.bouncycastle.jcajce.util.AnnotatedPrivateKey;
import java.security.PrivateKey;
import org.bouncycastle.asn1.ASN1Primitive;
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import java.util.Map;
import java.util.Set;

class CMSUtils
{
    private static final Set mqvAlgs;
    private static final Set ecAlgs;
    private static final Set gostAlgs;
    private static final Map asymmetricWrapperAlgNames;
    private static Map<ASN1ObjectIdentifier, String> wrapAlgNames;
    
    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 String getWrapAlgorithmName(final ASN1ObjectIdentifier asn1ObjectIdentifier) {
        return CMSUtils.wrapAlgNames.get(asn1ObjectIdentifier);
    }
    
    static PrivateKey cleanPrivateKey(final PrivateKey privateKey) {
        if (privateKey instanceof AnnotatedPrivateKey) {
            return cleanPrivateKey(((AnnotatedPrivateKey)privateKey).getKey());
        }
        return privateKey;
    }
    
    static IssuerAndSerialNumber getIssuerAndSerialNumber(final X509Certificate x509Certificate) throws CertificateEncodingException {
        return new IssuerAndSerialNumber(Certificate.getInstance(x509Certificate.getEncoded()).getIssuer(), x509Certificate.getSerialNumber());
    }
    
    static byte[] getSubjectKeyId(final X509Certificate x509Certificate) {
        final byte[] extensionValue = x509Certificate.getExtensionValue(Extension.subjectKeyIdentifier.getId());
        if (extensionValue != null) {
            return ASN1OctetString.getInstance(ASN1OctetString.getInstance(extensionValue).getOctets()).getOctets();
        }
        return null;
    }
    
    static EnvelopedDataHelper createContentHelper(final Provider provider) {
        if (provider != null) {
            return new EnvelopedDataHelper(new ProviderJcaJceExtHelper(provider));
        }
        return new EnvelopedDataHelper(new DefaultJcaJceExtHelper());
    }
    
    static EnvelopedDataHelper createContentHelper(final String s) {
        if (s != null) {
            return new EnvelopedDataHelper(new NamedJcaJceExtHelper(s));
        }
        return new EnvelopedDataHelper(new DefaultJcaJceExtHelper());
    }
    
    static ASN1Encodable extractParameters(final AlgorithmParameters algorithmParameters) throws CMSException {
        try {
            return AlgorithmParametersUtils.extractParameters(algorithmParameters);
        }
        catch (final IOException ex) {
            throw new CMSException("cannot extract parameters: " + ex.getMessage(), ex);
        }
    }
    
    static void loadParameters(final AlgorithmParameters algorithmParameters, final ASN1Encodable asn1Encodable) throws CMSException {
        try {
            AlgorithmParametersUtils.loadParameters(algorithmParameters, asn1Encodable);
        }
        catch (final IOException ex) {
            throw new CMSException("error encoding algorithm parameters.", ex);
        }
    }
    
    static Key getJceKey(final GenericKey genericKey) {
        if (genericKey.getRepresentation() instanceof Key) {
            return (Key)genericKey.getRepresentation();
        }
        if (genericKey.getRepresentation() instanceof byte[]) {
            return new SecretKeySpec((byte[])genericKey.getRepresentation(), "ENC");
        }
        throw new IllegalArgumentException("unknown generic key type");
    }
    
    static Cipher createAsymmetricWrapper(final JcaJceHelper jcaJceHelper, final ASN1ObjectIdentifier asn1ObjectIdentifier, final Map map) throws OperatorCreationException {
        try {
            String s = null;
            if (!map.isEmpty()) {
                s = map.get(asn1ObjectIdentifier);
            }
            if (s == null) {
                s = CMSUtils.asymmetricWrapperAlgNames.get(asn1ObjectIdentifier);
            }
            if (s != null) {
                try {
                    return jcaJceHelper.createCipher(s);
                }
                catch (final NoSuchAlgorithmException ex) {
                    if (s.equals("RSA/ECB/PKCS1Padding")) {
                        try {
                            return jcaJceHelper.createCipher("RSA/NONE/PKCS1Padding");
                        }
                        catch (final NoSuchAlgorithmException ex2) {}
                    }
                }
            }
            return jcaJceHelper.createCipher(asn1ObjectIdentifier.getId());
        }
        catch (final GeneralSecurityException ex3) {
            throw new OperatorCreationException("cannot create cipher: " + ex3.getMessage(), ex3);
        }
    }
    
    public static int getKekSize(final ASN1ObjectIdentifier asn1ObjectIdentifier) {
        if (asn1ObjectIdentifier.equals(CMSAlgorithm.AES256_WRAP) || asn1ObjectIdentifier.equals(CMSAlgorithm.AES256_WRAP_PAD)) {
            return 32;
        }
        if (asn1ObjectIdentifier.equals(CMSAlgorithm.AES128_WRAP) || asn1ObjectIdentifier.equals(CMSAlgorithm.AES128_WRAP_PAD)) {
            return 16;
        }
        if (asn1ObjectIdentifier.equals(CMSAlgorithm.AES192_WRAP) || asn1ObjectIdentifier.equals(CMSAlgorithm.AES192_WRAP_PAD)) {
            return 24;
        }
        throw new IllegalArgumentException("unknown wrap algorithm");
    }
    
    static {
        mqvAlgs = new HashSet();
        ecAlgs = new HashSet();
        gostAlgs = new HashSet();
        asymmetricWrapperAlgNames = new HashMap();
        (CMSUtils.wrapAlgNames = new HashMap<ASN1ObjectIdentifier, String>()).put(CMSAlgorithm.AES128_WRAP, "AESWRAP");
        CMSUtils.wrapAlgNames.put(CMSAlgorithm.AES192_WRAP, "AESWRAP");
        CMSUtils.wrapAlgNames.put(CMSAlgorithm.AES256_WRAP, "AESWRAP");
        CMSUtils.wrapAlgNames.put(CMSAlgorithm.AES128_WRAP_PAD, "AES-KWP");
        CMSUtils.wrapAlgNames.put(CMSAlgorithm.AES192_WRAP_PAD, "AES-KWP");
        CMSUtils.wrapAlgNames.put(CMSAlgorithm.AES256_WRAP_PAD, "AES-KWP");
        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(CryptoProObjectIdentifiers.gostR3410_2001);
        CMSUtils.gostAlgs.add(RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_256);
        CMSUtils.gostAlgs.add(RosstandartObjectIdentifiers.id_tc26_agreement_gost_3410_12_512);
        CMSUtils.gostAlgs.add(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256);
        CMSUtils.gostAlgs.add(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_512);
        CMSUtils.asymmetricWrapperAlgNames.put(PKCSObjectIdentifiers.rsaEncryption, "RSA/ECB/PKCS1Padding");
        CMSUtils.asymmetricWrapperAlgNames.put(OIWObjectIdentifiers.elGamalAlgorithm, "Elgamal/ECB/PKCS1Padding");
        CMSUtils.asymmetricWrapperAlgNames.put(PKCSObjectIdentifiers.id_RSAES_OAEP, "RSA/ECB/OAEPPadding");
        CMSUtils.asymmetricWrapperAlgNames.put(CryptoProObjectIdentifiers.gostR3410_2001, "ECGOST3410");
        CMSUtils.asymmetricWrapperAlgNames.put(ISOIECObjectIdentifiers.id_kem_rsa, "RSA-KTS-KEM-KWS");
    }
}
