1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 18:29: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 acb5a4a550
commit 92da00fc8c
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( encryptionKeySelector.selectEncryptionSubkeys(
info.getEncryptionSubkeys(userId, purpose)) info.getEncryptionSubkeys(userId, purpose))
if (subkeys.isEmpty()) { if (subkeys.isEmpty()) {
throw UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(cert.pgpPublicKeyRing)) throw UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(cert))
} }
for (subkey in subkeys) { for (subkey in subkeys) {
@ -296,12 +296,12 @@ class EncryptionOptions(private val purpose: EncryptionPurpose) {
info.primaryKeyExpirationDate info.primaryKeyExpirationDate
} catch (e: NoSuchElementException) { } catch (e: NoSuchElementException) {
throw UnacceptableSelfSignatureException( throw UnacceptableSelfSignatureException(
OpenPgpFingerprint.of(cert.pgpPublicKeyRing)) OpenPgpFingerprint.of(cert))
} }
if (primaryKeyExpiration != null && primaryKeyExpiration < evaluationDate) { if (primaryKeyExpiration != null && primaryKeyExpiration < evaluationDate) {
throw ExpiredKeyException( throw ExpiredKeyException(
OpenPgpFingerprint.of(cert.pgpPublicKeyRing), primaryKeyExpiration) OpenPgpFingerprint.of(cert), primaryKeyExpiration)
} }
var encryptionSubkeys = selector.selectEncryptionSubkeys(info.getEncryptionSubkeys(purpose)) var encryptionSubkeys = selector.selectEncryptionSubkeys(info.getEncryptionSubkeys(purpose))
@ -318,7 +318,7 @@ class EncryptionOptions(private val purpose: EncryptionPurpose) {
} }
if (encryptionSubkeys.isEmpty()) { if (encryptionSubkeys.isEmpty()) {
throw UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(cert.pgpPublicKeyRing)) throw UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(cert))
} }
for (subkey in encryptionSubkeys) { for (subkey in encryptionSubkeys) {

View file

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

View file

@ -9,6 +9,9 @@ import org.bouncycastle.bcpg.KeyIdentifier
import org.bouncycastle.openpgp.PGPKeyRing import org.bouncycastle.openpgp.PGPKeyRing
import org.bouncycastle.openpgp.PGPPublicKey import org.bouncycastle.openpgp.PGPPublicKey
import org.bouncycastle.openpgp.PGPSecretKey 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 import org.bouncycastle.util.encoders.Hex
/** Abstract super class of different version OpenPGP fingerprints. */ /** 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) @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 * 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 * fingerprint without whitespace is 64 characters long, it is either a v5 or v6