?? basecommand.java
字號:
/************************************************************************* * * * EJBCA: The OpenSource Certificate Authority * * * * This software is free software; you can redistribute it and/or * * modify it under the terms of the GNU Lesser General Public * * License as published by the Free Software Foundation; either * * version 2.1 of the License, or any later version. * * * * See terms of license at gnu.org. * * * *************************************************************************/ package se.anatom.ejbca.admin;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.security.KeyFactory;import java.security.KeyStore;import java.security.KeyStoreException;import java.security.NoSuchAlgorithmException;import java.security.NoSuchProviderException;import java.security.PrivateKey;import java.security.cert.CertificateException;import java.security.cert.X509Certificate;import java.security.spec.InvalidKeySpecException;import java.security.spec.PKCS8EncodedKeySpec;import javax.naming.*;import org.apache.log4j.Logger;import se.anatom.ejbca.ca.caadmin.ICAAdminSessionHome;import se.anatom.ejbca.log.Admin;import se.anatom.ejbca.util.Base64;import se.anatom.ejbca.util.CertTools;import se.anatom.ejbca.util.KeyTools;/** * Base for Commands, contains useful functions * * @version $Id: BaseCommand.java,v 1.5 2004/05/09 09:07:22 anatom Exp $ */public abstract class BaseCommand { /** Log4j instance for Base */ private static Logger baseLog = Logger.getLogger(BaseCommand.class); /** Log4j instance for actual class */ private Logger log; /** Cached initial context to save JNDI lookups */ private static InitialContext cacheCtx = null; protected Admin administrator = null; /** * Creates a new instance of the class * */ public BaseCommand() { log = Logger.getLogger(this.getClass()); administrator = new Admin(Admin.TYPE_CACOMMANDLINE_USER); } /** * Method checking if the application server is running. * * @return true if app server is running. */ protected boolean appServerRunning() { // Check that the application server is running by getting a home interface for user admin session try { Context ctx = getInitialContext(); ICAAdminSessionHome home = (ICAAdminSessionHome) javax.rmi.PortableRemoteObject.narrow(ctx.lookup("CAAdminSession"),ICAAdminSessionHome.class); return true; } catch (Exception e) { error("Appserver not running: ", e); return false; } } /** Priavet key with length 1024 bits */ static byte[] keys1024bit = Base64.decode( ("MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAKA5rNhYbPuVcArT" +"mkthfrW2tX1Z7SkCD01sDYrkiwOcodFmS1cSyz8eHM51iwHA7CW0WFvfUjomBT5y" +"gRQfIsf5M5DUtYcKM1hmGKSPzvmF4nYv+3UBUesCvBXVRN/wFZ44SZZ3CVvpQUYb" +"GWjyC+Dgol5n8oKOC287rnZUPEW5AgMBAAECgYEAhMtoeyLGqLlRVFfOoL1cVGTr" +"BMp8ail/30435y7GHKc74p6iwLcd5uEhROhc3oYz8ogHV5W+w9zxKbGjU7b+jmh+" +"h/WFao+Gu3sSrZ7ieg95fSuQsBlJp3w+eCAOZwlEu/JQQHDtURui25SPVblZ9/41" +"u8VwFjk9YQx+nT6LclECQQDYlC9bOr1SWL8PBlipXB/UszMsTM5xEH920A+JPF4E" +"4tw+AHecanjr5bXSluRbWSWUjtl5LV2edqAP9EsH1/A1AkEAvWOctUvTlm6fWHJq" +"lZhsWVvOhDG7cn5gFu34J8JJd5QHov0469CpSamY0Q/mPE/y3kDllmyYvnQ+yobB" +"ZRg39QJBAINCM/0/eVQ58vlBKGTkL2pyfNYhapB9pjK04GWVD4o4j7CICfXjVYvq" +"eSq7RoTSX4NMnCLjyrRqQpHIxdxoE+0CQQCz7MzWWGF+Cz6LUrf7w0E8a8H5SR4i" +"GfnEDvSxIR2W4yWWLShEsIoEF4G9LHO5XOMJT3JOxIEgf2OgGQHmv2l5AkBThYUo" +"ni82jZuue3YqXXHY2lz3rVmooAv7LfQ63yzHECFsQz7kDwuRVWWRsoCOURtymAHp" +"La09g2BE+Q5oUUFx").getBytes()); /** self signed cert done with above private key */ static byte[] certbytes = Base64.decode( ("MIICNzCCAaCgAwIBAgIIIOqiVwJHz+8wDQYJKoZIhvcNAQEFBQAwKzENMAsGA1UE" +"AxMEVGVzdDENMAsGA1UEChMEVGVzdDELMAkGA1UEBhMCU0UwHhcNMDQwNTA4MDkx" +"ODMwWhcNMDUwNTA4MDkyODMwWjArMQ0wCwYDVQQDEwRUZXN0MQ0wCwYDVQQKEwRU" +"ZXN0MQswCQYDVQQGEwJTRTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAgbf2" +"Sv34lsY43C8WJjbUd57TNuHJ6p2Es7ojS3D2yxtzQg/A8wL1OfXes344PPNGHkDd" +"QPBaaWYQrvLvqpjKwx/vA1835L3I92MsGs+uivq5L5oHfCxEh8Kwb9J2p3xjgeWX" +"YdZM5dBj3zzyu+Jer4iU4oCAnnyG+OlVnPsFt6ECAwEAAaNkMGIwDwYDVR0TAQH/" +"BAUwAwEB/zAPBgNVHQ8BAf8EBQMDBwYAMB0GA1UdDgQWBBQArVZXuGqbb9yhBLbu" +"XfzjSuXfHTAfBgNVHSMEGDAWgBQArVZXuGqbb9yhBLbuXfzjSuXfHTANBgkqhkiG" +"9w0BAQUFAAOBgQA1cB6wWzC2rUKBjFAzfkLvDUS3vEMy7ntYMqqQd6+5s1LHCoPw" +"eaR42kMWCxAbdSRgv5ATM0JU3Q9jWbLO54FkJDzq+vw2TaX+Y5T+UL1V0o4TPKxp" +"nKuay+xl5aoUcVEs3h3uJDjcpgMAtyusMEyv4d+RFYvWJWFzRTKDueyanw==").getBytes()); /** * Method checking if strong crypto is installed (extra package from java.sun.com) * * @return true if strong crypto is installed. */ protected boolean strongCryptoInstalled() throws IOException, KeyStoreException, CertificateException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException { CertTools.installBCProvider(); X509Certificate cert = CertTools.getCertfromByteArray(certbytes); PKCS8EncodedKeySpec pkKeySpec = new PKCS8EncodedKeySpec(keys1024bit); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey pk = keyFactory.generatePrivate(pkKeySpec); KeyStore ks = KeyTools.createP12("Foo", pk, cert, (X509Certificate)null); ByteArrayOutputStream baos = new ByteArrayOutputStream(); // If password below is more than 7 chars, strong crypto is needed ks.store(baos, "foo1234567890".toCharArray()); // If we didn't throw an exception, we were succesful return true; } /** * Gets InitialContext * * @return InitialContext */ protected InitialContext getInitialContext() throws NamingException { baseLog.debug(">getInitialContext()"); try { if (cacheCtx == null) { cacheCtx = new InitialContext(); } baseLog.debug("<getInitialContext()"); return cacheCtx; } catch (NamingException e) { baseLog.error("Can't get InitialContext", e); throw e; } } // getInitialContext /** * Logs a message with priority DEBUG * * @param msg Message */ public void debug(String msg) { log.debug(msg); } /** * Logs a message and an exception with priority DEBUG * * @param msg Message * @param t Exception */ public void debug(String msg, Throwable t) { log.debug(msg, t); } /** * Logs a message with priority INFO * * @param msg Message */ public void info(String msg) { log.info(msg); } /** * Logs a message and an exception with priority INFO * * @param msg Message * @param t Exception */ public void info(String msg, Throwable t) { log.info(msg, t); } /** * Logs a message with priority ERROR * * @param msg Message */ public void error(String msg) { log.error(msg); } /** * Logs a message and an exception with priority ERROR * * @param msg Message * @param t Exception */ public void error(String msg, Throwable t) { log.error(msg, t); }} //BaseCommand
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -