~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to test/src/org/bouncycastle/jce/provider/test/rsa3/RSA3CertTest.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.jce.provider.test.rsa3;
 
2
 
 
3
import junit.framework.Test;
 
4
import junit.framework.TestCase;
 
5
import junit.framework.TestSuite;
 
6
import org.bouncycastle.openssl.PEMReader;
 
7
 
 
8
import java.io.InputStreamReader;
 
9
import java.io.Reader;
 
10
import java.security.Security;
 
11
import java.security.Signature;
 
12
import java.security.cert.X509Certificate;
 
13
 
 
14
/**
 
15
 * Marius Schilder's Bleichenbacher's Forgery Attack Tests
 
16
 */
 
17
public class RSA3CertTest
 
18
    extends TestCase
 
19
{
 
20
    public void setUp()
 
21
    {
 
22
        if (Security.getProvider("BC") == null)
 
23
        {
 
24
            Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
 
25
        }
 
26
    }
 
27
    
 
28
    public void testA()
 
29
        throws Exception
 
30
    {
 
31
        doTest("self-testcase-A.pem");
 
32
    }
 
33
 
 
34
    public void testB()
 
35
        throws Exception
 
36
    {
 
37
        doTest("self-testcase-B.pem");
 
38
    }
 
39
    
 
40
    public void testC()
 
41
        throws Exception
 
42
    {
 
43
        doTest("self-testcase-C.pem");
 
44
    }
 
45
    
 
46
    public void testD()
 
47
        throws Exception
 
48
    {
 
49
        doTest("self-testcase-D.pem");
 
50
    }
 
51
    
 
52
    public void testE()
 
53
        throws Exception
 
54
    {
 
55
        doTest("self-testcase-E.pem");
 
56
    }
 
57
    
 
58
    public void testF()
 
59
        throws Exception
 
60
    {
 
61
        doTest("self-testcase-F.pem");
 
62
    }
 
63
    
 
64
    public void testG()
 
65
        throws Exception
 
66
    {
 
67
        doTest("self-testcase-G.pem");
 
68
    }
 
69
    
 
70
    public void testH()
 
71
        throws Exception
 
72
    {
 
73
        doTest("self-testcase-H.pem");
 
74
    }
 
75
    
 
76
    public void testI()
 
77
        throws Exception
 
78
    {
 
79
        doTest("self-testcase-I.pem");
 
80
    }
 
81
    
 
82
    public void testJ()
 
83
        throws Exception
 
84
    {
 
85
        doTest("self-testcase-J.pem");
 
86
    }
 
87
    
 
88
    public void testL()
 
89
        throws Exception
 
90
    {
 
91
        doTest("self-testcase-L.pem");
 
92
    }
 
93
    
 
94
    private void doTest(
 
95
        String      certName)
 
96
        throws Exception
 
97
    {
 
98
        X509Certificate  cert = loadCert(certName);
 
99
        byte[]           tbs = cert.getTBSCertificate();
 
100
        Signature        sig = Signature.getInstance(cert.getSigAlgName(), "BC");
 
101
        
 
102
        sig.initVerify(cert.getPublicKey());
 
103
        
 
104
        sig.update(tbs);
 
105
        
 
106
        assertFalse(sig.verify(cert.getSignature()));
 
107
    }
 
108
 
 
109
    private X509Certificate loadCert(
 
110
        String certName)
 
111
        throws Exception
 
112
    {
 
113
        Reader    in = new InputStreamReader(getClass().getResourceAsStream(certName));
 
114
        PEMReader rd = new PEMReader(in);
 
115
        
 
116
        return (X509Certificate)rd.readObject();
 
117
    }
 
118
    
 
119
    public static void main (String[] args) 
 
120
        throws Exception
 
121
    {
 
122
        junit.textui.TestRunner.run(suite());
 
123
    }
 
124
    
 
125
    public static Test suite() 
 
126
        throws Exception
 
127
    {   
 
128
        TestSuite suite = new TestSuite("Bleichenbacher's Forgery Attack Tests");
 
129
        
 
130
        suite.addTestSuite(RSA3CertTest.class);
 
131
        
 
132
        return suite;
 
133
    }
 
134
}