From 787d2987f012eb38d33986edbae096062ca42384 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 13 Feb 2024 15:15:34 +0100 Subject: [PATCH] Add PGPKeyPairExtensions containing key format conversion methods --- .../extensions/PGPKeyPairExtensions.kt | 32 +++++++++++++++++++ .../generation/OpenPgpComponentKeyBuilder.kt | 25 +++------------ .../key/generation/OpenPgpKeyPairGenerator.kt | 22 +++---------- 3 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 pgpainless-core/src/main/kotlin/org/pgpainless/bouncycastle/extensions/PGPKeyPairExtensions.kt diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/bouncycastle/extensions/PGPKeyPairExtensions.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/bouncycastle/extensions/PGPKeyPairExtensions.kt new file mode 100644 index 00000000..b4c0d0bb --- /dev/null +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/bouncycastle/extensions/PGPKeyPairExtensions.kt @@ -0,0 +1,32 @@ +// SPDX-FileCopyrightText: 2024 Paul Schaub +// +// 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)) +} diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/OpenPgpComponentKeyBuilder.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/OpenPgpComponentKeyBuilder.kt index a5911e73..3f7b62a0 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/OpenPgpComponentKeyBuilder.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/OpenPgpComponentKeyBuilder.kt @@ -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() } diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/OpenPgpKeyPairGenerator.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/OpenPgpKeyPairGenerator.kt index 2ddcaf4c..a8a41dbf 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/OpenPgpKeyPairGenerator.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/OpenPgpKeyPairGenerator.kt @@ -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() } } }