mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-12-11 06:41:09 +01:00
Improve KeyExceptions
This commit is contained in:
parent
ab1670598d
commit
a30ba00249
4 changed files with 99 additions and 48 deletions
|
|
@ -17,7 +17,6 @@ import org.pgpainless.bouncycastle.extensions.toOpenPGPCertificate
|
|||
import org.pgpainless.encryption_signing.EncryptionOptions.EncryptionKeySelector
|
||||
import org.pgpainless.exception.KeyException.*
|
||||
import org.pgpainless.implementation.ImplementationFactory
|
||||
import org.pgpainless.key.OpenPgpFingerprint
|
||||
import org.pgpainless.key.SubkeyIdentifier
|
||||
import org.pgpainless.key.info.KeyAccessor
|
||||
import org.pgpainless.key.info.KeyRingInfo
|
||||
|
|
@ -197,7 +196,7 @@ class EncryptionOptions(private val purpose: EncryptionPurpose) {
|
|||
encryptionKeySelector.selectEncryptionSubkeys(
|
||||
info.getEncryptionSubkeys(userId, purpose))
|
||||
if (subkeys.isEmpty()) {
|
||||
throw UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(cert))
|
||||
throw UnacceptableEncryptionKeyException(cert)
|
||||
}
|
||||
|
||||
for (subkey in subkeys) {
|
||||
|
|
@ -295,13 +294,11 @@ class EncryptionOptions(private val purpose: EncryptionPurpose) {
|
|||
try {
|
||||
info.primaryKeyExpirationDate
|
||||
} catch (e: NoSuchElementException) {
|
||||
throw UnacceptableSelfSignatureException(
|
||||
OpenPgpFingerprint.of(cert))
|
||||
throw UnacceptableSelfSignatureException(cert)
|
||||
}
|
||||
|
||||
if (primaryKeyExpiration != null && primaryKeyExpiration < evaluationDate) {
|
||||
throw ExpiredKeyException(
|
||||
OpenPgpFingerprint.of(cert), primaryKeyExpiration)
|
||||
throw ExpiredKeyException(cert, primaryKeyExpiration)
|
||||
}
|
||||
|
||||
var encryptionSubkeys = selector.selectEncryptionSubkeys(info.getEncryptionSubkeys(purpose))
|
||||
|
|
@ -318,7 +315,7 @@ class EncryptionOptions(private val purpose: EncryptionPurpose) {
|
|||
}
|
||||
|
||||
if (encryptionSubkeys.isEmpty()) {
|
||||
throw UnacceptableEncryptionKeyException(OpenPgpFingerprint.of(cert))
|
||||
throw UnacceptableEncryptionKeyException(cert)
|
||||
}
|
||||
|
||||
for (subkey in encryptionSubkeys) {
|
||||
|
|
|
|||
|
|
@ -157,14 +157,13 @@ class SigningOptions {
|
|||
|
||||
val signingPubKeys = keyRingInfo.signingSubkeys
|
||||
if (signingPubKeys.isEmpty()) {
|
||||
throw UnacceptableSigningKeyException(of(signingKey))
|
||||
throw UnacceptableSigningKeyException(signingKey)
|
||||
}
|
||||
|
||||
for (signingPubKey in signingPubKeys) {
|
||||
val signingSecKey: OpenPGPSecretKey =
|
||||
signingKey.getSecretKey(signingPubKey)
|
||||
?: throw MissingSecretKeyException(
|
||||
of(signingKey), signingPubKey.keyIdentifier.keyId)
|
||||
?: throw MissingSecretKeyException(signingPubKey)
|
||||
val signingPrivKey: OpenPGPPrivateKey =
|
||||
unlockSecretKey(signingSecKey, signingKeyProtector)
|
||||
val hashAlgorithms =
|
||||
|
|
@ -220,12 +219,11 @@ class SigningOptions {
|
|||
val keyRingInfo = inspectKeyRing(openPGPKey, evaluationDate)
|
||||
val signingPubKeys = keyRingInfo.signingSubkeys
|
||||
if (signingPubKeys.isEmpty()) {
|
||||
throw UnacceptableSigningKeyException(of(openPGPKey))
|
||||
throw UnacceptableSigningKeyException(openPGPKey)
|
||||
}
|
||||
|
||||
if (!signingPubKeys.any { it.keyIdentifier.matches(signingKey.keyIdentifier) }) {
|
||||
throw MissingSecretKeyException(
|
||||
of(openPGPKey), signingKey.keyIdentifier.keyId)
|
||||
throw MissingSecretKeyException(signingKey)
|
||||
}
|
||||
|
||||
val signingPrivKey = unlockSecretKey(signingKey, signingKeyProtector)
|
||||
|
|
@ -258,13 +256,16 @@ class SigningOptions {
|
|||
keyId: Long,
|
||||
signatureType: DocumentSignatureType = DocumentSignatureType.BINARY_DOCUMENT,
|
||||
subpacketsCallback: Callback? = null
|
||||
) =
|
||||
addInlineSignature(
|
||||
): SigningOptions {
|
||||
val key = signingKey.toOpenPGPKey()
|
||||
val subkeyIdentifier = KeyIdentifier(keyId)
|
||||
return addInlineSignature(
|
||||
signingKeyProtector,
|
||||
signingKey.toOpenPGPKey().getSecretKey(KeyIdentifier(keyId))
|
||||
?: throw MissingSecretKeyException(of(signingKey), keyId),
|
||||
key.getSecretKey(subkeyIdentifier)
|
||||
?: throw MissingSecretKeyException(of(signingKey), subkeyIdentifier),
|
||||
signatureType,
|
||||
subpacketsCallback)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add detached signatures with all key rings from the provided secret key ring collection.
|
||||
|
|
@ -332,14 +333,13 @@ class SigningOptions {
|
|||
|
||||
val signingPubKeys = keyRingInfo.signingSubkeys
|
||||
if (signingPubKeys.isEmpty()) {
|
||||
throw UnacceptableSigningKeyException(of(signingKey))
|
||||
throw UnacceptableSigningKeyException(signingKey)
|
||||
}
|
||||
|
||||
for (signingPubKey in signingPubKeys) {
|
||||
val signingSecKey: OpenPGPSecretKey =
|
||||
signingKey.getSecretKey(signingPubKey.keyIdentifier)
|
||||
?: throw MissingSecretKeyException(
|
||||
of(signingKey), signingPubKey.keyIdentifier.keyId)
|
||||
?: throw MissingSecretKeyException(signingPubKey)
|
||||
addDetachedSignature(
|
||||
signingKeyProtector, signingSecKey, userId, signatureType, subpacketCallback)
|
||||
}
|
||||
|
|
@ -421,14 +421,17 @@ class SigningOptions {
|
|||
keyId: Long,
|
||||
signatureType: DocumentSignatureType = DocumentSignatureType.BINARY_DOCUMENT,
|
||||
subpacketsCallback: Callback? = null
|
||||
) =
|
||||
addDetachedSignature(
|
||||
): SigningOptions {
|
||||
val key = signingKey.toOpenPGPKey()
|
||||
val signingKeyIdentifier = KeyIdentifier(keyId)
|
||||
return addDetachedSignature(
|
||||
signingKeyProtector,
|
||||
signingKey.toOpenPGPKey().getSecretKey(KeyIdentifier(keyId))
|
||||
?: throw MissingSecretKeyException(of(signingKey), keyId),
|
||||
key.getSecretKey(signingKeyIdentifier)
|
||||
?: throw MissingSecretKeyException(of(key), signingKeyIdentifier),
|
||||
null,
|
||||
signatureType,
|
||||
subpacketsCallback)
|
||||
}
|
||||
|
||||
private fun addSigningMethod(
|
||||
signingKey: OpenPGPPrivateKey,
|
||||
|
|
@ -443,10 +446,7 @@ class SigningOptions {
|
|||
if (!getPolicy().publicKeyAlgorithmPolicy.isAcceptable(publicKeyAlgorithm, bitStrength)) {
|
||||
throw UnacceptableSigningKeyException(
|
||||
PublicKeyAlgorithmPolicyException(
|
||||
of(signingKey),
|
||||
signingSecretKey.keyID,
|
||||
publicKeyAlgorithm,
|
||||
bitStrength))
|
||||
signingKey.secretKey, publicKeyAlgorithm, bitStrength))
|
||||
}
|
||||
|
||||
val generator: PGPSignatureGenerator =
|
||||
|
|
|
|||
|
|
@ -132,17 +132,13 @@ 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].
|
||||
*/
|
||||
/** 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)
|
||||
/** 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)
|
||||
@JvmStatic fun of(key: OpenPGPPrivateKey): OpenPgpFingerprint = of(key.secretKey)
|
||||
|
||||
/**
|
||||
* Try to parse an [OpenPgpFingerprint] from the given fingerprint string. If the trimmed
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue