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

📄 x509certificate.java

📁 linux下编程用 编译软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* X509Certificate.java -- X.509 certificate.   Copyright (C) 2003, 2004, 2006  Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package gnu.java.security.x509;import gnu.classpath.debug.Component;import gnu.classpath.debug.SystemLogger;import gnu.java.security.OID;import gnu.java.security.der.BitString;import gnu.java.security.der.DER;import gnu.java.security.der.DERReader;import gnu.java.security.der.DERValue;import gnu.java.security.x509.ext.BasicConstraints;import gnu.java.security.x509.ext.ExtendedKeyUsage;import gnu.java.security.x509.ext.Extension;import gnu.java.security.x509.ext.IssuerAlternativeNames;import gnu.java.security.x509.ext.KeyUsage;import gnu.java.security.x509.ext.SubjectAlternativeNames;import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;import java.io.Serializable;import java.io.StringWriter;import java.math.BigInteger;import java.security.AlgorithmParameters;import java.security.InvalidKeyException;import java.security.KeyFactory;import java.security.NoSuchAlgorithmException;import java.security.NoSuchProviderException;import java.security.Principal;import java.security.PublicKey;import java.security.Signature;import java.security.SignatureException;import java.security.cert.CertificateEncodingException;import java.security.cert.CertificateException;import java.security.cert.CertificateExpiredException;import java.security.cert.CertificateNotYetValidException;import java.security.cert.CertificateParsingException;import java.security.interfaces.DSAParams;import java.security.interfaces.DSAPublicKey;import java.security.spec.DSAParameterSpec;import java.security.spec.X509EncodedKeySpec;import java.util.ArrayList;import java.util.Arrays;import java.util.Collection;import java.util.Collections;import java.util.Date;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import java.util.logging.Level;import java.util.logging.Logger;import javax.security.auth.x500.X500Principal;/** * An implementation of X.509 certificates. * * @author Casey Marshall (rsdio@metastatic.org) */public class X509Certificate extends java.security.cert.X509Certificate  implements Serializable, GnuPKIExtension{  // Constants and fields.  // ------------------------------------------------------------------------  private static final Logger logger = SystemLogger.SYSTEM;  protected static final OID ID_DSA = new OID ("1.2.840.10040.4.1");  protected static final OID ID_DSA_WITH_SHA1 = new OID ("1.2.840.10040.4.3");  protected static final OID ID_RSA = new OID ("1.2.840.113549.1.1.1");  protected static final OID ID_RSA_WITH_MD2 = new OID ("1.2.840.113549.1.1.2");  protected static final OID ID_RSA_WITH_MD5 = new OID ("1.2.840.113549.1.1.4");  protected static final OID ID_RSA_WITH_SHA1 = new OID ("1.2.840.113549.1.1.5");  protected static final OID ID_ECDSA_WITH_SHA1 = new OID ("1.2.840.10045.4.1");  // This object SHOULD be serialized with an instance of  // java.security.cert.Certificate.CertificateRep, thus all fields are  // transient.  // The encoded certificate.  protected transient byte[] encoded;  // TBSCertificate part.  protected transient byte[] tbsCertBytes;  protected transient int version;  protected transient BigInteger serialNo;  protected transient OID algId;  protected transient byte[] algVal;  protected transient X500DistinguishedName issuer;  protected transient Date notBefore;  protected transient Date notAfter;  protected transient X500DistinguishedName subject;  protected transient PublicKey subjectKey;  protected transient BitString issuerUniqueId;  protected transient BitString subjectUniqueId;  protected transient Map extensions;  // Signature.  protected transient OID sigAlgId;  protected transient byte[] sigAlgVal;  protected transient byte[] signature;  // Constructors.  // ------------------------------------------------------------------------  /**   * Create a new X.509 certificate from the encoded data. The input   * data are expected to be the ASN.1 DER encoding of the certificate.   *   * @param encoded The encoded certificate data.   * @throws IOException If the certificate cannot be read, possibly   * from a formatting error.   * @throws CertificateException If the data read is not an X.509   * certificate.   */  public X509Certificate(InputStream encoded)    throws CertificateException, IOException  {    super();    extensions = new HashMap();    try      {        parse(encoded);      }    catch (IOException ioe)      {        logger.log (Component.X509, "", ioe);        throw ioe;      }    catch (Exception e)      {        logger.log (Component.X509, "", e);        CertificateException ce = new CertificateException(e.getMessage());        ce.initCause (e);        throw ce;      }  }  protected X509Certificate()  {    extensions = new HashMap();  }  // X509Certificate methods.  // ------------------------------------------------------------------------  public void checkValidity()    throws CertificateExpiredException, CertificateNotYetValidException  {    checkValidity(new Date());  }  public void checkValidity(Date date)    throws CertificateExpiredException, CertificateNotYetValidException  {    if (date.compareTo(notBefore) < 0)      {        throw new CertificateNotYetValidException();      }    if (date.compareTo(notAfter) > 0)      {        throw new CertificateExpiredException();      }  }  public int getVersion()  {    return version;  }  public BigInteger getSerialNumber()  {    return serialNo;  }  public Principal getIssuerDN()  {    return issuer;  }  public X500Principal getIssuerX500Principal()  {    return new X500Principal(issuer.getDer());  }  public Principal getSubjectDN()  {    return subject;  }  public X500Principal getSubjectX500Principal()  {    return new X500Principal(subject.getDer());  }  public Date getNotBefore()  {    return (Date) notBefore.clone();  }  public Date getNotAfter()  {    return (Date) notAfter.clone();  }  public byte[] getTBSCertificate() throws CertificateEncodingException  {    return (byte[]) tbsCertBytes.clone();  }  public byte[] getSignature()  {    return (byte[]) signature.clone();  }  public String getSigAlgName()  {    if (sigAlgId.equals(ID_DSA_WITH_SHA1))      {        return "SHA1withDSA";      }    if (sigAlgId.equals(ID_RSA_WITH_MD2))      {        return "MD2withRSA";      }    if (sigAlgId.equals(ID_RSA_WITH_MD5))      {        return "MD5withRSA";      }    if (sigAlgId.equals(ID_RSA_WITH_SHA1))      {        return "SHA1withRSA";      }    return "unknown";  }  public String getSigAlgOID()  {    return sigAlgId.toString();  }  public byte[] getSigAlgParams()  {    return (byte[]) sigAlgVal.clone();  }  public boolean[] getIssuerUniqueID()  {    if (issuerUniqueId != null)      {        return issuerUniqueId.toBooleanArray();      }    return null;  }  public boolean[] getSubjectUniqueID()  {    if (subjectUniqueId != null)      {        return subjectUniqueId.toBooleanArray();      }    return null;  }  public boolean[] getKeyUsage()  {    Extension e = getExtension(KeyUsage.ID);    if (e != null)      {        KeyUsage ku = (KeyUsage) e.getValue();        boolean[] result = new boolean[9];        boolean[] b = ku.getKeyUsage().toBooleanArray();        System.arraycopy(b, 0, result, 0, b.length);        return result;      }    return null;  }  public List getExtendedKeyUsage() throws CertificateParsingException  {    Extension e = getExtension(ExtendedKeyUsage.ID);    if (e != null)      {        List a = ((ExtendedKeyUsage) e.getValue()).getPurposeIds();        List b = new ArrayList(a.size());        for (Iterator it = a.iterator(); it.hasNext(); )          {            b.add(it.next().toString());          }        return Collections.unmodifiableList(b);      }    return null;  }  public int getBasicConstraints()  {    Extension e = getExtension(BasicConstraints.ID);    if (e != null)      {        return ((BasicConstraints) e.getValue()).getPathLengthConstraint();      }    return -1;  }  public Collection getSubjectAlternativeNames()    throws CertificateParsingException  {    Extension e = getExtension(SubjectAlternativeNames.ID);    if (e != null)      {        return ((SubjectAlternativeNames) e.getValue()).getNames();      }    return null;  }  public Collection getIssuerAlternativeNames()    throws CertificateParsingException  {    Extension e = getExtension(IssuerAlternativeNames.ID);    if (e != null)      {        return ((IssuerAlternativeNames) e.getValue()).getNames();      }    return null;  }// X509Extension methods.  // ------------------------------------------------------------------------

⌨️ 快捷键说明

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