Wip: working extract-cert, fix generate-key parameter passing

This commit is contained in:
Paul Schaub 2023-01-09 14:56:53 +01:00
parent e602cc16cc
commit efec4d9110
12 changed files with 430 additions and 122 deletions

View file

@ -0,0 +1,43 @@
package sop.external.operation;
import sop.ReadyWithResult;
import sop.SigningResult;
import sop.enums.SignAs;
import sop.exception.SOPGPException;
import sop.operation.DetachedSign;
import java.io.IOException;
import java.io.InputStream;
public class DetachedSignExternal implements DetachedSign {
private boolean noArmor;
private byte[] keyPassword;
@Override
public DetachedSign noArmor() {
this.noArmor = true;
return this;
}
@Override
public DetachedSign key(InputStream key) throws SOPGPException.KeyCannotSign, SOPGPException.BadData, SOPGPException.UnsupportedAsymmetricAlgo, IOException {
return null;
}
@Override
public DetachedSign withKeyPassword(byte[] password) throws SOPGPException.UnsupportedOption, SOPGPException.PasswordNotHumanReadable {
this.keyPassword = password;
return this;
}
@Override
public DetachedSign mode(SignAs mode) throws SOPGPException.UnsupportedOption {
return null;
}
@Override
public ReadyWithResult<SigningResult> data(InputStream data) throws IOException, SOPGPException.KeyIsProtected, SOPGPException.ExpectedText {
return null;
}
}

View file

@ -6,6 +6,7 @@ package sop.external.operation;
import sop.Ready;
import sop.exception.SOPGPException;
import sop.external.ExternalSOP;
import sop.operation.ExtractCert;
import java.io.IOException;
@ -13,16 +14,19 @@ 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;
public ExtractCertExternal(String binary) {
public ExtractCertExternal(String binary, Properties properties) {
this.binary = binary;
this.environment = properties;
}
@Override
@ -32,7 +36,7 @@ public class ExtractCertExternal implements ExtractCert {
}
@Override
public Ready key(InputStream keyInputStream) throws IOException, SOPGPException.BadData {
public Ready key(InputStream keyInputStream) throws SOPGPException.BadData {
List<String> commandList = new ArrayList<>();
commandList.add(binary);
@ -42,11 +46,15 @@ public class ExtractCertExternal implements ExtractCert {
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);
OutputStream stdOut = process.getOutputStream();
InputStream stdIn = process.getInputStream();
Process process = runtime.exec(command, env);
OutputStream extractOut = process.getOutputStream();
InputStream extractIn = process.getInputStream();
return new Ready() {
@Override
@ -54,12 +62,20 @@ public class ExtractCertExternal implements ExtractCert {
byte[] buf = new byte[4096];
int r;
while ((r = keyInputStream.read(buf)) > 0) {
stdOut.write(buf, 0, r);
extractOut.write(buf, 0, r);
}
while ((r = stdIn.read(buf)) > 0) {
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) {

View file

@ -6,6 +6,7 @@ package sop.external.operation;
import sop.Ready;
import sop.exception.SOPGPException;
import sop.external.ExternalSOP;
import sop.operation.GenerateKey;
import java.io.IOException;
@ -13,6 +14,7 @@ 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 {
@ -22,9 +24,11 @@ public class GenerateKeyExternal implements GenerateKey {
private String keyPassword;
private final Runtime runtime = Runtime.getRuntime();
private final Properties properties;
public GenerateKeyExternal(String binary) {
public GenerateKeyExternal(String binary, Properties environment) {
this.binary = binary;
this.properties = environment;
}
@Override
@ -60,16 +64,22 @@ public class GenerateKeyExternal implements GenerateKey {
if (keyPassword != null) {
commandList.add("--with-key-password");
commandList.add(keyPassword);
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);
Process process = runtime.exec(command, env);
InputStream stdIn = process.getInputStream();
return new Ready() {
@ -80,6 +90,8 @@ public class GenerateKeyExternal implements GenerateKey {
while ((r = stdIn.read(buf)) >= 0) {
outputStream.write(buf, 0, r);
}
ExternalSOP.finish(process);
}
};
} catch (IOException e) {

View file

@ -4,31 +4,37 @@
package sop.external.operation;
import sop.external.ExternalSOP;
import sop.operation.Version;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Properties;
public class VersionExternal implements Version {
private final Runtime runtime = Runtime.getRuntime();
private final String binary;
private final Properties environment;
public VersionExternal(String binaryName) {
public VersionExternal(String binaryName, Properties environment) {
this.binary = binaryName;
this.environment = environment;
}
@Override
public String getName() {
String[] command = new String[] {binary, "version"};
String[] env = ExternalSOP.propertiesToEnv(environment).toArray(new String[0]);
try {
Process process = runtime.exec(command);
Process process = runtime.exec(command, env);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = stdInput.readLine().trim();
if (line.contains(" ")) {
return line.substring(0, line.lastIndexOf(" "));
}
ExternalSOP.finish(process);
return line;
} catch (IOException e) {
throw new RuntimeException(e);
@ -38,13 +44,15 @@ public class VersionExternal implements Version {
@Override
public String getVersion() {
String[] command = new String[] {binary, "version"};
String[] env = ExternalSOP.propertiesToEnv(environment).toArray(new String[0]);
try {
Process process = runtime.exec(command);
Process process = runtime.exec(command, env);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = stdInput.readLine().trim();
if (line.contains(" ")) {
return line.substring(line.lastIndexOf(" ") + 1);
}
ExternalSOP.finish(process);
return line;
} catch (IOException e) {
throw new RuntimeException(e);
@ -54,14 +62,16 @@ public class VersionExternal implements Version {
@Override
public String getBackendVersion() {
String[] command = new String[] {binary, "version", "--backend"};
String[] env = ExternalSOP.propertiesToEnv(environment).toArray(new String[0]);
try {
Process process = runtime.exec(command);
Process process = runtime.exec(command, env);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = stdInput.readLine()) != null) {
sb.append(line).append('\n');
}
ExternalSOP.finish(process);
return sb.toString();
} catch (IOException e) {
throw new RuntimeException(e);
@ -71,14 +81,16 @@ public class VersionExternal implements Version {
@Override
public String getExtendedVersion() {
String[] command = new String[] {binary, "version", "--extended"};
String[] env = ExternalSOP.propertiesToEnv(environment).toArray(new String[0]);
try {
Process process = runtime.exec(command);
Process process = runtime.exec(command, env);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = stdInput.readLine()) != null) {
sb.append(line).append('\n');
}
ExternalSOP.finish(process);
return sb.toString();
} catch (IOException e) {
throw new RuntimeException(e);