WiP: Implement first prototypes of all SOP commands for external binaries

This commit is contained in:
Paul Schaub 2023-01-09 19:48:25 +01:00
parent efec4d9110
commit a63b29fe80
21 changed files with 1473 additions and 146 deletions

View file

@ -9,77 +9,30 @@ import sop.exception.SOPGPException;
import sop.external.ExternalSOP;
import sop.operation.ExtractCert;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class ExtractCertExternal implements ExtractCert {
private final String binary;
private final Runtime runtime = Runtime.getRuntime();
private final Properties environment;
private boolean noArmor;
private final List<String> commandList = new ArrayList<>();
private final List<String> envList;
public ExtractCertExternal(String binary, Properties properties) {
this.binary = binary;
this.environment = properties;
this.commandList.add(binary);
this.commandList.add("extract-cert");
this.envList = ExternalSOP.propertiesToEnv(properties);
}
@Override
public ExtractCert noArmor() {
this.noArmor = true;
this.commandList.add("--no-armor");
return this;
}
@Override
public Ready key(InputStream keyInputStream) throws SOPGPException.BadData {
List<String> commandList = new ArrayList<>();
commandList.add(binary);
commandList.add("extract-cert");
if (noArmor) {
commandList.add("--no-armor");
}
List<String> envList = ExternalSOP.propertiesToEnv(environment);
String[] command = commandList.toArray(new String[0]);
String[] env = envList.toArray(new String[0]);
try {
Process process = runtime.exec(command, env);
OutputStream extractOut = process.getOutputStream();
InputStream extractIn = process.getInputStream();
return new Ready() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
byte[] buf = new byte[4096];
int r;
while ((r = keyInputStream.read(buf)) > 0) {
extractOut.write(buf, 0, r);
}
keyInputStream.close();
extractOut.close();
while ((r = extractIn.read(buf)) > 0) {
outputStream.write(buf, 0 , r);
}
extractIn.close();
outputStream.close();
ExternalSOP.finish(process);
}
};
} catch (IOException e) {
throw new RuntimeException(e);
}
return ExternalSOP.ready(Runtime.getRuntime(), commandList, envList, keyInputStream);
}
}