Skip to content

Commit 33ce50e

Browse files
committed
added zero length message test - relates to github #2139
1 parent 11cea86 commit 33ce50e

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

pkix/src/test/java/org/bouncycastle/cms/test/CMSAuthEnvelopedDataStreamGeneratorTest.java

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.bouncycastle.cms.test;
22

33
import java.io.ByteArrayOutputStream;
4+
import java.io.InputStream;
45
import java.io.OutputStream;
56
import java.security.KeyPair;
67
import java.security.cert.X509Certificate;
@@ -24,8 +25,8 @@
2425
import org.bouncycastle.cms.CMSAlgorithm;
2526
import org.bouncycastle.cms.CMSAttributeTableGenerationException;
2627
import org.bouncycastle.cms.CMSAttributeTableGenerator;
27-
import org.bouncycastle.cms.CMSAuthEnvelopedDataStreamGenerator;
2828
import org.bouncycastle.cms.CMSAuthEnvelopedDataParser;
29+
import org.bouncycastle.cms.CMSAuthEnvelopedDataStreamGenerator;
2930
import org.bouncycastle.cms.CMSTypedStream;
3031
import org.bouncycastle.cms.RecipientInformation;
3132
import org.bouncycastle.cms.RecipientInformationStore;
@@ -111,6 +112,16 @@ public void setUp()
111112
init();
112113
}
113114

115+
public void testGCMCCMZeroLength()
116+
throws Exception
117+
{
118+
GCMCCMtest(CMSAlgorithm.AES128_GCM, false, new byte[0]);
119+
GCMCCMtest(CMSAlgorithm.AES128_GCM, true, new byte[0]);
120+
121+
GCMCCMtest(CMSAlgorithm.AES128_CCM, false, new byte[0]);
122+
GCMCCMtest(CMSAlgorithm.AES128_CCM, true, new byte[0]);
123+
}
124+
114125
public void testGCMCCM()
115126
throws Exception
116127
{
@@ -129,14 +140,19 @@ public void testGCMCCM()
129140
GCMCCMtest(CMSAlgorithm.AES256_CCM, true);
130141
}
131142

132-
public void GCMCCMtest(ASN1ObjectIdentifier oid, boolean berEncodeRecipientSet)
143+
private void GCMCCMtest(ASN1ObjectIdentifier oid, boolean berEncodeRecipientSet)
144+
throws Exception
145+
{
146+
GCMCCMtest(oid, berEncodeRecipientSet, Strings.toByteArray("Hello, world!"));
147+
}
148+
149+
private void GCMCCMtest(ASN1ObjectIdentifier oid, boolean berEncodeRecipientSet, byte[] message)
133150
throws Exception
134151
{
135152
if (!CMSTestUtil.isAeadAvailable())
136153
{
137154
return;
138155
}
139-
byte[] message = Strings.toByteArray("Hello, world!");
140156

141157
OutputEncryptor candidate = new JceCMSContentEncryptorBuilder(oid).setProvider(BC).build();
142158

@@ -209,6 +225,35 @@ public AttributeTable getAttributes(Map parameters)
209225
assertEquals(1, ep.getUnauthAttrs().size());
210226
}
211227
ep.close();
228+
229+
// alternate read approach
230+
ep = new CMSAuthEnvelopedDataParser(bOut.toByteArray());
231+
232+
recipients = ep.getRecipientInfos();
233+
234+
c = recipients.getRecipients();
235+
236+
it = c.iterator();
237+
238+
while (it.hasNext())
239+
{
240+
RecipientInformation recipient = (RecipientInformation)it.next();
241+
242+
assertEquals(recipient.getKeyEncryptionAlgOID(), "1.2.840.113549.1.1.1");
243+
244+
CMSTypedStream recData = recipient.getContentStream(new JceKeyTransAuthEnvelopedRecipient(_reciKP.getPrivate()).setProvider(BC));
245+
246+
byte[] buf = new byte[message.length];
247+
248+
InputStream contentStream = recData.getContentStream();
249+
250+
contentStream.read(buf);
251+
contentStream.close();
252+
253+
assertEquals(true, Arrays.equals(message, buf));
254+
assertTrue(Arrays.equals(ep.getMac(), recipient.getMac()));
255+
}
256+
ep.close();
212257
}
213258

214259
public void testNoAuthAttributes()

0 commit comments

Comments
 (0)