2023-01-09 19:48:25 +01:00
|
|
|
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sop.external.operation;
|
|
|
|
|
|
|
|
import sop.ReadyWithResult;
|
|
|
|
import sop.Signatures;
|
|
|
|
import sop.exception.SOPGPException;
|
|
|
|
import sop.external.ExternalSOP;
|
|
|
|
import sop.operation.InlineDetach;
|
|
|
|
|
2023-11-15 13:52:36 +01:00
|
|
|
import javax.annotation.Nonnull;
|
2023-01-12 14:58:42 +01:00
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
2023-01-09 19:48:25 +01:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Properties;
|
|
|
|
|
2023-01-19 17:29:29 +01:00
|
|
|
/**
|
|
|
|
* Implementation of the {@link InlineDetach} operation using an external SOP binary.
|
|
|
|
*/
|
2023-01-09 19:48:25 +01:00
|
|
|
public class InlineDetachExternal implements InlineDetach {
|
|
|
|
|
2023-01-12 14:58:42 +01:00
|
|
|
private final ExternalSOP.TempDirProvider tempDirProvider;
|
2023-01-09 19:48:25 +01:00
|
|
|
private final List<String> commandList = new ArrayList<>();
|
|
|
|
private final List<String> envList;
|
|
|
|
|
2023-01-12 14:58:42 +01:00
|
|
|
public InlineDetachExternal(String binary, Properties environment, ExternalSOP.TempDirProvider tempDirProvider) {
|
|
|
|
this.tempDirProvider = tempDirProvider;
|
2023-01-09 19:48:25 +01:00
|
|
|
commandList.add(binary);
|
|
|
|
commandList.add("inline-detach");
|
|
|
|
envList = ExternalSOP.propertiesToEnv(environment);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-11-15 13:52:36 +01:00
|
|
|
@Nonnull
|
2023-01-09 19:48:25 +01:00
|
|
|
public InlineDetach noArmor() {
|
|
|
|
commandList.add("--no-armor");
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-11-15 13:52:36 +01:00
|
|
|
@Nonnull
|
|
|
|
public ReadyWithResult<Signatures> message(@Nonnull InputStream messageInputStream) throws IOException, SOPGPException.BadData {
|
2023-01-12 14:58:42 +01:00
|
|
|
File tempDir = tempDirProvider.provideTempDirectory();
|
|
|
|
|
|
|
|
File signaturesOut = new File(tempDir, "signatures");
|
|
|
|
signaturesOut.delete();
|
|
|
|
commandList.add("--signatures-out=" + signaturesOut.getAbsolutePath());
|
|
|
|
|
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.getRuntime().exec(command, env);
|
|
|
|
OutputStream processOut = process.getOutputStream();
|
|
|
|
InputStream processIn = process.getInputStream();
|
|
|
|
|
|
|
|
return new ReadyWithResult<Signatures>() {
|
|
|
|
@Override
|
2023-11-15 13:52:36 +01:00
|
|
|
public Signatures writeTo(@Nonnull OutputStream outputStream) throws IOException {
|
2023-01-09 19:48:25 +01:00
|
|
|
byte[] buf = new byte[4096];
|
|
|
|
int r;
|
|
|
|
while ((r = messageInputStream.read(buf)) > 0) {
|
|
|
|
processOut.write(buf, 0, r);
|
|
|
|
}
|
|
|
|
|
|
|
|
messageInputStream.close();
|
|
|
|
processOut.close();
|
|
|
|
|
|
|
|
while ((r = processIn.read(buf)) > 0) {
|
|
|
|
outputStream.write(buf, 0 , r);
|
|
|
|
}
|
|
|
|
|
|
|
|
processIn.close();
|
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
ExternalSOP.finish(process);
|
|
|
|
|
2023-01-12 14:58:42 +01:00
|
|
|
FileInputStream signaturesOutIn = new FileInputStream(signaturesOut);
|
|
|
|
ByteArrayOutputStream signaturesBuffer = new ByteArrayOutputStream();
|
|
|
|
while ((r = signaturesOutIn.read(buf)) > 0) {
|
|
|
|
signaturesBuffer.write(buf, 0, r);
|
|
|
|
}
|
|
|
|
signaturesOutIn.close();
|
|
|
|
signaturesOut.delete();
|
|
|
|
|
|
|
|
final byte[] sigBytes = signaturesBuffer.toByteArray();
|
|
|
|
return new Signatures() {
|
|
|
|
@Override
|
2023-11-15 13:52:36 +01:00
|
|
|
public void writeTo(@Nonnull OutputStream signatureOutputStream) throws IOException {
|
2023-01-12 14:58:42 +01:00
|
|
|
signatureOutputStream.write(sigBytes);
|
|
|
|
}
|
|
|
|
};
|
2023-01-09 19:48:25 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|