1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-06-30 23:36:44 +02:00

Better differentiate Base- and OpenPgpKeyBuilder

This commit is contained in:
Paul Schaub 2024-01-22 17:17:26 +01:00
parent 3b335fa627
commit 54a9b4f258
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 29 additions and 4 deletions

View file

@ -67,9 +67,11 @@ class BaseOpenPgpKeyBuilder {
class BaseV4PrimaryKeyBuilder(type: KeyType, creationTime: Date, policy: Policy) : class BaseV4PrimaryKeyBuilder(type: KeyType, creationTime: Date, policy: Policy) :
BaseV4KeyBuilder<BaseV4PrimaryKeyBuilder>(type, creationTime, policy = policy) { BaseV4KeyBuilder<BaseV4PrimaryKeyBuilder>(type, creationTime, policy = policy) {
internal fun isWithoutUserIds() = !key.publicKey.userIDs.hasNext()
fun userId( fun userId(
userId: CharSequence, userId: CharSequence,
algorithmSuite: AlgorithmSuite, algorithmSuite: AlgorithmSuite = policy.keyGenerationAlgorithmSuite,
certificationType: CertificationType = CertificationType.POSITIVE, certificationType: CertificationType = CertificationType.POSITIVE,
bindingTime: Date = creationTime, bindingTime: Date = creationTime,
hashAlgorithm: HashAlgorithm = hashAlgorithm: HashAlgorithm =
@ -109,7 +111,7 @@ class BaseOpenPgpKeyBuilder {
fun userAttribute( fun userAttribute(
userAttribute: PGPUserAttributeSubpacketVector, userAttribute: PGPUserAttributeSubpacketVector,
algorithmSuite: AlgorithmSuite, algorithmSuite: AlgorithmSuite = policy.keyGenerationAlgorithmSuite,
certificationType: CertificationType = CertificationType.POSITIVE, certificationType: CertificationType = CertificationType.POSITIVE,
bindingTime: Date = creationTime, bindingTime: Date = creationTime,
hashAlgorithm: HashAlgorithm = hashAlgorithm: HashAlgorithm =
@ -154,24 +156,32 @@ class BaseOpenPgpKeyBuilder {
fun directKeySignature( fun directKeySignature(
bindingTime: Date = creationTime, bindingTime: Date = creationTime,
algorithmSuite: AlgorithmSuite = policy.keyGenerationAlgorithmSuite,
hashAlgorithm: HashAlgorithm = hashAlgorithm: HashAlgorithm =
policy.certificationSignatureHashAlgorithmPolicy.defaultHashAlgorithm(), policy.certificationSignatureHashAlgorithmPolicy.defaultHashAlgorithm(),
subpacketsCallback: SelfSignatureSubpackets.Callback = subpacketsCallback: SelfSignatureSubpackets.Callback =
SelfSignatureSubpackets.defaultCallback() SelfSignatureSubpackets.defaultCallback()
) = apply { ) = apply {
val sig = buildDirectKeySignature(bindingTime, hashAlgorithm, subpacketsCallback) val sig = buildDirectKeySignature(bindingTime, algorithmSuite, hashAlgorithm, subpacketsCallback)
key = PGPKeyPair(PGPPublicKey.addCertification(key.publicKey, sig), key.privateKey) key = PGPKeyPair(PGPPublicKey.addCertification(key.publicKey, sig), key.privateKey)
} }
fun buildDirectKeySignature( fun buildDirectKeySignature(
bindingTime: Date, bindingTime: Date,
algorithmSuite: AlgorithmSuite,
hashAlgorithm: HashAlgorithm, hashAlgorithm: HashAlgorithm,
subpacketsCallback: SelfSignatureSubpackets.Callback subpacketsCallback: SelfSignatureSubpackets.Callback
): PGPSignature { ): PGPSignature {
val builder = val builder =
DirectKeySelfSignatureBuilder(key.privateKey, key.publicKey, hashAlgorithm) DirectKeySelfSignatureBuilder(key.privateKey, key.publicKey, hashAlgorithm)
builder.hashedSubpackets.setSignatureCreationTime(bindingTime) builder.hashedSubpackets.apply {
setSignatureCreationTime(bindingTime)
setPreferredHashAlgorithms(algorithmSuite.hashAlgorithms)
setPreferredSymmetricKeyAlgorithms(algorithmSuite.symmetricKeyAlgorithms)
setPreferredCompressionAlgorithms(algorithmSuite.compressionAlgorithms)
}
builder.applyCallback(subpacketsCallback) builder.applyCallback(subpacketsCallback)
return builder.build() return builder.build()

View file

@ -109,6 +109,12 @@ open class OpenPgpKeyBuilder(
fun build( fun build(
protector: SecretKeyRingProtector = SecretKeyRingProtector.unprotectedKeys() protector: SecretKeyRingProtector = SecretKeyRingProtector.unprotectedKeys()
): PGPSecretKeyRing { ): PGPSecretKeyRing {
// Add DK sig in case of no user-id
if (primaryKey.isWithoutUserIds()) {
primaryKey.directKeySignature()
}
return PGPSecretKeyRing( return PGPSecretKeyRing(
mutableListOf( mutableListOf(
PGPSecretKey( PGPSecretKey(

View file

@ -36,4 +36,13 @@ class OpenPgpKeyBuilderTest {
.build() .build()
println(PGPainless.asciiArmor(key)) println(PGPainless.asciiArmor(key))
} }
@Test
fun minimalWithUserId() {
val key = OpenPgpKeyBuilder(Policy.getInstance())
.buildV4Key(KeyType.EDDSA(EdDSACurve._Ed25519))
.addUserId("Alice <alice@pgpainless.org>")
.build()
println(PGPainless.asciiArmor(key))
}
} }