mirror of
https://codeberg.org/PGPainless/sop-java.git
synced 2025-09-09 18:29:48 +02:00
Kotlin conversion: InlineVerifyExternal
This commit is contained in:
parent
7be71494cf
commit
8dc51b67a3
2 changed files with 91 additions and 122 deletions
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…
Add table
Add a link
Reference in a new issue