From f69d2d20dfccc6f9e041c1dd96cb452d77a05eb8 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Fri, 7 Jul 2023 19:59:02 +0200 Subject: [PATCH] WIP: Work on PGP-DSL --- .../kotlin/org/pgpainless/wot/WebOfTrust.kt | 38 ++++++++------- .../kotlin/org/pgpainless/wot/AdHocTest.kt | 7 ++- .../test/kotlin/org/pgpainless/wot/PGPDSL.kt | 47 +++++++++++++++++++ 3 files changed, 73 insertions(+), 19 deletions(-) create mode 100644 pgpainless-wot/src/test/kotlin/org/pgpainless/wot/PGPDSL.kt diff --git a/pgpainless-wot/src/main/kotlin/org/pgpainless/wot/WebOfTrust.kt b/pgpainless-wot/src/main/kotlin/org/pgpainless/wot/WebOfTrust.kt index 83398741..8a98a5b1 100644 --- a/pgpainless-wot/src/main/kotlin/org/pgpainless/wot/WebOfTrust.kt +++ b/pgpainless-wot/src/main/kotlin/org/pgpainless/wot/WebOfTrust.kt @@ -250,23 +250,6 @@ class WebOfTrust(private val certificateStore: PGPCertificateStore) { */ private fun Fingerprint(fingerprint: OpenPgpFingerprint) = Fingerprint(fingerprint.toString()) - /** - * Map a [PGPSignature] to its [RevocationState]. - * - * @param revocation optional revocation signature - */ - private fun RevocationState(revocation: PGPSignature?): RevocationState { - if (revocation == null) { - return RevocationState.notRevoked() - } - val revocationReason = SignatureSubpacketsUtil.getRevocationReason(revocation) - ?: return RevocationState.hardRevoked() - return if (RevocationAttributes.Reason.isHardRevocation(revocationReason.revocationReason)) - RevocationState.hardRevoked() - else - RevocationState.softRevoked(revocation.creationTime) - } - /** * Return the constructed, initialized [Network]. * @@ -336,4 +319,25 @@ class WebOfTrust(private val certificateStore: PGPCertificateStore) { } } } + + companion object { + + @JvmStatic + /** + * Map a [PGPSignature] to its [RevocationState]. + * + * @param revocation optional revocation signature + */ + fun RevocationState(revocation: PGPSignature?): RevocationState { + if (revocation == null) { + return RevocationState.notRevoked() + } + val revocationReason = SignatureSubpacketsUtil.getRevocationReason(revocation) + ?: return RevocationState.hardRevoked() + return if (RevocationAttributes.Reason.isHardRevocation(revocationReason.revocationReason)) + RevocationState.hardRevoked() + else + RevocationState.softRevoked(revocation.creationTime) + } + } } diff --git a/pgpainless-wot/src/test/kotlin/org/pgpainless/wot/AdHocTest.kt b/pgpainless-wot/src/test/kotlin/org/pgpainless/wot/AdHocTest.kt index 176b5d48..579bf6ff 100644 --- a/pgpainless-wot/src/test/kotlin/org/pgpainless/wot/AdHocTest.kt +++ b/pgpainless-wot/src/test/kotlin/org/pgpainless/wot/AdHocTest.kt @@ -2,12 +2,15 @@ package org.pgpainless.wot import org.junit.jupiter.api.Test import org.pgpainless.wot.testfixtures.AdHocVectors +import kotlin.test.assertEquals +import kotlin.test.assertTrue -class AdHocTest { +class AdHocTest: PGPDSL { @Test fun test() { - val store = AdHocVectors.BestViaRoot().pgpCertificateStore + val vectors = AdHocVectors.BestViaRoot() + val store = vectors.pgpCertificateStore val network = WebOfTrust(store).buildNetwork() } } \ No newline at end of file diff --git a/pgpainless-wot/src/test/kotlin/org/pgpainless/wot/PGPDSL.kt b/pgpainless-wot/src/test/kotlin/org/pgpainless/wot/PGPDSL.kt new file mode 100644 index 00000000..d25f59a9 --- /dev/null +++ b/pgpainless-wot/src/test/kotlin/org/pgpainless/wot/PGPDSL.kt @@ -0,0 +1,47 @@ +package org.pgpainless.wot + +import org.bouncycastle.openpgp.PGPPublicKeyRing +import org.bouncycastle.openpgp.PGPSignature +import org.pgpainless.algorithm.RevocationStateType +import org.pgpainless.key.OpenPgpFingerprint +import org.pgpainless.key.info.KeyRingInfo +import org.pgpainless.wot.dijkstra.sq.CertSynopsis +import org.pgpainless.wot.dijkstra.sq.Fingerprint +import org.pgpainless.wot.dijkstra.sq.RevocationState + +interface PGPDSL { + + fun CertSynopsis(certificate: PGPPublicKeyRing): CertSynopsis { + return CertSynopsis(Fingerprint(certificate), ) + } + + fun CertSynopsis(validatedCert: KeyRingInfo): CertSynopsis { + return CertSynopsis( + Fingerprint(validatedCert.fingerprint), + validatedCert.primaryKeyExpirationDate, + RevocationState(validatedCert.revocationState), + validatedCert.userIds.associateWith{ + RevocationState(validatedCert.getUserIdRevocation(it)) + }) + } + + fun Fingerprint(certificate: PGPPublicKeyRing): Fingerprint { + return Fingerprint(OpenPgpFingerprint.of(certificate)) + } + + fun Fingerprint(pgpFingerprint: OpenPgpFingerprint): Fingerprint { + return Fingerprint(pgpFingerprint.toString()) + } + + fun RevocationState(signature: PGPSignature?): RevocationState { + return WebOfTrust.RevocationState(signature) + } + + fun RevocationState(pgpRevocationState: org.pgpainless.algorithm.RevocationState): RevocationState { + return when(pgpRevocationState.type) { + RevocationStateType.hardRevoked -> RevocationState.hardRevoked() + RevocationStateType.notRevoked -> RevocationState.notRevoked() + else -> RevocationState.softRevoked(pgpRevocationState.date) + } + } +} \ No newline at end of file