11package org .bouncycastle .cms .test ;
22
33import java .io .ByteArrayOutputStream ;
4+ import java .io .InputStream ;
45import java .io .OutputStream ;
56import java .security .KeyPair ;
67import java .security .cert .X509Certificate ;
2425import org .bouncycastle .cms .CMSAlgorithm ;
2526import org .bouncycastle .cms .CMSAttributeTableGenerationException ;
2627import org .bouncycastle .cms .CMSAttributeTableGenerator ;
27- import org .bouncycastle .cms .CMSAuthEnvelopedDataStreamGenerator ;
2828import org .bouncycastle .cms .CMSAuthEnvelopedDataParser ;
29+ import org .bouncycastle .cms .CMSAuthEnvelopedDataStreamGenerator ;
2930import org .bouncycastle .cms .CMSTypedStream ;
3031import org .bouncycastle .cms .RecipientInformation ;
3132import 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