1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-12-25 12:27:58 +01:00

Flatten class hierarchy

This commit is contained in:
Paul Schaub 2024-02-18 11:23:40 +01:00
parent db01d566fd
commit 900860c1fa
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -49,8 +49,8 @@ class OpenPgpKeyGenerator internal constructor() {
policy: Policy = PGPainless.getPolicy(), policy: Policy = PGPainless.getPolicy(),
creationTime: Date = Date(), creationTime: Date = Date(),
preferences: AlgorithmSuite = policy.keyGenerationAlgorithmSuite preferences: AlgorithmSuite = policy.keyGenerationAlgorithmSuite
): OpinionatedDefinePrimaryKey.V4 { ): OpinionatedDefinePrimaryKeyV4 {
return OpinionatedDefinePrimaryKey.V4(policy, creationTime, preferences) return OpinionatedDefinePrimaryKeyV4(policy, creationTime, preferences)
} }
/** /**
@ -64,8 +64,8 @@ class OpenPgpKeyGenerator internal constructor() {
policy: Policy = PGPainless.getPolicy(), policy: Policy = PGPainless.getPolicy(),
creationTime: Date = Date(), creationTime: Date = Date(),
preferences: AlgorithmSuite = AlgorithmSuite.v6AlgorithmSuite preferences: AlgorithmSuite = AlgorithmSuite.v6AlgorithmSuite
): OpinionatedDefinePrimaryKey.V6 { ): OpinionatedDefinePrimaryKeyV6 {
return OpinionatedDefinePrimaryKey.V6(policy, creationTime, preferences) return OpinionatedDefinePrimaryKeyV6(policy, creationTime, preferences)
} }
} }
} }
@ -153,31 +153,29 @@ internal constructor(
} }
as B as B
} }
}
/** /**
* Implementation of an [OpinionatedDefinePrimaryKey] for OpenPGP v4 keys. * Implementation of an [OpinionatedDefinePrimaryKey] for OpenPGP v4 keys.
* *
* @param policy policy for algorithm compliance and fallbacks * @param policy policy for algorithm compliance and fallbacks
* @param creationTime creation time of the primary key * @param creationTime creation time of the primary key
*/ */
class V4 internal constructor(policy: Policy, creationTime: Date, preferences: AlgorithmSuite) : class OpinionatedDefinePrimaryKeyV4
OpinionatedDefinePrimaryKey<OpinionatedDefineSubkeys.V4, UnopinionatedDefineSubkeys.V4>( internal constructor(policy: Policy, creationTime: Date, preferences: AlgorithmSuite) :
policy, OpinionatedDefinePrimaryKey<OpinionatedDefineSubkeysV4, UnopinionatedDefineSubkeysV4>(
creationTime, policy, creationTime, preferences, UnopinionatedDefinePrimaryKeyV4(policy, creationTime)) {
preferences,
UnopinionatedDefinePrimaryKey.V4(policy, creationTime)) {
override fun unopinionated(): UnopinionatedDefinePrimaryKey.V4 = override fun unopinionated(): UnopinionatedDefinePrimaryKeyV4 =
unopinionated as UnopinionatedDefinePrimaryKey.V4 unopinionated as UnopinionatedDefinePrimaryKeyV4
override fun setPrimaryKey( override fun setPrimaryKey(
type: KeyType, type: KeyType,
creationTime: Date, creationTime: Date,
applyToPrimaryKey: (ApplyToPrimaryKey.() -> PGPKeyPair)? applyToPrimaryKey: (ApplyToPrimaryKey.() -> PGPKeyPair)?
): OpinionatedDefineSubkeys.V4 { ): OpinionatedDefineSubkeysV4 {
require( require(policy.publicKeyAlgorithmPolicy.isAcceptable(type.algorithm, type.bitStrength)) {
policy.publicKeyAlgorithmPolicy.isAcceptable(type.algorithm, type.bitStrength)) {
"Public Key algorithm ${type.algorithm} with ${type.bitStrength} is too weak" + "Public Key algorithm ${type.algorithm} with ${type.bitStrength} is too weak" +
" for the current public key algorithm policy." " for the current public key algorithm policy."
} }
@ -190,39 +188,36 @@ internal constructor(
} }
val unopinionatedSubkeys = unopinionated().setPrimaryKey(type, creationTime, applier) val unopinionatedSubkeys = unopinionated().setPrimaryKey(type, creationTime, applier)
return OpinionatedDefineSubkeys.V4( return OpinionatedDefineSubkeysV4(
unopinionatedSubkeys.primaryKey, policy, creationTime, unopinionatedSubkeys) unopinionatedSubkeys.primaryKey, policy, creationTime, unopinionatedSubkeys)
} }
} }
/** /**
* Implementation of an [OpinionatedDefinePrimaryKey] for OpenPGP v6 keys. * Implementation of an [OpinionatedDefinePrimaryKey] for OpenPGP v6 keys.
* *
* @param policy policy for algorithm compliance and fallbacks * @param policy policy for algorithm compliance and fallbacks
* @param creationTime creation time of the primary key * @param creationTime creation time of the primary key
*/ */
class V6 internal constructor(policy: Policy, creationTime: Date, preferences: AlgorithmSuite) : class OpinionatedDefinePrimaryKeyV6
OpinionatedDefinePrimaryKey<OpinionatedDefineSubkeys.V6, UnopinionatedDefineSubkeys.V6>( internal constructor(policy: Policy, creationTime: Date, preferences: AlgorithmSuite) :
policy, OpinionatedDefinePrimaryKey<OpinionatedDefineSubkeysV6, UnopinionatedDefineSubkeysV6>(
creationTime, policy, creationTime, preferences, UnopinionatedDefinePrimaryKeyV6(policy, creationTime)) {
preferences,
UnopinionatedDefinePrimaryKey.V6(policy, creationTime)) {
override fun unopinionated(): UnopinionatedDefinePrimaryKey.V6 = override fun unopinionated(): UnopinionatedDefinePrimaryKeyV6 =
unopinionated as UnopinionatedDefinePrimaryKey.V6 unopinionated as UnopinionatedDefinePrimaryKeyV6
override fun setPrimaryKey( override fun setPrimaryKey(
type: KeyType, type: KeyType,
creationTime: Date, creationTime: Date,
applyToPrimaryKey: (ApplyToPrimaryKey.() -> PGPKeyPair)? applyToPrimaryKey: (ApplyToPrimaryKey.() -> PGPKeyPair)?
): OpinionatedDefineSubkeys.V6 { ): OpinionatedDefineSubkeysV6 {
val applier = applyToPrimaryKey ?: { addDirectKeySignature(preferencesSubpackets()) } val applier = applyToPrimaryKey ?: { addDirectKeySignature(preferencesSubpackets()) }
return OpinionatedDefineSubkeys.V6( return OpinionatedDefineSubkeysV6(
policy, policy,
creationTime, creationTime,
unopinionated.setPrimaryKey(type, creationTime, applier) unopinionated.setPrimaryKey(type, creationTime, applier)
as UnopinionatedDefineSubkeys.V6) as UnopinionatedDefineSubkeysV6)
}
} }
} }
@ -241,47 +236,47 @@ internal constructor(
override fun preferencesSubpackets(): SelfSignatureSubpackets.Callback = override fun preferencesSubpackets(): SelfSignatureSubpackets.Callback =
SelfSignatureSubpackets.nop() SelfSignatureSubpackets.nop()
}
/** /**
* Implementation of an [UnopinionatedDefinePrimaryKey] for OpenPGP v4 keys. * Implementation of an [UnopinionatedDefinePrimaryKey] for OpenPGP v4 keys.
* *
* @param creationTime creation time of the primary key * @param creationTime creation time of the primary key
*/ */
class V4 internal constructor(policy: Policy, creationTime: Date) : class UnopinionatedDefinePrimaryKeyV4 internal constructor(policy: Policy, creationTime: Date) :
UnopinionatedDefinePrimaryKey<UnopinionatedDefineSubkeys.V4>(policy, creationTime) { UnopinionatedDefinePrimaryKey<UnopinionatedDefineSubkeysV4>(policy, creationTime) {
override fun setPrimaryKey( override fun setPrimaryKey(
type: KeyType, type: KeyType,
creationTime: Date, creationTime: Date,
applyToPrimaryKey: (ApplyToPrimaryKey.() -> PGPKeyPair)? applyToPrimaryKey: (ApplyToPrimaryKey.() -> PGPKeyPair)?
): UnopinionatedDefineSubkeys.V4 { ): UnopinionatedDefineSubkeysV4 {
// generate primary key // generate primary key
var primaryKey = OpenPgpKeyPairGenerator.V4().generatePrimaryKey(type, creationTime) var primaryKey = OpenPgpKeyPairGenerator.V4().generatePrimaryKey(type, creationTime)
// add user-ids and direct-key signatures if requested // add user-ids and direct-key signatures if requested
if (applyToPrimaryKey != null) { if (applyToPrimaryKey != null) {
primaryKey = ApplyToPrimaryKey.V4(primaryKey, this).applyToPrimaryKey() primaryKey = ApplyToPrimaryKeyV4(primaryKey, this).applyToPrimaryKey()
} }
// return builder for adding subkeys // return builder for adding subkeys
return UnopinionatedDefineSubkeys.V4(primaryKey, policy, creationTime) return UnopinionatedDefineSubkeysV4(primaryKey, policy, creationTime)
}
} }
}
/** /**
* Implementation of an [UnopinionatedDefinePrimaryKey] for OpenPGP v6 keys. * Implementation of an [UnopinionatedDefinePrimaryKey] for OpenPGP v6 keys.
* *
* @param creationTime creation time of the primary key * @param creationTime creation time of the primary key
*/ */
class V6 internal constructor(policy: Policy, creationTime: Date) : class UnopinionatedDefinePrimaryKeyV6 internal constructor(policy: Policy, creationTime: Date) :
UnopinionatedDefinePrimaryKey<UnopinionatedDefineSubkeys.V6>(policy, creationTime) { UnopinionatedDefinePrimaryKey<UnopinionatedDefineSubkeysV6>(policy, creationTime) {
override fun setPrimaryKey( override fun setPrimaryKey(
type: KeyType, type: KeyType,
creationTime: Date, creationTime: Date,
applyToPrimaryKey: (ApplyToPrimaryKey.() -> PGPKeyPair)? applyToPrimaryKey: (ApplyToPrimaryKey.() -> PGPKeyPair)?
): UnopinionatedDefineSubkeys.V6 { ): UnopinionatedDefineSubkeysV6 {
return UnopinionatedDefineSubkeys.V6(policy, creationTime) return UnopinionatedDefineSubkeysV6(policy, creationTime)
}
} }
} }
@ -342,8 +337,9 @@ abstract class OpinionatedDefineSubkeys internal constructor(policy: Policy, cre
override fun build(protector: SecretKeyRingProtector): PGPSecretKeyRing = override fun build(protector: SecretKeyRingProtector): PGPSecretKeyRing =
unopinionated.build(protector) unopinionated.build(protector)
}
/** /**
* Implementation of an [OpinionatedDefineSubkeys] builder for version 4 OpenPGP keys. * Implementation of an [OpinionatedDefineSubkeys] builder for version 4 OpenPGP keys.
* *
* @param primaryKey version 4 OpenPGP primary key * @param primaryKey version 4 OpenPGP primary key
@ -351,20 +347,22 @@ abstract class OpinionatedDefineSubkeys internal constructor(policy: Policy, cre
* @param creationTime creation time of the OpenPGP key * @param creationTime creation time of the OpenPGP key
* @param unopinionated unopinionated variant of this builder * @param unopinionated unopinionated variant of this builder
*/ */
class V4 class OpinionatedDefineSubkeysV4
internal constructor( internal constructor(
primaryKey: PGPKeyPair, primaryKey: PGPKeyPair,
policy: Policy, policy: Policy,
creationTime: Date, creationTime: Date,
override val unopinionated: UnopinionatedDefineSubkeys.V4 = override val unopinionated: UnopinionatedDefineSubkeysV4 =
UnopinionatedDefineSubkeys.V4(primaryKey, policy, creationTime) UnopinionatedDefineSubkeysV4(primaryKey, policy, creationTime)
) : OpinionatedDefineSubkeys(policy, creationTime) { ) : OpinionatedDefineSubkeys(policy, creationTime) {
override fun addSubkey( override fun addSubkey(
type: KeyType, type: KeyType,
creationTime: Date, creationTime: Date,
applyToSubkey: (ApplyToSubkey.() -> PGPKeyPair)? applyToSubkey: (ApplyToSubkey.() -> PGPKeyPair)?
): V4 = apply { unopinionated.addSubkey(type, creationTime, applyToSubkey) } ): OpinionatedDefineSubkeysV4 = apply {
unopinionated.addSubkey(type, creationTime, applyToSubkey)
}
fun addSigningSubkey( fun addSigningSubkey(
type: KeyType, type: KeyType,
@ -389,28 +387,30 @@ abstract class OpinionatedDefineSubkeys internal constructor(policy: Policy, cre
}) })
} }
) = addSubkey(type, creationTime, applyToSubkey) ) = addSubkey(type, creationTime, applyToSubkey)
} }
class V6 class OpinionatedDefineSubkeysV6
internal constructor( internal constructor(
policy: Policy, policy: Policy,
creationTime: Date, creationTime: Date,
override val unopinionated: UnopinionatedDefineSubkeys.V6 = override val unopinionated: UnopinionatedDefineSubkeysV6 =
UnopinionatedDefineSubkeys.V6(policy, creationTime) UnopinionatedDefineSubkeysV6(policy, creationTime)
) : OpinionatedDefineSubkeys(policy, creationTime) { ) : OpinionatedDefineSubkeys(policy, creationTime) {
override fun addSubkey( override fun addSubkey(
type: KeyType, type: KeyType,
creationTime: Date, creationTime: Date,
applyToSubkey: (ApplyToSubkey.() -> PGPKeyPair)? applyToSubkey: (ApplyToSubkey.() -> PGPKeyPair)?
): V6 = apply { unopinionated.addSubkey(type, creationTime, applyToSubkey) } ): OpinionatedDefineSubkeysV6 = apply {
unopinionated.addSubkey(type, creationTime, applyToSubkey)
} }
} }
abstract class UnopinionatedDefineSubkeys internal constructor(policy: Policy, creationTime: Date) : abstract class UnopinionatedDefineSubkeys internal constructor(policy: Policy, creationTime: Date) :
DefineSubkeys<UnopinionatedDefineSubkeys>(policy, creationTime) { DefineSubkeys<UnopinionatedDefineSubkeys>(policy, creationTime)
class V4 internal constructor(val primaryKey: PGPKeyPair, policy: Policy, creationTime: Date) : class UnopinionatedDefineSubkeysV4
internal constructor(val primaryKey: PGPKeyPair, policy: Policy, creationTime: Date) :
UnopinionatedDefineSubkeys(policy, creationTime) { UnopinionatedDefineSubkeys(policy, creationTime) {
private val subkeys: MutableList<PGPKeyPair> = mutableListOf() private val subkeys: MutableList<PGPKeyPair> = mutableListOf()
@ -419,13 +419,13 @@ abstract class UnopinionatedDefineSubkeys internal constructor(policy: Policy, c
type: KeyType, type: KeyType,
creationTime: Date, creationTime: Date,
applyToSubkey: (ApplyToSubkey.() -> PGPKeyPair)? applyToSubkey: (ApplyToSubkey.() -> PGPKeyPair)?
): V4 = apply { ): UnopinionatedDefineSubkeysV4 = apply {
val subkey = OpenPgpKeyPairGenerator.V4().generateSubkey(type, creationTime) val subkey = OpenPgpKeyPairGenerator.V4().generateSubkey(type, creationTime)
subkeys.add( subkeys.add(
if (applyToSubkey == null) { if (applyToSubkey == null) {
subkey subkey
} else { } else {
ApplyToSubkey.V4(primaryKey, subkey, this).applyToSubkey() ApplyToSubkeyV4(primaryKey, subkey, this).applyToSubkey()
}) })
} }
@ -451,23 +451,22 @@ abstract class UnopinionatedDefineSubkeys internal constructor(policy: Policy, c
} }
}) })
} }
} }
class V6 internal constructor(policy: Policy, creationTime: Date) : class UnopinionatedDefineSubkeysV6 internal constructor(policy: Policy, creationTime: Date) :
UnopinionatedDefineSubkeys(policy, creationTime) { UnopinionatedDefineSubkeys(policy, creationTime) {
override fun addSubkey( override fun addSubkey(
type: KeyType, type: KeyType,
creationTime: Date, creationTime: Date,
applyToSubkey: (ApplyToSubkey.() -> PGPKeyPair)? applyToSubkey: (ApplyToSubkey.() -> PGPKeyPair)?
): V6 { ): UnopinionatedDefineSubkeysV6 {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
override fun build(protector: SecretKeyRingProtector): PGPSecretKeyRing { override fun build(protector: SecretKeyRingProtector): PGPSecretKeyRing {
TODO("Not yet implemented") TODO("Not yet implemented")
} }
}
} }
/** /**
@ -567,8 +566,9 @@ internal constructor(var keyPair: PGPKeyPair, val builder: DefinePrimaryKey<*>)
* @return modified key pair after this and [other] have been executed * @return modified key pair after this and [other] have been executed
*/ */
abstract fun then(other: (ApplyToPrimaryKey.() -> PGPKeyPair)?): PGPKeyPair abstract fun then(other: (ApplyToPrimaryKey.() -> PGPKeyPair)?): PGPKeyPair
}
class V4 internal constructor(keyPair: PGPKeyPair, builder: DefinePrimaryKey<*>) : class ApplyToPrimaryKeyV4 internal constructor(keyPair: PGPKeyPair, builder: DefinePrimaryKey<*>) :
ApplyToPrimaryKey(keyPair, builder) { ApplyToPrimaryKey(keyPair, builder) {
override fun addUserId( override fun addUserId(
@ -580,12 +580,7 @@ internal constructor(var keyPair: PGPKeyPair, val builder: DefinePrimaryKey<*>)
): PGPKeyPair { ): PGPKeyPair {
val sig = val sig =
buildCertificationFor( buildCertificationFor(
keyPair, keyPair, userId, certificationType, bindingTime, hashAlgorithm, subpacketsCallback)
userId,
certificationType,
bindingTime,
hashAlgorithm,
subpacketsCallback)
keyPair = keyPair.plusCertification(userId, sig) keyPair = keyPair.plusCertification(userId, sig)
return keyPair return keyPair
@ -621,16 +616,14 @@ internal constructor(var keyPair: PGPKeyPair, val builder: DefinePrimaryKey<*>)
keyPair, keyPair,
hashAlgorithm, hashAlgorithm,
subpacketsCallback.then( subpacketsCallback.then(
SelfSignatureSubpackets.applyHashed { SelfSignatureSubpackets.applyHashed { setSignatureCreationTime(bindingTime) }))
setSignatureCreationTime(bindingTime)
}))
keyPair = keyPair.plusCertification(sig) keyPair = keyPair.plusCertification(sig)
return keyPair return keyPair
} }
override fun then(other: (ApplyToPrimaryKey.() -> PGPKeyPair)?): PGPKeyPair { override fun then(other: (ApplyToPrimaryKey.() -> PGPKeyPair)?): PGPKeyPair {
if (other != null) { if (other != null) {
keyPair = V4(keyPair, builder).other() keyPair = ApplyToPrimaryKeyV4(keyPair, builder).other()
} }
return keyPair return keyPair
} }
@ -664,8 +657,8 @@ internal constructor(var keyPair: PGPKeyPair, val builder: DefinePrimaryKey<*>)
} }
/** /**
* Build a certification signature that binds the given [userAttribute] to the given primary * Build a certification signature that binds the given [userAttribute] to the given primary key
* key [pair]. * [pair].
* *
* @param pair primary key pair * @param pair primary key pair
* @param userAttribute UserAttribute to be bound * @param userAttribute UserAttribute to be bound
@ -704,14 +697,12 @@ internal constructor(var keyPair: PGPKeyPair, val builder: DefinePrimaryKey<*>)
hashAlgorithm: HashAlgorithm, hashAlgorithm: HashAlgorithm,
subpacketsCallback: SelfSignatureSubpackets.Callback subpacketsCallback: SelfSignatureSubpackets.Callback
): PGPSignature { ): PGPSignature {
val builder = val builder = DirectKeySelfSignatureBuilder(pair.privateKey, pair.publicKey, hashAlgorithm)
DirectKeySelfSignatureBuilder(pair.privateKey, pair.publicKey, hashAlgorithm)
builder.applyCallback(subpacketsCallback) builder.applyCallback(subpacketsCallback)
return builder.build() return builder.build()
} }
}
} }
/** /**
@ -742,16 +733,17 @@ internal constructor(
builder.policy.certificationSignatureHashAlgorithmPolicy.defaultHashAlgorithm, builder.policy.certificationSignatureHashAlgorithmPolicy.defaultHashAlgorithm,
bindingTime: Date = subkey.publicKey.creationTime bindingTime: Date = subkey.publicKey.creationTime
): PGPKeyPair ): PGPKeyPair
}
/** /**
* Implementation of the [ApplyToSubkey] function tailored to OpenPGP v4 keys. * Implementation of the [ApplyToSubkey] function tailored to OpenPGP v4 keys.
* *
* @param primaryKey primary key pair * @param primaryKey primary key pair
* @param subkey subkey pair * @param subkey subkey pair
* @param builder builder instance that generated the subkey * @param builder builder instance that generated the subkey
*/ */
class V4 class ApplyToSubkeyV4
internal constructor(primaryKey: PGPKeyPair, subkey: PGPKeyPair, builder: DefineSubkeys<*>) : internal constructor(primaryKey: PGPKeyPair, subkey: PGPKeyPair, builder: DefineSubkeys<*>) :
ApplyToSubkey(primaryKey, subkey, builder) { ApplyToSubkey(primaryKey, subkey, builder) {
override fun addBindingSignature( override fun addBindingSignature(
@ -806,7 +798,6 @@ internal constructor(
val signCapableFlags = listOf(KeyFlag.SIGN_DATA, KeyFlag.CERTIFY_OTHER) val signCapableFlags = listOf(KeyFlag.SIGN_DATA, KeyFlag.CERTIFY_OTHER)
return flags?.any { signCapableFlags.contains(it) } ?: false return flags?.any { signCapableFlags.contains(it) } ?: false
} }
}
} }
/** Templates for OpenPGP key generation. */ /** Templates for OpenPGP key generation. */