Separate EdgeComponents in Certifications and Delegations

This commit is contained in:
Paul Schaub 2023-07-15 18:56:04 +02:00
parent cee061d01c
commit 0325122a31
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
2 changed files with 75 additions and 45 deletions

View File

@ -7,10 +7,7 @@ package org.pgpainless.wot.util
import org.bouncycastle.openpgp.PGPSignature import org.bouncycastle.openpgp.PGPSignature
import org.pgpainless.algorithm.SignatureType import org.pgpainless.algorithm.SignatureType
import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil import org.pgpainless.signature.subpackets.SignatureSubpacketsUtil
import org.pgpainless.wot.network.Node import org.pgpainless.wot.network.*
import org.pgpainless.wot.network.EdgeComponent
import org.pgpainless.wot.network.Depth
import org.pgpainless.wot.network.RegexSet
import org.pgpainless.wot.network.RegexSet.Companion.fromExpressionList import org.pgpainless.wot.network.RegexSet.Companion.fromExpressionList
class CertificationFactory { class CertificationFactory {
@ -20,7 +17,15 @@ class CertificationFactory {
fun fromDelegation(issuer: Node, fun fromDelegation(issuer: Node,
target: Node, target: Node,
signature: PGPSignature): EdgeComponent { 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 @JvmStatic
@ -28,48 +33,32 @@ class CertificationFactory {
target: Node, target: Node,
targetUserId: String, targetUserId: String,
signature: PGPSignature): EdgeComponent { signature: PGPSignature): EdgeComponent {
return fromSignature(issuer, target, targetUserId, signature) return Certification(issuer,
} target,
targetUserId,
@JvmStatic SignatureSubpacketsUtil.getSignatureCreationTime(signature)!!.time,
fun fromSignature(issuer: Node, SignatureSubpacketsUtil.getSignatureExpirationTimeAsDate(signature),
target: Node, SignatureSubpacketsUtil.isExportable(signature),
targetUserId: String?, getTrustAmountFrom(signature),
signature: PGPSignature): EdgeComponent { getTrustDepthFrom(signature))
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))
}
} }
@JvmStatic @JvmStatic
private fun getTrustAmountFrom(signature: PGPSignature): Int { private fun getTrustAmountFrom(signature: PGPSignature): Int {
if (signature.signatureType in intArrayOf(PGPSignature.KEY_REVOCATION, PGPSignature.CERTIFICATION_REVOCATION)) {
return 0
}
val packet = SignatureSubpacketsUtil.getTrustSignature(signature) val packet = SignatureSubpacketsUtil.getTrustSignature(signature)
return packet?.trustAmount ?: 120 return packet?.trustAmount ?: 120
} }
@JvmStatic @JvmStatic
private fun getTrustDepthFrom(signature: PGPSignature): Depth { 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) val packet = SignatureSubpacketsUtil.getTrustSignature(signature)
return if (packet != null) { return if (packet != null) {
Depth.auto(packet.depth) Depth.auto(packet.depth)

View File

@ -20,7 +20,7 @@ import java.util.*
* @param trustDepth degree to which the issuer trusts the target as trusted introducer * @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 * @param regexes regular expressions for user-ids which the target is allowed to introduce
*/ */
data class EdgeComponent( open class EdgeComponent(
val issuer: Node, val issuer: Node,
val target: Node, val target: Node,
val userId: String?, val userId: String?,
@ -30,14 +30,55 @@ data class EdgeComponent(
val trustAmount: Int, val trustAmount: Int,
val trustDepth: Depth, val trustDepth: Depth,
val regexes: RegexSet 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 { override fun toString(): String {
return if (trustDepth > 0) { return "${issuer.fingerprint} certifies binding: $userId <-> ${target.fingerprint} [$trustAmount]"
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]" 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]"
} }
} }