1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 18:29:39 +02:00

Replace KeyRingInfo.publicKey with primaryKey

This commit is contained in:
Paul Schaub 2025-02-26 13:40:56 +01:00
parent 6eaa483650
commit c886b56faf
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -47,13 +47,15 @@ class KeyRingInfo(
referenceDate: Date = Date() referenceDate: Date = Date()
) : this(keys, PGPainless.getPolicy(), referenceDate) ) : this(keys, PGPainless.getPolicy(), referenceDate)
// private val signatures: Signatures = Signatures(keys.pgpKeyRing, referenceDate, policy) /** Primary [OpenPGPCertificate.OpenPGPPrimaryKey]. */
val primaryKey: OpenPGPCertificate.OpenPGPPrimaryKey = keys.primaryKey
/** Primary [OpenPGPCertificate.OpenPGPPrimaryKey]. */ /** Primary [OpenPGPCertificate.OpenPGPPrimaryKey]. */
val publicKey: OpenPGPCertificate.OpenPGPPrimaryKey = keys.primaryKey @Deprecated("Use primaryKey instead.", replaceWith = ReplaceWith("primaryKey"))
val publicKey: OpenPGPCertificate.OpenPGPPrimaryKey = primaryKey
/** Primary key ID. */ /** Primary key ID. */
val keyIdentifier: KeyIdentifier = publicKey.keyIdentifier val keyIdentifier: KeyIdentifier = primaryKey.keyIdentifier
@Deprecated( @Deprecated(
"Use of raw key-ids is deprecated in favor of key-identifiers", "Use of raw key-ids is deprecated in favor of key-identifiers",
@ -61,17 +63,17 @@ class KeyRingInfo(
val keyId: Long = keyIdentifier.keyId val keyId: Long = keyIdentifier.keyId
/** Primary key fingerprint. */ /** Primary key fingerprint. */
val fingerprint: OpenPgpFingerprint = OpenPgpFingerprint.of(publicKey.pgpPublicKey) val fingerprint: OpenPgpFingerprint = OpenPgpFingerprint.of(primaryKey.pgpPublicKey)
/** All User-IDs (valid, expired, revoked). */ /** All User-IDs (valid, expired, revoked). */
val userIds: List<String> = KeyRingUtils.getUserIdsIgnoringInvalidUTF8(publicKey.pgpPublicKey) val userIds: List<String> = KeyRingUtils.getUserIdsIgnoringInvalidUTF8(primaryKey.pgpPublicKey)
/** Primary User-ID. */ /** Primary User-ID. */
val primaryUserId: String? = keys.getPrimaryUserId(referenceDate)?.userId val primaryUserId: String? = keys.getPrimaryUserId(referenceDate)?.userId
/** Revocation State. */ /** Revocation State. */
val revocationState: RevocationState = val revocationState: RevocationState =
publicKey.getLatestSelfSignature(referenceDate)?.let { primaryKey.getLatestSelfSignature(referenceDate)?.let {
if (!it.isRevocation) RevocationState.notRevoked() if (!it.isRevocation) RevocationState.notRevoked()
else if (it.isHardRevocation) RevocationState.hardRevoked() else if (it.isHardRevocation) RevocationState.hardRevoked()
else RevocationState.softRevoked(it.creationTime) else RevocationState.softRevoked(it.creationTime)
@ -94,7 +96,7 @@ class KeyRingInfo(
} else null } else null
/** OpenPGP key version. */ /** OpenPGP key version. */
val version: Int = publicKey.version val version: Int = primaryKey.version
/** /**
* Return all [public component keys][OpenPGPComponentKey] of this key ring. The first key in * Return all [public component keys][OpenPGPComponentKey] of this key ring. The first key in
@ -133,18 +135,18 @@ class KeyRingInfo(
/** Newest direct-key self-signature on the primary key. */ /** Newest direct-key self-signature on the primary key. */
val latestDirectKeySelfSignature: PGPSignature? = val latestDirectKeySelfSignature: PGPSignature? =
publicKey.getLatestDirectKeySelfSignature(referenceDate)?.signature primaryKey.getLatestDirectKeySelfSignature(referenceDate)?.signature
/** Newest primary-key revocation self-signature. */ /** Newest primary-key revocation self-signature. */
val revocationSelfSignature: PGPSignature? = val revocationSelfSignature: PGPSignature? =
publicKey.getLatestKeyRevocationSignature(referenceDate)?.signature primaryKey.getLatestKeyRevocationSignature(referenceDate)?.signature
/** Public-key encryption-algorithm of the primary key. */ /** Public-key encryption-algorithm of the primary key. */
val algorithm: PublicKeyAlgorithm = val algorithm: PublicKeyAlgorithm =
PublicKeyAlgorithm.requireFromId(publicKey.pgpPublicKey.algorithm) PublicKeyAlgorithm.requireFromId(primaryKey.pgpPublicKey.algorithm)
/** Creation date of the primary key. */ /** Creation date of the primary key. */
val creationDate: Date = publicKey.creationTime!! val creationDate: Date = primaryKey.creationTime!!
/** Latest date at which the key was modified (either by adding a subkey or self-signature). */ /** Latest date at which the key was modified (either by adding a subkey or self-signature). */
val lastModified: Date = keys.lastModificationDate val lastModified: Date = keys.lastModificationDate
@ -186,14 +188,14 @@ class KeyRingInfo(
get() { get() {
val directKeyExpirationDate: Date? = val directKeyExpirationDate: Date? =
latestDirectKeySelfSignature?.let { latestDirectKeySelfSignature?.let {
getKeyExpirationTimeAsDate(it, publicKey.pgpPublicKey) getKeyExpirationTimeAsDate(it, primaryKey.pgpPublicKey)
} }
val possiblyExpiredPrimaryUserId = getPossiblyExpiredPrimaryUserId() val possiblyExpiredPrimaryUserId = getPossiblyExpiredPrimaryUserId()
val primaryUserIdCertification = val primaryUserIdCertification =
possiblyExpiredPrimaryUserId?.let { getLatestUserIdCertification(it) } possiblyExpiredPrimaryUserId?.let { getLatestUserIdCertification(it) }
val userIdExpirationDate: Date? = val userIdExpirationDate: Date? =
primaryUserIdCertification?.let { primaryUserIdCertification?.let {
getKeyExpirationTimeAsDate(it, publicKey.pgpPublicKey) getKeyExpirationTimeAsDate(it, primaryKey.pgpPublicKey)
} }
if (latestDirectKeySelfSignature == null && primaryUserIdCertification == null) { if (latestDirectKeySelfSignature == null && primaryUserIdCertification == null) {
@ -276,7 +278,7 @@ class KeyRingInfo(
* @return expiration date * @return expiration date
*/ */
fun getSubkeyExpirationDate(keyId: Long): Date? { fun getSubkeyExpirationDate(keyId: Long): Date? {
if (publicKey.keyIdentifier.keyId == keyId) return primaryKeyExpirationDate if (primaryKey.keyIdentifier.keyId == keyId) return primaryKeyExpirationDate
val subkey = val subkey =
getPublicKey(keyId) getPublicKey(keyId)
?: throw NoSuchElementException( ?: throw NoSuchElementException(
@ -353,7 +355,7 @@ class KeyRingInfo(
): List<OpenPGPComponentKey> { ): List<OpenPGPComponentKey> {
if (userId != null && !isUserIdValid(userId)) { if (userId != null && !isUserIdValid(userId)) {
throw UnboundUserIdException( throw UnboundUserIdException(
OpenPgpFingerprint.of(publicKey.pgpPublicKey), OpenPgpFingerprint.of(primaryKey.pgpPublicKey),
userId.toString(), userId.toString(),
getLatestUserIdCertification(userId), getLatestUserIdCertification(userId),
getUserIdRevocation(userId)) getUserIdRevocation(userId))
@ -488,7 +490,7 @@ class KeyRingInfo(
* @return list of key flags * @return list of key flags
*/ */
fun getKeyFlagsOf(keyId: Long): List<KeyFlag> = fun getKeyFlagsOf(keyId: Long): List<KeyFlag> =
if (keyId == publicKey.keyIdentifier.keyId) { if (keyId == primaryKey.keyIdentifier.keyId) {
latestDirectKeySelfSignature?.let { sig -> latestDirectKeySelfSignature?.let { sig ->
SignatureSubpacketsUtil.parseKeyFlags(sig)?.let { flags -> SignatureSubpacketsUtil.parseKeyFlags(sig)?.let { flags ->
return flags return flags
@ -597,7 +599,7 @@ class KeyRingInfo(
* key of the key. * key of the key.
*/ */
fun getPublicKey(identifier: SubkeyIdentifier): OpenPGPComponentKey? { fun getPublicKey(identifier: SubkeyIdentifier): OpenPGPComponentKey? {
require(publicKey.keyIdentifier.equals(identifier.keyIdentifier)) { require(primaryKey.keyIdentifier.equals(identifier.keyIdentifier)) {
"Mismatching primary key ID." "Mismatching primary key ID."
} }
return getPublicKey(identifier.componentKeyIdentifier) return getPublicKey(identifier.componentKeyIdentifier)
@ -645,7 +647,7 @@ class KeyRingInfo(
fun isUserIdValid(userId: CharSequence): Boolean { fun isUserIdValid(userId: CharSequence): Boolean {
var valid = isUserIdBound(userId) var valid = isUserIdBound(userId)
if (primaryUserId != null) valid = valid && isUserIdBound(primaryUserId) if (primaryUserId != null) valid = valid && isUserIdBound(primaryUserId)
valid = valid && isKeyValidlyBound(publicKey.keyIdentifier) valid = valid && isKeyValidlyBound(primaryKey.keyIdentifier)
return valid return valid
} }