mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-25 04:17:59 +01:00
Flatten class hierarchy
This commit is contained in:
parent
db01d566fd
commit
900860c1fa
1 changed files with 391 additions and 400 deletions
|
@ -49,8 +49,8 @@ class OpenPgpKeyGenerator internal constructor() {
|
|||
policy: Policy = PGPainless.getPolicy(),
|
||||
creationTime: Date = Date(),
|
||||
preferences: AlgorithmSuite = policy.keyGenerationAlgorithmSuite
|
||||
): OpinionatedDefinePrimaryKey.V4 {
|
||||
return OpinionatedDefinePrimaryKey.V4(policy, creationTime, preferences)
|
||||
): OpinionatedDefinePrimaryKeyV4 {
|
||||
return OpinionatedDefinePrimaryKeyV4(policy, creationTime, preferences)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -64,8 +64,8 @@ class OpenPgpKeyGenerator internal constructor() {
|
|||
policy: Policy = PGPainless.getPolicy(),
|
||||
creationTime: Date = Date(),
|
||||
preferences: AlgorithmSuite = AlgorithmSuite.v6AlgorithmSuite
|
||||
): OpinionatedDefinePrimaryKey.V6 {
|
||||
return OpinionatedDefinePrimaryKey.V6(policy, creationTime, preferences)
|
||||
): OpinionatedDefinePrimaryKeyV6 {
|
||||
return OpinionatedDefinePrimaryKeyV6(policy, creationTime, preferences)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -153,6 +153,7 @@ internal constructor(
|
|||
}
|
||||
as B
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of an [OpinionatedDefinePrimaryKey] for OpenPGP v4 keys.
|
||||
|
@ -160,24 +161,21 @@ internal constructor(
|
|||
* @param policy policy for algorithm compliance and fallbacks
|
||||
* @param creationTime creation time of the primary key
|
||||
*/
|
||||
class V4 internal constructor(policy: Policy, creationTime: Date, preferences: AlgorithmSuite) :
|
||||
OpinionatedDefinePrimaryKey<OpinionatedDefineSubkeys.V4, UnopinionatedDefineSubkeys.V4>(
|
||||
policy,
|
||||
creationTime,
|
||||
preferences,
|
||||
UnopinionatedDefinePrimaryKey.V4(policy, creationTime)) {
|
||||
class OpinionatedDefinePrimaryKeyV4
|
||||
internal constructor(policy: Policy, creationTime: Date, preferences: AlgorithmSuite) :
|
||||
OpinionatedDefinePrimaryKey<OpinionatedDefineSubkeysV4, UnopinionatedDefineSubkeysV4>(
|
||||
policy, creationTime, preferences, UnopinionatedDefinePrimaryKeyV4(policy, creationTime)) {
|
||||
|
||||
override fun unopinionated(): UnopinionatedDefinePrimaryKey.V4 =
|
||||
unopinionated as UnopinionatedDefinePrimaryKey.V4
|
||||
override fun unopinionated(): UnopinionatedDefinePrimaryKeyV4 =
|
||||
unopinionated as UnopinionatedDefinePrimaryKeyV4
|
||||
|
||||
override fun setPrimaryKey(
|
||||
type: KeyType,
|
||||
creationTime: Date,
|
||||
applyToPrimaryKey: (ApplyToPrimaryKey.() -> PGPKeyPair)?
|
||||
): OpinionatedDefineSubkeys.V4 {
|
||||
): OpinionatedDefineSubkeysV4 {
|
||||
|
||||
require(
|
||||
policy.publicKeyAlgorithmPolicy.isAcceptable(type.algorithm, type.bitStrength)) {
|
||||
require(policy.publicKeyAlgorithmPolicy.isAcceptable(type.algorithm, type.bitStrength)) {
|
||||
"Public Key algorithm ${type.algorithm} with ${type.bitStrength} is too weak" +
|
||||
" for the current public key algorithm policy."
|
||||
}
|
||||
|
@ -190,7 +188,7 @@ internal constructor(
|
|||
}
|
||||
|
||||
val unopinionatedSubkeys = unopinionated().setPrimaryKey(type, creationTime, applier)
|
||||
return OpinionatedDefineSubkeys.V4(
|
||||
return OpinionatedDefineSubkeysV4(
|
||||
unopinionatedSubkeys.primaryKey, policy, creationTime, unopinionatedSubkeys)
|
||||
}
|
||||
}
|
||||
|
@ -201,28 +199,25 @@ internal constructor(
|
|||
* @param policy policy for algorithm compliance and fallbacks
|
||||
* @param creationTime creation time of the primary key
|
||||
*/
|
||||
class V6 internal constructor(policy: Policy, creationTime: Date, preferences: AlgorithmSuite) :
|
||||
OpinionatedDefinePrimaryKey<OpinionatedDefineSubkeys.V6, UnopinionatedDefineSubkeys.V6>(
|
||||
policy,
|
||||
creationTime,
|
||||
preferences,
|
||||
UnopinionatedDefinePrimaryKey.V6(policy, creationTime)) {
|
||||
class OpinionatedDefinePrimaryKeyV6
|
||||
internal constructor(policy: Policy, creationTime: Date, preferences: AlgorithmSuite) :
|
||||
OpinionatedDefinePrimaryKey<OpinionatedDefineSubkeysV6, UnopinionatedDefineSubkeysV6>(
|
||||
policy, creationTime, preferences, UnopinionatedDefinePrimaryKeyV6(policy, creationTime)) {
|
||||
|
||||
override fun unopinionated(): UnopinionatedDefinePrimaryKey.V6 =
|
||||
unopinionated as UnopinionatedDefinePrimaryKey.V6
|
||||
override fun unopinionated(): UnopinionatedDefinePrimaryKeyV6 =
|
||||
unopinionated as UnopinionatedDefinePrimaryKeyV6
|
||||
|
||||
override fun setPrimaryKey(
|
||||
type: KeyType,
|
||||
creationTime: Date,
|
||||
applyToPrimaryKey: (ApplyToPrimaryKey.() -> PGPKeyPair)?
|
||||
): OpinionatedDefineSubkeys.V6 {
|
||||
): OpinionatedDefineSubkeysV6 {
|
||||
val applier = applyToPrimaryKey ?: { addDirectKeySignature(preferencesSubpackets()) }
|
||||
return OpinionatedDefineSubkeys.V6(
|
||||
return OpinionatedDefineSubkeysV6(
|
||||
policy,
|
||||
creationTime,
|
||||
unopinionated.setPrimaryKey(type, creationTime, applier)
|
||||
as UnopinionatedDefineSubkeys.V6)
|
||||
}
|
||||
as UnopinionatedDefineSubkeysV6)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -241,30 +236,31 @@ internal constructor(
|
|||
|
||||
override fun preferencesSubpackets(): SelfSignatureSubpackets.Callback =
|
||||
SelfSignatureSubpackets.nop()
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of an [UnopinionatedDefinePrimaryKey] for OpenPGP v4 keys.
|
||||
*
|
||||
* @param creationTime creation time of the primary key
|
||||
*/
|
||||
class V4 internal constructor(policy: Policy, creationTime: Date) :
|
||||
UnopinionatedDefinePrimaryKey<UnopinionatedDefineSubkeys.V4>(policy, creationTime) {
|
||||
class UnopinionatedDefinePrimaryKeyV4 internal constructor(policy: Policy, creationTime: Date) :
|
||||
UnopinionatedDefinePrimaryKey<UnopinionatedDefineSubkeysV4>(policy, creationTime) {
|
||||
|
||||
override fun setPrimaryKey(
|
||||
type: KeyType,
|
||||
creationTime: Date,
|
||||
applyToPrimaryKey: (ApplyToPrimaryKey.() -> PGPKeyPair)?
|
||||
): UnopinionatedDefineSubkeys.V4 {
|
||||
): UnopinionatedDefineSubkeysV4 {
|
||||
// generate primary key
|
||||
var primaryKey = OpenPgpKeyPairGenerator.V4().generatePrimaryKey(type, creationTime)
|
||||
|
||||
// add user-ids and direct-key signatures if requested
|
||||
if (applyToPrimaryKey != null) {
|
||||
primaryKey = ApplyToPrimaryKey.V4(primaryKey, this).applyToPrimaryKey()
|
||||
primaryKey = ApplyToPrimaryKeyV4(primaryKey, this).applyToPrimaryKey()
|
||||
}
|
||||
|
||||
// return builder for adding subkeys
|
||||
return UnopinionatedDefineSubkeys.V4(primaryKey, policy, creationTime)
|
||||
return UnopinionatedDefineSubkeysV4(primaryKey, policy, creationTime)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -273,15 +269,14 @@ internal constructor(
|
|||
*
|
||||
* @param creationTime creation time of the primary key
|
||||
*/
|
||||
class V6 internal constructor(policy: Policy, creationTime: Date) :
|
||||
UnopinionatedDefinePrimaryKey<UnopinionatedDefineSubkeys.V6>(policy, creationTime) {
|
||||
class UnopinionatedDefinePrimaryKeyV6 internal constructor(policy: Policy, creationTime: Date) :
|
||||
UnopinionatedDefinePrimaryKey<UnopinionatedDefineSubkeysV6>(policy, creationTime) {
|
||||
override fun setPrimaryKey(
|
||||
type: KeyType,
|
||||
creationTime: Date,
|
||||
applyToPrimaryKey: (ApplyToPrimaryKey.() -> PGPKeyPair)?
|
||||
): UnopinionatedDefineSubkeys.V6 {
|
||||
return UnopinionatedDefineSubkeys.V6(policy, creationTime)
|
||||
}
|
||||
): UnopinionatedDefineSubkeysV6 {
|
||||
return UnopinionatedDefineSubkeysV6(policy, creationTime)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -342,6 +337,7 @@ abstract class OpinionatedDefineSubkeys internal constructor(policy: Policy, cre
|
|||
|
||||
override fun build(protector: SecretKeyRingProtector): PGPSecretKeyRing =
|
||||
unopinionated.build(protector)
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of an [OpinionatedDefineSubkeys] builder for version 4 OpenPGP keys.
|
||||
|
@ -351,20 +347,22 @@ abstract class OpinionatedDefineSubkeys internal constructor(policy: Policy, cre
|
|||
* @param creationTime creation time of the OpenPGP key
|
||||
* @param unopinionated unopinionated variant of this builder
|
||||
*/
|
||||
class V4
|
||||
class OpinionatedDefineSubkeysV4
|
||||
internal constructor(
|
||||
primaryKey: PGPKeyPair,
|
||||
policy: Policy,
|
||||
creationTime: Date,
|
||||
override val unopinionated: UnopinionatedDefineSubkeys.V4 =
|
||||
UnopinionatedDefineSubkeys.V4(primaryKey, policy, creationTime)
|
||||
override val unopinionated: UnopinionatedDefineSubkeysV4 =
|
||||
UnopinionatedDefineSubkeysV4(primaryKey, policy, creationTime)
|
||||
) : OpinionatedDefineSubkeys(policy, creationTime) {
|
||||
|
||||
override fun addSubkey(
|
||||
type: KeyType,
|
||||
creationTime: Date,
|
||||
applyToSubkey: (ApplyToSubkey.() -> PGPKeyPair)?
|
||||
): V4 = apply { unopinionated.addSubkey(type, creationTime, applyToSubkey) }
|
||||
): OpinionatedDefineSubkeysV4 = apply {
|
||||
unopinionated.addSubkey(type, creationTime, applyToSubkey)
|
||||
}
|
||||
|
||||
fun addSigningSubkey(
|
||||
type: KeyType,
|
||||
|
@ -391,26 +389,28 @@ abstract class OpinionatedDefineSubkeys internal constructor(policy: Policy, cre
|
|||
) = addSubkey(type, creationTime, applyToSubkey)
|
||||
}
|
||||
|
||||
class V6
|
||||
class OpinionatedDefineSubkeysV6
|
||||
internal constructor(
|
||||
policy: Policy,
|
||||
creationTime: Date,
|
||||
override val unopinionated: UnopinionatedDefineSubkeys.V6 =
|
||||
UnopinionatedDefineSubkeys.V6(policy, creationTime)
|
||||
override val unopinionated: UnopinionatedDefineSubkeysV6 =
|
||||
UnopinionatedDefineSubkeysV6(policy, creationTime)
|
||||
) : OpinionatedDefineSubkeys(policy, creationTime) {
|
||||
|
||||
override fun addSubkey(
|
||||
type: KeyType,
|
||||
creationTime: Date,
|
||||
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) :
|
||||
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) {
|
||||
|
||||
private val subkeys: MutableList<PGPKeyPair> = mutableListOf()
|
||||
|
@ -419,13 +419,13 @@ abstract class UnopinionatedDefineSubkeys internal constructor(policy: Policy, c
|
|||
type: KeyType,
|
||||
creationTime: Date,
|
||||
applyToSubkey: (ApplyToSubkey.() -> PGPKeyPair)?
|
||||
): V4 = apply {
|
||||
): UnopinionatedDefineSubkeysV4 = apply {
|
||||
val subkey = OpenPgpKeyPairGenerator.V4().generateSubkey(type, creationTime)
|
||||
subkeys.add(
|
||||
if (applyToSubkey == null) {
|
||||
subkey
|
||||
} else {
|
||||
ApplyToSubkey.V4(primaryKey, subkey, this).applyToSubkey()
|
||||
ApplyToSubkeyV4(primaryKey, subkey, this).applyToSubkey()
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -453,14 +453,14 @@ 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) {
|
||||
|
||||
override fun addSubkey(
|
||||
type: KeyType,
|
||||
creationTime: Date,
|
||||
applyToSubkey: (ApplyToSubkey.() -> PGPKeyPair)?
|
||||
): V6 {
|
||||
): UnopinionatedDefineSubkeysV6 {
|
||||
TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
|
@ -468,7 +468,6 @@ abstract class UnopinionatedDefineSubkeys internal constructor(policy: Policy, c
|
|||
TODO("Not yet implemented")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function that can be applied to the primary key.
|
||||
|
@ -567,8 +566,9 @@ internal constructor(var keyPair: PGPKeyPair, val builder: DefinePrimaryKey<*>)
|
|||
* @return modified key pair after this and [other] have been executed
|
||||
*/
|
||||
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) {
|
||||
|
||||
override fun addUserId(
|
||||
|
@ -580,12 +580,7 @@ internal constructor(var keyPair: PGPKeyPair, val builder: DefinePrimaryKey<*>)
|
|||
): PGPKeyPair {
|
||||
val sig =
|
||||
buildCertificationFor(
|
||||
keyPair,
|
||||
userId,
|
||||
certificationType,
|
||||
bindingTime,
|
||||
hashAlgorithm,
|
||||
subpacketsCallback)
|
||||
keyPair, userId, certificationType, bindingTime, hashAlgorithm, subpacketsCallback)
|
||||
|
||||
keyPair = keyPair.plusCertification(userId, sig)
|
||||
return keyPair
|
||||
|
@ -621,16 +616,14 @@ internal constructor(var keyPair: PGPKeyPair, val builder: DefinePrimaryKey<*>)
|
|||
keyPair,
|
||||
hashAlgorithm,
|
||||
subpacketsCallback.then(
|
||||
SelfSignatureSubpackets.applyHashed {
|
||||
setSignatureCreationTime(bindingTime)
|
||||
}))
|
||||
SelfSignatureSubpackets.applyHashed { setSignatureCreationTime(bindingTime) }))
|
||||
keyPair = keyPair.plusCertification(sig)
|
||||
return keyPair
|
||||
}
|
||||
|
||||
override fun then(other: (ApplyToPrimaryKey.() -> PGPKeyPair)?): PGPKeyPair {
|
||||
if (other != null) {
|
||||
keyPair = V4(keyPair, builder).other()
|
||||
keyPair = ApplyToPrimaryKeyV4(keyPair, builder).other()
|
||||
}
|
||||
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
|
||||
* key [pair].
|
||||
* Build a certification signature that binds the given [userAttribute] to the given primary key
|
||||
* [pair].
|
||||
*
|
||||
* @param pair primary key pair
|
||||
* @param userAttribute UserAttribute to be bound
|
||||
|
@ -704,15 +697,13 @@ internal constructor(var keyPair: PGPKeyPair, val builder: DefinePrimaryKey<*>)
|
|||
hashAlgorithm: HashAlgorithm,
|
||||
subpacketsCallback: SelfSignatureSubpackets.Callback
|
||||
): PGPSignature {
|
||||
val builder =
|
||||
DirectKeySelfSignatureBuilder(pair.privateKey, pair.publicKey, hashAlgorithm)
|
||||
val builder = DirectKeySelfSignatureBuilder(pair.privateKey, pair.publicKey, hashAlgorithm)
|
||||
|
||||
builder.applyCallback(subpacketsCallback)
|
||||
|
||||
return builder.build()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function that can be applied to subkeys.
|
||||
|
@ -742,6 +733,7 @@ internal constructor(
|
|||
builder.policy.certificationSignatureHashAlgorithmPolicy.defaultHashAlgorithm,
|
||||
bindingTime: Date = subkey.publicKey.creationTime
|
||||
): PGPKeyPair
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of the [ApplyToSubkey] function tailored to OpenPGP v4 keys.
|
||||
|
@ -750,7 +742,7 @@ internal constructor(
|
|||
* @param subkey subkey pair
|
||||
* @param builder builder instance that generated the subkey
|
||||
*/
|
||||
class V4
|
||||
class ApplyToSubkeyV4
|
||||
internal constructor(primaryKey: PGPKeyPair, subkey: PGPKeyPair, builder: DefineSubkeys<*>) :
|
||||
ApplyToSubkey(primaryKey, subkey, builder) {
|
||||
|
||||
|
@ -807,7 +799,6 @@ internal constructor(
|
|||
return flags?.any { signCapableFlags.contains(it) } ?: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Templates for OpenPGP key generation. */
|
||||
class OpenPgpKeyTemplates private constructor() {
|
||||
|
|
Loading…
Reference in a new issue