Skip to content

Commit ddf8b96

Browse files
committed
Document v6 signature tests
1 parent 6094412 commit ddf8b96

File tree

1 file changed

+53
-13
lines changed

1 file changed

+53
-13
lines changed

pg/src/test/java/org/bouncycastle/openpgp/test/PGPv6SignatureTest.java

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
import org.bouncycastle.bcpg.BCPGOutputStream;
77
import org.bouncycastle.bcpg.HashAlgorithmTags;
88
import org.bouncycastle.bcpg.PacketFormat;
9-
import org.bouncycastle.bcpg.PublicKeyAlgorithmTags;
10-
import org.bouncycastle.bcpg.SignaturePacket;
119
import org.bouncycastle.bcpg.SignatureSubpacket;
1210
import org.bouncycastle.bcpg.SignatureSubpacketTags;
1311
import org.bouncycastle.bcpg.sig.IssuerFingerprint;
@@ -27,7 +25,6 @@
2725
import org.bouncycastle.openpgp.PGPSignature;
2826
import org.bouncycastle.openpgp.PGPSignatureGenerator;
2927
import org.bouncycastle.openpgp.PGPSignatureList;
30-
import org.bouncycastle.openpgp.PGPSignatureSubpacketGenerator;
3128
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
3229
import org.bouncycastle.openpgp.operator.bc.BcPGPContentSignerBuilder;
3330
import org.bouncycastle.openpgp.operator.bc.BcPGPContentVerifierBuilderProvider;
@@ -41,13 +38,12 @@
4138
import java.io.InputStream;
4239
import java.io.OutputStream;
4340
import java.nio.charset.StandardCharsets;
44-
import java.util.Date;
4541
import java.util.Iterator;
4642

4743
public class PGPv6SignatureTest
4844
extends AbstractPacketTest
4945
{
50-
46+
// https://www.rfc-editor.org/rfc/rfc9580.html#name-sample-version-6-certificat
5147
private static final String ARMORED_CERT = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
5248
"\n" +
5349
"xioGY4d/4xsAAAAg+U2nu0jWCmHlZ3BqZYfQMxmZu52JGggkLq2EVD34laPCsQYf\n" +
@@ -60,6 +56,7 @@ public class PGPv6SignatureTest
6056
"j+VjFM21J0hqWlEg+bdiojWnKfA5AQpWUWtnNwDEM0g12vYxoWM8Y81W+bHBw805\n" +
6157
"I8kWVkXU6vFOi+HWvv/ira7ofJu16NnoUkhclkUrk0mXubZvyl4GBg==\n" +
6258
"-----END PGP PUBLIC KEY BLOCK-----";
59+
// https://www.rfc-editor.org/rfc/rfc9580.html#name-sample-version-6-secret-key
6360
private static final String ARMORED_KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
6461
"\n" +
6562
"xUsGY4d/4xsAAAAg+U2nu0jWCmHlZ3BqZYfQMxmZu52JGggkLq2EVD34laMAGXKB\n" +
@@ -85,13 +82,13 @@ public String getName()
8582
public void performTest()
8683
throws Exception
8784
{
88-
verifyV6DirectKeySignatureTestVector();
85+
verifySignatureOnTestKey();
8986

90-
verifyV6BinarySignature();
87+
verifyV6DetachedSignature();
9188
verifyV6InlineSignature();
9289
verifyV6CleartextSignature();
9390

94-
generateAndVerifyV6BinarySignature();
91+
generateAndVerifyV6DetachedSignature();
9592
generateAndVerifyV6InlineSignature();
9693
generateAndVerifyV6CleartextSignature();
9794

@@ -102,10 +99,13 @@ public void performTest()
10299
verifySignaturesOnEd448X448Key();
103100
generateAndVerifyInlineSignatureUsingRSAKey();
104101

105-
testVerificationOfV3SigWithV6KeyFails();
102+
testVerificationOfV4SigWithV6KeyFails();
106103
}
107104

108-
private void verifyV6DirectKeySignatureTestVector()
105+
/**
106+
* Verify that the known-good key signatures on the minimal test key verify properly.
107+
*/
108+
private void verifySignatureOnTestKey()
109109
throws IOException, PGPException
110110
{
111111
ByteArrayInputStream bIn = new ByteArrayInputStream(ARMORED_CERT.getBytes(StandardCharsets.UTF_8));
@@ -129,7 +129,10 @@ private void verifyV6DirectKeySignatureTestVector()
129129
subkeyBinding.verifyCertification(primaryKey, subkey));
130130
}
131131

132-
private void verifyV6BinarySignature()
132+
/**
133+
* Verify that a good v6 detached signature is verified properly.
134+
*/
135+
private void verifyV6DetachedSignature()
133136
throws IOException, PGPException
134137
{
135138
String msg = "Hello, World!\n";
@@ -161,6 +164,9 @@ private void verifyV6BinarySignature()
161164
binarySig.verify());
162165
}
163166

167+
/**
168+
* Verify that a good v6 inline signature is verified properly.
169+
*/
164170
private void verifyV6InlineSignature()
165171
throws IOException, PGPException
166172
{
@@ -203,6 +209,9 @@ private void verifyV6InlineSignature()
203209
isTrue("Verifying OPS signature MUST succeed", ops.verify(sig));
204210
}
205211

212+
/**
213+
* Verify that a good v6 cleartext signature is verified properly.
214+
*/
206215
private void verifyV6CleartextSignature()
207216
throws IOException, PGPException
208217
{
@@ -247,6 +256,10 @@ private void verifyV6CleartextSignature()
247256
isTrue("Cleartext Signature MUST verify successfully", sig.verify());
248257
}
249258

259+
/**
260+
* A v6 signature with too few salt bytes.
261+
* This test verifies that the signature is properly rejected.
262+
*/
250263
private void verifyingSignatureWithMismatchedSaltSizeFails()
251264
throws IOException
252265
{
@@ -285,6 +298,10 @@ private void verifyingSignatureWithMismatchedSaltSizeFails()
285298
}
286299
}
287300

301+
/**
302+
* Verify that a OPS signature where the length of the salt array does not match the expectations
303+
* is rejected properly.
304+
*/
288305
private void verifyingOPSWithMismatchedSaltSizeFails()
289306
throws IOException
290307
{
@@ -325,6 +342,10 @@ private void verifyingOPSWithMismatchedSaltSizeFails()
325342
}
326343
}
327344

345+
/**
346+
* Test verifying that an inline signature where the salt of the OPS packet mismatches that of the signature
347+
* is rejected properly.
348+
*/
328349
private void verifyingInlineSignatureWithSignatureSaltValueMismatchFails()
329350
throws IOException, PGPException
330351
{
@@ -378,6 +399,9 @@ private void verifyingInlineSignatureWithSignatureSaltValueMismatchFails()
378399
}
379400
}
380401

402+
/**
403+
* Verify self signatures on a v6 Ed448/X448 key.
404+
*/
381405
private void verifySignaturesOnEd448X448Key()
382406
throws PGPException, IOException
383407
{
@@ -518,7 +542,10 @@ private PGPPublicKey getSigningKeyFor(PGPKeyRing keys, PGPSignature sig)
518542
return null;
519543
}
520544

521-
private void generateAndVerifyV6BinarySignature()
545+
/**
546+
* Generate and verify a detached v6 signature using the v6 test key.
547+
*/
548+
private void generateAndVerifyV6DetachedSignature()
522549
throws IOException, PGPException
523550
{
524551
String msg = "Hello, World!\n";
@@ -547,6 +574,9 @@ private void generateAndVerifyV6BinarySignature()
547574
binarySig.verify());
548575
}
549576

577+
/**
578+
* Generate and verify a v6 inline signature using the v6 test key.
579+
*/
550580
private void generateAndVerifyV6InlineSignature()
551581
throws IOException, PGPException
552582
{
@@ -610,6 +640,9 @@ private void generateAndVerifyV6InlineSignature()
610640
isTrue("Generated Inline OPS signature MUST verify successful", ops.verify(sig));
611641
}
612642

643+
/**
644+
* Generate and verify a v6 signature using the cleartext signature framework and the v6 test key.
645+
*/
613646
private void generateAndVerifyV6CleartextSignature()
614647
throws IOException, PGPException
615648
{
@@ -677,6 +710,9 @@ private void generateAndVerifyV6CleartextSignature()
677710
isTrue("Generated Cleartext Signature MUST verify successfully", v);
678711
}
679712

713+
/**
714+
* Generate and verify an inline text signature using a v6 RSA key.
715+
*/
680716
private void generateAndVerifyInlineSignatureUsingRSAKey()
681717
throws PGPException, IOException
682718
{
@@ -787,7 +823,11 @@ private void generateAndVerifyInlineSignatureUsingRSAKey()
787823
isTrue("V6 inline sig made using RSA key MUST verify", ops.verify(sig));
788824
}
789825

790-
private void testVerificationOfV3SigWithV6KeyFails()
826+
/**
827+
* A version 4 signature generated using the v6 key.
828+
* This test verifies that the signature is properly rejected.
829+
*/
830+
private void testVerificationOfV4SigWithV6KeyFails()
791831
throws IOException
792832
{
793833
ByteArrayInputStream bIn = new ByteArrayInputStream(ARMORED_KEY.getBytes(StandardCharsets.UTF_8));

0 commit comments

Comments
 (0)