TimeStampReq.java revision f33eae7e84eb6d3b0f4e86b59605bb3de73009f3
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;
23
24import org.apache.harmony.security.asn1.ASN1Boolean;
25import org.apache.harmony.security.asn1.ASN1Implicit;
26import org.apache.harmony.security.asn1.ASN1Integer;
27import org.apache.harmony.security.asn1.ASN1Oid;
28import org.apache.harmony.security.asn1.ASN1Sequence;
29import org.apache.harmony.security.asn1.ASN1Type;
30import org.apache.harmony.security.asn1.BerInputStream;
31import org.apache.harmony.security.asn1.ObjectIdentifier;
32import org.apache.harmony.security.x509.Extensions;
33
34/**
35 * As defined in Time-Stamp Protocol (TSP)
36 * (http://www.ietf.org/rfc/rfc3161.txt)
37 *
38 * TimeStampReq ::= SEQUENCE  {
39 *    version                      INTEGER  { v1(1) },
40 *    messageImprint               MessageImprint,
41 *      --a hash algorithm OID and the hash value of the data to be
42 *      --time-stamped
43 *    reqPolicy             TSAPolicyId              OPTIONAL,
44 *    nonce                 INTEGER                  OPTIONAL,
45 *    certReq               BOOLEAN                  DEFAULT FALSE,
46 *    extensions            [0] IMPLICIT Extensions  OPTIONAL
47 *  }
48 *
49 *  TSAPolicyId ::= OBJECT IDENTIFIER
50 */
51public class TimeStampReq {
52    private final int version;
53
54    private final MessageImprint messageImprint;
55
56    private final String reqPolicy;
57
58    private final BigInteger nonce;
59
60    private final Boolean certReq;
61
62    private final Extensions extensions;
63
64    private byte [] encoding;
65
66    public TimeStampReq(int version, MessageImprint messageImprint,
67            String reqPolicy, BigInteger nonce, Boolean certReq,
68            Extensions extensions) {
69        this.version = version;
70        this.messageImprint = messageImprint;
71        this.reqPolicy = reqPolicy;
72        this.nonce = nonce;
73        this.certReq = certReq;
74        this.extensions = extensions;
75    }
76
77    private TimeStampReq(int version, MessageImprint messageImprint,
78            String reqPolicy, BigInteger nonce, Boolean certReq,
79            Extensions extensions, byte [] encoding) {
80        this (version, messageImprint, reqPolicy, nonce, certReq, extensions);
81        this.encoding = encoding;
82    }
83
84    public String toString() {
85        StringBuilder res = new StringBuilder();
86        res.append("-- TimeStampReq:");
87        res.append("\nversion : ");
88        res.append(version);
89        res.append("\nmessageImprint:  ");
90        res.append(messageImprint);
91        res.append("\nreqPolicy:  ");
92        res.append(reqPolicy);
93        res.append("\nnonce:  ");
94        res.append(nonce);
95        res.append("\ncertReq:  ");
96        res.append(certReq);
97        res.append("\nextensions:  ");
98        res.append(extensions);
99        res.append("\n-- TimeStampReq End\n");
100        return res.toString();
101    }
102
103    /**
104     * Returns ASN.1 encoded form of this TimeStampReq.
105     * @return a byte array containing ASN.1 encoded form.
106     */
107    public byte [] getEncoded(){
108        if (encoding == null) {
109            encoding = ASN1.encode(this);
110        }
111        return encoding;
112    }
113
114    /**
115     * @return Returns the certReq.
116     */
117    public Boolean getCertReq() {
118        return certReq;
119    }
120
121    /**
122     * @return Returns the extensions.
123     */
124    public Extensions getExtensions() {
125        return extensions;
126    }
127
128    /**
129     * @return Returns the messageImprint.
130     */
131    public MessageImprint getMessageImprint() {
132        return messageImprint;
133    }
134
135    /**
136     * @return Returns the nonce.
137     */
138    public BigInteger getNonce() {
139        return nonce;
140    }
141
142    /**
143     * @return Returns the reqPolicy.
144     */
145    public String getReqPolicy() {
146        return reqPolicy;
147    }
148
149    /**
150     * @return Returns the version.
151     */
152    public int getVersion() {
153        return version;
154    }
155
156    public static final ASN1Sequence ASN1 = new ASN1Sequence(new ASN1Type[] {
157            ASN1Integer.getInstance(),              // version
158            MessageImprint.ASN1,                    // messageImprint
159            ASN1Oid.getInstance(),                  // reqPolicy
160            ASN1Integer.getInstance(),              // nonce
161            ASN1Boolean.getInstance(),              // certReq
162            new ASN1Implicit(0, Extensions.ASN1)}) {// extensions
163
164        {
165            setDefault(Boolean.FALSE, 4);
166            setOptional(2);
167            setOptional(3);
168            setOptional(5);
169        }
170
171        protected Object getDecodedObject(BerInputStream in) {
172            Object[] values = (Object[]) in.content;
173
174            String objID = (values[2] == null) ? null : ObjectIdentifier
175                    .toString((int[]) values[2]);
176            BigInteger nonce = (values[3] == null) ? null : new BigInteger(
177                    (byte[]) values[3]);
178
179            if (values[5] == null) {
180                return new TimeStampReq(
181                        ASN1Integer.toIntValue(values[0]),
182                        (MessageImprint) values[1],
183                        objID,
184                        nonce,
185                        (Boolean) values[4],
186                        null,
187                        in.getEncoded()
188                   );
189            } else {
190                return new TimeStampReq(
191                        ASN1Integer.toIntValue(values[0]),
192                        (MessageImprint) values[1],
193                        objID,
194                        nonce,
195                        (Boolean) values[4],
196                        (Extensions) values[5],
197                        in.getEncoded()
198                   );
199            }
200        }
201
202        protected void getValues(Object object, Object[] values) {
203            TimeStampReq req = (TimeStampReq) object;
204            values[0] = ASN1Integer.fromIntValue(req.version);
205            values[1] = req.messageImprint;
206            values[2] = (req.reqPolicy == null) ? null : ObjectIdentifier
207                    .toIntArray(req.reqPolicy);
208            values[3] = (req.nonce == null) ? null : req.nonce.toByteArray();
209            values[4] = (req.certReq == null) ? Boolean.FALSE : req.certReq;
210            values[5] = req.extensions;
211        }
212    };
213
214}
215
216