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,93 +9,48 @@ import sop.exception.SOPGPException;
import sop.external.ExternalSOP;
import sop.operation.GenerateKey;
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 GenerateKeyExternal implements GenerateKey {
private final String binary;
private boolean noArmor = false;
private List<String> userIds = new ArrayList<>();
private String keyPassword;
private final List<String> commandList = new ArrayList<>();
private final List<String> envList;
private final Runtime runtime = Runtime.getRuntime();
private final Properties properties;
private int keyPasswordCounter = 0;
public GenerateKeyExternal(String binary, Properties environment) {
this.binary = binary;
this.properties = environment;
this.commandList.add(binary);
this.commandList.add("generate-key");
this.envList = ExternalSOP.propertiesToEnv(environment);
}
@Override
public GenerateKey noArmor() {
this.noArmor = true;
this.commandList.add("--no-armor");
return this;
}
@Override
public GenerateKey userId(String userId) {
this.userIds.add(userId);
this.commandList.add(userId);
return this;
}
@Override
public GenerateKey withKeyPassword(String password)
throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption {
this.keyPassword = password;
this.commandList.add("--with-key-password=@ENV:KEY_PASSWORD_" + keyPasswordCounter);
this.envList.add("KEY_PASSWORD_" + keyPasswordCounter + "=" + password);
keyPasswordCounter++;
return this;
}
@Override
public Ready generate()
throws SOPGPException.MissingArg, SOPGPException.UnsupportedAsymmetricAlgo {
List<String> commandList = new ArrayList<>();
commandList.add(binary);
commandList.add("generate-key");
if (noArmor) {
commandList.add("--no-armor");
}
if (keyPassword != null) {
commandList.add("--with-key-password");
commandList.add("@ENV:key_password");
}
for (String userId : userIds) {
commandList.add(userId);
}
List<String> envList = ExternalSOP.propertiesToEnv(properties);
if (keyPassword != null) {
envList.add("key_password=" + keyPassword);
}
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);
}
ExternalSOP.finish(process);
}
};
} catch (IOException e) {
throw new RuntimeException(e);
}
return ExternalSOP.ready(Runtime.getRuntime(), commandList, envList);
}
}