use of com.evolveum.prism.xml.ns._public.types_3.CipherDataType 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;
}
use of com.evolveum.prism.xml.ns._public.types_3.CipherDataType 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();
}
Aggregations