mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-12-25 12:27:58 +01:00
Add PGPKeyPairExtensions containing key format conversion methods
This commit is contained in:
parent
b5f8864861
commit
787d2987f0
3 changed files with 40 additions and 39 deletions
|
@ -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))
|
||||||
|
}
|
|
@ -6,9 +6,9 @@ package org.pgpainless.key.generation
|
||||||
|
|
||||||
import java.security.KeyPairGenerator
|
import java.security.KeyPairGenerator
|
||||||
import java.util.*
|
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.PGPKeyPair
|
||||||
import org.bouncycastle.openpgp.PGPPrivateKey
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKey
|
import org.bouncycastle.openpgp.PGPPublicKey
|
||||||
import org.bouncycastle.openpgp.PGPSignature
|
import org.bouncycastle.openpgp.PGPSignature
|
||||||
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector
|
import org.bouncycastle.openpgp.PGPUserAttributeSubpacketVector
|
||||||
|
@ -168,11 +168,7 @@ class OpenPgpComponentKeyBuilder {
|
||||||
return builder.build()
|
return builder.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toPrimaryOrSubkey(keyPair: PGPKeyPair) = toPrimaryKey(keyPair)
|
override fun toPrimaryOrSubkey(keyPair: PGPKeyPair) = keyPair.toPrimaryKeyFormat()
|
||||||
|
|
||||||
private fun toPrimaryKey(keyPair: PGPKeyPair): PGPKeyPair {
|
|
||||||
return keyPair // is already a secret key packet
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun primaryKey() = this
|
override fun primaryKey() = this
|
||||||
}
|
}
|
||||||
|
@ -227,20 +223,7 @@ class OpenPgpComponentKeyBuilder {
|
||||||
return builder.build(pair.publicKey)
|
return builder.build(pair.publicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun toPrimaryOrSubkey(keyPair: PGPKeyPair) = toSubkey(keyPair)
|
override fun toPrimaryOrSubkey(keyPair: PGPKeyPair) = keyPair.toSubkeyFormat()
|
||||||
|
|
||||||
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 primaryKey() = primaryKeyBuilder.primaryKey()
|
override fun primaryKey() = primaryKeyBuilder.primaryKey()
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,9 @@ package org.pgpainless.key.generation
|
||||||
import java.security.KeyPair
|
import java.security.KeyPair
|
||||||
import java.security.KeyPairGenerator
|
import java.security.KeyPairGenerator
|
||||||
import java.util.*
|
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.PGPKeyPair
|
||||||
import org.bouncycastle.openpgp.PGPPrivateKey
|
|
||||||
import org.bouncycastle.openpgp.PGPPublicKey
|
|
||||||
import org.pgpainless.implementation.ImplementationFactory
|
import org.pgpainless.implementation.ImplementationFactory
|
||||||
import org.pgpainless.key.generation.type.KeyType
|
import org.pgpainless.key.generation.type.KeyType
|
||||||
import org.pgpainless.provider.ProviderFactory
|
import org.pgpainless.provider.ProviderFactory
|
||||||
|
@ -61,24 +60,11 @@ internal interface OpenPgpKeyPairGenerator {
|
||||||
|
|
||||||
override fun generatePrimaryKey(type: KeyType, creationTime: Date): PGPKeyPair {
|
override fun generatePrimaryKey(type: KeyType, creationTime: Date): PGPKeyPair {
|
||||||
// already in primary key format
|
// already in primary key format
|
||||||
return generatePgpKeyPair(type, creationTime)
|
return generatePgpKeyPair(type, creationTime).toPrimaryKeyFormat()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun generateSubkey(type: KeyType, creationTime: Date): PGPKeyPair {
|
override fun generateSubkey(type: KeyType, creationTime: Date): PGPKeyPair {
|
||||||
val keyPair = generatePgpKeyPair(type, creationTime)
|
return generatePgpKeyPair(type, creationTime).toSubkeyFormat()
|
||||||
|
|
||||||
// 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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue