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.GenerateKey;
|
|
|
|
|
|
|
|
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-19 17:29:29 +01:00
|
|
|
/**
|
|
|
|
* Implementation of the {@link GenerateKey} operation using an external SOP binary.
|
|
|
|
*/
|
2023-01-07 15:23:57 +01:00
|
|
|
public class GenerateKeyExternal implements GenerateKey {
|
2023-01-05 03:06:22 +01:00
|
|
|
|
2023-01-09 19:48:25 +01:00
|
|
|
private final List<String> commandList = new ArrayList<>();
|
|
|
|
private final List<String> envList;
|
2023-01-05 03:06:22 +01:00
|
|
|
|
2023-01-09 19:48:25 +01:00
|
|
|
private int keyPasswordCounter = 0;
|
2023-01-05 03:06:22 +01:00
|
|
|
|
2023-01-09 14:56:53 +01:00
|
|
|
public GenerateKeyExternal(String binary, Properties environment) {
|
2023-01-09 19:48:25 +01:00
|
|
|
this.commandList.add(binary);
|
|
|
|
this.commandList.add("generate-key");
|
|
|
|
this.envList = ExternalSOP.propertiesToEnv(environment);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GenerateKey noArmor() {
|
2023-01-09 19:48:25 +01:00
|
|
|
this.commandList.add("--no-armor");
|
2023-01-05 03:06:22 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GenerateKey userId(String userId) {
|
2023-01-09 19:48:25 +01:00
|
|
|
this.commandList.add(userId);
|
2023-01-05 03:06:22 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GenerateKey withKeyPassword(String password)
|
|
|
|
throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption {
|
2023-01-09 19:48:25 +01:00
|
|
|
this.commandList.add("--with-key-password=@ENV:KEY_PASSWORD_" + keyPasswordCounter);
|
|
|
|
this.envList.add("KEY_PASSWORD_" + keyPasswordCounter + "=" + password);
|
|
|
|
keyPasswordCounter++;
|
|
|
|
|
2023-01-05 03:06:22 +01:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Ready generate()
|
|
|
|
throws SOPGPException.MissingArg, SOPGPException.UnsupportedAsymmetricAlgo {
|
2023-01-22 15:07:17 +01:00
|
|
|
return ExternalSOP.executeProducingOperation(Runtime.getRuntime(), commandList, envList);
|
2023-01-05 03:06:22 +01:00
|
|
|
}
|
|
|
|
}
|