Rename some classes

This commit is contained in:
Paul Schaub 2024-02-02 17:56:35 +01:00
parent 05c203177e
commit d650ae7371
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
2 changed files with 46 additions and 14 deletions

View File

@ -35,6 +35,9 @@ open class GenerateOpenPgpKey(
private val preferences: AlgorithmSuite = policy.keyGenerationAlgorithmSuite private val preferences: AlgorithmSuite = policy.keyGenerationAlgorithmSuite
) { ) {
/**
* Builder for OpenPGP secret keys.
*/
abstract class OpenPgpKeyBuilder( abstract class OpenPgpKeyBuilder(
protected val policy: Policy, protected val policy: Policy,
protected val referenceTime: Date, protected val referenceTime: Date,
@ -97,8 +100,8 @@ open class GenerateOpenPgpKey(
} }
private val primaryKey = private val primaryKey =
BaseOpenPgpKeyBuilder.BaseV4PrimaryKeyBuilder(primaryKeyType, referenceTime, policy) OpenPgpComponentKeyBuilder.V4PrimaryKeyBuilder(primaryKeyType, referenceTime, policy)
private val subkeys = mutableListOf<BaseOpenPgpKeyBuilder.BaseV4SubkeyBuilder>() private val subkeys = mutableListOf<OpenPgpComponentKeyBuilder.V4SubkeyBuilder>()
private val preferencesCallback = private val preferencesCallback =
SelfSignatureSubpackets.applyHashed { SelfSignatureSubpackets.applyHashed {
@ -183,7 +186,7 @@ open class GenerateOpenPgpKey(
subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop() subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop()
) = ) =
addSubkey( addSubkey(
BaseOpenPgpKeyBuilder.BaseV4SubkeyBuilder( OpenPgpComponentKeyBuilder.V4SubkeyBuilder(
keyType, creationTime, policy, primaryKey), keyType, creationTime, policy, primaryKey),
SelfSignatureSubpackets.applyHashed { SelfSignatureSubpackets.applyHashed {
setSignatureCreationTime(bindingTime) setSignatureCreationTime(bindingTime)
@ -192,13 +195,23 @@ open class GenerateOpenPgpKey(
.then(subpacketsCallback)) .then(subpacketsCallback))
fun addSubkey( fun addSubkey(
subkeyBuilder: BaseOpenPgpKeyBuilder.BaseV4SubkeyBuilder, subkeyBuilder: OpenPgpComponentKeyBuilder.V4SubkeyBuilder,
subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop() subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop()
) = apply { ) = apply {
sanitizePublicKeyAlgorithms(subkeyBuilder.type, policy) sanitizePublicKeyAlgorithms(subkeyBuilder.type, policy)
subkeys.add(subkeyBuilder.bindingSignature(subpacketsCallback = subpacketsCallback)) subkeys.add(subkeyBuilder.bindingSignature(subpacketsCallback = subpacketsCallback))
} }
/**
* Add a new subkey to be used for encryption.
* The binding signature will mark the key as encryption-capable using both
* [KeyFlag.ENCRYPT_COMMS] and [KeyFlag.ENCRYPT_STORAGE].
*
* @param keyType type of the encryption subkey
* @param creationTime time of creation of the subkey
* @param bindingTime creation time of the binding signature
* @return builder
*/
fun addEncryptionSubkey( fun addEncryptionSubkey(
keyType: KeyType, keyType: KeyType,
creationTime: Date = referenceTime, creationTime: Date = referenceTime,
@ -210,12 +223,31 @@ open class GenerateOpenPgpKey(
bindingTime, bindingTime,
listOf(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS)) listOf(KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS))
/**
* Add a new subkey to be used for creating data signatures.
* The binding signature will mark the key as signing-capable using [KeyFlag.SIGN_DATA].
*
* @param keyType type of the signing subkey
* @param creationTime time of creation of the subkey
* @param bindingTime creation time of the binding signature
* @return builder
*/
fun addSigningSubkey( fun addSigningSubkey(
keyType: KeyType, keyType: KeyType,
creationTime: Date = referenceTime, creationTime: Date = referenceTime,
bindingTime: Date = creationTime bindingTime: Date = creationTime
) = addSubkey(keyType, creationTime, bindingTime, listOf(KeyFlag.SIGN_DATA)) ) = addSubkey(keyType, creationTime, bindingTime, listOf(KeyFlag.SIGN_DATA))
/**
* Build the finished OpenPGP key.
* By default, the key will not be protected using passphrases.
* To set a passphrase, you can provide [SecretKeyRingProtector.unlockAnyKeyWith] with
* a passphrase of your choice.
*
* @param protector protector to secure the secret keys using passphrases.
* Defaults to [SecretKeyRingProtector.unprotectedKeys].
* @return OpenPGP Secret Key
*/
fun build( fun build(
protector: SecretKeyRingProtector = SecretKeyRingProtector.unprotectedKeys() protector: SecretKeyRingProtector = SecretKeyRingProtector.unprotectedKeys()
): PGPSecretKeyRing { ): PGPSecretKeyRing {
@ -233,7 +265,7 @@ open class GenerateOpenPgpKey(
} }
private fun toSecretKey( private fun toSecretKey(
key: BaseOpenPgpKeyBuilder.BaseV4KeyBuilder<*>, key: OpenPgpComponentKeyBuilder.V4ComponentKeyBuilder<*>,
isPrimaryKey: Boolean, isPrimaryKey: Boolean,
encryptor: PBESecretKeyEncryptor? encryptor: PBESecretKeyEncryptor?
): PGPSecretKey { ): PGPSecretKey {

View File

@ -26,9 +26,9 @@ import org.pgpainless.signature.builder.SelfSignatureBuilder
import org.pgpainless.signature.builder.SubkeyBindingSignatureBuilder import org.pgpainless.signature.builder.SubkeyBindingSignatureBuilder
import org.pgpainless.signature.subpackets.SelfSignatureSubpackets import org.pgpainless.signature.subpackets.SelfSignatureSubpackets
class BaseOpenPgpKeyBuilder { class OpenPgpComponentKeyBuilder {
abstract class BaseV4KeyBuilder<T : BaseV4KeyBuilder<T>>( abstract class V4ComponentKeyBuilder<T : V4ComponentKeyBuilder<T>>(
val type: KeyType, val type: KeyType,
val creationTime: Date, val creationTime: Date,
val certificateCreationTime: Date = Date(), val certificateCreationTime: Date = Date(),
@ -40,9 +40,9 @@ class BaseOpenPgpKeyBuilder {
fun subkey( fun subkey(
type: KeyType, type: KeyType,
creationTime: Date = certificateCreationTime creationTime: Date = certificateCreationTime
): BaseV4SubkeyBuilder = BaseV4SubkeyBuilder(type, creationTime, policy, primaryKey()) ): V4SubkeyBuilder = V4SubkeyBuilder(type, creationTime, policy, primaryKey())
internal abstract fun primaryKey(): BaseV4PrimaryKeyBuilder internal abstract fun primaryKey(): V4PrimaryKeyBuilder
// Note: The result is a *primary* key pair, so subkeys need adjustment (toPrimaryOrSubkey) // Note: The result is a *primary* key pair, so subkeys need adjustment (toPrimaryOrSubkey)
private fun generateKeyPair(): PGPKeyPair { private fun generateKeyPair(): PGPKeyPair {
@ -65,8 +65,8 @@ class BaseOpenPgpKeyBuilder {
protected abstract fun toPrimaryOrSubkey(keyPair: PGPKeyPair): PGPKeyPair protected abstract fun toPrimaryOrSubkey(keyPair: PGPKeyPair): PGPKeyPair
} }
class BaseV4PrimaryKeyBuilder(type: KeyType, creationTime: Date, policy: Policy) : class V4PrimaryKeyBuilder(type: KeyType, creationTime: Date, policy: Policy) :
BaseV4KeyBuilder<BaseV4PrimaryKeyBuilder>(type, creationTime, policy = policy) { V4ComponentKeyBuilder<V4PrimaryKeyBuilder>(type, creationTime, policy = policy) {
fun userId( fun userId(
userId: CharSequence, userId: CharSequence,
@ -179,12 +179,12 @@ class BaseOpenPgpKeyBuilder {
override fun primaryKey() = this override fun primaryKey() = this
} }
class BaseV4SubkeyBuilder( class V4SubkeyBuilder(
type: KeyType, type: KeyType,
creationTime: Date, creationTime: Date,
policy: Policy, policy: Policy,
private val primaryKeyBuilder: BaseV4PrimaryKeyBuilder private val primaryKeyBuilder: V4PrimaryKeyBuilder
) : BaseV4KeyBuilder<BaseV4SubkeyBuilder>(type, creationTime, policy = policy) { ) : V4ComponentKeyBuilder<V4SubkeyBuilder>(type, creationTime, policy = policy) {
fun bindingSignature( fun bindingSignature(
bindingTime: Date = creationTime, bindingTime: Date = creationTime,