1
1
package org .bouncycastle .openpgp .test ;
2
2
3
+ import org .bouncycastle .bcpg .ArmoredInputStream ;
4
+ import org .bouncycastle .bcpg .ArmoredOutputStream ;
5
+ import org .bouncycastle .bcpg .BCPGInputStream ;
6
+ import org .bouncycastle .bcpg .BCPGOutputStream ;
7
+ import org .bouncycastle .bcpg .HashAlgorithmTags ;
8
+ import org .bouncycastle .bcpg .PacketFormat ;
9
+ import org .bouncycastle .bcpg .PublicKeyAlgorithmTags ;
10
+ import org .bouncycastle .crypto .AsymmetricCipherKeyPair ;
11
+ import org .bouncycastle .crypto .CryptoServicesRegistrar ;
12
+ import org .bouncycastle .crypto .generators .Ed25519KeyPairGenerator ;
13
+ import org .bouncycastle .crypto .generators .X25519KeyPairGenerator ;
14
+ import org .bouncycastle .crypto .params .Ed25519KeyGenerationParameters ;
15
+ import org .bouncycastle .crypto .params .X25519KeyGenerationParameters ;
16
+ import org .bouncycastle .openpgp .PGPException ;
17
+ import org .bouncycastle .openpgp .PGPKeyPair ;
3
18
import org .bouncycastle .openpgp .PGPPadding ;
19
+ import org .bouncycastle .openpgp .PGPPublicKeyRing ;
20
+ import org .bouncycastle .openpgp .PGPSecretKey ;
21
+ import org .bouncycastle .openpgp .operator .PGPDigestCalculator ;
22
+ import org .bouncycastle .openpgp .operator .bc .BcKeyFingerprintCalculator ;
23
+ import org .bouncycastle .openpgp .operator .bc .BcPGPDigestCalculatorProvider ;
24
+ import org .bouncycastle .openpgp .operator .bc .BcPGPKeyPair ;
4
25
import org .bouncycastle .util .test .SimpleTest ;
5
26
27
+ import java .io .ByteArrayInputStream ;
28
+ import java .io .ByteArrayOutputStream ;
29
+ import java .io .IOException ;
30
+ import java .util .Arrays ;
31
+ import java .util .Date ;
32
+
6
33
public class PGPPaddingTest
7
34
extends SimpleTest
8
35
{
@@ -20,6 +47,8 @@ public void performTest()
20
47
fixedLenPaddingIsCorrectLength ();
21
48
negativePaddingLengthThrows ();
22
49
zeroPaddingLengthThrows ();
50
+
51
+ parsePaddedCertificate ();
23
52
}
24
53
25
54
private void randomPaddingIsInBounds ()
@@ -50,6 +79,48 @@ private void zeroPaddingLengthThrows()
50
79
testException (null , "IllegalArgumentException" , () -> new PGPPadding (0 ));
51
80
}
52
81
82
+ private void parsePaddedCertificate ()
83
+ throws PGPException , IOException
84
+ {
85
+ PGPDigestCalculator digestCalc = new BcPGPDigestCalculatorProvider ().get (HashAlgorithmTags .SHA1 );
86
+
87
+ Date creationTime = new Date (1000 * (new Date ().getTime () / 1000 ));
88
+ Ed25519KeyPairGenerator edGen = new Ed25519KeyPairGenerator ();
89
+ edGen .init (new Ed25519KeyGenerationParameters (CryptoServicesRegistrar .getSecureRandom ()));
90
+ AsymmetricCipherKeyPair edPair = edGen .generateKeyPair ();
91
+
92
+ X25519KeyPairGenerator xGen = new X25519KeyPairGenerator ();
93
+ xGen .init (new X25519KeyGenerationParameters (CryptoServicesRegistrar .getSecureRandom ()));
94
+ AsymmetricCipherKeyPair xPair = xGen .generateKeyPair ();
95
+
96
+ PGPKeyPair primayKeyPair = new BcPGPKeyPair (PublicKeyAlgorithmTags .Ed25519 , edPair , creationTime );
97
+ PGPKeyPair subKeyPair = new BcPGPKeyPair (PublicKeyAlgorithmTags .X25519 , xPair , creationTime );
98
+
99
+ PGPSecretKey secretPrimaryKey = new PGPSecretKey (primayKeyPair .getPrivateKey (), primayKeyPair .getPublicKey (), digestCalc , true , null );
100
+ PGPSecretKey secretSubKey = new PGPSecretKey (subKeyPair .getPrivateKey (), subKeyPair .getPublicKey (), digestCalc , false , null );
101
+
102
+ PGPPublicKeyRing certificate = new PGPPublicKeyRing (Arrays .asList (secretPrimaryKey .getPublicKey (), secretSubKey .getPublicKey ()));
103
+ PGPPadding padding = new PGPPadding ();
104
+
105
+ ByteArrayOutputStream bOut = new ByteArrayOutputStream ();
106
+ ArmoredOutputStream aOut = ArmoredOutputStream .builder ().clearHeaders ().build (bOut );
107
+ BCPGOutputStream pOut = new BCPGOutputStream (aOut , PacketFormat .CURRENT );
108
+ certificate .encode (pOut );
109
+ padding .encode (pOut );
110
+
111
+ pOut .close ();
112
+ aOut .close ();
113
+
114
+ ByteArrayInputStream bIn = new ByteArrayInputStream (bOut .toByteArray ());
115
+ ArmoredInputStream aIn = new ArmoredInputStream (bIn );
116
+ BCPGInputStream pIn = new BCPGInputStream (aIn );
117
+
118
+ PGPPublicKeyRing parsed = new PGPPublicKeyRing (pIn , new BcKeyFingerprintCalculator ());
119
+ isTrue (org .bouncycastle .util .Arrays .areEqual (
120
+ certificate .getEncoded (PacketFormat .CURRENT ),
121
+ parsed .getEncoded (PacketFormat .CURRENT )));
122
+ }
123
+
53
124
public static void main (String [] args )
54
125
{
55
126
runTest (new PGPPaddingTest ());
0 commit comments