diff --git a/external-sop/build.gradle b/external-sop/build.gradle index 83bfe3c..87b8e04 100644 --- a/external-sop/build.gradle +++ b/external-sop/build.gradle @@ -23,6 +23,9 @@ dependencies { // Compare version strings implementation 'org.apache.maven:maven-artifact:3.6.3' + + // @Nonnull, @Nullable... + implementation "com.google.code.findbugs:jsr305:$jsrVersion" } test { diff --git a/external-sop/src/main/java/sop/external/ExternalSOP.java b/external-sop/src/main/java/sop/external/ExternalSOP.java index b4763c6..c2ec911 100644 --- a/external-sop/src/main/java/sop/external/ExternalSOP.java +++ b/external-sop/src/main/java/sop/external/ExternalSOP.java @@ -33,9 +33,11 @@ import sop.operation.InlineVerify; import sop.operation.Version; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.nio.file.Files; import java.util.ArrayList; import java.util.List; import java.util.Properties; @@ -44,14 +46,24 @@ public class ExternalSOP implements SOP { private final String binaryName; private final Properties properties; + private final TempDirProvider tempDirProvider; public ExternalSOP(String binaryName) { this(binaryName, new Properties()); } public ExternalSOP(String binaryName, Properties properties) { + this(binaryName, properties, defaultTempDirProvider()); + } + + public ExternalSOP(String binaryName, TempDirProvider tempDirProvider) { + this(binaryName, new Properties(), tempDirProvider); + } + + public ExternalSOP(String binaryName, Properties properties, TempDirProvider tempDirProvider) { this.binaryName = binaryName; this.properties = properties; + this.tempDirProvider = tempDirProvider; } @Override @@ -101,7 +113,7 @@ public class ExternalSOP implements SOP { @Override public Decrypt decrypt() { - return new DecryptExternal(binaryName, properties); + return new DecryptExternal(binaryName, properties, tempDirProvider); } @Override @@ -303,4 +315,17 @@ public class ExternalSOP implements SOP { throw new RuntimeException(e); } } + + public interface TempDirProvider { + File provideTempDirectory() throws IOException; + } + + public static TempDirProvider defaultTempDirProvider() { + return new TempDirProvider() { + @Override + public File provideTempDirectory() throws IOException { + return Files.createTempDirectory("ext-sop").toFile(); + } + }; + } } diff --git a/external-sop/src/main/java/sop/external/operation/DecryptExternal.java b/external-sop/src/main/java/sop/external/operation/DecryptExternal.java index 9373bcd..f6b2b7f 100644 --- a/external-sop/src/main/java/sop/external/operation/DecryptExternal.java +++ b/external-sop/src/main/java/sop/external/operation/DecryptExternal.java @@ -12,6 +12,7 @@ import sop.external.ExternalSOP; import sop.operation.Decrypt; import sop.util.UTCUtil; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -23,6 +24,7 @@ import java.util.Properties; public class DecryptExternal implements Decrypt { + private final ExternalSOP.TempDirProvider tempDirProvider; private final List commandList = new ArrayList<>(); private final List envList; @@ -32,7 +34,8 @@ public class DecryptExternal implements Decrypt { private int keyCounter = 0; private int withKeyPasswordCounter = 0; - public DecryptExternal(String binary, Properties environment) { + public DecryptExternal(String binary, Properties environment, ExternalSOP.TempDirProvider tempDirProvider) { + this.tempDirProvider = tempDirProvider; this.commandList.add(binary); this.commandList.add("decrypt"); this.envList = ExternalSOP.propertiesToEnv(environment); @@ -41,14 +44,14 @@ public class DecryptExternal implements Decrypt { @Override public Decrypt verifyNotBefore(Date timestamp) throws SOPGPException.UnsupportedOption { - this.commandList.add("--not-before=" + UTCUtil.formatUTCDate(timestamp)); + this.commandList.add("--verify-not-before=" + UTCUtil.formatUTCDate(timestamp)); return this; } @Override public Decrypt verifyNotAfter(Date timestamp) throws SOPGPException.UnsupportedOption { - this.commandList.add("--not-after=" + UTCUtil.formatUTCDate(timestamp)); + this.commandList.add("--verify-not-after=" + UTCUtil.formatUTCDate(timestamp)); return this; } @@ -101,6 +104,14 @@ public class DecryptExternal implements Decrypt { public ReadyWithResult ciphertext(InputStream ciphertext) throws SOPGPException.BadData, SOPGPException.MissingArg, SOPGPException.CannotDecrypt, SOPGPException.KeyIsProtected, IOException { + + File tempDir = tempDirProvider.provideTempDirectory(); + File sessionKeyOut = new File(tempDir, "session-key-out"); + commandList.add("--session-key-out=" + sessionKeyOut.getAbsolutePath()); + + File verifyOut = new File(tempDir, "verify-out"); + commandList.add("--verify-out=" + verifyOut.getAbsolutePath()); + String[] command = commandList.toArray(new String[0]); String[] env = envList.toArray(new String[0]); try { diff --git a/sop-java/src/main/java/sop/exception/SOPGPException.java b/sop-java/src/main/java/sop/exception/SOPGPException.java index 71c14a7..7d15705 100644 --- a/sop-java/src/main/java/sop/exception/SOPGPException.java +++ b/sop-java/src/main/java/sop/exception/SOPGPException.java @@ -94,7 +94,7 @@ public abstract class SOPGPException extends RuntimeException { public static final int EXIT_CODE = 19; public MissingArg() { - + } public MissingArg(String message) {