Search in sources :

Example 6 with EncryptedDataType

use of com.evolveum.prism.xml.ns._public.types_3.EncryptedDataType 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 7 with EncryptedDataType

use of com.evolveum.prism.xml.ns._public.types_3.EncryptedDataType 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

EncryptedDataType (com.evolveum.prism.xml.ns._public.types_3.EncryptedDataType)6 ProtectedStringType (com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType)5 QName (javax.xml.namespace.QName)4 MapXNode (com.evolveum.midpoint.prism.xnode.MapXNode)3 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)2 CipherDataType (com.evolveum.prism.xml.ns._public.types_3.CipherDataType)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 ParsingContext (com.evolveum.midpoint.prism.ParsingContext)1 PrismContext (com.evolveum.midpoint.prism.PrismContext)1 EncryptionException (com.evolveum.midpoint.prism.crypto.EncryptionException)1 Protector (com.evolveum.midpoint.prism.crypto.Protector)1