diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/KeyRingBuilder.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/KeyRingBuilder.kt index 2eb86dcc..662ae331 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/KeyRingBuilder.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/key/generation/KeyRingBuilder.kt @@ -107,9 +107,7 @@ class KeyRingBuilder( hashedSubPacketGenerator.setPrimaryUserId() } - val generator = PGPSignatureSubpacketGenerator() - SignatureSubpacketsHelper.applyTo(hashedSubPacketGenerator, generator) - val hashedSubPackets = generator.generate() + val hashedSubPackets = hashedSubPacketGenerator.subpacketsGenerator.generate() val ringGenerator = if (userIds.isEmpty()) { PGPKeyRingGenerator( diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/BaseSignatureSubpackets.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/BaseSignatureSubpackets.kt index 2349ed9a..4b4c40d5 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/BaseSignatureSubpackets.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/BaseSignatureSubpackets.kt @@ -78,10 +78,13 @@ interface BaseSignatureSubpackets { expirationTime: SignatureExpirationTime? ): BaseSignatureSubpackets + @Deprecated("Usage of subpacket is discouraged") fun setSignerUserId(userId: CharSequence): BaseSignatureSubpackets + @Deprecated("Usage of subpacket is discouraged") fun setSignerUserId(isCritical: Boolean, userId: CharSequence): BaseSignatureSubpackets + @Deprecated("Usage of subpacket is discouraged") fun setSignerUserId(signerUserID: SignerUserID?): BaseSignatureSubpackets fun addNotationData( diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.kt index 7d160c5c..94e7b1c5 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SelfSignatureSubpackets.kt @@ -8,6 +8,7 @@ import java.util.* import org.bouncycastle.bcpg.sig.Features import org.bouncycastle.bcpg.sig.KeyExpirationTime import org.bouncycastle.bcpg.sig.KeyFlags +import org.bouncycastle.bcpg.sig.PreferredAEADCiphersuites import org.bouncycastle.bcpg.sig.PreferredAlgorithms import org.bouncycastle.bcpg.sig.PrimaryUserID import org.bouncycastle.bcpg.sig.RevocationKey @@ -114,18 +115,27 @@ interface SelfSignatureSubpackets : BaseSignatureSubpackets { fun setPreferredAEADCiphersuites(aeadAlgorithms: Set): SelfSignatureSubpackets + fun setPreferredAEADCiphersuites( + algorithms: PreferredAEADCiphersuites.Builder? + ): SelfSignatureSubpackets + + @Deprecated("Use of this subpacket is discouraged.") fun addRevocationKey(revocationKey: PGPPublicKey): SelfSignatureSubpackets + @Deprecated("Use of this subpacket is discouraged.") fun addRevocationKey(isCritical: Boolean, revocationKey: PGPPublicKey): SelfSignatureSubpackets + @Deprecated("Use of this subpacket is discouraged.") fun addRevocationKey( isCritical: Boolean, isSensitive: Boolean, revocationKey: PGPPublicKey ): SelfSignatureSubpackets + @Deprecated("Use of this subpacket is discouraged.") fun addRevocationKey(revocationKey: RevocationKey): SelfSignatureSubpackets + @Deprecated("Use of this subpacket is discouraged.") fun clearRevocationKeys(): SelfSignatureSubpackets fun setFeatures(vararg features: Feature): SelfSignatureSubpackets diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SignatureSubpackets.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SignatureSubpackets.kt index 192412c9..7869ca08 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SignatureSubpackets.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SignatureSubpackets.kt @@ -10,16 +10,18 @@ import java.util.* import kotlin.experimental.or import openpgp.secondsTill import openpgp.toSecondsPrecision -import org.bouncycastle.bcpg.SignatureSubpacket import org.bouncycastle.bcpg.SignatureSubpacketTags import org.bouncycastle.bcpg.sig.* import org.bouncycastle.openpgp.PGPPublicKey import org.bouncycastle.openpgp.PGPSignature +import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator import org.bouncycastle.openpgp.PGPSignatureSubpacketVector import org.pgpainless.algorithm.* import org.pgpainless.key.util.RevocationAttributes -class SignatureSubpackets : +class SignatureSubpackets( + val subpacketsGenerator: PGPSignatureSubpacketGenerator = PGPSignatureSubpacketGenerator() +) : BaseSignatureSubpackets, SelfSignatureSubpackets, CertificationSubpackets, @@ -27,32 +29,6 @@ class SignatureSubpackets : interface Callback : SignatureSubpacketCallback - var signatureCreationTimeSubpacket: SignatureCreationTime? = null - var signatureExpirationTimeSubpacket: SignatureExpirationTime? = null - var issuerKeyIdSubpacket: IssuerKeyID? = null - var issuerFingerprintSubpacket: IssuerFingerprint? = null - val notationDataSubpackets: List = mutableListOf() - val intendedRecipientFingerprintSubpackets: List = mutableListOf() - val revocationKeySubpackets: List = mutableListOf() - var exportableSubpacket: Exportable? = null - var signatureTargetSubpacket: SignatureTarget? = null - var featuresSubpacket: Features? = null - var keyFlagsSubpacket: KeyFlags? = null - var trustSubpacket: TrustSignature? = null - var preferredCompressionAlgorithmsSubpacket: PreferredAlgorithms? = null - var preferredSymmetricKeyAlgorithmsSubpacket: PreferredAlgorithms? = null - var preferredHashAlgorithmsSubpacket: PreferredAlgorithms? = null - var preferredAEADCiphersuites: List? = null - val embeddedSignatureSubpackets: List = mutableListOf() - var signerUserIdSubpacket: SignerUserID? = null - var keyExpirationTimeSubpacket: KeyExpirationTime? = null - var policyURISubpacket: PolicyURI? = null - var primaryUserIdSubpacket: PrimaryUserID? = null - var regularExpressionSubpacket: RegularExpression? = null - var revocableSubpacket: Revocable? = null - var revocationReasonSubpacket: RevocationReason? = null - val residualSubpackets: List = mutableListOf() - companion object { @JvmStatic @@ -78,7 +54,11 @@ class SignatureSubpackets : @JvmStatic fun createSubpacketsFrom(base: PGPSignatureSubpacketVector): SignatureSubpackets { - return SignatureSubpacketsHelper.applyFrom(base, SignatureSubpackets()) + return SignatureSubpackets(PGPSignatureSubpacketGenerator(base)).apply { + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.ISSUER_KEY_ID) + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.ISSUER_FINGERPRINT) + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.CREATION_TIME) + } } @JvmStatic @@ -88,7 +68,7 @@ class SignatureSubpackets : @JvmStatic fun createEmptySubpackets(): SignatureSubpackets { - return SignatureSubpackets() + return SignatureSubpackets(PGPSignatureSubpacketGenerator()) } /** Factory method for a [Callback] that does nothing. */ @@ -158,7 +138,11 @@ class SignatureSubpackets : } override fun setRevocationReason(reason: RevocationReason?): SignatureSubpackets = apply { - this.revocationReasonSubpacket = reason + reason?.let { + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.REVOCATION_REASON) + subpacketsGenerator.setRevocationReason( + it.isCritical, it.revocationReason, it.revocationDescription) + } } override fun setKeyFlags(vararg keyflags: KeyFlag): SignatureSubpackets = apply { @@ -175,7 +159,8 @@ class SignatureSubpackets : } override fun setKeyFlags(keyFlags: KeyFlags?): SignatureSubpackets = apply { - this.keyFlagsSubpacket = keyFlags + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.KEY_FLAGS) + keyFlags?.let { subpacketsGenerator.setKeyFlags(it.isCritical, it.flags) } } override fun setPrimaryUserId(): SignatureSubpackets = apply { setPrimaryUserId(true) } @@ -185,7 +170,10 @@ class SignatureSubpackets : } override fun setPrimaryUserId(primaryUserID: PrimaryUserID?): SignatureSubpackets = apply { - this.primaryUserIdSubpacket = primaryUserID + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.PRIMARY_USER_ID) + primaryUserID?.let { + subpacketsGenerator.setPrimaryUserID(it.isCritical, it.isPrimaryUserID) + } } override fun setKeyExpirationTime( @@ -222,7 +210,10 @@ class SignatureSubpackets : override fun setKeyExpirationTime(keyExpirationTime: KeyExpirationTime?): SignatureSubpackets = apply { - this.keyExpirationTimeSubpacket = keyExpirationTime + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.KEY_EXPIRE_TIME) + keyExpirationTime?.let { + subpacketsGenerator.setKeyExpirationTime(it.isCritical, it.time) + } } override fun setPreferredCompressionAlgorithms( @@ -251,7 +242,10 @@ class SignatureSubpackets : algorithms == null || algorithms.type == SignatureSubpacketTags.PREFERRED_COMP_ALGS) { "Invalid preferred compression algorithms type." } - this.preferredCompressionAlgorithmsSubpacket = algorithms + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.PREFERRED_COMP_ALGS) + algorithms?.let { + subpacketsGenerator.setPreferredCompressionAlgorithms(it.isCritical, it.preferences) + } } override fun setPreferredSymmetricKeyAlgorithms( @@ -280,7 +274,10 @@ class SignatureSubpackets : algorithms == null || algorithms.type == SignatureSubpacketTags.PREFERRED_SYM_ALGS) { "Invalid preferred symmetric algorithms type." } - this.preferredSymmetricKeyAlgorithmsSubpacket = algorithms + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.PREFERRED_SYM_ALGS) + algorithms?.let { + subpacketsGenerator.setPreferredSymmetricAlgorithms(it.isCritical, it.preferences) + } } override fun setPreferredHashAlgorithms(vararg algorithms: HashAlgorithm): SignatureSubpackets = @@ -310,12 +307,29 @@ class SignatureSubpackets : algorithms.type == SignatureSubpacketTags.PREFERRED_HASH_ALGS) { "Invalid preferred hash algorithms type." } - this.preferredHashAlgorithmsSubpacket = algorithms + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.PREFERRED_HASH_ALGS) + algorithms?.let { + subpacketsGenerator.setPreferredHashAlgorithms(it.isCritical, it.preferences) + } } override fun setPreferredAEADCiphersuites( aeadAlgorithms: Set - ): SignatureSubpackets = apply { this.preferredAEADCiphersuites = aeadAlgorithms.toList() } + ): SignatureSubpackets = + setPreferredAEADCiphersuites( + PreferredAEADCiphersuites.builder(false).apply { + for (algorithm in aeadAlgorithms) { + addCombination( + algorithm.ciphermode.algorithmId, algorithm.aeadAlgorithm.algorithmId) + } + }) + + override fun setPreferredAEADCiphersuites( + algorithms: PreferredAEADCiphersuites.Builder? + ): SignatureSubpackets = apply { + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.PREFERRED_AEAD_ALGORITHMS) + algorithms?.let { subpacketsGenerator.setPreferredAEADCiphersuites(algorithms) } + } override fun addRevocationKey(revocationKey: PGPPublicKey): SignatureSubpackets = apply { addRevocationKey(true, revocationKey) @@ -331,17 +345,17 @@ class SignatureSubpackets : isSensitive: Boolean, revocationKey: PGPPublicKey ): SignatureSubpackets = apply { - val clazz = 0x80.toByte() or if (isSensitive) 0x40.toByte() else 0x00.toByte() + val clazz = if (isSensitive) 0x80.toByte() or 0x40.toByte() else 0x80.toByte() addRevocationKey( RevocationKey(isCritical, clazz, revocationKey.algorithm, revocationKey.fingerprint)) } override fun addRevocationKey(revocationKey: RevocationKey): SignatureSubpackets = apply { - (this.revocationKeySubpackets as MutableList).add(revocationKey) + subpacketsGenerator.addCustomSubpacket(revocationKey) } override fun clearRevocationKeys(): SignatureSubpackets = apply { - (this.revocationKeySubpackets as MutableList).clear() + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.REVOCATION_KEY) } override fun setFeatures(vararg features: Feature): SignatureSubpackets = apply { @@ -354,7 +368,8 @@ class SignatureSubpackets : } override fun setFeatures(features: Features?): SignatureSubpackets = apply { - this.featuresSubpacket = features + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.FEATURES) + features?.let { subpacketsGenerator.setFeature(it.isCritical, it.features) } } override fun setAppropriateIssuerInfo(key: PGPPublicKey) = apply { @@ -384,23 +399,22 @@ class SignatureSubpackets : } override fun setIssuerKeyId(issuerKeyID: IssuerKeyID?): SignatureSubpackets = apply { - this.issuerKeyIdSubpacket = issuerKeyID + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.ISSUER_KEY_ID) + issuerKeyID?.let { subpacketsGenerator.setIssuerKeyID(it.isCritical, it.keyID) } } override fun setIssuerFingerprint( isCritical: Boolean, issuer: PGPPublicKey - ): SignatureSubpackets = apply { - setIssuerFingerprint(IssuerFingerprint(isCritical, issuer.version, issuer.fingerprint)) - } + ): SignatureSubpackets = apply { subpacketsGenerator.setIssuerFingerprint(isCritical, issuer) } - override fun setIssuerFingerprint(issuer: PGPPublicKey): SignatureSubpackets = apply { - setIssuerFingerprint(false, issuer) - } + override fun setIssuerFingerprint(issuer: PGPPublicKey): SignatureSubpackets = + setIssuerFingerprint(true, issuer) override fun setIssuerFingerprint(fingerprint: IssuerFingerprint?): SignatureSubpackets = apply { - this.issuerFingerprintSubpacket = fingerprint + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.ISSUER_FINGERPRINT) + fingerprint?.let { subpacketsGenerator.addCustomSubpacket(it) } } override fun setSignatureCreationTime(creationTime: Date): SignatureSubpackets = apply { @@ -416,7 +430,10 @@ class SignatureSubpackets : override fun setSignatureCreationTime( creationTime: SignatureCreationTime? - ): SignatureSubpackets = apply { this.signatureCreationTimeSubpacket = creationTime } + ): SignatureSubpackets = apply { + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.CREATION_TIME) + creationTime?.let { subpacketsGenerator.setSignatureCreationTime(it.isCritical, it.time) } + } override fun setSignatureExpirationTime( creationTime: Date, @@ -465,7 +482,12 @@ class SignatureSubpackets : override fun setSignatureExpirationTime( expirationTime: SignatureExpirationTime? - ): SignatureSubpackets = apply { this.signatureExpirationTimeSubpacket = expirationTime } + ): SignatureSubpackets = apply { + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.EXPIRE_TIME) + expirationTime?.let { + subpacketsGenerator.setSignatureExpirationTime(it.isCritical, it.time) + } + } override fun setSignerUserId(userId: CharSequence): SignatureSubpackets = apply { setSignerUserId(false, userId) @@ -477,7 +499,8 @@ class SignatureSubpackets : } override fun setSignerUserId(signerUserID: SignerUserID?): SignatureSubpackets = apply { - this.signerUserIdSubpacket = signerUserID + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.SIGNER_USER_ID) + signerUserID?.let { subpacketsGenerator.setSignerUserID(it.isCritical, it.rawID) } } override fun addNotationData( @@ -498,11 +521,15 @@ class SignatureSubpackets : } override fun addNotationData(notationData: NotationData): SignatureSubpackets = apply { - (this.notationDataSubpackets as MutableList).add(notationData) + subpacketsGenerator.addNotationData( + notationData.isCritical, + notationData.isHumanReadable, + notationData.notationName, + notationData.notationValue) } override fun clearNotationData(): SignatureSubpackets = apply { - (this.notationDataSubpackets as MutableList).clear() + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.NOTATION_DATA) } override fun addIntendedRecipientFingerprint(recipientKey: PGPPublicKey): SignatureSubpackets = @@ -514,19 +541,16 @@ class SignatureSubpackets : isCritical: Boolean, recipientKey: PGPPublicKey ): SignatureSubpackets = apply { - addIntendedRecipientFingerprint( - IntendedRecipientFingerprint( - isCritical, recipientKey.version, recipientKey.fingerprint)) + subpacketsGenerator.addIntendedRecipientFingerprint(isCritical, recipientKey) } override fun addIntendedRecipientFingerprint( intendedRecipient: IntendedRecipientFingerprint - ): SignatureSubpackets = apply { - (this.intendedRecipientFingerprintSubpackets as MutableList).add(intendedRecipient) - } + ): SignatureSubpackets = apply { subpacketsGenerator.addCustomSubpacket(intendedRecipient) } override fun clearIntendedRecipientFingerprints(): SignatureSubpackets = apply { - (this.intendedRecipientFingerprintSubpackets as MutableList).clear() + subpacketsGenerator.removePacketsOfType( + SignatureSubpacketTags.INTENDED_RECIPIENT_FINGERPRINT) } override fun setExportable(): SignatureSubpackets = apply { setExportable(true) } @@ -541,7 +565,8 @@ class SignatureSubpackets : } override fun setExportable(exportable: Exportable?): SignatureSubpackets = apply { - this.exportableSubpacket = exportable + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.EXPORTABLE) + exportable?.let { subpacketsGenerator.setExportable(it.isCritical, it.isExportable) } } override fun setPolicyUrl(policyUrl: URL): SignatureSubpackets = apply { @@ -553,7 +578,7 @@ class SignatureSubpackets : } override fun setPolicyUrl(policyUrl: PolicyURI?): SignatureSubpackets = apply { - this.policyURISubpacket = policyURISubpacket + policyUrl?.let { subpacketsGenerator.addPolicyURI(it.isCritical, it.uri) } } override fun setRegularExpression(regex: CharSequence): SignatureSubpackets = apply { @@ -568,7 +593,7 @@ class SignatureSubpackets : } override fun setRegularExpression(regex: RegularExpression?): SignatureSubpackets = apply { - this.regularExpressionSubpacket = regex + regex?.let { subpacketsGenerator.addRegularExpression(it.isCritical, it.regex) } } override fun setRevocable(): SignatureSubpackets = apply { setRevocable(true) } @@ -583,7 +608,8 @@ class SignatureSubpackets : } override fun setRevocable(revocable: Revocable?): SignatureSubpackets = apply { - this.revocableSubpacket = revocable + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.REVOCABLE) + revocable?.let { subpacketsGenerator.setRevocable(it.isCritical, it.isRevocable) } } override fun setSignatureTarget( @@ -607,7 +633,11 @@ class SignatureSubpackets : override fun setSignatureTarget(signatureTarget: SignatureTarget?): SignatureSubpackets = apply { - this.signatureTargetSubpacket = signatureTarget + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.SIGNATURE_TARGET) + signatureTarget?.let { + subpacketsGenerator.setSignatureTarget( + it.isCritical, it.publicKeyAlgorithm, it.hashAlgorithm, it.hashData) + } } override fun setTrust(depth: Int, amount: Int): SignatureSubpackets = apply { @@ -620,7 +650,8 @@ class SignatureSubpackets : } override fun setTrust(trust: TrustSignature?): SignatureSubpackets = apply { - this.trustSubpacket = trust + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.TRUST_SIG) + trust?.let { subpacketsGenerator.setTrust(it.isCritical, it.depth, it.trustAmount) } } override fun addEmbeddedSignature(signature: PGPSignature): SignatureSubpackets = apply { @@ -631,27 +662,19 @@ class SignatureSubpackets : isCritical: Boolean, signature: PGPSignature ): SignatureSubpackets = apply { - val sig = signature.encoded - val data = - if (sig.size - 1 > 256) { - ByteArray(sig.size - 3) - } else { - ByteArray(sig.size - 2) - } - System.arraycopy(sig, sig.size - data.size, data, 0, data.size) - addEmbeddedSignature(EmbeddedSignature(isCritical, false, data)) + subpacketsGenerator.addEmbeddedSignature(isCritical, signature) } override fun addEmbeddedSignature(embeddedSignature: EmbeddedSignature): SignatureSubpackets = apply { - (this.embeddedSignatureSubpackets as MutableList).add(embeddedSignature) + subpacketsGenerator.addCustomSubpacket(embeddedSignature) } override fun clearEmbeddedSignatures(): SignatureSubpackets = apply { - (this.embeddedSignatureSubpackets as MutableList).clear() + subpacketsGenerator.removePacketsOfType(SignatureSubpacketTags.EMBEDDED_SIGNATURE) } fun addResidualSubpacket( subpacket: org.bouncycastle.bcpg.SignatureSubpacket - ): SignatureSubpackets = apply { (residualSubpackets as MutableList).add(subpacket) } + ): SignatureSubpackets = apply { subpacketsGenerator.addCustomSubpacket(subpacket) } } diff --git a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SignatureSubpacketsHelper.kt b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SignatureSubpacketsHelper.kt index 8a6c16bf..ceb484d3 100644 --- a/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SignatureSubpacketsHelper.kt +++ b/pgpainless-core/src/main/kotlin/org/pgpainless/signature/subpackets/SignatureSubpacketsHelper.kt @@ -5,7 +5,6 @@ package org.pgpainless.signature.subpackets import org.bouncycastle.bcpg.sig.* -import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator import org.bouncycastle.openpgp.PGPSignatureSubpacketVector import org.pgpainless.algorithm.* import org.pgpainless.key.util.RevocationAttributes @@ -116,11 +115,11 @@ class SignatureSubpacketsHelper { } SignatureSubpacket.embeddedSignature -> (subpacket as EmbeddedSignature).let { - subpackets.addEmbeddedSignature(it) + subpackets.addResidualSubpacket(it) } SignatureSubpacket.intendedRecipientFingerprint -> (subpacket as IntendedRecipientFingerprint).let { - subpackets.addIntendedRecipientFingerprint(it) + subpackets.addResidualSubpacket(it) } SignatureSubpacket.policyUrl -> (subpacket as PolicyURI).let { subpackets.setPolicyUrl(it) } @@ -139,72 +138,14 @@ class SignatureSubpacketsHelper { } } - @JvmStatic - fun applyTo( - subpackets: SignatureSubpackets, - generator: PGPSignatureSubpacketGenerator - ): PGPSignatureSubpacketGenerator { - return generator.apply { - addSubpacket(subpackets.issuerKeyIdSubpacket) - addSubpacket(subpackets.issuerFingerprintSubpacket) - addSubpacket(subpackets.signatureCreationTimeSubpacket) - addSubpacket(subpackets.signatureExpirationTimeSubpacket) - addSubpacket(subpackets.exportableSubpacket) - addSubpacket(subpackets.policyURISubpacket) - addSubpacket(subpackets.regularExpressionSubpacket) - for (notation in subpackets.notationDataSubpackets) { - addSubpacket(notation) - } - for (recipient in subpackets.intendedRecipientFingerprintSubpackets) { - addSubpacket(recipient) - } - for (revocationKey in subpackets.revocationKeySubpackets) { - addSubpacket(revocationKey) - } - addSubpacket(subpackets.signatureTargetSubpacket) - addSubpacket(subpackets.featuresSubpacket) - addSubpacket(subpackets.keyFlagsSubpacket) - addSubpacket(subpackets.trustSubpacket) - addSubpacket(subpackets.preferredCompressionAlgorithmsSubpacket) - addSubpacket(subpackets.preferredSymmetricKeyAlgorithmsSubpacket) - addSubpacket(subpackets.preferredHashAlgorithmsSubpacket) - for (embedded in subpackets.embeddedSignatureSubpackets) { - addSubpacket(embedded) - } - addSubpacket(subpackets.signerUserIdSubpacket) - addSubpacket(subpackets.keyExpirationTimeSubpacket) - addSubpacket(subpackets.primaryUserIdSubpacket) - addSubpacket(subpackets.revocableSubpacket) - addSubpacket(subpackets.revocationReasonSubpacket) - for (residual in subpackets.residualSubpackets) { - addSubpacket(residual) - } - } - } - - @JvmStatic - private fun PGPSignatureSubpacketGenerator.addSubpacket( - subpacket: org.bouncycastle.bcpg.SignatureSubpacket? - ) { - if (subpacket != null) { - this.addCustomSubpacket(subpacket) - } - } - @JvmStatic fun toVector(subpackets: SignatureSubpackets): PGPSignatureSubpacketVector { - return PGPSignatureSubpacketGenerator().let { - applyTo(subpackets, it) - it.generate() - } + return subpackets.subpacketsGenerator.generate() } @JvmStatic fun toVector(subpackets: RevocationSignatureSubpackets): PGPSignatureSubpacketVector { - return PGPSignatureSubpacketGenerator().let { - applyTo(subpackets as SignatureSubpackets, it) - it.generate() - } + return (subpackets as SignatureSubpackets).subpacketsGenerator.generate() } } }