Search in sources :

Example 1 with Encryptor

use of org.identityconnectors.common.security.Encryptor 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 e) {
            subresult.recordPartialError("Reflection introspection failed", e);
        } catch (IllegalAccessException e) {
            subresult.recordPartialError("Reflection introspection failed", e);
        } catch (NoSuchFieldException e) {
            subresult.recordPartialError("Reflection introspection failed", e);
        } catch (SecurityException 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 opeation 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("GuardeString roundtrip failed; encrypted=" + origString + ", decrypted=" + (new String(decryptedChars)));
                }
            }
        });
        guardedStringSubresult.recordSuccessIfUnknown();
    } catch (Throwable e) {
        LOGGER.error("GuardedString operation error: {}", e.getMessage(), e);
        guardedStringSubresult.recordFatalError("GuardedString opeation error: " + e.getMessage(), e);
    }
    result.computeStatus();
}
Also used : OperationResult(com.evolveum.midpoint.schema.result.OperationResult) Encryptor(org.identityconnectors.common.security.Encryptor) GuardedString(org.identityconnectors.common.security.GuardedString) GuardedString(org.identityconnectors.common.security.GuardedString) Field(java.lang.reflect.Field) EncryptorFactory(org.identityconnectors.common.security.EncryptorFactory) ConnectorKey(org.identityconnectors.framework.api.ConnectorKey) Key(java.security.Key)

Aggregations

OperationResult (com.evolveum.midpoint.schema.result.OperationResult)1 Field (java.lang.reflect.Field)1 Key (java.security.Key)1 Encryptor (org.identityconnectors.common.security.Encryptor)1 EncryptorFactory (org.identityconnectors.common.security.EncryptorFactory)1 GuardedString (org.identityconnectors.common.security.GuardedString)1 ConnectorKey (org.identityconnectors.framework.api.ConnectorKey)1