diff --git a/pgpainless-wot/src/main/kotlin/org/pgpainless/wot/util/CertificationFactory.kt b/pgpainless-wot/src/main/kotlin/org/pgpainless/wot/util/CertificationFactory.kt index 6a8e7b84..7ba1a2f2 100644 --- a/pgpainless-wot/src/main/kotlin/org/pgpainless/wot/util/CertificationFactory.kt +++ b/pgpainless-wot/src/main/kotlin/org/pgpainless/wot/util/CertificationFactory.kt @@ -7,10 +7,7 @@ package org.pgpainless.wot.util import org.bouncycastle.openpgp.PGPSignature import org.pgpainless.algorithm.SignatureType import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil -import org.pgpainless.wot.network.Node -import org.pgpainless.wot.network.EdgeComponent -import org.pgpainless.wot.network.Depth -import org.pgpainless.wot.network.RegexSet +import org.pgpainless.wot.network.* import org.pgpainless.wot.network.RegexSet.Companion.fromExpressionList class CertificationFactory { @@ -20,7 +17,15 @@ class CertificationFactory { fun fromDelegation(issuer: Node, target: Node, signature: PGPSignature): EdgeComponent { - return fromSignature(issuer, target, null, signature) + return Delegation(issuer, + target, + SignatureSubpacketsUtil.getSignatureCreationTime(signature)!!.time, + SignatureSubpacketsUtil.getSignatureExpirationTimeAsDate(signature), + SignatureSubpacketsUtil.isExportable(signature), + getTrustAmountFrom(signature), + getTrustDepthFrom(signature), + regexSetFrom(signature) + ) } @JvmStatic @@ -28,48 +33,32 @@ class CertificationFactory { target: Node, targetUserId: String, signature: PGPSignature): EdgeComponent { - return fromSignature(issuer, target, targetUserId, signature) - } - - @JvmStatic - fun fromSignature(issuer: Node, - target: Node, - targetUserId: String?, - signature: PGPSignature): EdgeComponent { - if (signature.signatureType == SignatureType.CERTIFICATION_REVOCATION.code) { - // Revocations equate to trust of 0/0 - return EdgeComponent( - issuer, - target, - targetUserId, - SignatureSubpacketsUtil.getSignatureCreationTime(signature)!!.time, - SignatureSubpacketsUtil.getSignatureExpirationTimeAsDate(signature), - SignatureSubpacketsUtil.isExportable(signature), - 0, - Depth.limited(0), - regexSetFrom(signature)) - } else { - return EdgeComponent( - issuer, - target, - targetUserId, - SignatureSubpacketsUtil.getSignatureCreationTime(signature)!!.time, - SignatureSubpacketsUtil.getSignatureExpirationTimeAsDate(signature), - SignatureSubpacketsUtil.isExportable(signature), - getTrustAmountFrom(signature), - getTrustDepthFrom(signature), - regexSetFrom(signature)) - } + return Certification(issuer, + target, + targetUserId, + SignatureSubpacketsUtil.getSignatureCreationTime(signature)!!.time, + SignatureSubpacketsUtil.getSignatureExpirationTimeAsDate(signature), + SignatureSubpacketsUtil.isExportable(signature), + getTrustAmountFrom(signature), + getTrustDepthFrom(signature)) } @JvmStatic private fun getTrustAmountFrom(signature: PGPSignature): Int { + if (signature.signatureType in intArrayOf(PGPSignature.KEY_REVOCATION, PGPSignature.CERTIFICATION_REVOCATION)) { + return 0 + } + val packet = SignatureSubpacketsUtil.getTrustSignature(signature) return packet?.trustAmount ?: 120 } @JvmStatic private fun getTrustDepthFrom(signature: PGPSignature): Depth { + if (signature.signatureType in intArrayOf(PGPSignature.KEY_REVOCATION, PGPSignature.CERTIFICATION_REVOCATION)) { + return Depth.auto(0) + } + val packet = SignatureSubpacketsUtil.getTrustSignature(signature) return if (packet != null) { Depth.auto(packet.depth) diff --git a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/network/EdgeComponent.kt b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/network/EdgeComponent.kt index d439a964..27d90de9 100644 --- a/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/network/EdgeComponent.kt +++ b/wot-dijkstra/src/main/kotlin/org/pgpainless/wot/network/EdgeComponent.kt @@ -20,7 +20,7 @@ import java.util.* * @param trustDepth degree to which the issuer trusts the target as trusted introducer * @param regexes regular expressions for user-ids which the target is allowed to introduce */ -data class EdgeComponent( +open class EdgeComponent( val issuer: Node, val target: Node, val userId: String?, @@ -30,14 +30,55 @@ data class EdgeComponent( val trustAmount: Int, val trustDepth: Depth, val regexes: RegexSet -) { +) + +class Certification( + issuer: Node, + target: Node, + userId: String, + creationTime: Date, + expirationTime: Date?, + exportable: Boolean, + trustAmount: Int?, + trustDepth: Depth?, +): EdgeComponent( + issuer, + target, + userId, + creationTime, + expirationTime, + exportable, + trustAmount ?: 120, + trustDepth ?: Depth.limited(0), + RegexSet.wildcard()) { override fun toString(): String { - return if (trustDepth > 0) { - val scope = if (regexes.regexStrings.isEmpty()) "" else ", scope: $regexes" - "${issuer.fingerprint} delegates to ${target.fingerprint} [$trustAmount, depth $trustDepth$scope]" - } else { - "${issuer.fingerprint} certifies binding: $userId <-> ${target.fingerprint} [$trustAmount]" - } + return "${issuer.fingerprint} certifies binding: $userId <-> ${target.fingerprint} [$trustAmount]" + } +} + +class Delegation( + issuer: Node, + target: Node, + creationTime: Date, + expirationTime: Date?, + exportable: Boolean, + trustAmount: Int, + trustDepth: Depth, + regexes: RegexSet +): EdgeComponent( + issuer, + target, + null, + creationTime, + expirationTime, + exportable, + trustAmount, + trustDepth, + regexes) { + + override fun toString(): String { + val scope = if (regexes.regexStrings.isEmpty()) "" else ", scope: $regexes" + return "${issuer.fingerprint} delegates to ${target.fingerprint} [$trustAmount, depth $trustDepth$scope]" } } \ No newline at end of file