mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2024-12-22 12:57:57 +01:00
Kotlin conversion: InlineVerifyExternal
This commit is contained in:
parent
7be71494cf
commit
8dc51b67a3
2 changed files with 91 additions and 122 deletions
|
@ -1,122 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.external.operation;
|
||||
|
||||
import sop.ReadyWithResult;
|
||||
import sop.Verification;
|
||||
import sop.exception.SOPGPException;
|
||||
import sop.external.ExternalSOP;
|
||||
import sop.operation.InlineVerify;
|
||||
import sop.util.UTCUtil;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Implementation of the {@link InlineVerify} operation using an external SOP binary.
|
||||
*/
|
||||
public class InlineVerifyExternal implements InlineVerify {
|
||||
|
||||
private final ExternalSOP.TempDirProvider tempDirProvider;
|
||||
private final List<String> commandList = new ArrayList<>();
|
||||
private final List<String> envList;
|
||||
|
||||
private int certCounter = 0;
|
||||
|
||||
public InlineVerifyExternal(String binary, Properties environment, ExternalSOP.TempDirProvider tempDirProvider) {
|
||||
this.tempDirProvider = tempDirProvider;
|
||||
commandList.add(binary);
|
||||
commandList.add("inline-verify");
|
||||
envList = ExternalSOP.propertiesToEnv(environment);
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public InlineVerify notBefore(@Nonnull Date timestamp) throws SOPGPException.UnsupportedOption {
|
||||
commandList.add("--not-before=" + UTCUtil.formatUTCDate(timestamp));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public InlineVerify notAfter(@Nonnull Date timestamp) throws SOPGPException.UnsupportedOption {
|
||||
commandList.add("--not-after=" + UTCUtil.formatUTCDate(timestamp));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public InlineVerify cert(@Nonnull InputStream cert) throws SOPGPException.BadData, IOException {
|
||||
String envVar = "CERT_" + certCounter++;
|
||||
commandList.add("@ENV:" + envVar);
|
||||
envList.add(envVar + "=" + ExternalSOP.readString(cert));
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@Nonnull
|
||||
public ReadyWithResult<List<Verification>> data(@Nonnull InputStream data) throws IOException, SOPGPException.NoSignature, SOPGPException.BadData {
|
||||
File tempDir = tempDirProvider.provideTempDirectory();
|
||||
|
||||
File verificationsOut = new File(tempDir, "verifications-out");
|
||||
verificationsOut.delete();
|
||||
commandList.add("--verifications-out=" + verificationsOut.getAbsolutePath());
|
||||
|
||||
String[] command = commandList.toArray(new String[0]);
|
||||
String[] env = envList.toArray(new String[0]);
|
||||
|
||||
try {
|
||||
Process process = Runtime.getRuntime().exec(command, env);
|
||||
OutputStream processOut = process.getOutputStream();
|
||||
InputStream processIn = process.getInputStream();
|
||||
|
||||
return new ReadyWithResult<List<Verification>>() {
|
||||
@Override
|
||||
public List<Verification> writeTo(@Nonnull OutputStream outputStream) throws IOException, SOPGPException.NoSignature {
|
||||
byte[] buf = new byte[4096];
|
||||
int r;
|
||||
while ((r = data.read(buf)) > 0) {
|
||||
processOut.write(buf, 0, r);
|
||||
}
|
||||
|
||||
data.close();
|
||||
processOut.close();
|
||||
|
||||
|
||||
while ((r = processIn.read(buf)) > 0) {
|
||||
outputStream.write(buf, 0 , r);
|
||||
}
|
||||
|
||||
processIn.close();
|
||||
outputStream.close();
|
||||
|
||||
ExternalSOP.finish(process);
|
||||
|
||||
FileInputStream verificationsOutIn = new FileInputStream(verificationsOut);
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(verificationsOutIn));
|
||||
List<Verification> verificationList = new ArrayList<>();
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
verificationList.add(Verification.fromString(line.trim()));
|
||||
}
|
||||
|
||||
return verificationList;
|
||||
}
|
||||
};
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
91
external-sop/src/main/kotlin/sop/external/operation/InlineVerifyExternal.kt
vendored
Normal file
91
external-sop/src/main/kotlin/sop/external/operation/InlineVerifyExternal.kt
vendored
Normal file
|
@ -0,0 +1,91 @@
|
|||
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package sop.external.operation
|
||||
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
import sop.ReadyWithResult
|
||||
import sop.Verification
|
||||
import sop.Verification.Companion.fromString
|
||||
import sop.external.ExternalSOP
|
||||
import sop.external.ExternalSOP.Companion.finish
|
||||
import sop.operation.InlineVerify
|
||||
import sop.util.UTCUtil
|
||||
|
||||
/** Implementation of the [InlineVerify] operation using an external SOP binary. */
|
||||
class InlineVerifyExternal(
|
||||
binary: String,
|
||||
environment: Properties,
|
||||
private val tempDirProvider: ExternalSOP.TempDirProvider
|
||||
) : InlineVerify {
|
||||
|
||||
private val commandList = mutableListOf(binary, "inline-verify")
|
||||
private val envList = ExternalSOP.propertiesToEnv(environment).toMutableList()
|
||||
|
||||
private var argCounter = 0
|
||||
|
||||
override fun data(data: InputStream): ReadyWithResult<List<Verification>> {
|
||||
val tempDir = tempDirProvider.provideTempDirectory()
|
||||
|
||||
val verificationsOut = File(tempDir, "verifications-out")
|
||||
verificationsOut.delete()
|
||||
commandList.add("--verifications-out=${verificationsOut.absolutePath}")
|
||||
|
||||
try {
|
||||
val process =
|
||||
Runtime.getRuntime().exec(commandList.toTypedArray(), envList.toTypedArray())
|
||||
val processOut = process.outputStream
|
||||
val processIn = process.inputStream
|
||||
|
||||
return object : ReadyWithResult<List<Verification>>() {
|
||||
override fun writeTo(outputStream: OutputStream): List<Verification> {
|
||||
val buf = ByteArray(4096)
|
||||
var r: Int
|
||||
while (data.read(buf).also { r = it } > 0) {
|
||||
processOut.write(buf, 0, r)
|
||||
}
|
||||
|
||||
data.close()
|
||||
processOut.close()
|
||||
|
||||
while (processIn.read(buf).also { r = it } > 0) {
|
||||
outputStream.write(buf, 0, r)
|
||||
}
|
||||
|
||||
processIn.close()
|
||||
outputStream.close()
|
||||
|
||||
finish(process)
|
||||
|
||||
val verificationsOutIn = FileInputStream(verificationsOut)
|
||||
val reader = BufferedReader(InputStreamReader(verificationsOutIn))
|
||||
val verificationList: MutableList<Verification> = mutableListOf()
|
||||
var line: String
|
||||
while (reader.readLine().also { line = it } != null) {
|
||||
verificationList.add(fromString(line.trim()))
|
||||
}
|
||||
|
||||
return verificationList
|
||||
}
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
throw RuntimeException(e)
|
||||
}
|
||||
}
|
||||
|
||||
override fun notBefore(timestamp: Date): InlineVerify = apply {
|
||||
commandList.add("--not-before=${UTCUtil.formatUTCDate(timestamp)}")
|
||||
}
|
||||
|
||||
override fun notAfter(timestamp: Date): InlineVerify = apply {
|
||||
commandList.add("--not-after=${UTCUtil.formatUTCDate(timestamp)}")
|
||||
}
|
||||
|
||||
override fun cert(cert: InputStream): InlineVerify = apply {
|
||||
commandList.add("@ENV:CERT_$argCounter")
|
||||
envList.add("CERT_$argCounter=${ExternalSOP.readString(cert)}")
|
||||
argCounter += 1
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue