📄 ocspservlet.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.protocol;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.security.cert.X509Certificate;import java.util.Collection;import java.util.Date;import java.util.Enumeration;import java.util.Hashtable;import java.util.Iterator;import javax.servlet.ServletConfig;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;import org.bouncycastle.asn1.ASN1InputStream;import org.bouncycastle.asn1.ASN1OctetString;import org.bouncycastle.asn1.ASN1Sequence;import org.bouncycastle.asn1.DERGeneralizedTime;import org.bouncycastle.asn1.DERObjectIdentifier;import org.bouncycastle.asn1.ocsp.OCSPObjectIdentifiers;import org.bouncycastle.asn1.ocsp.RevokedInfo;import org.bouncycastle.asn1.x509.CRLReason;import org.bouncycastle.asn1.x509.X509Extension;import org.bouncycastle.asn1.x509.X509Extensions;import org.bouncycastle.ocsp.BasicOCSPResp;import org.bouncycastle.ocsp.BasicOCSPRespGenerator;import org.bouncycastle.ocsp.CertificateID;import org.bouncycastle.ocsp.CertificateStatus;import org.bouncycastle.ocsp.OCSPException;import org.bouncycastle.ocsp.OCSPReq;import org.bouncycastle.ocsp.OCSPResp;import org.bouncycastle.ocsp.OCSPRespGenerator;import org.bouncycastle.ocsp.Req;import org.bouncycastle.ocsp.RevokedStatus;import org.bouncycastle.ocsp.UnknownStatus;import se.anatom.ejbca.ca.caadmin.extendedcaservices.ExtendedCAServiceNotActiveException;import se.anatom.ejbca.ca.caadmin.extendedcaservices.ExtendedCAServiceRequestException;import se.anatom.ejbca.ca.caadmin.extendedcaservices.IllegalExtendedCAServiceRequestException;import se.anatom.ejbca.ca.caadmin.extendedcaservices.OCSPCAServiceRequest;import se.anatom.ejbca.ca.caadmin.extendedcaservices.OCSPCAServiceResponse;import se.anatom.ejbca.ca.crl.RevokedCertInfo;import se.anatom.ejbca.ca.exception.CADoesntExistsException;import se.anatom.ejbca.ca.exception.SignRequestException;import se.anatom.ejbca.ca.exception.SignRequestSignatureException;import se.anatom.ejbca.ca.sign.ISignSessionLocal;import se.anatom.ejbca.ca.sign.ISignSessionLocalHome;import se.anatom.ejbca.ca.store.CertificateDataBean;import se.anatom.ejbca.ca.store.ICertificateStoreSessionLocal;import se.anatom.ejbca.ca.store.ICertificateStoreSessionLocalHome;import se.anatom.ejbca.log.Admin;import se.anatom.ejbca.protocol.exception.MalformedRequestException;import se.anatom.ejbca.protocol.exception.NotSupportedException;import se.anatom.ejbca.util.CertTools;import se.anatom.ejbca.util.Hex;import se.anatom.ejbca.util.ServiceLocator;/** * Servlet implementing server side of the Online Certificate Status Protocol (OCSP) * For a detailed description of OCSP refer to RFC2560. * * @web.servlet name = "OCSP" * display-name = "OCSPServlet" * description="Answers OCSP requests" * load-on-startup = "99" * * @web.servlet-mapping url-pattern = "/ocsp" * * @web.servlet-init-param description="Algorithm used by server to generate signature on OCSP responses" * name="SignatureAlgorithm" * value="SHA1WithRSA" * * @web.servlet-init-param description="If set to true the servlet will enforce OCSP request signing" * name="enforceRequestSigning" * value="false" * * @web.servlet-init-param description="If set to true the certificate chain will be returned with the OCSP response" * name="includeCertChain" * value="true" * * @web.servlet-init-param description="If set to true the OCSP reponses will be signed directly by the CAs certificate instead of the CAs OCSP responder" * name="useCASigningCert" * value="${ocsp.usecasigningcert}" * * @web.servlet-init-param description="Specifies the subject of a certificate which is used to identifiy the responder which will generate responses when no real CA can be found from the request. This is used to generate 'unknown' responses when a request is received for a certificate that is not signed by any CA on this server" * name="defaultResponderID" * value="${ocsp.defaultresponder}" * * * @web.ejb-local-ref * name="ejb/CertificateStoreSessionLocal" * type="Session" * link="CertificateStoreSession" * home="se.anatom.ejbca.ca.store.ICertificateStoreSessionLocalHome" * local="se.anatom.ejbca.ca.store.ICertificateStoreSessionLocal" * * @web.ejb-local-ref * name="ejb/RSASignSessionLocal" * type="Session" * link="RSASignSession" * home="se.anatom.ejbca.ca.sign.ISignSessionLocalHome" * local="se.anatom.ejbca.ca.sign.ISignSessionLocal" * * @web.ejb-local-ref * name="ejb/CAAdminSessionLocal" * type="Session" * link="CAAdminSession" * home="se.anatom.ejbca.ca.caadmin.ICAAdminSessionLocalHome" * local="se.anatom.ejbca.ca.caadmin.ICAAdminSessionLocal" * * @author Thomas Meckel (Ophios GmbH), Tomas Gustavsson * @version $Id: OCSPServlet.java,v 1.40 2005/05/27 14:50:53 anatom Exp $ */public class OCSPServlet extends HttpServlet { private static Logger m_log = Logger.getLogger(OCSPServlet.class); private ICertificateStoreSessionLocal m_certStore; private ISignSessionLocal m_signsession = null; private Admin m_adm; private String m_sigAlg; private boolean m_reqMustBeSigned; private Collection m_cacerts = null; /** Cache time counter */ private long m_certValidTo = 0; /** Cached list of cacerts is valid 5 minutes */ private static final long VALID_TIME = 5 * 60 * 1000; /** String used to identify default responder id, used to generatwe responses when a request * for a certificate not signed by a CA on this server is received. */ private String m_defaultResponderId; /** Marks if the CAs certificate or the CAs OCSP responder certificate should be used for * signing the OCSP response. Defined in web.xml */ private boolean m_useCASigningCert; /** Marks if the CAs certificate chain shoudl be included in the OCSP response or not * Defined in web.xml */ private boolean m_includeChain; /** Loads cacertificates but holds a cache so it's reloaded only every five minutes is needed. */ protected synchronized void loadCertificates() throws IOException { // Kolla om vi har en cachad collection och om den inte ?r f?r gammal if (m_cacerts != null && m_certValidTo > new Date().getTime()) { return; } try { m_cacerts = m_certStore.findCertificatesByType(m_adm, CertificateDataBean.CERTTYPE_SUBCA + CertificateDataBean.CERTTYPE_ROOTCA, null); m_certValidTo = new Date().getTime() + VALID_TIME; } catch (Exception e) { m_log.error("Unable to load CA certificates from CA store.", e); throw new IOException(e.toString()); } } protected X509Certificate findCAByHash(CertificateID certId, Collection certs) throws OCSPException { if (null == certId) { throw new IllegalArgumentException(); } if (null == certs || certs.isEmpty()) { m_log.info("The passed certificate collection is empty."); return null; } Iterator iter = certs.iterator(); while (iter.hasNext()) { X509Certificate cacert = (X509Certificate) iter.next(); CertificateID issuerId = new CertificateID(certId.getHashAlgOID(), cacert, cacert.getSerialNumber()); if (m_log.isDebugEnabled()) { m_log.debug("Comparing the following certificate hashes:\n" + " Hash algorithm : '" + certId.getHashAlgOID() + "'\n" + " CA certificate hashes\n" + " Name hash : '" + Hex.encode(issuerId.getIssuerNameHash()) + "'\n" + " Key hash : '" + Hex.encode(issuerId.getIssuerKeyHash()) + "'\n" + " OCSP certificate hashes\n" + " Name hash : '" + Hex.encode(certId.getIssuerNameHash()) + "'\n" + " Key hash : '" + Hex.encode(certId.getIssuerKeyHash()) + "'\n"); } if ((issuerId.toASN1Object().getIssuerNameHash().equals(certId.toASN1Object().getIssuerNameHash())) && (issuerId.toASN1Object().getIssuerKeyHash().equals(certId.toASN1Object().getIssuerKeyHash()))) { m_log.debug("Found matching CA-cert with:\n" + " Name hash : '" + Hex.encode(issuerId.getIssuerNameHash()) + "'\n" + " Key hash : '" + Hex.encode(issuerId.getIssuerKeyHash()) + "'\n"); return cacert; } } m_log.debug("Did not find matching CA-cert for:\n" + " Name hash : '" + Hex.encode(certId.getIssuerNameHash()) + "'\n" + " Key hash : '" + Hex.encode(certId.getIssuerKeyHash()) + "'\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -