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

OpenPGPFingerprint: Add factory methods for new key / subkey classes

This commit is contained in:
Paul Schaub 2025-02-17 12:33:43 +01:00
parent 5161a46594
commit ab1670598d
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 28 additions and 13 deletions

View file

@ -197,7 +197,7 @@ class EncryptionOptions(private val purpose: EncryptionPurpose) {
encryptionKeySelector.selectEncryptionSubkeys(
info.getEncryptionSubkeys(userId, purpose))
if (subkeys.isEmpty()) {
throw UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(cert.pgpPublicKeyRing))
throw UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(cert))
}
for (subkey in subkeys) {
@ -296,12 +296,12 @@ class EncryptionOptions(private val purpose: EncryptionPurpose) {
info.primaryKeyExpirationDate
} catch (e: NoSuchElementException) {
throw UnacceptableSelfSignatureException(
OpenPgpFingerprint.of(cert.pgpPublicKeyRing))
OpenPgpFingerprint.of(cert))
}
if (primaryKeyExpiration != null && primaryKeyExpiration < evaluationDate) {
throw ExpiredKeyException(
OpenPgpFingerprint.of(cert.pgpPublicKeyRing), primaryKeyExpiration)
OpenPgpFingerprint.of(cert), primaryKeyExpiration)
}
var encryptionSubkeys = selector.selectEncryptionSubkeys(info.getEncryptionSubkeys(purpose))
@ -318,7 +318,7 @@ class EncryptionOptions(private val purpose: EncryptionPurpose) {
}
if (encryptionSubkeys.isEmpty()) {
throw UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(cert.pgpPublicKeyRing))
throw UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(cert))
}
for (subkey in encryptionSubkeys) {

View file

@ -149,7 +149,7 @@ class SigningOptions {
val keyRingInfo = inspectKeyRing(signingKey, evaluationDate)
if (userId != null && !keyRingInfo.isUserIdValid(userId)) {
throw UnboundUserIdException(
of(signingKey.pgpSecretKeyRing),
of(signingKey),
userId.toString(),
keyRingInfo.getLatestUserIdCertification(userId),
keyRingInfo.getUserIdRevocation(userId))
@ -157,14 +157,14 @@ class SigningOptions {
val signingPubKeys = keyRingInfo.signingSubkeys
if (signingPubKeys.isEmpty()) {
throw UnacceptableSigningKeyException(of(signingKey.pgpSecretKeyRing))
throw UnacceptableSigningKeyException(of(signingKey))
}
for (signingPubKey in signingPubKeys) {
val signingSecKey: OpenPGPSecretKey =
signingKey.getSecretKey(signingPubKey)
?: throw MissingSecretKeyException(
of(signingKey.pgpSecretKeyRing), signingPubKey.keyIdentifier.keyId)
of(signingKey), signingPubKey.keyIdentifier.keyId)
val signingPrivKey: OpenPGPPrivateKey =
unlockSecretKey(signingSecKey, signingKeyProtector)
val hashAlgorithms =
@ -220,12 +220,12 @@ class SigningOptions {
val keyRingInfo = inspectKeyRing(openPGPKey, evaluationDate)
val signingPubKeys = keyRingInfo.signingSubkeys
if (signingPubKeys.isEmpty()) {
throw UnacceptableSigningKeyException(of(openPGPKey.pgpSecretKeyRing))
throw UnacceptableSigningKeyException(of(openPGPKey))
}
if (!signingPubKeys.any { it.keyIdentifier.matches(signingKey.keyIdentifier) }) {
throw MissingSecretKeyException(
of(openPGPKey.pgpSecretKeyRing), signingKey.keyIdentifier.keyId)
of(openPGPKey), signingKey.keyIdentifier.keyId)
}
val signingPrivKey = unlockSecretKey(signingKey, signingKeyProtector)
@ -324,7 +324,7 @@ class SigningOptions {
val keyRingInfo = inspectKeyRing(signingKey, evaluationDate)
if (userId != null && !keyRingInfo.isUserIdValid(userId)) {
throw UnboundUserIdException(
of(signingKey.pgpSecretKeyRing),
of(signingKey),
userId.toString(),
keyRingInfo.getLatestUserIdCertification(userId),
keyRingInfo.getUserIdRevocation(userId))
@ -332,14 +332,14 @@ class SigningOptions {
val signingPubKeys = keyRingInfo.signingSubkeys
if (signingPubKeys.isEmpty()) {
throw UnacceptableSigningKeyException(of(signingKey.pgpSecretKeyRing))
throw UnacceptableSigningKeyException(of(signingKey))
}
for (signingPubKey in signingPubKeys) {
val signingSecKey: OpenPGPSecretKey =
signingKey.getSecretKey(signingPubKey.keyIdentifier)
?: throw MissingSecretKeyException(
of(signingKey.pgpSecretKeyRing), signingPubKey.keyIdentifier.keyId)
of(signingKey), signingPubKey.keyIdentifier.keyId)
addDetachedSignature(
signingKeyProtector, signingSecKey, userId, signatureType, subpacketCallback)
}
@ -443,7 +443,7 @@ class SigningOptions {
if (!getPolicy().publicKeyAlgorithmPolicy.isAcceptable(publicKeyAlgorithm, bitStrength)) {
throw UnacceptableSigningKeyException(
PublicKeyAlgorithmPolicyException(
of(signingKey.secretKey.pgpSecretKey),
of(signingKey),
signingSecretKey.keyID,
publicKeyAlgorithm,
bitStrength))

View file

@ -9,6 +9,9 @@ import org.bouncycastle.bcpg.KeyIdentifier
import org.bouncycastle.openpgp.PGPKeyRing
import org.bouncycastle.openpgp.PGPPublicKey
import org.bouncycastle.openpgp.PGPSecretKey
import org.bouncycastle.openpgp.api.OpenPGPCertificate
import org.bouncycastle.openpgp.api.OpenPGPCertificate.OpenPGPComponentKey
import org.bouncycastle.openpgp.api.OpenPGPKey.OpenPGPPrivateKey
import org.bouncycastle.util.encoders.Hex
/** Abstract super class of different version OpenPGP fingerprints. */
@ -129,6 +132,18 @@ abstract class OpenPgpFingerprint : CharSequence, Comparable<OpenPgpFingerprint>
*/
@JvmStatic fun of(keys: PGPKeyRing): OpenPgpFingerprint = of(keys.publicKey)
/**
* Return the [OpenPgpFingerprint] of the primary key of the given [OpenPGPCertificate].
*/
@JvmStatic fun of(cert: OpenPGPCertificate): OpenPgpFingerprint = of(cert.pgpPublicKeyRing)
/**
* Return the [OpenPgpFingerprint] of the given [OpenPGPComponentKey].
*/
@JvmStatic fun of (key: OpenPGPComponentKey): OpenPgpFingerprint = of(key.pgpPublicKey)
@JvmStatic fun of (key: OpenPGPPrivateKey): OpenPgpFingerprint = of(key.secretKey)
/**
* Try to parse an [OpenPgpFingerprint] from the given fingerprint string. If the trimmed
* fingerprint without whitespace is 64 characters long, it is either a v5 or v6