|
32 | 32 | import org.bouncycastle.asn1.cms.CMSAttributes; |
33 | 33 | import org.bouncycastle.asn1.cms.CMSObjectIdentifiers; |
34 | 34 | import org.bouncycastle.asn1.ocsp.OCSPResponse; |
| 35 | +import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers; |
| 36 | +import org.bouncycastle.asn1.x509.AlgorithmIdentifier; |
35 | 37 | import org.bouncycastle.cert.X509AttributeCertificateHolder; |
36 | 38 | import org.bouncycastle.cert.X509CertificateHolder; |
37 | 39 | import org.bouncycastle.cert.jcajce.JcaCRLStore; |
@@ -209,8 +211,45 @@ private void verifySignatures(CMSSignedDataParser sp, byte[] contentDigest) |
209 | 211 | assertEquals(certStore.getMatches(null).size(), sp.getCertificates().getMatches(null).size()); |
210 | 212 | assertEquals(crlStore.getMatches(null).size(), sp.getCRLs().getMatches(null).size()); |
211 | 213 | } |
212 | | - |
213 | | - private void verifySignatures(CMSSignedDataParser sp) |
| 214 | + |
| 215 | + private void verifySignatures(CMSSignedDataParser sp, byte[] contentDigest, boolean ignoreCounterSig) |
| 216 | + throws Exception |
| 217 | + { |
| 218 | + Store certStore = sp.getCertificates(); |
| 219 | + Store crlStore = sp.getCRLs(); |
| 220 | + SignerInformationStore signers = sp.getSignerInfos(); |
| 221 | + |
| 222 | + Set digestIDs = new HashSet(sp.getDigestAlgorithmIDs()); |
| 223 | + |
| 224 | + assertTrue(digestIDs.size() > 0); |
| 225 | + |
| 226 | + Collection c = signers.getSigners(); |
| 227 | + Iterator it = c.iterator(); |
| 228 | + |
| 229 | + while (it.hasNext()) |
| 230 | + { |
| 231 | + SignerInformation signer = (SignerInformation)it.next(); |
| 232 | + Collection certCollection = certStore.getMatches(signer.getSID()); |
| 233 | + |
| 234 | + Iterator certIt = certCollection.iterator(); |
| 235 | + X509CertificateHolder cert = (X509CertificateHolder)certIt.next(); |
| 236 | + |
| 237 | + assertEquals(true, signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider(BC).build(cert))); |
| 238 | + |
| 239 | + digestIDs.remove(signer.getDigestAlgorithmID()); |
| 240 | + |
| 241 | + if (contentDigest != null) |
| 242 | + { |
| 243 | + assertTrue(MessageDigest.isEqual(contentDigest, signer.getContentDigest())); |
| 244 | + } |
| 245 | + } |
| 246 | + |
| 247 | + assertTrue(digestIDs.size() > 0); |
| 248 | + assertEquals(certStore.getMatches(null).size(), sp.getCertificates().getMatches(null).size()); |
| 249 | + assertEquals(crlStore.getMatches(null).size(), sp.getCRLs().getMatches(null).size()); |
| 250 | + } |
| 251 | + |
| 252 | + private void verifySignatures(CMSSignedDataParser sp) |
214 | 253 | throws Exception |
215 | 254 | { |
216 | 255 | verifySignatures(sp, null); |
@@ -364,7 +403,126 @@ public void testDSANoAttributes() |
364 | 403 |
|
365 | 404 | verifySignatures(sp, md.digest(TEST_MESSAGE.getBytes())); |
366 | 405 | } |
367 | | - |
| 406 | + |
| 407 | + public void testAddDigestAlgorithm() |
| 408 | + throws Exception |
| 409 | + { |
| 410 | + List certList = new ArrayList(); |
| 411 | + List crlList = new ArrayList(); |
| 412 | + ByteArrayOutputStream bOut = new ByteArrayOutputStream(); |
| 413 | + |
| 414 | + certList.add(_origCert); |
| 415 | + certList.add(_signCert); |
| 416 | + |
| 417 | + crlList.add(_signCrl); |
| 418 | + crlList.add(_origCrl); |
| 419 | + |
| 420 | + Store certs = new JcaCertStore(certList); |
| 421 | + Store crls = new JcaCRLStore(crlList); |
| 422 | + |
| 423 | + CMSSignedDataStreamGenerator gen = new CMSSignedDataStreamGenerator(); |
| 424 | + |
| 425 | + ContentSigner sha1Signer = new JcaContentSignerBuilder("SHA1withRSA").setProvider(BC).build(_origKP.getPrivate()); |
| 426 | + |
| 427 | + gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(new JcaDigestCalculatorProviderBuilder().setProvider(BC).build()).build(sha1Signer, _origCert)); |
| 428 | + gen.addCertificates(certs); |
| 429 | + |
| 430 | + gen.addCRLs(crls); |
| 431 | + |
| 432 | + Set<AlgorithmIdentifier> oids = new HashSet<AlgorithmIdentifier>(); |
| 433 | + oids.add(new AlgorithmIdentifier(PKCSObjectIdentifiers.sha256WithRSAEncryption)); |
| 434 | + gen.addDigestAlgorithms(oids); |
| 435 | + |
| 436 | + OutputStream sigOut = gen.open(bOut); |
| 437 | + |
| 438 | + sigOut.write(TEST_MESSAGE.getBytes()); |
| 439 | + |
| 440 | + sigOut.close(); |
| 441 | + |
| 442 | + checkSigParseable(bOut.toByteArray()); |
| 443 | + |
| 444 | + CMSSignedDataParser sp = new CMSSignedDataParser(new JcaDigestCalculatorProviderBuilder().setProvider(BC).build(), |
| 445 | + new CMSTypedStream(new ByteArrayInputStream(TEST_MESSAGE.getBytes())), bOut.toByteArray()); |
| 446 | + |
| 447 | + sp.getSignedContent().drain(); |
| 448 | + |
| 449 | + // |
| 450 | + // compute expected content digest |
| 451 | + // |
| 452 | + MessageDigest md1 = MessageDigest.getInstance("SHA1", BC); |
| 453 | + verifySignatures(sp, md1.digest(TEST_MESSAGE.getBytes()), true); |
| 454 | + |
| 455 | + |
| 456 | + // |
| 457 | + // try using existing signer |
| 458 | + // |
| 459 | + gen = new CMSSignedDataStreamGenerator(); |
| 460 | + |
| 461 | + gen.addSigners(sp.getSignerInfos()); |
| 462 | + |
| 463 | + gen.addCertificates(sp.getCertificates()); |
| 464 | + gen.addCRLs(sp.getCRLs()); |
| 465 | + |
| 466 | + bOut.reset(); |
| 467 | + |
| 468 | + sigOut = gen.open(bOut, true); |
| 469 | + |
| 470 | + sigOut.write(TEST_MESSAGE.getBytes()); |
| 471 | + |
| 472 | + sigOut.close(); |
| 473 | + |
| 474 | + verifyEncodedData(bOut); |
| 475 | + sp = new CMSSignedDataParser(new JcaDigestCalculatorProviderBuilder().setProvider(BC).build(), |
| 476 | + new CMSTypedStream(new ByteArrayInputStream(TEST_MESSAGE.getBytes())), bOut.toByteArray()); |
| 477 | + |
| 478 | + sp.getSignedContent().drain(); |
| 479 | + |
| 480 | + // |
| 481 | + // look for the CRLs |
| 482 | + // |
| 483 | + Collection col = sp.getCRLs().getMatches(null); |
| 484 | + |
| 485 | + assertEquals(2, col.size()); |
| 486 | + assertTrue(col.contains(new JcaX509CRLHolder(_signCrl))); |
| 487 | + assertTrue(col.contains(new JcaX509CRLHolder(_origCrl))); |
| 488 | + } |
| 489 | + |
| 490 | + private void verifySignatures2(CMSSignedDataParser sp, byte[] contentDigest1, byte[] contentDigest2) |
| 491 | + throws Exception |
| 492 | + { |
| 493 | + Store certStore = sp.getCertificates(); |
| 494 | + Store crlStore = sp.getCRLs(); |
| 495 | + SignerInformationStore signers = sp.getSignerInfos(); |
| 496 | + |
| 497 | + Set digestIDs = new HashSet(sp.getDigestAlgorithmIDs()); |
| 498 | + |
| 499 | + assertTrue(digestIDs.size() > 0); |
| 500 | + |
| 501 | + Collection c = signers.getSigners(); |
| 502 | + Iterator it = c.iterator(); |
| 503 | + |
| 504 | + while (it.hasNext()) |
| 505 | + { |
| 506 | + SignerInformation signer = (SignerInformation)it.next(); |
| 507 | + Collection certCollection = certStore.getMatches(signer.getSID()); |
| 508 | + |
| 509 | + Iterator certIt = certCollection.iterator(); |
| 510 | + X509CertificateHolder cert = (X509CertificateHolder)certIt.next(); |
| 511 | + |
| 512 | + assertEquals(true, signer.verify(new JcaSimpleSignerInfoVerifierBuilder().setProvider(BC).build(cert))); |
| 513 | + |
| 514 | + digestIDs.remove(signer.getDigestAlgorithmID()); |
| 515 | + |
| 516 | + assertTrue(MessageDigest.isEqual(contentDigest1, signer.getContentDigest()) || |
| 517 | + MessageDigest.isEqual(contentDigest2, signer.getContentDigest())); |
| 518 | + |
| 519 | + } |
| 520 | + |
| 521 | + assertTrue(digestIDs.size() == 0); |
| 522 | + assertEquals(certStore.getMatches(null).size(), sp.getCertificates().getMatches(null).size()); |
| 523 | + assertEquals(crlStore.getMatches(null).size(), sp.getCRLs().getMatches(null).size()); |
| 524 | + } |
| 525 | + |
368 | 526 | public void testSHA1WithRSA() |
369 | 527 | throws Exception |
370 | 528 | { |
|
0 commit comments