mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2024-12-22 12:57:57 +01:00
Initial work on binary-sop
This module is intended to allow the use of SOP command line applications such as sqop, pgpainless-sop, etc. as drop-ins for sop-java.
This commit is contained in:
parent
ed296ec4b2
commit
28912618ea
9 changed files with 445 additions and 1 deletions
24
binary-sop/build.gradle
Normal file
24
binary-sop/build.gradle
Normal file
|
@ -0,0 +1,24 @@
|
|||
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
plugins {
|
||||
id 'java-library'
|
||||
}
|
||||
|
||||
group 'org.pgpainless'
|
||||
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
|
||||
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
|
||||
|
||||
api project(":sop-java")
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
91
binary-sop/src/main/java/sop/binary/BinarySop.java
Normal file
91
binary-sop/src/main/java/sop/binary/BinarySop.java
Normal file
|
@ -0,0 +1,91 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.binary;
|
||||
|
||||
import sop.SOP;
|
||||
import sop.binary.operation.BinaryExtractCert;
|
||||
import sop.binary.operation.BinaryGenerateKey;
|
||||
import sop.binary.operation.BinaryVersion;
|
||||
import sop.operation.Armor;
|
||||
import sop.operation.Dearmor;
|
||||
import sop.operation.Decrypt;
|
||||
import sop.operation.DetachedSign;
|
||||
import sop.operation.DetachedVerify;
|
||||
import sop.operation.Encrypt;
|
||||
import sop.operation.ExtractCert;
|
||||
import sop.operation.GenerateKey;
|
||||
import sop.operation.InlineDetach;
|
||||
import sop.operation.InlineSign;
|
||||
import sop.operation.InlineVerify;
|
||||
import sop.operation.Version;
|
||||
|
||||
public class BinarySop implements SOP {
|
||||
|
||||
private final String binaryName;
|
||||
|
||||
public BinarySop(String binaryName) {
|
||||
this.binaryName = binaryName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Version version() {
|
||||
return new BinaryVersion(binaryName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenerateKey generateKey() {
|
||||
return new BinaryGenerateKey(binaryName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExtractCert extractCert() {
|
||||
return new BinaryExtractCert(binaryName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DetachedSign detachedSign() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InlineSign inlineSign() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DetachedVerify detachedVerify() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InlineVerify inlineVerify() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public InlineDetach inlineDetach() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Encrypt encrypt() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Decrypt decrypt() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Armor armor() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dearmor dearmor() {
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.binary.operation;
|
||||
|
||||
import sop.Ready;
|
||||
import sop.exception.SOPGPException;
|
||||
import sop.operation.ExtractCert;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BinaryExtractCert implements ExtractCert {
|
||||
|
||||
private final String binary;
|
||||
private final Runtime runtime = Runtime.getRuntime();
|
||||
|
||||
private boolean noArmor;
|
||||
|
||||
public BinaryExtractCert(String binary) {
|
||||
this.binary = binary;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExtractCert noArmor() {
|
||||
this.noArmor = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Ready key(InputStream keyInputStream) throws IOException, SOPGPException.BadData {
|
||||
List<String> commandList = new ArrayList<>();
|
||||
|
||||
commandList.add(binary);
|
||||
commandList.add("extract-cert");
|
||||
|
||||
if (noArmor) {
|
||||
commandList.add("--no-armor");
|
||||
}
|
||||
|
||||
String[] command = commandList.toArray(new String[0]);
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
OutputStream stdOut = process.getOutputStream();
|
||||
InputStream stdIn = 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) {
|
||||
stdOut.write(buf, 0, r);
|
||||
}
|
||||
|
||||
while ((r = stdIn.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0 , r);
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.binary.operation;
|
||||
|
||||
import sop.Ready;
|
||||
import sop.exception.SOPGPException;
|
||||
import sop.operation.GenerateKey;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class BinaryGenerateKey implements GenerateKey {
|
||||
|
||||
private final String binary;
|
||||
private boolean noArmor = false;
|
||||
private List<String> userIds = new ArrayList<>();
|
||||
private String keyPassword;
|
||||
|
||||
private final Runtime runtime = Runtime.getRuntime();
|
||||
|
||||
public BinaryGenerateKey(String binary) {
|
||||
this.binary = binary;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenerateKey noArmor() {
|
||||
this.noArmor = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenerateKey userId(String userId) {
|
||||
this.userIds.add(userId);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public GenerateKey withKeyPassword(String password)
|
||||
throws SOPGPException.PasswordNotHumanReadable, SOPGPException.UnsupportedOption {
|
||||
this.keyPassword = password;
|
||||
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(keyPassword);
|
||||
}
|
||||
|
||||
for (String userId : userIds) {
|
||||
commandList.add(userId);
|
||||
}
|
||||
|
||||
String[] command = commandList.toArray(new String[0]);
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
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);
|
||||
}
|
||||
}
|
||||
};
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,87 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.binary.operation;
|
||||
|
||||
import sop.operation.Version;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
public class BinaryVersion implements Version {
|
||||
|
||||
private final Runtime runtime = Runtime.getRuntime();
|
||||
private final String binary;
|
||||
|
||||
public BinaryVersion(String binaryName) {
|
||||
this.binary = binaryName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
String[] command = new String[] {binary, "version"};
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
String line = stdInput.readLine().trim();
|
||||
if (line.contains(" ")) {
|
||||
return line.substring(0, line.lastIndexOf(" "));
|
||||
}
|
||||
return line;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getVersion() {
|
||||
String[] command = new String[] {binary, "version"};
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
String line = stdInput.readLine().trim();
|
||||
if (line.contains(" ")) {
|
||||
return line.substring(line.lastIndexOf(" ") + 1);
|
||||
}
|
||||
return line;
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getBackendVersion() {
|
||||
String[] command = new String[] {binary, "version", "--backend"};
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while ((line = stdInput.readLine()) != null) {
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getExtendedVersion() {
|
||||
String[] command = new String[] {binary, "version", "--extended"};
|
||||
try {
|
||||
Process process = runtime.exec(command);
|
||||
BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
String line;
|
||||
while ((line = stdInput.readLine()) != null) {
|
||||
sb.append(line).append('\n');
|
||||
}
|
||||
return sb.toString();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,8 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* Bindings for SOP subcommands to a SOP binary.
|
||||
*/
|
||||
package sop.binary.operation;
|
8
binary-sop/src/main/java/sop/binary/package-info.java
Normal file
8
binary-sop/src/main/java/sop/binary/package-info.java
Normal file
|
@ -0,0 +1,8 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
/**
|
||||
* Implementation of sop-java which delegates execution to a binary implementing the SOP command line interface.
|
||||
*/
|
||||
package sop.binary;
|
67
binary-sop/src/test/java/sop/binary/BinarySopTest.java
Normal file
67
binary-sop/src/test/java/sop/binary/BinarySopTest.java
Normal file
|
@ -0,0 +1,67 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.binary;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIf;
|
||||
import sop.SOP;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
@EnabledIf("sop.binary.BinarySopTest#sopBinaryInstalled")
|
||||
public class BinarySopTest {
|
||||
|
||||
private static final String BINARY = "/usr/bin/sqop";
|
||||
|
||||
private final SOP sop = new BinarySop(BINARY);
|
||||
|
||||
public static boolean sopBinaryInstalled() {
|
||||
return new File(BINARY).exists();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void versionNameTest() {
|
||||
assertEquals("sqop", sop.version().getName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void versionVersionTest() {
|
||||
String version = sop.version().getVersion();
|
||||
assertTrue(version.matches("\\d+(\\.\\d+)*"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void backendVersionTest() {
|
||||
String backend = sop.version().getBackendVersion();
|
||||
assertFalse(backend.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void extendedVersionTest() {
|
||||
String extended = sop.version().getExtendedVersion();
|
||||
assertFalse(extended.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateKeyTest() throws IOException {
|
||||
String key = new String(sop.generateKey().userId("Alice").generate().getBytes());
|
||||
assertTrue(key.startsWith("-----BEGIN PGP PRIVATE KEY BLOCK-----\n"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void extractCertTest() throws IOException {
|
||||
InputStream keyIn = sop.generateKey().userId("Alice").generate().getInputStream();
|
||||
String cert = new String(sop.extractCert().key(keyIn).getBytes());
|
||||
assertTrue(cert.startsWith("-----BEGIN PGP PUBLIC KEY BLOCK-----\n"));
|
||||
}
|
||||
}
|
|
@ -5,5 +5,6 @@
|
|||
rootProject.name = 'SOP-Java'
|
||||
|
||||
include 'sop-java',
|
||||
'sop-java-picocli'
|
||||
'sop-java-picocli',
|
||||
'binary-sop'
|
||||
|
||||
|
|
Loading…
Reference in a new issue