1 13 14 package se.anatom.ejbca.hardtoken; 15 16 import java.security.cert.X509Certificate ; 17 import java.util.ArrayList ; 18 19 import javax.naming.Context ; 20 import javax.naming.NamingException ; 21 22 import junit.framework.TestCase; 23 24 import org.apache.log4j.Logger; 25 import org.ejbca.core.ejb.ca.store.CertificateDataBean; 26 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionHome; 27 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionRemote; 28 import org.ejbca.core.ejb.hardtoken.IHardTokenSessionHome; 29 import org.ejbca.core.ejb.hardtoken.IHardTokenSessionRemote; 30 import org.ejbca.core.model.SecConst; 31 import org.ejbca.core.model.hardtoken.HardTokenData; 32 import org.ejbca.core.model.hardtoken.HardTokenExistsException; 33 import org.ejbca.core.model.hardtoken.types.SwedishEIDHardToken; 34 import org.ejbca.core.model.hardtoken.types.TurkishEIDHardToken; 35 import org.ejbca.core.model.log.Admin; 36 import org.ejbca.util.Base64; 37 import org.ejbca.util.CertTools; 38 39 44 public class TestHardToken extends TestCase { 45 private static Logger log = Logger.getLogger(TestHardToken.class); 46 private IHardTokenSessionRemote cacheAdmin; 47 private ICertificateStoreSessionRemote certStore; 48 49 50 private static IHardTokenSessionHome cacheHome; 51 private static ICertificateStoreSessionHome storeHome; 52 53 private static final Admin admin = new Admin(Admin.TYPE_INTERNALUSER); 54 55 static byte[] testcert = Base64.decode(("MIICWzCCAcSgAwIBAgIIJND6Haa3NoAwDQYJKoZIhvcNAQEFBQAwLzEPMA0GA1UE" 56 + "AxMGVGVzdENBMQ8wDQYDVQQKEwZBbmFUb20xCzAJBgNVBAYTAlNFMB4XDTAyMDEw" 57 + "ODA5MTE1MloXDTA0MDEwODA5MjE1MlowLzEPMA0GA1UEAxMGMjUxMzQ3MQ8wDQYD" 58 + "VQQKEwZBbmFUb20xCzAJBgNVBAYTAlNFMIGdMA0GCSqGSIb3DQEBAQUAA4GLADCB" 59 + "hwKBgQCQ3UA+nIHECJ79S5VwI8WFLJbAByAnn1k/JEX2/a0nsc2/K3GYzHFItPjy" 60 + "Bv5zUccPLbRmkdMlCD1rOcgcR9mmmjMQrbWbWp+iRg0WyCktWb/wUS8uNNuGQYQe" 61 + "ACl11SAHFX+u9JUUfSppg7SpqFhSgMlvyU/FiGLVEHDchJEdGQIBEaOBgTB/MA8G" 62 + "A1UdEwEB/wQFMAMBAQAwDwYDVR0PAQH/BAUDAwegADAdBgNVHQ4EFgQUyxKILxFM" 63 + "MNujjNnbeFpnPgB76UYwHwYDVR0jBBgwFoAUy5k/bKQ6TtpTWhsPWFzafOFgLmsw" 64 + "GwYDVR0RBBQwEoEQMjUxMzQ3QGFuYXRvbS5zZTANBgkqhkiG9w0BAQUFAAOBgQAS" 65 + "5wSOJhoVJSaEGHMPw6t3e+CbnEL9Yh5GlgxVAJCmIqhoScTMiov3QpDRHOZlZ15c" 66 + "UlqugRBtORuA9xnLkrdxYNCHmX6aJTfjdIW61+o/ovP0yz6ulBkqcKzopAZLirX+" 67 + "XSWf2uI9miNtxYMVnbQ1KPdEAt7Za3OQR6zcS0lGKg==").getBytes()); 68 69 70 75 public TestHardToken(String name) { 76 super(name); 77 } 78 79 protected void setUp() throws Exception { 80 81 log.debug(">setUp()"); 82 CertTools.installBCProvider(); 83 if (cacheAdmin == null) { 84 if (cacheHome == null) { 85 Context jndiContext = getInitialContext(); 86 Object obj1 = jndiContext.lookup(IHardTokenSessionHome.JNDI_NAME); 87 cacheHome = (IHardTokenSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, IHardTokenSessionHome.class); 88 89 } 90 91 cacheAdmin = cacheHome.create(); 92 } 93 if (certStore == null) { 94 if (storeHome == null) { 95 Context jndiContext = getInitialContext(); 96 Object obj1 = jndiContext.lookup(ICertificateStoreSessionHome.JNDI_NAME); 97 storeHome = (ICertificateStoreSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, ICertificateStoreSessionHome.class); 98 } 99 100 certStore = storeHome.create(); 101 } 102 103 104 log.debug("<setUp()"); 105 } 106 107 protected void tearDown() throws Exception { 108 } 109 110 private Context getInitialContext() throws NamingException { 111 log.debug(">getInitialContext"); 112 113 Context ctx = new javax.naming.InitialContext (); 114 log.debug("<getInitialContext"); 115 116 return ctx; 117 } 118 119 120 125 public void test01AddHardToken() throws Exception { 126 log.debug(">test01AddHardToken()"); 127 boolean ret = false; 128 try { 129 SwedishEIDHardToken token = new SwedishEIDHardToken("1234", "1234", "123456", "123456", 1); 130 131 ArrayList certs = new ArrayList (); 132 133 certs.add(CertTools.getCertfromByteArray(testcert)); 134 135 cacheAdmin.addHardToken(admin, "1234", "TESTUSER", "CN=TEST", SecConst.TOKEN_SWEDISHEID, token, certs, null); 136 137 TurkishEIDHardToken token2 = new TurkishEIDHardToken("1234", "123456", 1); 138 139 cacheAdmin.addHardToken(admin, "2345", "TESTUSER", "CN=TEST", SecConst.TOKEN_TURKISHEID, token2, certs, null); 140 141 ret = true; 142 } catch (HardTokenExistsException pee) { 143 } 144 145 assertTrue("Creating End Entity Profile failed", ret); 146 log.debug("<test01AddHardToken()"); 147 } 148 149 150 155 public void test02EditHardToken() throws Exception { 156 log.debug(">test02EditHardToken()"); 157 158 boolean ret = false; 159 160 HardTokenData token = cacheAdmin.getHardToken(admin, "1234"); 161 162 SwedishEIDHardToken swe = (SwedishEIDHardToken) token.getHardToken(); 163 164 assertTrue("Retrieving HardToken failed", swe.getInitialAuthEncPIN().equals("1234")); 165 166 swe.setInitialAuthEncPIN("5678"); 167 168 cacheAdmin.changeHardToken(admin, "1234", SecConst.TOKEN_SWEDISHEID, token.getHardToken()); 169 ret = true; 170 171 assertTrue("Editing HardToken failed", ret); 172 173 174 log.debug("<test02EditHardToken()"); 175 } 176 177 182 public void test03FindHardTokenByCertificate() throws Exception { 183 log.debug(">test03FindHardTokenByCertificate()"); 184 185 X509Certificate cert = CertTools.getCertfromByteArray(testcert); 186 if(certStore.findCertificateByFingerprint(admin, CertTools.getFingerprintAsString(cert)) == null){ 188 certStore.storeCertificate(admin,cert,"DUMMYUSER", CertTools.getFingerprintAsString(cert),CertificateDataBean.CERT_ACTIVE,CertificateDataBean.CERTTYPE_ENDENTITY); 189 } 190 String tokensn = cacheAdmin.findHardTokenByCertificateSNIssuerDN(admin, cert.getSerialNumber(), cert.getIssuerDN().toString()); 191 192 assertTrue("Couldn't find right hardtokensn", tokensn.equals("1234")); 193 194 log.debug("<test03FindHardTokenByCertificate()"); 195 } 196 197 202 public void test04removeHardTokens() throws Exception { 203 log.debug(">test04removeHardTokens()"); 204 boolean ret = false; 205 try { 206 cacheAdmin.removeHardToken(admin, "1234"); 207 cacheAdmin.removeHardToken(admin, "2345"); 208 209 ret = true; 210 } catch (Exception pee) { 211 } 212 assertTrue("Removing Hard Token failed", ret); 213 214 log.debug("<test04removeHardTokens()"); 215 } 216 217 218 } 219 | Popular Tags |