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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 package org.apache.harmony.security.x509.tsp;
22 import java.math.BigInteger;
23 import org.apache.harmony.security.asn1.ASN1Boolean;
24 import org.apache.harmony.security.asn1.ASN1Implicit;
25 import org.apache.harmony.security.asn1.ASN1Integer;
26 import org.apache.harmony.security.asn1.ASN1Oid;
27 import org.apache.harmony.security.asn1.ASN1Sequence;
28 import org.apache.harmony.security.asn1.ASN1Type;
29 import org.apache.harmony.security.asn1.BerInputStream;
30 import org.apache.harmony.security.asn1.ObjectIdentifier;
31 import org.apache.harmony.security.x509.Extensions;
34 * As defined in Time-Stamp Protocol (TSP)
35 * (http://www.ietf.org/rfc/rfc3161.txt)
37 * TimeStampReq ::= SEQUENCE {
38 * version INTEGER { v1(1) },
39 * messageImprint MessageImprint,
40 * --a hash algorithm OID and the hash value of the data to be
42 * reqPolicy TSAPolicyId OPTIONAL,
43 * nonce INTEGER OPTIONAL,
44 * certReq BOOLEAN DEFAULT FALSE,
45 * extensions [0] IMPLICIT Extensions OPTIONAL
48 * TSAPolicyId ::= OBJECT IDENTIFIER
50 public class TimeStampReq {
51 private final int version;
53 private final MessageImprint messageImprint;
55 private final String reqPolicy;
57 private final BigInteger nonce;
59 private final Boolean certReq;
61 private final Extensions extensions;
63 private byte [] encoding;
65 public TimeStampReq(int version, MessageImprint messageImprint,
66 String reqPolicy, BigInteger nonce, Boolean certReq,
67 Extensions extensions) {
68 this.version = version;
69 this.messageImprint = messageImprint;
70 this.reqPolicy = reqPolicy;
72 this.certReq = certReq;
73 this.extensions = extensions;
76 private TimeStampReq(int version, MessageImprint messageImprint,
77 String reqPolicy, BigInteger nonce, Boolean certReq,
78 Extensions extensions, byte [] encoding) {
79 this (version, messageImprint, reqPolicy, nonce, certReq, extensions);
80 this.encoding = encoding;
83 public String toString() {
84 StringBuilder res = new StringBuilder();
85 res.append("-- TimeStampReq:");
86 res.append("\nversion : ");
88 res.append("\nmessageImprint: ");
89 res.append(messageImprint);
90 res.append("\nreqPolicy: ");
91 res.append(reqPolicy);
92 res.append("\nnonce: ");
94 res.append("\ncertReq: ");
96 res.append("\nextensions: ");
97 res.append(extensions);
98 res.append("\n-- TimeStampReq End\n");
99 return res.toString();
103 * Returns ASN.1 encoded form of this TimeStampReq.
104 * @return a byte array containing ASN.1 encoded form.
106 public byte [] getEncoded(){
107 if (encoding == null) {
108 encoding = ASN1.encode(this);
114 * @return Returns the certReq.
116 public Boolean getCertReq() {
121 * @return Returns the extensions.
123 public Extensions getExtensions() {
128 * @return Returns the messageImprint.
130 public MessageImprint getMessageImprint() {
131 return messageImprint;
135 * @return Returns the nonce.
137 public BigInteger getNonce() {
142 * @return Returns the reqPolicy.
144 public String getReqPolicy() {
149 * @return Returns the version.
151 public int getVersion() {
155 public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
156 ASN1Integer.getInstance(), // version
157 MessageImprint.ASN1, // messageImprint
158 ASN1Oid.getInstance(), // reqPolicy
159 ASN1Integer.getInstance(), // nonce
160 ASN1Boolean.getInstance(), // certReq
161 new ASN1Implicit(0, Extensions.ASN1)}) {// extensions
164 setDefault(Boolean.FALSE, 4);
170 protected Object getDecodedObject(BerInputStream in) {
171 Object[] values = (Object[]) in.content;
173 String objID = (values[2] == null) ? null : ObjectIdentifier
174 .toString((int[]) values[2]);
175 BigInteger nonce = (values[3] == null) ? null : new BigInteger(
178 if (values[5] == null) {
179 return new TimeStampReq(
180 ASN1Integer.toIntValue(values[0]),
181 (MessageImprint) values[1],
189 return new TimeStampReq(
190 ASN1Integer.toIntValue(values[0]),
191 (MessageImprint) values[1],
195 (Extensions) values[5],
201 protected void getValues(Object object, Object[] values) {
202 TimeStampReq req = (TimeStampReq) object;
203 values[0] = ASN1Integer.fromIntValue(req.version);
204 values[1] = req.messageImprint;
205 values[2] = (req.reqPolicy == null) ? null : ObjectIdentifier
206 .toIntArray(req.reqPolicy);
207 values[3] = (req.nonce == null) ? null : req.nonce.toByteArray();
208 values[4] = (req.certReq == null) ? Boolean.FALSE : req.certReq;
209 values[5] = req.extensions;