⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 certificateinfo.java

📁 一个java开发的非常全面的关于证书发放
💻 JAVA
字号:
/*
  Name:         CertificateInfo.java
  Licensing:    LGPL

  API:          Sun (http://java.sun.com) JCE 1.2.2 API (cleanroom implementation by Bouncy Castle)
  Provider:     Bouncy Castle (http://www.bouncycastle.org)

  Disclaimer:

  COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY OF ANY KIND,
  EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES THAT THE COVERED CODE
  IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE OR NON-INFRINGING. THE ENTIRE
  RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED CODE IS WITH YOU. SHOULD ANY COVERED CODE
  PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR)
  ASSUME THE COST OF ANY NECESSARY SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY
  CONSTITUTES AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED
  HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
*/

package net.sourceforge.jcetaglib.taglib.x509;

import net.sourceforge.jcetaglib.lib.CertTools;
import net.sourceforge.jcetaglib.lib.Clean;
import net.sourceforge.jcetaglib.lib.X509Cert;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.Security;
import java.security.cert.X509Certificate;

/**
 * JSP tag used retrieving information from a X.509 certificate
 *
 * @jsp.tag
 * name="certificateinfo"
 * display-name="CertificateInfo"
 * body-content="empty"
 * example="<%-- Prints the certificate information --%>

 * <jce:certificateinfo

 * 	storefile=\"C:/keystores/bob.p12\"

 * 	storeentry=\"user\"

 * 	storepassword=\"password\"/>"
 *
 * description="JSP tag used retrieving information from a X.509 certificate"
 *
 * @author Gert Van Ham
 * @author hamgert@users.sourceforge.net
 * @author http://jcetaglib.sourceforge.net
 * @version $Id: CertificateInfo.java,v 1.7 2004/04/15 07:28:36 hamgert Exp $
 */

public class CertificateInfo extends TagSupport {
    private static final String PAGE = "page";
    private static final String REQUEST = "request";
    private static final String SESSION = "session";
    private static final String APPLICATION = "application";

    // return info
    private int scope = PageContext.PAGE_SCOPE;
    private String subject;
    private String issuer;
    private String validfrom;
    private String validto;
    private String algorithm;
    private String serialnumber;
    private String fingerprint;

    /* Attributes for X.509 keystore */

    // P12 keystore...
    private String storefile;            // tag attribute
    private String storeentry;           // tag attribute
    private StringBuffer storepassword;        // tag attribute

    // ... OR PEM string
    private String pemstring;            // tag attribute

    // ... OR PEM file
    private String pemfile;              // tag attribute

    public static int getScope(String scope) {
        int ret = PageContext.PAGE_SCOPE; // default

        if (REQUEST.equalsIgnoreCase(scope))
            ret = PageContext.REQUEST_SCOPE;
        else if (SESSION.equalsIgnoreCase(scope))
            ret = PageContext.SESSION_SCOPE;
        else if (APPLICATION.equalsIgnoreCase(scope))
            ret = PageContext.APPLICATION_SCOPE;
        else if (PAGE.equalsIgnoreCase(scope))
            ret = PageContext.PAGE_SCOPE;

        return ret;
    } //getScope()

    public int doEndTag() throws JspException {
        Security.addProvider(new BouncyCastleProvider());

        X509Certificate cert = null;

        try {
            // Retrieve the certificate from one of the three possible keystores
            if (storefile == null || storefile == "") {
                if (pemfile == null || pemfile == "") {
                    // use PEM string
                    InputStream pemstream = new ByteArrayInputStream(pemstring.getBytes());
                    cert = CertTools.getCertfromPEM(pemstream);
                } else {
                    // use PEM store
                    cert = CertTools.getCertfromPEM(pemfile);
                }
            } else {
                // use PKCS #12 keystore
                cert = X509Cert.getCertificateFromP12(storefile, storeentry, storepassword);
            }

            if (subject == null || subject == "") {
                // print values
                try {
                    JspWriter w = pageContext.getOut();
                    w.print("For: " + cert.getSubjectDN() + "<BR>");
                    w.print("Issued by: " + cert.getIssuerDN() + "<BR>");
                    w.print("Valid from " + cert.getNotBefore() + " to " + cert.getNotAfter() + "<BR>");
                    w.print("Certificate SN#: " + cert.getSerialNumber() + "<BR>");
                    w.print("Generated with: " + cert.getSigAlgName() + "<BR>");
                    w.print("Fingerprint: " + CertTools.getFingerprintAsString(cert) + "<BR>");
                } catch (IOException ex) {
                    throw new JspException(ex.getMessage(), ex);
                }
            } else {
                // return values
                pageContext.setAttribute(subject, cert.getSubjectDN(), scope);
                pageContext.setAttribute(issuer, cert.getIssuerDN(), scope);
                pageContext.setAttribute(validfrom, cert.getNotBefore(), scope);
                pageContext.setAttribute(validto, cert.getNotAfter(), scope);
                pageContext.setAttribute(algorithm, cert.getSigAlgName(), scope);
                pageContext.setAttribute(serialnumber, cert.getSerialNumber(), scope);
                pageContext.setAttribute(fingerprint, CertTools.getFingerprintAsString(cert), scope);
            }
        } catch (Exception e) {
            throw new JspException("JCE Exception: Could not retrieve certificate info: " + e.getMessage(), e);
        }

        return EVAL_PAGE;
    } // doEndTag()

    public void release() {
        // Cleanup all sensitive information
        Clean.blank(storepassword);

        super.release();
    } //release()

    /**
     * @jsp.attribute
     *     description="Scope of the return variables. Can be 'page', 'session', 'request' or 'application'. Default is 'page'"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="false"
     */
    public void setScope(String scope) {
        this.scope = getScope(scope);
    }

    /**
     * @jsp.attribute
     *     description="The PKCS#12 (P12) keystore where the certificate is stored"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setStorefile(String storefile) {
        this.storefile = storefile;
    }

    /**
     * @jsp.attribute
     *     description=" The PKCS#12 (P12) keystore entry name for this certificate"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setStoreentry(String storeentry) {
        this.storeentry = storeentry;
    }

    /**
     * @jsp.attribute
     *     description="The PKCS#12 (P12) keystore password"
     *     type="java.lang.StringBuffer"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setStorepassword(StringBuffer storepassword) {
        this.storepassword = storepassword;
    }

    /**
     * @jsp.attribute
     *     description="The certificate as a PEM formatted file"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setPemfile(String pemfile) {
        this.pemfile = pemfile;
    }

    /**
     * @jsp.attribute
     *     description="The certificate as a PEM formatted string"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setPemstring(String pemstring) {
        this.pemstring = pemstring;
    }

    /**
     * @jsp.attribute
     *     description="Return variable for storing the certificate's subject"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getSubject() {
        return subject;
    }

    /**
     * @jsp.attribute
     *     description="Return variable for storing the certificate's issuer"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setIssuer(String issuer) {
        this.issuer = issuer;
    }

    public String getIssuer() {
        return issuer;
    }

    /**
     * @jsp.attribute
     *     description="Return variable for storing the certificate's valid from field"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setValidfrom(String validfrom) {
        this.validfrom = validfrom;
    }

    public String getValidfrom() {
        return validfrom;
    }

    /**
     * @jsp.attribute
     *     description="Return variable for storing the certificate's valid to field"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setValidto(String validto) {
        this.validto = validto;
    }

    public String getValidto() {
        return validto;
    }

    /**
     * @jsp.attribute
     *     description="Return variable for storing the certificate's algorithm"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setAlgorithm(String algorithm) {
        this.algorithm = algorithm;
    }

    public String getAlgorithm() {
        return algorithm;
    }

    /**
     * @jsp.attribute
     *     description="Return variable for storing the certificate's serialnumber"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setSerialnumber(String serialnumber) {
        this.serialnumber = serialnumber;
    }

    public String getSerialnumber() {
        return serialnumber;
    }

    /**
     * @jsp.attribute
     *     description="Return variable for storing the certificate's fingerprint"
     *     type="java.lang.String"
     *     required="false"
     *     rtexprvalue="true"
     */
    public void setFingerprint(String fingerprint) {
        this.fingerprint = fingerprint;
    }

    public String getFingerprint() {
        return fingerprint;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -