1 13 14 package org.ejbca.core.protocol.cmp; 15 16 import java.io.ByteArrayInputStream ; 17 import java.rmi.RemoteException ; 18 import java.util.Properties ; 19 20 import javax.ejb.CreateException ; 21 22 import org.apache.commons.lang.StringUtils; 23 import org.apache.log4j.Logger; 24 import org.bouncycastle.asn1.ASN1InputStream; 25 import org.ejbca.core.model.log.Admin; 26 import org.ejbca.core.protocol.FailInfo; 27 import org.ejbca.core.protocol.IResponseMessage; 28 import org.ejbca.core.protocol.ResponseStatus; 29 import org.ejbca.util.CertTools; 30 31 import com.novosec.pkix.asn1.cmp.PKIBody; 32 import com.novosec.pkix.asn1.cmp.PKIHeader; 33 import com.novosec.pkix.asn1.cmp.PKIMessage; 34 35 55 public class CmpMessageDispatcher { 56 private static final Logger log = Logger.getLogger(CmpMessageDispatcher.class); 57 58 61 private boolean allowRaVerifyPopo = false; 62 63 private String defaultCA = null; 64 65 private String extractUsernameComponent = null; 66 private Admin admin; 67 68 private Properties properties; 69 70 public CmpMessageDispatcher(Admin adm, Properties prop) { 71 this.admin = adm; 72 this.properties = prop; 73 CertTools.installBCProvider(); 75 76 String str = prop.getProperty("allowRaVerifyPopo"); 78 if (StringUtils.equals("true", str)) { 79 log.debug("allowRAVerifyPopo=true"); 80 allowRaVerifyPopo = true; 81 } 82 str = prop.getProperty("defaultCA"); 83 log.debug("defaultCA="+str); 84 if (StringUtils.isNotEmpty(str)) { 85 defaultCA = str; 86 } 87 str = prop.getProperty("extractUsernameComponent"); 88 log.debug("extractUsernameComponent="+str); 89 if (StringUtils.isNotEmpty(str)) { 90 extractUsernameComponent = str; 91 } 92 } 93 94 99 public IResponseMessage dispatch(byte[] message) { 100 IResponseMessage ret = null; 101 try { 102 PKIMessage req = null; 103 try { 104 req = PKIMessage.getInstance(new ASN1InputStream(new ByteArrayInputStream (message)).readObject()); 105 } catch (Exception e) { 106 ret = CmpMessageHelper.createUnprotectedErrorMessage(null, ResponseStatus.FAILURE, FailInfo.BAD_REQUEST, "Can not parse request message"); 108 return ret; 109 } 110 PKIHeader header = req.getHeader(); 111 PKIBody body = req.getBody(); 112 113 int tagno = -1; 114 if (log.isDebugEnabled()) { 115 tagno = body.getTagNo(); 116 log.debug("Received CMP message with pvno="+header.getPvno()+", sender="+header.getSender()+", recipient="+header.getRecipient()); 117 log.debug("Body is of type: "+tagno); 118 log.debug(req); 119 } 121 BaseCmpMessage cmpMessage = null; 122 ICmpMessageHandler handler = null; 123 int unknownMessageType = -1; 124 switch (tagno) { 125 case 0: 126 handler = new CrmfMessageHandler(admin, properties); 128 cmpMessage = new CrmfRequestMessage(req, defaultCA, allowRaVerifyPopo, extractUsernameComponent); 129 break; 130 case 2: 131 handler = new CrmfMessageHandler(admin, properties); 132 cmpMessage = new CrmfRequestMessage(req, defaultCA, allowRaVerifyPopo, extractUsernameComponent); 133 break; 134 case 19: 135 handler = new ConfirmationMessageHandler(properties); 137 cmpMessage = new GeneralCmpMessage(req); 138 break; 139 case 24: 140 handler = new ConfirmationMessageHandler(properties); 142 cmpMessage = new GeneralCmpMessage(req); 143 break; 144 case 11: 145 handler = new RevocationMessageHandler(admin, properties); 147 cmpMessage = new GeneralCmpMessage(req); 148 break; 149 default: 150 unknownMessageType = tagno; 151 break; 152 } 153 if ( (handler != null) && (cmpMessage != null) ) { 154 ret = handler.handleMessage(cmpMessage); 155 if (ret != null) { 156 log.debug("Received a response message from CmpMessageHandler."); 157 } else { 158 log.error("CmpMessageHandler returned a null message"); 159 } 160 } else { 161 log.error("Something is null! Handler="+handler+", cmpMessage="+cmpMessage); 162 if (unknownMessageType > -1) { 163 log.error("Unknown message type "+unknownMessageType+" received, creating error message"); 164 ret = CmpMessageHelper.createUnprotectedErrorMessage(null, ResponseStatus.FAILURE, FailInfo.BAD_REQUEST, "Can not handle message type"); 165 } 166 167 } 168 } catch (CreateException e) { 169 log.error("Exception during CMP processing: ", e); 170 } catch (RemoteException e) { 171 log.error("Exception during CMP processing: ", e); 172 } 173 174 return ret; 175 } 176 177 } 178 | Popular Tags |