Showing the changes to java/pkg/sun/security/timestamp/TSResponse.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,384 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (c) 2003, 2004, Oracle and/or its affiliates. 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. Oracle designates this | ||
| 8 | * particular file as subject to the "Classpath" exception as provided | ||
| 9 | * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | ||
| 22 | * or visit www.oracle.com if you need additional information or have any | ||
| 23 | * questions. | ||
| 24 | */ | ||
| 25 | |||
| 26 | package sun.security.timestamp; | ||
| 27 | |||
| 28 | import java.io.IOException; | ||
| 29 | import java.math.BigInteger; | ||
| 30 | import sun.security.pkcs.PKCS7; | ||
| 31 | import sun.security.pkcs.PKCS9Attribute; | ||
| 32 | import sun.security.pkcs.PKCS9Attributes; | ||
| 33 | import sun.security.pkcs.ParsingException; | ||
| 34 | import sun.security.pkcs.SignerInfo; | ||
| 35 | import sun.security.util.DerValue; | ||
| 36 | import sun.security.x509.AlgorithmId; | ||
| 37 | import sun.security.x509.X500Name; | ||
| 38 | |||
| 39 | /** | ||
| 40 | * This class provides the response corresponding to a timestamp request, | ||
| 41 | * as defined in | ||
| 42 | * <a href="http://www.ietf.org/rfc/rfc3161.txt">RFC 3161</a>. | ||
| 43 | * | ||
| 44 | * The TimeStampResp ASN.1 type has the following definition: | ||
| 45 | * <pre> | ||
| 46 | * | ||
| 47 | * TimeStampResp ::= SEQUENCE { | ||
| 48 | * status PKIStatusInfo, | ||
| 49 | * timeStampToken TimeStampToken OPTIONAL ] | ||
| 50 | * | ||
| 51 | * PKIStatusInfo ::= SEQUENCE { | ||
| 52 | * status PKIStatus, | ||
| 53 | * statusString PKIFreeText OPTIONAL, | ||
| 54 | * failInfo PKIFailureInfo OPTIONAL } | ||
| 55 | * | ||
| 56 | * PKIStatus ::= INTEGER { | ||
| 57 | * granted (0), | ||
| 58 | * -- when the PKIStatus contains the value zero a TimeStampToken, as | ||
| 59 | * -- requested, is present. | ||
| 60 | * grantedWithMods (1), | ||
| 61 | * -- when the PKIStatus contains the value one a TimeStampToken, | ||
| 62 | * -- with modifications, is present. | ||
| 63 | * rejection (2), | ||
| 64 | * waiting (3), | ||
| 65 | * revocationWarning (4), | ||
| 66 | * -- this message contains a warning that a revocation is | ||
| 67 | * -- imminent | ||
| 68 | * revocationNotification (5) | ||
| 69 | * -- notification that a revocation has occurred } | ||
| 70 | * | ||
| 71 | * PKIFreeText ::= SEQUENCE SIZE (1..MAX) OF UTF8String | ||
| 72 | * -- text encoded as UTF-8 String (note: each UTF8String SHOULD | ||
| 73 | * -- include an RFC 1766 language tag to indicate the language | ||
| 74 | * -- of the contained text) | ||
| 75 | * | ||
| 76 | * PKIFailureInfo ::= BIT STRING { | ||
| 77 | * badAlg (0), | ||
| 78 | * -- unrecognized or unsupported Algorithm Identifier | ||
| 79 | * badRequest (2), | ||
| 80 | * -- transaction not permitted or supported | ||
| 81 | * badDataFormat (5), | ||
| 82 | * -- the data submitted has the wrong format | ||
| 83 | * timeNotAvailable (14), | ||
| 84 | * -- the TSA's time source is not available | ||
| 85 | * unacceptedPolicy (15), | ||
| 86 | * -- the requested TSA policy is not supported by the TSA | ||
| 87 | * unacceptedExtension (16), | ||
| 88 | * -- the requested extension is not supported by the TSA | ||
| 89 | * addInfoNotAvailable (17) | ||
| 90 | * -- the additional information requested could not be understood | ||
| 91 | * -- or is not available | ||
| 92 | * systemFailure (25) | ||
| 93 | * -- the request cannot be handled due to system failure } | ||
| 94 | * | ||
| 95 | * TimeStampToken ::= ContentInfo | ||
| 96 | * -- contentType is id-signedData | ||
| 97 | * -- content is SignedData | ||
| 98 | * -- eContentType within SignedData is id-ct-TSTInfo | ||
| 99 | * -- eContent within SignedData is TSTInfo | ||
| 100 | * | ||
| 101 | * </pre> | ||
| 102 | * | ||
| 103 | * @since 1.5 | ||
| 104 | * @author Vincent Ryan | ||
| 105 | * @see Timestamper | ||
| 106 | */ | ||
| 107 | |||
| 108 | public class TSResponse { | ||
| 109 | |||
| 110 | // Status codes (from RFC 3161) | ||
| 111 | |||
| 112 | /** | ||
| 113 | * The requested timestamp was granted. | ||
| 114 | */ | ||
| 115 | public static final int GRANTED = 0; | ||
| 116 | |||
| 117 | /** | ||
| 118 | * The requested timestamp was granted with some modifications. | ||
| 119 | */ | ||
| 120 | public static final int GRANTED_WITH_MODS = 1; | ||
| 121 | |||
| 122 | /** | ||
| 123 | * The requested timestamp was not granted. | ||
| 124 | */ | ||
| 125 | public static final int REJECTION = 2; | ||
| 126 | |||
| 127 | /** | ||
| 128 | * The requested timestamp has not yet been processed. | ||
| 129 | */ | ||
| 130 | public static final int WAITING = 3; | ||
| 131 | |||
| 132 | /** | ||
| 133 | * A warning that a certificate revocation is imminent. | ||
| 134 | */ | ||
| 135 | public static final int REVOCATION_WARNING = 4; | ||
| 136 | |||
| 137 | /** | ||
| 138 | * Notification that a certificate revocation has occurred. | ||
| 139 | */ | ||
| 140 | public static final int REVOCATION_NOTIFICATION = 5; | ||
| 141 | |||
| 142 | // Failure codes (from RFC 3161) | ||
| 143 | |||
| 144 | /** | ||
| 145 | * Unrecognized or unsupported algorithm identifier. | ||
| 146 | */ | ||
| 147 | public static final int BAD_ALG = 0; | ||
| 148 | |||
| 149 | /** | ||
| 150 | * The requested transaction is not permitted or supported. | ||
| 151 | */ | ||
| 152 | public static final int BAD_REQUEST = 2; | ||
| 153 | |||
| 154 | /** | ||
| 155 | * The data submitted has the wrong format. | ||
| 156 | */ | ||
| 157 | public static final int BAD_DATA_FORMAT = 5; | ||
| 158 | |||
| 159 | /** | ||
| 160 | * The TSA's time source is not available. | ||
| 161 | */ | ||
| 162 | public static final int TIME_NOT_AVAILABLE = 14; | ||
| 163 | |||
| 164 | /** | ||
| 165 | * The requested TSA policy is not supported by the TSA. | ||
| 166 | */ | ||
| 167 | public static final int UNACCEPTED_POLICY = 15; | ||
| 168 | |||
| 169 | /** | ||
| 170 | * The requested extension is not supported by the TSA. | ||
| 171 | */ | ||
| 172 | public static final int UNACCEPTED_EXTENSION = 16; | ||
| 173 | |||
| 174 | /** | ||
| 175 | * The additional information requested could not be understood or is not | ||
| 176 | * available. | ||
| 177 | */ | ||
| 178 | public static final int ADD_INFO_NOT_AVAILABLE = 17; | ||
| 179 | |||
| 180 | /** | ||
| 181 | * The request cannot be handled due to system failure. | ||
| 182 | */ | ||
| 183 | public static final int SYSTEM_FAILURE = 25; | ||
| 184 | |||
| 185 | private static final boolean DEBUG = false; | ||
| 186 | |||
| 187 | private int status; | ||
| 188 | |||
| 189 | private String[] statusString = null; | ||
| 190 | |||
| 191 | private int failureInfo = -1; | ||
| 192 | |||
| 193 | private byte[] encodedTsToken = null; | ||
| 194 | |||
| 195 | private PKCS7 tsToken = null; | ||
| 196 | |||
| 197 | /** | ||
| 198 | * Constructs an object to store the response to a timestamp request. | ||
| 199 | * | ||
| 200 | * @param status A buffer containing the ASN.1 BER encoded response. | ||
| 201 | * @throws IOException The exception is thrown if a problem is encountered | ||
| 202 | * parsing the timestamp response. | ||
| 203 | */ | ||
| 204 | TSResponse(byte[] tsReply) throws IOException { | ||
| 205 | parse(tsReply); | ||
| 206 | } | ||
| 207 | |||
| 208 | /** | ||
| 209 | * Retrieve the status code returned by the TSA. | ||
| 210 | */ | ||
| 211 | public int getStatusCode() { | ||
| 212 | return status; | ||
| 213 | } | ||
| 214 | |||
| 215 | /** | ||
| 216 | * Retrieve the status messages returned by the TSA. | ||
| 217 | * | ||
| 218 | * @return If null then no status messages were received. | ||
| 219 | */ | ||
| 220 | public String[] getStatusMessages() { | ||
| 221 | return statusString; | ||
| 222 | } | ||
| 223 | |||
| 224 | /** | ||
| 225 | * Retrieve the failure code returned by the TSA. | ||
| 226 | * | ||
| 227 | * @return If -1 then no failure code was received. | ||
| 228 | */ | ||
| 229 | public int getFailureCode() { | ||
| 230 | return failureInfo; | ||
| 231 | } | ||
| 232 | |||
| 233 | public String getStatusCodeAsText() { | ||
| 234 | |||
| 235 | switch (status) { | ||
| 236 | case GRANTED: | ||
| 237 | return "the timestamp request was granted."; | ||
| 238 | |||
| 239 | case GRANTED_WITH_MODS: | ||
| 240 | return | ||
| 241 | "the timestamp request was granted with some modifications."; | ||
| 242 | |||
| 243 | case REJECTION: | ||
| 244 | return "the timestamp request was rejected."; | ||
| 245 | |||
| 246 | case WAITING: | ||
| 247 | return "the timestamp request has not yet been processed."; | ||
| 248 | |||
| 249 | case REVOCATION_WARNING: | ||
| 250 | return "warning: a certificate revocation is imminent."; | ||
| 251 | |||
| 252 | case REVOCATION_NOTIFICATION: | ||
| 253 | return "notification: a certificate revocation has occurred."; | ||
| 254 | |||
| 255 | default: | ||
| 256 | return ("unknown status code " + status + "."); | ||
| 257 | } | ||
| 258 | } | ||
| 259 | |||
| 260 | public String getFailureCodeAsText() { | ||
| 261 | |||
| 262 | if (failureInfo == -1) { | ||
| 263 | return null; | ||
| 264 | } | ||
| 265 | |||
| 266 | switch (failureInfo) { | ||
| 267 | |||
| 268 | case BAD_ALG: | ||
| 269 | return "Unrecognized or unsupported alrorithm identifier."; | ||
| 270 | |||
| 271 | case BAD_REQUEST: | ||
| 272 | return "The requested transaction is not permitted or supported."; | ||
| 273 | |||
| 274 | case BAD_DATA_FORMAT: | ||
| 275 | return "The data submitted has the wrong format."; | ||
| 276 | |||
| 277 | case TIME_NOT_AVAILABLE: | ||
| 278 | return "The TSA's time source is not available."; | ||
| 279 | |||
| 280 | case UNACCEPTED_POLICY: | ||
| 281 | return "The requested TSA policy is not supported by the TSA."; | ||
| 282 | |||
| 283 | case UNACCEPTED_EXTENSION: | ||
| 284 | return "The requested extension is not supported by the TSA."; | ||
| 285 | |||
| 286 | case ADD_INFO_NOT_AVAILABLE: | ||
| 287 | return "The additional information requested could not be " + | ||
| 288 | "understood or is not available."; | ||
| 289 | |||
| 290 | case SYSTEM_FAILURE: | ||
| 291 | return "The request cannot be handled due to system failure."; | ||
| 292 | |||
| 293 | default: | ||
| 294 | return ("unknown status code " + status); | ||
| 295 | } | ||
| 296 | } | ||
| 297 | |||
| 298 | /** | ||
| 299 | * Retrieve the timestamp token returned by the TSA. | ||
| 300 | * | ||
| 301 | * @return If null then no token was received. | ||
| 302 | */ | ||
| 303 | public PKCS7 getToken() { | ||
| 304 | return tsToken; | ||
| 305 | } | ||
| 306 | |||
| 307 | /** | ||
| 308 | * Retrieve the ASN.1 BER encoded timestamp token returned by the TSA. | ||
| 309 | * | ||
| 310 | * @return If null then no token was received. | ||
| 311 | */ | ||
| 312 | public byte[] getEncodedToken() { | ||
| 313 | return encodedTsToken; | ||
| 314 | } | ||
| 315 | |||
| 316 | /* | ||
| 317 | * Parses the timestamp response. | ||
| 318 | * | ||
| 319 | * @param status A buffer containing the ASN.1 BER encoded response. | ||
| 320 | * @throws IOException The exception is thrown if a problem is encountered | ||
| 321 | * parsing the timestamp response. | ||
| 322 | */ | ||
| 323 | private void parse(byte[] tsReply) throws IOException { | ||
| 324 | // Decode TimeStampResp | ||
| 325 | |||
| 326 | DerValue derValue = new DerValue(tsReply); | ||
| 327 | if (derValue.tag != DerValue.tag_Sequence) { | ||
| 328 | throw new IOException("Bad encoding for timestamp response"); | ||
| 329 | } | ||
| 330 | |||
| 331 | // Parse status | ||
| 332 | |||
| 333 | DerValue status = derValue.data.getDerValue(); | ||
| 334 | // Parse status | ||
| 335 | this.status = status.data.getInteger(); | ||
| 336 | if (DEBUG) { | ||
| 337 | System.out.println("timestamp response: status=" + this.status); | ||
| 338 | } | ||
| 339 | // Parse statusString, if present | ||
| 340 | if (status.data.available() > 0) { | ||
| 341 | DerValue[] strings = status.data.getSequence(1); | ||
| 342 | statusString = new String[strings.length]; | ||
| 343 | for (int i = 0; i < strings.length; i++) { | ||
| 344 | statusString[i] = strings[i].data.getUTF8String(); | ||
| 345 | } | ||
| 346 | } | ||
| 347 | // Parse failInfo, if present | ||
| 348 | if (status.data.available() > 0) { | ||
| 349 | byte[] failInfo = status.data.getBitString(); | ||
| 350 | int failureInfo = (new Byte(failInfo[0])).intValue(); | ||
| 351 | if (failureInfo < 0 || failureInfo > 25 || failInfo.length != 1) { | ||
| 352 | throw new IOException("Bad encoding for timestamp response: " + | ||
| 353 | "unrecognized value for the failInfo element"); | ||
| 354 | } | ||
| 355 | this.failureInfo = failureInfo; | ||
| 356 | } | ||
| 357 | |||
| 358 | // Parse timeStampToken, if present | ||
| 359 | if (derValue.data.available() > 0) { | ||
| 360 | DerValue timestampToken = derValue.data.getDerValue(); | ||
| 361 | encodedTsToken = timestampToken.toByteArray(); | ||
| 362 | tsToken = new PKCS7(encodedTsToken); | ||
| 363 | } | ||
| 364 | |||
| 365 | // Check the format of the timestamp response | ||
| 366 | if (this.status == 0 || this.status == 1) { | ||
| 367 | if (tsToken == null) { | ||
| 368 | throw new TimestampException( | ||
| 369 | "Bad encoding for timestamp response: " + | ||
| 370 | "expected a timeStampToken element to be present"); | ||
| 371 | } | ||
| 372 | } else if (tsToken != null) { | ||
| 373 | throw new TimestampException( | ||
| 374 | "Bad encoding for timestamp response: " + | ||
| 375 | "expected no timeStampToken element to be present"); | ||
| 376 | } | ||
| 377 | } | ||
| 378 | |||
| 379 | final static class TimestampException extends IOException { | ||
| 380 | TimestampException(String message) { | ||
| 381 | super(message); | ||
| 382 | } | ||
| 383 | } | ||
| 384 | } | ||