KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > se > anatom > ejbca > keyrecovery > TestKeyRecovery


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package se.anatom.ejbca.keyrecovery;
15
16 import java.security.KeyPair JavaDoc;
17 import java.security.cert.X509Certificate JavaDoc;
18 import java.util.Arrays JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.Random JavaDoc;
21
22 import javax.naming.Context JavaDoc;
23 import javax.naming.NamingException JavaDoc;
24
25 import junit.framework.TestCase;
26
27 import org.apache.log4j.Logger;
28 import org.ejbca.core.ejb.ca.sign.ISignSessionHome;
29 import org.ejbca.core.ejb.ca.sign.ISignSessionRemote;
30 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionHome;
31 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionRemote;
32 import org.ejbca.core.ejb.ra.IUserAdminSessionHome;
33 import org.ejbca.core.ejb.ra.IUserAdminSessionRemote;
34 import org.ejbca.core.model.SecConst;
35 import org.ejbca.core.model.ca.catoken.CATokenConstants;
36 import org.ejbca.core.model.keyrecovery.KeyRecoveryData;
37 import org.ejbca.core.model.log.Admin;
38 import org.ejbca.util.CertTools;
39 import org.ejbca.util.KeyTools;
40
41 /**
42  * Tests the key recovery modules.
43  *
44  * @version $Id: TestKeyRecovery.java,v 1.6 2006/10/31 08:24:55 anatom Exp $
45  */

46 public class TestKeyRecovery extends TestCase {
47     private static Logger log = Logger.getLogger(TestKeyRecovery.class);
48
49     private IKeyRecoverySessionRemote cacheAdmin;
50
51     private static IKeyRecoverySessionHome cacheHome;
52
53     private static Admin admin = new Admin(Admin.TYPE_INTERNALUSER);
54
55     private static final String JavaDoc user = genRandomUserName();
56
57     private static KeyPair JavaDoc keypair = null;
58     private static X509Certificate JavaDoc cert = null;
59
60     /**
61      * Creates a new TestLog object.
62      *
63      * @param name name
64      */

65     public TestKeyRecovery(String JavaDoc name) {
66         super(name);
67         try {
68             Context JavaDoc jndiContext = getInitialContext();
69             if (cacheAdmin == null) {
70                 if (cacheHome == null) {
71                     Object JavaDoc obj1 = jndiContext.lookup("KeyRecoverySession");
72                     cacheHome = (IKeyRecoverySessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, IKeyRecoverySessionHome.class);
73                 }
74                 cacheAdmin = cacheHome.create();
75             }
76         } catch (Exception JavaDoc e) {
77             System.out.println("Error Creating TestKeyRecovery instance.");
78             e.printStackTrace();
79             assertTrue("Error Creating TestKeyRecovery instance", false);
80         }
81     }
82
83     protected void setUp() throws Exception JavaDoc {
84         log.debug(">setUp()");
85         CertTools.installBCProvider();
86         log.debug("<setUp()");
87     }
88
89     protected void tearDown() throws Exception JavaDoc {
90     }
91
92     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
93         //log.debug(">getInitialContext");
94
Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
95         //log.debug("<getInitialContext");
96
return ctx;
97     }
98
99
100     /**
101      * tests adding a keypair and checks if it can be read again.
102      *
103      * @throws Exception error
104      */

105     public void test01AddKeyPair() throws Exception JavaDoc {
106         log.debug(">test01AddKeyPair()");
107         // Generate test keypair and certificate.
108
try {
109
110             ISignSessionHome home = (ISignSessionHome) javax.rmi.PortableRemoteObject.narrow(getInitialContext().lookup("RSASignSession"), ISignSessionHome.class);
111             ISignSessionRemote ss = home.create();
112
113             Object JavaDoc obj = getInitialContext().lookup("UserAdminSession");
114             IUserAdminSessionHome userhome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, IUserAdminSessionHome.class);
115             IUserAdminSessionRemote usersession = userhome.create();
116
117             String JavaDoc email = "test@test.se";
118             if (!usersession.existsUser(admin, user)) {
119                 keypair = KeyTools.genKeys("512", CATokenConstants.KEYALGORITHM_RSA);
120                 usersession.addUser(admin, user, "foo123", "CN=TESTKEYREC", "rfc822name=" + email, email, false, SecConst.EMPTY_ENDENTITYPROFILE, SecConst.CERTPROFILE_FIXED_ENDUSER, SecConst.USER_ENDUSER, SecConst.TOKEN_SOFT_P12, 0, "CN=TEST".hashCode());
121                 cert = (X509Certificate JavaDoc) ss.createCertificate(admin, user, "foo123", keypair.getPublic());
122             }
123         } catch (Exception JavaDoc e) {
124             log.error("Exception generating keys/cert: ", e);
125             assertTrue("Exception generating keys/cert", false);
126         }
127         cacheAdmin.addKeyRecoveryData(admin, cert, user, keypair);
128
129         assertTrue("Couldn't save key's in database", cacheAdmin.existsKeys(admin, cert));
130
131         log.debug("<test01AddKeyPair()");
132     }
133
134     /**
135      * tests marks the keypair in database and recovers it.
136      *
137      * @throws Exception error
138      */

139     public void test02MarkAndRecoverKeyPair() throws Exception JavaDoc {
140         log.debug(">test02MarkAndRecoverKeyPair()");
141         CertTools.installBCProvider();
142         assertTrue("Couldn't mark user for recovery in database", !cacheAdmin.isUserMarked(admin, user));
143         cacheAdmin.markAsRecoverable(admin, cert,SecConst.EMPTY_ENDENTITYPROFILE);
144         assertTrue("Couldn't mark user for recovery in database", cacheAdmin.isUserMarked(admin, user));
145         KeyRecoveryData data = cacheAdmin.keyRecovery(admin, user, SecConst.EMPTY_ENDENTITYPROFILE);
146
147         assertTrue("Couldn't recover keys from database", Arrays.equals(data.getKeyPair().getPrivate().getEncoded(), keypair.getPrivate().getEncoded()));
148
149         log.debug("<test02MarkAndRecoverKeyPair()");
150     }
151
152     /**
153      * tests removes all keydata.
154      *
155      * @throws Exception error
156      */

157     public void test03RemoveKeyPair() throws Exception JavaDoc {
158         log.debug(">test03RemoveKeyPair()");
159         CertTools.installBCProvider();
160         cacheAdmin.removeKeyRecoveryData(admin, cert);
161         assertTrue("Couldn't remove keys from database", !cacheAdmin.existsKeys(admin, cert));
162
163         log.debug("<test03RemoveKeyPair()");
164     }
165
166     private static String JavaDoc genRandomUserName() {
167         // Gen random user
168
Random JavaDoc rand = new Random JavaDoc(new Date JavaDoc().getTime() + 4711);
169         String JavaDoc username = "";
170         for (int i = 0; i < 6; i++) {
171             int randint = rand.nextInt(9);
172             username += (new Integer JavaDoc(randint)).toString();
173         }
174         //log.debug("Generated random username: username =" + username);
175
return username;
176     } // genRandomUserName
177
}
178
Popular Tags