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);
}
}
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;
}
}
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);
}
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);
}
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;
}
}
Aggregations