1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 10:19:39 +02:00

Basic v6 key generation test

This commit is contained in:
Paul Schaub 2025-01-29 12:10:14 +01:00
parent da9c610d14
commit 0fceb4db2d
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 44 additions and 24 deletions

View file

@ -91,7 +91,7 @@ class KeyRingBuilder(private val version: OpenPGPKeyVersion) :
requireNotNull(primaryKeySpec) { "Primary Key spec required." }
val certKey = generateKeyPair(primaryKeySpec!!, version)
val signer = buildContentSigner(certKey)
val signatureGenerator = PGPSignatureGenerator(signer)
val signatureGenerator = PGPSignatureGenerator(signer, certKey.publicKey)
val hashedSubPacketGenerator = primaryKeySpec!!.subpacketGenerator
hashedSubPacketGenerator.setIssuerFingerprintAndKeyId(certKey.publicKey)
@ -203,7 +203,8 @@ class KeyRingBuilder(private val version: OpenPGPKeyVersion) :
return hashedSubpackets
}
val bindingSignatureGenerator = PGPSignatureGenerator(buildContentSigner(subKey))
val bindingSignatureGenerator =
PGPSignatureGenerator(buildContentSigner(subKey), subKey.publicKey)
bindingSignatureGenerator.init(SignatureType.PRIMARYKEY_BINDING.code, subKey.privateKey)
val primaryKeyBindingSig =
bindingSignatureGenerator.generateCertification(primaryKey.publicKey, subKey.publicKey)

View file

@ -125,25 +125,25 @@ class KeyRingTemplates(private val version: OpenPGPKeyVersion) {
fun simpleEcKeyRing(
userId: CharSequence?,
passphrase: Passphrase = Passphrase.emptyPassphrase()
): PGPSecretKeyRing =
buildKeyRing(version)
): PGPSecretKeyRing {
val signingKeyType =
if (version == OpenPGPKeyVersion.v6) KeyType.Ed25519()
else KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519)
val encryptionKeyType =
if (version == OpenPGPKeyVersion.v6) KeyType.X25519()
else KeyType.XDH_LEGACY(XDHLegacySpec._X25519)
return buildKeyRing(version)
.apply {
setPrimaryKey(
getBuilder(
KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519),
KeyFlag.CERTIFY_OTHER,
KeyFlag.SIGN_DATA))
setPrimaryKey(getBuilder(signingKeyType, KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA))
addSubkey(
getBuilder(
KeyType.XDH_LEGACY(XDHLegacySpec._X25519),
KeyFlag.ENCRYPT_STORAGE,
KeyFlag.ENCRYPT_COMMS))
getBuilder(encryptionKeyType, KeyFlag.ENCRYPT_STORAGE, KeyFlag.ENCRYPT_COMMS))
setPassphrase(passphrase)
if (userId != null) {
addUserId(userId.toString())
}
}
.build()
}
/**
* Creates a key ring consisting of an ed25519 EdDSA primary key and a X25519 XDH subkey. The
@ -175,25 +175,26 @@ class KeyRingTemplates(private val version: OpenPGPKeyVersion) {
fun modernKeyRing(
userId: CharSequence?,
passphrase: Passphrase = Passphrase.emptyPassphrase()
): PGPSecretKeyRing =
buildKeyRing(version)
): PGPSecretKeyRing {
val signingKeyType =
if (version == OpenPGPKeyVersion.v6) KeyType.Ed25519()
else KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519)
val encryptionKeyType =
if (version == OpenPGPKeyVersion.v6) KeyType.X25519()
else KeyType.XDH_LEGACY(XDHLegacySpec._X25519)
return buildKeyRing(version)
.apply {
setPrimaryKey(
getBuilder(
KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519), KeyFlag.CERTIFY_OTHER))
setPrimaryKey(getBuilder(signingKeyType, KeyFlag.CERTIFY_OTHER))
addSubkey(
getBuilder(
KeyType.XDH_LEGACY(XDHLegacySpec._X25519),
KeyFlag.ENCRYPT_COMMS,
KeyFlag.ENCRYPT_STORAGE))
addSubkey(
getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519), KeyFlag.SIGN_DATA))
getBuilder(encryptionKeyType, KeyFlag.ENCRYPT_COMMS, KeyFlag.ENCRYPT_STORAGE))
addSubkey(getBuilder(signingKeyType, KeyFlag.SIGN_DATA))
setPassphrase(passphrase)
if (userId != null) {
addUserId(userId)
}
}
.build()
}
/**
* Generate a modern PGP key ring consisting of an ed25519 EdDSA primary key which is used to

View file

@ -0,0 +1,18 @@
package org.pgpainless.key.generation;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.OpenPGPKeyVersion;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class GenerateV6KeyTest {
@Test
public void generateModernV6Key() {
PGPSecretKeyRing secretKey = PGPainless.generateKeyRing(OpenPGPKeyVersion.v6)
.modernKeyRing("Alice <alice@example.org>");
assertEquals(6, secretKey.getPublicKey().getVersion());
}
}