Improve readability of primaryUserId adjustment

This commit is contained in:
Paul Schaub 2024-02-03 13:36:58 +01:00
parent 2f7266d771
commit 9785582bd2
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
2 changed files with 23 additions and 17 deletions

View File

@ -147,26 +147,30 @@ open class GenerateOpenPgpKey(
userId: CharSequence, userId: CharSequence,
subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop() subpacketsCallback: SelfSignatureSubpackets.Callback = SelfSignatureSubpackets.nop()
) = apply { ) = apply {
val hasPrimaryUID =
primaryKey.pair.publicKey.userIDs.asSequence().any { uid ->
primaryKey.pair.publicKey.getSignaturesForID(uid).asSequence().any {
it.hashedSubPackets.isPrimaryUserID
}
}
val setPrimaryUID =
SelfSignatureSubpackets.applyHashed {
if (hasPrimaryUID) {
setPrimaryUserId(null)
} else {
setPrimaryUserId()
}
}
primaryKey.userId( primaryKey.userId(
userId, userId,
subpacketsCallback = subpacketsCallback =
preferencesCallback.then(setPrimaryUID).then(subpacketsCallback)) preferencesCallback.then(adjustPrimaryUserId()).then(subpacketsCallback))
} }
private fun adjustPrimaryUserId() =
SelfSignatureSubpackets.applyHashed {
if (primaryKeyHasPrimaryUserId()) {
setPrimaryUserId(null)
} else {
setPrimaryUserId()
}
}
private fun primaryKeyHasPrimaryUserId() =
primaryKey.pair.publicKey.let { pk ->
pk.userIDs.asSequence().any { uid ->
pk.getSignaturesForID(uid).asSequence().any {
it.hashedSubPackets.isPrimaryUserID
}
}
}
/** /**
* Add a user-attribute to the key. The subpackets of the binding signature are * Add a user-attribute to the key. The subpackets of the binding signature are
* prepopulated, setting algorithm preferences and features. However, the subpackets can * prepopulated, setting algorithm preferences and features. However, the subpackets can
@ -302,7 +306,8 @@ open class GenerateOpenPgpKey(
return PGPSecretKeyRing( return PGPSecretKeyRing(
mutableListOf( mutableListOf(
toSecretKey(primaryKey, true, protector.getEncryptor(primaryKey.pair.keyID))) toSecretKey(
primaryKey, true, protector.getEncryptor(primaryKey.pair.keyID)))
.plus( .plus(
subkeys.map { subkeys.map {
toSecretKey(it, false, protector.getEncryptor(it.pair.keyID)) toSecretKey(it, false, protector.getEncryptor(it.pair.keyID))

View File

@ -215,7 +215,8 @@ class OpenPgpComponentKeyBuilder {
// Create back-sig // Create back-sig
val backSigBuilder = val backSigBuilder =
PrimaryKeyBindingSignatureBuilder(pair.privateKey, pair.publicKey, hashAlgorithm) PrimaryKeyBindingSignatureBuilder(
pair.privateKey, pair.publicKey, hashAlgorithm)
backSigBuilder.hashedSubpackets.setSignatureCreationTime(bindingTime) backSigBuilder.hashedSubpackets.setSignatureCreationTime(bindingTime)