Path: blob/master/src/java.base/share/classes/sun/security/provider/certpath/BuildStep.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.security.cert.X509Certificate;2829/**30* Describes one step of a certification path build, consisting of a31* <code>Vertex</code> state description, a certificate, a possible throwable,32* and a result code.33*34* @author Anne Anderson35* @since 1.436* @see sun.security.provider.certpath.Vertex37*/38public class BuildStep {3940private Vertex vertex;41private X509Certificate cert;42private Throwable throwable;43private int result;4445/**46* result code associated with a certificate that may continue a path from47* the current certificate.48*/49public static final int POSSIBLE = 1;5051/**52* result code associated with a certificate that was tried, but that53* represents an unsuccessful path, so the certificate has been backed out54* to allow backtracking to the next possible path.55*/56public static final int BACK = 2;5758/**59* result code associated with a certificate that successfully continues the60* current path, but does not yet reach the target.61*/62public static final int FOLLOW = 3;6364/**65* result code associated with a certificate that represents the end of the66* last possible path, where no path successfully reached the target.67*/68public static final int FAIL = 4;6970/**71* result code associated with a certificate that represents the end of a72* path that successfully reaches the target.73*/74public static final int SUCCEED = 5;7576/**77* construct a BuildStep78*79* @param vtx description of the vertex at this step80* @param res result, where result is one of POSSIBLE, BACK,81* FOLLOW, FAIL, SUCCEED82*/83public BuildStep(Vertex vtx, int res) {84vertex = vtx;85if (vertex != null) {86cert = vertex.getCertificate();87throwable = vertex.getThrowable();88}89result = res;90}9192/**93* return vertex description for this build step94*95* @return Vertex96*/97public Vertex getVertex() {98return vertex;99}100101/**102* return the certificate associated with this build step103*104* @return X509Certificate105*/106public X509Certificate getCertificate() {107return cert;108}109110/**111* return string form of issuer name from certificate associated with this112* build step113*114* @return String form of issuer name or null, if no certificate.115*/116public String getIssuerName() {117return getIssuerName(null);118}119120/**121* return string form of issuer name from certificate associated with this122* build step, or a default name if no certificate associated with this123* build step, or if issuer name could not be obtained from the certificate.124*125* @param defaultName name to use as default if unable to return an issuer126* name from the certificate, or if no certificate.127* @return String form of issuer name or defaultName, if no certificate or128* exception received while trying to extract issuer name from certificate.129*/130public String getIssuerName(String defaultName) {131return (cert == null ? defaultName132: cert.getIssuerX500Principal().toString());133}134135/**136* return string form of subject name from certificate associated with this137* build step.138*139* @return String form of subject name or null, if no certificate.140*/141public String getSubjectName() {142return getSubjectName(null);143}144145/**146* return string form of subject name from certificate associated with this147* build step, or a default name if no certificate associated with this148* build step, or if subject name could not be obtained from the149* certificate.150*151* @param defaultName name to use as default if unable to return a subject152* name from the certificate, or if no certificate.153* @return String form of subject name or defaultName, if no certificate or154* if an exception was received while attempting to extract the subject name155* from the certificate.156*/157public String getSubjectName(String defaultName) {158return (cert == null ? defaultName159: cert.getSubjectX500Principal().toString());160}161162/**163* return the exception associated with this build step.164*165* @return Throwable166*/167public Throwable getThrowable() {168return throwable;169}170171/**172* return the result code associated with this build step. The result codes173* are POSSIBLE, FOLLOW, BACK, FAIL, SUCCEED.174*175* @return int result code176*/177public int getResult() {178return result;179}180181/**182* return a string representing the meaning of the result code associated183* with this build step.184*185* @param res result code186* @return String string representing meaning of the result code187*/188public String resultToString(int res) {189String resultString = "";190switch (res) {191case POSSIBLE:192resultString = "Certificate to be tried.\n";193break;194case BACK:195resultString = "Certificate backed out since path does not "196+ "satisfy build requirements.\n";197break;198case FOLLOW:199resultString = "Certificate satisfies conditions.\n";200break;201case FAIL:202resultString = "Certificate backed out since path does not "203+ "satisfy conditions.\n";204break;205case SUCCEED:206resultString = "Certificate satisfies conditions.\n";207break;208default:209resultString = "Internal error: Invalid step result value.\n";210}211return resultString;212}213214/**215* return a string representation of this build step, showing minimal216* detail.217*218* @return String219*/220@Override221public String toString() {222String out = "Internal Error\n";223switch (result) {224case BACK:225case FAIL:226out = resultToString(result);227out = out + vertex.throwableToString();228break;229case FOLLOW:230case SUCCEED:231case POSSIBLE:232out = resultToString(result);233break;234default:235out = "Internal Error: Invalid step result\n";236}237return out;238}239240/**241* return a string representation of this build step, showing all detail of242* the vertex state appropriate to the result of this build step, and the243* certificate contents.244*245* @return String246*/247public String verboseToString() {248String out = resultToString(getResult());249switch (result) {250case BACK:251case FAIL:252out = out + vertex.throwableToString();253break;254case FOLLOW:255case SUCCEED:256out = out + vertex.moreToString();257break;258case POSSIBLE:259break;260default:261break;262}263out = out + "Certificate contains:\n" + vertex.certToString();264return out;265}266267/**268* return a string representation of this build step, including all possible269* detail of the vertex state, but not including the certificate contents.270*271* @return String272*/273public String fullToString() {274return resultToString(getResult()) + vertex.toString();275}276}277278279