J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2005 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | package sun.security.provider.certpath; |
| 27 | |
| 28 | import sun.security.util.Debug; |
| 29 | import java.security.cert.X509Certificate; |
| 30 | |
| 31 | /** |
| 32 | * Describes one step of a certification path build, consisting of a |
| 33 | * <code>Vertex</code> state description, a certificate, a possible throwable, |
| 34 | * and a result code. |
| 35 | * |
| 36 | * @author Anne Anderson |
| 37 | * @since 1.4 |
| 38 | * @see sun.security.provider.certpath.Vertex |
| 39 | */ |
| 40 | public class BuildStep { |
| 41 | |
| 42 | private static final Debug debug = Debug.getInstance("certpath"); |
| 43 | private Vertex vertex; |
| 44 | private X509Certificate cert; |
| 45 | private Throwable throwable; |
| 46 | private int result; |
| 47 | |
| 48 | /** |
| 49 | * result code associated with a certificate that may continue a path from |
| 50 | * the current certificate. |
| 51 | */ |
| 52 | public static final int POSSIBLE = 1; |
| 53 | |
| 54 | /** |
| 55 | * result code associated with a certificate that was tried, but that |
| 56 | * represents an unsuccessful path, so the certificate has been backed out |
| 57 | * to allow backtracking to the next possible path. |
| 58 | */ |
| 59 | public static final int BACK = 2; |
| 60 | |
| 61 | /** |
| 62 | * result code associated with a certificate that successfully continues the |
| 63 | * current path, but does not yet reach the target. |
| 64 | */ |
| 65 | public static final int FOLLOW = 3; |
| 66 | |
| 67 | /** |
| 68 | * result code associated with a certificate that represents the end of the |
| 69 | * last possible path, where no path successfully reached the target. |
| 70 | */ |
| 71 | public static final int FAIL = 4; |
| 72 | |
| 73 | /** |
| 74 | * result code associated with a certificate that represents the end of a |
| 75 | * path that successfully reaches the target. |
| 76 | */ |
| 77 | public static final int SUCCEED = 5; |
| 78 | |
| 79 | /** |
| 80 | * construct a BuildStep |
| 81 | * |
| 82 | * @param vtx description of the vertex at this step |
| 83 | * @param res result, where result is one of POSSIBLE, BACK, |
| 84 | * FOLLOW, FAIL, SUCCEED |
| 85 | */ |
| 86 | public BuildStep(Vertex vtx, int res) { |
| 87 | vertex = vtx; |
| 88 | if (vertex != null) { |
| 89 | cert = (X509Certificate)vertex.getCertificate(); |
| 90 | throwable = vertex.getThrowable(); |
| 91 | } |
| 92 | result = res; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * return vertex description for this build step |
| 97 | * |
| 98 | * @returns Vertex |
| 99 | */ |
| 100 | public Vertex getVertex() { |
| 101 | return vertex; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * return the certificate associated with this build step |
| 106 | * |
| 107 | * @returns X509Certificate |
| 108 | */ |
| 109 | public X509Certificate getCertificate() { |
| 110 | return cert; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * return string form of issuer name from certificate associated with this |
| 115 | * build step |
| 116 | * |
| 117 | * @returns String form of issuer name or null, if no certificate. |
| 118 | */ |
| 119 | public String getIssuerName() { |
| 120 | return (cert == null ? null : cert.getIssuerX500Principal().toString()); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * return string form of issuer name from certificate associated with this |
| 125 | * build step, or a default name if no certificate associated with this |
| 126 | * build step, or if issuer name could not be obtained from the certificate. |
| 127 | * |
| 128 | * @param defaultName name to use as default if unable to return an issuer |
| 129 | * name from the certificate, or if no certificate. |
| 130 | * @returns String form of issuer name or defaultName, if no certificate or |
| 131 | * exception received while trying to extract issuer name from certificate. |
| 132 | */ |
| 133 | public String getIssuerName(String defaultName) { |
| 134 | return (cert == null ? defaultName |
| 135 | : cert.getIssuerX500Principal().toString()); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * return string form of subject name from certificate associated with this |
| 140 | * build step. |
| 141 | * |
| 142 | * @returns String form of subject name or null, if no certificate. |
| 143 | */ |
| 144 | public String getSubjectName() { |
| 145 | return (cert == null ? null : cert.getSubjectX500Principal().toString()); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * return string form of subject name from certificate associated with this |
| 150 | * build step, or a default name if no certificate associated with this |
| 151 | * build step, or if subject name could not be obtained from the |
| 152 | * certificate. |
| 153 | * |
| 154 | * @param defaultName name to use as default if unable to return a subject |
| 155 | * name from the certificate, or if no certificate. |
| 156 | * @returns String form of subject name or defaultName, if no certificate or |
| 157 | * if an exception was received while attempting to extract the subject name |
| 158 | * from the certificate. |
| 159 | */ |
| 160 | public String getSubjectName(String defaultName) { |
| 161 | return (cert == null ? defaultName |
| 162 | : cert.getSubjectX500Principal().toString()); |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * return the exception associated with this build step. |
| 167 | * |
| 168 | * @returns Throwable |
| 169 | */ |
| 170 | public Throwable getThrowable() { |
| 171 | return throwable; |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * return the result code associated with this build step. The result codes |
| 176 | * are POSSIBLE, FOLLOW, BACK, FAIL, SUCCEED. |
| 177 | * |
| 178 | * @returns int result code |
| 179 | */ |
| 180 | public int getResult() { |
| 181 | return result; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * return a string representing the meaning of the result code associated |
| 186 | * with this build step. |
| 187 | * |
| 188 | * @param res result code |
| 189 | * @returns String string representing meaning of the result code |
| 190 | */ |
| 191 | public String resultToString(int res) { |
| 192 | String resultString = ""; |
| 193 | switch (res) { |
| 194 | case BuildStep.POSSIBLE: |
| 195 | resultString = "Certificate to be tried.\n"; |
| 196 | break; |
| 197 | case BuildStep.BACK: |
| 198 | resultString = "Certificate backed out since path does not " |
| 199 | + "satisfy build requirements.\n"; |
| 200 | break; |
| 201 | case BuildStep.FOLLOW: |
| 202 | resultString = "Certificate satisfies conditions.\n"; |
| 203 | break; |
| 204 | case BuildStep.FAIL: |
| 205 | resultString = "Certificate backed out since path does not " |
| 206 | + "satisfy conditions.\n"; |
| 207 | break; |
| 208 | case BuildStep.SUCCEED: |
| 209 | resultString = "Certificate satisfies conditions.\n"; |
| 210 | break; |
| 211 | default: |
| 212 | resultString = "Internal error: Invalid step result value.\n"; |
| 213 | } |
| 214 | return resultString; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * return a string representation of this build step, showing minimal |
| 219 | * detail. |
| 220 | * |
| 221 | * @returns String |
| 222 | */ |
| 223 | public String toString() { |
| 224 | String out = "Internal Error\n"; |
| 225 | switch (result) { |
| 226 | case BACK: |
| 227 | case FAIL: |
| 228 | out = resultToString(result); |
| 229 | out = out + vertex.throwableToString(); |
| 230 | break; |
| 231 | case FOLLOW: |
| 232 | case SUCCEED: |
| 233 | case POSSIBLE: |
| 234 | out = resultToString(result); |
| 235 | break; |
| 236 | default: |
| 237 | out = "Internal Error: Invalid step result\n"; |
| 238 | } |
| 239 | return out; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * return a string representation of this build step, showing all detail of |
| 244 | * the vertex state appropriate to the result of this build step, and the |
| 245 | * certificate contents. |
| 246 | * |
| 247 | * @returns String |
| 248 | */ |
| 249 | public String verboseToString() { |
| 250 | String out = resultToString(getResult()); |
| 251 | switch (result) { |
| 252 | case BACK: |
| 253 | case FAIL: |
| 254 | out = out + vertex.throwableToString(); |
| 255 | break; |
| 256 | case FOLLOW: |
| 257 | case SUCCEED: |
| 258 | out = out + vertex.moreToString(); |
| 259 | break; |
| 260 | case POSSIBLE: |
| 261 | break; |
| 262 | default: |
| 263 | break; |
| 264 | } |
| 265 | out = out + "Certificate contains:\n" + vertex.certToString(); |
| 266 | return out; |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * return a string representation of this build step, including all possible |
| 271 | * detail of the vertex state, but not including the certificate contents. |
| 272 | * |
| 273 | * @returns String |
| 274 | */ |
| 275 | public String fullToString() { |
| 276 | String out = resultToString(getResult()); |
| 277 | out = out + vertex.toString(); |
| 278 | return out; |
| 279 | } |
| 280 | } |