Add PGPKeyPairExtensions containing key format conversion methods

This commit is contained in:
Paul Schaub 2024-02-13 15:15:34 +01:00
parent b5f8864861
commit 787d2987f0
Signed by: vanitasvitae
GPG Key ID: 62BEE9264BF17311
3 changed files with 40 additions and 39 deletions

View File

@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 2024 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.bouncycastle.extensions
import org.bouncycastle.bcpg.PublicKeyPacket
import org.bouncycastle.bcpg.PublicSubkeyPacket
import org.bouncycastle.openpgp.PGPKeyPair
import org.bouncycastle.openpgp.PGPPrivateKey
import org.bouncycastle.openpgp.PGPPublicKey
import org.pgpainless.implementation.ImplementationFactory
fun PGPKeyPair.toPrimaryKeyFormat(): PGPKeyPair {
val fpCalc = ImplementationFactory.getInstance().keyFingerprintCalculator
val subkey =
PublicKeyPacket(publicKey.algorithm, publicKey.creationTime, publicKey.publicKeyPacket.key)
return PGPKeyPair(
PGPPublicKey(subkey, fpCalc),
PGPPrivateKey(publicKey.keyID, subkey, privateKey.privateKeyDataPacket))
}
fun PGPKeyPair.toSubkeyFormat(): PGPKeyPair {
val fpCalc = ImplementationFactory.getInstance().keyFingerprintCalculator
// form subkey packet
val subkey =
PublicSubkeyPacket(
publicKey.algorithm, publicKey.creationTime, publicKey.publicKeyPacket.key)
return PGPKeyPair(
PGPPublicKey(subkey, fpCalc),
PGPPrivateKey(publicKey.keyID, subkey, privateKey.privateKeyDataPacket))
}

View File

@ -6,9 +6,9 @@ package org.pgpainless.key.generation
import java.security.KeyPairGenerator
import java.util.*
import org.bouncycastle.bcpg.PublicSubkeyPacket
import org.pgpainless.bouncycastle.extensions.toPrimaryKeyFormat
import org.pgpainless.bouncycastle.extensions.toSubkeyFormat
import org.bouncycastle.openpgp.PGPKeyPair
import org.bouncycastle.openpgp.PGPPrivateKey
import org.bouncycastle.openpgp.PGPPublicKey
import org.bouncycastle.openpgp.PGPSignature
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector
@ -168,11 +168,7 @@ class OpenPgpComponentKeyBuilder {
return builder.build()
}
override fun toPrimaryOrSubkey(keyPair: PGPKeyPair) = toPrimaryKey(keyPair)
private fun toPrimaryKey(keyPair: PGPKeyPair): PGPKeyPair {
return keyPair // is already a secret key packet
}
override fun toPrimaryOrSubkey(keyPair: PGPKeyPair) = keyPair.toPrimaryKeyFormat()
override fun primaryKey() = this
}
@ -227,20 +223,7 @@ class OpenPgpComponentKeyBuilder {
return builder.build(pair.publicKey)
}
override fun toPrimaryOrSubkey(keyPair: PGPKeyPair) = toSubkey(keyPair)
private fun toSubkey(keyPair: PGPKeyPair): PGPKeyPair {
val fpCalc = ImplementationFactory.getInstance().keyFingerprintCalculator
val pubkey = keyPair.publicKey
val privkey = keyPair.privateKey
// form subkey packet
val subkey =
PublicSubkeyPacket(
pubkey.algorithm, pubkey.creationTime, pubkey.publicKeyPacket.key)
return PGPKeyPair(
PGPPublicKey(subkey, fpCalc),
PGPPrivateKey(pubkey.keyID, subkey, privkey.privateKeyDataPacket))
}
override fun toPrimaryOrSubkey(keyPair: PGPKeyPair) = keyPair.toSubkeyFormat()
override fun primaryKey() = primaryKeyBuilder.primaryKey()
}

View File

@ -3,10 +3,9 @@ package org.pgpainless.key.generation
import java.security.KeyPair
import java.security.KeyPairGenerator
import java.util.*
import org.bouncycastle.bcpg.PublicSubkeyPacket
import org.pgpainless.bouncycastle.extensions.toPrimaryKeyFormat
import org.pgpainless.bouncycastle.extensions.toSubkeyFormat
import org.bouncycastle.openpgp.PGPKeyPair
import org.bouncycastle.openpgp.PGPPrivateKey
import org.bouncycastle.openpgp.PGPPublicKey
import org.pgpainless.implementation.ImplementationFactory
import org.pgpainless.key.generation.type.KeyType
import org.pgpainless.provider.ProviderFactory
@ -61,24 +60,11 @@ internal interface OpenPgpKeyPairGenerator {
override fun generatePrimaryKey(type: KeyType, creationTime: Date): PGPKeyPair {
// already in primary key format
return generatePgpKeyPair(type, creationTime)
return generatePgpKeyPair(type, creationTime).toPrimaryKeyFormat()
}
override fun generateSubkey(type: KeyType, creationTime: Date): PGPKeyPair {
val keyPair = generatePgpKeyPair(type, creationTime)
// We need to convert the keyPair which is in primary key format into subkey format
val fpCalc = ImplementationFactory.getInstance().keyFingerprintCalculator
val pubkey = keyPair.publicKey
val privkey = keyPair.privateKey
// transform to subkey packet
val subkey =
PublicSubkeyPacket(
pubkey.algorithm, pubkey.creationTime, pubkey.publicKeyPacket.key)
// return as PGP key pair
return PGPKeyPair(
PGPPublicKey(subkey, fpCalc),
PGPPrivateKey(pubkey.keyID, subkey, privkey.privateKeyDataPacket))
return generatePgpKeyPair(type, creationTime).toSubkeyFormat()
}
}
}