mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-24 11:57:59 +01:00
Better differentiate Base- and OpenPgpKeyBuilder
This commit is contained in:
parent
3b335fa627
commit
54a9b4f258
3 changed files with 29 additions and 4 deletions
|
@ -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()
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue