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:
024: import org.apache.harmony.security.asn1.ASN1Boolean;
025: import org.apache.harmony.security.asn1.ASN1Implicit;
026: import org.apache.harmony.security.asn1.ASN1Integer;
027: import org.apache.harmony.security.asn1.ASN1Oid;
028: import org.apache.harmony.security.asn1.ASN1Sequence;
029: import org.apache.harmony.security.asn1.ASN1Type;
030: import org.apache.harmony.security.asn1.BerInputStream;
031: import org.apache.harmony.security.asn1.ObjectIdentifier;
032: import org.apache.harmony.security.x509.Extensions;
033:
034: /**
035: * As defined in Time-Stamp Protocol (TSP)
036: * (http://www.ietf.org/rfc/rfc3161.txt)
037: *
038: * TimeStampReq ::= SEQUENCE {
039: * version INTEGER { v1(1) },
040: * messageImprint MessageImprint,
041: * --a hash algorithm OID and the hash value of the data to be
042: * --time-stamped
043: * reqPolicy TSAPolicyId OPTIONAL,
044: * nonce INTEGER OPTIONAL,
045: * certReq BOOLEAN DEFAULT FALSE,
046: * extensions [0] IMPLICIT Extensions OPTIONAL
047: * }
048: *
049: * TSAPolicyId ::= OBJECT IDENTIFIER
050: */
051: public class TimeStampReq {
052: private final int version;
053:
054: private final MessageImprint messageImprint;
055:
056: private final String reqPolicy;
057:
058: private final BigInteger nonce;
059:
060: private final Boolean certReq;
061:
062: private final Extensions extensions;
063:
064: private byte[] encoding;
065:
066: public TimeStampReq(int version, MessageImprint messageImprint,
067: String reqPolicy, BigInteger nonce, Boolean certReq,
068: Extensions extensions) {
069: this .version = version;
070: this .messageImprint = messageImprint;
071: this .reqPolicy = reqPolicy;
072: this .nonce = nonce;
073: this .certReq = certReq;
074: this .extensions = extensions;
075: }
076:
077: private TimeStampReq(int version, MessageImprint messageImprint,
078: String reqPolicy, BigInteger nonce, Boolean certReq,
079: Extensions extensions, byte[] encoding) {
080: this (version, messageImprint, reqPolicy, nonce, certReq,
081: extensions);
082: this .encoding = encoding;
083: }
084:
085: public String toString() {
086: StringBuffer res = new StringBuffer();
087: res.append("-- TimeStampReq:");
088: res.append("\nversion : ");
089: res.append(version);
090: res.append("\nmessageImprint: ");
091: res.append(messageImprint);
092: res.append("\nreqPolicy: ");
093: res.append(reqPolicy);
094: res.append("\nnonce: ");
095: res.append(nonce);
096: res.append("\ncertReq: ");
097: res.append(certReq);
098: res.append("\nextensions: ");
099: res.append(extensions);
100: res.append("\n-- TimeStampReq End\n");
101: return res.toString();
102: }
103:
104: /**
105: * Returns ASN.1 encoded form of this TimeStampReq.
106: * @return a byte array containing ASN.1 encoded form.
107: */
108: public byte[] getEncoded() {
109: if (encoding == null) {
110: encoding = ASN1.encode(this );
111: }
112: return encoding;
113: }
114:
115: /**
116: * @return Returns the certReq.
117: */
118: public Boolean getCertReq() {
119: return certReq;
120: }
121:
122: /**
123: * @return Returns the extensions.
124: */
125: public Extensions getExtensions() {
126: return extensions;
127: }
128:
129: /**
130: * @return Returns the messageImprint.
131: */
132: public MessageImprint getMessageImprint() {
133: return messageImprint;
134: }
135:
136: /**
137: * @return Returns the nonce.
138: */
139: public BigInteger getNonce() {
140: return nonce;
141: }
142:
143: /**
144: * @return Returns the reqPolicy.
145: */
146: public String getReqPolicy() {
147: return reqPolicy;
148: }
149:
150: /**
151: * @return Returns the version.
152: */
153: public int getVersion() {
154: return version;
155: }
156:
157: public static final ASN1Sequence ASN1 = new ASN1Sequence(
158: new ASN1Type[] { ASN1Integer.getInstance(), // version
159: MessageImprint.ASN1, // messageImprint
160: ASN1Oid.getInstance(), // reqPolicy
161: ASN1Integer.getInstance(), // nonce
162: ASN1Boolean.getInstance(), // certReq
163: new ASN1Implicit(0, Extensions.ASN1) }) {// extensions
164:
165: {
166: setDefault(Boolean.FALSE, 4);
167: setOptional(2);
168: setOptional(3);
169: setOptional(5);
170: }
171:
172: protected Object getDecodedObject(BerInputStream in) {
173: Object[] values = (Object[]) in.content;
174:
175: String objID = (values[2] == null) ? null
176: : ObjectIdentifier.toString((int[]) values[2]);
177: BigInteger nonce = (values[3] == null) ? null
178: : new BigInteger((byte[]) values[3]);
179:
180: if (values[5] == null) {
181: return new TimeStampReq(ASN1Integer
182: .toIntValue(values[0]),
183: (MessageImprint) values[1], objID, nonce,
184: (Boolean) values[4], null, in.getEncoded());
185: } else {
186: return new TimeStampReq(ASN1Integer
187: .toIntValue(values[0]),
188: (MessageImprint) values[1], objID, nonce,
189: (Boolean) values[4], (Extensions) values[5], in
190: .getEncoded());
191: }
192: }
193:
194: protected void getValues(Object object, Object[] values) {
195: TimeStampReq req = (TimeStampReq) object;
196: values[0] = ASN1Integer.fromIntValue(req.version);
197: values[1] = req.messageImprint;
198: values[2] = (req.reqPolicy == null) ? null
199: : ObjectIdentifier.toIntArray(req.reqPolicy);
200: values[3] = (req.nonce == null) ? null : req.nonce
201: .toByteArray();
202: values[4] = (req.certReq == null) ? Boolean.FALSE
203: : req.certReq;
204: values[5] = req.extensions;
205: }
206: };
207:
208: }
|