mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 18:29:39 +02:00
Pass down API instance in more places
This commit is contained in:
parent
0e48e94a91
commit
8a9b5aa567
4 changed files with 21 additions and 25 deletions
|
@ -48,9 +48,8 @@ class PGPainless(
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
fun generateKey(
|
fun generateKey(
|
||||||
version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4,
|
version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4,
|
||||||
creationTime: Date = Date(),
|
creationTime: Date = Date()
|
||||||
policy: Policy = algorithmPolicy
|
): KeyRingTemplates = KeyRingTemplates(version, creationTime, this)
|
||||||
): KeyRingTemplates = KeyRingTemplates(version, creationTime, policy)
|
|
||||||
|
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
fun buildKey(
|
fun buildKey(
|
||||||
|
@ -108,8 +107,8 @@ class PGPainless(
|
||||||
@JvmOverloads
|
@JvmOverloads
|
||||||
fun buildKeyRing(
|
fun buildKeyRing(
|
||||||
version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4,
|
version: OpenPGPKeyVersion = OpenPGPKeyVersion.v4,
|
||||||
policy: Policy = getInstance().algorithmPolicy
|
api: PGPainless = getInstance()
|
||||||
) = KeyRingBuilder(version, getInstance().implementation, policy)
|
) = KeyRingBuilder(version, api)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read an existing OpenPGP key ring.
|
* Read an existing OpenPGP key ring.
|
||||||
|
|
|
@ -25,11 +25,8 @@ import org.pgpainless.signature.subpackets.SignatureSubpackets
|
||||||
import org.pgpainless.signature.subpackets.SignatureSubpacketsHelper
|
import org.pgpainless.signature.subpackets.SignatureSubpacketsHelper
|
||||||
import org.pgpainless.util.Passphrase
|
import org.pgpainless.util.Passphrase
|
||||||
|
|
||||||
class KeyRingBuilder(
|
class KeyRingBuilder(private val version: OpenPGPKeyVersion, private val api: PGPainless) :
|
||||||
private val version: OpenPGPKeyVersion,
|
KeyRingBuilderInterface<KeyRingBuilder> {
|
||||||
private val implementation: OpenPGPImplementation,
|
|
||||||
private val policy: Policy = PGPainless.getInstance().algorithmPolicy
|
|
||||||
) : KeyRingBuilderInterface<KeyRingBuilder> {
|
|
||||||
|
|
||||||
private var primaryKeySpec: KeySpec? = null
|
private var primaryKeySpec: KeySpec? = null
|
||||||
private val subKeySpecs = mutableListOf<KeySpec>()
|
private val subKeySpecs = mutableListOf<KeySpec>()
|
||||||
|
@ -38,13 +35,13 @@ class KeyRingBuilder(
|
||||||
private var expirationDate: Date? = Date(System.currentTimeMillis() + (5 * MILLIS_IN_YEAR))
|
private var expirationDate: Date? = Date(System.currentTimeMillis() + (5 * MILLIS_IN_YEAR))
|
||||||
|
|
||||||
override fun setPrimaryKey(keySpec: KeySpec): KeyRingBuilder = apply {
|
override fun setPrimaryKey(keySpec: KeySpec): KeyRingBuilder = apply {
|
||||||
verifyKeySpecCompliesToPolicy(keySpec, policy)
|
verifyKeySpecCompliesToPolicy(keySpec, api.algorithmPolicy)
|
||||||
verifyPrimaryKeyCanCertify(keySpec)
|
verifyPrimaryKeyCanCertify(keySpec)
|
||||||
this.primaryKeySpec = keySpec
|
this.primaryKeySpec = keySpec
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun addSubkey(keySpec: KeySpec): KeyRingBuilder = apply {
|
override fun addSubkey(keySpec: KeySpec): KeyRingBuilder = apply {
|
||||||
verifyKeySpecCompliesToPolicy(keySpec, policy)
|
verifyKeySpecCompliesToPolicy(keySpec, api.algorithmPolicy)
|
||||||
subKeySpecs.add(keySpec)
|
subKeySpecs.add(keySpec)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,11 +81,11 @@ class KeyRingBuilder(
|
||||||
private fun keyIsCertificationCapable(keySpec: KeySpec) = keySpec.keyType.canCertify
|
private fun keyIsCertificationCapable(keySpec: KeySpec) = keySpec.keyType.canCertify
|
||||||
|
|
||||||
override fun build(): OpenPGPKey {
|
override fun build(): OpenPGPKey {
|
||||||
val checksumCalculator = implementation.checksumCalculator()
|
val checksumCalculator = api.implementation.checksumCalculator()
|
||||||
|
|
||||||
// generate primary key
|
// generate primary key
|
||||||
requireNotNull(primaryKeySpec) { "Primary Key spec required." }
|
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 secretKeyEncryptor = buildSecretKeyEncryptor(certKey.publicKey)
|
||||||
val secretKeyDecryptor = buildSecretKeyDecryptor()
|
val secretKeyDecryptor = buildSecretKeyDecryptor()
|
||||||
|
@ -164,12 +161,12 @@ class KeyRingBuilder(
|
||||||
secretKeyList.add(secretKeys.next())
|
secretKeyList.add(secretKeys.next())
|
||||||
}
|
}
|
||||||
val pgpSecretKeyRing = PGPSecretKeyRing(secretKeyList)
|
val pgpSecretKeyRing = PGPSecretKeyRing(secretKeyList)
|
||||||
return OpenPGPKey(pgpSecretKeyRing, implementation)
|
return OpenPGPKey(pgpSecretKeyRing, api.implementation)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun addSubKeys(primaryKey: PGPKeyPair, ringGenerator: PGPKeyRingGenerator) {
|
private fun addSubKeys(primaryKey: PGPKeyPair, ringGenerator: PGPKeyRingGenerator) {
|
||||||
for (subKeySpec in subKeySpecs) {
|
for (subKeySpec in subKeySpecs) {
|
||||||
val subKey = generateKeyPair(subKeySpec, version, implementation)
|
val subKey = generateKeyPair(subKeySpec, version, api.implementation)
|
||||||
if (subKeySpec.isInheritedSubPackets) {
|
if (subKeySpec.isInheritedSubPackets) {
|
||||||
ringGenerator.addSubKey(subKey)
|
ringGenerator.addSubKey(subKey)
|
||||||
} else {
|
} else {
|
||||||
|
@ -210,8 +207,9 @@ class KeyRingBuilder(
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildContentSigner(certKey: PGPKeyPair): PGPContentSignerBuilder {
|
private fun buildContentSigner(certKey: PGPKeyPair): PGPContentSignerBuilder {
|
||||||
val hashAlgorithm = policy.certificationSignatureHashAlgorithmPolicy.defaultHashAlgorithm
|
val hashAlgorithm =
|
||||||
return implementation.pgpContentSignerBuilder(
|
api.algorithmPolicy.certificationSignatureHashAlgorithmPolicy.defaultHashAlgorithm
|
||||||
|
return api.implementation.pgpContentSignerBuilder(
|
||||||
certKey.publicKey.algorithm, hashAlgorithm.algorithmId)
|
certKey.publicKey.algorithm, hashAlgorithm.algorithmId)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,10 +217,10 @@ class KeyRingBuilder(
|
||||||
publicKey: PGPPublicKey,
|
publicKey: PGPPublicKey,
|
||||||
): PBESecretKeyEncryptor? {
|
): PBESecretKeyEncryptor? {
|
||||||
check(passphrase.isValid) { "Passphrase was cleared." }
|
check(passphrase.isValid) { "Passphrase was cleared." }
|
||||||
val protectionSettings = policy.keyProtectionSettings
|
val protectionSettings = api.algorithmPolicy.keyProtectionSettings
|
||||||
return if (passphrase.isEmpty) null
|
return if (passphrase.isEmpty) null
|
||||||
else
|
else
|
||||||
implementation
|
api.implementation
|
||||||
.pbeSecretKeyEncryptorFactory(
|
.pbeSecretKeyEncryptorFactory(
|
||||||
protectionSettings.aead,
|
protectionSettings.aead,
|
||||||
protectionSettings.encryptionAlgorithm.algorithmId,
|
protectionSettings.encryptionAlgorithm.algorithmId,
|
||||||
|
@ -234,7 +232,7 @@ class KeyRingBuilder(
|
||||||
check(passphrase.isValid) { "Passphrase was cleared." }
|
check(passphrase.isValid) { "Passphrase was cleared." }
|
||||||
return if (passphrase.isEmpty) null
|
return if (passphrase.isEmpty) null
|
||||||
else
|
else
|
||||||
implementation
|
api.implementation
|
||||||
.pbeSecretKeyDecryptorBuilderProvider()
|
.pbeSecretKeyDecryptorBuilderProvider()
|
||||||
.provide()
|
.provide()
|
||||||
.build(passphrase.getChars())
|
.build(passphrase.getChars())
|
||||||
|
|
|
@ -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.eddsa_legacy.EdDSALegacyCurve
|
||||||
import org.pgpainless.key.generation.type.rsa.RsaLength
|
import org.pgpainless.key.generation.type.rsa.RsaLength
|
||||||
import org.pgpainless.key.generation.type.xdh_legacy.XDHLegacySpec
|
import org.pgpainless.key.generation.type.xdh_legacy.XDHLegacySpec
|
||||||
import org.pgpainless.policy.Policy
|
|
||||||
import org.pgpainless.util.Passphrase
|
import org.pgpainless.util.Passphrase
|
||||||
|
|
||||||
class KeyRingTemplates(
|
class KeyRingTemplates(
|
||||||
private val version: OpenPGPKeyVersion,
|
private val version: OpenPGPKeyVersion,
|
||||||
private val creationTime: Date = Date(),
|
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,
|
length: RsaLength,
|
||||||
passphrase: Passphrase = Passphrase.emptyPassphrase()
|
passphrase: Passphrase = Passphrase.emptyPassphrase()
|
||||||
): OpenPGPKey =
|
): OpenPGPKey =
|
||||||
buildKeyRing(version, policy)
|
buildKeyRing(version, api)
|
||||||
.apply {
|
.apply {
|
||||||
setPrimaryKey(
|
setPrimaryKey(
|
||||||
getBuilder(KeyType.RSA(length), KeyFlag.CERTIFY_OTHER)
|
getBuilder(KeyType.RSA(length), KeyFlag.CERTIFY_OTHER)
|
||||||
|
|
|
@ -271,7 +271,7 @@ class SecretKeyRingEditor(
|
||||||
protector: SecretKeyRingProtector
|
protector: SecretKeyRingProtector
|
||||||
): SecretKeyRingEditorInterface {
|
): SecretKeyRingEditorInterface {
|
||||||
val version = OpenPGPKeyVersion.from(secretKeyRing.publicKey.version)
|
val version = OpenPGPKeyVersion.from(secretKeyRing.publicKey.version)
|
||||||
val keyPair = KeyRingBuilder.generateKeyPair(keySpec, version)
|
val keyPair = KeyRingBuilder.generateKeyPair(keySpec, version, api.implementation)
|
||||||
val subkeyProtector =
|
val subkeyProtector =
|
||||||
PasswordBasedSecretKeyRingProtector.forKeyId(keyPair.keyIdentifier, subkeyPassphrase)
|
PasswordBasedSecretKeyRingProtector.forKeyId(keyPair.keyIdentifier, subkeyPassphrase)
|
||||||
val keyFlags = KeyFlag.fromBitmask(keySpec.subpackets.keyFlags).toMutableList()
|
val keyFlags = KeyFlag.fromBitmask(keySpec.subpackets.keyFlags).toMutableList()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue