1 13 14 package org.ejbca.core.model.ca.caadmin; 15 16 import java.security.cert.CertStore ; 17 import java.util.ArrayList ; 18 import java.util.Collection ; 19 import java.util.Iterator ; 20 21 import javax.naming.Context ; 22 import javax.naming.NamingException ; 23 import javax.security.auth.x500.X500Principal ; 24 25 import junit.framework.TestCase; 26 27 import org.apache.log4j.Logger; 28 import org.bouncycastle.cms.CMSProcessable; 29 import org.bouncycastle.cms.CMSSignedData; 30 import org.bouncycastle.cms.CMSSignedGenerator; 31 import org.bouncycastle.cms.SignerId; 32 import org.bouncycastle.cms.SignerInformation; 33 import org.bouncycastle.cms.SignerInformationStore; 34 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionHome; 35 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionRemote; 36 import org.ejbca.core.ejb.ca.sign.ISignSessionHome; 37 import org.ejbca.core.ejb.ca.sign.ISignSessionRemote; 38 import org.ejbca.core.model.ca.caadmin.extendedcaservices.CmsCAServiceInfo; 39 import org.ejbca.core.model.ca.caadmin.extendedcaservices.CmsCAServiceRequest; 40 import org.ejbca.core.model.ca.caadmin.extendedcaservices.CmsCAServiceResponse; 41 import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceInfo; 42 import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceNotActiveException; 43 import org.ejbca.core.model.log.Admin; 44 import org.ejbca.util.CertTools; 45 46 47 52 public class TestCmsCAService extends TestCase { 53 private static Logger log = Logger.getLogger(TestCmsCAService.class); 54 55 private byte[] doc = "foo123".getBytes(); 56 57 private static ISignSessionRemote remote; 58 private static ICAAdminSessionRemote casession; 59 private static int rsacaid = 0; 60 private Admin admin; 61 62 67 public TestCmsCAService(String name) throws Exception { 68 super(name); 69 CertTools.installBCProvider(); 71 Context ctx = getInitialContext(); 72 Object obj = ctx.lookup(ISignSessionHome.JNDI_NAME); 73 ISignSessionHome home = (ISignSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, ISignSessionHome.class); 74 remote = home.create(); 75 76 admin = new Admin(Admin.TYPE_BATCHCOMMANDLINE_USER); 77 78 obj = ctx.lookup(ICAAdminSessionHome.JNDI_NAME); 79 ICAAdminSessionHome cahome = (ICAAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, ICAAdminSessionHome.class); 80 casession = cahome.create(); 81 CAInfo inforsa = casession.getCAInfo(admin, "TEST"); 82 rsacaid = inforsa.getCAId(); 83 if (rsacaid == 0){ 84 assertTrue("No active RSA CA! Must have at least one active CA to run tests!", false); 85 } 86 87 } 88 89 protected void setUp() throws Exception { 90 log.debug(">setUp()"); 91 CertTools.installBCProvider(); 92 log.debug("<setUp()"); 93 } 94 95 protected void tearDown() throws Exception { 96 } 97 98 private Context getInitialContext() throws NamingException { 99 log.debug(">getInitialContext"); 100 101 Context ctx = new javax.naming.InitialContext (); 102 log.debug("<getInitialContext"); 103 104 return ctx; 105 } 106 107 109 public void test01CmsCAServiceNotActive() throws Exception { 110 CmsCAServiceRequest request = new CmsCAServiceRequest(doc, true); 111 112 boolean active = true; 114 try { 115 remote.extendedService(admin, rsacaid, request); 116 } catch (ExtendedCAServiceNotActiveException e) { 117 active = false; 118 } 119 assertTrue(!active); 121 122 } 123 124 126 public void test02ActivateCmsCAService() throws Exception { 127 CAInfo cainfo = casession.getCAInfo(admin, "TEST"); 129 ArrayList newlist = new ArrayList (); 130 newlist.add(new CmsCAServiceInfo(ExtendedCAServiceInfo.STATUS_ACTIVE, false)); 131 cainfo.setExtendedCAServiceInfos(newlist); 132 casession.editCA(admin, cainfo); 133 } 134 135 137 public void test03CmsCAServiceActive() throws Exception { 138 CmsCAServiceRequest request = new CmsCAServiceRequest(doc, true); 139 CmsCAServiceResponse resp = null; 140 boolean active = true; 142 try { 143 resp = (CmsCAServiceResponse)remote.extendedService(admin, rsacaid, request); 144 } catch (ExtendedCAServiceNotActiveException e) { 145 active = false; 146 } 147 assertTrue(active); 149 150 assertNotNull(resp); 151 byte[] respdoc = resp.getCmsDocument(); 152 assertNotNull(resp); 153 CMSSignedData csd = new CMSSignedData(respdoc); 154 SignerInformationStore infoStore = csd.getSignerInfos(); 155 Collection signers = infoStore.getSigners(); 156 Iterator iter = signers.iterator(); 157 if (iter.hasNext()) { 161 SignerInformation si = (SignerInformation)iter.next(); 162 assertNotNull(si); 163 assertEquals(CMSSignedGenerator.DIGEST_SHA1, si.getDigestAlgOID()); 165 SignerId sid = si.getSID(); 166 X500Principal issuer = sid.getIssuer(); 168 assertNotNull(issuer); 169 assertEquals("CN=TEST", issuer.getName()); 170 } 171 CertStore store = csd.getCertificatesAndCRLs("Collection", "BC"); 172 Collection certs = store.getCertificates(null); 173 assertEquals(2, certs.size()); 174 175 CMSProcessable cp = csd.getSignedContent(); 176 Object o = cp.getContent(); 177 byte[] ob = (byte[])o; 178 assertEquals(new String (doc), new String (ob)); 179 } 180 181 183 public void test04DeActivateCmsCAService() throws Exception { 184 CAInfo cainfo = casession.getCAInfo(admin, "TEST"); 186 ArrayList newlist = new ArrayList (); 187 newlist.add(new CmsCAServiceInfo(ExtendedCAServiceInfo.STATUS_INACTIVE, false)); 188 cainfo.setExtendedCAServiceInfos(newlist); 189 casession.editCA(admin, cainfo); 190 } 191 192 } 193 | Popular Tags |