1 13 14 package se.anatom.ejbca.keyrecovery; 15 16 import java.security.KeyPair ; 17 import java.security.cert.X509Certificate ; 18 import java.util.Arrays ; 19 import java.util.Date ; 20 import java.util.Random ; 21 22 import javax.naming.Context ; 23 import javax.naming.NamingException ; 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 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 user = genRandomUserName(); 56 57 private static KeyPair keypair = null; 58 private static X509Certificate cert = null; 59 60 65 public TestKeyRecovery(String name) { 66 super(name); 67 try { 68 Context jndiContext = getInitialContext(); 69 if (cacheAdmin == null) { 70 if (cacheHome == null) { 71 Object obj1 = jndiContext.lookup("KeyRecoverySession"); 72 cacheHome = (IKeyRecoverySessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, IKeyRecoverySessionHome.class); 73 } 74 cacheAdmin = cacheHome.create(); 75 } 76 } catch (Exception 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 { 84 log.debug(">setUp()"); 85 CertTools.installBCProvider(); 86 log.debug("<setUp()"); 87 } 88 89 protected void tearDown() throws Exception { 90 } 91 92 private Context getInitialContext() throws NamingException { 93 Context ctx = new javax.naming.InitialContext (); 95 return ctx; 97 } 98 99 100 105 public void test01AddKeyPair() throws Exception { 106 log.debug(">test01AddKeyPair()"); 107 try { 109 110 ISignSessionHome home = (ISignSessionHome) javax.rmi.PortableRemoteObject.narrow(getInitialContext().lookup("RSASignSession"), ISignSessionHome.class); 111 ISignSessionRemote ss = home.create(); 112 113 Object obj = getInitialContext().lookup("UserAdminSession"); 114 IUserAdminSessionHome userhome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, IUserAdminSessionHome.class); 115 IUserAdminSessionRemote usersession = userhome.create(); 116 117 String 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 ) ss.createCertificate(admin, user, "foo123", keypair.getPublic()); 122 } 123 } catch (Exception 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 139 public void test02MarkAndRecoverKeyPair() throws Exception { 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 157 public void test03RemoveKeyPair() throws Exception { 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 genRandomUserName() { 167 Random rand = new Random (new Date ().getTime() + 4711); 169 String username = ""; 170 for (int i = 0; i < 6; i++) { 171 int randint = rand.nextInt(9); 172 username += (new Integer (randint)).toString(); 173 } 174 return username; 176 } } 178 | Popular Tags |