1 13 14 package org.ejbca.core.model.ca.catoken; 15 16 import java.io.ByteArrayInputStream ; 17 import java.io.IOException ; 18 import java.security.PrivateKey ; 19 import java.security.PublicKey ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.Properties ; 23 24 import javax.ejb.EJBException ; 25 26 27 28 29 35 public class HardCATokenContainer extends CAToken{ 36 private IHardCAToken hardcatoken = null; 37 38 public static final float LATEST_VERSION = 1; 39 40 public static final int TYPE_CUSTOMPUBLISHERCONTAINER = 1; 41 42 44 protected static final String CLASSPATH = "classpath"; 45 protected static final String PROPERTYDATA = "propertydata"; 46 47 48 49 public HardCATokenContainer(){ 50 super(); 51 data.put(CATOKENTYPE, new Integer (CATokenInfo.CATOKENTYPE_HSM)); 52 setClassPath(null); 53 setPropertyData(""); 54 } 55 56 public HardCATokenContainer(HashMap data) { 57 loadData(data); 58 data.put(CATOKENTYPE, new Integer (CATokenInfo.CATOKENTYPE_HSM)); 59 } 60 61 63 66 public CATokenInfo getCATokenInfo() { 67 HardCATokenInfo info = new HardCATokenInfo(); 68 info.setClassPath(getClassPath()); 69 info.setProperties(getPropertyData()); 70 info.setSignatureAlgorithm(getSignatureAlgorithm()); 71 if ( hardcatoken!=null ){ 72 info.setCATokenStatus(hardcatoken.getCATokenStatus()); 73 }else{ 74 info.setCATokenStatus(IHardCAToken.STATUS_OFFLINE); 75 } 76 77 return info; 78 } 79 80 83 public void updateCATokenInfo(CATokenInfo catokeninfo) { 84 if (((HardCATokenInfo)catokeninfo).getClassPath() != null) { 86 this.setClassPath(((HardCATokenInfo)catokeninfo).getClassPath()); 87 this.hardcatoken = null; 88 } 89 if(getSignatureAlgorithm() == null) 90 this.setSignatureAlgorithm(catokeninfo.getSignatureAlgorithm()); 91 92 if(!this.getPropertyData().equals(((HardCATokenInfo)catokeninfo).getProperties())){ 93 this.setPropertyData(((HardCATokenInfo)catokeninfo).getProperties()); 94 this.hardcatoken = null; 95 } 96 } 97 98 101 public void activate(String authorizationcode) throws CATokenAuthenticationFailedException, CATokenOfflineException { 102 getHardCAToken().activate(authorizationcode); 103 } 104 105 108 public boolean deactivate() { 109 return getHardCAToken().deactivate(); 110 } 111 112 113 116 public PrivateKey getPrivateKey(int purpose) throws CATokenOfflineException{ 117 return getHardCAToken().getPrivateKey(purpose); 118 } 119 120 123 public PublicKey getPublicKey(int purpose) throws CATokenOfflineException{ 124 return getHardCAToken().getPublicKey(purpose); 125 } 126 127 128 131 public String getProvider() { 132 return getHardCAToken().getProvider(); 133 } 134 135 136 137 141 private String getClassPath(){ 142 return (String ) data.get(CLASSPATH); 143 } 144 145 148 private void setClassPath(String classpath){ 149 data.put(CLASSPATH, classpath); 150 } 151 152 155 private String getSignatureAlgorithm(){ 156 return (String ) data.get(SIGNATUREALGORITHM); 157 } 158 159 162 private void setSignatureAlgorithm(String signaturealgoritm){ 163 data.put(SIGNATUREALGORITHM, signaturealgoritm); 164 } 165 166 167 170 private String getPropertyData(){ 171 return (String ) data.get(PROPERTYDATA); 172 } 173 174 177 private void setPropertyData(String propertydata){ 178 data.put(PROPERTYDATA, propertydata); 179 } 180 181 private Properties getProperties() throws IOException { 182 Properties prop = new Properties (); 183 prop.load(new ByteArrayInputStream (getPropertyData().getBytes())); 184 return prop; 185 } 186 187 188 189 private IHardCAToken getHardCAToken() { 190 if(hardcatoken == null){ 191 try{ 192 Class implClass = Class.forName( getClassPath()); 193 Object obj = implClass.newInstance(); 194 this.hardcatoken = (IHardCAToken) obj; 195 this.hardcatoken.init(getProperties(), getSignatureAlgorithm()); 196 }catch(ClassNotFoundException e){ 197 throw new EJBException (e); 198 } 199 catch(IllegalAccessException iae){ 200 throw new EJBException (iae); 201 } 202 catch(IOException ioe){ 203 throw new EJBException (ioe); 204 } 205 catch(InstantiationException ie){ 206 throw new EJBException (ie); 207 } 208 } 209 210 return hardcatoken; 211 } 212 213 216 public Object clone() throws CloneNotSupportedException { 217 HardCATokenContainer clone = new HardCATokenContainer(); 218 HashMap clonedata = (HashMap ) clone.saveData(); 219 220 Iterator i = (data.keySet()).iterator(); 221 while(i.hasNext()){ 222 Object key = i.next(); 223 clonedata.put(key, data.get(key)); 224 } 225 226 clone.loadData(clonedata); 227 return clone; 228 } 229 230 233 public float getLatestVersion() { 234 return LATEST_VERSION; 235 } 236 237 238 239 public void upgrade() { 240 if(Float.compare(LATEST_VERSION, getVersion()) != 0) { 241 243 data.put(VERSION, new Float (LATEST_VERSION)); 244 } 245 } 246 247 248 } 249 | Popular Tags |