1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-26 14:22:05 +01:00

Adapt changes to PGPSignatureGenerator

This commit is contained in:
Paul Schaub 2024-06-30 18:55:27 +02:00
parent e2cf1a474e
commit a230c48393
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
6 changed files with 18 additions and 11 deletions

View file

@ -7,6 +7,7 @@ package org.pgpainless.encryption_signing
import java.security.MessageDigest import java.security.MessageDigest
import org.bouncycastle.openpgp.PGPException import org.bouncycastle.openpgp.PGPException
import org.bouncycastle.openpgp.PGPPrivateKey import org.bouncycastle.openpgp.PGPPrivateKey
import org.bouncycastle.openpgp.PGPPublicKey
import org.bouncycastle.openpgp.PGPSecretKeyRing import org.bouncycastle.openpgp.PGPSecretKeyRing
import org.bouncycastle.openpgp.PGPSignature import org.bouncycastle.openpgp.PGPSignature
import org.bouncycastle.openpgp.PGPSignatureGenerator import org.bouncycastle.openpgp.PGPSignatureGenerator
@ -29,7 +30,9 @@ class BcHashContextSigner {
return info.signingSubkeys return info.signingSubkeys
.mapNotNull { info.getSecretKey(it.keyID) } .mapNotNull { info.getSecretKey(it.keyID) }
.firstOrNull() .firstOrNull()
?.let { signHashContext(hashContext, signatureType, it.unlock(protector)) } ?.let {
signHashContext(hashContext, signatureType, it.unlock(protector), it.publicKey)
}
?: throw PGPException("Key does not contain suitable signing subkey.") ?: throw PGPException("Key does not contain suitable signing subkey.")
} }
@ -45,9 +48,11 @@ class BcHashContextSigner {
internal fun signHashContext( internal fun signHashContext(
hashContext: MessageDigest, hashContext: MessageDigest,
signatureType: SignatureType, signatureType: SignatureType,
privateKey: PGPPrivateKey privateKey: PGPPrivateKey,
publicKey: PGPPublicKey
): PGPSignature { ): PGPSignature {
return PGPSignatureGenerator(BcPGPHashContextContentSignerBuilder(hashContext)) return PGPSignatureGenerator(
BcPGPHashContextContentSignerBuilder(hashContext), publicKey)
.apply { init(signatureType.code, privateKey) } .apply { init(signatureType.code, privateKey) }
.generate() .generate()
} }

View file

@ -385,7 +385,7 @@ class SigningOptions {
val generator: PGPSignatureGenerator = val generator: PGPSignatureGenerator =
createSignatureGenerator( createSignatureGenerator(
signingSubkey, signingSubkey,
signingKey.getPublicKey(signingSubkey.keyID).version, signingKey.getPublicKey(signingSubkey.keyID),
hashAlgorithm, hashAlgorithm,
signatureType) signatureType)
@ -429,7 +429,7 @@ class SigningOptions {
@Throws(PGPException::class) @Throws(PGPException::class)
private fun createSignatureGenerator( private fun createSignatureGenerator(
privateKey: PGPPrivateKey, privateKey: PGPPrivateKey,
signatureVersion: Int, publicKey: PGPPublicKey,
hashAlgorithm: HashAlgorithm, hashAlgorithm: HashAlgorithm,
signatureType: DocumentSignatureType signatureType: DocumentSignatureType
): PGPSignatureGenerator { ): PGPSignatureGenerator {
@ -437,7 +437,7 @@ class SigningOptions {
.getPGPContentSignerBuilder( .getPGPContentSignerBuilder(
privateKey.publicKeyPacket.algorithm, hashAlgorithm.algorithmId) privateKey.publicKeyPacket.algorithm, hashAlgorithm.algorithmId)
.let { csb -> .let { csb ->
PGPSignatureGenerator(csb, signatureVersion).also { PGPSignatureGenerator(csb, publicKey).also {
it.init(signatureType.signatureType.code, privateKey) it.init(signatureType.signatureType.code, privateKey)
} }
} }

View file

@ -94,7 +94,7 @@ class KeyRingBuilder : KeyRingBuilderInterface<KeyRingBuilder> {
requireNotNull(primaryKeySpec) { "Primary Key spec required." } requireNotNull(primaryKeySpec) { "Primary Key spec required." }
val certKey = generateKeyPair(primaryKeySpec!!) val certKey = generateKeyPair(primaryKeySpec!!)
val signer = buildContentSigner(certKey) val signer = buildContentSigner(certKey)
val signatureGenerator = PGPSignatureGenerator(signer) val signatureGenerator = PGPSignatureGenerator(signer, certKey.publicKey)
val hashedSubPacketGenerator = primaryKeySpec!!.subpacketGenerator val hashedSubPacketGenerator = primaryKeySpec!!.subpacketGenerator
hashedSubPacketGenerator.setIssuerFingerprintAndKeyId(certKey.publicKey) hashedSubPacketGenerator.setIssuerFingerprintAndKeyId(certKey.publicKey)
@ -206,7 +206,8 @@ class KeyRingBuilder : KeyRingBuilderInterface<KeyRingBuilder> {
return hashedSubpackets return hashedSubpackets
} }
val bindingSignatureGenerator = PGPSignatureGenerator(buildContentSigner(subKey)) val bindingSignatureGenerator =
PGPSignatureGenerator(buildContentSigner(subKey), subKey.publicKey)
bindingSignatureGenerator.init(SignatureType.PRIMARYKEY_BINDING.code, subKey.privateKey) bindingSignatureGenerator.init(SignatureType.PRIMARYKEY_BINDING.code, subKey.privateKey)
val primaryKeyBindingSig = val primaryKeyBindingSig =
bindingSignatureGenerator.generateCertification(primaryKey.publicKey, subKey.publicKey) bindingSignatureGenerator.generateCertification(primaryKey.publicKey, subKey.publicKey)

View file

@ -226,7 +226,8 @@ class PublicKeyParameterValidationUtil {
PGPSignatureGenerator( PGPSignatureGenerator(
getInstance() getInstance()
.getPGPContentSignerBuilder( .getPGPContentSignerBuilder(
requireFromId(publicKey.algorithm), HashAlgorithm.SHA256)) requireFromId(publicKey.algorithm), HashAlgorithm.SHA256),
publicKey)
return try { return try {
signatureGenerator signatureGenerator
.apply { .apply {

View file

@ -111,7 +111,8 @@ abstract class AbstractSignatureBuilder<B : AbstractSignatureBuilder<B>>(
PGPSignatureGenerator( PGPSignatureGenerator(
ImplementationFactory.getInstance() ImplementationFactory.getInstance()
.getPGPContentSignerBuilder( .getPGPContentSignerBuilder(
publicSigningKey.algorithm, hashAlgorithm.algorithmId)) publicSigningKey.algorithm, hashAlgorithm.algorithmId),
publicSigningKey)
.apply { .apply {
setUnhashedSubpackets(SignatureSubpacketsHelper.toVector(_unhashedSubpackets)) setUnhashedSubpackets(SignatureSubpacketsHelper.toVector(_unhashedSubpackets))
setHashedSubpackets(SignatureSubpacketsHelper.toVector(_hashedSubpackets)) setHashedSubpackets(SignatureSubpacketsHelper.toVector(_hashedSubpackets))

View file

@ -6,7 +6,6 @@ package org.pgpainless.key
import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertNotNull import org.junit.jupiter.api.Assertions.assertNotNull
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test import org.junit.jupiter.api.Test
import org.pgpainless.PGPainless import org.pgpainless.PGPainless