Search in sources :

Example 1 with Protector

use of com.evolveum.midpoint.prism.crypto.Protector in project midpoint by Evolveum.

the class XNodeProcessorUtil method transformEncryptedValue.

private static void transformEncryptedValue(ProtectedDataType protectedType, PrismContext prismContext) throws SchemaException {
    Protector protector = prismContext.getDefaultProtector();
    if (protector == null) {
        return;
    }
    //		protector.init();
    try {
        protector.decrypt(protectedType);
        Object clearValue = protectedType.getClearValue();
        if (clearValue instanceof String) {
            String clear = (String) clearValue;
            if (clear.startsWith("<value>") && clear.endsWith("</value>")) {
                clear = clear.replace("<value>", "").replace("</value>", "");
                clearValue = (String) clear;
            }
            protectedType.setClearValue(clearValue);
            protector.encrypt(protectedType);
        }
    } catch (EncryptionException ex) {
        //System.out.println("failed to encrypt..");
        throw new IllegalArgumentException("failed to encrypt. " + ex);
    }
}
Also used : EncryptionException(com.evolveum.midpoint.prism.crypto.EncryptionException) Protector(com.evolveum.midpoint.prism.crypto.Protector)

Example 2 with Protector

use of com.evolveum.midpoint.prism.crypto.Protector in project midpoint by Evolveum.

the class PageSecurityQuestions method createUsersSecurityQuestionsList.

public List<SecurityQuestionAnswerDTO> createUsersSecurityQuestionsList(PrismObject<UserType> user) {
    SecurityQuestionsCredentialsType credentialsPolicyType = user.asObjectable().getCredentials().getSecurityQuestions();
    if (credentialsPolicyType == null) {
        return null;
    }
    List<SecurityQuestionAnswerType> secQuestAnsList = credentialsPolicyType.getQuestionAnswer();
    if (secQuestAnsList != null) {
        List<SecurityQuestionAnswerDTO> secQuestAnswListDTO = new ArrayList<SecurityQuestionAnswerDTO>();
        for (Iterator iterator = secQuestAnsList.iterator(); iterator.hasNext(); ) {
            SecurityQuestionAnswerType securityQuestionAnswerType = (SecurityQuestionAnswerType) iterator.next();
            Protector protector = getPrismContext().getDefaultProtector();
            String decoded = "";
            if (securityQuestionAnswerType.getQuestionAnswer().getEncryptedDataType() != null) {
                try {
                    decoded = protector.decryptString(securityQuestionAnswerType.getQuestionAnswer());
                } catch (EncryptionException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            secQuestAnswListDTO.add(new SecurityQuestionAnswerDTO(securityQuestionAnswerType.getQuestionIdentifier(), decoded));
        }
        return secQuestAnswListDTO;
    } else {
        return null;
    }
}
Also used : SecurityQuestionsCredentialsType(com.evolveum.midpoint.xml.ns._public.common.common_3.SecurityQuestionsCredentialsType) SecurityQuestionAnswerDTO(com.evolveum.midpoint.web.page.admin.home.dto.SecurityQuestionAnswerDTO) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) EncryptionException(com.evolveum.midpoint.prism.crypto.EncryptionException) SecurityQuestionAnswerType(com.evolveum.midpoint.xml.ns._public.common.common_3.SecurityQuestionAnswerType) Protector(com.evolveum.midpoint.prism.crypto.Protector)

Example 3 with Protector

use of com.evolveum.midpoint.prism.crypto.Protector in project midpoint by Evolveum.

the class TestProtectedString method testParseProtectedStringEncrypted.

@Test
public void testParseProtectedStringEncrypted() throws Exception {
    final String TEST_NAME = "testParseProtectedStringEncrypted";
    displayTestTitle(TEST_NAME);
    // GIVEN
    Protector protector = PrismInternalTestUtil.createProtector(XMLCipher.AES_128);
    ProtectedStringType protectedStringType = protector.encryptString("salalala");
    PrismContext prismContext = PrismTestUtil.getPrismContext();
    // WHEN
    MapXNode protectedStringTypeXNode = ((PrismContextImpl) prismContext).getBeanMarshaller().marshalProtectedDataType(protectedStringType, null);
    System.out.println("Protected string type XNode: " + protectedStringTypeXNode.debugDump());
    // THEN
    ProtectedStringType unmarshalled = new ProtectedStringType();
    XNodeProcessorUtil.parseProtectedType(unmarshalled, protectedStringTypeXNode, prismContext, ParsingContext.createDefault());
    System.out.println("Unmarshalled value: " + unmarshalled);
    assertEquals("Unmarshalled value differs from the original", protectedStringType, unmarshalled);
}
Also used : PrismContext(com.evolveum.midpoint.prism.PrismContext) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) ProtectedStringType(com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType) TestProtector(com.evolveum.midpoint.prism.crypto.TestProtector) Protector(com.evolveum.midpoint.prism.crypto.Protector) Test(org.testng.annotations.Test)

Example 4 with Protector

use of com.evolveum.midpoint.prism.crypto.Protector in project midpoint by Evolveum.

the class TestProtectedString method testParseProtectedStringHashed.

@Test
public void testParseProtectedStringHashed() throws Exception {
    final String TEST_NAME = "testParseProtectedStringHashed";
    displayTestTitle(TEST_NAME);
    // GIVEN
    ProtectedStringType protectedStringType = new ProtectedStringType();
    protectedStringType.setClearValue("blabla");
    Protector protector = PrismInternalTestUtil.createProtector(XMLCipher.AES_128);
    protector.hash(protectedStringType);
    PrismContext prismContext = PrismTestUtil.getPrismContext();
    // WHEN
    MapXNode protectedStringTypeXNode = ((PrismContextImpl) prismContext).getBeanMarshaller().marshalProtectedDataType(protectedStringType, null);
    System.out.println("Protected string type XNode: " + protectedStringTypeXNode.debugDump());
    // THEN
    ProtectedStringType unmarshalled = new ProtectedStringType();
    XNodeProcessorUtil.parseProtectedType(unmarshalled, protectedStringTypeXNode, prismContext, ParsingContext.createDefault());
    System.out.println("Unmarshalled value: " + unmarshalled);
    assertEquals("Unmarshalled value differs from the original", protectedStringType, unmarshalled);
}
Also used : PrismContext(com.evolveum.midpoint.prism.PrismContext) MapXNode(com.evolveum.midpoint.prism.xnode.MapXNode) ProtectedStringType(com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType) TestProtector(com.evolveum.midpoint.prism.crypto.TestProtector) Protector(com.evolveum.midpoint.prism.crypto.Protector) Test(org.testng.annotations.Test)

Example 5 with Protector

use of com.evolveum.midpoint.prism.crypto.Protector in project midpoint by Evolveum.

the class KeyStoreDumper method execute.

public void execute() {
    try {
        ApplicationContext context = new ClassPathXmlApplicationContext(CONTEXTS);
        Protector protector = context.getBean("protector", Protector.class);
        KeyStore keyStore = protector.getKeyStore();
        System.out.println("###################################################");
        System.out.println("Printing keys from key store");
        if (protector instanceof ProtectorImpl) {
            ProtectorImpl aesProtector = (ProtectorImpl) protector;
            System.out.println("Using key store from location: " + aesProtector.getKeyStorePath());
        //			System.out.println("Cipher: " + aesProtector.getXmlCipher());
        }
        Enumeration<String> aliases = keyStore.aliases();
        while (aliases.hasMoreElements()) {
            String alias = aliases.nextElement();
            System.out.println("===== ALIAS: " + alias + "=====");
            System.out.println("Creation date: " + keyStore.getCreationDate(alias));
            System.out.println("Type: " + keyStore.getType());
            if (keyStore.getCertificate(alias) != null) {
                System.out.println("Certificate: " + keyStore.getCertificate(alias));
            }
            if (keyStore.getCertificateChain(alias) != null) {
                System.out.println("Certificate chain: " + keyStore.getCertificateChain(alias));
            }
            ProtectionParameter protParam = new KeyStore.PasswordProtection("midpoint".toCharArray());
            Entry entry = keyStore.getEntry(alias, protParam);
            if (entry instanceof SecretKeyEntry) {
                System.out.println("Secret key entry: ");
                SecretKeyEntry skEntry = (SecretKeyEntry) entry;
                SecretKey key = skEntry.getSecretKey();
                System.out.println("	Algorithm: " + key.getAlgorithm());
                System.out.println("	Format: " + key.getFormat());
                System.out.println("	Key length: " + key.getEncoded().length * 8);
                if (protector instanceof ProtectorImpl) {
                    System.out.println("	Key name: " + ((ProtectorImpl) protector).getSecretKeyDigest(key));
                }
            //				Cipher cipher = Cipher.getInstance(key.getAlgorithm());
            //				System.out.println("	Cipher algorithm" + cipher.getAlgorithm());
            }
            //TODO: add dump also for other types of keys
            Provider provider = keyStore.getProvider();
            System.out.println("Provder name: " + provider.getName() + "\n");
        }
        System.out.println("###################################################");
    } catch (KeyStoreException ex) {
        System.out.println("Failed to print information about keyStore. Reason: " + ex.getMessage());
        return;
    } catch (UnrecoverableEntryException ex) {
        System.out.println("Failed to print information about keyStore. Reason: " + ex.getMessage());
        return;
    } catch (NoSuchAlgorithmException ex) {
        System.out.println("Failed to print information about keyStore. Reason: " + ex.getMessage());
        return;
    } catch (EncryptionException ex) {
        System.out.println("Failed to print information about keyStore. Reason: " + ex.getMessage());
        return;
    }
}
Also used : ProtectorImpl(com.evolveum.midpoint.prism.crypto.ProtectorImpl) SecretKeyEntry(java.security.KeyStore.SecretKeyEntry) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStore(java.security.KeyStore) Provider(java.security.Provider) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SecretKeyEntry(java.security.KeyStore.SecretKeyEntry) Entry(java.security.KeyStore.Entry) SecretKey(javax.crypto.SecretKey) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) EncryptionException(com.evolveum.midpoint.prism.crypto.EncryptionException) UnrecoverableEntryException(java.security.UnrecoverableEntryException) Protector(com.evolveum.midpoint.prism.crypto.Protector) ProtectionParameter(java.security.KeyStore.ProtectionParameter)

Aggregations

Protector (com.evolveum.midpoint.prism.crypto.Protector)10 EncryptionException (com.evolveum.midpoint.prism.crypto.EncryptionException)5 PrismContext (com.evolveum.midpoint.prism.PrismContext)4 ProtectorImpl (com.evolveum.midpoint.prism.crypto.ProtectorImpl)4 ArrayList (java.util.ArrayList)4 SecurityQuestionAnswerDTO (com.evolveum.midpoint.web.page.admin.home.dto.SecurityQuestionAnswerDTO)3 FunctionLibrary (com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary)2 TestProtector (com.evolveum.midpoint.prism.crypto.TestProtector)2 MapXNode (com.evolveum.midpoint.prism.xnode.MapXNode)2 ObjectResolver (com.evolveum.midpoint.schema.util.ObjectResolver)2 DirectoryFileObjectResolver (com.evolveum.midpoint.test.util.DirectoryFileObjectResolver)2 SecurityQuestionAnswerType (com.evolveum.midpoint.xml.ns._public.common.common_3.SecurityQuestionAnswerType)2 ProtectedStringType (com.evolveum.prism.xml.ns._public.types_3.ProtectedStringType)2 Iterator (java.util.Iterator)2 BeforeClass (org.testng.annotations.BeforeClass)2 Test (org.testng.annotations.Test)2 BasicExpressionFunctions (com.evolveum.midpoint.model.common.expression.functions.BasicExpressionFunctions)1 Jsr223ScriptEvaluator (com.evolveum.midpoint.model.common.expression.script.jsr223.Jsr223ScriptEvaluator)1 SecurityQuestionsCredentialsType (com.evolveum.midpoint.xml.ns._public.common.common_3.SecurityQuestionsCredentialsType)1 KeyStore (java.security.KeyStore)1