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