Search in sources :

Example 1 with CRMFException

use of com.github.zhenwei.pkix.cert.crmf.CRMFException in project LinLong-Java by zhenwei1108.

the class CRMFHelper method createContentCipher.

Cipher createContentCipher(final Key sKey, final AlgorithmIdentifier encryptionAlgID) throws CRMFException {
    return (Cipher) execute(new JCECallback() {

        public Object doInJCE() throws CRMFException, InvalidAlgorithmParameterException, InvalidKeyException, InvalidParameterSpecException, NoSuchAlgorithmException, NoSuchPaddingException, NoSuchProviderException {
            Cipher cipher = createCipher(encryptionAlgID.getAlgorithm());
            ASN1Primitive sParams = (ASN1Primitive) encryptionAlgID.getParameters();
            ASN1ObjectIdentifier encAlg = encryptionAlgID.getAlgorithm();
            if (sParams != null && !(sParams instanceof ASN1Null)) {
                try {
                    AlgorithmParameters params = createAlgorithmParameters(encryptionAlgID.getAlgorithm());
                    try {
                        AlgorithmParametersUtils.loadParameters(params, sParams);
                    } catch (IOException e) {
                        throw new CRMFException("error decoding algorithm parameters.", e);
                    }
                    cipher.init(Cipher.DECRYPT_MODE, sKey, params);
                } catch (NoSuchAlgorithmException e) {
                    if (encAlg.equals(CMSAlgorithm.DES_EDE3_CBC) || encAlg.equals(CMSAlgorithm.IDEA_CBC) || encAlg.equals(CMSAlgorithm.AES128_CBC) || encAlg.equals(CMSAlgorithm.AES192_CBC) || encAlg.equals(CMSAlgorithm.AES256_CBC)) {
                        cipher.init(Cipher.DECRYPT_MODE, sKey, new IvParameterSpec(ASN1OctetString.getInstance(sParams).getOctets()));
                    } else {
                        throw e;
                    }
                }
            } else {
                if (encAlg.equals(CMSAlgorithm.DES_EDE3_CBC) || encAlg.equals(CMSAlgorithm.IDEA_CBC) || encAlg.equals(CMSAlgorithm.CAST5_CBC)) {
                    cipher.init(Cipher.DECRYPT_MODE, sKey, new IvParameterSpec(new byte[8]));
                } else {
                    cipher.init(Cipher.DECRYPT_MODE, sKey);
                }
            }
            return cipher;
        }
    });
}
Also used : CRMFException(com.github.zhenwei.pkix.cert.crmf.CRMFException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ASN1Primitive(com.github.zhenwei.core.asn1.ASN1Primitive) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) ASN1Null(com.github.zhenwei.core.asn1.ASN1Null) AlgorithmParameters(java.security.AlgorithmParameters)

Example 2 with CRMFException

use of com.github.zhenwei.pkix.cert.crmf.CRMFException in project LinLong-Java by zhenwei1108.

the class CRMFHelper method toPublicKey.

PublicKey toPublicKey(SubjectPublicKeyInfo subjectPublicKeyInfo) throws CRMFException {
    try {
        X509EncodedKeySpec xspec = new X509EncodedKeySpec(subjectPublicKeyInfo.getEncoded());
        AlgorithmIdentifier keyAlg = subjectPublicKeyInfo.getAlgorithm();
        return createKeyFactory(keyAlg.getAlgorithm()).generatePublic(xspec);
    } catch (Exception e) {
        throw new CRMFException("invalid key: " + e.getMessage(), e);
    }
}
Also used : CRMFException(com.github.zhenwei.pkix.cert.crmf.CRMFException) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) GeneralSecurityException(java.security.GeneralSecurityException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) InvalidParameterSpecException(java.security.spec.InvalidParameterSpecException) CRMFException(com.github.zhenwei.pkix.cert.crmf.CRMFException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 3 with CRMFException

use of com.github.zhenwei.pkix.cert.crmf.CRMFException in project LinLong-Java by zhenwei1108.

the class CRMFHelper method generateParameters.

AlgorithmParameters generateParameters(ASN1ObjectIdentifier encryptionOID, SecretKey encKey, SecureRandom rand) throws CRMFException {
    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 CRMFException("parameters generation error: " + e, e);
            }
        }
        return pGen.generateParameters();
    } catch (NoSuchAlgorithmException e) {
        return null;
    } catch (GeneralSecurityException e) {
        throw new CRMFException("exception creating algorithm parameter generator: " + e, e);
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) CRMFException(com.github.zhenwei.pkix.cert.crmf.CRMFException) GeneralSecurityException(java.security.GeneralSecurityException) AlgorithmParameterGenerator(java.security.AlgorithmParameterGenerator) RC2ParameterSpec(javax.crypto.spec.RC2ParameterSpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

CRMFException (com.github.zhenwei.pkix.cert.crmf.CRMFException)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 IOException (java.io.IOException)2 GeneralSecurityException (java.security.GeneralSecurityException)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 ASN1Null (com.github.zhenwei.core.asn1.ASN1Null)1 ASN1ObjectIdentifier (com.github.zhenwei.core.asn1.ASN1ObjectIdentifier)1 ASN1Primitive (com.github.zhenwei.core.asn1.ASN1Primitive)1 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)1 AlgorithmParameterGenerator (java.security.AlgorithmParameterGenerator)1 AlgorithmParameters (java.security.AlgorithmParameters)1 InvalidKeyException (java.security.InvalidKeyException)1 NoSuchProviderException (java.security.NoSuchProviderException)1 InvalidParameterSpecException (java.security.spec.InvalidParameterSpecException)1 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)1 Cipher (javax.crypto.Cipher)1 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)1 IvParameterSpec (javax.crypto.spec.IvParameterSpec)1 RC2ParameterSpec (javax.crypto.spec.RC2ParameterSpec)1