Skip to content

Commit e7f8a72

Browse files
committed
Improve encapsulatedTest
1 parent 3205bb1 commit e7f8a72

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

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

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ public class NewSignedDataTest
705705
noParams.add(EdECObjectIdentifiers.id_Ed25519);
706706
noParams.add(EdECObjectIdentifiers.id_Ed448);
707707
}
708-
708+
709709
public NewSignedDataTest(String name)
710710
{
711711
super(name);
@@ -2492,55 +2492,57 @@ private void encapsulatedTest(
24922492
X509Certificate signatureCert,
24932493
String signatureAlgorithm,
24942494
ASN1ObjectIdentifier sigAlgOid,
2495-
AlgorithmIdentifier digAlgId)
2495+
AlgorithmIdentifier expectedDigAlgId)
24962496
throws Exception
24972497
{
2498-
List certList = new ArrayList();
2499-
List crlList = new ArrayList();
2500-
CMSTypedData msg = new CMSProcessableByteArray("Hello World!".getBytes());
2501-
2498+
CMSTypedData msg = new CMSProcessableByteArray("Hello World!".getBytes());
2499+
2500+
List certList = new ArrayList();
2501+
List crlList = new ArrayList();
2502+
25022503
certList.add(signatureCert);
25032504
certList.add(_origCert);
25042505

25052506
crlList.add(_signCrl);
25062507

2507-
Store certs = new JcaCertStore(certList);
2508-
Store crlStore = new JcaCRLStore(crlList);
2508+
Store certStore = new JcaCertStore(certList);
2509+
Store crlStore = new JcaCRLStore(crlList);
25092510

25102511
CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
25112512

25122513
ContentSigner contentSigner = new JcaContentSignerBuilder(signatureAlgorithm).setProvider(BC).build(signaturePair.getPrivate());
25132514

2514-
gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider(BC).build()).build(contentSigner, signatureCert));
2515+
DigestCalculatorProvider digCalcProv = new JcaDigestCalculatorProviderBuilder().setProvider(BC).build();
25152516

2516-
gen.addCertificates(certs);
2517-
2518-
CMSSignedData s = gen.generate(msg, true);
2517+
gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(digCalcProv).build(contentSigner, signatureCert));
25192518

2520-
ByteArrayInputStream bIn = new ByteArrayInputStream(s.getEncoded());
2521-
ASN1InputStream aIn = new ASN1InputStream(bIn);
2519+
gen.addCertificates(certStore);
2520+
gen.addCRLs(crlStore);
25222521

2523-
s = new CMSSignedData(ContentInfo.getInstance(aIn.readObject()));
2522+
CMSSignedData s = gen.generate(msg, true);
2523+
2524+
s = new CMSSignedData(ContentInfo.getInstance(s.getEncoded()));
25242525

25252526
Set digestAlgorithms = new HashSet(s.getDigestAlgorithmIDs());
25262527

25272528
assertTrue(digestAlgorithms.size() > 0);
25282529

2529-
if (digAlgId != null)
2530+
if (expectedDigAlgId != null)
25302531
{
2531-
assertTrue(digestAlgorithms.contains(digAlgId));
2532+
assertTrue(digestAlgorithms.contains(expectedDigAlgId));
25322533
}
25332534

2534-
certs = s.getCertificates();
2535-
2535+
certStore = s.getCertificates();
2536+
crlStore = s.getCRLs();
2537+
25362538
SignerInformationStore signers = s.getSignerInfos();
25372539
Collection c = signers.getSigners();
25382540
Iterator it = c.iterator();
25392541

25402542
while (it.hasNext())
25412543
{
25422544
SignerInformation signer = (SignerInformation)it.next();
2543-
Collection certCollection = certs.getMatches(signer.getSID());
2545+
Collection certCollection = certStore.getMatches(signer.getSID());
25442546

25452547
Iterator certIt = certCollection.iterator();
25462548
X509CertificateHolder cert = (X509CertificateHolder)certIt.next();
@@ -2592,26 +2594,25 @@ private void encapsulatedTest(
25922594
gen = new CMSSignedDataGenerator();
25932595

25942596
gen.addSigners(s.getSignerInfos());
2595-
2597+
25962598
gen.addCertificates(s.getCertificates());
2597-
2599+
gen.addCRLs(s.getCRLs());
2600+
25982601
s = gen.generate(msg, true);
2599-
2600-
bIn = new ByteArrayInputStream(s.getEncoded());
2601-
aIn = new ASN1InputStream(bIn);
2602-
2603-
s = new CMSSignedData(ContentInfo.getInstance(aIn.readObject()));
2604-
2605-
certs = s.getCertificates();
2606-
2602+
2603+
s = new CMSSignedData(ContentInfo.getInstance(s.getEncoded()));
2604+
2605+
certStore = s.getCertificates();
2606+
crlStore = s.getCRLs();
2607+
26072608
signers = s.getSignerInfos();
26082609
c = signers.getSigners();
26092610
it = c.iterator();
26102611

26112612
while (it.hasNext())
26122613
{
26132614
SignerInformation signer = (SignerInformation)it.next();
2614-
Collection certCollection = certs.getMatches(signer.getSID());
2615+
Collection certCollection = certStore.getMatches(signer.getSID());
26152616

26162617
Iterator certIt = certCollection.iterator();
26172618
X509CertificateHolder cert = (X509CertificateHolder)certIt.next();

0 commit comments

Comments
 (0)