Search in sources :

Example 1 with OperatorException

use of com.github.zhenwei.pkix.operator.OperatorException in project LinLong-Java by zhenwei1108.

the class EncryptedValueBuilder method encryptData.

private EncryptedValue encryptData(byte[] data) throws CRMFException {
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();
    OutputStream eOut = encryptor.getOutputStream(bOut);
    try {
        eOut.write(data);
        eOut.close();
    } catch (IOException e) {
        throw new CRMFException("cannot process data: " + e.getMessage(), e);
    }
    AlgorithmIdentifier intendedAlg = null;
    AlgorithmIdentifier symmAlg = encryptor.getAlgorithmIdentifier();
    DERBitString encSymmKey;
    try {
        wrapper.generateWrappedKey(encryptor.getKey());
        encSymmKey = new DERBitString(wrapper.generateWrappedKey(encryptor.getKey()));
    } catch (OperatorException e) {
        throw new CRMFException("cannot wrap key: " + e.getMessage(), e);
    }
    AlgorithmIdentifier keyAlg = wrapper.getAlgorithmIdentifier();
    ASN1OctetString valueHint = null;
    DERBitString encValue = new DERBitString(bOut.toByteArray());
    return new EncryptedValue(intendedAlg, symmAlg, encSymmKey, keyAlg, valueHint, encValue);
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DERBitString(com.github.zhenwei.core.asn1.DERBitString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) EncryptedValue(com.github.zhenwei.pkix.util.asn1.crmf.EncryptedValue) OperatorException(com.github.zhenwei.pkix.operator.OperatorException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 2 with OperatorException

use of com.github.zhenwei.pkix.operator.OperatorException in project LinLong-Java by zhenwei1108.

the class EncryptedValueBuilder method build.

/**
 * Build an EncryptedValue structure containing the private key contained in the passed info
 * structure.
 *
 * @param privateKeyInfo a PKCS#8 private key info structure.
 * @return an EncryptedValue containing an EncryptedPrivateKeyInfo structure.
 * @throws CRMFException on a failure to encrypt the data, or wrap the symmetric key for this
 *                       value.
 */
public EncryptedValue build(PrivateKeyInfo privateKeyInfo) throws CRMFException {
    PKCS8EncryptedPrivateKeyInfoBuilder encInfoBldr = new PKCS8EncryptedPrivateKeyInfoBuilder(privateKeyInfo);
    AlgorithmIdentifier intendedAlg = privateKeyInfo.getPrivateKeyAlgorithm();
    AlgorithmIdentifier symmAlg = encryptor.getAlgorithmIdentifier();
    DERBitString encSymmKey;
    try {
        PKCS8EncryptedPrivateKeyInfo encInfo = encInfoBldr.build(encryptor);
        encSymmKey = new DERBitString(wrapper.generateWrappedKey(encryptor.getKey()));
        AlgorithmIdentifier keyAlg = wrapper.getAlgorithmIdentifier();
        ASN1OctetString valueHint = null;
        return new EncryptedValue(intendedAlg, symmAlg, encSymmKey, keyAlg, valueHint, new DERBitString(encInfo.getEncryptedData()));
    } catch (IllegalStateException e) {
        throw new CRMFException("cannot encode key: " + e.getMessage(), e);
    } catch (OperatorException e) {
        throw new CRMFException("cannot wrap key: " + e.getMessage(), e);
    }
}
Also used : ASN1OctetString(com.github.zhenwei.core.asn1.ASN1OctetString) PKCS8EncryptedPrivateKeyInfoBuilder(com.github.zhenwei.pkix.pkcs.PKCS8EncryptedPrivateKeyInfoBuilder) DERBitString(com.github.zhenwei.core.asn1.DERBitString) PKCS8EncryptedPrivateKeyInfo(com.github.zhenwei.pkix.pkcs.PKCS8EncryptedPrivateKeyInfo) EncryptedValue(com.github.zhenwei.pkix.util.asn1.crmf.EncryptedValue) OperatorException(com.github.zhenwei.pkix.operator.OperatorException) AlgorithmIdentifier(com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)

Example 3 with OperatorException

use of com.github.zhenwei.pkix.operator.OperatorException in project LinLong-Java by zhenwei1108.

the class JceKeyTransRecipient method extractSecretKey.

protected Key extractSecretKey(AlgorithmIdentifier keyEncryptionAlgorithm, AlgorithmIdentifier encryptedKeyAlgorithm, byte[] encryptedEncryptionKey) throws CMSException {
    if (CMSUtils.isGOST(keyEncryptionAlgorithm.getAlgorithm())) {
        try {
            GostR3410KeyTransport transport = GostR3410KeyTransport.getInstance(encryptedEncryptionKey);
            GostR3410TransportParameters transParams = transport.getTransportParameters();
            KeyFactory keyFactory = helper.createKeyFactory(keyEncryptionAlgorithm.getAlgorithm());
            PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(transParams.getEphemeralPublicKey().getEncoded()));
            KeyAgreement agreement = helper.createKeyAgreement(keyEncryptionAlgorithm.getAlgorithm());
            agreement.init(recipientKey, new UserKeyingMaterialSpec(transParams.getUkm()));
            agreement.doPhase(pubKey, true);
            SecretKey key = agreement.generateSecret(CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_KeyWrap.getId());
            Cipher keyCipher = helper.createCipher(CryptoProObjectIdentifiers.id_Gost28147_89_CryptoPro_KeyWrap);
            keyCipher.init(Cipher.UNWRAP_MODE, key, new GOST28147WrapParameterSpec(transParams.getEncryptionParamSet(), transParams.getUkm()));
            Gost2814789EncryptedKey encKey = transport.getSessionEncryptedKey();
            return keyCipher.unwrap(Arrays.concatenate(encKey.getEncryptedKey(), encKey.getMacKey()), helper.getBaseCipherName(encryptedKeyAlgorithm.getAlgorithm()), Cipher.SECRET_KEY);
        } catch (Exception e) {
            throw new CMSException("exception unwrapping key: " + e.getMessage(), e);
        }
    } else {
        JceAsymmetricKeyUnwrapper unwrapper = helper.createAsymmetricUnwrapper(keyEncryptionAlgorithm, recipientKey).setMustProduceEncodableUnwrappedKey(unwrappedKeyMustBeEncodable);
        if (!extraMappings.isEmpty()) {
            for (Iterator it = extraMappings.keySet().iterator(); it.hasNext(); ) {
                ASN1ObjectIdentifier algorithm = (ASN1ObjectIdentifier) it.next();
                unwrapper.setAlgorithmMapping(algorithm, (String) extraMappings.get(algorithm));
            }
        }
        try {
            Key key = helper.getJceKey(encryptedKeyAlgorithm.getAlgorithm(), unwrapper.generateUnwrappedKey(encryptedKeyAlgorithm, encryptedEncryptionKey));
            if (validateKeySize) {
                helper.keySizeCheck(encryptedKeyAlgorithm, key);
            }
            return key;
        } catch (OperatorException e) {
            throw new CMSException("exception unwrapping key: " + e.getMessage(), e);
        }
    }
}
Also used : JceAsymmetricKeyUnwrapper(com.github.zhenwei.pkix.operator.jcajce.JceAsymmetricKeyUnwrapper) PublicKey(java.security.PublicKey) GOST28147WrapParameterSpec(com.github.zhenwei.provider.jcajce.spec.GOST28147WrapParameterSpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) UserKeyingMaterialSpec(com.github.zhenwei.provider.jcajce.spec.UserKeyingMaterialSpec) CMSException(com.github.zhenwei.pkix.cms.CMSException) OperatorException(com.github.zhenwei.pkix.operator.OperatorException) GostR3410KeyTransport(com.github.zhenwei.core.asn1.cryptopro.GostR3410KeyTransport) GostR3410TransportParameters(com.github.zhenwei.core.asn1.cryptopro.GostR3410TransportParameters) Gost2814789EncryptedKey(com.github.zhenwei.core.asn1.cryptopro.Gost2814789EncryptedKey) SecretKey(javax.crypto.SecretKey) Iterator(java.util.Iterator) Cipher(javax.crypto.Cipher) KeyAgreement(javax.crypto.KeyAgreement) KeyFactory(java.security.KeyFactory) ASN1ObjectIdentifier(com.github.zhenwei.core.asn1.ASN1ObjectIdentifier) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) Gost2814789EncryptedKey(com.github.zhenwei.core.asn1.cryptopro.Gost2814789EncryptedKey) SecretKey(javax.crypto.SecretKey) OperatorException(com.github.zhenwei.pkix.operator.OperatorException) CMSException(com.github.zhenwei.pkix.cms.CMSException)

Example 4 with OperatorException

use of com.github.zhenwei.pkix.operator.OperatorException in project LinLong-Java by zhenwei1108.

the class JceSymmetricKeyUnwrapper method generateUnwrappedKey.

public GenericKey generateUnwrappedKey(AlgorithmIdentifier encryptedKeyAlgorithm, byte[] encryptedKey) throws OperatorException {
    try {
        Cipher keyCipher = helper.createSymmetricWrapper(this.getAlgorithmIdentifier().getAlgorithm());
        keyCipher.init(Cipher.UNWRAP_MODE, secretKey);
        return new JceGenericKey(encryptedKeyAlgorithm, keyCipher.unwrap(encryptedKey, helper.getKeyAlgorithmName(encryptedKeyAlgorithm.getAlgorithm()), Cipher.SECRET_KEY));
    } catch (InvalidKeyException e) {
        throw new OperatorException("key invalid in message.", e);
    } catch (NoSuchAlgorithmException e) {
        throw new OperatorException("can't find algorithm.", e);
    }
}
Also used : Cipher(javax.crypto.Cipher) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) OperatorException(com.github.zhenwei.pkix.operator.OperatorException)

Example 5 with OperatorException

use of com.github.zhenwei.pkix.operator.OperatorException in project LinLong-Java by zhenwei1108.

the class JceSymmetricKeyWrapper method generateWrappedKey.

public byte[] generateWrappedKey(GenericKey encryptionKey) throws OperatorException {
    Key contentEncryptionKeySpec = OperatorUtils.getJceKey(encryptionKey);
    Cipher keyEncryptionCipher = helper.createSymmetricWrapper(this.getAlgorithmIdentifier().getAlgorithm());
    try {
        keyEncryptionCipher.init(Cipher.WRAP_MODE, wrappingKey, random);
        return keyEncryptionCipher.wrap(contentEncryptionKeySpec);
    } catch (GeneralSecurityException e) {
        throw new OperatorException("cannot wrap key: " + e.getMessage(), e);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) Cipher(javax.crypto.Cipher) Key(java.security.Key) SecretKey(javax.crypto.SecretKey) GenericKey(com.github.zhenwei.pkix.operator.GenericKey) OperatorException(com.github.zhenwei.pkix.operator.OperatorException)

Aggregations

OperatorException (com.github.zhenwei.pkix.operator.OperatorException)12 Cipher (javax.crypto.Cipher)7 GenericKey (com.github.zhenwei.pkix.operator.GenericKey)4 Key (java.security.Key)4 GeneralSecurityException (java.security.GeneralSecurityException)3 InvalidKeyException (java.security.InvalidKeyException)3 PrivateKey (java.security.PrivateKey)3 SecretKey (javax.crypto.SecretKey)3 ASN1OctetString (com.github.zhenwei.core.asn1.ASN1OctetString)2 DERBitString (com.github.zhenwei.core.asn1.DERBitString)2 Gost2814789EncryptedKey (com.github.zhenwei.core.asn1.cryptopro.Gost2814789EncryptedKey)2 GostR3410KeyTransport (com.github.zhenwei.core.asn1.cryptopro.GostR3410KeyTransport)2 GostR3410TransportParameters (com.github.zhenwei.core.asn1.cryptopro.GostR3410TransportParameters)2 AlgorithmIdentifier (com.github.zhenwei.core.asn1.x509.AlgorithmIdentifier)2 AsymmetricBlockCipher (com.github.zhenwei.core.crypto.AsymmetricBlockCipher)2 InvalidCipherTextException (com.github.zhenwei.core.crypto.InvalidCipherTextException)2 DEROtherInfo (com.github.zhenwei.core.crypto.util.DEROtherInfo)2 EncryptedValue (com.github.zhenwei.pkix.util.asn1.crmf.EncryptedValue)2 GOST28147WrapParameterSpec (com.github.zhenwei.provider.jcajce.spec.GOST28147WrapParameterSpec)2 KTSParameterSpec (com.github.zhenwei.provider.jcajce.spec.KTSParameterSpec)2