KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > jcetaglib > test > KeyTest


1 /*
2   Name: KeyTest.java
3   Licensing: LGPL
4
5   API: Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
6   Provider: Bouncy Castle (http://www.bouncycastle.org)
7
8   Disclaimer:
9
10   COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
11   EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
12   IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
13   RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
14   PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
15   ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
16   CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
17   HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
18
19   (C) Copyright 2003 Gert Van Ham
20 */

21
22 package net.sourceforge.jcetaglib.test;
23
24 import junit.framework.Assert;
25 import junit.framework.TestCase;
26 import net.sourceforge.jcetaglib.lib.Keystore;
27 import net.sourceforge.jcetaglib.lib.X509Cert;
28
29 import java.security.KeyPair JavaDoc;
30 import java.security.cert.X509Certificate JavaDoc;
31
32 /**
33  * A collection of symmetric & asymmetric key tests
34  * <P>
35  * These tests can be run using JUnit (http://www.junit.org)
36  *
37  * @author Gert Van Ham
38  * @author hamgert@users.sourceforge.net
39  * @author http://jcetaglib.sourceforge.net
40  * @version $Id: KeyTest.java,v 1.3 2004/04/15 07:28:36 hamgert Exp $
41  */

42 public class KeyTest extends TestCase {
43
44     /**
45      * test generating symmetric (block cipher) keystores
46      *
47      * @throws Exception
48      */

49     public void testGenerateKeys() throws Exception JavaDoc {
50         for (int i = 0; i < RunTest.alg.length; i++) {
51             String JavaDoc fileloc = RunTest.TEMPFOLDER + RunTest.alg[i][0] + "_" + RunTest.alg[i][1] + ".key";
52             Keystore.generateKey(RunTest.alg[i][0]
53                     , Integer.parseInt(RunTest.alg[i][1])
54                     , null
55                     , fileloc
56                     , new StringBuffer JavaDoc("password"));
57         }
58     }
59
60     /**
61      * testing X.509 certificate generation
62      *
63      * @throws Exception
64      */

65     public void testGenerateCertificates() throws Exception JavaDoc {
66
67         // generate key pair for CA
68
KeyPair JavaDoc kp = X509Cert.generateKeyPair("RSA", 1024, null);
69
70         // generate self-signed CA certificate
71
X509Certificate JavaDoc cacert = X509Cert.selfsign(kp.getPrivate()
72                 , kp.getPublic()
73                 , "MD5WithRSAEncryption"
74                 , 365
75                 , "C=BE, O=NET, OU=Sourceforge, CN=CertAuthority, EmailAddress=info@certauthority.org"
76                 , true
77                 , null);
78
79         // save as P12 keystore
80
X509Cert.saveAsP12(cacert, cacert, kp.getPrivate(), RunTest.TEMPFOLDER + "ca.p12", "ca", new StringBuffer JavaDoc("password"));
81
82         // generate key pair for Alice
83
KeyPair JavaDoc kpAlice = X509Cert.generateKeyPair("RSA", 1024, null);
84
85         // generate certificate for Alice signed by CA
86
X509Certificate JavaDoc alicecert = X509Cert.sign(kpAlice.getPublic()
87                 , kp.getPrivate()
88                 , cacert
89                 , "MD5WithRSAEncryption"
90                 , 365
91                 , "C=BE, O=NET, OU=Sourceforge, CN=Alice, EmailAddress=alice@sourceforge.net"
92                 , true
93                 , null
94                 , "client");
95
96         // save as P12 keystore
97
X509Cert.saveAsP12(alicecert, cacert, kpAlice.getPrivate(), RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
98
99         // generate key pair for Bob
100
KeyPair JavaDoc kpBob = X509Cert.generateKeyPair("RSA", 1024, null);
101
102         // generate certificate for Alice signed by CA
103
X509Certificate JavaDoc bobcert = X509Cert.sign(kpBob.getPublic()
104                 , kp.getPrivate()
105                 , cacert
106                 , "MD5WithRSAEncryption"
107                 , 365
108                 , "C=BE, O=NET, OU=Sourceforge, CN=Bob, EmailAddress=bob@sourceforge.net"
109                 , true
110                 , null
111                 , "all");
112
113         // save as P12 keystore
114
X509Cert.saveAsP12(bobcert, cacert, kpBob.getPrivate(), RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer JavaDoc("password"));
115     }
116
117     /**
118      * reading test of X.509 certificates
119      *
120      * @throws Exception
121      */

122     public void testReadCertificates() throws Exception JavaDoc {
123         // load Alice's certificate
124
X509Certificate JavaDoc alicecert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "alice.p12", "alice", new StringBuffer JavaDoc("password"));
125
126         alicecert.checkValidity();
127
128         Assert.assertEquals(alicecert.getSubjectDN().toString(), "C=BE,O=NET,OU=Sourceforge,CN=Alice,E=alice@sourceforge.net");
129         Assert.assertEquals(alicecert.getSigAlgName(), "MD5WithRSAEncryption");
130         Assert.assertEquals(alicecert.getIssuerDN().toString(), "C=BE,O=NET,OU=Sourceforge,CN=CertAuthority,E=info@certauthority.org");
131
132         // load Bob's certificate
133
X509Certificate JavaDoc bobcert = X509Cert.getCertificateFromP12(RunTest.TEMPFOLDER + "bob.p12", "bob", new StringBuffer JavaDoc("password"));
134
135         bobcert.checkValidity();
136
137         Assert.assertEquals(bobcert.getSubjectDN().toString(), "C=BE,O=NET,OU=Sourceforge,CN=Bob,E=bob@sourceforge.net");
138         Assert.assertEquals(bobcert.getSigAlgName(), "MD5WithRSAEncryption");
139         Assert.assertEquals(bobcert.getIssuerDN().toString(), "C=BE,O=NET,OU=Sourceforge,CN=CertAuthority,E=info@certauthority.org");
140     }
141 }
142
Popular Tags