Search in sources :

Example 1 with CMSException

use of com.github.zhenwei.pkix.cms.CMSException in project LinLong-Java by zhenwei1108.

the class BcPasswordRecipient method extractSecretKey.

protected KeyParameter extractSecretKey(AlgorithmIdentifier keyEncryptionAlgorithm, AlgorithmIdentifier contentEncryptionAlgorithm, byte[] derivedKey, byte[] encryptedContentEncryptionKey) throws CMSException {
    Wrapper keyEncryptionCipher = EnvelopedDataHelper.createRFC3211Wrapper(keyEncryptionAlgorithm.getAlgorithm());
    keyEncryptionCipher.init(false, new ParametersWithIV(new KeyParameter(derivedKey), ASN1OctetString.getInstance(keyEncryptionAlgorithm.getParameters()).getOctets()));
    try {
        return new KeyParameter(keyEncryptionCipher.unwrap(encryptedContentEncryptionKey, 0, encryptedContentEncryptionKey.length));
    } catch (InvalidCipherTextException e) {
        throw new CMSException("unable to unwrap key: " + e.getMessage(), e);
    }
}
Also used : Wrapper(com.github.zhenwei.core.crypto.Wrapper) ParametersWithIV(com.github.zhenwei.core.crypto.params.ParametersWithIV) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) CMSException(com.github.zhenwei.pkix.cms.CMSException)

Example 2 with CMSException

use of com.github.zhenwei.pkix.cms.CMSException in project LinLong-Java by zhenwei1108.

the class BcPasswordRecipient method calculateDerivedKey.

public byte[] calculateDerivedKey(int schemeID, AlgorithmIdentifier derivationAlgorithm, int keySize) throws CMSException {
    PBKDF2Params params = PBKDF2Params.getInstance(derivationAlgorithm.getParameters());
    byte[] encodedPassword = (schemeID == PasswordRecipient.PKCS5_SCHEME2) ? PBEParametersGenerator.PKCS5PasswordToBytes(password) : PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password);
    try {
        PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(EnvelopedDataHelper.getPRF(params.getPrf()));
        gen.init(encodedPassword, params.getSalt(), params.getIterationCount().intValue());
        return ((KeyParameter) gen.generateDerivedParameters(keySize)).getKey();
    } catch (Exception e) {
        throw new CMSException("exception creating derived key: " + e.getMessage(), e);
    }
}
Also used : PKCS5S2ParametersGenerator(com.github.zhenwei.core.crypto.generators.PKCS5S2ParametersGenerator) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) PBKDF2Params(com.github.zhenwei.core.asn1.pkcs.PBKDF2Params) CMSException(com.github.zhenwei.pkix.cms.CMSException) InvalidCipherTextException(com.github.zhenwei.core.crypto.InvalidCipherTextException) CMSException(com.github.zhenwei.pkix.cms.CMSException)

Example 3 with CMSException

use of com.github.zhenwei.pkix.cms.CMSException in project LinLong-Java by zhenwei1108.

the class BcPasswordRecipientInfoGenerator method calculateDerivedKey.

protected byte[] calculateDerivedKey(int schemeID, AlgorithmIdentifier derivationAlgorithm, int keySize) throws CMSException {
    PBKDF2Params params = PBKDF2Params.getInstance(derivationAlgorithm.getParameters());
    byte[] encodedPassword = (schemeID == PasswordRecipient.PKCS5_SCHEME2) ? PBEParametersGenerator.PKCS5PasswordToBytes(password) : PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(password);
    try {
        PKCS5S2ParametersGenerator gen = new PKCS5S2ParametersGenerator(EnvelopedDataHelper.getPRF(params.getPrf()));
        gen.init(encodedPassword, params.getSalt(), params.getIterationCount().intValue());
        return ((KeyParameter) gen.generateDerivedParameters(keySize)).getKey();
    } catch (Exception e) {
        throw new CMSException("exception creating derived key: " + e.getMessage(), e);
    }
}
Also used : PKCS5S2ParametersGenerator(com.github.zhenwei.core.crypto.generators.PKCS5S2ParametersGenerator) KeyParameter(com.github.zhenwei.core.crypto.params.KeyParameter) PBKDF2Params(com.github.zhenwei.core.asn1.pkcs.PBKDF2Params) CMSException(com.github.zhenwei.pkix.cms.CMSException) CMSException(com.github.zhenwei.pkix.cms.CMSException)

Example 4 with CMSException

use of com.github.zhenwei.pkix.cms.CMSException in project LinLong-Java by zhenwei1108.

the class EnvelopedDataHelper method calculateDerivedKey.

byte[] calculateDerivedKey(int schemeID, char[] password, AlgorithmIdentifier derivationAlgorithm, int keySize) throws CMSException {
    PBKDF2Params params = PBKDF2Params.getInstance(derivationAlgorithm.getParameters());
    try {
        SecretKeyFactory keyFact;
        if (schemeID == PasswordRecipient.PKCS5_SCHEME2) {
            keyFact = helper.createSecretKeyFactory("PBKDF2with8BIT");
        } else {
            keyFact = helper.createSecretKeyFactory((String) PBKDF2_ALG_NAMES.get(params.getPrf()));
        }
        SecretKey key = keyFact.generateSecret(new PBEKeySpec(password, params.getSalt(), params.getIterationCount().intValue(), keySize));
        return key.getEncoded();
    } catch (GeneralSecurityException e) {
        throw new CMSException("Unable to calculate derived key from password: " + e.getMessage(), e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) GeneralSecurityException(java.security.GeneralSecurityException) PBKDF2Params(com.github.zhenwei.core.asn1.pkcs.PBKDF2Params) ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) DEROctetString(com.github.zhenwei.core.asn1.DEROctetString) SecretKeyFactory(javax.crypto.SecretKeyFactory) CMSException(com.github.zhenwei.pkix.cms.CMSException)

Example 5 with CMSException

use of com.github.zhenwei.pkix.cms.CMSException in project LinLong-Java by zhenwei1108.

the class EnvelopedDataHelper method generateParameters.

AlgorithmParameters generateParameters(ASN1ObjectIdentifier encryptionOID, SecretKey encKey, SecureRandom rand) throws CMSException {
    try {
        AlgorithmParameterGenerator pGen = createAlgorithmParameterGenerator(encryptionOID);
        if (encryptionOID.equals(CMSAlgorithm.RC2_CBC)) {
            byte[] iv = new byte[8];
            rand.nextBytes(iv);
            try {
                pGen.init(new RC2ParameterSpec(encKey.getEncoded().length * 8, iv), rand);
            } catch (InvalidAlgorithmParameterException e) {
                throw new CMSException("parameters generation error: " + e, e);
            }
        }
        return pGen.generateParameters();
    } catch (NoSuchAlgorithmException e) {
        return null;
    } catch (GeneralSecurityException e) {
        throw new CMSException("exception creating algorithm parameter generator: " + e, e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) GeneralSecurityException(java.security.GeneralSecurityException) AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CMSException(com.github.zhenwei.pkix.cms.CMSException)

Aggregations

CMSException (com.github.zhenwei.pkix.cms.CMSException)26 IOException (java.io.IOException)10 GeneralSecurityException (java.security.GeneralSecurityException)9 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)5 Cipher (javax.crypto.Cipher)5 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)4 DigestCalculator (com.github.zhenwei.pkix.operator.DigestCalculator)4 OperatorCreationException (com.github.zhenwei.pkix.operator.OperatorCreationException)4 PublicKey (java.security.PublicKey)4 SecretKey (javax.crypto.SecretKey)4 DEROctetString (com.github.zhenwei.core.asn1.DEROctetString)3 Gost2814789EncryptedKey (com.github.zhenwei.core.asn1.cryptopro.Gost2814789EncryptedKey)3 PBKDF2Params (com.github.zhenwei.core.asn1.pkcs.PBKDF2Params)3 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)3 KeyParameter (com.github.zhenwei.core.crypto.params.KeyParameter)3 TimeStampToken (com.github.zhenwei.pkix.tsp.TimeStampToken)3 TimeStampTokenInfo (com.github.zhenwei.pkix.tsp.TimeStampTokenInfo)3 ASN1Encodable (com.github.zhenwei.core.asn1.ASN1Encodable)2 SubjectPublicKeyInfo (com.github.zhenwei.core.asn1.x509.SubjectPublicKeyInfo)2 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)2