mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 18:29:39 +02:00
SOP: Implement merge-certs subcommand
This commit is contained in:
parent
9f2371932e
commit
68be1ffc5f
2 changed files with 73 additions and 1 deletions
|
@ -0,0 +1,72 @@
|
||||||
|
// SPDX-FileCopyrightText: 2025 Paul Schaub <vanitasvitae@fsfe.org>
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
|
package org.pgpainless.sop
|
||||||
|
|
||||||
|
import org.bouncycastle.bcpg.KeyIdentifier
|
||||||
|
import org.bouncycastle.bcpg.PacketFormat
|
||||||
|
import org.bouncycastle.openpgp.api.OpenPGPCertificate
|
||||||
|
import org.pgpainless.PGPainless
|
||||||
|
import org.pgpainless.util.ArmoredOutputStreamFactory
|
||||||
|
import sop.Ready
|
||||||
|
import sop.operation.MergeCerts
|
||||||
|
import java.io.InputStream
|
||||||
|
import java.io.OutputStream
|
||||||
|
|
||||||
|
class MergeCertsImpl(private val api: PGPainless) : MergeCerts {
|
||||||
|
|
||||||
|
private var armor = true
|
||||||
|
private val baseCerts: MutableMap<KeyIdentifier, OpenPGPCertificate> = mutableMapOf()
|
||||||
|
private val updateCerts: MutableList<OpenPGPCertificate> = mutableListOf()
|
||||||
|
|
||||||
|
// from standard input
|
||||||
|
override fun baseCertificates(certs: InputStream): Ready {
|
||||||
|
return object : Ready() {
|
||||||
|
override fun writeTo(outputStream: OutputStream) {
|
||||||
|
val certList = api.readKey().parseCertificates(certs)
|
||||||
|
for (cert in certList) {
|
||||||
|
if (!baseCerts.contains(cert.keyIdentifier)) {
|
||||||
|
baseCerts[cert.keyIdentifier] = cert
|
||||||
|
} else {
|
||||||
|
val baseCert = baseCerts[cert.keyIdentifier]!!
|
||||||
|
baseCerts[cert.keyIdentifier] = api.mergeCertificate(baseCert, cert)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (update in updateCerts) {
|
||||||
|
if (baseCerts[update.keyIdentifier] == null) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
val baseCert = baseCerts[update.keyIdentifier]!!
|
||||||
|
baseCerts[update.keyIdentifier] = api.mergeCertificate(baseCert, update)
|
||||||
|
}
|
||||||
|
|
||||||
|
val out = if (armor) {
|
||||||
|
ArmoredOutputStreamFactory.get(outputStream)
|
||||||
|
} else {
|
||||||
|
outputStream
|
||||||
|
}
|
||||||
|
|
||||||
|
for (merged in baseCerts.values) {
|
||||||
|
out.write(merged.getEncoded(PacketFormat.CURRENT))
|
||||||
|
}
|
||||||
|
|
||||||
|
if (armor) {
|
||||||
|
out.close()
|
||||||
|
}
|
||||||
|
outputStream.close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun noArmor(): MergeCerts = apply {
|
||||||
|
armor = false
|
||||||
|
}
|
||||||
|
|
||||||
|
// from command line
|
||||||
|
override fun updates(updateCerts: InputStream): MergeCerts = apply {
|
||||||
|
this.updateCerts.addAll(api.readKey().parseCertificates(updateCerts))
|
||||||
|
}
|
||||||
|
}
|
|
@ -62,7 +62,7 @@ class SOPImpl(
|
||||||
|
|
||||||
override fun listProfiles(): ListProfiles = ListProfilesImpl(api)
|
override fun listProfiles(): ListProfiles = ListProfilesImpl(api)
|
||||||
|
|
||||||
override fun mergeCerts(): MergeCerts? = null
|
override fun mergeCerts(): MergeCerts = MergeCertsImpl(api)
|
||||||
|
|
||||||
override fun revokeKey(): RevokeKey = RevokeKeyImpl(api)
|
override fun revokeKey(): RevokeKey = RevokeKeyImpl(api)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue