From ab42a7503f247522f0c99d1deff128827bcb8cfd Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 4 Sep 2023 14:37:18 +0200 Subject: [PATCH] Replace usage of KeyIdUtil.formatKeyId() in Kotlin classes with Long.hexKeyId() --- .../OpenPgpMessageInputStream.kt | 18 +++++++++--------- .../org/pgpainless/key/SubkeyIdentifier.kt | 6 +++--- .../CachingSecretKeyRingProtector.kt | 6 +++--- .../key/protection/UnlockSecretKey.kt | 4 ++-- .../subpackets/SignatureSubpacketsUtil.kt | 4 ++-- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/decryption_verification/OpenPgpMessageInputStream.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/decryption_verification/OpenPgpMessageInputStream.kt index 1f34ec23..3c056cfa 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/decryption_verification/OpenPgpMessageInputStream.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/decryption_verification/OpenPgpMessageInputStream.kt @@ -4,6 +4,7 @@ package org.pgpainless.decryption_verification +import _kotlin.hexKeyId import org.bouncycastle.bcpg.BCPGInputStream import org.bouncycastle.bcpg.UnsupportedPacketVersionException import org.bouncycastle.openpgp.* @@ -21,7 +22,6 @@ import org.pgpainless.exception.* import org.pgpainless.implementation.ImplementationFactory import org.pgpainless.key.SubkeyIdentifier import org.pgpainless.key.protection.UnlockSecretKey -import org.pgpainless.key.util.KeyIdUtil import org.pgpainless.key.util.KeyRingUtils import org.pgpainless.policy.Policy import org.pgpainless.signature.SignatureUtils @@ -180,7 +180,7 @@ class OpenPgpMessageInputStream( private fun processOnePassSignature() { syntaxVerifier.next(InputSymbol.ONE_PASS_SIGNATURE) val ops = packetInputStream!!.readOnePassSignature() - LOGGER.debug("One-Pass-Signature Packet by key ${KeyIdUtil.formatKeyId(ops.keyID)} at depth ${layerMetadata.depth} encountered.") + LOGGER.debug("One-Pass-Signature Packet by key ${ops.keyID.hexKeyId()} at depth ${layerMetadata.depth} encountered.") signatures.addOnePassSignature(ops) } @@ -197,11 +197,11 @@ class OpenPgpMessageInputStream( val keyId = SignatureUtils.determineIssuerKeyId(signature) if (isSigForOps) { - LOGGER.debug("Signature Packet corresponding to One-Pass-Signature by key ${KeyIdUtil.formatKeyId(keyId)} at depth ${layerMetadata.depth} encountered.") + LOGGER.debug("Signature Packet corresponding to One-Pass-Signature by key ${keyId.hexKeyId()} at depth ${layerMetadata.depth} encountered.") signatures.leaveNesting() // TODO: Only leave nesting if all OPSs of the nesting layer are dealt with signatures.addCorrespondingOnePassSignature(signature, layerMetadata, policy) } else { - LOGGER.debug("Prepended Signature Packet by key ${KeyIdUtil.formatKeyId(keyId)} at depth ${layerMetadata.depth} encountered.") + LOGGER.debug("Prepended Signature Packet by key ${keyId.hexKeyId()} at depth ${layerMetadata.depth} encountered.") signatures.addPrependedSignature(signature) } } @@ -282,10 +282,10 @@ class OpenPgpMessageInputStream( // try (known) secret keys for (pkesk in esks.pkesks) { val keyId = pkesk.keyID - LOGGER.debug("Encountered PKESK for recipient ${KeyIdUtil.formatKeyId(keyId)}") + LOGGER.debug("Encountered PKESK for recipient ${keyId.hexKeyId()}") val decryptionKeys = getDecryptionKey(keyId) if (decryptionKeys == null) { - LOGGER.debug("Skipping PKESK because no matching key ${KeyIdUtil.formatKeyId(keyId)} was provided.") + LOGGER.debug("Skipping PKESK because no matching key ${keyId.hexKeyId()} was provided.") continue } val secretKey = decryptionKeys.getSecretKey(keyId) @@ -618,7 +618,7 @@ class OpenPgpMessageInputStream( if (check != null) { detachedSignatures.add(check) } else { - LOGGER.debug("No suitable certificate for verification of signature by key ${KeyIdUtil.formatKeyId(keyId)} found.") + LOGGER.debug("No suitable certificate for verification of signature by key ${keyId.hexKeyId()} found.") detachedSignaturesWithMissingCert.add(SignatureVerification.Failure( SignatureVerification(signature, null), SignatureValidationException("Missing verification key."))) @@ -631,7 +631,7 @@ class OpenPgpMessageInputStream( if (check != null) { prependedSignatures.add(check) } else { - LOGGER.debug("No suitable certificate for verification of signature by key ${KeyIdUtil.formatKeyId(keyId)} found.") + LOGGER.debug("No suitable certificate for verification of signature by key ${keyId.hexKeyId()} found.") prependedSignaturesWithMissingCert.add(SignatureVerification.Failure( SignatureVerification(signature, null), SignatureValidationException("Missing verification key") @@ -693,7 +693,7 @@ class OpenPgpMessageInputStream( } if (!found) { - LOGGER.debug("No suitable certificate for verification of signature by key ${KeyIdUtil.formatKeyId(keyId)} found.") + LOGGER.debug("No suitable certificate for verification of signature by key ${keyId.hexKeyId()} found.") inbandSignaturesWithMissingCert.add(SignatureVerification.Failure( SignatureVerification(signature, null), SignatureValidationException("Missing verification key.") diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/key/SubkeyIdentifier.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/key/SubkeyIdentifier.kt index 61b45874..c09c1ee5 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/key/SubkeyIdentifier.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/key/SubkeyIdentifier.kt @@ -4,9 +4,9 @@ package org.pgpainless.key +import _kotlin.hexKeyId import org.bouncycastle.openpgp.PGPKeyRing import org.bouncycastle.openpgp.PGPPublicKey -import org.pgpainless.key.util.KeyIdUtil /** * Tuple class used to identify a subkey by fingerprints of the primary key of the subkeys key ring, @@ -22,7 +22,7 @@ class SubkeyIdentifier( constructor(keys: PGPKeyRing, keyId: Long): this( OpenPgpFingerprint.of(keys.publicKey), OpenPgpFingerprint.of(keys.getPublicKey(keyId) ?: - throw NoSuchElementException("OpenPGP key does not contain subkey ${KeyIdUtil.formatKeyId(keyId)}"))) + throw NoSuchElementException("OpenPGP key does not contain subkey ${keyId.hexKeyId()}"))) constructor(keys: PGPKeyRing, subkeyFingerprint: OpenPgpFingerprint): this(OpenPgpFingerprint.of(keys), subkeyFingerprint) val keyId = subkeyFingerprint.keyId @@ -42,7 +42,7 @@ class SubkeyIdentifier( return false } - return primaryKeyFingerprint.equals(other.primaryKeyFingerprint) && subkeyFingerprint.equals(other.subkeyFingerprint) + return primaryKeyFingerprint == other.primaryKeyFingerprint && subkeyFingerprint == other.subkeyFingerprint } override fun hashCode(): Int { diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/key/protection/CachingSecretKeyRingProtector.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/key/protection/CachingSecretKeyRingProtector.kt index 4451aa0f..7a2bcc26 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/key/protection/CachingSecretKeyRingProtector.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/key/protection/CachingSecretKeyRingProtector.kt @@ -4,11 +4,11 @@ package org.pgpainless.key.protection +import _kotlin.hexKeyId import org.bouncycastle.openpgp.PGPKeyRing import org.bouncycastle.openpgp.PGPPublicKey import org.pgpainless.key.OpenPgpFingerprint import org.pgpainless.key.protection.passphrase_provider.SecretKeyPassphraseProvider -import org.pgpainless.key.util.KeyIdUtil import org.pgpainless.util.Passphrase /** @@ -54,7 +54,7 @@ class CachingSecretKeyRingProtector : SecretKeyRingProtector, SecretKeyPassphras */ fun addPassphrase(keyId: Long, passphrase: Passphrase) = apply { require(!cache.containsKey(keyId)) { - "The cache already holds a passphrase for ID ${KeyIdUtil.formatKeyId(keyId)}.\n" + + "The cache already holds a passphrase for ID ${keyId.hexKeyId()}.\n" + "If you want to replace this passphrase, use replacePassphrase(Long, Passphrase) instead." } cache[keyId] = passphrase @@ -90,7 +90,7 @@ class CachingSecretKeyRingProtector : SecretKeyRingProtector, SecretKeyPassphras // check for existing passphrases before doing anything keyRing.publicKeys.forEach { require(!cache.containsKey(it.keyID)) { - "The cache already holds a passphrase for the key with ID ${KeyIdUtil.formatKeyId(it.keyID)}.\n" + + "The cache already holds a passphrase for the key with ID ${it.keyID.hexKeyId()}.\n" + "If you want to replace the passphrase, use replacePassphrase(PGPKeyRing, Passphrase) instead." } } diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/key/protection/UnlockSecretKey.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/key/protection/UnlockSecretKey.kt index 533b33a7..7385b265 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/key/protection/UnlockSecretKey.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/key/protection/UnlockSecretKey.kt @@ -5,6 +5,7 @@ package org.pgpainless.key.protection +import _kotlin.hexKeyId import org.bouncycastle.openpgp.PGPException import org.bouncycastle.openpgp.PGPPrivateKey import org.bouncycastle.openpgp.PGPSecretKey @@ -13,7 +14,6 @@ import org.pgpainless.PGPainless import org.pgpainless.exception.KeyIntegrityException import org.pgpainless.exception.WrongPassphraseException import org.pgpainless.key.info.KeyInfo -import org.pgpainless.key.util.KeyIdUtil import org.pgpainless.key.util.PublicKeyParameterValidationUtil import org.pgpainless.util.Passphrase import kotlin.jvm.Throws @@ -43,7 +43,7 @@ class UnlockSecretKey { if (privateKey == null) { if (secretKey.s2K.type in 100..110) { - throw PGPException("Cannot decrypt secret key ${KeyIdUtil.formatKeyId(secretKey.keyID)}: \n" + + throw PGPException("Cannot decrypt secret key ${secretKey.keyID.hexKeyId()}: \n" + "Unsupported private S2K type ${secretKey.s2K.type}") } throw PGPException("Cannot decrypt secret key.") diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SignatureSubpacketsUtil.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SignatureSubpacketsUtil.kt index 23fe716c..1b9b432a 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SignatureSubpacketsUtil.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SignatureSubpacketsUtil.kt @@ -4,6 +4,7 @@ package org.pgpainless.signature.subpackets +import _kotlin.hexKeyId import org.bouncycastle.bcpg.sig.* import org.bouncycastle.openpgp.PGPPublicKey import org.bouncycastle.openpgp.PGPSignature @@ -17,7 +18,6 @@ import org.pgpainless.key.OpenPgpV4Fingerprint import org.pgpainless.key.OpenPgpV5Fingerprint import org.pgpainless.key.OpenPgpV6Fingerprint import org.pgpainless.key.generation.type.KeyType -import org.pgpainless.key.util.KeyIdUtil import org.pgpainless.signature.SignatureUtils import java.util.* @@ -143,7 +143,7 @@ class SignatureSubpacketsUtil { @JvmStatic fun getKeyExpirationTimeAsDate(signature: PGPSignature, signingKey: PGPPublicKey): Date? = require(signature.keyID == signingKey.keyID) { - "Provided key (${KeyIdUtil.formatKeyId(signingKey.keyID)}) did not create the signature (${KeyIdUtil.formatKeyId(signature.keyID)})" + "Provided key (${signingKey.keyID.hexKeyId()}) did not create the signature (${signature.keyID.hexKeyId()})" }.run { getKeyExpirationTime(signature)?.let { SignatureUtils.datePlusSeconds(signingKey.creationTime, it.time)