001: package org.bouncycastle.jce.provider.test.rsa3;
002:
003: import junit.framework.Test;
004: import junit.framework.TestCase;
005: import junit.framework.TestSuite;
006: import org.bouncycastle.openssl.PEMReader;
007:
008: import java.io.InputStreamReader;
009: import java.io.Reader;
010: import java.security.Security;
011: import java.security.Signature;
012: import java.security.cert.X509Certificate;
013:
014: /**
015: * Marius Schilder's Bleichenbacher's Forgery Attack Tests
016: */
017: public class RSA3CertTest extends TestCase {
018: public void setUp() {
019: if (Security.getProvider("BC") == null) {
020: Security
021: .addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
022: }
023: }
024:
025: public void testA() throws Exception {
026: doTest("self-testcase-A.pem");
027: }
028:
029: public void testB() throws Exception {
030: doTest("self-testcase-B.pem");
031: }
032:
033: public void testC() throws Exception {
034: doTest("self-testcase-C.pem");
035: }
036:
037: public void testD() throws Exception {
038: doTest("self-testcase-D.pem");
039: }
040:
041: public void testE() throws Exception {
042: doTest("self-testcase-E.pem");
043: }
044:
045: public void testF() throws Exception {
046: doTest("self-testcase-F.pem");
047: }
048:
049: public void testG() throws Exception {
050: doTest("self-testcase-G.pem");
051: }
052:
053: public void testH() throws Exception {
054: doTest("self-testcase-H.pem");
055: }
056:
057: public void testI() throws Exception {
058: doTest("self-testcase-I.pem");
059: }
060:
061: public void testJ() throws Exception {
062: doTest("self-testcase-J.pem");
063: }
064:
065: public void testL() throws Exception {
066: doTest("self-testcase-L.pem");
067: }
068:
069: private void doTest(String certName) throws Exception {
070: X509Certificate cert = loadCert(certName);
071: byte[] tbs = cert.getTBSCertificate();
072: Signature sig = Signature.getInstance(cert.getSigAlgName(),
073: "BC");
074:
075: sig.initVerify(cert.getPublicKey());
076:
077: sig.update(tbs);
078:
079: assertFalse(sig.verify(cert.getSignature()));
080: }
081:
082: private X509Certificate loadCert(String certName) throws Exception {
083: Reader in = new InputStreamReader(getClass()
084: .getResourceAsStream(certName));
085: PEMReader rd = new PEMReader(in);
086:
087: return (X509Certificate) rd.readObject();
088: }
089:
090: public static void main(String[] args) throws Exception {
091: junit.textui.TestRunner.run(suite());
092: }
093:
094: public static Test suite() throws Exception {
095: TestSuite suite = new TestSuite(
096: "Bleichenbacher's Forgery Attack Tests");
097:
098: suite.addTestSuite(RSA3CertTest.class);
099:
100: return suite;
101: }
102: }
|