001: /*************************************************************************
002: * *
003: * EJBCA: The OpenSource Certificate Authority *
004: * *
005: * This software is free software; you can redistribute it and/or *
006: * modify it under the terms of the GNU Lesser General Public *
007: * License as published by the Free Software Foundation; either *
008: * version 2.1 of the License, or any later version. *
009: * *
010: * See terms of license at gnu.org. *
011: * *
012: *************************************************************************/package se.anatom.ejbca.keyrecovery;
013:
014: import java.security.KeyPair;
015: import java.security.cert.X509Certificate;
016: import java.util.Arrays;
017: import java.util.Date;
018: import java.util.Random;
019:
020: import javax.naming.Context;
021: import javax.naming.NamingException;
022:
023: import junit.framework.TestCase;
024:
025: import org.apache.log4j.Logger;
026: import org.ejbca.core.ejb.ca.sign.ISignSessionHome;
027: import org.ejbca.core.ejb.ca.sign.ISignSessionRemote;
028: import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionHome;
029: import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionRemote;
030: import org.ejbca.core.ejb.ra.IUserAdminSessionHome;
031: import org.ejbca.core.ejb.ra.IUserAdminSessionRemote;
032: import org.ejbca.core.model.SecConst;
033: import org.ejbca.core.model.ca.catoken.CATokenConstants;
034: import org.ejbca.core.model.keyrecovery.KeyRecoveryData;
035: import org.ejbca.core.model.log.Admin;
036: import org.ejbca.util.CertTools;
037: import org.ejbca.util.KeyTools;
038:
039: /**
040: * Tests the key recovery modules.
041: *
042: * @version $Id: TestKeyRecovery.java,v 1.6 2006/10/31 08:24:55 anatom Exp $
043: */
044: public class TestKeyRecovery extends TestCase {
045: private static Logger log = Logger.getLogger(TestKeyRecovery.class);
046:
047: private IKeyRecoverySessionRemote cacheAdmin;
048:
049: private static IKeyRecoverySessionHome cacheHome;
050:
051: private static Admin admin = new Admin(Admin.TYPE_INTERNALUSER);
052:
053: private static final String user = genRandomUserName();
054:
055: private static KeyPair keypair = null;
056: private static X509Certificate cert = null;
057:
058: /**
059: * Creates a new TestLog object.
060: *
061: * @param name name
062: */
063: public TestKeyRecovery(String name) {
064: super (name);
065: try {
066: Context jndiContext = getInitialContext();
067: if (cacheAdmin == null) {
068: if (cacheHome == null) {
069: Object obj1 = jndiContext
070: .lookup("KeyRecoverySession");
071: cacheHome = (IKeyRecoverySessionHome) javax.rmi.PortableRemoteObject
072: .narrow(obj1, IKeyRecoverySessionHome.class);
073: }
074: cacheAdmin = cacheHome.create();
075: }
076: } catch (Exception e) {
077: System.out
078: .println("Error Creating TestKeyRecovery instance.");
079: e.printStackTrace();
080: assertTrue("Error Creating TestKeyRecovery instance", false);
081: }
082: }
083:
084: protected void setUp() throws Exception {
085: log.debug(">setUp()");
086: CertTools.installBCProvider();
087: log.debug("<setUp()");
088: }
089:
090: protected void tearDown() throws Exception {
091: }
092:
093: private Context getInitialContext() throws NamingException {
094: //log.debug(">getInitialContext");
095: Context ctx = new javax.naming.InitialContext();
096: //log.debug("<getInitialContext");
097: return ctx;
098: }
099:
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 {
106: log.debug(">test01AddKeyPair()");
107: // Generate test keypair and certificate.
108: try {
109:
110: ISignSessionHome home = (ISignSessionHome) javax.rmi.PortableRemoteObject
111: .narrow(getInitialContext()
112: .lookup("RSASignSession"),
113: ISignSessionHome.class);
114: ISignSessionRemote ss = home.create();
115:
116: Object obj = getInitialContext().lookup("UserAdminSession");
117: IUserAdminSessionHome userhome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject
118: .narrow(obj, IUserAdminSessionHome.class);
119: IUserAdminSessionRemote usersession = userhome.create();
120:
121: String email = "test@test.se";
122: if (!usersession.existsUser(admin, user)) {
123: keypair = KeyTools.genKeys("512",
124: CATokenConstants.KEYALGORITHM_RSA);
125: usersession.addUser(admin, user, "foo123",
126: "CN=TESTKEYREC", "rfc822name=" + email, email,
127: false, SecConst.EMPTY_ENDENTITYPROFILE,
128: SecConst.CERTPROFILE_FIXED_ENDUSER,
129: SecConst.USER_ENDUSER, SecConst.TOKEN_SOFT_P12,
130: 0, "CN=TEST".hashCode());
131: cert = (X509Certificate) ss.createCertificate(admin,
132: user, "foo123", keypair.getPublic());
133: }
134: } catch (Exception e) {
135: log.error("Exception generating keys/cert: ", e);
136: assertTrue("Exception generating keys/cert", false);
137: }
138: cacheAdmin.addKeyRecoveryData(admin, cert, user, keypair);
139:
140: assertTrue("Couldn't save key's in database", cacheAdmin
141: .existsKeys(admin, cert));
142:
143: log.debug("<test01AddKeyPair()");
144: }
145:
146: /**
147: * tests marks the keypair in database and recovers it.
148: *
149: * @throws Exception error
150: */
151: public void test02MarkAndRecoverKeyPair() throws Exception {
152: log.debug(">test02MarkAndRecoverKeyPair()");
153: CertTools.installBCProvider();
154: assertTrue("Couldn't mark user for recovery in database",
155: !cacheAdmin.isUserMarked(admin, user));
156: cacheAdmin.markAsRecoverable(admin, cert,
157: SecConst.EMPTY_ENDENTITYPROFILE);
158: assertTrue("Couldn't mark user for recovery in database",
159: cacheAdmin.isUserMarked(admin, user));
160: KeyRecoveryData data = cacheAdmin.keyRecovery(admin, user,
161: SecConst.EMPTY_ENDENTITYPROFILE);
162:
163: assertTrue("Couldn't recover keys from database", Arrays
164: .equals(data.getKeyPair().getPrivate().getEncoded(),
165: keypair.getPrivate().getEncoded()));
166:
167: log.debug("<test02MarkAndRecoverKeyPair()");
168: }
169:
170: /**
171: * tests removes all keydata.
172: *
173: * @throws Exception error
174: */
175: public void test03RemoveKeyPair() throws Exception {
176: log.debug(">test03RemoveKeyPair()");
177: CertTools.installBCProvider();
178: cacheAdmin.removeKeyRecoveryData(admin, cert);
179: assertTrue("Couldn't remove keys from database", !cacheAdmin
180: .existsKeys(admin, cert));
181:
182: log.debug("<test03RemoveKeyPair()");
183: }
184:
185: private static String genRandomUserName() {
186: // Gen random user
187: Random rand = new Random(new Date().getTime() + 4711);
188: String username = "";
189: for (int i = 0; i < 6; i++) {
190: int randint = rand.nextInt(9);
191: username += (new Integer(randint)).toString();
192: }
193: //log.debug("Generated random username: username =" + username);
194: return username;
195: } // genRandomUserName
196: }
|