Path: blob/master/src/java.base/share/classes/sun/security/provider/certpath/Vertex.java
41161 views
/*1* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.security.provider.certpath;2627import java.io.IOException;28import java.security.cert.CertificateException;29import java.security.cert.X509Certificate;3031import sun.security.util.Debug;32import sun.security.x509.AuthorityKeyIdentifierExtension;33import sun.security.x509.KeyIdentifier;34import sun.security.x509.SubjectKeyIdentifierExtension;35import sun.security.x509.X509CertImpl;3637/*38* This class represents a vertex in the adjacency list. A39* vertex in the builder's view is just a distinguished name40* in the directory. The Vertex contains a certificate41* along an attempted certification path, along with a pointer42* to a list of certificates that followed this one in various43* attempted certification paths.44*45* @author Sean Mullan46* @since 1.447*/48public class Vertex {4950private static final Debug debug = Debug.getInstance("certpath");51private X509Certificate cert;52private int index;53private Throwable throwable;5455/**56* Constructor; creates vertex with index of -157* Use setIndex method to set another index.58*59* @param cert X509Certificate associated with vertex60*/61Vertex(X509Certificate cert) {62this.cert = cert;63this.index = -1;64}6566/**67* return the certificate for this vertex68*69* @return X509Certificate70*/71public X509Certificate getCertificate() {72return cert;73}7475/**76* get the index for this vertex, where the index is the row of the77* adjacency list that contains certificates that could follow this78* certificate.79*80* @return int index for this vertex, or -1 if no following certificates.81*/82public int getIndex() {83return index;84}8586/**87* set the index for this vertex, where the index is the row of the88* adjacency list that contains certificates that could follow this89* certificate.90*91* @param ndx int index for vertex, or -1 if no following certificates.92*/93void setIndex(int ndx) {94index = ndx;95}9697/**98* return the throwable associated with this vertex;99* returns null if none.100*101* @return Throwable102*/103public Throwable getThrowable() {104return throwable;105}106107/**108* set throwable associated with this vertex; default value is null.109*110* @param throwable Throwable associated with this vertex111* (or null)112*/113void setThrowable(Throwable throwable) {114this.throwable = throwable;115}116117/**118* Return full string representation of vertex119*120* @return String representation of vertex121*/122@Override123public String toString() {124return certToString() + throwableToString() + indexToString();125}126127/**128* Return string representation of this vertex's129* certificate information.130*131* @return String representation of certificate info132*/133public String certToString() {134StringBuilder sb = new StringBuilder();135136X509CertImpl x509Cert = null;137try {138x509Cert = X509CertImpl.toImpl(cert);139} catch (CertificateException ce) {140if (debug != null) {141debug.println("Vertex.certToString() unexpected exception");142ce.printStackTrace();143}144return sb.toString();145}146147sb.append("Issuer: ").append148(x509Cert.getIssuerX500Principal()).append("\n");149sb.append("Subject: ").append150(x509Cert.getSubjectX500Principal()).append("\n");151sb.append("SerialNum: ").append152(x509Cert.getSerialNumber().toString(16)).append("\n");153sb.append("Expires: ").append154(x509Cert.getNotAfter().toString()).append("\n");155boolean[] iUID = x509Cert.getIssuerUniqueID();156if (iUID != null) {157sb.append("IssuerUID: ");158for (boolean b : iUID) {159sb.append(b ? 1 : 0);160}161sb.append("\n");162}163boolean[] sUID = x509Cert.getSubjectUniqueID();164if (sUID != null) {165sb.append("SubjectUID: ");166for (boolean b : sUID) {167sb.append(b ? 1 : 0);168}169sb.append("\n");170}171try {172SubjectKeyIdentifierExtension sKeyID =173x509Cert.getSubjectKeyIdentifierExtension();174if (sKeyID != null) {175KeyIdentifier keyID = sKeyID.get(176SubjectKeyIdentifierExtension.KEY_ID);177sb.append("SubjKeyID: ").append(keyID.toString());178}179AuthorityKeyIdentifierExtension aKeyID =180x509Cert.getAuthorityKeyIdentifierExtension();181if (aKeyID != null) {182KeyIdentifier keyID = (KeyIdentifier)aKeyID.get(183AuthorityKeyIdentifierExtension.KEY_ID);184sb.append("AuthKeyID: ").append(keyID.toString());185}186} catch (IOException e) {187if (debug != null) {188debug.println("Vertex.certToString() unexpected exception");189e.printStackTrace();190}191}192return sb.toString();193}194195/**196* return Vertex throwable as String compatible with197* the way toString returns other information198*199* @return String form of exception (or "none")200*/201public String throwableToString() {202StringBuilder sb = new StringBuilder("Exception: ");203if (throwable != null)204sb.append(throwable.toString());205else206sb.append("null");207sb.append("\n");208return sb.toString();209}210211/**212* return Vertex index as String compatible with213* the way other Vertex.xToString() methods display214* information.215*216* @return String form of index as "Last cert? [Yes/No]217*/218public String moreToString() {219StringBuilder sb = new StringBuilder("Last cert? ");220sb.append((index == -1) ? "Yes" : "No");221sb.append("\n");222return sb.toString();223}224225/**226* return Vertex index as String compatible with227* the way other Vertex.xToString() methods displays other information.228*229* @return String form of index as "Index: [numeric index]"230*/231public String indexToString() {232return "Index: " + index + "\n";233}234}235236237