Search in sources :

Example 1 with CryptoException

use of org.craftercms.commons.crypto.CryptoException in project profile by craftercms.

the class RememberMeManagerImpl method serializeLogin.

protected String serializeLogin(PersistentLogin login) throws RememberMeException {
    StringBuilder serializedLogin = new StringBuilder();
    serializedLogin.append(login.getId()).append(SERIALIZED_LOGIN_SEPARATOR);
    serializedLogin.append(login.getProfileId()).append(SERIALIZED_LOGIN_SEPARATOR);
    serializedLogin.append(login.getToken());
    try {
        return encryptor.encrypt(serializedLogin.toString());
    } catch (CryptoException e) {
        throw new RememberMeException("Unable to encrypt remember me cookie", e);
    }
}
Also used : CryptoException(org.craftercms.commons.crypto.CryptoException) RememberMeException(org.craftercms.security.exception.rememberme.RememberMeException)

Example 2 with CryptoException

use of org.craftercms.commons.crypto.CryptoException in project commons by craftercms.

the class SecretKeyRepositoryImpl method getKey.

@Override
public SecretKey getKey(String name, boolean create) throws CryptoException {
    try {
        SecretKey key = (SecretKey) keyStore.getKey(name, keyStorePassword);
        if (key == null) {
            logger.debug(LOG_KEY_KEY_NOT_FOUND, name);
            if (create) {
                key = CryptoUtils.generateKey(defaultKeyAlgorithm);
                saveKey(name, key);
                logger.debug(LOG_KEY_KEY_CREATED, name);
            }
        } else {
            logger.debug(LOG_KEY_KEY_FOUND, name);
        }
        return key;
    } catch (GeneralSecurityException e) {
        throw new CryptoException(ERROR_KEY_GET_KEY_ERROR, e);
    }
}
Also used : SecretKey(javax.crypto.SecretKey) GeneralSecurityException(java.security.GeneralSecurityException) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 3 with CryptoException

use of org.craftercms.commons.crypto.CryptoException in project commons by craftercms.

the class SecretKeyRepositoryImpl method saveKey.

@Override
public void saveKey(String name, SecretKey key) throws CryptoException {
    KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(keyStorePassword);
    KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(key);
    try {
        keyStore.setEntry(name, entry, protParam);
    } catch (GeneralSecurityException e) {
        throw new CryptoException(ERROR_KEY_SAVE_KEY_ERROR, e);
    }
    logger.debug(LOG_KEY_KEY_SAVED, name);
    storeKeyStore();
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) CryptoException(org.craftercms.commons.crypto.CryptoException) KeyStore(java.security.KeyStore)

Example 4 with CryptoException

use of org.craftercms.commons.crypto.CryptoException in project commons by craftercms.

the class PbkAesTextEncryptor method generateKey.

private static Key generateKey(String password, String salt) throws CryptoException {
    try {
        KeySpec keySpec = new PBEKeySpec(password.toCharArray(), Base64.decodeBase64(salt), PBK_ITER, PBK_LEN);
        SecretKeyFactory factory = SecretKeyFactory.getInstance(PBK_ALGORITHM);
        return new SecretKeySpec(factory.generateSecret(keySpec).getEncoded(), CryptoUtils.AES_CIPHER_ALGORITHM);
    } catch (GeneralSecurityException e) {
        throw new CryptoException("Unable to generate PBK key", e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) CryptoException(org.craftercms.commons.crypto.CryptoException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 5 with CryptoException

use of org.craftercms.commons.crypto.CryptoException in project commons by craftercms.

the class EncryptionTool method main.

public static final void main(String... args) {
    Options options = createOptions();
    CommandLineParser parser = new DefaultParser();
    try {
        CommandLine line = parser.parse(options, args);
        if (line.hasOption(HELP_OPTION)) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("java -jar JARNAME [-e|-d|-e64|-d64 text] [-p password] [-s salt]", options);
        } else if (line.hasOption(ENC_OPTION)) {
            encrypt(line);
        } else if (line.hasOption(DEC_OPTION)) {
            decrypt(line);
        } else if (line.hasOption(ENC_BASE64_OPTION)) {
            encodeBase64(line);
        } else if (line.hasOption(DEC_BASE64_OPTION)) {
            decodeBase64(line);
        } else {
            throw new MissingOptionException("Either -" + ENC_OPTION + ", -" + DEC_OPTION + ", -" + ENC_BASE64_OPTION + ", -" + DEC_BASE64_OPTION + " or -" + HELP_OPTION + " must be provided");
        }
    } catch (ParseException e) {
        System.err.println("Error parsing command line. Reason:");
        e.printStackTrace(System.err);
    } catch (CryptoException e) {
        System.err.println("Error while executing encryption/decryption. Reason:");
        e.printStackTrace(System.err);
    }
}
Also used : HelpFormatter(org.apache.commons.cli.HelpFormatter) Options(org.apache.commons.cli.Options) CommandLine(org.apache.commons.cli.CommandLine) CommandLineParser(org.apache.commons.cli.CommandLineParser) ParseException(org.apache.commons.cli.ParseException) CryptoException(org.craftercms.commons.crypto.CryptoException) MissingOptionException(org.apache.commons.cli.MissingOptionException) DefaultParser(org.apache.commons.cli.DefaultParser)

Aggregations

CryptoException (org.craftercms.commons.crypto.CryptoException)6 GeneralSecurityException (java.security.GeneralSecurityException)3 RememberMeException (org.craftercms.security.exception.rememberme.RememberMeException)2 KeyStore (java.security.KeyStore)1 KeySpec (java.security.spec.KeySpec)1 SecretKey (javax.crypto.SecretKey)1 SecretKeyFactory (javax.crypto.SecretKeyFactory)1 PBEKeySpec (javax.crypto.spec.PBEKeySpec)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1 CommandLine (org.apache.commons.cli.CommandLine)1 CommandLineParser (org.apache.commons.cli.CommandLineParser)1 DefaultParser (org.apache.commons.cli.DefaultParser)1 HelpFormatter (org.apache.commons.cli.HelpFormatter)1 MissingOptionException (org.apache.commons.cli.MissingOptionException)1 Options (org.apache.commons.cli.Options)1 ParseException (org.apache.commons.cli.ParseException)1 PersistentLogin (org.craftercms.profile.api.PersistentLogin)1 InvalidCookieException (org.craftercms.security.exception.rememberme.InvalidCookieException)1