OpenSSLWrapper.java
3.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* 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;
}
}
}