Replace usage of KeyIdUtil.formatKeyId() in Kotlin classes with Long.hexKeyId()

This commit is contained in:
Paul Schaub 2023-09-04 14:37:18 +02:00
parent 44c22f9044
commit ab42a7503f
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
5 changed files with 19 additions and 19 deletions

View File

@ -4,6 +4,7 @@
package org.pgpainless.decryption_verification package org.pgpainless.decryption_verification
import _kotlin.hexKeyId
import org.bouncycastle.bcpg.BCPGInputStream import org.bouncycastle.bcpg.BCPGInputStream
import org.bouncycastle.bcpg.UnsupportedPacketVersionException import org.bouncycastle.bcpg.UnsupportedPacketVersionException
import org.bouncycastle.openpgp.* import org.bouncycastle.openpgp.*
@ -21,7 +22,6 @@ import org.pgpainless.exception.*
import org.pgpainless.implementation.ImplementationFactory import org.pgpainless.implementation.ImplementationFactory
import org.pgpainless.key.SubkeyIdentifier import org.pgpainless.key.SubkeyIdentifier
import org.pgpainless.key.protection.UnlockSecretKey import org.pgpainless.key.protection.UnlockSecretKey
import org.pgpainless.key.util.KeyIdUtil
import org.pgpainless.key.util.KeyRingUtils import org.pgpainless.key.util.KeyRingUtils
import org.pgpainless.policy.Policy import org.pgpainless.policy.Policy
import org.pgpainless.signature.SignatureUtils import org.pgpainless.signature.SignatureUtils
@ -180,7 +180,7 @@ class OpenPgpMessageInputStream(
private fun processOnePassSignature() { private fun processOnePassSignature() {
syntaxVerifier.next(InputSymbol.ONE_PASS_SIGNATURE) syntaxVerifier.next(InputSymbol.ONE_PASS_SIGNATURE)
val ops = packetInputStream!!.readOnePassSignature() 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) signatures.addOnePassSignature(ops)
} }
@ -197,11 +197,11 @@ class OpenPgpMessageInputStream(
val keyId = SignatureUtils.determineIssuerKeyId(signature) val keyId = SignatureUtils.determineIssuerKeyId(signature)
if (isSigForOps) { 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.leaveNesting() // TODO: Only leave nesting if all OPSs of the nesting layer are dealt with
signatures.addCorrespondingOnePassSignature(signature, layerMetadata, policy) signatures.addCorrespondingOnePassSignature(signature, layerMetadata, policy)
} else { } 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) signatures.addPrependedSignature(signature)
} }
} }
@ -282,10 +282,10 @@ class OpenPgpMessageInputStream(
// try (known) secret keys // try (known) secret keys
for (pkesk in esks.pkesks) { for (pkesk in esks.pkesks) {
val keyId = pkesk.keyID 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) val decryptionKeys = getDecryptionKey(keyId)
if (decryptionKeys == null) { 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 continue
} }
val secretKey = decryptionKeys.getSecretKey(keyId) val secretKey = decryptionKeys.getSecretKey(keyId)
@ -618,7 +618,7 @@ class OpenPgpMessageInputStream(
if (check != null) { if (check != null) {
detachedSignatures.add(check) detachedSignatures.add(check)
} else { } 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( detachedSignaturesWithMissingCert.add(SignatureVerification.Failure(
SignatureVerification(signature, null), SignatureVerification(signature, null),
SignatureValidationException("Missing verification key."))) SignatureValidationException("Missing verification key.")))
@ -631,7 +631,7 @@ class OpenPgpMessageInputStream(
if (check != null) { if (check != null) {
prependedSignatures.add(check) prependedSignatures.add(check)
} else { } 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( prependedSignaturesWithMissingCert.add(SignatureVerification.Failure(
SignatureVerification(signature, null), SignatureVerification(signature, null),
SignatureValidationException("Missing verification key") SignatureValidationException("Missing verification key")
@ -693,7 +693,7 @@ class OpenPgpMessageInputStream(
} }
if (!found) { 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( inbandSignaturesWithMissingCert.add(SignatureVerification.Failure(
SignatureVerification(signature, null), SignatureVerification(signature, null),
SignatureValidationException("Missing verification key.") SignatureValidationException("Missing verification key.")

View File

@ -4,9 +4,9 @@
package org.pgpainless.key package org.pgpainless.key
import _kotlin.hexKeyId
import org.bouncycastle.openpgp.PGPKeyRing import org.bouncycastle.openpgp.PGPKeyRing
import org.bouncycastle.openpgp.PGPPublicKey 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, * 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( constructor(keys: PGPKeyRing, keyId: Long): this(
OpenPgpFingerprint.of(keys.publicKey), OpenPgpFingerprint.of(keys.publicKey),
OpenPgpFingerprint.of(keys.getPublicKey(keyId) ?: 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) constructor(keys: PGPKeyRing, subkeyFingerprint: OpenPgpFingerprint): this(OpenPgpFingerprint.of(keys), subkeyFingerprint)
val keyId = subkeyFingerprint.keyId val keyId = subkeyFingerprint.keyId
@ -42,7 +42,7 @@ class SubkeyIdentifier(
return false return false
} }
return primaryKeyFingerprint.equals(other.primaryKeyFingerprint) && subkeyFingerprint.equals(other.subkeyFingerprint) return primaryKeyFingerprint == other.primaryKeyFingerprint && subkeyFingerprint == other.subkeyFingerprint
} }
override fun hashCode(): Int { override fun hashCode(): Int {

View File

@ -4,11 +4,11 @@
package org.pgpainless.key.protection package org.pgpainless.key.protection
import _kotlin.hexKeyId
import org.bouncycastle.openpgp.PGPKeyRing import org.bouncycastle.openpgp.PGPKeyRing
import org.bouncycastle.openpgp.PGPPublicKey import org.bouncycastle.openpgp.PGPPublicKey
import org.pgpainless.key.OpenPgpFingerprint import org.pgpainless.key.OpenPgpFingerprint
import org.pgpainless.key.protection.passphrase_provider.SecretKeyPassphraseProvider import org.pgpainless.key.protection.passphrase_provider.SecretKeyPassphraseProvider
import org.pgpainless.key.util.KeyIdUtil
import org.pgpainless.util.Passphrase import org.pgpainless.util.Passphrase
/** /**
@ -54,7 +54,7 @@ class CachingSecretKeyRingProtector : SecretKeyRingProtector, SecretKeyPassphras
*/ */
fun addPassphrase(keyId: Long, passphrase: Passphrase) = apply { fun addPassphrase(keyId: Long, passphrase: Passphrase) = apply {
require(!cache.containsKey(keyId)) { 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." "If you want to replace this passphrase, use replacePassphrase(Long, Passphrase) instead."
} }
cache[keyId] = passphrase cache[keyId] = passphrase
@ -90,7 +90,7 @@ class CachingSecretKeyRingProtector : SecretKeyRingProtector, SecretKeyPassphras
// check for existing passphrases before doing anything // check for existing passphrases before doing anything
keyRing.publicKeys.forEach { keyRing.publicKeys.forEach {
require(!cache.containsKey(it.keyID)) { 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." "If you want to replace the passphrase, use replacePassphrase(PGPKeyRing, Passphrase) instead."
} }
} }

View File

@ -5,6 +5,7 @@
package org.pgpainless.key.protection package org.pgpainless.key.protection
import _kotlin.hexKeyId
import org.bouncycastle.openpgp.PGPException import org.bouncycastle.openpgp.PGPException
import org.bouncycastle.openpgp.PGPPrivateKey import org.bouncycastle.openpgp.PGPPrivateKey
import org.bouncycastle.openpgp.PGPSecretKey import org.bouncycastle.openpgp.PGPSecretKey
@ -13,7 +14,6 @@ import org.pgpainless.PGPainless
import org.pgpainless.exception.KeyIntegrityException import org.pgpainless.exception.KeyIntegrityException
import org.pgpainless.exception.WrongPassphraseException import org.pgpainless.exception.WrongPassphraseException
import org.pgpainless.key.info.KeyInfo import org.pgpainless.key.info.KeyInfo
import org.pgpainless.key.util.KeyIdUtil
import org.pgpainless.key.util.PublicKeyParameterValidationUtil import org.pgpainless.key.util.PublicKeyParameterValidationUtil
import org.pgpainless.util.Passphrase import org.pgpainless.util.Passphrase
import kotlin.jvm.Throws import kotlin.jvm.Throws
@ -43,7 +43,7 @@ class UnlockSecretKey {
if (privateKey == null) { if (privateKey == null) {
if (secretKey.s2K.type in 100..110) { 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}") "Unsupported private S2K type ${secretKey.s2K.type}")
} }
throw PGPException("Cannot decrypt secret key.") throw PGPException("Cannot decrypt secret key.")

View File

@ -4,6 +4,7 @@
package org.pgpainless.signature.subpackets package org.pgpainless.signature.subpackets
import _kotlin.hexKeyId
import org.bouncycastle.bcpg.sig.* import org.bouncycastle.bcpg.sig.*
import org.bouncycastle.openpgp.PGPPublicKey import org.bouncycastle.openpgp.PGPPublicKey
import org.bouncycastle.openpgp.PGPSignature import org.bouncycastle.openpgp.PGPSignature
@ -17,7 +18,6 @@ import org.pgpainless.key.OpenPgpV4Fingerprint
import org.pgpainless.key.OpenPgpV5Fingerprint import org.pgpainless.key.OpenPgpV5Fingerprint
import org.pgpainless.key.OpenPgpV6Fingerprint import org.pgpainless.key.OpenPgpV6Fingerprint
import org.pgpainless.key.generation.type.KeyType import org.pgpainless.key.generation.type.KeyType
import org.pgpainless.key.util.KeyIdUtil
import org.pgpainless.signature.SignatureUtils import org.pgpainless.signature.SignatureUtils
import java.util.* import java.util.*
@ -143,7 +143,7 @@ class SignatureSubpacketsUtil {
@JvmStatic @JvmStatic
fun getKeyExpirationTimeAsDate(signature: PGPSignature, signingKey: PGPPublicKey): Date? = fun getKeyExpirationTimeAsDate(signature: PGPSignature, signingKey: PGPPublicKey): Date? =
require(signature.keyID == signingKey.keyID) { 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 { }.run {
getKeyExpirationTime(signature)?.let { getKeyExpirationTime(signature)?.let {
SignatureUtils.datePlusSeconds(signingKey.creationTime, it.time) SignatureUtils.datePlusSeconds(signingKey.creationTime, it.time)