2023-01-05 03:06:22 +01:00
|
|
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2023-01-07 15:23:57 +01:00
|
|
|
package sop.external;
|
2023-01-05 03:06:22 +01:00
|
|
|
|
2023-01-09 19:48:25 +01:00
|
|
|
import sop.Ready;
|
2023-01-05 03:06:22 +01:00
|
|
|
import sop.SOP;
|
2023-01-09 14:56:53 +01:00
|
|
|
import sop.exception.SOPGPException;
|
2023-01-09 19:48:25 +01:00
|
|
|
import sop.external.operation.ArmorExternal;
|
|
|
|
import sop.external.operation.DearmorExternal;
|
|
|
|
import sop.external.operation.DecryptExternal;
|
|
|
|
import sop.external.operation.DetachedSignExternal;
|
|
|
|
import sop.external.operation.DetachedVerifyExternal;
|
|
|
|
import sop.external.operation.EncryptExternal;
|
2023-01-07 15:23:57 +01:00
|
|
|
import sop.external.operation.ExtractCertExternal;
|
|
|
|
import sop.external.operation.GenerateKeyExternal;
|
2023-01-09 19:48:25 +01:00
|
|
|
import sop.external.operation.InlineDetachExternal;
|
|
|
|
import sop.external.operation.InlineSignExternal;
|
|
|
|
import sop.external.operation.InlineVerifyExternal;
|
2023-01-07 15:23:57 +01:00
|
|
|
import sop.external.operation.VersionExternal;
|
2023-01-05 03:06:22 +01:00
|
|
|
import sop.operation.Armor;
|
|
|
|
import sop.operation.Dearmor;
|
|
|
|
import sop.operation.Decrypt;
|
|
|
|
import sop.operation.DetachedSign;
|
|
|
|
import sop.operation.DetachedVerify;
|
|
|
|
import sop.operation.Encrypt;
|
|
|
|
import sop.operation.ExtractCert;
|
|
|
|
import sop.operation.GenerateKey;
|
|
|
|
import sop.operation.InlineDetach;
|
|
|
|
import sop.operation.InlineSign;
|
|
|
|
import sop.operation.InlineVerify;
|
|
|
|
import sop.operation.Version;
|
|
|
|
|
2023-01-19 17:51:29 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2023-01-09 14:56:53 +01:00
|
|
|
import java.io.ByteArrayOutputStream;
|
2023-01-12 13:53:49 +01:00
|
|
|
import java.io.File;
|
2023-01-09 14:56:53 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
2023-01-09 19:48:25 +01:00
|
|
|
import java.io.OutputStream;
|
2023-01-12 13:53:49 +01:00
|
|
|
import java.nio.file.Files;
|
2023-01-12 15:08:26 +01:00
|
|
|
import java.nio.file.attribute.FileAttribute;
|
2023-01-09 14:56:53 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
2023-01-19 17:29:29 +01:00
|
|
|
/**
|
|
|
|
* Implementation of the {@link SOP} API using an external SOP binary.
|
|
|
|
*/
|
2023-01-07 15:23:57 +01:00
|
|
|
public class ExternalSOP implements SOP {
|
2023-01-05 03:06:22 +01:00
|
|
|
|
|
|
|
private final String binaryName;
|
2023-01-09 14:56:53 +01:00
|
|
|
private final Properties properties;
|
2023-01-12 13:53:49 +01:00
|
|
|
private final TempDirProvider tempDirProvider;
|
2023-01-05 03:06:22 +01:00
|
|
|
|
2023-01-19 17:51:29 +01:00
|
|
|
/**
|
|
|
|
* Instantiate an {@link ExternalSOP} object for the given binary and pass it empty environment variables,
|
|
|
|
* as well as a default {@link TempDirProvider}.
|
|
|
|
*
|
|
|
|
* @param binaryName name / path of the SOP binary
|
|
|
|
*/
|
|
|
|
public ExternalSOP(@Nonnull String binaryName) {
|
2023-01-09 14:56:53 +01:00
|
|
|
this(binaryName, new Properties());
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:51:29 +01:00
|
|
|
/**
|
|
|
|
* Instantiate an {@link ExternalSOP} object for the given binary, and pass it the given properties as
|
|
|
|
* environment variables, as well as a default {@link TempDirProvider}.
|
|
|
|
*
|
|
|
|
* @param binaryName name / path of the SOP binary
|
|
|
|
* @param properties environment variables
|
|
|
|
*/
|
|
|
|
public ExternalSOP(@Nonnull String binaryName, @Nonnull Properties properties) {
|
2023-01-12 13:53:49 +01:00
|
|
|
this(binaryName, properties, defaultTempDirProvider());
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:51:29 +01:00
|
|
|
/**
|
|
|
|
* Instantiate an {@link ExternalSOP} object for the given binary and the given {@link TempDirProvider}
|
|
|
|
* using empty environment variables.
|
|
|
|
*
|
|
|
|
* @param binaryName name / path of the SOP binary
|
|
|
|
* @param tempDirProvider custom tempDirProvider
|
|
|
|
*/
|
|
|
|
public ExternalSOP(@Nonnull String binaryName, @Nonnull TempDirProvider tempDirProvider) {
|
2023-01-12 13:53:49 +01:00
|
|
|
this(binaryName, new Properties(), tempDirProvider);
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:51:29 +01:00
|
|
|
/**
|
|
|
|
* Instantiate an {@link ExternalSOP} object for the given binary using the given properties and
|
|
|
|
* custom {@link TempDirProvider}.
|
|
|
|
*
|
|
|
|
* @param binaryName name / path of the SOP binary
|
|
|
|
* @param properties environment variables
|
|
|
|
* @param tempDirProvider tempDirProvider
|
|
|
|
*/
|
|
|
|
public ExternalSOP(@Nonnull String binaryName, @Nonnull Properties properties, @Nonnull TempDirProvider tempDirProvider) {
|
2023-01-05 03:06:22 +01:00
|
|
|
this.binaryName = binaryName;
|
2023-01-09 14:56:53 +01:00
|
|
|
this.properties = properties;
|
2023-01-12 13:53:49 +01:00
|
|
|
this.tempDirProvider = tempDirProvider;
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Version version() {
|
2023-01-09 14:56:53 +01:00
|
|
|
return new VersionExternal(binaryName, properties);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GenerateKey generateKey() {
|
2023-01-09 14:56:53 +01:00
|
|
|
return new GenerateKeyExternal(binaryName, properties);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ExtractCert extractCert() {
|
2023-01-09 14:56:53 +01:00
|
|
|
return new ExtractCertExternal(binaryName, properties);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DetachedSign detachedSign() {
|
2023-01-12 14:40:00 +01:00
|
|
|
return new DetachedSignExternal(binaryName, properties, tempDirProvider);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InlineSign inlineSign() {
|
2023-01-09 19:48:25 +01:00
|
|
|
return new InlineSignExternal(binaryName, properties);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public DetachedVerify detachedVerify() {
|
2023-01-09 19:48:25 +01:00
|
|
|
return new DetachedVerifyExternal(binaryName, properties);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InlineVerify inlineVerify() {
|
2023-01-12 14:58:42 +01:00
|
|
|
return new InlineVerifyExternal(binaryName, properties, tempDirProvider);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public InlineDetach inlineDetach() {
|
2023-01-12 14:58:42 +01:00
|
|
|
return new InlineDetachExternal(binaryName, properties, tempDirProvider);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Encrypt encrypt() {
|
2023-01-09 19:48:25 +01:00
|
|
|
return new EncryptExternal(binaryName, properties);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Decrypt decrypt() {
|
2023-01-12 13:53:49 +01:00
|
|
|
return new DecryptExternal(binaryName, properties, tempDirProvider);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Armor armor() {
|
2023-01-09 19:48:25 +01:00
|
|
|
return new ArmorExternal(binaryName, properties);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Dearmor dearmor() {
|
2023-01-09 19:48:25 +01:00
|
|
|
return new DearmorExternal(binaryName, properties);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
2023-01-09 14:56:53 +01:00
|
|
|
|
2023-01-19 17:51:29 +01:00
|
|
|
public static void finish(@Nonnull Process process) throws IOException {
|
2023-01-09 14:56:53 +01:00
|
|
|
try {
|
|
|
|
mapExitCodeOrException(process);
|
2023-01-19 17:51:29 +01:00
|
|
|
} catch (InterruptedException e) {
|
2023-01-09 14:56:53 +01:00
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:51:29 +01:00
|
|
|
/**
|
|
|
|
* Wait for the {@link Process} to finish and read out its exit code.
|
|
|
|
* If the exit code is {@value "0"}, this method just returns.
|
|
|
|
* Otherwise, the exit code gets mapped to a {@link SOPGPException} which then gets thrown.
|
|
|
|
* If the exit code does not match any of the known exit codes defined in the SOP specification,
|
|
|
|
* this method throws a {@link RuntimeException} instead.
|
|
|
|
*
|
|
|
|
* @param process process
|
|
|
|
* @throws InterruptedException if the thread is interrupted before the process could exit
|
|
|
|
* @throws IOException in case of an IO error
|
|
|
|
*/
|
|
|
|
private static void mapExitCodeOrException(@Nonnull Process process) throws InterruptedException, IOException {
|
2023-01-22 15:07:17 +01:00
|
|
|
// wait for process termination
|
2023-01-09 14:56:53 +01:00
|
|
|
int exitCode = process.waitFor();
|
|
|
|
|
|
|
|
if (exitCode == 0) {
|
2023-01-22 15:07:17 +01:00
|
|
|
// we're good, bye
|
2023-01-09 14:56:53 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-01-22 15:07:17 +01:00
|
|
|
// Read error message
|
2023-01-09 14:56:53 +01:00
|
|
|
InputStream errIn = process.getErrorStream();
|
2023-01-22 15:07:17 +01:00
|
|
|
String errorMessage = readString(errIn);
|
2023-01-09 14:56:53 +01:00
|
|
|
|
|
|
|
switch (exitCode) {
|
|
|
|
case SOPGPException.NoSignature.EXIT_CODE:
|
|
|
|
throw new SOPGPException.NoSignature("External SOP backend reported error NoSignature (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.UnsupportedAsymmetricAlgo.EXIT_CODE:
|
|
|
|
throw new UnsupportedOperationException("External SOP backend reported error UnsupportedAsymmetricAlgo (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.CertCannotEncrypt.EXIT_CODE:
|
|
|
|
throw new SOPGPException.CertCannotEncrypt("External SOP backend reported error CertCannotEncrypt (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.MissingArg.EXIT_CODE:
|
|
|
|
throw new SOPGPException.MissingArg("External SOP backend reported error MissingArg (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.IncompleteVerification.EXIT_CODE:
|
|
|
|
throw new SOPGPException.IncompleteVerification("External SOP backend reported error IncompleteVerification (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.CannotDecrypt.EXIT_CODE:
|
|
|
|
throw new SOPGPException.CannotDecrypt("External SOP backend reported error CannotDecrypt (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.PasswordNotHumanReadable.EXIT_CODE:
|
|
|
|
throw new SOPGPException.PasswordNotHumanReadable("External SOP backend reported error PasswordNotHumanReadable (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.UnsupportedOption.EXIT_CODE:
|
|
|
|
throw new SOPGPException.UnsupportedOption("External SOP backend reported error UnsupportedOption (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.BadData.EXIT_CODE:
|
|
|
|
throw new SOPGPException.BadData("External SOP backend reported error BadData (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.ExpectedText.EXIT_CODE:
|
|
|
|
throw new SOPGPException.ExpectedText("External SOP backend reported error ExpectedText (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.OutputExists.EXIT_CODE:
|
|
|
|
throw new SOPGPException.OutputExists("External SOP backend reported error OutputExists (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.MissingInput.EXIT_CODE:
|
|
|
|
throw new SOPGPException.MissingInput("External SOP backend reported error MissingInput (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.KeyIsProtected.EXIT_CODE:
|
|
|
|
throw new SOPGPException.KeyIsProtected("External SOP backend reported error KeyIsProtected (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.UnsupportedSubcommand.EXIT_CODE:
|
|
|
|
throw new SOPGPException.UnsupportedSubcommand("External SOP backend reported error UnsupportedSubcommand (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.UnsupportedSpecialPrefix.EXIT_CODE:
|
|
|
|
throw new SOPGPException.UnsupportedSpecialPrefix("External SOP backend reported error UnsupportedSpecialPrefix (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.AmbiguousInput.EXIT_CODE:
|
|
|
|
throw new SOPGPException.AmbiguousInput("External SOP backend reported error AmbiguousInput (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
case SOPGPException.KeyCannotSign.EXIT_CODE:
|
|
|
|
throw new SOPGPException.KeyCannotSign("External SOP backend reported error KeyCannotSign (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new RuntimeException("External SOP backend reported unknown exit code (" +
|
|
|
|
exitCode + "):\n" + errorMessage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:51:29 +01:00
|
|
|
/**
|
|
|
|
* Return all key-value pairs from the given {@link Properties} object as a list with items of the form
|
|
|
|
* <pre>key=value</pre>.
|
|
|
|
*
|
|
|
|
* @param properties properties
|
|
|
|
* @return list of key=value strings
|
|
|
|
*/
|
|
|
|
public static List<String> propertiesToEnv(@Nonnull Properties properties) {
|
2023-01-09 14:56:53 +01:00
|
|
|
List<String> env = new ArrayList<>();
|
|
|
|
for (Object key : properties.keySet()) {
|
|
|
|
env.add(key + "=" + properties.get(key));
|
|
|
|
}
|
|
|
|
return env;
|
|
|
|
}
|
2023-01-09 19:48:25 +01:00
|
|
|
|
2023-01-19 17:51:29 +01:00
|
|
|
/**
|
|
|
|
* Read the contents of the {@link InputStream} and return them as a {@link String}.
|
|
|
|
*
|
|
|
|
* @param inputStream input stream
|
|
|
|
* @return string
|
|
|
|
* @throws IOException in case of an IO error
|
|
|
|
*/
|
2023-01-22 15:07:17 +01:00
|
|
|
public static String readString(@Nonnull InputStream inputStream) throws IOException {
|
2023-01-09 19:48:25 +01:00
|
|
|
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
|
|
|
byte[] buf = new byte[4096];
|
|
|
|
int r;
|
|
|
|
while ((r = inputStream.read(buf)) > 0) {
|
|
|
|
bOut.write(buf, 0, r);
|
|
|
|
}
|
|
|
|
return bOut.toString();
|
|
|
|
}
|
|
|
|
|
2023-01-19 17:51:29 +01:00
|
|
|
/**
|
|
|
|
* Execute the given command on the given {@link Runtime} with the given list of environment variables.
|
2023-01-22 15:07:17 +01:00
|
|
|
* This command does not transform any input data, and instead is purely a producer.
|
2023-01-19 17:51:29 +01:00
|
|
|
*
|
|
|
|
* @param runtime runtime
|
|
|
|
* @param commandList command
|
|
|
|
* @param envList environment variables
|
|
|
|
* @return ready to read the result from
|
|
|
|
*/
|
2023-01-22 15:07:17 +01:00
|
|
|
public static Ready executeProducingOperation(@Nonnull Runtime runtime,
|
|
|
|
@Nonnull List<String> commandList,
|
|
|
|
@Nonnull List<String> envList) {
|
2023-01-09 19:48:25 +01:00
|
|
|
String[] command = commandList.toArray(new String[0]);
|
|
|
|
String[] env = envList.toArray(new String[0]);
|
|
|
|
|
|
|
|
try {
|
|
|
|
Process process = runtime.exec(command, env);
|
|
|
|
InputStream stdIn = process.getInputStream();
|
|
|
|
|
|
|
|
return new Ready() {
|
|
|
|
@Override
|
|
|
|
public void writeTo(OutputStream outputStream) throws IOException {
|
|
|
|
byte[] buf = new byte[4096];
|
|
|
|
int r;
|
|
|
|
while ((r = stdIn.read(buf)) >= 0) {
|
|
|
|
outputStream.write(buf, 0, r);
|
|
|
|
}
|
|
|
|
|
2023-01-13 17:03:05 +01:00
|
|
|
outputStream.flush();
|
2023-01-09 19:48:25 +01:00
|
|
|
outputStream.close();
|
2023-01-22 15:07:17 +01:00
|
|
|
|
2023-01-09 19:48:25 +01:00
|
|
|
ExternalSOP.finish(process);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 15:08:26 +01:00
|
|
|
|
2023-01-19 17:51:29 +01:00
|
|
|
/**
|
|
|
|
* Execute the given command on the given runtime using the given environment variables.
|
|
|
|
* The given input stream provides input for the process.
|
2023-01-22 15:07:17 +01:00
|
|
|
* This command is a transformation, meaning it is given input data and transforms it into output data.
|
2023-01-19 17:51:29 +01:00
|
|
|
*
|
|
|
|
* @param runtime runtime
|
|
|
|
* @param commandList command
|
|
|
|
* @param envList environment variables
|
|
|
|
* @param standardIn stream of input data for the process
|
|
|
|
* @return ready to read the result from
|
|
|
|
*/
|
2023-01-22 15:07:17 +01:00
|
|
|
public static Ready executeTransformingOperation(@Nonnull Runtime runtime, @Nonnull List<String> commandList, @Nonnull List<String> envList, @Nonnull InputStream standardIn) {
|
2023-01-09 19:48:25 +01:00
|
|
|
String[] command = commandList.toArray(new String[0]);
|
|
|
|
String[] env = envList.toArray(new String[0]);
|
|
|
|
try {
|
|
|
|
Process process = runtime.exec(command, env);
|
|
|
|
OutputStream processOut = process.getOutputStream();
|
|
|
|
InputStream processIn = process.getInputStream();
|
|
|
|
|
|
|
|
return new Ready() {
|
|
|
|
@Override
|
|
|
|
public void writeTo(OutputStream outputStream) throws IOException {
|
|
|
|
byte[] buf = new byte[4096];
|
|
|
|
int r;
|
|
|
|
while ((r = standardIn.read(buf)) > 0) {
|
|
|
|
processOut.write(buf, 0, r);
|
|
|
|
}
|
|
|
|
standardIn.close();
|
2023-01-13 17:03:05 +01:00
|
|
|
|
2023-01-13 18:50:19 +01:00
|
|
|
try {
|
|
|
|
processOut.flush();
|
|
|
|
processOut.close();
|
|
|
|
} catch (IOException e) {
|
2023-01-22 15:07:17 +01:00
|
|
|
// Perhaps the stream is already closed, in which case we ignore the exception.
|
|
|
|
if (!"Stream closed".equals(e.getMessage())) {
|
|
|
|
throw e;
|
|
|
|
}
|
2023-01-13 18:50:19 +01:00
|
|
|
}
|
2023-01-09 19:48:25 +01:00
|
|
|
|
|
|
|
while ((r = processIn.read(buf)) > 0) {
|
|
|
|
outputStream.write(buf, 0 , r);
|
|
|
|
}
|
|
|
|
processIn.close();
|
2023-01-13 17:03:05 +01:00
|
|
|
|
|
|
|
outputStream.flush();
|
2023-01-09 19:48:25 +01:00
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
finish(process);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
2023-01-12 13:53:49 +01:00
|
|
|
|
2023-01-12 15:08:26 +01:00
|
|
|
/**
|
|
|
|
* This interface can be used to provide a directory in which external SOP binaries can temporarily store
|
|
|
|
* additional results of OpenPGP operations such that the binding classes can parse them out from there.
|
|
|
|
* Unfortunately, on Java you cannot open {@link java.io.FileDescriptor FileDescriptors} arbitrarily, so we
|
|
|
|
* have to rely on temporary files to pass results.
|
|
|
|
* An example:
|
|
|
|
* <pre>sop decrypt</pre> can emit signature verifications via <pre>--verify-out=/path/to/tempfile</pre>.
|
|
|
|
* {@link DecryptExternal} will then parse the temp file to make the result available to consumers.
|
|
|
|
* Temporary files are deleted after being read, yet creating temp files for sensitive information on disk
|
|
|
|
* might pose a security risk. Use with care!
|
|
|
|
*/
|
2023-01-12 13:53:49 +01:00
|
|
|
public interface TempDirProvider {
|
|
|
|
File provideTempDirectory() throws IOException;
|
|
|
|
}
|
|
|
|
|
2023-01-12 15:08:26 +01:00
|
|
|
/**
|
|
|
|
* Default implementation of the {@link TempDirProvider} which stores temporary files in the systems temp dir
|
|
|
|
* ({@link Files#createTempDirectory(String, FileAttribute[])}).
|
|
|
|
*
|
|
|
|
* @return default implementation
|
|
|
|
*/
|
2023-01-12 13:53:49 +01:00
|
|
|
public static TempDirProvider defaultTempDirProvider() {
|
|
|
|
return new TempDirProvider() {
|
|
|
|
@Override
|
|
|
|
public File provideTempDirectory() throws IOException {
|
|
|
|
return Files.createTempDirectory("ext-sop").toFile();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|