OpenSSLWrapper.java 3.07 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.commons.exec.CommandLine
 *  org.apache.commons.exec.DefaultExecutor
 *  org.apache.commons.exec.ExecuteWatchdog
 */
package com.adobe.cq.dtm.impl.handler.openssl;

import java.io.File;
import java.io.IOException;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteWatchdog;

public class OpenSSLWrapper {
    private static final String OPENSSL_CMD = "openssl";
    private static final String DECRYPT_ARG = "-d";
    private static final String INPUT_ARG = "-in";
    private static final String OUTPUT_ARG = "-out";
    private static final String PASSPHRASE_ARG = "-k";
    private File input;
    private File output;
    private OpenSSLCypher cypher;
    private long processTimeout = 5000;
    private CommandLine cmdLine = new CommandLine("openssl");

    public OpenSSLWrapper(OpenSSLCypher cypher, File input, File output) {
        if (cypher == null) {
            throw new IllegalArgumentException("cypher parameter must not be null.");
        }
        if (input == null || !input.exists()) {
            throw new IllegalArgumentException("input file must not be null and must exists.");
        }
        if (output == null || !output.exists()) {
            throw new IllegalArgumentException("output file must not be null and must exists");
        }
        this.cypher = cypher;
        this.input = input;
        this.output = output;
        this.initOpenSSLCommandArgs();
    }

    public void setPassPhrase(String passPhrase) {
        if (passPhrase == null) {
            throw new IllegalArgumentException("passPhrase parameter must not be null.");
        }
        this.cmdLine.addArgument("-k");
        this.cmdLine.addArgument(passPhrase);
    }

    public long getProcessTimeout() {
        return this.processTimeout;
    }

    public void setProcessTimeout(long processTimeout) {
        this.processTimeout = processTimeout;
    }

    public boolean decryptFile() throws IOException {
        DefaultExecutor executor = new DefaultExecutor();
        ExecuteWatchdog watchdog = new ExecuteWatchdog(this.processTimeout);
        executor.setWatchdog(watchdog);
        int exitCode = executor.execute(this.cmdLine);
        return !executor.isFailure(exitCode);
    }

    private void initOpenSSLCommandArgs() {
        this.cmdLine.addArgument(this.cypher.toString());
        this.cmdLine.addArgument("-d");
        this.cmdLine.addArgument("-in");
        this.cmdLine.addArgument(this.input.getAbsolutePath());
        this.cmdLine.addArgument("-out");
        this.cmdLine.addArgument(this.output.getAbsolutePath());
    }

    public static enum OpenSSLCypher {
        AES_256_CBC("aes-256-cbc"),
        AES_256_ECB("aes-256-ecb"),
        AES_128_CBC("aes-128-cbc"),
        AES_128_ECB("aes-128-ecb");
        
        private final String value;

        private OpenSSLCypher(String value) {
            this.value = value;
        }

        public String toString() {
            return this.value;
        }
    }

}