use of org.identityconnectors.common.security.EncryptorFactory in project midpoint by Evolveum.
the class ConnectorFactoryConnIdImpl method selfTestGuardedString.
private void selfTestGuardedString(OperationResult parentTestResult) {
OperationResult result = parentTestResult.createSubresult(ConnectorFactoryConnIdImpl.class + ".selfTestGuardedString");
OperationResult subresult = result.createSubresult(ConnectorFactoryConnIdImpl.class + ".selfTestGuardedString.encryptorReflection");
EncryptorFactory encryptorFactory = EncryptorFactory.getInstance();
subresult.addReturn("encryptorFactoryImpl", encryptorFactory.getClass());
LOGGER.debug("Encryptor factory implementation class: {}", encryptorFactory.getClass());
Encryptor encryptor = EncryptorFactory.getInstance().newRandomEncryptor();
subresult.addReturn("encryptorImpl", encryptor.getClass());
LOGGER.debug("Encryptor implementation class: {}", encryptor.getClass());
if (encryptor.getClass().getName().equals("org.identityconnectors.common.security.impl.EncryptorImpl")) {
// let's do some reflection magic to have a look inside
try {
LOGGER.trace("Encryptor fields: {}", Arrays.asList(encryptor.getClass().getDeclaredFields()));
Field keyField = encryptor.getClass().getDeclaredField("key");
keyField.setAccessible(true);
Key key = (Key) keyField.get(encryptor);
subresult.addReturn("keyAlgorithm", key.getAlgorithm());
subresult.addReturn("keyLength", key.getEncoded().length * 8);
subresult.addReturn("keyFormat", key.getFormat());
subresult.recordSuccess();
} catch (IllegalArgumentException | SecurityException | NoSuchFieldException | IllegalAccessException e) {
subresult.recordPartialError("Reflection introspection failed", e);
}
}
OperationResult encryptorSubresult = result.createSubresult(ConnectorFactoryConnIdImpl.class + ".selfTestGuardedString.encryptor");
try {
String plainString = "Scurvy seadog";
byte[] encryptedBytes = encryptor.encrypt(plainString.getBytes());
byte[] decryptedBytes = encryptor.decrypt(encryptedBytes);
String decryptedString = new String(decryptedBytes);
if (!plainString.equals(decryptedString)) {
encryptorSubresult.recordFatalError("Encryptor roundtrip failed; encrypted=" + plainString + ", decrypted=" + decryptedString);
} else {
encryptorSubresult.recordSuccess();
}
} catch (Throwable e) {
LOGGER.error("Encryptor operation error: {}", e.getMessage(), e);
encryptorSubresult.recordFatalError("Encryptor operation error: " + e.getMessage(), e);
}
final OperationResult guardedStringSubresult = result.createSubresult(ConnectorFactoryConnIdImpl.class + ".selfTestGuardedString.guardedString");
// try to encrypt and decrypt GuardedString
try {
final String origString = "Shiver me timbers";
// This should encrypt it
GuardedString guardedString = new GuardedString(origString.toCharArray());
// and this should decrypt it
guardedString.access(new GuardedString.Accessor() {
@Override
public void access(char[] decryptedChars) {
if (!(new String(decryptedChars)).equals(origString)) {
guardedStringSubresult.recordFatalError("GuardedString roundtrip failed; encrypted=" + origString + ", decrypted=" + (new String(decryptedChars)));
}
}
});
guardedStringSubresult.recordSuccessIfUnknown();
} catch (Throwable e) {
LOGGER.error("GuardedString operation error: {}", e.getMessage(), e);
guardedStringSubresult.recordFatalError("GuardedString operation error: " + e.getMessage(), e);
}
result.computeStatus();
}
Aggregations