1 13 package org.ejbca.core.model.approval; 14 15 import java.io.ByteArrayInputStream ; 16 import java.io.Externalizable ; 17 import java.io.IOException ; 18 import java.io.ObjectInput ; 19 import java.io.ObjectOutput ; 20 import java.security.cert.CertificateException ; 21 import java.security.cert.CertificateFactory ; 22 import java.security.cert.X509Certificate ; 23 import java.util.List ; 24 25 import javax.ejb.CreateException ; 26 import javax.ejb.EJBException ; 27 28 import org.apache.commons.lang.StringUtils; 29 import org.apache.log4j.Logger; 30 import org.ejbca.core.ejb.ServiceLocator; 31 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocal; 32 import org.ejbca.core.ejb.ca.caadmin.ICAAdminSessionLocalHome; 33 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocal; 34 import org.ejbca.core.ejb.ca.store.ICertificateStoreSessionLocalHome; 35 import org.ejbca.core.ejb.hardtoken.IHardTokenSessionLocal; 36 import org.ejbca.core.ejb.hardtoken.IHardTokenSessionLocalHome; 37 import org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionLocal; 38 import org.ejbca.core.ejb.ra.raadmin.IRaAdminSessionLocalHome; 39 import org.ejbca.core.model.SecConst; 40 import org.ejbca.core.model.log.Admin; 41 import org.ejbca.util.Base64; 42 import org.ejbca.util.CertTools; 43 44 45 46 61 62 public abstract class ApprovalRequest implements Externalizable { 63 64 private static final long serialVersionUID = -1L; 65 66 private static final Logger log = Logger.getLogger(ApprovalRequest.class); 67 68 private static final int LATEST_VERSION = 2; 69 70 74 public static final int REQUESTTYPE_SIMPLE = 1; 75 76 81 public static final int REQUESTTYPE_COMPARING = 2; 82 83 87 protected static final long DEFAULT_REQUESTVALIDITY = 28800 * 1000; 88 protected static final String DEFAULT_REQUESTVALIDITYSTRING = "@approval.defaultrequestvalidity@"; 89 90 94 protected static final long DEFAULT_APPROVALVALIDITY = 28800 * 1000; 95 protected static final String DEFAULT_APPROVALVALIDITYSTRING = "@approval.defaultapprovalvalidity@"; 96 97 private Admin requestAdmin = null; 99 private String requestSignature = null; 100 101 private int approvalRequestType = REQUESTTYPE_SIMPLE; 102 103 private int numOfRequiredApprovals = 0; 104 105 private int cAId = 0; 106 107 private int endEntityProfileId = 0; 108 109 118 protected ApprovalRequest(Admin requestAdmin, String requestSignature, 119 int approvalRequestType, int numOfRequiredApprovals, int cAId, int endEntityProfileId) { 120 super(); 121 122 setRequestAdmin(requestAdmin); 123 this.requestSignature = requestSignature; 124 this.approvalRequestType = approvalRequestType; 125 this.numOfRequiredApprovals = numOfRequiredApprovals; 126 this.cAId = cAId; 127 this.endEntityProfileId = endEntityProfileId; 128 } 129 130 133 public ApprovalRequest(){ 134 } 135 136 142 public abstract boolean isExecutable(); 143 144 151 public abstract void execute() throws ApprovalRequestExecutionException; 152 153 158 public abstract int generateApprovalId(); 159 160 167 public abstract List getNewRequestDataAsText(Admin admin); 168 169 179 public abstract List getOldRequestDataAsText(Admin admin); 180 181 189 public boolean isAllowedTransition() { 190 return false; 191 } 192 193 199 public long getRequestValidity(){ 200 long ret = DEFAULT_REQUESTVALIDITY; 201 if (StringUtils.isNotEmpty(DEFAULT_REQUESTVALIDITYSTRING)) { 202 ret = Long.parseLong(DEFAULT_REQUESTVALIDITYSTRING) * 1000; 203 } 204 return ret; 205 } 206 207 213 public long getApprovalValidity(){ 214 long ret = DEFAULT_APPROVALVALIDITY; 215 if (StringUtils.isNotEmpty(DEFAULT_APPROVALVALIDITYSTRING)) { 216 ret = Long.parseLong(DEFAULT_APPROVALVALIDITYSTRING) * 1000; 217 } 218 return ret; 219 } 220 221 222 225 public abstract int getApprovalType(); 226 227 228 229 232 public int getNumOfRequiredApprovals(){ 233 return numOfRequiredApprovals; 234 } 235 236 237 241 public int getApprovalRequestType() { 242 return approvalRequestType; 243 } 244 245 248 public String getRequestSignature() { 249 return requestSignature; 250 } 251 252 257 public int getCAId() { 258 return cAId; 259 } 260 261 266 public int getEndEntityProfileId() { 267 return endEntityProfileId; 268 } 269 270 271 private void setRequestAdmin(Admin requestAdmin) { 272 this.requestAdmin = requestAdmin; 273 } 274 275 278 public X509Certificate getRequestAdminCert() { 279 return requestAdmin.getAdminInformation().getX509Certificate(); 280 } 281 282 283 public Admin getRequestAdmin() { 284 return requestAdmin; 285 } 286 287 288 public void writeExternal(ObjectOutput out) throws IOException { 289 out.writeInt(LATEST_VERSION); 290 out.writeObject(this.requestAdmin); 291 out.writeObject(this.requestSignature); 292 out.writeInt(this.approvalRequestType); 293 out.writeInt(this.numOfRequiredApprovals); 294 out.writeInt(this.cAId); 295 out.writeInt(this.endEntityProfileId); 296 } 297 298 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 299 300 int version = in.readInt(); 301 if(version == 1){ 302 String requestAdminCert = (String ) in.readObject(); 303 byte[] certbuf = Base64.decode(requestAdminCert.getBytes()); 304 CertificateFactory cf = CertTools.getCertificateFactory(); 305 X509Certificate x509cert = null; 306 try { 307 x509cert = (X509Certificate )cf.generateCertificate(new ByteArrayInputStream (certbuf)); 308 } catch (CertificateException e) { 309 log.error(e); 310 } 311 this.requestAdmin = new Admin(x509cert); 312 313 this.requestSignature = (String ) in.readObject(); 314 this.approvalRequestType = in.readInt(); 315 this.numOfRequiredApprovals = in.readInt(); 316 this.cAId = in.readInt(); 317 this.endEntityProfileId = in.readInt(); 318 } 319 if(version == 2){ 320 this.requestAdmin = (Admin) in.readObject(); 321 this.requestSignature = (String ) in.readObject(); 322 this.approvalRequestType = in.readInt(); 323 this.numOfRequiredApprovals = in.readInt(); 324 this.cAId = in.readInt(); 325 this.endEntityProfileId = in.readInt(); 326 } 327 328 } 329 330 protected String getCAName(Admin admin,int caid){ 332 String caname; 333 334 try { 335 ServiceLocator locator = ServiceLocator.getInstance(); 336 ICAAdminSessionLocalHome home = (ICAAdminSessionLocalHome) locator.getLocalHome(ICAAdminSessionLocalHome.COMP_NAME); 337 ICAAdminSessionLocal session = home.create(); 338 caname = session.getCAInfo(admin, caid).getName(); 339 340 } catch (CreateException e) { 341 throw new EJBException (e); 342 } 343 344 return caname; 345 } 346 347 protected String getEndEntityProfileName(Admin admin,int profileid){ 348 String name; 349 350 try { 351 ServiceLocator locator = ServiceLocator.getInstance(); 352 IRaAdminSessionLocalHome home = (IRaAdminSessionLocalHome) locator.getLocalHome(IRaAdminSessionLocalHome.COMP_NAME); 353 IRaAdminSessionLocal session = home.create(); 354 name = session.getEndEntityProfileName(admin, profileid); 355 } catch (CreateException e) { 356 throw new EJBException (e); 357 } 358 359 return name; 360 361 } 362 363 protected String getCertificateProfileName(Admin admin,int profileid){ 364 String name; 365 366 try { 367 ServiceLocator locator = ServiceLocator.getInstance(); 368 ICertificateStoreSessionLocalHome home = (ICertificateStoreSessionLocalHome) locator.getLocalHome(ICertificateStoreSessionLocalHome.COMP_NAME); 369 ICertificateStoreSessionLocal session = home.create(); 370 name = session.getCertificateProfileName(admin, profileid); 371 } catch (CreateException e) { 372 throw new EJBException (e); 373 } 374 375 return name; 376 } 377 378 protected ApprovalDataText getTokenName(Admin admin,int tokenid){ 379 ApprovalDataText retval; 380 381 try { 382 if(tokenid <= SecConst.TOKEN_SOFT ){ 383 int tokenindex=0; 384 for(int i=0;i<SecConst.TOKENIDS.length;i++){ 385 if(SecConst.TOKENIDS[i] == tokenid){ 386 tokenindex = i; 387 } 388 } 389 retval = new ApprovalDataText("TOKEN" ,SecConst.TOKENTEXTS[tokenindex],true,true); 390 391 }else{ 392 ServiceLocator locator = ServiceLocator.getInstance(); 393 IHardTokenSessionLocalHome home = (IHardTokenSessionLocalHome) locator.getLocalHome(IHardTokenSessionLocalHome.COMP_NAME); 394 IHardTokenSessionLocal session = home.create(); 395 String name = session.getHardTokenProfileName(admin, tokenid); 396 retval = new ApprovalDataText("TOKEN" ,name,true,false); 397 } 398 } catch (CreateException e) { 399 throw new EJBException (e); 400 } 401 402 return retval; 403 } 404 405 protected String getHardTokenIssuerName(Admin admin,int issuerid){ 406 String name; 407 408 try { 409 ServiceLocator locator = ServiceLocator.getInstance(); 410 IHardTokenSessionLocalHome home = (IHardTokenSessionLocalHome) locator.getLocalHome(IHardTokenSessionLocalHome.COMP_NAME); 411 IHardTokenSessionLocal session = home.create(); 412 name = session.getHardTokenIssuerAlias(admin, issuerid); 413 } catch (CreateException e) { 414 throw new EJBException (e); 415 } 416 417 return name; 418 } 419 420 } 421 | Popular Tags |