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

Port BcHashContextSigner and test

This commit is contained in:
Paul Schaub 2025-04-02 20:05:12 +02:00
parent 0963f110a4
commit 46d58f230e
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 24 additions and 20 deletions

View file

@ -4,16 +4,15 @@
package org.pgpainless.encryption_signing package org.pgpainless.encryption_signing
import java.security.MessageDigest
import org.bouncycastle.openpgp.PGPException import org.bouncycastle.openpgp.PGPException
import org.bouncycastle.openpgp.PGPPrivateKey
import org.bouncycastle.openpgp.PGPSecretKeyRing
import org.bouncycastle.openpgp.PGPSignature
import org.bouncycastle.openpgp.PGPSignatureGenerator import org.bouncycastle.openpgp.PGPSignatureGenerator
import org.bouncycastle.openpgp.api.OpenPGPKey
import org.bouncycastle.openpgp.api.OpenPGPSignature.OpenPGPDocumentSignature
import org.pgpainless.PGPainless import org.pgpainless.PGPainless
import org.pgpainless.algorithm.SignatureType import org.pgpainless.algorithm.SignatureType
import org.pgpainless.bouncycastle.extensions.unlock
import org.pgpainless.key.protection.SecretKeyRingProtector import org.pgpainless.key.protection.SecretKeyRingProtector
import org.pgpainless.key.protection.UnlockSecretKey
import java.security.MessageDigest
class BcHashContextSigner { class BcHashContextSigner {
@ -22,15 +21,15 @@ class BcHashContextSigner {
fun signHashContext( fun signHashContext(
hashContext: MessageDigest, hashContext: MessageDigest,
signatureType: SignatureType, signatureType: SignatureType,
secretKey: PGPSecretKeyRing, secretKey: OpenPGPKey,
protector: SecretKeyRingProtector protector: SecretKeyRingProtector
): PGPSignature { ): OpenPGPDocumentSignature {
val info = PGPainless.inspectKeyRing(secretKey) val info = PGPainless.getInstance().inspect(secretKey)
return info.signingSubkeys return info.signingSubkeys
.mapNotNull { info.getSecretKey(it.keyIdentifier) } .mapNotNull { info.getSecretKey(it.keyIdentifier) }
.firstOrNull() .firstOrNull()
?.let { ?.let {
signHashContext(hashContext, signatureType, it.pgpSecretKey.unlock(protector)) signHashContext(hashContext, signatureType, UnlockSecretKey.unlockSecretKey(it, protector))
} }
?: throw PGPException("Key does not contain suitable signing subkey.") ?: throw PGPException("Key does not contain suitable signing subkey.")
} }
@ -47,11 +46,14 @@ class BcHashContextSigner {
internal fun signHashContext( internal fun signHashContext(
hashContext: MessageDigest, hashContext: MessageDigest,
signatureType: SignatureType, signatureType: SignatureType,
privateKey: PGPPrivateKey privateKey: OpenPGPKey.OpenPGPPrivateKey
): PGPSignature { ): OpenPGPDocumentSignature {
return PGPSignatureGenerator(BcPGPHashContextContentSignerBuilder(hashContext)) return PGPSignatureGenerator(
.apply { init(signatureType.code, privateKey) } BcPGPHashContextContentSignerBuilder(hashContext),
privateKey.keyPair.publicKey)
.apply { init(signatureType.code, privateKey.keyPair.privateKey) }
.generate() .generate()
.let { OpenPGPDocumentSignature(it, privateKey.publicKey) }
} }
} }
} }

View file

@ -16,9 +16,9 @@ import java.security.NoSuchAlgorithmException;
import org.bouncycastle.jce.provider.BouncyCastleProvider; import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.api.OpenPGPCertificate; import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.api.OpenPGPKey; import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.openpgp.api.OpenPGPSignature;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
@ -66,13 +66,15 @@ public class BcHashContextSignerTest {
@Test @Test
public void signContextWithRSAKeys() throws PGPException, NoSuchAlgorithmException, IOException { public void signContextWithRSAKeys() throws PGPException, NoSuchAlgorithmException, IOException {
OpenPGPKey secretKeys = PGPainless.generateKeyRing().simpleRsaKeyRing("Sigfried", RsaLength._3072); OpenPGPKey secretKeys = PGPainless.getInstance().generateKey()
.simpleRsaKeyRing("Sigfried", RsaLength._3072);
signWithKeys(secretKeys); signWithKeys(secretKeys);
} }
@Test @Test
public void signContextWithEcKeys() throws PGPException, NoSuchAlgorithmException, IOException { public void signContextWithEcKeys() throws PGPException, NoSuchAlgorithmException, IOException {
OpenPGPKey secretKeys = PGPainless.generateKeyRing().simpleEcKeyRing("Sigfried"); OpenPGPKey secretKeys = PGPainless.getInstance().generateKey()
.simpleEcKeyRing("Sigfried");
signWithKeys(secretKeys); signWithKeys(secretKeys);
} }
@ -91,8 +93,8 @@ public class BcHashContextSignerTest {
byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8); byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
ByteArrayInputStream messageIn = new ByteArrayInputStream(messageBytes); ByteArrayInputStream messageIn = new ByteArrayInputStream(messageBytes);
PGPSignature signature = signMessage(messageBytes, hashAlgorithm, secretKeys); OpenPGPSignature.OpenPGPDocumentSignature signature = signMessage(messageBytes, hashAlgorithm, secretKeys);
assertEquals(hashAlgorithm.getAlgorithmId(), signature.getHashAlgorithm()); assertEquals(hashAlgorithm.getAlgorithmId(), signature.getSignature().getHashAlgorithm());
DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify() DecryptionStream decryptionStream = PGPainless.decryptAndOrVerify()
.onInputStream(messageIn) .onInputStream(messageIn)
@ -108,13 +110,13 @@ public class BcHashContextSignerTest {
assertTrue(metadata.isVerifiedSigned()); assertTrue(metadata.isVerifiedSigned());
} }
private PGPSignature signMessage(byte[] message, HashAlgorithm hashAlgorithm, OpenPGPKey secretKeys) private OpenPGPSignature.OpenPGPDocumentSignature signMessage(byte[] message, HashAlgorithm hashAlgorithm, OpenPGPKey secretKeys)
throws NoSuchAlgorithmException { throws NoSuchAlgorithmException {
// Prepare the hash context // Prepare the hash context
// This would be done by the caller application // This would be done by the caller application
MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm.getAlgorithmName(), new BouncyCastleProvider()); MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm.getAlgorithmName(), new BouncyCastleProvider());
messageDigest.update(message); messageDigest.update(message);
return BcHashContextSigner.signHashContext(messageDigest, SignatureType.BINARY_DOCUMENT, secretKeys.getPGPSecretKeyRing(), SecretKeyRingProtector.unprotectedKeys()); return BcHashContextSigner.signHashContext(messageDigest, SignatureType.BINARY_DOCUMENT, secretKeys, SecretKeyRingProtector.unprotectedKeys());
} }
} }