001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.harmony.security.x509.tsp;
021:
022: import java.math.BigInteger;
023: import java.util.Date;
024:
025: import org.apache.harmony.security.asn1.ASN1Boolean;
026: import org.apache.harmony.security.asn1.ASN1Explicit;
027: import org.apache.harmony.security.asn1.ASN1GeneralizedTime;
028: import org.apache.harmony.security.asn1.ASN1Implicit;
029: import org.apache.harmony.security.asn1.ASN1Integer;
030: import org.apache.harmony.security.asn1.ASN1Oid;
031: import org.apache.harmony.security.asn1.ASN1Sequence;
032: import org.apache.harmony.security.asn1.ASN1Type;
033: import org.apache.harmony.security.asn1.BerInputStream;
034: import org.apache.harmony.security.asn1.ObjectIdentifier;
035: import org.apache.harmony.security.internal.nls.Messages;
036: import org.apache.harmony.security.x509.Extensions;
037: import org.apache.harmony.security.x509.GeneralName;
038:
039: /**
040: * As defined in Time-Stamp Protocol (TSP)
041: * (http://www.ietf.org/rfc/rfc3161.txt)
042: *
043: * TSTInfo ::= SEQUENCE {
044: * version INTEGER { v1(1) },
045: * policy TSAPolicyId,
046: * messageImprint MessageImprint,
047: * -- MUST have the same value as the similar field in
048: * -- TimeStampReq
049: * serialNumber INTEGER,
050: * -- Time-Stamping users MUST be ready to accommodate integers
051: * -- up to 160 bits.
052: * genTime GeneralizedTime,
053: * accuracy Accuracy OPTIONAL,
054: * ordering BOOLEAN DEFAULT FALSE,
055: * nonce INTEGER OPTIONAL,
056: * -- MUST be present if the similar field was present
057: * -- in TimeStampReq. In that case it MUST have the same value.
058: * tsa [0] GeneralName OPTIONAL,
059: * extensions [1] IMPLICIT Extensions OPTIONAL
060: * }
061: *
062: * TSAPolicyId ::= OBJECT IDENTIFIER
063: *
064: * "tsa [0] GeneralName OPTIONAL" is EXPLICIT and the word EXPLICIT is omitted.
065: */
066: public class TSTInfo {
067:
068: private final int version;
069:
070: private final String policy;
071:
072: private final MessageImprint messageImprint;
073:
074: private final BigInteger serialNumber;
075:
076: private final Date genTime;
077:
078: private final int[] accuracy;
079:
080: private final Boolean ordering;
081:
082: private final BigInteger nonce;
083:
084: private final GeneralName tsa;
085:
086: private final Extensions extensions;
087:
088: public TSTInfo(int version, String policy,
089: MessageImprint messageImprint, BigInteger serialNumber,
090: Date genTime, int[] accuracy, Boolean ordering,
091: BigInteger nonce, GeneralName tsa, Extensions extensions) {
092: this .version = version;
093: this .policy = policy;
094: this .messageImprint = messageImprint;
095: this .serialNumber = serialNumber;
096: this .genTime = genTime;
097: this .accuracy = accuracy;
098: this .ordering = ordering;
099: this .nonce = nonce;
100: this .tsa = tsa;
101: this .extensions = extensions;
102: }
103:
104: public String toString() {
105: StringBuffer res = new StringBuffer();
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]
120: + " millis, " + 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 = new ASN1Sequence(
211: new ASN1Type[] { ASN1Integer.getInstance(),
212: ASN1Integer.getInstance(),
213: ASN1Integer.getInstance() }) {
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(
229: // Msg: "Time-stamp accuracy value is incorrect: {}"
230: Messages.getString("security.1A3",
231: accuracy[i]));
232: }
233: }
234: }
235: return accuracy;
236: }
237:
238: protected void getValues(Object object, Object[] values) {
239: int[] accuracy = (int[]) object;
240: for (int i = 0; i < 3; i++) {
241: if (i > 0 && (accuracy[i] < 0 || accuracy[i] > 999)) {
242: throw new RuntimeException(
243: // Msg: "Time-stamp accuracy value is incorrect: {0}"
244: Messages.getString("security.1A3",
245: accuracy[i]));
246: }
247: values[i] = BigInteger.valueOf(accuracy[i])
248: .toByteArray();
249: }
250: }
251: };
252:
253: public static final ASN1Sequence ASN1 = new ASN1Sequence(
254: new ASN1Type[] { ASN1Integer.getInstance(), // version
255: ASN1Oid.getInstance(), // policy
256: MessageImprint.ASN1, // messageImprint
257: ASN1Integer.getInstance(), // serialNumber
258: ASN1GeneralizedTime.getInstance(), // genTime
259: ACCURACY, // accuracy
260: ASN1Boolean.getInstance(), // ordering
261: ASN1Integer.getInstance(), // nonce
262: new ASN1Explicit(0, GeneralName.ASN1), // tsa
263: new ASN1Implicit(1, Extensions.ASN1) }) {// extensions
264: {
265: setOptional(5);
266: setDefault(Boolean.FALSE, 6);
267: setOptional(7);
268: setOptional(8);
269: setOptional(9);
270: }
271:
272: protected Object getDecodedObject(BerInputStream in) {
273: Object[] values = (Object[]) in.content;
274:
275: BigInteger nonce = (values[7] == null) ? null
276: : new BigInteger((byte[]) values[7]);
277:
278: return new TSTInfo(ASN1Integer.toIntValue(values[0]),
279: ObjectIdentifier.toString((int[]) values[1]),
280: (MessageImprint) values[2], new BigInteger(
281: (byte[]) values[3]), (Date) values[4],
282: (int[]) values[5], (Boolean) values[6], nonce,
283: (GeneralName) values[8], (Extensions) values[9]);
284: }
285:
286: protected void getValues(Object object, Object[] values) {
287: TSTInfo info = (TSTInfo) object;
288:
289: values[0] = ASN1Integer.fromIntValue(info.version);
290: values[1] = ObjectIdentifier.toIntArray(info.policy);
291: values[2] = info.messageImprint;
292: values[3] = info.serialNumber.toByteArray();
293: values[4] = info.genTime;
294: values[5] = info.accuracy;
295: values[6] = info.ordering;
296: values[7] = (info.nonce == null) ? null : info.nonce
297: .toByteArray();
298: values[8] = info.tsa;
299: values[9] = info.extensions;
300: }
301: };
302: }
|