1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20package org.apache.harmony.security.x509.tsp; 21 22import java.math.BigInteger; 23import java.util.Date; 24import org.apache.harmony.security.asn1.ASN1Boolean; 25import org.apache.harmony.security.asn1.ASN1Explicit; 26import org.apache.harmony.security.asn1.ASN1GeneralizedTime; 27import org.apache.harmony.security.asn1.ASN1Implicit; 28import org.apache.harmony.security.asn1.ASN1Integer; 29import org.apache.harmony.security.asn1.ASN1Oid; 30import org.apache.harmony.security.asn1.ASN1Sequence; 31import org.apache.harmony.security.asn1.ASN1Type; 32import org.apache.harmony.security.asn1.BerInputStream; 33import org.apache.harmony.security.asn1.ObjectIdentifier; 34import org.apache.harmony.security.x509.Extensions; 35import org.apache.harmony.security.x509.GeneralName; 36 37/** 38 * As defined in Time-Stamp Protocol (TSP) 39 * (http://www.ietf.org/rfc/rfc3161.txt) 40 * 41 * TSTInfo ::= SEQUENCE { 42 * version INTEGER { v1(1) }, 43 * policy TSAPolicyId, 44 * messageImprint MessageImprint, 45 * -- MUST have the same value as the similar field in 46 * -- TimeStampReq 47 * serialNumber INTEGER, 48 * -- Time-Stamping users MUST be ready to accommodate integers 49 * -- up to 160 bits. 50 * genTime GeneralizedTime, 51 * accuracy Accuracy OPTIONAL, 52 * ordering BOOLEAN DEFAULT FALSE, 53 * nonce INTEGER OPTIONAL, 54 * -- MUST be present if the similar field was present 55 * -- in TimeStampReq. In that case it MUST have the same value. 56 * tsa [0] GeneralName OPTIONAL, 57 * extensions [1] IMPLICIT Extensions OPTIONAL 58 * } 59 * 60 * TSAPolicyId ::= OBJECT IDENTIFIER 61 * 62 * "tsa [0] GeneralName OPTIONAL" is EXPLICIT and the word EXPLICIT is omitted. 63 */ 64public class TSTInfo { 65 66 private final int version; 67 68 private final String policy; 69 70 private final MessageImprint messageImprint; 71 72 private final BigInteger serialNumber; 73 74 private final Date genTime; 75 76 private final int [] accuracy; 77 78 private final Boolean ordering; 79 80 private final BigInteger nonce; 81 82 private final GeneralName tsa; 83 84 private final Extensions extensions; 85 86 public TSTInfo(int version, String policy, MessageImprint messageImprint, 87 BigInteger serialNumber, Date genTime, int[] accuracy, 88 Boolean ordering, BigInteger nonce, GeneralName tsa, 89 Extensions extensions) { 90 this.version = version; 91 this.policy = policy; 92 this.messageImprint = messageImprint; 93 this.serialNumber = serialNumber; 94 this.genTime = genTime; 95 this.accuracy = accuracy; 96 this.ordering = ordering; 97 this.nonce = nonce; 98 this.tsa = tsa; 99 this.extensions = extensions; 100 } 101 102 public String toString() { 103 StringBuilder res = new StringBuilder(); 104 res.append("-- TSTInfo:"); 105 res.append("\nversion: "); 106 res.append(version); 107 res.append("\npolicy: "); 108 res.append(policy); 109 res.append("\nmessageImprint: "); 110 res.append(messageImprint); 111 res.append("\nserialNumber: "); 112 res.append(serialNumber); 113 res.append("\ngenTime: "); 114 res.append(genTime); 115 res.append("\naccuracy: "); 116 if (accuracy != null) { 117 res.append(accuracy[0] + " sec, " + accuracy[1] + " millis, " 118 + accuracy[2] + " micros"); 119 } 120 res.append("\nordering: "); 121 res.append(ordering); 122 res.append("\nnonce: "); 123 res.append(nonce); 124 res.append("\ntsa: "); 125 res.append(tsa); 126 res.append("\nextensions: "); 127 res.append(extensions); 128 res.append("\n-- TSTInfo End\n"); 129 return res.toString(); 130 } 131 132 /** 133 * @return Returns the accuracy. 134 */ 135 public int[] getAccuracy() { 136 return accuracy; 137 } 138 139 /** 140 * @return Returns the extensions. 141 */ 142 public Extensions getExtensions() { 143 return extensions; 144 } 145 146 /** 147 * @return Returns the genTime. 148 */ 149 public Date getGenTime() { 150 return genTime; 151 } 152 153 /** 154 * @return Returns the messageImprint. 155 */ 156 public MessageImprint getMessageImprint() { 157 return messageImprint; 158 } 159 160 /** 161 * @return Returns the nonce. 162 */ 163 public BigInteger getNonce() { 164 return nonce; 165 } 166 167 /** 168 * @return Returns the ordering. 169 */ 170 public Boolean getOrdering() { 171 return ordering; 172 } 173 174 /** 175 * @return Returns the policy. 176 */ 177 public String getPolicy() { 178 return policy; 179 } 180 181 /** 182 * @return Returns the serialNumber. 183 */ 184 public BigInteger getSerialNumber() { 185 return serialNumber; 186 } 187 188 /** 189 * @return Returns the tsa. 190 */ 191 public GeneralName getTsa() { 192 return tsa; 193 } 194 195 /** 196 * @return Returns the version. 197 */ 198 public int getVersion() { 199 return version; 200 } 201 202 /** 203 Accuracy ::= SEQUENCE { 204 seconds INTEGER OPTIONAL, 205 millis [0] INTEGER (1..999) OPTIONAL, 206 micros [1] INTEGER (1..999) OPTIONAL } 207 */ 208 public static final ASN1Sequence ACCURACY 209 = new ASN1Sequence(new ASN1Type[] { 210 ASN1Integer.getInstance(), 211 ASN1Integer.getInstance(), 212 ASN1Integer.getInstance() 213 }) { 214 { 215 setOptional(0); 216 setOptional(1); 217 setOptional(2); 218 } 219 220 protected Object getDecodedObject(BerInputStream in) { 221 Object[] values = (Object[]) in.content; 222 223 int [] accuracy = new int [3]; 224 for (int i = 0; i < 3; i++) { 225 if (values[i] != null) { 226 accuracy[i] = ASN1Integer.toIntValue(values[i]); 227 if (i > 0 && (accuracy[i] < 0 || accuracy[i] > 999)) { 228 throw new RuntimeException("Time-stamp accuracy value is incorrect: " + accuracy[i]); 229 } 230 } 231 } 232 return accuracy; 233 } 234 235 protected void getValues(Object object, Object[] values) { 236 int [] accuracy = (int []) object; 237 for (int i = 0; i < 3; i++) { 238 if (i > 0 && (accuracy[i] < 0 || accuracy[i] > 999)) { 239 throw new RuntimeException("Time-stamp accuracy value is incorrect: " + accuracy[i]); 240 } 241 values[i] = BigInteger.valueOf(accuracy[i]).toByteArray(); 242 } 243 } 244 }; 245 246 public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] { 247 ASN1Integer.getInstance(), // version 248 ASN1Oid.getInstance(), // policy 249 MessageImprint.ASN1, // messageImprint 250 ASN1Integer.getInstance(), // serialNumber 251 ASN1GeneralizedTime.getInstance(), // genTime 252 ACCURACY, // accuracy 253 ASN1Boolean.getInstance(), // ordering 254 ASN1Integer.getInstance(), // nonce 255 new ASN1Explicit(0, GeneralName.ASN1), // tsa 256 new ASN1Implicit(1, Extensions.ASN1) }) {// extensions 257 { 258 setOptional(5); 259 setDefault(Boolean.FALSE, 6); 260 setOptional(7); 261 setOptional(8); 262 setOptional(9); 263 } 264 265 protected Object getDecodedObject(BerInputStream in) { 266 Object[] values = (Object[]) in.content; 267 268 BigInteger nonce = (values[7] == null) ? null : new BigInteger( 269 (byte[]) values[7]); 270 271 return new TSTInfo( 272 ASN1Integer.toIntValue(values[0]), 273 ObjectIdentifier.toString((int[]) values[1]), 274 (MessageImprint) values[2], 275 new BigInteger((byte[]) values[3]), 276 (Date) values[4], 277 (int []) values[5], 278 (Boolean) values[6], 279 nonce, 280 (GeneralName) values[8], 281 (Extensions) values[9]); 282 } 283 284 protected void getValues(Object object, Object[] values) { 285 TSTInfo info = (TSTInfo) object; 286 287 values[0] = ASN1Integer.fromIntValue(info.version); 288 values[1] = ObjectIdentifier.toIntArray(info.policy); 289 values[2] = info.messageImprint; 290 values[3] = info.serialNumber.toByteArray(); 291 values[4] = info.genTime; 292 values[5] = info.accuracy; 293 values[6] = info.ordering; 294 values[7] = (info.nonce == null) ? null : info.nonce.toByteArray(); 295 values[8] = info.tsa; 296 values[9] = info.extensions; 297 } 298 }; 299} 300