1 13 14 package org.ejbca.ui.cli; 15 16 import java.io.ByteArrayOutputStream ; 17 import java.io.IOException ; 18 import java.io.PrintStream ; 19 import java.rmi.RemoteException ; 20 import java.security.KeyFactory ; 21 import java.security.KeyStore ; 22 import java.security.KeyStoreException ; 23 import java.security.NoSuchAlgorithmException ; 24 import java.security.NoSuchProviderException ; 25 import java.security.PrivateKey ; 26 import java.security.cert.CertificateException ; 27 import java.security.cert.X509Certificate ; 28 import java.security.spec.InvalidKeySpecException ; 29 import java.security.spec.PKCS8EncodedKeySpec ; 30 31 import javax.ejb.CreateException ; 32 import javax.naming.Context ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 36 import org.apache.log4j.Logger; 37 import org.ejbca.core.ejb.InitialContextBuilder; 38 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionHome; 39 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionRemote; 40 import org.ejbca.core.ejb.ca.publisher.IPublisherSessionHome; 41 import org.ejbca.core.ejb.ca.publisher.IPublisherSessionRemote; 42 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionHome; 43 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionRemote; 44 import org.ejbca.core.ejb.ra.IUserAdminSessionHome; 45 import org.ejbca.core.ejb.ra.IUserAdminSessionRemote; 46 import org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionHome; 47 import org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionRemote; 48 import org.ejbca.core.model.log.Admin; 49 import org.ejbca.util.Base64; 50 import org.ejbca.util.CertTools; 51 import org.ejbca.util.KeyTools; 52 53 58 public abstract class BaseCommand { 59 60 private static Logger baseLog = Logger.getLogger(BaseAdminCommand.class); 61 62 private Logger log; 63 64 65 private IUserAdminSessionRemote cacheAdmin = null; 66 67 private static IUserAdminSessionHome cacheHome = null; 68 69 private IRaAdminSessionRemote raadminsession = null; 70 71 private static IRaAdminSessionHome raadminHomesession = null; 72 73 private ICAAdminSessionRemote caadminsession = null; 74 75 private ICertificateStoreSessionRemote certstoresession = null; 76 77 private IPublisherSessionRemote publishersession = null; 78 79 protected Admin administrator = null; 80 81 82 private PrintStream outStream = System.out; 83 84 85 protected String [] args = null; 86 87 91 public BaseCommand() { 92 init(null, Admin.TYPE_CACOMMANDLINE_USER, "cli", System.out); 93 } 94 95 102 protected void init(String [] args, int adminType, String adminId, PrintStream outStream) { 103 log = Logger.getLogger(this.getClass()); 104 this.args = args; 105 if( outStream != null ) { 106 this.outStream = outStream; 107 } 108 administrator = new Admin(adminType, adminId); 109 } 110 111 116 protected InitialContext getInitialContext() throws NamingException { 117 baseLog.debug(">getInitialContext()"); 118 119 try { 120 InitialContext cacheCtx = InitialContextBuilder.getInstance().getInitialContext(); 121 baseLog.debug("<getInitialContext()"); 122 return cacheCtx; 123 } catch (NamingException e) { 124 baseLog.error("Can't get InitialContext", e); 125 throw e; 126 } 127 } 129 132 protected ICAAdminSessionRemote getCAAdminSessionRemote() throws Exception { 133 if(caadminsession == null){ 134 Context ctx = getInitialContext(); 135 ICAAdminSessionHome home = (ICAAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("CAAdminSession"), ICAAdminSessionHome.class ); 136 caadminsession = home.create(); 137 } 138 return caadminsession; 139 } 141 144 protected ICertificateStoreSessionRemote getCertificateStoreSession() throws Exception { 145 if(certstoresession == null){ 146 Context ctx = getInitialContext(); 147 ICertificateStoreSessionHome home = (ICertificateStoreSessionHome) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("CertificateStoreSession"), ICertificateStoreSessionHome.class ); 148 certstoresession = home.create(); 149 } 150 return certstoresession; 151 } 153 156 protected IPublisherSessionRemote getPublisherSession() throws Exception { 157 if(publishersession == null){ 158 Context ctx = getInitialContext(); 159 IPublisherSessionHome home = (IPublisherSessionHome) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("PublisherSession"), IPublisherSessionHome.class ); 160 publishersession = home.create(); 161 } 162 return publishersession; 163 } 167 protected IUserAdminSessionRemote getAdminSession() 168 throws CreateException , NamingException , RemoteException { 169 debug(">getAdminSession()"); 170 try { 171 if (cacheAdmin == null) { 172 if (cacheHome == null) { 173 Context jndiContext = getInitialContext(); 174 Object obj1 = jndiContext.lookup("UserAdminSession"); 175 cacheHome = (IUserAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, 176 IUserAdminSessionHome.class); 177 } 178 cacheAdmin = cacheHome.create(); 179 } 180 debug("<getAdminSession()"); 181 return cacheAdmin; 182 } catch (NamingException e) { 183 error("Can't get Admin session", e); 184 throw e; 185 } 186 } 188 191 protected IRaAdminSessionRemote getRaAdminSession() throws CreateException , NamingException , RemoteException { 192 debug(">getRaAdminSession()"); 193 administrator = new Admin(Admin.TYPE_RA_USER); 194 try { 195 if( raadminsession == null ) { 196 if (raadminHomesession == null) { 197 Context jndiContext = getInitialContext(); 198 Object obj1 = jndiContext.lookup("RaAdminSession"); 199 raadminHomesession = (IRaAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(obj1, IRaAdminSessionHome.class); 200 } 201 raadminsession = raadminHomesession.create(); 202 } 203 debug("<getRaAdminSession()"); 204 return raadminsession; 205 } catch (NamingException e ) { 206 error("Can't get RaAdmin session", e); 207 throw e; 208 } 209 } 211 216 protected boolean appServerRunning() { 217 try { 219 Context ctx = getInitialContext(); 220 ICAAdminSessionHome home = (ICAAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("CAAdminSession"),ICAAdminSessionHome.class); 221 home.getClass(); return true; 223 } catch (Exception e) { 224 error("Appserver not running: ", e); 225 return false; 226 } 227 } 228 229 230 static byte[] keys1024bit = Base64.decode( 231 ("MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAKA5rNhYbPuVcArT" 232 +"mkthfrW2tX1Z7SkCD01sDYrkiwOcodFmS1cSyz8eHM51iwHA7CW0WFvfUjomBT5y" 233 +"gRQfIsf5M5DUtYcKM1hmGKSPzvmF4nYv+3UBUesCvBXVRN/wFZ44SZZ3CVvpQUYb" 234 +"GWjyC+Dgol5n8oKOC287rnZUPEW5AgMBAAECgYEAhMtoeyLGqLlRVFfOoL1cVGTr" 235 +"BMp8ail/30435y7GHKc74p6iwLcd5uEhROhc3oYz8ogHV5W+w9zxKbGjU7b+jmh+" 236 +"h/WFao+Gu3sSrZ7ieg95fSuQsBlJp3w+eCAOZwlEu/JQQHDtURui25SPVblZ9/41" 237 +"u8VwFjk9YQx+nT6LclECQQDYlC9bOr1SWL8PBlipXB/UszMsTM5xEH920A+JPF4E" 238 +"4tw+AHecanjr5bXSluRbWSWUjtl5LV2edqAP9EsH1/A1AkEAvWOctUvTlm6fWHJq" 239 +"lZhsWVvOhDG7cn5gFu34J8JJd5QHov0469CpSamY0Q/mPE/y3kDllmyYvnQ+yobB" 240 +"ZRg39QJBAINCM/0/eVQ58vlBKGTkL2pyfNYhapB9pjK04GWVD4o4j7CICfXjVYvq" 241 +"eSq7RoTSX4NMnCLjyrRqQpHIxdxoE+0CQQCz7MzWWGF+Cz6LUrf7w0E8a8H5SR4i" 242 +"GfnEDvSxIR2W4yWWLShEsIoEF4G9LHO5XOMJT3JOxIEgf2OgGQHmv2l5AkBThYUo" 243 +"ni82jZuue3YqXXHY2lz3rVmooAv7LfQ63yzHECFsQz7kDwuRVWWRsoCOURtymAHp" 244 +"La09g2BE+Q5oUUFx").getBytes()); 245 246 static byte[] certbytes = Base64.decode( 247 ("MIICNzCCAaCgAwIBAgIIIOqiVwJHz+8wDQYJKoZIhvcNAQEFBQAwKzENMAsGA1UE" 248 +"AxMEVGVzdDENMAsGA1UEChMEVGVzdDELMAkGA1UEBhMCU0UwHhcNMDQwNTA4MDkx" 249 +"ODMwWhcNMDUwNTA4MDkyODMwWjArMQ0wCwYDVQQDEwRUZXN0MQ0wCwYDVQQKEwRU" 250 +"ZXN0MQswCQYDVQQGEwJTRTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAgbf2" 251 +"Sv34lsY43C8WJjbUd57TNuHJ6p2Es7ojS3D2yxtzQg/A8wL1OfXes344PPNGHkDd" 252 +"QPBaaWYQrvLvqpjKwx/vA1835L3I92MsGs+uivq5L5oHfCxEh8Kwb9J2p3xjgeWX" 253 +"YdZM5dBj3zzyu+Jer4iU4oCAnnyG+OlVnPsFt6ECAwEAAaNkMGIwDwYDVR0TAQH/" 254 +"BAUwAwEB/zAPBgNVHQ8BAf8EBQMDBwYAMB0GA1UdDgQWBBQArVZXuGqbb9yhBLbu" 255 +"XfzjSuXfHTAfBgNVHSMEGDAWgBQArVZXuGqbb9yhBLbuXfzjSuXfHTANBgkqhkiG" 256 +"9w0BAQUFAAOBgQA1cB6wWzC2rUKBjFAzfkLvDUS3vEMy7ntYMqqQd6+5s1LHCoPw" 257 +"eaR42kMWCxAbdSRgv5ATM0JU3Q9jWbLO54FkJDzq+vw2TaX+Y5T+UL1V0o4TPKxp" 258 +"nKuay+xl5aoUcVEs3h3uJDjcpgMAtyusMEyv4d+RFYvWJWFzRTKDueyanw==").getBytes()); 259 260 265 protected boolean strongCryptoInstalled() throws IOException , KeyStoreException , CertificateException , NoSuchProviderException , NoSuchAlgorithmException , InvalidKeySpecException { 266 CertTools.installBCProvider(); 267 X509Certificate cert = CertTools.getCertfromByteArray(certbytes); 268 PKCS8EncodedKeySpec pkKeySpec = new PKCS8EncodedKeySpec (keys1024bit); 269 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 270 PrivateKey pk = keyFactory.generatePrivate(pkKeySpec); 271 KeyStore ks = KeyTools.createP12("Foo", pk, cert, (X509Certificate )null); 272 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 273 ks.store(baos, "foo1234567890".toCharArray()); 275 return true; 277 } 278 279 284 public void debug(String msg) { 285 log.debug(msg); 286 } 287 288 294 public void debug(String msg, Throwable t) { 295 log.debug(msg, t); 296 } 297 298 303 public void info(String msg) { 304 log.info(msg); 305 } 306 307 313 public void info(String msg, Throwable t) { 314 log.info(msg, t); 315 } 316 317 322 public void error(String msg) { 323 log.error(msg); 324 } 325 326 332 public void error(String msg, Throwable t) { 333 log.error(msg, t); 334 } 335 336 337 341 public PrintStream getOutputStream() { 342 return outStream; 343 } 344 345 350 public void setOutputStream(PrintStream outStream) { 351 if( outStream == null ) 352 this.outStream = System.out; 353 else 354 this.outStream = outStream; 355 } 356 357 } | Popular Tags |