1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-06-25 04:54:49 +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) :
BaseV4KeyBuilder<BaseV4PrimaryKeyBuilder>(type, creationTime, policy = policy) {
internal fun isWithoutUserIds() = !key.publicKey.userIDs.hasNext()
fun userId(
userId: CharSequence,
algorithmSuite: AlgorithmSuite,
algorithmSuite: AlgorithmSuite = policy.keyGenerationAlgorithmSuite,
certificationType: CertificationType = CertificationType.POSITIVE,
bindingTime: Date = creationTime,
hashAlgorithm: HashAlgorithm =
@ -109,7 +111,7 @@ class BaseOpenPgpKeyBuilder {
fun userAttribute(
userAttribute: PGPUserAttributeSubpacketVector,
algorithmSuite: AlgorithmSuite,
algorithmSuite: AlgorithmSuite = policy.keyGenerationAlgorithmSuite,
certificationType: CertificationType = CertificationType.POSITIVE,
bindingTime: Date = creationTime,
hashAlgorithm: HashAlgorithm =
@ -154,24 +156,32 @@ class BaseOpenPgpKeyBuilder {
fun directKeySignature(
bindingTime: Date = creationTime,
algorithmSuite: AlgorithmSuite = policy.keyGenerationAlgorithmSuite,
hashAlgorithm: HashAlgorithm =
policy.certificationSignatureHashAlgorithmPolicy.defaultHashAlgorithm(),
subpacketsCallback: SelfSignatureSubpackets.Callback =
SelfSignatureSubpackets.defaultCallback()
) = apply {
val sig = buildDirectKeySignature(bindingTime, hashAlgorithm, subpacketsCallback)
val sig = buildDirectKeySignature(bindingTime, algorithmSuite, hashAlgorithm, subpacketsCallback)
key = PGPKeyPair(PGPPublicKey.addCertification(key.publicKey, sig), key.privateKey)
}
fun buildDirectKeySignature(
bindingTime: Date,
algorithmSuite: AlgorithmSuite,
hashAlgorithm: HashAlgorithm,
subpacketsCallback: SelfSignatureSubpackets.Callback
): PGPSignature {
val builder =
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)
return builder.build()

View file

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

View file

@ -36,4 +36,13 @@ class OpenPgpKeyBuilderTest {
.build()
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))
}
}