📄 ocspservletbase.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 org.ejbca.ui.web.protocol;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.math.BigInteger;import java.security.cert.Certificate;import java.security.cert.X509Certificate;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Date;import java.util.HashMap;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.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.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 org.bouncycastle.util.encoders.Hex;import org.ejbca.core.ejb.ca.store.CertificateDataBean;import org.ejbca.core.model.InternalResources;import org.ejbca.core.model.ca.MalformedRequestException;import org.ejbca.core.model.ca.SignRequestException;import org.ejbca.core.model.ca.SignRequestSignatureException;import org.ejbca.core.model.ca.caadmin.CADoesntExistsException;import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceNotActiveException;import org.ejbca.core.model.ca.caadmin.extendedcaservices.ExtendedCAServiceRequestException;import org.ejbca.core.model.ca.caadmin.extendedcaservices.IllegalExtendedCAServiceRequestException;import org.ejbca.core.model.ca.caadmin.extendedcaservices.OCSPCAServiceRequest;import org.ejbca.core.model.ca.caadmin.extendedcaservices.OCSPCAServiceResponse;import org.ejbca.core.model.ca.crl.RevokedCertInfo;import org.ejbca.core.model.log.Admin;import org.ejbca.core.protocol.ocsp.IOCSPExtension;import org.ejbca.core.protocol.ocsp.OCSPResponseItem;import org.ejbca.util.CertTools;/** * @web.servlet-init-param description="Algorithm used by server to generate signature on OCSP responses" * name="SignatureAlgorithm" * value="${ocsp.signaturealgorithm}" * * @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.servlet-init-param description="Specifies OCSP extension oids that will result in a call to an extension class, separate multiple entries with ;" * name="extensionOid" * value="${ocsp.extensionoid}" * * @web.servlet-init-param description="Specifies classes implementing OCSP extensions matching oids above, separate multiple entries with ;" * name="extensionClass" * value="${ocsp.extensionclass}" * * @web.servlet-init-param description="Specifies classes implementing OCSP extensions matching oids above, separate multiple entries with ;" * name="unidDataSource" * value="${ocsp.uniddatsource}" * * @web.servlet-init-param description="Directory containing certificates of trusted entities allowed to query for Fnrs." * name="unidTrustDir" * value="${ocsp.unidtrustdir}" * * @web.servlet-init-param description="File containing the CA-certificate, in PEM format, that signed the trusted clients." * name="unidCACert" * value="${ocsp.unidcacert}" * * @author Thomas Meckel (Ophios GmbH), Tomas Gustavsson, Lars Silven * @version $Id: OCSPServletBase.java,v 1.28 2007/01/16 11:46:14 anatom Exp $ */abstract class OCSPServletBase extends HttpServlet { private static final Logger m_log = Logger.getLogger(OCSPServletBase.class); /** Internal localization of logs and errors */ private static final InternalResources intres = InternalResources.getInstance(); private Admin m_adm; private String m_sigAlg; private boolean m_reqMustBeSigned; 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; /** Configures OCSP extensions, these init-params are optional */ private Collection m_extensionOids = new ArrayList(); private Collection m_extensionClasses = new ArrayList(); private HashMap m_extensionMap = null; /** Loads cacertificates but holds a cache so it's reloaded only every five minutes is needed. */ protected synchronized void loadCertificates() throws IOException, ServletException { // 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; } m_cacerts = findCertificatesByType(m_adm, CertificateDataBean.CERTTYPE_SUBCA + CertificateDataBean.CERTTYPE_ROOTCA, null); if (m_log.isDebugEnabled()) { m_log.debug("Loaded "+m_cacerts == null ? "0":m_cacerts.size()+" ca certificates"); } loadPrivateKeys(m_adm); m_certValidTo = new Date().getTime() + VALID_TIME; } abstract protected void loadPrivateKeys(Admin adm) throws ServletException, IOException; abstract protected Collection findCertificatesByType(Admin adm, int i, String issuerDN); abstract protected Certificate findCertificateByIssuerAndSerno(Admin adm, String issuerDN, BigInteger serno); abstract protected OCSPCAServiceResponse extendedService(Admin m_adm2, int caid, OCSPCAServiceRequest request) throws CADoesntExistsException, ExtendedCAServiceRequestException, IllegalExtendedCAServiceRequestException, ExtendedCAServiceNotActiveException; abstract protected RevokedCertInfo isRevoked(Admin m_adm2, String name, BigInteger serialNumber); protected X509Certificate findCAByHash(CertificateID certId, Collection certs) throws OCSPException { if (null == certId) { throw new IllegalArgumentException(); } if (null == certs || certs.isEmpty()) { String iMsg = intres.getLocalizedMessage("ocsp.certcollectionempty"); m_log.info(iMsg); return null; } Iterator iter = certs.iterator(); while (iter.hasNext()) { X509Certificate cacert = (X509Certificate) iter.next(); try { 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\n" + " CA SubjectDN: '" + cacert.getSubjectDN().getName() + "'\n" + " SerialNumber: '" + cacert.getSerialNumber().toString(16) + "'\n" + " CA certificate hashes\n" + " Name hash : '" + new String(Hex.encode(issuerId.getIssuerNameHash())) + "'\n" + " Key hash : '" + new String(Hex.encode(issuerId.getIssuerKeyHash())) + "'\n" + " OCSP certificate hashes\n" + " Name hash : '" + new String(Hex.encode(certId.getIssuerNameHash())) + "'\n" + " Key hash : '" + new String(Hex.encode(certId.getIssuerKeyHash())) + "'\n"); } if ((issuerId.toASN1Object().getIssuerNameHash().equals(certId.toASN1Object().getIssuerNameHash())) && (issuerId.toASN1Object().getIssuerKeyHash().equals(certId.toASN1Object().getIssuerKeyHash()))) { if (m_log.isDebugEnabled()) { m_log.debug("Found matching CA-cert with:\n" + " Name hash : '" + new String(Hex.encode(issuerId.getIssuerNameHash())) + "'\n" + " Key hash : '" + new String(Hex.encode(issuerId.getIssuerKeyHash())) + "'\n"); } return cacert; } } catch (OCSPException e) { String errMsg = intres.getLocalizedMessage("ocsp.errorcomparehash", cacert.getIssuerDN()); m_log.error(errMsg, e); } } if (m_log.isDebugEnabled()) { m_log.debug("Did not find matching CA-cert for:\n" + " Name hash : '" + new String(Hex.encode(certId.getIssuerNameHash())) + "'\n" + " Key hash : '" + new String(Hex.encode(certId.getIssuerKeyHash())) + "'\n"); } return null; } protected X509Certificate findCertificateBySubject(String subjectDN, Collection certs) { if (certs == null || null == subjectDN) { throw new IllegalArgumentException(); } if (null == certs || certs.isEmpty()) { String iMsg = intres.getLocalizedMessage("ocsp.certcollectionempty"); m_log.info(iMsg); return null; } String dn = CertTools.stringToBCDNString(subjectDN); Iterator iter = certs.iterator(); while (iter.hasNext()) { X509Certificate cacert = (X509Certificate) iter.next(); if (m_log.isDebugEnabled()) { m_log.debug("Comparing the following certificates:\n" + " CA certificate DN: " + cacert.getSubjectDN() + "\n Subject DN: " + dn); } if (dn.equalsIgnoreCase(CertTools.stringToBCDNString(cacert.getSubjectDN().getName()))) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -