From 34f706191d15f1476f4e0bf3ee444dfa07b678d2 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 7 Jul 2023 15:49:39 +0200 Subject: [PATCH] KeyRingCertificateStore: Allow for multiple PGPPublicKeyRingCollections as input --- .../pgpainless/wot/KeyRingCertificateStore.kt | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/pgpainless-wot/src/main/kotlin/org/pgpainless/wot/KeyRingCertificateStore.kt b/pgpainless-wot/src/main/kotlin/org/pgpainless/wot/KeyRingCertificateStore.kt index 821b2978..b7c20d1d 100644 --- a/pgpainless-wot/src/main/kotlin/org/pgpainless/wot/KeyRingCertificateStore.kt +++ b/pgpainless-wot/src/main/kotlin/org/pgpainless/wot/KeyRingCertificateStore.kt @@ -15,25 +15,33 @@ import pgp.certificate_store.exception.BadNameException import java.io.InputStream /** - * Implementation of [PGPCertificateStore] which is based on a [PGPPublicKeyRingCollection]. - * During initialization, all items in the [PGPPublicKeyRingCollection] are converted into [Certificates][Certificate] + * Implementation of [PGPCertificateStore] which is based on one or more [PGPPublicKeyRingCollection]. + * During initialization, all items in the [PGPPublicKeyRingCollection]s are converted into [Certificates][Certificate] * and stored in a map keyed by their fingerprints. + * + * In case of fingerprint collisions across certificates from different collections, [Certificate] objects + * from a [PGPPublicKeyRingCollection] instance with a higher list index take precedence. + * * [Certificates][Certificate] being inserted using [insertCertificate] or [insertCertificateBySpecialName] are also * stored in that map, but are not being written into the [PGPPublicKeyRingCollection]. */ -class KeyRingCertificateStore(baseKeyRing: PGPPublicKeyRingCollection) : PGPCertificateStore { +class KeyRingCertificateStore(baseKeyRings: List) : PGPCertificateStore { // Keep certificates inserted only in memory private val certificates = mutableMapOf() init { - for (publicKeyRing in baseKeyRing) { - val fingerprint = OpenPgpFingerprint.of(publicKeyRing).toString() - val certificate = CertificateFactory.certificateFromPublicKeyRing(publicKeyRing, null) - certificates[fingerprint] = certificate + baseKeyRings.forEach { store -> + store.forEach { + val fingerprint = OpenPgpFingerprint.of(it).toString() + val certificate = CertificateFactory.certificateFromPublicKeyRing(it, null) + certificates[fingerprint] = certificate + } } } + constructor(baseKeyRing: PGPPublicKeyRingCollection): this(listOf(baseKeyRing)) + override fun getCertificate(identifier: String?): Certificate { if (identifier == null) { throw BadNameException("Identifier MUST NOT be null.")