1 13 package org.ejbca.core.model.approval.approvalrequests; 14 15 import java.io.IOException ; 16 import java.io.ObjectInput ; 17 import java.io.ObjectOutput ; 18 import java.security.cert.CertificateEncodingException ; 19 import java.security.cert.CertificateException ; 20 import java.security.cert.X509Certificate ; 21 import java.util.ArrayList ; 22 import java.util.List ; 23 24 import javax.ejb.CreateException ; 25 import javax.ejb.EJBException ; 26 27 import org.apache.log4j.Logger; 28 import org.ejbca.core.ejb.ServiceLocator; 29 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionLocal; 30 import org.ejbca.core.ejb.keyrecovery.IKeyRecoverySessionLocalHome; 31 import org.ejbca.core.model.approval.ApprovalDataText; 32 import org.ejbca.core.model.approval.ApprovalDataVO; 33 import org.ejbca.core.model.approval.ApprovalException; 34 import org.ejbca.core.model.approval.ApprovalRequest; 35 import org.ejbca.core.model.approval.ApprovalRequestExecutionException; 36 import org.ejbca.core.model.approval.WaitingForApprovalException; 37 import org.ejbca.core.model.authorization.AuthorizationDeniedException; 38 import org.ejbca.core.model.log.Admin; 39 import org.ejbca.util.Base64; 40 import org.ejbca.util.CertTools; 41 42 51 public class KeyRecoveryApprovalRequest extends ApprovalRequest { 52 53 private static final long serialVersionUID = -1L; 54 55 private static final Logger log = Logger.getLogger(KeyRecoveryApprovalRequest.class); 56 57 private static final int LATEST_VERSION = 1; 58 59 private String username; 60 private X509Certificate cert; 61 62 63 private boolean recoverNewestCert = false; 64 65 66 67 70 public KeyRecoveryApprovalRequest() {} 71 72 73 public KeyRecoveryApprovalRequest(X509Certificate cert, String username, boolean recoverNewestCert, Admin requestAdmin, String requestSignature, int numOfReqApprovals, int cAId, int endEntityProfileId) { 74 super(requestAdmin, requestSignature, REQUESTTYPE_SIMPLE, 75 numOfReqApprovals, cAId, endEntityProfileId); 76 this.username = username; 77 this.cert = cert; 78 this.recoverNewestCert = recoverNewestCert; 79 } 80 81 82 public void execute() throws ApprovalRequestExecutionException { 83 log.debug("Executing mark for recovery for user:" + username); 84 try{ 85 ServiceLocator locator = ServiceLocator.getInstance(); 86 IKeyRecoverySessionLocalHome keyrechome = (IKeyRecoverySessionLocalHome) locator.getLocalHome(IKeyRecoverySessionLocalHome.COMP_NAME); 87 IKeyRecoverySessionLocal keyrecsession = keyrechome.create(); 88 89 if(recoverNewestCert){ 90 keyrecsession.markNewestAsRecoverable(getRequestAdmin(), username, getEndEntityProfileId()); 91 }else{ 92 keyrecsession.markAsRecoverable(getRequestAdmin(), cert, getEndEntityProfileId()); 93 } 94 95 96 }catch (CreateException e) { 97 throw new ApprovalRequestExecutionException("Error creating new userdata session", e); 98 } catch (AuthorizationDeniedException e) { 99 throw new ApprovalRequestExecutionException("Authorization Denied :" + e.getMessage(), e); 100 } catch (ApprovalException e) { 101 throw new EJBException ("This should never happen",e); 102 } catch (WaitingForApprovalException e) { 103 throw new EJBException ("This should never happen",e); 104 } 105 106 } 107 108 111 public int generateApprovalId() { 112 return new String (getApprovalType() + ";" + username).hashCode(); 113 } 114 115 116 public int getApprovalType() { 117 return ApprovalDataVO.APPROVALTYPE_KEYRECOVERY; 118 } 119 120 121 public List getNewRequestDataAsText(Admin admin) { 122 ArrayList retval = new ArrayList (); 123 retval.add(new ApprovalDataText("USERNAME",username,true,false)); 124 retval.add(new ApprovalDataText("CERTSERIALNUMBER",cert.getSerialNumber().toString(16),true,false)); 125 retval.add(new ApprovalDataText("SUBJECTDN",cert.getSubjectDN().toString(),true,false)); 126 retval.add(new ApprovalDataText("ISSUERDN",cert.getIssuerDN().toString(),true,false)); 127 return retval; 128 } 129 130 public List getOldRequestDataAsText(Admin admin) { 131 return null; 132 } 133 134 135 public boolean isExecutable() { 136 return true; 137 } 138 139 public void writeExternal(ObjectOutput out) throws IOException { 140 super.writeExternal(out); 141 out.writeInt(LATEST_VERSION); 142 out.writeObject(username); 143 out.writeBoolean(recoverNewestCert); 144 try { 145 String certString = new String (Base64.encode(cert.getEncoded()),"UTF8"); 146 out.writeObject(certString); 147 } catch (CertificateEncodingException e) { 148 log.debug("Error serializing certificate", e); 149 throw new IOException (e.getMessage()); 150 } 151 152 } 153 154 public void readExternal(ObjectInput in) throws IOException , ClassNotFoundException { 155 super.readExternal(in); 156 int version = in.readInt(); 157 if(version == 1){ 158 username = (String ) in.readObject(); 159 recoverNewestCert = in.readBoolean(); 160 String certString = (String ) in.readObject(); 161 try { 162 cert = CertTools.getCertfromByteArray(Base64.decode(certString.getBytes("UTF8"))); 163 } catch (CertificateException e) { 164 log.debug("Error deserializing certificate", e); 165 throw new IOException (e.getMessage()); 166 } 167 } 168 169 } 170 171 } 172 | Popular Tags |