Search in sources :

Example 1 with EncryptionMethodType

use of com.evolveum.prism.xml.ns._public.types_3.EncryptionMethodType in project midpoint by Evolveum.

the class ProtectorImpl method decryptBytes.

@Override
protected <T> byte[] decryptBytes(ProtectedData<T> protectedData) throws SchemaException, EncryptionException {
    EncryptedDataType encryptedDataType = protectedData.getEncryptedDataType();
    EncryptionMethodType encryptionMethodType = encryptedDataType.getEncryptionMethod();
    if (encryptionMethodType == null) {
        throw new SchemaException("No encryptionMethod element in protected data");
    }
    String algorithmUri = encryptionMethodType.getAlgorithm();
    if (StringUtils.isBlank(algorithmUri)) {
        throw new SchemaException("No algorithm URI in encryptionMethod element in protected data");
    }
    KeyInfoType keyInfo = encryptedDataType.getKeyInfo();
    if (keyInfo == null) {
        throw new SchemaException("No keyInfo element in protected data");
    }
    String keyName = keyInfo.getKeyName();
    if (StringUtils.isBlank(keyName)) {
        throw new SchemaException("No keyName defined in keyInfo element in protected data");
    }
    SecretKey key = getSecretKeyByDigest(keyName);
    CipherDataType cipherData = encryptedDataType.getCipherData();
    if (cipherData == null) {
        throw new SchemaException("No cipherData element in protected data");
    }
    byte[] encryptedBytes = cipherData.getCipherValue();
    if (encryptedBytes == null || encryptedBytes.length == 0) {
        throw new SchemaException("No cipherValue in cipherData element in protected data");
    }
    byte[] decryptedData;
    try {
        decryptedData = decryptBytes(encryptedBytes, algorithmUri, key);
    } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
        throw new EncryptionException(e.getMessage(), e);
    }
    return decryptedData;
}
Also used : SchemaException(com.evolveum.midpoint.util.exception.SchemaException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) EncryptedDataType(com.evolveum.prism.xml.ns._public.types_3.EncryptedDataType) EncryptionMethodType(com.evolveum.prism.xml.ns._public.types_3.EncryptionMethodType) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) KeyInfoType(com.evolveum.prism.xml.ns._public.types_3.KeyInfoType) SecretKey(javax.crypto.SecretKey) CipherDataType(com.evolveum.prism.xml.ns._public.types_3.CipherDataType) NoSuchProviderException(java.security.NoSuchProviderException)

Example 2 with EncryptionMethodType

use of com.evolveum.prism.xml.ns._public.types_3.EncryptionMethodType in project midpoint by Evolveum.

the class ProtectorImpl method encrypt.

@Override
public <T> void encrypt(ProtectedData<T> protectedData) throws EncryptionException {
    if (protectedData.isEncrypted()) {
        throw new IllegalArgumentException("Attempt to encrypt protected data that are already encrypted");
    }
    SecretKey key = getSecretKeyByAlias(getEncryptionKeyAlias());
    String algorithm = getCipherAlgorithm();
    byte[] clearBytes = protectedData.getClearBytes();
    byte[] encryptedBytes;
    try {
        encryptedBytes = encryptBytes(clearBytes, algorithm, key);
    } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | NoSuchProviderException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
        throw new EncryptionException(e.getMessage(), e);
    }
    // Construct encryption types
    EncryptedDataType encryptedDataType = new EncryptedDataType();
    EncryptionMethodType encryptionMethodType = new EncryptionMethodType();
    encryptionMethodType.setAlgorithm(algorithm);
    encryptedDataType.setEncryptionMethod(encryptionMethodType);
    KeyInfoType keyInfoType = new KeyInfoType();
    keyInfoType.setKeyName(getSecretKeyDigest(key));
    encryptedDataType.setKeyInfo(keyInfoType);
    CipherDataType cipherDataType = new CipherDataType();
    cipherDataType.setCipherValue(encryptedBytes);
    encryptedDataType.setCipherData(cipherDataType);
    protectedData.setEncryptedData(encryptedDataType);
    protectedData.destroyCleartext();
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) EncryptedDataType(com.evolveum.prism.xml.ns._public.types_3.EncryptedDataType) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) EncryptionMethodType(com.evolveum.prism.xml.ns._public.types_3.EncryptionMethodType) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) BadPaddingException(javax.crypto.BadPaddingException) InvalidKeyException(java.security.InvalidKeyException) KeyInfoType(com.evolveum.prism.xml.ns._public.types_3.KeyInfoType) SecretKey(javax.crypto.SecretKey) CipherDataType(com.evolveum.prism.xml.ns._public.types_3.CipherDataType) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

CipherDataType (com.evolveum.prism.xml.ns._public.types_3.CipherDataType)2 EncryptedDataType (com.evolveum.prism.xml.ns._public.types_3.EncryptedDataType)2 EncryptionMethodType (com.evolveum.prism.xml.ns._public.types_3.EncryptionMethodType)2 KeyInfoType (com.evolveum.prism.xml.ns._public.types_3.KeyInfoType)2 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)2 InvalidKeyException (java.security.InvalidKeyException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 NoSuchProviderException (java.security.NoSuchProviderException)2 BadPaddingException (javax.crypto.BadPaddingException)2 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 SecretKey (javax.crypto.SecretKey)2 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)1