Search in sources :

Example 16 with ShellMethod

use of org.springframework.shell.standard.ShellMethod in project cas by apereo.

the class StringableCipherExecutorCommand method decipher.

/**
 * Decipher.
 *
 * @param value                   the value
 * @param secretKeyEncryption     the secret key encryption
 * @param secretKeySigning        the secret key signing
 * @param secretKeyEncryptionSize the secret key encryption size
 * @param secretKeySigningSize    the secret key signing size
 * @param encryptionEnabled       the encryption enabled
 * @param signingEnabled          the signing enabled
 * @return the string
 */
@SneakyThrows
@ShellMethod(key = { "decipher-text", "decode-text" }, value = "Decrypt and verify text data using keys")
public String decipher(@ShellOption(value = { "value", "--value" }, defaultValue = ShellOption.NULL, help = "Value to put through the cipher") final String value, @ShellOption(value = { "encryption-key", "--encryption-key" }, defaultValue = ShellOption.NULL, help = "Encryption key") final String secretKeyEncryption, @ShellOption(value = { "signing-key", "--signing-key" }, defaultValue = ShellOption.NULL, help = "Signing key") final String secretKeySigning, @ShellOption(value = { "encryption-key-size", "--encryption-key-size" }, defaultValue = StringUtils.EMPTY + CipherExecutor.DEFAULT_STRINGABLE_ENCRYPTION_KEY_SIZE, help = "Encryption key size") final int secretKeyEncryptionSize, @ShellOption(value = { "signing-key-size", "--signing-key-size" }, defaultValue = StringUtils.EMPTY + CipherExecutor.DEFAULT_STRINGABLE_SIGNING_KEY_SIZE, help = "Signing key size") final int secretKeySigningSize, @ShellOption(value = { "enable-encryption", "--enable-encryption" }, defaultValue = "true", help = "Whether value should be encrypted") final boolean encryptionEnabled, @ShellOption(value = { "enable-signing", "--enable-signing" }, defaultValue = "true", help = "Whether value should be signed") final boolean signingEnabled) {
    var toEncode = value;
    if (value != null && new File(value).exists()) {
        toEncode = FileUtils.readFileToString(new File(value), StandardCharsets.UTF_8);
    }
    if (StringUtils.isNotBlank(toEncode)) {
        val cipher = new ShellStringCipherExecutor(secretKeyEncryption, secretKeySigning, encryptionEnabled, signingEnabled, secretKeySigningSize, secretKeyEncryptionSize);
        val decoded = cipher.decode(toEncode);
        LOGGER.info("Decoded value: [{}]", decoded);
        return decoded;
    }
    return null;
}
Also used : lombok.val(lombok.val) File(java.io.File) ShellMethod(org.springframework.shell.standard.ShellMethod) SneakyThrows(lombok.SneakyThrows)

Example 17 with ShellMethod

use of org.springframework.shell.standard.ShellMethod in project cas by apereo.

the class JasyptListAlgorithmsCommand method listAlgorithms.

/**
 * List algorithms you can use Jasypt.
 *
 * @param includeBC whether to include the BouncyCastle provider
 */
@ShellMethod(key = "jasypt-list-algorithms", value = "List alogrithms you can use with Jasypt for property encryption")
public void listAlgorithms(@ShellOption(value = { "includeBC", "--includeBC" }, help = "Include Bouncy Castle provider", defaultValue = "false") final Boolean includeBC) {
    if (includeBC) {
        Security.addProvider(new BouncyCastleProvider());
    } else {
        Security.removeProvider(BouncyCastleProvider.PROVIDER_NAME);
    }
    val providers = Security.getProviders();
    LOGGER.info("Loaded providers: ");
    for (val provider : providers) {
        LOGGER.info("Provider: [{}] [{}]", provider.getName(), provider.getClass().getName());
    }
    val pbeAlgos = AlgorithmRegistry.getAllPBEAlgorithms();
    LOGGER.info("==== JASYPT Password Based Encryption Algorithms ====\n");
    for (val pbeAlgo : pbeAlgos) {
        LOGGER.info(pbeAlgo.toString());
    }
}
Also used : lombok.val(lombok.val) BouncyCastleProvider(org.bouncycastle.jce.provider.BouncyCastleProvider) ShellMethod(org.springframework.shell.standard.ShellMethod)

Example 18 with ShellMethod

use of org.springframework.shell.standard.ShellMethod in project cas by apereo.

the class JasyptTestAlgorithmsCommand method validateAlgorithms.

/**
 * List algorithms you can use Jasypt.
 */
@ShellMethod(key = "jasypt-test-algorithms", value = "Test encryption algorithms you can use with Jasypt to make sure encryption and decryption both work")
public void validateAlgorithms() {
    val providers = new String[] { BouncyCastleProvider.PROVIDER_NAME, "SunJCE" };
    LOGGER.info("==== JASYPT Password Based Encryption Algorithms ====\n");
    val password = "SecretKeyValue";
    val value = "ValueToEncrypt";
    val pbeAlgos = AlgorithmRegistry.getAllPBEAlgorithms();
    for (val provider : providers) {
        LOGGER.trace("Testing provider [{}]", provider);
        for (val algorithm : pbeAlgos) {
            val cipher = new CasConfigurationJasyptCipherExecutor(this.environment);
            val algorithmStr = algorithm.toString();
            cipher.setPassword(password);
            cipher.setKeyObtentionIterations("1");
            cipher.setProviderName(provider);
            if (cipher.isVectorInitializationRequiredFor(algorithmStr)) {
                cipher.configureInitializationVector();
            }
            try {
                var encryptedValue = StringUtils.EMPTY;
                try {
                    LOGGER.trace("Testing algorithm [{}]", algorithmStr);
                    cipher.setAlgorithm(algorithmStr);
                    encryptedValue = cipher.encryptValuePropagateExceptions(value);
                } catch (final Exception e) {
                    LOGGER.trace(e.getMessage(), e);
                    continue;
                }
                LOGGER.info("Provider: [{}] Algorithm: [{}]", provider, algorithmStr);
                try {
                    cipher.decryptValuePropagateExceptions(encryptedValue);
                    LOGGER.info("Encrypted Value: [{}] Decryption succeeded", encryptedValue);
                } catch (final Exception e) {
                    LOGGER.warn("Encrypted Value: [{}] Decryption Failed", encryptedValue);
                }
            } catch (final Exception e) {
                if (e.getCause() instanceof NoSuchAlgorithmException) {
                    LOGGER.warn("Provider: [{}] does not support Algorithm: [{}]", provider, algorithmStr);
                } else {
                    LOGGER.warn("Error encrypting using provider: [{}] and algorithm: [{}], Message: [{}]", provider, algorithmStr, e.getMessage());
                }
            }
        }
    }
}
Also used : lombok.val(lombok.val) CasConfigurationJasyptCipherExecutor(org.apereo.cas.configuration.support.CasConfigurationJasyptCipherExecutor) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) ShellMethod(org.springframework.shell.standard.ShellMethod)

Aggregations

lombok.val (lombok.val)18 ShellMethod (org.springframework.shell.standard.ShellMethod)18 File (java.io.File)7 CasConfigurationJasyptCipherExecutor (org.apereo.cas.configuration.support.CasConfigurationJasyptCipherExecutor)3 SneakyThrows (lombok.SneakyThrows)2 BouncyCastleProvider (org.bouncycastle.jce.provider.BouncyCastleProvider)2 BufferedReader (java.io.BufferedReader)1 InputStreamReader (java.io.InputStreamReader)1 Writer (java.io.Writer)1 HttpURLConnection (java.net.HttpURLConnection)1 Charset (java.nio.charset.Charset)1 Files (java.nio.file.Files)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Provider (java.security.Provider)1 HashMap (java.util.HashMap)1 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)1 Entity (javax.persistence.Entity)1 MappedSuperclass (javax.persistence.MappedSuperclass)1 Slf4j (lombok.extern.slf4j.Slf4j)1 StringUtils (org.apache.commons.lang3.StringUtils)1