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.operation;
|
2023-01-05 03:06:22 +01:00
|
|
|
|
|
|
|
import sop.Ready;
|
|
|
|
import sop.exception.SOPGPException;
|
2023-01-09 14:56:53 +01:00
|
|
|
import sop.external.ExternalSOP;
|
2023-01-05 03:06:22 +01:00
|
|
|
import sop.operation.ExtractCert;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
2023-01-09 14:56:53 +01:00
|
|
|
import java.util.Properties;
|
2023-01-05 03:06:22 +01:00
|
|
|
|
2023-01-07 15:23:57 +01:00
|
|
|
public class ExtractCertExternal implements ExtractCert {
|
2023-01-05 03:06:22 +01:00
|
|
|
|
|
|
|
private final String binary;
|
|
|
|
private final Runtime runtime = Runtime.getRuntime();
|
2023-01-09 14:56:53 +01:00
|
|
|
private final Properties environment;
|
2023-01-05 03:06:22 +01:00
|
|
|
|
|
|
|
private boolean noArmor;
|
|
|
|
|
2023-01-09 14:56:53 +01:00
|
|
|
public ExtractCertExternal(String binary, Properties properties) {
|
2023-01-05 03:06:22 +01:00
|
|
|
this.binary = binary;
|
2023-01-09 14:56:53 +01:00
|
|
|
this.environment = properties;
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public ExtractCert noArmor() {
|
|
|
|
this.noArmor = true;
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2023-01-09 14:56:53 +01:00
|
|
|
public Ready key(InputStream keyInputStream) throws SOPGPException.BadData {
|
2023-01-05 03:06:22 +01:00
|
|
|
List<String> commandList = new ArrayList<>();
|
|
|
|
|
|
|
|
commandList.add(binary);
|
|
|
|
commandList.add("extract-cert");
|
|
|
|
|
|
|
|
if (noArmor) {
|
|
|
|
commandList.add("--no-armor");
|
|
|
|
}
|
|
|
|
|
2023-01-09 14:56:53 +01:00
|
|
|
List<String> envList = ExternalSOP.propertiesToEnv(environment);
|
|
|
|
|
2023-01-05 03:06:22 +01:00
|
|
|
String[] command = commandList.toArray(new String[0]);
|
2023-01-09 14:56:53 +01:00
|
|
|
String[] env = envList.toArray(new String[0]);
|
|
|
|
|
2023-01-05 03:06:22 +01:00
|
|
|
try {
|
2023-01-09 14:56:53 +01:00
|
|
|
Process process = runtime.exec(command, env);
|
|
|
|
OutputStream extractOut = process.getOutputStream();
|
|
|
|
InputStream extractIn = process.getInputStream();
|
2023-01-05 03:06:22 +01:00
|
|
|
|
|
|
|
return new Ready() {
|
|
|
|
@Override
|
|
|
|
public void writeTo(OutputStream outputStream) throws IOException {
|
|
|
|
byte[] buf = new byte[4096];
|
|
|
|
int r;
|
|
|
|
while ((r = keyInputStream.read(buf)) > 0) {
|
2023-01-09 14:56:53 +01:00
|
|
|
extractOut.write(buf, 0, r);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
2023-01-09 14:56:53 +01:00
|
|
|
keyInputStream.close();
|
|
|
|
extractOut.close();
|
|
|
|
|
|
|
|
while ((r = extractIn.read(buf)) > 0) {
|
2023-01-05 03:06:22 +01:00
|
|
|
outputStream.write(buf, 0 , r);
|
|
|
|
}
|
2023-01-09 14:56:53 +01:00
|
|
|
|
|
|
|
extractIn.close();
|
|
|
|
outputStream.close();
|
|
|
|
|
|
|
|
ExternalSOP.finish(process);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|