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

Pass down API instance in more places

This commit is contained in:
Paul Schaub 2025-03-18 12:51:13 +01:00
parent 0e48e94a91
commit 8a9b5aa567
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 21 additions and 25 deletions

View file

@ -48,9 +48,8 @@ class PGPainless(
@JvmOverloads
fun generateKey(
version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4,
creationTime: Date = Date(),
policy: Policy = algorithmPolicy
): KeyRingTemplates = KeyRingTemplates(version, creationTime, policy)
creationTime: Date = Date()
): KeyRingTemplates = KeyRingTemplates(version, creationTime, this)
@JvmOverloads
fun buildKey(
@ -108,8 +107,8 @@ class PGPainless(
@JvmOverloads
fun buildKeyRing(
version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4,
policy: Policy = getInstance().algorithmPolicy
) = KeyRingBuilder(version, getInstance().implementation, policy)
api: PGPainless = getInstance()
) = KeyRingBuilder(version, api)
/**
* Read an existing OpenPGP key ring.

View file

@ -25,11 +25,8 @@ import org.pgpainless.signature.subpackets.SignatureSubpackets
import org.pgpainless.signature.subpackets.SignatureSubpacketsHelper
import org.pgpainless.util.Passphrase
class KeyRingBuilder(
private val version: OpenPGPKeyVersion,
private val implementation: OpenPGPImplementation,
private val policy: Policy = PGPainless.getInstance().algorithmPolicy
) : KeyRingBuilderInterface<KeyRingBuilder> {
class KeyRingBuilder(private val version: OpenPGPKeyVersion, private val api: PGPainless) :
KeyRingBuilderInterface<KeyRingBuilder> {
private var primaryKeySpec: KeySpec? = null
private val subKeySpecs = mutableListOf<KeySpec>()
@ -38,13 +35,13 @@ class KeyRingBuilder(
private var expirationDate: Date? = Date(System.currentTimeMillis() + (5 * MILLIS_IN_YEAR))
override fun setPrimaryKey(keySpec: KeySpec): KeyRingBuilder = apply {
verifyKeySpecCompliesToPolicy(keySpec, policy)
verifyKeySpecCompliesToPolicy(keySpec, api.algorithmPolicy)
verifyPrimaryKeyCanCertify(keySpec)
this.primaryKeySpec = keySpec
}
override fun addSubkey(keySpec: KeySpec): KeyRingBuilder = apply {
verifyKeySpecCompliesToPolicy(keySpec, policy)
verifyKeySpecCompliesToPolicy(keySpec, api.algorithmPolicy)
subKeySpecs.add(keySpec)
}
@ -84,11 +81,11 @@ class KeyRingBuilder(
private fun keyIsCertificationCapable(keySpec: KeySpec) = keySpec.keyType.canCertify
override fun build(): OpenPGPKey {
val checksumCalculator = implementation.checksumCalculator()
val checksumCalculator = api.implementation.checksumCalculator()
// generate primary key
requireNotNull(primaryKeySpec) { "Primary Key spec required." }
val certKey = generateKeyPair(primaryKeySpec!!, version, implementation)
val certKey = generateKeyPair(primaryKeySpec!!, version, api.implementation)
val secretKeyEncryptor = buildSecretKeyEncryptor(certKey.publicKey)
val secretKeyDecryptor = buildSecretKeyDecryptor()
@ -164,12 +161,12 @@ class KeyRingBuilder(
secretKeyList.add(secretKeys.next())
}
val pgpSecretKeyRing = PGPSecretKeyRing(secretKeyList)
return OpenPGPKey(pgpSecretKeyRing, implementation)
return OpenPGPKey(pgpSecretKeyRing, api.implementation)
}
private fun addSubKeys(primaryKey: PGPKeyPair, ringGenerator: PGPKeyRingGenerator) {
for (subKeySpec in subKeySpecs) {
val subKey = generateKeyPair(subKeySpec, version, implementation)
val subKey = generateKeyPair(subKeySpec, version, api.implementation)
if (subKeySpec.isInheritedSubPackets) {
ringGenerator.addSubKey(subKey)
} else {
@ -210,8 +207,9 @@ class KeyRingBuilder(
}
private fun buildContentSigner(certKey: PGPKeyPair): PGPContentSignerBuilder {
val hashAlgorithm = policy.certificationSignatureHashAlgorithmPolicy.defaultHashAlgorithm
return implementation.pgpContentSignerBuilder(
val hashAlgorithm =
api.algorithmPolicy.certificationSignatureHashAlgorithmPolicy.defaultHashAlgorithm
return api.implementation.pgpContentSignerBuilder(
certKey.publicKey.algorithm, hashAlgorithm.algorithmId)
}
@ -219,10 +217,10 @@ class KeyRingBuilder(
publicKey: PGPPublicKey,
): PBESecretKeyEncryptor? {
check(passphrase.isValid) { "Passphrase was cleared." }
val protectionSettings = policy.keyProtectionSettings
val protectionSettings = api.algorithmPolicy.keyProtectionSettings
return if (passphrase.isEmpty) null
else
implementation
api.implementation
.pbeSecretKeyEncryptorFactory(
protectionSettings.aead,
protectionSettings.encryptionAlgorithm.algorithmId,
@ -234,7 +232,7 @@ class KeyRingBuilder(
check(passphrase.isValid) { "Passphrase was cleared." }
return if (passphrase.isEmpty) null
else
implementation
api.implementation
.pbeSecretKeyDecryptorBuilderProvider()
.provide()
.build(passphrase.getChars())

View file

@ -15,13 +15,12 @@ import org.pgpainless.key.generation.type.KeyType
import org.pgpainless.key.generation.type.eddsa_legacy.EdDSALegacyCurve
import org.pgpainless.key.generation.type.rsa.RsaLength
import org.pgpainless.key.generation.type.xdh_legacy.XDHLegacySpec
import org.pgpainless.policy.Policy
import org.pgpainless.util.Passphrase
class KeyRingTemplates(
private val version: OpenPGPKeyVersion,
private val creationTime: Date = Date(),
private val policy: Policy = PGPainless.getInstance().algorithmPolicy
private val api: PGPainless = PGPainless.getInstance()
) {
/**
@ -39,7 +38,7 @@ class KeyRingTemplates(
length: RsaLength,
passphrase: Passphrase = Passphrase.emptyPassphrase()
): OpenPGPKey =
buildKeyRing(version, policy)
buildKeyRing(version, api)
.apply {
setPrimaryKey(
getBuilder(KeyType.RSA(length), KeyFlag.CERTIFY_OTHER)

View file

@ -271,7 +271,7 @@ class SecretKeyRingEditor(
protector: SecretKeyRingProtector
): SecretKeyRingEditorInterface {
val version = OpenPGPKeyVersion.from(secretKeyRing.publicKey.version)
val keyPair = KeyRingBuilder.generateKeyPair(keySpec, version)
val keyPair = KeyRingBuilder.generateKeyPair(keySpec, version, api.implementation)
val subkeyProtector =
PasswordBasedSecretKeyRingProtector.forKeyId(keyPair.keyIdentifier, subkeyPassphrase)
val keyFlags = KeyFlag.fromBitmask(keySpec.subpackets.keyFlags).toMutableList()