📄 x509certificate.java
字号:
/* X509Certificate.java -- X.509 certificate. Copyright (C) 2003 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., 59 Temple Place, Suite 330, Boston, MA02111-1307 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 java.io.ByteArrayInputStream;import java.io.InputStream;import java.io.IOException;import java.io.ObjectStreamException;import java.io.Serializable;import java.math.BigInteger;import java.net.InetAddress;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.spec.DSAParameterSpec;import java.security.spec.DSAPublicKeySpec;import java.security.spec.RSAPublicKeySpec;import java.util.ArrayList;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.LinkedList;import java.util.List;import java.util.Set;import javax.security.auth.x500.X500Principal;import gnu.java.io.ASN1ParsingException;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.der.DERWriter;/** * An implementation of X.509 certificates. * * @author Casey Marshall (rsdio@metastatic.org) */public class X509Certificate extends java.security.cert.X509Certificate implements Serializable{ // Constants and fields. // ------------------------------------------------------------------------ private static final OID ID_DSA = new OID("1.2.840.10040.4.1"); private static final OID ID_DSA_WITH_SHA1 = new OID("1.2.840.10040.4.3"); private static final OID ID_RSA = new OID("1.2.840.113549.1.1.1"); private static final OID ID_RSA_WITH_MD2 = new OID("1.2.840.113549.1.1.2"); private static final OID ID_RSA_WITH_MD5 = new OID("1.2.840.113549.1.1.4"); private static final OID ID_RSA_WITH_SHA1 = new OID("1.2.840.113549.1.1.5"); private static final OID ID_EXTENSION = new OID("2.5.29"); private static final OID ID_KEY_USAGE = ID_EXTENSION.getChild(15); private static final OID ID_BASIC_CONSTRAINTS = ID_EXTENSION.getChild(19); private static final OID ID_EXT_KEY_USAGE = ID_EXTENSION.getChild(37); private static final int OTHER_NAME = 0; private static final int RFC882_NAME = 1; private static final int DNS_NAME = 2; private static final int X400_ADDRESS = 3; private static final int DIRECTORY_NAME = 4; private static final int EDI_PARTY_NAME = 5; private static final int URI = 6; private static final int IP_ADDRESS = 7; private static final int REGISTERED_ID = 8; // This object SHOULD be serialized with an instance of // java.security.cert.Certificate.CertificateRep, thus all fields are // transient. // The encoded certificate. private transient byte[] encoded; // TBSCertificate part. private transient byte[] tbsCertBytes; private transient int version; private transient BigInteger serialNo; private transient OID algId; private transient byte[] algVal; private transient X500Principal issuer; private transient Date notBefore; private transient Date notAfter; private transient X500Principal subject; private transient PublicKey subjectKey; private transient BitString issuerUniqueId; private transient BitString subjectUniqueId; private transient HashMap extensions; private transient HashSet critOids; private transient HashSet nonCritOids; private transient BitString keyUsage; private transient int basicConstraints = -1; // Signature. private transient OID sigAlgId; private transient byte[] sigAlgVal; private 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(); critOids = new HashSet(); nonCritOids = new HashSet(); try { parse(encoded); } catch (IOException ioe) { throw ioe; } catch (Exception e) { throw new CertificateException(e.toString()); } } // 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 getIssuerX500Principal(); } public X500Principal getIssuerX500Principal() { return issuer; } public Principal getSubjectDN() { return getSubjectX500Principal(); } public X500Principal getSubjectX500Principal() { return subject; } 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"; // return sigAlgId.getShortName(); } 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() { if (keyUsage != null) return keyUsage.toBooleanArray(); return null; } public List getExtendedKeyUsage() throws CertificateParsingException { byte[] ext = (byte[]) extensions.get("2.5.29.37"); if (ext == null) return null; LinkedList usages = new LinkedList(); try { DERReader der = new DERReader(new ByteArrayInputStream(ext)); DERValue seq = der.read(); if (!seq.isConstructed()) throw new CertificateParsingException(); int len = 0; while (len < seq.getLength()) { DERValue oid = der.read(); if (!(oid.getValue() instanceof OID)) throw new CertificateParsingException(); usages.add(oid.getValue().toString()); len += DERWriter.definiteEncodingSize(oid.getLength()) + oid.getLength() + 1; } } catch (IOException ioe) { throw new CertificateParsingException(); } return usages; } public int getBasicConstraints() { return basicConstraints; } public Collection getSubjectAlternativeNames() throws CertificateParsingException { byte[] ext = getExtensionValue("2.5.29.17"); if (ext == null) return null; return getAltNames(ext); } public Collection getIssuerAlternativeNames()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -